Some refactoring to update everything to use the new structures and some new considerations for how Symbols and Instructions shoudl be represented.
This commit is contained in:
+8
-6
@@ -4,8 +4,6 @@
|
||||
#include <string.h>
|
||||
#include "symbols_table.h"
|
||||
|
||||
#define OPCODECOUNT 13
|
||||
|
||||
typedef enum {
|
||||
R1, R2, R3, R4, R5, R6, R7, R8
|
||||
} Registers;
|
||||
@@ -16,9 +14,10 @@ typedef enum {
|
||||
} Mnemonic;
|
||||
|
||||
typedef enum {
|
||||
None,
|
||||
Constant,
|
||||
Address
|
||||
NoParameter,
|
||||
ConstantParameter,
|
||||
AddressParameter,
|
||||
RegisterParameter
|
||||
} OpcodeParameter;
|
||||
|
||||
typedef struct {
|
||||
@@ -37,10 +36,13 @@ typedef struct {
|
||||
} ParameterTwo;
|
||||
} Instruction;
|
||||
|
||||
extern Instruction instructions[OPCODECOUNT];
|
||||
//extern Instruction instructions[OPCODECOUNT];
|
||||
|
||||
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]);
|
||||
//const Instruction* GetOpcodeDetails(Mnemonic);
|
||||
unsigned char GetInstructionMask(Mnemonic type);
|
||||
|
||||
|
||||
+14
-14
@@ -18,24 +18,24 @@
|
||||
// int resolved;
|
||||
// } Symbol;
|
||||
|
||||
typedef struct {
|
||||
Mnemonic Mnemonic;
|
||||
TokenClass ParameterOneType;
|
||||
TokenClass ParameterTwoType;
|
||||
// typedef struct {
|
||||
// Mnemonic Mnemonic;
|
||||
// TokenClass ParameterOneType;
|
||||
// TokenClass ParameterTwoType;
|
||||
|
||||
union {
|
||||
uint16_t Constant;
|
||||
Symbol* Address;
|
||||
} ParameterOne;
|
||||
// union {
|
||||
// uint16_t Constant;
|
||||
// Symbol* Address;
|
||||
// } ParameterOne;
|
||||
|
||||
union {
|
||||
uint16_t Constant;
|
||||
Symbol* Address;
|
||||
} ParameterTwo;
|
||||
} IROpcode;
|
||||
// union {
|
||||
// uint16_t Constant;
|
||||
// Symbol* Address;
|
||||
// } ParameterTwo;
|
||||
// } IROpcode;
|
||||
|
||||
typedef struct {
|
||||
List* Opcodes;
|
||||
List* Instructions;
|
||||
SymbolTable* SymbolsTable;
|
||||
} IRState;
|
||||
|
||||
|
||||
@@ -8,11 +8,21 @@
|
||||
|
||||
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||
|
||||
typedef enum {
|
||||
Data,
|
||||
Address
|
||||
} SymbolType;
|
||||
|
||||
typedef struct {
|
||||
void* Value;
|
||||
char* Name;
|
||||
int Length;
|
||||
int Resolved;
|
||||
|
||||
union {
|
||||
char* Text;
|
||||
int Number;
|
||||
//Instruction Instruction;
|
||||
} Value;
|
||||
} Symbol;
|
||||
|
||||
typedef struct {
|
||||
|
||||
+69
-31
@@ -2,22 +2,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Instruction instructions[OPCODECOUNT] = {
|
||||
{ "copy", COPY, Reg | Address, Reg | Constant | Address },
|
||||
{ "add", ADD, Reg, Reg | Constant },
|
||||
{ "sub", SUB, Reg, Reg | Constant },
|
||||
{ "jz", JZ, Address, None },
|
||||
{ "int", INT, Constant, None },
|
||||
{ "yld", YLD, None, None },
|
||||
{ "ret", RET, None, None },
|
||||
{ "cmp", CMP, Reg, Reg | Constant },
|
||||
{ "in", IN, Constant | Reg, None},
|
||||
{ "out", OUT, None, None},
|
||||
{ "nop", NOP, None, None},
|
||||
{ "jmp", JMP, Address, None},
|
||||
{ "call", CALL, Address, None}
|
||||
#define OPCODECOUNT 13
|
||||
|
||||
struct _instruction {
|
||||
char* Name;
|
||||
Mnemonic Mnemonic;
|
||||
};
|
||||
|
||||
struct _instruction instructions[OPCODECOUNT] = {
|
||||
{ "copy", COPY },//, Reg | Address, Reg | Constant | Address },
|
||||
{ "add", ADD },//, Reg, Reg | Constant },
|
||||
{ "sub", SUB },//, Reg, Reg | Constant },
|
||||
{ "jz", JZ },//, Address, None },
|
||||
{ "int", INT },//, Constant, None },
|
||||
{ "yld", YLD },//, None, None },
|
||||
{ "ret", RET },//, None, None },
|
||||
{ "cmp", CMP },//, Reg, Reg | Constant },
|
||||
{ "in", IN },//, Constant | Reg, None},
|
||||
{ "out", OUT },//, None, None},
|
||||
{ "nop", NOP },//, None, None},
|
||||
{ "jmp", JMP },//, Address, None},
|
||||
{ "call", CALL },//, Address, None}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
unsigned char GetInstructionMask(Mnemonic type) {
|
||||
switch (type) {
|
||||
case COPY:
|
||||
@@ -49,6 +76,17 @@ unsigned char GetInstructionMask(Mnemonic type) {
|
||||
}
|
||||
}
|
||||
|
||||
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]) {
|
||||
memset(buffer, '\0', 12);
|
||||
|
||||
for(int i = 0; i < OPCODECOUNT; i++) {
|
||||
if (instructions[i].Mnemonic == mnemonic) {
|
||||
strncpy(buffer, instructions[i].Name, 11);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
NOP - 0000 0000 NOP
|
||||
JZ - 0000 0001 JZ Address
|
||||
@@ -82,30 +120,30 @@ unsigned char GetInstructionMask(Mnemonic type) {
|
||||
//Address XXX1 0XXX
|
||||
//R1 XXXX X000 -> XXXX X111 (R1 to R8)
|
||||
|
||||
Register registers[REGISTERCOUNT] = {
|
||||
{ "r1", R1 },
|
||||
{ "r2", R2 },
|
||||
{ "r3", R3 },
|
||||
{ "r4", R4 },
|
||||
{ "r5", R5 },
|
||||
{ "r6", R6 },
|
||||
{ "r7", R7 },
|
||||
{ "r8", R8 }
|
||||
};
|
||||
// Register registers[REGISTERCOUNT] = {
|
||||
// { "r1", R1 },
|
||||
// { "r2", R2 },
|
||||
// { "r3", R3 },
|
||||
// { "r4", R4 },
|
||||
// { "r5", R5 },
|
||||
// { "r6", R6 },
|
||||
// { "r7", R7 },
|
||||
// { "r8", R8 }
|
||||
// };
|
||||
|
||||
const Instruction* GetOpcodeDetails(Mnemonic type) {
|
||||
for(int i = 0; i < OPCODECOUNT; i++) {
|
||||
if (instructions[i].op == type) return &instructions[i];
|
||||
}
|
||||
// const Instruction* GetOpcodeDetails(Mnemonic type) {
|
||||
// for(int i = 0; i < OPCODECOUNT; i++) {
|
||||
// if (instructions[i].op == type) return &instructions[i];
|
||||
// }
|
||||
|
||||
return NULL;
|
||||
}
|
||||
// return NULL;
|
||||
// }
|
||||
|
||||
int IsOpcode(const char* text, Mnemonic* opcode) {
|
||||
if (!text) return 0;
|
||||
|
||||
for(int i = 0; i < OPCODECOUNT; i++) {
|
||||
if (strcmp(instructions[i], text) == 0) {
|
||||
if (strcmp(instructions[i].Name, text) == 0) {
|
||||
if (opcode) *opcode = instructions[i].Mnemonic;
|
||||
return 1;
|
||||
}
|
||||
|
||||
+17
-12
@@ -10,7 +10,7 @@ int CurrentToken = 0;
|
||||
//int HeapStart = 4; // zero indexed, used for declared variables.
|
||||
//int BinaryEnd = 4; // Zero indexed (binary starts with a 4 byte header)
|
||||
|
||||
IROpcode* CreateIROpcode(Mnemonic mnemonic);
|
||||
//IROpcode* CreateIROpcode(Mnemonic mnemonic);
|
||||
void PrintSymbols(void);
|
||||
void HandleOperation(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
@@ -19,8 +19,8 @@ Token* PeekToken(void);
|
||||
int ParserAtEnd(void);
|
||||
int Expect(int, ...);
|
||||
//List* SymbolsTable;
|
||||
unsigned int HeapTop = 4;
|
||||
unsigned int ProgramCounter = 0;
|
||||
// unsigned int HeapTop = 4;
|
||||
// unsigned int ProgramCounter = 0;
|
||||
// unsigned char Heap[HIGHMEMORY];
|
||||
// unsigned char Memory[HIGHMEMORY];
|
||||
IRState MachineState;
|
||||
@@ -34,7 +34,7 @@ IRState* ParseTokens(List* tokens) {
|
||||
TokensList = tokens;
|
||||
|
||||
MachineState.SymbolsTable = CreateSymbolTable();
|
||||
MachineState.Opcodes = CreateList();
|
||||
MachineState.Instructions = CreateList();
|
||||
|
||||
while(!ParserAtEnd()) {
|
||||
Token* t = PeekToken();
|
||||
@@ -48,7 +48,8 @@ IRState* ParseTokens(List* tokens) {
|
||||
break;
|
||||
case LabelClass:
|
||||
printf("%s: \n", t->Lemexe);
|
||||
AddSymbol(t, 2, ProgramCounter, 1);
|
||||
AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable);
|
||||
//AddSymbol(t, 2, ProgramCounter, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -105,7 +106,7 @@ void HandleAssemblerDirective(void) {
|
||||
// }
|
||||
|
||||
//ProgramCounter += strlen(PeekToken()->lexeme);
|
||||
HeapTop += strlen(identifier->Lemexe);
|
||||
//HeapTop += strlen(identifier->Lemexe);
|
||||
symbol->Length = strlen(identifier->Lemexe);
|
||||
|
||||
AdvanceParser();
|
||||
@@ -124,7 +125,7 @@ void HandleAssemblerDirective(void) {
|
||||
// identifier->value = identifier->lexeme;
|
||||
// identifier->lexeme[oldLength + 1] = '\0';//*PeekToken()->lexeme & 0x0F;
|
||||
symbol->Length++; //Basically, read in the NULL byte at the end of the string.
|
||||
HeapTop++;
|
||||
//HeapTop++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -140,11 +141,11 @@ void HandleAssemblerDirective(void) {
|
||||
void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
//Memory[ProgramCounter] = mask;
|
||||
|
||||
ProgramCounter++;
|
||||
//ProgramCounter++;
|
||||
|
||||
Token* token = PeekToken();
|
||||
|
||||
IROpcode* opcode = CreateIROpcode(token->Value.Mnemonic);
|
||||
Instruction* opcode = CreateInstruction(token->Value.Mnemonic); //CreateIROpcode(token->Value.Mnemonic);
|
||||
|
||||
AdvanceParser();
|
||||
|
||||
@@ -152,15 +153,19 @@ void HandleRegisterBasedOpcode(unsigned char mask) {
|
||||
|
||||
if (token->Class == RegisterClass) {
|
||||
//Memory[ProgramCounter - 1] |= (PeekToken()->type - R1) & 0x07;
|
||||
opcode->ParameterOneType = RegisterClass;
|
||||
opcode->ParameterOneType = RegisterParameter;
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (token->Class & AddressClass && token->Value.Mnemonic == COPY) {
|
||||
//opcode->ParameterOneType = Address;
|
||||
|
||||
Symbol* symbol = GetSymbol(token->Lemexe);
|
||||
Symbol* symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable); //GetSymbol(token->Lemexe);
|
||||
|
||||
if (!symbol) opcode->ParameterOne.Address = AddSymbol(token, 2, 0, 0);
|
||||
if (!symbol) {
|
||||
opcode->ParameterOneType = AddressParameter;
|
||||
//AddSymbol(token, 2, 0, 0);
|
||||
opcode->ParameterOne.Symbol = AddSymbolToTable(token->Lemexe, NULL, strlen(token->Lemexe), MachineState.SymbolsTable);
|
||||
}
|
||||
else opcode->ParameterOne.Address = symbol;//CreateSymbol(token, symbol->address, symbol->resolved);
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user