mlcodegen.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 _MLCODEGEN_H
  23. #define _MLCODEGEN_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. /* Forwards. */
  32. /*
  33. struct RedFsmAp;
  34. struct RedStateAp;
  35. struct CodeGenData;
  36. struct GenAction;
  37. struct NameInst;
  38. struct GenInlineItem;
  39. struct GenInlineList;
  40. struct RedAction;
  41. struct LongestMatch;
  42. struct LongestMatchPart;
  43. */
  44. /* Integer array line length. */
  45. #define IALL 8
  46. //string itoa( int i );
  47. /*
  48. * class OCamlCodeGen
  49. */
  50. class OCamlCodeGen : public CodeGenData
  51. {
  52. public:
  53. OCamlCodeGen( ostream &out );
  54. virtual ~OCamlCodeGen() {}
  55. virtual void finishRagelDef();
  56. virtual void writeInit();
  57. virtual void writeStart();
  58. virtual void writeFirstFinal();
  59. virtual void writeError();
  60. protected:
  61. string data_prefix;
  62. string FSM_NAME();
  63. string START_STATE_ID();
  64. ostream &ACTIONS_ARRAY();
  65. string GET_WIDE_KEY();
  66. string GET_WIDE_KEY( RedStateAp *state );
  67. string TABS( int level );
  68. string KEY( Key key );
  69. string ALPHA_KEY( Key key );
  70. string LDIR_PATH( char *path );
  71. void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish );
  72. void CONDITION( ostream &ret, GenAction *condition );
  73. string ALPH_TYPE();
  74. string WIDE_ALPH_TYPE();
  75. string ARRAY_TYPE( unsigned long maxVal );
  76. string ARRAY_TYPE( unsigned long maxVal, bool forceSigned );
  77. virtual string ARR_OFF( string ptr, string offset );
  78. virtual string CAST( string type );
  79. virtual string UINT();
  80. virtual string NULL_ITEM();
  81. virtual string POINTER();
  82. virtual string GET_KEY();
  83. virtual ostream &SWITCH_DEFAULT();
  84. string P();
  85. string PE();
  86. string vEOF();
  87. // string ACCESS();
  88. string vCS();
  89. string STACK();
  90. string TOP();
  91. string TOKSTART();
  92. string TOKEND();
  93. string ACT();
  94. // ++x
  95. string PRE_INCR(string);
  96. string PRE_DECR(string);
  97. // x++
  98. string POST_INCR(string);
  99. string POST_DECR(string);
  100. string DATA_PREFIX();
  101. string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
  102. string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
  103. string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
  104. string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
  105. string I() { return "_" + DATA_PREFIX() + "indicies"; }
  106. string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
  107. string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
  108. string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
  109. string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
  110. string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
  111. string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
  112. string A() { return "_" + DATA_PREFIX() + "actions"; }
  113. string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
  114. string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
  115. string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
  116. string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
  117. string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
  118. string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
  119. string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
  120. string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
  121. string TYPE_STATE() { return "_" + DATA_PREFIX() + "state"; }
  122. string START() { return DATA_PREFIX() + "start"; }
  123. string ERROR() { return DATA_PREFIX() + "error"; }
  124. string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
  125. string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
  126. void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish );
  127. virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
  128. virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
  129. virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
  130. virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  131. virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  132. virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem,
  133. int targState, bool inFinish ) = 0;
  134. virtual void RET( ostream &ret, bool inFinish ) = 0;
  135. virtual void BREAK( ostream &ret, int targState ) = 0;
  136. virtual void CURS( ostream &ret, bool inFinish ) = 0;
  137. virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
  138. void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
  139. void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, int inFinish );
  140. void SET_ACT( ostream &ret, GenInlineItem *item );
  141. void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
  142. void INIT_ACT( ostream &ret, GenInlineItem *item );
  143. void SET_TOKSTART( ostream &ret, GenInlineItem *item );
  144. void SET_TOKEND( ostream &ret, GenInlineItem *item );
  145. void GET_TOKEND( ostream &ret, GenInlineItem *item );
  146. void SUB_ACTION( ostream &ret, GenInlineItem *item,
  147. int targState, bool inFinish );
  148. void STATE_IDS();
  149. string ERROR_STATE();
  150. string FIRST_FINAL_STATE();
  151. virtual string PTR_CONST();
  152. virtual ostream &OPEN_ARRAY( string type, string name );
  153. virtual ostream &CLOSE_ARRAY();
  154. virtual ostream &STATIC_VAR( string type, string name );
  155. virtual string CTRL_FLOW();
  156. // toplevel phrase separator
  157. string TOP_SEP();
  158. // array elements separator
  159. string ARR_SEP();
  160. // access array
  161. string AT(const string& array, const string& index);
  162. string make_access(char const* name, GenInlineList* x, bool prefix);
  163. ostream &source_warning(const InputLoc &loc);
  164. ostream &source_error(const InputLoc &loc);
  165. unsigned int arrayTypeSize( unsigned long maxVal );
  166. bool outLabelUsed;
  167. bool testEofUsed;
  168. bool againLabelUsed;
  169. bool useIndicies;
  170. public:
  171. /* Determine if we should use indicies. */
  172. virtual void calcIndexSize() {}
  173. void genLineDirective( ostream &out );
  174. };
  175. #define MAX(a, b) (a > b ? a : b)
  176. #endif