tes.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This file contains the main part of the top element size computation phase.
  3. *
  4. * Author: Hans van Eck.
  5. */
  6. #include <stdio.h>
  7. #include <em_spec.h>
  8. #include <em_mnem.h>
  9. #include <em_pseu.h>
  10. #include "param.h"
  11. #include "assert.h"
  12. #include "types.h"
  13. #include "tes.h"
  14. #include "alloc.h"
  15. #include "proinf.h"
  16. #include "line.h"
  17. #include "ext.h"
  18. #include "pop_push.h"
  19. #include "util.h"
  20. extern char *pop_push[];
  21. extern char flow_tab[];
  22. #define NON_CONTINUABLE(i) (flow_tab[i]&JUMP)
  23. #define ISABRANCH(i) (flow_tab[i]&HASLABEL)
  24. #define ISCONDBRANCH(i) (flow_tab[i]&CONDBRA)
  25. #define INSTR(lnp) (lnp->l_instr & BMASK)
  26. #define TYPE(lnp) lnp->l_optyp
  27. #define SHORT(lnp) lnp->l_a.la_short
  28. #define MINI(lnp) ((lnp->l_optyp & BMASK) - Z_OPMINI)
  29. #define IS_MINI(lnp) (lnp->l_optyp >= OPMINI)
  30. #define IS_LOC(l) (l!=(line_p) 0 && INSTR(l)==op_loc && IS_MINI(l))
  31. int state;
  32. static int stacktop = 0;
  33. void init_state()
  34. {
  35. stacktop = 0;
  36. state = KNOWN;
  37. }
  38. void tes_pseudos()
  39. {
  40. line_p lp;
  41. for (lp = pseudos; lp != (line_p)0; lp = lp->l_next) {
  42. switch(INSTR(lp)) {
  43. case ps_con:
  44. case ps_rom:
  45. if (lp->l_optyp == OPLIST) {
  46. arg_p ap = lp->l_a.la_arg;
  47. while (ap != (arg_p) 0) {
  48. if (ap->a_typ == ARGNUM) {
  49. assign_label(ap->a_a.a_np->n_repl);
  50. }
  51. ap = ap->a_next;
  52. }
  53. } else if (lp->l_optyp == OPNUMLAB)
  54. assign_label(lp->l_a.la_np->n_repl);
  55. }
  56. }
  57. }
  58. void tes_instr(line_p lnp, line_p x, line_p y)
  59. {
  60. char *s;
  61. int instr = INSTR(lnp);
  62. int arg, argdef;
  63. int neg = 0;
  64. if (instr == op_lab) {
  65. do_inst_label(lnp);
  66. return;
  67. }
  68. if (instr < sp_fmnem || instr > sp_lmnem) {
  69. return;
  70. }
  71. if (state == NOTREACHED) return; /* What else ? */
  72. s = pop_push[instr];
  73. if (*s != '0') {
  74. while (*s != '\0') {
  75. neg = (*s++ == '-');
  76. if (TYPE(lnp) == OPSHORT) {
  77. arg = SHORT(lnp);
  78. if (arg < wordsize) arg = wordsize;
  79. argdef = TRUE;
  80. } else if (IS_MINI(lnp)) {
  81. arg = MINI(lnp);
  82. if (arg > 0 && arg < wordsize) arg = wordsize;
  83. if (arg < 0 && -arg < wordsize) arg = -wordsize;
  84. argdef = TRUE;
  85. } else {
  86. argdef = FALSE;
  87. }
  88. switch (*s++) {
  89. case 'w': stacktop = wordsize; break;
  90. case 'd': stacktop = wordsize * 2; break;
  91. case 'p': stacktop = pointersize; break;
  92. case 'a':
  93. if (argdef == FALSE || instr == op_ass) {
  94. stacktop = 0;
  95. } else {
  96. stacktop = arg;
  97. }
  98. break;
  99. case 'x':
  100. if (IS_LOC(x)) {
  101. arg = MINI(x);
  102. if (arg < wordsize) arg = wordsize;
  103. stacktop = arg;
  104. } else {
  105. stacktop = 0;
  106. }
  107. break;
  108. case 'y':
  109. if (IS_LOC(y)) {
  110. arg = MINI(y);
  111. if (arg < wordsize) arg = wordsize;
  112. stacktop = arg;
  113. } else {
  114. stacktop = 0;
  115. }
  116. break;
  117. case '?':
  118. stacktop = 0;
  119. break;
  120. default:
  121. assert(FALSE);
  122. }
  123. }
  124. /*
  125. * When the last argument was negative, the element size
  126. * must be negated. This is to catch 'asp -4'.
  127. */
  128. if (neg) stacktop = -stacktop;
  129. }
  130. if (stacktop < 0) stacktop = 0;
  131. if (ISABRANCH(instr)) do_inst_label(lnp);
  132. if (NON_CONTINUABLE(instr)) {
  133. state = NOTREACHED;
  134. stacktop = 0;
  135. }
  136. }
  137. void assign_label(num_p label)
  138. {
  139. if (label->n_flags & NUMSET) {
  140. if (state == NOTREACHED || stacktop > label->n_size) {
  141. stacktop = label->n_size;
  142. } else if ( stacktop < label->n_size) {
  143. label->n_size = stacktop;
  144. }
  145. } else {
  146. label->n_size = stacktop;
  147. label->n_flags |= NUMSET;
  148. }
  149. }
  150. void do_inst_label(line_p lnp) /* (re-)install a label */
  151. {
  152. num_p label = lnp->l_a.la_np->n_repl;
  153. int instr = INSTR(lnp);
  154. assign_label(label);
  155. if (instr == op_lab) {
  156. if (state == NOTREACHED) {
  157. } else {
  158. label->n_flags |= NUMFALLTHROUGH;
  159. }
  160. } else if (ISCONDBRANCH(instr)) { /* conditional branch */
  161. label->n_flags |= NUMCOND;
  162. }
  163. state = KNOWN;
  164. }