From 6741dde7cec07cf341e5b92d981f1609e3b1ef6b Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 18 Jun 2023 14:51:37 -0500 Subject: [PATCH] Hooked up the program counter in the parser to set the memory offset of symbols, well labels to be more acurrate. --- misc/another_test.asm | 2 +- src/main.c | 14 +++++++++- src/parser.c | 65 ++++++++++++++++++++++++++++++++++++++----- src/scanner.c | 16 ----------- 4 files changed, 72 insertions(+), 25 deletions(-) diff --git a/misc/another_test.asm b/misc/another_test.asm index cac3c57..eb4355e 100644 --- a/misc/another_test.asm +++ b/misc/another_test.asm @@ -1,6 +1,6 @@ .db MAX_MEM 0xFFFF .db VIDEO_MEM 0xF37F -;.db MAX_LENGTH MAX_MEM - MAX_LENGTH +.db MAX_LENGTH 32;MAX_MEM - MAX_LENGTH .db MSG "Hello, World!", 0 _start: diff --git a/src/main.c b/src/main.c index a2616ae..a992676 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include "../includes/parser.h" List* LIST; +SymbolTable* Symbols; void print(void); @@ -27,12 +28,23 @@ int main(int argc, char* args[]) { LIST = GenerateTokenList(source_code); - ParseTokens(LIST); + IRState* state = ParseTokens(LIST); + Symbols = state->SymbolsTable; free(source_code); } +// void assemble() { +// for(int i = 0; i < LIST->size; i++) { +// Token* current = LIST->content[i]; + +// if (current->Class != MnemonicClass) continue; + + +// } +// } + void print(void) { char mnemonic[12]; printf("printing tokens...\n"); diff --git a/src/parser.c b/src/parser.c index c1bcb0a..72367a0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -26,7 +26,7 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options); void ExpectCharacterClass(Symbol* symbol); void ExpectLineEndOrFileEnd(ExpectOptions options); -int HeapSize = 0; +//int HeapSize = 0; int PC = 0; IRState* ParseTokens(List* tokens) { @@ -58,8 +58,12 @@ IRState* ParseTokens(List* tokens) { exit(1); } - if (!found) AddSymbolToTable(t->Lemexe, HeapSize, 1, MachineState.SymbolsTable); - else symbol->Resolved = 1; + if (!found) AddSymbolToTable(t->Lemexe, PC, 1, MachineState.SymbolsTable); + else + { + symbol->Address = PC; + symbol->Resolved = 1; + } RemoveCurrentToken(); //label @@ -101,11 +105,12 @@ void HandleAssemblerDirective() { } else if (PeekToken()->Class == IdentifierClass) { symbol = ExpectIdentifier(0, RemoveExpected); + symbol->Length = 2; ExpectLineEndOrFileEnd(RemoveExpected); } - HeapSize += symbol->Length; + //HeapSize += symbol->Length; } break; default: @@ -138,6 +143,8 @@ void HandleOpcode(void) { arg = PeekToken(); } + PC++; + if (arg->Class == RegisterClass) { ExpectRegister(); @@ -149,6 +156,8 @@ void HandleOpcode(void) { opcode->Value.Mnemonic = isByte ? COPYB : COPYI; opcode->Lemexe = isByte ? "copyb" : "copyi"; + isByte ? PC++ : (PC += 2); + AdvanceParser(); } else if (arg->Class == RegisterClass) { @@ -162,10 +171,13 @@ void HandleOpcode(void) { opcode->Value.Mnemonic = COPY; opcode->Lemexe = "copy"; + + PC++; } else if (arg->Class & IdentifierClass) { opcode->Value.Mnemonic = isByte ? COPYAB : COPYA; opcode->Lemexe = isByte ? "copyab" : "copya"; + PC += 2; ExpectIdentifier(0, ForwardParser); } @@ -177,7 +189,8 @@ void HandleOpcode(void) { ExpectPuncuation(RBracket, ForwardParser); opcode->Value.Mnemonic = isByte ? COPYRAB : COPYRA; - opcode->Lemexe = isByte ? "copyrab" : "copyra"; + opcode->Lemexe = isByte ? "copyrab" : "copyra"; + PC++; } } else { @@ -197,6 +210,8 @@ void HandleOpcode(void) { opcode->Value.Mnemonic = isByte ? COPYRARAB : COPYRARA; opcode->Lemexe = isByte ? "copyrarab" : "copyrara"; + + PC++; } } break; @@ -208,20 +223,28 @@ void HandleOpcode(void) { TokenClass class = PeekToken()->Class; + PC++; + if (class == NumberClass) { opcode->Value.Mnemonic = CMPI; opcode->Lemexe = "CMPI"; AdvanceParser(); + + PC += 2; } else if (class & IdentifierClass) { opcode->Value.Mnemonic = CMPI; opcode->Lemexe = "CMPI"; ExpectIdentifier(0, ForwardParser); + + PC += 2; } else { ExpectRegister(); + + PC++; } } break; @@ -230,14 +253,19 @@ void HandleOpcode(void) { case AND: case OR: case XOR: + PC++; + ExpectRegister(); ExpectPuncuation(Comma, ForwardParser); ExpectRegister(); + + PC++; break; case SHL: case SHR: + PC++; ExpectRegister(); ExpectPuncuation(Comma, ForwardParser); @@ -249,15 +277,20 @@ void HandleOpcode(void) { } AdvanceParser(); + + PC += 2; break; case INC: case DEC: case PUSH: case POP: + PC++; ExpectRegister(); break; case JMP: { + PC++; + Token* arg = PeekToken(); if (arg->Class & IdentifierClass) { @@ -265,6 +298,8 @@ void HandleOpcode(void) { opcode->Value.Mnemonic = JMP; opcode->Lemexe = "jmp"; + + PC += 2; } else { @@ -281,29 +316,44 @@ void HandleOpcode(void) { break; case JZ: { + PC++; + ExpectIdentifier(0, ForwardParser); + PC += 2; + opcode->Value.Mnemonic = JZ; opcode->Lemexe = "jz"; } break; case JG: { + PC++; + ExpectIdentifier(0, ForwardParser); + + PC += 2; + opcode->Value.Mnemonic = JG; opcode->Lemexe = "jg"; } break; case JL: { + PC++; + ExpectIdentifier(0, ForwardParser); + PC += 2; + opcode->Value.Mnemonic = JL; opcode->Lemexe = "jl"; } break; case CALL: { + PC++; + Token* arg = PeekToken(); if ((arg->Class & IdentifierClass) == 0) { @@ -313,6 +363,8 @@ void HandleOpcode(void) { } ExpectIdentifier(0, ForwardParser); + + PC += 2; } break; default: @@ -363,11 +415,10 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options) { } if (found) { - symbol->Address = HeapSize; symbol->Resolved = resolved || symbol->Resolved; } else { - symbol = AddSymbolToTable(token->Lemexe, HeapSize, resolved, MachineState.SymbolsTable); + symbol = AddSymbolToTable(token->Lemexe, PC, resolved, MachineState.SymbolsTable); } if (options & RemoveExpected) RemoveCurrentToken(); diff --git a/src/scanner.c b/src/scanner.c index 8ba6286..da11532 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -283,14 +283,6 @@ Token* ParseIdentifier(void) { return token; } - // if (strcmp(lexeme, "load") == 0) { - // Token* token = CreateToken(Line, DirectiveClass); - - // token->Lemexe = lexeme; - // token->Value.Directive = Load; - - // return token; - // } if (strcmp(lexeme, "byte") == 0) { Token* token = CreateToken(Line, DirectiveClass); @@ -299,14 +291,6 @@ Token* ParseIdentifier(void) { return token; } - // if (strcmp(lexeme, "store") == 0) { - // Token* token = CreateToken(Line, DirectiveClass); - - // token->Lemexe = lexeme; - // token->Value.Directive = Store; - - // return token; - // } Token* token = CreateToken(Line, IdentifierClass);