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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
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)
36 typedef unsigned char uint8_t; /* same as below */
37 #elif SCHAR_MIN == -128
38 typedef unsigned char uint8_t; /* same as above */
40 #if SHRT_MAX == 0x7FFF
41 typedef short int16_t;
42 typedef unsigned short uint16_t;
43 #elif INT_MAX == 0x7FFF
45 typedef unsigned int uint16_t;
47 #if INT_MAX == 0x7FFFFFFF
49 typedef unsigned int uint32_t;
50 #elif LONG_MAX == 0x7FFFFFFF
52 typedef unsigned long uint32_t;
54 #ifdef _LP64 /* long pointer == 64 */
55 typedef unsigned long uintptr_t;
56 typedef long intptr_t;
58 typedef unsigned int uintptr_t;
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 intptr_size_is_correct [sizeof(uintptr_t)== sizeof(int*)?1:-1];
72 //===================================================================
73 //============================ lex.c ================================
74 //===================================================================
76 FILE *file; /* file handler */
77 char *name; /* name of file */
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? */
89 * It's important that this table never exceed 32 keywords, the ascii
90 * table starts at 33 (and we don't want conflicts)
97 #define TOKEN_CONTINUE 5
98 #define TOKEN_RETURN 6
100 #define TOKEN_FOR 8 // extension
101 #define TOKEN_TYPEDEF 9 // extension
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
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
117 #define LEX_COMMENT 1128
118 #define LEX_CHRLIT 1129
119 #define LEX_STRLIT 1130
120 #define LEX_IDENT 1131
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 *);
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 *, ...);
138 //===================================================================
139 //========================== parse.c ================================
140 //===================================================================
141 int parse_gen(struct lex_file *);
143 //===================================================================
144 //========================== typedef.c ==============================
145 //===================================================================
146 typedef struct typedef_node_t {
151 void typedef_clear();
152 typedef_node *typedef_find(const char *);
153 int typedef_add (struct lex_file *file, const char *, const char *);
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 void util_meminfo ();
163 char *util_strdup (const char *);
164 char *util_strrq (char *);
165 char *util_strrnl (char *);
166 void util_debug (const char *, const char *, ...);
167 int util_getline (char **, size_t *, FILE *);
168 void util_endianswap(void *, int, int);
171 # define mem_a(x) malloc(x)
172 # define mem_d(x) free (x)
174 # define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
175 # define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
178 #define VECTOR_MAKE(T,N) \
179 T* N##_data = NULL; \
180 long N##_elements = 0; \
181 long N##_allocated = 0; \
182 int N##_add(T element) { \
183 if (N##_elements == N##_allocated) { \
184 if (N##_allocated == 0) { \
185 N##_allocated = 12; \
187 N##_allocated *= 2; \
189 void *temp = mem_a(N##_allocated * sizeof(T)); \
194 memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
196 N##_data = (T*)temp; \
198 N##_data[N##_elements] = element; \
199 return N##_elements++; \
202 //===================================================================
203 //=========================== code.c ================================
204 //===================================================================
206 #define TYPE_STRING 1
208 #define TYPE_VECTOR 3
209 #define TYPE_ENTITY 4
211 #define TYPE_FUNCTION 6
212 #define TYPE_POINTER 7
215 * Each paramater incerements by 3 since vector types hold
216 * 3 components (x,y,z).
220 #define OFS_PARM0 (OFS_RETURN+3)
221 #define OFS_PARM1 (OFS_PARM0 +3)
222 #define OFS_PARM2 (OFS_PARM1 +3)
223 #define OFS_PARM3 (OFS_PARM2 +3)
224 #define OFS_PARM4 (OFS_PARM3 +3)
225 #define OFS_PARM5 (OFS_PARM4 +3)
226 #define OFS_PARM6 (OFS_PARM5 +3)
227 #define OFS_PARM7 (OFS_PARM6 +3)
234 int16_t s1; /* signed */
235 uint16_t u1; /* unsigned */
239 int16_t s2; /* signed */
240 uint16_t u2; /* unsigned */
244 int16_t s3; /* signed */
245 uint16_t u3; /* unsigned */
249 * This is the same as the structure in darkplaces
254 * But this one is more sane to work with, and the
255 * type sizes are guranteed.
257 } prog_section_statement;
268 * 7 = ev_pointer -- engine only
269 * 8 = ev_bad -- engine only
275 typedef prog_section_both prog_section_def;
276 typedef prog_section_both prog_section_field;
279 int32_t entry; /* in statement table for instructions */
280 uint32_t firstlocal; /* First local in local table */
281 uint32_t locals; /* Total ints of params + locals */
282 uint32_t profile; /* Always zero (engine uses this) */
283 uint32_t name; /* name of function in string table */
284 uint32_t file; /* file of the source file */
285 uint32_t nargs; /* number of arguments */
286 uint8_t argsize[8]; /* size of arguments (keep 8 always?) */
287 } prog_section_function;
291 * These are the external instructions supported by the interperter
292 * this is what things compile to (from the C code).
364 * The symbols below are created by the following
367 * VECTOR_MAKE(prog_section_statement, code_statements);
368 * VECTOR_MAKE(prog_section_def, code_defs );
369 * VECTOR_MAKE(prog_section_field, code_fields );
370 * VECTOR_MAKE(prog_section_function, code_functions );
371 * VECTOR_MAKE(int, code_globals );
372 * VECTOR_MAKE(char, code_chars );
374 int code_statements_add(prog_section_statement);
375 int code_defs_add (prog_section_def);
376 int code_fields_add (prog_section_field);
377 int code_functions_add (prog_section_function);
378 int code_globals_add (int);
379 int code_chars_add (char);
380 int code_strings_add (const char *); /* function wrapping code_chars_add */
381 extern long code_statements_elements;
382 extern long code_chars_elements;
383 extern long code_globals_elements;
384 extern long code_functions_elements;
385 extern long code_fields_elements;
386 extern long code_defs_elements;
389 * code_write -- writes out the compiled file
390 * code_init -- prepares the code file
395 //===================================================================
396 //========================= assembler.c =============================
397 //===================================================================
398 void asm_init (const char *, FILE **);
399 void asm_close(FILE *);
400 void asm_parse(FILE *);