return ret;
}
-
-/*
- * Language specific, but style independent code generators functions.
- */
-
-string CCodeGen::PTR_CONST()
-{
- return "const ";
-}
-
-std::ostream &CCodeGen::OPEN_ARRAY( string type, string name )
-{
- out << "static const " << type << " " << name << "[] = {\n";
- return out;
-}
-
-std::ostream &CCodeGen::START_ARRAY_LINE()
-{
- out << "\t";
- return out;
-}
-
-std::ostream &CCodeGen::ARRAY_ITEM( int item, int count, bool last )
-{
- out << item;
- if ( !last )
- {
- out << ", ";
- if ( count % IALL == 0 )
- {
- END_ARRAY_LINE();
- START_ARRAY_LINE();
- }
- }
- return out;
-}
-
-std::ostream &CCodeGen::END_ARRAY_LINE()
-{
- out << "\n";
- return out;
-}
-
-std::ostream &CCodeGen::CLOSE_ARRAY()
-{
- return out << "};\n";
-}
-
-std::ostream &CCodeGen::STATIC_VAR( string type, string name )
-{
- out << "static const " << type << " " << name;
- return out;
-}
-
-string CCodeGen::UINT( )
-{
- return "unsigned int";
-}
-
-string CCodeGen::ARR_OFF( string ptr, string offset )
-{
- return ptr + " + " + offset;
-}
-
-string CCodeGen::CAST( string type )
-{
- return "(" + type + ")";
-}
-
-string CCodeGen::NULL_ITEM()
-{
- return "0";
-}
-
-string CCodeGen::POINTER()
-{
- return " *";
-}
-
-std::ostream &CCodeGen::SWITCH_DEFAULT()
-{
- return out;
-}
-
-string CCodeGen::CTRL_FLOW()
-{
- return "";
-}
-
-/*
- * D Specific
- */
-
-string DCodeGen::NULL_ITEM()
-{
- return "null";
-}
-
-string DCodeGen::POINTER()
-{
- // multiple items seperated by commas can also be pointer types.
- return "* ";
-}
-
-string DCodeGen::PTR_CONST()
-{
- return "";
-}
-
-std::ostream &DCodeGen::OPEN_ARRAY( string type, string name )
-{
- out << "static const " << type << "[] " << name << " = [\n";
- return out;
-}
-
-std::ostream &DCodeGen::START_ARRAY_LINE()
-{
- out << "\t";
- return out;
-}
-
-std::ostream &DCodeGen::ARRAY_ITEM( int item, int count, bool last )
-{
- out << item;
- if ( !last )
- {
- out << ", ";
- if ( count % IALL == 0 )
- {
- END_ARRAY_LINE();
- START_ARRAY_LINE();
- }
- }
- return out;
-}
-
-std::ostream &DCodeGen::END_ARRAY_LINE()
-{
- out << "\n";
- return out;
-}
-
-std::ostream &DCodeGen::CLOSE_ARRAY()
-{
- return out << "];\n";
-}
-
-std::ostream &DCodeGen::STATIC_VAR( string type, string name )
-{
- out << "static const " << type << " " << name;
- return out;
-}
-
-string DCodeGen::ARR_OFF( string ptr, string offset )
-{
- return "&" + ptr + "[" + offset + "]";
-}
-
-string DCodeGen::CAST( string type )
-{
- return "cast(" + type + ")";
-}
-
-string DCodeGen::UINT( )
-{
- return "uint";
-}
-
-std::ostream &DCodeGen::SWITCH_DEFAULT()
-{
- out << " default: break;\n";
- return out;
-}
-
-string DCodeGen::CTRL_FLOW()
-{
- return "if (true) ";
-}
-
-
/*
* Java Specific
*/
virtual void calcIndexSize() {}
};
-class CCodeGen : virtual public FsmCodeGen
-{
-public:
- CCodeGen( ostream &out ) : FsmCodeGen(out) {}
-
- virtual string NULL_ITEM();
- virtual string POINTER();
- virtual ostream &SWITCH_DEFAULT();
- virtual ostream &OPEN_ARRAY( string type, string name );
- virtual ostream &START_ARRAY_LINE();
- virtual ostream &ARRAY_ITEM( int item, int count, bool last );
- virtual ostream &END_ARRAY_LINE();
- virtual ostream &CLOSE_ARRAY();
- virtual ostream &STATIC_VAR( string type, string name );
- virtual string ARR_OFF( string ptr, string offset );
- virtual string CAST( string type );
- virtual string UINT();
- virtual string PTR_CONST();
- virtual string CTRL_FLOW();
-};
-
-class DCodeGen : virtual public FsmCodeGen
-{
-public:
- DCodeGen( ostream &out ) : FsmCodeGen(out) {}
-
- virtual string NULL_ITEM();
- virtual string POINTER();
- virtual ostream &SWITCH_DEFAULT();
- virtual ostream &OPEN_ARRAY( string type, string name );
- virtual ostream &CLOSE_ARRAY();
- virtual ostream &START_ARRAY_LINE();
- virtual ostream &ARRAY_ITEM( int item, int count, bool last );
- virtual ostream &END_ARRAY_LINE();
- virtual ostream &STATIC_VAR( string type, string name );
- virtual string ARR_OFF( string ptr, string offset );
- virtual string CAST( string type );
- virtual string UINT();
- virtual string PTR_CONST();
- virtual string CTRL_FLOW();
-};
-
class JavaCodeGen : virtual public FsmCodeGen
{
private:
return out;
}
-void TabCodeGen::LOCATE_TRANS()
-{
- out <<
- " _keys = " << ARR_OFF( K(), KO() + "[" + CS() + "]" ) << ";\n"
- " _trans = " << IO() << "[" << CS() << "];\n"
- "\n"
- " _klen = " << SL() << "[" << CS() << "];\n"
- " if ( _klen > 0 ) {\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + _klen - 1;\n"
- " while (1) {\n"
- " if ( _upper < _lower )\n"
- " break;\n"
- "\n"
- " _mid = _lower + ((_upper-_lower) >> 1);\n"
- " if ( " << GET_WIDE_KEY() << " < *_mid )\n"
- " _upper = _mid - 1;\n"
- " else if ( " << GET_WIDE_KEY() << " > *_mid )\n"
- " _lower = _mid + 1;\n"
- " else {\n"
- " _trans += (_mid - _keys);\n"
- " goto _match;\n"
- " }\n"
- " }\n"
- " _keys += _klen;\n"
- " _trans += _klen;\n"
- " }\n"
- "\n"
- " _klen = " << RL() << "[" << CS() << "];\n"
- " if ( _klen > 0 ) {\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
- " while (1) {\n"
- " if ( _upper < _lower )\n"
- " break;\n"
- "\n"
- " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
- " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
- " _upper = _mid - 2;\n"
- " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
- " _lower = _mid + 2;\n"
- " else {\n"
- " _trans += ((_mid - _keys)>>1);\n"
- " goto _match;\n"
- " }\n"
- " }\n"
- " _trans += _klen;\n"
- " }\n"
- "\n";
-}
-
-void TabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
-{
- ret << "{" << CS() << " = " << gotoDest << "; " <<
- CTRL_FLOW() << "goto _again;}";
-}
-
-void TabCodeGen::GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
-{
- ret << "{" << CS() << " = (";
- INLINE_LIST( ret, ilItem->children, 0, inFinish );
- ret << "); " << CTRL_FLOW() << "goto _again;}";
-}
void TabCodeGen::CURS( ostream &ret, bool inFinish )
{
ret << ");";
}
-void TabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
-{
- ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " <<
- callDest << "; " << CTRL_FLOW() << "goto _again;}";
-}
-
-void TabCodeGen::CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish )
-{
- ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
- INLINE_LIST( ret, ilItem->children, targState, inFinish );
- ret << "); " << CTRL_FLOW() << "goto _again;}";
-}
-
-void TabCodeGen::RET( ostream &ret, bool inFinish )
-{
- ret << "{" << CS() << " = " << STACK() << "[--" <<
- TOP() << "]; " << CTRL_FLOW() << "goto _again;}";
-}
-
-void TabCodeGen::BREAK( ostream &ret, int targState )
-{
- outLabelUsed = true;
- ret << CTRL_FLOW() << "goto _out;";
-}
-
void TabCodeGen::writeOutData()
{
/* If there are any transtion functions then output the array. If there
}
}
-void TabCodeGen::COND_TRANSLATE()
-{
- out <<
- " _widec = " << GET_KEY() << ";\n"
- " _klen = " << CL() << "[" << CS() << "];\n"
- " _keys = " << ARR_OFF( CK(), "(" + CO() + "[" + CS() + "]*2)" ) << ";\n"
- " if ( _klen > 0 ) {\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_lower = _keys;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_mid;\n"
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_upper = _keys + (_klen<<1) - 2;\n"
- " while (1) {\n"
- " if ( _upper < _lower )\n"
- " break;\n"
- "\n"
- " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
- " if ( " << GET_WIDE_KEY() << " < _mid[0] )\n"
- " _upper = _mid - 2;\n"
- " else if ( " << GET_WIDE_KEY() << " > _mid[1] )\n"
- " _lower = _mid + 2;\n"
- " else {\n"
- " switch ( " << C() << "[" << CO() << "[" << CS() << "]"
- " + ((_mid - _keys)>>1)] ) {\n";
-
- for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
- CondSpace *condSpace = csi;
- out << " case " << condSpace->condSpaceId << ": {\n";
- out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
- KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
- " - " << KEY(keyOps->minKey) << "));\n";
-
- for ( CondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
- out << TABS(2) << "if ( ";
- CONDITION( out, *csi );
- Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
- out << " ) _widec += " << condValOffset << ";\n";
- }
-
- out <<
- " break;\n"
- " }\n";
- }
-
- SWITCH_DEFAULT();
-
- out <<
- " }\n"
- " break;\n"
- " }\n"
- " }\n"
- " }\n"
- "\n";
-}
-
-void TabCodeGen::writeOutExec()
-{
- outLabelUsed = false;
-
- out <<
- " {\n"
- " int _klen";
-
- if ( redFsm->anyRegCurStateRef() )
- out << ", _ps";
-
- out <<
- ";\n"
- " " << UINT() << " _trans;\n";
-
- if ( redFsm->anyConditions() )
- out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
-
- if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
- || redFsm->anyFromStateActions() )
- {
- out <<
- " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << POINTER() << "_acts;\n"
- " " << UINT() << " _nacts;\n";
- }
-
- out <<
- " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
- "\n";
-
- if ( hasEnd ) {
- outLabelUsed = true;
- out <<
- " if ( " << P() << " == " << PE() << " )\n"
- " goto _out;\n";
- }
-
- out << "_resume:\n";
-
- if ( redFsm->errState != 0 ) {
- outLabelUsed = true;
- out <<
- " if ( " << CS() << " == " << redFsm->errState->id << " )\n"
- " goto _out;\n";
- }
-
- if ( redFsm->anyFromStateActions() ) {
- out <<
- " _acts = " << ARR_OFF( A(), FSA() + "[" + CS() + "]" ) << ";\n"
- " _nacts = " << CAST(UINT()) << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- FROM_STATE_ACTION_SWITCH();
- SWITCH_DEFAULT() <<
- " }\n"
- " }\n"
- "\n";
- }
-
- if ( redFsm->anyConditions() )
- COND_TRANSLATE();
-
- LOCATE_TRANS();
-
- out << "_match:\n";
-
- if ( redFsm->anyRegCurStateRef() )
- out << " _ps = " << CS() << ";\n";
-
- if ( useIndicies )
- out << " _trans = " << I() << "[_trans];\n";
-
- out <<
- " " << CS() << " = " << TT() << "[_trans];\n"
- "\n";
-
- if ( redFsm->anyRegActions() ) {
- out <<
- " if ( " << TA() << "[_trans] == 0 )\n"
- " goto _again;\n"
- "\n"
- " _acts = " << ARR_OFF( A(), TA() + "[_trans]" ) << ";\n"
- " _nacts = " << CAST(UINT()) << " *_acts++;\n"
- " while ( _nacts-- > 0 )\n {\n"
- " switch ( *_acts++ )\n {\n";
- ACTION_SWITCH();
- SWITCH_DEFAULT() <<
- " }\n"
- " }\n"
- "\n";
- }
-
- if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
- redFsm->anyActionCalls() || redFsm->anyActionRets() )
- out << "_again:\n";
-
- if ( redFsm->anyToStateActions() ) {
- out <<
- " _acts = " << ARR_OFF( A(), TSA() + "[" + CS() + "]" ) << ";\n"
- " _nacts = " << CAST(UINT()) << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- TO_STATE_ACTION_SWITCH();
- SWITCH_DEFAULT() <<
- " }\n"
- " }\n"
- "\n";
- }
-
- if ( hasEnd ) {
- out <<
- " if ( ++" << P() << " != " << PE() << " )\n"
- " goto _resume;\n";
- }
- else {
- out <<
- " " << P() << " += 1;\n"
- " goto _resume;\n";
- }
-
- if ( outLabelUsed )
- out << " _out: {}\n";
-
- out << " }\n";
-}
-
-
-void TabCodeGen::writeOutEOF()
-{
- if ( redFsm->anyEofActions() ) {
- out <<
- " {\n"
- " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << POINTER() << "_acts = " <<
- ARR_OFF( A(), EA() + "[" + CS() + "]" ) << ";\n"
- " " << UINT() << " _nacts = " << CAST(UINT()) << " *_acts++;\n"
- " while ( _nacts-- > 0 ) {\n"
- " switch ( *_acts++ ) {\n";
- EOF_ACTION_SWITCH();
- SWITCH_DEFAULT() <<
- " }\n"
- " }\n"
- " }\n"
- "\n";
- }
-}
TabCodeGen( ostream &out ) : FsmCodeGen(out) {}
virtual ~TabCodeGen() { }
virtual void writeOutData();
- virtual void writeOutExec();
protected:
std::ostream &TO_STATE_ACTION_SWITCH();
std::ostream &TRANS_ACTIONS();
std::ostream &TRANS_TARGS_WI();
std::ostream &TRANS_ACTIONS_WI();
- void LOCATE_TRANS();
- void COND_TRANSLATE();
-
- void GOTO( ostream &ret, int gotoDest, bool inFinish );
- void CALL( ostream &ret, int callDest, int targState, bool inFinish );
void NEXT( ostream &ret, int nextDest, bool inFinish );
- void GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish );
void NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish );
- void CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish );
void CURS( ostream &ret, bool inFinish );
void TARGS( ostream &ret, bool inFinish, int targState );
- void RET( ostream &ret, bool inFinish );
- void BREAK( ostream &ret, int targState );
virtual int TO_STATE_ACTION( RedStateAp *state );
virtual int FROM_STATE_ACTION( RedStateAp *state );
virtual int EOF_ACTION( RedStateAp *state );
virtual int TRANS_ACTION( RedTransAp *trans );
virtual void calcIndexSize();
- virtual void writeOutEOF();
-};
-
-
-/*
- * CTabCodeGen
- */
-struct CTabCodeGen
- : public TabCodeGen, public CCodeGen
-{
- CTabCodeGen( ostream &out ) :
- FsmCodeGen(out), TabCodeGen(out), CCodeGen(out) {}
-};
-
-/*
- * DTabCodeGen
- */
-struct DTabCodeGen
- : public TabCodeGen, public DCodeGen
-{
- DTabCodeGen( ostream &out ) :
- FsmCodeGen(out), TabCodeGen(out), DCodeGen(out) {}
};