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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T *self = (T*)mem_a(sizeof(T)); \
32 ast_node_init((ast_node*)self, ctx); \
33 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn;
35 /* It must not be possible to get here. */
36 static void _ast_node_destroy(ast_node *self)
38 fprintf(stderr, "ast node missing destroy()\n");
42 /* Initialize main ast node aprts */
43 static void ast_node_init(ast_node *self, lex_ctx ctx)
45 self->node.context = ctx;
46 self->node.destroy = &_ast_node_destroy;
47 self->node.keep = false;
50 /* General expression initialization */
51 static void ast_expression_init(ast_expression *self,
52 ast_expression_codegen *codegen)
54 self->expression.codegen = codegen;
57 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
59 ast_instantiate(ast_value, ctx, ast_value_delete);
60 ast_expression_init((ast_expression*)self,
61 (ast_expression_codegen*)&ast_value_codegen);
62 self->expression.node.keep = true; /* keep */
64 self->name = name ? util_strdup(name) : NULL;
67 MEM_VECTOR_INIT(self, params);
68 self->isconst = false;
69 memset(&self->constval, 0, sizeof(self->constval));
75 MEM_VEC_FUNCTIONS(ast_value, ast_value*, params)
77 void ast_value_delete(ast_value* self)
81 mem_d((void*)self->name);
82 for (i = 0; i < self->params_count; ++i)
83 ast_unref(self->params[i]);
84 MEM_VECTOR_CLEAR(self, params);
85 if (self->next) /* delete, not unref, types are always copied */
86 ast_delete(self->next);
91 mem_d((void*)self->constval.vstring);
94 /* unlink us from the function node */
95 self->constval.vfunc->vtype = NULL;
97 /* NOTE: delete function? currently collected in
98 * the parser structure
107 bool ast_value_set_name(ast_value *self, const char *name)
110 mem_d((void*)self->name);
111 self->name = util_strdup(name);
115 ast_binary* ast_binary_new(lex_ctx ctx, int op,
116 ast_expression* left, ast_expression* right)
118 ast_instantiate(ast_binary, ctx, ast_binary_delete);
119 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
128 void ast_binary_delete(ast_binary *self)
130 ast_unref(self->left);
131 ast_unref(self->right);
135 ast_store* ast_store_new(lex_ctx ctx, int op,
136 ast_value *dest, ast_expression *source)
138 ast_instantiate(ast_store, ctx, ast_store_delete);
139 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
143 self->source = source;
148 void ast_store_delete(ast_store *self)
150 ast_unref(self->dest);
151 ast_unref(self->source);
155 ast_block* ast_block_new(lex_ctx ctx)
157 ast_instantiate(ast_block, ctx, ast_block_delete);
158 ast_expression_init((ast_expression*)self,
159 (ast_expression_codegen*)&ast_block_codegen);
161 MEM_VECTOR_INIT(self, locals);
162 MEM_VECTOR_INIT(self, exprs);
166 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
167 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
169 void ast_block_delete(ast_block *self)
172 for (i = 0; i < self->exprs_count; ++i)
173 ast_unref(self->exprs[i]);
174 MEM_VECTOR_CLEAR(self, exprs);
175 for (i = 0; i < self->locals_count; ++i)
176 ast_delete(self->locals[i]);
177 MEM_VECTOR_CLEAR(self, locals);
181 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
187 if (vtype->vtype != TYPE_FUNCTION)
190 ast_instantiate(ast_function, ctx, ast_function_delete);
193 self->name = name ? util_strdup(name) : NULL;
194 MEM_VECTOR_INIT(self, blocks);
196 vtype->isconst = true;
197 vtype->constval.vfunc = self;
202 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
204 void ast_function_delete(ast_function *self)
208 mem_d((void*)self->name);
210 /* ast_value_delete(self->vtype); */
211 self->vtype->isconst = false;
212 self->vtype->constval.vfunc = NULL;
213 /* We use unref - if it was stored in a global table it is supposed
214 * to be deleted from *there*
216 ast_unref(self->vtype);
218 for (i = 0; i < self->blocks_count; ++i)
219 ast_delete(self->blocks[i]);
220 MEM_VECTOR_CLEAR(self, blocks);
224 /*********************************************************************/
228 /* Some dummies so it compiles... */
229 bool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
234 bool ast_block_codegen(ast_block *self, ast_function *func, ir_value **out)
239 bool ast_store_codegen(ast_store *self, ast_function *func, ir_value **out)
244 bool ast_binary_codegen(ast_binary *self, ast_function *func, ir_value **out)