An emulator, assembler, and disassembler for the Sega Game Gear
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

432 líneas
13 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 "instructions.h"
  8. #include "inst_args.h"
  9. #include "parse_util.h"
  10. #include "../logging.h"
  11. #include "../mmu.h"
  12. #include "../rom.h"
  13. /* Internal structs */
  14. typedef struct {
  15. size_t size;
  16. const ASMLine **overlap_table;
  17. const ASMLine **overlap_origins;
  18. const ASMLine *origin;
  19. uint8_t bank;
  20. bool cross_blocks;
  21. } ASMLayoutInfo;
  22. typedef struct {
  23. int8_t slots[MMU_NUM_ROM_BANKS];
  24. const ASMLine *lines[MMU_NUM_ROM_BANKS];
  25. } ASMSlotInfo;
  26. /* Sentinel values for overlap table */
  27. const ASMLine header_sentinel, bounds_sentinel;
  28. /* Typedef for parse_util data parser functions */
  29. typedef bool (*parser_func)(uint8_t**, size_t*, const char*, ssize_t);
  30. /*
  31. Return the address of a given ROM offset when mapped into the given slot.
  32. */
  33. static inline uint16_t map_into_slot(size_t offset, int8_t slot)
  34. {
  35. return (slot * MMU_ROM_BANK_SIZE) + (offset & (MMU_ROM_BANK_SIZE - 1));
  36. }
  37. /*
  38. Return the default slot associated with a given memory bank.
  39. */
  40. static inline int8_t default_bank_slot(uint8_t bank)
  41. {
  42. return bank > 2 ? 2 : bank;
  43. }
  44. /*
  45. Add a given line, representing a label, to the symbol table.
  46. Return NULL on success and an ErrorInfo object on failure (e.g. in the case
  47. of duplicate labels, or labels sharing names with registers/conditions).
  48. */
  49. static ErrorInfo* add_label_to_table(
  50. ASMSymbolTable *symtable, const ASMLine *line, size_t offset, int8_t slot)
  51. {
  52. ASMArgRegister reg;
  53. if (parse_register(&reg, line->data, line->length - 1))
  54. return error_info_create(line, ET_SYMBOL, ED_SYM_IS_REGISTER);
  55. ASMArgCondition cond;
  56. if (parse_condition(&cond, line->data, line->length - 1))
  57. return error_info_create(line, ET_SYMBOL, ED_SYM_IS_CONDITION);
  58. char *symbol = strndup(line->data, line->length - 1);
  59. if (!symbol)
  60. OUT_OF_MEMORY()
  61. const ASMSymbol *current = asm_symtable_find(symtable, symbol);
  62. if (current) {
  63. ErrorInfo *ei = error_info_create(line, ET_SYMBOL, ED_SYM_DUPE_LABELS);
  64. error_info_append(ei, current->line);
  65. free(symbol);
  66. return ei;
  67. }
  68. ASMSymbol *label = malloc(sizeof(ASMSymbol));
  69. if (!label)
  70. OUT_OF_MEMORY()
  71. label->offset = map_into_slot(offset,
  72. (slot >= 0) ? slot : default_bank_slot(offset / MMU_ROM_BANK_SIZE));
  73. label->symbol = symbol;
  74. label->line = line;
  75. asm_symtable_insert(symtable, label);
  76. return NULL;
  77. }
  78. /*
  79. Handle an origin directive by updating the offset.
  80. Return NULL on success and an ErrorInfo object on failure.
  81. */
  82. static ErrorInfo* handle_origin_directive(const ASMLine *line, size_t *offset)
  83. {
  84. if (!DIRECTIVE_HAS_ARG(line, DIR_ORIGIN))
  85. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  86. uint32_t arg;
  87. if (!dparse_uint32_t(&arg, line, DIR_ORIGIN))
  88. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  89. if (arg >= ROM_SIZE_MAX)
  90. return error_info_create(line, ET_PREPROC, ED_PP_ARG_RANGE);
  91. *offset = arg;
  92. return NULL;
  93. }
  94. /*
  95. Handle a block directive by updating the offset and slot.
  96. Return NULL on success and an ErrorInfo object on failure.
  97. */
  98. static ErrorInfo* handle_block_directive(
  99. const ASMLine *line, size_t *offset, ASMSlotInfo *si)
  100. {
  101. if (!DIRECTIVE_HAS_ARG(line, DIR_BLOCK))
  102. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  103. uint8_t *args, bank, slot;
  104. size_t dir_offset = DIRECTIVE_OFFSET(line, DIR_BLOCK) + 1, nargs;
  105. if (!parse_bytes(&args, &nargs, line->data + dir_offset,
  106. line->length - dir_offset))
  107. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  108. if (nargs < 1 || nargs > 2)
  109. return free(args), error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  110. bank = args[0];
  111. slot = nargs == 2 ? args[1] : default_bank_slot(bank);
  112. free(args);
  113. if (bank >= MMU_NUM_ROM_BANKS || slot >= MMU_NUM_SLOTS)
  114. return error_info_create(line, ET_PREPROC, ED_PP_ARG_RANGE);
  115. if (bank == 0 && slot != 0)
  116. return error_info_create(line, ET_LAYOUT, ED_LYT_BLOCK0);
  117. if (si->slots[bank] >= 0 && si->slots[bank] != slot) {
  118. ErrorInfo *ei = error_info_create(line, ET_LAYOUT, ED_LYT_SLOTS);
  119. error_info_append(ei, si->lines[bank]);
  120. return ei;
  121. }
  122. *offset = bank * MMU_ROM_BANK_SIZE;
  123. si->slots[bank] = slot;
  124. if (!si->lines[bank])
  125. si->lines[bank] = line;
  126. return NULL;
  127. }
  128. /*
  129. Parse a .space directive, which fills a region with a single byte.
  130. */
  131. static bool parse_space(
  132. uint8_t **result, size_t *length, const char *arg, ssize_t size)
  133. {
  134. uint8_t *bytes;
  135. size_t nbytes;
  136. if (!parse_bytes(&bytes, &nbytes, arg, size))
  137. return false;
  138. if (nbytes < 1 || nbytes > 2) {
  139. free(bytes);
  140. return false;
  141. }
  142. *length = bytes[0];
  143. if (!(*result = malloc(sizeof(uint8_t) * (*length))))
  144. OUT_OF_MEMORY()
  145. memset(*result, nbytes == 2 ? bytes[1] : 0, *length);
  146. free(bytes);
  147. return true;
  148. }
  149. /*
  150. Parse data encoded in a line into an ASMData object.
  151. On success, return NULL and store the instruction in *data_ptr. On failure,
  152. return an ErrorInfo object; *data_ptr is not modified.
  153. */
  154. static ErrorInfo* parse_data(
  155. const ASMLine *line, ASMData **data_ptr, size_t offset)
  156. {
  157. const char *directive;
  158. parser_func parser = (parser_func) parse_string;
  159. if (IS_DIRECTIVE(line, DIR_BYTE)) {
  160. directive = DIR_BYTE;
  161. parser = parse_bytes;
  162. } else if (IS_DIRECTIVE(line, DIR_SPACE)) {
  163. directive = DIR_SPACE;
  164. parser = parse_space;
  165. } else if (IS_DIRECTIVE(line, DIR_ASCII)) {
  166. directive = DIR_ASCII;
  167. } else if (IS_DIRECTIVE(line, DIR_ASCIZ)) {
  168. directive = DIR_ASCIZ;
  169. } else if (IS_DIRECTIVE(line, DIR_ASCIIZ)) {
  170. directive = DIR_ASCIIZ;
  171. } else {
  172. return error_info_create(line, ET_PREPROC, ED_PP_UNKNOWN);
  173. }
  174. if (!DIRECTIVE_HAS_ARG(line, directive))
  175. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG);
  176. size_t dir_offset = DIRECTIVE_OFFSET(line, directive) + 1;
  177. const char *arg = line->data + dir_offset;
  178. size_t arglen = line->length - dir_offset;
  179. ASMData *data = malloc(sizeof(ASMData));
  180. if (!data)
  181. OUT_OF_MEMORY()
  182. data->loc.offset = offset;
  183. data->next = NULL;
  184. if (!parser(&data->bytes, &data->loc.length, arg, arglen)) {
  185. free(data);
  186. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG);
  187. }
  188. *data_ptr = data;
  189. return NULL;
  190. }
  191. /*
  192. Parse an instruction encoded in a line into an ASMInstruction object.
  193. On success, return NULL and store the instruction in *inst_ptr. On failure,
  194. return an ErrorInfo object; *inst_ptr is not modified.
  195. */
  196. static ErrorInfo* parse_instruction(
  197. const ASMLine *line, ASMInstruction **inst_ptr, size_t offset)
  198. {
  199. char mnemonic[MAX_MNEMONIC_SIZE] = {0};
  200. size_t i = 0;
  201. while (i < line->length) {
  202. char c = line->data[i];
  203. if (c == ' ')
  204. break;
  205. if (i >= MAX_MNEMONIC_SIZE)
  206. return error_info_create(line, ET_PARSER, ED_PS_OP_TOO_LONG);
  207. if ((c < 'a' || c > 'z') && (c < '0' || c > '9'))
  208. return error_info_create(line, ET_PARSER, ED_PS_OP_INVALID);
  209. mnemonic[i++] = c;
  210. }
  211. if (i < MIN_MNEMONIC_SIZE)
  212. return error_info_create(line, ET_PARSER, ED_PS_OP_TOO_SHORT);
  213. if (i + 1 < line->length)
  214. i++; // Advance past space
  215. uint8_t *bytes;
  216. size_t arglen = line->length - i, length;
  217. char *argstart = arglen > 0 ? line->data + i : NULL, *symbol = NULL;
  218. ASMInstParser parser = get_inst_parser(mnemonic);
  219. if (!parser)
  220. return error_info_create(line, ET_PARSER, ED_PS_OP_UNKNOWN);
  221. ASMErrorDesc edesc = parser(&bytes, &length, &symbol, argstart, arglen);
  222. if (edesc != ED_NONE)
  223. return error_info_create(line, ET_PARSER, edesc);
  224. ASMInstruction *inst = malloc(sizeof(ASMInstruction));
  225. if (!inst)
  226. OUT_OF_MEMORY()
  227. inst->loc.offset = offset;
  228. inst->loc.length = length;
  229. inst->bytes = bytes;
  230. inst->symbol = symbol;
  231. inst->line = line;
  232. inst->next = NULL;
  233. *inst_ptr = inst;
  234. return NULL;
  235. }
  236. /*
  237. Check if the given object location is legal.
  238. Checks include ROM size bounding, overlapping with existing objects, and
  239. block-crossing assuming the .cross_blocks directive has not been specified.
  240. On success, return NULL and add the location to the overlap table.
  241. On failure, return an ErrorInfo object.
  242. */
  243. static ErrorInfo* check_layout(
  244. ASMLayoutInfo *li, const ASMLocation *loc, const ASMLine *line)
  245. {
  246. const ASMLine *clash = NULL, *clash_origin;
  247. if (loc->offset + loc->length > li->size) {
  248. clash = &bounds_sentinel;
  249. } else {
  250. for (size_t i = 0; i < loc->length; i++) {
  251. if (li->overlap_table[loc->offset + i]) {
  252. clash = li->overlap_table[loc->offset + i];
  253. clash_origin = li->overlap_origins[loc->offset + i];
  254. break;
  255. }
  256. }
  257. }
  258. if (clash) {
  259. ErrorInfo *ei = error_info_create(line, ET_LAYOUT,
  260. (clash == &header_sentinel) ? ED_LYT_OVERLAP_HEAD :
  261. (clash == &bounds_sentinel) ? ED_LYT_BOUNDS : ED_LYT_OVERLAP);
  262. if (li->origin)
  263. error_info_append(ei, li->origin);
  264. if (clash != &header_sentinel && clash != &bounds_sentinel) {
  265. error_info_append(ei, clash);
  266. if (clash_origin)
  267. error_info_append(ei, clash_origin);
  268. }
  269. return ei;
  270. }
  271. uint8_t bank = (loc->offset + loc->length - 1) / MMU_ROM_BANK_SIZE;
  272. if (bank != li->bank && !li->cross_blocks) {
  273. ErrorInfo *ei = error_info_create(line, ET_LAYOUT, ED_LYT_BLOCK_CROSS);
  274. if (li->origin)
  275. error_info_append(ei, li->origin);
  276. return ei;
  277. }
  278. for (size_t i = 0; i < loc->length; i++) {
  279. li->overlap_table[loc->offset + i] = line;
  280. li->overlap_origins[loc->offset + i] = li->origin;
  281. }
  282. return NULL;
  283. }
  284. /*
  285. Tokenize ASMLines into ASMInstructions and ASMData.
  286. NULL is returned on success and an ErrorInfo object is returned on failure.
  287. state->instructions, state->data, and state->symtable may or may not be
  288. modified regardless of success.
  289. */
  290. ErrorInfo* tokenize(AssemblerState *state)
  291. {
  292. ASMLayoutInfo li = {
  293. .size = state->rom_size ? state->rom_size : ROM_SIZE_MAX,
  294. .origin = NULL, .bank = 0, .cross_blocks = state->cross_blocks
  295. };
  296. li.overlap_table = calloc(li.size, sizeof(const ASMLine*));
  297. li.overlap_origins = calloc(li.size, sizeof(const ASMLine*));
  298. if (!li.overlap_table || !li.overlap_origins)
  299. OUT_OF_MEMORY()
  300. ErrorInfo *ei = NULL;
  301. ASMInstruction dummy_inst = {.next = NULL}, *inst, *prev_inst = &dummy_inst;
  302. ASMData dummy_data = {.next = NULL}, *data, *prev_data = &dummy_data;
  303. const ASMLine *line = state->lines;
  304. size_t offset = 0;
  305. ASMSlotInfo si = {.lines = {0}};
  306. for (size_t i = 0; i < HEADER_SIZE; i++)
  307. li.overlap_table[state->header.offset + i] = &header_sentinel;
  308. memset(si.slots, -1, MMU_NUM_ROM_BANKS);
  309. while (line) {
  310. if (line->is_label) {
  311. if (offset >= li.size) {
  312. ei = error_info_create(line, ET_LAYOUT, ED_LYT_BOUNDS);
  313. goto cleanup;
  314. }
  315. int8_t slot = si.slots[offset / MMU_NUM_ROM_BANKS];
  316. if ((ei = add_label_to_table(state->symtable, line, offset, slot)))
  317. goto cleanup;
  318. }
  319. else if (IS_LOCAL_DIRECTIVE(line)) {
  320. if (IS_DIRECTIVE(line, DIR_ORIGIN)) {
  321. if ((ei = handle_origin_directive(line, &offset)))
  322. goto cleanup;
  323. li.origin = line;
  324. li.bank = offset / MMU_ROM_BANK_SIZE;
  325. }
  326. else if (IS_DIRECTIVE(line, DIR_BLOCK)) {
  327. if ((ei = handle_block_directive(line, &offset, &si)))
  328. goto cleanup;
  329. li.origin = line;
  330. li.bank = offset / MMU_ROM_BANK_SIZE;
  331. }
  332. else {
  333. if ((ei = parse_data(line, &data, offset)))
  334. goto cleanup;
  335. offset += data->loc.length;
  336. prev_data->next = data;
  337. prev_data = data;
  338. if ((ei = check_layout(&li, &data->loc, line)))
  339. goto cleanup;
  340. }
  341. }
  342. else {
  343. if ((ei = parse_instruction(line, &inst, offset)))
  344. goto cleanup;
  345. offset += inst->loc.length;
  346. prev_inst->next = inst;
  347. prev_inst = inst;
  348. if ((ei = check_layout(&li, &inst->loc, line)))
  349. goto cleanup;
  350. }
  351. line = line->next;
  352. }
  353. cleanup:
  354. state->instructions = dummy_inst.next;
  355. state->data = dummy_data.next;
  356. free(li.overlap_table);
  357. free(li.overlap_origins);
  358. return ei;
  359. }