Debian Codegraph @

arch/pdp11/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.25 2019/04/23 16:14:39 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 #include "unicode.h"
28 #include "pass1.h"
29
30 #ifndef LANG_CXX
31 #define NODE P1ND
32 #undef NIL
33 #define NIL NULL
34 #define fwalk p1fwalk
35 #define nfree p1nfree
36 #endif
37
38 /* this file contains code which is dependent on the target machine */
39
40 /* clocal() is called to do local transformations on
41 * an expression tree preparitory to its being
42 * written out in intermediate code.
43 *
44 * the major essential job is rewriting the
45 * automatic variables and arguments in terms of
46 * REG and OREG nodes
47 * conversion ops which are not necessary are also clobbered here
48 * in addition, any special features (such as rewriting
49 * exclusive or) are easily handled here as well
50 */
51 NODE *
52 clocal(NODE *p)
53 {
54
55 CONSZ c;
56 register struct symtab *q;
57 register NODE *r, *l;
58 register int o;
59 TWORD m;
60
61 #ifdef PCC_DEBUG
62 if (xdebug) {
63 printf("clocal: %p\n", p);
64 fwalk(p, eprint, 0);
65 }
66 #endif
67 switch( o = p->n_op ){
68
69 case NAME:
70 if ((q = p->n_sp) == NULL)
71 return p; /* Nothing to care about */
72
73 switch (q->sclass) {
74
75 case PARAM:
76 case AUTO:
77 /* fake up a structure reference */
78 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
79 slval(r, 0);
80 r->n_rval = FPREG;
81 p = stref(block(STREF, r, p, 0, 0, 0));
82 break;
83
84 case STATIC:
85 if (q->slevel == 0)
86 break;
87 slval(p, 0);
88 break;
89
90 case REGISTER:
91 p->n_op = REG;
92 slval(p, 0);
93 p->n_rval = q->soffset;
94 break;
95
96 case EXTERN:
97 case EXTDEF:
98 break;
99 }
100 break;
101
102 case CBRANCH:
103 l = p->n_left;
104 if (coptype(l->n_op) != BITYPE)
105 break;
106 if (l->n_left->n_op != SCONV || l->n_right->n_op != ICON)
107 break;
108 if ((r = l->n_left->n_left)->n_type > INT)
109 break;
110 c = glval(l->n_right);
111 if (c < MIN_INT || c > MAX_UNSIGNED)
112 break;
113 /* compare with constant without casting */
114 nfree(l->n_left);
115 l->n_left = r;
116 l->n_right->n_type = l->n_left->n_type;
117 break;
118
119 case SCONV:
120 if (p->n_left->n_op == COMOP)
121 break; /* may propagate wrong type later */
122 l = p->n_left;
123
124 if (p->n_type == l->n_type) {
125 p1nfree(p);
126 return l;
127 }
128
129 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
130 tsize(p->n_type, p->n_df, p->n_ap) ==
131 tsize(l->n_type, l->n_df, l->n_ap)) {
132 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
133 l->n_type != FLOAT && l->n_type != DOUBLE &&
134 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
135 if (l->n_op == NAME || l->n_op == UMUL ||
136 l->n_op == TEMP) {
137 l->n_type = p->n_type;
138 p1nfree(p);
139 return l;
140 }
141 }
142 }
143
144 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
145 coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
146 l->n_op != QUEST && l->n_op != ASSIGN && l->n_op != RS) {
147 l->n_type = p->n_type;
148 p1nfree(p);
149 return l;
150 }
151
152 o = l->n_op;
153 m = p->n_type;
154 if (o == ICON) {
155 /*
156 * Can only end up here if o is an address,
157 * and in that case the only compile-time conversion
158 * possible is to int.
159 */
160
161 if (l->n_sp == 0) {
162 p->n_type = UNSIGNED;
163 concast(l, m);
164 } else if (m != INT && m != UNSIGNED)
165 break;
166 l->n_type = m;
167 l->n_ap = 0;
168 p1nfree(p);
169 return l;
170 }
171
172 if (p->n_type != UCHAR || l->n_type != CHAR ||
173 l->n_op != UMUL || (l->n_left->n_op != TEMP))
174 break;
175 l->n_type = UCHAR;
176 MODTYPE(l->n_left->n_type, UCHAR);
177 p = nfree(p);
178 break;
179
180 #if 0
181 case STASG: /* struct assignment, modify left */
182 l = p->n_left;
183 if (ISSOU(l->n_type))
184 p->n_left = buildtree(ADDROF, l, NIL);
185 break;
186 #endif
187
188 case FORCE:
189 /* put return value in return reg */
190 p->n_op = ASSIGN;
191 p->n_right = p->n_left;
192 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
193 p->n_left->n_rval = p->n_left->n_type == BOOL ?
194 RETREG(CHAR) : RETREG(p->n_type);
195 break;
196
197 }
198 #ifdef PCC_DEBUG
199 if (xdebug) {
200 printf("clocal end: %p\n", p);
201 fwalk(p, eprint, 0);
202 }
203 #endif
204 return(p);
205 }
206
207 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
208 void
209 myp2tree(NODE *p)
210 {
211 struct symtab *sp;
212
213 if (p->n_op != FCON)
214 return;
215
216 sp = IALLOC(sizeof(struct symtab));
217 sp->sclass = STATIC;
218 sp->sap = 0;
219 sp->slevel = 1; /* fake numeric label */
220 sp->soffset = getlab();
221 sp->sflags = 0;
222 sp->stype = p->n_type;
223 sp->squal = (CON >> TSHIFT);
224 sp->sname = NULL;
225
226 locctr(DATA, sp);
227 defloc(sp);
228 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
229
230 p->n_op = NAME;
231 slval(p, 0);
232 p->n_sp = sp;
233 }
234
235 /*ARGSUSED*/
236 int
237 andable(NODE *p)
238 {
239 return(1); /* all names can have & taken on them */
240 }
241
242 /*
243 * Return 1 if a variable of type type is OK to put in register.
244 */
245 int
246 cisreg(TWORD t)
247 {
248 // if (t == FLOAT || t == DOUBLE || t == LDOUBLE ||
249 // t == LONGLONG || t == ULONGLONG)
250 // return 0; /* not yet */
251 return 1;
252 }
253
254 /*
255 * Allocate off bits on the stack. p is a tree that when evaluated
256 * is the multiply count for off, t is a storeable node where to write
257 * the allocated address.
258 */
259 void
260 spalloc(NODE *t, NODE *p, OFFSZ off)
261 {
262 NODE *sp;
263
264 p = buildtree(MUL, p, bcon(off/SZCHAR)); /* XXX word alignment? */
265
266 /* sub the size from sp */
267 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
268 slval(sp, 0);
269 sp->n_rval = STKREG;
270 ecomp(buildtree(MINUSEQ, sp, p));
271
272 /* save the address of sp */
273 sp = block(REG, NIL, NIL, PTR+INT, t->n_df, t->n_ap);
274 slval(sp, 0);
275 sp->n_rval = STKREG;
276 sp = buildtree(PLUS, sp, bcon(1));
277 t->n_type = sp->n_type;
278 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
279
280 }
281
282 /*
283 * Print out a string of characters.
284 * Assume that the assembler understands C-style escape
285 * sequences.
286 */
287 void
288 instring(struct symtab *sp)
289 {
290 unsigned short sh[2];
291 int val, cnt;
292 TWORD t;
293 char *s;
294
295 defloc(sp);
296 t = BTYPE(sp->stype);
297 s = sp->sname;
298 if (t == UNSIGNED) {
299 /* convert to UTF-16 */
300 while (*s) {
301 cp2u16(u82cp(&s), sh);
302 if (sh[0]) printf("%o\n", sh[0]);
303 if (sh[1]) printf("%o\n", sh[1]);
304 }
305 printf("0\n");
306 } else if (t == CHAR) {
307 for (cnt = 0; *s != 0; ) {
308 if (cnt++ == 0)
309 printf(".byte ");
310 if (*s == '\\')
311 val = esccon(&s);
312 else
313 val = *s++;
314 printf("%o", val & 0377);
315 if (cnt > 15) {
316 cnt = 0;
317 printf("\n");
318 } else
319 printf(",");
320 }
321 printf("%s0\n", cnt ? "" : ".byte ");
322 } else
323 cerror("instring");
324 }
325
326 /*
327 * print out a constant node, may be associated with a label.
328 * Do not free the node after use.
329 * off is bit offset from the beginning of the aggregate
330 * fsz is the number of bits this is referring to
331 */
332 int
333 ninval(CONSZ off, int fsz, NODE *p)
334 {
335 #ifndef LANG_CXX
336 SFP sfp = p->n_scon;
337 #endif
338 TWORD t;
339 int i;
340
341 t = p->n_type;
342 switch (t) {
343 case LONGLONG:
344 case ULONGLONG:
345 i = (glval(p) >> 32);
346 slval(p, glval(p) & 0xffffffff);
347 p->n_type = INT;
348 ninval(off, 32, p);
349 slval(p, i);
350 ninval(off+32, 32, p);
351 break;
352 case LONG:
353 case ULONG:
354 printf("%o ; %o\n", (int)((glval(p) >> 16) & 0177777),
355 (int)(glval(p) & 0177777));
356 break;
357 #ifndef LANG_CXX
358 case FLOAT:
359 printf("%o ; %o\n", sfp->fp[0] & 0xffff, sfp->fp[0] >> 16);
360 break;
361 case LDOUBLE:
362 case DOUBLE:
363 printf("%o ; %o ; %o ; %o\n", sfp->fp[0] & 0xffff,
364 sfp->fp[0] >> 16, sfp->fp[1] & 0xffff, sfp->fp[1] >> 16);
365 break;
366 #endif
367 default:
368 return 0;
369 }
370 return 1;
371 }
372
373 /* make a name look like an external name in the local machine */
374 char *
375 exname(char *p)
376 {
377 #define NCHNAM 256
378 static char text[NCHNAM+1];
379 int i;
380
381 if (p == NULL)
382 return "";
383
384 text[0] = '_';
385 for (i=1; *p && i<NCHNAM; ++i)
386 text[i] = *p++;
387
388 text[i] = '\0';
389 text[NCHNAM] = '\0'; /* truncate */
390
391 return (text);
392
393 }
394
395 /*
396 * map types which are not defined on the local machine
397 */
398 TWORD
399 ctype(TWORD type)
400 {
401 switch (BTYPE(type)) {
402 case SHORT:
403 MODTYPE(type,INT);
404 break;
405
406 case USHORT:
407 MODTYPE(type,UNSIGNED);
408 break;
409
410 case LDOUBLE:
411 MODTYPE(type,DOUBLE);
412 break;
413
414 /* XXX remove as soon as 64-bit is added */
415 case LONGLONG:
416 MODTYPE(type,LONG);
417 break;
418 case ULONGLONG:
419 MODTYPE(type,ULONG);
420 break;
421 }
422 return (type);
423 }
424
425 void
426 calldec(NODE *p, NODE *q)
427 {
428 }
429
430 void
431 extdec(struct symtab *q)
432 {
433 }
434
435 /* make a common declaration for id, if reasonable */
436 void
437 defzero(struct symtab *sp)
438 {
439 extern int lastloc;
440 char *n;
441 int off;
442
443 off = tsize(sp->stype, sp->sdf, sp->sap);
444 off = (off+(SZCHAR-1))/SZCHAR;
445 n = getexname(sp);
446 if (sp->sclass == STATIC) {
447 printf(".bss\n");
448 if (sp->slevel == 0)
449 printf("%s:", n);
450 else
451 printf(LABFMT ":", sp->soffset);
452 printf(" .=.+%o\n", off);
453 lastloc = -1;
454 return;
455 }
456 printf(".comm ");
457 if (sp->slevel == 0)
458 printf("%s,0%o\n", n, off);
459 else
460 printf(LABFMT ",0%o\n", sp->soffset, off);
461 }
462
463 /*
464 * Give target the opportunity of handling pragmas.
465 */
466 int
467 mypragma(char *str)
468 {
469 return 0;
470 }
471
472 /*
473 * Called when a identifier has been declared.
474 */
475 void
476 fixdef(struct symtab *sp)
477 {
478 }
479
480 void
481 pass1_lastchance(struct interpass *ip)
482 {
483 }