An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

160 rivejä
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 *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_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_BLOCK_CROSS] = "instruction or data extends past block boundary",
  31. [ED_LYT_OVERLAP] = "multiple instructions/data occupy same location",
  32. [ED_LYT_OVERLAP_HEAD] = "location overlaps with ROM header",
  33. [ED_SYM_DUPE_LABELS] = "duplicate definitions for label",
  34. [ED_SYM_NO_LABEL] = "undefined reference to label",
  35. [ED_PARSE_SYNTAX] = "syntax error"
  36. };
  37. /* Internal structs */
  38. struct ASMErrorLine {
  39. char *data;
  40. size_t length;
  41. size_t lineno;
  42. char *filename;
  43. struct ASMErrorLine *next;
  44. };
  45. typedef struct ASMErrorLine ASMErrorLine;
  46. struct ErrorInfo {
  47. ASMErrorType type;
  48. ASMErrorDesc desc;
  49. ASMErrorLine *line;
  50. };
  51. /*
  52. Create an ASMErrorLine object from an ASMLine.
  53. */
  54. static ASMErrorLine* create_error_line(const ASMLine *line)
  55. {
  56. ASMErrorLine *el = malloc(sizeof(ASMErrorLine));
  57. if (!el)
  58. OUT_OF_MEMORY()
  59. const char *source = line->original->data;
  60. size_t length = line->original->length;
  61. if (!(el->data = malloc(sizeof(char) * length)))
  62. OUT_OF_MEMORY()
  63. // Ignore spaces at beginning:
  64. while (length > 0 && (*source == ' ' || *source == '\t'))
  65. source++, length--;
  66. memcpy(el->data, source, length);
  67. el->length = length;
  68. el->lineno = line->original->lineno;
  69. el->filename = strdup(line->filename);
  70. if (!el->filename)
  71. OUT_OF_MEMORY()
  72. el->next = NULL;
  73. return el;
  74. }
  75. /*
  76. Create an ErrorInfo object describing a particular error.
  77. The ErrorInfo object can be printed with error_info_print(), and must be
  78. freed when done with error_info_destroy().
  79. This function never fails (OOM triggers an exit()); the caller can be
  80. confident the returned object is valid.
  81. */
  82. ErrorInfo* error_info_create(
  83. const ASMLine *line, ASMErrorType err_type, ASMErrorDesc err_desc)
  84. {
  85. ErrorInfo *einfo = malloc(sizeof(ErrorInfo));
  86. if (!einfo)
  87. OUT_OF_MEMORY()
  88. einfo->type = err_type;
  89. einfo->desc = err_desc;
  90. einfo->line = line ? create_error_line(line) : NULL;
  91. return einfo;
  92. }
  93. /*
  94. Add an ASMLine to an ErrorInfo object, as part of a file trace.
  95. */
  96. void error_info_append(ErrorInfo *einfo, const ASMLine *line)
  97. {
  98. ASMErrorLine* el = create_error_line(line);
  99. el->next = einfo->line;
  100. einfo->line = el;
  101. }
  102. /*
  103. Print an ErrorInfo object returned by assemble() to the given stream.
  104. */
  105. void error_info_print(const ErrorInfo *einfo, FILE *file)
  106. {
  107. ASMErrorLine *line = einfo->line;
  108. fprintf(file, "error: %s: %s\n", error_types[einfo->type],
  109. error_descs[einfo->desc]);
  110. while (line) {
  111. fprintf(file, "%s:%zu:\n", line->filename, line->lineno);
  112. fprintf(file, " %.*s\n", (int) line->length, line->data);
  113. line = line->next;
  114. }
  115. }
  116. /*
  117. Destroy an ErrorInfo object created by assemble().
  118. */
  119. void error_info_destroy(ErrorInfo *error_info)
  120. {
  121. if (!error_info)
  122. return;
  123. ASMErrorLine *line = error_info->line, *temp;
  124. while (line) {
  125. temp = line->next;
  126. free(line->data);
  127. free(line->filename);
  128. free(line);
  129. line = temp;
  130. }
  131. free(error_info);
  132. }