]> git.xonotic.org Git - xonotic/gmqcc.git/blob - gmqcc.h
Updated README
[xonotic/gmqcc.git] / gmqcc.h
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef GMQCC_HDR
24 #define GMQCC_HDR
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 /*
31  * stdint.h and inttypes.h -less subset
32  * for systems that don't have it, which we must
33  * assume is all systems. (int8_t not required)
34  */
35 #if   CHAR_MIN  == -128
36         typedef unsigned char  uint8_t; /* same as below */
37 #elif SCHAR_MIN == -128
38         typedef unsigned char  uint8_t; /* same as above */
39 #endif
40 #if   SHRT_MAX  == 0x7FFF
41         typedef short          int16_t;
42         typedef unsigned short uint16_t;
43 #elif INT_MAX   == 0x7FFF
44         typedef int            int16_t;
45         typedef unsigned int   uint16_t;
46 #endif
47 #if   INT_MAX   == 0x7FFFFFFF 
48         typedef int            int32_t;
49         typedef unsigned int   uint32_t;
50 #elif LONG_MAX  == 0x7FFFFFFF
51         typedef long           int32_t;
52         typedef unsigned long  uint32_t;
53 #endif
54 #ifdef _LP64 /* long pointer == 64 */
55         typedef unsigned long  uintptr_t;
56         typedef long           intptr_t;
57 #else
58         typedef unsigned int   uintptr_t;
59         typedef int            intptr_t;
60 #endif 
61 /* Ensure type sizes are correct: */
62 typedef char uint8_size_is_correct  [sizeof(uint8_t)  == 1?1:-1];
63 typedef char uint16_size_if_correct [sizeof(uint16_t) == 2?1:-1];
64 typedef char uint32_size_is_correct [sizeof(uint32_t) == 4?1:-1];
65 typedef char int8_size_is_correct   [sizeof(int8_t)   == 1?1:-1];
66 typedef char int16_size_if_correct  [sizeof(int16_t)  == 2?1:-1];
67 typedef char int32_size_is_correct  [sizeof(int32_t)  == 4?1:-1];
68 /* intptr_t / uintptr_t correct size check */
69 typedef char uintptr_size_is_correct[sizeof(intptr_t) == sizeof(int*)?1:-1];
70 typedef char uintptr_size_is_correct[sizeof(uintptr_t)== sizeof(int*)?1:-1];
71
72 //===================================================================
73 //============================ lex.c ================================
74 //===================================================================
75 struct lex_file {
76         FILE *file;        /* file handler */
77         char *name;        /* name of file */
78         char  peek  [5];  
79         char  lastok[8192];
80         
81         int   last;    /* last token                   */
82         int   current; /* current token                */
83         int   length;  /* bytes left to parse          */
84         int   size;    /* never changes (size of file) */
85         int   line;    /* what line are we on?         */
86 };
87
88 /*
89  * It's important that this table never exceed 32 keywords, the ascii
90  * table starts at 33 (and we don't want conflicts)
91  */
92 #define TOKEN_DO       0
93 #define TOKEN_ELSE     1
94 #define TOKEN_IF       2
95 #define TOKEN_WHILE    3
96 #define TOKEN_BREAK    4
97 #define TOKEN_CONTINUE 5
98 #define TOKEN_RETURN   6
99 #define TOKEN_GOTO     7
100 #define TOKEN_FOR      8   // extension
101 #define TOKEN_TYPEDEF  9   // extension
102
103 // ensure the token types are out of the
104 // bounds of anyothers that may conflict.
105 #define TOKEN_FLOAT    110
106 #define TOKEN_VECTOR   111
107 #define TOKEN_STRING   112
108 #define TOKEN_ENTITY   113
109 #define TOKEN_VOID     114
110
111 /*
112  * Lexer state constants, these are numbers for where exactly in
113  * the lexing the lexer is at. Or where it decided to stop if a lexer
114  * error occurs.  These numbers must be > where the ascii-table ends
115  * and > the last type token which is TOKEN_VOID
116  */
117 #define LEX_COMMENT    1128 
118 #define LEX_CHRLIT     1129
119 #define LEX_STRLIT     1130
120 #define LEX_IDENT      1131
121
122 int              lex_token  (struct lex_file *);
123 void             lex_reset  (struct lex_file *);
124 void             lex_close  (struct lex_file *);
125 struct lex_file *lex_include(struct lex_file *, char *);
126 struct lex_file *lex_open   (FILE *);
127
128 //===================================================================
129 //========================== error.c ================================
130 //===================================================================
131 #define ERROR_LEX      (SHRT_MAX+0)
132 #define ERROR_PARSE    (SHRT_MAX+1)
133 #define ERROR_INTERNAL (SHRT_MAX+2)
134 #define ERROR_COMPILER (SHRT_MAX+3)
135 #define ERROR_PREPRO   (SHRT_MAX+4)
136 int error(struct lex_file *, int, const char *, ...);
137
138 //===================================================================
139 //========================== parse.c ================================
140 //===================================================================
141 int parse_gen(struct lex_file *);
142
143 //===================================================================
144 //========================== typedef.c ==============================
145 //===================================================================
146 typedef struct typedef_node_t {
147         char      *name;
148 } typedef_node;
149
150 void          typedef_init();
151 void          typedef_clear();
152 typedef_node *typedef_find(const char *);
153 int           typedef_add (struct lex_file *file, const char *, const char *);
154
155
156 //===================================================================
157 //=========================== util.c ================================
158 //===================================================================
159 void *util_memory_a(unsigned int, unsigned int, const char *);
160 void  util_memory_d(void       *, unsigned int, const char *);
161 char *util_strdup  (const char *);
162 char *util_strrq   (char *);
163 void  util_debug   (const char *, const char *, ...);
164
165 #ifdef NOTRACK
166 #       define mem_a(x) malloc(x)
167 #       define mem_d(x) free  (x)
168 #else
169 #       define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
170 #       define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
171 #endif
172
173 #define VECTOR_MAKE(T,N)                                                 \
174     T*     N##_data      = NULL;                                         \
175     long   N##_elements  = 0;                                            \
176     long   N##_allocated = 0;                                            \
177     int    N##_add(T element) {                                          \
178         if (N##_elements == N##_allocated) {                             \
179             if (N##_allocated == 0) {                                    \
180                 N##_allocated = 12;                                      \
181             } else {                                                     \
182                 N##_allocated *= 2;                                      \
183             }                                                            \
184             void *temp = realloc(N##_data, (N##_allocated * sizeof(T))); \
185             if  (!temp) {                                                \
186                 free(temp);                                              \
187                 return -1;                                               \
188             }                                                            \
189             N##_data = (T*)temp;                                         \
190         }                                                                \
191         N##_data[N##_elements] = element;                                \
192         return   N##_elements++;                                         \
193     }
194
195 //===================================================================
196 //=========================== code.c ================================
197 //===================================================================
198 #define TYPE_VOID     0
199 #define TYPE_STRING   1
200 #define TYPE_FLOAT    2
201 #define TYPE_VECTOR   3
202 #define TYPE_ENTITY   4
203 #define TYPE_FIELD    5
204 #define TYPE_FUNCTION 6
205 #define TYPE_POINTER  7
206
207 /*
208  * Each paramater incerements by 3 since vector types hold
209  * 3 components (x,y,z).
210  */
211 #define OFS_NULL      0
212 #define OFS_RETURN    1
213 #define OFS_PARM0     (OFS_RETURN+3)
214 #define OFS_PARM1     (OFS_PARM0 +3)
215 #define OFS_PARM2     (OFS_PARM1 +3)
216 #define OFS_PARM3     (OFS_PARM2 +3)
217 #define OFS_PARM4     (OFS_PARM3 +3)
218 #define OFS_PARM5     (OFS_PARM4 +3)
219 #define OFS_PARM6     (OFS_PARM5 +3)
220 #define OFS_PARM7     (OFS_PARM6 +3)
221
222 typedef struct {
223         uint16_t opcode;
224         
225         /* operand 1 */
226         union {
227                 int16_t  s1; /* signed   */
228                 uint16_t u1; /* unsigned */
229         };
230         /* operand 2 */
231         union {
232                 int16_t  s2; /* signed   */
233                 uint16_t u2; /* unsigned */
234         };
235         /* operand 3 */
236         union {
237                 int16_t  s3; /* signed   */
238                 uint16_t u3; /* unsigned */
239         };
240         
241         /*
242          * This is the same as the structure in darkplaces
243          * {
244          *     unsigned short op;
245          *     short          a,b,c;
246          * }
247          * But this one is more sane to work with, and the
248          * type sizes are guranteed.
249          */
250 } prog_section_statement;
251
252 typedef struct {
253         /* The types:
254          * 0 = ev_void
255          * 1 = ev_string
256          * 2 = ev_float
257          * 3 = ev_vector
258          * 4 = ev_entity
259          * 5 = ev_field
260          * 6 = ev_function
261          * 7 = ev_pointer -- engine only
262          * 8 = ev_bad     -- engine only
263          */
264         uint16_t type;
265         uint16_t offset;
266         uint32_t name;
267 } prog_section_both;
268 typedef prog_section_both prog_section_def;
269 typedef prog_section_both prog_section_field;
270
271 typedef struct {
272         int32_t   entry;      /* in statement table for instructions  */
273         uint32_t  firstlocal; /* First local in local table           */
274         uint32_t  locals;     /* Total ints of params + locals        */
275         uint32_t  profile;    /* Always zero (engine uses this)       */
276         uint32_t  name;       /* name of function in string table     */
277         uint32_t  file;       /* file of the source file              */
278         uint32_t  nargs;      /* number of arguments                  */
279         uint8_t   argsize[8]; /* size of arguments (keep 8 always?)   */
280 } prog_section_function;
281
282 /* 
283  * Instructions 
284  * These are the external instructions supported by the interperter
285  * this is what things compile to (from the C code).
286  */
287 enum {
288         INSTR_DONE,
289         INSTR_MUL_F,
290         INSTR_MUL_V,
291         INSTR_MUL_FV,
292         INSTR_MUL_VF,
293         INSTR_DIV_F,
294         INSTR_ADD_F,
295         INSTR_ADD_V,
296         INSTR_SUB_F,
297         INSTR_SUB_V,
298         INSTR_EQ_F,
299         INSTR_EQ_V,
300         INSTR_EQ_S,
301         INSTR_EQ_E,
302         INSTR_EQ_FNC,
303         INSTR_NE_F,
304         INSTR_NE_V,
305         INSTR_NE_S,
306         INSTR_NE_E,
307         INSTR_NE_FNC,
308         INSTR_LE,
309         INSTR_GE,
310         INSTR_LT,
311         INSTR_GT,
312         INSTR_LOAD_F,
313         INSTR_LOAD_V,
314         INSTR_LOAD_S,
315         INSTR_LOAD_ENT,
316         INSTR_LOAD_FLD,
317         INSTR_LOAD_FNC,
318         INSTR_ADDRESS,
319         INSTR_STORE_F,
320         INSTR_STORE_V,
321         INSTR_STORE_S,
322         INSTR_STORE_ENT,
323         INSTR_STORE_FLD,
324         INSTR_STORE_FNC,
325         INSTR_STOREP_F,
326         INSTR_STOREP_V,
327         INSTR_STOREP_S,
328         INSTR_STOREP_ENT,
329         INSTR_STOREP_FLD,
330         INSTR_STOREP_FNC,
331         INSTR_RETURN,
332         INSTR_NOT_F,
333         INSTR_NOT_V,
334         INSTR_NOT_S,
335         INSTR_NOT_ENT,
336         INSTR_NOT_FNC,
337         INSTR_IF,
338         INSTR_IFNOT,
339         INSTR_CALL0,
340         INSTR_CALL1,
341         INSTR_CALL2,
342         INSTR_CALL3,
343         INSTR_CALL4,
344         INSTR_CALL5,
345         INSTR_CALL6,
346         INSTR_CALL7,
347         INSTR_CALL8,
348         INSTR_STATE,
349         INSTR_GOTO,
350         INSTR_AND,
351         INSTR_OR,
352         INSTR_BITAND,
353         INSTR_BITOR
354 };
355
356 /*
357  * The symbols below are created by the following
358  * expanded macros:
359  * 
360  * VECTOR_MAKE(prog_section_statement, code_statements);
361  * VECTOR_MAKE(prog_section_def,       code_defs      );
362  * VECTOR_MAKE(prog_section_field,     code_fields    );
363  * VECTOR_MAKE(prog_section_function,  code_functions );
364  * VECTOR_MAKE(int,                    code_globals   );
365  * VECTOR_MAKE(char,                   code_strings   );
366  */
367 int         code_statements_add(prog_section_statement);
368 int         code_defs_add     (prog_section_def);
369 int         code_fields_add   (prog_section_field);
370 int         code_functions_add(prog_section_function);
371 int         code_globals_add  (int);
372 int         code_strings_add  (char);
373 extern long code_statements_elements;
374 extern long code_strings_elements;
375 extern long code_globals_elements;
376 extern long code_functions_elements;
377 extern long code_fields_elements;
378 extern long code_defs_elements;
379
380 /*
381  * code_write -- writes out the compiled file
382  * code_init  -- prepares the code file
383  */
384 void code_write ();
385 void code_init  ();
386
387 //===================================================================
388 //========================= assembler.c =============================
389 //===================================================================
390 void asm_init (const char *, FILE **);
391 void asm_close(FILE *);
392 void asm_parse(FILE *);
393 #endif