]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
ast_binary_new to initialize codegen function ptr, codegen proto for store and binary...
[xonotic/gmqcc.git] / ast.c
1 /*
2  * Copyright (C) 2012 
3  *     Wolfgang Bumiller
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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "ast.h"
29
30 #define ast_setfunc(me, fn, what) ( *(void**)&((me)->fn) = what )
31
32 #define ast_instantiate(T, ctx, destroyfn)                    \
33     T *self = (T*)mem_a(sizeof(T));                           \
34     ast_node_init((ast_node*)self, ctx);                      \
35     ast_setfunc(&((ast_node*)self)->node, destroy, destroyfn)
36
37 /* It must not be possible to get here. */
38 static void _ast_node_destroy(ast_node *self)
39 {
40     fprintf(stderr, "ast node missing destroy()\n");
41     abort();
42 }
43
44 /* Initialize main ast node aprts */
45 static void ast_node_init(ast_node *self, lex_ctx_t ctx)
46 {
47     self->node.context = ctx;
48     self->node.destroy = &_ast_node_destroy;
49 }
50
51 /* General expression initialization */
52 static void ast_expression_init(ast_expression *self,
53                                 ast_expression_codegen *codegen)
54 {
55     ast_setfunc(&self->expression, codegen, codegen);
56 }
57
58 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
59 {
60     ast_instantiate(ast_value, ctx, ast_value_delete);
61     ast_expression_init((ast_expression*)self,
62                         (ast_expression_codegen*)&ast_value_codegen);
63
64     self->name = name ? util_strdup(name) : NULL;
65     self->vtype = t;
66     self->next = NULL;
67     MEM_VECTOR_INIT(self, params);
68     self->isconst = false;
69     memset(&self->constval, 0, sizeof(self->constval));
70
71     self->ir_v = NULL;
72
73     return self;
74 }
75 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
76
77 void ast_value_delete(ast_value* self)
78 {
79     size_t i;
80     if (self->name)
81         mem_d((void*)self->name);
82     for (i = 0; i < self->params_count; ++i)
83         ast_delete(self->params[i]);
84     MEM_VECTOR_CLEAR(self, params);
85     if (self->next)
86         ast_delete(self->next);
87     if (self->isconst) {
88         switch (self->vtype)
89         {
90         case qc_string:
91             mem_d((void*)self->constval.vstring);
92             break;
93         /* NOTE: delete function? currently collected in
94          * the parser structure
95          */
96         default:
97             break;
98         }
99     }
100     mem_d(self);
101 }
102
103 void ast_value_set_name(ast_value *self, const char *name)
104 {
105     if (self->name)
106         mem_d((void*)self->name);
107     self->name = util_strdup(name);
108 }
109
110 ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
111                            ast_value* left, ast_value* right)
112 {
113     ast_instantiate(ast_binary, ctx, ast_binary_delete);
114     switch (op) {
115     case INSTR_STORE_F:
116     case INSTR_STORE_V:
117     case INSTR_STORE_S:
118     case INSTR_STORE_ENT:
119     case INSTR_STORE_FLD:
120     case INSTR_STORE_FNC:
121     case INSTR_STOREP_F:
122     case INSTR_STOREP_V:
123     case INSTR_STOREP_S:
124     case INSTR_STOREP_ENT:
125     case INSTR_STOREP_FLD:
126     case INSTR_STOREP_FNC:
127 #if 0
128     case INSTR_STORE_I:
129     case INSTR_STORE_IF:
130     case INSTR_STORE_FI:
131     case INSTR_STOREP_I:
132     case INSTR_STOREP_IF:
133     case INSTR_STOREP_FI:
134     case INSTR_STORE_P:
135     case INSTR_STOREP_P:
136     case INSTR_STOREP_C:
137 #endif
138         ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_bin_store_codegen);
139         break;
140     default:
141         ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
142         break;
143     }
144
145     self->op = op;
146     self->left = left;
147     self->right = right;
148
149     return self;
150 }
151
152 void ast_binary_delete(ast_binary *self)
153 {
154     mem_d(self);
155 }
156
157 ast_block* ast_block_new(lex_ctx_t ctx)
158 {
159     ast_instantiate(ast_block, ctx, ast_block_delete);
160     ast_expression_init((ast_expression*)self,
161                         (ast_expression_codegen*)&ast_block_codegen);
162
163     MEM_VECTOR_INIT(self, locals);
164     MEM_VECTOR_INIT(self, exprs);
165
166     return self;
167 }
168 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
169 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
170
171 void ast_block_delete(ast_block *self)
172 {
173     size_t i;
174     for (i = 0; i < self->locals_count; ++i)
175         ast_delete(self->locals[i]);
176     MEM_VECTOR_CLEAR(self, locals);
177     for (i = 0; i < self->exprs_count; ++i)
178         ast_delete(self->exprs[i]);
179     MEM_VECTOR_CLEAR(self, exprs);
180     mem_d(self);
181 }
182
183 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
184 {
185     ast_instantiate(ast_function, ctx, ast_function_delete);
186
187     self->vtype = vtype;
188     self->name = name ? util_strdup(name) : NULL;
189     MEM_VECTOR_INIT(self, blocks);
190
191     return self;
192 }
193
194 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
195
196 void ast_function_delete(ast_function *self)
197 {
198     size_t i;
199     if (self->name)
200         mem_d((void*)self->name);
201     if (self->vtype)
202         ast_value_delete(self->vtype);
203     for (i = 0; i < self->blocks_count; ++i)
204         ast_delete(self->blocks[i]);
205     MEM_VECTOR_CLEAR(self, blocks);
206     mem_d(self);
207 }
208
209 /*********************************************************************/
210 /* AST codegen aprt
211  */
212
213 /* Some dummies so it compiles... */
214 bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
215 {
216     return false;
217 }
218
219 bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
220 {
221     return false;
222 }
223
224 bool ast_bin_store_codegen(ast_binary *self, ast_function *func, ir_value **out)
225 {
226     return false;
227 }
228
229 bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)
230 {
231     return false;
232 }