1 /* 2 * Copyright 2001-2006 Adrian Thurston <thurston@complang.org> 3 * 2004 Erich 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 _CDCODEGEN_H 25 #define _CDCODEGEN_H 26 27 #include <iostream> 28 #include <string> 29 #include <stdio.h> 30 #include "common.h" 31 #include "gendata.h" 32 33 using std::string; 34 using std::ostream; 35 36 /* Integer array line length. */ 37 #define IALL 8 38 39 /* Forwards. */ 40 struct RedFsmAp; 41 struct RedStateAp; 42 struct CodeGenData; 43 struct GenAction; 44 struct NameInst; 45 struct GenInlineItem; 46 struct GenInlineList; 47 struct RedAction; 48 struct LongestMatch; 49 struct LongestMatchPart; 50 51 string itoa( int i ); 52 53 /* 54 * class FsmCodeGen 55 */ 56 class FsmCodeGen : public CodeGenData 57 { 58 public: 59 FsmCodeGen( ostream &out ); ~FsmCodeGen()60 virtual ~FsmCodeGen() {} 61 62 virtual void finishRagelDef(); 63 virtual void writeInit(); 64 virtual void writeStart(); 65 virtual void writeFirstFinal(); 66 virtual void writeError(); 67 68 protected: 69 string FSM_NAME(); 70 string START_STATE_ID(); 71 ostream &ACTIONS_ARRAY(); 72 string GET_WIDE_KEY(); 73 string GET_WIDE_KEY( RedStateAp *state ); 74 string TABS( int level ); 75 string KEY( Key key ); 76 string WIDE_KEY( RedStateAp *state, Key key ); 77 string LDIR_PATH( char *path ); 78 virtual void ACTION( ostream &ret, GenAction *action, int targState, 79 bool inFinish, bool csForced ); 80 void CONDITION( ostream &ret, GenAction *condition ); 81 string ALPH_TYPE(); 82 string WIDE_ALPH_TYPE(); 83 string ARRAY_TYPE( unsigned long maxVal ); 84 85 bool isAlphTypeSigned(); 86 bool isWideAlphTypeSigned(); 87 88 virtual string ARR_OFF( string ptr, string offset ) = 0; 89 virtual string CAST( string type ) = 0; 90 virtual string UINT() = 0; 91 virtual string NULL_ITEM() = 0; 92 virtual string POINTER() = 0; 93 virtual string GET_KEY(); 94 virtual ostream &SWITCH_DEFAULT() = 0; 95 96 string P(); 97 string PE(); 98 string vEOF(); 99 100 string ACCESS(); 101 string vCS(); 102 string STACK(); 103 string TOP(); 104 string TOKSTART(); 105 string TOKEND(); 106 string ACT(); 107 108 string DATA_PREFIX(); PM()109 string PM() { return "_" + DATA_PREFIX() + "partition_map"; } C()110 string C() { return "_" + DATA_PREFIX() + "cond_spaces"; } CK()111 string CK() { return "_" + DATA_PREFIX() + "cond_keys"; } K()112 string K() { return "_" + DATA_PREFIX() + "trans_keys"; } I()113 string I() { return "_" + DATA_PREFIX() + "indicies"; } CO()114 string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; } KO()115 string KO() { return "_" + DATA_PREFIX() + "key_offsets"; } IO()116 string IO() { return "_" + DATA_PREFIX() + "index_offsets"; } CL()117 string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; } SL()118 string SL() { return "_" + DATA_PREFIX() + "single_lengths"; } RL()119 string RL() { return "_" + DATA_PREFIX() + "range_lengths"; } A()120 string A() { return "_" + DATA_PREFIX() + "actions"; } TA()121 string TA() { return "_" + DATA_PREFIX() + "trans_actions"; } TT()122 string TT() { return "_" + DATA_PREFIX() + "trans_targs"; } TSA()123 string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; } FSA()124 string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; } EA()125 string EA() { return "_" + DATA_PREFIX() + "eof_actions"; } ET()126 string ET() { return "_" + DATA_PREFIX() + "eof_trans"; } SP()127 string SP() { return "_" + DATA_PREFIX() + "key_spans"; } CSP()128 string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; } START()129 string START() { return DATA_PREFIX() + "start"; } ERROR()130 string ERROR() { return DATA_PREFIX() + "error"; } FIRST_FINAL()131 string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; } CTXDATA()132 string CTXDATA() { return DATA_PREFIX() + "ctxdata"; } 133 134 void EOF_CHECK( ostream &ret ); 135 136 void INLINE_LIST( ostream &ret, GenInlineList *inlineList, 137 int targState, bool inFinish, bool csForced ); 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, GenInlineItem *ilItem, bool inFinish ) = 0; 142 virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0; 143 virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, 144 int targState, bool inFinish ) = 0; 145 virtual void RET( ostream &ret, bool inFinish ) = 0; 146 virtual void BREAK( ostream &ret, int targState, bool csForced ) = 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, GenInlineItem *item, int targState, int inFinish ); 150 void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, 151 int inFinish, bool csForced ); 152 void SET_ACT( ostream &ret, GenInlineItem *item ); 153 void INIT_TOKSTART( ostream &ret, GenInlineItem *item ); 154 void INIT_ACT( ostream &ret, GenInlineItem *item ); 155 void SET_TOKSTART( ostream &ret, GenInlineItem *item ); 156 void SET_TOKEND( ostream &ret, GenInlineItem *item ); 157 void GET_TOKEND( ostream &ret, GenInlineItem *item ); 158 virtual void SUB_ACTION( ostream &ret, GenInlineItem *item, 159 int targState, bool inFinish, bool csForced ); 160 void STATE_IDS(); 161 162 string ERROR_STATE(); 163 string FIRST_FINAL_STATE(); 164 165 virtual string PTR_CONST() = 0; 166 virtual string PTR_CONST_END() = 0; 167 virtual ostream &OPEN_ARRAY( string type, string name ) = 0; 168 virtual ostream &CLOSE_ARRAY() = 0; 169 virtual ostream &STATIC_VAR( string type, string name ) = 0; 170 171 virtual string CTRL_FLOW() = 0; 172 173 ostream &source_warning(const InputLoc &loc); 174 ostream &source_error(const InputLoc &loc); 175 176 unsigned int arrayTypeSize( unsigned long maxVal ); 177 178 bool outLabelUsed; 179 bool testEofUsed; 180 bool againLabelUsed; 181 bool useIndicies; 182 183 void genLineDirective( ostream &out ); 184 185 public: 186 /* Determine if we should use indicies. */ calcIndexSize()187 virtual void calcIndexSize() {} 188 }; 189 190 class CCodeGen : virtual public FsmCodeGen 191 { 192 public: CCodeGen(ostream & out)193 CCodeGen( ostream &out ) : FsmCodeGen(out) {} 194 195 virtual string NULL_ITEM(); 196 virtual string POINTER(); 197 virtual ostream &SWITCH_DEFAULT(); 198 virtual ostream &OPEN_ARRAY( string type, string name ); 199 virtual ostream &CLOSE_ARRAY(); 200 virtual ostream &STATIC_VAR( string type, string name ); 201 virtual string ARR_OFF( string ptr, string offset ); 202 virtual string CAST( string type ); 203 virtual string UINT(); 204 virtual string PTR_CONST(); 205 virtual string PTR_CONST_END(); 206 virtual string CTRL_FLOW(); 207 208 virtual void writeExports(); 209 }; 210 211 class DCodeGen : virtual public FsmCodeGen 212 { 213 public: DCodeGen(ostream & out)214 DCodeGen( ostream &out ) : FsmCodeGen(out) {} 215 216 virtual string NULL_ITEM(); 217 virtual string POINTER(); 218 virtual ostream &SWITCH_DEFAULT(); 219 virtual ostream &OPEN_ARRAY( string type, string name ); 220 virtual ostream &CLOSE_ARRAY(); 221 virtual ostream &STATIC_VAR( string type, string name ); 222 virtual string ARR_OFF( string ptr, string offset ); 223 virtual string CAST( string type ); 224 virtual string UINT(); 225 virtual string PTR_CONST(); 226 virtual string PTR_CONST_END(); 227 virtual string CTRL_FLOW(); 228 229 virtual void writeExports(); 230 }; 231 232 class D2CodeGen : virtual public FsmCodeGen 233 { 234 public: D2CodeGen(ostream & out)235 D2CodeGen( ostream &out ) : FsmCodeGen(out) {} 236 237 virtual string NULL_ITEM(); 238 virtual string POINTER(); 239 virtual ostream &SWITCH_DEFAULT(); 240 virtual ostream &OPEN_ARRAY( string type, string name ); 241 virtual ostream &CLOSE_ARRAY(); 242 virtual ostream &STATIC_VAR( string type, string name ); 243 virtual string ARR_OFF( string ptr, string offset ); 244 virtual string CAST( string type ); 245 virtual string UINT(); 246 virtual string PTR_CONST(); 247 virtual string PTR_CONST_END(); 248 virtual string CTRL_FLOW(); 249 250 virtual void writeExports(); 251 virtual void SUB_ACTION( ostream &ret, GenInlineItem *item, 252 int targState, bool inFinish, bool csForced ); 253 virtual void ACTION( ostream &ret, GenAction *action, int targState, 254 bool inFinish, bool csForced ); 255 256 }; 257 258 #endif 259