Correct literal format

This commit is contained in:
hiperiondev
2023-08-22 19:31:06 -03:00
parent 6b8e95d250
commit 52048f2466

View File

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