]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
ast and ir testers - to use: compile into gmqcc and execut the functions in main()
authorWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Apr 2012 19:42:12 +0000 (21:42 +0200)
committerWolfgang Bumiller <wolfgang.linux@bumiller.com>
Sat, 28 Apr 2012 19:42:21 +0000 (21:42 +0200)
test/ast-test.c [new file with mode: 0644]
test/ir-test.c [new file with mode: 0644]

diff --git a/test/ast-test.c b/test/ast-test.c
new file mode 100644 (file)
index 0000000..43a8669
--- /dev/null
@@ -0,0 +1,111 @@
+#include "gmqcc.h"
+#include "ast.h"
+
+/* NOTE: it's a test - I'll abort() on epic-failure */
+
+#ifdef assert
+#   undef assert
+#endif
+/* (note: 'do {} while(0)' forces the need for a semicolon after assert() */
+#define assert(x) do { if ( !(x) ) { printf("Assertion failed: %s\n", #x); abort(); } } while(0)
+
+VECTOR_MAKE(ast_value*, globals);
+VECTOR_MAKE(ast_function*, functions);
+
+void testast()
+{
+    ast_expression *exp;
+    size_t i;
+    lex_ctx ctx;
+    ctx.file = NULL;
+    ctx.line = 1;
+
+    /* globals */
+    ast_value *gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
+        assert(gfoo);
+    assert(globals_add(gfoo) >= 0);
+
+    ast_value *gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
+        assert(gbar);
+    assert(globals_add(gbar) >= 0);
+
+    ast_value *const_3 = ast_value_new(ctx, "3", TYPE_FLOAT);
+        assert(const_3);
+    assert(globals_add(const_3) >= 0);
+    const_3->isconst = true;
+    const_3->constval.vfloat = 3.0;
+
+    ast_value *const_4 = ast_value_new(ctx, "4", TYPE_FLOAT);
+        assert(const_4);
+    assert(globals_add(const_4) >= 0);
+    const_4->isconst = true;
+    const_4->constval.vfloat = 4.0;
+
+    /* defining a function */
+    ast_value *vmain = ast_value_new(ctx, "main", TYPE_FUNCTION);
+        assert(vmain);
+    assert(globals_add(vmain) >= 0);
+    /* This happens in ast_function_new:
+        vmain->isconst = true;
+        vmain->constval.vfunc = fmain;
+       Creating a function node connects the global to the function.
+       You still have to delete *BOTH*, deleting one will NOT delete the other,
+       it will only unlink them.
+     */
+
+    /* creating a function body */
+    ast_function *fmain = ast_function_new(ctx, "main", vmain);
+        assert(fmain);
+    assert(functions_add(fmain) >= 0);
+
+    /* { block */
+    ast_block *ba = ast_block_new(ctx);
+        assert(ba);
+    assert(ast_function_blocks_add(fmain, ba));
+
+    /* local variable i */
+    ast_value *li = ast_value_new(ctx, "i", TYPE_FLOAT);
+        assert(li);
+    assert(ast_block_locals_add(ba, li));
+
+    /* I realize having to provide the opcode isn't the best way to go, but,
+     * urgh... */
+    /* foo = 3; */
+    exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gfoo, (ast_expression*)const_3);
+        assert(exp);
+    assert(ast_block_exprs_add(ba, exp));
+    /* bar = 4; */
+    exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, gbar, (ast_expression*)const_4);
+        assert(exp);
+    assert(ast_block_exprs_add(ba, exp));
+
+    /* i = foo * bar */
+    exp = (ast_expression*)ast_binary_new(ctx,  INSTR_MUL_F, (ast_expression*)gbar, (ast_expression*)gfoo);
+        assert(exp);
+    exp = (ast_expression*)ast_store_new(ctx, INSTR_STORE_F, li, exp);
+        assert(exp);
+    assert(ast_block_exprs_add(ba, exp));
+
+    /* } block */
+
+    /* Next up: Having the AST generate IR */
+
+    /* cleanup */
+    /* Functions must be deleted FIRST since their expressions
+     * reference global variables.
+     */
+    for (i = 0; i < functions_elements; ++i) {
+        ast_function_delete(functions_data[i]);
+    }
+    if (functions_data)
+        mem_d(functions_data);
+
+    /* We must delete not only globals, but also the functions'
+     * ast_values (their type and name), that's why we added them to the globals vector.
+     */
+    for (i = 0; i < globals_elements; ++i) {
+        ast_value_delete(globals_data[i]);
+    }
+    if (globals_data)
+        mem_d(globals_data);
+}
diff --git a/test/ir-test.c b/test/ir-test.c
new file mode 100644 (file)
index 0000000..5e678de
--- /dev/null
@@ -0,0 +1,88 @@
+#include "ir.h"
+void builder1()
+{
+       ir_builder *b = ir_builder_new("test");
+       ir_value *va = ir_builder_create_global(b, "a", TYPE_FLOAT);
+       ir_value *v3 = ir_builder_create_global(b, "const_f_3", TYPE_FLOAT);
+       ir_value *vb = ir_builder_create_global(b, "b", TYPE_FLOAT);
+       ir_value *vc = ir_builder_create_global(b, "c", TYPE_FLOAT);
+       ir_value *vd = ir_builder_create_global(b, "d", TYPE_FLOAT);
+
+       if (!ir_value_set_float(v3, 3.0f)  ||
+           !ir_value_set_float(vb, 4.0f)  ||
+           !ir_value_set_float(vc, 10.0f) ||
+           !ir_value_set_float(vd, 20.0f) )
+           abort();
+
+       ir_function *fmain = ir_builder_create_function(b, "main");
+
+       ir_value *la = ir_function_create_local(fmain, "loc1", TYPE_FLOAT);
+       (void)la;
+
+       ir_block *bmain = ir_function_create_block(fmain, "top");
+       ir_block *blt   = ir_function_create_block(fmain, "less");
+       ir_block *bge   = ir_function_create_block(fmain, "greaterequal");
+       ir_block *bend  = ir_function_create_block(fmain, "end");
+
+       if (!ir_block_create_store_op(bmain, INSTR_STORE_F, va, v3)) abort();
+       ir_value *sum = ir_block_create_add(bmain, "%sum", va, vb);
+       ir_value *prd = ir_block_create_mul(bmain, "%mul", sum, vc);
+       ir_value *less = ir_block_create_binop(bmain, "%less",
+                                              INSTR_LT, prd, vd);
+
+       if (!ir_block_create_if(bmain, less, blt, bge)) abort();
+
+       ir_value *x1 = ir_block_create_binop(blt, "%x1", INSTR_ADD_F, sum, v3);
+       if (!ir_block_create_goto(blt, bend)) abort();
+
+       ir_value *vig = ir_block_create_binop(bge, "%ignore", INSTR_ADD_F, va, vb);
+       if (!ir_block_create_store_op(bge, INSTR_STORE_F, la, vig)) abort();
+       ir_value *x2 = ir_block_create_binop(bge, "%x2", INSTR_ADD_F, sum, v3);
+       if (!ir_block_create_goto(bge, bend)) abort();
+
+       ir_instr *retphi = ir_block_create_phi(bend, "%retval", TYPE_FLOAT);
+       if (!ir_phi_add(retphi, blt, x1) ||
+           !ir_phi_add(retphi, bge, x2) )
+           abort();
+       ir_value *retval = ir_phi_value(retphi);
+       if (!ir_block_create_return(bend, retval)) abort();
+
+       /*
+       printf("%i  should be 1\n", ir_value_life_merge(va, 31));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 33));
+       printf("%i  should be 0\n", ir_value_life_merge(va, 33));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 1));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 2));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 20));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 21));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 8));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 9));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 3));
+       printf("%i  should be 0\n", ir_value_life_merge(va, 9));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 17));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 18));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 19));
+       printf("%i  should be 0\n", ir_value_life_merge(va, 19));
+       ir_value_dump_life(va, printf);
+       printf("%i  should be 1\n", ir_value_life_merge(va, 10));
+       printf("%i  should be 1\n", ir_value_life_merge(va, 9));
+       printf("%i  should be 0\n", ir_value_life_merge(va, 10));
+       printf("%i  should be 0\n", ir_value_life_merge(va, 10));
+       ir_value_dump_life(va, printf);
+       */
+
+       ir_builder_dump(b, printf);
+       if (!ir_function_finalize(fmain)) abort();
+       ir_builder_dump(b, printf);
+
+       ir_value_dump_life(sum, printf);
+       ir_value_dump_life(prd, printf);
+       ir_value_dump_life(less, printf);
+       ir_value_dump_life(x1, printf);
+       ir_value_dump_life(x2, printf);
+       ir_value_dump_life(retval, printf);
+       ir_value_dump_life(vig, printf);
+       ir_value_dump_life(la, printf);
+
+       ir_builder_delete(b);
+}