]> git.xonotic.org Git - xonotic/gmqcc.git/blob - asm.c
More assembly work
[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  * Some assembler keywords not part of the opcodes above: these are
26  * for creating functions, or constants.
27  */
28 const char *const asm_keys[] = {
29     "FLOAT"    , /* define float  */
30     "VECTOR"   , /* define vector */
31     "ENTITY"   , /* define ent    */
32     "FIELD"    , /* define field  */
33     "STRING"   , /* define string */
34     "FUNCTION"
35 };
36
37 static char *const asm_getline(size_t *byte, FILE *fp) {
38     char   *line = NULL;
39     ssize_t read = util_getline(&line, byte, fp);
40     *byte = read;
41     if (read == -1) {
42         mem_d (line);
43         return NULL;
44     }
45     return line;
46 }
47
48 void asm_init(const char *file, FILE **fp) {
49     *fp = fopen(file, "r");
50     code_init();
51 }
52
53 void asm_close(FILE *fp) {
54     fclose(fp);
55     code_write();
56 }
57
58 /*
59  * Following parse states:
60  *     ASM_FUNCTION -- in a function accepting input statements
61  *     ....
62  */
63 typedef enum {
64     ASM_NULL,
65     ASM_FUNCTION
66 } asm_state;
67
68 typedef struct {
69     char *name;   /* name of constant    */
70     int   offset; /* location in globals */
71 } globals;
72 VECTOR_MAKE(globals, assembly_constants);
73
74 void asm_clear() {
75     size_t i = 0;
76     for (; i < assembly_constants_elements; i++)
77         mem_d(assembly_constants_data[i].name);
78     mem_d(assembly_constants_data);
79 }
80
81 /*
82  * Parses a type, could be global or not depending on the
83  * assembly state: global scope with assignments are constants.
84  * globals with no assignments are globals.  Function body types
85  * are locals.
86  */
87 static inline bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
88     if (strstr(skip, "FLOAT:")  == &skip[0]) { return true; }
89     if (strstr(skip, "VECTOR:") == &skip[0]) { return true; }
90     if (strstr(skip, "ENTITY:") == &skip[0]) { return true; }
91     if (strstr(skip, "FIELD:")  == &skip[0]) { return true; }
92     if (strstr(skip, "STRING:") == &skip[0]) { return true; }
93     return false;
94 }
95
96 /*
97  * Parses a function: trivial case, handles occurances of duplicated
98  * names among other things.  Ensures valid name as well, and even
99  * internal engine function selection.
100  */
101 static inline bool asm_parse_func(const char *skip, size_t line, asm_state *state) {
102     if (*state == ASM_FUNCTION && (strstr(skip, "FUNCTION:") == &skip[0]))
103         return false;
104
105     if (strstr(skip, "FUNCTION:") == &skip[0]) {
106         char  *copy = util_strsws(skip+10);
107         char  *name = util_strchp(copy, strchr(copy, '\0'));
108
109         /* TODO: failure system, missing name */
110         if (!name) {
111             printf("expected name on function\n");
112             mem_d(copy);
113             mem_d(name);
114             return false;
115         }
116         /* TODO: failure system, invalid name */
117         if (!isalpha(*name) || isupper(*name)) {
118             printf("invalid identifer for function name\n");
119             mem_d(copy);
120             mem_d(name);
121             return false;
122         }
123
124         printf("NAME: %s\n", name);
125
126         mem_d(copy);
127         mem_d(name);
128         return true;
129     }
130     return false;
131 }
132
133 void asm_parse(FILE *fp) {
134     char     *data  = NULL;
135     char     *skip  = NULL;
136     long      line  = 1; /* current line */
137     size_t    size  = 0; /* size of line */
138     asm_state state = ASM_NULL;
139
140     #define asm_end(x)            \
141         do {                      \
142             mem_d(data);          \
143             mem_d(copy);          \
144             line++;               \
145             util_debug("ASM", x); \
146         } while (0); continue
147     
148     while ((data = asm_getline (&size, fp)) != NULL) {
149         char *copy = util_strsws(data); /* skip   whitespace */
150               skip = util_strrnl(copy); /* delete newline    */
151
152         /* parse type */
153         if(asm_parse_type(skip, line, &state)){ asm_end("asm_parse_type\n"); }
154         /* parse func */
155         if(asm_parse_func(skip, line, &state)){ asm_end("asm_parse_func\n"); }
156
157         /* statement closure */
158         if (state == ASM_FUNCTION && (
159             (strstr(skip, "DONE")   == &skip[0])||
160             (strstr(skip, "RETURN") == &skip[0]))) state = ASM_NULL;
161         
162         /* TODO: everything */
163         (void)state;
164         asm_end("asm_parse_end\n");
165     }
166     #undef asm_end
167         asm_clear();
168 }