asm_struct.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * asm_struct.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "asm_struct.h"
  10. t_asm_register * allocRegister(int ID, int indirect)
  11. {
  12. t_asm_register *result;
  13. result = _ASM_ALLOC_FUNCTION(sizeof(t_asm_register));
  14. /* test the out of memory condition */
  15. if (result == NULL)
  16. return NULL;
  17. /* set the fields of the instance of `t_asm_register' */
  18. result->ID = ID;
  19. result->indirect = indirect;
  20. /* postconditions */
  21. return result;
  22. }
  23. t_asm_address * allocAddress(int displacement, t_asm_label *label)
  24. {
  25. t_asm_address *result;
  26. result = _ASM_ALLOC_FUNCTION(sizeof(t_asm_address));
  27. /* test the out of memory condition */
  28. if (result == NULL)
  29. return NULL;
  30. /* set the fields of the instance of `t_asm_address' */
  31. if (label == NULL)
  32. result->addr = displacement;
  33. else
  34. result->addr = 0;
  35. result->label = label;
  36. /* postconditions */
  37. return result;
  38. }
  39. t_asm_label * allocLabel(char *ID, void *data)
  40. {
  41. t_asm_label *result;
  42. result = _ASM_ALLOC_FUNCTION(sizeof(t_asm_label));
  43. /* test the out of memory condition */
  44. if (result == NULL)
  45. return NULL;
  46. /* set the fields of the instance of `t_asm_label' */
  47. result->data = data;
  48. result->ID = ID;
  49. /* postconditions */
  50. return result;
  51. }
  52. t_asm_instruction * allocInstruction(int opcode)
  53. {
  54. t_asm_instruction *result;
  55. result = _ASM_ALLOC_FUNCTION(sizeof(t_asm_instruction));
  56. /* test the out of memory condition */
  57. if (result == NULL)
  58. return NULL;
  59. /* set the fields of the instance of `t_asm_instruction' */
  60. result->opcode = opcode;
  61. result->reg_1 = NULL;
  62. result->reg_2 = NULL;
  63. result->reg_3 = NULL;
  64. result->immediate = 0;
  65. result->format = 0;
  66. result->address = NULL;
  67. result->user_comment = NULL;
  68. /* postconditions */
  69. return result;
  70. }
  71. t_asm_instruction * init_opcode3(int opcode, t_asm_register *reg_1
  72. , t_asm_register *reg_2, t_asm_register *reg_3)
  73. {
  74. t_asm_instruction *result;
  75. /* create an instance of `t_asm_instruction' */
  76. result = allocInstruction(opcode);
  77. /* test `out of memory' */
  78. if (result == NULL)
  79. return NULL;
  80. /* store the correct values for the instruction */
  81. result->format = ASM_FORMAT_TER;
  82. result->reg_1 = reg_1;
  83. result->reg_2 = reg_2;
  84. result->reg_3 = reg_3;
  85. /* postcondition: return the just created and initialized instance
  86. * of `t_asm_instruction' */
  87. return result;
  88. }
  89. t_asm_instruction * init_opcode2(int opcode, t_asm_register *reg_1
  90. , t_asm_register *reg_2, int immediate)
  91. {
  92. t_asm_instruction *result;
  93. /* create an instance of `t_asm_instruction' */
  94. result = allocInstruction(opcode);
  95. /* test `out of memory' */
  96. if (result == NULL)
  97. return NULL;
  98. /* store the correct values for the instruction */
  99. result->format = ASM_FORMAT_BIN;
  100. result->reg_1 = reg_1;
  101. result->reg_2 = reg_2;
  102. result->immediate = immediate;
  103. /* postcondition: return the just created and initialized instance
  104. * of `t_asm_instruction' */
  105. return result;
  106. }
  107. t_asm_instruction * init_opcodeI(int opcode, t_asm_register *reg_1
  108. , t_asm_address *addr)
  109. {
  110. t_asm_instruction *result;
  111. /* create an instance of `t_asm_instruction' */
  112. result = allocInstruction(opcode);
  113. /* test `out of memory' */
  114. if (result == NULL)
  115. return NULL;
  116. /* store the correct values for the instruction */
  117. result->format = ASM_FORMAT_UNR;
  118. result->reg_1 = reg_1;
  119. result->address = addr;
  120. /* postcondition: return the just created and initialized instance
  121. * of `t_asm_instruction' */
  122. return result;
  123. }
  124. t_asm_instruction * init_ccode(int opcode, t_asm_address *addr)
  125. {
  126. t_asm_instruction *result;
  127. /* create an instance of `t_asm_instruction' */
  128. result = allocInstruction(opcode);
  129. /* test `out of memory' */
  130. if (result == NULL)
  131. return NULL;
  132. /* store the correct values for the instruction */
  133. result->format = ASM_FORMAT_JMP;
  134. result->address = addr;
  135. /* postcondition: return the just created and initialized instance
  136. * of `t_asm_instruction' */
  137. return result;
  138. }
  139. t_asm_instruction * init_halt()
  140. {
  141. t_asm_instruction *result;
  142. /* create an instance of `t_asm_instruction' */
  143. result = allocInstruction(HALT_OP);
  144. /* test `out of memory' */
  145. if (result == NULL)
  146. return NULL;
  147. /* store the correct values for the instruction */
  148. result->format = ASM_FORMAT_NULL;
  149. /* postcondition: return the just created and initialized instance
  150. * of `t_asm_instruction' */
  151. return result;
  152. }
  153. t_asm_instruction * init_nop()
  154. {
  155. t_asm_instruction *result;
  156. /* create an instance of `t_asm_instruction' */
  157. result = allocInstruction(NOP_OP);
  158. /* test `out of memory' */
  159. if (result == NULL)
  160. return NULL;
  161. /* store the correct values for the instruction */
  162. result->format = ASM_FORMAT_NULL;
  163. /* postcondition: return the just created and initialized instance
  164. * of `t_asm_instruction' */
  165. return result;
  166. }
  167. t_asm_data * allocData(int dataType, int value)
  168. {
  169. t_asm_data *result;
  170. result = _ASM_ALLOC_FUNCTION(sizeof(t_asm_data));
  171. /* test the out of memory condition */
  172. if (result == NULL)
  173. return NULL;
  174. /* initialize the fields of `result' */
  175. result->dataType = dataType;
  176. result->value = value;
  177. /* postconditions: return result */
  178. return result;
  179. }
  180. void freeInstruction(t_asm_instruction *inst)
  181. {
  182. /* preconditions */
  183. if (inst == NULL)
  184. return;
  185. /* free memory */
  186. if (inst->reg_1 != NULL)
  187. _ASM_FREE_FUNCTION(inst->reg_1);
  188. if (inst->reg_2 != NULL)
  189. _ASM_FREE_FUNCTION(inst->reg_2);
  190. if (inst->reg_3 != NULL)
  191. _ASM_FREE_FUNCTION(inst->reg_3);
  192. if (inst->address != NULL)
  193. _ASM_FREE_FUNCTION(inst->address);
  194. _ASM_FREE_FUNCTION(inst);
  195. }
  196. /* finalize a data info. */
  197. void freeData(t_asm_data *data)
  198. {
  199. if (data != NULL)
  200. _ASM_FREE_FUNCTION(data);
  201. }