/* * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer; * redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution; * neither the name of the copyright holders nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * $Id$ * */ #include "mem/slicc/generator/mif_gen.hh" #include "mem/slicc/symbols/State.hh" #include "mem/slicc/symbols/Event.hh" #include "mem/slicc/symbols/Action.hh" #include "mem/slicc/symbols/Transition.hh" // -- Helper functions string formatShorthand(const string shorthand); string formatCellRuling(const string shorthand); void printStateTableMIF(const StateMachine& sm, ostream& out) { const string mif_prolog1 = " # Generated by Multifacet MIF Mungers Inc\n\ \n\ \n\ \n\ \n\ # \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ > # end of TblFormat\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ > # end of Pgf\n\ \n\ \n\ \n\ \n\ > # end of Marker\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ > # end of Row\n\ > # end of TblH\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ > # end of TblFormat\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ > # end of Pgf\n\ \n\ \n\ \n\ \n\ > # end of Marker\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ > # end of Row\n\ > # end of TblH\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ > # end of TblFormat\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ > # end of Pgf\n\ \n\ \n\ \n\ \n\ > # end of Marker\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ > # end of Row\n\ > # end of TblH\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ "; const string tbl_fmt_before_num_cols = " > # end of TblFormat\n\ \n\ \n\ "; const string tbl_before_each_header = " \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ "; const string row_cell_before_ruling = " max_col_width) ? max_col_width : column_width)); out << tbl_fmt_before_num_cols; out << num_cols; for( i = 0; i < num_cols; i++ ) { out << tbl_fmt_each_col_width_begin << column_width << tbl_fmt_each_col_width_end; } // -- Column headers out << tbl_before_first_header1; out << formatShorthand( sm.getShorthand() ); out << " transitions"; out << tbl_before_first_header2; out << tbl_before_each_header; out << "State"; out << tbl_after_each_header; for( i = 0; i < sm.numEvents(); i++ ) { out << tbl_before_each_rot_header; out << formatShorthand( sm.getEvent(i).getShorthand() ); out << tbl_after_each_header; } out << before_first_row; // -- Body of table for( i = 0; i < num_rows; i++ ) { // -- Each row out << row_before_first_cell; // -- Figure out ruling if (sm.getState(i).existPair("format")) { row_ruling = formatCellRuling( sm.getState(i).lookupPair("format")); } else { row_ruling = ""; } // -- First column = state out << row_cell_before_ruling; out << row_ruling; out << row_cell_before_contents; out << formatShorthand( sm.getState(i).getShorthand() ); out << row_cell_after_contents; // -- One column for each event for( j = 0; j < sm.numEvents(); j++ ) { const Transition* trans_ptr = sm.getTransPtr( i, j ); // -- Figure out ruling if (sm.getEvent(j).existPair("format")) { col_ruling = formatCellRuling(sm.getEvent(j).lookupPair("format")); } else { col_ruling = ""; } out << row_cell_before_ruling; out << row_ruling; out << col_ruling; if( trans_ptr != NULL ) { string actions; string nextState; // -- Get the actions actions = formatShorthand( trans_ptr->getActionShorthands() ); // -- Get the next state // FIXME: should compare index, not the string if (trans_ptr->getNextStateShorthand() != sm.getState(i).getShorthand() ) { nextState = formatShorthand( trans_ptr->getNextStateShorthand() ); } else { nextState = ""; } // -- Print out "actions/next-state" out << row_cell_before_contents; out << actions; if ((nextState.length() != 0) && (actions.length() != 0)) { out << "/"; } out << nextState; out << row_cell_after_contents; } else { out << row_empty_cell; } } out << row_after_last_cell; } // -- Epilog out << mif_epilog; } /* void printTBETableMIF(const StateMachine& sm, const Vector& fields, ostream& out) { const string mif_prolog1 = " # Generated by Multifacet MIF Mungers Inc\n\ \n\ \n\ \n\ \n\ # # \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ \n\ \n\ \n\ > # end of TblColumnH\n\ \n\ > # end of TblColumnBody\n\ \n\ > # end of TblColumnF\n\ > # end of TblColumn\n\ > # end of TblFormat\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ > # end of Pgf\n\ \n\ \n\ \n\ \n\ > # end of Marker\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ \n\ > # end of ParaLine\n\ > # end of Para\n\ > # end of CellContent\n\ > # end of Cell\n\ > # end of Row\n\ > # end of TblH\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ > # end of PgfFont\n\ >\n\ bottom of this row is ruled // -- - `r' => right of this column is ruled // -- string formatCellRuling( const string shorthand) { for( unsigned int i = 0; i < shorthand.length(); i++ ) { if( shorthand[i] == '!' ) { // -- OK, found beginning of ruling information for( unsigned int j = i+1; j < shorthand.length(); j++ ) { if( shorthand[j] == 'b') { // -- Rule the bottom return "\n"; } else if( shorthand[j] == 'r') { // -- Rule the bottom return "\n"; } } // -- No ruling directives recognized, return default ruling return ""; } } // -- No ruling information found, return default ruling return ""; }