Debian Codegraph @

arch/i86/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.3 2018/11/13 17:47:32 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 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 p1tfree tfree
36 #define p1listf listf
37 #endif
38
39 /*
40 * Print out assembler segment name.
41 */
42 void
43 setseg(int seg, char *name)
44 {
45 switch (seg) {
46 case PROG: name = ".TEXT"; break;
47 case DATA:
48 case LDATA: name = ".DATA"; break;
49 case UDATA: break;
50 case STRNG:
51 case RDATA: name = ".DATA"; break;
52 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
53 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
54 case NMSEG:
55 printf("\t.section %s,\"a%c\",@progbits\n", name,
56 cftnsp ? 'x' : 'w');
57 return;
58 }
59 printf("\t%s\n", name);
60 }
61
62 /*
63 * Define everything needed to print out some data (or text).
64 * This means segment, alignment, visibility, etc.
65 */
66 void
67 defloc(struct symtab *sp)
68 {
69 char *name;
70
71 name = getexname(sp);
72 if (sp->sclass == EXTDEF) {
73 printf(" .globl %s\n", name);
74 }
75 if (sp->slevel == 0)
76 printf("%s:\n", name);
77 else
78 printf(LABFMT ":\n", sp->soffset);
79 }
80
81 int structrettemp;
82
83 /*
84 * code for the end of a function
85 * deals with struct return here
86 */
87 void
88 efcode(void)
89 {
90 extern int gotnr;
91 P1ND *p, *q;
92
93 gotnr = 0; /* new number for next fun */
94 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
95 return;
96 /* Create struct assignment */
97 q = tempnode(structrettemp, PTR+STRTY, 0, cftnsp->sap);
98 q = buildtree(UMUL, q, NULL);
99 p = block(REG, NULL, NULL, PTR+STRTY, 0, cftnsp->sap);
100 p = buildtree(UMUL, p, NULL);
101 p = buildtree(ASSIGN, q, p);
102 ecomp(p);
103
104 /* put hidden arg in ax on return */
105 q = tempnode(structrettemp, INT, 0, 0);
106 p = block(REG, NULL, NULL, INT, 0, 0);
107 regno(p) = AX;
108 ecomp(buildtree(ASSIGN, p, q));
109 }
110
111 static TWORD longregs[] = { AXDX, DXCX };
112 static TWORD regpregs[] = { AX, DX, CX };
113 static TWORD charregs[] = { AL, DL, CL };
114
115 /*
116 * code for the beginning of a function; a is an array of
117 * indices in symtab for the arguments; n is the number
118 *
119 * Classifying args on i386; not simple:
120 * - Args may be on stack or in registers (regparm)
121 * - There may be a hidden first arg, unless OpenBSD struct return.
122 * - Regparm syntax is not well documented.
123 * - There may be stdcall functions, where the called function pops stack
124 * - ...probably more
125 */
126 void
127 bfcode(struct symtab **sp, int cnt)
128 {
129 extern int argstacksize;
130 #ifdef GCC_COMPAT
131 struct attr *ap;
132 #endif
133 struct symtab *sp2;
134 extern int gotnr;
135 P1ND *n, *p;
136 int i, regparmarg;
137 int argbase, nrarg, sz;
138
139 argbase = ARGINIT;
140 nrarg = regparmarg = 0;
141
142 #ifdef GCC_COMPAT
143 #if 0
144 if (attr_find(cftnsp->sap, GCC_ATYP_STDCALL) != NULL)
145 cftnsp->sflags |= SSTDCALL;
146 #endif
147 if ((ap = attr_find(cftnsp->sap, GCC_ATYP_REGPARM)))
148 regparmarg = ap->iarg(0);
149 #endif
150
151 /* Function returns struct, create return arg node */
152 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
153 {
154 if (regparmarg) {
155 n = block(REG, 0, 0, INT, 0, 0);
156 regno(n) = regpregs[nrarg++];
157 } else {
158 n = block(OREG, 0, 0, INT, 0, 0);
159 slval(n, argbase/SZCHAR);
160 argbase += SZINT;
161 regno(n) = FPREG;
162 }
163 p = tempnode(0, INT, 0, 0);
164 structrettemp = regno(p);
165 p = buildtree(ASSIGN, p, n);
166 ecomp(p);
167 }
168 }
169
170 /*
171 * Find where all params are so that they end up at the right place.
172 * At the same time recalculate their arg offset on stack.
173 * We also get the "pop size" for stdcall.
174 */
175 for (i = 0; i < cnt; i++) {
176 sp2 = sp[i];
177 sz = tsize(sp2->stype, sp2->sdf, sp2->sap);
178
179 SETOFF(sz, SZINT);
180
181 if (cisreg(sp2->stype) == 0 ||
182 ((regparmarg - nrarg) * SZINT < sz)) { /* not in reg */
183 sp2->soffset = argbase;
184 argbase += sz;
185 nrarg = regparmarg; /* no more in reg either */
186 } else { /* in reg */
187 sp2->soffset = nrarg;
188 nrarg += sz/SZINT;
189 sp2->sclass = REGISTER;
190 }
191 }
192
193 /*
194 * Now (argbase - ARGINIT) is used space on stack.
195 * Move (if necessary) the args to something new.
196 */
197 for (i = 0; i < cnt; i++) {
198 int reg, j;
199
200 sp2 = sp[i];
201
202 if (ISSOU(sp2->stype) && sp2->sclass == REGISTER) {
203 /* must move to stack */
204 sz = tsize(sp2->stype, sp2->sdf, sp2->sap);
205 SETOFF(sz, SZINT);
206 SETOFF(autooff, SZINT);
207 reg = sp2->soffset;
208 sp2->sclass = AUTO;
209 sp2->soffset = NOOFFSET;
210 oalloc(sp2, &autooff);
211 for (j = 0; j < sz/SZCHAR; j += 4) {
212 p = block(OREG, 0, 0, INT, 0, 0);
213 slval(p, sp2->soffset/SZCHAR + j);
214 regno(p) = FPREG;
215 n = block(REG, 0, 0, INT, 0, 0);
216 regno(n) = regpregs[reg++];
217 p = block(ASSIGN, p, n, INT, 0, 0);
218 ecomp(p);
219 }
220 } else if (cisreg(sp2->stype) && !ISSOU(sp2->stype) &&
221 ((cqual(sp2->stype, sp2->squal) & VOL) == 0)) {
222 /* just put rest in temps */
223 if (sp2->sclass == REGISTER) {
224 n = block(REG, 0, 0, sp2->stype,
225 sp2->sdf, sp2->sap);
226 if (ISLONGLONG(sp2->stype)|| sp2->stype == LONG || sp2->stype == ULONG)
227 regno(n) = longregs[sp2->soffset];
228 else if (DEUNSIGN(sp2->stype) == CHAR || sp2->stype == BOOL)
229 regno(n) = charregs[sp2->soffset];
230 else
231 regno(n) = regpregs[sp2->soffset];
232 } else {
233 n = block(OREG, 0, 0, sp2->stype,
234 sp2->sdf, sp2->sap);
235 slval(n, sp2->soffset/SZCHAR);
236 regno(n) = FPREG;
237 }
238 p = tempnode(0, sp2->stype, sp2->sdf, sp2->sap);
239 sp2->soffset = regno(p);
240 sp2->sflags |= STNODE;
241 n = buildtree(ASSIGN, p, n);
242 ecomp(n);
243 }
244 }
245
246 argstacksize = 0;
247 if (attr_find(cftnsp->sap, GCC_ATYP_STDCALL)) {
248 argstacksize = (argbase - ARGINIT)/SZCHAR;
249 }
250
251 }
252
253
254 /* called just before final exit */
255 /* flag is 1 if errors, 0 if none */
256 void
257 ejobcode(int flag)
258 {
259 printf("\t.asciz \"PCC: %s\"\n", VERSSTR);
260 }
261
262 void
263 bjobcode(void)
264 {
265 astypnames[INT] = astypnames[UNSIGNED] = "\t.long";
266 }
267
268 /*
269 * Convert FUNARG to assign in case of regparm.
270 */
271 static int regcvt, rparg;
272 static void
273 addreg(P1ND *p)
274 {
275 TWORD t;
276 P1ND *q;
277 int sz, r;
278
279 sz = tsize(p->n_type, p->n_df, p->n_ap)/SZCHAR;
280 sz = (sz + 3) >> 2; /* sz in regs */
281 if ((regcvt+sz) > rparg) {
282 regcvt = rparg;
283 return;
284 }
285 if (sz > 2)
286 uerror("cannot put struct in 3 regs (yet)");
287
288 if (sz == 2)
289 r = regcvt == 0 ? AXDX : DXCX;
290 else
291 r = regcvt == 0 ? AX : regcvt == 1 ? DX : CX;
292
293 if (p->n_op == FUNARG) {
294 /* at most 2 regs */
295 if (p->n_type < INT) {
296 p->n_left = ccast(p->n_left, INT, 0, 0, 0);
297 p->n_type = INT;
298 }
299
300 p->n_op = ASSIGN;
301 p->n_right = p->n_left;
302 } else if (p->n_op == STARG) {
303 /* convert to ptr, put in reg */
304 q = p->n_left;
305 t = sz == 2 ? LONGLONG : INT;
306 q = cast(q, INCREF(t), 0);
307 q = buildtree(UMUL, q, NULL);
308 p->n_op = ASSIGN;
309 p->n_type = t;
310 p->n_right = q;
311 } else
312 cerror("addreg");
313 p->n_left = block(REG, 0, 0, p->n_type, 0, 0);
314 regno(p->n_left) = r;
315 regcvt += sz;
316 }
317
318 /*
319 * Called with a function call with arguments as argument.
320 * This is done early in buildtree() and only done once.
321 * Returns p.
322 */
323 P1ND *
324 funcode(P1ND *p)
325 {
326 extern int gotnr;
327 #ifdef GCC_COMPAT
328 struct attr *ap;
329 #endif
330 P1ND *r, *l;
331 TWORD t = DECREF(DECREF(p->n_left->n_type));
332 int stcall;
333
334 stcall = ISSOU(t);
335 /*
336 * We may have to prepend:
337 * - Hidden arg0 for struct return (in reg or on stack).
338 * - ebx in case of PIC code.
339 */
340
341 /* Fix function call arguments. On x86, just add funarg */
342 for (r = p->n_right; r->n_op == CM; r = r->n_left) {
343 if (r->n_right->n_op != STARG) {
344 r->n_right = intprom(r->n_right);
345 r->n_right = block(FUNARG, r->n_right, NULL,
346 r->n_right->n_type, r->n_right->n_df,
347 r->n_right->n_ap);
348 }
349 }
350 if (r->n_op != STARG) {
351 l = p1alloc();
352 *l = *r;
353 r->n_op = FUNARG;
354 r->n_left = l;
355 r->n_left = intprom(r->n_left);
356 r->n_type = r->n_left->n_type;
357 }
358 if (stcall) {
359 /* Prepend a placeholder for struct address. */
360 /* Use BP, can never show up under normal circumstances */
361 l = p1alloc();
362 *l = *r;
363 r->n_op = CM;
364 r->n_right = l;
365 r->n_type = INT;
366 l = block(REG, 0, 0, INCREF(VOID), 0, 0);
367 regno(l) = BP;
368 l = block(FUNARG, l, 0, INCREF(VOID), 0, 0);
369 r->n_left = l;
370 }
371
372 #ifdef GCC_COMPAT
373 if ((ap = attr_find(p->n_left->n_ap, GCC_ATYP_REGPARM)))
374 rparg = ap->iarg(0);
375 else
376 #endif
377 rparg = 0;
378
379 regcvt = 0;
380 if (rparg)
381 p1listf(p->n_right, addreg);
382
383 return p;
384 }
385
386 /* fix up type of field p */
387 void
388 fldty(struct symtab *p)
389 {
390 }
391
392 /*
393 * XXX - fix genswitch.
394 */
395 int
396 mygenswitch(int num, TWORD type, struct swents **p, int n)
397 {
398 return 0;
399 }
400
401 P1ND *
402 builtin_return_address(const struct bitable *bt, P1ND *a)
403 {
404 int nframes;
405 P1ND *f;
406
407 if (a->n_op != ICON)
408 goto bad;
409
410 nframes = glval(a);
411
412 p1tfree(a);
413
414 f = block(REG, NULL, NULL, PTR+VOID, 0, 0);
415 regno(f) = FPREG;
416
417 while (nframes--)
418 f = block(UMUL, f, NULL, PTR+VOID, 0, 0);
419
420 f = block(PLUS, f, bcon(2), INCREF(PTR+VOID), 0, 0);
421 f = buildtree(UMUL, f, NULL);
422
423 return f;
424 bad:
425 uerror("bad argument to __builtin_return_address");
426 return bcon(0);
427 }
428
429 P1ND *
430 builtin_frame_address(const struct bitable *bt, P1ND *a)
431 {
432 int nframes;
433 P1ND *f;
434
435 if (a->n_op != ICON)
436 goto bad;
437
438 nframes = glval(a);
439
440 p1tfree(a);
441
442 f = block(REG, NULL, NULL, PTR+VOID, 0, 0);
443 regno(f) = FPREG;
444
445 while (nframes--)
446 f = block(UMUL, f, NULL, PTR+VOID, 0, 0);
447
448 return f;
449 bad:
450 uerror("bad argument to __builtin_frame_address");
451 return bcon(0);
452 }
453
454 /*
455 * Return "canonical frame address".
456 */
457 P1ND *
458 builtin_cfa(const struct bitable *bt, P1ND *a)
459 {
460 uerror("missing builtin_cfa");
461 return bcon(0);
462 }
463