]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - test/ast-test.c
test/ast-test.c must define opts_ variables since we don't compile main.c into it
[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 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
16 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
17
18 uint32_t    opts_O        = 1;
19 const char *opts_output   = "progs.dat";
20 int         opts_standard = COMPILER_GMQCC;
21 bool        opts_debug    = false;
22 bool        opts_memchk   = false;
23
24 #include "ast-macros.h"
25
26 int main()
27 {
28     size_t i;
29
30     ir_builder     *ir;
31
32     TESTVARS();
33
34     DEFVAR(vi);
35     DEFVAR(vx);
36     DEFVAR(f0);
37     DEFVAR(f1);
38     DEFVAR(f5);
39     DEFVAR(sHello);
40     DEFVAR(print);
41
42     /* opts_debug = true; */
43
44 BUILTIN(print, TYPE_VOID, -1);
45 PARAM(TYPE_STRING, text);
46 ENDBUILTIN();
47
48     TESTINIT();
49 VAR(TYPE_FLOAT, f0);
50 VAR(TYPE_FLOAT, f1);
51 VAR(TYPE_FLOAT, f5);
52 VAR(TYPE_STRING, sHello);
53 MKCONSTFLOAT(f0, 0.0);
54 MKCONSTFLOAT(f1, 1.0);
55 MKCONSTFLOAT(f5, 5.0);
56 MKCONSTSTRING(sHello, "Hello, World\n");
57
58 FUNCTION(foo, TYPE_VOID);
59 ENDFUNCTION(foo);
60
61 FUNCTION(main, TYPE_VOID);
62
63     VAR(TYPE_FLOAT, vi);
64     VAR(TYPE_FLOAT, vx);
65
66     MKLOCAL(vi);
67     MKLOCAL(vx);
68
69     STATE(ASSIGN(STORE_F, vi, f0));
70     WHILE(BIN(LT, vi, f5));
71         STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
72         STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
73     ENDWHILE();
74
75     CALL(print)
76     CALLPARAM(sHello)
77     ENDCALL();
78
79 ENDFUNCTION(main);
80
81     ir = ir_builder_new("ast_test");
82     assert(ir);
83
84     /* gen globals */
85     for (i = 0; i < globals_elements; ++i) {
86         if (!ast_global_codegen(globals_data[i], ir)) {
87             assert(!"failed to generate global");
88         }
89     }
90
91     /* gen functions */
92     for (i = 0; i < functions_elements; ++i) {
93         if (!ast_function_codegen(functions_data[i], ir)) {
94             assert(!"failed to generate function");
95         }
96         if (!ir_function_finalize(functions_data[i]->ir_func))
97             assert(!"finalize on function failed...");
98     }
99
100
101     /* dump */
102     ir_builder_dump(ir, printf);
103
104     /* Now create a file */
105     if (!ir_builder_generate(ir, "test_ast.dat"))
106         printf("*** failed to generate code\n");
107
108     /* ir cleanup */
109     ir_builder_delete(ir);
110
111     /* cleanup */
112     /* Functions must be deleted FIRST since their expressions
113      * reference global variables.
114      */
115     for (i = 0; i < functions_elements; ++i) {
116         ast_function_delete(functions_data[i]);
117     }
118     if (functions_data)
119         mem_d(functions_data);
120
121     /* We must delete not only globals, but also the functions'
122      * ast_values (their type and name), that's why we added them to the globals vector.
123      */
124     for (i = 0; i < globals_elements; ++i) {
125         ast_value_delete(globals_data[i]);
126     }
127     if (globals_data)
128         mem_d(globals_data);
129     return 0;
130 }