From b085022577d7573d6b9746356175c8ebd7cb0c10 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Sat, 23 Feb 2019 18:33:57 -0500 Subject: [PATCH] Added initial draft of knight-posix support and first knight-posix test --- CHANGELOG.org | 1 + cc.c | 2 +- cc_core.c | 342 ++++++++++++++++++------- makefile | 10 +- test/common_knight/ELF-knight.hex2 | 25 ++ test/common_knight/knight_defs.M1 | 253 ++++++++++++++++++ test/common_knight/libc-core.M1 | 29 +++ test/test.answers | 5 +- test/test00/hello-knight-posix.sh | 41 +++ test/test00/{hello.sh => hello-x86.sh} | 4 +- test/test100/proof.answer | 2 +- 11 files changed, 608 insertions(+), 106 deletions(-) create mode 100644 test/common_knight/ELF-knight.hex2 create mode 100644 test/common_knight/knight_defs.M1 create mode 100644 test/common_knight/libc-core.M1 create mode 100755 test/test00/hello-knight-posix.sh rename test/test00/{hello.sh => hello-x86.sh} (94%) diff --git a/CHANGELOG.org b/CHANGELOG.org index e55eb38..fb473c8 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -26,6 +26,7 @@ Added support for octal and binary numbers Added support for \0 Added support for GET_MACHINE_FLAGS in tests Added --architecture flag +Added first knight-posix test ** Changed Converted M2-Planet to use GNU style error message diff --git a/cc.c b/cc.c index 7c28b7d..24c365b 100644 --- a/cc.c +++ b/cc.c @@ -110,7 +110,7 @@ int main(int argc, char** argv) } /* Temp solution to aborting when an architecture isn't supported yet but is expected to be fully supported */ - if(!in_set(Architecture, "\x02")) + if(!in_set(Architecture, "\x01\x02")) { file_print("ALL IS FIRE\n\n", stderr); exit(EXIT_FAILURE); diff --git a/cc_core.c b/cc_core.c index 0d77f8d..08fcd69 100644 --- a/cc_core.c +++ b/cc_core.c @@ -60,10 +60,7 @@ void emit_out(char* s) struct token_list* uniqueID(char* s, struct token_list* l, char* num) { - l = emit(s, l); - l = emit("_", l); - l = emit(num, l); - l = emit("\n", l); + l = emit("\n", emit(num, emit("_", emit(s, l)))); return l; } @@ -115,21 +112,33 @@ void function_call(char* s, int bool) { require_match("ERROR in process_expression_list\nNo ( was found\n", "("); int passed = 0; - emit_out("PUSH_edi\t# Prevent overwriting in recursion\n"); - emit_out("PUSH_ebp\t# Protect the old base pointer\n"); - emit_out("COPY_esp_to_edi\t# Copy new base pointer\n"); + + if(1 == Architecture) + { + emit_out("PUSHR R13 R15\t# Prevent overwriting in recursion\n"); + emit_out("PUSHR R14 R15\t# Protect the old base pointer\n"); + emit_out("COPY R13 R15\t# Copy new base pointer\n"); + } + else if(2 == Architecture) + { + emit_out("PUSH_edi\t# Prevent overwriting in recursion\n"); + emit_out("PUSH_ebp\t# Protect the old base pointer\n"); + emit_out("COPY_esp_to_edi\t# Copy new base pointer\n"); + } if(global_token->s[0] != ')') { expression(); - emit_out("PUSH_eax\t#_process_expression1\n"); + if(1 == Architecture) emit_out("PUSHR R0 R15\t#_process_expression1\n"); + else if(2 == Architecture) emit_out("PUSH_eax\t#_process_expression1\n"); passed = 1; while(global_token->s[0] == ',') { global_token = global_token->next; expression(); - emit_out("PUSH_eax\t#_process_expression2\n"); + if(1 == Architecture) emit_out("PUSHR R0 R15\t#_process_expression2\n"); + else if(2 == Architecture) emit_out("PUSH_eax\t#_process_expression2\n"); passed = passed + 1; } } @@ -138,31 +147,62 @@ void function_call(char* s, int bool) if(TRUE == bool) { - emit_out("LOAD_BASE_ADDRESS_eax %"); - emit_out(s); - emit_out("\nLOAD_INTEGER\n"); - emit_out("COPY_edi_to_ebp\n"); - emit_out("CALL_eax\n"); + if(1 == Architecture) + { + emit_out("LOAD R0 R15 @"); + emit_out(s); + emit_out("MOVE R14 R13\n"); + emit_out("CALL R0 R15\n"); + } + else if(2 == Architecture) + { + emit_out("LOAD_BASE_ADDRESS_eax %"); + emit_out(s); + emit_out("\nLOAD_INTEGER\n"); + emit_out("COPY_edi_to_ebp\n"); + emit_out("CALL_eax\n"); + } } else { - emit_out("COPY_edi_to_ebp\n"); - emit_out("CALL_IMMEDIATE %FUNCTION_"); - emit_out(s); - emit_out("\n"); + if(1 == Architecture) + { + emit_out("MOVE R14 R13\n"); + emit_out("CALLI R15 @FUNCTION_"); + emit_out(s); + emit_out("\n"); + } + else if(2 == Architecture) + { + emit_out("COPY_edi_to_ebp\n"); + emit_out("CALL_IMMEDIATE %FUNCTION_"); + emit_out(s); + emit_out("\n"); + } } for(; passed > 0; passed = passed - 1) { - emit_out("POP_ebx\t# _process_expression_locals\n"); + if(1 == Architecture) emit_out("POPR R1 R15\t# _process_expression_locals\n"); + else if(2 == Architecture) emit_out("POP_ebx\t# _process_expression_locals\n"); + } + + if(1 == Architecture) + { + emit_out("POPR R14 R15\t# Restore old base pointer\n"); + emit_out("POPR R13 R15\t# Prevent overwrite\n"); + } + else if(2 == Architecture) + { + emit_out("POP_ebp\t# Restore old base pointer\n"); + emit_out("POP_edi\t# Prevent overwrite\n"); } - emit_out("POP_ebp\t# Restore old base pointer\n"); - emit_out("POP_edi\t# Prevent overwrite\n"); } void constant_load(struct token_list* a) { - emit_out("LOAD_IMMEDIATE_eax %"); + if(1 == Architecture) emit_out("LOADI R0 @"); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %"); emit_out(a->arguments->s); emit_out("\n"); } @@ -175,13 +215,17 @@ void variable_load(struct token_list* a) return; } current_target = a->type; - emit_out("LOAD_BASE_ADDRESS_eax %"); + + if(1 == Architecture) emit_out("ADDI R0 R14 @"); + else if(2 == Architecture) emit_out("LOAD_BASE_ADDRESS_eax %"); + emit_out(numerate_number(a->depth)); emit_out("\n"); if(TRUE == Address_of) return; if(match("=", global_token->s)) return; - emit_out("LOAD_INTEGER\n"); + if(1 == Architecture) emit_out("LOAD R0 R0 0\n"); + else if(2 == Architecture) emit_out("LOAD_INTEGER\n"); } void function_load(struct token_list* a) @@ -192,7 +236,8 @@ void function_load(struct token_list* a) return; } - emit_out("LOAD_IMMEDIATE_eax &FUNCTION_"); + if(1 == Architecture) emit_out("LOADUI R0 $FUNCTION_"); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax &FUNCTION_"); emit_out(a->s); emit_out("\n"); } @@ -200,10 +245,15 @@ void function_load(struct token_list* a) void global_load(struct token_list* a) { current_target = a->type; - emit_out("LOAD_IMMEDIATE_eax &GLOBAL_"); + if(1 == Architecture) emit_out("LOADUI R0 $GLOBAL_"); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax &GLOBAL_"); emit_out(a->s); emit_out("\n"); - if(!match("=", global_token->s)) emit_out("LOAD_INTEGER\n"); + if(!match("=", global_token->s)) + { + if(1 == Architecture) emit_out("LOAD R0 R0 0\n"); + else if(2 == Architecture) emit_out("LOAD_INTEGER\n"); + } } /* @@ -229,7 +279,8 @@ void primary_expr_string() { char* number_string = numerate_number(current_count); current_count = current_count + 1; - emit_out("LOAD_IMMEDIATE_eax &STRING_"); + if(1 == Architecture) emit_out("LOADUI R0 $STRING_"); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax &STRING_"); uniqueID_out(function->s, number_string); /* The target */ @@ -243,7 +294,8 @@ void primary_expr_string() void primary_expr_char() { - emit_out("LOAD_IMMEDIATE_eax %"); + if(1 == Architecture) emit_out("LOADI R0 "); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %"); emit_out(numerate_number(escape_lookup(global_token->s + 1))); emit_out("\n"); global_token = global_token->next; @@ -251,7 +303,8 @@ void primary_expr_char() void primary_expr_number() { - emit_out("LOAD_IMMEDIATE_eax %"); + if(1 == Architecture) emit_out("LOADI R0 "); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %"); emit_out(global_token->s); emit_out("\n"); global_token = global_token->next; @@ -329,10 +382,15 @@ void common_recursion(FUNCTION f) { last_type = current_target; global_token = global_token->next; - emit_out("PUSH_eax\t#_common_recursion\n"); + + if(1 == Architecture) emit_out("PUSHR R0 R15\t#_common_recursion\n"); + else if(2 == Architecture) emit_out("PUSH_eax\t#_common_recursion\n"); + f(); current_target = promote_type(current_target, last_type); - emit_out("POP_ebx\t# _common_recursion\n"); + + if(1 == Architecture) emit_out("POPR R1 R15\t# _common_recursion\n"); + else if(2 == Architecture) emit_out("POP_ebx\t# _common_recursion\n"); } void general_recursion( FUNCTION f, char* s, char* name, FUNCTION iterate) @@ -382,14 +440,24 @@ void postfix_expr_arrow() if(0 != i->offset) { emit_out("# -> offset calculation\n"); - emit_out("LOAD_IMMEDIATE_ebx %"); - emit_out(numerate_number(i->offset)); - emit_out("\nADD_ebx_to_eax\n"); + if(1 == Architecture) + { + emit_out("ADDUI R0 R0 "); + emit_out(numerate_number(i->offset)); + emit_out("\n"); + } + else if(2 == Architecture) + { + emit_out("LOAD_IMMEDIATE_ebx %"); + emit_out(numerate_number(i->offset)); + emit_out("\nADD_ebx_to_eax\n"); + } } if((!match("=", global_token->s) && (4 >= i->size))) { - emit_out("LOAD_INTEGER\n"); + if(1 == Architecture) emit_out("LOAD R0 R0 0\n"); + else if(2 == Architecture) emit_out("LOAD_INTEGER\n"); } } @@ -398,21 +466,28 @@ void postfix_expr_array() struct type* array = current_target; common_recursion(expression); current_target = array; - char* assign = "LOAD_INTEGER\n"; + char* assign; + if(1 == Architecture) assign = "LOAD R0 R0 0\n"; + else if(2 == Architecture) assign = "LOAD_INTEGER\n"; /* Add support for Ints */ - if(match("char*", current_target->name)) + if(match("char*", current_target->name)) { - assign = "LOAD_BYTE\n"; + if(1 == Architecture) assign = "LOAD8 R0 R0 0\n"; + else if(2 == Architecture) assign = "LOAD_BYTE\n"; } else { - emit_out("SAL_eax_Immediate8 !"); + if(1 == Architecture) emit_out("SALI R0 "); + else if(2 == Architecture) emit_out("SAL_eax_Immediate8 !"); + emit_out(numerate_number(ceil_log2(current_target->indirect->size))); emit_out("\n"); } - emit_out("ADD_ebx_to_eax\n"); + if(1 == Architecture) emit_out("ADD R0 R0 R1\n"); + else if(2 == Architecture) emit_out("ADD_ebx_to_eax\n"); + require_match("ERROR in postfix_expr\nMissing ]\n", "]"); if(match("=", global_token->s)) @@ -438,7 +513,8 @@ void unary_expr_sizeof() struct type* a = type_name(); require_match("ERROR in unary_expr\nMissing )\n", ")"); - emit_out("LOAD_IMMEDIATE_eax %"); + if(1 == Architecture) emit_out("LOADUI R0 "); + else if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %"); emit_out(numerate_number(a->size)); emit_out("\n"); } @@ -477,13 +553,26 @@ void postfix_expr() */ void additive_expr_stub() { - general_recursion(postfix_expr, "ADD_ebx_to_eax\n", "+", additive_expr_stub); - general_recursion(postfix_expr, "SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n", "-", additive_expr_stub); - general_recursion(postfix_expr, "MULTIPLY_eax_by_ebx_into_eax\n", "*", additive_expr_stub); - general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nDIVIDE_eax_by_ebx_into_eax\n", "/", additive_expr_stub); - general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nMODULUS_eax_from_ebx_into_ebx\nMOVE_edx_to_eax\n", "%", additive_expr_stub); - general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAL_eax_cl\n", "<<", additive_expr_stub); - general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAR_eax_cl\n", ">>", additive_expr_stub); + if(1 == Architecture) + { + general_recursion(postfix_expr, "ADD R0 R0 R1\n", "+", additive_expr_stub); + general_recursion(postfix_expr, "SUB R0 R1 R0\n", "-", additive_expr_stub); + general_recursion(postfix_expr, "MUL R0 R0 R1\n", "*", additive_expr_stub); + general_recursion(postfix_expr, "DIV R0 R0 R1\n", "/", additive_expr_stub); + general_recursion(postfix_expr, "MOD R0 R0 R1\n", "%", additive_expr_stub); + general_recursion(postfix_expr, "SAL R0 R1 R0\n", "<<", additive_expr_stub); + general_recursion(postfix_expr, "SAR R0 R1 R0\n", ">>", additive_expr_stub); + } + else if(2 == Architecture) + { + general_recursion(postfix_expr, "ADD_ebx_to_eax\n", "+", additive_expr_stub); + general_recursion(postfix_expr, "SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n", "-", additive_expr_stub); + general_recursion(postfix_expr, "MULTIPLY_eax_by_ebx_into_eax\n", "*", additive_expr_stub); + general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nDIVIDE_eax_by_ebx_into_eax\n", "/", additive_expr_stub); + general_recursion(postfix_expr, "XCHG_eax_ebx\nLOAD_IMMEDIATE_edx %0\nMODULUS_eax_from_ebx_into_ebx\nMOVE_edx_to_eax\n", "%", additive_expr_stub); + general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAL_eax_cl\n", "<<", additive_expr_stub); + general_recursion(postfix_expr, "COPY_eax_to_ecx\nCOPY_ebx_to_eax\nSAR_eax_cl\n", ">>", additive_expr_stub); + } } @@ -505,12 +594,24 @@ void additive_expr() void relational_expr_stub() { - general_recursion(additive_expr, "CMP\nSETL\nMOVEZBL\n", "<", relational_expr_stub); - general_recursion(additive_expr, "CMP\nSETLE\nMOVEZBL\n", "<=", relational_expr_stub); - general_recursion(additive_expr, "CMP\nSETGE\nMOVEZBL\n", ">=", relational_expr_stub); - general_recursion(additive_expr, "CMP\nSETG\nMOVEZBL\n", ">", relational_expr_stub); - general_recursion(additive_expr, "CMP\nSETE\nMOVEZBL\n", "==", relational_expr_stub); - general_recursion(additive_expr, "CMP\nSETNE\nMOVEZBL\n", "!=", relational_expr_stub); + if(1 == Architecture) + { + general_recursion(additive_expr, "CMPSKIP.L R0 R1\nLOADUI R0 1\n", "<", relational_expr_stub); + general_recursion(additive_expr, "CMPSKIP.LE R0 R1\nLOADUI R0 1\n", "<=", relational_expr_stub); + general_recursion(additive_expr, "CMPSKIP.GE R0 R1\nLOADUI R0 1\n", ">=", relational_expr_stub); + general_recursion(additive_expr, "CMPSKIP.G R0 R1\nLOADUI R0 1\n", ">", relational_expr_stub); + general_recursion(additive_expr, "CMPSKIP.E R0 R1\nLOADUI R0 1\n", "==", relational_expr_stub); + general_recursion(additive_expr, "CMPSKIP.NE R0 R1\nLOADUI R0 1\n", "!=", relational_expr_stub); + } + else if(2 == Architecture) + { + general_recursion(additive_expr, "CMP\nSETL\nMOVEZBL\n", "<", relational_expr_stub); + general_recursion(additive_expr, "CMP\nSETLE\nMOVEZBL\n", "<=", relational_expr_stub); + general_recursion(additive_expr, "CMP\nSETGE\nMOVEZBL\n", ">=", relational_expr_stub); + general_recursion(additive_expr, "CMP\nSETG\nMOVEZBL\n", ">", relational_expr_stub); + general_recursion(additive_expr, "CMP\nSETE\nMOVEZBL\n", "==", relational_expr_stub); + general_recursion(additive_expr, "CMP\nSETNE\nMOVEZBL\n", "!=", relational_expr_stub); + } } void relational_expr() @@ -530,11 +631,22 @@ void relational_expr() */ void bitwise_expr_stub() { - general_recursion(relational_expr, "AND_eax_ebx\n", "&", bitwise_expr_stub); - general_recursion(relational_expr, "AND_eax_ebx\n", "&&", bitwise_expr_stub); - general_recursion(relational_expr, "OR_eax_ebx\n", "|", bitwise_expr_stub); - general_recursion(relational_expr, "OR_eax_ebx\n", "||", bitwise_expr_stub); - general_recursion(relational_expr, "XOR_ebx_eax_into_eax\n", "^", bitwise_expr_stub); + if(1 == Architecture) + { + general_recursion(relational_expr, "AND R0 R0 R1\n", "&", bitwise_expr_stub); + general_recursion(relational_expr, "AND R0 R0 R1\n", "&&", bitwise_expr_stub); + general_recursion(relational_expr, "OR R0 R0 R1\n", "|", bitwise_expr_stub); + general_recursion(relational_expr, "OR R0 R0 R1\n", "||", bitwise_expr_stub); + general_recursion(relational_expr, "XOR R0 R0 R1\n", "^", bitwise_expr_stub); + } + else if(2 == Architecture) + { + general_recursion(relational_expr, "AND_eax_ebx\n", "&", bitwise_expr_stub); + general_recursion(relational_expr, "AND_eax_ebx\n", "&&", bitwise_expr_stub); + general_recursion(relational_expr, "OR_eax_ebx\n", "|", bitwise_expr_stub); + general_recursion(relational_expr, "OR_eax_ebx\n", "||", bitwise_expr_stub); + general_recursion(relational_expr, "XOR_ebx_eax_into_eax\n", "^", bitwise_expr_stub); + } } @@ -565,20 +677,28 @@ void primary_expr() if(match("sizeof", global_token->s)) unary_expr_sizeof(); else if('-' == global_token->s[0]) { - emit_out("LOAD_IMMEDIATE_eax %0\n"); + if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %0\n"); + common_recursion(primary_expr); - emit_out("SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n"); + + if(1 == Architecture) emit_out("NEG R0 R0\n"); + else if(2 == Architecture) emit_out("SUBTRACT_eax_from_ebx_into_ebx\nMOVE_ebx_to_eax\n"); } else if('!' == global_token->s[0]) { - emit_out("LOAD_IMMEDIATE_eax %1\n"); + if(2 == Architecture) emit_out("LOAD_IMMEDIATE_eax %1\n"); + common_recursion(postfix_expr); - emit_out("XOR_ebx_eax_into_eax\n"); + + if(1 == Architecture) emit_out("XORI R0 R0 1\n"); + else if(2 == Architecture) emit_out("XOR_ebx_eax_into_eax\n"); } else if('~' == global_token->s[0]) { common_recursion(postfix_expr); - emit_out("NOT_eax\n"); + + if(1 == Architecture) emit_out("NOT R0 R0\n"); + else if(2 == Architecture) emit_out("NOT_eax\n"); } else if(global_token->s[0] == '(') { @@ -598,14 +718,16 @@ void expression() bitwise_expr(); if(match("=", global_token->s)) { - char* store; + char* store = ""; if(!match("]", global_token->prev->s) || !match("char*", current_target->name)) { - store = "STORE_INTEGER\n"; + if(1 == Architecture) store = "STORE R0 R1 0\n"; + else if(2 == Architecture) store = "STORE_INTEGER\n"; } else { - store = "STORE_CHAR\n"; + if(1 == Architecture) store = "STORE8 R0 R1 0\n"; + else if(2 == Architecture) store = "STORE_CHAR\n"; } common_recursion(expression); @@ -622,19 +744,23 @@ void collect_local() struct token_list* a = sym_declare(global_token->s, type_size, function->locals); if(match("main", function->s) && (NULL == function->locals)) { - a->depth = -20; + if(1 == Architecture) a->depth = 20; + else if(2 == Architecture) a->depth = -20; } else if((NULL == function->arguments) && (NULL == function->locals)) { - a->depth = -8; + if(1 == Architecture) a->depth = 8; + else if(2 == Architecture) a->depth = -8; } else if(NULL == function->locals) { - a->depth = function->arguments->depth - 8; + if(1 == Architecture) a->depth = function->arguments->depth + 8; + else if(2 == Architecture) a->depth = function->arguments->depth - 8; } else { - a->depth = function->locals->depth - 4; + if(1 == Architecture) a->depth = function->locals->depth + 4; + else if(2 == Architecture) a->depth = function->locals->depth - 4; } function->locals = a; @@ -653,7 +779,8 @@ void collect_local() require_match("ERROR in collect_local\nMissing ;\n", ";"); - emit_out("PUSH_eax\t#"); + if(1 == Architecture) emit_out("PUSHR R0 R15\t#"); + else if(2 == Architecture) emit_out("PUSH_eax\t#"); emit_out(a->s); emit_out("\n"); } @@ -673,13 +800,17 @@ void process_if() require_match("ERROR in process_if\nMISSING (\n", "("); expression(); - emit_out("TEST\nJUMP_EQ %ELSE_"); + if(1 == Architecture) emit_out("JUMP.Z R0 @ELSE_"); + else if(2 == Architecture) emit_out("TEST\nJUMP_EQ %ELSE_"); + uniqueID_out(function->s, number_string); require_match("ERROR in process_if\nMISSING )\n", ")"); statement(); - emit_out("JUMP %_END_IF_"); + if(1 == Architecture) emit_out("JUMP @_END_IF_"); + else if(2 == Architecture) emit_out("JUMP %_END_IF_"); + uniqueID_out(function->s, number_string); emit_out(":ELSE_"); uniqueID_out(function->s, number_string); @@ -725,9 +856,11 @@ void process_for() require_match("ERROR in process_for\nMISSING ;1\n", ";"); expression(); - emit_out("TEST\nJUMP_EQ %FOR_END_"); + if(1 == Architecture) emit_out("JUMP.Z R0 @FOR_END_"); + else if(2 == Architecture) emit_out("TEST\nJUMP_EQ %FOR_END_"); uniqueID_out(function->s, number_string); - emit_out("JUMP %FOR_THEN_"); + if(1 == Architecture) emit_out("JUMP @FOR_THEN_"); + else if(2 == Architecture) emit_out("JUMP %FOR_THEN_"); uniqueID_out(function->s, number_string); emit_out(":FOR_ITER_"); uniqueID_out(function->s, number_string); @@ -735,7 +868,8 @@ void process_for() require_match("ERROR in process_for\nMISSING ;2\n", ";"); expression(); - emit_out("JUMP %FOR_"); + if(1 == Architecture) emit_out("JUMP @FOR_"); + else if(2 == Architecture) emit_out("JUMP %FOR_"); uniqueID_out(function->s, number_string); emit_out(":FOR_THEN_"); uniqueID_out(function->s, number_string); @@ -743,7 +877,8 @@ void process_for() require_match("ERROR in process_for\nMISSING )\n", ")"); statement(); - emit_out("JUMP %FOR_ITER_"); + if(1 == Architecture) emit_out("JUMP @FOR_ITER_"); + else if(2 == Architecture) emit_out("JUMP %FOR_ITER_"); uniqueID_out(function->s, number_string); emit_out(":FOR_END_"); uniqueID_out(function->s, number_string); @@ -759,8 +894,8 @@ void process_asm() { global_token = global_token->next; require_match("ERROR in process_asm\nMISSING (\n", "("); - while(34 == global_token->s[0]) - {/* 34 == " */ + while('"' == global_token->s[0]) + { emit_out((global_token->s + 1)); emit_out("\n"); global_token = global_token->next; @@ -797,7 +932,8 @@ void process_do() require_match("ERROR in process_do\nMISSING )\n", ")"); require_match("ERROR in process_do\nMISSING ;\n", ";"); - emit_out("TEST\nJUMP_NE %DO_"); + if(1 == Architecture) emit_out("JUMP.NZ R0 @DO_"); + else if(2 == Architecture) emit_out("TEST\nJUMP_NE %DO_"); uniqueID_out(function->s, number_string); emit_out(":DO_END_"); uniqueID_out(function->s, number_string); @@ -832,7 +968,8 @@ void process_while() require_match("ERROR in process_while\nMISSING (\n", "("); expression(); - emit_out("TEST\nJUMP_EQ %END_WHILE_"); + if(1 == Architecture) emit_out("JUMP.Z R0 @END_WHILE_"); + else if(2 == Architecture) emit_out("TEST\nJUMP_EQ %END_WHILE_"); uniqueID_out(function->s, number_string); emit_out("# THEN_while_"); uniqueID_out(function->s, number_string); @@ -840,7 +977,8 @@ void process_while() require_match("ERROR in process_while\nMISSING )\n", ")"); statement(); - emit_out("JUMP %WHILE_"); + if(1 == Architecture) emit_out("JUMP @WHILE_"); + else if(2 == Architecture) emit_out("JUMP %WHILE_"); uniqueID_out(function->s, number_string); emit_out(":END_WHILE_"); uniqueID_out(function->s, number_string); @@ -862,9 +1000,12 @@ void return_result() struct token_list* i; for(i = function->locals; NULL != i; i = i->next) { - emit_out("POP_ebx\t# _return_result_locals\n"); + if(1 == Architecture) emit_out("POPR R1 R15\t# _return_result_locals\n"); + else if(2 == Architecture) emit_out("POP_ebx\t# _return_result_locals\n"); } - emit_out("RETURN\n"); + + if(1 == Architecture) emit_out("RET R15\n"); + else if(2 == Architecture) emit_out("RETURN\n"); } void process_break() @@ -879,11 +1020,15 @@ void process_break() while(i != break_frame) { if(NULL == i) break; - emit_out("POP_ebx\t# break_cleanup_locals\n"); + if(1 == Architecture) emit_out("POPR R1 R15\t# break_cleanup_locals\n"); + else if(2 == Architecture) emit_out("POP_ebx\t# break_cleanup_locals\n"); i = i->next; } global_token = global_token->next; - emit_out("JUMP %"); + + if(1 == Architecture) emit_out("JUMP @"); + else if(2 == Architecture) emit_out("JUMP %"); + emit_out(break_target_head); emit_out(break_target_func); emit_out("_"); @@ -904,12 +1049,14 @@ void recursive_statement() global_token = global_token->next; /* Clean up any locals added */ - if(!match("RETURN\n", out->s)) + + if(((2 == Architecture) && !match("RETURN\n", out->s)) || ((1 == Architecture) && !match("RET R15\n", out->s))) { struct token_list* i; for(i = function->locals; frame != i; i = i->next) { - emit_out( "POP_ebx\t# _recursive_statement_locals\n"); + if(1 == Architecture) emit_out("POPR R1 R15\t@ _recursive_statement_locals\n"); + else if(2 == Architecture) emit_out( "POP_ebx\t# _recursive_statement_locals\n"); } } function->locals = frame; @@ -974,7 +1121,8 @@ void statement() else if(match("goto", global_token->s)) { global_token = global_token->next; - emit_out("JUMP %"); + if(1 == Architecture) emit_out("JUMP @"); + else if(2 == Architecture) emit_out("JUMP %"); emit_out(global_token->s); emit_out("\n"); global_token = global_token->next; @@ -1020,11 +1168,13 @@ void collect_arguments() struct token_list* a = sym_declare(global_token->s, type_size, function->arguments); if(NULL == function->arguments) { - a->depth = -4; + if(1 == Architecture) a->depth = 4; + else if(2 == Architecture) a->depth = -4; } else { - a->depth = function->arguments->depth - 4; + if(1 == Architecture) a->depth = function->arguments->depth + 4; + else if(2 == Architecture) a->depth = function->arguments->depth - 4; } global_token = global_token->next; @@ -1059,10 +1209,8 @@ void declare_function() statement(); /* Prevent duplicate RETURNS */ - if(!match("RETURN\n", out->s)) - { - emit_out("RETURN\n"); - } + if((1 == Architecture) && !match("RET R15\n", out->s)) emit_out("RET R15\n"); + else if((2 == Architecture) && !match("RETURN\n", out->s)) emit_out("RETURN\n"); } } diff --git a/makefile b/makefile index ca1dcc1..a2cf1d8 100644 --- a/makefile +++ b/makefile @@ -62,7 +62,8 @@ results: mkdir -p test/results # tests -test: test00-binary \ +test: test00-x86-binary \ + test00-knight-posix-binary \ test01-binary \ test02-binary \ test03-binary \ @@ -91,8 +92,11 @@ test: test00-binary \ test100-binary | results sha256sum -c test/test.answers -test00-binary: M2-Planet | results - test/test00/hello.sh +test00-x86-binary: M2-Planet | results + test/test00/hello-x86.sh + +test00-knight-posix-binary: M2-Planet | results + test/test00/hello-knight-posix.sh test01-binary: M2-Planet | results test/test01/hello.sh diff --git a/test/common_knight/ELF-knight.hex2 b/test/common_knight/ELF-knight.hex2 new file mode 100644 index 0000000..9b58d57 --- /dev/null +++ b/test/common_knight/ELF-knight.hex2 @@ -0,0 +1,25 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen +### This file is part of M2-Planet. +### +### M2-Planet is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### M2-Planet is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with M2-Planet. If not, see . + +### stage0's hex2 format for x86 +### !