axe_struct.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_struct.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "axe_struct.h"
  10. /* create an expression */
  11. t_axe_expression create_expression (int value, int type)
  12. {
  13. t_axe_expression expression;
  14. expression.value = value;
  15. expression.expression_type = type;
  16. return expression;
  17. }
  18. t_axe_expression create_expression_inf (int value, int inf, int type)
  19. {
  20. t_axe_expression expression;
  21. expression.value = value;
  22. expression.expression_type = type;
  23. expression.infty = inf;
  24. return expression;
  25. }
  26. /* create and initialize an instance of `t_axe_register' */
  27. t_axe_register * alloc_register(int ID, int indirect)
  28. {
  29. t_axe_register *result;
  30. /* create an instance of `t_axe_register' */
  31. result = (t_axe_register *)
  32. _AXE_ALLOC_FUNCTION(sizeof(t_axe_register));
  33. /* check the postconditions */
  34. if (result == NULL)
  35. return NULL;
  36. /* initialize the new label */
  37. result->ID = ID;
  38. result->indirect = indirect;
  39. /* return the label */
  40. return result;
  41. }
  42. /* create and initialize an instance of `t_axe_instruction' */
  43. t_axe_instruction * alloc_instruction(int opcode)
  44. {
  45. t_axe_instruction *result;
  46. /* create an instance of `t_axe_data' */
  47. result = (t_axe_instruction *) _AXE_ALLOC_FUNCTION(sizeof(t_axe_instruction));
  48. /* check the postconditions */
  49. if (result == NULL)
  50. return NULL;
  51. /* ininitialize the fields of `result' */
  52. result->opcode = opcode;
  53. result->reg_1 = NULL;
  54. result->reg_2 = NULL;
  55. result->reg_3 = NULL;
  56. result->immediate = 0;
  57. result->labelID = NULL;
  58. result->address = NULL;
  59. result->user_comment = NULL;
  60. /* return `result' */
  61. return result;
  62. }
  63. /* create and initialize an instance of `t_axe_data' */
  64. t_axe_data * alloc_data(int directiveType, int value, t_axe_label *label)
  65. {
  66. t_axe_data *result;
  67. /* create an instance of `t_axe_data' */
  68. result = (t_axe_data *) _AXE_ALLOC_FUNCTION(sizeof(t_axe_data));
  69. /* check the postconditions */
  70. if (result == NULL)
  71. return NULL;
  72. /* initialize the new directive */
  73. result->directiveType = directiveType;
  74. result->value = value;
  75. result->labelID = label;
  76. /* return the new data */
  77. return result;
  78. }
  79. t_while_statement create_while_statement()
  80. {
  81. t_while_statement statement;
  82. /* initialize the WHILE informations */
  83. statement.label_condition = NULL;
  84. statement.label_end = NULL;
  85. /* return a new instance of `t_while_statement' */
  86. return statement;
  87. }
  88. t_axe_label * alloc_label(int value)
  89. {
  90. t_axe_label *result;
  91. /* create an instance of t_axe_label */
  92. result = (t_axe_label *)
  93. _AXE_ALLOC_FUNCTION(sizeof(t_axe_label));
  94. /* initialize the internal value of `result' */
  95. result->labelID = value;
  96. /* return the just initialized new instance of `t_axe_label' */
  97. return result;
  98. }
  99. t_axe_declaration * alloc_declaration
  100. (char *ID, int isArray, int arraySize, int init_val)
  101. {
  102. t_axe_declaration *result;
  103. /* allocate memory for the new declaration */
  104. result = (t_axe_declaration *)
  105. _AXE_ALLOC_FUNCTION(sizeof(t_axe_declaration));
  106. /* check the postconditions */
  107. if (result == NULL)
  108. return NULL;
  109. /* initialize the content of `result' */
  110. result->isArray = isArray;
  111. result->arraySize = arraySize;
  112. result->ID = ID;
  113. result->init_val = init_val;
  114. /* return the just created and initialized instance of t_axe_declaration */
  115. return result;
  116. }
  117. /* finalize an instance of `t_axe_variable' */
  118. void free_variable (t_axe_variable *variable)
  119. {
  120. _AXE_FREE_FUNCTION(variable);
  121. }
  122. /* create and initialize an instance of `t_axe_variable' */
  123. t_axe_variable * alloc_variable
  124. (char *ID, int type, int isArray, int arraySize, int init_val)
  125. {
  126. t_axe_variable *result;
  127. /* allocate memory for the new variable */
  128. result = (t_axe_variable *)
  129. _AXE_ALLOC_FUNCTION(sizeof(t_axe_variable));
  130. /* check the postconditions */
  131. if (result == NULL)
  132. return NULL;
  133. /* initialize the content of `result' */
  134. result->type = type;
  135. result->isArray = isArray;
  136. result->arraySize = arraySize;
  137. result->ID = ID;
  138. result->init_val = init_val;
  139. result->labelID = NULL;
  140. /* return the just created and initialized instance of t_axe_variable */
  141. return result;
  142. }
  143. /* finalize an instruction info. */
  144. void free_Instruction(t_axe_instruction *inst)
  145. {
  146. /* preconditions */
  147. if (inst == NULL)
  148. return;
  149. /* free memory */
  150. if (inst->reg_1 != NULL)
  151. _AXE_FREE_FUNCTION(inst->reg_1);
  152. if (inst->reg_2 != NULL)
  153. _AXE_FREE_FUNCTION(inst->reg_2);
  154. if (inst->reg_3 != NULL)
  155. _AXE_FREE_FUNCTION(inst->reg_3);
  156. if (inst->address != NULL)
  157. _AXE_FREE_FUNCTION(inst->address);
  158. _AXE_FREE_FUNCTION(inst);
  159. }
  160. /* finalize a data info. */
  161. void free_Data(t_axe_data *data)
  162. {
  163. if (data != NULL)
  164. _AXE_FREE_FUNCTION(data);
  165. }
  166. t_axe_address * alloc_address(int type, int address, t_axe_label *label)
  167. {
  168. t_axe_address *result;
  169. result = (t_axe_address *)
  170. _AXE_ALLOC_FUNCTION(sizeof(t_axe_address));
  171. if (result == NULL)
  172. return NULL;
  173. /* initialize the new instance of `t_axe_address' */
  174. result->type = type;
  175. result->addr = address;
  176. result->labelID = label;
  177. /* return the new address */
  178. return result;
  179. }