csfgoto.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
  3. * 2004 Erich Ocean <eric.ocean@ampede.com>
  4. * 2005 Alan West <alan@alanz.com>
  5. */
  6. /* This file is part of Ragel.
  7. *
  8. * Ragel is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Ragel is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Ragel; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "ragel.h"
  23. #include "csfgoto.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. #include "bstmap.h"
  27. std::ostream &CSharpFGotoCodeGen::EXEC_ACTIONS()
  28. {
  29. /* Loop the actions. */
  30. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  31. if ( redAct->numTransRefs > 0 ) {
  32. /* We are at the start of a glob, write the case. */
  33. out << "f" << redAct->actListId << ":\n";
  34. /* Write each action in the list of action items. */
  35. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  36. ACTION( out, item->value, 0, false );
  37. out << "\tgoto _again;\n";
  38. }
  39. }
  40. return out;
  41. }
  42. /* Write out the function switch. This switch is keyed on the values
  43. * of the func index. */
  44. std::ostream &CSharpFGotoCodeGen::TO_STATE_ACTION_SWITCH()
  45. {
  46. /* Loop the actions. */
  47. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  48. if ( redAct->numToStateRefs > 0 ) {
  49. /* Write the entry label. */
  50. out << "\tcase " << redAct->actListId+1 << ":\n";
  51. /* Write each action in the list of action items. */
  52. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  53. ACTION( out, item->value, 0, false );
  54. out << "\tbreak;\n";
  55. }
  56. }
  57. genLineDirective( out );
  58. return out;
  59. }
  60. /* Write out the function switch. This switch is keyed on the values
  61. * of the func index. */
  62. std::ostream &CSharpFGotoCodeGen::FROM_STATE_ACTION_SWITCH()
  63. {
  64. /* Loop the actions. */
  65. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  66. if ( redAct->numFromStateRefs > 0 ) {
  67. /* Write the entry label. */
  68. out << "\tcase " << redAct->actListId+1 << ":\n";
  69. /* Write each action in the list of action items. */
  70. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  71. ACTION( out, item->value, 0, false );
  72. out << "\tbreak;\n";
  73. }
  74. }
  75. genLineDirective( out );
  76. return out;
  77. }
  78. std::ostream &CSharpFGotoCodeGen::EOF_ACTION_SWITCH()
  79. {
  80. /* Loop the actions. */
  81. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  82. if ( redAct->numEofRefs > 0 ) {
  83. /* Write the entry label. */
  84. out << "\tcase " << redAct->actListId+1 << ":\n";
  85. /* Write each action in the list of action items. */
  86. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  87. ACTION( out, item->value, 0, true );
  88. out << "\tbreak;\n";
  89. }
  90. }
  91. genLineDirective( out );
  92. return out;
  93. }
  94. std::ostream &CSharpFGotoCodeGen::FINISH_CASES()
  95. {
  96. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  97. /* States that are final and have an out action need a case. */
  98. if ( st->eofAction != 0 ) {
  99. /* Write the case label. */
  100. out << "\t\tcase " << st->id << ": ";
  101. /* Jump to the func. */
  102. out << "goto f" << st->eofAction->actListId << ";\n";
  103. }
  104. }
  105. return out;
  106. }
  107. unsigned int CSharpFGotoCodeGen::TO_STATE_ACTION( RedStateAp *state )
  108. {
  109. int act = 0;
  110. if ( state->toStateAction != 0 )
  111. act = state->toStateAction->actListId+1;
  112. return act;
  113. }
  114. unsigned int CSharpFGotoCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  115. {
  116. int act = 0;
  117. if ( state->fromStateAction != 0 )
  118. act = state->fromStateAction->actListId+1;
  119. return act;
  120. }
  121. unsigned int CSharpFGotoCodeGen::EOF_ACTION( RedStateAp *state )
  122. {
  123. int act = 0;
  124. if ( state->eofAction != 0 )
  125. act = state->eofAction->actListId+1;
  126. return act;
  127. }
  128. void CSharpFGotoCodeGen::writeData()
  129. {
  130. if ( redFsm->anyToStateActions() ) {
  131. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  132. TO_STATE_ACTIONS();
  133. CLOSE_ARRAY() <<
  134. "\n";
  135. }
  136. if ( redFsm->anyFromStateActions() ) {
  137. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  138. FROM_STATE_ACTIONS();
  139. CLOSE_ARRAY() <<
  140. "\n";
  141. }
  142. if ( redFsm->anyEofActions() ) {
  143. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  144. EOF_ACTIONS();
  145. CLOSE_ARRAY() <<
  146. "\n";
  147. }
  148. STATE_IDS();
  149. }
  150. void CSharpFGotoCodeGen::writeExec()
  151. {
  152. testEofUsed = false;
  153. outLabelUsed = false;
  154. out << " {\n";
  155. if ( redFsm->anyRegCurStateRef() )
  156. out << " int _ps = 0;\n";
  157. if ( redFsm->anyConditions() )
  158. out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
  159. if ( !noEnd ) {
  160. testEofUsed = true;
  161. out <<
  162. " if ( " << P() << " == " << PE() << " )\n"
  163. " goto _test_eof;\n";
  164. }
  165. if ( redFsm->errState != 0 ) {
  166. outLabelUsed = true;
  167. out <<
  168. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  169. " goto _out;\n";
  170. }
  171. out << "_resume:\n";
  172. if ( redFsm->anyFromStateActions() ) {
  173. out <<
  174. " switch ( " << FSA() << "[" << vCS() << "] ) {\n";
  175. FROM_STATE_ACTION_SWITCH();
  176. SWITCH_DEFAULT() <<
  177. " }\n"
  178. "\n";
  179. }
  180. out <<
  181. " switch ( " << vCS() << " ) {\n";
  182. STATE_GOTOS();
  183. SWITCH_DEFAULT() <<
  184. " }\n"
  185. "\n";
  186. TRANSITIONS() <<
  187. "\n";
  188. if ( redFsm->anyRegActions() )
  189. EXEC_ACTIONS() << "\n";
  190. out << "_again:\n";
  191. if ( redFsm->anyToStateActions() ) {
  192. out <<
  193. " switch ( " << TSA() << "[" << vCS() << "] ) {\n";
  194. TO_STATE_ACTION_SWITCH();
  195. SWITCH_DEFAULT() <<
  196. " }\n"
  197. "\n";
  198. }
  199. if ( redFsm->errState != 0 ) {
  200. outLabelUsed = true;
  201. out <<
  202. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  203. " goto _out;\n";
  204. }
  205. if ( !noEnd ) {
  206. out <<
  207. " if ( ++" << P() << " != " << PE() << " )\n"
  208. " goto _resume;\n";
  209. }
  210. else {
  211. out <<
  212. " " << P() << " += 1;\n"
  213. " goto _resume;\n";
  214. }
  215. if ( testEofUsed )
  216. out << " _test_eof: {}\n";
  217. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  218. out <<
  219. " if ( " << P() << " == " << vEOF() << " )\n"
  220. " {\n";
  221. if ( redFsm->anyEofTrans() ) {
  222. out <<
  223. " switch ( " << vCS() << " ) {\n";
  224. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  225. if ( st->eofTrans != 0 )
  226. out << " case " << st->id << ": goto tr" << st->eofTrans->id << ";\n";
  227. }
  228. SWITCH_DEFAULT() <<
  229. " }\n";
  230. }
  231. if ( redFsm->anyEofActions() ) {
  232. out <<
  233. " switch ( " << EA() << "[" << vCS() << "] ) {\n";
  234. EOF_ACTION_SWITCH();
  235. SWITCH_DEFAULT() <<
  236. " }\n";
  237. }
  238. out <<
  239. " }\n"
  240. "\n";
  241. }
  242. if ( outLabelUsed )
  243. out << " _out: {}\n";
  244. out << " }\n";
  245. }