]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - test/ast-test.c
make the 'vector' type use qcfloat instead of float
[xonotic/gmqcc.git] / test / ast-test.c
index 00e8c052e3220dba789651fde674f7473099ef97..74800dd8ce7e0d0e753793d0d959109f0f50f895 100644 (file)
 #define assert(x) do { if ( !(x) ) { printf("Assertion failed: %s\n", #x); abort(); } } while(0)
 
 VECTOR_MAKE(ast_value*, globals);
+VECTOR_MAKE(ast_value*, fields);
 VECTOR_MAKE(ast_function*, functions);
 
+uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
+uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
+
+uint32_t    opts_O        = 1;
+const char *opts_output   = "progs.dat";
+int         opts_standard = COMPILER_GMQCC;
+bool        opts_debug    = false;
+bool        opts_memchk   = false;
+
+#include "ast-macros.h"
+
 int main()
 {
-    /* AST */
-    ast_expression *exp;
-    ast_value      *gfoo    = NULL;
-    ast_value      *gbar    = NULL;
-    ast_value      *const_3 = NULL;
-    ast_value      *const_4 = NULL;
-    ast_value      *vmain   = NULL;
-    ast_function   *fmain   = NULL;
-    ast_block      *ba      = NULL;
-    ast_value      *li      = NULL;
-
-    /* IR */
-    ir_builder     *ir = NULL;
-
-    /* common stuff */
-
     size_t i;
-    lex_ctx ctx;
-    ctx.file = NULL;
-    ctx.line = 1;
-
-    /* globals */
-    gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT);
-        assert(gfoo);
-    assert(globals_add(gfoo) >= 0);
-
-    gbar = ast_value_new(ctx, "bar", TYPE_FLOAT);
-        assert(gbar);
-    assert(globals_add(gbar) >= 0);
-
-    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;
-
-    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 */
-    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 */
-    fmain = ast_function_new(ctx, "main", vmain);
-        assert(fmain);
-    assert(functions_add(fmain) >= 0);
-
-    /* { block */
-    ba = ast_block_new(ctx);
-        assert(ba);
-    assert(ast_function_blocks_add(fmain, ba));
-
-    /* local variable i */
-    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 */
+    ir_builder     *ir;
+
+    TESTVARS();
+
+    DEFVAR(vi);
+    DEFVAR(vx);
+    DEFVAR(f0);
+    DEFVAR(f1);
+    DEFVAR(f5);
+    DEFVAR(cv3x4x5);
+    DEFVAR(cv1x1x1);
+    DEFVAR(sHello);
+    DEFVAR(sNL);
+    DEFVAR(print);
+    DEFVAR(ftos);
+    DEFVAR(spawn);
+
+    DEFVAR(mema);
+    DEFVAR(memb);
+    DEFVAR(memv);
+    DEFVAR(pawn);
+
+    /* opts_debug = true; */
+
+BUILTIN(print, TYPE_VOID, -1);
+PARAM(TYPE_STRING, text);
+ENDBUILTIN();
+
+BUILTIN(ftos, TYPE_STRING, -2);
+PARAM(TYPE_FLOAT, value);
+ENDBUILTIN();
+
+BUILTIN(spawn, TYPE_ENTITY, -3);
+ENDBUILTIN();
+
+    TESTINIT();
+VAR(TYPE_FLOAT, f0);
+VAR(TYPE_FLOAT, f1);
+VAR(TYPE_FLOAT, f5);
+VAR(TYPE_STRING, sHello);
+VAR(TYPE_STRING, sNL);
+VAR(TYPE_VECTOR, cv3x4x5);
+VAR(TYPE_VECTOR, cv1x1x1);
+
+FIELD(TYPE_FLOAT, mema);
+FIELD(TYPE_FLOAT, memb);
+FIELD(TYPE_VECTOR, memv);
+
+MKCONSTFLOAT(f0, 0.0);
+MKCONSTFLOAT(f1, 1.0);
+MKCONSTFLOAT(f5, 5.0);
+MKCONSTSTRING(sHello, "Hello, World\n");
+MKCONSTSTRING(sNL, "\n");
+MKCONSTVECTOR(cv3x4x5, 3, 4, 5);
+MKCONSTVECTOR(cv1x1x1, 1, 1, 1);
+
+FUNCTION(foo, TYPE_VOID);
+ENDFUNCTION(foo);
+
+#define PRINTNL() do { CALL(print) CALLPARAM(sNL) ENDCALL(); } while(0)
+
+FUNCTION(main, TYPE_VOID);
+
+    VAR(TYPE_FLOAT, vi);
+    VAR(TYPE_FLOAT, vx);
+    VAR(TYPE_ENTITY, pawn);
+
+    MKLOCAL(vi);
+    MKLOCAL(vx);
+    MKLOCAL(pawn);
+
+    STATE(ASSIGN(STORE_F, vi, f0));
+    WHILE(BIN(LT, vi, f5));
+        STATE(ASSIGN(STORE_F, vx, BIN(MUL_F, vi, f5)));
+        STATE(ASSIGN(STORE_F, vi, BIN(ADD_F, vi, f1)));
+    ENDWHILE();
+
+    CALL(print)
+    CALLPARAM(sHello)
+    ENDCALL();
+
+    CALL(spawn)
+    ENDCALLWITH(newent, STATE(ASSIGN(STORE_ENT, pawn, newent)));
+
+    STATE(ASSIGN(STOREP_F, ENTFIELD(pawn, mema), f5));
+    STATE(ASSIGN(STOREP_F, ENTFIELD(pawn, memb), f1));
+    STATE(ASSIGN(STOREP_V, ENTFIELD(pawn, memv), cv3x4x5));
+    CALL(ftos)
+    CALLPARAM(ENTFIELD(pawn, mema))
+    ENDCALLWITH(output,
+        CALL(print)
+        CALLPARAM(output)
+        CALLPARAM(sNL)
+        ENDCALL();
+    );
+    CALL(ftos)
+    CALLPARAM(ENTFIELD(pawn, memb))
+    ENDCALLWITH(output,
+        CALL(print)
+        CALLPARAM(output)
+        CALLPARAM(sNL)
+        ENDCALL();
+    );
+    CALL(ftos)
+    CALLPARAM(ENTFIELD(pawn, VECMEM(memv, 2)))
+    ENDCALLWITH(output,
+        CALL(print)
+        CALLPARAM(output)
+        CALLPARAM(sNL)
+        ENDCALL();
+    );
+
+ENDFUNCTION(main);
 
     ir = ir_builder_new("ast_test");
     assert(ir);
 
+    /* gen fields */
+    for (i = 0; i < fields_elements; ++i) {
+        if (!ast_global_codegen(fields_data[i], ir)) {
+            assert(!"failed to generate field");
+        }
+    }
     /* gen globals */
     for (i = 0; i < globals_elements; ++i) {
         if (!ast_global_codegen(globals_data[i], ir)) {
@@ -120,11 +163,18 @@ int main()
         if (!ast_function_codegen(functions_data[i], ir)) {
             assert(!"failed to generate function");
         }
+        if (!ir_function_finalize(functions_data[i]->ir_func))
+            assert(!"finalize on function failed...");
     }
 
+
     /* dump */
     ir_builder_dump(ir, printf);
 
+    /* Now create a file */
+    if (!ir_builder_generate(ir, "test_ast.dat"))
+        printf("*** failed to generate code\n");
+
     /* ir cleanup */
     ir_builder_delete(ir);