gendata.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*
  2. * Copyright 2005-2007 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 "gendata.h"
  21. #include "ragel.h"
  22. #include <iostream>
  23. /*
  24. * Code generators.
  25. */
  26. #include "cstable.h"
  27. #include "csftable.h"
  28. #include "csflat.h"
  29. #include "csfflat.h"
  30. #include "csgoto.h"
  31. #include "csfgoto.h"
  32. #include "csipgoto.h"
  33. #include "cssplit.h"
  34. #include "cdtable.h"
  35. #include "cdftable.h"
  36. #include "cdflat.h"
  37. #include "cdfflat.h"
  38. #include "cdgoto.h"
  39. #include "cdfgoto.h"
  40. #include "cdipgoto.h"
  41. #include "cdsplit.h"
  42. #include "dotcodegen.h"
  43. #include "javacodegen.h"
  44. #include "gocodegen.h"
  45. #include "gotable.h"
  46. #include "goftable.h"
  47. #include "goflat.h"
  48. #include "gofflat.h"
  49. #include "gogoto.h"
  50. #include "gofgoto.h"
  51. #include "goipgoto.h"
  52. #include "mltable.h"
  53. #include "mlftable.h"
  54. #include "mlflat.h"
  55. #include "mlfflat.h"
  56. #include "mlgoto.h"
  57. #include "mlfgoto.h"
  58. #include "rubytable.h"
  59. #include "rubyftable.h"
  60. #include "rubyflat.h"
  61. #include "rubyfflat.h"
  62. #include "rbxgoto.h"
  63. string itoa( int i )
  64. {
  65. char buf[16];
  66. sprintf( buf, "%i", i );
  67. return buf;
  68. }
  69. using std::cout;
  70. using std::cerr;
  71. using std::endl;
  72. /* Invoked by the parser when a ragel definition is opened. */
  73. CodeGenData *dotMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  74. {
  75. CodeGenData *codeGen = new GraphvizDotGen(out);
  76. codeGen->sourceFileName = sourceFileName;
  77. codeGen->fsmName = fsmName;
  78. /* For normal code generation we want a transition on every character so we never
  79. * end up in an undefined state. For graphviz this just clutters the
  80. * drawing so we turn it off. */
  81. codeGen->wantComplete = false;
  82. return codeGen;
  83. }
  84. /* Invoked by the parser when a ragel definition is opened. */
  85. CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  86. {
  87. CodeGenData *codeGen = 0;
  88. switch ( hostLang->lang ) {
  89. case HostLang::C:
  90. switch ( codeStyle ) {
  91. case GenTables:
  92. codeGen = new CTabCodeGen(out);
  93. break;
  94. case GenFTables:
  95. codeGen = new CFTabCodeGen(out);
  96. break;
  97. case GenFlat:
  98. codeGen = new CFlatCodeGen(out);
  99. break;
  100. case GenFFlat:
  101. codeGen = new CFFlatCodeGen(out);
  102. break;
  103. case GenGoto:
  104. codeGen = new CGotoCodeGen(out);
  105. break;
  106. case GenFGoto:
  107. codeGen = new CFGotoCodeGen(out);
  108. break;
  109. case GenIpGoto:
  110. codeGen = new CIpGotoCodeGen(out);
  111. break;
  112. case GenSplit:
  113. codeGen = new CSplitCodeGen(out);
  114. break;
  115. }
  116. break;
  117. case HostLang::D:
  118. switch ( codeStyle ) {
  119. case GenTables:
  120. codeGen = new DTabCodeGen(out);
  121. break;
  122. case GenFTables:
  123. codeGen = new DFTabCodeGen(out);
  124. break;
  125. case GenFlat:
  126. codeGen = new DFlatCodeGen(out);
  127. break;
  128. case GenFFlat:
  129. codeGen = new DFFlatCodeGen(out);
  130. break;
  131. case GenGoto:
  132. codeGen = new DGotoCodeGen(out);
  133. break;
  134. case GenFGoto:
  135. codeGen = new DFGotoCodeGen(out);
  136. break;
  137. case GenIpGoto:
  138. codeGen = new DIpGotoCodeGen(out);
  139. break;
  140. case GenSplit:
  141. codeGen = new DSplitCodeGen(out);
  142. break;
  143. }
  144. break;
  145. case HostLang::D2:
  146. switch ( codeStyle ) {
  147. case GenTables:
  148. codeGen = new D2TabCodeGen(out);
  149. break;
  150. case GenFTables:
  151. codeGen = new D2FTabCodeGen(out);
  152. break;
  153. case GenFlat:
  154. codeGen = new D2FlatCodeGen(out);
  155. break;
  156. case GenFFlat:
  157. codeGen = new D2FFlatCodeGen(out);
  158. break;
  159. case GenGoto:
  160. codeGen = new D2GotoCodeGen(out);
  161. break;
  162. case GenFGoto:
  163. codeGen = new D2FGotoCodeGen(out);
  164. break;
  165. case GenIpGoto:
  166. codeGen = new D2IpGotoCodeGen(out);
  167. break;
  168. case GenSplit:
  169. codeGen = new D2SplitCodeGen(out);
  170. break;
  171. }
  172. break;
  173. default: break;
  174. }
  175. codeGen->sourceFileName = sourceFileName;
  176. codeGen->fsmName = fsmName;
  177. return codeGen;
  178. }
  179. /* Invoked by the parser when a ragel definition is opened. */
  180. CodeGenData *javaMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  181. {
  182. CodeGenData *codeGen = new JavaTabCodeGen(out);
  183. codeGen->sourceFileName = sourceFileName;
  184. codeGen->fsmName = fsmName;
  185. return codeGen;
  186. }
  187. /* Invoked by the parser when a ragel definition is opened. */
  188. CodeGenData *goMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  189. {
  190. CodeGenData *codeGen = 0;
  191. switch ( codeStyle ) {
  192. case GenTables:
  193. codeGen = new GoTabCodeGen(out);
  194. break;
  195. case GenFTables:
  196. codeGen = new GoFTabCodeGen(out);
  197. break;
  198. case GenFlat:
  199. codeGen = new GoFlatCodeGen(out);
  200. break;
  201. case GenFFlat:
  202. codeGen = new GoFFlatCodeGen(out);
  203. break;
  204. case GenGoto:
  205. codeGen = new GoGotoCodeGen(out);
  206. break;
  207. case GenFGoto:
  208. codeGen = new GoFGotoCodeGen(out);
  209. break;
  210. case GenIpGoto:
  211. codeGen = new GoIpGotoCodeGen(out);
  212. break;
  213. default:
  214. cerr << "Invalid output style, only -T0, -T1, -F0, -F1, -G0, -G1 and -G2 are supported for Go.\n";
  215. exit(1);
  216. }
  217. codeGen->sourceFileName = sourceFileName;
  218. codeGen->fsmName = fsmName;
  219. return codeGen;
  220. }
  221. /* Invoked by the parser when a ragel definition is opened. */
  222. CodeGenData *rubyMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  223. {
  224. CodeGenData *codeGen = 0;
  225. switch ( codeStyle ) {
  226. case GenTables:
  227. codeGen = new RubyTabCodeGen(out);
  228. break;
  229. case GenFTables:
  230. codeGen = new RubyFTabCodeGen(out);
  231. break;
  232. case GenFlat:
  233. codeGen = new RubyFlatCodeGen(out);
  234. break;
  235. case GenFFlat:
  236. codeGen = new RubyFFlatCodeGen(out);
  237. break;
  238. case GenGoto:
  239. if ( rubyImpl == Rubinius ) {
  240. codeGen = new RbxGotoCodeGen(out);
  241. } else {
  242. cerr << "Goto style is still _very_ experimental "
  243. "and only supported using Rubinius.\n"
  244. "You may want to enable the --rbx flag "
  245. " to give it a try.\n";
  246. exit(1);
  247. }
  248. break;
  249. default:
  250. cout << "Invalid code style\n";
  251. exit(1);
  252. break;
  253. }
  254. codeGen->sourceFileName = sourceFileName;
  255. codeGen->fsmName = fsmName;
  256. return codeGen;
  257. }
  258. /* Invoked by the parser when a ragel definition is opened. */
  259. CodeGenData *csharpMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  260. {
  261. CodeGenData *codeGen = 0;
  262. switch ( codeStyle ) {
  263. case GenTables:
  264. codeGen = new CSharpTabCodeGen(out);
  265. break;
  266. case GenFTables:
  267. codeGen = new CSharpFTabCodeGen(out);
  268. break;
  269. case GenFlat:
  270. codeGen = new CSharpFlatCodeGen(out);
  271. break;
  272. case GenFFlat:
  273. codeGen = new CSharpFFlatCodeGen(out);
  274. break;
  275. case GenGoto:
  276. codeGen = new CSharpGotoCodeGen(out);
  277. break;
  278. case GenFGoto:
  279. codeGen = new CSharpFGotoCodeGen(out);
  280. break;
  281. case GenIpGoto:
  282. codeGen = new CSharpIpGotoCodeGen(out);
  283. break;
  284. case GenSplit:
  285. codeGen = new CSharpSplitCodeGen(out);
  286. break;
  287. }
  288. codeGen->sourceFileName = sourceFileName;
  289. codeGen->fsmName = fsmName;
  290. return codeGen;
  291. }
  292. /* Invoked by the parser when a ragel definition is opened. */
  293. CodeGenData *ocamlMakeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  294. {
  295. CodeGenData *codeGen = 0;
  296. switch ( codeStyle ) {
  297. case GenTables:
  298. codeGen = new OCamlTabCodeGen(out);
  299. break;
  300. case GenFTables:
  301. codeGen = new OCamlFTabCodeGen(out);
  302. break;
  303. case GenFlat:
  304. codeGen = new OCamlFlatCodeGen(out);
  305. break;
  306. case GenFFlat:
  307. codeGen = new OCamlFFlatCodeGen(out);
  308. break;
  309. case GenGoto:
  310. codeGen = new OCamlGotoCodeGen(out);
  311. break;
  312. case GenFGoto:
  313. codeGen = new OCamlFGotoCodeGen(out);
  314. break;
  315. default:
  316. cerr << "I only support the -T0 -T1 -F0 -F1 -G0 and -G1 output styles for OCaml.\n";
  317. exit(1);
  318. }
  319. codeGen->sourceFileName = sourceFileName;
  320. codeGen->fsmName = fsmName;
  321. return codeGen;
  322. }
  323. CodeGenData *makeCodeGen( const char *sourceFileName, const char *fsmName, ostream &out )
  324. {
  325. CodeGenData *cgd = 0;
  326. if ( generateDot )
  327. cgd = dotMakeCodeGen( sourceFileName, fsmName, out );
  328. else if ( hostLang == &hostLangC )
  329. cgd = cdMakeCodeGen( sourceFileName, fsmName, out );
  330. else if ( hostLang == &hostLangD )
  331. cgd = cdMakeCodeGen( sourceFileName, fsmName, out );
  332. else if ( hostLang == &hostLangD2 )
  333. cgd = cdMakeCodeGen( sourceFileName, fsmName, out );
  334. else if ( hostLang == &hostLangGo )
  335. cgd = goMakeCodeGen( sourceFileName, fsmName, out );
  336. else if ( hostLang == &hostLangJava )
  337. cgd = javaMakeCodeGen( sourceFileName, fsmName, out );
  338. else if ( hostLang == &hostLangRuby )
  339. cgd = rubyMakeCodeGen( sourceFileName, fsmName, out );
  340. else if ( hostLang == &hostLangCSharp )
  341. cgd = csharpMakeCodeGen( sourceFileName, fsmName, out );
  342. else if ( hostLang == &hostLangOCaml )
  343. cgd = ocamlMakeCodeGen( sourceFileName, fsmName, out );
  344. return cgd;
  345. }
  346. void lineDirective( ostream &out, const char *fileName, int line )
  347. {
  348. if ( !generateDot ) {
  349. if ( hostLang == &hostLangC )
  350. cdLineDirective( out, fileName, line );
  351. else if ( hostLang == &hostLangD )
  352. cdLineDirective( out, fileName, line );
  353. else if ( hostLang == &hostLangD2 )
  354. cdLineDirective( out, fileName, line );
  355. else if ( hostLang == &hostLangGo )
  356. goLineDirective( out, fileName, line );
  357. else if ( hostLang == &hostLangJava )
  358. javaLineDirective( out, fileName, line );
  359. else if ( hostLang == &hostLangRuby )
  360. rubyLineDirective( out, fileName, line );
  361. else if ( hostLang == &hostLangCSharp )
  362. csharpLineDirective( out, fileName, line );
  363. else if ( hostLang == &hostLangOCaml )
  364. ocamlLineDirective( out, fileName, line );
  365. }
  366. }
  367. void genLineDirective( ostream &out )
  368. {
  369. std::streambuf *sbuf = out.rdbuf();
  370. output_filter *filter = static_cast<output_filter*>(sbuf);
  371. lineDirective( out, filter->fileName, filter->line + 1 );
  372. }
  373. /* Total error count. */
  374. /* int gblErrorCount = 0; */
  375. CodeGenData::CodeGenData( ostream &out )
  376. :
  377. sourceFileName(0),
  378. fsmName(0),
  379. out(out),
  380. redFsm(0),
  381. allActions(0),
  382. allActionTables(0),
  383. allConditions(0),
  384. allCondSpaces(0),
  385. allStates(0),
  386. nameIndex(0),
  387. startState(-1),
  388. errState(-1),
  389. getKeyExpr(0),
  390. accessExpr(0),
  391. prePushExpr(0),
  392. postPopExpr(0),
  393. pExpr(0),
  394. peExpr(0),
  395. eofExpr(0),
  396. csExpr(0),
  397. topExpr(0),
  398. stackExpr(0),
  399. actExpr(0),
  400. tokstartExpr(0),
  401. tokendExpr(0),
  402. dataExpr(0),
  403. wantComplete(true),
  404. hasLongestMatch(false),
  405. noEnd(false),
  406. noPrefix(false),
  407. noFinal(false),
  408. noError(false),
  409. noEntry(false),
  410. noCS(false)
  411. {}
  412. void CodeGenData::createMachine()
  413. {
  414. redFsm = new RedFsmAp();
  415. }
  416. void CodeGenData::initActionList( unsigned long length )
  417. {
  418. allActions = new GenAction[length];
  419. for ( unsigned long a = 0; a < length; a++ )
  420. actionList.append( allActions+a );
  421. }
  422. void CodeGenData::newAction( int anum, const char *name,
  423. const InputLoc &loc, GenInlineList *inlineList )
  424. {
  425. allActions[anum].actionId = anum;
  426. allActions[anum].name = name;
  427. allActions[anum].loc = loc;
  428. allActions[anum].inlineList = inlineList;
  429. }
  430. void CodeGenData::initActionTableList( unsigned long length )
  431. {
  432. allActionTables = new RedAction[length];
  433. }
  434. void CodeGenData::initStateList( unsigned long length )
  435. {
  436. allStates = new RedStateAp[length];
  437. for ( unsigned long s = 0; s < length; s++ )
  438. redFsm->stateList.append( allStates+s );
  439. /* We get the start state as an offset, set the pointer now. */
  440. if ( startState >= 0 )
  441. redFsm->startState = allStates + startState;
  442. if ( errState >= 0 )
  443. redFsm->errState = allStates + errState;
  444. for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ )
  445. redFsm->entryPoints.insert( allStates + *en );
  446. /* The nextStateId is no longer used to assign state ids (they come in set
  447. * from the frontend now), however generation code still depends on it.
  448. * Should eventually remove this variable. */
  449. redFsm->nextStateId = redFsm->stateList.length();
  450. }
  451. void CodeGenData::setStartState( unsigned long startState )
  452. {
  453. this->startState = startState;
  454. }
  455. void CodeGenData::setErrorState( unsigned long errState )
  456. {
  457. this->errState = errState;
  458. }
  459. void CodeGenData::addEntryPoint( char *name, unsigned long entryState )
  460. {
  461. entryPointIds.append( entryState );
  462. entryPointNames.append( name );
  463. }
  464. void CodeGenData::initTransList( int snum, unsigned long length )
  465. {
  466. /* Could preallocate the out range to save time growing it. For now do
  467. * nothing. */
  468. }
  469. void CodeGenData::newTrans( int snum, int tnum, Key lowKey,
  470. Key highKey, long targ, long action )
  471. {
  472. /* Get the current state and range. */
  473. RedStateAp *curState = allStates + snum;
  474. RedTransList &destRange = curState->outRange;
  475. if ( curState == redFsm->errState )
  476. return;
  477. /* Make the new transitions. */
  478. RedStateAp *targState = targ >= 0 ? (allStates + targ) :
  479. wantComplete ? redFsm->getErrorState() : 0;
  480. RedAction *actionTable = action >= 0 ? (allActionTables + action) : 0;
  481. RedTransAp *trans = redFsm->allocateTrans( targState, actionTable );
  482. RedTransEl transEl( lowKey, highKey, trans );
  483. if ( wantComplete ) {
  484. /* If the machine is to be complete then we need to fill any gaps with
  485. * the error transitions. */
  486. if ( destRange.length() == 0 ) {
  487. /* Range is currently empty. */
  488. if ( keyOps->minKey < lowKey ) {
  489. /* The first range doesn't start at the low end. */
  490. Key fillHighKey = lowKey;
  491. fillHighKey.decrement();
  492. /* Create the filler with the state's error transition. */
  493. RedTransEl newTel( keyOps->minKey, fillHighKey, redFsm->getErrorTrans() );
  494. destRange.append( newTel );
  495. }
  496. }
  497. else {
  498. /* The range list is not empty, get the the last range. */
  499. RedTransEl *last = &destRange[destRange.length()-1];
  500. Key nextKey = last->highKey;
  501. nextKey.increment();
  502. if ( nextKey < lowKey ) {
  503. /* There is a gap to fill. Make the high key. */
  504. Key fillHighKey = lowKey;
  505. fillHighKey.decrement();
  506. /* Create the filler with the state's error transtion. */
  507. RedTransEl newTel( nextKey, fillHighKey, redFsm->getErrorTrans() );
  508. destRange.append( newTel );
  509. }
  510. }
  511. }
  512. /* Filler taken care of. Append the range. */
  513. destRange.append( RedTransEl( lowKey, highKey, trans ) );
  514. }
  515. void CodeGenData::finishTransList( int snum )
  516. {
  517. /* Get the current state and range. */
  518. RedStateAp *curState = allStates + snum;
  519. RedTransList &destRange = curState->outRange;
  520. if ( curState == redFsm->errState )
  521. return;
  522. /* If building a complete machine we may need filler on the end. */
  523. if ( wantComplete ) {
  524. /* Check if there are any ranges already. */
  525. if ( destRange.length() == 0 ) {
  526. /* Fill with the whole alphabet. */
  527. /* Add the range on the lower and upper bound. */
  528. RedTransEl newTel( keyOps->minKey, keyOps->maxKey, redFsm->getErrorTrans() );
  529. destRange.append( newTel );
  530. }
  531. else {
  532. /* Get the last and check for a gap on the end. */
  533. RedTransEl *last = &destRange[destRange.length()-1];
  534. if ( last->highKey < keyOps->maxKey ) {
  535. /* Make the high key. */
  536. Key fillLowKey = last->highKey;
  537. fillLowKey.increment();
  538. /* Create the new range with the error trans and append it. */
  539. RedTransEl newTel( fillLowKey, keyOps->maxKey, redFsm->getErrorTrans() );
  540. destRange.append( newTel );
  541. }
  542. }
  543. }
  544. }
  545. void CodeGenData::setId( int snum, int id )
  546. {
  547. RedStateAp *curState = allStates + snum;
  548. curState->id = id;
  549. }
  550. void CodeGenData::setFinal( int snum )
  551. {
  552. RedStateAp *curState = allStates + snum;
  553. curState->isFinal = true;
  554. }
  555. void CodeGenData::setStateActions( int snum, long toStateAction,
  556. long fromStateAction, long eofAction )
  557. {
  558. RedStateAp *curState = allStates + snum;
  559. if ( toStateAction >= 0 )
  560. curState->toStateAction = allActionTables + toStateAction;
  561. if ( fromStateAction >= 0 )
  562. curState->fromStateAction = allActionTables + fromStateAction;
  563. if ( eofAction >= 0 )
  564. curState->eofAction = allActionTables + eofAction;
  565. }
  566. void CodeGenData::setEofTrans( int snum, long eofTarget, long actId )
  567. {
  568. RedStateAp *curState = allStates + snum;
  569. RedStateAp *targState = allStates + eofTarget;
  570. RedAction *eofAct = allActionTables + actId;
  571. curState->eofTrans = redFsm->allocateTrans( targState, eofAct );
  572. }
  573. void CodeGenData::resolveTargetStates( GenInlineList *inlineList )
  574. {
  575. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  576. switch ( item->type ) {
  577. case GenInlineItem::Goto: case GenInlineItem::Call:
  578. case GenInlineItem::Next: case GenInlineItem::Entry:
  579. item->targState = allStates + item->targId;
  580. break;
  581. default:
  582. break;
  583. }
  584. if ( item->children != 0 )
  585. resolveTargetStates( item->children );
  586. }
  587. }
  588. void CodeGenData::closeMachine()
  589. {
  590. for ( GenActionList::Iter a = actionList; a.lte(); a++ )
  591. resolveTargetStates( a->inlineList );
  592. /* Note that even if we want a complete graph we do not give the error
  593. * state a default transition. All machines break out of the processing
  594. * loop when in the error state. */
  595. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  596. for ( GenStateCondList::Iter sci = st->stateCondList; sci.lte(); sci++ )
  597. st->stateCondVect.append( sci );
  598. }
  599. }
  600. bool CodeGenData::setAlphType( const char *data )
  601. {
  602. HostType *alphType = findAlphTypeInternal( data );
  603. if ( alphType == 0 )
  604. return false;
  605. thisKeyOps.setAlphType( alphType );
  606. return true;
  607. }
  608. void CodeGenData::initCondSpaceList( ulong length )
  609. {
  610. allCondSpaces = new GenCondSpace[length];
  611. for ( ulong c = 0; c < length; c++ )
  612. condSpaceList.append( allCondSpaces + c );
  613. }
  614. void CodeGenData::newCondSpace( int cnum, int condSpaceId, Key baseKey )
  615. {
  616. GenCondSpace *cond = allCondSpaces + cnum;
  617. cond->condSpaceId = condSpaceId;
  618. cond->baseKey = baseKey;
  619. }
  620. void CodeGenData::condSpaceItem( int cnum, long condActionId )
  621. {
  622. GenCondSpace *cond = allCondSpaces + cnum;
  623. cond->condSet.append( allActions + condActionId );
  624. }
  625. void CodeGenData::initStateCondList( int snum, ulong length )
  626. {
  627. /* Could preallocate these, as we could with transitions. */
  628. }
  629. void CodeGenData::addStateCond( int snum, Key lowKey, Key highKey, long condNum )
  630. {
  631. RedStateAp *curState = allStates + snum;
  632. /* Create the new state condition. */
  633. GenStateCond *stateCond = new GenStateCond;
  634. stateCond->lowKey = lowKey;
  635. stateCond->highKey = highKey;
  636. /* Assign it a cond space. */
  637. GenCondSpace *condSpace = allCondSpaces + condNum;
  638. stateCond->condSpace = condSpace;
  639. curState->stateCondList.append( stateCond );
  640. }
  641. GenCondSpace *CodeGenData::findCondSpace( Key lowKey, Key highKey )
  642. {
  643. for ( CondSpaceList::Iter cs = condSpaceList; cs.lte(); cs++ ) {
  644. Key csHighKey = cs->baseKey;
  645. csHighKey += keyOps->alphSize() * (1 << cs->condSet.length());
  646. if ( lowKey >= cs->baseKey && highKey <= csHighKey )
  647. return cs;
  648. }
  649. return 0;
  650. }
  651. Condition *CodeGenData::findCondition( Key key )
  652. {
  653. for ( ConditionList::Iter cond = conditionList; cond.lte(); cond++ ) {
  654. Key upperKey = cond->baseKey + (1 << cond->condSet.length());
  655. if ( cond->baseKey <= key && key <= upperKey )
  656. return cond;
  657. }
  658. return 0;
  659. }
  660. Key CodeGenData::findMaxKey()
  661. {
  662. Key maxKey = keyOps->maxKey;
  663. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  664. assert( st->outSingle.length() == 0 );
  665. assert( st->defTrans == 0 );
  666. long rangeLen = st->outRange.length();
  667. if ( rangeLen > 0 ) {
  668. Key highKey = st->outRange[rangeLen-1].highKey;
  669. if ( highKey > maxKey )
  670. maxKey = highKey;
  671. }
  672. }
  673. return maxKey;
  674. }
  675. void CodeGenData::findFinalActionRefs()
  676. {
  677. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  678. /* Rerence count out of single transitions. */
  679. for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
  680. if ( rtel->value->action != 0 ) {
  681. rtel->value->action->numTransRefs += 1;
  682. for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
  683. item->value->numTransRefs += 1;
  684. }
  685. }
  686. /* Reference count out of range transitions. */
  687. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  688. if ( rtel->value->action != 0 ) {
  689. rtel->value->action->numTransRefs += 1;
  690. for ( GenActionTable::Iter item = rtel->value->action->key; item.lte(); item++ )
  691. item->value->numTransRefs += 1;
  692. }
  693. }
  694. /* Reference count default transition. */
  695. if ( st->defTrans != 0 && st->defTrans->action != 0 ) {
  696. st->defTrans->action->numTransRefs += 1;
  697. for ( GenActionTable::Iter item = st->defTrans->action->key; item.lte(); item++ )
  698. item->value->numTransRefs += 1;
  699. }
  700. /* Reference count eof transitions. */
  701. if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
  702. st->eofTrans->action->numTransRefs += 1;
  703. for ( GenActionTable::Iter item = st->eofTrans->action->key; item.lte(); item++ )
  704. item->value->numTransRefs += 1;
  705. }
  706. /* Reference count to state actions. */
  707. if ( st->toStateAction != 0 ) {
  708. st->toStateAction->numToStateRefs += 1;
  709. for ( GenActionTable::Iter item = st->toStateAction->key; item.lte(); item++ )
  710. item->value->numToStateRefs += 1;
  711. }
  712. /* Reference count from state actions. */
  713. if ( st->fromStateAction != 0 ) {
  714. st->fromStateAction->numFromStateRefs += 1;
  715. for ( GenActionTable::Iter item = st->fromStateAction->key; item.lte(); item++ )
  716. item->value->numFromStateRefs += 1;
  717. }
  718. /* Reference count EOF actions. */
  719. if ( st->eofAction != 0 ) {
  720. st->eofAction->numEofRefs += 1;
  721. for ( GenActionTable::Iter item = st->eofAction->key; item.lte(); item++ )
  722. item->value->numEofRefs += 1;
  723. }
  724. }
  725. }
  726. void CodeGenData::analyzeAction( GenAction *act, GenInlineList *inlineList )
  727. {
  728. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  729. /* Only consider actions that are referenced. */
  730. if ( act->numRefs() > 0 ) {
  731. if ( item->type == GenInlineItem::Goto || item->type == GenInlineItem::GotoExpr )
  732. redFsm->bAnyActionGotos = true;
  733. else if ( item->type == GenInlineItem::Call || item->type == GenInlineItem::CallExpr )
  734. redFsm->bAnyActionCalls = true;
  735. else if ( item->type == GenInlineItem::Ret )
  736. redFsm->bAnyActionRets = true;
  737. if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
  738. redFsm->bAnyActionByValControl = true;
  739. }
  740. /* Check for various things in regular actions. */
  741. if ( act->numTransRefs > 0 || act->numToStateRefs > 0 || act->numFromStateRefs > 0 ) {
  742. /* Any returns in regular actions? */
  743. if ( item->type == GenInlineItem::Ret )
  744. redFsm->bAnyRegActionRets = true;
  745. /* Any next statements in the regular actions? */
  746. if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
  747. redFsm->bAnyRegNextStmt = true;
  748. /* Any by value control in regular actions? */
  749. if ( item->type == GenInlineItem::CallExpr || item->type == GenInlineItem::GotoExpr )
  750. redFsm->bAnyRegActionByValControl = true;
  751. /* Any references to the current state in regular actions? */
  752. if ( item->type == GenInlineItem::Curs )
  753. redFsm->bAnyRegCurStateRef = true;
  754. if ( item->type == GenInlineItem::Break )
  755. redFsm->bAnyRegBreak = true;
  756. }
  757. if ( item->children != 0 )
  758. analyzeAction( act, item->children );
  759. }
  760. }
  761. void CodeGenData::analyzeActionList( RedAction *redAct, GenInlineList *inlineList )
  762. {
  763. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  764. /* Any next statements in the action table? */
  765. if ( item->type == GenInlineItem::Next || item->type == GenInlineItem::NextExpr )
  766. redAct->bAnyNextStmt = true;
  767. /* Any references to the current state. */
  768. if ( item->type == GenInlineItem::Curs )
  769. redAct->bAnyCurStateRef = true;
  770. if ( item->type == GenInlineItem::Break )
  771. redAct->bAnyBreakStmt = true;
  772. if ( item->children != 0 )
  773. analyzeActionList( redAct, item->children );
  774. }
  775. }
  776. /* Assign ids to referenced actions. */
  777. void CodeGenData::assignActionIds()
  778. {
  779. int nextActionId = 0;
  780. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  781. /* Only ever interested in referenced actions. */
  782. if ( act->numRefs() > 0 )
  783. act->actionId = nextActionId++;
  784. }
  785. }
  786. void CodeGenData::setValueLimits()
  787. {
  788. redFsm->maxSingleLen = 0;
  789. redFsm->maxRangeLen = 0;
  790. redFsm->maxKeyOffset = 0;
  791. redFsm->maxIndexOffset = 0;
  792. redFsm->maxActListId = 0;
  793. redFsm->maxActionLoc = 0;
  794. redFsm->maxActArrItem = 0;
  795. redFsm->maxSpan = 0;
  796. redFsm->maxCondSpan = 0;
  797. redFsm->maxFlatIndexOffset = 0;
  798. redFsm->maxCondOffset = 0;
  799. redFsm->maxCondLen = 0;
  800. redFsm->maxCondSpaceId = 0;
  801. redFsm->maxCondIndexOffset = 0;
  802. /* In both of these cases the 0 index is reserved for no value, so the max
  803. * is one more than it would be if they started at 0. */
  804. redFsm->maxIndex = redFsm->transSet.length();
  805. redFsm->maxCond = condSpaceList.length();
  806. /* The nextStateId - 1 is the last state id assigned. */
  807. redFsm->maxState = redFsm->nextStateId - 1;
  808. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  809. if ( csi->condSpaceId > redFsm->maxCondSpaceId )
  810. redFsm->maxCondSpaceId = csi->condSpaceId;
  811. }
  812. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  813. /* Maximum cond length. */
  814. if ( st->stateCondList.length() > redFsm->maxCondLen )
  815. redFsm->maxCondLen = st->stateCondList.length();
  816. /* Maximum single length. */
  817. if ( st->outSingle.length() > redFsm->maxSingleLen )
  818. redFsm->maxSingleLen = st->outSingle.length();
  819. /* Maximum range length. */
  820. if ( st->outRange.length() > redFsm->maxRangeLen )
  821. redFsm->maxRangeLen = st->outRange.length();
  822. /* The key offset index offset for the state after last is not used, skip it.. */
  823. if ( ! st.last() ) {
  824. redFsm->maxCondOffset += st->stateCondList.length();
  825. redFsm->maxKeyOffset += st->outSingle.length() + st->outRange.length()*2;
  826. redFsm->maxIndexOffset += st->outSingle.length() + st->outRange.length() + 2;
  827. }
  828. /* Max cond span. */
  829. if ( st->condList != 0 ) {
  830. unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
  831. if ( span > redFsm->maxCondSpan )
  832. redFsm->maxCondSpan = span;
  833. }
  834. /* Max key span. */
  835. if ( st->transList != 0 ) {
  836. unsigned long long span = keyOps->span( st->lowKey, st->highKey );
  837. if ( span > redFsm->maxSpan )
  838. redFsm->maxSpan = span;
  839. }
  840. /* Max cond index offset. */
  841. if ( ! st.last() ) {
  842. if ( st->condList != 0 )
  843. redFsm->maxCondIndexOffset += keyOps->span( st->condLowKey, st->condHighKey );
  844. }
  845. /* Max flat index offset. */
  846. if ( ! st.last() ) {
  847. if ( st->transList != 0 )
  848. redFsm->maxFlatIndexOffset += keyOps->span( st->lowKey, st->highKey );
  849. redFsm->maxFlatIndexOffset += 1;
  850. }
  851. }
  852. for ( GenActionTableMap::Iter at = redFsm->actionMap; at.lte(); at++ ) {
  853. /* Maximum id of action lists. */
  854. if ( at->actListId+1 > redFsm->maxActListId )
  855. redFsm->maxActListId = at->actListId+1;
  856. /* Maximum location of items in action array. */
  857. if ( at->location+1 > redFsm->maxActionLoc )
  858. redFsm->maxActionLoc = at->location+1;
  859. /* Maximum values going into the action array. */
  860. if ( at->key.length() > redFsm->maxActArrItem )
  861. redFsm->maxActArrItem = at->key.length();
  862. for ( GenActionTable::Iter item = at->key; item.lte(); item++ ) {
  863. if ( item->value->actionId > redFsm->maxActArrItem )
  864. redFsm->maxActArrItem = item->value->actionId;
  865. }
  866. }
  867. }
  868. /* Gather various info on the machine. */
  869. void CodeGenData::analyzeMachine()
  870. {
  871. /* Find the true count of action references. */
  872. findFinalActionRefs();
  873. /* Check if there are any calls in action code. */
  874. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  875. /* Record the occurrence of various kinds of actions. */
  876. if ( act->numToStateRefs > 0 )
  877. redFsm->bAnyToStateActions = true;
  878. if ( act->numFromStateRefs > 0 )
  879. redFsm->bAnyFromStateActions = true;
  880. if ( act->numEofRefs > 0 )
  881. redFsm->bAnyEofActions = true;
  882. if ( act->numTransRefs > 0 )
  883. redFsm->bAnyRegActions = true;
  884. /* Recurse through the action's parse tree looking for various things. */
  885. analyzeAction( act, act->inlineList );
  886. }
  887. /* Analyze reduced action lists. */
  888. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  889. for ( GenActionTable::Iter act = redAct->key; act.lte(); act++ )
  890. analyzeActionList( redAct, act->value->inlineList );
  891. }
  892. /* Find states that have transitions with actions that have next
  893. * statements. */
  894. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  895. /* Check any actions out of outSinge. */
  896. for ( RedTransList::Iter rtel = st->outSingle; rtel.lte(); rtel++ ) {
  897. if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
  898. st->bAnyRegCurStateRef = true;
  899. }
  900. /* Check any actions out of outRange. */
  901. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  902. if ( rtel->value->action != 0 && rtel->value->action->anyCurStateRef() )
  903. st->bAnyRegCurStateRef = true;
  904. }
  905. /* Check any action out of default. */
  906. if ( st->defTrans != 0 && st->defTrans->action != 0 &&
  907. st->defTrans->action->anyCurStateRef() )
  908. st->bAnyRegCurStateRef = true;
  909. if ( st->stateCondList.length() > 0 )
  910. redFsm->bAnyConditions = true;
  911. if ( st->eofTrans != 0 )
  912. redFsm->bAnyEofTrans = true;
  913. }
  914. /* Assign ids to actions that are referenced. */
  915. assignActionIds();
  916. /* Set the maximums of various values used for deciding types. */
  917. setValueLimits();
  918. }
  919. void CodeGenData::write_option_error( InputLoc &loc, char *arg )
  920. {
  921. source_warning(loc) << "unrecognized write option \"" << arg << "\"" << endl;
  922. }
  923. /* returns true if the following section should generate line directives. */
  924. bool CodeGenData::writeStatement( InputLoc &loc, int nargs, char **args )
  925. {
  926. bool followLineDirective = false;
  927. if ( strcmp( args[0], "data" ) == 0 ) {
  928. out << '\n';
  929. genLineDirective( out );
  930. followLineDirective = true;
  931. for ( int i = 1; i < nargs; i++ ) {
  932. if ( strcmp( args[i], "noerror" ) == 0 )
  933. noError = true;
  934. else if ( strcmp( args[i], "noprefix" ) == 0 )
  935. noPrefix = true;
  936. else if ( strcmp( args[i], "nofinal" ) == 0 )
  937. noFinal = true;
  938. else if ( strcmp( args[i], "noentry" ) == 0 )
  939. noEntry = true;
  940. else
  941. write_option_error( loc, args[i] );
  942. }
  943. writeData();
  944. }
  945. else if ( strcmp( args[0], "init" ) == 0 ) {
  946. out << '\n';
  947. genLineDirective( out );
  948. followLineDirective = true;
  949. for ( int i = 1; i < nargs; i++ ) {
  950. if ( strcmp( args[i], "nocs" ) == 0 )
  951. noCS = true;
  952. else
  953. write_option_error( loc, args[i] );
  954. }
  955. writeInit();
  956. }
  957. else if ( strcmp( args[0], "exec" ) == 0 ) {
  958. out << '\n';
  959. genLineDirective( out );
  960. followLineDirective = true;
  961. for ( int i = 1; i < nargs; i++ ) {
  962. if ( strcmp( args[i], "noend" ) == 0 )
  963. noEnd = true;
  964. else
  965. write_option_error( loc, args[i] );
  966. }
  967. writeExec();
  968. }
  969. else if ( strcmp( args[0], "exports" ) == 0 ) {
  970. out << '\n';
  971. genLineDirective( out );
  972. followLineDirective = true;
  973. for ( int i = 1; i < nargs; i++ )
  974. write_option_error( loc, args[i] );
  975. writeExports();
  976. }
  977. else if ( strcmp( args[0], "start" ) == 0 ) {
  978. for ( int i = 1; i < nargs; i++ )
  979. write_option_error( loc, args[i] );
  980. writeStart();
  981. }
  982. else if ( strcmp( args[0], "first_final" ) == 0 ) {
  983. for ( int i = 1; i < nargs; i++ )
  984. write_option_error( loc, args[i] );
  985. writeFirstFinal();
  986. }
  987. else if ( strcmp( args[0], "error" ) == 0 ) {
  988. for ( int i = 1; i < nargs; i++ )
  989. write_option_error( loc, args[i] );
  990. writeError();
  991. }
  992. else {
  993. /* EMIT An error here. */
  994. source_error(loc) << "unrecognized write command \"" <<
  995. args[0] << "\"" << endl;
  996. }
  997. return followLineDirective;
  998. }
  999. ostream &CodeGenData::source_warning( const InputLoc &loc )
  1000. {
  1001. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
  1002. return cerr;
  1003. }
  1004. ostream &CodeGenData::source_error( const InputLoc &loc )
  1005. {
  1006. gblErrorCount += 1;
  1007. assert( sourceFileName != 0 );
  1008. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
  1009. return cerr;
  1010. }