cdftable.cpp 9.9 KB

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