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:
+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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user