cdcodegen.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* This file is part of Ragel.
  7. *
  8. * Ragel is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Ragel is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Ragel; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #ifndef _CDCODEGEN_H
  23. #define _CDCODEGEN_H
  24. #include <iostream>
  25. #include <string>
  26. #include <stdio.h>
  27. #include "common.h"
  28. #include "gendata.h"
  29. using std::string;
  30. using std::ostream;
  31. /* Integer array line length. */
  32. #define IALL 8
  33. /* Forwards. */
  34. struct RedFsmAp;
  35. struct RedStateAp;
  36. struct CodeGenData;
  37. struct GenAction;
  38. struct NameInst;
  39. struct GenInlineItem;
  40. struct GenInlineList;
  41. struct RedAction;
  42. struct LongestMatch;
  43. struct LongestMatchPart;
  44. string itoa( int i );
  45. /*
  46. * class FsmCodeGen
  47. */
  48. class FsmCodeGen : public CodeGenData
  49. {
  50. public:
  51. FsmCodeGen( ostream &out );
  52. virtual ~FsmCodeGen() {}
  53. virtual void finishRagelDef();
  54. virtual void writeInit();
  55. virtual void writeStart();
  56. virtual void writeFirstFinal();
  57. virtual void writeError();
  58. protected:
  59. string FSM_NAME();
  60. string START_STATE_ID();
  61. ostream &ACTIONS_ARRAY();
  62. string GET_WIDE_KEY();
  63. string GET_WIDE_KEY( RedStateAp *state );
  64. string TABS( int level );
  65. string KEY( Key key );
  66. string WIDE_KEY( RedStateAp *state, Key key );
  67. string LDIR_PATH( char *path );
  68. virtual void ACTION( ostream &ret, GenAction *action, int targState,
  69. bool inFinish, bool csForced );
  70. void CONDITION( ostream &ret, GenAction *condition );
  71. string ALPH_TYPE();
  72. string WIDE_ALPH_TYPE();
  73. string ARRAY_TYPE( unsigned long maxVal );
  74. bool isAlphTypeSigned();
  75. bool isWideAlphTypeSigned();
  76. virtual string ARR_OFF( string ptr, string offset ) = 0;
  77. virtual string CAST( string type ) = 0;
  78. virtual string UINT() = 0;
  79. virtual string NULL_ITEM() = 0;
  80. virtual string POINTER() = 0;
  81. virtual string GET_KEY();
  82. virtual ostream &SWITCH_DEFAULT() = 0;
  83. string P();
  84. string PE();
  85. string vEOF();
  86. string ACCESS();
  87. string vCS();
  88. string STACK();
  89. string TOP();
  90. string TOKSTART();
  91. string TOKEND();
  92. string ACT();
  93. string DATA_PREFIX();
  94. string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
  95. string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
  96. string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
  97. string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
  98. string I() { return "_" + DATA_PREFIX() + "indicies"; }
  99. string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
  100. string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
  101. string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
  102. string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
  103. string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
  104. string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
  105. string A() { return "_" + DATA_PREFIX() + "actions"; }
  106. string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
  107. string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
  108. string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
  109. string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
  110. string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
  111. string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
  112. string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
  113. string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
  114. string START() { return DATA_PREFIX() + "start"; }
  115. string ERROR() { return DATA_PREFIX() + "error"; }
  116. string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
  117. string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
  118. void EOF_CHECK( ostream &ret );
  119. void INLINE_LIST( ostream &ret, GenInlineList *inlineList,
  120. int targState, bool inFinish, bool csForced );
  121. virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
  122. virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
  123. virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
  124. virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  125. virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  126. virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem,
  127. int targState, bool inFinish ) = 0;
  128. virtual void RET( ostream &ret, bool inFinish ) = 0;
  129. virtual void BREAK( ostream &ret, int targState, bool csForced ) = 0;
  130. virtual void CURS( ostream &ret, bool inFinish ) = 0;
  131. virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
  132. void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
  133. void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState,
  134. int inFinish, bool csForced );
  135. void SET_ACT( ostream &ret, GenInlineItem *item );
  136. void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
  137. void INIT_ACT( ostream &ret, GenInlineItem *item );
  138. void SET_TOKSTART( ostream &ret, GenInlineItem *item );
  139. void SET_TOKEND( ostream &ret, GenInlineItem *item );
  140. void GET_TOKEND( ostream &ret, GenInlineItem *item );
  141. virtual void SUB_ACTION( ostream &ret, GenInlineItem *item,
  142. int targState, bool inFinish, bool csForced );
  143. void STATE_IDS();
  144. string ERROR_STATE();
  145. string FIRST_FINAL_STATE();
  146. virtual string PTR_CONST() = 0;
  147. virtual string PTR_CONST_END() = 0;
  148. virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
  149. virtual ostream &CLOSE_ARRAY() = 0;
  150. virtual ostream &STATIC_VAR( string type, string name ) = 0;
  151. virtual string CTRL_FLOW() = 0;
  152. ostream &source_warning(const InputLoc &loc);
  153. ostream &source_error(const InputLoc &loc);
  154. unsigned int arrayTypeSize( unsigned long maxVal );
  155. bool outLabelUsed;
  156. bool testEofUsed;
  157. bool againLabelUsed;
  158. bool useIndicies;
  159. void genLineDirective( ostream &out );
  160. public:
  161. /* Determine if we should use indicies. */
  162. virtual void calcIndexSize() {}
  163. };
  164. class CCodeGen : virtual public FsmCodeGen
  165. {
  166. public:
  167. CCodeGen( ostream &out ) : FsmCodeGen(out) {}
  168. virtual string NULL_ITEM();
  169. virtual string POINTER();
  170. virtual ostream &SWITCH_DEFAULT();
  171. virtual ostream &OPEN_ARRAY( string type, string name );
  172. virtual ostream &CLOSE_ARRAY();
  173. virtual ostream &STATIC_VAR( string type, string name );
  174. virtual string ARR_OFF( string ptr, string offset );
  175. virtual string CAST( string type );
  176. virtual string UINT();
  177. virtual string PTR_CONST();
  178. virtual string PTR_CONST_END();
  179. virtual string CTRL_FLOW();
  180. virtual void writeExports();
  181. };
  182. class DCodeGen : virtual public FsmCodeGen
  183. {
  184. public:
  185. DCodeGen( ostream &out ) : FsmCodeGen(out) {}
  186. virtual string NULL_ITEM();
  187. virtual string POINTER();
  188. virtual ostream &SWITCH_DEFAULT();
  189. virtual ostream &OPEN_ARRAY( string type, string name );
  190. virtual ostream &CLOSE_ARRAY();
  191. virtual ostream &STATIC_VAR( string type, string name );
  192. virtual string ARR_OFF( string ptr, string offset );
  193. virtual string CAST( string type );
  194. virtual string UINT();
  195. virtual string PTR_CONST();
  196. virtual string PTR_CONST_END();
  197. virtual string CTRL_FLOW();
  198. virtual void writeExports();
  199. };
  200. class D2CodeGen : virtual public FsmCodeGen
  201. {
  202. public:
  203. D2CodeGen( ostream &out ) : FsmCodeGen(out) {}
  204. virtual string NULL_ITEM();
  205. virtual string POINTER();
  206. virtual ostream &SWITCH_DEFAULT();
  207. virtual ostream &OPEN_ARRAY( string type, string name );
  208. virtual ostream &CLOSE_ARRAY();
  209. virtual ostream &STATIC_VAR( string type, string name );
  210. virtual string ARR_OFF( string ptr, string offset );
  211. virtual string CAST( string type );
  212. virtual string UINT();
  213. virtual string PTR_CONST();
  214. virtual string PTR_CONST_END();
  215. virtual string CTRL_FLOW();
  216. virtual void writeExports();
  217. virtual void SUB_ACTION( ostream &ret, GenInlineItem *item,
  218. int targState, bool inFinish, bool csForced );
  219. virtual void ACTION( ostream &ret, GenAction *action, int targState,
  220. bool inFinish, bool csForced );
  221. };
  222. #endif