csflat.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 "csflat.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. std::ostream &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 );
  68. out << "\tbreak;\n";
  69. }
  70. }
  71. genLineDirective( out );
  72. return out;
  73. }
  74. std::ostream &CSharpFlatCodeGen::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 );
  83. out << "\tbreak;\n";
  84. }
  85. }
  86. genLineDirective( out );
  87. return out;
  88. }
  89. std::ostream &CSharpFlatCodeGen::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 );
  98. out << "\tbreak;\n";
  99. }
  100. }
  101. genLineDirective( out );
  102. return out;
  103. }
  104. std::ostream &CSharpFlatCodeGen::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 );
  113. out << "\tbreak;\n";
  114. }
  115. }
  116. genLineDirective( out );
  117. return out;
  118. }
  119. std::ostream &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 &CSharpFlatCodeGen::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 << ALPHA_KEY( st->condLowKey ) << ", ";
  235. out << ALPHA_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. if ( keyOps->alphType->isChar )
  242. out << "(char) " << 0 << "\n";
  243. else
  244. out << 0 << "\n";
  245. return out;
  246. }
  247. std::ostream &CSharpFlatCodeGen::COND_KEY_SPANS()
  248. {
  249. out << "\t";
  250. int totalStateNum = 0;
  251. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  252. /* Write singles length. */
  253. unsigned long long span = 0;
  254. if ( st->condList != 0 )
  255. span = keyOps->span( st->condLowKey, st->condHighKey );
  256. out << span;
  257. if ( !st.last() ) {
  258. out << ", ";
  259. if ( ++totalStateNum % IALL == 0 )
  260. out << "\n\t";
  261. }
  262. }
  263. out << "\n";
  264. return out;
  265. }
  266. std::ostream &CSharpFlatCodeGen::CONDS()
  267. {
  268. int totalTrans = 0;
  269. out << '\t';
  270. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  271. if ( st->condList != 0 ) {
  272. /* Walk the singles. */
  273. unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
  274. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  275. if ( st->condList[pos] != 0 )
  276. out << st->condList[pos]->condSpaceId + 1 << ", ";
  277. else
  278. out << "0, ";
  279. if ( ++totalTrans % IALL == 0 )
  280. out << "\n\t";
  281. }
  282. }
  283. }
  284. /* Output one last number so we don't have to figure out when the last
  285. * entry is and avoid writing a comma. */
  286. out << 0 << "\n";
  287. return out;
  288. }
  289. std::ostream &CSharpFlatCodeGen::COND_INDEX_OFFSET()
  290. {
  291. out << "\t";
  292. int totalStateNum = 0, curIndOffset = 0;
  293. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  294. /* Write the index offset. */
  295. out << curIndOffset;
  296. if ( !st.last() ) {
  297. out << ", ";
  298. if ( ++totalStateNum % IALL == 0 )
  299. out << "\n\t";
  300. }
  301. /* Move the index offset ahead. */
  302. if ( st->condList != 0 )
  303. curIndOffset += keyOps->span( st->condLowKey, st->condHighKey );
  304. }
  305. out << "\n";
  306. return out;
  307. }
  308. std::ostream &CSharpFlatCodeGen::KEYS()
  309. {
  310. out << '\t';
  311. int totalTrans = 0;
  312. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  313. /* Emit just low key and high key. */
  314. out << ALPHA_KEY( st->lowKey ) << ", ";
  315. out << ALPHA_KEY( st->highKey ) << ", ";
  316. if ( ++totalTrans % IALL == 0 )
  317. out << "\n\t";
  318. }
  319. /* Output one last number so we don't have to figure out when the last
  320. * entry is and avoid writing a comma. */
  321. if ( keyOps->alphType->isChar )
  322. out << "(char) " << 0 << "\n";
  323. else
  324. out << 0 << "\n";
  325. return out;
  326. }
  327. std::ostream &CSharpFlatCodeGen::INDICIES()
  328. {
  329. int totalTrans = 0;
  330. out << '\t';
  331. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  332. if ( st->transList != 0 ) {
  333. /* Walk the singles. */
  334. unsigned long long span = keyOps->span( st->lowKey, st->highKey );
  335. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  336. out << st->transList[pos]->id << ", ";
  337. if ( ++totalTrans % IALL == 0 )
  338. out << "\n\t";
  339. }
  340. }
  341. /* The state's default index goes next. */
  342. if ( st->defTrans != 0 )
  343. out << st->defTrans->id << ", ";
  344. if ( ++totalTrans % IALL == 0 )
  345. out << "\n\t";
  346. }
  347. /* Output one last number so we don't have to figure out when the last
  348. * entry is and avoid writing a comma. */
  349. out << 0 << "\n";
  350. return out;
  351. }
  352. std::ostream &CSharpFlatCodeGen::TRANS_TARGS()
  353. {
  354. /* Transitions must be written ordered by their id. */
  355. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  356. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  357. transPtrs[trans->id] = trans;
  358. /* Keep a count of the num of items in the array written. */
  359. out << '\t';
  360. int totalStates = 0;
  361. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  362. /* Record the position, need this for eofTrans. */
  363. RedTransAp *trans = transPtrs[t];
  364. trans->pos = t;
  365. /* Write out the target state. */
  366. out << trans->targ->id;
  367. if ( t < redFsm->transSet.length()-1 ) {
  368. out << ", ";
  369. if ( ++totalStates % IALL == 0 )
  370. out << "\n\t";
  371. }
  372. }
  373. out << "\n";
  374. delete[] transPtrs;
  375. return out;
  376. }
  377. std::ostream &CSharpFlatCodeGen::TRANS_ACTIONS()
  378. {
  379. /* Transitions must be written ordered by their id. */
  380. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  381. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  382. transPtrs[trans->id] = trans;
  383. /* Keep a count of the num of items in the array written. */
  384. out << '\t';
  385. int totalAct = 0;
  386. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  387. /* Write the function for the transition. */
  388. RedTransAp *trans = transPtrs[t];
  389. TRANS_ACTION( trans );
  390. if ( t < redFsm->transSet.length()-1 ) {
  391. out << ", ";
  392. if ( ++totalAct % IALL == 0 )
  393. out << "\n\t";
  394. }
  395. }
  396. out << "\n";
  397. delete[] transPtrs;
  398. return out;
  399. }
  400. void CSharpFlatCodeGen::LOCATE_TRANS()
  401. {
  402. out <<
  403. " _keys = " << vCS() << "<<1;\n"
  404. " _inds = " << IO() << "[" << vCS() << "];\n"
  405. "\n"
  406. " _slen = " << SP() << "[" << vCS() << "];\n"
  407. " _trans = " << I() << "[_inds + (\n"
  408. " _slen > 0 && " << K() << "[_keys] <=" << GET_WIDE_KEY() << " &&\n"
  409. " " << GET_WIDE_KEY() << " <= " << K() <<"[_keys+1] ?\n"
  410. " " << GET_WIDE_KEY() << " - " << K() << "[_keys] : _slen ) ];\n"
  411. "\n";
  412. }
  413. void CSharpFlatCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
  414. {
  415. ret << "{" << vCS() << " = " << gotoDest << "; " <<
  416. CTRL_FLOW() << "goto _again;}";
  417. }
  418. void CSharpFlatCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  419. {
  420. ret << "{" << vCS() << " = (";
  421. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  422. ret << "); " << CTRL_FLOW() << "goto _again;}";
  423. }
  424. void CSharpFlatCodeGen::CURS( ostream &ret, bool inFinish )
  425. {
  426. ret << "(_ps)";
  427. }
  428. void CSharpFlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
  429. {
  430. ret << "(" << vCS() << ")";
  431. }
  432. void CSharpFlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
  433. {
  434. ret << vCS() << " = " << nextDest << ";";
  435. }
  436. void CSharpFlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  437. {
  438. ret << vCS() << " = (";
  439. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  440. ret << ");";
  441. }
  442. void CSharpFlatCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
  443. {
  444. if ( prePushExpr != 0 ) {
  445. ret << "{";
  446. INLINE_LIST( ret, prePushExpr, 0, false );
  447. }
  448. ret << "{" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = " <<
  449. callDest << "; " << CTRL_FLOW() << "goto _again;}";
  450. if ( prePushExpr != 0 )
  451. ret << "}";
  452. }
  453. void CSharpFlatCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
  454. {
  455. if ( prePushExpr != 0 ) {
  456. ret << "{";
  457. INLINE_LIST( ret, prePushExpr, 0, false );
  458. }
  459. ret << "{" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = (";
  460. INLINE_LIST( ret, ilItem->children, targState, inFinish );
  461. ret << "); " << CTRL_FLOW() << "goto _again;}";
  462. if ( prePushExpr != 0 )
  463. ret << "}";
  464. }
  465. void CSharpFlatCodeGen::RET( ostream &ret, bool inFinish )
  466. {
  467. ret << "{" << vCS() << " = " << STACK() << "[--" << TOP() << "];";
  468. if ( postPopExpr != 0 ) {
  469. ret << "{";
  470. INLINE_LIST( ret, postPopExpr, 0, false );
  471. ret << "}";
  472. }
  473. ret << CTRL_FLOW() << "goto _again;}";
  474. }
  475. void CSharpFlatCodeGen::BREAK( ostream &ret, int targState )
  476. {
  477. outLabelUsed = true;
  478. ret << "{" << P() << "++; " << CTRL_FLOW() << "goto _out; }";
  479. }
  480. void CSharpFlatCodeGen::writeData()
  481. {
  482. /* If there are any transtion functions then output the array. If there
  483. * are none, don't bother emitting an empty array that won't be used. */
  484. if ( redFsm->anyActions() ) {
  485. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
  486. ACTIONS_ARRAY();
  487. CLOSE_ARRAY() <<
  488. "\n";
  489. }
  490. if ( redFsm->anyConditions() ) {
  491. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  492. COND_KEYS();
  493. CLOSE_ARRAY() <<
  494. "\n";
  495. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
  496. COND_KEY_SPANS();
  497. CLOSE_ARRAY() <<
  498. "\n";
  499. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
  500. CONDS();
  501. CLOSE_ARRAY() <<
  502. "\n";
  503. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
  504. COND_INDEX_OFFSET();
  505. CLOSE_ARRAY() <<
  506. "\n";
  507. }
  508. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  509. KEYS();
  510. CLOSE_ARRAY() <<
  511. "\n";
  512. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
  513. KEY_SPANS();
  514. CLOSE_ARRAY() <<
  515. "\n";
  516. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
  517. FLAT_INDEX_OFFSET();
  518. CLOSE_ARRAY() <<
  519. "\n";
  520. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  521. INDICIES();
  522. CLOSE_ARRAY() <<
  523. "\n";
  524. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  525. TRANS_TARGS();
  526. CLOSE_ARRAY() <<
  527. "\n";
  528. if ( redFsm->anyActions() ) {
  529. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  530. TRANS_ACTIONS();
  531. CLOSE_ARRAY() <<
  532. "\n";
  533. }
  534. if ( redFsm->anyToStateActions() ) {
  535. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  536. TO_STATE_ACTIONS();
  537. CLOSE_ARRAY() <<
  538. "\n";
  539. }
  540. if ( redFsm->anyFromStateActions() ) {
  541. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  542. FROM_STATE_ACTIONS();
  543. CLOSE_ARRAY() <<
  544. "\n";
  545. }
  546. if ( redFsm->anyEofActions() ) {
  547. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  548. EOF_ACTIONS();
  549. CLOSE_ARRAY() <<
  550. "\n";
  551. }
  552. if ( redFsm->anyEofTrans() ) {
  553. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  554. EOF_TRANS();
  555. CLOSE_ARRAY() <<
  556. "\n";
  557. }
  558. STATE_IDS();
  559. }
  560. void CSharpFlatCodeGen::COND_TRANSLATE()
  561. {
  562. out <<
  563. " _widec = " << GET_KEY() << ";\n";
  564. out <<
  565. " _keys = " << vCS() << "<<1;\n"
  566. " _conds = " << CO() << "[" << vCS() << "];\n"
  567. // " _keys = " << ARR_OFF( CK(), "(" + vCS() + "<<1)" ) << ";\n"
  568. // " _conds = " << ARR_OFF( C(), CO() + "[" + vCS() + "]" ) << ";\n"
  569. "\n"
  570. " _slen = " << CSP() << "[" << vCS() << "];\n"
  571. " if (_slen > 0 && " << CK() << "[_keys] <="
  572. << GET_WIDE_KEY() << " &&\n"
  573. " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys+1])\n"
  574. " _cond = " << C() << "[_conds+" << GET_WIDE_KEY() << " - " <<
  575. CK() << "[_keys]];\n"
  576. " else\n"
  577. " _cond = 0;"
  578. "\n";
  579. /* XXX This version of the code doesn't work because Mono is weird. Works
  580. * fine in Microsoft's csc, even though the bug report filed claimed it
  581. * didn't.
  582. " _slen = " << CSP() << "[" << vCS() << "];\n"
  583. " _cond = _slen > 0 && " << CK() << "[_keys] <="
  584. << GET_WIDE_KEY() << " &&\n"
  585. " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys+1] ?\n"
  586. " " << C() << "[_conds+" << GET_WIDE_KEY() << " - " << CK()
  587. << "[_keys]] : 0;\n"
  588. "\n";
  589. */
  590. out <<
  591. " switch ( _cond ) {\n";
  592. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  593. GenCondSpace *condSpace = csi;
  594. out << " case " << condSpace->condSpaceId + 1 << ": {\n";
  595. out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
  596. KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
  597. " - " << KEY(keyOps->minKey) << "));\n";
  598. for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
  599. out << TABS(2) << "if ( ";
  600. CONDITION( out, *csi );
  601. Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
  602. out << " ) _widec += " << condValOffset << ";\n";
  603. }
  604. out << " }\n";
  605. out << " break;\n";
  606. }
  607. SWITCH_DEFAULT();
  608. out <<
  609. " }\n";
  610. }
  611. void CSharpFlatCodeGen::writeExec()
  612. {
  613. testEofUsed = false;
  614. outLabelUsed = false;
  615. initVarTypes();
  616. out <<
  617. " {\n"
  618. " " << slenType << " _slen";
  619. if ( redFsm->anyRegCurStateRef() )
  620. out << ", _ps";
  621. out <<
  622. ";\n"
  623. " " << transType << " _trans";
  624. if ( redFsm->anyConditions() )
  625. out << ", _cond";
  626. out << ";\n";
  627. if ( redFsm->anyToStateActions() ||
  628. redFsm->anyRegActions() || redFsm->anyFromStateActions() )
  629. {
  630. out <<
  631. " int _acts;\n"
  632. " int _nacts;\n";
  633. }
  634. out <<
  635. " " << "int _keys;\n"
  636. " " << indsType << " _inds;\n";
  637. /*
  638. " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
  639. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxIndex) << POINTER() << "_inds;\n";*/
  640. if ( redFsm->anyConditions() ) {
  641. out <<
  642. " " << condsType << " _conds;\n"
  643. " " << WIDE_ALPH_TYPE() << " _widec;\n";
  644. }
  645. out << "\n";
  646. if ( !noEnd ) {
  647. testEofUsed = true;
  648. out <<
  649. " if ( " << P() << " == " << PE() << " )\n"
  650. " goto _test_eof;\n";
  651. }
  652. if ( redFsm->errState != 0 ) {
  653. outLabelUsed = true;
  654. out <<
  655. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  656. " goto _out;\n";
  657. }
  658. out << "_resume:\n";
  659. if ( redFsm->anyFromStateActions() ) {
  660. out <<
  661. " _acts = " << FSA() << "[" << vCS() << "];\n"
  662. " _nacts = " << A() << "[_acts++];\n"
  663. " while ( _nacts-- > 0 ) {\n"
  664. " switch ( " << A() << "[_acts++] ) {\n";
  665. FROM_STATE_ACTION_SWITCH();
  666. SWITCH_DEFAULT() <<
  667. " }\n"
  668. " }\n"
  669. "\n";
  670. }
  671. if ( redFsm->anyConditions() )
  672. COND_TRANSLATE();
  673. LOCATE_TRANS();
  674. if ( redFsm->anyEofTrans() )
  675. out << "_eof_trans:\n";
  676. if ( redFsm->anyRegCurStateRef() )
  677. out << " _ps = " << vCS() << ";\n";
  678. out <<
  679. " " << vCS() << " = " << TT() << "[_trans];\n"
  680. "\n";
  681. if ( redFsm->anyRegActions() ) {
  682. out <<
  683. " if ( " << TA() << "[_trans] == 0 )\n"
  684. " goto _again;\n"
  685. "\n"
  686. " _acts = " << TA() << "[_trans];\n"
  687. " _nacts = " << A() << "[_acts++];\n"
  688. " while ( _nacts-- > 0 ) {\n"
  689. " switch ( " << A() << "[_acts++] )\n {\n";
  690. ACTION_SWITCH();
  691. SWITCH_DEFAULT() <<
  692. " }\n"
  693. " }\n"
  694. "\n";
  695. }
  696. if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  697. redFsm->anyActionCalls() || redFsm->anyActionRets() )
  698. out << "_again:\n";
  699. if ( redFsm->anyToStateActions() ) {
  700. out <<
  701. " _acts = " << TSA() << "[" << vCS() << "];\n"
  702. " _nacts = " << A() << "[_acts++];\n"
  703. " while ( _nacts-- > 0 ) {\n"
  704. " switch ( " << A() << "[_acts++] ) {\n";
  705. TO_STATE_ACTION_SWITCH();
  706. SWITCH_DEFAULT() <<
  707. " }\n"
  708. " }\n"
  709. "\n";
  710. }
  711. if ( redFsm->errState != 0 ) {
  712. outLabelUsed = true;
  713. out <<
  714. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  715. " goto _out;\n";
  716. }
  717. if ( !noEnd ) {
  718. out <<
  719. " if ( ++" << P() << " != " << PE() << " )\n"
  720. " goto _resume;\n";
  721. }
  722. else {
  723. out <<
  724. " " << P() << " += 1;\n"
  725. " goto _resume;\n";
  726. }
  727. if ( testEofUsed )
  728. out << " _test_eof: {}\n";
  729. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  730. out <<
  731. " if ( " << P() << " == " << vEOF() << " )\n"
  732. " {\n";
  733. if ( redFsm->anyEofTrans() ) {
  734. out <<
  735. " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
  736. " _trans = " << CAST(transType) << " (" << ET() <<
  737. "[" << vCS() << "] - 1);\n"
  738. " goto _eof_trans;\n"
  739. " }\n";
  740. }
  741. if ( redFsm->anyEofActions() ) {
  742. out <<
  743. " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxActArrItem) <<
  744. POINTER() << "__acts = " <<
  745. EA() << "[" << vCS() << "];\n"
  746. " " << UINT() << " __nacts = " << CAST(UINT()) << " " <<
  747. A() << "[__acts++];\n"
  748. " while ( __nacts-- > 0 ) {\n"
  749. " switch ( " << A() << "[__acts++] ) {\n";
  750. EOF_ACTION_SWITCH();
  751. SWITCH_DEFAULT() <<
  752. " }\n"
  753. " }\n";
  754. }
  755. out <<
  756. " }\n"
  757. "\n";
  758. }
  759. if ( outLabelUsed )
  760. out << " _out: {}\n";
  761. out << " }\n";
  762. }
  763. void CSharpFlatCodeGen::initVarTypes()
  764. {
  765. slenType = ARRAY_TYPE(MAX(redFsm->maxSpan, redFsm->maxCondSpan));
  766. transType = ARRAY_TYPE(redFsm->maxIndex+1);
  767. indsType = ARRAY_TYPE(redFsm->maxFlatIndexOffset);
  768. condsType = ARRAY_TYPE(redFsm->maxCondIndexOffset);
  769. }