Moved more analysis code from FsmCodeGen into CodeGenData. Eliminated the
[external/ragel.git] / rlcodegen / fsmcodegen.h
1 /*
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>
5  */
6
7 /*  This file is part of Ragel.
8  *
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.
13  * 
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.
18  * 
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 
22  */
23
24 #ifndef _FSMCODEGEN_H
25 #define _FSMCODEGEN_H
26
27 #include <iostream>
28 #include <string>
29 #include <stdio.h>
30 #include "common.h"
31
32 using std::string;
33 using std::ostream;
34
35 /* Integer array line length. */
36 #define IALL 8
37
38 /* Forwards. */
39 struct RedFsmAp;
40 struct RedStateAp;
41 struct CodeGenData;
42 struct Action;
43 struct NameInst;
44 struct InlineItem;
45 struct InlineList;
46 struct RedAction;
47 struct LongestMatch;
48 struct LongestMatchPart;
49
50 inline string itoa( int i )
51 {
52         char buf[16];
53         sprintf( buf, "%i", i );
54         return buf;
55 }
56
57 /*
58  * class FsmCodeGen
59  */
60 class FsmCodeGen
61 {
62 public:
63         FsmCodeGen( ostream &out );
64         virtual ~FsmCodeGen() {}
65
66         virtual void writeOutData() = 0;
67         virtual void writeOutInit();
68         virtual void writeOutExec() = 0;
69         virtual void writeOutEOF() = 0;
70         
71
72 protected:
73         friend struct CodeGenData;
74
75         string FSM_NAME();
76         string START_STATE_ID();
77         ostream &ACTIONS_ARRAY();
78         string GET_WIDE_KEY();
79         string GET_WIDE_KEY( RedStateAp *state );
80         string TABS( int level );
81         string KEY( Key key );
82         string LDIR_PATH( char *path );
83         void ACTION( ostream &ret, Action *action, int targState, bool inFinish );
84         void CONDITION( ostream &ret, Action *condition );
85         string ALPH_TYPE();
86         string WIDE_ALPH_TYPE();
87         string ARRAY_TYPE( unsigned long maxVal );
88
89         virtual string ARR_OFF( string ptr, string offset ) = 0;
90         virtual string CAST( string type ) = 0;
91         virtual string UINT() = 0;
92         virtual string NULL_ITEM() = 0;
93         virtual string POINTER() = 0;
94         virtual string GET_KEY();
95         virtual ostream &SWITCH_DEFAULT() = 0;
96
97         string P() { return "p"; }
98         string PE() { return "pe"; }
99
100         string ACCESS();
101         string CS();
102         string STACK() { return ACCESS() + "stack"; }
103         string TOP() { return ACCESS() + "top"; }
104         string TOKSTART() { return ACCESS() + "tokstart"; }
105         string TOKEND() { return ACCESS() + "tokend"; }
106         string ACT() { return ACCESS() + "act"; }
107
108         string DATA_PREFIX();
109         string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
110         string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
111         string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
112         string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
113         string I() { return "_" + DATA_PREFIX() + "indicies"; }
114         string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
115         string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
116         string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
117         string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
118         string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
119         string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
120         string A() { return "_" + DATA_PREFIX() + "actions"; }
121         string TA() { return "_" + DATA_PREFIX() + "trans_actions_wi"; }
122         string TT() { return "_" + DATA_PREFIX() + "trans_targs_wi"; }
123         string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
124         string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
125         string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
126         string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
127         string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
128         string START() { return DATA_PREFIX() + "start"; }
129         string ERROR() { return DATA_PREFIX() + "error"; }
130         string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
131         string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
132
133         void INLINE_LIST( ostream &ret, InlineList *inlineList, int targState, bool inFinish );
134         virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
135         virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
136         virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
137         virtual void GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
138         virtual void NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
139         virtual void CALL_EXPR( ostream &ret, InlineItem *ilItem, 
140                         int targState, bool inFinish ) = 0;
141         virtual void RET( ostream &ret, bool inFinish ) = 0;
142         virtual void BREAK( ostream &ret, int targState ) = 0;
143         virtual void CURS( ostream &ret, bool inFinish ) = 0;
144         virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
145         void EXEC( ostream &ret, InlineItem *item, int targState, int inFinish );
146         void EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish );
147         void LM_SWITCH( ostream &ret, InlineItem *item, int targState, int inFinish );
148         void SET_ACT( ostream &ret, InlineItem *item );
149         void INIT_TOKSTART( ostream &ret, InlineItem *item );
150         void INIT_ACT( ostream &ret, InlineItem *item );
151         void SET_TOKSTART( ostream &ret, InlineItem *item );
152         void SET_TOKEND( ostream &ret, InlineItem *item );
153         void GET_TOKEND( ostream &ret, InlineItem *item );
154         void SUB_ACTION( ostream &ret, InlineItem *item, 
155                         int targState, bool inFinish );
156
157         string ERROR_STATE();
158         string FIRST_FINAL_STATE();
159
160         virtual string PTR_CONST() = 0;
161         virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
162         virtual ostream &CLOSE_ARRAY() = 0;
163         virtual ostream &STATIC_VAR( string type, string name ) = 0;
164
165         virtual string CTRL_FLOW() = 0;
166
167         unsigned int arrayTypeSize( unsigned long maxVal );
168
169         bool anyActions();
170         bool anyToStateActions()        { return bAnyToStateActions; }
171         bool anyFromStateActions()      { return bAnyFromStateActions; }
172         bool anyRegActions()            { return bAnyRegActions; }
173         bool anyEofActions()            { return bAnyEofActions; }
174         bool anyActionGotos()           { return bAnyActionGotos; }
175         bool anyActionCalls()           { return bAnyActionCalls; }
176         bool anyActionRets()            { return bAnyActionRets; }
177         bool anyRegActionRets()         { return bAnyRegActionRets; }
178         bool anyRegActionByValControl() { return bAnyRegActionByValControl; }
179         bool anyRegNextStmt()           { return bAnyRegNextStmt; }
180         bool anyRegCurStateRef()        { return bAnyRegCurStateRef; }
181         bool anyRegBreak()              { return bAnyRegBreak; }
182         bool anyLmSwitchError()         { return bAnyLmSwitchError; }
183         bool anyConditions()            { return bAnyConditions; }
184
185         /* Set up labelNeeded flag for each state. Differs for each goto style so
186          * is virtual. */
187         virtual void setLabelsNeeded() {}
188
189         /* Determine if we should use indicies. */
190         virtual void calcIndexSize() {}
191
192         /* Are there any regular transition functions, any out transition functions. */
193         CodeGenData *cgd;
194         RedFsmAp *redFsm;
195
196         bool outLabelUsed;
197         bool againLabelUsed;
198
199 protected:
200         ostream &out;
201
202         bool bAnyToStateActions;
203         bool bAnyFromStateActions;
204         bool bAnyRegActions;
205         bool bAnyEofActions;
206         bool bAnyActionGotos;
207         bool bAnyActionCalls;
208         bool bAnyActionRets;
209         bool bAnyRegActionRets;
210         bool bAnyRegActionByValControl;
211         bool bAnyRegNextStmt;
212         bool bAnyRegCurStateRef;
213         bool bAnyRegBreak;
214         bool bAnyLmSwitchError;
215         bool bAnyConditions;
216
217         int maxState;
218         int maxSingleLen;
219         int maxRangeLen;
220         int maxKeyOffset;
221         int maxIndexOffset;
222         int maxIndex;
223         int maxActListId;
224         int maxActionLoc;
225         int maxActArrItem;
226         unsigned long long maxSpan;
227         unsigned long long maxCondSpan;
228         int maxFlatIndexOffset;
229         Key maxKey;
230         int maxCondOffset;
231         int maxCondLen;
232         int maxCondSpaceId;
233         int maxCondIndexOffset;
234         int maxCond;
235
236         bool useIndicies;
237 };
238
239 class CCodeGen : virtual public FsmCodeGen
240 {
241 public:
242         CCodeGen( ostream &out ) : FsmCodeGen(out) {}
243
244         virtual string NULL_ITEM();
245         virtual string POINTER();
246         virtual ostream &SWITCH_DEFAULT();
247         virtual ostream &OPEN_ARRAY( string type, string name );
248         virtual ostream &CLOSE_ARRAY();
249         virtual ostream &STATIC_VAR( string type, string name );
250         virtual string ARR_OFF( string ptr, string offset );
251         virtual string CAST( string type );
252         virtual string UINT();
253         virtual string PTR_CONST();
254         virtual string CTRL_FLOW();
255 };
256
257 class DCodeGen : virtual public FsmCodeGen
258 {
259 public:
260         DCodeGen( ostream &out ) : FsmCodeGen(out) {}
261
262         virtual string NULL_ITEM();
263         virtual string POINTER();
264         virtual ostream &SWITCH_DEFAULT();
265         virtual ostream &OPEN_ARRAY( string type, string name );
266         virtual ostream &CLOSE_ARRAY();
267         virtual ostream &STATIC_VAR( string type, string name );
268         virtual string ARR_OFF( string ptr, string offset );
269         virtual string CAST( string type );
270         virtual string UINT();
271         virtual string PTR_CONST();
272         virtual string CTRL_FLOW();
273 };
274
275 class JavaCodeGen : virtual public FsmCodeGen
276 {
277 public:
278         JavaCodeGen( ostream &out ) : FsmCodeGen(out) {}
279
280         virtual string NULL_ITEM();
281         virtual string POINTER();
282         virtual ostream &SWITCH_DEFAULT();
283         virtual ostream &OPEN_ARRAY( string type, string name );
284         virtual ostream &CLOSE_ARRAY();
285         virtual ostream &STATIC_VAR( string type, string name );
286         virtual string ARR_OFF( string ptr, string offset );
287         virtual string CAST( string type );
288         virtual string UINT();
289         virtual string PTR_CONST();
290         virtual string GET_KEY();
291         virtual string CTRL_FLOW();
292 };
293
294 #endif /* _FSMCODEGEN_H */