Pristine Ack-5.5
[Ack-5.5.git] / mach / proto / ncg / gencode.c
1 #ifndef NORCSID
2 static char rcsid[] = "$Id: gencode.c,v 0.16 1994/06/24 13:27:25 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 <cgg_cg.h>
11 #include "data.h"
12 #include "result.h"
13 #include "extern.h"
14 #ifdef USE_TES
15 #include "mach.h"
16 #endif
17
18 /*
19  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
20  * See the copyright notice in the ACK home directory, in the file "Copyright".
21  *
22  * Author: Hans van Staveren
23  */
24
25 string mystrcpy();
26
27 FILE *codefile;
28 extern FILE *freopen();
29
30 out_init(filename) char *filename; {
31
32 #ifndef NDEBUG
33         static char stderrbuff[BUFSIZ];
34
35     if (Debug) {
36         codefile = stderr;
37         if (!isatty(2))
38                 setbuf(stderr,stderrbuff);
39     } else {
40 #endif
41         if (filename == (char *) 0)
42                 codefile = stdout;
43         else
44                 if ((codefile=freopen(filename,"w",stdout))==NULL)
45                         error("Can't create %s",filename);
46 #ifndef NDEBUG
47     }
48 #endif
49 }
50
51 out_finish() {
52
53 #ifndef NDEBUG
54         if (Debug)
55                 fflush(stderr);
56         else
57 #endif
58                 if (codefile) fclose(codefile);
59 #ifdef TABLEDEBUG
60         termlset();
61 #endif
62 }
63
64 tstoutput() {
65
66         if (ferror(codefile))
67                 error("Write error on output");
68 }
69
70 genstr(stringno) {
71
72         fputs(codestrings[stringno],codefile);
73 }
74
75 string ad2str(ad) addr_t ad; {
76         static char buf[100];
77
78         if (ad.ea_str==0)
79                 ad.ea_str="";
80         if ((long)ad.ea_off==(long)0) {
81                 if(ad.ea_str[0]==0)
82                         return(mystrcpy("0")); /* don't return empty string */
83                 else
84                         return(mystrcpy(ad.ea_str));
85         }
86         sprintf(buf,"%s%c%ld",ad.ea_str,ad.ea_off>=0 ? '+' : ' ',(long)ad.ea_off);
87         return(mystrcpy(buf));
88 }
89
90 praddr(ad) addr_t ad; {
91
92         if (ad.ea_str==0 || *(ad.ea_str) == '\0')
93                 fprintf(codefile,WRD_FMT,ad.ea_off);
94         else {
95                 fputs(ad.ea_str, codefile);
96                 if (ad.ea_off<0) {
97                         putc('-', codefile);
98                         fprintf(codefile,WRD_FMT,-ad.ea_off);           
99                 }
100                 else if(ad.ea_off>0) {
101                         putc('+',codefile);
102                         fprintf(codefile,WRD_FMT,ad.ea_off);
103                 }
104         }
105 }
106
107 gennl() {
108         putc('\n',codefile);
109 }
110
111 prtoken(tp,leadingchar) token_p tp; {
112         register c;
113         register char *code;
114         register tkdef_p tdp;
115
116         putc(leadingchar,codefile);
117         if (tp->t_token == -1) {
118                 fputs(codestrings[machregs[tp->t_att[0].ar].r_repr],codefile);
119                 return;
120         }
121         tdp = &tokens[tp->t_token];
122         assert(tdp->t_format != -1);
123         code = codestrings[tdp->t_format];
124         while ((c = *code++) != 0) {
125                 if (c>=' ' && c<='~')
126                         putc(c,codefile);
127                 else {
128                         assert(c>0 && c<=TOKENSIZE);
129                         switch(tdp->t_type[c-1]) {
130                         default:
131                                 assert(FALSE);
132                         case EV_INT:
133                                 fprintf(codefile,WRD_FMT,tp->t_att[c-1].aw);
134                                 break;
135                         case EV_ADDR:
136                                 praddr(tp->t_att[c-1].aa);
137                                 break;
138                         case EV_REG:
139                                 fputs(codestrings[machregs[tp->t_att[c-1].ar].r_repr],codefile);
140                                 break;
141                         }
142                 }
143         }
144 }
145
146 #ifdef USE_TES
147 printlabel(labnum)
148 int labnum;
149 {
150         newilb(dollar[labnum].e_v.e_addr.ea_str);
151 }
152 #endif