]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
ast-test should also finalize functions directly
[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 int main()
16 {
17     /* AST */
18     ast_expression *exp;
19     ast_value      *gfoo    = NULL;
20     ast_value      *gbar    = NULL;
21     ast_value      *const_3 = NULL;
22     ast_value      *const_4 = NULL;
23     ast_value      *vmain   = NULL;
24     ast_function   *fmain   = NULL;
25     ast_block      *ba      = NULL;
26     ast_value      *li      = NULL;
27
28     /* IR */
29     ir_builder     *ir = NULL;
30
31     /* common stuff */
32
33     size_t i;
34     lex_ctx ctx;
35     ctx.file = NULL;
36     ctx.line = 1;
37
38     /* globals */
39     gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
40         assert(gfoo);
41     assert(globals_add(gfoo) >= 0);
42
43     gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
44         assert(gbar);
45     assert(globals_add(gbar) >= 0);
46
47     const_3 = ast_value_new(ctx, "3", TYPE_FLOAT);
48         assert(const_3);
49     assert(globals_add(const_3) >= 0);
50     const_3->isconst = true;
51     const_3->constval.vfloat = 3.0;
52
53     const_4 = ast_value_new(ctx, "4", TYPE_FLOAT);
54         assert(const_4);
55     assert(globals_add(const_4) >= 0);
56     const_4->isconst = true;
57     const_4->constval.vfloat = 4.0;
58
59     /* defining a function */
60     vmain = ast_value_new(ctx, "main", TYPE_FUNCTION);
61         assert(vmain);
62     assert(globals_add(vmain) >= 0);
63     /* This happens in ast_function_new:
64         vmain->isconst = true;
65         vmain->constval.vfunc = fmain;
66        Creating a function node connects the global to the function.
67        You still have to delete *BOTH*, deleting one will NOT delete the other,
68        it will only unlink them.
69      */
70
71     /* creating a function body */
72     fmain = ast_function_new(ctx, "main", vmain);
73         assert(fmain);
74     assert(functions_add(fmain) >= 0);
75
76     /* { block */
77     ba = ast_block_new(ctx);
78         assert(ba);
79     assert(ast_function_blocks_add(fmain, ba));
80
81     /* local variable i */
82     li = ast_value_new(ctx, "i", TYPE_FLOAT);
83         assert(li);
84     assert(ast_block_locals_add(ba, li));
85
86     /* I realize having to provide the opcode isn't the best way to go, but,
87      * urgh... */
88     /* foo = 3; */
89     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gfoo, (ast_expression*)const_3);
90         assert(exp);
91     assert(ast_block_exprs_add(ba, exp));
92     /* bar = 4; */
93     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gbar, (ast_expression*)const_4);
94         assert(exp);
95     assert(ast_block_exprs_add(ba, exp));
96
97     /* i = foo * bar */
98     exp = (ast_expression*)ast_binary_new(ctx,  INSTR_MUL_F, (ast_expression*)gbar, (ast_expression*)gfoo);
99         assert(exp);
100     exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, li, exp);
101         assert(exp);
102     assert(ast_block_exprs_add(ba, exp));
103
104     /* } block */
105
106     /* Next up: Having the AST generate IR */
107
108     ir = ir_builder_new("ast_test");
109     assert(ir);
110
111     /* gen globals */
112     for (i = 0; i < globals_elements; ++i) {
113         if (!ast_global_codegen(globals_data[i], ir)) {
114             assert(!"failed to generate global");
115         }
116     }
117
118     /* gen functions */
119     for (i = 0; i < functions_elements; ++i) {
120         if (!ast_function_codegen(functions_data[i], ir)) {
121             assert(!"failed to generate function");
122         }
123         if (!ir_function_finalize(functions_data[i]->ir_func))
124             assert(!"finalize on function failed...");
125     }
126
127     /* dump */
128     ir_builder_dump(ir, printf);
129
130     /* ir cleanup */
131     ir_builder_delete(ir);
132
133     /* cleanup */
134     /* Functions must be deleted FIRST since their expressions
135      * reference global variables.
136      */
137     for (i = 0; i < functions_elements; ++i) {
138         ast_function_delete(functions_data[i]);
139     }
140     if (functions_data)
141         mem_d(functions_data);
142
143     /* We must delete not only globals, but also the functions'
144      * ast_values (their type and name), that's why we added them to the globals vector.
145      */
146     for (i = 0; i < globals_elements; ++i) {
147         ast_value_delete(globals_data[i]);
148     }
149     if (globals_data)
150         mem_d(globals_data);
151     return 0;
152 }