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.

tokenizer.c 14 KiB

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