axe_struct.c 5.0 KB

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