Debian Codegraph @

arch/i86/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.4 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27
28 #include "pass1.h"
29
30 /* this file contains code which is dependent on the target machine */
31
32 #ifdef LANG_CXX
33 #define P1ND NODE
34 #define p1alloc talloc
35 #define p1tfree tfree
36 #define p1nfree nfree
37 #define p1listf listf
38 #define p1fwalk fwalk
39 #endif
40
41 #ifdef notyet
42 /*
43 * Check if a constant is too large for a type.
44 */
45 static int
46 toolarge(TWORD t, CONSZ con)
47 {
48 U_CONSZ ucon = con;
49
50 switch (t) {
51 case ULONGLONG:
52 case LONGLONG:
53 break; /* cannot be too large */
54 #define SCHK(i) case i: if (con > MAX_##i || con < MIN_##i) return 1; break
55 #define UCHK(i) case i: if (ucon > MAX_##i) return 1; break
56 SCHK(INT);
57 SCHK(SHORT);
58 case BOOL:
59 SCHK(CHAR);
60 UCHK(UNSIGNED);
61 UCHK(USHORT);
62 UCHK(UCHAR);
63 default:
64 cerror("toolarge");
65 }
66 return 0;
67 }
68 #endif
69
70 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
71
72 static char *
73 getsoname(struct symtab *sp)
74 {
75 struct attr *ap;
76 return (ap = attr_find(sp->sap, ATTR_SONAME)) ?
77 ap->sarg(0) : sp->sname;
78
79 }
80
81 int gotnr; /* tempnum for GOT register */
82 int argstacksize;
83
84 /* clocal() is called to do local transformations on
85 * an expression tree preparitory to its being
86 * written out in intermediate code.
87 *
88 * the major essential job is rewriting the
89 * automatic variables and arguments in terms of
90 * REG and OREG nodes
91 * conversion ops which are not necessary are also clobbered here
92 * in addition, any special features (such as rewriting
93 * exclusive or) are easily handled here as well
94 */
95 P1ND *
96 clocal(P1ND *p)
97 {
98
99 struct attr *ap;
100 register struct symtab *q;
101 register P1ND *r, *l;
102 register int o;
103 register int m;
104
105 #ifdef PCC_DEBUG
106 if (xdebug) {
107 printf("clocal: %p\n", p);
108 p1fwalk(p, eprint, 0);
109 }
110 #endif
111 switch( o = p->n_op ){
112
113 case NAME:
114 if ((q = p->n_sp) == NULL)
115 return p; /* Nothing to care about */
116
117 switch (q->sclass) {
118
119 case PARAM:
120 case AUTO:
121 /* fake up a structure reference */
122 r = block(REG, NULL, NULL, PTR+STRTY, 0, 0);
123 slval(r, 0);
124 r->n_rval = FPREG;
125 p = stref(block(STREF, r, p, 0, 0, 0));
126 break;
127
128 case USTATIC:
129 break;
130 case STATIC:
131 break;
132
133 case REGISTER:
134 p->n_op = REG;
135 slval(p, 0);
136 p->n_rval = q->soffset;
137 break;
138
139 case EXTERN:
140 case EXTDEF:
141 break;
142 }
143 break;
144
145 case ADDROF:
146 break;
147
148 case UCALL:
149 break;
150
151 case USTCALL:
152 /* Add hidden arg0 */
153 r = block(REG, NULL, NULL, INCREF(VOID), 0, 0);
154 regno(r) = BP;
155 if ((ap = attr_find(p->n_ap, GCC_ATYP_REGPARM)) != NULL &&
156 ap->iarg(0) > 0) {
157 l = block(REG, NULL, NULL, INCREF(VOID), 0, 0);
158 regno(l) = AX;
159 p->n_right = buildtree(ASSIGN, l, r);
160 } else
161 p->n_right = block(FUNARG, r, NULL, INCREF(VOID), 0, 0);
162 p->n_op -= (UCALL-CALL);
163
164 if (kflag == 0)
165 break;
166 l = block(REG, NULL, NULL, INT, 0, 0);
167 regno(l) = BX;
168 r = buildtree(ASSIGN, l, tempnode(gotnr, INT, 0, 0));
169 p->n_right = block(CM, r, p->n_right, INT, 0, 0);
170 break;
171
172 #ifdef notyet
173 /* XXX breaks sometimes */
174 case CBRANCH:
175 l = p->n_left;
176
177 /*
178 * Remove unnecessary conversion ops.
179 */
180 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
181 break;
182 if (coptype(l->n_op) != BITYPE)
183 break;
184 if (l->n_right->n_op != ICON)
185 break;
186 r = l->n_left->n_left;
187 if (r->n_type >= FLOAT)
188 break;
189 if (toolarge(r->n_type, glval(l->n_right)))
190 break;
191 l->n_right->n_type = r->n_type;
192 if (l->n_op >= ULE && l->n_op <= UGT)
193 l->n_op -= (UGT-ULE);
194 p->n_left = buildtree(l->n_op, r, l->n_right);
195 p1nfree(l->n_left);
196 p1nfree(l);
197 break;
198 #endif
199
200 case PCONV:
201 l = p->n_left;
202
203 /* Make int type before pointer */
204 if (l->n_type < INT || l->n_type == LONGLONG ||
205 l->n_type == ULONGLONG || l->n_type == BOOL) {
206 /* float etc? */
207 p->n_left = block(SCONV, l, NULL, UNSIGNED, 0, 0);
208 }
209 break;
210
211 case SCONV:
212 if (p->n_left->n_op == COMOP)
213 break; /* may propagate wrong type later */
214 l = p->n_left;
215
216 if (p->n_type == l->n_type) {
217 p1nfree(p);
218 return l;
219 }
220
221 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
222 tsize(p->n_type, p->n_df, p->n_ap) ==
223 tsize(l->n_type, l->n_df, l->n_ap)) {
224 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
225 l->n_type != FLOAT && l->n_type != DOUBLE &&
226 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
227 if (l->n_op == NAME || l->n_op == UMUL ||
228 l->n_op == TEMP) {
229 l->n_type = p->n_type;
230 p1nfree(p);
231 return l;
232 }
233 }
234 }
235
236 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
237 coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
238 l->n_op != QUEST) {
239 l->n_type = p->n_type;
240 p1nfree(p);
241 return l;
242 }
243
244 o = l->n_op;
245 m = p->n_type;
246
247 if (o == ICON) {
248 /*
249 * Can only end up here if o is an address,
250 * and in that case the only compile-time conversion
251 * possible is to int.
252 */
253 if ((TMASK & l->n_type) == 0 && l->n_sp == NULL)
254 cerror("SCONV ICON");
255 if (l->n_sp == 0) {
256 p->n_type = UNSIGNED;
257 concast(l, m);
258 } else if (m != INT && m != UNSIGNED)
259 break;
260 l->n_type = m;
261 l->n_ap = 0;
262 p1nfree(p);
263 return l;
264 } else if (l->n_op == FCON)
265 cerror("SCONV FCON");
266 if ((p->n_type == CHAR || p->n_type == UCHAR ||
267 p->n_type == SHORT || p->n_type == USHORT) &&
268 (l->n_type == FLOAT || l->n_type == DOUBLE ||
269 l->n_type == LDOUBLE)) {
270 p = block(SCONV, p, NULL, p->n_type, p->n_df, p->n_ap);
271 p->n_left->n_type = INT;
272 return p;
273 }
274 break;
275
276 case MOD:
277 case DIV:
278 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
279 break;
280 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
281 break;
282 /* make it an int division by inserting conversions */
283 p->n_left = makety(p->n_left, INT, 0, 0, 0);
284 p->n_right = makety(p->n_right, INT, 0, 0, 0);
285 o = p->n_type;
286 p->n_type = INT;
287 p = makety(p, o, 0, 0, 0);
288 break;
289
290 case FORCE:
291 /* put return value in return reg */
292 p->n_op = ASSIGN;
293 p->n_right = p->n_left;
294 p->n_left = block(REG, NULL, NULL, p->n_type, 0, 0);
295 p->n_left->n_rval = p->n_left->n_type == BOOL ?
296 RETREG(CHAR) : RETREG(p->n_type);
297 break;
298
299 case LS:
300 case RS:
301 /* shift count must be in a char */
302 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
303 break;
304 p->n_right = block(SCONV, p->n_right, NULL, CHAR, 0, 0);
305 break;
306 }
307 #ifdef PCC_DEBUG
308 if (xdebug) {
309 printf("clocal end: %p\n", p);
310 p1fwalk(p, eprint, 0);
311 }
312 #endif
313 return(p);
314 }
315
316 static void mangle(P1ND *p);
317
318 void
319 myp2tree(P1ND *p)
320 {
321 struct symtab *sp;
322
323 mangle(p);
324
325 if (p->n_op != FCON)
326 return;
327
328 sp = IALLOC(sizeof(struct symtab));
329 sp->sclass = STATIC;
330 sp->sap = 0;
331 sp->slevel = 1; /* fake numeric label */
332 sp->soffset = getlab();
333 sp->sflags = 0;
334 sp->stype = p->n_type;
335 sp->squal = (CON >> TSHIFT);
336 sp->sname = NULL;
337
338 locctr(DATA, sp);
339 defloc(sp);
340 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
341
342 p->n_op = NAME;
343 slval(p, 0);
344 p->n_sp = sp;
345 }
346
347 /*ARGSUSED*/
348 int
349 andable(P1ND *p)
350 {
351 return(1); /* all names can have & taken on them */
352 }
353
354 /*
355 * Return 1 if a variable of type type is OK to put in register.
356 */
357 int
358 cisreg(TWORD t)
359 {
360 if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
361 return 0; /* not yet */
362 return 1;
363 }
364
365 /*
366 * Allocate off bits on the stack. p is a tree that when evaluated
367 * is the multiply count for off, t is a storeable node where to write
368 * the allocated address.
369 */
370 void
371 spalloc(P1ND *t, P1ND *p, OFFSZ off)
372 {
373 P1ND *sp;
374
375 p = buildtree(MUL, p, bcon(off/SZCHAR)); /* XXX word alignment? */
376
377 /* sub the size from sp */
378 sp = block(REG, NULL, NULL, p->n_type, 0, 0);
379 slval(sp, 0);
380 sp->n_rval = STKREG;
381 ecomp(buildtree(MINUSEQ, sp, p));
382
383 /* save the address of sp */
384 sp = block(REG, NULL, NULL, PTR+INT, t->n_df, t->n_ap);
385 slval(sp, 0);
386 sp->n_rval = STKREG;
387 t->n_type = sp->n_type;
388 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
389
390 }
391
392 /*
393 * print out a constant node, may be associated with a label.
394 * Do not free the node after use.
395 * off is bit offset from the beginning of the aggregate
396 * fsz is the number of bits this is referring to
397 */
398 int
399 ninval(CONSZ off, int fsz, P1ND *p)
400 {
401 union { float f; double d; long double l; int i[3]; } u;
402 int i;
403
404 switch (p->n_type) {
405 case LONGLONG:
406 case ULONGLONG:
407 i = (int)(glval(p) >> 32);
408 slval(p, glval(p) & 0xffffffff);
409 p->n_type = INT;
410 inval(off, 32, p);
411 slval(p, i);
412 inval(off+32, 32, p);
413 break;
414 case LDOUBLE:
415 u.i[2] = 0;
416 u.l = (long double)((FLT *)p->n_dcon)->fp;
417 #if defined(HOST_BIG_ENDIAN)
418 /* XXX probably broken on most hosts */
419 printf("\t.long\t0x%x,0x%x,0x%x\n", u.i[2], u.i[1], u.i[0]);
420 #else
421 printf("\t.long\t%d,%d,%d\n", u.i[0], u.i[1], u.i[2] & 0177777);
422 #endif
423 break;
424 case DOUBLE:
425 u.d = (double)((FLT *)p->n_dcon)->fp;
426 #if defined(HOST_BIG_ENDIAN)
427 printf("\t.long\t0x%x,0x%x\n", u.i[1], u.i[0]);
428 #else
429 printf("\t.long\t%d,%d\n", u.i[0], u.i[1]);
430 #endif
431 break;
432 case FLOAT:
433 u.f = (float)((FLT *)p->n_dcon)->fp;
434 printf("\t.long\t%d\n", u.i[0]);
435 break;
436 default:
437 return 0;
438 }
439 return 1;
440 }
441
442 /* make a name look like an external name in the local machine */
443 char *
444 exname(char *p)
445 {
446 #define NCHNAM 256
447 static char text[NCHNAM+1];
448 int i;
449
450 if (p == NULL)
451 return "";
452
453 text[0] = '_';
454 for (i=1; *p && i<NCHNAM; ++i)
455 text[i] = *p++;
456
457 text[i] = '\0';
458 text[NCHNAM] = '\0'; /* truncate */
459
460 return (text);
461 }
462
463 /*
464 * map types which are not defined on the local machine
465 *
466 * For 8086 we map short/ushort onto int/unsigned int which
467 * removes some of the table stuff later on
468 */
469 TWORD
470 ctype(TWORD type)
471 {
472 switch (BTYPE(type)) {
473 case SHORT:
474 MODTYPE(type,INT);
475 break;
476
477 case USHORT:
478 MODTYPE(type,UNSIGNED);
479
480 }
481 return (type);
482 }
483
484 void
485 calldec(P1ND *p, P1ND *q)
486 {
487 }
488
489 void
490 extdec(struct symtab *q)
491 {
492 }
493
494 /* make a common declaration for id, if reasonable */
495 void
496 defzero(struct symtab *sp)
497 {
498 int off;
499 int al;
500 char *name;
501
502 name = getexname(sp);
503 al = talign(sp->stype, sp->sap)/SZCHAR;
504 off = (int)tsize(sp->stype, sp->sdf, sp->sap);
505 SETOFF(off,SZCHAR);
506 off /= SZCHAR;
507 printf("\t.align %d\n", al);
508 printf("%s:\t.blkb %d\n", name, off);
509 }
510
511 static int stdcall;
512 static char *alias;
513 static int constructor;
514 static int destructor;
515
516 /*
517 * Give target the opportunity of handling pragmas.
518 */
519 int
520 mypragma(char *str)
521 {
522 char *a2 = pragtok(NULL);
523
524 if (strcmp(str, "stdcall") == 0) {
525 stdcall = 1;
526 return 1;
527 }
528 if (strcmp(str, "cdecl") == 0) {
529 stdcall = 0;
530 return 1;
531 }
532 #ifndef AOUTABI
533 if (strcmp(str, "constructor") == 0 || strcmp(str, "init") == 0) {
534 constructor = 1;
535 return 1;
536 }
537 if (strcmp(str, "destructor") == 0 || strcmp(str, "fini") == 0) {
538 destructor = 1;
539 return 1;
540 }
541 #endif
542 if (strcmp(str, "alias") == 0 && a2 != NULL) {
543 alias = tmpstrdup(a2);
544 return 1;
545 }
546
547 return 0;
548 }
549
550 /*
551 * Called when a identifier has been declared.
552 */
553 void
554 fixdef(struct symtab *sp)
555 {
556 #ifdef GCC_COMPAT
557 struct attr *ap;
558 #endif
559 #ifdef GCC_COMPAT
560 #ifdef HAVE_WEAKREF
561 /* not many as'es have this directive */
562 if ((ap = attr_find(sp->sap, GCC_ATYP_WEAKREF)) != NULL) {
563 char *wr = ap->sarg(0);
564 char *sn = getsoname(sp);
565 if (sp->sclass != STATIC && sp->sclass != USTATIC)
566 uerror("weakref %s must be static", sp->sname);
567 if (wr == NULL) {
568 if ((ap = attr_find(sp->sap, GCC_ATYP_ALIAS))) {
569 wr = ap->sarg(0);
570 }
571 }
572 if (wr == NULL)
573 printf("\t.weak %s\n", sn);
574 else
575 printf("\t.weakref %s,%s\n", sn, wr);
576 } else
577 #endif
578 if ((ap = attr_find(sp->sap, GCC_ATYP_ALIAS)) != NULL) {
579 char *an = ap->sarg(0);
580 char *sn = getsoname(sp);
581 char *v;
582
583 v = attr_find(sp->sap, GCC_ATYP_WEAK) ? "weak" : "globl";
584 printf("\t.%s %s\n", v, sn);
585 printf("\t.set %s,%s\n", sn, an);
586 }
587 #endif
588 if (alias != NULL && (sp->sclass != PARAM)) {
589 char *name = getexname(sp);
590 printf("\t.globl %s\n", name);
591 printf("%s = ", name);
592 printf("%s\n", exname(alias));
593 alias = NULL;
594 }
595 if ((constructor || destructor) && (sp->sclass != PARAM)) {
596 printf("\t.section .%ctors,\"w\"\n",
597 constructor ? 'c' : 'd');
598 printf("\t.p2align 2\n");
599 printf("\t.long %s\n", exname(sp->sname));
600 printf("\t.previous\n");
601 constructor = destructor = 0;
602 }
603 #if 0
604 if (stdcall && (sp->sclass != PARAM)) {
605 sp->sflags |= SSTDCALL;
606 stdcall = 0;
607 }
608 #endif
609 }
610
611 /*
612 * Postfix external functions with the arguments size.
613 */
614 static void
615 mangle(P1ND *p)
616 {
617 P1ND *l;
618
619 if (p->n_op != CALL && p->n_op != STCALL &&
620 p->n_op != UCALL && p->n_op != USTCALL)
621 return;
622
623 l = p->n_left;
624 while (cdope(l->n_op) & CALLFLG)
625 l = l->n_left;
626 if (l->n_op == TEMP)
627 return;
628 if (l->n_op == ADDROF)
629 l = l->n_left;
630 if (l->n_sp == NULL)
631 return;
632 #ifdef GCC_COMPAT
633 #if 0
634 if (attr_find(l->n_sp->sap, GCC_ATYP_STDCALL) != NULL)
635 l->n_sp->sflags |= SSTDCALL;
636 #endif
637 #endif
638 }
639
640 void
641 pass1_lastchance(struct interpass *ip)
642 {
643 if (ip->type == IP_NODE &&
644 (ip->ip_node->n_op == CALL || ip->ip_node->n_op == UCALL) &&
645 ISFTY(ip->ip_node->n_type))
646 ip->ip_node->n_ap = attr_add(ip->ip_node->n_ap,
647 attr_new(ATTR_I86_FPPOP, 1));
648 if (ip->type == IP_EPILOG) {
649 struct interpass_prolog *ipp = (struct interpass_prolog *)ip;
650 ipp->ipp_argstacksize = argstacksize;
651 }
652 }