From: Dale Weiler Date: Sun, 29 Apr 2012 20:54:05 +0000 (-0400) Subject: added test_ast, test_ir, and test, as well as default, and all to the Makefile target... X-Git-Tag: 0.1-rc1~547 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=453ca4517610abfd5fdc28d32e89a9814e72fd52;p=xonotic%2Fgmqcc.git added test_ast, test_ir, and test, as well as default, and all to the Makefile target list. Also made all tests C90 conformant code. --- diff --git a/Makefile b/Makefile index d73c641..25ee1a4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC ?= clang -CFLAGS += -Wall -pedantic-errors -std=c90 +CFLAGS += -Wall -I. -pedantic-errors -std=c90 OBJ = main.o \ lex.o \ error.o \ @@ -10,12 +10,28 @@ OBJ = main.o \ asm.o \ ast.o \ ir.o +# ast and ir test +TEST_AST = test/ast-test.o +TEST_IR = test/ir-test.o +#default is compiler only +default: gmqcc %.o: %.c $(CC) -c $< -o $@ $(CFLAGS) +# test targets +test_ast: $(TEST_AST) $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) +test_ir: $(TEST_IR) $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) +test: test_ast test_ir + +# compiler target gmqcc: $(OBJ) $(CC) -o $@ $^ $(CFLAGS) + +#all target is test and all +all: test gmqcc clean: - rm -f *.o gmqcc + rm -f *.o gmqcc test_ast test_ir test/*.o diff --git a/test/ast-test.c b/test/ast-test.c index 43a8669..5667c18 100644 --- a/test/ast-test.c +++ b/test/ast-test.c @@ -15,34 +15,43 @@ VECTOR_MAKE(ast_function*, functions); void testast() { 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; + size_t i; lex_ctx ctx; ctx.file = NULL; ctx.line = 1; /* globals */ - ast_value *gfoo = ast_value_new(ctx, "foo", TYPE_FLOAT); + 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); + 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); + 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); + 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); + vmain = ast_value_new(ctx, "main", TYPE_FUNCTION); assert(vmain); assert(globals_add(vmain) >= 0); /* This happens in ast_function_new: @@ -54,17 +63,17 @@ void testast() */ /* creating a function body */ - ast_function *fmain = ast_function_new(ctx, "main", vmain); + fmain = ast_function_new(ctx, "main", vmain); assert(fmain); assert(functions_add(fmain) >= 0); /* { block */ - ast_block *ba = ast_block_new(ctx); + 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); + li = ast_value_new(ctx, "i", TYPE_FLOAT); assert(li); assert(ast_block_locals_add(ba, li)); diff --git a/test/ir-test.c b/test/ir-test.c index 5e678de..aedc290 100644 --- a/test/ir-test.c +++ b/test/ir-test.c @@ -1,12 +1,28 @@ +#include "gmqcc.h" #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); + 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); + + ir_function *fmain = NULL; + ir_value *la = NULL; + ir_block *bmain = NULL; + ir_block *blt = NULL; + ir_block *bge = NULL; + ir_block *bend = NULL; + ir_value *sum = NULL; + ir_value *prd = NULL; + ir_value *less = NULL; + ir_value *x1 = NULL; + ir_value *vig = NULL; + ir_value *x2 = NULL; + ir_instr *retphi = NULL; + ir_value *retval = NULL; if (!ir_value_set_float(v3, 3.0f) || !ir_value_set_float(vb, 4.0f) || @@ -14,37 +30,37 @@ void builder1() !ir_value_set_float(vd, 20.0f) ) abort(); - ir_function *fmain = ir_builder_create_function(b, "main"); + fmain = ir_builder_create_function(b, "main"); - ir_value *la = ir_function_create_local(fmain, "loc1", TYPE_FLOAT); + 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"); + bmain = ir_function_create_block(fmain, "top"); + blt = ir_function_create_block(fmain, "less"); + bge = ir_function_create_block(fmain, "greaterequal"); + 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", + sum = ir_block_create_add(bmain, "%sum", va, vb); + prd = ir_block_create_mul(bmain, "%mul", sum, vc); + 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); + 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); + 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); + 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); + 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); + retval = ir_phi_value(retphi); if (!ir_block_create_return(bend, retval)) abort(); /*