Debian Codegraph @

arch/nova/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.15 2021/10/13 17:07:36 ragge Exp $ */
2 /*
3 * Copyright (c) 2006 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 # include "pass1.h"
31
32 #ifdef LANG_CXX
33 #define P1ND NODE
34 #define p1alloc talloc
35 #define p1nfree nfree
36 #define p1fwalk fwalk
37 #define p1tcopy ccopy
38 #define p1listf listf
39 #endif
40
41 /*
42 * cause the alignment to become a multiple of n
43 * never called for text segment.
44 */
45 void
46 defalign(int n)
47 {
48 /* alignment are always correct */
49 }
50
51 /*
52 * Print out assembler segment name.
53 */
54 void
55 setseg(int seg, char *name)
56 {
57 #ifndef os_none
58 switch (seg) {
59 case PROG: name = ".text"; break;
60 case DATA:
61 case LDATA: name = ".data"; break;
62 case STRNG:
63 case RDATA: name = ".rodata"; break;
64 case UDATA: break;
65 default:
66 cerror((char *)__func__);
67 }
68 #else
69 name = ".NREL";
70 #endif
71 printf("\t%s\n", name);
72 }
73
74 /*
75 * Define everything needed to print out some data (or text).
76 * This means segment, alignment, visibility, etc.
77 */
78 void
79 defloc(struct symtab *sp)
80 {
81 char *name;
82
83 name = getexname(sp);
84
85 if (ISFTN(sp->stype))
86 return;
87 if (sp->sclass == EXTDEF)
88 printf("\t.globl %s\n", name);
89 if (sp->slevel == 0)
90 printf("%s:\n", name);
91 else
92 printf(LABFMT ":\n", sp->soffset);
93 }
94
95 /* make a common declaration for id, if reasonable */
96 void
97 defzero(struct symtab *sp)
98 {
99 int off, al;
100 char *name;
101
102 name = getexname(sp);
103 off = tsize(sp->stype, sp->sdf, sp->sap);
104 SETOFF(off,SZCHAR);
105 off /= SZCHAR;
106 al = talign(sp->stype, sp->sap)/SZCHAR;
107
108 if (sp->sclass == STATIC) {
109 if (sp->slevel == 0) {
110 printf("\t.local %s\n", name);
111 } else
112 printf("\t.local " LABFMT "\n", sp->soffset);
113 }
114 if (sp->slevel == 0) {
115 printf("\t.comm %s,0%o,%d\n", name, off, al);
116 } else
117 printf("\t.comm " LABFMT ",0%o,%d\n", sp->soffset, off, al);
118 }
119
120
121 //static int ac3temp;
122 /*
123 * code for the end of a function
124 * deals with struct return here
125 */
126 void
127 efcode(void)
128 {
129 P1ND *p, *q;
130 // int sz;
131
132 #if 0
133 /* restore ac3 */
134 p = block(REG, 0, 0, INT, 0, 0);
135 regno(p) = 3;
136 q = tempnode(ac3temp, INT, 0, 0);
137 ecomp(buildtree(ASSIGN, p, q));
138 #endif
139
140
141 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
142 return;
143 cerror("efcode");
144 /* address of return struct is in eax */
145 /* create a call to memcpy() */
146 /* will get the result in eax */
147 p = block(REG, NULL, NULL, CHAR+PTR, 0, 0);
148 // p->n_rval = EAX;
149 q = block(OREG, NULL, NULL, CHAR+PTR, 0, 0);
150 // q->n_rval = EBP;
151 slval(q, 8); /* return buffer offset */
152 p = block(CM, q, p, INT, 0, 0);
153 // sz = (tsize(STRTY, cftnsp->sdf, cftnsp->ssue)+SZCHAR-1)/SZCHAR;
154 // p = block(CM, p, bcon(sz), INT, 0, 0);
155 p->n_right->n_name = "";
156 p = block(CALL, bcon(0), p, CHAR+PTR, 0, 0);
157 p->n_left->n_name = "memcpy";
158 p = clocal(p);
159 send_passt(IP_NODE, p);
160 }
161
162 /*
163 * code for the beginning of a function; a is an array of
164 * indices in symtab for the arguments; n is the number
165 */
166 void
167 bfcode(struct symtab **a, int n)
168 {
169 // P1ND *p, *q;
170 int i;
171
172 for (i = 0; i < n; i++) {
173 if (a[i]->stype == CHAR)
174 a[i]->stype = INT;
175 if (a[i]->stype == UCHAR)
176 a[i]->stype = UNSIGNED;
177 }
178
179 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
180 return;
181 cerror("bfcode");
182 /* Function returns struct, adjust arg offset */
183 for (i = 0; i < n; i++)
184 a[i]->soffset += SZPOINT(INT);
185 }
186
187
188 /* called just before final exit */
189 /* flag is 1 if errors, 0 if none */
190 void
191 ejobcode(int flag)
192 {
193 #ifdef os_none
194 printf("\t.END\n");
195 #endif
196 }
197
198 void
199 bjobcode(void)
200 {
201 #ifdef os_none
202 printf("\t.TITL foo\n");
203 printf("\t.TXTM 1\n"); /* big endian */
204 printf("\t.NREL\n"); /* relocatable */
205 printf("\t.EXTU\n"); /* undefined syms are external */
206 #endif
207 }
208
209 /* fix up type of field p */
210 void
211 fldty(struct symtab *p)
212 {
213 }
214
215 /*
216 * XXX - fix genswitch.
217 */
218 int
219 mygenswitch(int num, TWORD type, struct swents **p, int n)
220 {
221 return 0;
222 }
223
224 static int xoff;
225
226 static void
227 fnummer(P1ND *p)
228 {
229 if (p->n_op != FUNARG)
230 return;
231 p->n_rval = xoff;
232 xoff -= tsize(p->n_type, p->n_df, p->n_ap)/SZSHORT;
233 }
234
235 /*
236 * Called with a function call with arguments as argument.
237 * This is done early in buildtree() and only done once.
238 */
239 P1ND *
240 funcode(P1ND *p)
241 {
242 P1ND *r, *l;
243
244 /* Fix function call arguments. On nova, just add funarg */
245 for (r = p->n_right; r->n_op == CM; r = r->n_left) {
246 if (r->n_right->n_op != STARG) {
247 r->n_right = block(FUNARG, r->n_right, NULL,
248 r->n_right->n_type, r->n_right->n_df,
249 r->n_right->n_ap);
250 }
251 }
252 if (r->n_op != STARG) {
253 l = p1alloc();
254 *l = *r;
255 r->n_op = FUNARG;
256 r->n_left = l;
257 r->n_type = l->n_type;
258 }
259
260 xoff = -2;
261 p1listf(p->n_right, fnummer);
262 return p;
263 }
264
265 /*
266 * Return return as given by a.
267 */
268 P1ND *
269 builtin_return_address(const struct bitable *bt, P1ND *a)
270 {
271 cerror((char *)__func__);
272 return 0;
273 }
274
275 /*
276 * Return frame as given by a.
277 */
278 P1ND *
279 builtin_frame_address(const struct bitable *bt, P1ND *a)
280 {
281 cerror((char *)__func__);
282 return 0;
283 }
284
285 /*
286 * Return "canonical frame address".
287 */
288 P1ND *
289 builtin_cfa(const struct bitable *bt, P1ND *a)
290 {
291 cerror((char *)__func__);
292 return 0;
293 }
294