mlcodegen.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 "mlcodegen.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. #include <sstream>
  27. #include <iomanip>
  28. #include <string>
  29. #include <assert.h>
  30. using std::ostream;
  31. using std::ostringstream;
  32. using std::string;
  33. using std::cerr;
  34. using std::endl;
  35. using std::istream;
  36. using std::ifstream;
  37. using std::ostream;
  38. using std::ios;
  39. using std::cin;
  40. using std::cout;
  41. using std::cerr;
  42. using std::endl;
  43. void ocamlLineDirective( ostream &out, const char *fileName, int line )
  44. {
  45. if ( noLineDirectives )
  46. return;
  47. /* Write the line info for to the input file. */
  48. out << "# " << line << " \"";
  49. for ( const char *pc = fileName; *pc != 0; pc++ ) {
  50. if ( *pc == '\\' || *pc == '"' )
  51. out << "\\";
  52. out << *pc;
  53. }
  54. out << "\"\n";
  55. }
  56. void OCamlCodeGen::genLineDirective( ostream &out )
  57. {
  58. std::streambuf *sbuf = out.rdbuf();
  59. output_filter *filter = static_cast<output_filter*>(sbuf);
  60. ocamlLineDirective( out, filter->fileName, filter->line + 1 );
  61. }
  62. /* Init code gen with in parameters. */
  63. OCamlCodeGen::OCamlCodeGen( ostream &out )
  64. :
  65. CodeGenData(out)
  66. {
  67. }
  68. unsigned int OCamlCodeGen::arrayTypeSize( unsigned long maxVal )
  69. {
  70. long long maxValLL = (long long) maxVal;
  71. HostType *arrayType = keyOps->typeSubsumes( maxValLL );
  72. assert( arrayType != 0 );
  73. return arrayType->size;
  74. }
  75. string OCamlCodeGen::ARRAY_TYPE( unsigned long maxVal )
  76. {
  77. return ARRAY_TYPE( maxVal, false );
  78. }
  79. string OCamlCodeGen::ARRAY_TYPE( unsigned long maxVal, bool forceSigned )
  80. {
  81. long long maxValLL = (long long) maxVal;
  82. HostType *arrayType;
  83. if (forceSigned)
  84. arrayType = keyOps->typeSubsumes(true, maxValLL);
  85. else
  86. arrayType = keyOps->typeSubsumes( maxValLL );
  87. assert( arrayType != 0 );
  88. string ret = arrayType->data1;
  89. if ( arrayType->data2 != 0 ) {
  90. ret += " ";
  91. ret += arrayType->data2;
  92. }
  93. return ret;
  94. }
  95. /* Write out the fsm name. */
  96. string OCamlCodeGen::FSM_NAME()
  97. {
  98. return fsmName;
  99. }
  100. /* Emit the offset of the start state as a decimal integer. */
  101. string OCamlCodeGen::START_STATE_ID()
  102. {
  103. ostringstream ret;
  104. ret << redFsm->startState->id;
  105. return ret.str();
  106. };
  107. /* Write out the array of actions. */
  108. std::ostream &OCamlCodeGen::ACTIONS_ARRAY()
  109. {
  110. out << "\t0; ";
  111. int totalActions = 1;
  112. for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
  113. /* Write out the length, which will never be the last character. */
  114. out << act->key.length() << ARR_SEP();
  115. /* Put in a line break every 8 */
  116. if ( totalActions++ % 8 == 7 )
  117. out << "\n\t";
  118. for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) {
  119. out << item->value->actionId;
  120. if ( ! (act.last() && item.last()) )
  121. out << ARR_SEP();
  122. /* Put in a line break every 8 */
  123. if ( totalActions++ % 8 == 7 )
  124. out << "\n\t";
  125. }
  126. }
  127. out << "\n";
  128. return out;
  129. }
  130. /*
  131. string OCamlCodeGen::ACCESS()
  132. {
  133. ostringstream ret;
  134. if ( accessExpr != 0 )
  135. INLINE_LIST( ret, accessExpr, 0, false );
  136. return ret.str();
  137. }
  138. */
  139. string OCamlCodeGen::make_access(char const* name, GenInlineList* x, bool prefix = true)
  140. {
  141. ostringstream ret;
  142. if ( x == 0 )
  143. {
  144. if (prefix && accessExpr != 0)
  145. {
  146. INLINE_LIST( ret, accessExpr, 0, false);
  147. ret << name;
  148. }
  149. else
  150. ret << name << ".contents"; // ref cell
  151. }
  152. else {
  153. ret << "(";
  154. INLINE_LIST( ret, x, 0, false );
  155. ret << ")";
  156. }
  157. return ret.str();
  158. }
  159. string OCamlCodeGen::P() { return make_access("p", pExpr, false); }
  160. string OCamlCodeGen::PE() { return make_access("pe", peExpr, false); }
  161. string OCamlCodeGen::vEOF() { return make_access("eof", eofExpr, false); }
  162. string OCamlCodeGen::vCS() { return make_access("cs", csExpr); }
  163. string OCamlCodeGen::TOP() { return make_access("top", topExpr); }
  164. string OCamlCodeGen::STACK() { return make_access("stack", stackExpr); }
  165. string OCamlCodeGen::ACT() { return make_access("act", actExpr); }
  166. string OCamlCodeGen::TOKSTART() { return make_access("ts", tokstartExpr); }
  167. string OCamlCodeGen::TOKEND() { return make_access("te", tokendExpr); }
  168. string OCamlCodeGen::GET_WIDE_KEY()
  169. {
  170. if ( redFsm->anyConditions() )
  171. return "_widec";
  172. else
  173. { ostringstream ret; ret << "Char.code " << GET_KEY(); return ret.str(); }
  174. }
  175. string OCamlCodeGen::GET_WIDE_KEY( RedStateAp *state )
  176. {
  177. if ( state->stateCondList.length() > 0 )
  178. return "_widec";
  179. else
  180. { ostringstream ret; ret << "Char.code " << GET_KEY(); return ret.str(); }
  181. }
  182. /* Write out level number of tabs. Makes the nested binary search nice
  183. * looking. */
  184. string OCamlCodeGen::TABS( int level )
  185. {
  186. string result;
  187. while ( level-- > 0 )
  188. result += "\t";
  189. return result;
  190. }
  191. /* Write out a key from the fsm code gen. Depends on wether or not the key is
  192. * signed. */
  193. string OCamlCodeGen::KEY( Key key )
  194. {
  195. ostringstream ret;
  196. if ( keyOps->isSigned || !hostLang->explicitUnsigned )
  197. ret << key.getVal();
  198. else
  199. ret << (unsigned long) key.getVal() << 'u';
  200. return ret.str();
  201. }
  202. string OCamlCodeGen::ALPHA_KEY( Key key )
  203. {
  204. ostringstream ret;
  205. ret << key.getVal();
  206. /*
  207. if (key.getVal() > 0xFFFF) {
  208. ret << key.getVal();
  209. } else {
  210. ret << "'\\u" << std::hex << std::setw(4) << std::setfill('0') <<
  211. key.getVal() << "'";
  212. }
  213. */
  214. //ret << "(char) " << key.getVal();
  215. return ret.str();
  216. }
  217. void OCamlCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
  218. {
  219. // The parser gives fexec two children.
  220. ret << "begin " << P() << " <- ";
  221. INLINE_LIST( ret, item->children, targState, inFinish );
  222. ret << " - 1 end; ";
  223. }
  224. void OCamlCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item,
  225. int targState, int inFinish )
  226. {
  227. bool catch_all = false;
  228. ret <<
  229. " begin match " << ACT() << " with\n";
  230. for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
  231. /* Write the case label, the action and the case break. */
  232. if ( lma->lmId < 0 )
  233. {
  234. catch_all = true;
  235. ret << " | _ ->\n";
  236. }
  237. else
  238. ret << " | " << lma->lmId << " ->\n";
  239. /* Write the block and close it off. */
  240. ret << " begin ";
  241. INLINE_LIST( ret, lma->children, targState, inFinish );
  242. ret << " end\n";
  243. }
  244. if (!catch_all)
  245. ret << " | _ -> assert false\n";
  246. ret <<
  247. " end;\n"
  248. "\t";
  249. }
  250. void OCamlCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
  251. {
  252. ret << ACT() << " <- " << item->lmId << "; ";
  253. }
  254. void OCamlCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
  255. {
  256. /* The tokend action sets tokend. */
  257. ret << TOKEND() << " <- " << P();
  258. if ( item->offset != 0 )
  259. out << "+" << item->offset;
  260. out << "; ";
  261. }
  262. void OCamlCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
  263. {
  264. ret << TOKEND();
  265. }
  266. void OCamlCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
  267. {
  268. ret << TOKSTART() << " <- " << NULL_ITEM() << "; ";
  269. }
  270. void OCamlCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
  271. {
  272. ret << ACT() << " <- 0;";
  273. }
  274. void OCamlCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
  275. {
  276. ret << TOKSTART() << " <- " << P() << "; ";
  277. }
  278. void OCamlCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item,
  279. int targState, bool inFinish )
  280. {
  281. if ( item->children->length() > 0 ) {
  282. /* Write the block and close it off. */
  283. ret << "begin ";
  284. INLINE_LIST( ret, item->children, targState, inFinish );
  285. ret << " end";
  286. }
  287. }
  288. /* Write out an inline tree structure. Walks the list and possibly calls out
  289. * to virtual functions than handle language specific items in the tree. */
  290. void OCamlCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList,
  291. int targState, bool inFinish )
  292. {
  293. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  294. switch ( item->type ) {
  295. case GenInlineItem::Text:
  296. ret << item->data;
  297. break;
  298. case GenInlineItem::Goto:
  299. GOTO( ret, item->targState->id, inFinish );
  300. break;
  301. case GenInlineItem::Call:
  302. CALL( ret, item->targState->id, targState, inFinish );
  303. break;
  304. case GenInlineItem::Next:
  305. NEXT( ret, item->targState->id, inFinish );
  306. break;
  307. case GenInlineItem::Ret:
  308. RET( ret, inFinish );
  309. break;
  310. case GenInlineItem::PChar:
  311. ret << P();
  312. break;
  313. case GenInlineItem::Char:
  314. ret << GET_KEY();
  315. break;
  316. case GenInlineItem::Hold:
  317. ret << P() << " <- " << P() << " - 1; ";
  318. break;
  319. case GenInlineItem::Exec:
  320. EXEC( ret, item, targState, inFinish );
  321. break;
  322. case GenInlineItem::Curs:
  323. CURS( ret, inFinish );
  324. break;
  325. case GenInlineItem::Targs:
  326. TARGS( ret, inFinish, targState );
  327. break;
  328. case GenInlineItem::Entry:
  329. ret << item->targState->id;
  330. break;
  331. case GenInlineItem::GotoExpr:
  332. GOTO_EXPR( ret, item, inFinish );
  333. break;
  334. case GenInlineItem::CallExpr:
  335. CALL_EXPR( ret, item, targState, inFinish );
  336. break;
  337. case GenInlineItem::NextExpr:
  338. NEXT_EXPR( ret, item, inFinish );
  339. break;
  340. case GenInlineItem::LmSwitch:
  341. LM_SWITCH( ret, item, targState, inFinish );
  342. break;
  343. case GenInlineItem::LmSetActId:
  344. SET_ACT( ret, item );
  345. break;
  346. case GenInlineItem::LmSetTokEnd:
  347. SET_TOKEND( ret, item );
  348. break;
  349. case GenInlineItem::LmGetTokEnd:
  350. GET_TOKEND( ret, item );
  351. break;
  352. case GenInlineItem::LmInitTokStart:
  353. INIT_TOKSTART( ret, item );
  354. break;
  355. case GenInlineItem::LmInitAct:
  356. INIT_ACT( ret, item );
  357. break;
  358. case GenInlineItem::LmSetTokStart:
  359. SET_TOKSTART( ret, item );
  360. break;
  361. case GenInlineItem::SubAction:
  362. SUB_ACTION( ret, item, targState, inFinish );
  363. break;
  364. case GenInlineItem::Break:
  365. BREAK( ret, targState );
  366. break;
  367. }
  368. }
  369. }
  370. /* Write out paths in line directives. Escapes any special characters. */
  371. string OCamlCodeGen::LDIR_PATH( char *path )
  372. {
  373. ostringstream ret;
  374. for ( char *pc = path; *pc != 0; pc++ ) {
  375. if ( *pc == '\\' )
  376. ret << "\\\\";
  377. else
  378. ret << *pc;
  379. }
  380. return ret.str();
  381. }
  382. void OCamlCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish )
  383. {
  384. /* Write the preprocessor line info for going into the source file. */
  385. ocamlLineDirective( ret, action->loc.fileName, action->loc.line );
  386. /* Write the block and close it off. */
  387. ret << "\t\tbegin ";
  388. INLINE_LIST( ret, action->inlineList, targState, inFinish );
  389. ret << " end;\n";
  390. }
  391. void OCamlCodeGen::CONDITION( ostream &ret, GenAction *condition )
  392. {
  393. ret << "\n";
  394. ocamlLineDirective( ret, condition->loc.fileName, condition->loc.line );
  395. INLINE_LIST( ret, condition->inlineList, 0, false );
  396. }
  397. string OCamlCodeGen::ERROR_STATE()
  398. {
  399. ostringstream ret;
  400. if ( redFsm->errState != 0 )
  401. ret << redFsm->errState->id;
  402. else
  403. ret << "-1";
  404. return ret.str();
  405. }
  406. string OCamlCodeGen::FIRST_FINAL_STATE()
  407. {
  408. ostringstream ret;
  409. if ( redFsm->firstFinState != 0 )
  410. ret << redFsm->firstFinState->id;
  411. else
  412. ret << redFsm->nextStateId;
  413. return ret.str();
  414. }
  415. void OCamlCodeGen::writeInit()
  416. {
  417. out << " begin\n";
  418. if ( !noCS )
  419. out << "\t" << vCS() << " <- " << START() << ";\n";
  420. /* If there are any calls, then the stack top needs initialization. */
  421. if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
  422. out << "\t" << TOP() << " <- 0;\n";
  423. if ( hasLongestMatch ) {
  424. out <<
  425. " " << TOKSTART() << " <- " << NULL_ITEM() << ";\n"
  426. " " << TOKEND() << " <- " << NULL_ITEM() << ";\n"
  427. " " << ACT() << " <- 0;\n";
  428. }
  429. out << " end;\n";
  430. }
  431. string OCamlCodeGen::PRE_INCR(string val)
  432. {
  433. ostringstream ret;
  434. ret << "(" << val << " <- " << val << " + 1; " << val << ")";
  435. return ret.str();
  436. }
  437. string OCamlCodeGen::POST_INCR(string val)
  438. {
  439. ostringstream ret;
  440. ret << "(let temp = " << val << " in " << val << " <- " << val << " + 1; temp)";
  441. return ret.str();
  442. }
  443. string OCamlCodeGen::PRE_DECR(string val)
  444. {
  445. ostringstream ret;
  446. ret << "(" << val << " <- " << val << " - 1; " << val << ")";
  447. return ret.str();
  448. }
  449. string OCamlCodeGen::POST_DECR(string val)
  450. {
  451. ostringstream ret;
  452. ret << "(let temp = " << val << " in " << val << " <- " << val << " - 1; temp)";
  453. return ret.str();
  454. }
  455. string OCamlCodeGen::DATA_PREFIX()
  456. {
  457. if ( data_prefix.empty() ) // init
  458. {
  459. data_prefix = string(fsmName) + "_";
  460. if (data_prefix.size() > 0)
  461. data_prefix[0] = ::tolower(data_prefix[0]); // uncapitalize
  462. }
  463. if ( !noPrefix )
  464. return data_prefix;
  465. return "";
  466. }
  467. /* Emit the alphabet data type. */
  468. string OCamlCodeGen::ALPH_TYPE()
  469. {
  470. string ret = keyOps->alphType->data1;
  471. if ( keyOps->alphType->data2 != 0 ) {
  472. ret += " ";
  473. ret += + keyOps->alphType->data2;
  474. }
  475. return ret;
  476. }
  477. /* Emit the alphabet data type. */
  478. string OCamlCodeGen::WIDE_ALPH_TYPE()
  479. {
  480. string ret;
  481. if ( redFsm->maxKey <= keyOps->maxKey )
  482. ret = ALPH_TYPE();
  483. else {
  484. long long maxKeyVal = redFsm->maxKey.getLongLong();
  485. HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
  486. assert( wideType != 0 );
  487. ret = wideType->data1;
  488. if ( wideType->data2 != 0 ) {
  489. ret += " ";
  490. ret += wideType->data2;
  491. }
  492. }
  493. return ret;
  494. }
  495. void OCamlCodeGen::STATE_IDS()
  496. {
  497. if ( redFsm->startState != 0 )
  498. STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << TOP_SEP ();
  499. if ( !noFinal )
  500. STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << TOP_SEP();
  501. if ( !noError )
  502. STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << TOP_SEP();
  503. out << "\n";
  504. if ( !noEntry && entryPointNames.length() > 0 ) {
  505. for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
  506. STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) <<
  507. " = " << entryPointIds[en.pos()] << TOP_SEP();
  508. }
  509. out << "\n";
  510. }
  511. }
  512. void OCamlCodeGen::writeStart()
  513. {
  514. out << START_STATE_ID();
  515. }
  516. void OCamlCodeGen::writeFirstFinal()
  517. {
  518. out << FIRST_FINAL_STATE();
  519. }
  520. void OCamlCodeGen::writeError()
  521. {
  522. out << ERROR_STATE();
  523. }
  524. string OCamlCodeGen::GET_KEY()
  525. {
  526. ostringstream ret;
  527. if ( getKeyExpr != 0 ) {
  528. /* Emit the user supplied method of retrieving the key. */
  529. ret << "(";
  530. INLINE_LIST( ret, getKeyExpr, 0, false );
  531. ret << ")";
  532. }
  533. else {
  534. /* Expression for retrieving the key, use simple dereference. */
  535. ret << "data.[" << P() << "]";
  536. }
  537. return ret.str();
  538. }
  539. string OCamlCodeGen::NULL_ITEM()
  540. {
  541. return "-1";
  542. }
  543. string OCamlCodeGen::POINTER()
  544. {
  545. // XXX C# has no pointers
  546. // multiple items seperated by commas can also be pointer types.
  547. return " ";
  548. }
  549. string OCamlCodeGen::PTR_CONST()
  550. {
  551. return "";
  552. }
  553. std::ostream &OCamlCodeGen::OPEN_ARRAY( string type, string name )
  554. {
  555. out << "let " << name << " : " << type << " array = [|" << endl;
  556. return out;
  557. }
  558. std::ostream &OCamlCodeGen::CLOSE_ARRAY()
  559. {
  560. return out << "|]" << TOP_SEP();
  561. }
  562. string OCamlCodeGen::TOP_SEP()
  563. {
  564. return "\n"; // original syntax
  565. }
  566. string OCamlCodeGen::ARR_SEP()
  567. {
  568. return "; ";
  569. }
  570. string OCamlCodeGen::AT(const string& array, const string& index)
  571. {
  572. ostringstream ret;
  573. ret << array << ".(" << index << ")";
  574. return ret.str();
  575. }
  576. std::ostream &OCamlCodeGen::STATIC_VAR( string type, string name )
  577. {
  578. out << "let " << name << " : " << type;
  579. return out;
  580. }
  581. string OCamlCodeGen::ARR_OFF( string ptr, string offset )
  582. {
  583. // XXX C# can't do pointer arithmetic
  584. return "&" + ptr + "[" + offset + "]";
  585. }
  586. string OCamlCodeGen::CAST( string type )
  587. {
  588. return "";
  589. // return "(" + type + ")";
  590. }
  591. string OCamlCodeGen::UINT( )
  592. {
  593. return "uint";
  594. }
  595. std::ostream &OCamlCodeGen::SWITCH_DEFAULT()
  596. {
  597. out << " | _ -> ()\n";
  598. return out;
  599. }
  600. string OCamlCodeGen::CTRL_FLOW()
  601. {
  602. return "if true then ";
  603. }
  604. void OCamlCodeGen::finishRagelDef()
  605. {
  606. if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
  607. codeStyle == GenIpGoto || codeStyle == GenSplit )
  608. {
  609. /* For directly executable machines there is no required state
  610. * ordering. Choose a depth-first ordering to increase the
  611. * potential for fall-throughs. */
  612. redFsm->depthFirstOrdering();
  613. }
  614. else {
  615. /* The frontend will do this for us, but it may be a good idea to
  616. * force it if the intermediate file is edited. */
  617. redFsm->sortByStateId();
  618. }
  619. /* Choose default transitions and the single transition. */
  620. redFsm->chooseDefaultSpan();
  621. /* Maybe do flat expand, otherwise choose single. */
  622. if ( codeStyle == GenFlat || codeStyle == GenFFlat )
  623. redFsm->makeFlat();
  624. else
  625. redFsm->chooseSingle();
  626. /* If any errors have occured in the input file then don't write anything. */
  627. if ( gblErrorCount > 0 )
  628. return;
  629. if ( codeStyle == GenSplit )
  630. redFsm->partitionFsm( numSplitPartitions );
  631. if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
  632. redFsm->setInTrans();
  633. /* Anlayze Machine will find the final action reference counts, among
  634. * other things. We will use these in reporting the usage
  635. * of fsm directives in action code. */
  636. analyzeMachine();
  637. /* Determine if we should use indicies. */
  638. calcIndexSize();
  639. }
  640. ostream &OCamlCodeGen::source_warning( const InputLoc &loc )
  641. {
  642. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
  643. return cerr;
  644. }
  645. ostream &OCamlCodeGen::source_error( const InputLoc &loc )
  646. {
  647. gblErrorCount += 1;
  648. assert( sourceFileName != 0 );
  649. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
  650. return cerr;
  651. }