From 3031fa8b9bc32d1af8c5e312f7ab7c608693ed90 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 29 Jun 2023 20:53:39 -0500 Subject: [PATCH] Code cleanup. --- includes/opcodes.h | 8 -------- includes/parser.h | 9 +-------- misc/another_test.asm | 2 +- src/main.c | 8 +++----- src/opcodes.c | 20 -------------------- src/parser.c | 22 +++++++++++----------- 6 files changed, 16 insertions(+), 53 deletions(-) diff --git a/includes/opcodes.h b/includes/opcodes.h index 6c3955c..f2ab1bf 100644 --- a/includes/opcodes.h +++ b/includes/opcodes.h @@ -4,7 +4,6 @@ #include #include #include -//#include "symbols_table.h" typedef enum { R1 = 0, R2, R3, R4, R5, R6, R7, R8 = 7 @@ -17,16 +16,9 @@ typedef enum { JMPI = 0xB8, JMP = 0x02, JZ = 0x03, JG = 0x04, JL = 0x05, NOP = 0x01, CALL = 0x06, RET = 0x07 } Mnemonic; -typedef struct { - Mnemonic Mnemonic; -} Instruction; - -Instruction* CreateInstruction(Mnemonic mnemonic); -void FreeInstruction(Instruction* instruction); int IsOpcode(const char*, Mnemonic*); int IsRegister(const char*, Registers*); void GetMnemonicText(Mnemonic mnemonic, char buffer[12]); void GetRegisterText(Registers reg, char buffer[3]); -unsigned char GetInstructionMask(Mnemonic type); #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index 4962385..c9804f3 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -11,13 +11,6 @@ #include "opcodes.h" #include "symbols_table.h" -//#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF - -typedef struct { - List* Instructions; - SymbolTable* SymbolsTable; -} IRState; - -IRState* ParseTokens(List*); +SymbolTable* ParseTokens(List* tokens); #endif \ No newline at end of file diff --git a/misc/another_test.asm b/misc/another_test.asm index eb4355e..fae43e5 100644 --- a/misc/another_test.asm +++ b/misc/another_test.asm @@ -3,7 +3,7 @@ .db MAX_LENGTH 32;MAX_MEM - MAX_LENGTH .db MSG "Hello, World!", 0 -_start: +__start: copy r1, MSG ; Pointer into r1 copy r8, VIDEO_MEM call strlen diff --git a/src/main.c b/src/main.c index cf94871..4e2d284 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include "../includes/scanner.h" #include "../includes/parser.h" +const char* MagicStartName = "__start"; List* LIST; SymbolTable* Symbols; @@ -30,9 +31,7 @@ int main(int argc, char* args[]) { LIST = GenerateTokenList(source_code); - IRState* state = ParseTokens(LIST); - - Symbols = state->SymbolsTable; + Symbols = ParseTokens(LIST); free(source_code); @@ -125,8 +124,7 @@ unsigned short GetValueFromToken(Token* token) { Symbol* symbol; if (TryGetSymbol(token->Lemexe, Symbols, &symbol)) { - printf("Returning %d for address of %s.\n", symbol->Address, symbol->Name); - return 0xABCD;//symbol->Address; + return symbol->Address; } else { fprintf(stderr, "Failed to find symbol %s while assembling.\n", token->Lemexe); diff --git a/src/opcodes.c b/src/opcodes.c index 0466840..70ea440 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -44,26 +44,6 @@ struct _instruction instructions[31] = { //yld }; -Instruction* CreateInstruction(Mnemonic mnemonic) { - Instruction* instruction = calloc(1, sizeof(Instruction)); - - if (!instruction) { - fprintf(stderr, "Failed to calloc room for an Instruction. %s.\n", strerror(errno)); - - return NULL; - } - - instruction->Mnemonic = mnemonic; - - return instruction; -} - -void FreeInstruction(Instruction* instruction) { - if (!instruction) return; - - free(instruction); -} - void GetMnemonicText(Mnemonic mnemonic, char buffer[12]) { memset(buffer, '\0', 12); diff --git a/src/parser.c b/src/parser.c index 3b8eaac..cb090ff 100644 --- a/src/parser.c +++ b/src/parser.c @@ -18,7 +18,6 @@ void AdvanceParser(void); void IgnoreParserLine(void); Token* PeekToken(void); int ParserAtEnd(void); -IRState MachineState; void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options); void ExpectRegister(void); @@ -26,16 +25,17 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options); void ExpectCharacterClass(Symbol* symbol); void ExpectLineEndOrFileEnd(ExpectOptions options); +SymbolTable* SymbolsTable; + //int HeapSize = 0; int PC = 0; -IRState* ParseTokens(List* tokens) { +SymbolTable* ParseTokens(List* tokens) { if (!tokens) return NULL; TokensList = tokens; - MachineState.SymbolsTable = CreateSymbolTable(); - MachineState.Instructions = CreateList(); + SymbolsTable = CreateSymbolTable(); while(!ParserAtEnd()) { Token* t = PeekToken(); @@ -51,14 +51,14 @@ IRState* ParseTokens(List* tokens) { case LabelClass: { Symbol* symbol; - int found = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable, &symbol); + int found = TryGetSymbol(t->Lemexe, SymbolsTable, &symbol); if (found && symbol->Resolved) { fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe); exit(1); } - if (!found) AddSymbolToTable(t->Lemexe, PC, 1, MachineState.SymbolsTable); + if (!found) AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable); else { symbol->Address = PC; @@ -80,7 +80,7 @@ IRState* ParseTokens(List* tokens) { PrintSymbols(); - return &MachineState; + return SymbolsTable; } void HandleAssemblerDirective() { @@ -407,7 +407,7 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options) { Symbol* symbol; - int found = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable, &symbol); + int found = TryGetSymbol(token->Lemexe, SymbolsTable, &symbol); if (found && symbol->Resolved && resolved) { fprintf(stderr, "[Line %d] Redefinition of symbol '%s'.\n", token->LineNumber, token->Lemexe); @@ -418,7 +418,7 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options) { symbol->Resolved = resolved || symbol->Resolved; } else { - symbol = AddSymbolToTable(token->Lemexe, PC, resolved, MachineState.SymbolsTable); + symbol = AddSymbolToTable(token->Lemexe, PC, resolved, SymbolsTable); } if (options & RemoveExpected) RemoveCurrentToken(); @@ -481,8 +481,8 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) { void PrintSymbols(void) { //char mn[12]; printf("-----SYMBOLS-----\n"); - for(int i = 0; i < MachineState.SymbolsTable->Size; i++) { - Symbol* symbol = MachineState.SymbolsTable->Symbols[i]; + for(int i = 0; i < SymbolsTable->Size; i++) { + Symbol* symbol = SymbolsTable->Symbols[i]; printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);