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.
 
 
 
 
 

313 lines
9.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 <string.h>
  5. #include "tokenizer.h"
  6. #include "directives.h"
  7. #include "parse_util.h"
  8. #include "../logging.h"
  9. #include "../mmu.h"
  10. #include "../rom.h"
  11. /* Internal structs */
  12. typedef struct {
  13. int8_t slots[MMU_NUM_ROM_BANKS];
  14. const ASMLine *lines[MMU_NUM_ROM_BANKS];
  15. } ASMSlotInfo;
  16. /* Sentinel values for overlap table */
  17. const ASMLine header_sentinel, bounds_sentinel;
  18. /*
  19. Return the address of a given ROM offset when mapped into the given slot.
  20. */
  21. static inline uint16_t map_into_slot(size_t offset, int8_t slot)
  22. {
  23. return (slot * MMU_ROM_BANK_SIZE) + (offset & (MMU_ROM_BANK_SIZE - 1));
  24. }
  25. /*
  26. Return the default slot associated with a given memory bank.
  27. */
  28. static inline int8_t default_bank_slot(size_t bank)
  29. {
  30. return bank > 2 ? 2 : bank;
  31. }
  32. /*
  33. Add a given line, representing a label, to the symbol table.
  34. Return NULL on success and an ErrorInfo object on failure (in the case of
  35. duplicate labels).
  36. */
  37. static ErrorInfo* add_label_to_table(
  38. ASMSymbolTable *symtable, const ASMLine *line, size_t offset, int8_t slot)
  39. {
  40. char *symbol = strndup(line->data, line->length - 1);
  41. if (!symbol)
  42. OUT_OF_MEMORY()
  43. const ASMSymbol *current = asm_symtable_find(symtable, symbol);
  44. if (current) {
  45. ErrorInfo *ei = error_info_create(line, ET_SYMBOL, ED_SYM_DUPE_LABELS);
  46. error_info_append(ei, current->line);
  47. return ei;
  48. }
  49. ASMSymbol *label = malloc(sizeof(ASMSymbol));
  50. if (!label)
  51. OUT_OF_MEMORY()
  52. label->offset = map_into_slot(offset,
  53. (slot >= 0) ? slot : default_bank_slot(offset / MMU_ROM_BANK_SIZE));
  54. label->symbol = symbol;
  55. label->line = line;
  56. asm_symtable_insert(symtable, label);
  57. return NULL;
  58. }
  59. /*
  60. Handle an origin directive by updating the offset.
  61. Return NULL on success and an ErrorInfo object on failure.
  62. */
  63. static ErrorInfo* handle_origin_directive(const ASMLine *line, size_t *offset)
  64. {
  65. if (!DIRECTIVE_HAS_ARG(line, DIR_ORIGIN))
  66. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  67. uint32_t arg;
  68. if (!dparse_uint32_t(&arg, line, DIR_ORIGIN))
  69. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  70. if (arg >= MMU_NUM_ROM_BANKS * MMU_ROM_BANK_SIZE)
  71. return error_info_create(line, ET_PREPROC, ED_PP_ARG_RANGE);
  72. *offset = arg;
  73. return NULL;
  74. }
  75. /*
  76. Handle a block directive by updating the offset and slot.
  77. Return NULL on success and an ErrorInfo object on failure.
  78. */
  79. static ErrorInfo* handle_block_directive(
  80. const ASMLine *line, size_t *offset, ASMSlotInfo *si)
  81. {
  82. if (!DIRECTIVE_HAS_ARG(line, DIR_BLOCK))
  83. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  84. uint8_t *args, bank, slot;
  85. size_t dir_offset = DIRECTIVE_OFFSET(line, DIR_BLOCK) + 1, nargs;
  86. if (!parse_bytes(&args, &nargs, line->data + dir_offset,
  87. line->length - dir_offset))
  88. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  89. if (nargs < 1 || nargs > 2)
  90. return free(args), error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  91. bank = args[0];
  92. slot = nargs == 2 ? args[1] : default_bank_slot(bank);
  93. free(args);
  94. if (bank >= MMU_NUM_ROM_BANKS || slot >= MMU_NUM_SLOTS)
  95. return error_info_create(line, ET_PREPROC, ED_PP_ARG_RANGE);
  96. if (nargs == 2) {
  97. if (bank == 0 && slot != 0)
  98. return error_info_create(line, ET_LAYOUT, ED_LYT_BLOCK0);
  99. if (si->slots[bank] >= 0 && si->slots[bank] != slot) {
  100. ErrorInfo *ei = error_info_create(line, ET_LAYOUT, ED_LYT_SLOTS);
  101. error_info_append(ei, si->lines[bank]);
  102. return ei;
  103. }
  104. }
  105. *offset = bank * MMU_ROM_BANK_SIZE;
  106. si->slots[bank] = slot;
  107. if (!si->lines[bank])
  108. si->lines[bank] = line;
  109. return NULL;
  110. }
  111. /*
  112. Parse data encoded in a line into an ASMData object.
  113. On success, return NULL and store the instruction in *data_ptr. On failure,
  114. return an ErrorInfo object; *data_ptr is not modified.
  115. */
  116. static ErrorInfo* parse_data(
  117. const ASMLine *line, ASMData **data_ptr, size_t offset)
  118. {
  119. // TODO
  120. DEBUG("parse_data(): %.*s", (int) line->length, line->data)
  121. // return error_info_create(line, ET_PARSER, ED_PARSE_SYNTAX);
  122. ASMData *data = malloc(sizeof(ASMData));
  123. if (!data)
  124. OUT_OF_MEMORY()
  125. data->loc.offset = offset;
  126. data->loc.length = 6;
  127. data->data = (uint8_t*) strdup("foobar");
  128. data->next = NULL;
  129. *data_ptr = data;
  130. return NULL;
  131. }
  132. /*
  133. Parse an instruction encoded in a line into an ASMInstruction object.
  134. On success, return NULL and store the instruction in *inst_ptr. On failure,
  135. return an ErrorInfo object; *inst_ptr is not modified.
  136. */
  137. static ErrorInfo* parse_instruction(
  138. const ASMLine *line, ASMInstruction **inst_ptr, size_t offset)
  139. {
  140. // TODO
  141. DEBUG("parse_instruction(): %.*s", (int) line->length, line->data)
  142. // return error_info_create(line, ET_PARSER, ED_PARSE_SYNTAX);
  143. ASMInstruction *inst = malloc(sizeof(ASMInstruction));
  144. if (!inst)
  145. OUT_OF_MEMORY()
  146. inst->loc.offset = offset;
  147. inst->loc.length = 1;
  148. inst->b1 = 0x3C;
  149. inst->symbol = NULL;
  150. inst->line = line;
  151. inst->next = NULL;
  152. *inst_ptr = inst;
  153. return NULL;
  154. }
  155. /*
  156. Check if the given location overlaps with any existing objects.
  157. On success, return NULL and add the location to the overlap table.
  158. On failure, return an ErrorInfo object.
  159. */
  160. static ErrorInfo* check_layout(
  161. const ASMLine **overlap_table, size_t size, const ASMLocation *loc,
  162. const ASMLine *line, const ASMLine *origin)
  163. {
  164. // TODO: never let boundaries cross without state->cross_blocks
  165. const ASMLine *clash = NULL;
  166. if (loc->offset + loc->length > size) {
  167. clash = &bounds_sentinel;
  168. } else {
  169. for (size_t i = 0; i < loc->length; i++) {
  170. if (overlap_table[loc->offset + i]) {
  171. clash = overlap_table[loc->offset + i];
  172. break;
  173. }
  174. }
  175. }
  176. if (clash) {
  177. ErrorInfo *ei = error_info_create(line, ET_LAYOUT,
  178. (clash == &header_sentinel) ? ED_LYT_OVERLAP_HEAD :
  179. (clash == &bounds_sentinel) ? ED_LYT_BOUNDS : ED_LYT_OVERLAP);
  180. if (origin)
  181. error_info_append(ei, origin);
  182. if (clash != &header_sentinel && clash != &bounds_sentinel)
  183. error_info_append(ei, clash);
  184. return ei;
  185. }
  186. for (size_t i = 0; i < loc->length; i++)
  187. overlap_table[loc->offset + i] = line;
  188. return NULL;
  189. }
  190. /*
  191. Tokenize ASMLines into ASMInstructions and ASMData.
  192. NULL is returned on success and an ErrorInfo object is returned on failure.
  193. state->instructions, state->data, and state->symtable may or may not be
  194. modified regardless of success.
  195. */
  196. ErrorInfo* tokenize(AssemblerState *state)
  197. {
  198. size_t size = state->rom_size ? state->rom_size : ROM_SIZE_MAX;
  199. const ASMLine **overlap_table = calloc(size, sizeof(const ASMLine*));
  200. if (!overlap_table)
  201. OUT_OF_MEMORY()
  202. ErrorInfo *ei = NULL;
  203. ASMInstruction dummy_inst = {.next = NULL}, *inst, *prev_inst = &dummy_inst;
  204. ASMData dummy_data = {.next = NULL}, *data, *prev_data = &dummy_data;
  205. const ASMLine *line = state->lines, *origin = NULL;
  206. size_t offset = 0;
  207. ASMSlotInfo si = {.lines = {0}};
  208. for (size_t i = 0; i < HEADER_SIZE; i++)
  209. overlap_table[state->header.offset + i] = &header_sentinel;
  210. memset(si.slots, -1, MMU_NUM_ROM_BANKS);
  211. while (line) {
  212. if (line->is_label) {
  213. if (offset >= size) {
  214. ei = error_info_create(line, ET_LAYOUT, ED_LYT_BOUNDS);
  215. goto cleanup;
  216. }
  217. int8_t slot = si.slots[offset / MMU_NUM_ROM_BANKS];
  218. if ((ei = add_label_to_table(state->symtable, line, offset, slot)))
  219. goto cleanup;
  220. }
  221. else if (IS_LOCAL_DIRECTIVE(line)) {
  222. if (IS_DIRECTIVE(line, DIR_ORIGIN)) {
  223. if ((ei = handle_origin_directive(line, &offset)))
  224. goto cleanup;
  225. origin = line;
  226. }
  227. else if (IS_DIRECTIVE(line, DIR_BLOCK)) {
  228. if ((ei = handle_block_directive(line, &offset, &si)))
  229. goto cleanup;
  230. origin = line;
  231. }
  232. else {
  233. if ((ei = parse_data(line, &data, offset)))
  234. goto cleanup;
  235. offset += data->loc.length;
  236. prev_data->next = data;
  237. prev_data = data;
  238. if ((ei = check_layout(overlap_table, size, &data->loc, line, origin)))
  239. goto cleanup;
  240. }
  241. }
  242. else {
  243. if ((ei = parse_instruction(line, &inst, offset)))
  244. goto cleanup;
  245. offset += inst->loc.length;
  246. prev_inst->next = inst;
  247. prev_inst = inst;
  248. if ((ei = check_layout(overlap_table, size, &inst->loc, line, origin)))
  249. goto cleanup;
  250. }
  251. line = line->next;
  252. }
  253. cleanup:
  254. state->instructions = dummy_inst.next;
  255. state->data = dummy_data.next;
  256. free(overlap_table);
  257. return ei;
  258. }