An emulator, assembler, and disassembler for the Sega Game Gear
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

159 行
4.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 *asm_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 *asm_error_descs[] = {
  17. [ED_INC_BAD_ARG] = "missing or invalid argument",
  18. [ED_INC_RECURSION] = "infinite recursion detected",
  19. [ED_INC_FILE_READ] = "couldn't read included file",
  20. [ED_PP_UNKNOWN] = "unknown directive",
  21. [ED_PP_DUPLICATE] = "multiple values for directive",
  22. [ED_PP_NO_ARG] = "missing argument for directive",
  23. [ED_PP_BAD_ARG] = "invalid argument for directive",
  24. [ED_PP_ARG_RANGE] = "directive argument out of range",
  25. [ED_LYT_HEADER_RANGE] = "header offset exceeds given ROM size",
  26. [ED_LYT_DECL_RANGE] = "declared ROM size in header exceeds actual size",
  27. [ED_LYT_BOUNDS] = "location is out of bounds for the ROM size",
  28. [ED_LYT_BLOCK0] = "block zero cannot be mapped into a nonzero slot",
  29. [ED_LYT_SLOTS] = "multiple slot declarations for block directive",
  30. [ED_LYT_OVERLAP] = "location overlaps with instruction or data",
  31. [ED_LYT_OVERLAP_HEAD] = "location overlaps with ROM header",
  32. [ED_SYM_DUPE_LABELS] = "duplicate definitions for label",
  33. [ED_SYM_NO_LABEL] = "undefined reference to label",
  34. [ED_PARSE_SYNTAX] = "syntax error"
  35. };
  36. /* Internal structs */
  37. struct ASMErrorLine {
  38. char *data;
  39. size_t length;
  40. size_t lineno;
  41. char *filename;
  42. struct ASMErrorLine *next;
  43. };
  44. typedef struct ASMErrorLine ASMErrorLine;
  45. struct ErrorInfo {
  46. ASMErrorType type;
  47. ASMErrorDesc desc;
  48. ASMErrorLine *line;
  49. };
  50. /*
  51. Create an ASMErrorLine object from an ASMLine.
  52. */
  53. static ASMErrorLine* create_error_line(const ASMLine *line)
  54. {
  55. ASMErrorLine *el = malloc(sizeof(ASMErrorLine));
  56. if (!el)
  57. OUT_OF_MEMORY()
  58. const char *source = line->original->data;
  59. size_t length = line->original->length;
  60. if (!(el->data = malloc(sizeof(char) * length)))
  61. OUT_OF_MEMORY()
  62. // Ignore spaces at beginning:
  63. while (length > 0 && (*source == ' ' || *source == '\t'))
  64. source++, length--;
  65. memcpy(el->data, source, length);
  66. el->length = length;
  67. el->lineno = line->original->lineno;
  68. el->filename = strdup(line->filename);
  69. if (!el->filename)
  70. OUT_OF_MEMORY()
  71. el->next = NULL;
  72. return el;
  73. }
  74. /*
  75. Create an ErrorInfo object describing a particular error.
  76. The ErrorInfo object can be printed with error_info_print(), and must be
  77. freed when done with error_info_destroy().
  78. This function never fails (OOM triggers an exit()); the caller can be
  79. confident the returned object is valid.
  80. */
  81. ErrorInfo* error_info_create(
  82. const ASMLine *line, ASMErrorType err_type, ASMErrorDesc err_desc)
  83. {
  84. ErrorInfo *einfo = malloc(sizeof(ErrorInfo));
  85. if (!einfo)
  86. OUT_OF_MEMORY()
  87. einfo->type = err_type;
  88. einfo->desc = err_desc;
  89. einfo->line = line ? create_error_line(line) : NULL;
  90. return einfo;
  91. }
  92. /*
  93. Add an ASMLine to an ErrorInfo object, as part of a file trace.
  94. */
  95. void error_info_append(ErrorInfo *einfo, const ASMLine *line)
  96. {
  97. ASMErrorLine* el = create_error_line(line);
  98. el->next = einfo->line;
  99. einfo->line = el;
  100. }
  101. /*
  102. Print an ErrorInfo object returned by assemble() to the given stream.
  103. */
  104. void error_info_print(const ErrorInfo *einfo, FILE *file)
  105. {
  106. ASMErrorLine *line = einfo->line;
  107. fprintf(file, "error: %s: %s\n", asm_error_types[einfo->type],
  108. asm_error_descs[einfo->desc]);
  109. while (line) {
  110. fprintf(file, "%s:%zu:\n", line->filename, line->lineno);
  111. fprintf(file, " %.*s\n", (int) line->length, line->data);
  112. line = line->next;
  113. }
  114. }
  115. /*
  116. Destroy an ErrorInfo object created by assemble().
  117. */
  118. void error_info_destroy(ErrorInfo *error_info)
  119. {
  120. if (!error_info)
  121. return;
  122. ASMErrorLine *line = error_info->line, *temp;
  123. while (line) {
  124. temp = line->next;
  125. free(line->data);
  126. free(line->filename);
  127. free(line);
  128. line = temp;
  129. }
  130. free(error_info);
  131. }