goftable.cpp 10 KB

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