]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - asm.c
All code is now C89/C90 compat
[xonotic/gmqcc.git] / asm.c
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 #include "gmqcc.h"
24 /*
25  * Following parse states:
26  *     ASM_FUNCTION -- in a function accepting input statements
27  *     ....
28  */
29 typedef enum {
30     ASM_NULL,
31     ASM_FUNCTION
32 } asm_state;
33
34 typedef struct {
35     char *name;   /* name of constant    */
36     int   offset; /* location in globals */
37 } globals;
38 VECTOR_MAKE(globals, assembly_constants);
39
40 /*
41  * Assembly text processing: this handles the internal collection
42  * of text to allow parsing and assemblation.
43  */
44 static char *const asm_getline(size_t *byte, FILE *fp) {
45     char   *line = NULL;
46     size_t  read = util_getline(&line, byte, fp);
47     *byte = read;
48     if (read == -1) {
49         mem_d (line);
50         return NULL;
51     }
52     return line;
53 }
54
55 /*
56  * Entire external interface for main.c - to perform actual assemblation
57  * of assembly files.
58  */
59 void asm_init(const char *file, FILE **fp) {
60     *fp = fopen(file, "r");
61     code_init();
62 }
63 void asm_close(FILE *fp) {
64     fclose(fp);
65     code_write();
66 }
67 void asm_clear() {
68     size_t i = 0;
69     for (; i < assembly_constants_elements; i++)
70         mem_d(assembly_constants_data[i].name);
71     mem_d(assembly_constants_data);
72 }
73
74 /*
75  * Parses a type, could be global or not depending on the
76  * assembly state: global scope with assignments are constants.
77  * globals with no assignments are globals.  Function body types
78  * are locals.
79  */
80 static GMQCC_INLINE bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
81     if (!(strstr(skip, "FLOAT:")  == &skip[0]) &&
82          (strstr(skip, "VECTOR:") == &skip[0]) &&
83          (strstr(skip, "ENTITY:") == &skip[0]) &&
84          (strstr(skip, "FIELD:")  == &skip[0]) &&
85          (strstr(skip, "STRING:") == &skip[0])) return false;
86
87     /* TODO: determine if constant, global, or local */
88     switch (*skip) {
89         /* VECTOR */ case 'V': {
90             float val1;
91             float val2;
92             float val3;
93
94             const char *find = skip + 7;
95             while (*find == ' ' || *find == '\t') find++;
96
97             /*
98              * Parse all three elements of the vector.  This will only
99              * pass the first try if we hit a constant, otherwise it's
100              * a global.
101              */
102             #define PARSE_ELEMENT(X,Y,Z)                    \
103                 if (isdigit(*X)  || *X == '-'||*X == '+') { \
104                     bool negated = (*X == '-');             \
105                     if  (negated || *X == '+')   { X++; }   \
106                     Y = (negated)?-atof(X):atof(X);         \
107                     X = strchr(X, ',');                     \
108                     Z                                       \
109                 }
110
111             PARSE_ELEMENT(find, val1, { if(find) { find +=3; }});
112             PARSE_ELEMENT(find, val2, { if(find) { find +=2; }});
113             PARSE_ELEMENT(find, val3, { if(find) { find +=1; }});
114             #undef PARSE_ELEMENT
115
116             printf("X:[0] = %f\n", val1);
117             printf("Y:[1] = %f\n", val2);
118             printf("Z:[2] = %f\n", val3);
119
120             break;
121         }
122         /* ENTITY */ case 'E': {
123             const char *find = skip + 7;
124             while (*find == ' ' || *find == '\t') find++;
125             printf("found ENTITY %s\n", find);
126             break;
127         }
128         /* STRING */ case 'S': {
129             const char *find = skip + 7;
130             while (*find == ' ' || *find == '\t') find++;
131             printf("found STRING %s\n", find);
132             break;
133         }
134     }
135
136     return false;
137 }
138
139 /*
140  * Parses a function: trivial case, handles occurances of duplicated
141  * names among other things.  Ensures valid name as well, and even
142  * internal engine function selection.
143  */
144 static GMQCC_INLINE bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
145     if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
146         return false;
147
148     if (strstr(skip, "FUNCTION:") == &skip[0]) {
149         char  *copy = util_strsws(skip+10);
150         char  *name = util_strchp(copy, strchr(copy, '\0'));
151
152         /* TODO: failure system, missing name */
153         if (!name) {
154             printf("expected name on function\n");
155             mem_d(copy);
156             mem_d(name);
157             return false;
158         }
159         /* TODO: failure system, invalid name */
160         if (!isalpha(*name) || util_strupper(name)) {
161             printf("invalid identifer for function name\n");
162             mem_d(copy);
163             mem_d(name);
164             return false;
165         }
166
167         /*
168          * Function could be internal function, look for $
169          * to determine this.
170          */
171         if (strchr(name, ',')) {
172             prog_section_function function;
173             prog_section_def      def;
174             
175             char *find = strchr(name, ',') + 1;
176
177             /* skip whitespace */
178             while (*find == ' ' || *find == '\t')
179                 find++;
180
181             if (*find != '$') {
182                 printf("expected $ for internal function selection, got %s instead\n", find);
183                 mem_d(copy);
184                 mem_d(name);
185                 return false;
186             }
187             find ++;
188             if (!isdigit(*find)) {
189                 printf("invalid internal identifier, expected valid number\n");
190                 mem_d(copy);
191                 mem_d(name);
192                 return false;
193             }
194             *strchr(name, ',')='\0';
195
196             /*
197              * Now add the following items to the code system:
198              *  function
199              *  definition (optional)
200              *  global     (optional)
201              *  name
202              */
203             function.entry      = -atoi(find);
204             function.firstlocal = 0;
205             function.profile    = 0;
206             function.name       = code_chars_elements;
207             function.file       = 0;
208             function.nargs      = 0;
209             def.type            = TYPE_FUNCTION;
210             def.offset          = code_globals_elements;
211             def.name            = code_chars_elements;
212             code_functions_add(function);
213             code_defs_add     (def);
214             code_globals_add  (code_chars_elements);
215             code_chars_put    (name, strlen(name));
216             code_chars_add    ('\0');
217
218             /*
219              * Sanatize the numerical constant used to select the
220              * internal function.  Must ensure it's all numeric, since
221              * atoi can silently drop characters from a string and still
222              * produce a valid constant that would lead to runtime problems.
223              */
224             if (util_strdigit(find))
225                 printf("found internal function %s, -%d\n", name, atoi(find));
226             else
227                 printf("invalid internal function identifier, must be all numeric\n");
228
229         } else {
230             printf("Found function %s\n", name);
231         }
232
233         mem_d(copy);
234         mem_d(name);
235         return true;
236     }
237     return false;
238 }
239
240 void asm_parse(FILE *fp) {
241     char     *data  = NULL;
242     char     *skip  = NULL;
243     long      line  = 1; /* current line */
244     size_t    size  = 0; /* size of line */
245     asm_state state = ASM_NULL;
246
247     #define asm_end(x)            \
248         do {                      \
249             mem_d(data);          \
250             mem_d(copy);          \
251             line++;               \
252             util_debug("ASM", x); \
253         } while (0); continue
254
255     while ((data = asm_getline (&size, fp)) != NULL) {
256         char *copy = util_strsws(data); /* skip   whitespace */
257               skip = util_strrnl(copy); /* delete newline    */
258
259         /* parse type */
260         if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
261         /* parse func */
262         if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
263
264         /* statement closure */
265         if (state == ASM_FUNCTION && (
266             (strstr(skip, "DONE")   == &skip[0])||
267             (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
268
269         /* TODO: everything */
270         (void)state;
271         asm_end("asm_parse_end\n");
272     }
273     #undef asm_end
274         asm_clear();
275 }