Pristine Ack-5.5
[Ack-5.5.git] / mach / proto / ncg / move.c
1 #ifndef NORCSID
2 static char rcsid[] = "$Id: move.c,v 0.6 1994/06/24 13:27:40 ceriel Exp $";
3 #endif
4
5 #include "assert.h"
6 #include "param.h"
7 #include "tables.h"
8 #include "types.h"
9 #include <cgg_cg.h>
10 #include "data.h"
11 #include "result.h"
12 #include "extern.h"
13
14 /*
15  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
16  * See the copyright notice in the ACK home directory, in the file "Copyright".
17  *
18  * Author: Hans van Staveren
19  */
20
21 move(tp1,tp2,ply,toplevel,maxcost) token_p tp1,tp2; unsigned maxcost; {
22         register move_p mp;
23         unsigned t;
24         register struct reginfo *rp;
25         register byte *tdpb;
26         int i;
27         unsigned codegen();
28
29         if (eqtoken(tp1,tp2))
30                 return(0);
31         if (tp2->t_token == -1) {
32                 if (tp1->t_token == -1) {
33                         if (eqtoken(&machregs[tp1->t_att[0].ar].r_contents,
34                                     &machregs[tp2->t_att[0].ar].r_contents) &&
35                               machregs[tp1->t_att[0].ar].r_contents.t_token!=0)
36                                 return(0);
37                 } else {
38                         if (eqtoken(&machregs[tp2->t_att[0].ar].r_contents,tp1))
39                                 return(0);
40                 }
41                 erasereg(tp2->t_att[0].ar);
42         } else if (tp1->t_token == -1) {
43                 if (eqtoken(tp2,&machregs[tp1->t_att[0].ar].r_contents))
44                         return(0);
45         }
46         /*
47          * If we arrive here the move must really be executed
48          */
49         for (mp=moves;mp->m_set1>=0;mp++) {
50                 if (!match(tp1,&machsets[mp->m_set1],mp->m_expr1))
51                         continue;
52                 if (match(tp2,&machsets[mp->m_set2],mp->m_expr2))
53                         break;
54                 /*
55                  * Correct move rule is found
56                  */
57         }
58         assert(mp->m_set1>=0);
59         /*
60          * To get correct interpretation of things like %[1]
61          * in move code we stack tp2 and tp1. This little trick
62          * saves a lot of testing in other places.
63          */
64
65         fakestack[stackheight] = *tp2;
66         fakestack[stackheight+1] = *tp1;
67         stackheight += 2;
68         tokpatlen += 2;
69         t = codegen(&coderules[mp->m_cindex],ply,toplevel,maxcost,0);
70         tokpatlen -= 2;
71         stackheight -= 2;
72         if (tp2->t_token == -1) {
73                 rp = &machregs[tp2->t_att[0].ar];
74                 if (tp1->t_token == -1) {
75                         rp->r_contents =
76                           machregs[tp1->t_att[0].ar].r_contents ;
77                 }
78                 else    rp->r_contents = *tp1;
79                 if (rp->r_contents.t_token > 0) {
80                         tdpb = &(tokens[rp->r_contents.t_token].t_type[0]);
81                         for (i=0;i<TOKENSIZE;i++)
82                                 if (*tdpb++ == EV_REG &&
83                                     clash(rp->r_contents.t_att[i].ar,tp2->t_att[0].ar)) {
84                                         rp->r_contents.t_token = 0;
85                                         for (i = 0; i < TOKENSIZE; i++)
86                                                 rp->r_contents.t_att[i].aw = 0;
87                                         break;
88                                 }
89                 }
90         }
91         else if (tp1->t_token == -1)
92                 machregs[tp1->t_att[0].ar].r_contents = *tp2;
93         return(t);
94 }
95
96 #define cocoreg machregs[0].r_contents
97
98 setcc(tp) token_p tp; {
99
100         cocoreg = *tp;
101 }
102
103 test(tp,ply,toplevel,maxcost) token_p tp; unsigned maxcost; {
104         register test_p mp;
105         unsigned t;
106         unsigned codegen();
107
108         if (cocoreg.t_token!=0) {
109                 if (eqtoken(tp,&cocoreg))
110                         return(0);
111                 if (tp->t_token == -1) {
112                         if (eqtoken(&machregs[tp->t_att[0].ar].r_contents,&cocoreg))
113                                 return(0);
114                 }
115         }
116         /*
117          * If we arrive here the test must really be executed
118          */
119         for (mp=tests;mp->t_set>=0;mp++) {
120                 if (match(tp,&machsets[mp->t_set],mp->t_expr))
121                         break;
122                 /*
123                  * Correct move rule is found
124                  */
125         }
126         assert(mp->t_set>=0);
127         /*
128          * To get correct interpretation of things like %[1]
129          * in test code we stack tp. This little trick
130          * saves a lot of testing in other places.
131          */
132
133         fakestack[stackheight] = *tp;
134         stackheight++;
135         tokpatlen++;
136         t = codegen(&coderules[mp->t_cindex],ply,toplevel,maxcost,0);
137         tokpatlen--;
138         stackheight--;
139         return(t);
140 }