switch.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* $Header$ */
  2. /* 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 */
  3. #include <em.h>
  4. #include "debug.h"
  5. #include "botch_free.h"
  6. #include "density.h"
  7. #include "Lpars.h"
  8. #include "idf.h"
  9. #include "label.h"
  10. #include "arith.h"
  11. #include "switch.h"
  12. #include "code.h"
  13. #include "storage.h"
  14. #include "assert.h"
  15. #include "expr.h"
  16. #include "type.h"
  17. extern char options[];
  18. #define compact(nr, low, up) (nr != 0 && (up - low) / nr <= (DENSITY - 1))
  19. static struct switch_hdr *switch_stack = 0;
  20. /* (EB 86.05.20) The following rules hold for switch statements:
  21. - the expression E in "switch(E)" is cast to 'int' (RM 9.7)
  22. - the expression E in "case E:" must be 'int' (RM 9.7)
  23. - the values in the CSA/CSB tables are words (EM 7.4)
  24. For simplicity, we suppose int_size == word_size.
  25. */
  26. code_startswitch(expp)
  27. struct expr **expp;
  28. {
  29. /* Check the expression, stack a new case header and
  30. fill in the necessary fields.
  31. */
  32. register label l_table = text_label();
  33. register label l_break = text_label();
  34. register struct switch_hdr *sh = new_switch_hdr();
  35. int fund = any2arith(expp, SWITCH); /* INT, LONG or DOUBLE */
  36. switch (fund) {
  37. case LONG:
  38. if (options['R'])
  39. warning("long in switch (cast to int)");
  40. int2int(expp, int_type);
  41. break;
  42. #ifndef NOFLOAT
  43. case DOUBLE:
  44. error("float/double in switch");
  45. erroneous2int(expp);
  46. break;
  47. #endif NOFLOAT
  48. }
  49. stack_stmt(l_break, NO_LABEL);
  50. sh->sh_break = l_break;
  51. sh->sh_default = 0;
  52. sh->sh_table = l_table;
  53. sh->sh_nrofentries = 0;
  54. sh->sh_type = (*expp)->ex_type; /* the expression switched */
  55. sh->sh_lowerbd = sh->sh_upperbd = (arith)0; /* immaterial ??? */
  56. sh->sh_entries = (struct case_entry *) 0; /* case-entry list */
  57. sh->next = switch_stack; /* push onto switch-stack */
  58. switch_stack = sh;
  59. code_expr(*expp, RVAL, TRUE, NO_LABEL, NO_LABEL);
  60. /* evaluate the switch expr. */
  61. C_bra(l_table); /* goto start of switch_table */
  62. }
  63. code_endswitch()
  64. {
  65. register struct switch_hdr *sh = switch_stack;
  66. register label tablabel;
  67. register struct case_entry *ce;
  68. if (sh->sh_default == 0) /* no default occurred yet */
  69. sh->sh_default = sh->sh_break;
  70. C_bra(sh->sh_break); /* skip the switch table now */
  71. C_df_ilb(sh->sh_table); /* switch table entry */
  72. tablabel = data_label(); /* the rom must have a label */
  73. C_df_dlb(tablabel);
  74. C_rom_ilb(sh->sh_default);
  75. if (compact(sh->sh_nrofentries, sh->sh_lowerbd, sh->sh_upperbd)) {
  76. /* CSA */
  77. register arith val;
  78. C_rom_cst(sh->sh_lowerbd);
  79. C_rom_cst(sh->sh_upperbd - sh->sh_lowerbd);
  80. ce = sh->sh_entries;
  81. for (val = sh->sh_lowerbd; val <= sh->sh_upperbd; val++) {
  82. ASSERT(ce);
  83. if (val == ce->ce_value) {
  84. C_rom_ilb(ce->ce_label);
  85. ce = ce->next;
  86. }
  87. else
  88. C_rom_ilb(sh->sh_default);
  89. }
  90. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  91. C_csa(sh->sh_type->tp_size);
  92. }
  93. else { /* CSB */
  94. C_rom_cst((arith)sh->sh_nrofentries);
  95. for (ce = sh->sh_entries; ce; ce = ce->next) {
  96. /* generate the entries: value + prog.label */
  97. C_rom_cst(ce->ce_value);
  98. C_rom_ilb(ce->ce_label);
  99. }
  100. C_lae_dlb(tablabel, (arith)0); /* perform the switch */
  101. C_csb(sh->sh_type->tp_size);
  102. }
  103. C_df_ilb(sh->sh_break);
  104. switch_stack = sh->next; /* unstack the switch descriptor */
  105. /* free the allocated switch structure */
  106. ce = sh->sh_entries;
  107. while (ce) {
  108. register struct case_entry *tmp = ce->next;
  109. free_case_entry(ce);
  110. ce = tmp;
  111. }
  112. free_switch_hdr(sh);
  113. unstack_stmt();
  114. }
  115. code_case(expr)
  116. struct expr *expr;
  117. {
  118. register arith val;
  119. register struct case_entry *ce;
  120. register struct switch_hdr *sh = switch_stack;
  121. ASSERT(is_cp_cst(expr));
  122. if (sh == 0) {
  123. error("case statement not in switch");
  124. return;
  125. }
  126. if (expr->ex_flags & EX_ERROR) {
  127. /* is probably 0 anyway */
  128. return;
  129. }
  130. ch7cast(&expr, SWITCH, sh->sh_type);
  131. ce = new_case_entry();
  132. C_df_ilb(ce->ce_label = text_label());
  133. ce->ce_value = val = expr->VL_VALUE;
  134. if (sh->sh_entries == 0) {
  135. /* first case entry */
  136. ce->next = (struct case_entry *) 0;
  137. sh->sh_entries = ce;
  138. sh->sh_lowerbd = sh->sh_upperbd = val;
  139. sh->sh_nrofentries = 1;
  140. }
  141. else {
  142. /* second etc. case entry */
  143. /* find the proper place to put ce into the list */
  144. register struct case_entry *c1 = sh->sh_entries, *c2 = 0;
  145. if (val < sh->sh_lowerbd)
  146. sh->sh_lowerbd = val;
  147. else
  148. if (val > sh->sh_upperbd)
  149. sh->sh_upperbd = val;
  150. while (c1 && c1->ce_value < ce->ce_value) {
  151. c2 = c1;
  152. c1 = c1->next;
  153. }
  154. /* At this point three cases are possible:
  155. 1: c1 != 0 && c2 != 0:
  156. insert ce somewhere in the middle
  157. 2: c1 != 0 && c2 == 0:
  158. insert ce right after the head
  159. 3: c1 == 0 && c2 != 0:
  160. append ce to last element
  161. The case c1 == 0 && c2 == 0 cannot occur, since
  162. the list is guaranteed not to be empty.
  163. */
  164. if (c1) {
  165. if (c1->ce_value == ce->ce_value) {
  166. error("multiple case entry for value %ld",
  167. ce->ce_value);
  168. free_case_entry(ce);
  169. return;
  170. }
  171. if (c2) {
  172. ce->next = c2->next;
  173. c2->next = ce;
  174. }
  175. else {
  176. ce->next = sh->sh_entries;
  177. sh->sh_entries = ce;
  178. }
  179. }
  180. else {
  181. ASSERT(c2);
  182. ce->next = (struct case_entry *) 0;
  183. c2->next = ce;
  184. }
  185. (sh->sh_nrofentries)++;
  186. }
  187. }
  188. code_default()
  189. {
  190. register struct switch_hdr *sh = switch_stack;
  191. if (sh == 0) {
  192. error("default not in switch");
  193. return;
  194. }
  195. if (sh->sh_default != 0) {
  196. error("multiple entry for default in switch");
  197. return;
  198. }
  199. C_df_ilb(sh->sh_default = text_label());
  200. }