Debian Codegraph @

arch/i386/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.102 2019/11/08 12:47:44 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 p1listf listf
34 #define p1tfree tfree
35 #else
36 #define NODE P1ND
37 #define talloc p1alloc
38 #endif
39
40 /*
41 * Print out assembler segment name.
42 */
43 void
44 setseg(int seg, char *name)
45 {
46 switch (seg) {
47 case PROG: name = ".text"; break;
48 case DATA:
49 case LDATA: name = ".data"; break;
50 case UDATA: break;
51 #ifdef MACHOABI
52 case PICLDATA:
53 case PICDATA: name = ".section .data.rel.rw,\"aw\""; break;
54 case PICRDATA: name = ".section .data.rel.ro,\"aw\""; break;
55 case STRNG: name = ".cstring"; break;
56 case RDATA: name = ".const_data"; break;
57 #else
58 case PICLDATA: name = ".section .data.rel.local,\"aw\",@progbits";break;
59 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
60 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
61 case STRNG:
62 #ifdef AOUTABI
63 case RDATA: name = ".data"; break;
64 #else
65 case RDATA: name = ".section .rodata"; break;
66 #endif
67 #endif
68 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
69 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
70 #ifdef MACHOABI
71 case CTORS: name = ".mod_init_func\n\t.align 2"; break;
72 case DTORS: name = ".mod_term_func\n\t.align 2"; break;
73 #else
74 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
75 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
76 #endif
77 case NMSEG:
78 printf(PRTPREF "\t.section %s,\"a%c\",@progbits\n", name,
79 cftnsp ? 'x' : 'w');
80 return;
81 }
82 printf(PRTPREF "\t%s\n", name);
83 }
84
85 #ifdef MACHOABI
86 void
87 defalign(int al)
88 {
89 printf(PRTPREF "\t.align %d\n", ispow2(al/ALCHAR));
90 }
91 #endif
92
93 /*
94 * Define everything needed to print out some data (or text).
95 * This means segment, alignment, visibility, etc.
96 */
97 void
98 defloc(struct symtab *sp)
99 {
100 char *name;
101
102 name = getexname(sp);
103 if (sp->sclass == EXTDEF) {
104 printf(PRTPREF " .globl %s\n", name);
105 #if defined(ELFABI)
106 printf(PRTPREF "\t.type %s,@%s\n", name,
107 ISFTN(sp->stype)? "function" : "object");
108 #endif
109 }
110 #if defined(ELFABI)
111 if (!ISFTN(sp->stype)) {
112 if (sp->slevel == 0)
113 printf(PRTPREF "\t.size %s,%d\n", name,
114 (int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR);
115 else
116 printf(PRTPREF "\t.size " LABFMT ",%d\n", sp->soffset,
117 (int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR);
118 }
119 #endif
120 if (sp->slevel == 0)
121 printf(PRTPREF "%s:\n", name);
122 else
123 printf(PRTPREF LABFMT ":\n", sp->soffset);
124 }
125
126 int structrettemp;
127
128 /*
129 * code for the end of a function
130 * deals with struct return here
131 */
132 void
133 efcode(void)
134 {
135 extern int gotnr;
136 NODE *p, *q;
137 int sz;
138
139 gotnr = 0; /* new number for next fun */
140 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
141 return;
142
143 /* struct return for small structs */
144 sz = (int)tsize(BTYPE(cftnsp->stype), cftnsp->sdf, cftnsp->sap);
145 #if defined(os_openbsd)
146 if (sz == SZCHAR || sz == SZSHORT || sz == SZINT || sz == SZLONGLONG) {
147 #else
148 if (sz == SZLONGLONG && attr_find(cftnsp->sap, ATTR_COMPLEX)) {
149 #endif
150 /* Pointer to struct in eax */
151 if (sz == SZLONGLONG) {
152 q = block(OREG, NIL, NIL, INT, 0, 0);
153 slval(q, 4);
154 p = block(REG, NIL, NIL, INT, 0, 0);
155 p->n_rval = EDX;
156 ecomp(buildtree(ASSIGN, p, q));
157 }
158 if (sz < SZSHORT) sz = CHAR;
159 else if (sz > SZSHORT) sz = INT;
160 else sz = SHORT;
161 q = block(OREG, NIL, NIL, sz, 0, 0);
162 if (sz < SZINT)
163 q = cast(q, INT, 0);
164 p = block(REG, NIL, NIL, INT, 0, 0);
165 p = (buildtree(ASSIGN, p, q));
166 ecomp(p);
167 return;
168 }
169
170 /* Create struct assignment */
171 q = tempnode(structrettemp, PTR+STRTY, 0, cftnsp->sap);
172 q = buildtree(UMUL, q, NIL);
173 p = block(REG, NIL, NIL, PTR+STRTY, 0, cftnsp->sap);
174 p = buildtree(UMUL, p, NIL);
175 p = buildtree(ASSIGN, q, p);
176 ecomp(p);
177
178 /* put hidden arg in eax on return */
179 q = tempnode(structrettemp, INT, 0, 0);
180 p = block(REG, NIL, NIL, INT, 0, 0);
181 regno(p) = EAX;
182 ecomp(buildtree(ASSIGN, p, q));
183 }
184
185 #ifdef GCC_COMPAT
186 static TWORD reparegs[] = { EAX, EDX, ECX };
187 static TWORD fastregs[] = { ECX, EDX };
188 #endif
189 static TWORD longregs[] = { EAXEDX, EDXECX };
190 #ifdef NOBREGS
191 static TWORD charregs[] = { EAX, EDX, ECX };
192 #else
193 static TWORD charregs[] = { AL, DL, CL };
194 #endif
195 static TWORD *regpregs;
196
197 /*
198 * code for the beginning of a function; a is an array of
199 * indices in symtab for the arguments; n is the number
200 *
201 * Classifying args on i386; not simple:
202 * - Args may be on stack or in registers (regparm)
203 * - There may be a hidden first arg, unless OpenBSD struct return.
204 * - Regparm syntax is not well documented.
205 * - There may be stdcall functions, where the called function pops stack
206 * - ...probably more
207 */
208 void
209 bfcode(struct symtab **sp, int cnt)
210 {
211 extern int argstacksize;
212 #ifdef GCC_COMPAT
213 struct attr *ap;
214 #endif
215 struct symtab *sp2;
216 extern int gotnr;
217 NODE *n, *p;
218 int i, regparmarg;
219 int argbase, nrarg, sz;
220
221 /* Take care of PIC stuff first */
222 if (kflag) {
223 #define STL 200
224 char *str = xmalloc(STL);
225 #if !defined(MACHOABI)
226 int l = getlab();
227 #else
228 char *name;
229 #endif
230
231 /* Generate extended assembler for PIC prolog */
232 p = tempnode(0, INT, 0, 0);
233 gotnr = regno(p);
234 p = block(XARG, p, NIL, INT, 0, 0);
235 p->n_name = "=g";
236 p = block(XASM, p, bcon(0), INT, 0, 0);
237
238 #if defined(MACHOABI)
239 name = getexname(cftnsp);
240 if (snprintf(str, STL, "call L%s$pb\nL%s$pb:\n\tpopl %%0\n",
241 name, name) >= STL)
242 cerror("bfcode");
243 #else
244 if (snprintf(str, STL,
245 "call " LABFMT ";" LABFMT ":;\tpopl %%0;"
246 "\taddl $_GLOBAL_OFFSET_TABLE_+[.-" LABFMT "], %%0;",
247 l, l, l) >= STL)
248 cerror("bfcode");
249 #endif
250 p->n_name = addstring(str);
251 p->n_right->n_type = STRTY;
252 free(str);
253 ecomp(p);
254 }
255
256 argbase = ARGINIT;
257 nrarg = regparmarg = 0;
258 argstacksize = 0;
259
260 #ifdef GCC_COMPAT
261 regpregs = reparegs;
262 if ((ap = attr_find(cftnsp->sap, GCC_ATYP_REGPARM)))
263 regparmarg = ap->iarg(0);
264 if ((ap = attr_find(cftnsp->sap, GCC_ATYP_FASTCALL)))
265 regparmarg = 2, regpregs = fastregs;
266 #endif
267
268 /* Function returns struct, create return arg node */
269 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
270 sz = (int)tsize(BTYPE(cftnsp->stype), cftnsp->sdf, cftnsp->sap);
271 #if defined(os_openbsd)
272 /* OpenBSD uses non-standard return for small structs */
273 if (sz > SZLONGLONG)
274 #else
275 if (sz != SZLONGLONG ||
276 attr_find(cftnsp->sap, ATTR_COMPLEX) == 0)
277 #endif
278 {
279 if (regparmarg) {
280 n = block(REG, 0, 0, INT, 0, 0);
281 regno(n) = regpregs[nrarg++];
282 } else {
283 n = block(OREG, 0, 0, INT, 0, 0);
284 slval(n, argbase/SZCHAR);
285 argbase += SZINT;
286 regno(n) = FPREG;
287 argstacksize += 4; /* popped by callee */
288 }
289 p = tempnode(0, INT, 0, 0);
290 structrettemp = regno(p);
291 p = buildtree(ASSIGN, p, n);
292 ecomp(p);
293 }
294 }
295
296 /*
297 * Find where all params are so that they end up at the right place.
298 * At the same time recalculate their arg offset on stack.
299 * We also get the "pop size" for stdcall.
300 */
301 for (i = 0; i < cnt; i++) {
302 sp2 = sp[i];
303 sz = (int)tsize(sp2->stype, sp2->sdf, sp2->sap);
304
305 SETOFF(sz, SZINT);
306
307 if (cisreg(sp2->stype) == 0 ||
308 ((regparmarg - nrarg) * SZINT < sz)) { /* not in reg */
309 sp2->soffset = argbase;
310 argbase += sz;
311 nrarg = regparmarg; /* no more in reg either */
312 } else { /* in reg */
313 sp2->soffset = regpregs[nrarg];
314 nrarg += sz/SZINT;
315 sp2->sclass = REGISTER;
316 }
317 }
318
319 /*
320 * Now (argbase - ARGINIT) is used space on stack.
321 * Move (if necessary) the args to something new.
322 */
323 for (i = 0; i < cnt; i++) {
324 int reg, j;
325
326 sp2 = sp[i];
327
328 if ((ISSOU(sp2->stype) && sp2->sclass == REGISTER) ||
329 (sp2->sclass == REGISTER && xtemps == 0)) {
330 /* must move to stack */
331 sz = (int)tsize(sp2->stype, sp2->sdf, sp2->sap);
332 SETOFF(sz, SZINT);
333 SETOFF(autooff, SZINT);
334 reg = sp2->soffset;
335 sp2->sclass = AUTO;
336 sp2->soffset = NOOFFSET;
337 oalloc(sp2, &autooff);
338 for (j = 0; j < sz/SZCHAR; j += 4) {
339 p = block(OREG, 0, 0, INT, 0, 0);
340 slval(p, sp2->soffset/SZCHAR + j);
341 regno(p) = FPREG;
342 n = block(REG, 0, 0, INT, 0, 0);
343 regno(n) = regpregs[reg++];
344 p = block(ASSIGN, p, n, INT, 0, 0);
345 ecomp(p);
346 }
347 } else if (cisreg(sp2->stype) && !ISSOU(sp2->stype) &&
348 ((cqual(sp2->stype, sp2->squal) & VOL) == 0) && xtemps) {
349 /* just put rest in temps */
350 if (sp2->sclass == REGISTER) {
351 n = block(REG, 0, 0, sp2->stype,
352 sp2->sdf, sp2->sap);
353 if (ISLONGLONG(sp2->stype))
354 regno(n) = longregs[sp2->soffset];
355 else if (DEUNSIGN(sp2->stype) == CHAR ||
356 sp2->stype == BOOL)
357 regno(n) = charregs[sp2->soffset];
358 else
359 regno(n) = regpregs[sp2->soffset];
360 } else {
361 n = block(OREG, 0, 0, sp2->stype,
362 sp2->sdf, sp2->sap);
363 slval(n, sp2->soffset/SZCHAR);
364 regno(n) = FPREG;
365 }
366 p = tempnode(0, sp2->stype, sp2->sdf, sp2->sap);
367 sp2->soffset = regno(p);
368 sp2->sflags |= STNODE;
369 n = buildtree(ASSIGN, p, n);
370 ecomp(n);
371 }
372 }
373
374 if (attr_find(cftnsp->sap, GCC_ATYP_STDCALL)) {
375 #ifdef PECOFFABI
376 char buf[256];
377 char *name;
378 #endif
379 /* XXX interaction STDCALL and struct return? */
380 argstacksize += (argbase - ARGINIT)/SZCHAR;
381 #ifdef PECOFFABI
382 /*
383 * mangle name in symbol table as a callee.
384 */
385 name = getexname(cftnsp);
386 snprintf(buf, 256, "%s@%d", name, argstacksize);
387 cftnsp->soname = addname(buf);
388 #endif
389 }
390
391 }
392
393 #if defined(MACHOABI)
394 struct stub stublist;
395 struct stub nlplist;
396 #endif
397
398 /* called just before final exit */
399 /* flag is 1 if errors, 0 if none */
400 void
401 ejobcode(int flag)
402 {
403 #if defined(MACHOABI)
404 /*
405 * iterate over the stublist and output the PIC stubs
406 ` */
407 if (kflag) {
408 struct stub *p;
409
410 DLIST_FOREACH(p, &stublist, link) {
411 printf(PRTPREF "\t.section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5\n");
412 printf(PRTPREF "L%s$stub:\n", p->name);
413 printf(PRTPREF "\t.indirect_symbol %s\n", p->name);
414 printf(PRTPREF "\thlt ; hlt ; hlt ; hlt ; hlt\n");
415 printf(PRTPREF "\t.subsections_via_symbols\n");
416 }
417
418 printf(PRTPREF "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
419 DLIST_FOREACH(p, &nlplist, link) {
420 printf(PRTPREF "L%s$non_lazy_ptr:\n", p->name);
421 printf(PRTPREF "\t.indirect_symbol %s\n", p->name);
422 printf(PRTPREF "\t.long 0\n");
423 }
424
425 }
426 #endif
427
428 printf(PRTPREF "\t.ident \"PCC: %s\"\n", VERSSTR);
429 }
430
431 void
432 bjobcode(void)
433 {
434 #ifdef os_sunos
435 astypnames[SHORT] = astypnames[USHORT] = "\t.2byte";
436 #endif
437 astypnames[INT] = astypnames[UNSIGNED] = "\t.long";
438 #if defined(MACHOABI)
439 DLIST_INIT(&stublist, link);
440 DLIST_INIT(&nlplist, link);
441 #endif
442 #if defined(__GNUC__) || defined(__PCC__)
443 /* Be sure that the compiler uses full x87 */
444 /* XXX cross-compiling will fail here */
445 volatile int fcw;
446 __asm("fstcw (%0)" : : "r"(&fcw));
447 fcw |= 0x300;
448 __asm("fldcw (%0)" : : "r"(&fcw));
449 #endif
450 }
451
452 /*
453 * Convert FUNARG to assign in case of regparm.
454 */
455 static int regcvt, rparg, fcall;
456 static void
457 addreg(NODE *p)
458 {
459 TWORD t;
460 NODE *q;
461 int sz, r;
462
463 sz = (int)tsize(p->n_type, p->n_df, p->n_ap)/SZCHAR;
464 sz = (sz + 3) >> 2; /* sz in regs */
465 if ((regcvt+sz) > rparg) {
466 regcvt = rparg;
467 return;
468 }
469 if (sz > 2)
470 uerror("cannot put struct in 3 regs (yet)");
471
472 if (sz == 2)
473 r = regcvt == 0 ? EAXEDX : EDXECX;
474 else if (fcall)
475 r = regcvt == 0 ? ECX : EDX;
476 else
477 r = regcvt == 0 ? EAX : regcvt == 1 ? EDX : ECX;
478
479 if (p->n_op == FUNARG) {
480 /* at most 2 regs */
481 if (p->n_type < INT) {
482 p->n_left = ccast(p->n_left, INT, 0, 0, 0);
483 p->n_type = INT;
484 }
485
486 p->n_op = ASSIGN;
487 p->n_right = p->n_left;
488 } else if (p->n_op == STARG) {
489 /* convert to ptr, put in reg */
490 q = p->n_left;
491 t = sz == 2 ? LONGLONG : INT;
492 q = cast(q, INCREF(t), 0);
493 q = buildtree(UMUL, q, NIL);
494 p->n_op = ASSIGN;
495 p->n_type = t;
496 p->n_right = q;
497 } else
498 cerror("addreg");
499 p->n_left = block(REG, 0, 0, p->n_type, 0, 0);
500 regno(p->n_left) = r;
501 regcvt += sz;
502 }
503
504 /*
505 * Called with a function call with arguments as argument.
506 * This is done early in buildtree() and only done once.
507 * Returns p.
508 */
509 NODE *
510 funcode(NODE *p)
511 {
512 extern int gotnr;
513 struct attr *ap;
514 NODE *r, *l;
515 TWORD t = DECREF(DECREF(p->n_left->n_type));
516 int stcall;
517
518 stcall = ISSOU(t);
519 /*
520 * We may have to prepend:
521 * - Hidden arg0 for struct return (in reg or on stack).
522 * - ebx in case of PIC code.
523 */
524
525 /* Fix function call arguments. On x86, just add funarg */
526 for (r = p->n_right; r->n_op == CM; r = r->n_left) {
527 if (r->n_right->n_op != STARG)
528 r->n_right = block(FUNARG, r->n_right, NIL,
529 r->n_right->n_type, r->n_right->n_df,
530 r->n_right->n_ap);
531 }
532 if (r->n_op != STARG) {
533 l = talloc();
534 *l = *r;
535 r->n_op = FUNARG;
536 r->n_left = l;
537 r->n_type = l->n_type;
538 }
539 #ifdef os_openbsd
540 if (stcall && (ap = strattr(p->n_left->n_ap)) &&
541 ap->amsize != SZCHAR && ap->amsize != SZSHORT &&
542 ap->amsize != SZINT && ap->amsize != SZLONGLONG)
543 #else
544 if (stcall &&
545 (attr_find(p->n_left->n_ap, ATTR_COMPLEX) == 0 ||
546 ((ap = strattr(p->n_left->n_ap)) && ap->amsize > SZLONGLONG)))
547 #endif
548 {
549 /* Prepend a placeholder for struct address. */
550 /* Use EBP, can never show up under normal circumstances */
551 l = talloc();
552 *l = *r;
553 r->n_op = CM;
554 r->n_right = l;
555 r->n_type = INT;
556 l = block(REG, 0, 0, INCREF(VOID), 0, 0);
557 regno(l) = EBP;
558 l = block(FUNARG, l, 0, INCREF(VOID), 0, 0);
559 r->n_left = l;
560 }
561
562 #ifdef GCC_COMPAT
563 fcall = 0;
564 if ((ap = attr_find(p->n_left->n_ap, GCC_ATYP_REGPARM)))
565 rparg = ap->iarg(0);
566 else if ((ap = attr_find(p->n_left->n_ap, GCC_ATYP_FASTCALL)))
567 fcall = rparg = 2;
568 else
569 #endif
570 rparg = 0;
571
572 regcvt = 0;
573 if (rparg)
574 p1listf(p->n_right, addreg);
575
576 if (kflag == 0)
577 return p;
578
579 #if defined(ELFABI)
580 /* Create an ASSIGN node for ebx */
581 l = block(REG, NIL, NIL, INT, 0, 0);
582 l->n_rval = EBX;
583 l = buildtree(ASSIGN, l, tempnode(gotnr, INT, 0, 0));
584 if (p->n_right->n_op != CM) {
585 p->n_right = block(CM, l, p->n_right, INT, 0, 0);
586 } else {
587 for (r = p->n_right; r->n_left->n_op == CM; r = r->n_left)
588 ;
589 r->n_left = block(CM, l, r->n_left, INT, 0, 0);
590 }
591 #endif
592 return p;
593 }
594
595 /* fix up type of field p */
596 void
597 fldty(struct symtab *p)
598 {
599 }
600
601 /*
602 * XXX - fix genswitch.
603 */
604 int
605 mygenswitch(int num, TWORD type, struct swents **p, int n)
606 {
607 return 0;
608 }
609
610 NODE *
611 builtin_return_address(const struct bitable *bt, NODE *a)
612 {
613 int nframes;
614 NODE *f;
615
616 if (a->n_op != ICON)
617 goto bad;
618
619 nframes = (int)glval(a);
620
621 p1tfree(a);
622
623 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
624 regno(f) = FPREG;
625
626 while (nframes--)
627 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
628
629 f = block(PLUS, f, bcon(4), INCREF(PTR+VOID), 0, 0);
630 f = buildtree(UMUL, f, NIL);
631
632 return f;
633 bad:
634 uerror("bad argument to __builtin_return_address");
635 return bcon(0);
636 }
637
638 NODE *
639 builtin_frame_address(const struct bitable *bt, NODE *a)
640 {
641 int nframes;
642 NODE *f;
643
644 if (a->n_op != ICON)
645 goto bad;
646
647 nframes = (int)glval(a);
648
649 p1tfree(a);
650
651 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
652 regno(f) = FPREG;
653
654 while (nframes--)
655 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
656
657 return f;
658 bad:
659 uerror("bad argument to __builtin_frame_address");
660 return bcon(0);
661 }
662
663 /*
664 * Return "canonical frame address".
665 */
666 NODE *
667 builtin_cfa(const struct bitable *bt, NODE *a)
668 {
669 uerror("missing builtin_cfa");
670 return bcon(0);
671 }
672