cdsplit.cpp 13 KB

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