axe_array.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_array.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "axe_array.h"
  10. #include "axe_gencode.h"
  11. #include "symbol_table.h"
  12. #include "axe_utils.h"
  13. #include "axe_errors.h"
  14. void storeArrayElement(t_program_infos *program, char *ID
  15. , t_axe_expression index, t_axe_expression data)
  16. {
  17. int address;
  18. address = loadArrayAddress(program, ID, index);
  19. if (data.expression_type == REGISTER)
  20. {
  21. /* load the value indirectly into `mova_register' */
  22. gen_add_instruction(program, address, REG_0
  23. , data.value, CG_INDIRECT_DEST);
  24. }
  25. else
  26. {
  27. int imm_register;
  28. imm_register = gen_load_immediate(program, data.value);
  29. /* load the value indirectly into `load_register' */
  30. gen_add_instruction(program, address, REG_0
  31. ,imm_register, CG_INDIRECT_DEST);
  32. }
  33. }
  34. int loadArrayElement(t_program_infos *program
  35. , char *ID, t_axe_expression index)
  36. {
  37. int load_register;
  38. int address;
  39. /* retrieve the address of the array slot */
  40. address = loadArrayAddress(program, ID, index);
  41. /* get a new register */
  42. load_register = getNewRegister(program);
  43. /* load the value into `load_register' */
  44. gen_add_instruction(program, load_register, REG_0
  45. , address, CG_INDIRECT_SOURCE);
  46. /* return the register ID that holds the required data */
  47. return load_register;
  48. }
  49. int loadArrayAddress(t_program_infos *program
  50. , char *ID, t_axe_expression index)
  51. {
  52. int mova_register;
  53. t_axe_label *label;
  54. /* preconditions */
  55. if (program == NULL)
  56. notifyError(AXE_PROGRAM_NOT_INITIALIZED);
  57. if (ID == NULL)
  58. notifyError(AXE_VARIABLE_ID_UNSPECIFIED);
  59. /* retrieve the label associated with the given
  60. * identifier */
  61. label = getLabelFromVariableID(program, ID);
  62. /* test if an error occurred */
  63. if (label == NULL)
  64. return REG_INVALID;
  65. /* get a new register */
  66. mova_register = getNewRegister(program);
  67. /* generate the MOVA instruction */
  68. gen_mova_instruction(program, mova_register, label, 0);
  69. if (index.expression_type == IMMEDIATE)
  70. {
  71. if (index.value != 0)
  72. {
  73. if (is_int16(index.value))
  74. {
  75. gen_addi_instruction (program, mova_register
  76. , mova_register, index.value);
  77. }
  78. else
  79. {
  80. int tmp_reg = gen_load_immediate (program, index.value);
  81. gen_add_instruction (program, mova_register
  82. , mova_register, tmp_reg, CG_DIRECT_ALL);
  83. }
  84. }
  85. }
  86. else
  87. {
  88. assert(index.expression_type == REGISTER);
  89. /* We are making the following assumption:
  90. * the type can only be an INTEGER_TYPE */
  91. gen_add_instruction(program, mova_register, mova_register
  92. , index.value, CG_DIRECT_ALL);
  93. }
  94. /* return the identifier of the register that contains
  95. * the value of the array slot */
  96. return mova_register;
  97. }