Code cleanup.
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
//#include "symbols_table.h"
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
R1 = 0, R2, R3, R4, R5, R6, R7, R8 = 7
|
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
|
JMPI = 0xB8, JMP = 0x02, JZ = 0x03, JG = 0x04, JL = 0x05, NOP = 0x01, CALL = 0x06, RET = 0x07
|
||||||
} Mnemonic;
|
} Mnemonic;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Mnemonic Mnemonic;
|
|
||||||
} Instruction;
|
|
||||||
|
|
||||||
Instruction* CreateInstruction(Mnemonic mnemonic);
|
|
||||||
void FreeInstruction(Instruction* instruction);
|
|
||||||
int IsOpcode(const char*, Mnemonic*);
|
int IsOpcode(const char*, Mnemonic*);
|
||||||
int IsRegister(const char*, Registers*);
|
int IsRegister(const char*, Registers*);
|
||||||
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]);
|
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]);
|
||||||
void GetRegisterText(Registers reg, char buffer[3]);
|
void GetRegisterText(Registers reg, char buffer[3]);
|
||||||
unsigned char GetInstructionMask(Mnemonic type);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+1
-8
@@ -11,13 +11,6 @@
|
|||||||
#include "opcodes.h"
|
#include "opcodes.h"
|
||||||
#include "symbols_table.h"
|
#include "symbols_table.h"
|
||||||
|
|
||||||
//#define HIGHMEMORY 65535 //64KiB - 1 AKA 0xFFFF
|
SymbolTable* ParseTokens(List* tokens);
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
List* Instructions;
|
|
||||||
SymbolTable* SymbolsTable;
|
|
||||||
} IRState;
|
|
||||||
|
|
||||||
IRState* ParseTokens(List*);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
.db MAX_LENGTH 32;MAX_MEM - MAX_LENGTH
|
.db MAX_LENGTH 32;MAX_MEM - MAX_LENGTH
|
||||||
.db MSG "Hello, World!", 0
|
.db MSG "Hello, World!", 0
|
||||||
|
|
||||||
_start:
|
__start:
|
||||||
copy r1, MSG ; Pointer into r1
|
copy r1, MSG ; Pointer into r1
|
||||||
copy r8, VIDEO_MEM
|
copy r8, VIDEO_MEM
|
||||||
call strlen
|
call strlen
|
||||||
|
|||||||
+3
-5
@@ -8,6 +8,7 @@
|
|||||||
#include "../includes/scanner.h"
|
#include "../includes/scanner.h"
|
||||||
#include "../includes/parser.h"
|
#include "../includes/parser.h"
|
||||||
|
|
||||||
|
const char* MagicStartName = "__start";
|
||||||
List* LIST;
|
List* LIST;
|
||||||
SymbolTable* Symbols;
|
SymbolTable* Symbols;
|
||||||
|
|
||||||
@@ -30,9 +31,7 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
LIST = GenerateTokenList(source_code);
|
LIST = GenerateTokenList(source_code);
|
||||||
|
|
||||||
IRState* state = ParseTokens(LIST);
|
Symbols = ParseTokens(LIST);
|
||||||
|
|
||||||
Symbols = state->SymbolsTable;
|
|
||||||
|
|
||||||
free(source_code);
|
free(source_code);
|
||||||
|
|
||||||
@@ -125,8 +124,7 @@ unsigned short GetValueFromToken(Token* token) {
|
|||||||
Symbol* symbol;
|
Symbol* symbol;
|
||||||
|
|
||||||
if (TryGetSymbol(token->Lemexe, Symbols, &symbol)) {
|
if (TryGetSymbol(token->Lemexe, Symbols, &symbol)) {
|
||||||
printf("Returning %d for address of %s.\n", symbol->Address, symbol->Name);
|
return symbol->Address;
|
||||||
return 0xABCD;//symbol->Address;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "Failed to find symbol %s while assembling.\n", token->Lemexe);
|
fprintf(stderr, "Failed to find symbol %s while assembling.\n", token->Lemexe);
|
||||||
|
|||||||
@@ -44,26 +44,6 @@ struct _instruction instructions[31] = {
|
|||||||
//yld
|
//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]) {
|
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]) {
|
||||||
memset(buffer, '\0', 12);
|
memset(buffer, '\0', 12);
|
||||||
|
|
||||||
|
|||||||
+11
-11
@@ -18,7 +18,6 @@ void AdvanceParser(void);
|
|||||||
void IgnoreParserLine(void);
|
void IgnoreParserLine(void);
|
||||||
Token* PeekToken(void);
|
Token* PeekToken(void);
|
||||||
int ParserAtEnd(void);
|
int ParserAtEnd(void);
|
||||||
IRState MachineState;
|
|
||||||
|
|
||||||
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options);
|
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options);
|
||||||
void ExpectRegister(void);
|
void ExpectRegister(void);
|
||||||
@@ -26,16 +25,17 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options);
|
|||||||
void ExpectCharacterClass(Symbol* symbol);
|
void ExpectCharacterClass(Symbol* symbol);
|
||||||
void ExpectLineEndOrFileEnd(ExpectOptions options);
|
void ExpectLineEndOrFileEnd(ExpectOptions options);
|
||||||
|
|
||||||
|
SymbolTable* SymbolsTable;
|
||||||
|
|
||||||
//int HeapSize = 0;
|
//int HeapSize = 0;
|
||||||
int PC = 0;
|
int PC = 0;
|
||||||
|
|
||||||
IRState* ParseTokens(List* tokens) {
|
SymbolTable* ParseTokens(List* tokens) {
|
||||||
if (!tokens) return NULL;
|
if (!tokens) return NULL;
|
||||||
|
|
||||||
TokensList = tokens;
|
TokensList = tokens;
|
||||||
|
|
||||||
MachineState.SymbolsTable = CreateSymbolTable();
|
SymbolsTable = CreateSymbolTable();
|
||||||
MachineState.Instructions = CreateList();
|
|
||||||
|
|
||||||
while(!ParserAtEnd()) {
|
while(!ParserAtEnd()) {
|
||||||
Token* t = PeekToken();
|
Token* t = PeekToken();
|
||||||
@@ -51,14 +51,14 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
case LabelClass:
|
case LabelClass:
|
||||||
{
|
{
|
||||||
Symbol* symbol;
|
Symbol* symbol;
|
||||||
int found = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable, &symbol);
|
int found = TryGetSymbol(t->Lemexe, SymbolsTable, &symbol);
|
||||||
|
|
||||||
if (found && symbol->Resolved) {
|
if (found && symbol->Resolved) {
|
||||||
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
|
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) AddSymbolToTable(t->Lemexe, PC, 1, MachineState.SymbolsTable);
|
if (!found) AddSymbolToTable(t->Lemexe, PC, 1, SymbolsTable);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
symbol->Address = PC;
|
symbol->Address = PC;
|
||||||
@@ -80,7 +80,7 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
PrintSymbols();
|
PrintSymbols();
|
||||||
|
|
||||||
return &MachineState;
|
return SymbolsTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HandleAssemblerDirective() {
|
void HandleAssemblerDirective() {
|
||||||
@@ -407,7 +407,7 @@ Symbol* ExpectIdentifier(int resolved, ExpectOptions options) {
|
|||||||
|
|
||||||
Symbol* symbol;
|
Symbol* symbol;
|
||||||
|
|
||||||
int found = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable, &symbol);
|
int found = TryGetSymbol(token->Lemexe, SymbolsTable, &symbol);
|
||||||
|
|
||||||
if (found && symbol->Resolved && resolved) {
|
if (found && symbol->Resolved && resolved) {
|
||||||
fprintf(stderr, "[Line %d] Redefinition of symbol '%s'.\n", token->LineNumber, token->Lemexe);
|
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;
|
symbol->Resolved = resolved || symbol->Resolved;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
symbol = AddSymbolToTable(token->Lemexe, PC, resolved, MachineState.SymbolsTable);
|
symbol = AddSymbolToTable(token->Lemexe, PC, resolved, SymbolsTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options & RemoveExpected) RemoveCurrentToken();
|
if (options & RemoveExpected) RemoveCurrentToken();
|
||||||
@@ -481,8 +481,8 @@ void ExpectLineEndOrFileEnd(ExpectOptions options) {
|
|||||||
void PrintSymbols(void) {
|
void PrintSymbols(void) {
|
||||||
//char mn[12];
|
//char mn[12];
|
||||||
printf("-----SYMBOLS-----\n");
|
printf("-----SYMBOLS-----\n");
|
||||||
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
for(int i = 0; i < SymbolsTable->Size; i++) {
|
||||||
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
|
Symbol* symbol = SymbolsTable->Symbols[i];
|
||||||
|
|
||||||
printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);
|
printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user