cdflat.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 "cdflat.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. std::ostream &FlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
  27. {
  28. int act = 0;
  29. if ( state->toStateAction != 0 )
  30. act = state->toStateAction->location+1;
  31. out << act;
  32. return out;
  33. }
  34. std::ostream &FlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  35. {
  36. int act = 0;
  37. if ( state->fromStateAction != 0 )
  38. act = state->fromStateAction->location+1;
  39. out << act;
  40. return out;
  41. }
  42. std::ostream &FlatCodeGen::EOF_ACTION( RedStateAp *state )
  43. {
  44. int act = 0;
  45. if ( state->eofAction != 0 )
  46. act = state->eofAction->location+1;
  47. out << act;
  48. return out;
  49. }
  50. std::ostream &FlatCodeGen::TRANS_ACTION( RedTransAp *trans )
  51. {
  52. /* If there are actions, emit them. Otherwise emit zero. */
  53. int act = 0;
  54. if ( trans->action != 0 )
  55. act = trans->action->location+1;
  56. out << act;
  57. return out;
  58. }
  59. std::ostream &FlatCodeGen::TO_STATE_ACTION_SWITCH()
  60. {
  61. /* Walk the list of functions, printing the cases. */
  62. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  63. /* Write out referenced actions. */
  64. if ( act->numToStateRefs > 0 ) {
  65. /* Write the case label, the action and the case break */
  66. out << "\tcase " << act->actionId << ":\n";
  67. ACTION( out, act, 0, false, false );
  68. out << "\tbreak;\n";
  69. }
  70. }
  71. genLineDirective( out );
  72. return out;
  73. }
  74. std::ostream &FlatCodeGen::FROM_STATE_ACTION_SWITCH()
  75. {
  76. /* Walk the list of functions, printing the cases. */
  77. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  78. /* Write out referenced actions. */
  79. if ( act->numFromStateRefs > 0 ) {
  80. /* Write the case label, the action and the case break */
  81. out << "\tcase " << act->actionId << ":\n";
  82. ACTION( out, act, 0, false, false );
  83. out << "\tbreak;\n";
  84. }
  85. }
  86. genLineDirective( out );
  87. return out;
  88. }
  89. std::ostream &FlatCodeGen::EOF_ACTION_SWITCH()
  90. {
  91. /* Walk the list of functions, printing the cases. */
  92. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  93. /* Write out referenced actions. */
  94. if ( act->numEofRefs > 0 ) {
  95. /* Write the case label, the action and the case break */
  96. out << "\tcase " << act->actionId << ":\n";
  97. ACTION( out, act, 0, true, false );
  98. out << "\tbreak;\n";
  99. }
  100. }
  101. genLineDirective( out );
  102. return out;
  103. }
  104. std::ostream &FlatCodeGen::ACTION_SWITCH()
  105. {
  106. /* Walk the list of functions, printing the cases. */
  107. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  108. /* Write out referenced actions. */
  109. if ( act->numTransRefs > 0 ) {
  110. /* Write the case label, the action and the case break */
  111. out << "\tcase " << act->actionId << ":\n";
  112. ACTION( out, act, 0, false, false );
  113. out << "\tbreak;\n";
  114. }
  115. }
  116. genLineDirective( out );
  117. return out;
  118. }
  119. std::ostream &FlatCodeGen::FLAT_INDEX_OFFSET()
  120. {
  121. out << "\t";
  122. int totalStateNum = 0, curIndOffset = 0;
  123. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  124. /* Write the index offset. */
  125. out << curIndOffset;
  126. if ( !st.last() ) {
  127. out << ", ";
  128. if ( ++totalStateNum % IALL == 0 )
  129. out << "\n\t";
  130. }
  131. /* Move the index offset ahead. */
  132. if ( st->transList != 0 )
  133. curIndOffset += keyOps->span( st->lowKey, st->highKey );
  134. if ( st->defTrans != 0 )
  135. curIndOffset += 1;
  136. }
  137. out << "\n";
  138. return out;
  139. }
  140. std::ostream &FlatCodeGen::KEY_SPANS()
  141. {
  142. out << "\t";
  143. int totalStateNum = 0;
  144. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  145. /* Write singles length. */
  146. unsigned long long span = 0;
  147. if ( st->transList != 0 )
  148. span = keyOps->span( st->lowKey, st->highKey );
  149. out << span;
  150. if ( !st.last() ) {
  151. out << ", ";
  152. if ( ++totalStateNum % IALL == 0 )
  153. out << "\n\t";
  154. }
  155. }
  156. out << "\n";
  157. return out;
  158. }
  159. std::ostream &FlatCodeGen::TO_STATE_ACTIONS()
  160. {
  161. out << "\t";
  162. int totalStateNum = 0;
  163. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  164. /* Write any eof action. */
  165. TO_STATE_ACTION(st);
  166. if ( !st.last() ) {
  167. out << ", ";
  168. if ( ++totalStateNum % IALL == 0 )
  169. out << "\n\t";
  170. }
  171. }
  172. out << "\n";
  173. return out;
  174. }
  175. std::ostream &FlatCodeGen::FROM_STATE_ACTIONS()
  176. {
  177. out << "\t";
  178. int totalStateNum = 0;
  179. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  180. /* Write any eof action. */
  181. FROM_STATE_ACTION(st);
  182. if ( !st.last() ) {
  183. out << ", ";
  184. if ( ++totalStateNum % IALL == 0 )
  185. out << "\n\t";
  186. }
  187. }
  188. out << "\n";
  189. return out;
  190. }
  191. std::ostream &FlatCodeGen::EOF_ACTIONS()
  192. {
  193. out << "\t";
  194. int totalStateNum = 0;
  195. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  196. /* Write any eof action. */
  197. EOF_ACTION(st);
  198. if ( !st.last() ) {
  199. out << ", ";
  200. if ( ++totalStateNum % IALL == 0 )
  201. out << "\n\t";
  202. }
  203. }
  204. out << "\n";
  205. return out;
  206. }
  207. std::ostream &FlatCodeGen::EOF_TRANS()
  208. {
  209. out << "\t";
  210. int totalStateNum = 0;
  211. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  212. /* Write any eof action. */
  213. long trans = 0;
  214. if ( st->eofTrans != 0 ) {
  215. assert( st->eofTrans->pos >= 0 );
  216. trans = st->eofTrans->pos+1;
  217. }
  218. out << trans;
  219. if ( !st.last() ) {
  220. out << ", ";
  221. if ( ++totalStateNum % IALL == 0 )
  222. out << "\n\t";
  223. }
  224. }
  225. out << "\n";
  226. return out;
  227. }
  228. std::ostream &FlatCodeGen::COND_KEYS()
  229. {
  230. out << '\t';
  231. int totalTrans = 0;
  232. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  233. /* Emit just cond low key and cond high key. */
  234. out << KEY( st->condLowKey ) << ", ";
  235. out << KEY( st->condHighKey ) << ", ";
  236. if ( ++totalTrans % IALL == 0 )
  237. out << "\n\t";
  238. }
  239. /* Output one last number so we don't have to figure out when the last
  240. * entry is and avoid writing a comma. */
  241. out << 0 << "\n";
  242. return out;
  243. }
  244. std::ostream &FlatCodeGen::COND_KEY_SPANS()
  245. {
  246. out << "\t";
  247. int totalStateNum = 0;
  248. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  249. /* Write singles length. */
  250. unsigned long long span = 0;
  251. if ( st->condList != 0 )
  252. span = keyOps->span( st->condLowKey, st->condHighKey );
  253. out << span;
  254. if ( !st.last() ) {
  255. out << ", ";
  256. if ( ++totalStateNum % IALL == 0 )
  257. out << "\n\t";
  258. }
  259. }
  260. out << "\n";
  261. return out;
  262. }
  263. std::ostream &FlatCodeGen::CONDS()
  264. {
  265. int totalTrans = 0;
  266. out << '\t';
  267. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  268. if ( st->condList != 0 ) {
  269. /* Walk the singles. */
  270. unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
  271. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  272. if ( st->condList[pos] != 0 )
  273. out << st->condList[pos]->condSpaceId + 1 << ", ";
  274. else
  275. out << "0, ";
  276. if ( ++totalTrans % IALL == 0 )
  277. out << "\n\t";
  278. }
  279. }
  280. }
  281. /* Output one last number so we don't have to figure out when the last
  282. * entry is and avoid writing a comma. */
  283. out << 0 << "\n";
  284. return out;
  285. }
  286. std::ostream &FlatCodeGen::COND_INDEX_OFFSET()
  287. {
  288. out << "\t";
  289. int totalStateNum = 0, curIndOffset = 0;
  290. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  291. /* Write the index offset. */
  292. out << curIndOffset;
  293. if ( !st.last() ) {
  294. out << ", ";
  295. if ( ++totalStateNum % IALL == 0 )
  296. out << "\n\t";
  297. }
  298. /* Move the index offset ahead. */
  299. if ( st->condList != 0 )
  300. curIndOffset += keyOps->span( st->condLowKey, st->condHighKey );
  301. }
  302. out << "\n";
  303. return out;
  304. }
  305. std::ostream &FlatCodeGen::KEYS()
  306. {
  307. out << '\t';
  308. int totalTrans = 0;
  309. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  310. /* Emit just low key and high key. */
  311. out << KEY( st->lowKey ) << ", ";
  312. out << KEY( st->highKey ) << ", ";
  313. if ( ++totalTrans % IALL == 0 )
  314. out << "\n\t";
  315. }
  316. /* Output one last number so we don't have to figure out when the last
  317. * entry is and avoid writing a comma. */
  318. out << 0 << "\n";
  319. return out;
  320. }
  321. std::ostream &FlatCodeGen::INDICIES()
  322. {
  323. int totalTrans = 0;
  324. out << '\t';
  325. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  326. if ( st->transList != 0 ) {
  327. /* Walk the singles. */
  328. unsigned long long span = keyOps->span( st->lowKey, st->highKey );
  329. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  330. out << st->transList[pos]->id << ", ";
  331. if ( ++totalTrans % IALL == 0 )
  332. out << "\n\t";
  333. }
  334. }
  335. /* The state's default index goes next. */
  336. if ( st->defTrans != 0 )
  337. out << st->defTrans->id << ", ";
  338. if ( ++totalTrans % IALL == 0 )
  339. out << "\n\t";
  340. }
  341. /* Output one last number so we don't have to figure out when the last
  342. * entry is and avoid writing a comma. */
  343. out << 0 << "\n";
  344. return out;
  345. }
  346. std::ostream &FlatCodeGen::TRANS_TARGS()
  347. {
  348. /* Transitions must be written ordered by their id. */
  349. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  350. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  351. transPtrs[trans->id] = trans;
  352. /* Keep a count of the num of items in the array written. */
  353. out << '\t';
  354. int totalStates = 0;
  355. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  356. /* Save the position. Needed for eofTargs. */
  357. RedTransAp *trans = transPtrs[t];
  358. trans->pos = t;
  359. /* Write out the target state. */
  360. out << trans->targ->id;
  361. if ( t < redFsm->transSet.length()-1 ) {
  362. out << ", ";
  363. if ( ++totalStates % IALL == 0 )
  364. out << "\n\t";
  365. }
  366. }
  367. out << "\n";
  368. delete[] transPtrs;
  369. return out;
  370. }
  371. std::ostream &FlatCodeGen::TRANS_ACTIONS()
  372. {
  373. /* Transitions must be written ordered by their id. */
  374. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  375. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  376. transPtrs[trans->id] = trans;
  377. /* Keep a count of the num of items in the array written. */
  378. out << '\t';
  379. int totalAct = 0;
  380. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  381. /* Write the function for the transition. */
  382. RedTransAp *trans = transPtrs[t];
  383. TRANS_ACTION( trans );
  384. if ( t < redFsm->transSet.length()-1 ) {
  385. out << ", ";
  386. if ( ++totalAct % IALL == 0 )
  387. out << "\n\t";
  388. }
  389. }
  390. out << "\n";
  391. delete[] transPtrs;
  392. return out;
  393. }
  394. void FlatCodeGen::LOCATE_TRANS()
  395. {
  396. out <<
  397. " _keys = " << ARR_OFF( K(), "(" + vCS() + "<<1)" ) << ";\n"
  398. " _inds = " << ARR_OFF( I(), IO() + "[" + vCS() + "]" ) << ";\n"
  399. "\n"
  400. " _slen = " << SP() << "[" << vCS() << "];\n"
  401. " _trans = _inds[ _slen > 0 && _keys[0] <=" << GET_WIDE_KEY() << " &&\n"
  402. " " << GET_WIDE_KEY() << " <= _keys[1] ?\n"
  403. " " << GET_WIDE_KEY() << " - _keys[0] : _slen ];\n"
  404. "\n";
  405. }
  406. void FlatCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
  407. {
  408. ret << "{";
  409. ret << vCS() << " = " << gotoDest << ";";
  410. if ( inFinish && !noEnd )
  411. EOF_CHECK( ret );
  412. ret << CTRL_FLOW() << "goto _again;";
  413. ret << "}";
  414. }
  415. void FlatCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  416. {
  417. ret << "{";
  418. ret << vCS() << " = (";
  419. INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
  420. ret << "); ";
  421. if ( inFinish && !noEnd )
  422. EOF_CHECK( ret );
  423. ret << CTRL_FLOW() << "goto _again;";
  424. ret << "}";
  425. }
  426. void FlatCodeGen::CURS( ostream &ret, bool inFinish )
  427. {
  428. ret << "(_ps)";
  429. }
  430. void FlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
  431. {
  432. ret << "(" << vCS() << ")";
  433. }
  434. void FlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
  435. {
  436. ret << vCS() << " = " << nextDest << ";";
  437. }
  438. void FlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  439. {
  440. ret << vCS() << " = (";
  441. INLINE_LIST( ret, ilItem->children, 0, inFinish, false );
  442. ret << ");";
  443. }
  444. void FlatCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
  445. {
  446. if ( prePushExpr != 0 ) {
  447. ret << "{";
  448. INLINE_LIST( ret, prePushExpr, 0, false, false );
  449. }
  450. ret << "{";
  451. ret << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = " << callDest << ";";
  452. if ( inFinish && !noEnd )
  453. EOF_CHECK( ret );
  454. ret << CTRL_FLOW() << "goto _again;";
  455. ret << "}";
  456. if ( prePushExpr != 0 )
  457. ret << "}";
  458. }
  459. void FlatCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
  460. {
  461. if ( prePushExpr != 0 ) {
  462. ret << "{";
  463. INLINE_LIST( ret, prePushExpr, 0, false, false );
  464. }
  465. ret << "{" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = (";
  466. INLINE_LIST( ret, ilItem->children, targState, inFinish, false );
  467. ret << ");";
  468. if ( inFinish && !noEnd )
  469. EOF_CHECK( ret );
  470. ret << CTRL_FLOW() << "goto _again;";
  471. ret << "}";
  472. if ( prePushExpr != 0 )
  473. ret << "}";
  474. }
  475. void FlatCodeGen::RET( ostream &ret, bool inFinish )
  476. {
  477. ret << "{" << vCS() << " = " << STACK() << "[--" << TOP() << "];";
  478. if ( postPopExpr != 0 ) {
  479. ret << "{";
  480. INLINE_LIST( ret, postPopExpr, 0, false, false );
  481. ret << "}";
  482. }
  483. if ( inFinish && !noEnd )
  484. EOF_CHECK( ret );
  485. ret << CTRL_FLOW() << "goto _again;";
  486. ret << "}";
  487. }
  488. void FlatCodeGen::BREAK( ostream &ret, int targState, bool csForced )
  489. {
  490. outLabelUsed = true;
  491. ret << "{" << P() << "++; " << CTRL_FLOW() << "goto _out; }";
  492. }
  493. void FlatCodeGen::writeData()
  494. {
  495. /* If there are any transtion functions then output the array. If there
  496. * are none, don't bother emitting an empty array that won't be used. */
  497. if ( redFsm->anyActions() ) {
  498. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
  499. ACTIONS_ARRAY();
  500. CLOSE_ARRAY() <<
  501. "\n";
  502. }
  503. if ( redFsm->anyConditions() ) {
  504. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  505. COND_KEYS();
  506. CLOSE_ARRAY() <<
  507. "\n";
  508. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
  509. COND_KEY_SPANS();
  510. CLOSE_ARRAY() <<
  511. "\n";
  512. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
  513. CONDS();
  514. CLOSE_ARRAY() <<
  515. "\n";
  516. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
  517. COND_INDEX_OFFSET();
  518. CLOSE_ARRAY() <<
  519. "\n";
  520. }
  521. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  522. KEYS();
  523. CLOSE_ARRAY() <<
  524. "\n";
  525. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
  526. KEY_SPANS();
  527. CLOSE_ARRAY() <<
  528. "\n";
  529. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
  530. FLAT_INDEX_OFFSET();
  531. CLOSE_ARRAY() <<
  532. "\n";
  533. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  534. INDICIES();
  535. CLOSE_ARRAY() <<
  536. "\n";
  537. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  538. TRANS_TARGS();
  539. CLOSE_ARRAY() <<
  540. "\n";
  541. if ( redFsm->anyActions() ) {
  542. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  543. TRANS_ACTIONS();
  544. CLOSE_ARRAY() <<
  545. "\n";
  546. }
  547. if ( redFsm->anyToStateActions() ) {
  548. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  549. TO_STATE_ACTIONS();
  550. CLOSE_ARRAY() <<
  551. "\n";
  552. }
  553. if ( redFsm->anyFromStateActions() ) {
  554. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  555. FROM_STATE_ACTIONS();
  556. CLOSE_ARRAY() <<
  557. "\n";
  558. }
  559. if ( redFsm->anyEofActions() ) {
  560. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  561. EOF_ACTIONS();
  562. CLOSE_ARRAY() <<
  563. "\n";
  564. }
  565. if ( redFsm->anyEofTrans() ) {
  566. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  567. EOF_TRANS();
  568. CLOSE_ARRAY() <<
  569. "\n";
  570. }
  571. STATE_IDS();
  572. }
  573. void FlatCodeGen::COND_TRANSLATE()
  574. {
  575. out <<
  576. " _widec = " << GET_KEY() << ";\n";
  577. out <<
  578. " _keys = " << ARR_OFF( CK(), "(" + vCS() + "<<1)" ) << ";\n"
  579. " _conds = " << ARR_OFF( C(), CO() + "[" + vCS() + "]" ) << ";\n"
  580. "\n"
  581. " _slen = " << CSP() << "[" << vCS() << "];\n"
  582. " _cond = _slen > 0 && _keys[0] <=" << GET_WIDE_KEY() << " &&\n"
  583. " " << GET_WIDE_KEY() << " <= _keys[1] ?\n"
  584. " _conds[" << GET_WIDE_KEY() << " - _keys[0]] : 0;\n"
  585. "\n";
  586. out <<
  587. " switch ( _cond ) {\n";
  588. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  589. GenCondSpace *condSpace = csi;
  590. out << " case " << condSpace->condSpaceId + 1 << ": {\n";
  591. out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
  592. KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
  593. " - " << KEY(keyOps->minKey) << "));\n";
  594. for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
  595. out << TABS(2) << "if ( ";
  596. CONDITION( out, *csi );
  597. Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
  598. out << " ) _widec += " << condValOffset << ";\n";
  599. }
  600. out << " }\n";
  601. out << " break;\n";
  602. }
  603. SWITCH_DEFAULT();
  604. out <<
  605. " }\n";
  606. }
  607. void FlatCodeGen::writeExec()
  608. {
  609. testEofUsed = false;
  610. outLabelUsed = false;
  611. out <<
  612. " {\n"
  613. " int _slen";
  614. if ( redFsm->anyRegCurStateRef() )
  615. out << ", _ps";
  616. out <<
  617. ";\n"
  618. " int _trans";
  619. if ( redFsm->anyConditions() )
  620. out << ", _cond";
  621. out << ";\n";
  622. if ( redFsm->anyToStateActions() ||
  623. redFsm->anyRegActions() || redFsm->anyFromStateActions() )
  624. {
  625. out <<
  626. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << PTR_CONST_END() << POINTER() << "_acts;\n"
  627. " " << UINT() << " _nacts;\n";
  628. }
  629. out <<
  630. " " << PTR_CONST() << WIDE_ALPH_TYPE() << PTR_CONST_END() << POINTER() << "_keys;\n"
  631. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxIndex) << PTR_CONST_END() << POINTER() << "_inds;\n";
  632. if ( redFsm->anyConditions() ) {
  633. out <<
  634. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxCond) << PTR_CONST_END() << POINTER() << "_conds;\n"
  635. " " << WIDE_ALPH_TYPE() << " _widec;\n";
  636. }
  637. out << "\n";
  638. if ( !noEnd ) {
  639. testEofUsed = true;
  640. out <<
  641. " if ( " << P() << " == " << PE() << " )\n"
  642. " goto _test_eof;\n";
  643. }
  644. if ( redFsm->errState != 0 ) {
  645. outLabelUsed = true;
  646. out <<
  647. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  648. " goto _out;\n";
  649. }
  650. out << "_resume:\n";
  651. if ( redFsm->anyFromStateActions() ) {
  652. out <<
  653. " _acts = " << ARR_OFF( A(), FSA() + "[" + vCS() + "]" ) << ";\n"
  654. " _nacts = " << CAST(UINT()) << " *_acts++;\n"
  655. " while ( _nacts-- > 0 ) {\n"
  656. " switch ( *_acts++ ) {\n";
  657. FROM_STATE_ACTION_SWITCH();
  658. SWITCH_DEFAULT() <<
  659. " }\n"
  660. " }\n"
  661. "\n";
  662. }
  663. if ( redFsm->anyConditions() )
  664. COND_TRANSLATE();
  665. LOCATE_TRANS();
  666. if ( redFsm->anyEofTrans() )
  667. out << "_eof_trans:\n";
  668. if ( redFsm->anyRegCurStateRef() )
  669. out << " _ps = " << vCS() << ";\n";
  670. out <<
  671. " " << vCS() << " = " << TT() << "[_trans];\n"
  672. "\n";
  673. if ( redFsm->anyRegActions() ) {
  674. out <<
  675. " if ( " << TA() << "[_trans] == 0 )\n"
  676. " goto _again;\n"
  677. "\n"
  678. " _acts = " << ARR_OFF( A(), TA() + "[_trans]" ) << ";\n"
  679. " _nacts = " << CAST(UINT()) << " *_acts++;\n"
  680. " while ( _nacts-- > 0 ) {\n"
  681. " switch ( *(_acts++) )\n {\n";
  682. ACTION_SWITCH();
  683. SWITCH_DEFAULT() <<
  684. " }\n"
  685. " }\n"
  686. "\n";
  687. }
  688. if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  689. redFsm->anyActionCalls() || redFsm->anyActionRets() )
  690. out << "_again:\n";
  691. if ( redFsm->anyToStateActions() ) {
  692. out <<
  693. " _acts = " << ARR_OFF( A(), TSA() + "[" + vCS() + "]" ) << ";\n"
  694. " _nacts = " << CAST(UINT()) << " *_acts++;\n"
  695. " while ( _nacts-- > 0 ) {\n"
  696. " switch ( *_acts++ ) {\n";
  697. TO_STATE_ACTION_SWITCH();
  698. SWITCH_DEFAULT() <<
  699. " }\n"
  700. " }\n"
  701. "\n";
  702. }
  703. if ( redFsm->errState != 0 ) {
  704. outLabelUsed = true;
  705. out <<
  706. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  707. " goto _out;\n";
  708. }
  709. if ( !noEnd ) {
  710. out <<
  711. " if ( ++" << P() << " != " << PE() << " )\n"
  712. " goto _resume;\n";
  713. }
  714. else {
  715. out <<
  716. " " << P() << " += 1;\n"
  717. " goto _resume;\n";
  718. }
  719. if ( testEofUsed )
  720. out << " _test_eof: {}\n";
  721. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  722. out <<
  723. " if ( " << P() << " == " << vEOF() << " )\n"
  724. " {\n";
  725. if ( redFsm->anyEofTrans() ) {
  726. out <<
  727. " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
  728. " _trans = " << ET() << "[" << vCS() << "] - 1;\n"
  729. " goto _eof_trans;\n"
  730. " }\n";
  731. }
  732. if ( redFsm->anyEofActions() ) {
  733. out <<
  734. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) << PTR_CONST_END() <<
  735. POINTER() << "__acts = " <<
  736. ARR_OFF( A(), EA() + "[" + vCS() + "]" ) << ";\n"
  737. " " << UINT() << " __nacts = " << CAST(UINT()) << " *__acts++;\n"
  738. " while ( __nacts-- > 0 ) {\n"
  739. " switch ( *__acts++ ) {\n";
  740. EOF_ACTION_SWITCH();
  741. SWITCH_DEFAULT() <<
  742. " }\n"
  743. " }\n";
  744. }
  745. out <<
  746. " }\n"
  747. "\n";
  748. }
  749. if ( outLabelUsed )
  750. out << " _out: {}\n";
  751. out << " }\n";
  752. }