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.
 
 
 
 
 

165 linhas
4.7 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_PARSE_OP_LONG] = "opcode mnemonic is too long (2-4 characters)",
  37. [ED_PARSE_OP_SHORT] = "opcode mnemonic is too short (2-4 characters)",
  38. [ED_PARSE_OP_CHARS] = "invalid characters in opcode mnemonic",
  39. [ED_PARSE_OP_UNKNOWN] = "unknown opcode mnemonic"
  40. };
  41. /* Internal structs */
  42. struct ASMErrorLine {
  43. char *data;
  44. size_t length;
  45. size_t lineno;
  46. char *filename;
  47. struct ASMErrorLine *next;
  48. };
  49. typedef struct ASMErrorLine ASMErrorLine;
  50. struct ErrorInfo {
  51. ASMErrorType type;
  52. ASMErrorDesc desc;
  53. ASMErrorLine *line;
  54. };
  55. /*
  56. Create an ASMErrorLine object from an ASMLine.
  57. */
  58. static ASMErrorLine* create_error_line(const ASMLine *line)
  59. {
  60. ASMErrorLine *el = malloc(sizeof(ASMErrorLine));
  61. if (!el)
  62. OUT_OF_MEMORY()
  63. const char *source = line->original->data;
  64. size_t length = line->original->length;
  65. if (!(el->data = malloc(sizeof(char) * length)))
  66. OUT_OF_MEMORY()
  67. // Ignore spaces at beginning:
  68. while (length > 0 && (*source == ' ' || *source == '\t'))
  69. source++, length--;
  70. memcpy(el->data, source, length);
  71. el->length = length;
  72. el->lineno = line->original->lineno;
  73. el->filename = strdup(line->filename);
  74. if (!el->filename)
  75. OUT_OF_MEMORY()
  76. el->next = NULL;
  77. return el;
  78. }
  79. /*
  80. Create an ErrorInfo object describing a particular error.
  81. The ErrorInfo object can be printed with error_info_print(), and must be
  82. freed when done with error_info_destroy().
  83. This function never fails (OOM triggers an exit()); the caller can be
  84. confident the returned object is valid.
  85. */
  86. ErrorInfo* error_info_create(
  87. const ASMLine *line, ASMErrorType err_type, ASMErrorDesc err_desc)
  88. {
  89. ErrorInfo *einfo = malloc(sizeof(ErrorInfo));
  90. if (!einfo)
  91. OUT_OF_MEMORY()
  92. einfo->type = err_type;
  93. einfo->desc = err_desc;
  94. einfo->line = line ? create_error_line(line) : NULL;
  95. return einfo;
  96. }
  97. /*
  98. Add an ASMLine to an ErrorInfo object, as part of a file trace.
  99. */
  100. void error_info_append(ErrorInfo *einfo, const ASMLine *line)
  101. {
  102. ASMErrorLine* el = create_error_line(line);
  103. el->next = einfo->line;
  104. einfo->line = el;
  105. }
  106. /*
  107. Print an ErrorInfo object returned by assemble() to the given stream.
  108. */
  109. void error_info_print(const ErrorInfo *einfo, FILE *file)
  110. {
  111. ASMErrorLine *line = einfo->line;
  112. fprintf(file, "error: %s: %s\n", error_types[einfo->type],
  113. error_descs[einfo->desc]);
  114. while (line) {
  115. fprintf(file, "%s:%zu:\n", line->filename, line->lineno);
  116. fprintf(file, " %.*s\n", (int) line->length, line->data);
  117. line = line->next;
  118. }
  119. }
  120. /*
  121. Destroy an ErrorInfo object created by assemble().
  122. */
  123. void error_info_destroy(ErrorInfo *error_info)
  124. {
  125. if (!error_info)
  126. return;
  127. ASMErrorLine *line = error_info->line, *temp;
  128. while (line) {
  129. temp = line->next;
  130. free(line->data);
  131. free(line->filename);
  132. free(line);
  133. line = temp;
  134. }
  135. free(error_info);
  136. }