Pristine Ack-5.5
[Ack-5.5.git] / mach / proto / cg / gencode.c
1 #ifndef NORCSID
2 static char rcsid[] = "$Id: gencode.c,v 2.5 1994/06/24 13:23:42 ceriel Exp $";
3 #endif
4
5 #include "assert.h"
6 #include <stdio.h>
7 #include "param.h"
8 #include "tables.h"
9 #include "types.h"
10 #include <cg_pattern.h>
11 #include "data.h"
12 #include "result.h"
13 #include "extern.h"
14
15 /*
16  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
17  * See the copyright notice in the ACK home directory, in the file "Copyright".
18  *
19  * Author: Hans van Staveren
20  */
21
22 FILE *codefile;
23 extern FILE *freopen();
24
25 out_init(filename) char *filename; {
26
27 #ifndef NDEBUG
28         static char stderrbuff[BUFSIZ];
29
30     if (Debug) {
31         codefile = stderr;
32         if (!isatty(2))
33                 setbuf(stderr,stderrbuff);
34     } else {
35 #endif
36         if (filename == (char *) 0)
37                 codefile = stdout;
38         else
39                 if ((codefile=freopen(filename,"w",stdout))==NULL)
40                         error("Can't create %s",filename);
41 #ifndef NDEBUG
42     }
43 #endif
44 }
45
46 out_finish() {
47
48 #ifndef NDEBUG
49         if (Debug)
50                 fflush(stderr);
51         else
52 #endif
53                 fclose(codefile);
54 }
55
56 tstoutput() {
57
58         if (ferror(codefile))
59                 error("Write error on output");
60 }
61
62 gencode(code) register char *code; {
63         register c;
64         int tokno,fldno,insno,regno,subno;
65         register token_p tp;
66
67         swtxt();
68         while ((c= *code++)!=0) switch(c) {
69         default:
70                 fputc(c,codefile);
71                 break;
72         case PR_TOK:
73                 tokno = *code++;
74                 tp = &fakestack[stackheight-tokno];
75                 if (tp->t_token==-1)
76                         fprintf(codefile,"%s",codestrings[machregs[tp->t_att[0].ar].r_repr]);
77                 else
78                         prtoken(tp);
79                 break;
80         case PR_TOKFLD:
81                 tokno = *code++;
82                 fldno = *code++;
83                 tp = &fakestack[stackheight-tokno];
84                 assert(tp->t_token != -1);
85                 switch(tokens[tp->t_token].t_type[fldno-1]) {
86                 default:
87                         assert(FALSE);
88                 case EV_INT:
89                         fprintf(codefile,WRD_FMT,tp->t_att[fldno-1].aw);
90                         break;
91                 case EV_STR:
92                         fprintf(codefile,"%s",tp->t_att[fldno-1].as);
93                         break;
94                 case EV_REG:
95                         assert(tp->t_att[fldno-1].ar>0 && tp->t_att[fldno-1].ar<NREGS);
96                         fprintf(codefile,"%s",codestrings[machregs[tp->t_att[fldno-1].ar].r_repr]);
97                         break;
98                 }
99                 break;
100         case PR_EMINT:
101                 insno = *code++;
102                 fprintf(codefile,WRD_FMT,dollar[insno-1].e_v.e_con);
103                 break;
104         case PR_EMSTR:
105                 insno = *code++;
106                 fprintf(codefile,"%s",dollar[insno-1].e_v.e_str);
107                 break;
108         case PR_ALLREG:
109                 regno = *code++;
110                 subno = (*code++)&0377;
111                 assert(regno>=1 && regno<=nallreg);
112                 regno = allreg[regno-1];
113 #if MAXMEMBERS!=0
114                 if (subno!=255) {
115                         assert(subno>=1 && subno<=MAXMEMBERS);
116                         regno = machregs[regno].r_members[subno-1];
117                         assert(regno!=0);
118                 }
119 #endif
120                 fprintf(codefile,"%s",codestrings[machregs[regno].r_repr]);
121                 break;
122 #if MAXMEMBERS!=0
123         case PR_SUBREG:
124                 tokno = *code++;
125                 subno = *code++;
126                 tp = &fakestack[stackheight-tokno];
127                 assert(tp->t_token == -1);
128                 fprintf(codefile,"%s",codestrings[machregs[machregs[tp->t_att[0].ar].r_members[subno-1]].r_repr]);
129                 break;
130 #endif
131         }
132 }
133
134 genexpr(nodeno) {
135         result_t result;
136
137         result= compute(&enodes[nodeno]);
138         switch(result.e_typ) {
139         default: assert(FALSE);
140         case EV_INT:
141                 fprintf(codefile,WRD_FMT,result.e_v.e_con);
142                 break;
143         case EV_REG:
144                 fprintf(codefile,"%s", codestrings[machregs[result.e_v.e_reg].r_repr]);
145                 break;
146         case EV_STR:
147                 fprintf(codefile,"%s",result.e_v.e_str);
148                 break;
149         }
150 }
151
152 gennl() {
153         fputc('\n',codefile);
154 }
155
156 prtoken(tp) token_p tp; {
157         register c;
158         register char *code;
159         register tkdef_p tdp;
160
161         tdp = &tokens[tp->t_token];
162         assert(tdp->t_format != -1);
163         code = codestrings[tdp->t_format];
164         while ((c = *code++) != 0) {
165                 if (c>=' ' && c<='~')
166                         fputc(c,codefile);
167                 else {
168                         assert(c>0 && c<=TOKENSIZE);
169                         switch(tdp->t_type[c-1]) {
170                         default:
171                                 assert(FALSE);
172                         case EV_INT:
173                                 fprintf(codefile,WRD_FMT,tp->t_att[c-1].aw);
174                                 break;
175                         case EV_STR:
176                                 fprintf(codefile,"%s",tp->t_att[c-1].as);
177                                 break;
178                         case EV_REG:
179                                 fprintf(codefile,"%s",codestrings[machregs[tp->t_att[c-1].ar].r_repr]);
180                                 break;
181                         }
182                 }
183         }
184 }