Moved more analysis code from FsmCodeGen into CodeGenData. Eliminated the
[external/ragel.git] / rlcodegen / gendata.h
1 /*
2  *  Copyright 2005-2006 Adrian Thurston <thurston@cs.queensu.ca>
3  */
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
22 #ifndef _GENDATA_H
23 #define _GENDATA_H
24
25 #include <iostream>
26 #include "redfsm.h"
27 #include "fsmcodegen.h"
28 #include "common.h"
29
30 struct NameInst;
31 typedef DList<Action> ActionList;
32
33 typedef unsigned long ulong;
34
35 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
36 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
37
38 #define WO_NOEND    0x01
39 #define WO_NOERROR  0x02
40 #define WO_NOPREFIX 0x04
41 #define WO_NOFF     0x08
42
43 struct CodeGenData
44 {
45         CodeGenData( char *fileName, char *fsmName, ostream &out, bool wantComplete )
46         :
47                 fileName(fileName),
48                 fsmName(fsmName), 
49                 out(out),
50                 redFsm(0), 
51                 allActions(0),
52                 allActionTables(0),
53                 allConditions(0),
54                 allCondSpaces(0),
55                 allStates(0),
56                 nameIndex(0),
57                 startState(0),
58                 getKeyExpr(0),
59                 accessExpr(0),
60                 curStateExpr(0),
61                 codeGen(0),
62                 wantComplete(wantComplete),
63                 writeOps(0),
64                 writeData(false),
65                 writeInit(false),
66                 writeExec(false),
67                 writeEOF(false),
68                 hasLongestMatch(false),
69                 hasEnd(true),
70                 dataPrefix(true),
71                 writeFirstFinal(true),
72                 writeErr(true),
73                 hasBeenPrepared(false)
74         { }
75
76         /* 
77          * Collecting the machine.
78          */
79
80         char *fileName;
81         char *fsmName;
82         ostream &out;
83         RedFsmAp *redFsm;
84         Action *allActions;
85         RedAction *allActionTables;
86         Condition *allConditions;
87         CondSpace *allCondSpaces;
88         RedStateAp *allStates;
89         NameInst **nameIndex;
90         int startState;
91         ActionList actionList;
92         ConditionList conditionList;
93         CondSpaceList condSpaceList;
94         InlineList *getKeyExpr;
95         InlineList *accessExpr;
96         InlineList *curStateExpr;
97         FsmCodeGen *codeGen;
98         KeyOps thisKeyOps;
99         bool wantComplete;
100         int writeOps;
101         bool writeData;
102         bool writeInit;
103         bool writeExec;
104         bool writeEOF;
105         EntryIdVect entryPointIds;
106         EntryNameVect entryPointNames;
107         bool hasLongestMatch;
108
109         /* Write options. */
110         bool hasEnd;
111         bool dataPrefix;
112         bool writeFirstFinal;
113         bool writeErr;
114
115         void createMachine();
116         void initActionList( unsigned long length );
117         void newAction( int anum, char *name, int line, int col, InlineList *inlineList );
118         void initActionTableList( unsigned long length );
119         void initStateList( unsigned long length );
120         void setStartState( unsigned long startState );
121         void addEntryPoint( char *name, unsigned long entryState );
122         void setFinal( int snum );
123         void initTransList( int snum, unsigned long length );
124         void newTrans( int snum, int tnum, Key lowKey, Key highKey, 
125                         long targ, long act );
126         void finishTransList( int snum );
127         void setStateActions( int snum, long toStateAction, 
128                         long fromStateAction, long eofAction );
129         void finishMachine();
130         void setForcedErrorState()
131                 { redFsm->forcedErrorState = true; }
132         
133         void initCondSpaceList( ulong length );
134         void condSpaceItem( int cnum, long condActionId );
135         void newCondSpace( int cnum, int condSpaceId, Key baseKey );
136
137         void initStateCondList( int snum, ulong length );
138         void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
139
140         CondSpace *findCondSpace( Key lowKey, Key highKey );
141         Condition *findCondition( Key key );
142
143         bool setAlphType( char *data );
144
145         void makeCodeGen();
146         void generateGraphviz();
147         void resolveTargetStates( InlineList *inlineList );
148         Key findMaxKey();
149
150         void generate();
151         void generateCode();
152
153         /* Gather various info on the machine. */
154         void analyzeActionList( RedAction *redAct, InlineList *inlineList );
155         void analyzeAction( Action *act, InlineList *inlineList );
156         void findFinalActionRefs();
157         void analyzeMachine();
158
159         void setValueLimits();
160         void assignActionIds();
161         void prepareMachine();
162         bool hasBeenPrepared;
163 };
164
165 extern CodeGenData *cgd;
166
167 void lineDirective( ostream &out, char *fileName, int line );
168 void genLineDirective( ostream &out );
169
170 #endif /* _GENDATA_H */