An emulator, assembler, and disassembler for the Sega Game Gear
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

parse_util.c 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 <limits.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "parse_util.h"
  7. #include "directives.h"
  8. #include "../util.h"
  9. #define MAX_REGION_SIZE 32
  10. #define LCASE(c) ((c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c)
  11. #define DIRECTIVE_PARSE_FUNC(name, type) \
  12. bool dparse_##name(type *result, const ASMLine *line, const char *directive)
  13. /*
  14. All public functions in this file follow the same return conventions:
  15. - Return true on success and false on failure.
  16. - *result is only modified on success.
  17. */
  18. /*
  19. Adjust *ap_info for an indirect argument, like (hl) or (ix+*).
  20. ap_info->size must be > 2 to begin with, and will always be > 0 on success.
  21. *ap_info is not modified on failure.
  22. */
  23. static bool adjust_for_indirection(ASMArgParseInfo *ap_info)
  24. {
  25. const char *arg = ap_info->arg;
  26. ssize_t size = ap_info->size;
  27. if (arg[0] != '(' || arg[size - 1] != ')')
  28. return false;
  29. arg++;
  30. size -= 2;
  31. if (arg[0] == ' ') {
  32. arg++;
  33. if (--size <= 0)
  34. return false;
  35. }
  36. if (arg[size - 1] == ' ') {
  37. if (--size <= 0)
  38. return false;
  39. }
  40. ap_info->arg = arg;
  41. ap_info->size = size;
  42. return true;
  43. }
  44. /*
  45. Calculate the mask field for an ASMArgImmediate based on its uval/sval.
  46. */
  47. static void calculate_immediate_mask(ASMArgImmediate *imm)
  48. {
  49. imm->mask = 0;
  50. if (imm->sval < 0) {
  51. if (imm->sval >= INT8_MIN)
  52. imm->mask |= IMM_S8;
  53. if (imm->sval >= INT8_MIN + 2)
  54. imm->mask |= IMM_REL;
  55. } else {
  56. imm->mask = IMM_U16;
  57. if (imm->uval <= UINT8_MAX)
  58. imm->mask |= IMM_U8;
  59. if (imm->uval <= INT8_MAX)
  60. imm->mask |= IMM_S8;
  61. if (imm->uval <= INT8_MAX + 2)
  62. imm->mask |= IMM_REL;
  63. if (imm->uval <= 7)
  64. imm->mask |= IMM_BIT;
  65. if (!(imm->uval & ~0x38))
  66. imm->mask |= IMM_RST;
  67. if (imm->uval <= 2)
  68. imm->mask |= IMM_IM;
  69. }
  70. }
  71. /*
  72. Read in a boolean value and store it in *result.
  73. */
  74. bool parse_bool(bool *result, const char *arg, ssize_t size)
  75. {
  76. switch (size) {
  77. case 1: // 0, 1
  78. if (*arg == '0' || *arg == '1')
  79. return (*result = *arg - '0'), true;
  80. return false;
  81. case 2: // on
  82. if (!strncmp(arg, "on", 2))
  83. return (*result = true), true;
  84. return false;
  85. case 3: // off
  86. if (!strncmp(arg, "off", 3))
  87. return (*result = false), true;
  88. return false;
  89. case 4: // true
  90. if (!strncmp(arg, "true", 4))
  91. return (*result = true), true;
  92. return false;
  93. case 5: // false
  94. if (!strncmp(arg, "false", 5))
  95. return (*result = false), true;
  96. return false;
  97. }
  98. return false;
  99. }
  100. /*
  101. Read in a 32-bit integer and store it in *result.
  102. */
  103. bool parse_uint32_t(uint32_t *result, const char *arg, ssize_t size)
  104. {
  105. if (size <= 0)
  106. return false;
  107. const char *end = arg + size;
  108. uint64_t value = 0;
  109. if (*arg == '$') {
  110. arg++;
  111. if (arg == end)
  112. return false;
  113. while (arg < end) {
  114. if (*arg >= '0' && *arg <= '9')
  115. value = (value * 0x10) + (*arg - '0');
  116. else if (*arg >= 'a' && *arg <= 'f')
  117. value = (value * 0x10) + 0xA + (*arg - 'a');
  118. else
  119. return false;
  120. if (value > UINT32_MAX)
  121. return false;
  122. arg++;
  123. }
  124. }
  125. else {
  126. while (arg < end) {
  127. if (*arg < '0' || *arg > '9')
  128. return false;
  129. value = (value * 10) + (*arg - '0');
  130. if (value > UINT32_MAX)
  131. return false;
  132. arg++;
  133. }
  134. }
  135. *result = value;
  136. return true;
  137. }
  138. /*
  139. Read in a string, possibly with escape sequences, and store it in *result.
  140. *length is also updated to the size of the string, which is *not*
  141. null-terminated. *result must be free()'d when finished.
  142. */
  143. bool parse_string(char **result, size_t *length, const char *arg, ssize_t size)
  144. {
  145. if (size < 2 || arg[0] != '"' || arg[size - 1] != '"')
  146. return false;
  147. ssize_t i, slashes = 0;
  148. for (i = 1; i < size; i++) {
  149. if (arg[i] == '"' && (slashes % 2) == 0)
  150. break;
  151. // TODO: parse escape codes here
  152. if (arg[i] == '\\')
  153. slashes++;
  154. else
  155. slashes = 0;
  156. }
  157. if (i != size - 1) // Junk present after closing quote
  158. return false;
  159. *length = size - 2;
  160. *result = cr_malloc(sizeof(char) * (*length));
  161. memcpy(*result, arg + 1, *length);
  162. return true;
  163. }
  164. /*
  165. Read in a space-separated sequence of bytes and store it in *result.
  166. *length is also updated to the number of bytes in the array. *result must
  167. be free()'d when finished.
  168. */
  169. bool parse_bytes(uint8_t **result, size_t *length, const char *arg, ssize_t size)
  170. {
  171. if (size <= 0)
  172. return false;
  173. const char *end = arg + size;
  174. uint8_t *bytes = NULL;
  175. size_t nbytes = 0;
  176. while (arg < end) {
  177. const char *start = arg;
  178. while (arg != end && *arg != ' ' && *arg != ',')
  179. arg++;
  180. uint32_t temp;
  181. if (!parse_uint32_t(&temp, start, arg - start) || temp > UINT8_MAX) {
  182. free(bytes);
  183. return false;
  184. }
  185. nbytes++;
  186. bytes = cr_realloc(bytes, sizeof(uint8_t) * nbytes);
  187. bytes[nbytes - 1] = temp;
  188. if (arg < end - 1 && *arg == ',' && *(arg + 1) == ' ')
  189. arg += 2;
  190. else if (arg++ >= end)
  191. break;
  192. }
  193. *result = bytes;
  194. *length = nbytes;
  195. return true;
  196. }
  197. /*
  198. Read in a register argument and store it in *result.
  199. */
  200. bool argparse_register(ASMArgRegister *result, ASMArgParseInfo ai)
  201. {
  202. if (ai.size < 1 || ai.size > 3)
  203. return false;
  204. char buf[3] = {'\0'};
  205. switch (ai.size) {
  206. case 3: buf[2] = LCASE(ai.arg[2]);
  207. case 2: buf[1] = LCASE(ai.arg[1]);
  208. case 1: buf[0] = LCASE(ai.arg[0]);
  209. }
  210. switch (ai.size) {
  211. case 1:
  212. switch (buf[0]) {
  213. case 'a': return (*result = REG_A), true;
  214. case 'f': return (*result = REG_F), true;
  215. case 'b': return (*result = REG_B), true;
  216. case 'c': return (*result = REG_C), true;
  217. case 'd': return (*result = REG_D), true;
  218. case 'e': return (*result = REG_E), true;
  219. case 'h': return (*result = REG_H), true;
  220. case 'l': return (*result = REG_L), true;
  221. case 'i': return (*result = REG_I), true;
  222. case 'r': return (*result = REG_R), true;
  223. }
  224. return false;
  225. case 2:
  226. switch ((buf[0] << 8) + buf[1]) {
  227. case 0x6166: return (*result = REG_AF), true;
  228. case 0x6263: return (*result = REG_BC), true;
  229. case 0x6465: return (*result = REG_DE), true;
  230. case 0x686C: return (*result = REG_HL), true;
  231. case 0x6978: return (*result = REG_IX), true;
  232. case 0x6979: return (*result = REG_IY), true;
  233. case 0x7063: return (*result = REG_PC), true;
  234. case 0x7370: return (*result = REG_SP), true;
  235. }
  236. return false;
  237. case 3:
  238. switch ((buf[0] << 16) + (buf[1] << 8) + buf[2]) {
  239. case 0x616627: return (*result = REG_AF_), true;
  240. case 0x697868: return (*result = REG_IXH), true;
  241. case 0x69786C: return (*result = REG_IXL), true;
  242. case 0x697968: return (*result = REG_IYH), true;
  243. case 0x69796C: return (*result = REG_IYL), true;
  244. }
  245. return false;
  246. }
  247. return false;
  248. }
  249. /*
  250. Read in a condition argument and store it in *result.
  251. */
  252. bool argparse_condition(ASMArgCondition *result, ASMArgParseInfo ai)
  253. {
  254. if (ai.size < 1 || ai.size > 2)
  255. return false;
  256. char buf[2] = {'\0'};
  257. switch (ai.size) {
  258. case 2: buf[1] = LCASE(ai.arg[1]);
  259. case 1: buf[0] = LCASE(ai.arg[0]);
  260. }
  261. switch (ai.size) {
  262. case 1:
  263. switch (buf[0]) {
  264. case 'n': return (*result = COND_N), true;
  265. case 'c': return (*result = COND_C), true;
  266. case 'p': return (*result = COND_P), true;
  267. case 'm': return (*result = COND_M), true;
  268. }
  269. return false;
  270. case 2:
  271. switch ((buf[0] << 8) + buf[1]) {
  272. case 0x6E7A: return (*result = COND_NZ), true;
  273. case 0x6E63: return (*result = COND_NC), true;
  274. case 0x706F: return (*result = COND_PO), true;
  275. case 0x7065: return (*result = COND_PE), true;
  276. }
  277. return false;
  278. }
  279. return false;
  280. }
  281. /*
  282. Read in a label immediate argument and store it in *result.
  283. */
  284. static bool argparse_imm_label(ASMArgImmediate *result, ASMArgParseInfo ai)
  285. {
  286. if (ai.size >= MAX_SYMBOL_SIZE)
  287. return false;
  288. for (const char *i = ai.arg; i < ai.arg + ai.size; i++) {
  289. if (!is_valid_symbol_char(*i, i == ai.arg))
  290. return false;
  291. }
  292. result->mask = IMM_U16;
  293. result->is_label = true;
  294. strncpy(result->label, ai.arg, ai.size);
  295. result->label[ai.size] = '\0';
  296. return true;
  297. }
  298. /*
  299. Read in an immediate argument and store it in *result.
  300. */
  301. bool argparse_immediate(ASMArgImmediate *result, ASMArgParseInfo ai)
  302. {
  303. if (ai.size <= 0)
  304. return false;
  305. bool negative = false, modifiers = false;
  306. ssize_t i = 0;
  307. while (ai.arg[i] == '-' || ai.arg[i] == '+' || ai.arg[i] == ' ') {
  308. modifiers = true;
  309. if (ai.arg[i] == '-')
  310. negative = !negative;
  311. if (++i >= ai.size)
  312. return false;
  313. }
  314. ai.arg += i;
  315. ai.size -= i;
  316. const ASMDefine *define = asm_deftable_find(ai.deftable, ai.arg, ai.size);
  317. if (define) {
  318. if (negative) {
  319. calculate_immediate_mask(result);
  320. result->is_label = false;
  321. result->uval = define->value.uval;
  322. result->sval = -define->value.sval;
  323. } else {
  324. *result = define->value;
  325. }
  326. return true;
  327. }
  328. uint32_t uval;
  329. if (!parse_uint32_t(&uval, ai.arg, ai.size)) {
  330. if (!modifiers && argparse_imm_label(result, ai))
  331. return true;
  332. return false;
  333. }
  334. if (uval > UINT16_MAX)
  335. return false;
  336. int32_t sval = negative ? -uval : uval;
  337. if (sval < INT16_MIN)
  338. return false;
  339. calculate_immediate_mask(result);
  340. result->is_label = false;
  341. result->uval = uval;
  342. result->sval = sval;
  343. return true;
  344. }
  345. /*
  346. Read in an indirect argument and store it in *result.
  347. */
  348. bool argparse_indirect(ASMArgIndirect *result, ASMArgParseInfo ai)
  349. {
  350. if (ai.size < 3 || !adjust_for_indirection(&ai))
  351. return false;
  352. ASMArgRegister reg;
  353. ASMArgImmediate imm;
  354. if (argparse_register(&reg, ai)) {
  355. if (reg == REG_BC || reg == REG_DE || reg == REG_HL ||
  356. reg == REG_IX || reg == REG_IY) {
  357. result->type = AT_REGISTER;
  358. result->addr.reg = reg;
  359. return true;
  360. }
  361. } else if (argparse_immediate(&imm, ai)) {
  362. if (imm.mask & IMM_U16) {
  363. result->type = AT_IMMEDIATE;
  364. result->addr.imm = imm;
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. /*
  371. Read in an indexed argument and store it in *result.
  372. */
  373. bool argparse_indexed(ASMArgIndexed *result, ASMArgParseInfo ai)
  374. {
  375. if (ai.size < 4 || !adjust_for_indirection(&ai) || ai.size < 2)
  376. return false;
  377. ASMArgRegister reg;
  378. if (ai.arg[0] != 'i')
  379. return false;
  380. if (ai.arg[1] == 'x')
  381. reg = REG_IX;
  382. else if (ai.arg[1] == 'y')
  383. reg = REG_IY;
  384. else
  385. return false;
  386. ai.arg += 2;
  387. ai.size -= 2;
  388. if (ai.size > 0 && ai.arg[0] == ' ') {
  389. ai.arg++;
  390. ai.size--;
  391. }
  392. if (ai.size > 0) {
  393. ASMArgImmediate imm;
  394. if (!argparse_immediate(&imm, ai) || !(imm.mask & IMM_S8))
  395. return false;
  396. result->offset = imm.sval;
  397. } else {
  398. result->offset = 0;
  399. }
  400. result->reg = reg;
  401. return true;
  402. }
  403. /*
  404. Read in a port argument and store it in *result.
  405. */
  406. bool argparse_port(ASMArgPort *result, ASMArgParseInfo ai)
  407. {
  408. if (ai.size < 3 || !adjust_for_indirection(&ai))
  409. return false;
  410. ASMArgRegister reg;
  411. ASMArgImmediate imm;
  412. if (argparse_register(&reg, ai)) {
  413. if (reg == REG_C) {
  414. result->type = AT_REGISTER;
  415. result->port.reg = reg;
  416. return true;
  417. }
  418. } else if (argparse_immediate(&imm, ai)) {
  419. if (imm.mask & IMM_U8) {
  420. result->type = AT_IMMEDIATE;
  421. result->port.imm = imm;
  422. return true;
  423. }
  424. }
  425. return false;
  426. }
  427. /*
  428. Read in a boolean argument from the given line and store it in *result.
  429. */
  430. DIRECTIVE_PARSE_FUNC(bool, bool)
  431. {
  432. size_t offset = DIRECTIVE_OFFSET(line, directive) + 1;
  433. return parse_bool(result, line->data + offset, line->length - offset);
  434. }
  435. /*
  436. Read in a 32-bit int argument from the given line and store it in *result.
  437. */
  438. DIRECTIVE_PARSE_FUNC(uint32_t, uint32_t)
  439. {
  440. size_t offset = DIRECTIVE_OFFSET(line, directive) + 1;
  441. return parse_uint32_t(result, line->data + offset, line->length - offset);
  442. }
  443. /*
  444. Read in a 16-bit int argument from the given line and store it in *result.
  445. */
  446. DIRECTIVE_PARSE_FUNC(uint16_t, uint16_t)
  447. {
  448. uint32_t value;
  449. if (dparse_uint32_t(&value, line, directive) && value <= UINT16_MAX)
  450. return (*result = value), true;
  451. return false;
  452. }
  453. /*
  454. Read in an 8-bit int argument from the given line and store it in *result.
  455. */
  456. DIRECTIVE_PARSE_FUNC(uint8_t, uint8_t)
  457. {
  458. uint32_t value;
  459. if (dparse_uint32_t(&value, line, directive) && value <= UINT8_MAX)
  460. return (*result = value), true;
  461. return false;
  462. }
  463. /*
  464. Parse a ROM size string in an ASMLine and store it in *result.
  465. */
  466. DIRECTIVE_PARSE_FUNC(rom_size, uint32_t)
  467. {
  468. const char *arg = line->data + DIRECTIVE_OFFSET(line, directive) + 1;
  469. const char *end = line->data + line->length - 1;
  470. if (end - arg < 5)
  471. return false;
  472. if (*(arg++) != '"' || *(end--) != '"')
  473. return false;
  474. if (*end != 'B' && *end != 'b')
  475. return false;
  476. end--;
  477. uint32_t factor;
  478. if (*end == 'K' || *end == 'k')
  479. factor = 1 << 10;
  480. else if (*end == 'M' || *end == 'm')
  481. factor = 1 << 20;
  482. else
  483. return false;
  484. end--;
  485. if (*end != ' ')
  486. return false;
  487. uint32_t value = 0;
  488. while (arg < end) {
  489. if (*arg < '0' || *arg > '9')
  490. return false;
  491. value = (value * 10) + (*arg - '0');
  492. if (value > UINT16_MAX)
  493. return false;
  494. arg++;
  495. }
  496. *result = value * factor;
  497. return true;
  498. }
  499. /*
  500. Parse a region code string in an ASMLine and store it in *result.
  501. */
  502. DIRECTIVE_PARSE_FUNC(region_string, uint8_t)
  503. {
  504. char buffer[MAX_REGION_SIZE];
  505. size_t offset = DIRECTIVE_OFFSET(line, directive) + 1;
  506. const char *arg = line->data + offset;
  507. ssize_t len = line->length - offset;
  508. if (len <= 2 || len >= MAX_REGION_SIZE + 2) // Account for double quotes
  509. return false;
  510. if (arg[0] != '"' || arg[len - 1] != '"')
  511. return false;
  512. strncpy(buffer, arg + 1, len - 2);
  513. buffer[len - 2] = '\0';
  514. uint8_t code = region_string_to_code(buffer);
  515. if (code)
  516. return (*result = code), true;
  517. return false;
  518. }
  519. /*
  520. Parse a size code in an ASMLine and store it in *result.
  521. */
  522. DIRECTIVE_PARSE_FUNC(size_code, uint8_t)
  523. {
  524. uint32_t bytes;
  525. if (!dparse_uint32_t(&bytes, line, directive)) {
  526. if (!dparse_rom_size(&bytes, line, directive))
  527. return false;
  528. }
  529. uint8_t code = size_bytes_to_code(bytes);
  530. if (code != INVALID_SIZE_CODE)
  531. return (*result = code), true;
  532. return false;
  533. }