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.
 
 
 
 
 

796 lines
23 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 <errno.h>
  4. #include <libgen.h>
  5. #include <limits.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include "assembler.h"
  10. #include "assembler/errors.h"
  11. #include "assembler/state.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #define DEFAULT_HEADER_OFFSET 0x7FF0
  15. #define DEFAULT_REGION "GG Export"
  16. #define DIRECTIVE_MARKER '.'
  17. #define DIR_INCLUDE ".include"
  18. #define DIR_ORIGIN ".org"
  19. #define DIR_OPTIMIZER ".optimizer"
  20. #define DIR_ROM_SIZE ".rom_size"
  21. #define DIR_ROM_HEADER ".rom_header"
  22. #define DIR_ROM_CHECKSUM ".rom_checksum"
  23. #define DIR_ROM_PRODUCT ".rom_product"
  24. #define DIR_ROM_VERSION ".rom_version"
  25. #define DIR_ROM_REGION ".rom_region"
  26. #define DIR_ROM_DECLSIZE ".rom_declsize"
  27. #define DIRECTIVE_HAS_ARG(line, d) ((line)->length > strlen(d))
  28. #define IS_DIRECTIVE(line, d) \
  29. (((line)->length >= strlen(d)) && \
  30. !strncmp((line)->data, d, strlen(d)) && \
  31. (!DIRECTIVE_HAS_ARG(line, d) || (line)->data[strlen(d)] == ' '))
  32. #define DIRECTIVE_OFFSET(line, d) \
  33. (DIRECTIVE_HAS_ARG(line, d) ? strlen(d) : 0)
  34. /*
  35. Deallocate a LineBuffer previously created with read_source_file().
  36. */
  37. static void free_line_buffer(LineBuffer *buffer)
  38. {
  39. Line *line = buffer->lines, *temp;
  40. while (line) {
  41. temp = line->next;
  42. free(line->data);
  43. free(line);
  44. line = temp;
  45. }
  46. free(buffer->filename);
  47. free(buffer);
  48. }
  49. /*
  50. Read the contents of the source file at the given path into a line buffer.
  51. Return the buffer if reading was successful; it must be freed with
  52. free_line_buffer() when done. Return NULL if an error occurred while
  53. reading. If print_errors is true, a message will also be printed to stderr.
  54. */
  55. static LineBuffer* read_source_file(const char *path, bool print_errors)
  56. {
  57. FILE *fp;
  58. struct stat st;
  59. if (!(fp = fopen(path, "r"))) {
  60. if (print_errors)
  61. ERROR_ERRNO("couldn't open source file")
  62. return NULL;
  63. }
  64. if (fstat(fileno(fp), &st)) {
  65. fclose(fp);
  66. if (print_errors)
  67. ERROR_ERRNO("couldn't open source file")
  68. return NULL;
  69. }
  70. if (!(st.st_mode & S_IFREG)) {
  71. fclose(fp);
  72. if (print_errors)
  73. ERROR("couldn't open source file: %s", st.st_mode & S_IFDIR ?
  74. "Is a directory" : "Is not a regular file")
  75. return NULL;
  76. }
  77. LineBuffer *source = malloc(sizeof(LineBuffer));
  78. if (!source)
  79. OUT_OF_MEMORY()
  80. source->lines = NULL;
  81. source->filename = strdup(path);
  82. if (!source->filename)
  83. OUT_OF_MEMORY()
  84. Line dummy = {.next = NULL};
  85. Line *line, *prev = &dummy;
  86. size_t lineno = 1;
  87. while (1) {
  88. char *data = NULL;
  89. size_t cap = 0;
  90. ssize_t len;
  91. if ((len = getline(&data, &cap, fp)) < 0) {
  92. if (feof(fp))
  93. break;
  94. if (errno == ENOMEM)
  95. OUT_OF_MEMORY()
  96. if (print_errors)
  97. ERROR_ERRNO("couldn't read source file")
  98. fclose(fp);
  99. source->lines = dummy.next;
  100. free_line_buffer(source);
  101. return NULL;
  102. }
  103. line = malloc(sizeof(Line));
  104. if (!line)
  105. OUT_OF_MEMORY()
  106. line->data = data;
  107. line->length = feof(fp) ? len : (len - 1);
  108. line->lineno = lineno++;
  109. line->next = NULL;
  110. prev->next = line;
  111. prev = line;
  112. }
  113. fclose(fp);
  114. source->lines = dummy.next;
  115. return source;
  116. }
  117. /*
  118. Write an assembled binary file to the given path.
  119. Return whether the file was written successfully. On error, a message is
  120. printed to stderr.
  121. */
  122. static bool write_binary_file(const char *path, const uint8_t *data, size_t size)
  123. {
  124. FILE *fp;
  125. if (!(fp = fopen(path, "wb"))) {
  126. ERROR_ERRNO("couldn't open destination file")
  127. return false;
  128. }
  129. if (!fwrite(data, size, 1, fp)) {
  130. fclose(fp);
  131. ERROR_ERRNO("couldn't write to destination file")
  132. return false;
  133. }
  134. fclose(fp);
  135. return true;
  136. }
  137. /*
  138. Initialize default values in an AssemblerState object.
  139. */
  140. static void init_state(AssemblerState *state)
  141. {
  142. state->header.offset = DEFAULT_HEADER_OFFSET;
  143. state->header.checksum = true;
  144. state->header.product_code = 0;
  145. state->header.version = 0;
  146. state->header.region = region_string_to_code(DEFAULT_REGION);
  147. state->header.rom_size = 0;
  148. state->optimizer = false;
  149. state->rom_size = 0;
  150. state->lines = NULL;
  151. state->includes = NULL;
  152. state->instructions = NULL;
  153. state->symtable = NULL;
  154. }
  155. /*
  156. Deallocate an ASMLine list.
  157. */
  158. static void free_asm_lines(ASMLine *line)
  159. {
  160. while (line) {
  161. ASMLine *temp = line->next;
  162. free(line->data);
  163. free(line);
  164. line = temp;
  165. }
  166. }
  167. /*
  168. Deallocate an ASMInclude list.
  169. */
  170. static void free_asm_includes(ASMInclude *include)
  171. {
  172. while (include) {
  173. ASMInclude *temp = include->next;
  174. free_line_buffer(include->lines);
  175. free(include);
  176. include = temp;
  177. }
  178. }
  179. /*
  180. Deallocate an ASMInstruction list.
  181. */
  182. static void free_asm_instructions(ASMInstruction *inst)
  183. {
  184. while (inst) {
  185. ASMInstruction *temp = inst->next;
  186. if (inst->symbol)
  187. free(inst->symbol);
  188. free(inst);
  189. inst = temp;
  190. }
  191. }
  192. /*
  193. Deallocate an ASMSymbolTable.
  194. */
  195. static void free_asm_symtable(ASMSymbolTable *symtable)
  196. {
  197. if (!symtable)
  198. return;
  199. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++) {
  200. ASMSymbol *sym = symtable->buckets[bucket], *temp;
  201. while (sym) {
  202. temp = sym->next;
  203. free(sym->symbol);
  204. free(sym);
  205. sym = temp;
  206. }
  207. }
  208. free(symtable);
  209. }
  210. /*
  211. Preprocess a single source line (source, length) into a normalized ASMLine.
  212. *Only* the data and length fields in the ASMLine object are populated. The
  213. normalization process converts tabs to spaces, lowercases all alphabetical
  214. characters, and removes runs of multiple spaces (outside of string
  215. literals), strips comments, and other things.
  216. Return NULL if an ASM line was not generated from the source, i.e. if it is
  217. blank after being stripped.
  218. */
  219. static ASMLine* normalize_line(const char *source, size_t length)
  220. {
  221. char *data = malloc(sizeof(char) * length);
  222. if (!data)
  223. OUT_OF_MEMORY()
  224. size_t si, di, slashes = 0;
  225. bool has_content = false, space_pending = false, in_string = false;
  226. for (si = di = 0; si < length; si++) {
  227. char c = source[si];
  228. if (c == '\\')
  229. slashes++;
  230. else
  231. slashes = 0;
  232. if (in_string) {
  233. if (c == '"' && (slashes % 2) == 0)
  234. in_string = false;
  235. data[di++] = c;
  236. } else {
  237. if (c == ';')
  238. break;
  239. if (c == '"' && (slashes % 2) == 0)
  240. in_string = true;
  241. if (c >= 'A' && c <= 'Z')
  242. c += 'a' - 'A';
  243. if (c == ' ' || c == '\t')
  244. space_pending = true;
  245. else {
  246. if (space_pending) {
  247. if (has_content)
  248. data[di++] = ' ';
  249. space_pending = false;
  250. }
  251. has_content = true;
  252. data[di++] = c;
  253. }
  254. }
  255. }
  256. if (!has_content) {
  257. free(data);
  258. return NULL;
  259. }
  260. ASMLine *line = malloc(sizeof(ASMLine));
  261. if (!line)
  262. OUT_OF_MEMORY()
  263. data = realloc(data, sizeof(char) * di);
  264. if (!data)
  265. OUT_OF_MEMORY()
  266. line->data = data;
  267. line->length = di;
  268. return line;
  269. }
  270. /*
  271. Read and return the target path from an include directive.
  272. This function allocates a buffer to store the filename; it must be free()'d
  273. after calling read_source_file(). If a syntax error occurs while trying to
  274. read the path, it returns NULL.
  275. */
  276. char* read_include_path(const ASMLine *line)
  277. {
  278. size_t maxlen = strlen(line->filename) + line->length, i, start, slashes;
  279. if (maxlen >= INT_MAX) // Allows us to safely downcast to int later
  280. return NULL;
  281. char *path = malloc(sizeof(char) * maxlen);
  282. if (!path)
  283. OUT_OF_MEMORY()
  284. if (!(i = DIRECTIVE_OFFSET(line, DIR_INCLUDE)))
  285. goto error;
  286. if (line->length - i <= 4) // Not long enough to hold a non-zero argument
  287. goto error;
  288. if (line->data[i++] != ' ' || line->data[i++] != '"')
  289. goto error;
  290. // TODO: parse escaped characters properly
  291. for (start = i, slashes = 0; i < line->length; i++) {
  292. if (line->data[i] == '"' && (slashes % 2) == 0)
  293. break;
  294. if (line->data[i] == '\\')
  295. slashes++;
  296. else
  297. slashes = 0;
  298. }
  299. if (i != line->length - 1) // Junk present after closing quote
  300. goto error;
  301. char *dup = strdup(line->filename);
  302. if (!dup)
  303. OUT_OF_MEMORY()
  304. // TODO: should normalize filenames in some way to prevent accidental dupes
  305. snprintf(path, maxlen, "%s/%.*s", dirname(dup), (int) (i - start),
  306. line->data + start);
  307. free(dup);
  308. return path;
  309. error:
  310. free(path);
  311. return NULL;
  312. }
  313. /*
  314. Return whether the given path has already been loaded.
  315. */
  316. static bool path_has_been_loaded(
  317. const char *path, const LineBuffer *root, const ASMInclude *include)
  318. {
  319. if (!strcmp(path, root->filename))
  320. return true;
  321. while (include) {
  322. if (!strcmp(path, include->lines->filename))
  323. return true;
  324. include = include->next;
  325. }
  326. return false;
  327. }
  328. /*
  329. Build a LineBuffer into a ASMLines, normalizing them along the way.
  330. This function operates recursively to handle includes, but handles no other
  331. preprocessor directives.
  332. On success, NULL is returned; *head points to the head of the new ASMLine
  333. list, and *tail to its tail (assuming it is non-NULL). On error, an
  334. ErrorInfo object is returned, and *head and *tail are not modified.
  335. *includes may be updated in either case.
  336. */
  337. static ErrorInfo* build_asm_lines(
  338. const LineBuffer *root, const LineBuffer *source, ASMLine **head,
  339. ASMLine **tail, ASMInclude **includes)
  340. {
  341. ASMLine dummy = {.next = NULL};
  342. ASMLine *line, *prev = &dummy;
  343. const Line *orig, *next_orig = source->lines;
  344. while ((orig = next_orig)) {
  345. line = normalize_line(orig->data, orig->length);
  346. next_orig = orig->next;
  347. if (!line)
  348. continue;
  349. // Populate ASMLine fields not set by normalize_line():
  350. line->original = orig;
  351. line->filename = source->filename;
  352. line->next = NULL;
  353. if (IS_DIRECTIVE(line, DIR_INCLUDE)) {
  354. ErrorInfo *ei;
  355. char *path = read_include_path(line);
  356. if (!path) {
  357. ei = error_info_create(line, ET_INCLUDE, ED_INC_BAD_ARG);
  358. free_asm_lines(line);
  359. free_asm_lines(dummy.next);
  360. return ei;
  361. }
  362. if (path_has_been_loaded(path, root, *includes)) {
  363. ei = error_info_create(line, ET_INCLUDE, ED_INC_RECURSION);
  364. free_asm_lines(line);
  365. free_asm_lines(dummy.next);
  366. free(path);
  367. return ei;
  368. }
  369. DEBUG("- reading included file: %s", path)
  370. LineBuffer *incbuffer = read_source_file(path, false);
  371. free(path);
  372. if (!incbuffer) {
  373. ei = error_info_create(line, ET_INCLUDE, ED_INC_FILE_READ);
  374. free_asm_lines(line);
  375. free_asm_lines(dummy.next);
  376. return ei;
  377. }
  378. ASMInclude *include = malloc(sizeof(ASMInclude));
  379. if (!include)
  380. OUT_OF_MEMORY()
  381. include->lines = incbuffer;
  382. include->next = *includes;
  383. *includes = include;
  384. ASMLine *inchead, *inctail;
  385. if ((ei = build_asm_lines(root, incbuffer, &inchead, &inctail,
  386. includes))) {
  387. error_info_append(ei, line);
  388. free_asm_lines(line);
  389. free_asm_lines(dummy.next);
  390. return ei;
  391. }
  392. prev->next = inchead;
  393. prev = inctail;
  394. free_asm_lines(line); // Destroy only the .include line
  395. }
  396. else {
  397. prev->next = line;
  398. prev = line;
  399. }
  400. }
  401. *head = dummy.next;
  402. if (tail)
  403. *tail = prev;
  404. return NULL;
  405. }
  406. /*
  407. Read in a boolean argument from the given line and store it in *result.
  408. auto_val is used if the argument's value is "auto". Return true on success
  409. and false on failure; in the latter case, *result is not modified.
  410. */
  411. static inline bool read_bool_argument(
  412. bool *result, const ASMLine *line, const char *directive, bool auto_val)
  413. {
  414. const char *arg = line->data + (DIRECTIVE_OFFSET(line, directive) + 1);
  415. ssize_t len = line->length - (DIRECTIVE_OFFSET(line, directive) + 1);
  416. if (len <= 0 || len > 5)
  417. return false;
  418. switch (len) {
  419. case 1: // 0, 1
  420. if (*arg == '0' || *arg == '1')
  421. return (*result = *arg - '0'), true;
  422. return false;
  423. case 2: // on
  424. if (!strncmp(arg, "on", 2))
  425. return (*result = true), true;
  426. return false;
  427. case 3: // off
  428. if (!strncmp(arg, "off", 3))
  429. return (*result = false), true;
  430. return false;
  431. case 4: // true, auto
  432. if (!strncmp(arg, "true", 4))
  433. return (*result = true), true;
  434. if (!strncmp(arg, "auto", 4))
  435. return (*result = auto_val), true;
  436. return false;
  437. case 5: // false
  438. if (!strncmp(arg, "false", 5))
  439. return (*result = false), true;
  440. return false;
  441. }
  442. return false;
  443. }
  444. /*
  445. Preprocess the LineBuffer into ASMLines. Change some state along the way.
  446. This function processes include directives, so read_source_file() may be
  447. called multiple times (along with the implications that has), and
  448. state->includes may be modified.
  449. On success, NULL is returned. On error, an ErrorInfo object is returned.
  450. state->lines and state->includes may still be modified.
  451. */
  452. static ErrorInfo* preprocess(AssemblerState *state, const LineBuffer *source)
  453. {
  454. // state->header.offset <-- check in list of acceptable values
  455. // state->header.product_code <-- range check
  456. // state->header.version <-- range check
  457. // state->header.region <-- string conversion, check
  458. // state->header.rom_size <-- value/range check
  459. // state->rom_size <-- value check
  460. // if giving rom size, check header offset is in rom size range
  461. // if giving reported and actual rom size, check reported is <= actual
  462. #define CATCH_DUPES(line, first, oldval, newval) \
  463. if (first && oldval != newval) { \
  464. ei = error_info_create(line, ET_PREPROC, ED_PP_DUPLICATE); \
  465. error_info_append(ei, first); \
  466. free_asm_lines(condemned); \
  467. return ei; \
  468. } \
  469. oldval = newval; \
  470. first = line;
  471. #define REQUIRE_ARG(line, d) \
  472. if (!DIRECTIVE_HAS_ARG(line, d)) { \
  473. free_asm_lines(condemned); \
  474. return error_info_create(line, ET_PREPROC, ED_PP_NO_ARG); \
  475. }
  476. #define VALIDATE(retval) \
  477. if (!(retval)) { \
  478. free_asm_lines(condemned); \
  479. return error_info_create(line, ET_PREPROC, ED_PP_BAD_ARG); \
  480. }
  481. DEBUG("Running preprocessor:")
  482. ErrorInfo* ei;
  483. if ((ei = build_asm_lines(source, source, &state->lines, NULL,
  484. &state->includes)))
  485. return ei;
  486. ASMLine dummy = {.next = state->lines};
  487. ASMLine *prev, *line = &dummy, *next = state->lines, *condemned = NULL;
  488. const ASMLine *first_optimizer = NULL, *first_checksum = NULL;
  489. while ((prev = line, line = next)) {
  490. next = line->next;
  491. if (line->data[0] == DIRECTIVE_MARKER) {
  492. if (IS_DIRECTIVE(line, DIR_ORIGIN))
  493. continue; // Origins are handled by tokenizer
  494. DEBUG("- handling directive: %.*s", (int) line->length, line->data)
  495. if (IS_DIRECTIVE(line, DIR_OPTIMIZER)) {
  496. REQUIRE_ARG(line, DIR_OPTIMIZER)
  497. bool arg;
  498. VALIDATE(read_bool_argument(&arg, line, DIR_OPTIMIZER, false))
  499. CATCH_DUPES(line, first_optimizer, state->optimizer, arg)
  500. }
  501. else if (IS_DIRECTIVE(line, DIR_ROM_SIZE)) {
  502. // TODO
  503. }
  504. else if (IS_DIRECTIVE(line, DIR_ROM_HEADER)) {
  505. // TODO
  506. }
  507. else if (IS_DIRECTIVE(line, DIR_ROM_CHECKSUM)) {
  508. REQUIRE_ARG(line, DIR_ROM_CHECKSUM)
  509. bool arg;
  510. VALIDATE(read_bool_argument(&arg, line, DIR_ROM_CHECKSUM, true))
  511. CATCH_DUPES(line, first_checksum, state->header.checksum, arg)
  512. }
  513. else if (IS_DIRECTIVE(line, DIR_ROM_PRODUCT)) {
  514. // TODO
  515. }
  516. else if (IS_DIRECTIVE(line, DIR_ROM_VERSION)) {
  517. // TODO
  518. }
  519. else if (IS_DIRECTIVE(line, DIR_ROM_REGION)) {
  520. // TODO
  521. }
  522. else if (IS_DIRECTIVE(line, DIR_ROM_DECLSIZE)) {
  523. // TODO
  524. }
  525. else {
  526. free_asm_lines(condemned);
  527. return error_info_create(line, ET_PREPROC, ED_PP_UNKNOWN);
  528. }
  529. // Remove directive from lines, and schedule it for deletion:
  530. line->next = condemned;
  531. condemned = line;
  532. prev->next = next;
  533. line = prev;
  534. }
  535. }
  536. state->rom_size = 8; // TODO
  537. free_asm_lines(condemned);
  538. state->lines = dummy.next; // Fix list head if first line was a directive
  539. #ifdef DEBUG_MODE
  540. DEBUG("Dumping ASMLines:")
  541. const ASMLine *temp = state->lines;
  542. while (temp) {
  543. DEBUG("- %-40.*s [%s:%02zu]", (int) temp->length, temp->data,
  544. temp->filename, temp->original->lineno)
  545. temp = temp->next;
  546. }
  547. #endif
  548. return NULL;
  549. #undef VALIDATE
  550. #undef REQUIRE_ARG
  551. #undef CATCH_DUPES
  552. }
  553. /*
  554. Tokenize ASMLines into ASMInstructions.
  555. On success, state->instructions is modified and NULL is returned. On error,
  556. an ErrorInfo object is returned and state->instructions is not modified.
  557. state->symtable may or may not be modified regardless of success.
  558. */
  559. static ErrorInfo* tokenize(AssemblerState *state)
  560. {
  561. // TODO
  562. // verify no instructions clash with header offset
  563. // if rom size is set, verify nothing overflows
  564. return NULL;
  565. }
  566. /*
  567. Resolve default placeholder values in assembler state, such as ROM size.
  568. On success, no new heap objects are allocated. On error, an ErrorInfo
  569. object is returned.
  570. */
  571. static ErrorInfo* resolve_defaults(AssemblerState *state)
  572. {
  573. // TODO
  574. // if (!state.rom_size)
  575. // set to max possible >= 32 KB, or error if too many instructions
  576. // if (state.header.rom_size)
  577. // check reported rom size is <= actual rom size
  578. // if (!state.header.rom_size)
  579. // set to actual rom size
  580. return NULL;
  581. }
  582. /*
  583. Resolve symbol placeholders in instructions such as jumps and branches.
  584. On success, no new heap objects are allocated. On error, an ErrorInfo
  585. object is returned.
  586. */
  587. static ErrorInfo* resolve_symbols(AssemblerState *state)
  588. {
  589. // TODO
  590. return NULL;
  591. }
  592. /*
  593. Convert finalized ASMInstructions into a binary data block.
  594. This function should never fail.
  595. */
  596. static void serialize_binary(AssemblerState *state, uint8_t *binary)
  597. {
  598. // TODO
  599. for (size_t i = 0; i < state->rom_size; i++)
  600. binary[i] = 'X';
  601. }
  602. /*
  603. Assemble the z80 source code in the source code buffer into binary data.
  604. If successful, return the size of the assembled binary data and change
  605. *binary_ptr to point to the assembled ROM data buffer. *binary_ptr must be
  606. free()'d when finished.
  607. If an error occurred, return 0 and update *ei_ptr to point to an ErrorInfo
  608. object which can be shown to the user with error_info_print(). The
  609. ErrorInfo object must be destroyed with error_info_destroy() when finished.
  610. In either case, only one of *binary_ptr and *ei_ptr is modified.
  611. */
  612. size_t assemble(const LineBuffer *source, uint8_t **binary_ptr, ErrorInfo **ei_ptr)
  613. {
  614. AssemblerState state;
  615. ErrorInfo *error_info;
  616. size_t retval = 0;
  617. init_state(&state);
  618. if ((error_info = preprocess(&state, source)))
  619. goto error;
  620. if (!(state.symtable = malloc(sizeof(ASMSymbolTable))))
  621. OUT_OF_MEMORY()
  622. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++)
  623. state.symtable->buckets[bucket] = NULL;
  624. if ((error_info = tokenize(&state)))
  625. goto error;
  626. if ((error_info = resolve_defaults(&state)))
  627. goto error;
  628. if ((error_info = resolve_symbols(&state)))
  629. goto error;
  630. uint8_t *binary = malloc(sizeof(uint8_t) * state.rom_size);
  631. if (!binary)
  632. OUT_OF_MEMORY()
  633. serialize_binary(&state, binary);
  634. *binary_ptr = binary;
  635. retval = state.rom_size;
  636. goto cleanup;
  637. error:
  638. *ei_ptr = error_info;
  639. cleanup:
  640. free_asm_lines(state.lines);
  641. free_asm_includes(state.includes);
  642. free_asm_instructions(state.instructions);
  643. free_asm_symtable(state.symtable);
  644. return retval;
  645. }
  646. /*
  647. Assemble the z80 source code at the input path into a binary file.
  648. Return true if the operation was a success and false if it was a failure.
  649. Errors are printed to STDOUT; if the operation was successful then nothing
  650. is printed.
  651. */
  652. bool assemble_file(const char *src_path, const char *dst_path)
  653. {
  654. LineBuffer *source = read_source_file(src_path, true);
  655. if (!source)
  656. return false;
  657. uint8_t *binary;
  658. ErrorInfo *error_info;
  659. size_t size = assemble(source, &binary, &error_info);
  660. free_line_buffer(source);
  661. if (!size) {
  662. error_info_print(error_info, stderr);
  663. error_info_destroy(error_info);
  664. return false;
  665. }
  666. bool success = write_binary_file(dst_path, binary, size);
  667. free(binary);
  668. return success;
  669. }