]> git.xonotic.org Git - xonotic/gmqcc.git/blob - asm.c
Get rid of ast_setfunc
[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 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             *strchr(name, ',')='\0';
165             
166             /*
167              * Now add the following items to the code system:
168              *  function
169              *  definition (optional)
170              *  global     (optional)
171              *  name
172              */
173             code_functions_add((prog_section_function){
174                 -atoi(find), /* needs to be negated */
175                  0, 0, 0,
176                 .name = code_chars_elements,
177                  0, 0,{0}
178             });
179             code_defs_add((prog_section_def){
180                 .type   = TYPE_FUNCTION,
181                 .offset = code_globals_elements,
182                 .name   = code_chars_elements
183             });
184             code_globals_add(code_chars_elements);
185             
186             code_chars_put(name, strlen(name));
187             code_chars_add('\0');
188
189             /*
190              * Sanatize the numerical constant used to select the
191              * internal function.  Must ensure it's all numeric, since
192              * atoi can silently drop characters from a string and still
193              * produce a valid constant that would lead to runtime problems.
194              */
195             if (util_strdigit(find))
196                 printf("found internal function %s, -%d\n", name, atoi(find));
197             else
198                 printf("invalid internal function identifier, must be all numeric\n");
199                 
200         } else {
201             /* TODO: function bodies */
202         }
203
204         mem_d(copy);
205         mem_d(name);
206         return true;
207     }
208     return false;
209 }
210
211 void asm_parse(FILE *fp) {
212     char     *data  = NULL;
213     char     *skip  = NULL;
214     long      line  = 1; /* current line */
215     size_t    size  = 0; /* size of line */
216     asm_state state = ASM_NULL;
217
218     #define asm_end(x)            \
219         do {                      \
220             mem_d(data);          \
221             mem_d(copy);          \
222             line++;               \
223             util_debug("ASM", x); \
224         } while (0); continue
225     
226     while ((data = asm_getline (&size, fp)) != NULL) {
227         char *copy = util_strsws(data); /* skip   whitespace */
228               skip = util_strrnl(copy); /* delete newline    */
229
230         /* parse type */
231         if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
232         /* parse func */
233         if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
234
235         /* statement closure */
236         if (state == ASM_FUNCTION && (
237             (strstr(skip, "DONE")   == &skip[0])||
238             (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
239         
240         /* TODO: everything */
241         (void)state;
242         asm_end("asm_parse_end\n");
243     }
244     #undef asm_end
245         asm_clear();
246 }