4 /* NOTE: it's a test - I'll abort() on epic-failure */
9 /* (note: 'do {} while(0)' forces the need for a semicolon after assert() */
10 #define assert(x) do { if ( !(x) ) { printf("Assertion failed: %s\n", #x); abort(); } } while(0)
12 VECTOR_MAKE(ast_value*, globals);
13 VECTOR_MAKE(ast_function*, functions);
20 ast_value *gfoo = NULL;
21 ast_value *gbar = NULL;
22 ast_value *const_3 = NULL;
23 ast_value *const_4 = NULL;
24 ast_value *vmain = NULL;
25 ast_function *fmain = NULL;
30 ir_builder *ir = NULL;
40 gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
42 assert(globals_add(gfoo) >= 0);
44 gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
46 assert(globals_add(gbar) >= 0);
48 const_3 = ast_value_new(ctx, "3", TYPE_FLOAT);
50 assert(globals_add(const_3) >= 0);
51 const_3->isconst = true;
52 const_3->constval.vfloat = 3.0;
54 const_4 = ast_value_new(ctx, "4", TYPE_FLOAT);
56 assert(globals_add(const_4) >= 0);
57 const_4->isconst = true;
58 const_4->constval.vfloat = 4.0;
60 /* defining a function */
61 vmain = ast_value_new(ctx, "main", TYPE_FUNCTION);
63 assert(globals_add(vmain) >= 0);
64 /* This happens in ast_function_new:
65 vmain->isconst = true;
66 vmain->constval.vfunc = fmain;
67 Creating a function node connects the global to the function.
68 You still have to delete *BOTH*, deleting one will NOT delete the other,
69 it will only unlink them.
72 /* creating a function body */
73 fmain = ast_function_new(ctx, "main", vmain);
75 assert(functions_add(fmain) >= 0);
78 ba = ast_block_new(ctx);
80 assert(ast_function_blocks_add(fmain, ba));
82 /* local variable i */
83 li = ast_value_new(ctx, "i", TYPE_FLOAT);
85 assert(ast_block_locals_add(ba, li));
87 /* I realize having to provide the opcode isn't the best way to go, but,
90 exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gfoo, (ast_expression*)const_3);
92 assert(ast_block_exprs_add(ba, exp));
94 exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gbar, (ast_expression*)const_4);
96 assert(ast_block_exprs_add(ba, exp));
99 exp = (ast_expression*)ast_binary_new(ctx, INSTR_MUL_F, (ast_expression*)gbar, (ast_expression*)gfoo);
101 exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, li, exp);
103 assert(ast_block_exprs_add(ba, exp));
107 /* Next up: Having the AST generate IR */
109 ir = ir_builder_new("ast_test");
113 for (i = 0; i < globals_elements; ++i) {
114 if (!ast_global_codegen(globals_data[i], ir)) {
115 assert(!"failed to generate global");
120 for (i = 0; i < functions_elements; ++i) {
121 if (!ast_function_codegen(functions_data[i], ir)) {
122 assert(!"failed to generate function");
124 if (!ir_function_finalize(functions_data[i]->ir_func))
125 assert(!"finalize on function failed...");
129 ir_builder_dump(ir, printf);
132 ir_builder_delete(ir);
135 /* Functions must be deleted FIRST since their expressions
136 * reference global variables.
138 for (i = 0; i < functions_elements; ++i) {
139 ast_function_delete(functions_data[i]);
142 mem_d(functions_data);
144 /* We must delete not only globals, but also the functions'
145 * ast_values (their type and name), that's why we added them to the globals vector.
147 for (i = 0; i < globals_elements; ++i) {
148 ast_value_delete(globals_data[i]);
156 #include "ast-macros.h"
176 MKCONSTFLOAT(f0, 0.0);
177 MKCONSTFLOAT(f1, 1.0);
178 MKCONSTFLOAT(f5, 5.0);
188 STATE(ASSIGN(STORE_F, vi, f0));
189 WHILE(BIN(LT, vi, f5));
190 STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
191 STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
196 ir = ir_builder_new("ast_test");
200 for (i = 0; i < globals_elements; ++i) {
201 if (!ast_global_codegen(globals_data[i], ir)) {
202 assert(!"failed to generate global");
207 for (i = 0; i < functions_elements; ++i) {
208 if (!ast_function_codegen(functions_data[i], ir)) {
209 assert(!"failed to generate function");
211 if (!ir_function_finalize(functions_data[i]->ir_func))
212 assert(!"finalize on function failed...");
217 ir_builder_dump(ir, printf);
220 ir_builder_delete(ir);
223 /* Functions must be deleted FIRST since their expressions
224 * reference global variables.
226 for (i = 0; i < functions_elements; ++i) {
227 ast_function_delete(functions_data[i]);
230 mem_d(functions_data);
232 /* We must delete not only globals, but also the functions'
233 * ast_values (their type and name), that's why we added them to the globals vector.
235 for (i = 0; i < globals_elements; ++i) {
236 ast_value_delete(globals_data[i]);