asm_struct.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * asm_struct.h
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #ifndef _ASM_STRUCT_H
  10. #define _ASM_STRUCT_H
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include "asm_constants.h"
  16. #ifndef _ASM_ALLOC_FUNCTION
  17. # define _ASM_ALLOC_FUNCTION malloc
  18. #endif
  19. #ifndef _ASM_FREE_FUNCTION
  20. # define _ASM_FREE_FUNCTION free
  21. #endif
  22. #define t_asm_comment char
  23. typedef struct t_asm_register
  24. {
  25. int ID;
  26. int indirect;
  27. }t_asm_register;
  28. typedef struct t_asm_label
  29. {
  30. char *ID; /* identifier */
  31. void *data; /* a memory word referenced by this label */
  32. }t_asm_label;
  33. typedef struct t_asm_address
  34. {
  35. int addr;
  36. t_asm_label *label;
  37. }t_asm_address;
  38. typedef struct t_asm_instruction
  39. {
  40. int opcode;
  41. int format;
  42. t_asm_register *reg_1;
  43. t_asm_register *reg_2;
  44. t_asm_register *reg_3;
  45. int immediate;
  46. t_asm_address *address;
  47. t_asm_comment *user_comment;
  48. }t_asm_instruction;
  49. typedef struct t_asm_data
  50. {
  51. int dataType;
  52. int value;
  53. }t_asm_data;
  54. /* create an instance of `t_asm_register' */
  55. extern t_asm_register * allocRegister(int ID, int indirect);
  56. /* create an instance of `t_asm_address' */
  57. extern t_asm_address * allocAddress(int displacement, t_asm_label *label);
  58. /* create an instance of `t_asm_label' */
  59. extern t_asm_label * allocLabel(char *ID, void *data);
  60. /* create an instance of `t_asm_instruction' */
  61. extern t_asm_instruction * allocInstruction(int opcode);
  62. /* create an instance of `t_asm_data' */
  63. extern t_asm_data * allocData(int dataType, int value);
  64. /* initialize an instruction with three operands' */
  65. extern t_asm_instruction * init_opcode3(int opcode, t_asm_register *reg_1
  66. , t_asm_register *reg_2, t_asm_register *reg_3);
  67. /* initialize an instruction with two operands' */
  68. extern t_asm_instruction * init_opcode2(int opcode, t_asm_register *reg_1
  69. , t_asm_register *reg_2, int immediate);
  70. /* initialize an instruction with a single operand' */
  71. extern t_asm_instruction * init_opcodeI(int opcode, t_asm_register *reg_1
  72. , t_asm_address *addr);
  73. /* initialize a branch instruction */
  74. extern t_asm_instruction * init_ccode(int opcode, t_asm_address *addr);
  75. /* initialize a HALT instruction */
  76. extern t_asm_instruction * init_halt();
  77. /* initialize a NOP instruction */
  78. extern t_asm_instruction * init_nop();
  79. /* finalize an instruction info. */
  80. extern void freeInstruction(t_asm_instruction *inst);
  81. /* finalize a data info. */
  82. extern void freeData(t_asm_data *data);
  83. #endif