casestat.C 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* C A S E S T A T E M E N T C O D E G E N E R A T I O N */
  2. #include "debug.h"
  3. #include <alloc.h>
  4. #include <assert.h>
  5. #include <em.h>
  6. #include "LLlex.h"
  7. #include "Lpars.h"
  8. #include "chk_expr.h"
  9. #include "density.h"
  10. #include "main.h"
  11. #include "node.h"
  12. #include "type.h"
  13. struct case_hdr {
  14. struct case_hdr *ch_next; /* in the free list */
  15. int ch_nrofentries; /* number of cases */
  16. struct type *ch_type; /* type of case expression */
  17. arith ch_lowerbd; /* lowest case label */
  18. arith ch_upperbd; /* highest case label */
  19. struct case_entry *ch_entries; /* the cases */
  20. };
  21. /* ALLOCDEF "case_hdr" 5 */
  22. struct case_entry {
  23. struct case_entry *ce_next; /* next in list */
  24. arith ce_value; /* value of case label */
  25. label ce_label; /* generated label */
  26. };
  27. /* ALLOCDEF "case_entry" 10 */
  28. /* The constant DENSITY determines when CSA and when CSB instructions
  29. are generated. Reasonable values are: 2, 3, 4.
  30. On machines that have lots of address space and memory, higher values
  31. might also be reasonable. On these machines the density of jump tables
  32. may be lower.
  33. */
  34. #define compact(nr, low, up) (nr != 0 && (up - low) / nr <= DENSITY)
  35. CaseExpr(nd)
  36. struct node *nd;
  37. {
  38. /* Check the expression and generate code for it
  39. */
  40. register struct node *expp = nd->nd_left;
  41. if( !ChkExpression(expp) ) return;
  42. if( !(expp->nd_type->tp_fund & T_ORDINAL) ) {
  43. node_error(expp, "case-expression must be ordinal");
  44. return;
  45. }
  46. if( !err_occurred ) {
  47. CodePExpr(expp);
  48. C_bra(nd->nd_lab);
  49. }
  50. }
  51. CaseEnd(nd, exit_label)
  52. struct node *nd;
  53. label exit_label;
  54. {
  55. /* Stack a new case header and fill in the necessary fields.
  56. */
  57. register struct case_hdr *ch = new_case_hdr();
  58. register struct node *right;
  59. assert(nd->nd_class == Link && nd->nd_symb == CASE);
  60. ch->ch_type = nd->nd_left->nd_type;
  61. right = nd->nd_right;
  62. /* Now, create case label list
  63. */
  64. while( right ) {
  65. assert(right->nd_class == Link && right->nd_symb == ':');
  66. if( !AddCases(ch, right->nd_left, right->nd_lab) ) {
  67. FreeCh(ch);
  68. return;
  69. }
  70. right = right->nd_right;
  71. }
  72. if( !err_occurred )
  73. CaseCode(nd->nd_lab, ch, exit_label);
  74. FreeCh(ch);
  75. }
  76. FreeCh(ch)
  77. register struct case_hdr *ch;
  78. {
  79. /* free the allocated case structure
  80. */
  81. register struct case_entry *ce;
  82. ce = ch->ch_entries;
  83. while( ce ) {
  84. struct case_entry *tmp = ce->ce_next;
  85. free_case_entry(ce);
  86. ce = tmp;
  87. }
  88. free_case_hdr(ch);
  89. }
  90. AddCases(ch, nd, CaseLabel)
  91. register struct case_hdr *ch;
  92. register struct node *nd;
  93. label CaseLabel;
  94. {
  95. while( nd ) {
  96. if( !AddOneCase(ch, nd, CaseLabel) )
  97. return 0;
  98. nd = nd->nd_next;
  99. }
  100. return 1;
  101. }
  102. AddOneCase(ch, nd, lbl)
  103. register struct case_hdr *ch;
  104. register struct node *nd;
  105. label lbl;
  106. {
  107. register struct case_entry *ce = new_case_entry();
  108. register struct case_entry *c1 = ch->ch_entries, *c2 = 0;
  109. ce->ce_value = nd->nd_INT;
  110. ce->ce_label = lbl;
  111. if( !TstCompat(ch->ch_type, nd->nd_type) ) {
  112. node_error(nd, "case-statement: type incompatibility in case");
  113. free_case_entry(ce);
  114. return 0;
  115. }
  116. if( bounded(ch->ch_type) ) {
  117. arith lo, hi;
  118. getbounds(ch->ch_type, &lo, &hi);
  119. if( ce->ce_value < lo || ce->ce_value > hi )
  120. warning("case-statement: constant out of bounds");
  121. }
  122. if( !ch->ch_entries ) {
  123. /* first case entry
  124. */
  125. ce->ce_next = (struct case_entry *) 0;
  126. ch->ch_entries = ce;
  127. ch->ch_lowerbd = ch->ch_upperbd = ce->ce_value;
  128. ch->ch_nrofentries = 1;
  129. }
  130. else {
  131. /* second etc. case entry
  132. find the proper place to put ce into the list
  133. */
  134. if( ce->ce_value < ch->ch_lowerbd )
  135. ch->ch_lowerbd = ce->ce_value;
  136. else if( ce->ce_value > ch->ch_upperbd )
  137. ch->ch_upperbd = ce->ce_value;
  138. while( c1 && c1->ce_value < ce->ce_value ) {
  139. c2 = c1;
  140. c1 = c1->ce_next;
  141. }
  142. /* At this point three cases are possible:
  143. 1: c1 != 0 && c2 != 0:
  144. insert ce somewhere in the middle
  145. 2: c1 != 0 && c2 == 0:
  146. insert ce right after the head
  147. 3: c1 == 0 && c2 != 0:
  148. append ce to last element
  149. The case c1 == 0 && c2 == 0 cannot occur, since
  150. the list is guaranteed not to be empty.
  151. */
  152. if( c1 ) {
  153. if( c1->ce_value == ce->ce_value ) {
  154. node_error(nd,
  155. "case-statement: multiple case entry");
  156. free_case_entry(ce);
  157. return 0;
  158. }
  159. if( c2 ) {
  160. ce->ce_next = c2->ce_next;
  161. c2->ce_next = ce;
  162. }
  163. else {
  164. ce->ce_next = ch->ch_entries;
  165. ch->ch_entries = ce;
  166. }
  167. }
  168. else {
  169. assert(c2);
  170. ce->ce_next = (struct case_entry *) 0;
  171. c2->ce_next = ce;
  172. }
  173. (ch->ch_nrofentries)++;
  174. }
  175. return 1;
  176. }
  177. CaseCode(lbl, ch, exit_label)
  178. label lbl;
  179. struct case_hdr *ch;
  180. label exit_label;
  181. {
  182. label CaseDescrLab = ++data_label; /* rom must have a label */
  183. register struct case_entry *ce;
  184. register arith val;
  185. C_df_dlb(CaseDescrLab);
  186. C_rom_icon("0", pointer_size);
  187. if( compact(ch->ch_nrofentries, ch->ch_lowerbd, ch->ch_upperbd) ) {
  188. /* CSA */
  189. C_rom_cst(ch->ch_lowerbd);
  190. C_rom_cst(ch->ch_upperbd - ch->ch_lowerbd);
  191. ce = ch->ch_entries;
  192. for( val = ch->ch_lowerbd; val <= ch->ch_upperbd; val++ ) {
  193. assert(ce);
  194. if( val == ce->ce_value ) {
  195. C_rom_ilb(ce->ce_label);
  196. ce = ce->ce_next;
  197. }
  198. else
  199. C_rom_icon("0", pointer_size);
  200. }
  201. C_df_ilb(lbl);
  202. C_lae_dlb(CaseDescrLab, (arith) 0);
  203. C_csa(word_size);
  204. }
  205. else {
  206. /* CSB */
  207. C_rom_cst((arith) ch->ch_nrofentries);
  208. for( ce = ch->ch_entries; ce; ce = ce->ce_next ) {
  209. C_rom_cst(ce->ce_value);
  210. C_rom_ilb(ce->ce_label);
  211. }
  212. C_df_ilb(lbl);
  213. C_lae_dlb(CaseDescrLab, (arith) 0);
  214. C_csb(word_size);
  215. }
  216. C_df_ilb(exit_label);
  217. }