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