Debian Codegraph @

arch/m68k/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.17 2018/12/02 10:50:00 ragge Exp $ */
2 /*
3 * Copyright (c) 2014 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 #undef NIL
31 #define NIL NULL
32
33 #ifdef LANG_CXX
34 #define P1ND NODE
35 #define p1nfree nfree
36 #define p1fwalk fwalk
37 #define p1tcopy tcopy
38 #define p1alloc talloc
39 #else
40 #define NODE P1ND
41 #define nfree p1nfree
42 #define fwalk p1fwalk
43 #endif
44
45
46 /* this file contains code which is dependent on the target machine */
47
48 int gotnr;
49
50 /*
51 * Make a symtab entry for PIC use.
52 */
53 static struct symtab *
54 picsymtab(char *p, char *s, char *s2)
55 {
56 struct symtab *sp = permalloc(sizeof(struct symtab));
57 size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
58
59 sp->sname = permalloc(len);
60 strlcpy(sp->sname, p, len);
61 strlcat(sp->sname, s, len);
62 strlcat(sp->sname, s2, len);
63 sp->sap = attr_new(ATTR_SONAME, 1);
64 sp->sap->sarg(0) = sp->sname;
65 sp->sclass = EXTERN;
66 sp->sflags = sp->slevel = 0;
67 sp->stype = 0xdeadbeef;
68 return sp;
69 }
70
71 /*
72 * Create a reference for an extern variable.
73 */
74 static NODE *
75 picext(NODE *p)
76 {
77 NODE *q, *r;
78 struct symtab *sp;
79 char *name;
80
81 q = tempnode(gotnr, PTR|VOID, 0, 0);
82 name = getexname(p->n_sp);
83
84 #ifdef notdef
85 struct attr *ga;
86 if ((ga = attr_find(p->n_sp->sap, GCC_ATYP_VISIBILITY)) &&
87 strcmp(ga->sarg(0), "hidden") == 0) {
88 /* For hidden vars use GOTOFF */
89 sp = picsymtab("", name, "@GOTOFF");
90 r = xbcon(0, sp, INT);
91 q = buildtree(PLUS, q, r);
92 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
93 q->n_sp = p->n_sp; /* for init */
94 nfree(p);
95 return q;
96 }
97 #endif
98
99 sp = picsymtab("", name, "@GOT");
100 r = xbcon(0, sp, INT);
101 q = buildtree(PLUS, q, r);
102 q = block(UMUL, q, 0, PTR|VOID, 0, 0);
103 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
104 q->n_sp = p->n_sp; /* for init */
105 nfree(p);
106 return q;
107 }
108
109 static char *
110 getsoname(struct symtab *sp)
111 {
112 struct attr *ap;
113 return (ap = attr_find(sp->sap, ATTR_SONAME)) ?
114 ap->sarg(0) : sp->sname;
115
116 }
117
118
119 static NODE *
120 picstatic(NODE *p)
121 {
122 NODE *q, *r;
123 struct symtab *sp;
124
125 q = tempnode(gotnr, PTR|VOID, 0, 0);
126 if (p->n_sp->slevel > 0) {
127 char buf[32];
128 if ((p->n_sp->sflags & SMASK) == SSTRING)
129 p->n_sp->sflags |= SASG;
130 snprintf(buf, 32, LABFMT, (int)p->n_sp->soffset);
131 sp = picsymtab("", buf, "@GOT");
132 } else {
133 sp = picsymtab("", getsoname(p->n_sp), "@GOT");
134 }
135
136 sp->sclass = STATIC;
137 sp->stype = p->n_sp->stype;
138 r = xbcon(0, sp, INT);
139 q = buildtree(PLUS, q, r);
140 q = block(UMUL, q, 0, PTR|VOID, 0, 0);
141 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
142 q->n_sp = p->n_sp; /* for init */
143 nfree(p);
144 return q;
145 }
146
147 /* clocal() is called to do local transformations on
148 * an expression tree preparitory to its being
149 * written out in intermediate code.
150 *
151 * the major essential job is rewriting the
152 * automatic variables and arguments in terms of
153 * REG and OREG nodes
154 * conversion ops which are not necessary are also clobbered here
155 * in addition, any special features (such as rewriting
156 * exclusive or) are easily handled here as well
157 */
158 NODE *
159 clocal(NODE *p)
160 {
161
162 register struct symtab *q;
163 register NODE *r, *l;
164 register int o;
165 TWORD t;
166
167 #ifdef PCC_DEBUG
168 if (xdebug) {
169 printf("clocal: %p\n", p);
170 fwalk(p, eprint, 0);
171 }
172 #endif
173 switch( o = p->n_op ){
174
175 case NAME:
176 if ((q = p->n_sp) == NULL)
177 return p; /* Nothing to care about */
178
179 switch (q->sclass) {
180
181 case PARAM:
182 case AUTO:
183 /* fake up a structure reference */
184 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
185 slval(r, 0);
186 r->n_rval = FPREG;
187 p = stref(block(STREF, r, p, 0, 0, 0));
188 break;
189
190 case REGISTER:
191 p->n_op = REG;
192 slval(p, 0);
193 p->n_rval = q->soffset;
194 break;
195
196 case USTATIC:
197 case STATIC:
198 if (kflag == 0)
199 break;
200 if (blevel > 0 && !statinit)
201 p = picstatic(p);
202 break;
203
204 case EXTERN:
205 case EXTDEF:
206 if (kflag == 0)
207 break;
208 if (blevel > 0 && !statinit)
209 p = picext(p);
210 break;
211 }
212 break;
213
214 case ADDROF:
215 if (kflag == 0 || blevel == 0 || statinit)
216 break;
217 /* char arrays may end up here */
218 l = p->n_left;
219 if (l->n_op != NAME ||
220 (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
221 break;
222 l = p;
223 p = picstatic(p->n_left);
224 nfree(l);
225 if (p->n_op != UMUL)
226 cerror("ADDROF error");
227 l = p;
228 p = p->n_left;
229 nfree(l);
230 break;
231
232 case STASG: /* convert struct assignment to call memcpy */
233 l = p->n_left;
234 if (l->n_op == NAME && ISFTN(l->n_sp->stype))
235 break; /* struct return, do nothing */
236 /* first construct arg list */
237 p->n_left = buildtree(ADDROF, p->n_left, 0);
238 r = bcon(tsize(STRTY, p->n_df, p->n_ap)/SZCHAR);
239 p->n_left = buildtree(CM, p->n_left, p->n_right);
240 p->n_right = r;
241 p->n_op = CM;
242 p->n_type = INT;
243
244 r = block(NAME, NIL, NIL, INT, 0, 0);
245 r->n_sp = lookup(addname("memcpy"), SNORMAL);
246 if (r->n_sp->sclass == SNULL) {
247 r->n_sp->sclass = EXTERN;
248 r->n_sp->stype = INCREF(VOID+PTR)+(FTN-PTR);
249 }
250 r->n_type = r->n_sp->stype;
251 p = buildtree(CALL, r, p);
252 break;
253
254 case SCONV:
255 l = p->n_left;
256 if (l->n_op == ICON && ISPTR(l->n_type)) {
257 /* Do immediate cast here */
258 /* Should be common code */
259 q = l->n_sp;
260 l->n_sp = NULL;
261 l->n_type = UNSIGNED;
262 if (concast(l, p->n_type) == 0)
263 cerror("clocal");
264 p = nfree(p);
265 p->n_sp = q;
266 }
267 break;
268
269 case FORCE:
270 /* put return value in return reg */
271 p->n_op = ASSIGN;
272 p->n_right = p->n_left;
273 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
274 t = p->n_type;
275 if (ISITY(t))
276 t = t - (FIMAG-FLOAT);
277 p->n_left->n_rval = RETREG(t);
278 break;
279 }
280 #ifdef PCC_DEBUG
281 if (xdebug) {
282 printf("clocal end: %p\n", p);
283 fwalk(p, eprint, 0);
284 }
285 #endif
286 return(p);
287 }
288
289 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
290
291 void
292 myp2tree(NODE *p)
293 {
294 struct symtab *sp;
295 NODE *l;
296
297 if (cdope(p->n_op) & CALLFLG) {
298 if (p->n_left->n_op == ADDROF &&
299 p->n_left->n_left->n_op == NAME) {
300 p->n_left = nfree(p->n_left);
301 l = p->n_left;
302 l->n_op = ICON;
303 if (l->n_sp->sclass != STATIC &&
304 l->n_sp->sclass != USTATIC)
305 l->n_sp =
306 picsymtab(l->n_sp->sname, "@PLTPC", "");
307 }
308 }
309
310 if (p->n_op != FCON)
311 return;
312
313 sp = IALLOC(sizeof(struct symtab));
314 sp->sclass = STATIC;
315 sp->sap = 0;
316 sp->slevel = 1; /* fake numeric label */
317 sp->soffset = getlab();
318 sp->sflags = 0;
319 sp->stype = p->n_type;
320 sp->squal = (CON >> TSHIFT);
321 sp->sname = NULL;
322
323 locctr(DATA, sp);
324 defloc(sp);
325 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
326
327 p->n_op = NAME;
328 slval(p, 0);
329 p->n_sp = sp;
330
331 }
332
333 /*
334 * Convert ADDROF NAME to ICON?
335 */
336 int
337 andable(NODE *p)
338 {
339 #ifdef notdef
340 /* shared libraries cannot have direct referenced static syms */
341 if (p->n_sp->sclass == STATIC || p->n_sp->sclass == USTATIC)
342 return 1;
343 #endif
344 return 1;
345 }
346
347 /*
348 * Return 1 if a variable of type type is OK to put in register.
349 */
350 int
351 cisreg(TWORD t)
352 {
353 return 1;
354 }
355
356 /*
357 * Allocate off bits on the stack. p is a tree that when evaluated
358 * is the multiply count for off, t is a storeable node where to write
359 * the allocated address.
360 */
361 void
362 spalloc(NODE *t, NODE *p, OFFSZ off)
363 {
364 NODE *sp;
365
366 p = buildtree(MUL, p, bcon(off/SZCHAR));
367 p = buildtree(PLUS, p, bcon(30));
368 p = buildtree(AND, p, xbcon(-16, NULL, UNSIGNED));
369 p = cast(p, UNSIGNED, 0);
370
371 /* sub the size from sp */
372 sp = block(REG, NIL, NIL, UNSIGNED+PTR, 0, 0);
373 slval(sp, 0);
374 sp->n_rval = STKREG;
375 p = (buildtree(MINUSEQ, sp, p));
376 ecomp(p);
377
378 /* save the address of sp */
379 sp = block(REG, NIL, NIL, PTR+UNSIGNED, t->n_df, t->n_ap);
380 slval(sp, 0);
381 sp->n_rval = STKREG;
382 t->n_type = sp->n_type;
383 p = (buildtree(ASSIGN, t, sp)); /* Emit! */
384 ecomp(p);
385
386 }
387
388 /*
389 * print out a constant node, may be associated with a label.
390 * Do not free the node after use.
391 * off is bit offset from the beginning of the aggregate
392 * fsz is the number of bits this is referring to
393 */
394 int
395 ninval(CONSZ off, int fsz, NODE *p)
396 {
397 union { float f; double d; long double l; int i[3]; } u;
398
399 switch (p->n_type) {
400 case LONGLONG:
401 case ULONGLONG:
402 printf("\t.long\t0x%x\n", (int)(off >> 32) & 0xffffffff);
403 printf("\t.long\t0x%x\n", (int)(off) & 0xffffffff);
404 break;
405 default:
406 return 0;
407 }
408 return 1;
409 }
410
411 /* make a name look like an external name in the local machine */
412 char *
413 exname(char *p)
414 {
415 return (p == NULL ? "" : p);
416 }
417
418 /*
419 * map types which are not defined on the local machine
420 */
421 TWORD
422 ctype(TWORD type)
423 {
424 switch (BTYPE(type)) {
425 case LONG:
426 MODTYPE(type,INT);
427 break;
428
429 case ULONG:
430 MODTYPE(type,UNSIGNED);
431
432 }
433 return (type);
434 }
435
436 void
437 calldec(NODE *p, NODE *q)
438 {
439 }
440
441 void
442 extdec(struct symtab *q)
443 {
444 }
445
446 /* make a common declaration for id, if reasonable */
447 void
448 defzero(struct symtab *sp)
449 {
450 int off, al;
451 char *name;
452
453 name = getexname(sp);
454 off = tsize(sp->stype, sp->sdf, sp->sap);
455 SETOFF(off,SZCHAR);
456 off /= SZCHAR;
457 al = talign(sp->stype, sp->sap)/SZCHAR;
458
459 if (sp->sclass == STATIC) {
460 if (sp->slevel == 0) {
461 printf("\t.local %s\n", name);
462 } else
463 printf("\t.local " LABFMT "\n", sp->soffset);
464 }
465 if (sp->slevel == 0) {
466 printf("\t.comm %s,0%o,%d\n", name, off, al);
467 } else
468 printf("\t.comm " LABFMT ",0%o,%d\n", sp->soffset, off, al);
469 }
470
471 char *nextsect;
472 static char *alias;
473 static int constructor;
474 static int destructor;
475
476 /*
477 * Give target the opportunity of handling pragmas.
478 */
479 int
480 mypragma(char *str)
481 {
482 return 0;
483 }
484
485 /*
486 * Called when a identifier has been declared.
487 */
488 void
489 fixdef(struct symtab *sp)
490 {
491 struct attr *ga;
492
493 #ifdef HAVE_WEAKREF
494 /* not many as'es have this directive */
495 if ((ga = attr_find(sp->sap, GCC_ATYP_WEAKREF)) != NULL) {
496 char *wr = ga->sarg(0);
497 char *sn = getsoname(sp);
498 if (wr == NULL) {
499 if ((ga = attr_find(sp->sap, GCC_ATYP_ALIAS))) {
500 wr = ga->sarg(0);
501 }
502 }
503 if (wr == NULL)
504 printf("\t.weak %s\n", sn);
505 else
506 printf("\t.weakref %s,%s\n", sn, wr);
507 } else
508 if ((ga = attr_find(sp->sap, GCC_ATYP_ALIAS)) != NULL) {
509 char *an = ga->sarg(0);
510 char *sn = getsoname(sp);
511 char *v;
512
513 v = attr_find(sp->sap, GCC_ATYP_WEAK) ? "weak" : "globl";
514 printf("\t.%s %s\n", v, sn);
515 printf("\t.set %s,%s\n", sn, an);
516 }
517 if (alias != NULL && (sp->sclass != PARAM)) {
518 char *name = getexname(sp);
519 printf("\t.globl %s\n", name);
520 printf("%s = ", name);
521 printf("%s\n", exname(alias));
522 alias = NULL;
523 }
524 if ((constructor || destructor) && (sp->sclass != PARAM)) {
525 NODE *p = p1alloc();
526
527 p->n_op = NAME;
528 p->n_sp =
529 (struct symtab *)(constructor ? "constructor" : "destructor");
530 sp->sap = attr_add(sp->sap, gcc_attr_parse(p));
531 constructor = destructor = 0;
532 }
533 #endif
534 }
535
536 void
537 pass1_lastchance(struct interpass *ip)
538 {
539 }