home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / scc_parse.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-01-29  |  10.4 KB  |  484 lines

  1. /* ScummC
  2.  * Copyright (C) 2004-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /// @defgroup scc Compiler
  21. /**
  22.  * @file scc_parse.h
  23.  * @ingroup scc
  24.  * @brief ScummC compiler
  25.  */
  26.  
  27. #define SCC_MAX_ARGS         31
  28. #define SCC_MAX_CLASS        32
  29.  
  30.  
  31. /// @name Statement type
  32. //@{
  33. /// direct integer vals
  34. #define SCC_ST_VAL     0
  35. /// ressources (room, actor, verb, etc)
  36. #define SCC_ST_RES     1
  37. /// string
  38. #define SCC_ST_STR     2
  39. /// function call
  40. #define SCC_ST_CALL    3
  41. /// list
  42. #define SCC_ST_LIST    4
  43. /// variables
  44. #define SCC_ST_VAR     5
  45. /// operation
  46. #define SCC_ST_OP      6
  47. /// statement chain
  48. #define SCC_ST_CHAIN   7
  49. //@}
  50.  
  51. /// @name Variable type
  52. //@{
  53. #define SCC_VOID        0
  54. #define SCC_VAR_BIT     1
  55. #define SCC_VAR_NIBBLE  2
  56. #define SCC_VAR_BYTE    3
  57. #define SCC_VAR_CHAR    4
  58. #define SCC_VAR_WORD    5
  59.  
  60. #define SCC_VAR_ARRAY   0x100
  61. //@}
  62.  
  63. typedef struct scc_code_st scc_code_t;
  64. typedef struct scc_func_st scc_func_t;
  65. typedef struct scc_arg_st scc_arg_t;
  66. typedef struct scc_statement_st scc_statement_t;
  67. typedef struct scc_symbol_st scc_symbol_t;
  68. typedef struct scc_scr_arg_st scc_scr_arg_t;
  69. typedef struct scc_script_st scc_script_t;
  70. typedef struct scc_decl_st scc_decl_t;
  71. typedef struct scc_instruct_st scc_instruct_t;
  72. typedef struct scc_loop_st scc_loop_t;
  73. typedef struct scc_call_st scc_call_t;
  74. typedef struct scc_op_st scc_op_t;
  75. typedef struct scc_operator_st scc_operator_t;
  76. typedef struct scc_sym_fix_st scc_sym_fix_t;
  77. typedef struct scc_str_st scc_str_t;
  78. typedef struct scc_verb_script_st scc_verb_script_t;
  79.  
  80. /// @name Function argument type
  81. //@{
  82.  
  83. /// Mask to match any reference type
  84. #define SCC_FA_REF    0xF00
  85. /// Byte reference
  86. #define SCC_FA_BREF   0x100
  87. /// Word reference
  88. #define SCC_FA_WREF   0x200
  89. /// String reference
  90. #define SCC_FA_SREF   0x400
  91.  
  92. /// Argument have a default value
  93. #define SCC_FA_DEFAULT  0x10000
  94.  
  95. /// value, on stack
  96. #define SCC_FA_VAL      0
  97. /// list of value, on stack
  98. #define SCC_FA_LIST     1
  99. /// string, emmbeded in the code
  100. #define SCC_FA_STR     (2|SCC_FA_SREF)
  101. /// array pointers
  102. #define SCC_FA_ARRAY   (3|SCC_FA_WREF)
  103. /// offset to the begining of the call
  104. #define SCC_FA_SELF_OFF 4
  105. /// extra op argument, the op must be set in the higher 16bits
  106. /// this is for functions of the form foo([ op, ... ])
  107. #define SCC_FA_OP       5
  108. /// Macro to place the opcode in the argument list
  109. #define SCC_FA_OPC(x)   (SCC_FA_OP | ((x)<<16))
  110. //@}
  111.  
  112. /// @name Operation type
  113. //@{
  114. /// Asignement
  115. #define SCC_OT_ASSIGN  0
  116. /// Unary operation
  117. #define SCC_OT_UNARY   1
  118. /// Binary operation
  119. #define SCC_OT_BINARY  2
  120. /// Ternary operation
  121. #define SCC_OT_TERNARY 3
  122. //@}
  123.  
  124. /// Code block
  125. struct scc_code_st {
  126.   scc_code_t* next;
  127.  
  128.   uint8_t* data;
  129.   int fix,len;
  130. };
  131.  
  132. /// Function definition
  133. struct scc_func_st {
  134.   /// Symbol
  135.   char* sym;
  136.  
  137.   /// Opcode
  138.   uint16_t opcode;
  139.  
  140.   /// True if it return something
  141.   int ret;
  142.   /// Number of arguments
  143.   int argc;
  144.   /// Hidden argument, for wait and iMUSE
  145.   int hidden_args;
  146.   /// Argument types
  147.   int argt[SCC_MAX_ARGS];
  148.   /// Default argument value
  149.   int dfault[SCC_MAX_ARGS];
  150. };
  151.  
  152. /// Function call
  153. struct scc_call_st {
  154.   scc_func_t* func;
  155.   int user_script;
  156.   int argc;
  157.   scc_statement_t* argv;
  158. };
  159.  
  160. /// Operation
  161. struct scc_op_st {
  162.   int type;
  163.   int op;
  164.   int argc;
  165.   scc_statement_t* argv;
  166. };
  167.  
  168. /// @name String element types
  169. //@{
  170. /// Plain string
  171. #define SCC_STR_CHAR 0
  172. // here the values are the one used as code in the string
  173. // don't change them.
  174. /// Integer reference
  175. #define SCC_STR_INT      4
  176. /// Verb reference
  177. #define SCC_STR_VERB     5
  178. /// Object name reference
  179. #define SCC_STR_NAME     6
  180. /// String (array) reference
  181. #define SCC_STR_STR      7
  182. /// Voice
  183. #define SCC_STR_VOICE   10
  184. /// Font color change
  185. #define SCC_STR_COLOR   12
  186. /// Font change
  187. #define SCC_STR_FONT    14
  188. //@}
  189.  
  190. /// String
  191. struct scc_str_st {
  192.   scc_str_t* next;
  193.   int type;
  194.   scc_symbol_t* sym; // for var
  195.   char* str;
  196. };
  197.  
  198. /// Statement
  199. struct scc_statement_st {
  200.   scc_statement_t* next;
  201.   int type;
  202.   union {
  203.     /// Value
  204.     uint16_t i;
  205.     /// Ressource
  206.     scc_symbol_t* r;
  207.     /// String
  208.     scc_str_t* s;
  209.     /// Function call
  210.     scc_call_t c;
  211.     /// list / chains
  212.     scc_statement_t* l;
  213.     /// variable
  214.     struct {
  215.       scc_symbol_t* r;
  216.       /// Index for arrays
  217.       scc_statement_t* x, *y;
  218.     } v;
  219.     /// Operation
  220.     scc_op_t o;
  221.   } val;
  222. };
  223.  
  224. /// @name Instruction types
  225. //@{
  226. #define SCC_INST_ST       0
  227. #define SCC_INST_IF       1
  228. #define SCC_INST_FOR      2
  229. #define SCC_INST_WHILE    3
  230. #define SCC_INST_DO       4
  231. #define SCC_INST_BRANCH   5
  232. #define SCC_INST_SWITCH   6
  233. #define SCC_INST_CASE     7
  234. #define SCC_INST_CUTSCENE 8
  235. #define SCC_INST_OVERRIDE 9
  236.  
  237. /// @name Branching instruction types
  238. //@{
  239. #define SCC_BRANCH_BREAK     0
  240. #define SCC_BRANCH_CONTINUE  1
  241. #define SCC_BRANCH_RETURN    2
  242. //@}
  243.  
  244. //@}
  245.  
  246. /// @name Code fix types
  247. //@{
  248. #define SCC_FIX_NONE    0
  249. #define SCC_FIX_BRANCH  1
  250. #define SCC_FIX_RETURN  2
  251. #define SCC_FIX_RES     0x100
  252. //@}
  253.  
  254. /// Instruction
  255. struct scc_instruct_st {
  256.   scc_instruct_t* next;
  257.  
  258.   int type,subtype;
  259.   char* sym;
  260.  
  261.   /// pre-statement, used by for loops and simple statments
  262.   scc_statement_t* pre;
  263.   /// Condition, used by if and loops
  264.   scc_statement_t* cond;
  265.   /// Body, used by if, loops, cutscene and try
  266.   scc_instruct_t* body;
  267.   /// Second body, used by override
  268.   scc_instruct_t* body2;
  269.   /// post-statment, used by for loops
  270.   scc_statement_t* post;
  271. };
  272.  
  273. /// @name Ressource types
  274. //@{
  275.  
  276. /// Global variable
  277. #define SCC_RES_VAR      1
  278. /// Room
  279. #define SCC_RES_ROOM     2
  280. /// Global script
  281. #define SCC_RES_SCR      3
  282. /// Costume
  283. #define SCC_RES_COST     4
  284. /// Sound (midi music)
  285. #define SCC_RES_SOUND    5
  286. /// Charset (font)
  287. #define SCC_RES_CHSET    6
  288. /// Local script
  289. #define SCC_RES_LSCR     7
  290. /// Verb
  291. #define SCC_RES_VERB     8
  292. /// Object
  293. #define SCC_RES_OBJ      9
  294. /// Objectstate
  295. #define SCC_RES_STATE   10
  296. /// Local variable
  297. #define SCC_RES_LVAR    11
  298. /// Bit variable
  299. #define SCC_RES_BVAR    12
  300. /// Palette
  301. #define SCC_RES_PAL     13
  302. /// Palette cycle
  303. #define SCC_RES_CYCL    14
  304. /// Actor
  305. #define SCC_RES_ACTOR   15
  306. /// Box
  307. #define SCC_RES_BOX     16
  308. /// Object class
  309. #define SCC_RES_CLASS   17
  310. /// Voice
  311. #define SCC_RES_VOICE   18
  312. /// Number of ressource type
  313. #define SCC_RES_LAST    19
  314. //@}
  315.  
  316. /// Symbol
  317. struct scc_symbol_st {
  318.   scc_symbol_t* next;
  319.  
  320.   /// Symbol type
  321.   int type;
  322.   /// Subtype, used for variable type and the like
  323.   int subtype;
  324.   /// Symbol name
  325.   char* sym;
  326.   
  327.   /// Address, 0 for automatic allocation
  328.   int addr;
  329.   /// RID, used for auto-allocated address
  330.   int rid;
  331.  
  332.   /// Child symbols for room
  333.   scc_symbol_t* childs;
  334.   /// Parent symbol
  335.   scc_symbol_t* parent;
  336.  
  337.   /// Used by the linker
  338.   char status;
  339. };
  340.  
  341. /// Symbol fix
  342. struct scc_sym_fix_st {
  343.   scc_sym_fix_t* next;
  344.  
  345.   /// Offset of the code to fix
  346.   uint32_t off;
  347.   /// Symbol to use for the fix.
  348.   scc_symbol_t* sym;
  349. };
  350.  
  351. /// @name Script type
  352. //@{
  353. #define SCC_SCR_LOCAL  0
  354. #define SCC_SCR_GLOBAL 1
  355. //@}
  356.  
  357. /// Script argument declaration
  358. struct scc_scr_arg_st {
  359.   scc_scr_arg_t* next;
  360.   int type;
  361.   char* sym;
  362. };
  363.   
  364.  
  365. /// Script
  366. struct scc_script_st {
  367.   scc_script_t* next;
  368.  
  369.   /// The symbol of this script
  370.   scc_symbol_t* sym;
  371.   
  372.   /// The script code
  373.   uint8_t* code;
  374.   /// The code size
  375.   uint32_t code_len;
  376.   /// List of the symbol to fix
  377.   scc_sym_fix_t* sym_fix;
  378. };
  379.  
  380. /// Verb script
  381. struct scc_verb_script_st {
  382.   scc_verb_script_t* next;
  383.   scc_symbol_t* sym;
  384.   scc_instruct_t* inst;
  385. };
  386.  
  387. /// Operator
  388. struct scc_operator_st {
  389.   int scc_op;
  390.   int op;
  391.   int assign_op;
  392. };
  393.  
  394. /// @name SCUMM op-codes
  395. //@{
  396. #define SCC_OP_PUSH_B            0x00
  397. #define SCC_OP_PUSH              0x01
  398. #define SCC_OP_VAR_READ_B        0x02
  399. #define SCC_OP_VAR_READ          0x03
  400.  
  401. #define SCC_OP_ARRAY_READ_B      0x06
  402. #define SCC_OP_ARRAY_READ        0x07
  403.  
  404. #define SCC_OP_ARRAY2_READ_B     0x0A
  405. #define SCC_OP_ARRAY2_READ       0x0B
  406. #define SCC_OP_DUP               0x0C
  407. #define SCC_OP_NOT               0x0D
  408. #define SCC_OP_EQ                0x0E
  409. #define SCC_OP_NEQ               0x0F
  410. #define SCC_OP_G                 0x10
  411. #define SCC_OP_L                 0x11
  412. #define SCC_OP_LE                0x12
  413. #define SCC_OP_GE                0x13
  414. #define SCC_OP_ADD               0x14
  415. #define SCC_OP_SUB               0x15
  416. #define SCC_OP_MUL               0x16
  417. #define SCC_OP_DIV               0x17
  418. #define SCC_OP_LAND              0x18
  419. #define SCC_OP_LOR               0x19
  420.  
  421. #define SCC_OP_POP               0x1A
  422.  
  423. #define SCC_OP_VAR_WRITE_B       0x42
  424. #define SCC_OP_VAR_WRITE         0x43
  425.  
  426. #define SCC_OP_ARRAY_WRITE_B     0x46
  427. #define SCC_OP_ARRAY_WRITE       0x47
  428.  
  429. #define SCC_OP_ARRAY2_WRITE_B    0x4A
  430. #define SCC_OP_ARRAY2_WRITE      0x4B
  431.  
  432. #define SCC_OP_INC_VAR_B         0x4E
  433. #define SCC_OP_INC_VAR           0x4F
  434.  
  435. #define SCC_OP_INC_ARRAY_B       0x52
  436. #define SCC_OP_INC_ARRAY         0x53
  437.  
  438. #define SCC_OP_DEC_VAR_B         0x56
  439. #define SCC_OP_DEC_VAR           0x57
  440.  
  441. #define SCC_OP_DEC_ARRAY_B       0x5A
  442. #define SCC_OP_DEC_ARRAY         0x5B
  443.  
  444. #define SCC_OP_JNZ               0x5C
  445. #define SCC_OP_JZ                0x5D
  446.  
  447. #define SCC_OP_VERB_RET          0x65
  448. #define SCC_OP_SCR_RET           0x66
  449.  
  450. #define SCC_OP_CUTSCENE_END      0x67
  451. #define SCC_OP_CUTSCENE_BEGIN    0x68
  452.  
  453. #define SCC_OP_JMP               0x73
  454.  
  455. #define SCC_OP_OVERRIDE_BEGIN    0x95
  456. #define SCC_OP_OVERRIDE_END      0x96
  457.  
  458. #define SCC_OP_ARRAY_WRITE_STR   0xA4CD
  459.  
  460. #define SCC_OP_ARRAY_WRITE_LIST  0xA4D0
  461. #define SCC_OP_ARRAY2_WRITE_LIST 0xA4D4
  462.  
  463. #define SCC_OP_BAND              0xD6
  464. #define SCC_OP_BOR               0xD7
  465. //@}
  466.  
  467. #define SCC_VAR_RETURN 0xFE
  468.  
  469. typedef struct scc_target_st {
  470.     /// Target version
  471.     int          version;
  472.     /// List of function list
  473.     scc_func_t** func_list;
  474.     /// Highst possible address for each ressource type
  475.     int*         addr_max;
  476.     /// Lowest address that can be used
  477.     int*         addr_min;
  478.     /// Maximal number of global scripts
  479.     int          max_global_scr;
  480. } scc_target_t;
  481.  
  482. scc_target_t* scc_get_target(int version);
  483.  
  484.