Code cleanup.

This commit is contained in:
2023-06-29 20:53:39 -05:00
parent 2918f2964d
commit 3031fa8b9b
6 changed files with 16 additions and 53 deletions
-8
View File
@@ -4,7 +4,6 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
//#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
+1 -8
View File
@@ -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
+1 -1
View File
@@ -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
+3 -5
View File
@@ -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);
-20
View File
@@ -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);
+11 -11
View File
@@ -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);