From bdb4d2b2414a51669b98f1195e4eb8da3c8a697e Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 6 Oct 2022 21:11:22 -0500 Subject: [PATCH] Updated the way Instructions keep track of symbols. Only their name is important but we need the Symbols to be able to track which Instruction they point to (if they do that is, i.e. a label symbol). --- includes/opcodes.h | 6 ++++-- includes/symbols_table.h | 9 +++------ src/main.c | 38 ++++++++++++++++++-------------------- src/parser.c | 28 ++++++++++++++++++++++------ 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/includes/opcodes.h b/includes/opcodes.h index 25fbf11..b5257f8 100644 --- a/includes/opcodes.h +++ b/includes/opcodes.h @@ -2,7 +2,9 @@ #define OPCODES_H #include -#include "symbols_table.h" +#include +#include +//#include "symbols_table.h" typedef enum { R1, R2, R3, R4, R5, R6, R7, R8 @@ -27,7 +29,7 @@ typedef struct { union { int Number; //ConstanrParam Registers Register; //Register param - Symbol* Symbol; //Address param + char* Symbol; //Symbol* Symbol; //Address param } Value; } Parameter; diff --git a/includes/symbols_table.h b/includes/symbols_table.h index 155b560..23f9b10 100644 --- a/includes/symbols_table.h +++ b/includes/symbols_table.h @@ -5,23 +5,20 @@ #include #include #include +#include "opcodes.h" #define SYMBOLSTABLE_DEFAULT_CAPACITY 128 -typedef enum { - ValueAt, - Address -} SymbolType; - typedef struct { char* Name; int Length; int Resolved; + int InstructionPointer; union { char* Text; int Number; - //Instruction Instruction; + Instruction* Instruction; } Value; } Symbol; diff --git a/src/main.c b/src/main.c index 2da383c..4c5c576 100644 --- a/src/main.c +++ b/src/main.c @@ -96,25 +96,23 @@ int main(int argc, char* args[]) { continue; } - if (ins->ParameterOne->ParameterType == RegisterParameter) { - GetRegisterText(ins->ParameterOne->Value.Register, reg); + switch(ins->ParameterOne->ParameterType) { + case RegisterParameter: + GetRegisterText(ins->ParameterOne->Value.Register, reg); - if (ins->ParameterOne->InterpretedAs == AddressParameter) { - printf("[%s]", reg); - } - else { - printf("%s", reg); - } - } - - if (ins->ParameterOne->ParameterType == ConstantParameter) { - if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%d]", ins->ParameterOne->Value.Number); - else printf("%d", ins->ParameterOne->Value.Number); - } - - if (ins->ParameterOne->ParameterType == AddressParameter) { - if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%s]", ins->ParameterOne->Value.Symbol->Name); - else printf("%s", ins->ParameterOne->Value.Symbol->Name); + if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%s]", reg); + else printf("%s", reg); + break; + case ConstantParameter: + if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%d]", ins->ParameterOne->Value.Number); + else printf("%d", ins->ParameterOne->Value.Number); + break; + case AddressParameter: + if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%s]", ins->ParameterOne->Value.Symbol); + else printf("%s", ins->ParameterOne->Value.Symbol); + break; + default: + printf("\n"); } if (!ins->ParameterTwo) { @@ -134,8 +132,8 @@ int main(int argc, char* args[]) { else printf(", %d\n", ins->ParameterTwo->Value.Number); break; case AddressParameter: - if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%s]\n", ins->ParameterTwo->Value.Symbol->Name); - else printf(", %s\n", ins->ParameterTwo->Value.Symbol->Name); + if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%s]\n", ins->ParameterTwo->Value.Symbol); + else printf(", %s\n", ins->ParameterTwo->Value.Symbol); break; default: printf("\n"); diff --git a/src/parser.c b/src/parser.c index 6f64bd8..988f9f5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,10 +1,11 @@ #include "../includes/parser.h" +#include const List* TokensList; int CurrentToken = 0; void PrintSymbols(void); -void HandleOperation(void); +Instruction* HandleOperation(void); void HandleAssemblerDirective(void); void AdvanceParser(void); void IgnoreParserLine(void); @@ -45,6 +46,13 @@ IRState* ParseTokens(List* tokens) { else symbol->Resolved = 1; AdvanceParser(); //Consume the NewLine + + // Instruction* ins = HandleOperation(); + + // if (!ins) continue; + + // symbol->Value.Instruction = ins; + // symbol->InstructionPointer = 1; } break; default: @@ -78,13 +86,13 @@ Parameter* GetParameterType() { //case LabelClass: case IdentifierClass: param->ParameterType = AddressParameter; - param->InterpretedAs = ConstantParameter; + param->InterpretedAs = AddressParameter; symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable); if (!symbol) symbol = AddSymbolToTable(token->Lemexe, token->Lemexe, strlen(token->Lemexe), MachineState.SymbolsTable); - param->Value.Symbol = symbol; + param->Value.Symbol = symbol->Name; AdvanceParser(); @@ -111,13 +119,13 @@ Parameter* GetParameterType() { } else if (token->Class & AddressClass) { param->ParameterType = AddressParameter; - param->ParameterType = AddressParameter; + param->InterpretedAs = AddressParameter; symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable); if (!symbol) symbol = AddSymbolToTable(token->Lemexe, NULL, strlen(token->Lemexe), MachineState.SymbolsTable); - param->Value.Symbol = symbol; + param->Value.Symbol = symbol->Name; } else { fprintf(stderr, "[Error] Line %d: Expected identifier, constant number or register.\n", token->LineNumber); @@ -154,10 +162,16 @@ Parameter* GetParameterType() { return param; } -void HandleOperation() { +Instruction* HandleOperation() { Token* token = PeekToken(); char mn[12]; + if (token->Class != MnemonicClass) { + fprintf(stderr, "[Error] Line %d: Expected instruction mnemonic\n", token->LineNumber); + IgnoreParserLine(); + return NULL; + } + Instruction* ins = CreateInstruction(token->Value.Mnemonic); GetMnemonicText(ins->Mnemonic, mn); @@ -202,6 +216,8 @@ void HandleOperation() { AdvanceParser(); //Consume the line break free(ins); + + return MachineState.Instructions->content[(MachineState.Instructions->size - 1) * sizeof(Instruction)]; } void PrintSymbols(void) {