An emulator, assembler, and disassembler for the Sega Game Gear
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

104 lignes
2.2 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 "state.h"
  5. #include "io.h"
  6. #include "../logging.h"
  7. /*
  8. Initialize default values in an AssemblerState object.
  9. */
  10. void state_init(AssemblerState *state)
  11. {
  12. state->header.offset = DEFAULT_HEADER_OFFSET;
  13. state->header.checksum = true;
  14. state->header.product_code = 0;
  15. state->header.version = 0;
  16. state->header.region = DEFAULT_REGION;
  17. state->header.rom_size = 0;
  18. state->optimizer = false;
  19. state->rom_size = 0;
  20. state->lines = NULL;
  21. state->includes = NULL;
  22. state->instructions = NULL;
  23. state->symtable = NULL;
  24. }
  25. /*
  26. Initialize an ASMSymbolTable and place it in *symtable_ptr.
  27. */
  28. void asm_symtable_init(ASMSymbolTable **symtable_ptr)
  29. {
  30. ASMSymbolTable *symtable;
  31. if (!(symtable = malloc(sizeof(ASMSymbolTable))))
  32. OUT_OF_MEMORY()
  33. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++)
  34. symtable->buckets[bucket] = NULL;
  35. *symtable_ptr = symtable;
  36. }
  37. /*
  38. Deallocate an ASMLine list.
  39. */
  40. void asm_lines_free(ASMLine *line)
  41. {
  42. while (line) {
  43. ASMLine *temp = line->next;
  44. free(line->data);
  45. free(line);
  46. line = temp;
  47. }
  48. }
  49. /*
  50. Deallocate an ASMInclude list.
  51. */
  52. void asm_includes_free(ASMInclude *include)
  53. {
  54. while (include) {
  55. ASMInclude *temp = include->next;
  56. line_buffer_free(include->lines);
  57. free(include);
  58. include = temp;
  59. }
  60. }
  61. /*
  62. Deallocate an ASMInstruction list.
  63. */
  64. void asm_instructions_free(ASMInstruction *inst)
  65. {
  66. while (inst) {
  67. ASMInstruction *temp = inst->next;
  68. if (inst->symbol)
  69. free(inst->symbol);
  70. free(inst);
  71. inst = temp;
  72. }
  73. }
  74. /*
  75. Deallocate an ASMSymbolTable.
  76. */
  77. void asm_symtable_free(ASMSymbolTable *symtable)
  78. {
  79. if (!symtable)
  80. return;
  81. for (size_t bucket = 0; bucket < SYMBOL_TABLE_BUCKETS; bucket++) {
  82. ASMSymbol *sym = symtable->buckets[bucket], *temp;
  83. while (sym) {
  84. temp = sym->next;
  85. free(sym->symbol);
  86. free(sym);
  87. sym = temp;
  88. }
  89. }
  90. free(symtable);
  91. }