An emulator, assembler, and disassembler for the Sega Game Gear
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

175 linhas
5.4 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdlib.h>
  4. #include "errors.h"
  5. #include "state.h"
  6. #include "../assembler.h"
  7. #include "../logging.h"
  8. /* Error strings */
  9. static const char *error_types[] = {
  10. [ET_INCLUDE] = "include directive",
  11. [ET_PREPROC] = "preprocessor",
  12. [ET_LAYOUT] = "memory layout",
  13. [ET_SYMBOL] = "symbol table",
  14. [ET_PARSER] = "instruction parser"
  15. };
  16. static const char *error_descs[] = {
  17. [ED_NONE] = "undefined error",
  18. [ED_INC_BAD_ARG] = "missing or invalid argument",
  19. [ED_INC_DEPTH] = "maximum include depth exceeded",
  20. [ED_INC_FILE_READ] = "couldn't read included file",
  21. [ED_PP_UNKNOWN] = "unknown directive",
  22. [ED_PP_DUPLICATE] = "multiple values for directive",
  23. [ED_PP_NO_ARG] = "missing argument for directive",
  24. [ED_PP_BAD_ARG] = "invalid argument for directive",
  25. [ED_PP_ARG_RANGE] = "directive argument out of range",
  26. [ED_LYT_HEADER_RANGE] = "header offset exceeds given ROM size",
  27. [ED_LYT_DECL_RANGE] = "declared ROM size in header exceeds actual size",
  28. [ED_LYT_BOUNDS] = "location is out of bounds for the ROM size",
  29. [ED_LYT_BLOCK0] = "block zero cannot be mapped into a nonzero slot",
  30. [ED_LYT_SLOTS] = "multiple slot declarations for block directive",
  31. [ED_LYT_BLOCK_CROSS] = "instruction or data extends past block boundary",
  32. [ED_LYT_OVERLAP] = "multiple instructions/data occupy same location",
  33. [ED_LYT_OVERLAP_HEAD] = "location overlaps with ROM header",
  34. [ED_SYM_DUPE_LABELS] = "duplicate definitions for label",
  35. [ED_SYM_NO_LABEL] = "undefined reference to label",
  36. [ED_SYM_IS_REGISTER] = "labels cannot share names with registers",
  37. [ED_SYM_IS_CONDITION] = "labels cannot share names with condition codes",
  38. [ED_PS_OP_TOO_LONG] = "opcode mnemonic is too long (2-4 characters)",
  39. [ED_PS_OP_TOO_SHORT] = "opcode mnemonic is too short (2-4 characters)",
  40. [ED_PS_OP_INVALID] = "invalid characters in opcode mnemonic",
  41. [ED_PS_OP_UNKNOWN] = "unknown opcode mnemonic",
  42. [ED_PS_TOO_FEW_ARGS] = "too few arguments for opcode",
  43. [ED_PS_TOO_MANY_ARGS] = "too many arguments for opcode",
  44. [ED_PS_ARG_SYNTAX] = "invalid syntax in argument(s)",
  45. [ED_PS_ARG0_TYPE] = "invalid type for first argument",
  46. [ED_PS_ARG0_BAD_REG] = "unsupported register as first argument",
  47. [ED_PS_ARG1_TYPE] = "invalid type for second argument",
  48. [ED_PS_ARG1_BAD_REG] = "unsupported register as second argument",
  49. [ED_PS_ARG1_RANGE] = "second argument out of range"
  50. };
  51. /* Internal structs */
  52. struct ASMErrorLine {
  53. char *data;
  54. size_t length;
  55. size_t lineno;
  56. char *filename;
  57. struct ASMErrorLine *next;
  58. };
  59. typedef struct ASMErrorLine ASMErrorLine;
  60. struct ErrorInfo {
  61. ASMErrorType type;
  62. ASMErrorDesc desc;
  63. ASMErrorLine *line;
  64. };
  65. /*
  66. Create an ASMErrorLine object from an ASMLine.
  67. */
  68. static ASMErrorLine* create_error_line(const ASMLine *line)
  69. {
  70. ASMErrorLine *el = malloc(sizeof(ASMErrorLine));
  71. if (!el)
  72. OUT_OF_MEMORY()
  73. const char *source = line->original->data;
  74. size_t length = line->original->length;
  75. if (!(el->data = malloc(sizeof(char) * length)))
  76. OUT_OF_MEMORY()
  77. // Ignore spaces at beginning:
  78. while (length > 0 && (*source == ' ' || *source == '\t'))
  79. source++, length--;
  80. memcpy(el->data, source, length);
  81. el->length = length;
  82. el->lineno = line->original->lineno;
  83. el->filename = strdup(line->filename);
  84. if (!el->filename)
  85. OUT_OF_MEMORY()
  86. el->next = NULL;
  87. return el;
  88. }
  89. /*
  90. Create an ErrorInfo object describing a particular error.
  91. The ErrorInfo object can be printed with error_info_print(), and must be
  92. freed when done with error_info_destroy().
  93. This function never fails (OOM triggers an exit()); the caller can be
  94. confident the returned object is valid.
  95. */
  96. ErrorInfo* error_info_create(
  97. const ASMLine *line, ASMErrorType err_type, ASMErrorDesc err_desc)
  98. {
  99. ErrorInfo *einfo = malloc(sizeof(ErrorInfo));
  100. if (!einfo)
  101. OUT_OF_MEMORY()
  102. einfo->type = err_type;
  103. einfo->desc = err_desc;
  104. einfo->line = line ? create_error_line(line) : NULL;
  105. return einfo;
  106. }
  107. /*
  108. Add an ASMLine to an ErrorInfo object, as part of a file trace.
  109. */
  110. void error_info_append(ErrorInfo *einfo, const ASMLine *line)
  111. {
  112. ASMErrorLine* el = create_error_line(line);
  113. el->next = einfo->line;
  114. einfo->line = el;
  115. }
  116. /*
  117. Print an ErrorInfo object returned by assemble() to the given stream.
  118. */
  119. void error_info_print(const ErrorInfo *einfo, FILE *file)
  120. {
  121. ASMErrorLine *line = einfo->line;
  122. fprintf(file, "error: %s: %s\n", error_types[einfo->type],
  123. error_descs[einfo->desc]);
  124. while (line) {
  125. fprintf(file, "%s:%zu:\n", line->filename, line->lineno);
  126. fprintf(file, " %.*s\n", (int) line->length, line->data);
  127. line = line->next;
  128. }
  129. }
  130. /*
  131. Destroy an ErrorInfo object created by assemble().
  132. */
  133. void error_info_destroy(ErrorInfo *error_info)
  134. {
  135. if (!error_info)
  136. return;
  137. ASMErrorLine *line = error_info->line, *temp;
  138. while (line) {
  139. temp = line->next;
  140. free(line->data);
  141. free(line->filename);
  142. free(line);
  143. line = temp;
  144. }
  145. free(error_info);
  146. }