mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Correct literal format
This commit is contained in:
@@ -231,7 +231,7 @@ static void dis_disassembler_deinit(dis_program_t **prg) {
|
||||
free((*prg));
|
||||
}
|
||||
|
||||
static uint8_t dis_load_file(const char *filename, dis_program_t **prg) {
|
||||
static uint8_t dis_load_file(const char *filename, dis_program_t **prg, bool alt_fmt) {
|
||||
FILE *f;
|
||||
size_t fsize, bytes;
|
||||
uint32_t count = 0;
|
||||
@@ -252,18 +252,26 @@ static uint8_t dis_load_file(const char *filename, dis_program_t **prg) {
|
||||
(*prg)->program[count++] = buf;
|
||||
|
||||
(*prg)->len = fsize;
|
||||
|
||||
if (!alt_fmt)
|
||||
printf("\nFile: %s\nSize: %zu\n", filename, fsize);
|
||||
else
|
||||
printf("\n.comment File: %s, Size: %zu\n", filename, fsize);
|
||||
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void dis_read_header(dis_program_t **prg) {
|
||||
static void dis_read_header(dis_program_t **prg, bool alt_fmt) {
|
||||
const unsigned char major = readByte((*prg)->program, &((*prg)->pc));
|
||||
const unsigned char minor = readByte((*prg)->program, &((*prg)->pc));
|
||||
const unsigned char patch = readByte((*prg)->program, &((*prg)->pc));
|
||||
const char *build = readString((*prg)->program, &((*prg)->pc));
|
||||
|
||||
if (!alt_fmt)
|
||||
printf("[Header Version: %d.%d.%d (%s)]\n", major, minor, patch, build);
|
||||
else
|
||||
printf(".comment Header Version: %d.%d.%d (%s)\n", major, minor, patch, build);
|
||||
}
|
||||
|
||||
static void dis_print_opcode(uint8_t op) {
|
||||
@@ -309,8 +317,7 @@ static void dis_print_opcode(uint8_t op) {
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
static void dis_disassemble_section(dis_program_t **prg, uint32_t pc,
|
||||
uint32_t len, uint8_t spaces, bool is_function, bool alt_fmt) {
|
||||
static void dis_disassemble_section(dis_program_t **prg, uint32_t pc, uint32_t len, uint8_t spaces, bool is_function, bool alt_fmt) {
|
||||
uint8_t opcode;
|
||||
uint32_t uint;
|
||||
int32_t intg;
|
||||
@@ -341,10 +348,12 @@ static void dis_disassemble_section(dis_program_t **prg, uint32_t pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| ");
|
||||
} else
|
||||
printf(" ");
|
||||
|
||||
printf("[%05d](%03d) ", (pc++) - pc_start, opcode);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf("[%05d] ", (pc++) - pc_start);
|
||||
}
|
||||
|
||||
dis_print_opcode(opcode);
|
||||
|
||||
if (opcode > DIS_OP_END_OPCODES)
|
||||
@@ -356,8 +365,7 @@ static void dis_disassemble_section(dis_program_t **prg, uint32_t pc,
|
||||
}
|
||||
|
||||
#define LIT_ADD(a, b, c) b[c] = a; ++c;
|
||||
static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
uint8_t spaces, char *tree, bool alt_fmt) {
|
||||
static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc, uint8_t spaces, char *tree, bool alt_fmt) {
|
||||
uint32_t literal_count = 0;
|
||||
uint8_t literal_type[65536];
|
||||
|
||||
@@ -380,9 +388,12 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( null )\n", i);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit NULL\n");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DIS_LITERAL_BOOLEAN: {
|
||||
@@ -391,9 +402,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( boolean %s )\n", i, b ? "true" : "false");
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit BOOLEAN %s\n", b ? "true" : "false");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -403,9 +416,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( integer %d )\n", i, d);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit INTEGER %d\n", d);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -415,9 +430,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( float %f )\n", i, f);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit FLOAT %f\n", f);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -427,9 +444,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( string \"%s\" )\n", i, s);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit STRING \"%s\"\n", s);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -439,9 +458,12 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( array ", i);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit ARRAY ");
|
||||
}
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int index = readWord((*prg)->program, pc);
|
||||
printf("%d ", index);
|
||||
@@ -456,7 +478,10 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf(")\n");
|
||||
if (!alt_fmt)
|
||||
printf(")");
|
||||
printf("\n");
|
||||
|
||||
LIT_ADD(DIS_LITERAL_ARRAY, literal_type, literal_count);
|
||||
}
|
||||
break;
|
||||
@@ -467,13 +492,20 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( dictionary ", i);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit DICTIONARY ");
|
||||
}
|
||||
for (int i = 0; i < length / 2; i++) {
|
||||
int key = readWord((*prg)->program, pc);
|
||||
int val = readWord((*prg)->program, pc);
|
||||
|
||||
if (!alt_fmt)
|
||||
printf("(key: %d, val:%d) ", key, val);
|
||||
else
|
||||
printf("%d,%d ", key, val);
|
||||
|
||||
if (!(i % 5) && i != 0) {
|
||||
printf("\n");
|
||||
if (!alt_fmt) {
|
||||
@@ -484,21 +516,24 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf(")\n");
|
||||
if (!alt_fmt)
|
||||
printf(")");
|
||||
printf("\n");
|
||||
LIT_ADD(DIS_LITERAL_DICTIONARY, literal_type, literal_count);
|
||||
}
|
||||
break;
|
||||
|
||||
case DIS_LITERAL_FUNCTION: {
|
||||
unsigned short index = readWord((*prg)->program, pc);
|
||||
LIT_ADD(DIS_LITERAL_FUNCTION_INTERMEDIATE, literal_type,
|
||||
literal_count);
|
||||
LIT_ADD(DIS_LITERAL_FUNCTION_INTERMEDIATE, literal_type, literal_count);
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( function index: %d )\n", i, index);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit FUNCTION %d\n", index);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -508,9 +543,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( identifier %s )\n", i, str);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit IDENTIFIER %s\n", str);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -521,19 +558,24 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf("[%05d] ( type %s: %d)\n", i, (LIT_STR[literalType] + 12), constant);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf("[%05d] ( type %s: %d)\n", i, (LIT_STR[literalType] + 12),
|
||||
constant);
|
||||
printf(".lit TYPE %s %d", (LIT_STR[literalType] + 12), constant);
|
||||
}
|
||||
|
||||
if (literalType == DIS_LITERAL_ARRAY) {
|
||||
uint16_t vt = readWord((*prg)->program, pc);
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf("\n ( subtype: %d)", vt);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(" ( subtype: %d)\n", vt);
|
||||
printf(" SUBTYPE %d", vt);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if (literalType == DIS_LITERAL_DICTIONARY) {
|
||||
uint8_t kt = readWord((*prg)->program, pc);
|
||||
@@ -541,10 +583,13 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf("\n ( subtype: [%d, %d] )", kt, vt);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(" ( subtype: [%d, %d] )\n", kt, vt);
|
||||
printf(" SUBTYPE %d, %d", kt, vt);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
LIT_ADD(literalType, literal_type, literal_count);
|
||||
}
|
||||
break;
|
||||
@@ -554,9 +599,11 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
if (!alt_fmt) {
|
||||
SPC(spaces);
|
||||
printf("| | ");
|
||||
} else
|
||||
printf(" ");
|
||||
printf("[%05d] ( blank )\n", i);
|
||||
} else {
|
||||
printf(" ");
|
||||
printf(".lit BLANK\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -569,7 +616,6 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
printf("--- ( end literal section ) ---\n");
|
||||
}
|
||||
|
||||
|
||||
int functionCount = readWord((*prg)->program, pc);
|
||||
int functionSize = readWord((*prg)->program, pc);
|
||||
|
||||
@@ -612,8 +658,6 @@ static void dis_read_interpreter_sections(dis_program_t **prg, uint32_t *pc,
|
||||
} else
|
||||
printf("\nLIT_FUN_%s:", tree_local);
|
||||
|
||||
|
||||
|
||||
if ((*prg)->program[*pc + size - 1] != DIS_OP_FN_END) {
|
||||
printf("\nERROR: Failed to find function end\n");
|
||||
exit(1);
|
||||
@@ -666,10 +710,10 @@ void disassemble(const char *filename, bool alt_fmt) {
|
||||
queue_rear = NULL;
|
||||
|
||||
dis_disassembler_init(&prg);
|
||||
if (dis_load_file(filename, &prg))
|
||||
if (dis_load_file(filename, &prg, alt_fmt))
|
||||
exit(1);
|
||||
|
||||
dis_read_header(&prg);
|
||||
dis_read_header(&prg, alt_fmt);
|
||||
|
||||
consumeByte(DIS_OP_SECTION_END, prg->program, &(prg->pc));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user