axe_struct.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_struct.h
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #ifndef _AXE_STRUCT_H
  10. #define _AXE_STRUCT_H
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <assert.h>
  14. #include "axe_constants.h"
  15. #ifndef _AXE_ALLOC_FUNCTION
  16. # define _AXE_ALLOC_FUNCTION malloc
  17. #endif
  18. #ifndef _AXE_FREE_FUNCTION
  19. # define _AXE_FREE_FUNCTION free
  20. #endif
  21. typedef struct t_axe_label
  22. {
  23. int labelID; /* label identifier */
  24. } t_axe_label;
  25. typedef struct t_axe_register
  26. {
  27. int ID; /* an identifier of the register */
  28. int indirect; /* a boolean value: 1 if the register value is a pointer */
  29. }t_axe_register;
  30. typedef struct t_axe_address
  31. {
  32. int addr; /* a Program Counter */
  33. t_axe_label *labelID; /* a label identifier */
  34. int type; /* one of ADDRESS_TYPE or LABEL_TYPE */
  35. }t_axe_address;
  36. /* A structure that defines the internal data of a `Acse variable' */
  37. typedef struct t_axe_variable
  38. {
  39. int type; /* a valid data type @see `axe_constants.h' */
  40. int isArray; /* must be TRUE if the current variable is an array */
  41. int arraySize; /* the size of the array. This information is useful only
  42. * if the field `isArray' is TRUE */
  43. int init_val; /* initial value of the current variable. Actually it is
  44. * implemented as a integer value. `int' is
  45. * the only supported type at the moment,
  46. * future developments could consist of a modification of
  47. * the supported type system. Thus, maybe init_val will be
  48. * modified in future. */
  49. char *ID; /* variable identifier (should never be a NULL
  50. * pointer or an empty string "") */
  51. t_axe_label *labelID; /* a label that refers to the location
  52. * of the variable inside the data segment */
  53. } t_axe_variable;
  54. /* a simbolic assembly instruction */
  55. typedef struct t_axe_instruction
  56. {
  57. int opcode; /* instruction opcode (for example: AXE_ADD ) */
  58. t_axe_register *reg_1; /* destination register */
  59. t_axe_register *reg_2; /* first source register */
  60. t_axe_register *reg_3; /* second source register */
  61. int immediate; /* immediate value */
  62. t_axe_address *address; /* an address operand */
  63. char *user_comment; /* if defined it is set to the source code
  64. * instruction that generated the current
  65. * assembly. This string will be written
  66. * into the output code as a comment */
  67. t_axe_label *labelID; /* a label associated with the current
  68. * instruction */
  69. }t_axe_instruction;
  70. /* this structure is used in order to define assembler directives.
  71. * Directives are used in many cases such the definition of variables
  72. * inside the data segment. Every instance `t_axe_data' contains
  73. * all the informations about a single directive.
  74. * An example is the directive .word that is required when the assembler
  75. * must reserve a word of data inside the data segment. */
  76. typedef struct t_axe_data
  77. {
  78. int directiveType; /* the type of the current directive
  79. * (for example: DIR_WORD) */
  80. int value; /* the value associated with the directive */
  81. t_axe_label *labelID; /* label associated with the current data */
  82. }t_axe_data;
  83. typedef struct t_axe_expression
  84. {
  85. int value; /* an immediate value or a register identifier */
  86. int expression_type; /* actually only integer values are supported */
  87. } t_axe_expression;
  88. typedef struct t_axe_declaration
  89. {
  90. int isArray; /* must be TRUE if the current variable is an array */
  91. int arraySize; /* the size of the array. This information is useful only
  92. * if the field `isArray' is TRUE */
  93. int init_val; /* initial value of the current variable. */
  94. char *ID; /* variable identifier (should never be a NULL pointer
  95. * or an empty string "") */
  96. } t_axe_declaration;
  97. typedef struct t_while_statement
  98. {
  99. t_axe_label *label_condition; /* this label points to the expression
  100. * that is used as loop condition */
  101. t_axe_label *label_end; /* this label points to the instruction
  102. * that follows the while construct */
  103. } t_while_statement;
  104. /* create a label */
  105. extern t_axe_label * alloc_label(int value);
  106. /* create an expression */
  107. extern t_axe_expression create_expression (int value, int type);
  108. /* create an instance that will mantain infos about a while statement */
  109. extern t_while_statement create_while_statement();
  110. /* create an instance of `t_axe_register' */
  111. extern t_axe_register * alloc_register(int ID, int indirect);
  112. /* create an instance of `t_axe_instruction' */
  113. extern t_axe_instruction * alloc_instruction(int opcode);
  114. /* create an instance of `t_axe_address' */
  115. extern t_axe_address * alloc_address(int type, int address, t_axe_label *label);
  116. /* create an instance of `t_axe_data' */
  117. extern t_axe_data * alloc_data(int directiveType, int value, t_axe_label *label);
  118. /* create an instance of `t_axe_variable' */
  119. extern t_axe_variable * alloc_variable
  120. (char *ID, int type, int isArray, int arraySize, int init_val);
  121. /* finalize an instance of `t_axe_variable' */
  122. extern void free_variable (t_axe_variable *variable);
  123. /* create an instance of `t_axe_variable' */
  124. extern t_axe_declaration * alloc_declaration
  125. (char *ID, int isArray, int arraySize, int init_val);
  126. /* finalize an instruction info. */
  127. extern void free_Instruction(t_axe_instruction *inst);
  128. /* finalize a data info. */
  129. extern void free_Data(t_axe_data *data);
  130. #endif