csftable.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 "csftable.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. /* Determine if we should use indicies or not. */
  27. void CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 &CSharpFTabCodeGen::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 );
  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 &CSharpFTabCodeGen::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 );
  113. out << "\tbreak;\n";
  114. }
  115. }
  116. genLineDirective( out );
  117. return out;
  118. }
  119. std::ostream &CSharpFTabCodeGen::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 );
  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 &CSharpFTabCodeGen::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 );
  147. out << "\tbreak;\n";
  148. }
  149. }
  150. genLineDirective( out );
  151. return out;
  152. }
  153. void CSharpFTabCodeGen::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 CSharpFTabCodeGen::writeExec()
  248. {
  249. testEofUsed = false;
  250. outLabelUsed = false;
  251. initVarTypes();
  252. out <<
  253. " {\n"
  254. " " << klenType << " _klen";
  255. if ( redFsm->anyRegCurStateRef() )
  256. out << ", _ps";
  257. out <<
  258. ";\n"
  259. " " << keysType << " _keys;\n"
  260. " " << transType << " _trans;\n";
  261. if ( redFsm->anyConditions() )
  262. out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
  263. out << "\n";
  264. if ( !noEnd ) {
  265. testEofUsed = true;
  266. out <<
  267. " if ( " << P() << " == " << PE() << " )\n"
  268. " goto _test_eof;\n";
  269. }
  270. if ( redFsm->errState != 0 ) {
  271. outLabelUsed = true;
  272. out <<
  273. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  274. " goto _out;\n";
  275. }
  276. out << "_resume:\n";
  277. if ( redFsm->anyFromStateActions() ) {
  278. out <<
  279. " switch ( " << FSA() << "[" << vCS() << "] ) {\n";
  280. FROM_STATE_ACTION_SWITCH();
  281. SWITCH_DEFAULT() <<
  282. " }\n"
  283. "\n";
  284. }
  285. if ( redFsm->anyConditions() )
  286. COND_TRANSLATE();
  287. LOCATE_TRANS();
  288. out << "_match:\n";
  289. if ( useIndicies )
  290. out << " _trans = " << CAST(transType) << I() << "[_trans];\n";
  291. if ( redFsm->anyEofTrans() )
  292. out << "_eof_trans:\n";
  293. if ( redFsm->anyRegCurStateRef() )
  294. out << " _ps = " << vCS() << ";\n";
  295. out <<
  296. " " << vCS() << " = " << TT() << "[_trans];\n"
  297. "\n";
  298. if ( redFsm->anyRegActions() ) {
  299. out <<
  300. " if ( " << TA() << "[_trans] == 0 )\n"
  301. " goto _again;\n"
  302. "\n"
  303. " switch ( " << TA() << "[_trans] ) {\n";
  304. ACTION_SWITCH();
  305. SWITCH_DEFAULT() <<
  306. " }\n"
  307. "\n";
  308. }
  309. if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
  310. redFsm->anyActionCalls() || redFsm->anyActionRets() )
  311. out << "_again:\n";
  312. if ( redFsm->anyToStateActions() ) {
  313. out <<
  314. " switch ( " << TSA() << "[" << vCS() << "] ) {\n";
  315. TO_STATE_ACTION_SWITCH();
  316. SWITCH_DEFAULT() <<
  317. " }\n"
  318. "\n";
  319. }
  320. if ( redFsm->errState != 0 ) {
  321. outLabelUsed = true;
  322. out <<
  323. " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
  324. " goto _out;\n";
  325. }
  326. if ( !noEnd ) {
  327. out <<
  328. " if ( ++" << P() << " != " << PE() << " )\n"
  329. " goto _resume;\n";
  330. }
  331. else {
  332. out <<
  333. " " << P() << " += 1;\n"
  334. " goto _resume;\n";
  335. }
  336. if ( testEofUsed )
  337. out << " _test_eof: {}\n";
  338. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  339. out <<
  340. " if ( " << P() << " == " << vEOF() << " )\n"
  341. " {\n";
  342. if ( redFsm->anyEofTrans() ) {
  343. out <<
  344. " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
  345. " _trans = " << CAST(transType) << " (" << ET() <<
  346. "[" << vCS() << "] - 1);\n"
  347. " goto _eof_trans;\n"
  348. " }\n";
  349. }
  350. if ( redFsm->anyEofActions() ) {
  351. out <<
  352. " switch ( " << EA() << "[" << vCS() << "] ) {\n";
  353. EOF_ACTION_SWITCH();
  354. SWITCH_DEFAULT() <<
  355. " }\n";
  356. }
  357. out <<
  358. " }\n"
  359. "\n";
  360. }
  361. if ( outLabelUsed )
  362. out << " _out: {}\n";
  363. out << " }\n";
  364. }