switch.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "nofloat.h"
  9. #ifndef LINT
  10. #include <em.h>
  11. #else
  12. #include "l_em.h"
  13. #endif /* LINT */
  14. #include "debug.h"
  15. #include "botch_free.h"
  16. #include <alloc.h>
  17. #include "density.h"
  18. #include "Lpars.h"
  19. #include "idf.h"
  20. #include "label.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 "noRoption.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)" is cast to 'int' (RM 9.7)
  43. - the expression E in "case E:" must be 'int' (RM 9.7)
  44. - the values in the CSA/CSB tables are words (EM 7.4)
  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); /* INT, LONG or DOUBLE */
  57. switch (fund) {
  58. case LONG:
  59. #ifndef NOROPTION
  60. if (options['R'])
  61. warning("long in switch (cast to int)");
  62. #endif
  63. int2int(expp, int_type);
  64. break;
  65. #ifndef NOFLOAT
  66. case DOUBLE:
  67. error("float/double in switch");
  68. erroneous2int(expp);
  69. break;
  70. #endif /* NOFLOAT */
  71. }
  72. stack_stmt(l_break, NO_LABEL);
  73. sh->sh_break = l_break;
  74. sh->sh_default = 0;
  75. sh->sh_table = l_table;
  76. sh->sh_nrofentries = 0;
  77. sh->sh_type = (*expp)->ex_type; /* the expression switched */
  78. sh->sh_lowerbd = sh->sh_upperbd = (arith)0; /* immaterial ??? */
  79. sh->sh_entries = (struct case_entry *) 0; /* case-entry list */
  80. sh->sh_expr = *expp;
  81. #ifdef LINT
  82. code_expr(sh->sh_expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  83. #endif
  84. sh->next = switch_stack; /* push onto switch-stack */
  85. switch_stack = sh;
  86. C_bra(l_table); /* goto start of switch_table */
  87. }
  88. code_endswitch()
  89. {
  90. register struct switch_hdr *sh = switch_stack;
  91. register label tablabel;
  92. register struct case_entry *ce;
  93. if (sh->sh_default == 0) /* no default occurred yet */
  94. sh->sh_default = sh->sh_break;
  95. C_bra(sh->sh_break); /* skip the switch table now */
  96. C_df_ilb(sh->sh_table); /* switch table entry */
  97. /* evaluate the switch expr. */
  98. #ifndef LINT
  99. code_expr(sh->sh_expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  100. #endif
  101. tablabel = data_label(); /* the rom must have a label */
  102. C_df_dlb(tablabel);
  103. C_rom_ilb(sh->sh_default);
  104. if (compact(sh->sh_nrofentries, sh->sh_lowerbd, sh->sh_upperbd)) {
  105. /* CSA */
  106. register arith val;
  107. C_rom_cst(sh->sh_lowerbd);
  108. C_rom_cst(sh->sh_upperbd - sh->sh_lowerbd);
  109. ce = sh->sh_entries;
  110. if (sh->sh_nrofentries)
  111. for (val = sh->sh_lowerbd; val <= sh->sh_upperbd; val++) {
  112. ASSERT(ce);
  113. if (val == ce->ce_value) {
  114. C_rom_ilb(ce->ce_label);
  115. ce = ce->next;
  116. }
  117. else
  118. C_rom_ilb(sh->sh_default);
  119. }
  120. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  121. C_csa(sh->sh_type->tp_size);
  122. }
  123. else { /* CSB */
  124. C_rom_cst((arith)sh->sh_nrofentries);
  125. for (ce = sh->sh_entries; ce; ce = ce->next) {
  126. /* generate the entries: value + prog.label */
  127. C_rom_cst(ce->ce_value);
  128. C_rom_ilb(ce->ce_label);
  129. }
  130. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  131. C_csb(sh->sh_type->tp_size);
  132. }
  133. C_df_ilb(sh->sh_break);
  134. switch_stack = sh->next; /* unstack the switch descriptor */
  135. for (ce = sh->sh_entries; ce;) { /* free allocated switch structure */
  136. register struct case_entry *tmp = ce->next;
  137. free_case_entry(ce);
  138. ce = tmp;
  139. }
  140. free_switch_hdr(sh);
  141. unstack_stmt();
  142. }
  143. code_case(expr)
  144. struct expr *expr;
  145. {
  146. register arith val;
  147. register struct case_entry *ce;
  148. register struct switch_hdr *sh = switch_stack;
  149. ASSERT(is_cp_cst(expr));
  150. if (sh == 0) {
  151. error("case statement not in switch");
  152. return;
  153. }
  154. if (expr->ex_flags & EX_ERROR) /* is probably 0 anyway */
  155. return;
  156. ch7cast(&expr, CASE, sh->sh_type);
  157. ce = new_case_entry();
  158. C_df_ilb(ce->ce_label = text_label());
  159. ce->ce_value = val = expr->VL_VALUE;
  160. if (sh->sh_entries == 0) { /* first case entry */
  161. ce->next = (struct case_entry *) 0;
  162. sh->sh_entries = ce;
  163. sh->sh_lowerbd = sh->sh_upperbd = val;
  164. sh->sh_nrofentries = 1;
  165. }
  166. else { /* second etc. case entry; put ce into proper place */
  167. register struct case_entry *c1 = sh->sh_entries, *c2 = 0;
  168. if (val < sh->sh_lowerbd)
  169. sh->sh_lowerbd = val;
  170. else
  171. if (val > sh->sh_upperbd)
  172. sh->sh_upperbd = val;
  173. while (c1 && c1->ce_value < ce->ce_value) {
  174. c2 = c1;
  175. c1 = c1->next;
  176. }
  177. /* At this point three cases are possible:
  178. 1: c1 != 0 && c2 != 0: insert ce somewhere in the middle
  179. 2: c1 != 0 && c2 == 0: insert ce right after the head
  180. 3: c1 == 0 && c2 != 0: append ce to last element
  181. The case c1 == 0 && c2 == 0 cannot occur, since
  182. the list is guaranteed to be non-empty.
  183. */
  184. if (c1) {
  185. if (c1->ce_value == ce->ce_value) {
  186. error("multiple case entry for value %ld",
  187. ce->ce_value);
  188. free_case_entry(ce);
  189. return;
  190. }
  191. if (c2) {
  192. ce->next = c2->next;
  193. c2->next = ce;
  194. }
  195. else {
  196. ce->next = sh->sh_entries;
  197. sh->sh_entries = ce;
  198. }
  199. }
  200. else {
  201. ASSERT(c2);
  202. ce->next = (struct case_entry *) 0;
  203. c2->next = ce;
  204. }
  205. (sh->sh_nrofentries)++;
  206. }
  207. }
  208. code_default()
  209. {
  210. register struct switch_hdr *sh = switch_stack;
  211. if (sh == 0) {
  212. error("default statement not in switch");
  213. return;
  214. }
  215. if (sh->sh_default != 0) {
  216. error("multiple entry for default in switch");
  217. return;
  218. }
  219. C_df_ilb(sh->sh_default = text_label());
  220. }