2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Eric Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
9 * Ragel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Ragel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Ragel; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 /* Integer array line length. */
48 struct LongestMatchPart;
50 inline string itoa( int i )
53 sprintf( buf, "%i", i );
64 virtual ~FsmCodeGen() {}
66 virtual void writeOutData() = 0;
67 virtual void writeOutInit();
68 virtual void writeOutExec() = 0;
69 virtual void writeOutEOF() = 0;
71 /* Gather various info on the machine. */
72 void analyzeAction( Action *act, InlineList *inlineList );
73 void analyzeActionList( RedAction *redAct, InlineList *inlineList );
74 void analyzeMachine();
77 friend struct CodeGenData;
80 string START_STATE_ID();
81 ostream &ACTIONS_ARRAY();
82 string GET_WIDE_KEY();
83 string GET_WIDE_KEY( RedStateAp *state );
84 string TABS( int level );
85 string KEY( Key key );
86 string LDIR_PATH( char *path );
87 void ACTION( ostream &ret, Action *action, int targState, bool inFinish );
88 void CONDITION( ostream &ret, Action *condition );
90 string WIDE_ALPH_TYPE();
91 string ARRAY_TYPE( unsigned long maxVal );
93 virtual string ARR_OFF( string ptr, string offset ) = 0;
94 virtual string CAST( string type ) = 0;
95 virtual string UINT() = 0;
96 virtual string NULL_ITEM() = 0;
97 virtual string POINTER() = 0;
98 virtual string GET_KEY();
99 virtual ostream &SWITCH_DEFAULT() = 0;
101 string P() { return "p"; }
102 string PE() { return "pe"; }
106 string STACK() { return ACCESS() + "stack"; }
107 string TOP() { return ACCESS() + "top"; }
108 string TOKSTART() { return ACCESS() + "tokstart"; }
109 string TOKEND() { return ACCESS() + "tokend"; }
110 string ACT() { return ACCESS() + "act"; }
112 string DATA_PREFIX();
113 string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
114 string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
115 string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
116 string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
117 string I() { return "_" + DATA_PREFIX() + "indicies"; }
118 string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
119 string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
120 string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
121 string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
122 string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
123 string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
124 string A() { return "_" + DATA_PREFIX() + "actions"; }
125 string TA() { return "_" + DATA_PREFIX() + "trans_actions_wi"; }
126 string TT() { return "_" + DATA_PREFIX() + "trans_targs_wi"; }
127 string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
128 string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
129 string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
130 string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
131 string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
132 string START() { return DATA_PREFIX() + "start"; }
133 string ERROR() { return DATA_PREFIX() + "error"; }
134 string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
135 string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
137 void INLINE_LIST( ostream &ret, InlineList *inlineList, int targState, bool inFinish );
138 virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
139 virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
140 virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
141 virtual void GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
142 virtual void NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
143 virtual void CALL_EXPR( ostream &ret, InlineItem *ilItem,
144 int targState, bool inFinish ) = 0;
145 virtual void RET( ostream &ret, bool inFinish ) = 0;
146 virtual void BREAK( ostream &ret, int targState ) = 0;
147 virtual void CURS( ostream &ret, bool inFinish ) = 0;
148 virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
149 void EXEC( ostream &ret, InlineItem *item, int targState, int inFinish );
150 void EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish );
151 void LM_SWITCH( ostream &ret, InlineItem *item, int targState, int inFinish );
152 void SET_ACT( ostream &ret, InlineItem *item );
153 void INIT_TOKSTART( ostream &ret, InlineItem *item );
154 void INIT_ACT( ostream &ret, InlineItem *item );
155 void SET_TOKSTART( ostream &ret, InlineItem *item );
156 void SET_TOKEND( ostream &ret, InlineItem *item );
157 void GET_TOKEND( ostream &ret, InlineItem *item );
158 void SUB_ACTION( ostream &ret, InlineItem *item,
159 int targState, bool inFinish );
161 string ERROR_STATE();
162 string FIRST_FINAL_STATE();
164 virtual string PTR_CONST() = 0;
165 virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
166 virtual ostream &CLOSE_ARRAY() = 0;
167 virtual ostream &STATIC_VAR( string type, string name ) = 0;
169 virtual string CTRL_FLOW() = 0;
171 unsigned int arrayTypeSize( unsigned long maxVal );
174 bool anyToStateActions() { return bAnyToStateActions; }
175 bool anyFromStateActions() { return bAnyFromStateActions; }
176 bool anyRegActions() { return bAnyRegActions; }
177 bool anyEofActions() { return bAnyEofActions; }
178 bool anyActionGotos() { return bAnyActionGotos; }
179 bool anyActionCalls() { return bAnyActionCalls; }
180 bool anyActionRets() { return bAnyActionRets; }
181 bool anyRegActionRets() { return bAnyRegActionRets; }
182 bool anyRegActionByValControl() { return bAnyRegActionByValControl; }
183 bool anyRegNextStmt() { return bAnyRegNextStmt; }
184 bool anyRegCurStateRef() { return bAnyRegCurStateRef; }
185 bool anyRegBreak() { return bAnyRegBreak; }
186 bool anyLmSwitchError() { return bAnyLmSwitchError; }
187 bool anyConditions() { return bAnyConditions; }
189 /* Set up labelNeeded flag for each state. Differs for each goto style so
191 virtual void setLabelsNeeded() {}
193 /* Determine if we should use indicies. */
194 virtual void calcIndexSize() {}
196 void findFinalActionRefs();
197 void assignActionIds();
198 void setValueLimits();
200 /* Are there any regular transition functions, any out transition functions. */
211 bool bAnyToStateActions;
212 bool bAnyFromStateActions;
215 bool bAnyActionGotos;
216 bool bAnyActionCalls;
218 bool bAnyRegActionRets;
219 bool bAnyRegActionByValControl;
220 bool bAnyRegNextStmt;
221 bool bAnyRegCurStateRef;
223 bool bAnyLmSwitchError;
235 unsigned long long maxSpan;
236 unsigned long long maxCondSpan;
237 int maxFlatIndexOffset;
242 int maxCondIndexOffset;
248 class CCodeGen : virtual public FsmCodeGen
251 virtual string NULL_ITEM();
252 virtual string POINTER();
253 virtual ostream &SWITCH_DEFAULT();
254 virtual ostream &OPEN_ARRAY( string type, string name );
255 virtual ostream &CLOSE_ARRAY();
256 virtual ostream &STATIC_VAR( string type, string name );
257 virtual string ARR_OFF( string ptr, string offset );
258 virtual string CAST( string type );
259 virtual string UINT();
260 virtual string PTR_CONST();
261 virtual string CTRL_FLOW();
264 class DCodeGen : virtual public FsmCodeGen
267 virtual string NULL_ITEM();
268 virtual string POINTER();
269 virtual ostream &SWITCH_DEFAULT();
270 virtual ostream &OPEN_ARRAY( string type, string name );
271 virtual ostream &CLOSE_ARRAY();
272 virtual ostream &STATIC_VAR( string type, string name );
273 virtual string ARR_OFF( string ptr, string offset );
274 virtual string CAST( string type );
275 virtual string UINT();
276 virtual string PTR_CONST();
277 virtual string CTRL_FLOW();
280 class JavaCodeGen : virtual public FsmCodeGen
283 virtual string NULL_ITEM();
284 virtual string POINTER();
285 virtual ostream &SWITCH_DEFAULT();
286 virtual ostream &OPEN_ARRAY( string type, string name );
287 virtual ostream &CLOSE_ARRAY();
288 virtual ostream &STATIC_VAR( string type, string name );
289 virtual string ARR_OFF( string ptr, string offset );
290 virtual string CAST( string type );
291 virtual string UINT();
292 virtual string PTR_CONST();
293 virtual string GET_KEY();
294 virtual string CTRL_FLOW();
297 #endif /* _FSMCODEGEN_H */