]> git.xonotic.org Git - xonotic/gmqcc.git/blob - asm.c
type parsing for constants, globals and locals. Sanatize constants to select interna...
[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     ssize_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 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             const char *find = skip + 7;
91             while (*find == ' ' || *find == '\t') find++;
92             printf("found VECTOR %s\n", find);
93             break;
94         }
95         /* ENTITY */ case 'E': {
96             const char *find = skip + 7;
97             while (*find == ' ' || *find == '\t') find++;
98             printf("found ENTITY %s\n", find);
99             break;
100         }
101         /* STRING */ case 'S': {
102             const char *find = skip + 7;
103             while (*find == ' ' || *find == '\t') find++;
104             printf("found STRING %s\n", find);
105             break;
106         }
107     }
108     
109     return false;
110 }
111
112 /*
113  * Parses a function: trivial case, handles occurances of duplicated
114  * names among other things.  Ensures valid name as well, and even
115  * internal engine function selection.
116  */
117 static inline bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
118     if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
119         return false;
120
121     if (strstr(skip, "FUNCTION:") == &skip[0]) {
122         char  *copy = util_strsws(skip+10);
123         char  *name = util_strchp(copy, strchr(copy, '\0'));
124
125         /* TODO: failure system, missing name */
126         if (!name) {
127             printf("expected name on function\n");
128             mem_d(copy);
129             mem_d(name);
130             return false;
131         }
132         /* TODO: failure system, invalid name */
133         if (!isalpha(*name) || util_strupper(name)) {
134             printf("invalid identifer for function name\n");
135             mem_d(copy);
136             mem_d(name);
137             return false;
138         }
139
140         /*
141          * Function could be internal function, look for $
142          * to determine this.
143          */
144         if (strchr(name, ',')) {
145             char *find = strchr(name, ',') + 1;
146             
147             /* skip whitespace */
148             while (*find == ' ' || *find == '\t')
149                 find++;
150             
151             if (*find != '$') {
152                 printf("expected $ for internal function selection, got %s instead\n", find);
153                 mem_d(copy);
154                 mem_d(name);
155                 return false;
156             }
157             find ++;
158             if (!isdigit(*find)) {
159                 printf("invalid internal identifier, expected valid number\n");
160                 mem_d(copy);
161                 mem_d(name);
162                 return false;
163             }
164             /* reassign name */
165             mem_d(name);
166             name = util_strchp(name, strchr(name, ','));
167
168             /*
169              * Now add the following items to the code system:
170              *  function
171              *  definition (optional)
172              *  global     (optional)
173              *  name
174              */
175             code_functions_add((prog_section_function){
176                 -atoi(find), /* needs to be negated */
177                  0, 0, 0,
178                 .name = code_chars_elements,
179                  0, 0,{0}
180             });
181             code_defs_add((prog_section_def){
182                 .type   = TYPE_FUNCTION,
183                 .offset = code_globals_elements,
184                 .name   = code_chars_elements
185             });
186             code_globals_add(code_chars_elements);
187             
188             code_chars_put(name, strlen(name));
189             code_chars_add('\0');
190
191             /*
192              * Sanatize the numerical constant used to select the
193              * internal function.  Must ensure it's all numeric, since
194              * atoi can silently drop characters from a string and still
195              * produce a valid constant that would lead to runtime problems.
196              */
197             if (util_strdigit(find))
198                 printf("found internal function %s, -%d\n", name, atoi(find));
199             else
200                 printf("invalid internal function identifier, must be all numeric\n");
201                 
202         } else {
203             /* TODO: function bodies */
204         }
205
206         mem_d(copy);
207         mem_d(name);
208         return true;
209     }
210     return false;
211 }
212
213 void asm_parse(FILE *fp) {
214     char     *data  = NULL;
215     char     *skip  = NULL;
216     long      line  = 1; /* current line */
217     size_t    size  = 0; /* size of line */
218     asm_state state = ASM_NULL;
219
220     #define asm_end(x)            \
221         do {                      \
222             mem_d(data);          \
223             mem_d(copy);          \
224             line++;               \
225             util_debug("ASM", x); \
226         } while (0); continue
227     
228     while ((data = asm_getline (&size, fp)) != NULL) {
229         char *copy = util_strsws(data); /* skip   whitespace */
230               skip = util_strrnl(copy); /* delete newline    */
231
232         /* parse type */
233         if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
234         /* parse func */
235         if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
236
237         /* statement closure */
238         if (state == ASM_FUNCTION && (
239             (strstr(skip, "DONE")   == &skip[0])||
240             (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
241         
242         /* TODO: everything */
243         (void)state;
244         asm_end("asm_parse_end\n");
245     }
246     #undef asm_end
247         asm_clear();
248 }