| * or this transition (does not change state) */ |
* or this transition (does not change state) */ |
| }; |
}; |
| |
|
| struct tpastate { /* tree parsing automaton (like) state */ |
struct tpa_state { /* tree parsing automaton (like) state */ |
| /* labeling is back-to-front */ |
/* labeling is back-to-front */ |
| struct waypoint *inst; /* in front of instruction */ |
struct waypoint *inst; /* in front of instruction */ |
| struct waypoint *trans; /* in front of instruction and transition */ |
struct waypoint *trans; /* in front of instruction and transition */ |
| }; |
}; |
| |
|
| struct tpastate *termstate = NULL; /* initialized in loader() */ |
struct tpa_state *termstate = NULL; /* initialized in loader() */ |
| |
|
| void init_waypoints(struct waypoint ws[]) |
void init_waypoints(struct waypoint ws[]) |
| { |
{ |
| ws[k].cost=INF_COST; |
ws[k].cost=INF_COST; |
| } |
} |
| |
|
| struct tpastate *empty_tpastate() |
struct tpa_state *empty_tpa_state() |
| { |
{ |
| struct tpastate *s = malloc(sizeof(struct tpastate)); |
struct tpa_state *s = malloc(sizeof(struct tpa_state)); |
| |
|
| s->inst = malloc(maxstates*sizeof(struct waypoint)); |
s->inst = malloc(maxstates*sizeof(struct waypoint)); |
| init_waypoints(s->inst); |
init_waypoints(s->inst); |
| return s; |
return s; |
| } |
} |
| |
|
| void transitions(struct tpastate *t) |
void transitions(struct tpa_state *t) |
| { |
{ |
| int k; |
int k; |
| struct super_state *l; |
struct super_state *l; |
| } |
} |
| } |
} |
| |
|
| struct tpastate *make_termstate() |
struct tpa_state *make_termstate() |
| { |
{ |
| struct tpastate *s=empty_tpastate(); |
struct tpa_state *s=empty_tpa_state(); |
| |
|
| s->inst[CANONICAL_STATE].cost = 0; |
s->inst[CANONICAL_STATE].cost = 0; |
| transitions(s); |
transitions(s); |
| return s; |
return s; |
| } |
} |
| |
|
| |
#define TPA_SIZE 16384 |
| |
|
| |
struct tpa_entry { |
| |
struct tpa_entry *next; |
| |
PrimNum inst; |
| |
struct tpa_state *state_behind; /* note: brack-to-front labeling */ |
| |
struct tpa_state *state_infront; /* note: brack-to-front labeling */ |
| |
} *tpa_table[TPA_SIZE]; |
| |
|
| |
int hash_tpa(PrimNum p, struct tpa_state *t) |
| |
{ |
| |
UCell it = (UCell )t; |
| |
return (p+it+(it>>14))&(TPA_SIZE-1); |
| |
} |
| |
|
| |
struct tpa_state **lookup_tpa(PrimNum p, struct tpa_state *t2) |
| |
{ |
| |
int hash=hash_tpa(p, t2); |
| |
struct tpa_entry *te = tpa_table[hash]; |
| |
|
| |
for (; te!=NULL; te = te->next) { |
| |
if (p == te->inst && t2 == te->state_behind) |
| |
return &(te->state_infront); |
| |
} |
| |
te = (struct tpa_entry *)malloc(sizeof(struct tpa_entry)); |
| |
te->next = tpa_table[hash]; |
| |
te->inst = p; |
| |
te->state_behind = t2; |
| |
te->state_infront = NULL; |
| |
tpa_table[hash] = te; |
| |
return &(te->state_infront); |
| |
} |
| |
|
| /* use dynamic programming to find the shortest paths within the basic |
/* use dynamic programming to find the shortest paths within the basic |
| block origs[0..ninsts-1] and rewrite the instructions pointed to by |
block origs[0..ninsts-1] and rewrite the instructions pointed to by |
| instps to use it */ |
instps to use it */ |
| void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts) |
void optimize_rewrite(Cell *instps[], PrimNum origs[], int ninsts) |
| { |
{ |
| int i,j; |
int i,j; |
| struct tpastate *ts[ninsts+1]; |
struct tpa_state *ts[ninsts+1]; |
| int nextdyn, nextstate, no_transition; |
int nextdyn, nextstate, no_transition; |
| |
|
| ts[ninsts] = termstate; |
ts[ninsts] = termstate; |
| for (i=ninsts-1; i>=0; i--) { |
for (i=ninsts-1; i>=0; i--) { |
| ts[i] = empty_tpastate(); |
struct tpa_state **tp = lookup_tpa(origs[i],ts[i+1]); |
| |
struct tpa_state *t = *tp; |
| |
if (t) |
| |
ts[i] = t; |
| |
else { |
| |
ts[i] = empty_tpa_state(); |
| for (j=1; j<=max_super && i+j<=ninsts; j++) { |
for (j=1; j<=max_super && i+j<=ninsts; j++) { |
| struct super_state **superp = lookup_super(origs+i, j); |
struct super_state **superp = lookup_super(origs+i, j); |
| if (superp!=NULL) { |
if (superp!=NULL) { |
| } |
} |
| } |
} |
| transitions(ts[i]); |
transitions(ts[i]); |
| |
*tp = ts[i]; |
| |
} |
| } |
} |
| /* now rewrite the instructions */ |
/* now rewrite the instructions */ |
| nextdyn=0; |
nextdyn=0; |