Debian Codegraph @

arch/amd64/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.70 2018/11/24 21:03:55 ragge Exp $ */
2 /*
3 * Copyright (c) 2008 Michael Shalayeff
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 # include "pass2.h"
31 # include <ctype.h>
32 # include <string.h>
33
34 static int stkpos;
35
36 void
37 deflab(int label)
38 {
39 printf(LABFMT ":\n", label);
40 }
41
42 static int regoff[MAXREGS];
43 static TWORD ftype;
44 char *rbyte[], *rshort[], *rlong[];
45 static int needframe;
46 int mcmodel = MCSMALL;
47
48 /*
49 * Print out the prolog assembler.
50 * addto and regoff are already calculated.
51 */
52 static void
53 prtprolog(struct interpass_prolog *ipp, int addto)
54 {
55 int i;
56
57 printf("\tpushq %%rbp\n");
58 printf("\tmovq %%rsp,%%rbp\n");
59 addto = (addto+15) & ~15; /* 16-byte aligned */
60 if (addto)
61 printf("\tsubq $%d,%%rsp\n", addto);
62
63 /* save permanent registers */
64 for (i = 0; i < MAXREGS; i++)
65 if (TESTBIT(p2env.p_regs, i))
66 printf("\tmovq %s,-%d(%s)\n",
67 rnames[i], regoff[i], rnames[FPREG]);
68 }
69
70 /*
71 * calculate stack size and offsets
72 */
73 static int
74 offcalc(struct interpass_prolog *ipp)
75 {
76 int i, addto;
77
78 addto = p2maxautooff;
79 if (addto >= AUTOINIT/SZCHAR)
80 addto -= AUTOINIT/SZCHAR;
81 for (i = 0; i < MAXREGS; i++)
82 if (TESTBIT(p2env.p_regs, i)) {
83 addto += SZLONG/SZCHAR;
84 regoff[i] = addto;
85 }
86 return addto;
87 }
88
89 /*
90 * Traverse a tree to check if we need to emit a frame at all.
91 * We emit it if:
92 * - any function call
93 * - rsp or rbp referenced
94 * Return 1 if frame is needed, 0 otherwise.
95 */
96 static int
97 chkf(NODE *p)
98 {
99 int o = p->n_op;
100
101 if ((o == REG || o == OREG) && (regno(p) == RBP || regno(p) == RSP))
102 return 1;
103 if (callop(o))
104 return 1;
105 if (optype(o) == UTYPE)
106 return chkf(p->n_left);
107 else if (optype(o) == BITYPE)
108 return chkf(p->n_left) || chkf(p->n_right);
109 return 0;
110 }
111
112 static int
113 chkframe(struct interpass_prolog *ipp)
114 {
115 struct interpass *ip;
116
117 DLIST_FOREACH(ip, &ipp->ipp_ip, qelem) {
118 if (ip->type == IP_EPILOG)
119 break;
120 if (ip->type == IP_NODE) {
121 if (chkf(ip->ip_node))
122 return 1;
123 }
124 }
125 return 0;
126 }
127
128 void
129 prologue(struct interpass_prolog *ipp)
130 {
131 int addto;
132
133 ftype = ipp->ipp_type;
134
135 if (xdeljumps)
136 needframe = chkframe(ipp);
137 else
138 needframe = 1;
139
140 #ifdef LANG_F77
141 if (ipp->ipp_vis)
142 printf(" .globl %s\n", ipp->ipp_name);
143 printf(" .align 16\n");
144 printf("%s:\n", ipp->ipp_name);
145 #endif
146 /*
147 * We here know what register to save and how much to
148 * add to the stack.
149 */
150 addto = offcalc(ipp);
151 if (addto)
152 needframe = 1;
153 if (needframe)
154 prtprolog(ipp, addto);
155 }
156
157 void
158 eoftn(struct interpass_prolog *ipp)
159 {
160 int i;
161
162 if (ipp->ipp_ip.ip_lbl == 0)
163 return; /* no code needs to be generated */
164
165 if (needframe) {
166 /* return from function code */
167 for (i = 0; i < MAXREGS; i++)
168 if (TESTBIT(p2env.p_regs, i))
169 printf(" movq -%d(%s),%s\n",
170 regoff[i], rnames[FPREG], rnames[i]);
171
172 /* struct return needs special treatment */
173 if (ftype == STRTY || ftype == UNIONTY) {
174 printf(" movl 8(%%ebp),%%eax\n");
175 printf(" leave\n");
176 printf(" ret $%d\n", 4);
177 } else {
178 printf(" leave\n");
179 printf(" ret\n");
180 }
181 } else
182 printf("\tret\n");
183
184 #ifndef MACHOABI
185 printf("\t.size %s,.-%s\n", ipp->ipp_name, ipp->ipp_name);
186 #endif
187 }
188
189 /*
190 * add/sub/...
191 *
192 * Param given:
193 */
194 void
195 hopcode(int f, int o)
196 {
197 char *str;
198
199 switch (o) {
200 case PLUS:
201 str = "add";
202 break;
203 case MINUS:
204 str = "sub";
205 break;
206 case AND:
207 str = "and";
208 break;
209 case OR:
210 str = "or";
211 break;
212 case ER:
213 str = "xor";
214 break;
215 default:
216 comperr("hopcode2: %d", o);
217 str = 0; /* XXX gcc */
218 }
219 printf("%s%c", str, f);
220 }
221
222 /*
223 * Return type size in bytes. Used by R2REGS, arg 2 to offset().
224 */
225 int
226 tlen(NODE *p)
227 {
228 switch(p->n_type) {
229 case CHAR:
230 case UCHAR:
231 return(1);
232
233 case SHORT:
234 case USHORT:
235 return(SZSHORT/SZCHAR);
236
237 case DOUBLE:
238 return(SZDOUBLE/SZCHAR);
239
240 case INT:
241 case UNSIGNED:
242 return(SZINT/SZCHAR);
243
244 case LONG:
245 case ULONG:
246 case LONGLONG:
247 case ULONGLONG:
248 return SZLONGLONG/SZCHAR;
249
250 default:
251 if (!ISPTR(p->n_type))
252 comperr("tlen type %d not pointer");
253 return SZPOINT(p->n_type)/SZCHAR;
254 }
255 }
256
257 /*
258 * Compare two floating point numbers.
259 */
260 static void
261 fcomp(NODE *p)
262 {
263
264 int swap = ((p->n_su & DORIGHT) != 0);
265 int failjump = attr_find(p->n_ap, ATTR_FP_SWAPPED) != 0;
266
267 //printf("fcomp: DOR %d op %s\n", swap, opst[p->n_op]);
268 if (p->n_op == GT || p->n_op == GE) {
269 swap ^= 1;
270 p->n_op = (p->n_op == GT ? LT : LE);
271 }
272 //printf("fcomp2: DOR %d op %s\n", swap, opst[p->n_op]);
273 if (swap)
274 expand(p, 0, "\tfxch\n");
275
276 expand(p, 0, "\tfucomip %st(1),%st\n"); /* emit compare insn */
277 expand(p, 0, "\tfstp %st(0)\n"); /* pop fromstack */
278
279 switch (p->n_op) {
280 case EQ: /* jump if: Z is set and P is clear */
281 expand(p, 0, "\tjne 1f\n");
282 expand(p, 0, "\tjnp LC\n");
283 expand(p, 0, "\t1:\n");
284 break;
285 case NE: /* jump if: Z is clear or P is set */
286 expand(p, 0, "\tjne LC\n");
287 expand(p, 0, "\tjp LC\n");
288 break;
289 case LT:
290 expand(p, 0, "\tja LC\n");
291 if (failjump)
292 expand(p, 0, "\tjp LC\n");
293 break;
294 case LE:
295 expand(p, 0, "\tjae LC\n");
296 if (failjump)
297 expand(p, 0, "\tjp LC\n");
298 break;
299 }
300 }
301
302 int
303 fldexpand(NODE *p, int cookie, char **cp)
304 {
305 comperr("fldexpand");
306 return 0;
307 }
308
309 static void
310 stasg(NODE *p)
311 {
312 struct attr *ap = attr_find(p->n_ap, ATTR_P2STRUCT);
313 expand(p, INAREG, " leaq AL,%rdi\n");
314 if (ap->iarg(0) >= 8)
315 printf("\tmovl $%d,%%ecx\n\trep movsq\n", ap->iarg(0) >> 3);
316 if (ap->iarg(0) & 4)
317 printf("\tmovsl\n");
318 if (ap->iarg(0) & 2)
319 printf("\tmovsw\n");
320 if (ap->iarg(0) & 1)
321 printf("\tmovsb\n");
322 }
323
324 #define E(x) expand(p, 0, x)
325 /*
326 * Generate code to convert an unsigned long to xmm float/double.
327 */
328 static void
329 ultofd(NODE *p)
330 {
331
332 E(" movq AL,A1\n");
333 E(" testq A1,A1\n");
334 E(" js 2f\n");
335 E(" cvtsi2sZfq A1,A3\n");
336 E(" jmp 3f\n");
337 E("2:\n");
338 E(" movq A1,A2\n");
339 E(" shrq A2\n");
340 E(" andq $1,A1\n");
341 E(" orq A1,A2\n");
342 E(" cvtsi2sZfq A2,A3\n");
343 E(" addsZf A3,A3\n");
344 E("3:\n");
345 }
346
347 /*
348 * Generate code to convert an SSE float/double to an unsigned long.
349 */
350 static void
351 fdtoul(NODE *p)
352 {
353 if (p->n_left->n_type == FLOAT)
354 E(" movabsq $0x5f000000,A1\n");
355 else
356 E(" movabsq $0x43e0000000000000,A1\n");
357 E(" movd A1,A3\n");
358 E(" ucomisZg A3,AL\n");
359 E(" jae 2f\n");
360 E(" cvttsZg2siq AL,A1\n");
361 E(" jmp 3f\n");
362 E("2:\n");
363 E(" subsZg A3,AL\n");
364 E(" cvttsZg2siq AL,A1\n");
365 E(" movabsq $0x8000000000000000,A2\n");
366 E(" xorq A2,A1\n");
367 E("3:\n");
368 }
369 #undef E
370
371 void
372 zzzcode(NODE *p, int c)
373 {
374 struct attr *ap, *ap2;
375 NODE *l;
376 int pr, lr, s;
377 char **rt;
378
379 switch (c) {
380 case 'A': /* swap st0 and st1 if right is evaluated second */
381 if ((p->n_su & DORIGHT) == 0) {
382 if (logop(p->n_op))
383 printf(" fxch\n");
384 else
385 printf("r");
386 }
387 break;
388
389 case 'b': /* float/double to unsigned long cast */
390 fdtoul(p);
391 break;
392
393 case 'C': /* remove from stack after subroutine call */
394 pr = p->n_qual;
395 if (p->n_op == UCALL)
396 return; /* XXX remove ZC from UCALL */
397 if (pr)
398 printf(" addq $%d, %s\n", pr, rnames[RSP]);
399 #define STRREG 6
400 #define STRSSE 8
401 #define STRIF 9
402 #define STRFI 10
403 #define STRX87 11
404 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
405 ap2 = attr_find(p->n_ap, ATTR_AMD64_CMPLRET);
406 if ((p->n_op == STCALL || p->n_op == USTCALL) &&
407 ap->iarg(0) == 32 && ap2->iarg(0) == STRX87) {
408 printf("\tfstpt -%d(%%rbp)\n", stkpos);
409 printf("\tfstpt -%d(%%rbp)\n", stkpos-16);
410 printf("\tleaq -%d(%%rbp),%%rax\n", stkpos);
411 }
412 if ((p->n_op == STCALL || p->n_op == USTCALL) &&
413 ap->iarg(0) <= 16) {
414 /* store reg-passed structs on stack */
415 if (ap2->iarg(0) == STRREG || ap2->iarg(0) == STRIF)
416 printf("\tmovq %%rax,-%d(%%rbp)\n", stkpos);
417 else
418 printf("\tmovsd %%xmm0,-%d(%%rbp)\n", stkpos);
419 if (ap->iarg(0) > 8) {
420 if (ap2->iarg(0) == STRREG)
421 printf("\tmovq %%rdx");
422 else if (ap2->iarg(0) == STRFI)
423 printf("\tmovq %%rax");
424 else if (ap2->iarg(0) == STRIF)
425 printf("\tmovsd %%xmm0");
426 else
427 printf("\tmovsd %%xmm1");
428 printf(",-%d(%%rbp)\n", stkpos-8);
429 }
430 printf("\tleaq -%d(%%rbp),%%rax\n", stkpos);
431 }
432 break;
433
434 case 'c': /* xor label */
435 if ((ap = attr_find(p->n_ap, ATTR_AMD64_XORLBL)) == NULL)
436 comperr("missing xor label");
437 printf(LABFMT, ap->iarg(0));
438 break;
439
440 case 'F': /* Structure argument */
441 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
442 printf(" subq $%d,%%rsp\n", ap->iarg(0));
443 printf(" movq %%rsp,%%rsi\n");
444 stasg(p);
445 break;
446
447 case 'G': /* Floating point compare */
448 fcomp(p);
449 break;
450
451 case 'j': /* convert unsigned long to f/d */
452 ultofd(p);
453 break;
454
455 case 'M': /* Output sconv move, if needed */
456 l = getlr(p, 'L');
457 /* XXX fixneed: regnum */
458 pr = DECRA(p->n_reg, 0);
459 lr = DECRA(l->n_reg, 0);
460 if (pr == lr)
461 break;
462 printf(" movb %s,%s\n", rbyte[lr], rbyte[pr]);
463 l->n_rval = l->n_reg = p->n_reg; /* XXX - not pretty */
464 break;
465
466 case 'N': /* output long reg name */
467 printf("%s", rlong[getlr(p, '1')->n_rval]);
468 break;
469
470 case 'P': /* Put hidden argument in rdi */
471 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
472 ap2 = attr_find(p->n_ap, ATTR_AMD64_CMPLRET);
473 if (ap->iarg(0) > 16 && ap2->iarg(0) != STRX87)
474 printf("\tleaq -%d(%%rbp),%%rdi\n", stkpos);
475 break;
476
477 case 'Q': /* emit struct assign */
478 stasg(p);
479 break;
480
481 case 'R': /* print opname based on right type */
482 case 'L': /* print opname based on left type */
483 switch (getlr(p, c)->n_type) {
484 case CHAR: case UCHAR: s = 'b'; break;
485 case SHORT: case USHORT: s = 'w'; break;
486 case INT: case UNSIGNED: s = 'l'; break;
487 default: s = 'q'; break;
488 }
489 printf("%c", s);
490 break;
491
492 case 'U': { /* output branch insn for ucomi */
493 static char *fpcb[] = { "jz", "jnz", "jbe", "jc", "jnc", "ja" };
494 if (p->n_op < EQ || p->n_op > GT)
495 comperr("bad fp branch");
496 if (p->n_op == NE || p->n_op == GT || p->n_op == GE)
497 expand(p, 0, " jp LC\n");
498 else if (p->n_op == EQ)
499 printf("\tjp 1f\n");
500 printf(" %s ", fpcb[p->n_op - EQ]);
501 expand(p, 0, "LC\n");
502 if (p->n_op == EQ)
503 printf("1:\n");
504 break;
505 }
506
507 case '8': /* special reg name printout (64-bit) */
508 case '1': /* special reg name printout (32-bit) */
509 l = getlr(p, '1');
510 rt = c == '8' ? rnames : rlong;
511 printf("%s", rt[l->n_rval]);
512 break;
513
514 case 'g':
515 p = p->n_left;
516 /* FALLTHROUGH */
517 case 'f': /* float or double */
518 printf("%c", p->n_type == FLOAT ? 's' : 'd');
519 break;
520
521 case 'q': /* int or long */
522 printf("%c", p->n_left->n_type == LONG ? 'q' : ' ');
523 break;
524
525 default:
526 comperr("zzzcode %c", c);
527 }
528 }
529
530 int canaddr(NODE *);
531 int
532 canaddr(NODE *p)
533 {
534 int o = p->n_op;
535
536 if (o==NAME || o==REG || o==ICON || o==OREG ||
537 (o==UMUL && shumul(p->n_left, SOREG)))
538 return(1);
539 return(0);
540 }
541
542 /*
543 * Does the bitfield shape match?
544 */
545 int
546 flshape(NODE *p)
547 {
548 comperr("flshape");
549 return(0);
550 }
551
552 /* INTEMP shapes must not contain any temporary registers */
553 /* XXX should this go away now? */
554 int
555 shtemp(NODE *p)
556 {
557 return 0;
558 #if 0
559 int r;
560
561 if (p->n_op == STARG )
562 p = p->n_left;
563
564 switch (p->n_op) {
565 case REG:
566 return (!istreg(p->n_rval));
567
568 case OREG:
569 r = p->n_rval;
570 if (R2TEST(r)) {
571 if (istreg(R2UPK1(r)))
572 return(0);
573 r = R2UPK2(r);
574 }
575 return (!istreg(r));
576
577 case UMUL:
578 p = p->n_left;
579 return (p->n_op != UMUL && shtemp(p));
580 }
581
582 if (optype(p->n_op) != LTYPE)
583 return(0);
584 return(1);
585 #endif
586 }
587
588 void
589 adrcon(CONSZ val)
590 {
591 printf("$" CONFMT, val);
592 }
593
594 void
595 conput(FILE *fp, NODE *p)
596 {
597 long val = getlval(p);
598
599 switch (p->n_op) {
600 case ICON:
601 if (p->n_name[0] != '\0') {
602 fprintf(fp, "%s", p->n_name);
603 if (val)
604 fprintf(fp, "+%ld", val);
605 } else
606 fprintf(fp, "%ld", val);
607 return;
608
609 default:
610 comperr("illegal conput, p %p", p);
611 }
612 }
613
614 /*ARGSUSED*/
615 void
616 insput(NODE *p)
617 {
618 comperr("insput");
619 }
620
621 /*
622 * Write out the upper address, like the upper register of a 2-register
623 * reference, or the next memory location.
624 * XXX - not needed on amd64
625 */
626 void
627 upput(NODE *p, int size)
628 {
629
630 size /= SZCHAR;
631 switch (p->n_op) {
632 case REG:
633 printf("%%%s", &rnames[p->n_rval][3]);
634 break;
635
636 case NAME:
637 case OREG:
638 setlval(p, getlval(p) + size);
639 adrput(stdout, p);
640 setlval(p, getlval(p) - size);
641 break;
642 case ICON:
643 printf("$" CONFMT, getlval(p) >> 32);
644 break;
645 default:
646 comperr("upput bad op %d size %d", p->n_op, size);
647 }
648 }
649
650 void
651 adrput(FILE *io, NODE *p)
652 {
653 int r;
654 char **rc;
655 /* output an address, with offsets, from p */
656
657 switch (p->n_op) {
658
659 case NAME:
660 if (p->n_name[0] != '\0') {
661 if (getlval(p) != 0)
662 fprintf(io, CONFMT "+", getlval(p));
663 fprintf(io, "%s(%%rip)", p->n_name);
664 } else
665 fprintf(io, CONFMT, getlval(p));
666 return;
667
668 case OREG:
669 r = p->n_rval;
670 if (p->n_name[0])
671 printf("%s%s", p->n_name, getlval(p) ? "+" : "");
672 if (getlval(p))
673 fprintf(io, "%lld", getlval(p));
674 if (R2TEST(r)) {
675 int r1 = R2UPK1(r);
676 int r2 = R2UPK2(r);
677 int sh = R2UPK3(r);
678
679 fprintf(io, "(%s,%s,%d)",
680 r1 == MAXREGS ? "" : rnames[r1],
681 r2 == MAXREGS ? "" : rnames[r2], sh);
682 } else
683 fprintf(io, "(%s)", rnames[p->n_rval]);
684 return;
685 case ICON:
686 /* addressable value of the constant */
687 fputc('$', io);
688 conput(io, p);
689 return;
690
691 case REG:
692 switch (p->n_type) {
693 case CHAR:
694 case UCHAR:
695 rc = rbyte;
696 break;
697 case SHORT:
698 case USHORT:
699 rc = rshort;
700 break;
701 case INT:
702 case UNSIGNED:
703 rc = rlong;
704 break;
705 default:
706 rc = rnames;
707 break;
708 }
709 fprintf(io, "%s", rc[p->n_rval]);
710 return;
711
712 default:
713 comperr("illegal address, op %d, node %p", p->n_op, p);
714 return;
715
716 }
717 }
718
719 static char *
720 ccbranches[] = {
721 "je", /* jumpe */
722 "jne", /* jumpn */
723 "jle", /* jumple */
724 "jl", /* jumpl */
725 "jge", /* jumpge */
726 "jg", /* jumpg */
727 "jbe", /* jumple (jlequ) */
728 "jb", /* jumpl (jlssu) */
729 "jae", /* jumpge (jgequ) */
730 "ja", /* jumpg (jgtru) */
731 };
732
733
734 /* printf conditional and unconditional branches */
735 void
736 cbgen(int o, int lab)
737 {
738 if (o < EQ || o > UGT)
739 comperr("bad conditional branch: %s", opst[o]);
740 printf(" %s " LABFMT "\n", ccbranches[o-EQ], lab);
741 }
742
743 /*
744 * gcc xasm has the ability to generate different asm types
745 * via some magic.
746 *
747 * Only support AT&T asm for now.
748 */
749 static char *
750 adjustname(char *s)
751 {
752 size_t len = strlen(s);
753 char *d = tmpalloc(len+1);
754 int i, j, flvl, tlvl;
755
756 flvl = tlvl = 0;
757 for (i = j = 0; i < (int)len; i++) {
758 switch (s[i]) {
759 case '{': tlvl++; break;
760 case '}': if (tlvl)tlvl--; else flvl--; break;
761 case '|': tlvl--; flvl++; break;
762 default:
763 if (flvl == 0)
764 d[j++] = s[i];
765 break;
766 }
767 }
768 d[j] = 0;
769 return d;
770 }
771
772 static void
773 fixcalls(NODE *p, void *arg)
774 {
775 int ps;
776
777 /* Prepare for struct return by allocating bounce space on stack */
778 switch (p->n_op) {
779 case STCALL:
780 case USTCALL:
781 ps = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
782 if (ps < 16)
783 ps = 16;
784 if (ps+p2autooff > stkpos)
785 stkpos = ps+p2autooff;
786 break;
787 case XASM:
788 p->n_name = adjustname(p->n_name);
789 break;
790 }
791 }
792
793 void
794 myreader(struct interpass *ipole)
795 {
796 struct interpass *ip;
797
798 stkpos = p2autooff;
799 DLIST_FOREACH(ip, ipole, qelem) {
800 if (ip->type != IP_NODE)
801 continue;
802 walkf(ip->ip_node, fixcalls, 0);
803 }
804 if (stkpos > p2autooff)
805 p2autooff = stkpos;
806 if (stkpos > p2maxautooff)
807 p2maxautooff = stkpos;
808 if (x2debug)
809 printip(ipole);
810 }
811
812 /*
813 * Remove some PCONVs after OREGs are created.
814 */
815 static void
816 pconv2(NODE *p, void *arg)
817 {
818 NODE *q;
819
820 if (p->n_op == PLUS) {
821 if (p->n_type == (PTR|SHORT) || p->n_type == (PTR|USHORT)) {
822 if (p->n_right->n_op != ICON)
823 return;
824 if (p->n_left->n_op != PCONV)
825 return;
826 if (p->n_left->n_left->n_op != OREG)
827 return;
828 q = p->n_left->n_left;
829 nfree(p->n_left);
830 p->n_left = q;
831 /*
832 * This will be converted to another OREG later.
833 */
834 }
835 }
836 if (mcmodel & (MCMEDIUM|MCLARGE)) {
837 if (p->n_op == NAME) {
838 q = tcopy(p);
839 p->n_left = q;
840 p->n_op = UMUL;
841 q->n_op = ICON;
842 q->n_type = INCREF(q->n_type);
843 } else if (p->n_op == ADDROF && p->n_left->n_op == UMUL) {
844 q = p->n_left;
845 *p = *p->n_left->n_left;
846 q = nfree(q);
847 nfree(q);
848 }
849 }
850 }
851
852 void
853 mycanon(NODE *p)
854 {
855 walkf(p, pconv2, 0);
856 }
857
858 void
859 myoptim(struct interpass *ip)
860 {
861 }
862
863 void
864 rmove(int s, int d, TWORD t)
865 {
866
867 switch (t) {
868 case INT:
869 case UNSIGNED:
870 printf(" movl %s,%s\n", rlong[s], rlong[d]);
871 break;
872 case CHAR:
873 case UCHAR:
874 printf(" movb %s,%s\n", rbyte[s], rbyte[d]);
875 break;
876 case SHORT:
877 case USHORT:
878 printf(" movw %s,%s\n", rshort[s], rshort[d]);
879 break;
880 case FLOAT:
881 printf(" movss %s,%s\n", rnames[s], rnames[d]);
882 break;
883 case DOUBLE:
884 printf(" movsd %s,%s\n", rnames[s], rnames[d]);
885 break;
886 case LDOUBLE:
887 #ifdef notdef
888 /* a=b()*c(); will generate this */
889 /* XXX can it fail anyway? */
890 comperr("bad float rmove: %d %d", s, d);
891 #endif
892 break;
893 default:
894 printf(" movq %s,%s\n", rnames[s], rnames[d]);
895 break;
896 }
897 }
898
899 /*
900 * For class c, find worst-case displacement of the number of
901 * registers in the array r[] indexed by class.
902 */
903 int
904 COLORMAP(int c, int *r)
905 {
906
907 switch (c) {
908 case CLASSA:
909 return r[CLASSA] < 14;
910 case CLASSB:
911 return r[CLASSB] < 16;
912 case CLASSC:
913 return r[CLASSC] < CREGCNT;
914 }
915 return 0; /* XXX gcc */
916 }
917
918 char *rnames[MAXREGS] = {
919 "%rax", "%rdx", "%rcx", "%rbx", "%rsi", "%rdi", "%rbp", "%rsp",
920 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
921 "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7",
922 "%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13", "%xmm14",
923 "%xmm15",
924 };
925
926 /* register names for shorter sizes */
927 char *rbyte[] = {
928 "%al", "%dl", "%cl", "%bl", "%sil", "%dil", "%bpl", "%spl",
929 "%r8b", "%r9b", "%r10b", "%r11b", "%r12b", "%r13b", "%r14b", "%r15b",
930 };
931 char *rshort[] = {
932 "%ax", "%dx", "%cx", "%bx", "%si", "%di", "%bp", "%sp",
933 "%r8w", "%r9w", "%r10w", "%r11w", "%r12w", "%r13w", "%r14w", "%r15w",
934 };
935 char *rlong[] = {
936 "%eax", "%edx", "%ecx", "%ebx", "%esi", "%edi", "%ebp", "%esp",
937 "%r8d", "%r9d", "%r10d", "%r11d", "%r12d", "%r13d", "%r14d", "%r15d",
938 };
939
940
941 /*
942 * Return a class suitable for a specific type.
943 */
944 int
945 gclass(TWORD t)
946 {
947 if (t == LDOUBLE)
948 return CLASSC;
949 if (t == FLOAT || t == DOUBLE)
950 return CLASSB;
951 return CLASSA;
952 }
953
954 static int
955 argsiz(NODE *p)
956 {
957 TWORD t = p->n_type;
958
959 if (p->n_left->n_op == REG)
960 return 0; /* not on stack */
961 if (t == LDOUBLE)
962 return 16;
963 if (p->n_op == STASG)
964 return attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
965 return 8;
966 }
967
968 /*
969 * Calculate argument sizes.
970 */
971 void
972 lastcall(NODE *p)
973 {
974 NODE *op = p;
975 int size = 0;
976
977 p->n_qual = 0;
978 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
979 return;
980 for (p = p->n_right; p->n_op == CM; p = p->n_left)
981 size += argsiz(p->n_right);
982 size += argsiz(p);
983 size = (size+15) & ~15;
984 if (size)
985 printf(" subq $%d,%s\n", size, rnames[RSP]);
986 op->n_qual = size; /* XXX */
987 }
988
989 /*
990 * Special shapes.
991 */
992 int
993 special(NODE *p, int shape)
994 {
995 int o = p->n_op;
996
997 switch (shape) {
998 case SFUNCALL:
999 if (o == STCALL || o == USTCALL)
1000 return SRREG;
1001 break;
1002 case SPCON:
1003 if (o != ICON || p->n_name[0] ||
1004 getlval(p) < 0 || getlval(p) > 0x7fffffff)
1005 break;
1006 return SRDIR;
1007 case SMIXOR:
1008 return tshape(p, SZERO);
1009 case SMILWXOR:
1010 if (o != ICON || p->n_name[0] ||
1011 getlval(p) == 0 || getlval(p) & 0xffffffff)
1012 break;
1013 return SRDIR;
1014 case SMIHWXOR:
1015 if (o != ICON || p->n_name[0] ||
1016 getlval(p) == 0 || (getlval(p) >> 32) != 0)
1017 break;
1018 return SRDIR;
1019 case SCON32:
1020 if (o != ICON || p->n_name[0])
1021 break;
1022 if (getlval(p) < MIN_INT || getlval(p) > MAX_INT)
1023 break;
1024 return SRDIR;
1025 default:
1026 cerror("special: %x\n", shape);
1027 }
1028 return SRNOPE;
1029 }
1030
1031 /*
1032 * Target-dependent command-line options.
1033 */
1034 void
1035 mflags(char *str)
1036 {
1037 #define S(x) (strcmp(x, str) == 0)
1038
1039 if (S("cmodel=small"))
1040 mcmodel = MCSMALL;
1041 else if (S("cmodel=medium"))
1042 mcmodel = MCMEDIUM;
1043 else if (S("cmodel=large"))
1044 mcmodel = MCLARGE;
1045 else
1046 comperr("bad -m arg");
1047 }
1048
1049 /*
1050 * Do something target-dependent for xasm arguments.
1051 */
1052 int
1053 myxasm(struct interpass *ip, NODE *p)
1054 {
1055 struct interpass *ip2;
1056 int Cmax[] = { 31, 63, 127, 0xffff, 3, 255 };
1057 NODE *in = 0, *ut = 0;
1058 TWORD t;
1059 char *w;
1060 int reg;
1061 int c, cw, v;
1062
1063 cw = xasmcode(p->n_name);
1064 if (cw & (XASMASG|XASMINOUT))
1065 ut = p->n_left;
1066 if ((cw & XASMASG) == 0)
1067 in = p->n_left;
1068
1069 c = XASMVAL(cw);
1070 retry: switch (c) {
1071 case 'D': reg = RDI; break;
1072 case 'S': reg = RSI; break;
1073 case 'A':
1074 case 'a': reg = RAX; break;
1075 case 'b': reg = RBX; break;
1076 case 'c': reg = RCX; break;
1077 case 'd': reg = RDX; break;
1078
1079 case 'Q': reg = RDX; break; /* Always dx for now */
1080
1081 case 'x':
1082 case 'q':
1083 case 't':
1084 case 'u':
1085 p->n_name = tmpstrdup(p->n_name);
1086 w = strchr(p->n_name, c);
1087 *w = 'r'; /* now reg */
1088 return c == 'q' || c == 'x' || c == 't' ? 0 : 1;
1089
1090 case 'I':
1091 case 'J':
1092 case 'K':
1093 case 'L':
1094 case 'M':
1095 case 'N':
1096 if (p->n_left->n_op != ICON) {
1097 if ((c = XASMVAL1(cw))) {
1098 if (c == 'r') {
1099 p->n_name++;
1100 return 0;
1101 }
1102 goto retry;
1103 }
1104 uerror("xasm arg not constant");
1105 }
1106 v = (int)getlval(p->n_left);
1107 if ((c == 'K' && v < -128) ||
1108 (c == 'L' && v != 0xff && v != 0xffff) ||
1109 (c != 'K' && v < 0) ||
1110 (v > Cmax[c-'I']))
1111 uerror("xasm val out of range");
1112 p->n_name = "i";
1113 return 1;
1114
1115 default:
1116 return 0;
1117 }
1118 /* If there are requested either memory or register, delete memory */
1119 w = p->n_name = tmpstrdup(p->n_name);
1120 if (*w == '=')
1121 w++;
1122 *w++ = 'r';
1123 *w = 0;
1124
1125 t = p->n_left->n_type;
1126
1127 if (t == FLOAT || t == DOUBLE) {
1128 reg += 16;
1129 } else if (t == LDOUBLE) {
1130 reg += 32;
1131 }
1132
1133 if (in && ut)
1134 in = tcopy(in);
1135 p->n_left = mklnode(REG, 0, reg, t);
1136 if (ut) {
1137 ip2 = ipnode(mkbinode(ASSIGN, ut, tcopy(p->n_left), t));
1138 DLIST_INSERT_AFTER(ip, ip2, qelem);
1139 }
1140 if (in) {
1141 ip2 = ipnode(mkbinode(ASSIGN, tcopy(p->n_left), in, t));
1142 DLIST_INSERT_BEFORE(ip, ip2, qelem);
1143 }
1144
1145 return 1;
1146 }
1147
1148 void
1149 targarg(char *w, void *arg, int n)
1150 {
1151 NODE **ary = arg;
1152 NODE *p, *q;
1153
1154 if (w[1] < '0' || w[1] > (n + '0'))
1155 uerror("bad xasm arg number %c", w[1]);
1156 if (w[1] == (n + '0'))
1157 p = ary[(int)w[1]-'0' - 1]; /* XXX */
1158 else
1159 p = ary[(int)w[1]-'0'];
1160 p = p->n_left;
1161
1162 if (optype(p->n_op) != LTYPE)
1163 comperr("bad xarg op %d", p->n_op);
1164 q = tcopy(p);
1165 if (q->n_op == REG) {
1166 if (*w == 'k') {
1167 q->n_type = INT;
1168 } else if (*w == 'q') {
1169 q->n_type = LONG;
1170 } else if (*w == 'h' || *w == 'b') {
1171 /* Can do this only because we know dx is used */
1172 printf("%%d%c", *w == 'h' ? 'h' : 'l');
1173 tfree(q);
1174 return;
1175 } else if (*w != 'w') {
1176 cerror("targarg"); /* XXX ??? */
1177 if (q->n_type > UCHAR) {
1178 regno(q) = regno(q)*2+8;
1179 if (*w == 'h')
1180 regno(q)++;
1181 }
1182 q->n_type = INT;
1183 } else
1184 q->n_type = SHORT;
1185 }
1186 adrput(stdout, q);
1187 tfree(q);
1188 }
1189
1190 /*
1191 * target-specific conversion of numeric arguments.
1192 */
1193 int
1194 numconv(void *ip, void *p1, void *q1)
1195 {
1196 NODE *p = p1, *q = q1;
1197 int cw = xasmcode(q->n_name);
1198
1199 switch (XASMVAL(cw)) {
1200 case 'a':
1201 case 'b':
1202 case 'c':
1203 case 'd':
1204 p->n_name = tmpcalloc(2);
1205 p->n_name[0] = XASMVAL(cw);
1206 return 1;
1207 default:
1208 return 0;
1209 }
1210 }
1211
1212 static struct {
1213 char *name; int num;
1214 } xcr[] = {
1215 { "rax", RAX },
1216 { "rbx", RBX },
1217 { "rcx", RCX },
1218 { "rdx", RDX },
1219 { "rsi", RSI },
1220 { "rdi", RDI },
1221 { "r8", R08 },
1222 { "r9", R09 },
1223 { "r10", R10 },
1224 { "r11", R11 },
1225 { "r12", R12 },
1226 { "r13", R13 },
1227 { "r14", R14 },
1228 { "r15", R15 },
1229 { "st", 040 },
1230 { "st(0)", 040 },
1231 { "st(1)", 041 },
1232 { "st(2)", 042 },
1233 { "st(3)", 043 },
1234 { "st(4)", 044 },
1235 { "st(5)", 045 },
1236 { "st(6)", 046 },
1237 { "st(7)", 047 },
1238 { NULL, 0 },
1239 };
1240
1241 /*
1242 * Check for other names of the xasm constraints registers.
1243 */
1244 int xasmconstregs(char *s)
1245 {
1246 int i;
1247
1248 for (i = 0; xcr[i].name; i++)
1249 if (strcmp(xcr[i].name, s) == 0)
1250 return xcr[i].num;
1251 return -1;
1252 }
1253