Debian Codegraph @

arch/powerpc/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.32 2016/07/10 09:49:52 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 #include <assert.h>
30 #include <stdlib.h>
31
32 #include "pass1.h"
33 #include "pass2.h"
34
35 #ifdef LANG_CXX
36 #define p1listf listf
37 #define p1tfree tfree
38 #define p1nfree nfree
39 #define p1tcopy tcopy
40 #else
41 #define NODE P1ND
42 #define talloc p1alloc
43 #endif
44
45 static void genswitch_bintree(int num, TWORD ty, struct swents **p, int n);
46
47 #if 0
48 static void genswitch_table(int num, struct swents **p, int n);
49 static void genswitch_mrst(int num, struct swents **p, int n);
50 #endif
51
52 static int rvnr;
53
54 /*
55 * Print out assembler segment name.
56 */
57 void
58 setseg(int seg, char *name)
59 {
60 switch (seg) {
61 case PROG: name = ".text"; break;
62 case DATA:
63 case LDATA: name = ".data"; break;
64 case UDATA: break;
65 #ifdef MACHOABI
66 case PICLDATA:
67 case PICDATA: name = ".section .data.rel.rw,\"aw\""; break;
68 case PICRDATA: name = ".section .data.rel.ro,\"aw\""; break;
69 case STRNG: name = ".cstring"; break;
70 case RDATA: name = ".const_data"; break;
71 #else
72 case PICLDATA: name = ".section .data.rel.local,\"aw\",@progbits";break;
73 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
74 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
75 case STRNG:
76 #ifdef AOUTABI
77 case RDATA: name = ".data"; break;
78 #else
79 case RDATA: name = ".section .rodata"; break;
80 #endif
81 #endif
82 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
83 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
84 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
85 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
86 case NMSEG:
87 printf("\t.section %s,\"a%c\",@progbits\n", name,
88 cftnsp ? 'x' : 'w');
89 return;
90 }
91 printf("\t%s\n", name);
92 }
93
94 void
95 defalign(int al)
96 {
97 if (ispow2(al/ALCHAR))
98 printf("\t.p2align %d\n", ispow2(al/ALCHAR));
99 }
100
101
102 /*
103 * Define everything needed to print out some data (or text).
104 * This means segment, alignment, visibility, etc.
105 */
106 void
107 defloc(struct symtab *sp)
108 {
109 char *name;
110
111 name = getexname(sp);
112 if (sp->sclass == EXTDEF)
113 printf(" .globl %s\n", name);
114 if (sp->slevel == 0)
115 printf("%s:\n", name);
116 else
117 printf(LABFMT ":\n", sp->soffset);
118 }
119
120 /* Put a symbol in a temporary
121 * used by bfcode() and its helpers
122 */
123 static void
124 putintemp(struct symtab *sym)
125 {
126 NODE *p;
127
128 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
129 p = buildtree(ASSIGN, p, nametree(sym));
130 sym->soffset = regno(p->n_left);
131 sym->sflags |= STNODE;
132 ecomp(p);
133 }
134
135 /* setup a 64-bit parameter (double/ldouble/longlong)
136 * used by bfcode() */
137 static void
138 param_64bit(struct symtab *sym, int *argofsp, int dotemps)
139 {
140 int argofs = *argofsp;
141 NODE *p, *q;
142 int navail;
143
144 #if ALLONGLONG == 64
145 /* alignment */
146 ++argofs;
147 argofs &= ~1;
148 #endif
149
150 navail = NARGREGS - argofs;
151
152 if (navail < 2) {
153 /* half in and half out of the registers */
154 q = block(REG, NIL, NIL, INT, 0, 0);
155 regno(q) = R3 + argofs;
156 p = block(REG, NIL, NIL, INT, 0, 0);
157 regno(p) = FPREG;
158 p = block(PLUS, p, bcon(sym->soffset/SZCHAR), PTR+INT, 0, 0);
159 p = block(UMUL, p, NIL, INT, 0, 0);
160 } else {
161 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
162 regno(q) = R3R4 + argofs;
163 if (dotemps) {
164 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
165 sym->soffset = regno(p);
166 sym->sflags |= STNODE;
167 } else {
168 p = nametree(sym);
169 }
170 }
171 p = buildtree(ASSIGN, p, q);
172 ecomp(p);
173 *argofsp = argofs + 2;
174 }
175
176 /* setup a 32-bit param on the stack
177 * used by bfcode() */
178 static void
179 param_32bit(struct symtab *sym, int *argofsp, int dotemps)
180 {
181 NODE *p, *q;
182
183 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
184 regno(q) = R3 + (*argofsp)++;
185 if (dotemps) {
186 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
187 sym->soffset = regno(p);
188 sym->sflags |= STNODE;
189 } else {
190 p = nametree(sym);
191 }
192 p = buildtree(ASSIGN, p, q);
193 ecomp(p);
194 }
195
196 /* setup a double param on the stack
197 * used by bfcode() */
198 static void
199 param_double(struct symtab *sym, int *argofsp, int dotemps)
200 {
201 NODE *p, *q, *t;
202 int tmpnr;
203
204 /*
205 * we have to dump the double from the general register
206 * into a temp, since the register allocator doesn't like
207 * floats to be in CLASSA. This may not work for -xtemps.
208 */
209
210 if (xtemps) {
211 q = block(REG, NIL, NIL, ULONGLONG, 0, 0);
212 regno(q) = R3R4 + *argofsp;
213 p = block(REG, NIL, NIL, PTR+ULONGLONG, 0, 0);
214 regno(p) = SPREG;
215 p = block(PLUS, p, bcon(-8), INT, 0, 0);
216 p = block(UMUL, p, NIL, ULONGLONG, 0, 0);
217 p = buildtree(ASSIGN, p, q);
218 ecomp(p);
219
220 t = tempnode(0, sym->stype, sym->sdf, sym->sap);
221 tmpnr = regno(t);
222 p = block(REG, NIL, NIL,
223 INCREF(sym->stype), sym->sdf, sym->sap);
224 regno(p) = SPREG;
225 p = block(PLUS, p, bcon(-8), INT, 0, 0);
226 p = block(UMUL, p, NIL, sym->stype, sym->sdf, sym->sap);
227 p = buildtree(ASSIGN, t, p);
228 ecomp(p);
229 } else {
230 /* bounce straight into temp */
231 p = block(REG, NIL, NIL, ULONGLONG, 0, 0);
232 regno(p) = R3R4 + *argofsp;
233 t = tempnode(0, ULONGLONG, 0, 0);
234 tmpnr = regno(t);
235 p = buildtree(ASSIGN, t, p);
236 ecomp(p);
237 }
238
239 (*argofsp) += 2;
240
241 sym->soffset = tmpnr;
242 sym->sflags |= STNODE;
243 }
244
245 /* setup a float param on the stack
246 * used by bfcode() */
247 static void
248 param_float(struct symtab *sym, int *argofsp, int dotemps)
249 {
250 NODE *p, *q, *t;
251 int tmpnr;
252
253 /*
254 * we have to dump the float from the general register
255 * into a temp, since the register allocator doesn't like
256 * floats to be in CLASSA. This may not work for -xtemps.
257 */
258
259 if (xtemps) {
260 /* bounce onto TOS */
261 q = block(REG, NIL, NIL, INT, 0, 0);
262 regno(q) = R3 + (*argofsp);
263 p = block(REG, NIL, NIL, INT, 0, 0);
264 regno(p) = SPREG;
265 p = block(PLUS, p, bcon(-4), INT, 0, 0);
266 p = block(UMUL, p, NIL, INT, 0, 0);
267 p = buildtree(ASSIGN, p, q);
268 ecomp(p);
269
270 t = tempnode(0, sym->stype, sym->sdf, sym->sap);
271 tmpnr = regno(t);
272 p = block(REG, NIL, NIL, INCREF(sym->stype),
273 sym->sdf, sym->sap);
274 regno(p) = SPREG;
275 p = block(PLUS, p, bcon(-4), INT, 0, 0);
276 p = block(UMUL, p, NIL, sym->stype, sym->sdf, sym->sap);
277 p = buildtree(ASSIGN, t, p);
278 ecomp(p);
279 } else {
280 /* bounce straight into temp */
281 p = block(REG, NIL, NIL, INT, 0, 0);
282 regno(p) = R3 + (*argofsp);
283 t = tempnode(0, INT, 0, 0);
284 tmpnr = regno(t);
285 p = buildtree(ASSIGN, t, p);
286 ecomp(p);
287 }
288
289 (*argofsp)++;
290
291 sym->soffset = tmpnr;
292 sym->sflags |= STNODE;
293 }
294
295 /* setup the hidden pointer to struct return parameter
296 * used by bfcode() */
297 static void
298 param_retstruct(void)
299 {
300 NODE *p, *q;
301
302 p = tempnode(0, INCREF(cftnsp->stype), 0, cftnsp->sap);
303 rvnr = regno(p);
304 q = block(REG, NIL, NIL, INCREF(cftnsp->stype),
305 cftnsp->sdf, cftnsp->sap);
306 regno(q) = R3;
307 p = buildtree(ASSIGN, p, q);
308 ecomp(p);
309 }
310
311
312 /* setup struct parameter
313 * push the registers out to memory
314 * used by bfcode() */
315 static void
316 param_struct(struct symtab *sym, int *argofsp)
317 {
318 int argofs = *argofsp;
319 NODE *p, *q;
320 int navail;
321 int sz;
322 int off;
323 int num;
324 int i;
325
326 navail = NARGREGS - argofs;
327 sz = tsize(sym->stype, sym->sdf, sym->sap) / SZINT;
328 off = ARGINIT/SZINT + argofs;
329 num = sz > navail ? navail : sz;
330 for (i = 0; i < num; i++) {
331 q = block(REG, NIL, NIL, INT, 0, 0);
332 regno(q) = R3 + argofs++;
333 p = block(REG, NIL, NIL, INT, 0, 0);
334 regno(p) = SPREG;
335 p = block(PLUS, p, bcon(4*off++), INT, 0, 0);
336 p = block(UMUL, p, NIL, INT, 0, 0);
337 p = buildtree(ASSIGN, p, q);
338 ecomp(p);
339 }
340
341 *argofsp = argofs;
342 }
343
344 /*
345 * code for the beginning of a function
346 * sp is an array of indices in symtab for the arguments
347 * cnt is the number of arguments
348 */
349 void
350 bfcode(struct symtab **sp, int cnt)
351 {
352 #ifdef USE_GOTNR
353 extern int gotnr;
354 #endif
355
356 struct symtab *sp2;
357 union arglist *usym;
358 int saveallargs = 0;
359 int i, argofs = 0;
360
361 /*
362 * Detect if this function has ellipses and save all
363 * argument registers onto stack.
364 */
365 usym = cftnsp->sdf->dfun;
366 while (usym && usym->type != TNULL) {
367 if (usym->type == TELLIPSIS) {
368 saveallargs = 1;
369 break;
370 }
371 ++usym;
372 }
373
374 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
375 param_retstruct();
376 ++argofs;
377 }
378
379 #ifdef USE_GOTNR
380 if (kflag) {
381 /* put GOT register into temporary */
382 NODE *q, *p;
383 q = block(REG, NIL, NIL, INT, 0, 0);
384 regno(q) = GOTREG;
385 p = tempnode(0, INT, 0, 0);
386 gotnr = regno(p);
387 ecomp(buildtree(ASSIGN, p, q));
388 }
389 #endif
390
391 /* recalculate the arg offset and create TEMP moves */
392 for (i = 0; i < cnt; i++) {
393
394 if (sp[i] == NULL)
395 continue;
396
397 if ((argofs >= NARGREGS) && !xtemps)
398 break;
399
400 if (argofs >= NARGREGS) {
401 putintemp(sp[i]);
402 } else if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY) {
403 param_struct(sp[i], &argofs);
404 } else if (DEUNSIGN(sp[i]->stype) == LONGLONG) {
405 param_64bit(sp[i], &argofs, xtemps && !saveallargs);
406 } else if (sp[i]->stype == DOUBLE || sp[i]->stype == LDOUBLE) {
407 if (features(FEATURE_HARDFLOAT))
408 param_double(sp[i], &argofs,
409 xtemps && !saveallargs);
410 else
411 param_64bit(sp[i], &argofs,
412 xtemps && !saveallargs);
413 } else if (sp[i]->stype == FLOAT) {
414 if (features(FEATURE_HARDFLOAT))
415 param_float(sp[i], &argofs,
416 xtemps && !saveallargs);
417 else
418 param_32bit(sp[i], &argofs,
419 xtemps && !saveallargs);
420 } else {
421 param_32bit(sp[i], &argofs, xtemps && !saveallargs);
422 }
423 }
424
425 /* if saveallargs, save the rest of the args onto the stack */
426 while (saveallargs && argofs < NARGREGS) {
427 NODE *p, *q;
428 /* int off = (ARGINIT+FIXEDSTACKSIZE*SZCHAR)/SZINT + argofs; */
429 int off = ARGINIT/SZINT + argofs;
430 q = block(REG, NIL, NIL, INT, 0, 0);
431 regno(q) = R3 + argofs++;
432 p = block(REG, NIL, NIL, INT, 0, 0);
433 regno(p) = FPREG;
434 p = block(PLUS, p, bcon(4*off), INT, 0, 0);
435 p = block(UMUL, p, NIL, INT, 0, 0);
436 p = buildtree(ASSIGN, p, q);
437 ecomp(p);
438 }
439
440 /* profiling */
441 if (pflag) {
442 NODE *p;
443
444 #if defined(ELFABI)
445
446 sp2 = lookup("_mcount", 0);
447 sp2->stype = EXTERN;
448 p = nametree(sp2);
449 p->n_sp->sclass = EXTERN;
450 p = clocal(p);
451 p = buildtree(ADDROF, p, NIL);
452 p = block(UCALL, p, NIL, INT, 0, 0);
453 ecomp(funcode(p));
454
455
456 #elif defined(MACHOABI)
457
458 NODE *q;
459 int tmpnr;
460
461 q = block(REG, NIL, NIL, INT, 0, 0);
462 regno(q) = R0;
463 p = tempnode(0, INT, 0, 0);
464 tmpnr = regno(p);
465 p = buildtree(ASSIGN, p, q);
466 ecomp(p);
467
468 q = tempnode(tmpnr, INT, 0, 0);
469
470 sp2 = lookup("mcount", 0);
471 sp2->stype = EXTERN;
472 p = nametree(sp2);
473 p->n_sp->sclass = EXTERN;
474 p = clocal(p);
475 p = buildtree(ADDROF, p, NIL);
476 p = block(CALL, p, q, INT, 0, 0);
477 ecomp(funcode(p));
478
479 #endif
480 }
481 }
482
483 /*
484 * code for the end of a function
485 * deals with struct return here
486 */
487 void
488 efcode(void)
489 {
490 NODE *p, *q;
491 int tempnr;
492 int ty;
493
494 #ifdef USE_GOTNR
495 extern int gotnr;
496 gotnr = 0;
497 #endif
498
499 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
500 return;
501
502 ty = cftnsp->stype - FTN;
503
504 q = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap);
505 regno(q) = R3;
506 p = tempnode(0, INCREF(ty), 0, cftnsp->sap);
507 tempnr = regno(p);
508 p = buildtree(ASSIGN, p, q);
509 ecomp(p);
510
511 q = tempnode(tempnr, INCREF(ty), 0, cftnsp->sap);
512 q = buildtree(UMUL, q, NIL);
513
514 p = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap);
515 p = buildtree(UMUL, p, NIL);
516
517 p = buildtree(ASSIGN, p, q);
518 ecomp(p);
519
520 q = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap);
521 p = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap);
522 regno(p) = R3;
523 p = buildtree(ASSIGN, p, q);
524 ecomp(p);
525 }
526
527 struct stub stublist;
528 struct stub nlplist;
529
530 /* called just before final exit */
531 /* flag is 1 if errors, 0 if none */
532 void
533 ejobcode(int flag)
534 {
535
536 #if defined(MACHOABI)
537 /*
538 * iterate over the stublist and output the PIC stubs
539 ` */
540 if (kflag) {
541 struct stub *p;
542
543 DLIST_FOREACH(p, &stublist, link) {
544 printf("\t.section __TEXT, __picsymbolstub1,symbol_stubs,pure_instructions,32\n");
545 printf("\t.align 5\n");
546 printf("L%s$stub:\n", p->name);
547 if (strcmp(p->name, "mcount") == 0)
548 printf("\t.indirect_symbol %s\n", p->name);
549 else
550 printf("\t.indirect_symbol %s\n", p->name);
551 printf("\tmflr r0\n");
552 printf("\tbcl 20,31,L%s$spb\n", p->name);
553 printf("L%s$spb:\n", p->name);
554 printf("\tmflr r11\n");
555 printf("\taddis r11,r11,ha16(L%s$lazy_ptr-L%s$spb)\n",
556 p->name, p->name);
557 printf("\tmtlr r0\n");
558 printf("\tlwzu r12,lo16(L%s$lazy_ptr-L%s$spb)(r11)\n",
559 p->name, p->name);
560 printf("\tmtctr r12\n");
561 printf("\tbctr\n");
562 printf("\t.lazy_symbol_pointer\n");
563 printf("L%s$lazy_ptr:\n", p->name);
564 if (strcmp(p->name, "mcount") == 0)
565 printf("\t.indirect_symbol %s\n", p->name);
566 else
567 printf("\t.indirect_symbol %s\n", p->name);
568 printf("\t.long dyld_stub_binding_helper\n");
569 printf("\t.subsections_via_symbols\n");
570 }
571
572 printf("\t.non_lazy_symbol_pointer\n");
573 DLIST_FOREACH(p, &nlplist, link) {
574 printf("L%s$non_lazy_ptr:\n", p->name);
575 if (strcmp(p->name, "mcount") == 0)
576 printf("\t.indirect_symbol %s\n", p->name);
577 else
578 printf("\t.indirect_symbol %s\n", p->name);
579 printf("\t.long 0\n");
580 }
581
582 }
583 #endif
584
585 #ifndef os_darwin
586 printf("\t.ident \"PCC: %s\"\n", VERSSTR);
587 #endif
588
589 }
590
591 void
592 bjobcode(void)
593 {
594 DLIST_INIT(&stublist, link);
595 DLIST_INIT(&nlplist, link);
596 }
597
598 #ifdef notdef
599 /*
600 * Print character t at position i in one string, until t == -1.
601 * Locctr & label is already defined.
602 */
603 void
604 bycode(int t, int i)
605 {
606 static int lastoctal = 0;
607
608 /* put byte i+1 in a string */
609
610 if (t < 0) {
611 if (i != 0)
612 puts("\"");
613 } else {
614 if (i == 0)
615 printf("\t.ascii \"");
616 if (t == '\\' || t == '"') {
617 lastoctal = 0;
618 putchar('\\');
619 putchar(t);
620 } else if (t < 040 || t >= 0177) {
621 lastoctal++;
622 printf("\\%o",t);
623 } else if (lastoctal && '0' <= t && t <= '9') {
624 lastoctal = 0;
625 printf("\"\n\t.ascii \"%c", t);
626 } else {
627 lastoctal = 0;
628 putchar(t);
629 }
630 }
631 }
632 #endif
633
634 /* fix up type of field p */
635 void
636 fldty(struct symtab *p)
637 {
638 }
639
640 /*
641 * XXX - fix genswitch.
642 */
643 int
644 mygenswitch(int num, TWORD type, struct swents **p, int n)
645 {
646 if (num < 0) {
647 genswitch_bintree(num, type, p, n);
648 return 1;
649 }
650
651 return 0;
652
653 #if 0
654 if (0)
655 genswitch_table(num, p, n);
656 if (0)
657 genswitch_bintree(num, p, n);
658 genswitch_mrst(num, p, n);
659 #endif
660 }
661
662 static void bintree_rec(TWORD ty, int num,
663 struct swents **p, int n, int s, int e);
664
665 static void
666 genswitch_bintree(int num, TWORD ty, struct swents **p, int n)
667 {
668 int lab = getlab();
669
670 if (p[0]->slab == 0)
671 p[0]->slab = lab;
672
673 bintree_rec(ty, num, p, n, 1, n);
674
675 plabel(lab);
676 }
677
678 static void
679 bintree_rec(TWORD ty, int num, struct swents **p, int n, int s, int e)
680 {
681 NODE *r;
682 int rlabel;
683 int h;
684
685 if (s == e) {
686 r = tempnode(num, ty, 0, 0);
687 r = buildtree(NE, r, bcon(p[s]->sval));
688 cbranch(buildtree(NOT, r, NIL), bcon(p[s]->slab));
689 branch(p[0]->slab);
690 return;
691 }
692
693 rlabel = getlab();
694
695 h = s + (e - s) / 2;
696
697 r = tempnode(num, ty, 0, 0);
698 r = buildtree(GT, r, bcon(p[h]->sval));
699 cbranch(r, bcon(rlabel));
700 bintree_rec(ty, num, p, n, s, h);
701 plabel(rlabel);
702 bintree_rec(ty, num, p, n, h+1, e);
703 }
704
705
706 #if 0
707
708 static void
709 genswitch_table(int num, struct swents **p, int n)
710 {
711 NODE *r, *t;
712 int tval;
713 int minval, maxval, range;
714 int deflabel, tbllabel;
715 int i, j;
716
717 minval = p[1]->sval;
718 maxval = p[n]->sval;
719
720 range = maxval - minval + 1;
721
722 if (n < 10 || range > 3 * n) {
723 /* too small or too sparse for jump table */
724 genswitch_simple(num, p, n);
725 return;
726 }
727
728 r = tempnode(num, UNSIGNED, 0, 0);
729 r = buildtree(MINUS, r, bcon(minval));
730 t = tempnode(0, UNSIGNED, 0, 0);
731 tval = regno(t);
732 r = buildtree(ASSIGN, t, r);
733 ecomp(r);
734
735 deflabel = p[0]->slab;
736 if (deflabel == 0)
737 deflabel = getlab();
738
739 t = tempnode(tval, UNSIGNED, 0, 0);
740 cbranch(buildtree(GT, t, bcon(maxval-minval)), bcon(deflabel));
741
742 tbllabel = getlab();
743 struct symtab *strtbl = lookup("__switch_table", SLBLNAME|STEMP);
744 strtbl->soffset = tbllabel;
745 strtbl->sclass = ILABEL;
746 strtbl->stype = INCREF(UCHAR);
747
748 t = block(NAME, NIL, NIL, UNSIGNED, 0, 0);
749 t->n_sp = strtbl;
750 t = buildtree(ADDROF, t, NIL);
751 r = tempnode(tval, UNSIGNED, 0, 0);
752 r = buildtree(PLUS, t, r);
753 t = tempnode(0, INCREF(UNSIGNED), 0, 0);
754 r = buildtree(ASSIGN, t, r);
755 ecomp(r);
756
757 r = tempnode(regno(t), INCREF(UNSIGNED), 0, 0);
758 r = buildtree(UMUL, r, NIL);
759 t = block(NAME, NIL, NIL, UCHAR, 0, 0);
760 t->n_sp = strtbl;
761 t = buildtree(ADDROF, t, NIL);
762 r = buildtree(PLUS, t, r);
763 r = block(GOTO, r, NIL, 0, 0, 0);
764 ecomp(r);
765
766 plabel(tbllabel);
767 for (i = minval, j=1; i <= maxval; i++) {
768 char *entry = tmpalloc(20);
769 int lab = deflabel;
770 //printf("; minval=%d, maxval=%d, i=%d, j=%d p[j]=%lld\n", minval, maxval, i, j, p[j]->sval);
771 if (p[j]->sval == i) {
772 lab = p[j]->slab;
773 j++;
774 }
775 snprintf(entry, 20, "\t.long " LABFMT "-" LABFMT "\n", lab, tbllabel);
776 send_passt(IP_ASM, entry);
777 }
778
779 if (p[0]->slab <= 0)
780 plabel(deflabel);
781 }
782
783 #define DPRINTF(x) if (xdebug) printf x
784 //#define DPRINTF(x) do { } while(0)
785
786 #define MIN_TABLE_SIZE 8
787
788 /*
789 * Multi-way Radix Search Tree (MRST)
790 */
791
792 static void mrst_rec(int num, struct swents **p, int n, int *state, int lab);
793 static unsigned long mrst_find_window(struct swents **p, int n, int *state, int lab, int *len, int *lowbit);
794 void mrst_put_entry_and_recurse(int num, struct swents **p, int n, int *state, int tbllabel, int lab, unsigned long j, unsigned long tblsize, unsigned long Wmax, int lowbit);
795
796 static void
797 genswitch_mrst(int num, struct swents **p, int n)
798 {
799 int *state;
800 int i;
801 int putlabel = 0;
802
803 if (n < 10) {
804 /* too small for MRST */
805 genswitch_simple(num, p, n);
806 return;
807 }
808
809 state = tmpalloc((n+1)*sizeof(int));
810 for (i = 0; i <= n; i++)
811 state[i] = 0;
812
813 if (p[0]->slab == 0) {
814 p[0]->slab = getlab();
815 putlabel = 1;
816 }
817
818 mrst_rec(num, p, n, state, 0);
819
820 if (putlabel)
821 plabel(p[0]->slab);
822 }
823
824
825 /*
826 * Look through the cases and generate a table or
827 * list of simple comparisons. If generating a table,
828 * invoke mrst_put_entry_and_recurse() to put
829 * an entry in the table and recurse.
830 */
831 static void
832 mrst_rec(int num, struct swents **p, int n, int *state, int lab)
833 {
834 int len, lowbit;
835 unsigned long Wmax;
836 unsigned int tblsize;
837 NODE *t;
838 NODE *r;
839 int tval;
840 int i;
841
842 DPRINTF(("mrst_rec: num=%d, n=%d, lab=%d\n", num, n, lab));
843
844 /* find best window to cover set*/
845 Wmax = mrst_find_window(p, n, state, lab, &len, &lowbit);
846 tblsize = (1 << len);
847 assert(len > 0 && tblsize > 0);
848
849 DPRINTF(("mrst_rec: Wmax=%lu, lowbit=%d, tblsize=%u\n",
850 Wmax, lowbit, tblsize));
851
852 if (lab)
853 plabel(lab);
854
855 if (tblsize <= MIN_TABLE_SIZE) {
856 DPRINTF(("msrt_rec: break the recursion\n"));
857 for (i = 1; i <= n; i++) {
858 if (state[i] == lab) {
859 t = tempnode(num, UNSIGNED, 0, 0);
860 cbranch(buildtree(EQ, t, bcon(p[i]->sval)),
861 bcon(p[i]->slab));
862 }
863 }
864 branch(p[0]->slab);
865 return;
866 }
867
868 DPRINTF(("generating table with %d elements\n", tblsize));
869
870 // AND with Wmax
871 t = tempnode(num, UNSIGNED, 0, 0);
872 r = buildtree(AND, t, bcon(Wmax));
873
874 // RS lowbits
875 r = buildtree(RS, r, bcon(lowbit));
876
877 t = tempnode(0, UNSIGNED, 0, 0);
878 tval = regno(t);
879 r = buildtree(ASSIGN, t, r);
880 ecomp(r);
881
882 int tbllabel = getlab();
883 struct symtab *strtbl = lookup("__switch_table", SLBLNAME|STEMP);
884 strtbl->sclass = STATIC;
885 strtbl->sap = 0;
886 strtbl->slevel = 1;
887 strtbl->soffset = tbllabel;
888 strtbl->stype = INCREF(UCHAR);
889 strtbl->squal = (CON >> TSHIFT);
890
891 t = block(NAME, NIL, NIL, UNSIGNED, 0, 0);
892 t->n_sp = strtbl;
893 t = buildtree(ADDROF, t, NIL);
894 r = tempnode(tval, UNSIGNED, 0, 0);
895 r = buildtree(PLUS, t, r);
896 t = tempnode(0, INCREF(UNSIGNED), 0, 0);
897 r = buildtree(ASSIGN, t, r);
898 ecomp(r);
899
900 r = tempnode(regno(t), INCREF(UNSIGNED), 0, 0);
901 r = buildtree(UMUL, r, NIL);
902 t = block(NAME, NIL, NIL, UCHAR, 0, 0);
903 t->n_sp = strtbl;
904 t = buildtree(ADDROF, t, NIL);
905 r = buildtree(PLUS, t, r);
906 r = block(GOTO, r, NIL, 0, 0, 0);
907 ecomp(r);
908
909 plabel(tbllabel);
910
911 mrst_put_entry_and_recurse(num, p, n, state, tbllabel, lab,
912 0, tblsize, Wmax, lowbit);
913 }
914
915
916 /*
917 * Put an entry into the table and recurse to the next entry
918 * in the table. On the way back through the recursion, invoke
919 * mrst_rec() to check to see if we should generate another
920 * table.
921 */
922 void
923 mrst_put_entry_and_recurse(int num, struct swents **p, int n, int *state,
924 int tbllabel, int labval,
925 unsigned long j, unsigned long tblsize, unsigned long Wmax, int lowbit)
926 {
927 int i;
928 int found = 0;
929 int lab = getlab();
930
931 /*
932 * Look for labels which map to this table entry.
933 * Mark each one in "state" that they fall inside this table.
934 */
935 for (i = 1; i <= n; i++) {
936 unsigned int val = (p[i]->sval & Wmax) >> lowbit;
937 if (val == j && state[i] == labval) {
938 found = 1;
939 state[i] = lab;
940 }
941 }
942
943 /* couldn't find any labels? goto the default label */
944 if (!found)
945 lab = p[0]->slab;
946
947 /* generate the table entry */
948 char *entry = tmpalloc(20);
949 snprintf(entry, 20, "\t.long " LABFMT "-" LABFMT "\n", lab, tbllabel);
950 send_passt(IP_ASM, entry);
951
952 DPRINTF(("mrst_put_entry: table=%d, pos=%lu/%lu, label=%d\n",
953 tbllabel, j, tblsize, lab));
954
955 /* go to the next table entry */
956 if (j+1 < tblsize) {
957 mrst_put_entry_and_recurse(num, p, n, state, tbllabel, labval,
958 j+1, tblsize, Wmax, lowbit);
959 }
960
961 /* if we are going to the default label, bail now */
962 if (!found)
963 return;
964
965 #ifdef PCC_DEBUG
966 if (xdebug) {
967 printf("state: ");
968 for (i = 1; i <= n; i++)
969 printf("%d ", state[i]);
970 printf("\n");
971 }
972 #endif
973
974 /* build another table */
975 mrst_rec(num, p, n, state, lab);
976 }
977
978 /*
979 * counts the number of entries in a table of size (1 << L) which would
980 * be used given the cases and the mask (W, lowbit).
981 */
982 static unsigned int
983 mrst_cardinality(struct swents **p, int n, int *state, int step, unsigned long W, int L, int lowbit)
984 {
985 unsigned int count = 0;
986 int i;
987
988 if (W == 0)
989 return 0;
990
991 int *vals = (int *)calloc(1 << L, sizeof(int));
992 assert(vals);
993
994 DPRINTF(("mrst_cardinality: "));
995 for (i = 1; i <= n; i++) {
996 int idx;
997 if (state[i] != step)
998 continue;
999 idx = (p[i]->sval & W) >> lowbit;
1000 DPRINTF(("%llu->%d, ", p[i]->sval, idx));
1001 if (!vals[idx]) {
1002 count++;
1003 }
1004 vals[idx] = 1;
1005 }
1006 DPRINTF((": found %d entries\n", count));
1007 free(vals);
1008
1009 return count;
1010 }
1011
1012 /*
1013 * Find the maximum window (table size) which would best cover
1014 * the set of labels. Algorithm explained in:
1015 *
1016 * Ulfar Erlingsson, Mukkai Krishnamoorthy and T.V. Raman.
1017 * Efficient Multiway Radix Search Trees.
1018 * Information Processing Letters 60:3 115-120 (November 1996)
1019 */
1020
1021 static unsigned long
1022 mrst_find_window(struct swents **p, int n, int *state, int lab, int *len, int *lowbit)
1023 {
1024 unsigned int tblsize;
1025 unsigned long W = 0;
1026 unsigned long Wmax = 0;
1027 unsigned long Wleft = (1 << (SZLONG-1));
1028 unsigned int C = 0;
1029 unsigned int Cmax = 0;
1030 int L = 0;
1031 int Lmax = 0;
1032 int lowmax = 0;
1033 int no_b = SZLONG-1;
1034 unsigned long b = (1 << (SZLONG-1));
1035
1036 DPRINTF(("mrst_find_window: n=%d, lab=%d\n", n, lab));
1037
1038 for (; b > 0; b >>= 1, no_b--) {
1039
1040 // select the next bit
1041 W |= b;
1042 L += 1;
1043
1044 tblsize = 1 << L;
1045 assert(tblsize > 0);
1046
1047 DPRINTF(("no_b=%d, b=0x%lx, Wleft=0x%lx, W=0x%lx, Wmax=0x%lx, L=%d, Lmax=%d, Cmax=%u, lowmax=%d, tblsize=%u\n", no_b, b, Wleft, W, Wmax, L, Lmax, Cmax, lowmax, tblsize));
1048
1049 C = mrst_cardinality(p, n, state, lab, W, L, no_b);
1050 DPRINTF((" -> cardinality is %d\n", C));
1051
1052 if (2*C >= tblsize) {
1053 DPRINTF(("(found good match, keep adding to table)\n"));
1054 Wmax = W;
1055 Lmax = L;
1056 lowmax = no_b;
1057 Cmax = C;
1058 } else {
1059 DPRINTF(("(too sparse)\n"));
1060 assert((W & Wleft) != 0);
1061
1062 /* flip the MSB and see if we get a better match */
1063 W ^= Wleft;
1064 Wleft >>= 1;
1065 L -= 1;
1066
1067 DPRINTF((" --> trying W=0x%lx and L=%d and Cmax=%u\n", W, L, Cmax));
1068 C = mrst_cardinality(p, n, state, lab, W, L, no_b);
1069 DPRINTF((" --> C=%u\n", C));
1070 if (C > Cmax) {
1071 Wmax = W;
1072 Lmax = L;
1073 lowmax = no_b;
1074 Cmax = C;
1075 DPRINTF((" --> better!\n"));
1076 } else {
1077 DPRINTF((" --> no better\n"));
1078 }
1079 }
1080
1081 }
1082
1083 #ifdef PCC_DEBUG
1084 if (xdebug) {
1085 int i;
1086 int hibit = lowmax + Lmax;
1087 printf("msrt_find_window: Wmax=0x%lx, lowbit=%d, result=", Wmax, lowmax);
1088 for (i = 31; i >= 0; i--) {
1089 int mask = (1 << i);
1090 if (i == hibit)
1091 printf("[");
1092 if (Wmax & mask)
1093 printf("1");
1094 else
1095 printf("0");
1096 if (i == lowmax)
1097 printf("]");
1098 }
1099 printf("\n");
1100 }
1101 #endif
1102
1103 assert(Lmax > 0);
1104 *len = Lmax;
1105 *lowbit = lowmax;
1106
1107 DPRINTF(("msrt_find_window: returning Wmax=%lu, len=%d, lowbit=%d [tblsize=%u, entries=%u]\n", Wmax, Lmax, lowmax, tblsize, C));
1108
1109 return Wmax;
1110 }
1111 #endif
1112
1113 /*
1114 * Straighten a chain of CM ops so that the CM nodes
1115 * only appear on the left node.
1116 *
1117 * CM CM
1118 * CM CM CM b
1119 * x y a b CM a
1120 * x y
1121 *
1122 * CM CM
1123 * CM CM CM c
1124 * CM z CM c CM b
1125 * x y a b CM a
1126 * CM z
1127 * x y
1128 */
1129 static NODE *
1130 straighten(NODE *p)
1131 {
1132 NODE *r = p->n_right;
1133
1134 if (p->n_op != CM || r->n_op != CM)
1135 return p;
1136
1137 p->n_right = r->n_left;
1138 r->n_left = straighten(p);
1139
1140 return r;
1141 }
1142
1143 static NODE *
1144 reverse1(NODE *p, NODE *a)
1145 {
1146 NODE *l = p->n_left;
1147 NODE *r = p->n_right;
1148
1149 a->n_right = r;
1150 p->n_left = a;
1151
1152 if (l->n_op == CM) {
1153 return reverse1(l, p);
1154 } else {
1155 p->n_right = l;
1156 return p;
1157 }
1158 }
1159
1160 /*
1161 * Reverse a chain of CM ops
1162 */
1163 static NODE *
1164 reverse(NODE *p)
1165 {
1166 NODE *l = p->n_left;
1167 NODE *r = p->n_right;
1168
1169 p->n_left = r;
1170
1171 if (l->n_op == CM)
1172 return reverse1(l, p);
1173
1174 p->n_right = l;
1175
1176 return p;
1177 }
1178
1179 /* push arg onto the stack */
1180 /* called by moveargs() */
1181 static NODE *
1182 pusharg(NODE *p, int *regp)
1183 {
1184 NODE *q;
1185 int sz;
1186 int off;
1187
1188 /* convert to register size, if smaller */
1189 sz = tsize(p->n_type, p->n_df, p->n_ap);
1190 if (sz < SZINT)
1191 p = block(SCONV, p, NIL, INT, 0, 0);
1192
1193 q = block(REG, NIL, NIL, INCREF(p->n_type), p->n_df, p->n_ap);
1194 regno(q) = SPREG;
1195
1196 off = ARGINIT/SZCHAR + 4 * (*regp - R3);
1197 q = block(PLUS, q, bcon(off), INT, 0, 0);
1198 q = block(UMUL, q, NIL, p->n_type, p->n_df, p->n_ap);
1199 (*regp) += szty(p->n_type);
1200
1201 return buildtree(ASSIGN, q, p);
1202 }
1203
1204 /* setup call stack with 32-bit argument */
1205 /* called from moveargs() */
1206 static NODE *
1207 movearg_32bit(NODE *p, int *regp)
1208 {
1209 int reg = *regp;
1210 NODE *q;
1211
1212 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
1213 regno(q) = reg++;
1214 q = buildtree(ASSIGN, q, p);
1215
1216 *regp = reg;
1217 return q;
1218 }
1219
1220 /* setup call stack with 64-bit argument */
1221 /* called from moveargs() */
1222 static NODE *
1223 movearg_64bit(NODE *p, int *regp)
1224 {
1225 int reg = *regp;
1226 NODE *q, *r;
1227
1228 #if ALLONGLONG == 64
1229 /* alignment */
1230 ++reg;
1231 reg &= ~1;
1232 #endif
1233
1234 if (reg > R10) {
1235 *regp = reg;
1236 q = pusharg(p, regp);
1237 } else if (reg == R10) {
1238 /* half in and half out of the registers */
1239 r = p1tcopy(p);
1240 if (!features(FEATURE_BIGENDIAN)) {
1241 q = block(SCONV, p, NIL, INT, 0, 0);
1242 q = movearg_32bit(q, regp); /* little-endian */
1243 r = buildtree(RS, r, bcon(32));
1244 r = block(SCONV, r, NIL, INT, 0, 0);
1245 r = pusharg(r, regp); /* little-endian */
1246 } else {
1247 q = buildtree(RS, p, bcon(32));
1248 q = block(SCONV, q, NIL, INT, 0, 0);
1249 q = movearg_32bit(q, regp); /* big-endian */
1250 r = block(SCONV, r, NIL, INT, 0, 0);
1251 r = pusharg(r, regp); /* big-endian */
1252 }
1253 q = straighten(block(CM, q, r, p->n_type, p->n_df, p->n_ap));
1254 } else {
1255 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
1256 regno(q) = R3R4 + (reg - R3);
1257 q = buildtree(ASSIGN, q, p);
1258 *regp = reg + 2;
1259 }
1260
1261 return q;
1262 }
1263
1264 /* setup call stack with float argument */
1265 /* called from moveargs() */
1266 static NODE *
1267 movearg_float(NODE *p, int *fregp, int *regp)
1268 {
1269 #if defined(MACHOABI)
1270 NODE *q, *r;
1271 TWORD ty = INCREF(p->n_type);
1272 int tmpnr;
1273 #endif
1274
1275 p = movearg_32bit(p, fregp);
1276
1277 /*
1278 * On OS/X, floats are passed in the floating-point registers
1279 * and in the general registers for compatibily with libraries
1280 * compiled to handle soft-float.
1281 */
1282
1283 #if defined(MACHOABI)
1284
1285 if (xtemps) {
1286 /* bounce into TOS */
1287 r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap);
1288 regno(r) = SPREG;
1289 r = block(PLUS, r, bcon(-4), INT, 0, 0);
1290 r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap);
1291 r = buildtree(ASSIGN, r, p);
1292 ecomp(r);
1293
1294 /* bounce into temp */
1295 r = block(REG, NIL, NIL, PTR+INT, 0, 0);
1296 regno(r) = SPREG;
1297 r = block(PLUS, r, bcon(-4), INT, 0, 0);
1298 r = block(UMUL, r, NIL, INT, 0, 0);
1299 q = tempnode(0, INT, 0, 0);
1300 tmpnr = regno(q);
1301 r = buildtree(ASSIGN, q, r);
1302 ecomp(r);
1303 } else {
1304 /* copy directly into temp */
1305 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
1306 tmpnr = regno(q);
1307 r = buildtree(ASSIGN, q, p);
1308 ecomp(r);
1309 }
1310
1311 /* copy from temp to register parameter */
1312 r = tempnode(tmpnr, INT, 0, 0);
1313 q = block(REG, NIL, NIL, INT, 0, 0);
1314 regno(q) = (*regp)++;
1315 p = buildtree(ASSIGN, q, r);
1316
1317 #endif
1318 return p;
1319
1320 }
1321
1322 /* setup call stack with float/double argument */
1323 /* called from moveargs() */
1324 static NODE *
1325 movearg_double(NODE *p, int *fregp, int *regp)
1326 {
1327 #if defined(MACHOABI)
1328 NODE *q, *r;
1329 TWORD ty = INCREF(p->n_type);
1330 int tmpnr;
1331 #endif
1332
1333 /* this does the move to a single register for us */
1334 p = movearg_32bit(p, fregp);
1335
1336 /*
1337 * On OS/X, doubles are passed in the floating-point registers
1338 * and in the general registers for compatibily with libraries
1339 * compiled to handle soft-float.
1340 */
1341
1342 #if defined(MACHOABI)
1343
1344 if (xtemps) {
1345 /* bounce on TOS */
1346 r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap);
1347 regno(r) = SPREG;
1348 r = block(PLUS, r, bcon(-8), ty, p->n_df, p->n_ap);
1349 r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap);
1350 r = buildtree(ASSIGN, r, p);
1351 ecomp(r);
1352
1353 /* bounce into temp */
1354 r = block(REG, NIL, NIL, PTR+LONGLONG, 0, 0);
1355 regno(r) = SPREG;
1356 r = block(PLUS, r, bcon(-8), PTR+LONGLONG, 0, 0);
1357 r = block(UMUL, r, NIL, LONGLONG, 0, 0);
1358 q = tempnode(0, LONGLONG, 0, 0);
1359 tmpnr = regno(q);
1360 r = buildtree(ASSIGN, q, r);
1361 ecomp(r);
1362 } else {
1363 /* copy directly into temp */
1364 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
1365 tmpnr = regno(q);
1366 r = buildtree(ASSIGN, q, p);
1367 ecomp(r);
1368 }
1369
1370 /* copy from temp to register parameter */
1371 r = tempnode(tmpnr, LONGLONG, 0, 0);
1372 q = block(REG, NIL, NIL, LONGLONG, 0, 0);
1373 regno(q) = R3R4 - R3 + (*regp);
1374 p = buildtree(ASSIGN, q, r);
1375
1376 (*regp) += 2;
1377
1378 #endif
1379
1380 return p;
1381 }
1382
1383 /* setup call stack with a structure */
1384 /* called from moveargs() */
1385 static NODE *
1386 movearg_struct(NODE *p, int *regp)
1387 {
1388 int reg = *regp;
1389 NODE *l, *q, *t, *r;
1390 int tmpnr;
1391 int navail;
1392 int num;
1393 int sz;
1394 int ty;
1395 int i;
1396
1397 assert(p->n_op == STARG);
1398
1399 navail = NARGREGS - (reg - R3);
1400 navail = navail < 0 ? 0 : navail;
1401 sz = tsize(p->n_type, p->n_df, p->n_ap) / SZINT;
1402 num = sz > navail ? navail : sz;
1403
1404 /* remove STARG node */
1405 l = p->n_left;
1406 p1nfree(p);
1407 ty = l->n_type;
1408
1409 /*
1410 * put it into a TEMP, rather than tcopy(), since the tree
1411 * in p may have side-affects
1412 */
1413 t = tempnode(0, ty, l->n_df, l->n_ap);
1414 tmpnr = regno(t);
1415 q = buildtree(ASSIGN, t, l);
1416
1417 /* copy structure into registers */
1418 for (i = 0; i < num; i++) {
1419 t = tempnode(tmpnr, ty, 0, 0);
1420 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
1421 t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0);
1422 t = buildtree(UMUL, t, NIL);
1423
1424 r = block(REG, NIL, NIL, INT, 0, 0);
1425 regno(r) = reg++;
1426 r = buildtree(ASSIGN, r, t);
1427
1428 q = block(CM, q, r, INT, 0, 0);
1429 }
1430
1431 /* put the rest of the structure on the stack */
1432 for (i = num; i < sz; i++) {
1433 t = tempnode(tmpnr, ty, 0, 0);
1434 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
1435 t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0);
1436 t = buildtree(UMUL, t, NIL);
1437 r = pusharg(t, &reg);
1438 q = block(CM, q, r, INT, 0, 0);
1439 }
1440
1441 q = reverse(q);
1442
1443 *regp = reg;
1444 return q;
1445 }
1446
1447
1448 static NODE *
1449 moveargs(NODE *p, int *regp, int *fregp)
1450 {
1451 NODE *r, **rp;
1452 int reg, freg;
1453
1454 if (p->n_op == CM) {
1455 p->n_left = moveargs(p->n_left, regp, fregp);
1456 r = p->n_right;
1457 rp = &p->n_right;
1458 } else {
1459 r = p;
1460 rp = &p;
1461 }
1462
1463 reg = *regp;
1464 freg = *fregp;
1465
1466 #define ISFLOAT(p) (p->n_type == FLOAT || \
1467 p->n_type == DOUBLE || \
1468 p->n_type == LDOUBLE)
1469
1470 if (reg > R10 && r->n_op != STARG) {
1471 *rp = pusharg(r, regp);
1472 } else if (r->n_op == STARG) {
1473 *rp = movearg_struct(r, regp);
1474 } else if (DEUNSIGN(r->n_type) == LONGLONG) {
1475 *rp = movearg_64bit(r, regp);
1476 } else if (r->n_type == DOUBLE || r->n_type == LDOUBLE) {
1477 if (features(FEATURE_HARDFLOAT))
1478 *rp = movearg_double(r, fregp, regp);
1479 else
1480 *rp = movearg_64bit(r, regp);
1481 } else if (r->n_type == FLOAT) {
1482 if (features(FEATURE_HARDFLOAT))
1483 *rp = movearg_float(r, fregp, regp);
1484 else
1485 *rp = movearg_32bit(r, regp);
1486 } else {
1487 *rp = movearg_32bit(r, regp);
1488 }
1489
1490 return straighten(p);
1491 }
1492
1493 /*
1494 * Fixup arguments to pass pointer-to-struct as first argument.
1495 *
1496 * called from funcode().
1497 */
1498 static NODE *
1499 retstruct(NODE *p)
1500 {
1501 struct symtab s;
1502 NODE *l, *r, *t, *q;
1503 TWORD ty;
1504
1505 l = p->n_left;
1506 r = p->n_right;
1507
1508 ty = DECREF(l->n_type) - FTN;
1509
1510 s.sclass = AUTO;
1511 s.stype = ty;
1512 s.sdf = l->n_df;
1513 s.sap = l->n_ap;
1514 oalloc(&s, &autooff);
1515 q = block(REG, NIL, NIL, INCREF(ty), l->n_df, l->n_ap);
1516 regno(q) = FPREG;
1517 q = block(MINUS, q, bcon(autooff/SZCHAR), INCREF(ty),
1518 l->n_df, l->n_ap);
1519
1520 /* insert hidden assignment at beginning of list */
1521 if (r->n_op != CM) {
1522 p->n_right = block(CM, q, r, INCREF(ty), l->n_df, l->n_ap);
1523 } else {
1524 for (t = r; t->n_left->n_op == CM; t = t->n_left)
1525 ;
1526 t->n_left = block(CM, q, t->n_left, INCREF(ty),
1527 l->n_df, l->n_ap);
1528 }
1529
1530 return p;
1531 }
1532
1533 /*
1534 * Called with a function call with arguments as argument.
1535 * This is done early in buildtree() and only done once.
1536 */
1537 NODE *
1538 funcode(NODE *p)
1539 {
1540 int regnum = R3;
1541 int fregnum = F1;
1542
1543 if (DECREF(p->n_left->n_type) == STRTY+FTN ||
1544 DECREF(p->n_left->n_type) == UNIONTY+FTN)
1545 p = retstruct(p);
1546
1547 p->n_right = moveargs(p->n_right, &regnum, &fregnum);
1548
1549 if (p->n_right == NULL)
1550 p->n_op += (UCALL - CALL);
1551
1552 return p;
1553 }