rubyftable.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * 2007 Victor Hugo Borja <vic@rubyforge.org>
  3. * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
  4. */
  5. /* This file is part of Ragel.
  6. *
  7. * Ragel is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ragel is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ragel; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <iomanip>
  22. #include <sstream>
  23. #include "redfsm.h"
  24. #include "gendata.h"
  25. #include "ragel.h"
  26. #include "rubyftable.h"
  27. using std::ostream;
  28. using std::ostringstream;
  29. using std::string;
  30. using std::cerr;
  31. using std::endl;
  32. void RubyFTabCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
  33. {
  34. out <<
  35. " begin\n"
  36. " " << vCS() << " = " << gotoDest << "\n"
  37. " _goto_level = _again\n"
  38. " next\n"
  39. " end\n";
  40. }
  41. void RubyFTabCodeGen::GOTO_EXPR( ostream &out, GenInlineItem *ilItem, bool inFinish )
  42. {
  43. out <<
  44. " begin\n"
  45. " " << vCS() << " = (";
  46. INLINE_LIST( out, ilItem->children, 0, inFinish );
  47. out << ")\n";
  48. out <<
  49. " _goto_level = _again\n"
  50. " next\n"
  51. " end\n";
  52. }
  53. void RubyFTabCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
  54. {
  55. if ( prePushExpr != 0 ) {
  56. out << "begin\n";
  57. INLINE_LIST( out, prePushExpr, 0, false );
  58. }
  59. out <<
  60. " begin\n"
  61. " " << STACK() << "[" << TOP() << "] = " << vCS() << "\n"
  62. " " << TOP() << "+= 1\n"
  63. " " << vCS() << " = " << callDest << "\n"
  64. " _goto_level = _again\n"
  65. " next\n"
  66. " end\n";
  67. if ( prePushExpr != 0 )
  68. out << "end\n";
  69. }
  70. void RubyFTabCodeGen::CALL_EXPR(ostream &out, GenInlineItem *ilItem,
  71. int targState, bool inFinish )
  72. {
  73. if ( prePushExpr != 0 ) {
  74. out << "begin\n";
  75. INLINE_LIST( out, prePushExpr, 0, false );
  76. }
  77. out <<
  78. " begin\n"
  79. " " << STACK() << "[" << TOP() << "] = " << vCS() << "\n"
  80. " " << TOP() << " += 1\n"
  81. " " << vCS() << " = (";
  82. INLINE_LIST( out, ilItem->children, targState, inFinish );
  83. out << ")\n";
  84. out <<
  85. " _goto_level = _again\n"
  86. " next\n"
  87. " end\n";
  88. if ( prePushExpr != 0 )
  89. out << "end\n";
  90. }
  91. void RubyFTabCodeGen::RET( ostream &out, bool inFinish )
  92. {
  93. out <<
  94. " begin\n"
  95. " " << TOP() << " -= 1\n"
  96. " " << vCS() << " = " << STACK() << "[" << TOP() << "]\n";
  97. if ( postPopExpr != 0 ) {
  98. out << "begin\n";
  99. INLINE_LIST( out, postPopExpr, 0, false );
  100. out << "end\n";
  101. }
  102. out <<
  103. " _goto_level = _again\n"
  104. " next\n"
  105. " end\n";
  106. }
  107. void RubyFTabCodeGen::BREAK( ostream &out, int targState )
  108. {
  109. out <<
  110. " begin\n"
  111. " " << P() << " += 1\n"
  112. " _goto_level = _out\n"
  113. " next\n"
  114. " end\n";
  115. }
  116. std::ostream &RubyFTabCodeGen::TO_STATE_ACTION_SWITCH()
  117. {
  118. /* Loop the actions. */
  119. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  120. if ( redAct->numToStateRefs > 0 ) {
  121. /* Write the entry label. */
  122. out << "\twhen " << redAct->actListId+1 << " then\n";
  123. /* Write each action in the list of action items. */
  124. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  125. ACTION( out, item->value, 0, false );
  126. }
  127. }
  128. genLineDirective( out );
  129. return out;
  130. }
  131. /* Write out the function switch. This switch is keyed on the values
  132. * of the func index. */
  133. std::ostream &RubyFTabCodeGen::FROM_STATE_ACTION_SWITCH()
  134. {
  135. /* Loop the actions. */
  136. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  137. if ( redAct->numFromStateRefs > 0 ) {
  138. /* Write the entry label. */
  139. out << "\twhen " << redAct->actListId+1 << " then\n";
  140. /* Write each action in the list of action items. */
  141. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  142. ACTION( out, item->value, 0, false );
  143. }
  144. }
  145. genLineDirective( out );
  146. return out;
  147. }
  148. std::ostream &RubyFTabCodeGen::EOF_ACTION_SWITCH()
  149. {
  150. /* Loop the actions. */
  151. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  152. if ( redAct->numEofRefs > 0 ) {
  153. /* Write the entry label. */
  154. out << "\twhen " << redAct->actListId+1 << " then\n";
  155. /* Write each action in the list of action items. */
  156. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  157. ACTION( out, item->value, 0, true );
  158. }
  159. }
  160. genLineDirective( out );
  161. return out;
  162. }
  163. /* Write out the function switch. This switch is keyed on the values
  164. * of the func index. */
  165. std::ostream &RubyFTabCodeGen::ACTION_SWITCH()
  166. {
  167. /* Loop the actions. */
  168. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  169. if ( redAct->numTransRefs > 0 ) {
  170. /* Write the entry label. */
  171. out << "\twhen " << redAct->actListId+1 << " then\n";
  172. /* Write each action in the list of action items. */
  173. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  174. ACTION( out, item->value, 0, false );
  175. }
  176. }
  177. genLineDirective( out );
  178. return out;
  179. }
  180. int RubyFTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
  181. {
  182. int act = 0;
  183. if ( state->toStateAction != 0 )
  184. act = state->toStateAction->actListId+1;
  185. return act;
  186. }
  187. int RubyFTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  188. {
  189. int act = 0;
  190. if ( state->fromStateAction != 0 )
  191. act = state->fromStateAction->actListId+1;
  192. return act;
  193. }
  194. int RubyFTabCodeGen::EOF_ACTION( RedStateAp *state )
  195. {
  196. int act = 0;
  197. if ( state->eofAction != 0 )
  198. act = state->eofAction->actListId+1;
  199. return act;
  200. }
  201. /* Write out the function for a transition. */
  202. int RubyFTabCodeGen::TRANS_ACTION( RedTransAp *trans )
  203. {
  204. int action = 0;
  205. if ( trans->action != 0 )
  206. action = trans->action->actListId+1;
  207. return action;
  208. }
  209. void RubyFTabCodeGen::writeData()
  210. {
  211. if ( redFsm->anyConditions() ) {
  212. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
  213. COND_OFFSETS();
  214. CLOSE_ARRAY() <<
  215. "\n";
  216. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
  217. COND_LENS();
  218. CLOSE_ARRAY() <<
  219. "\n";
  220. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  221. COND_KEYS();
  222. CLOSE_ARRAY() <<
  223. "\n";
  224. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
  225. COND_SPACES();
  226. CLOSE_ARRAY() <<
  227. "\n";
  228. }
  229. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
  230. KEY_OFFSETS();
  231. CLOSE_ARRAY() <<
  232. "\n";
  233. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  234. KEYS();
  235. CLOSE_ARRAY() <<
  236. "\n";
  237. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
  238. SINGLE_LENS();
  239. CLOSE_ARRAY() <<
  240. "\n";
  241. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
  242. RANGE_LENS();
  243. CLOSE_ARRAY() <<
  244. "\n";
  245. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
  246. INDEX_OFFSETS();
  247. CLOSE_ARRAY() <<
  248. "\n";
  249. if ( useIndicies ) {
  250. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  251. INDICIES();
  252. CLOSE_ARRAY() <<
  253. "\n";
  254. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  255. TRANS_TARGS_WI();
  256. CLOSE_ARRAY() <<
  257. "\n";
  258. if ( redFsm->anyActions() ) {
  259. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
  260. TRANS_ACTIONS_WI();
  261. CLOSE_ARRAY() <<
  262. "\n";
  263. }
  264. }
  265. else {
  266. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  267. TRANS_TARGS();
  268. CLOSE_ARRAY() <<
  269. "\n";
  270. if ( redFsm->anyActions() ) {
  271. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
  272. TRANS_ACTIONS();
  273. CLOSE_ARRAY() <<
  274. "\n";
  275. }
  276. }
  277. if ( redFsm->anyToStateActions() ) {
  278. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  279. TO_STATE_ACTIONS();
  280. CLOSE_ARRAY() <<
  281. "\n";
  282. }
  283. if ( redFsm->anyFromStateActions() ) {
  284. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  285. FROM_STATE_ACTIONS();
  286. CLOSE_ARRAY() <<
  287. "\n";
  288. }
  289. if ( redFsm->anyEofActions() ) {
  290. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
  291. EOF_ACTIONS();
  292. CLOSE_ARRAY() <<
  293. "\n";
  294. }
  295. if ( redFsm->anyEofTrans() ) {
  296. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  297. EOF_TRANS();
  298. CLOSE_ARRAY() <<
  299. "\n";
  300. }
  301. STATE_IDS();
  302. }
  303. void RubyFTabCodeGen::writeExec()
  304. {
  305. out <<
  306. "begin\n"
  307. " testEof = false\n"
  308. " _klen, _trans, _keys";
  309. if ( redFsm->anyRegCurStateRef() )
  310. out << ", _ps";
  311. if ( redFsm->anyConditions() )
  312. out << ", _widec";
  313. out << " = nil\n";
  314. out <<
  315. " _goto_level = 0\n"
  316. " _resume = 10\n"
  317. " _eof_trans = 15\n"
  318. " _again = 20\n"
  319. " _test_eof = 30\n"
  320. " _out = 40\n";
  321. out <<
  322. " while true\n"
  323. " if _goto_level <= 0\n";
  324. if ( !noEnd ) {
  325. out <<
  326. " if " << P() << " == " << PE() << "\n"
  327. " _goto_level = _test_eof\n"
  328. " next\n"
  329. " end\n";
  330. }
  331. if ( redFsm->errState != 0 ) {
  332. out <<
  333. " if " << vCS() << " == " << redFsm->errState->id << "\n"
  334. " _goto_level = _out\n"
  335. " next\n"
  336. " end\n";
  337. }
  338. /* The resume label. */
  339. out <<
  340. " end\n"
  341. " if _goto_level <= _resume\n";
  342. if ( redFsm->anyFromStateActions() ) {
  343. out <<
  344. " case " << FSA() << "[" << vCS() << "] \n";
  345. FROM_STATE_ACTION_SWITCH() <<
  346. " end # from state action switch \n"
  347. "\n";
  348. }
  349. if ( redFsm->anyConditions() )
  350. COND_TRANSLATE();
  351. LOCATE_TRANS();
  352. if ( useIndicies )
  353. out << " _trans = " << I() << "[_trans];\n";
  354. if ( redFsm->anyEofTrans() ) {
  355. out <<
  356. " end\n"
  357. " if _goto_level <= _eof_trans\n";
  358. }
  359. if ( redFsm->anyRegCurStateRef() )
  360. out << " _ps = " << vCS() << ";\n";
  361. out <<
  362. " " << vCS() << " = " << TT() << "[_trans];\n"
  363. "\n";
  364. if ( redFsm->anyRegActions() ) {
  365. out <<
  366. " if " << TA() << "[_trans] != 0\n"
  367. "\n"
  368. " case " << TA() << "[_trans] \n";
  369. ACTION_SWITCH() <<
  370. " end # action switch \n"
  371. " end\n"
  372. "\n";
  373. }
  374. /* The again label. */
  375. out <<
  376. " end\n"
  377. " if _goto_level <= _again\n";
  378. if ( redFsm->anyToStateActions() ) {
  379. out <<
  380. " case " << TSA() << "[" << vCS() << "] \n";
  381. TO_STATE_ACTION_SWITCH() <<
  382. " end\n"
  383. "\n";
  384. }
  385. if ( redFsm->errState != 0 ) {
  386. out <<
  387. " if " << vCS() << " == " << redFsm->errState->id << "\n"
  388. " _goto_level = _out\n"
  389. " next\n"
  390. " end\n";
  391. }
  392. out << " " << P() << " += 1\n";
  393. if ( !noEnd ) {
  394. out <<
  395. " if " << P() << " != " << PE() << "\n"
  396. " _goto_level = _resume\n"
  397. " next\n"
  398. " end\n";
  399. }
  400. else {
  401. out <<
  402. " _goto_level = _resume\n"
  403. " next\n";
  404. }
  405. /* The test eof label. */
  406. out <<
  407. " end\n"
  408. " if _goto_level <= _test_eof\n";
  409. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  410. out <<
  411. " if " << P() << " == " << vEOF() << "\n";
  412. if ( redFsm->anyEofTrans() ) {
  413. out <<
  414. " if " << ET() << "[" << vCS() << "] > 0\n"
  415. " _trans = " << ET() << "[" << vCS() << "] - 1;\n"
  416. " _goto_level = _eof_trans\n"
  417. " next;\n"
  418. " end\n";
  419. }
  420. if ( redFsm->anyEofActions() ) {
  421. out <<
  422. " begin\n"
  423. " case ( " << EA() << "[" << vCS() << "] )\n";
  424. EOF_ACTION_SWITCH() <<
  425. " end\n"
  426. " end\n";
  427. }
  428. out <<
  429. " end\n"
  430. "\n";
  431. }
  432. out <<
  433. " end\n"
  434. " if _goto_level <= _out\n"
  435. " break\n"
  436. " end\n"
  437. "end\n";
  438. /* Wrapping the execute block. */
  439. out << " end\n";
  440. }
  441. void RubyFTabCodeGen::calcIndexSize()
  442. {
  443. int sizeWithInds = 0, sizeWithoutInds = 0;
  444. /* Calculate cost of using with indicies. */
  445. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  446. int totalIndex = st->outSingle.length() + st->outRange.length() +
  447. (st->defTrans == 0 ? 0 : 1);
  448. sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
  449. }
  450. sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
  451. if ( redFsm->anyActions() )
  452. sizeWithInds += arrayTypeSize(redFsm->maxActListId) * redFsm->transSet.length();
  453. /* Calculate the cost of not using indicies. */
  454. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  455. int totalIndex = st->outSingle.length() + st->outRange.length() +
  456. (st->defTrans == 0 ? 0 : 1);
  457. sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
  458. if ( redFsm->anyActions() )
  459. sizeWithoutInds += arrayTypeSize(redFsm->maxActListId) * totalIndex;
  460. }
  461. /* If using indicies reduces the size, use them. */
  462. useIndicies = sizeWithInds < sizeWithoutInds;
  463. }
  464. /*
  465. * Local Variables:
  466. * mode: c++
  467. * indent-tabs-mode: 1
  468. * c-file-style: "bsd"
  469. * End:
  470. */