cstable.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 "cstable.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. /* Determine if we should use indicies or not. */
  27. void CSharpTabCodeGen::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->maxActionLoc) * 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->maxActionLoc) * totalIndex;
  46. }
  47. /* If using indicies reduces the size, use them. */
  48. useIndicies = sizeWithInds < sizeWithoutInds;
  49. }
  50. std::ostream &CSharpTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
  51. {
  52. int act = 0;
  53. if ( state->toStateAction != 0 )
  54. act = state->toStateAction->location+1;
  55. out << act;
  56. return out;
  57. }
  58. std::ostream &CSharpTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  59. {
  60. int act = 0;
  61. if ( state->fromStateAction != 0 )
  62. act = state->fromStateAction->location+1;
  63. out << act;
  64. return out;
  65. }
  66. std::ostream &CSharpTabCodeGen::EOF_ACTION( RedStateAp *state )
  67. {
  68. int act = 0;
  69. if ( state->eofAction != 0 )
  70. act = state->eofAction->location+1;
  71. out << act;
  72. return out;
  73. }
  74. std::ostream &CSharpTabCodeGen::TRANS_ACTION( RedTransAp *trans )
  75. {
  76. /* If there are actions, emit them. Otherwise emit zero. */
  77. int act = 0;
  78. if ( trans->action != 0 )
  79. act = trans->action->location+1;
  80. out << act;
  81. return out;
  82. }
  83. std::ostream &CSharpTabCodeGen::TO_STATE_ACTION_SWITCH()
  84. {
  85. /* Walk the list of functions, printing the cases. */
  86. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  87. /* Write out referenced actions. */
  88. if ( act->numToStateRefs > 0 ) {
  89. /* Write the case label, the action and the case break. */
  90. out << "\tcase " << act->actionId << ":\n";
  91. ACTION( out, act, 0, false );
  92. out << "\tbreak;\n";
  93. }
  94. }
  95. genLineDirective( out );
  96. return out;
  97. }
  98. std::ostream &CSharpTabCodeGen::FROM_STATE_ACTION_SWITCH()
  99. {
  100. /* Walk the list of functions, printing the cases. */
  101. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  102. /* Write out referenced actions. */
  103. if ( act->numFromStateRefs > 0 ) {
  104. /* Write the case label, the action and the case break. */
  105. out << "\tcase " << act->actionId << ":\n";
  106. ACTION( out, act, 0, false );
  107. out << "\tbreak;\n";
  108. }
  109. }
  110. genLineDirective( out );
  111. return out;
  112. }
  113. std::ostream &CSharpTabCodeGen::EOF_ACTION_SWITCH()
  114. {
  115. /* Walk the list of functions, printing the cases. */
  116. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  117. /* Write out referenced actions. */
  118. if ( act->numEofRefs > 0 ) {
  119. /* Write the case label, the action and the case break. */
  120. out << "\tcase " << act->actionId << ":\n";
  121. ACTION( out, act, 0, true );
  122. out << "\tbreak;\n";
  123. }
  124. }
  125. genLineDirective( out );
  126. return out;
  127. }
  128. std::ostream &CSharpTabCodeGen::ACTION_SWITCH()
  129. {
  130. /* Walk the list of functions, printing the cases. */
  131. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  132. /* Write out referenced actions. */
  133. if ( act->numTransRefs > 0 ) {
  134. /* Write the case label, the action and the case break. */
  135. out << "\tcase " << act->actionId << ":\n";
  136. ACTION( out, act, 0, false );
  137. out << "\tbreak;\n";
  138. }
  139. }
  140. genLineDirective( out );
  141. return out;
  142. }
  143. std::ostream &CSharpTabCodeGen::COND_OFFSETS()
  144. {
  145. out << "\t";
  146. int totalStateNum = 0, curKeyOffset = 0;
  147. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  148. /* Write the key offset. */
  149. out << curKeyOffset;
  150. if ( !st.last() ) {
  151. out << ", ";
  152. if ( ++totalStateNum % IALL == 0 )
  153. out << "\n\t";
  154. }
  155. /* Move the key offset ahead. */
  156. curKeyOffset += st->stateCondList.length();
  157. }
  158. out << "\n";
  159. return out;
  160. }
  161. std::ostream &CSharpTabCodeGen::KEY_OFFSETS()
  162. {
  163. out << "\t";
  164. int totalStateNum = 0, curKeyOffset = 0;
  165. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  166. /* Write the key offset. */
  167. out << curKeyOffset;
  168. if ( !st.last() ) {
  169. out << ", ";
  170. if ( ++totalStateNum % IALL == 0 )
  171. out << "\n\t";
  172. }
  173. /* Move the key offset ahead. */
  174. curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
  175. }
  176. out << "\n";
  177. return out;
  178. }
  179. std::ostream &CSharpTabCodeGen::INDEX_OFFSETS()
  180. {
  181. out << "\t";
  182. int totalStateNum = 0, curIndOffset = 0;
  183. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  184. /* Write the index offset. */
  185. out << curIndOffset;
  186. if ( !st.last() ) {
  187. out << ", ";
  188. if ( ++totalStateNum % IALL == 0 )
  189. out << "\n\t";
  190. }
  191. /* Move the index offset ahead. */
  192. curIndOffset += st->outSingle.length() + st->outRange.length();
  193. if ( st->defTrans != 0 )
  194. curIndOffset += 1;
  195. }
  196. out << "\n";
  197. return out;
  198. }
  199. std::ostream &CSharpTabCodeGen::COND_LENS()
  200. {
  201. out << "\t";
  202. int totalStateNum = 0;
  203. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  204. /* Write singles length. */
  205. out << st->stateCondList.length();
  206. if ( !st.last() ) {
  207. out << ", ";
  208. if ( ++totalStateNum % IALL == 0 )
  209. out << "\n\t";
  210. }
  211. }
  212. out << "\n";
  213. return out;
  214. }
  215. std::ostream &CSharpTabCodeGen::SINGLE_LENS()
  216. {
  217. out << "\t";
  218. int totalStateNum = 0;
  219. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  220. /* Write singles length. */
  221. out << st->outSingle.length();
  222. if ( !st.last() ) {
  223. out << ", ";
  224. if ( ++totalStateNum % IALL == 0 )
  225. out << "\n\t";
  226. }
  227. }
  228. out << "\n";
  229. return out;
  230. }
  231. std::ostream &CSharpTabCodeGen::RANGE_LENS()
  232. {
  233. out << "\t";
  234. int totalStateNum = 0;
  235. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  236. /* Emit length of range index. */
  237. out << st->outRange.length();
  238. if ( !st.last() ) {
  239. out << ", ";
  240. if ( ++totalStateNum % IALL == 0 )
  241. out << "\n\t";
  242. }
  243. }
  244. out << "\n";
  245. return out;
  246. }
  247. std::ostream &CSharpTabCodeGen::TO_STATE_ACTIONS()
  248. {
  249. out << "\t";
  250. int totalStateNum = 0;
  251. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  252. /* Write any eof action. */
  253. TO_STATE_ACTION(st);
  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 &CSharpTabCodeGen::FROM_STATE_ACTIONS()
  264. {
  265. out << "\t";
  266. int totalStateNum = 0;
  267. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  268. /* Write any eof action. */
  269. FROM_STATE_ACTION(st);
  270. if ( !st.last() ) {
  271. out << ", ";
  272. if ( ++totalStateNum % IALL == 0 )
  273. out << "\n\t";
  274. }
  275. }
  276. out << "\n";
  277. return out;
  278. }
  279. std::ostream &CSharpTabCodeGen::EOF_ACTIONS()
  280. {
  281. out << "\t";
  282. int totalStateNum = 0;
  283. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  284. /* Write any eof action. */
  285. EOF_ACTION(st);
  286. if ( !st.last() ) {
  287. out << ", ";
  288. if ( ++totalStateNum % IALL == 0 )
  289. out << "\n\t";
  290. }
  291. }
  292. out << "\n";
  293. return out;
  294. }
  295. std::ostream &CSharpTabCodeGen::EOF_TRANS()
  296. {
  297. out << "\t";
  298. int totalStateNum = 0;
  299. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  300. /* Write any eof action. */
  301. long trans = 0;
  302. if ( st->eofTrans != 0 ) {
  303. assert( st->eofTrans->pos >= 0 );
  304. trans = st->eofTrans->pos+1;
  305. }
  306. out << trans;
  307. if ( !st.last() ) {
  308. out << ", ";
  309. if ( ++totalStateNum % IALL == 0 )
  310. out << "\n\t";
  311. }
  312. }
  313. out << "\n";
  314. return out;
  315. }
  316. std::ostream &CSharpTabCodeGen::COND_KEYS()
  317. {
  318. out << '\t';
  319. int totalTrans = 0;
  320. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  321. /* Loop the state's transitions. */
  322. for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
  323. /* Lower key. */
  324. out << ALPHA_KEY( sc->lowKey ) << ", ";
  325. if ( ++totalTrans % IALL == 0 )
  326. out << "\n\t";
  327. /* Upper key. */
  328. out << ALPHA_KEY( sc->highKey ) << ", ";
  329. if ( ++totalTrans % IALL == 0 )
  330. out << "\n\t";
  331. }
  332. }
  333. /* Output one last number so we don't have to figure out when the last
  334. * entry is and avoid writing a comma. */
  335. if ( keyOps->alphType->isChar )
  336. out << "(char) " << 0 << "\n";
  337. else
  338. out << 0 << "\n";
  339. return out;
  340. }
  341. std::ostream &CSharpTabCodeGen::COND_SPACES()
  342. {
  343. out << '\t';
  344. int totalTrans = 0;
  345. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  346. /* Loop the state's transitions. */
  347. for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
  348. /* Cond Space id. */
  349. out << sc->condSpace->condSpaceId << ", ";
  350. if ( ++totalTrans % IALL == 0 )
  351. out << "\n\t";
  352. }
  353. }
  354. /* Output one last number so we don't have to figure out when the last
  355. * entry is and avoid writing a comma. */
  356. out << 0 << "\n";
  357. return out;
  358. }
  359. std::ostream &CSharpTabCodeGen::KEYS()
  360. {
  361. out << '\t';
  362. int totalTrans = 0;
  363. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  364. /* Loop the singles. */
  365. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  366. out << ALPHA_KEY( stel->lowKey ) << ", ";
  367. if ( ++totalTrans % IALL == 0 )
  368. out << "\n\t";
  369. }
  370. /* Loop the state's transitions. */
  371. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  372. /* Lower key. */
  373. out << ALPHA_KEY( rtel->lowKey ) << ", ";
  374. if ( ++totalTrans % IALL == 0 )
  375. out << "\n\t";
  376. /* Upper key. */
  377. out << ALPHA_KEY( rtel->highKey ) << ", ";
  378. if ( ++totalTrans % IALL == 0 )
  379. out << "\n\t";
  380. }
  381. }
  382. /* Output one last number so we don't have to figure out when the last
  383. * entry is and avoid writing a comma. */
  384. if ( keyOps->alphType->isChar )
  385. out << "(char) " << 0 << "\n";
  386. else
  387. out << 0 << "\n";
  388. return out;
  389. }
  390. std::ostream &CSharpTabCodeGen::INDICIES()
  391. {
  392. int totalTrans = 0;
  393. out << '\t';
  394. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  395. /* Walk the singles. */
  396. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  397. out << stel->value->id << ", ";
  398. if ( ++totalTrans % IALL == 0 )
  399. out << "\n\t";
  400. }
  401. /* Walk the ranges. */
  402. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  403. out << rtel->value->id << ", ";
  404. if ( ++totalTrans % IALL == 0 )
  405. out << "\n\t";
  406. }
  407. /* The state's default index goes next. */
  408. if ( st->defTrans != 0 ) {
  409. out << st->defTrans->id << ", ";
  410. if ( ++totalTrans % IALL == 0 )
  411. out << "\n\t";
  412. }
  413. }
  414. /* Output one last number so we don't have to figure out when the last
  415. * entry is and avoid writing a comma. */
  416. out << 0 << "\n";
  417. return out;
  418. }
  419. std::ostream &CSharpTabCodeGen::TRANS_TARGS()
  420. {
  421. int totalTrans = 0;
  422. out << '\t';
  423. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  424. /* Walk the singles. */
  425. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  426. RedTransAp *trans = stel->value;
  427. out << trans->targ->id << ", ";
  428. if ( ++totalTrans % IALL == 0 )
  429. out << "\n\t";
  430. }
  431. /* Walk the ranges. */
  432. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  433. RedTransAp *trans = rtel->value;
  434. out << trans->targ->id << ", ";
  435. if ( ++totalTrans % IALL == 0 )
  436. out << "\n\t";
  437. }
  438. /* The state's default target state. */
  439. if ( st->defTrans != 0 ) {
  440. RedTransAp *trans = st->defTrans;
  441. out << trans->targ->id << ", ";
  442. if ( ++totalTrans % IALL == 0 )
  443. out << "\n\t";
  444. }
  445. }
  446. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  447. if ( st->eofTrans != 0 ) {
  448. RedTransAp *trans = st->eofTrans;
  449. trans->pos = totalTrans;
  450. out << trans->targ->id << ", ";
  451. if ( ++totalTrans % IALL == 0 )
  452. out << "\n\t";
  453. }
  454. }
  455. /* Output one last number so we don't have to figure out when the last
  456. * entry is and avoid writing a comma. */
  457. out << 0 << "\n";
  458. return out;
  459. }
  460. std::ostream &CSharpTabCodeGen::TRANS_ACTIONS()
  461. {
  462. int totalTrans = 0;
  463. out << '\t';
  464. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  465. /* Walk the singles. */
  466. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  467. RedTransAp *trans = stel->value;
  468. TRANS_ACTION( trans ) << ", ";
  469. if ( ++totalTrans % IALL == 0 )
  470. out << "\n\t";
  471. }
  472. /* Walk the ranges. */
  473. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  474. RedTransAp *trans = rtel->value;
  475. TRANS_ACTION( trans ) << ", ";
  476. if ( ++totalTrans % IALL == 0 )
  477. out << "\n\t";
  478. }
  479. /* The state's default index goes next. */
  480. if ( st->defTrans != 0 ) {
  481. RedTransAp *trans = st->defTrans;
  482. TRANS_ACTION( trans ) << ", ";
  483. if ( ++totalTrans % IALL == 0 )
  484. out << "\n\t";
  485. }
  486. }
  487. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  488. if ( st->eofTrans != 0 ) {
  489. RedTransAp *trans = st->eofTrans;
  490. TRANS_ACTION( trans ) << ", ";
  491. if ( ++totalTrans % IALL == 0 )
  492. out << "\n\t";
  493. }
  494. }
  495. /* Output one last number so we don't have to figure out when the last
  496. * entry is and avoid writing a comma. */
  497. out << 0 << "\n";
  498. return out;
  499. }
  500. std::ostream &CSharpTabCodeGen::TRANS_TARGS_WI()
  501. {
  502. /* Transitions must be written ordered by their id. */
  503. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  504. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  505. transPtrs[trans->id] = trans;
  506. /* Keep a count of the num of items in the array written. */
  507. out << '\t';
  508. int totalStates = 0;
  509. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  510. /* Record the position, need this for eofTrans. */
  511. RedTransAp *trans = transPtrs[t];
  512. trans->pos = t;
  513. /* Write out the target state. */
  514. out << trans->targ->id;
  515. if ( t < redFsm->transSet.length()-1 ) {
  516. out << ", ";
  517. if ( ++totalStates % IALL == 0 )
  518. out << "\n\t";
  519. }
  520. }
  521. out << "\n";
  522. delete[] transPtrs;
  523. return out;
  524. }
  525. std::ostream &CSharpTabCodeGen::TRANS_ACTIONS_WI()
  526. {
  527. /* Transitions must be written ordered by their id. */
  528. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  529. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  530. transPtrs[trans->id] = trans;
  531. /* Keep a count of the num of items in the array written. */
  532. out << '\t';
  533. int totalAct = 0;
  534. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  535. /* Write the function for the transition. */
  536. RedTransAp *trans = transPtrs[t];
  537. TRANS_ACTION( trans );
  538. if ( t < redFsm->transSet.length()-1 ) {
  539. out << ", ";
  540. if ( ++totalAct % IALL == 0 )
  541. out << "\n\t";
  542. }
  543. }
  544. out << "\n";
  545. delete[] transPtrs;
  546. return out;
  547. }
  548. void CSharpTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
  549. {
  550. ret << "{" << vCS() << " = " << gotoDest << ";";
  551. ret << CTRL_FLOW() << "goto _again;";
  552. ret << "}";
  553. }
  554. void CSharpTabCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  555. {
  556. ret << "{" << vCS() << " = (";
  557. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  558. ret << ");";
  559. ret << CTRL_FLOW() << "goto _again;";
  560. ret << "}";
  561. }
  562. void CSharpTabCodeGen::CURS( ostream &ret, bool inFinish )
  563. {
  564. ret << "(_ps)";
  565. }
  566. void CSharpTabCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
  567. {
  568. ret << "(" << vCS() << ")";
  569. }
  570. void CSharpTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
  571. {
  572. ret << vCS() << " = " << nextDest << ";";
  573. }
  574. void CSharpTabCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  575. {
  576. ret << vCS() << " = (";
  577. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  578. ret << ");";
  579. }
  580. void CSharpTabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
  581. {
  582. if ( prePushExpr != 0 ) {
  583. ret << "{";
  584. INLINE_LIST( ret, prePushExpr, 0, false );
  585. }
  586. ret << "{";
  587. ret << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = " << callDest << ";";
  588. ret << CTRL_FLOW() << "goto _again;";
  589. ret << "}";
  590. if ( prePushExpr != 0 )
  591. ret << "}";
  592. }
  593. void CSharpTabCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
  594. {
  595. if ( prePushExpr != 0 ) {
  596. ret << "{";
  597. INLINE_LIST( ret, prePushExpr, 0, false );
  598. }
  599. ret << "{";
  600. ret << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = (";
  601. INLINE_LIST( ret, ilItem->children, targState, inFinish );
  602. ret << ");";
  603. ret << CTRL_FLOW() << "goto _again;";
  604. ret << "}";
  605. if ( prePushExpr != 0 )
  606. ret << "}";
  607. }
  608. void CSharpTabCodeGen::RET( ostream &ret, bool inFinish )
  609. {
  610. ret << "{";
  611. ret << vCS() << " = " << STACK() << "[--" << TOP() << "]; ";
  612. if ( postPopExpr != 0 ) {
  613. ret << "{";
  614. INLINE_LIST( ret, postPopExpr, 0, false );
  615. ret << "}";
  616. }
  617. ret << CTRL_FLOW() << "goto _again;";
  618. ret << "}";
  619. }
  620. void CSharpTabCodeGen::BREAK( ostream &ret, int targState )
  621. {
  622. outLabelUsed = true;
  623. ret << "{" << P() << "++; " << CTRL_FLOW() << "goto _out; }";
  624. }
  625. void CSharpTabCodeGen::writeData()
  626. {
  627. /* If there are any transtion functions then output the array. If there
  628. * are none, don't bother emitting an empty array that won't be used. */
  629. if ( redFsm->anyActions() ) {
  630. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
  631. ACTIONS_ARRAY();
  632. CLOSE_ARRAY() <<
  633. "\n";
  634. }
  635. if ( redFsm->anyConditions() ) {
  636. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
  637. COND_OFFSETS();
  638. CLOSE_ARRAY() <<
  639. "\n";
  640. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
  641. COND_LENS();
  642. CLOSE_ARRAY() <<
  643. "\n";
  644. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  645. COND_KEYS();
  646. CLOSE_ARRAY() <<
  647. "\n";
  648. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
  649. COND_SPACES();
  650. CLOSE_ARRAY() <<
  651. "\n";
  652. }
  653. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
  654. KEY_OFFSETS();
  655. CLOSE_ARRAY() <<
  656. "\n";
  657. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  658. KEYS();
  659. CLOSE_ARRAY() <<
  660. "\n";
  661. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
  662. SINGLE_LENS();
  663. CLOSE_ARRAY() <<
  664. "\n";
  665. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
  666. RANGE_LENS();
  667. CLOSE_ARRAY() <<
  668. "\n";
  669. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
  670. INDEX_OFFSETS();
  671. CLOSE_ARRAY() <<
  672. "\n";
  673. if ( useIndicies ) {
  674. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  675. INDICIES();
  676. CLOSE_ARRAY() <<
  677. "\n";
  678. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  679. TRANS_TARGS_WI();
  680. CLOSE_ARRAY() <<
  681. "\n";
  682. if ( redFsm->anyActions() ) {
  683. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  684. TRANS_ACTIONS_WI();
  685. CLOSE_ARRAY() <<
  686. "\n";
  687. }
  688. }
  689. else {
  690. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  691. TRANS_TARGS();
  692. CLOSE_ARRAY() <<
  693. "\n";
  694. if ( redFsm->anyActions() ) {
  695. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  696. TRANS_ACTIONS();
  697. CLOSE_ARRAY() <<
  698. "\n";
  699. }
  700. }
  701. if ( redFsm->anyToStateActions() ) {
  702. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  703. TO_STATE_ACTIONS();
  704. CLOSE_ARRAY() <<
  705. "\n";
  706. }
  707. if ( redFsm->anyFromStateActions() ) {
  708. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  709. FROM_STATE_ACTIONS();
  710. CLOSE_ARRAY() <<
  711. "\n";
  712. }
  713. if ( redFsm->anyEofActions() ) {
  714. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  715. EOF_ACTIONS();
  716. CLOSE_ARRAY() <<
  717. "\n";
  718. }
  719. if ( redFsm->anyEofTrans() ) {
  720. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  721. EOF_TRANS();
  722. CLOSE_ARRAY() <<
  723. "\n";
  724. }
  725. STATE_IDS();
  726. }
  727. void CSharpTabCodeGen::LOCATE_TRANS()
  728. {
  729. out <<
  730. " _keys = " << KO() + "[" + vCS() + "]" << ";\n"
  731. " _trans = " << CAST(transType) << IO() << "[" << vCS() << "];\n"
  732. "\n"
  733. " _klen = " << SL() << "[" << vCS() << "];\n"
  734. " if ( _klen > 0 ) {\n"
  735. " " << signedKeysType << " _lower = _keys;\n"
  736. " " << signedKeysType << " _mid;\n"
  737. " " << signedKeysType << " _upper = " << CAST(signedKeysType) <<
  738. " (_keys + _klen - 1);\n"
  739. " while (true) {\n"
  740. " if ( _upper < _lower )\n"
  741. " break;\n"
  742. "\n"
  743. " _mid = " << CAST(signedKeysType) <<
  744. " (_lower + ((_upper-_lower) >> 1));\n"
  745. " if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
  746. " _upper = " << CAST(signedKeysType) << " (_mid - 1);\n"
  747. " else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid] )\n"
  748. " _lower = " << CAST(signedKeysType) << " (_mid + 1);\n"
  749. " else {\n"
  750. " _trans += " << CAST(transType) << " (_mid - _keys);\n"
  751. " goto _match;\n"
  752. " }\n"
  753. " }\n"
  754. " _keys += " << CAST(keysType) << " _klen;\n"
  755. " _trans += " << CAST(transType) << " _klen;\n"
  756. " }\n"
  757. "\n"
  758. " _klen = " << RL() << "[" << vCS() << "];\n"
  759. " if ( _klen > 0 ) {\n"
  760. " " << signedKeysType << " _lower = _keys;\n"
  761. " " << signedKeysType << " _mid;\n"
  762. " " << signedKeysType << " _upper = " << CAST(signedKeysType) <<
  763. " (_keys + (_klen<<1) - 2);\n"
  764. " while (true) {\n"
  765. " if ( _upper < _lower )\n"
  766. " break;\n"
  767. "\n"
  768. " _mid = " << CAST(signedKeysType) <<
  769. " (_lower + (((_upper-_lower) >> 1) & ~1));\n"
  770. " if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
  771. " _upper = " << CAST(signedKeysType) << " (_mid - 2);\n"
  772. " else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid+1] )\n"
  773. " _lower = " << CAST(signedKeysType) << " (_mid + 2);\n"
  774. " else {\n"
  775. " _trans += " << CAST(transType) << "((_mid - _keys)>>1);\n"
  776. " goto _match;\n"
  777. " }\n"
  778. " }\n"
  779. " _trans += " << CAST(transType) << " _klen;\n"
  780. " }\n"
  781. "\n";
  782. }
  783. void CSharpTabCodeGen::COND_TRANSLATE()
  784. {
  785. out <<
  786. " _widec = " << GET_KEY() << ";\n"
  787. " _klen = " << CL() << "[" << vCS() << "];\n"
  788. " _keys = " << CAST(keysType) << " ("<< CO() << "[" << vCS() << "]*2);\n"
  789. " if ( _klen > 0 ) {\n"
  790. " " << signedKeysType << " _lower = _keys;\n"
  791. " " << signedKeysType << " _mid;\n"
  792. " " << signedKeysType << " _upper = " << CAST(signedKeysType) <<
  793. " (_keys + (_klen<<1) - 2);\n"
  794. " while (true) {\n"
  795. " if ( _upper < _lower )\n"
  796. " break;\n"
  797. "\n"
  798. " _mid = " << CAST(signedKeysType) <<
  799. " (_lower + (((_upper-_lower) >> 1) & ~1));\n"
  800. " if ( " << GET_WIDE_KEY() << " < " << CK() << "[_mid] )\n"
  801. " _upper = " << CAST(signedKeysType) << " (_mid - 2);\n"
  802. " else if ( " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1] )\n"
  803. " _lower = " << CAST(signedKeysType) << " (_mid + 2);\n"
  804. " else {\n"
  805. " switch ( " << C() << "[" << CO() << "[" << vCS() << "]"
  806. " + ((_mid - _keys)>>1)] ) {\n";
  807. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  808. GenCondSpace *condSpace = csi;
  809. out << " case " << condSpace->condSpaceId << ": {\n";
  810. out << TABS(2) << "_widec = " << CAST(WIDE_ALPH_TYPE()) << "(" <<
  811. KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
  812. " - " << KEY(keyOps->minKey) << "));\n";
  813. for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
  814. out << TABS(2) << "if ( ";
  815. CONDITION( out, *csi );
  816. Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
  817. out << " ) _widec += " << condValOffset << ";\n";
  818. }
  819. out <<
  820. " break;\n"
  821. " }\n";
  822. }
  823. SWITCH_DEFAULT();
  824. out <<
  825. " }\n"
  826. " break;\n"
  827. " }\n"
  828. " }\n"
  829. " }\n"
  830. "\n";
  831. }
  832. void CSharpTabCodeGen::writeExec()
  833. {
  834. testEofUsed = false;
  835. outLabelUsed = false;
  836. initVarTypes();
  837. out <<
  838. " {\n"
  839. " " << klenType << " _klen";
  840. if ( redFsm->anyRegCurStateRef() )
  841. out << ", _ps";
  842. out <<
  843. ";\n"
  844. " " << transType << " _trans;\n";
  845. if ( redFsm->anyConditions() )
  846. out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
  847. if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
  848. || redFsm->anyFromStateActions() )
  849. {
  850. out <<
  851. " int _acts;\n"
  852. " int _nacts;\n";
  853. }
  854. out <<
  855. " " << keysType << " _keys;\n"
  856. "\n";
  857. // " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
  858. if ( !noEnd ) {
  859. testEofUsed = true;
  860. out <<
  861. " if ( " << P() << " == " << PE() << " )\n"
  862. " goto _test_eof;\n";
  863. }
  864. if ( redFsm->errState != 0 ) {
  865. outLabelUsed = true;
  866. out <<
  867. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  868. " goto _out;\n";
  869. }
  870. out << "_resume:\n";
  871. if ( redFsm->anyFromStateActions() ) {
  872. out <<
  873. " _acts = " << FSA() << "[" + vCS() + "]" << ";\n"
  874. " _nacts = " << A() << "[_acts++];\n"
  875. " while ( _nacts-- > 0 ) {\n"
  876. " switch ( " << A() << "[_acts++] ) {\n";
  877. FROM_STATE_ACTION_SWITCH();
  878. SWITCH_DEFAULT() <<
  879. " }\n"
  880. " }\n"
  881. "\n";
  882. }
  883. if ( redFsm->anyConditions() )
  884. COND_TRANSLATE();
  885. LOCATE_TRANS();
  886. out << "_match:\n";
  887. if ( useIndicies )
  888. out << " _trans = " << CAST(transType) << I() << "[_trans];\n";
  889. if ( redFsm->anyEofTrans() )
  890. out << "_eof_trans:\n";
  891. if ( redFsm->anyRegCurStateRef() )
  892. out << " _ps = " << vCS() << ";\n";
  893. out <<
  894. " " << vCS() << " = " << TT() << "[_trans];\n"
  895. "\n";
  896. if ( redFsm->anyRegActions() ) {
  897. out <<
  898. " if ( " << TA() << "[_trans] == 0 )\n"
  899. " goto _again;\n"
  900. "\n"
  901. " _acts = " << TA() << "[_trans]" << ";\n"
  902. " _nacts = " << A() << "[_acts++];\n"
  903. " while ( _nacts-- > 0 )\n {\n"
  904. " switch ( " << A() << "[_acts++] )\n {\n";
  905. ACTION_SWITCH();
  906. SWITCH_DEFAULT() <<
  907. " }\n"
  908. " }\n"
  909. "\n";
  910. }
  911. if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  912. redFsm->anyActionCalls() || redFsm->anyActionRets() )
  913. out << "_again:\n";
  914. if ( redFsm->anyToStateActions() ) {
  915. out <<
  916. " _acts = " << TSA() << "[" << vCS() << "]" << ";\n"
  917. " _nacts = " << A() << "[_acts++];\n"
  918. " while ( _nacts-- > 0 ) {\n"
  919. " switch ( " << A() << "[_acts++] ) {\n";
  920. TO_STATE_ACTION_SWITCH();
  921. SWITCH_DEFAULT() <<
  922. " }\n"
  923. " }\n"
  924. "\n";
  925. }
  926. if ( redFsm->errState != 0 ) {
  927. outLabelUsed = true;
  928. out <<
  929. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  930. " goto _out;\n";
  931. }
  932. if ( !noEnd ) {
  933. out <<
  934. " if ( ++" << P() << " != " << PE() << " )\n"
  935. " goto _resume;\n";
  936. }
  937. else {
  938. out <<
  939. " " << P() << " += 1;\n"
  940. " goto _resume;\n";
  941. }
  942. if ( testEofUsed )
  943. out << " _test_eof: {}\n";
  944. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  945. out <<
  946. " if ( " << P() << " == " << vEOF() << " )\n"
  947. " {\n";
  948. if ( redFsm->anyEofTrans() ) {
  949. out <<
  950. " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
  951. " _trans = " << CAST(transType) << " (" << ET() <<
  952. "[" << vCS() << "] - 1);\n"
  953. " goto _eof_trans;\n"
  954. " }\n";
  955. }
  956. if ( redFsm->anyEofActions() ) {
  957. out <<
  958. " int __acts = " <<
  959. EA() << "[" << vCS() << "]" << ";\n"
  960. " int __nacts = " <<
  961. A() << "[__acts++];\n"
  962. " while ( __nacts-- > 0 ) {\n"
  963. " switch ( " << A() << "[__acts++] ) {\n";
  964. EOF_ACTION_SWITCH();
  965. SWITCH_DEFAULT() <<
  966. " }\n"
  967. " }\n";
  968. }
  969. out <<
  970. " }\n"
  971. "\n";
  972. }
  973. if ( outLabelUsed )
  974. out << " _out: {}\n";
  975. out << " }\n";
  976. }
  977. void CSharpTabCodeGen::initVarTypes()
  978. {
  979. int klenMax = MAX(MAX(redFsm->maxCondLen, redFsm->maxRangeLen),
  980. redFsm->maxSingleLen);
  981. int keysMax = MAX(MAX(redFsm->maxKeyOffset, klenMax),
  982. redFsm->maxCondOffset);
  983. int transMax = MAX(MAX(redFsm->maxIndex+1, redFsm->maxIndexOffset), keysMax);
  984. transMax = MAX(transMax, klenMax);
  985. transType = ARRAY_TYPE(transMax);
  986. klenType = ARRAY_TYPE(klenMax);
  987. keysType = ARRAY_TYPE(keysMax);
  988. signedKeysType = ARRAY_TYPE(keysMax, true);
  989. }