javacodegen.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. * Copyright 2006-2007 Adrian Thurston <thurston@complang.org>
  3. * 2007 Colin Fleming <colin.fleming@caverock.com>
  4. */
  5. /* This file is part of Ragel.
  6. *
  7. * Ragel is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ragel is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ragel; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "ragel.h"
  22. #include "javacodegen.h"
  23. #include "redfsm.h"
  24. #include "gendata.h"
  25. #include <iomanip>
  26. #include <sstream>
  27. /* Integer array line length. */
  28. #define IALL 12
  29. /* Static array initialization item count
  30. * (should be multiple of IALL). */
  31. #define SAIIC 8184
  32. #define _resume 1
  33. #define _again 2
  34. #define _eof_trans 3
  35. #define _test_eof 4
  36. #define _out 5
  37. using std::setw;
  38. using std::ios;
  39. using std::ostringstream;
  40. using std::string;
  41. using std::cerr;
  42. using std::istream;
  43. using std::ifstream;
  44. using std::ostream;
  45. using std::ios;
  46. using std::cin;
  47. using std::cout;
  48. using std::cerr;
  49. using std::endl;
  50. using std::setiosflags;
  51. void javaLineDirective( ostream &out, const char *fileName, int line )
  52. {
  53. /* Write the preprocessor line info for to the input file. */
  54. out << "// line " << line << " \"";
  55. for ( const char *pc = fileName; *pc != 0; pc++ ) {
  56. if ( *pc == '\\' )
  57. out << "\\\\";
  58. else
  59. out << *pc;
  60. }
  61. out << "\"\n";
  62. }
  63. void JavaTabCodeGen::genLineDirective( ostream &out )
  64. {
  65. std::streambuf *sbuf = out.rdbuf();
  66. output_filter *filter = static_cast<output_filter*>(sbuf);
  67. javaLineDirective( out, filter->fileName, filter->line + 1 );
  68. }
  69. void JavaTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
  70. {
  71. ret << "{" << vCS() << " = " << gotoDest << "; _goto_targ = " << _again << "; " <<
  72. CTRL_FLOW() << "continue _goto;}";
  73. }
  74. void JavaTabCodeGen::GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  75. {
  76. ret << "{" << vCS() << " = (";
  77. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  78. ret << "); _goto_targ = " << _again << "; " << CTRL_FLOW() << "continue _goto;}";
  79. }
  80. void JavaTabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
  81. {
  82. if ( prePushExpr != 0 ) {
  83. ret << "{";
  84. INLINE_LIST( ret, prePushExpr, 0, false );
  85. }
  86. ret << "{" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = " <<
  87. callDest << "; _goto_targ = " << _again << "; " << CTRL_FLOW() << "continue _goto;}";
  88. if ( prePushExpr != 0 )
  89. ret << "}";
  90. }
  91. void JavaTabCodeGen::CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish )
  92. {
  93. if ( prePushExpr != 0 ) {
  94. ret << "{";
  95. INLINE_LIST( ret, prePushExpr, 0, false );
  96. }
  97. ret << "{" << STACK() << "[" << TOP() << "++] = " << vCS() << "; " << vCS() << " = (";
  98. INLINE_LIST( ret, ilItem->children, targState, inFinish );
  99. ret << "); _goto_targ = " << _again << "; " << CTRL_FLOW() << "continue _goto;}";
  100. if ( prePushExpr != 0 )
  101. ret << "}";
  102. }
  103. void JavaTabCodeGen::RET( ostream &ret, bool inFinish )
  104. {
  105. ret << "{" << vCS() << " = " << STACK() << "[--" << TOP() << "];";
  106. if ( postPopExpr != 0 ) {
  107. ret << "{";
  108. INLINE_LIST( ret, postPopExpr, 0, false );
  109. ret << "}";
  110. }
  111. ret << "_goto_targ = " << _again << "; " << CTRL_FLOW() << "continue _goto;}";
  112. }
  113. void JavaTabCodeGen::BREAK( ostream &ret, int targState )
  114. {
  115. ret << "{ " << P() << " += 1; _goto_targ = " << _out << "; " <<
  116. CTRL_FLOW() << " continue _goto;}";
  117. }
  118. void JavaTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
  119. {
  120. ret << vCS() << " = " << nextDest << ";";
  121. }
  122. void JavaTabCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  123. {
  124. ret << vCS() << " = (";
  125. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  126. ret << ");";
  127. }
  128. void JavaTabCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
  129. {
  130. /* The parser gives fexec two children. The double brackets are for D
  131. * code. If the inline list is a single word it will get interpreted as a
  132. * C-style cast by the D compiler. */
  133. ret << "{" << P() << " = ((";
  134. INLINE_LIST( ret, item->children, targState, inFinish );
  135. ret << "))-1;}";
  136. }
  137. /* Write out an inline tree structure. Walks the list and possibly calls out
  138. * to virtual functions than handle language specific items in the tree. */
  139. void JavaTabCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList,
  140. int targState, bool inFinish )
  141. {
  142. for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
  143. switch ( item->type ) {
  144. case GenInlineItem::Text:
  145. ret << item->data;
  146. break;
  147. case GenInlineItem::Goto:
  148. GOTO( ret, item->targState->id, inFinish );
  149. break;
  150. case GenInlineItem::Call:
  151. CALL( ret, item->targState->id, targState, inFinish );
  152. break;
  153. case GenInlineItem::Next:
  154. NEXT( ret, item->targState->id, inFinish );
  155. break;
  156. case GenInlineItem::Ret:
  157. RET( ret, inFinish );
  158. break;
  159. case GenInlineItem::PChar:
  160. ret << P();
  161. break;
  162. case GenInlineItem::Char:
  163. ret << GET_KEY();
  164. break;
  165. case GenInlineItem::Hold:
  166. ret << P() << "--;";
  167. break;
  168. case GenInlineItem::Exec:
  169. EXEC( ret, item, targState, inFinish );
  170. break;
  171. case GenInlineItem::Curs:
  172. ret << "(_ps)";
  173. break;
  174. case GenInlineItem::Targs:
  175. ret << "(" << vCS() << ")";
  176. break;
  177. case GenInlineItem::Entry:
  178. ret << item->targState->id;
  179. break;
  180. case GenInlineItem::GotoExpr:
  181. GOTO_EXPR( ret, item, inFinish );
  182. break;
  183. case GenInlineItem::CallExpr:
  184. CALL_EXPR( ret, item, targState, inFinish );
  185. break;
  186. case GenInlineItem::NextExpr:
  187. NEXT_EXPR( ret, item, inFinish );
  188. break;
  189. case GenInlineItem::LmSwitch:
  190. LM_SWITCH( ret, item, targState, inFinish );
  191. break;
  192. case GenInlineItem::LmSetActId:
  193. SET_ACT( ret, item );
  194. break;
  195. case GenInlineItem::LmSetTokEnd:
  196. SET_TOKEND( ret, item );
  197. break;
  198. case GenInlineItem::LmGetTokEnd:
  199. GET_TOKEND( ret, item );
  200. break;
  201. case GenInlineItem::LmInitTokStart:
  202. INIT_TOKSTART( ret, item );
  203. break;
  204. case GenInlineItem::LmInitAct:
  205. INIT_ACT( ret, item );
  206. break;
  207. case GenInlineItem::LmSetTokStart:
  208. SET_TOKSTART( ret, item );
  209. break;
  210. case GenInlineItem::SubAction:
  211. SUB_ACTION( ret, item, targState, inFinish );
  212. break;
  213. case GenInlineItem::Break:
  214. BREAK( ret, targState );
  215. break;
  216. }
  217. }
  218. }
  219. string JavaTabCodeGen::DATA_PREFIX()
  220. {
  221. if ( !noPrefix )
  222. return FSM_NAME() + "_";
  223. return "";
  224. }
  225. /* Emit the alphabet data type. */
  226. string JavaTabCodeGen::ALPH_TYPE()
  227. {
  228. string ret = keyOps->alphType->data1;
  229. if ( keyOps->alphType->data2 != 0 ) {
  230. ret += " ";
  231. ret += + keyOps->alphType->data2;
  232. }
  233. return ret;
  234. }
  235. /* Emit the alphabet data type. */
  236. string JavaTabCodeGen::WIDE_ALPH_TYPE()
  237. {
  238. string ret;
  239. if ( redFsm->maxKey <= keyOps->maxKey )
  240. ret = ALPH_TYPE();
  241. else {
  242. long long maxKeyVal = redFsm->maxKey.getLongLong();
  243. HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
  244. assert( wideType != 0 );
  245. ret = wideType->data1;
  246. if ( wideType->data2 != 0 ) {
  247. ret += " ";
  248. ret += wideType->data2;
  249. }
  250. }
  251. return ret;
  252. }
  253. void JavaTabCodeGen::COND_TRANSLATE()
  254. {
  255. out <<
  256. " _widec = " << GET_KEY() << ";\n"
  257. " _keys = " << CO() << "[" << vCS() << "]*2\n;"
  258. " _klen = " << CL() << "[" << vCS() << "];\n"
  259. " if ( _klen > 0 ) {\n"
  260. " int _lower = _keys\n;"
  261. " int _mid;\n"
  262. " int _upper = _keys + (_klen<<1) - 2;\n"
  263. " while (true) {\n"
  264. " if ( _upper < _lower )\n"
  265. " break;\n"
  266. "\n"
  267. " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
  268. " if ( " << GET_WIDE_KEY() << " < " << CK() << "[_mid] )\n"
  269. " _upper = _mid - 2;\n"
  270. " else if ( " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1] )\n"
  271. " _lower = _mid + 2;\n"
  272. " else {\n"
  273. " switch ( " << C() << "[" << CO() << "[" << vCS() << "]"
  274. " + ((_mid - _keys)>>1)] ) {\n"
  275. ;
  276. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  277. GenCondSpace *condSpace = csi;
  278. out << " case " << condSpace->condSpaceId << ": {\n";
  279. out << TABS(2) << "_widec = " << KEY(condSpace->baseKey) <<
  280. " + (" << GET_KEY() << " - " << KEY(keyOps->minKey) << ");\n";
  281. for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
  282. out << TABS(2) << "if ( ";
  283. CONDITION( out, *csi );
  284. Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
  285. out << " ) _widec += " << condValOffset << ";\n";
  286. }
  287. out <<
  288. " break;\n"
  289. " }\n";
  290. }
  291. out <<
  292. " }\n"
  293. " break;\n"
  294. " }\n"
  295. " }\n"
  296. " }\n"
  297. "\n";
  298. }
  299. void JavaTabCodeGen::LOCATE_TRANS()
  300. {
  301. out <<
  302. " _match: do {\n"
  303. " _keys = " << KO() << "[" << vCS() << "]" << ";\n"
  304. " _trans = " << IO() << "[" << vCS() << "];\n"
  305. " _klen = " << SL() << "[" << vCS() << "];\n"
  306. " if ( _klen > 0 ) {\n"
  307. " int _lower = _keys;\n"
  308. " int _mid;\n"
  309. " int _upper = _keys + _klen - 1;\n"
  310. " while (true) {\n"
  311. " if ( _upper < _lower )\n"
  312. " break;\n"
  313. "\n"
  314. " _mid = _lower + ((_upper-_lower) >> 1);\n"
  315. " if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
  316. " _upper = _mid - 1;\n"
  317. " else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid] )\n"
  318. " _lower = _mid + 1;\n"
  319. " else {\n"
  320. " _trans += (_mid - _keys);\n"
  321. " break _match;\n"
  322. " }\n"
  323. " }\n"
  324. " _keys += _klen;\n"
  325. " _trans += _klen;\n"
  326. " }\n"
  327. "\n"
  328. " _klen = " << RL() << "[" << vCS() << "];\n"
  329. " if ( _klen > 0 ) {\n"
  330. " int _lower = _keys;\n"
  331. " int _mid;\n"
  332. " int _upper = _keys + (_klen<<1) - 2;\n"
  333. " while (true) {\n"
  334. " if ( _upper < _lower )\n"
  335. " break;\n"
  336. "\n"
  337. " _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
  338. " if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
  339. " _upper = _mid - 2;\n"
  340. " else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid+1] )\n"
  341. " _lower = _mid + 2;\n"
  342. " else {\n"
  343. " _trans += ((_mid - _keys)>>1);\n"
  344. " break _match;\n"
  345. " }\n"
  346. " }\n"
  347. " _trans += _klen;\n"
  348. " }\n"
  349. " } while (false);\n"
  350. "\n";
  351. }
  352. /* Determine if we should use indicies or not. */
  353. void JavaTabCodeGen::calcIndexSize()
  354. {
  355. int sizeWithInds = 0, sizeWithoutInds = 0;
  356. /* Calculate cost of using with indicies. */
  357. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  358. int totalIndex = st->outSingle.length() + st->outRange.length() +
  359. (st->defTrans == 0 ? 0 : 1);
  360. sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
  361. }
  362. sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
  363. if ( redFsm->anyActions() )
  364. sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
  365. /* Calculate the cost of not using indicies. */
  366. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  367. int totalIndex = st->outSingle.length() + st->outRange.length() +
  368. (st->defTrans == 0 ? 0 : 1);
  369. sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
  370. if ( redFsm->anyActions() )
  371. sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
  372. }
  373. /* If using indicies reduces the size, use them. */
  374. useIndicies = sizeWithInds < sizeWithoutInds;
  375. }
  376. int JavaTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
  377. {
  378. int act = 0;
  379. if ( state->toStateAction != 0 )
  380. act = state->toStateAction->location+1;
  381. return act;
  382. }
  383. int JavaTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  384. {
  385. int act = 0;
  386. if ( state->fromStateAction != 0 )
  387. act = state->fromStateAction->location+1;
  388. return act;
  389. }
  390. int JavaTabCodeGen::EOF_ACTION( RedStateAp *state )
  391. {
  392. int act = 0;
  393. if ( state->eofAction != 0 )
  394. act = state->eofAction->location+1;
  395. return act;
  396. }
  397. int JavaTabCodeGen::TRANS_ACTION( RedTransAp *trans )
  398. {
  399. /* If there are actions, emit them. Otherwise emit zero. */
  400. int act = 0;
  401. if ( trans->action != 0 )
  402. act = trans->action->location+1;
  403. return act;
  404. }
  405. std::ostream &JavaTabCodeGen::TO_STATE_ACTION_SWITCH()
  406. {
  407. /* Walk the list of functions, printing the cases. */
  408. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  409. /* Write out referenced actions. */
  410. if ( act->numToStateRefs > 0 ) {
  411. /* Write the case label, the action and the case break. */
  412. out << "\tcase " << act->actionId << ":\n";
  413. ACTION( out, act, 0, false );
  414. out << "\tbreak;\n";
  415. }
  416. }
  417. genLineDirective( out );
  418. return out;
  419. }
  420. std::ostream &JavaTabCodeGen::FROM_STATE_ACTION_SWITCH()
  421. {
  422. /* Walk the list of functions, printing the cases. */
  423. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  424. /* Write out referenced actions. */
  425. if ( act->numFromStateRefs > 0 ) {
  426. /* Write the case label, the action and the case break. */
  427. out << "\tcase " << act->actionId << ":\n";
  428. ACTION( out, act, 0, false );
  429. out << "\tbreak;\n";
  430. }
  431. }
  432. genLineDirective( out );
  433. return out;
  434. }
  435. std::ostream &JavaTabCodeGen::EOF_ACTION_SWITCH()
  436. {
  437. /* Walk the list of functions, printing the cases. */
  438. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  439. /* Write out referenced actions. */
  440. if ( act->numEofRefs > 0 ) {
  441. /* Write the case label, the action and the case break. */
  442. out << "\tcase " << act->actionId << ":\n";
  443. ACTION( out, act, 0, true );
  444. out << "\tbreak;\n";
  445. }
  446. }
  447. genLineDirective( out );
  448. return out;
  449. }
  450. std::ostream &JavaTabCodeGen::ACTION_SWITCH()
  451. {
  452. /* Walk the list of functions, printing the cases. */
  453. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  454. /* Write out referenced actions. */
  455. if ( act->numTransRefs > 0 ) {
  456. /* Write the case label, the action and the case break. */
  457. out << "\tcase " << act->actionId << ":\n";
  458. ACTION( out, act, 0, false );
  459. out << "\tbreak;\n";
  460. }
  461. }
  462. genLineDirective( out );
  463. return out;
  464. }
  465. std::ostream &JavaTabCodeGen::COND_OFFSETS()
  466. {
  467. int curKeyOffset = 0;
  468. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  469. /* Write the key offset. */
  470. ARRAY_ITEM( INT(curKeyOffset), st.last() );
  471. /* Move the key offset ahead. */
  472. curKeyOffset += st->stateCondList.length();
  473. }
  474. return out;
  475. }
  476. std::ostream &JavaTabCodeGen::KEY_OFFSETS()
  477. {
  478. int curKeyOffset = 0;
  479. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  480. /* Write the key offset. */
  481. ARRAY_ITEM( INT(curKeyOffset), st.last() );
  482. /* Move the key offset ahead. */
  483. curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
  484. }
  485. return out;
  486. }
  487. std::ostream &JavaTabCodeGen::INDEX_OFFSETS()
  488. {
  489. int curIndOffset = 0;
  490. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  491. /* Write the index offset. */
  492. ARRAY_ITEM( INT(curIndOffset), st.last() );
  493. /* Move the index offset ahead. */
  494. curIndOffset += st->outSingle.length() + st->outRange.length();
  495. if ( st->defTrans != 0 )
  496. curIndOffset += 1;
  497. }
  498. return out;
  499. }
  500. std::ostream &JavaTabCodeGen::COND_LENS()
  501. {
  502. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  503. /* Write singles length. */
  504. ARRAY_ITEM( INT(st->stateCondList.length()), st.last() );
  505. }
  506. return out;
  507. }
  508. std::ostream &JavaTabCodeGen::SINGLE_LENS()
  509. {
  510. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  511. /* Write singles length. */
  512. ARRAY_ITEM( INT(st->outSingle.length()), st.last() );
  513. }
  514. return out;
  515. }
  516. std::ostream &JavaTabCodeGen::RANGE_LENS()
  517. {
  518. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  519. /* Emit length of range index. */
  520. ARRAY_ITEM( INT(st->outRange.length()), st.last() );
  521. }
  522. return out;
  523. }
  524. std::ostream &JavaTabCodeGen::TO_STATE_ACTIONS()
  525. {
  526. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  527. /* Write any eof action. */
  528. ARRAY_ITEM( INT(TO_STATE_ACTION(st)), st.last() );
  529. }
  530. return out;
  531. }
  532. std::ostream &JavaTabCodeGen::FROM_STATE_ACTIONS()
  533. {
  534. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  535. /* Write any eof action. */
  536. ARRAY_ITEM( INT(FROM_STATE_ACTION(st)), st.last() );
  537. }
  538. return out;
  539. }
  540. std::ostream &JavaTabCodeGen::EOF_ACTIONS()
  541. {
  542. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  543. /* Write any eof action. */
  544. ARRAY_ITEM( INT(EOF_ACTION(st)), st.last() );
  545. }
  546. return out;
  547. }
  548. std::ostream &JavaTabCodeGen::EOF_TRANS()
  549. {
  550. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  551. /* Write any eof action. */
  552. long trans = 0;
  553. if ( st->eofTrans != 0 ) {
  554. assert( st->eofTrans->pos >= 0 );
  555. trans = st->eofTrans->pos+1;
  556. }
  557. /* Write any eof action. */
  558. ARRAY_ITEM( INT(trans), st.last() );
  559. }
  560. return out;
  561. }
  562. std::ostream &JavaTabCodeGen::COND_KEYS()
  563. {
  564. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  565. /* Loop the state's transitions. */
  566. for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
  567. /* Lower key. */
  568. ARRAY_ITEM( KEY( sc->lowKey ), false );
  569. ARRAY_ITEM( KEY( sc->highKey ), false );
  570. }
  571. }
  572. /* Output one last number so we don't have to figure out when the last
  573. * entry is and avoid writing a comma. */
  574. ARRAY_ITEM( INT(0), true );
  575. return out;
  576. }
  577. std::ostream &JavaTabCodeGen::COND_SPACES()
  578. {
  579. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  580. /* Loop the state's transitions. */
  581. for ( GenStateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
  582. /* Cond Space id. */
  583. ARRAY_ITEM( KEY( sc->condSpace->condSpaceId ), false );
  584. }
  585. }
  586. /* Output one last number so we don't have to figure out when the last
  587. * entry is and avoid writing a comma. */
  588. ARRAY_ITEM( INT(0), true );
  589. return out;
  590. }
  591. std::ostream &JavaTabCodeGen::KEYS()
  592. {
  593. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  594. /* Loop the singles. */
  595. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  596. ARRAY_ITEM( KEY( stel->lowKey ), false );
  597. }
  598. /* Loop the state's transitions. */
  599. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  600. /* Lower key. */
  601. ARRAY_ITEM( KEY( rtel->lowKey ), false );
  602. /* Upper key. */
  603. ARRAY_ITEM( KEY( rtel->highKey ), false );
  604. }
  605. }
  606. /* Output one last number so we don't have to figure out when the last
  607. * entry is and avoid writing a comma. */
  608. ARRAY_ITEM( INT(0), true );
  609. return out;
  610. }
  611. std::ostream &JavaTabCodeGen::INDICIES()
  612. {
  613. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  614. /* Walk the singles. */
  615. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  616. ARRAY_ITEM( KEY( stel->value->id ), false );
  617. }
  618. /* Walk the ranges. */
  619. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  620. ARRAY_ITEM( KEY( rtel->value->id ), false );
  621. }
  622. /* The state's default index goes next. */
  623. if ( st->defTrans != 0 ) {
  624. ARRAY_ITEM( KEY( st->defTrans->id ), false );
  625. }
  626. }
  627. /* Output one last number so we don't have to figure out when the last
  628. * entry is and avoid writing a comma. */
  629. ARRAY_ITEM( INT(0), true );
  630. return out;
  631. }
  632. std::ostream &JavaTabCodeGen::TRANS_TARGS()
  633. {
  634. int totalTrans = 0;
  635. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  636. /* Walk the singles. */
  637. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  638. RedTransAp *trans = stel->value;
  639. ARRAY_ITEM( KEY( trans->targ->id ), false );
  640. totalTrans++;
  641. }
  642. /* Walk the ranges. */
  643. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  644. RedTransAp *trans = rtel->value;
  645. ARRAY_ITEM( KEY( trans->targ->id ), false );
  646. totalTrans++;
  647. }
  648. /* The state's default target state. */
  649. if ( st->defTrans != 0 ) {
  650. RedTransAp *trans = st->defTrans;
  651. ARRAY_ITEM( KEY( trans->targ->id ), false );
  652. totalTrans++;
  653. }
  654. }
  655. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  656. if ( st->eofTrans != 0 ) {
  657. RedTransAp *trans = st->eofTrans;
  658. trans->pos = totalTrans++;
  659. ARRAY_ITEM( KEY( trans->targ->id ), false );
  660. }
  661. }
  662. /* Output one last number so we don't have to figure out when the last
  663. * entry is and avoid writing a comma. */
  664. ARRAY_ITEM( INT(0), true );
  665. return out;
  666. }
  667. std::ostream &JavaTabCodeGen::TRANS_ACTIONS()
  668. {
  669. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  670. /* Walk the singles. */
  671. for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
  672. RedTransAp *trans = stel->value;
  673. ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
  674. }
  675. /* Walk the ranges. */
  676. for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
  677. RedTransAp *trans = rtel->value;
  678. ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
  679. }
  680. /* The state's default index goes next. */
  681. if ( st->defTrans != 0 ) {
  682. RedTransAp *trans = st->defTrans;
  683. ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
  684. }
  685. }
  686. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  687. if ( st->eofTrans != 0 ) {
  688. RedTransAp *trans = st->eofTrans;
  689. ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
  690. }
  691. }
  692. /* Output one last number so we don't have to figure out when the last
  693. * entry is and avoid writing a comma. */
  694. ARRAY_ITEM( INT(0), true );
  695. return out;
  696. }
  697. std::ostream &JavaTabCodeGen::TRANS_TARGS_WI()
  698. {
  699. /* Transitions must be written ordered by their id. */
  700. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  701. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  702. transPtrs[trans->id] = trans;
  703. /* Keep a count of the num of items in the array written. */
  704. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  705. /* Save the position. Needed for eofTargs. */
  706. RedTransAp *trans = transPtrs[t];
  707. trans->pos = t;
  708. /* Write out the target state. */
  709. ARRAY_ITEM( INT(trans->targ->id), ( t >= redFsm->transSet.length()-1 ) );
  710. }
  711. delete[] transPtrs;
  712. return out;
  713. }
  714. std::ostream &JavaTabCodeGen::TRANS_ACTIONS_WI()
  715. {
  716. /* Transitions must be written ordered by their id. */
  717. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  718. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  719. transPtrs[trans->id] = trans;
  720. /* Keep a count of the num of items in the array written. */
  721. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  722. /* Write the function for the transition. */
  723. RedTransAp *trans = transPtrs[t];
  724. ARRAY_ITEM( INT(TRANS_ACTION( trans )), ( t >= redFsm->transSet.length()-1 ) );
  725. }
  726. delete[] transPtrs;
  727. return out;
  728. }
  729. void JavaTabCodeGen::writeExports()
  730. {
  731. if ( exportList.length() > 0 ) {
  732. for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
  733. STATIC_VAR( ALPH_TYPE(), DATA_PREFIX() + "ex_" + ex->name )
  734. << " = " << KEY(ex->key) << ";\n";
  735. }
  736. out << "\n";
  737. }
  738. }
  739. void JavaTabCodeGen::writeStart()
  740. {
  741. out << START_STATE_ID();
  742. }
  743. void JavaTabCodeGen::writeFirstFinal()
  744. {
  745. out << FIRST_FINAL_STATE();
  746. }
  747. void JavaTabCodeGen::writeError()
  748. {
  749. out << ERROR_STATE();
  750. }
  751. void JavaTabCodeGen::writeData()
  752. {
  753. /* If there are any transtion functions then output the array. If there
  754. * are none, don't bother emitting an empty array that won't be used. */
  755. if ( redFsm->anyActions() ) {
  756. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
  757. ACTIONS_ARRAY();
  758. CLOSE_ARRAY() <<
  759. "\n";
  760. }
  761. if ( redFsm->anyConditions() ) {
  762. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
  763. COND_OFFSETS();
  764. CLOSE_ARRAY() <<
  765. "\n";
  766. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
  767. COND_LENS();
  768. CLOSE_ARRAY() <<
  769. "\n";
  770. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  771. COND_KEYS();
  772. CLOSE_ARRAY() <<
  773. "\n";
  774. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
  775. COND_SPACES();
  776. CLOSE_ARRAY() <<
  777. "\n";
  778. }
  779. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
  780. KEY_OFFSETS();
  781. CLOSE_ARRAY() <<
  782. "\n";
  783. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  784. KEYS();
  785. CLOSE_ARRAY() <<
  786. "\n";
  787. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
  788. SINGLE_LENS();
  789. CLOSE_ARRAY() <<
  790. "\n";
  791. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
  792. RANGE_LENS();
  793. CLOSE_ARRAY() <<
  794. "\n";
  795. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
  796. INDEX_OFFSETS();
  797. CLOSE_ARRAY() <<
  798. "\n";
  799. if ( useIndicies ) {
  800. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  801. INDICIES();
  802. CLOSE_ARRAY() <<
  803. "\n";
  804. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  805. TRANS_TARGS_WI();
  806. CLOSE_ARRAY() <<
  807. "\n";
  808. if ( redFsm->anyActions() ) {
  809. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  810. TRANS_ACTIONS_WI();
  811. CLOSE_ARRAY() <<
  812. "\n";
  813. }
  814. }
  815. else {
  816. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  817. TRANS_TARGS();
  818. CLOSE_ARRAY() <<
  819. "\n";
  820. if ( redFsm->anyActions() ) {
  821. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  822. TRANS_ACTIONS();
  823. CLOSE_ARRAY() <<
  824. "\n";
  825. }
  826. }
  827. if ( redFsm->anyToStateActions() ) {
  828. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  829. TO_STATE_ACTIONS();
  830. CLOSE_ARRAY() <<
  831. "\n";
  832. }
  833. if ( redFsm->anyFromStateActions() ) {
  834. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  835. FROM_STATE_ACTIONS();
  836. CLOSE_ARRAY() <<
  837. "\n";
  838. }
  839. if ( redFsm->anyEofActions() ) {
  840. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  841. EOF_ACTIONS();
  842. CLOSE_ARRAY() <<
  843. "\n";
  844. }
  845. if ( redFsm->anyEofTrans() ) {
  846. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  847. EOF_TRANS();
  848. CLOSE_ARRAY() <<
  849. "\n";
  850. }
  851. if ( redFsm->startState != 0 )
  852. STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
  853. if ( !noFinal )
  854. STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
  855. if ( !noError )
  856. STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
  857. out << "\n";
  858. if ( !noEntry && entryPointNames.length() > 0 ) {
  859. for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
  860. STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) <<
  861. " = " << entryPointIds[en.pos()] << ";\n";
  862. }
  863. out << "\n";
  864. }
  865. }
  866. void JavaTabCodeGen::writeExec()
  867. {
  868. out <<
  869. " {\n"
  870. " int _klen";
  871. if ( redFsm->anyRegCurStateRef() )
  872. out << ", _ps";
  873. out <<
  874. ";\n"
  875. " int _trans = 0;\n";
  876. if ( redFsm->anyConditions() )
  877. out << " int _widec;\n";
  878. if ( redFsm->anyToStateActions() || redFsm->anyRegActions() ||
  879. redFsm->anyFromStateActions() )
  880. {
  881. out <<
  882. " int _acts;\n"
  883. " int _nacts;\n";
  884. }
  885. out <<
  886. " int _keys;\n"
  887. " int _goto_targ = 0;\n"
  888. "\n";
  889. out <<
  890. " _goto: while (true) {\n"
  891. " switch ( _goto_targ ) {\n"
  892. " case 0:\n";
  893. if ( !noEnd ) {
  894. out <<
  895. " if ( " << P() << " == " << PE() << " ) {\n"
  896. " _goto_targ = " << _test_eof << ";\n"
  897. " continue _goto;\n"
  898. " }\n";
  899. }
  900. if ( redFsm->errState != 0 ) {
  901. out <<
  902. " if ( " << vCS() << " == " << redFsm->errState->id << " ) {\n"
  903. " _goto_targ = " << _out << ";\n"
  904. " continue _goto;\n"
  905. " }\n";
  906. }
  907. out << "case " << _resume << ":\n";
  908. if ( redFsm->anyFromStateActions() ) {
  909. out <<
  910. " _acts = " << FSA() << "[" << vCS() << "]" << ";\n"
  911. " _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
  912. " while ( _nacts-- > 0 ) {\n"
  913. " switch ( " << A() << "[_acts++] ) {\n";
  914. FROM_STATE_ACTION_SWITCH() <<
  915. " }\n"
  916. " }\n"
  917. "\n";
  918. }
  919. if ( redFsm->anyConditions() )
  920. COND_TRANSLATE();
  921. LOCATE_TRANS();
  922. if ( useIndicies )
  923. out << " _trans = " << I() << "[_trans];\n";
  924. if ( redFsm->anyEofTrans() )
  925. out << "case " << _eof_trans << ":\n";
  926. if ( redFsm->anyRegCurStateRef() )
  927. out << " _ps = " << vCS() << ";\n";
  928. out <<
  929. " " << vCS() << " = " << TT() << "[_trans];\n"
  930. "\n";
  931. if ( redFsm->anyRegActions() ) {
  932. out <<
  933. " if ( " << TA() << "[_trans] != 0 ) {\n"
  934. " _acts = " << TA() << "[_trans]" << ";\n"
  935. " _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
  936. " while ( _nacts-- > 0 )\n {\n"
  937. " switch ( " << A() << "[_acts++] )\n"
  938. " {\n";
  939. ACTION_SWITCH() <<
  940. " }\n"
  941. " }\n"
  942. " }\n"
  943. "\n";
  944. }
  945. out << "case " << _again << ":\n";
  946. if ( redFsm->anyToStateActions() ) {
  947. out <<
  948. " _acts = " << TSA() << "[" << vCS() << "]" << ";\n"
  949. " _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
  950. " while ( _nacts-- > 0 ) {\n"
  951. " switch ( " << A() << "[_acts++] ) {\n";
  952. TO_STATE_ACTION_SWITCH() <<
  953. " }\n"
  954. " }\n"
  955. "\n";
  956. }
  957. if ( redFsm->errState != 0 ) {
  958. out <<
  959. " if ( " << vCS() << " == " << redFsm->errState->id << " ) {\n"
  960. " _goto_targ = " << _out << ";\n"
  961. " continue _goto;\n"
  962. " }\n";
  963. }
  964. if ( !noEnd ) {
  965. out <<
  966. " if ( ++" << P() << " != " << PE() << " ) {\n"
  967. " _goto_targ = " << _resume << ";\n"
  968. " continue _goto;\n"
  969. " }\n";
  970. }
  971. else {
  972. out <<
  973. " " << P() << " += 1;\n"
  974. " _goto_targ = " << _resume << ";\n"
  975. " continue _goto;\n";
  976. }
  977. out << "case " << _test_eof << ":\n";
  978. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  979. out <<
  980. " if ( " << P() << " == " << vEOF() << " )\n"
  981. " {\n";
  982. if ( redFsm->anyEofTrans() ) {
  983. out <<
  984. " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
  985. " _trans = " << ET() << "[" << vCS() << "] - 1;\n"
  986. " _goto_targ = " << _eof_trans << ";\n"
  987. " continue _goto;\n"
  988. " }\n";
  989. }
  990. if ( redFsm->anyEofActions() ) {
  991. out <<
  992. " int __acts = " << EA() << "[" << vCS() << "]" << ";\n"
  993. " int __nacts = " << CAST("int") << " " << A() << "[__acts++];\n"
  994. " while ( __nacts-- > 0 ) {\n"
  995. " switch ( " << A() << "[__acts++] ) {\n";
  996. EOF_ACTION_SWITCH() <<
  997. " }\n"
  998. " }\n";
  999. }
  1000. out <<
  1001. " }\n"
  1002. "\n";
  1003. }
  1004. out << "case " << _out << ":\n";
  1005. /* The switch and goto loop. */
  1006. out << " }\n";
  1007. out << " break; }\n";
  1008. /* The execute block. */
  1009. out << " }\n";
  1010. }
  1011. std::ostream &JavaTabCodeGen::OPEN_ARRAY( string type, string name )
  1012. {
  1013. array_type = type;
  1014. array_name = name;
  1015. item_count = 0;
  1016. div_count = 1;
  1017. out << "private static " << type << "[] init_" << name << "_0()\n"
  1018. "{\n\t"
  1019. "return new " << type << " [] {\n\t";
  1020. return out;
  1021. }
  1022. std::ostream &JavaTabCodeGen::ARRAY_ITEM( string item, bool last )
  1023. {
  1024. item_count++;
  1025. out << setw(5) << setiosflags(ios::right) << item;
  1026. if ( !last ) {
  1027. if ( item_count % SAIIC == 0 ) {
  1028. out << "\n\t};\n};\n"
  1029. "private static "<< array_type << "[] init_" <<
  1030. array_name << "_" << div_count << "()\n"
  1031. "{\n\t"
  1032. "return new " << array_type << " [] {\n\t";
  1033. div_count++;
  1034. } else if (item_count % IALL == 0) {
  1035. out << ",\n\t";
  1036. } else {
  1037. out << ",";
  1038. }
  1039. }
  1040. return out;
  1041. }
  1042. std::ostream &JavaTabCodeGen::CLOSE_ARRAY()
  1043. {
  1044. out << "\n\t};\n}\n\n";
  1045. if (item_count < SAIIC) {
  1046. out << "private static final " << array_type << " " << array_name <<
  1047. "[] = init_" << array_name << "_0();\n\n";
  1048. } else {
  1049. out << "private static final " << array_type << " [] combine_" << array_name
  1050. << "() {\n\t"
  1051. << array_type << " [] combined = new " << array_type <<
  1052. " [ " << item_count << " ];\n\t";
  1053. int block = 0;
  1054. int full_blocks = item_count / SAIIC;
  1055. for (;block < full_blocks; ++block) {
  1056. out << "System.arraycopy ( init_" << array_name << "_" << block <<
  1057. "(), 0, combined, " << SAIIC * block << ", " << SAIIC << " );\n\t";
  1058. }
  1059. if ( (item_count % SAIIC) > 0 ) {
  1060. out << "System.arraycopy ( init_" << array_name << "_" << block <<
  1061. "(), 0, combined, " << SAIIC * block << ", " <<
  1062. (item_count % SAIIC) << " );\n\t";
  1063. }
  1064. out << "return combined;\n}\n";
  1065. out << "private static final " << array_type << " [] " << array_name <<
  1066. " = combine_" << array_name << "();";
  1067. }
  1068. return out;
  1069. }
  1070. std::ostream &JavaTabCodeGen::STATIC_VAR( string type, string name )
  1071. {
  1072. out << "static final " << type << " " << name;
  1073. return out;
  1074. }
  1075. string JavaTabCodeGen::ARR_OFF( string ptr, string offset )
  1076. {
  1077. return ptr + " + " + offset;
  1078. }
  1079. string JavaTabCodeGen::CAST( string type )
  1080. {
  1081. return "(" + type + ")";
  1082. }
  1083. string JavaTabCodeGen::NULL_ITEM()
  1084. {
  1085. /* In java we use integers instead of pointers. */
  1086. return "-1";
  1087. }
  1088. string JavaTabCodeGen::GET_KEY()
  1089. {
  1090. ostringstream ret;
  1091. if ( getKeyExpr != 0 ) {
  1092. /* Emit the user supplied method of retrieving the key. */
  1093. ret << "(";
  1094. INLINE_LIST( ret, getKeyExpr, 0, false );
  1095. ret << ")";
  1096. }
  1097. else {
  1098. /* Expression for retrieving the key, use simple dereference. */
  1099. ret << DATA() << "[" << P() << "]";
  1100. }
  1101. return ret.str();
  1102. }
  1103. string JavaTabCodeGen::CTRL_FLOW()
  1104. {
  1105. return "if (true) ";
  1106. }
  1107. unsigned int JavaTabCodeGen::arrayTypeSize( unsigned long maxVal )
  1108. {
  1109. long long maxValLL = (long long) maxVal;
  1110. HostType *arrayType = keyOps->typeSubsumes( maxValLL );
  1111. assert( arrayType != 0 );
  1112. return arrayType->size;
  1113. }
  1114. string JavaTabCodeGen::ARRAY_TYPE( unsigned long maxVal )
  1115. {
  1116. long long maxValLL = (long long) maxVal;
  1117. HostType *arrayType = keyOps->typeSubsumes( maxValLL );
  1118. assert( arrayType != 0 );
  1119. string ret = arrayType->data1;
  1120. if ( arrayType->data2 != 0 ) {
  1121. ret += " ";
  1122. ret += arrayType->data2;
  1123. }
  1124. return ret;
  1125. }
  1126. /* Write out the fsm name. */
  1127. string JavaTabCodeGen::FSM_NAME()
  1128. {
  1129. return fsmName;
  1130. }
  1131. /* Emit the offset of the start state as a decimal integer. */
  1132. string JavaTabCodeGen::START_STATE_ID()
  1133. {
  1134. ostringstream ret;
  1135. ret << redFsm->startState->id;
  1136. return ret.str();
  1137. };
  1138. /* Write out the array of actions. */
  1139. std::ostream &JavaTabCodeGen::ACTIONS_ARRAY()
  1140. {
  1141. ARRAY_ITEM( INT(0), false );
  1142. for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
  1143. /* Write out the length, which will never be the last character. */
  1144. ARRAY_ITEM( INT(act->key.length()), false );
  1145. for ( GenActionTable::Iter item = act->key; item.lte(); item++ )
  1146. ARRAY_ITEM( INT(item->value->actionId), (act.last() && item.last()) );
  1147. }
  1148. return out;
  1149. }
  1150. string JavaTabCodeGen::ACCESS()
  1151. {
  1152. ostringstream ret;
  1153. if ( accessExpr != 0 )
  1154. INLINE_LIST( ret, accessExpr, 0, false );
  1155. return ret.str();
  1156. }
  1157. string JavaTabCodeGen::P()
  1158. {
  1159. ostringstream ret;
  1160. if ( pExpr == 0 )
  1161. ret << "p";
  1162. else {
  1163. ret << "(";
  1164. INLINE_LIST( ret, pExpr, 0, false );
  1165. ret << ")";
  1166. }
  1167. return ret.str();
  1168. }
  1169. string JavaTabCodeGen::PE()
  1170. {
  1171. ostringstream ret;
  1172. if ( peExpr == 0 )
  1173. ret << "pe";
  1174. else {
  1175. ret << "(";
  1176. INLINE_LIST( ret, peExpr, 0, false );
  1177. ret << ")";
  1178. }
  1179. return ret.str();
  1180. }
  1181. string JavaTabCodeGen::vEOF()
  1182. {
  1183. ostringstream ret;
  1184. if ( eofExpr == 0 )
  1185. ret << "eof";
  1186. else {
  1187. ret << "(";
  1188. INLINE_LIST( ret, eofExpr, 0, false );
  1189. ret << ")";
  1190. }
  1191. return ret.str();
  1192. }
  1193. string JavaTabCodeGen::vCS()
  1194. {
  1195. ostringstream ret;
  1196. if ( csExpr == 0 )
  1197. ret << ACCESS() << "cs";
  1198. else {
  1199. /* Emit the user supplied method of retrieving the key. */
  1200. ret << "(";
  1201. INLINE_LIST( ret, csExpr, 0, false );
  1202. ret << ")";
  1203. }
  1204. return ret.str();
  1205. }
  1206. string JavaTabCodeGen::TOP()
  1207. {
  1208. ostringstream ret;
  1209. if ( topExpr == 0 )
  1210. ret << ACCESS() + "top";
  1211. else {
  1212. ret << "(";
  1213. INLINE_LIST( ret, topExpr, 0, false );
  1214. ret << ")";
  1215. }
  1216. return ret.str();
  1217. }
  1218. string JavaTabCodeGen::STACK()
  1219. {
  1220. ostringstream ret;
  1221. if ( stackExpr == 0 )
  1222. ret << ACCESS() + "stack";
  1223. else {
  1224. ret << "(";
  1225. INLINE_LIST( ret, stackExpr, 0, false );
  1226. ret << ")";
  1227. }
  1228. return ret.str();
  1229. }
  1230. string JavaTabCodeGen::ACT()
  1231. {
  1232. ostringstream ret;
  1233. if ( actExpr == 0 )
  1234. ret << ACCESS() + "act";
  1235. else {
  1236. ret << "(";
  1237. INLINE_LIST( ret, actExpr, 0, false );
  1238. ret << ")";
  1239. }
  1240. return ret.str();
  1241. }
  1242. string JavaTabCodeGen::TOKSTART()
  1243. {
  1244. ostringstream ret;
  1245. if ( tokstartExpr == 0 )
  1246. ret << ACCESS() + "ts";
  1247. else {
  1248. ret << "(";
  1249. INLINE_LIST( ret, tokstartExpr, 0, false );
  1250. ret << ")";
  1251. }
  1252. return ret.str();
  1253. }
  1254. string JavaTabCodeGen::TOKEND()
  1255. {
  1256. ostringstream ret;
  1257. if ( tokendExpr == 0 )
  1258. ret << ACCESS() + "te";
  1259. else {
  1260. ret << "(";
  1261. INLINE_LIST( ret, tokendExpr, 0, false );
  1262. ret << ")";
  1263. }
  1264. return ret.str();
  1265. }
  1266. string JavaTabCodeGen::DATA()
  1267. {
  1268. ostringstream ret;
  1269. if ( dataExpr == 0 )
  1270. ret << ACCESS() + "data";
  1271. else {
  1272. ret << "(";
  1273. INLINE_LIST( ret, dataExpr, 0, false );
  1274. ret << ")";
  1275. }
  1276. return ret.str();
  1277. }
  1278. string JavaTabCodeGen::GET_WIDE_KEY()
  1279. {
  1280. if ( redFsm->anyConditions() )
  1281. return "_widec";
  1282. else
  1283. return GET_KEY();
  1284. }
  1285. string JavaTabCodeGen::GET_WIDE_KEY( RedStateAp *state )
  1286. {
  1287. if ( state->stateCondList.length() > 0 )
  1288. return "_widec";
  1289. else
  1290. return GET_KEY();
  1291. }
  1292. /* Write out level number of tabs. Makes the nested binary search nice
  1293. * looking. */
  1294. string JavaTabCodeGen::TABS( int level )
  1295. {
  1296. string result;
  1297. while ( level-- > 0 )
  1298. result += "\t";
  1299. return result;
  1300. }
  1301. string JavaTabCodeGen::KEY( Key key )
  1302. {
  1303. ostringstream ret;
  1304. if ( keyOps->isSigned || !hostLang->explicitUnsigned )
  1305. ret << key.getVal();
  1306. else
  1307. ret << (unsigned long) key.getVal();
  1308. return ret.str();
  1309. }
  1310. string JavaTabCodeGen::INT( int i )
  1311. {
  1312. ostringstream ret;
  1313. ret << i;
  1314. return ret.str();
  1315. }
  1316. void JavaTabCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item,
  1317. int targState, int inFinish )
  1318. {
  1319. ret <<
  1320. " switch( " << ACT() << " ) {\n";
  1321. for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
  1322. /* Write the case label, the action and the case break. */
  1323. if ( lma->lmId < 0 )
  1324. ret << " default:\n";
  1325. else
  1326. ret << " case " << lma->lmId << ":\n";
  1327. /* Write the block and close it off. */
  1328. ret << " {";
  1329. INLINE_LIST( ret, lma->children, targState, inFinish );
  1330. ret << "}\n";
  1331. ret << " break;\n";
  1332. }
  1333. ret <<
  1334. " }\n"
  1335. "\t";
  1336. }
  1337. void JavaTabCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
  1338. {
  1339. ret << ACT() << " = " << item->lmId << ";";
  1340. }
  1341. void JavaTabCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
  1342. {
  1343. /* The tokend action sets tokend. */
  1344. ret << TOKEND() << " = " << P();
  1345. if ( item->offset != 0 )
  1346. out << "+" << item->offset;
  1347. out << ";";
  1348. }
  1349. void JavaTabCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
  1350. {
  1351. ret << TOKEND();
  1352. }
  1353. void JavaTabCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
  1354. {
  1355. ret << TOKSTART() << " = " << NULL_ITEM() << ";";
  1356. }
  1357. void JavaTabCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
  1358. {
  1359. ret << ACT() << " = 0;";
  1360. }
  1361. void JavaTabCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
  1362. {
  1363. ret << TOKSTART() << " = " << P() << ";";
  1364. }
  1365. void JavaTabCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item,
  1366. int targState, bool inFinish )
  1367. {
  1368. if ( item->children->length() > 0 ) {
  1369. /* Write the block and close it off. */
  1370. ret << "{";
  1371. INLINE_LIST( ret, item->children, targState, inFinish );
  1372. ret << "}";
  1373. }
  1374. }
  1375. void JavaTabCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish )
  1376. {
  1377. /* Write the preprocessor line info for going into the source file. */
  1378. javaLineDirective( ret, action->loc.fileName, action->loc.line );
  1379. /* Write the block and close it off. */
  1380. ret << "\t{";
  1381. INLINE_LIST( ret, action->inlineList, targState, inFinish );
  1382. ret << "}\n";
  1383. }
  1384. void JavaTabCodeGen::CONDITION( ostream &ret, GenAction *condition )
  1385. {
  1386. ret << "\n";
  1387. javaLineDirective( ret, condition->loc.fileName, condition->loc.line );
  1388. INLINE_LIST( ret, condition->inlineList, 0, false );
  1389. }
  1390. string JavaTabCodeGen::ERROR_STATE()
  1391. {
  1392. ostringstream ret;
  1393. if ( redFsm->errState != 0 )
  1394. ret << redFsm->errState->id;
  1395. else
  1396. ret << "-1";
  1397. return ret.str();
  1398. }
  1399. string JavaTabCodeGen::FIRST_FINAL_STATE()
  1400. {
  1401. ostringstream ret;
  1402. if ( redFsm->firstFinState != 0 )
  1403. ret << redFsm->firstFinState->id;
  1404. else
  1405. ret << redFsm->nextStateId;
  1406. return ret.str();
  1407. }
  1408. void JavaTabCodeGen::writeInit()
  1409. {
  1410. out << " {\n";
  1411. if ( !noCS )
  1412. out << "\t" << vCS() << " = " << START() << ";\n";
  1413. /* If there are any calls, then the stack top needs initialization. */
  1414. if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
  1415. out << "\t" << TOP() << " = 0;\n";
  1416. if ( hasLongestMatch ) {
  1417. out <<
  1418. " " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
  1419. " " << TOKEND() << " = " << NULL_ITEM() << ";\n"
  1420. " " << ACT() << " = 0;\n";
  1421. }
  1422. out << " }\n";
  1423. }
  1424. void JavaTabCodeGen::finishRagelDef()
  1425. {
  1426. /* The frontend will do this for us, but it may be a good idea to force it
  1427. * if the intermediate file is edited. */
  1428. redFsm->sortByStateId();
  1429. /* Choose default transitions and the single transition. */
  1430. redFsm->chooseDefaultSpan();
  1431. /* Maybe do flat expand, otherwise choose single. */
  1432. redFsm->chooseSingle();
  1433. /* If any errors have occured in the input file then don't write anything. */
  1434. if ( gblErrorCount > 0 )
  1435. return;
  1436. /* Anlayze Machine will find the final action reference counts, among
  1437. * other things. We will use these in reporting the usage
  1438. * of fsm directives in action code. */
  1439. analyzeMachine();
  1440. /* Determine if we should use indicies. */
  1441. calcIndexSize();
  1442. }
  1443. ostream &JavaTabCodeGen::source_warning( const InputLoc &loc )
  1444. {
  1445. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
  1446. return cerr;
  1447. }
  1448. ostream &JavaTabCodeGen::source_error( const InputLoc &loc )
  1449. {
  1450. gblErrorCount += 1;
  1451. assert( sourceFileName != 0 );
  1452. cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
  1453. return cerr;
  1454. }
  1455. #undef _resume
  1456. #undef _again
  1457. #undef _eof_trans
  1458. #undef _test_eof
  1459. #undef _out