1987-03-10 17:51:10 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1986-03-10 13:07:55 +00:00
|
|
|
/* $Header$ */
|
|
|
|
/* 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 */
|
|
|
|
|
1986-09-24 13:53:16 +00:00
|
|
|
#include "nofloat.h"
|
1986-03-25 16:40:43 +00:00
|
|
|
#include <em.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "botch_free.h"
|
1987-01-24 00:25:56 +00:00
|
|
|
#include <alloc.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "density.h"
|
1986-03-14 16:15:16 +00:00
|
|
|
#include "Lpars.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "idf.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "switch.h"
|
|
|
|
#include "code.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "type.h"
|
1987-03-25 23:14:43 +00:00
|
|
|
#include "noRoption.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-03-14 16:15:16 +00:00
|
|
|
extern char options[];
|
|
|
|
|
1987-10-05 10:17:44 +00:00
|
|
|
compact(nr, low, up)
|
|
|
|
arith low, up;
|
|
|
|
{
|
|
|
|
/* Careful! up - low might not fit in an arith. And then,
|
|
|
|
the test "up-low < 0" might also not work to detect this
|
|
|
|
situation! Or is this just a bug in the M68020/M68000?
|
|
|
|
*/
|
|
|
|
arith diff = up - low;
|
|
|
|
|
1987-11-13 16:21:33 +00:00
|
|
|
return (nr == 0 || (diff >= 0 && diff / nr <= (DENSITY - 1)));
|
1987-10-05 10:17:44 +00:00
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
static struct switch_hdr *switch_stack = 0;
|
|
|
|
|
1986-05-28 08:40:06 +00:00
|
|
|
/* (EB 86.05.20) The following rules hold for switch statements:
|
|
|
|
- the expression E in "switch(E)" is cast to 'int' (RM 9.7)
|
|
|
|
- the expression E in "case E:" must be 'int' (RM 9.7)
|
|
|
|
- the values in the CSA/CSB tables are words (EM 7.4)
|
|
|
|
For simplicity, we suppose int_size == word_size.
|
|
|
|
*/
|
|
|
|
|
1986-09-02 15:22:54 +00:00
|
|
|
code_startswitch(expp)
|
|
|
|
struct expr **expp;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-03-14 16:15:16 +00:00
|
|
|
/* Check the expression, stack a new case header and
|
|
|
|
fill in the necessary fields.
|
1986-03-10 13:07:55 +00:00
|
|
|
*/
|
|
|
|
register label l_table = text_label();
|
|
|
|
register label l_break = text_label();
|
|
|
|
register struct switch_hdr *sh = new_switch_hdr();
|
1986-09-02 15:22:54 +00:00
|
|
|
int fund = any2arith(expp, SWITCH); /* INT, LONG or DOUBLE */
|
1986-03-14 16:15:16 +00:00
|
|
|
|
1986-09-29 14:01:34 +00:00
|
|
|
switch (fund) {
|
1986-03-14 16:15:16 +00:00
|
|
|
case LONG:
|
1987-03-25 23:14:43 +00:00
|
|
|
#ifndef NOROPTION
|
1986-03-14 16:15:16 +00:00
|
|
|
if (options['R'])
|
1986-05-28 08:40:06 +00:00
|
|
|
warning("long in switch (cast to int)");
|
1987-03-25 23:14:43 +00:00
|
|
|
#endif
|
1986-09-02 15:22:54 +00:00
|
|
|
int2int(expp, int_type);
|
1986-03-14 16:15:16 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-14 16:15:16 +00:00
|
|
|
case DOUBLE:
|
|
|
|
error("float/double in switch");
|
1986-09-02 15:22:54 +00:00
|
|
|
erroneous2int(expp);
|
1986-03-14 16:15:16 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-03-14 16:15:16 +00:00
|
|
|
}
|
1986-07-18 21:10:42 +00:00
|
|
|
stack_stmt(l_break, NO_LABEL);
|
1986-03-10 13:07:55 +00:00
|
|
|
sh->sh_break = l_break;
|
|
|
|
sh->sh_default = 0;
|
|
|
|
sh->sh_table = l_table;
|
|
|
|
sh->sh_nrofentries = 0;
|
1986-09-02 15:22:54 +00:00
|
|
|
sh->sh_type = (*expp)->ex_type; /* the expression switched */
|
1986-03-14 16:15:16 +00:00
|
|
|
sh->sh_lowerbd = sh->sh_upperbd = (arith)0; /* immaterial ??? */
|
1986-03-10 13:07:55 +00:00
|
|
|
sh->sh_entries = (struct case_entry *) 0; /* case-entry list */
|
|
|
|
sh->next = switch_stack; /* push onto switch-stack */
|
|
|
|
switch_stack = sh;
|
1986-09-29 14:01:34 +00:00
|
|
|
/* evaluate the switch expr. */
|
1986-09-02 15:22:54 +00:00
|
|
|
code_expr(*expp, RVAL, TRUE, NO_LABEL, NO_LABEL);
|
1986-03-10 13:07:55 +00:00
|
|
|
C_bra(l_table); /* goto start of switch_table */
|
|
|
|
}
|
|
|
|
|
|
|
|
code_endswitch()
|
|
|
|
{
|
|
|
|
register struct switch_hdr *sh = switch_stack;
|
|
|
|
register label tablabel;
|
1986-03-14 16:15:16 +00:00
|
|
|
register struct case_entry *ce;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
if (sh->sh_default == 0) /* no default occurred yet */
|
|
|
|
sh->sh_default = sh->sh_break;
|
|
|
|
C_bra(sh->sh_break); /* skip the switch table now */
|
1986-03-11 15:21:30 +00:00
|
|
|
C_df_ilb(sh->sh_table); /* switch table entry */
|
1986-03-10 13:07:55 +00:00
|
|
|
tablabel = data_label(); /* the rom must have a label */
|
1986-03-11 15:21:30 +00:00
|
|
|
C_df_dlb(tablabel);
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_ilb(sh->sh_default);
|
1986-03-10 13:07:55 +00:00
|
|
|
if (compact(sh->sh_nrofentries, sh->sh_lowerbd, sh->sh_upperbd)) {
|
|
|
|
/* CSA */
|
|
|
|
register arith val;
|
|
|
|
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_cst(sh->sh_lowerbd);
|
|
|
|
C_rom_cst(sh->sh_upperbd - sh->sh_lowerbd);
|
1986-03-10 13:07:55 +00:00
|
|
|
ce = sh->sh_entries;
|
1987-11-13 16:21:33 +00:00
|
|
|
if (sh->sh_nrofentries)
|
|
|
|
for (val = sh->sh_lowerbd; val <= sh->sh_upperbd; val++) {
|
1986-03-10 13:07:55 +00:00
|
|
|
ASSERT(ce);
|
1986-09-29 14:01:34 +00:00
|
|
|
if (val == ce->ce_value) {
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_ilb(ce->ce_label);
|
1986-03-10 13:07:55 +00:00
|
|
|
ce = ce->next;
|
|
|
|
}
|
|
|
|
else
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_ilb(sh->sh_default);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1986-03-11 15:21:30 +00:00
|
|
|
C_lae_dlb(tablabel, (arith)0); /* perform the switch */
|
1986-03-10 13:07:55 +00:00
|
|
|
C_csa(sh->sh_type->tp_size);
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
else { /* CSB */
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_cst((arith)sh->sh_nrofentries);
|
1986-09-29 14:01:34 +00:00
|
|
|
for (ce = sh->sh_entries; ce; ce = ce->next) {
|
1986-03-10 13:07:55 +00:00
|
|
|
/* generate the entries: value + prog.label */
|
1986-03-13 13:27:44 +00:00
|
|
|
C_rom_cst(ce->ce_value);
|
|
|
|
C_rom_ilb(ce->ce_label);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1986-03-11 15:21:30 +00:00
|
|
|
C_lae_dlb(tablabel, (arith)0); /* perform the switch */
|
1986-03-10 13:07:55 +00:00
|
|
|
C_csb(sh->sh_type->tp_size);
|
|
|
|
}
|
1986-03-11 15:21:30 +00:00
|
|
|
C_df_ilb(sh->sh_break);
|
1986-03-10 13:07:55 +00:00
|
|
|
switch_stack = sh->next; /* unstack the switch descriptor */
|
1986-09-29 14:01:34 +00:00
|
|
|
for (ce = sh->sh_entries; ce;) { /* free allocated switch structure */
|
1986-03-14 16:15:16 +00:00
|
|
|
register struct case_entry *tmp = ce->next;
|
1986-09-29 14:01:34 +00:00
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
free_case_entry(ce);
|
1986-03-14 16:15:16 +00:00
|
|
|
ce = tmp;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
free_switch_hdr(sh);
|
1986-07-18 21:10:42 +00:00
|
|
|
unstack_stmt();
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
1986-03-14 16:15:16 +00:00
|
|
|
code_case(expr)
|
|
|
|
struct expr *expr;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-03-14 16:15:16 +00:00
|
|
|
register arith val;
|
1986-03-10 13:07:55 +00:00
|
|
|
register struct case_entry *ce;
|
|
|
|
register struct switch_hdr *sh = switch_stack;
|
1986-03-14 16:15:16 +00:00
|
|
|
|
1986-04-02 08:37:17 +00:00
|
|
|
ASSERT(is_cp_cst(expr));
|
1986-09-29 14:01:34 +00:00
|
|
|
if (sh == 0) {
|
1986-03-10 13:07:55 +00:00
|
|
|
error("case statement not in switch");
|
|
|
|
return;
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
if (expr->ex_flags & EX_ERROR) /* is probably 0 anyway */
|
1986-03-27 18:17:48 +00:00
|
|
|
return;
|
1986-05-28 08:40:06 +00:00
|
|
|
ch7cast(&expr, SWITCH, sh->sh_type);
|
1986-03-10 13:07:55 +00:00
|
|
|
ce = new_case_entry();
|
1986-03-11 15:21:30 +00:00
|
|
|
C_df_ilb(ce->ce_label = text_label());
|
1986-03-14 16:15:16 +00:00
|
|
|
ce->ce_value = val = expr->VL_VALUE;
|
1986-09-29 14:01:34 +00:00
|
|
|
if (sh->sh_entries == 0) { /* first case entry */
|
1986-03-10 13:07:55 +00:00
|
|
|
ce->next = (struct case_entry *) 0;
|
|
|
|
sh->sh_entries = ce;
|
1986-03-14 16:15:16 +00:00
|
|
|
sh->sh_lowerbd = sh->sh_upperbd = val;
|
1986-03-10 13:07:55 +00:00
|
|
|
sh->sh_nrofentries = 1;
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
else { /* second etc. case entry; put ce into proper place */
|
1986-03-10 13:07:55 +00:00
|
|
|
register struct case_entry *c1 = sh->sh_entries, *c2 = 0;
|
|
|
|
|
|
|
|
if (val < sh->sh_lowerbd)
|
|
|
|
sh->sh_lowerbd = val;
|
|
|
|
else
|
|
|
|
if (val > sh->sh_upperbd)
|
|
|
|
sh->sh_upperbd = val;
|
1986-09-29 14:01:34 +00:00
|
|
|
while (c1 && c1->ce_value < ce->ce_value) {
|
1986-03-10 13:07:55 +00:00
|
|
|
c2 = c1;
|
|
|
|
c1 = c1->next;
|
|
|
|
}
|
|
|
|
/* At this point three cases are possible:
|
1986-09-29 14:01:34 +00:00
|
|
|
1: c1 != 0 && c2 != 0: insert ce somewhere in the middle
|
|
|
|
2: c1 != 0 && c2 == 0: insert ce right after the head
|
|
|
|
3: c1 == 0 && c2 != 0: append ce to last element
|
1986-03-14 16:15:16 +00:00
|
|
|
The case c1 == 0 && c2 == 0 cannot occur, since
|
1986-09-29 14:01:34 +00:00
|
|
|
the list is guaranteed to be non-empty.
|
1986-03-10 13:07:55 +00:00
|
|
|
*/
|
1986-09-29 14:01:34 +00:00
|
|
|
if (c1) {
|
|
|
|
if (c1->ce_value == ce->ce_value) {
|
1986-03-10 13:07:55 +00:00
|
|
|
error("multiple case entry for value %ld",
|
|
|
|
ce->ce_value);
|
|
|
|
free_case_entry(ce);
|
|
|
|
return;
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
if (c2) {
|
1986-03-10 13:07:55 +00:00
|
|
|
ce->next = c2->next;
|
|
|
|
c2->next = ce;
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
else {
|
1986-03-10 13:07:55 +00:00
|
|
|
ce->next = sh->sh_entries;
|
|
|
|
sh->sh_entries = ce;
|
|
|
|
}
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
else {
|
1986-03-10 13:07:55 +00:00
|
|
|
ASSERT(c2);
|
|
|
|
ce->next = (struct case_entry *) 0;
|
|
|
|
c2->next = ce;
|
|
|
|
}
|
|
|
|
(sh->sh_nrofentries)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code_default()
|
|
|
|
{
|
|
|
|
register struct switch_hdr *sh = switch_stack;
|
|
|
|
|
1988-09-16 23:19:50 +00:00
|
|
|
if (sh == 0) {
|
|
|
|
error("default statement not in switch");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sh->sh_default != 0) {
|
1986-03-10 13:07:55 +00:00
|
|
|
error("multiple entry for default in switch");
|
1988-09-16 23:19:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
C_df_ilb(sh->sh_default = text_label());
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|