Added some debug print out for the instruction list and a helper function to get a string for any Registers enum.
This commit is contained in:
@@ -43,6 +43,7 @@ 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]);
|
||||||
unsigned char GetInstructionMask(Mnemonic type);
|
unsigned char GetInstructionMask(Mnemonic type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+105
-45
@@ -20,67 +20,127 @@ int main(int argc, char* args[]) {
|
|||||||
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
||||||
|
|
||||||
List* list = GenerateTokenList(source_code);
|
List* list = GenerateTokenList(source_code);
|
||||||
char mnemonic[12];
|
// char mnemonic[12];
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
// for(int i = 0; i < list->size; i++) {
|
||||||
Token* t = (Token*) list->content[i];
|
// Token* t = (Token*) list->content[i];
|
||||||
|
|
||||||
if (t->EndOfFile) {
|
// if (t->EndOfFile) {
|
||||||
printf("EOF\n");
|
// printf("EOF\n");
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == PunctuationClass){
|
// if (t->Class == PunctuationClass){
|
||||||
if(t->Value.Punctuation == NewLine) {
|
// if(t->Value.Punctuation == NewLine) {
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
printf("<%d>", t->LineNumber);
|
// printf("<%d>", t->LineNumber);
|
||||||
|
|
||||||
printf("%c ", t->Value.Punctuation);
|
// printf("%c ", t->Value.Punctuation);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
printf("<%d>", t->LineNumber);
|
// printf("<%d>", t->LineNumber);
|
||||||
|
|
||||||
if (t->Class == LabelClass) {
|
// if (t->Class == LabelClass) {
|
||||||
printf("[L]%s* ", t->Lemexe);
|
// printf("[L]%s* ", t->Lemexe);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == IdentifierClass) {
|
// if (t->Class == IdentifierClass) {
|
||||||
printf("[I]%s ", t->Lemexe);
|
// printf("[I]%s ", t->Lemexe);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == RegisterClass) {
|
// if (t->Class == RegisterClass) {
|
||||||
printf("[R]%d", t->Value.Register);
|
// printf("[R]%d", t->Value.Register);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == NumberClass) {
|
// if (t->Class == NumberClass) {
|
||||||
printf("[N]%d ", t->Value.Number);
|
// printf("[N]%d ", t->Value.Number);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == MnemonicClass) {
|
// if (t->Class == MnemonicClass) {
|
||||||
GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
// GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
||||||
printf("[M]%s ", mnemonic);
|
// printf("[M]%s ", mnemonic);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == DirectiveClass) {
|
// if (t->Class == DirectiveClass) {
|
||||||
printf("[D]%d ", t->Value.Directive);
|
// printf("[D]%d ", t->Value.Directive);
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (t->Class == CharacterClass) {
|
// if (t->Class == CharacterClass) {
|
||||||
printf("%s ", t->Lemexe);
|
// printf("%s ", t->Lemexe);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
IRState* image = ParseTokens(list);
|
IRState* image = ParseTokens(list);
|
||||||
|
char instruction[12];
|
||||||
|
char reg[3];
|
||||||
|
|
||||||
|
for(int i = 0; i < image->Instructions->size; i++) {
|
||||||
|
Instruction* ins = image->Instructions->content[i];
|
||||||
|
|
||||||
|
GetMnemonicText(ins->Mnemonic, instruction);
|
||||||
|
|
||||||
|
printf("%s ", instruction);
|
||||||
|
|
||||||
|
if (!ins->ParameterOne) {
|
||||||
|
printf("\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ins->ParameterOne->ParameterType == 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->ParameterTwo) {
|
||||||
|
printf("\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(ins->ParameterTwo->ParameterType) {
|
||||||
|
case RegisterParameter:
|
||||||
|
GetRegisterText(ins->ParameterTwo->Value.Register, reg);
|
||||||
|
|
||||||
|
if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%s]\n", reg);
|
||||||
|
else printf(", %s\n", reg);
|
||||||
|
break;
|
||||||
|
case ConstantParameter:
|
||||||
|
if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%d]\n", ins->ParameterTwo->Value.Number);
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
//free(image);
|
//free(image);
|
||||||
//Disassemble(image);
|
//Disassemble(image);
|
||||||
//for(int i = 0; i < image->Opcodes->size; i++) {
|
//for(int i = 0; i < image->Opcodes->size; i++) {
|
||||||
|
|||||||
@@ -105,6 +105,15 @@ void GetMnemonicText(Mnemonic mnemonic, char buffer[12]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GetRegisterText(Registers reg, char buffer[3]) {
|
||||||
|
memset(buffer, '\0', 3);
|
||||||
|
|
||||||
|
if (reg < R1 || reg > R8) return;
|
||||||
|
|
||||||
|
buffer[0] = 'r';
|
||||||
|
buffer[1] = reg + 49;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NOP - 0000 0000 NOP
|
NOP - 0000 0000 NOP
|
||||||
JZ - 0000 0001 JZ Address
|
JZ - 0000 0001 JZ Address
|
||||||
|
|||||||
+17
-15
@@ -31,9 +31,21 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
HandleOperation();
|
HandleOperation();
|
||||||
break;
|
break;
|
||||||
case LabelClass:
|
case LabelClass:
|
||||||
AdvanceParser();
|
{
|
||||||
AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1;
|
Symbol* symbol = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable);
|
||||||
AdvanceParser(); //Consume the NewLine
|
|
||||||
|
if (symbol && symbol->Resolved) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
AdvanceParser(); //Consume the label token
|
||||||
|
|
||||||
|
if (!symbol) AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1;
|
||||||
|
else symbol->Resolved = 1;
|
||||||
|
|
||||||
|
AdvanceParser(); //Consume the NewLine
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "[Warning] Line %d: Syntax error, expected start of expression, got '%c' [%d].\n", t->LineNumber, t->Value.Punctuation, t->Class);
|
fprintf(stderr, "[Warning] Line %d: Syntax error, expected start of expression, got '%c' [%d].\n", t->LineNumber, t->Value.Punctuation, t->Class);
|
||||||
@@ -45,16 +57,6 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
if (MachineState.SymbolsTable->Size > 0) PrintSymbols();
|
if (MachineState.SymbolsTable->Size > 0) PrintSymbols();
|
||||||
|
|
||||||
for(int i = 0; i < MachineState.Instructions->size; i++) {
|
|
||||||
char mn[12];
|
|
||||||
|
|
||||||
Instruction* ins = MachineState.Instructions->content[i];
|
|
||||||
|
|
||||||
GetMnemonicText(ins->Mnemonic, mn);
|
|
||||||
|
|
||||||
printf("%s\n", mn);
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MachineState;
|
return &MachineState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,10 +75,10 @@ Parameter* GetParameterType() {
|
|||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
return param;
|
return param;
|
||||||
case LabelClass:
|
//case LabelClass:
|
||||||
case IdentifierClass:
|
case IdentifierClass:
|
||||||
param->ParameterType = AddressParameter;
|
param->ParameterType = AddressParameter;
|
||||||
param->InterpretedAs = AddressParameter;
|
param->InterpretedAs = ConstantParameter;
|
||||||
|
|
||||||
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user