cssplit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Copyright 2006 Adrian Thurston <thurston@complang.org>
  3. */
  4. /* This file is part of Ragel.
  5. *
  6. * Ragel is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Ragel is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ragel; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "ragel.h"
  21. #include "cssplit.h"
  22. #include "gendata.h"
  23. #include <assert.h>
  24. using std::ostream;
  25. using std::ios;
  26. using std::endl;
  27. /* Emit the goto to take for a given transition. */
  28. std::ostream &CSharpSplitCodeGen::TRANS_GOTO( RedTransAp *trans, int level )
  29. {
  30. if ( trans->targ->partition == currentPartition ) {
  31. if ( trans->action != 0 ) {
  32. /* Go to the transition which will go to the state. */
  33. out << TABS(level) << "goto tr" << trans->id << ";";
  34. }
  35. else {
  36. /* Go directly to the target state. */
  37. out << TABS(level) << "goto st" << trans->targ->id << ";";
  38. }
  39. }
  40. else {
  41. if ( trans->action != 0 ) {
  42. /* Go to the transition which will go to the state. */
  43. out << TABS(level) << "goto ptr" << trans->id << ";";
  44. trans->partitionBoundary = true;
  45. }
  46. else {
  47. /* Go directly to the target state. */
  48. out << TABS(level) << "goto pst" << trans->targ->id << ";";
  49. trans->targ->partitionBoundary = true;
  50. }
  51. }
  52. return out;
  53. }
  54. /* Called from before writing the gotos for each state. */
  55. void CSharpSplitCodeGen::GOTO_HEADER( RedStateAp *state, bool stateInPartition )
  56. {
  57. bool anyWritten = IN_TRANS_ACTIONS( state );
  58. if ( state->labelNeeded )
  59. out << "st" << state->id << ":\n";
  60. if ( state->toStateAction != 0 ) {
  61. /* Remember that we wrote an action. Write every action in the list. */
  62. anyWritten = true;
  63. for ( GenActionTable::Iter item = state->toStateAction->key; item.lte(); item++ )
  64. ACTION( out, item->value, state->id, false );
  65. }
  66. /* Advance and test buffer pos. */
  67. if ( state->labelNeeded ) {
  68. if ( !noEnd ) {
  69. out <<
  70. " if ( ++" << P() << " == " << PE() << " )\n"
  71. " goto _out" << state->id << ";\n";
  72. }
  73. else {
  74. out <<
  75. " " << P() << " += 1;\n";
  76. }
  77. }
  78. /* Give the state a switch case. */
  79. out << "case " << state->id << ":\n";
  80. if ( state->fromStateAction != 0 ) {
  81. /* Remember that we wrote an action. Write every action in the list. */
  82. anyWritten = true;
  83. for ( GenActionTable::Iter item = state->fromStateAction->key; item.lte(); item++ )
  84. ACTION( out, item->value, state->id, false );
  85. }
  86. if ( anyWritten )
  87. genLineDirective( out );
  88. /* Record the prev state if necessary. */
  89. if ( state->anyRegCurStateRef() )
  90. out << " _ps = " << state->id << ";\n";
  91. }
  92. std::ostream &CSharpSplitCodeGen::STATE_GOTOS( int partition )
  93. {
  94. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  95. if ( st->partition == partition ) {
  96. if ( st == redFsm->errState )
  97. STATE_GOTO_ERROR();
  98. else {
  99. /* We call into the base of the goto which calls back into us
  100. * using virtual functions. Set the current partition rather
  101. * than coding parameter passing throughout. */
  102. currentPartition = partition;
  103. /* Writing code above state gotos. */
  104. GOTO_HEADER( st, st->partition == partition );
  105. if ( st->stateCondVect.length() > 0 ) {
  106. out << " _widec = " << GET_KEY() << ";\n";
  107. emitCondBSearch( st, 1, 0, st->stateCondVect.length() - 1 );
  108. }
  109. /* Try singles. */
  110. if ( st->outSingle.length() > 0 )
  111. emitSingleSwitch( st );
  112. /* Default case is to binary search for the ranges, if that fails then */
  113. if ( st->outRange.length() > 0 )
  114. emitRangeBSearch( st, 1, 0, st->outRange.length() - 1 );
  115. /* Write the default transition. */
  116. TRANS_GOTO( st->defTrans, 1 ) << "\n";
  117. }
  118. }
  119. }
  120. return out;
  121. }
  122. std::ostream &CSharpSplitCodeGen::PART_TRANS( int partition )
  123. {
  124. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
  125. if ( trans->partitionBoundary ) {
  126. out <<
  127. "ptr" << trans->id << ":\n";
  128. if ( trans->action != 0 ) {
  129. /* If the action contains a next, then we must preload the current
  130. * state since the action may or may not set it. */
  131. if ( trans->action->anyNextStmt() )
  132. out << " " << vCS() << " = " << trans->targ->id << ";\n";
  133. /* Write each action in the list. */
  134. for ( GenActionTable::Iter item = trans->action->key; item.lte(); item++ )
  135. ACTION( out, item->value, trans->targ->id, false );
  136. }
  137. out <<
  138. " goto pst" << trans->targ->id << ";\n";
  139. trans->targ->partitionBoundary = true;
  140. }
  141. }
  142. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  143. if ( st->partitionBoundary ) {
  144. out <<
  145. " pst" << st->id << ":\n"
  146. " " << vCS() << " = " << st->id << ";\n";
  147. if ( st->toStateAction != 0 ) {
  148. /* Remember that we wrote an action. Write every action in the list. */
  149. for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
  150. ACTION( out, item->value, st->id, false );
  151. genLineDirective( out );
  152. }
  153. ptOutLabelUsed = true;
  154. out << " goto _pt_out; \n";
  155. }
  156. }
  157. return out;
  158. }
  159. std::ostream &CSharpSplitCodeGen::EXIT_STATES( int partition )
  160. {
  161. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  162. if ( st->partition == partition && st->outNeeded ) {
  163. outLabelUsed = true;
  164. out << " _out" << st->id << ": " << vCS() << " = " <<
  165. st->id << "; goto _out; \n";
  166. }
  167. }
  168. return out;
  169. }
  170. std::ostream &CSharpSplitCodeGen::PARTITION( int partition )
  171. {
  172. outLabelUsed = false;
  173. ptOutLabelUsed = false;
  174. /* Initialize the partition boundaries, which get set during the writing
  175. * of states. After the state writing we will */
  176. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  177. trans->partitionBoundary = false;
  178. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
  179. st->partitionBoundary = false;
  180. out << " " << ALPH_TYPE() << " *p = *_pp, *pe = *_ppe;\n";
  181. if ( redFsm->anyRegCurStateRef() )
  182. out << " int _ps = 0;\n";
  183. if ( redFsm->anyConditions() )
  184. out << " " << WIDE_ALPH_TYPE() << " _widec;\n";
  185. if ( useAgainLabel() ) {
  186. out <<
  187. " goto _resume;\n"
  188. "\n"
  189. "_again:\n"
  190. " switch ( " << vCS() << " ) {\n";
  191. AGAIN_CASES() <<
  192. " default: break;\n"
  193. " }\n"
  194. "\n";
  195. if ( !noEnd ) {
  196. outLabelUsed = true;
  197. out <<
  198. " if ( ++" << P() << " == " << PE() << " )\n"
  199. " goto _out;\n";
  200. }
  201. else {
  202. out <<
  203. " " << P() << " += 1;\n";
  204. }
  205. out <<
  206. "_resume:\n";
  207. }
  208. out <<
  209. " switch ( " << vCS() << " )\n {\n";
  210. STATE_GOTOS( partition );
  211. SWITCH_DEFAULT() <<
  212. " }\n";
  213. PART_TRANS( partition );
  214. EXIT_STATES( partition );
  215. if ( outLabelUsed ) {
  216. out <<
  217. "\n"
  218. " _out:\n"
  219. " *_pp = p;\n"
  220. " *_ppe = pe;\n"
  221. " return 0;\n";
  222. }
  223. if ( ptOutLabelUsed ) {
  224. out <<
  225. "\n"
  226. " _pt_out:\n"
  227. " *_pp = p;\n"
  228. " *_ppe = pe;\n"
  229. " return 1;\n";
  230. }
  231. return out;
  232. }
  233. std::ostream &CSharpSplitCodeGen::PART_MAP()
  234. {
  235. int *partMap = new int[redFsm->stateList.length()];
  236. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
  237. partMap[st->id] = st->partition;
  238. out << "\t";
  239. int totalItem = 0;
  240. for ( int i = 0; i < redFsm->stateList.length(); i++ ) {
  241. out << partMap[i];
  242. if ( i != redFsm->stateList.length() - 1 ) {
  243. out << ", ";
  244. if ( ++totalItem % IALL == 0 )
  245. out << "\n\t";
  246. }
  247. }
  248. delete[] partMap;
  249. return out;
  250. }
  251. void CSharpSplitCodeGen::writeData()
  252. {
  253. out <<
  254. "const int " << START() << " = " << START_STATE_ID() << ";\n"
  255. "\n";
  256. if ( !noFinal ) {
  257. out <<
  258. "const int " << FIRST_FINAL() << " = " << FIRST_FINAL_STATE() << ";\n"
  259. "\n";
  260. }
  261. if ( !noError ) {
  262. out <<
  263. "const int " << ERROR() << " = " << ERROR_STATE() << ";\n"
  264. "\n";
  265. }
  266. OPEN_ARRAY( ARRAY_TYPE(numSplitPartitions), PM() );
  267. PART_MAP();
  268. CLOSE_ARRAY() <<
  269. "\n";
  270. for ( int p = 0; p < redFsm->nParts; p++ ) {
  271. out << "int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
  272. " **_ppe, struct " << FSM_NAME() << " *fsm );\n";
  273. }
  274. out << "\n";
  275. }
  276. std::ostream &CSharpSplitCodeGen::ALL_PARTITIONS()
  277. {
  278. /* compute the format string. */
  279. int width = 0, high = redFsm->nParts - 1;
  280. while ( high > 0 ) {
  281. width++;
  282. high /= 10;
  283. }
  284. assert( width <= 8 );
  285. char suffFormat[] = "_%6.6d.c";
  286. suffFormat[2] = suffFormat[4] = ( '0' + width );
  287. for ( int p = 0; p < redFsm->nParts; p++ ) {
  288. char suffix[10];
  289. sprintf( suffix, suffFormat, p );
  290. const char *fn = fileNameFromStem( sourceFileName, suffix );
  291. const char *include = fileNameFromStem( sourceFileName, ".h" );
  292. /* Create the filter on the output and open it. */
  293. output_filter *partFilter = new output_filter( fn );
  294. partFilter->open( fn, ios::out|ios::trunc );
  295. if ( !partFilter->is_open() ) {
  296. error() << "error opening " << fn << " for writing" << endl;
  297. exit(1);
  298. }
  299. /* Attach the new file to the output stream. */
  300. std::streambuf *prev_rdbuf = out.rdbuf( partFilter );
  301. out <<
  302. "#include \"" << include << "\"\n"
  303. "int partition" << p << "( " << ALPH_TYPE() << " **_pp, " << ALPH_TYPE() <<
  304. " **_ppe, struct " << FSM_NAME() << " *fsm )\n"
  305. "{\n";
  306. PARTITION( p ) <<
  307. "}\n\n";
  308. out.flush();
  309. /* Fix the output stream. */
  310. out.rdbuf( prev_rdbuf );
  311. }
  312. return out;
  313. }
  314. void CSharpSplitCodeGen::writeExec()
  315. {
  316. /* Must set labels immediately before writing because we may depend on the
  317. * noend write option. */
  318. setLabelsNeeded();
  319. out <<
  320. " {\n"
  321. " int _stat = 0;\n";
  322. if ( !noEnd ) {
  323. out <<
  324. " if ( " << P() << " == " << PE() << " )\n"
  325. " goto _out;\n";
  326. }
  327. out << " goto _resume;\n";
  328. /* In this reentry, to-state actions have already been executed on the
  329. * partition-switch exit from the last partition. */
  330. out << "_reenter:\n";
  331. if ( !noEnd ) {
  332. out <<
  333. " if ( ++" << P() << " == " << PE() << " )\n"
  334. " goto _out;\n";
  335. }
  336. else {
  337. out <<
  338. " " << P() << " += 1;\n";
  339. }
  340. out << "_resume:\n";
  341. out <<
  342. " switch ( " << PM() << "[" << vCS() << "] ) {\n";
  343. for ( int p = 0; p < redFsm->nParts; p++ ) {
  344. out <<
  345. " case " << p << ":\n"
  346. " _stat = partition" << p << "( &p, &pe, fsm );\n"
  347. " break;\n";
  348. }
  349. out <<
  350. " }\n"
  351. " if ( _stat )\n"
  352. " goto _reenter;\n";
  353. if ( !noEnd )
  354. out << " _out: {}\n";
  355. out <<
  356. " }\n";
  357. ALL_PARTITIONS();
  358. }
  359. void CSharpSplitCodeGen::setLabelsNeeded( RedStateAp *fromState, GenInlineList *inlineList )
  360. {
  361. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  362. switch ( item->type ) {
  363. case GenInlineItem::Goto: case GenInlineItem::Call: {
  364. /* In split code gen we only need labels for transitions across
  365. * partitions. */
  366. if ( fromState->partition == item->targState->partition ){
  367. /* Mark the target as needing a label. */
  368. item->targState->labelNeeded = true;
  369. }
  370. break;
  371. }
  372. default: break;
  373. }
  374. if ( item->children != 0 )
  375. setLabelsNeeded( fromState, item->children );
  376. }
  377. }
  378. void CSharpSplitCodeGen::setLabelsNeeded( RedStateAp *fromState, RedTransAp *trans )
  379. {
  380. /* In the split code gen we don't need labels for transitions across
  381. * partitions. */
  382. if ( fromState->partition == trans->targ->partition ) {
  383. /* If there is no action with a next statement, then the label will be
  384. * needed. */
  385. trans->labelNeeded = true;
  386. if ( trans->action == 0 || !trans->action->anyNextStmt() )
  387. trans->targ->labelNeeded = true;
  388. }
  389. /* Need labels for states that have goto or calls in action code
  390. * invoked on characters (ie, not from out action code). */
  391. if ( trans->action != 0 ) {
  392. /* Loop the actions. */
  393. for ( GenActionTable::Iter act = trans->action->key; act.lte(); act++ ) {
  394. /* Get the action and walk it's tree. */
  395. setLabelsNeeded( fromState, act->value->inlineList );
  396. }
  397. }
  398. }
  399. /* Set up labelNeeded flag for each state. */
  400. void CSharpSplitCodeGen::setLabelsNeeded()
  401. {
  402. /* If we use the _again label, then we the _again switch, which uses all
  403. * labels. */
  404. if ( useAgainLabel() ) {
  405. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
  406. st->labelNeeded = true;
  407. }
  408. else {
  409. /* Do not use all labels by default, init all labelNeeded vars to false. */
  410. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
  411. st->labelNeeded = false;
  412. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  413. trans->labelNeeded = false;
  414. /* Walk all transitions and set only those that have targs. */
  415. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  416. for ( RedTransList::Iter tel = st->outRange; tel.lte(); tel++ )
  417. setLabelsNeeded( st, tel->value );
  418. for ( RedTransList::Iter tel = st->outSingle; tel.lte(); tel++ )
  419. setLabelsNeeded( st, tel->value );
  420. if ( st->defTrans != 0 )
  421. setLabelsNeeded( st, st->defTrans );
  422. }
  423. }
  424. if ( !noEnd ) {
  425. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
  426. st->outNeeded = st->labelNeeded;
  427. }
  428. else {
  429. if ( redFsm->errState != 0 )
  430. redFsm->errState->outNeeded = true;
  431. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ ) {
  432. /* Any state with a transition in that has a break will need an
  433. * out label. */
  434. if ( trans->action != 0 && trans->action->anyBreakStmt() )
  435. trans->targ->outNeeded = true;
  436. }
  437. }
  438. }