diff options
author | Adrian Thurston <thurston@colm.net> | 2019-04-27 13:11:05 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2019-04-27 13:11:05 -0400 |
commit | 90db6870cc60667ffd2e4cdd55ae8c8f40321219 (patch) | |
tree | ccb51f77b2e398ee20aa0d214c2079a3b455f824 | |
parent | a215ab09b140b9b6ff5038bb93855fd41305800f (diff) | |
download | ragel-90db6870cc60667ffd2e4cdd55ae8c8f40321219.tar.gz |
use of label vars in goto codegens
-rw-r--r-- | src/codegen.cc | 9 | ||||
-rw-r--r-- | src/codegen.h | 5 | ||||
-rw-r--r-- | src/goto.cc | 48 | ||||
-rw-r--r-- | src/goto.h | 13 | ||||
-rw-r--r-- | src/gotoexp.cc | 5 | ||||
-rw-r--r-- | src/gotoloop.cc | 3 | ||||
-rw-r--r-- | src/ipgoto.cc | 70 |
7 files changed, 70 insertions, 83 deletions
diff --git a/src/codegen.cc b/src/codegen.cc index 515596f5..479563d7 100644 --- a/src/codegen.cc +++ b/src/codegen.cc @@ -886,15 +886,6 @@ string CodeGen::LDIR_PATH( char *path ) return ret.str(); } -void CodeGen::EOF_CHECK( ostream &ret ) -{ - ret << - " if ( " << P() << " == " << PE() << " )\n" - " goto _test_eof;\n"; - - testEofUsed = true; -} - void CodeGen::ACTION( ostream &ret, GenAction *action, IlOpts opts ) { ret << '\t'; diff --git a/src/codegen.h b/src/codegen.h index 6ed7ffb0..c0b49cd2 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -233,8 +233,6 @@ protected: void INLINE_BLOCK( ostream &ret, GenInlineExpr *inlineExpr ); void INLINE_PLAIN( ostream &ret, GenInlineExpr *inlineExpr ); - void EOF_CHECK( ostream &ret ); - void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish, bool csForced ); virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0; @@ -277,8 +275,6 @@ protected: string STR( int v ); - bool outLabelUsed; - bool testEofUsed; bool againLabelUsed; bool matchCondLabelUsed; @@ -457,6 +453,7 @@ protected: virtual void LOCATE_COND() {} virtual void EOF_TRANS() {} + virtual void COND_EXEC( std::string expr ) {} virtual void COND_BIN_SEARCH( Variable &var, TableArray &keys, std::string ok, std::string error ) {} diff --git a/src/goto.cc b/src/goto.cc index e40ea761..0c0e3ca6 100644 --- a/src/goto.cc +++ b/src/goto.cc @@ -351,10 +351,9 @@ void Goto::COND_B_SEARCH( RedTransAp *trans, CondKey lower, void Goto::STATE_GOTO_ERROR() { /* Label the state and bail immediately. */ - outLabelUsed = true; RedStateAp *state = redFsm->errState; out << "case " << state->id << ":\n"; - out << " goto _again;\n"; + out << " goto " << _again << ";\n"; } std::ostream &Goto::STATE_GOTOS() @@ -403,7 +402,7 @@ std::ostream &Goto::TRANSITION( RedCondPair *pair ) } else { /* No code to execute, just loop around. */ - out << "goto _again;\n"; + out << "goto " << _again << ";\n"; } return out; } @@ -592,6 +591,12 @@ void Goto::taNfaPopTrans() nfaPopTrans.finish(); } +void Goto::EOF_CHECK( ostream &ret ) +{ + ret << + " if ( " << P() << " == " << PE() << " )\n" + " goto " << _test_eof << ";\n"; +} void Goto::GOTO( ostream &ret, int gotoDest, bool inFinish ) { @@ -600,7 +605,7 @@ void Goto::GOTO( ostream &ret, int gotoDest, bool inFinish ) if ( inFinish && !noEnd ) EOF_CHECK( ret ); - ret << "goto _again;"; + ret << "goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -614,7 +619,7 @@ void Goto::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) if ( inFinish && !noEnd ) EOF_CHECK( ret ); - ret << " goto _again;"; + ret << " goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -658,7 +663,7 @@ void Goto::CALL( ostream &ret, int callDest, int targState, bool inFinish ) if ( inFinish && !noEnd ) EOF_CHECK( ret ); - ret << " goto _again;"; + ret << " goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -696,7 +701,7 @@ void Goto::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool i if ( inFinish && !noEnd ) EOF_CHECK( ret ); - ret << " goto _again;"; + ret << " goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -730,7 +735,7 @@ void Goto::RET( ostream &ret, bool inFinish ) if ( inFinish && !noEnd ) EOF_CHECK( ret ); - ret << "goto _again;" << CLOSE_GEN_BLOCK(); + ret << "goto " << _again << ";" << CLOSE_GEN_BLOCK(); } void Goto::NRET( ostream &ret, bool inFinish ) @@ -748,13 +753,11 @@ void Goto::NRET( ostream &ret, bool inFinish ) void Goto::BREAK( ostream &ret, int targState, bool csForced ) { - outLabelUsed = true; - ret << OPEN_GEN_BLOCK() << P() << " += 1; " << "goto _out; " << CLOSE_GEN_BLOCK(); + ret << OPEN_GEN_BLOCK() << P() << " += 1; " << "goto " << _out << "; " << CLOSE_GEN_BLOCK(); } void Goto::NBREAK( ostream &ret, int targState, bool csForced ) { - outLabelUsed = true; ret << OPEN_GEN_BLOCK() << P() << " += 1; " << nbreak << " = 1; " << CLOSE_GEN_BLOCK(); } @@ -829,9 +832,6 @@ void Goto::writeData() void Goto::writeExec() { - testEofUsed = false; - outLabelUsed = false; - int maxCtrId = redFsm->nextCondId > redFsm->nextTransId ? redFsm->nextCondId : redFsm->nextTransId; ctrLabel = allocateLabels( ctrLabel, IpLabel::Ctr, maxCtrId ); @@ -857,7 +857,7 @@ void Goto::writeExec() out << "\n"; - out << "_resume:\n"; + out << EMIT_LABEL( _resume ); /* Do we break out on no more input. */ bool eof = redFsm->anyEofActivity() || redFsm->anyNfaStates(); @@ -865,12 +865,12 @@ void Goto::writeExec() if ( eof ) { out << " if ( " << P() << " == " << PE() << " && " << P() << " != " << vEOF() << " )\n" - " goto _out;\n"; + " goto " << _out << ";\n"; } else { out << " if ( " << P() << " == " << PE() << " )\n" - " goto _out;\n"; + " goto " << _out << ";\n"; } } @@ -898,7 +898,7 @@ void Goto::writeExec() " }\n"; out << - " goto _again;\n" + " goto " << _again << ";\n" "}\n"; } @@ -915,13 +915,13 @@ void Goto::writeExec() if ( redFsm->anyRegActions() ) EXEC_FUNCS() << "\n"; - out << "_again:\n"; + out << EMIT_LABEL( _again ); if ( !noEnd && eof ) { out << " if ( " << P() << " == " << vEOF() << " ) {\n" " if ( " << vCS() << " >= " << FIRST_FINAL_STATE() << " )\n" - " goto _out;\n" + " goto " << _out << ";\n" " }\n" " else {\n"; } @@ -935,7 +935,7 @@ void Goto::writeExec() out << " " << P() << " += 1;\n" - " goto _resume;\n"; + " goto " << _resume << ";\n"; if ( redFsm->errState != 0 ) { out << @@ -950,7 +950,7 @@ void Goto::writeExec() if ( redFsm->anyNfaStates() ) { out << " if ( nfa_len == 0 )\n" - " goto _out;\n" + " goto " << _out << ";\n" "\n" " nfa_count += 1;\n" " nfa_len -= 1;\n" @@ -976,10 +976,10 @@ void Goto::writeExec() NFA_POST_POP(); - out << "goto _resume;\n"; + out << "goto " << _resume << ";\n"; } - out << "_out: {}\n"; + out << EMIT_LABEL( _out ); out << "}\n"; } @@ -103,6 +103,11 @@ public: ck( "_ck" ), nbreak( "_nbreak" ), ps( "_ps" ), + _out("_out"), + _pop("_pop"), + _again("_again"), + _resume("_resume"), + _test_eof("_test_eof"), actions( "actions", *this ), toStateActions( "to_state_actions", *this ), fromStateActions( "from_state_actions", *this ), @@ -128,6 +133,12 @@ public: Variable nbreak; Variable ps; + GotoLabel _out; + GotoLabel _pop; + GotoLabel _again; + GotoLabel _resume; + GotoLabel _test_eof; + TableArray actions; TableArray toStateActions; TableArray fromStateActions; @@ -144,6 +155,8 @@ public: void taNfaPushActions(); void taNfaPopTrans(); + void EOF_CHECK( ostream &ret ); + void GOTO( ostream &ret, int gotoDest, bool inFinish ); void CALL( ostream &ret, int callDest, int targState, bool inFinish ); void NCALL( ostream &ret, int callDest, int targState, bool inFinish ); diff --git a/src/gotoexp.cc b/src/gotoexp.cc index 08c37c51..dea9029c 100644 --- a/src/gotoexp.cc +++ b/src/gotoexp.cc @@ -46,12 +46,11 @@ std::ostream &GotoExp::EXEC_FUNCS() if ( redFsm->anyRegNbreak() ) { out << " if ( " << nbreak << " == 1 )\n" - " goto _out;\n"; - outLabelUsed = true; + " goto " << _out << ";\n"; } - out << "\n\tgoto _again;\n"; + out << "goto " << _again << ";\n"; } } return out; diff --git a/src/gotoloop.cc b/src/gotoloop.cc index 5eab9434..dc536e6d 100644 --- a/src/gotoloop.cc +++ b/src/gotoloop.cc @@ -141,8 +141,7 @@ std::ostream &GotoLoop::EXEC_FUNCS() if ( redFsm->anyRegNbreak() ) { out << " if ( " << nbreak << " == 1 )\n" - " goto _out;\n"; - outLabelUsed = true; + " goto " << _out << ";\n"; } out << diff --git a/src/ipgoto.cc b/src/ipgoto.cc index 52b6760e..64ec499a 100644 --- a/src/ipgoto.cc +++ b/src/ipgoto.cc @@ -84,8 +84,6 @@ void IpGoto::EOF_CHECK( ostream &ret, int gotoDest ) ret << " if ( " << P() << " == " << PE() << " )\n" " goto " << eofLabel[gotoDest].reference() << ";\n"; - - testEofUsed = true; } void IpGoto::GOTO( ostream &ret, int gotoDest, bool inFinish ) @@ -109,9 +107,9 @@ void IpGoto::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) /* Since we are setting CS above and can select on it, call the all-state * test_eof. */ if ( inFinish && !noEnd ) - CodeGen::EOF_CHECK( ret ); + Goto::EOF_CHECK( ret ); - ret << " goto _again;"; + ret << " goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -170,9 +168,9 @@ void IpGoto::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool /* Since we are setting CS above and can select on it, call the all-state * test_eof. */ if ( inFinish && !noEnd ) - CodeGen::EOF_CHECK( ret ); + Goto::EOF_CHECK( ret ); - ret << " goto _again;"; + ret << " goto " << _again << ";"; ret << CLOSE_GEN_BLOCK(); } @@ -205,9 +203,9 @@ void IpGoto::RET( ostream &ret, bool inFinish ) } if ( inFinish && !noEnd ) - CodeGen::EOF_CHECK( ret ); + Goto::EOF_CHECK( ret ); - ret << "goto _again;" << CLOSE_GEN_BLOCK(); + ret << "goto " << _again << ";" << CLOSE_GEN_BLOCK(); } void IpGoto::NRET( ostream &ret, bool inFinish ) @@ -248,16 +246,14 @@ void IpGoto::TARGS( ostream &ret, bool inFinish, int targState ) void IpGoto::BREAK( ostream &ret, int targState, bool csForced ) { - outLabelUsed = true; ret << "{" << P() << "+= 1; "; if ( !csForced ) ret << vCS() << " = " << targState << "; "; - ret << "goto _out;}"; + ret << "goto " << _out << ";}"; } void IpGoto::NBREAK( ostream &ret, int targState, bool csForced ) { - outLabelUsed = true; ret << "{" << P() << "+= 1; "; if ( !csForced ) ret << vCS() << " = " << targState << "; "; @@ -315,15 +311,14 @@ bool IpGoto::IN_TRANS_ACTIONS( RedStateAp *state ) if ( redFsm->anyRegNbreak() ) { out << "if ( " << nbreak << " == 1 )\n" - " goto _out;\n"; - outLabelUsed = true; + " goto " << _out << ";\n"; } /* If the action contains a next then we need to reload, otherwise * jump directly to the target state. */ if ( trans->action->anyNextStmt() ) - out << "\n\tgoto _again;\n"; + out << "\n\tgoto " << _again << ";\n"; else out << "\n\tgoto " << stLabel[trans->targ->id].reference() << ";\n"; } @@ -348,11 +343,10 @@ void IpGoto::GOTO_HEADER( RedStateAp *state ) out << "if ( " << P() << " == " << vEOF() << " ) {\n" " if ( " << vCS() << " >= " << FIRST_FINAL_STATE() << " )\n" - " goto _out;\n" + " goto " << _out << ";\n" " else\n" - " goto _pop;\n" + " goto " << _pop << ";\n" "}\n"; - outLabelUsed = true; } if ( state->toStateAction != 0 ) { @@ -407,9 +401,8 @@ void IpGoto::STATE_GOTO_ERROR() out << "_st" << state->id << ":\n"; /* Break out here. */ - outLabelUsed = true; out << vCS() << " = " << state->id << ";\n"; - out << "goto _pop;\n"; + out << "goto " << _pop << ";\n"; } @@ -469,9 +462,8 @@ std::ostream &IpGoto::EXIT_STATES() { for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) { if ( eofLabel[st->id].isReferenced ) { - testEofUsed = true; out << eofLabel[st->id].define() << ": " << vCS() << " = " << - st->id << "; goto _test_eof; \n"; + st->id << "; goto " << _test_eof << "; \n"; } } return out; @@ -689,8 +681,6 @@ void IpGoto::writeExec() /* Must set labels immediately before writing because we may depend on the * noend write option. */ setLabelsNeeded(); - testEofUsed = false; - outLabelUsed = false; out << "{\n"; @@ -703,17 +693,19 @@ void IpGoto::writeExec() DECLARE( INT(), alt ); if ( !noEnd ) { - testEofUsed = true; out << " if ( " << P() << " == " << PE() << " )\n" - " goto _test_eof;\n"; + " goto " << _test_eof << ";\n"; } - if ( useAgainLabel() ) { + if ( _again.isReferenced ) { out << - " goto _resume;\n" - "\n" - "_again:\n" + " goto " << _resume << ";\n" + "\n"; + + out << EMIT_LABEL( _again ); + + out << " switch ( " << vCS() << " ) {\n"; AGAIN_CASES() << " }\n" @@ -721,8 +713,7 @@ void IpGoto::writeExec() } - if ( useAgainLabel() || redFsm->anyNfaStates() ) - out << "_resume:\n"; + out << EMIT_LABEL( _resume ); out << " switch ( " << vCS() << " ) {\n"; @@ -734,8 +725,7 @@ void IpGoto::writeExec() EXIT_STATES() << "\n"; - if ( testEofUsed ) - out << " _test_eof: {}\n"; + out << EMIT_LABEL( _test_eof ); if ( redFsm->anyEofActivity() ) { out << @@ -777,8 +767,7 @@ void IpGoto::writeExec() out << "}\n"; out << vCS() << " = " << ERROR_STATE() << ";\n"; - out << "goto _pop;\n"; - outLabelUsed = true; + out << "goto " << _pop << ";\n"; out << "}\n"; } @@ -805,15 +794,14 @@ void IpGoto::writeExec() out << "if ( " << vCS() << " >= " << FIRST_FINAL_STATE() << " )\n" - " goto _out; "; + " goto " << _out << "; "; - if ( outLabelUsed ) - out << " _pop: {}\n"; + out << EMIT_LABEL( _pop ); if ( redFsm->anyNfaStates() ) { out << "if ( nfa_len == 0 )\n" - " goto _out;\n" + " goto " << _out << ";\n" "\n"; out << @@ -840,10 +828,10 @@ void IpGoto::writeExec() NFA_POST_POP(); - out << "goto _resume;\n"; + out << "goto " << _resume << ";\n"; } - out << "_out: {}\n"; + out << EMIT_LABEL( _out ); out << "}\n"; |