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 IsRegister(const char*, Registers*);
|
||||
void GetMnemonicText(Mnemonic mnemonic, char buffer[12]);
|
||||
void GetRegisterText(Registers reg, char buffer[3]);
|
||||
unsigned char GetInstructionMask(Mnemonic type);
|
||||
|
||||
#endif
|
||||
+105
-45
@@ -20,67 +20,127 @@ int main(int argc, char* args[]) {
|
||||
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
||||
|
||||
List* list = GenerateTokenList(source_code);
|
||||
char mnemonic[12];
|
||||
// char mnemonic[12];
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
Token* t = (Token*) list->content[i];
|
||||
// for(int i = 0; i < list->size; i++) {
|
||||
// Token* t = (Token*) list->content[i];
|
||||
|
||||
if (t->EndOfFile) {
|
||||
printf("EOF\n");
|
||||
break;
|
||||
}
|
||||
// if (t->EndOfFile) {
|
||||
// printf("EOF\n");
|
||||
// break;
|
||||
// }
|
||||
|
||||
if (t->Class == PunctuationClass){
|
||||
if(t->Value.Punctuation == NewLine) {
|
||||
printf("\n");
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == PunctuationClass){
|
||||
// if(t->Value.Punctuation == NewLine) {
|
||||
// printf("\n");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
printf("<%d>", t->LineNumber);
|
||||
// printf("<%d>", t->LineNumber);
|
||||
|
||||
printf("%c ", t->Value.Punctuation);
|
||||
continue;
|
||||
}
|
||||
// printf("%c ", t->Value.Punctuation);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
printf("<%d>", t->LineNumber);
|
||||
// printf("<%d>", t->LineNumber);
|
||||
|
||||
if (t->Class == LabelClass) {
|
||||
printf("[L]%s* ", t->Lemexe);
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == LabelClass) {
|
||||
// printf("[L]%s* ", t->Lemexe);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (t->Class == IdentifierClass) {
|
||||
printf("[I]%s ", t->Lemexe);
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == IdentifierClass) {
|
||||
// printf("[I]%s ", t->Lemexe);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (t->Class == RegisterClass) {
|
||||
printf("[R]%d", t->Value.Register);
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == RegisterClass) {
|
||||
// printf("[R]%d", t->Value.Register);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (t->Class == NumberClass) {
|
||||
printf("[N]%d ", t->Value.Number);
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == NumberClass) {
|
||||
// printf("[N]%d ", t->Value.Number);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (t->Class == MnemonicClass) {
|
||||
GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
||||
printf("[M]%s ", mnemonic);
|
||||
continue;
|
||||
}
|
||||
// if (t->Class == MnemonicClass) {
|
||||
// GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
||||
// printf("[M]%s ", mnemonic);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (t->Class == DirectiveClass) {
|
||||
printf("[D]%d ", t->Value.Directive);
|
||||
// if (t->Class == DirectiveClass) {
|
||||
// printf("[D]%d ", t->Value.Directive);
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
if (t->Class == CharacterClass) {
|
||||
printf("%s ", t->Lemexe);
|
||||
}
|
||||
}
|
||||
// if (t->Class == CharacterClass) {
|
||||
// printf("%s ", t->Lemexe);
|
||||
// }
|
||||
// }
|
||||
|
||||
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);
|
||||
//Disassemble(image);
|
||||
//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
|
||||
JZ - 0000 0001 JZ Address
|
||||
|
||||
+17
-15
@@ -31,9 +31,21 @@ IRState* ParseTokens(List* tokens) {
|
||||
HandleOperation();
|
||||
break;
|
||||
case LabelClass:
|
||||
AdvanceParser();
|
||||
AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1;
|
||||
AdvanceParser(); //Consume the NewLine
|
||||
{
|
||||
Symbol* symbol = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable);
|
||||
|
||||
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;
|
||||
default:
|
||||
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();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -73,10 +75,10 @@ Parameter* GetParameterType() {
|
||||
AdvanceParser();
|
||||
|
||||
return param;
|
||||
case LabelClass:
|
||||
//case LabelClass:
|
||||
case IdentifierClass:
|
||||
param->ParameterType = AddressParameter;
|
||||
param->InterpretedAs = AddressParameter;
|
||||
param->InterpretedAs = ConstantParameter;
|
||||
|
||||
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user