mlftable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "mlftable.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. /* Determine if we should use indicies or not. */
  27. void OCamlFTabCodeGen::calcIndexSize()
  28. {
  29. int sizeWithInds = 0, sizeWithoutInds = 0;
  30. /* Calculate cost of using with indicies. */
  31. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  32. int totalIndex = st->outSingle.length() + st->outRange.length() +
  33. (st->defTrans == 0 ? 0 : 1);
  34. sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
  35. }
  36. sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
  37. if ( redFsm->anyActions() )
  38. sizeWithInds += arrayTypeSize(redFsm->maxActListId) * redFsm->transSet.length();
  39. /* Calculate the cost of not using indicies. */
  40. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  41. int totalIndex = st->outSingle.length() + st->outRange.length() +
  42. (st->defTrans == 0 ? 0 : 1);
  43. sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
  44. if ( redFsm->anyActions() )
  45. sizeWithoutInds += arrayTypeSize(redFsm->maxActListId) * totalIndex;
  46. }
  47. /* If using indicies reduces the size, use them. */
  48. useIndicies = sizeWithInds < sizeWithoutInds;
  49. }
  50. std::ostream &OCamlFTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
  51. {
  52. int act = 0;
  53. if ( state->toStateAction != 0 )
  54. act = state->toStateAction->actListId+1;
  55. out << act;
  56. return out;
  57. }
  58. std::ostream &OCamlFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  59. {
  60. int act = 0;
  61. if ( state->fromStateAction != 0 )
  62. act = state->fromStateAction->actListId+1;
  63. out << act;
  64. return out;
  65. }
  66. std::ostream &OCamlFTabCodeGen::EOF_ACTION( RedStateAp *state )
  67. {
  68. int act = 0;
  69. if ( state->eofAction != 0 )
  70. act = state->eofAction->actListId+1;
  71. out << act;
  72. return out;
  73. }
  74. /* Write out the function for a transition. */
  75. std::ostream &OCamlFTabCodeGen::TRANS_ACTION( RedTransAp *trans )
  76. {
  77. int action = 0;
  78. if ( trans->action != 0 )
  79. action = trans->action->actListId+1;
  80. out << action;
  81. return out;
  82. }
  83. /* Write out the function switch. This switch is keyed on the values
  84. * of the func index. */
  85. std::ostream &OCamlFTabCodeGen::TO_STATE_ACTION_SWITCH()
  86. {
  87. /* Loop the actions. */
  88. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  89. if ( redAct->numToStateRefs > 0 ) {
  90. /* Write the entry label. */
  91. out << "\t| " << redAct->actListId+1 << " ->\n";
  92. /* Write each action in the list of action items. */
  93. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  94. ACTION( out, item->value, 0, false );
  95. out << "\t()\n";
  96. }
  97. }
  98. genLineDirective( out );
  99. return out;
  100. }
  101. /* Write out the function switch. This switch is keyed on the values
  102. * of the func index. */
  103. std::ostream &OCamlFTabCodeGen::FROM_STATE_ACTION_SWITCH()
  104. {
  105. /* Loop the actions. */
  106. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  107. if ( redAct->numFromStateRefs > 0 ) {
  108. /* Write the entry label. */
  109. out << "\t| " << redAct->actListId+1 << " ->\n";
  110. /* Write each action in the list of action items. */
  111. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  112. ACTION( out, item->value, 0, false );
  113. out << "\t()\n";
  114. }
  115. }
  116. genLineDirective( out );
  117. return out;
  118. }
  119. std::ostream &OCamlFTabCodeGen::EOF_ACTION_SWITCH()
  120. {
  121. /* Loop the actions. */
  122. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  123. if ( redAct->numEofRefs > 0 ) {
  124. /* Write the entry label. */
  125. out << "\t| " << redAct->actListId+1 << " ->\n";
  126. /* Write each action in the list of action items. */
  127. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  128. ACTION( out, item->value, 0, true );
  129. out << "\t()\n";
  130. }
  131. }
  132. genLineDirective( out );
  133. return out;
  134. }
  135. /* Write out the function switch. This switch is keyed on the values
  136. * of the func index. */
  137. std::ostream &OCamlFTabCodeGen::ACTION_SWITCH()
  138. {
  139. /* Loop the actions. */
  140. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  141. if ( redAct->numTransRefs > 0 ) {
  142. /* Write the entry label. */
  143. out << "\t| " << redAct->actListId+1 << " ->\n";
  144. /* Write each action in the list of action items. */
  145. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  146. ACTION( out, item->value, 0, false );
  147. out << "\t()\n";
  148. }
  149. }
  150. genLineDirective( out );
  151. return out;
  152. }
  153. void OCamlFTabCodeGen::writeData()
  154. {
  155. if ( redFsm->anyConditions() ) {
  156. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
  157. COND_OFFSETS();
  158. CLOSE_ARRAY() <<
  159. "\n";
  160. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
  161. COND_LENS();
  162. CLOSE_ARRAY() <<
  163. "\n";
  164. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  165. COND_KEYS();
  166. CLOSE_ARRAY() <<
  167. "\n";
  168. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
  169. COND_SPACES();
  170. CLOSE_ARRAY() <<
  171. "\n";
  172. }
  173. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
  174. KEY_OFFSETS();
  175. CLOSE_ARRAY() <<
  176. "\n";
  177. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  178. KEYS();
  179. CLOSE_ARRAY() <<
  180. "\n";
  181. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
  182. SINGLE_LENS();
  183. CLOSE_ARRAY() <<
  184. "\n";
  185. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
  186. RANGE_LENS();
  187. CLOSE_ARRAY() <<
  188. "\n";
  189. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
  190. INDEX_OFFSETS();
  191. CLOSE_ARRAY() <<
  192. "\n";
  193. if ( useIndicies ) {
  194. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  195. INDICIES();
  196. CLOSE_ARRAY() <<
  197. "\n";
  198. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  199. TRANS_TARGS_WI();
  200. CLOSE_ARRAY() <<
  201. "\n";
  202. if ( redFsm->anyActions() ) {
  203. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
  204. TRANS_ACTIONS_WI();
  205. CLOSE_ARRAY() <<
  206. "\n";
  207. }
  208. }
  209. else {
  210. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  211. TRANS_TARGS();
  212. CLOSE_ARRAY() <<
  213. "\n";
  214. if ( redFsm->anyActions() ) {
  215. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
  216. TRANS_ACTIONS();
  217. CLOSE_ARRAY() <<
  218. "\n";
  219. }
  220. }
  221. if ( redFsm->anyToStateActions() ) {
  222. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  223. TO_STATE_ACTIONS();
  224. CLOSE_ARRAY() <<
  225. "\n";
  226. }
  227. if ( redFsm->anyFromStateActions() ) {
  228. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  229. FROM_STATE_ACTIONS();
  230. CLOSE_ARRAY() <<
  231. "\n";
  232. }
  233. if ( redFsm->anyEofActions() ) {
  234. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
  235. EOF_ACTIONS();
  236. CLOSE_ARRAY() <<
  237. "\n";
  238. }
  239. if ( redFsm->anyEofTrans() ) {
  240. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  241. EOF_TRANS();
  242. CLOSE_ARRAY() <<
  243. "\n";
  244. }
  245. STATE_IDS();
  246. out << "type " << TYPE_STATE() << " = { mutable keys : int; mutable trans : int; }"
  247. << TOP_SEP();
  248. out << "exception Goto_match" << TOP_SEP();
  249. out << "exception Goto_again" << TOP_SEP();
  250. out << "exception Goto_eof_trans" << TOP_SEP();
  251. }
  252. void OCamlFTabCodeGen::writeExec()
  253. {
  254. testEofUsed = false;
  255. outLabelUsed = false;
  256. initVarTypes();
  257. out <<
  258. " begin\n";
  259. // if ( redFsm->anyRegCurStateRef() )
  260. // out << klenType ", _ps";
  261. out <<
  262. " let state = { keys = 0; trans = 0; } in\n"
  263. " let rec do_start () =\n";
  264. // if ( redFsm->anyConditions() )
  265. // out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
  266. if ( !noEnd ) {
  267. testEofUsed = true;
  268. out <<
  269. " if " << P() << " = " << PE() << " then\n"
  270. " do_test_eof ()\n"
  271. "\telse\n";
  272. }
  273. if ( redFsm->errState != 0 ) {
  274. outLabelUsed = true;
  275. out <<
  276. " if " << vCS() << " = " << redFsm->errState->id << " then\n"
  277. " do_out ()\n"
  278. "\telse\n";
  279. }
  280. out << "\tdo_resume ()\n";
  281. out << "and do_resume () =\n";
  282. if ( redFsm->anyFromStateActions() ) {
  283. out <<
  284. " begin match " << AT( FSA(), vCS() ) << " with\n";
  285. FROM_STATE_ACTION_SWITCH();
  286. SWITCH_DEFAULT() <<
  287. " end;\n"
  288. "\n";
  289. }
  290. if ( redFsm->anyConditions() )
  291. COND_TRANSLATE();
  292. out << "\tbegin try\n";
  293. LOCATE_TRANS();
  294. out << "\twith Goto_match -> () end;\n";
  295. out <<
  296. "\tdo_match ()\n";
  297. out << "and do_match () =\n";
  298. if ( useIndicies )
  299. out << " state.trans <- " << CAST(transType) << AT( I(), "state.trans" ) << ";\n";
  300. out << "\tdo_eof_trans ()\n";
  301. // if ( redFsm->anyEofTrans() )
  302. out << "and do_eof_trans () =\n";
  303. if ( redFsm->anyRegCurStateRef() )
  304. out << " let ps = " << vCS() << " in\n";
  305. out <<
  306. " " << vCS() << " <- " << AT( TT() ,"state.trans" ) << ";\n"
  307. "\n";
  308. if ( redFsm->anyRegActions() ) {
  309. out <<
  310. " begin try if " << AT( TA() , "state.trans" ) << " = 0 then\n"
  311. " raise Goto_again;\n"
  312. "\n"
  313. " match " << AT( TA(), "state.trans" ) << " with\n";
  314. ACTION_SWITCH();
  315. SWITCH_DEFAULT() <<
  316. " with Goto_again -> () end;\n"
  317. "\n";
  318. }
  319. out << "\tdo_again ()\n";
  320. // if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  321. // redFsm->anyActionCalls() || redFsm->anyActionRets() )
  322. out << "\tand do_again () =\n";
  323. if ( redFsm->anyToStateActions() ) {
  324. out <<
  325. " begin match " << AT( TSA(), vCS() ) << " with\n";
  326. TO_STATE_ACTION_SWITCH();
  327. SWITCH_DEFAULT() <<
  328. " end;\n"
  329. "\n";
  330. }
  331. if ( redFsm->errState != 0 ) {
  332. outLabelUsed = true;
  333. out <<
  334. " match " << vCS() << " with\n"
  335. "\t| " << redFsm->errState->id << " -> do_out ()\n"
  336. "\t| _ ->\n";
  337. }
  338. out << "\t" << P() << " <- " << P() << " + 1;\n";
  339. if ( !noEnd ) {
  340. out <<
  341. " if " << P() << " <> " << PE() << " then\n"
  342. " do_resume ()\n"
  343. "\telse do_test_eof ()\n";
  344. }
  345. else {
  346. out <<
  347. " do_resume ()\n";
  348. }
  349. // if ( testEofUsed )
  350. out << "and do_test_eof () =\n";
  351. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  352. out <<
  353. " if " << P() << " = " << vEOF() << " then\n"
  354. " begin try\n";
  355. if ( redFsm->anyEofTrans() ) {
  356. out <<
  357. " if " << AT( ET(), vCS() ) << " > 0 then\n"
  358. " begin\n"
  359. " state.trans <- " << CAST(transType) << "(" << AT( ET(), vCS() ) << " - 1);\n"
  360. " raise Goto_eof_trans;\n"
  361. " end;\n";
  362. }
  363. if ( redFsm->anyEofActions() ) {
  364. out <<
  365. " begin match " << AT( EA(), vCS() ) << " with\n";
  366. EOF_ACTION_SWITCH();
  367. SWITCH_DEFAULT() <<
  368. " end\n";
  369. }
  370. out <<
  371. " with Goto_again -> do_again ()\n"
  372. " | Goto_eof_trans -> do_eof_trans () end\n"
  373. "\n";
  374. }
  375. else
  376. {
  377. out << "\t()\n";
  378. }
  379. if ( outLabelUsed )
  380. out << " and do_out () = ()\n";
  381. out << "\tin do_start ()\n";
  382. out << " end;\n";
  383. }