]> git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
TYPE_VARIANT in codegen, writing code from ast-test
[xonotic/gmqcc.git] / test / ast-test.c
1 #include "gmqcc.h"
2 #include "ast.h"
3
4 /* NOTE: it's a test - I'll abort() on epic-failure */
5
6 #ifdef assert
7 #   undef assert
8 #endif
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)
11
12 VECTOR_MAKE(ast_value*, globals);
13 VECTOR_MAKE(ast_function*, functions);
14
15 #if 0
16 int main()
17 {
18     /* AST */
19     ast_expression *exp;
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;
26     ast_block      *ba      = NULL;
27     ast_value      *li      = NULL;
28
29     /* IR */
30     ir_builder     *ir = NULL;
31
32     /* common stuff */
33
34     size_t i;
35     lex_ctx ctx;
36     ctx.file = NULL;
37     ctx.line = 1;
38
39     /* globals */
40     gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
41         assert(gfoo);
42     assert(globals_add(gfoo) >= 0);
43
44     gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
45         assert(gbar);
46     assert(globals_add(gbar) >= 0);
47
48     const_3 = ast_value_new(ctx, "3", TYPE_FLOAT);
49         assert(const_3);
50     assert(globals_add(const_3) >= 0);
51     const_3->isconst = true;
52     const_3->constval.vfloat = 3.0;
53
54     const_4 = ast_value_new(ctx, "4", TYPE_FLOAT);
55         assert(const_4);
56     assert(globals_add(const_4) >= 0);
57     const_4->isconst = true;
58     const_4->constval.vfloat = 4.0;
59
60     /* defining a function */
61     vmain = ast_value_new(ctx, "main", TYPE_FUNCTION);
62         assert(vmain);
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.
70      */
71
72     /* creating a function body */
73     fmain = ast_function_new(ctx, "main", vmain);
74         assert(fmain);
75     assert(functions_add(fmain) >= 0);
76
77     /* { block */
78     ba = ast_block_new(ctx);
79         assert(ba);
80     assert(ast_function_blocks_add(fmain, ba));
81
82     /* local variable i */
83     li = ast_value_new(ctx, "i", TYPE_FLOAT);
84         assert(li);
85     assert(ast_block_locals_add(ba, li));
86
87     /* I realize having to provide the opcode isn't the best way to go, but,
88      * urgh... */
89     /* foo = 3; */
90     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gfoo, (ast_expression*)const_3);
91         assert(exp);
92     assert(ast_block_exprs_add(ba, exp));
93     /* bar = 4; */
94     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gbar, (ast_expression*)const_4);
95         assert(exp);
96     assert(ast_block_exprs_add(ba, exp));
97
98     /* i = foo * bar */
99     exp = (ast_expression*)ast_binary_new(ctx,  INSTR_MUL_F, (ast_expression*)gbar, (ast_expression*)gfoo);
100         assert(exp);
101     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, li, exp);
102         assert(exp);
103     assert(ast_block_exprs_add(ba, exp));
104
105     /* } block */
106
107     /* Next up: Having the AST generate IR */
108
109     ir = ir_builder_new("ast_test");
110     assert(ir);
111
112     /* gen globals */
113     for (i = 0; i < globals_elements; ++i) {
114         if (!ast_global_codegen(globals_data[i], ir)) {
115             assert(!"failed to generate global");
116         }
117     }
118
119     /* gen functions */
120     for (i = 0; i < functions_elements; ++i) {
121         if (!ast_function_codegen(functions_data[i], ir)) {
122             assert(!"failed to generate function");
123         }
124         if (!ir_function_finalize(functions_data[i]->ir_func))
125             assert(!"finalize on function failed...");
126     }
127
128     /* dump */
129     ir_builder_dump(ir, printf);
130
131     /* ir cleanup */
132     ir_builder_delete(ir);
133
134     /* cleanup */
135     /* Functions must be deleted FIRST since their expressions
136      * reference global variables.
137      */
138     for (i = 0; i < functions_elements; ++i) {
139         ast_function_delete(functions_data[i]);
140     }
141     if (functions_data)
142         mem_d(functions_data);
143
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.
146      */
147     for (i = 0; i < globals_elements; ++i) {
148         ast_value_delete(globals_data[i]);
149     }
150     if (globals_data)
151         mem_d(globals_data);
152     return 0;
153 }
154 #endif
155
156 #include "ast-macros.h"
157
158 int main()
159 {
160     size_t i;
161
162     ir_builder     *ir;
163
164     TESTVARS();
165
166     DEFVAR(vi);
167     DEFVAR(vx);
168     DEFVAR(f0);
169     DEFVAR(f1);
170     DEFVAR(f5);
171     
172     TESTINIT();
173 VAR(TYPE_FLOAT, f0);
174 VAR(TYPE_FLOAT, f1);
175 VAR(TYPE_FLOAT, f5);
176 MKCONSTFLOAT(f0, 0.0);
177 MKCONSTFLOAT(f1, 1.0);
178 MKCONSTFLOAT(f5, 5.0);
179
180 FUNCTION(main);
181
182 VAR(TYPE_FLOAT, vi);
183 VAR(TYPE_FLOAT, vx);
184
185 MKLOCAL(vi);
186 MKLOCAL(vx);
187
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)));
192 ENDWHILE();
193
194 ENDFUNCTION(main);
195
196     ir = ir_builder_new("ast_test");
197     assert(ir);
198
199     /* gen globals */
200     for (i = 0; i < globals_elements; ++i) {
201         if (!ast_global_codegen(globals_data[i], ir)) {
202             assert(!"failed to generate global");
203         }
204     }
205
206     /* gen functions */
207     for (i = 0; i < functions_elements; ++i) {
208         if (!ast_function_codegen(functions_data[i], ir)) {
209             assert(!"failed to generate function");
210         }
211         if (!ir_function_finalize(functions_data[i]->ir_func))
212             assert(!"finalize on function failed...");
213     }
214
215
216     /* dump */
217     ir_builder_dump(ir, printf);
218
219     /* Now create a file */
220     if (!ir_builder_generate(ir, "test_ast.dat"))
221         printf("*** failed to generate code\n");
222
223     /* ir cleanup */
224     ir_builder_delete(ir);
225
226     /* cleanup */
227     /* Functions must be deleted FIRST since their expressions
228      * reference global variables.
229      */
230     for (i = 0; i < functions_elements; ++i) {
231         ast_function_delete(functions_data[i]);
232     }
233     if (functions_data)
234         mem_d(functions_data);
235
236     /* We must delete not only globals, but also the functions'
237      * ast_values (their type and name), that's why we added them to the globals vector.
238      */
239     for (i = 0; i < globals_elements; ++i) {
240         ast_value_delete(globals_data[i]);
241     }
242     if (globals_data)
243         mem_d(globals_data);
244     return 0;
245 }