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.
 
 
 
 
 

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