switch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* S W I T C H - S T A T E M E N T A D M I N I S T R A T I O N */
  7. #include "lint.h"
  8. #ifndef LINT
  9. #include <em.h>
  10. #else
  11. #include "l_em.h"
  12. #endif /* LINT */
  13. #include "debug.h"
  14. #include "botch_free.h"
  15. #include <alloc.h>
  16. #include "density.h"
  17. #include "Lpars.h"
  18. #include "idf.h"
  19. #include "label.h"
  20. #include <flt_arith.h>
  21. #include "arith.h"
  22. #include "switch.h"
  23. #include "code.h"
  24. #include "assert.h"
  25. #include "expr.h"
  26. #include "type.h"
  27. #include "sizes.h"
  28. extern char options[];
  29. int density = DENSITY;
  30. compact(nr, low, up)
  31. arith low, up;
  32. {
  33. /* Careful! up - low might not fit in an arith. And then,
  34. the test "up-low < 0" might also not work to detect this
  35. situation! Or is this just a bug in the M68020/M68000?
  36. */
  37. arith diff = up - low;
  38. return (nr == 0 || (diff >= 0 && diff / nr <= (density - 1)));
  39. }
  40. static struct switch_hdr *switch_stack = 0;
  41. /* (EB 86.05.20) The following rules hold for switch statements:
  42. - the expression E in "switch(E)" shall have integral type (3.6.4.2)
  43. - the expression E in "case E:" is converted to the promoted type
  44. of the controlling expression
  45. For simplicity, we suppose int_size == word_size.
  46. */
  47. code_startswitch(expp)
  48. struct expr **expp;
  49. {
  50. /* Check the expression, stack a new case header and
  51. fill in the necessary fields.
  52. */
  53. register label l_table = text_label();
  54. register label l_break = text_label();
  55. register struct switch_hdr *sh = new_switch_hdr();
  56. int fund = any2arith(expp, SWITCH);
  57. /* INT, LONG, FLOAT, DOUBLE or LNGDBL */
  58. switch (fund) {
  59. case FLOAT:
  60. case DOUBLE:
  61. case LNGDBL:
  62. error("floating point type in switch");
  63. erroneous2int(expp);
  64. break;
  65. }
  66. stack_stmt(l_break, NO_LABEL);
  67. sh->sh_break = l_break;
  68. /* sh->sh_default = 0; */
  69. sh->sh_table = l_table;
  70. /* sh->sh_nrofentries = 0; */
  71. sh->sh_type = (*expp)->ex_type; /* the expression switched */
  72. /* sh->sh_entries = (struct case_entry *) 0; -- case-entry list */
  73. sh->sh_expr = *expp;
  74. #ifdef LINT
  75. code_expr(sh->sh_expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  76. #endif
  77. sh->next = switch_stack; /* push onto switch-stack */
  78. switch_stack = sh;
  79. C_bra(l_table); /* goto start of switch_table */
  80. }
  81. extern char *long2str();
  82. code_endswitch()
  83. {
  84. register struct switch_hdr *sh = switch_stack;
  85. register label tablabel;
  86. register struct case_entry *ce;
  87. arith size = sh->sh_type->tp_size;
  88. if (sh->sh_default == 0) /* no default occurred yet */
  89. sh->sh_default = sh->sh_break;
  90. #ifndef LINT
  91. C_bra(sh->sh_break); /* skip the switch table now */
  92. C_df_ilb(sh->sh_table); /* switch table entry */
  93. /* evaluate the switch expr. */
  94. code_expr(sh->sh_expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  95. if (sh->sh_nrofentries <= 1) {
  96. if (sh->sh_nrofentries) {
  97. load_cst(sh->sh_lowerbd, size);
  98. C_cms(size);
  99. C_zeq(sh->sh_entries->ce_label);
  100. }
  101. else C_asp(size);
  102. C_bra(sh->sh_default);
  103. }
  104. else {
  105. tablabel = data_label(); /* the rom must have a label */
  106. C_df_dlb(tablabel);
  107. C_rom_ilb(sh->sh_default);
  108. if (compact(sh->sh_nrofentries, sh->sh_lowerbd, sh->sh_upperbd)) {
  109. /* CSA */
  110. register arith val;
  111. C_rom_icon(long2str((long)sh->sh_lowerbd,10), size);
  112. C_rom_icon(long2str((long)(sh->sh_upperbd - sh->sh_lowerbd),10),
  113. size);
  114. ce = sh->sh_entries;
  115. for (val = sh->sh_lowerbd; val <= sh->sh_upperbd; val++) {
  116. ASSERT(ce);
  117. if (val == ce->ce_value) {
  118. C_rom_ilb(ce->ce_label);
  119. ce = ce->next;
  120. }
  121. else
  122. C_rom_ilb(sh->sh_default);
  123. }
  124. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  125. C_csa(size);
  126. }
  127. else { /* CSB */
  128. C_rom_icon(long2str((long)sh->sh_nrofentries,10),size);
  129. for (ce = sh->sh_entries; ce; ce = ce->next) {
  130. /* generate the entries: value + prog.label */
  131. C_rom_icon(long2str((long)ce->ce_value,10),size);
  132. C_rom_ilb(ce->ce_label);
  133. }
  134. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  135. C_csb(size);
  136. }
  137. }
  138. C_df_ilb(sh->sh_break);
  139. #endif
  140. switch_stack = sh->next; /* unstack the switch descriptor */
  141. for (ce = sh->sh_entries; ce;) { /* free allocated switch structure */
  142. register struct case_entry *tmp = ce->next;
  143. free_case_entry(ce);
  144. ce = tmp;
  145. }
  146. free_switch_hdr(sh);
  147. unstack_stmt();
  148. }
  149. code_case(expr)
  150. struct expr *expr;
  151. {
  152. register arith val;
  153. register struct case_entry *ce;
  154. register struct switch_hdr *sh = switch_stack;
  155. ASSERT(is_cp_cst(expr));
  156. if (sh == 0) {
  157. error("case statement not in switch");
  158. return;
  159. }
  160. if (expr->ex_flags & EX_ERROR) /* is probably 0 anyway */
  161. return;
  162. ch3cast(&expr, CASE, sh->sh_type);
  163. ce = new_case_entry();
  164. C_df_ilb(ce->ce_label = text_label());
  165. ce->ce_value = val = expr->VL_VALUE;
  166. if (sh->sh_entries == 0) { /* first case entry */
  167. /* ce->next = (struct case_entry *) 0; */
  168. sh->sh_entries = ce;
  169. sh->sh_lowerbd = sh->sh_upperbd = val;
  170. sh->sh_nrofentries = 1;
  171. }
  172. else { /* second etc. case entry; put ce into proper place */
  173. register struct case_entry *c1 = sh->sh_entries, *c2 = 0;
  174. if (val < sh->sh_lowerbd)
  175. sh->sh_lowerbd = val;
  176. else
  177. if (val > sh->sh_upperbd)
  178. sh->sh_upperbd = val;
  179. while (c1 && c1->ce_value < ce->ce_value) {
  180. c2 = c1;
  181. c1 = c1->next;
  182. }
  183. /* At this point three cases are possible:
  184. 1: c1 != 0 && c2 != 0: insert ce somewhere in the middle
  185. 2: c1 != 0 && c2 == 0: insert ce right after the head
  186. 3: c1 == 0 && c2 != 0: append ce to last element
  187. The case c1 == 0 && c2 == 0 cannot occur, since
  188. the list is guaranteed to be non-empty.
  189. */
  190. if (c1) {
  191. if (c1->ce_value == ce->ce_value) {
  192. error("multiple case entry for value %ld",
  193. ce->ce_value);
  194. free_case_entry(ce);
  195. return;
  196. }
  197. if (c2) {
  198. ce->next = c2->next;
  199. c2->next = ce;
  200. }
  201. else {
  202. ce->next = sh->sh_entries;
  203. sh->sh_entries = ce;
  204. }
  205. }
  206. else {
  207. ASSERT(c2);
  208. ce->next = (struct case_entry *) 0;
  209. c2->next = ce;
  210. }
  211. (sh->sh_nrofentries)++;
  212. }
  213. }
  214. code_default()
  215. {
  216. register struct switch_hdr *sh = switch_stack;
  217. if (sh == 0) {
  218. error("default statement not in switch");
  219. return;
  220. }
  221. if (sh->sh_default != 0) {
  222. error("multiple entry for default in switch");
  223. return;
  224. }
  225. C_df_ilb(sh->sh_default = text_label());
  226. }