An emulator, assembler, and disassembler for the Sega Game Gear
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

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