From 39756f4e89a86f8ba7bb3d7ff40dcc802e4d34ab Mon Sep 17 00:00:00 2001 From: Curle Date: Sun, 6 Mar 2022 02:28:42 +0000 Subject: [PATCH] Small fixes to assembly output --- src/Assembler.c | 22 +++++++++++++++++----- src/Pointers.c | 2 +- tests/sieve.er | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/sieve.er diff --git a/src/Assembler.c b/src/Assembler.c index 6216b6c..0068a95 100644 --- a/src/Assembler.c +++ b/src/Assembler.c @@ -226,16 +226,28 @@ int AssembleTree(struct ASTNode* Node, int Register, int LoopBeginLabel, int Loo return AsShiftRight(LeftVal, RightVal); case OP_POSTINC: - return AsLdGlobalVar(Node->Symbol, Node->Operation); + if (Node->Symbol->Storage == SC_LOCAL || Node->Symbol->Storage == SC_PARAM) + return AsLdLocalVar(Node->Symbol, Node->Operation); + else + return AsLdGlobalVar(Node->Symbol, Node->Operation); case OP_POSTDEC: - return AsLdGlobalVar(Node->Symbol, Node->Operation); + if (Node->Symbol->Storage == SC_LOCAL || Node->Symbol->Storage == SC_PARAM) + return AsLdLocalVar(Node->Symbol, Node->Operation); + else + return AsLdGlobalVar(Node->Symbol, Node->Operation); case OP_PREINC: - return AsLdGlobalVar(Node->Symbol, Node->Operation); + if (Node->Symbol->Storage == SC_LOCAL || Node->Symbol->Storage == SC_PARAM) + return AsLdLocalVar(Node->Symbol, Node->Operation); + else + return AsLdGlobalVar(Node->Symbol, Node->Operation); case OP_PREDEC: - return AsLdGlobalVar(Node->Symbol, Node->Operation); + if (Node->Symbol->Storage == SC_LOCAL || Node->Symbol->Storage == SC_PARAM) + return AsLdLocalVar(Node->Symbol, Node->Operation); + else + return AsLdGlobalVar(Node->Symbol, Node->Operation); case OP_BOOLNOT: return AsBooleanNOT(LeftVal); @@ -562,7 +574,7 @@ int AsShl(int Register, int Val) { */ int AsLdGlobalVar(struct SymbolTableEntry* Entry, int Operation) { int Reg = RetrieveRegister(); - + printf("\tGlobally storing a %s\n", ScopeNames[Entry->Storage]); printf("\tStoring %s's contents into %s, globally\n", Entry->Name, Registers[Reg]); int TypeSize = PrimitiveSize(Entry->Type); diff --git a/src/Pointers.c b/src/Pointers.c index 6c83280..4b62586 100644 --- a/src/Pointers.c +++ b/src/Pointers.c @@ -178,7 +178,7 @@ struct ASTNode* AccessArray() { LeftNode = ConstructASTNode(OP_ADD, Entry->Type, LeftNode, NULL, RightNode, NULL, 0); printf("\tAccessArray: Preparing LeftNode for dereference.\r\n"); - LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, NULL, 0); + LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, Entry, 0); printf("\tArray Access constructed\r\n"); return LeftNode; } diff --git a/tests/sieve.er b/tests/sieve.er new file mode 100644 index 0000000..65ec885 --- /dev/null +++ b/tests/sieve.er @@ -0,0 +1,32 @@ +import "tests/import/defs.eh" + +long num[100]; +int :: main() { + long i; + i = 0; + long j; + j = 0; + + for (i = 0; i < 100; i++) { + num[i] = i + 1; + } + + for (i = 1; (num[i] * num[i]) <= 100; i++) { + if (num[i] != 0) { + for (j = num[i] * num[i]; j <= 100; j = num[i] + j) { + num[j - 1] = 0; + } + } + } + + printf("Finding primes 2..%d\n\n",100); + + for (i = 1; i < 100; i++) { + if (num[i] != 0) { + printf("%d\t", num[i]); + } + } + printf("\n"); + + return (0); +} \ No newline at end of file