gofflat.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright 2004-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 "gofflat.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. using std::endl;
  27. std::ostream &GoFFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
  28. {
  29. int act = 0;
  30. if ( state->toStateAction != 0 )
  31. act = state->toStateAction->actListId+1;
  32. out << act;
  33. return out;
  34. }
  35. std::ostream &GoFFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  36. {
  37. int act = 0;
  38. if ( state->fromStateAction != 0 )
  39. act = state->fromStateAction->actListId+1;
  40. out << act;
  41. return out;
  42. }
  43. std::ostream &GoFFlatCodeGen::EOF_ACTION( RedStateAp *state )
  44. {
  45. int act = 0;
  46. if ( state->eofAction != 0 )
  47. act = state->eofAction->actListId+1;
  48. out << act;
  49. return out;
  50. }
  51. /* Write out the function for a transition. */
  52. std::ostream &GoFFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
  53. {
  54. int action = 0;
  55. if ( trans->action != 0 )
  56. action = trans->action->actListId+1;
  57. out << action;
  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 &GoFFlatCodeGen::TO_STATE_ACTION_SWITCH( int level )
  63. {
  64. /* Loop the actions. */
  65. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  66. if ( redAct->numToStateRefs > 0 ) {
  67. /* Write the entry label. */
  68. out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
  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, false );
  72. }
  73. }
  74. genLineDirective( out );
  75. return out;
  76. }
  77. /* Write out the function switch. This switch is keyed on the values
  78. * of the func index. */
  79. std::ostream &GoFFlatCodeGen::FROM_STATE_ACTION_SWITCH( int level )
  80. {
  81. /* Loop the actions. */
  82. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  83. if ( redAct->numFromStateRefs > 0 ) {
  84. /* Write the entry label. */
  85. out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
  86. /* Write each action in the list of action items. */
  87. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  88. ACTION( out, item->value, 0, false, false );
  89. }
  90. }
  91. genLineDirective( out );
  92. return out;
  93. }
  94. std::ostream &GoFFlatCodeGen::EOF_ACTION_SWITCH( int level )
  95. {
  96. /* Loop the actions. */
  97. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  98. if ( redAct->numEofRefs > 0 ) {
  99. /* Write the entry label. */
  100. out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
  101. /* Write each action in the list of action items. */
  102. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  103. ACTION( out, item->value, 0, true, false );
  104. }
  105. }
  106. genLineDirective( out );
  107. return out;
  108. }
  109. /* Write out the function switch. This switch is keyed on the values
  110. * of the func index. */
  111. std::ostream &GoFFlatCodeGen::ACTION_SWITCH( int level )
  112. {
  113. /* Loop the actions. */
  114. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  115. if ( redAct->numTransRefs > 0 ) {
  116. /* Write the entry label. */
  117. out << TABS(level) << "case " << redAct->actListId+1 << ":" << endl;
  118. /* Write each action in the list of action items. */
  119. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  120. ACTION( out, item->value, 0, false, false );
  121. }
  122. }
  123. genLineDirective( out );
  124. return out;
  125. }
  126. void GoFFlatCodeGen::writeData()
  127. {
  128. if ( redFsm->anyConditions() ) {
  129. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  130. COND_KEYS();
  131. CLOSE_ARRAY() <<
  132. endl;
  133. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
  134. COND_KEY_SPANS();
  135. CLOSE_ARRAY() <<
  136. endl;
  137. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
  138. CONDS();
  139. CLOSE_ARRAY() <<
  140. endl;
  141. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
  142. COND_INDEX_OFFSET();
  143. CLOSE_ARRAY() <<
  144. endl;
  145. }
  146. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  147. KEYS();
  148. CLOSE_ARRAY() <<
  149. endl;
  150. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
  151. KEY_SPANS();
  152. CLOSE_ARRAY() <<
  153. endl;
  154. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
  155. FLAT_INDEX_OFFSET();
  156. CLOSE_ARRAY() <<
  157. endl;
  158. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  159. INDICIES();
  160. CLOSE_ARRAY() <<
  161. endl;
  162. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  163. TRANS_TARGS();
  164. CLOSE_ARRAY() <<
  165. endl;
  166. if ( redFsm->anyActions() ) {
  167. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
  168. TRANS_ACTIONS();
  169. CLOSE_ARRAY() <<
  170. endl;
  171. }
  172. if ( redFsm->anyToStateActions() ) {
  173. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  174. TO_STATE_ACTIONS();
  175. CLOSE_ARRAY() <<
  176. endl;
  177. }
  178. if ( redFsm->anyFromStateActions() ) {
  179. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  180. FROM_STATE_ACTIONS();
  181. CLOSE_ARRAY() <<
  182. endl;
  183. }
  184. if ( redFsm->anyEofActions() ) {
  185. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
  186. EOF_ACTIONS();
  187. CLOSE_ARRAY() <<
  188. endl;
  189. }
  190. if ( redFsm->anyEofTrans() ) {
  191. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  192. EOF_TRANS();
  193. CLOSE_ARRAY() <<
  194. endl;
  195. }
  196. STATE_IDS();
  197. }
  198. void GoFFlatCodeGen::writeExec()
  199. {
  200. testEofUsed = false;
  201. outLabelUsed = false;
  202. out <<
  203. " {" << endl <<
  204. " var _slen " << INT() << endl;
  205. if ( redFsm->anyRegCurStateRef() )
  206. out << " var _ps " << INT() << endl;
  207. out << " var _trans " << INT() << endl;
  208. if ( redFsm->anyConditions() )
  209. out << " var _cond " << INT() << endl;
  210. out <<
  211. " var _keys " << INT() << endl <<
  212. " var _inds " << INT() << endl;
  213. if ( redFsm->anyConditions() ) {
  214. out <<
  215. " var _conds " << INT() << endl <<
  216. " var _widec " << WIDE_ALPH_TYPE() << endl;
  217. }
  218. if ( !noEnd ) {
  219. testEofUsed = true;
  220. out <<
  221. " if " << P() << " == " << PE() << " {" << endl <<
  222. " goto _test_eof" << endl <<
  223. " }" << endl;
  224. }
  225. if ( redFsm->errState != 0 ) {
  226. outLabelUsed = true;
  227. out <<
  228. " if " << vCS() << " == " << redFsm->errState->id << " {" << endl <<
  229. " goto _out" << endl <<
  230. " }" << endl;
  231. }
  232. out << "_resume:" << endl;
  233. if ( redFsm->anyFromStateActions() ) {
  234. out <<
  235. " switch " << FSA() << "[" << vCS() << "] {" << endl;
  236. FROM_STATE_ACTION_SWITCH(1);
  237. out <<
  238. " }" << endl <<
  239. endl;
  240. }
  241. if ( redFsm->anyConditions() )
  242. COND_TRANSLATE();
  243. LOCATE_TRANS();
  244. if ( redFsm->anyEofTrans() )
  245. out << "_eof_trans:" << endl;
  246. if ( redFsm->anyRegCurStateRef() )
  247. out << " _ps = " << vCS() << endl;
  248. out <<
  249. " " << vCS() << " = " << CAST(INT(), TT() + "[_trans]") << endl <<
  250. endl;
  251. if ( redFsm->anyRegActions() ) {
  252. out <<
  253. " if " << TA() << "[_trans] == 0 {" << endl <<
  254. " goto _again" << endl <<
  255. " }" << endl <<
  256. endl <<
  257. " switch " << TA() << "[_trans] {" << endl;
  258. ACTION_SWITCH(1);
  259. out <<
  260. " }" << endl <<
  261. endl;
  262. }
  263. if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  264. redFsm->anyActionCalls() || redFsm->anyActionRets() )
  265. out << "_again:" << endl;
  266. if ( redFsm->anyToStateActions() ) {
  267. out <<
  268. " switch " << TSA() << "[" << vCS() << "] {" << endl;
  269. TO_STATE_ACTION_SWITCH(1);
  270. out <<
  271. " }" << endl <<
  272. endl;
  273. }
  274. if ( redFsm->errState != 0 ) {
  275. outLabelUsed = true;
  276. out <<
  277. " if " << vCS() << " == " << redFsm->errState->id << " {" << endl <<
  278. " goto _out" << endl <<
  279. " }" << endl;
  280. }
  281. if ( !noEnd ) {
  282. out <<
  283. " if " << P() << "++; " << P() << " != " << PE() << " {"
  284. " goto _resume" << endl <<
  285. " }" << endl;
  286. }
  287. else {
  288. out <<
  289. " " << P() << "++" << endl <<
  290. " goto _resume" << endl;
  291. }
  292. if ( testEofUsed )
  293. out << " _test_eof: {}" << endl;
  294. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  295. out <<
  296. " if " << P() << " == " << vEOF() << " {" << endl;
  297. if ( redFsm->anyEofTrans() ) {
  298. out <<
  299. " if " << ET() << "[" << vCS() << "] > 0 {" << endl <<
  300. " _trans = " << CAST(INT(), ET() + "[" + vCS() + "] - 1") << endl <<
  301. " goto _eof_trans" << endl <<
  302. " }" << endl;
  303. }
  304. if ( redFsm->anyEofActions() ) {
  305. out <<
  306. " switch " << EA() << "[" << vCS() << "] {" << endl;
  307. EOF_ACTION_SWITCH(2);
  308. out <<
  309. " }" << endl;
  310. }
  311. out <<
  312. " }" << endl <<
  313. endl;
  314. }
  315. if ( outLabelUsed )
  316. out << " _out: {}" << endl;
  317. out << " }" << endl;
  318. }