Debian Codegraph @

arch/pdp7/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.9 2017/02/16 18:55:31 ragge Exp $ */
2 /*
3 * Copyright (c) 2017 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 "pass2.h"
28 # include <ctype.h>
29 # include <string.h>
30
31 int msettings;
32
33 #if defined(PECOFFABI) || defined(MACHOABI) || defined(AOUTABI)
34 #define EXPREFIX "_"
35 #else
36 #define EXPREFIX ""
37 #endif
38
39 static struct consts {
40 struct consts *next;
41 int lblnum;
42 CONSZ val;
43 char *str;
44 int printed;
45 } *copole;
46
47 struct ttemp {
48 struct ttemp *next;
49 int tno;
50 } *tpole;
51
52 static int curargs, prearg, minum, indirs;
53
54
55 void
56 deflab(int label)
57 {
58 printf(LABFMT ":\n", label);
59 }
60
61 void
62 prologue(struct interpass_prolog *ipp)
63 {
64 printf("0\n"); /* return address */
65 }
66
67 void
68 eoftn(struct interpass_prolog *ipp)
69 {
70 struct consts *co;
71
72 if (ipp->ipp_ip.ip_lbl)
73 printf(" jmp %s i\n", ipp->ipp_name);
74 for (co = copole; co; co = co->next) {
75 if (co->printed)
76 continue;
77 printf("LC%d: ", co->lblnum);
78 if (co->str && co->str[0] != '\0') {
79 printf("%s", co->str);
80 if (co->val)
81 printf("+0%llo", co->val);
82 } else
83 printf("0%llo", co->val);
84 printf("\n");
85 co->printed = 1;
86 }
87 for (; tpole; tpole = tpole->next)
88 printf("LT%d: 0\n", tpole->tno);
89 if (indirs)
90 printf("Lindir: 0\n");
91 }
92
93 static int
94 addicon(int v, char *s)
95 {
96 struct consts *co;
97
98 for (co = copole; co; co = co->next) {
99 if (co->val == v && co->str == s)
100 return co->lblnum;
101 }
102 co = permalloc(sizeof(struct consts));
103 memset(co, 0, sizeof(struct consts));
104 co->val = v & 0777777;
105 co->str = s;
106 co->lblnum = minum++;
107 co->next = copole;
108 copole = co;
109 return co->lblnum;
110 }
111
112 /*
113 * add/sub/...
114 *
115 * Param given:
116 */
117 void
118 hopcode(int f, int o)
119 {
120 char *str;
121
122 switch (o) {
123 case PLUS:
124 str = "add";
125 break;
126 case MINUS:
127 str = "sub";
128 break;
129 case AND:
130 str = "and";
131 break;
132 case OR:
133 str = "or";
134 break;
135 case ER:
136 str = "xor";
137 break;
138 default:
139 comperr("hopcode2: %d", o);
140 str = 0; /* XXX gcc */
141 }
142 printf("%s%c", str, f);
143 }
144
145 /*
146 * Return type size in bytes. Used by R2REGS, arg 2 to offset().
147 */
148 int
149 tlen(NODE *p)
150 {
151 switch(p->n_type) {
152 case CHAR:
153 case UCHAR:
154 return(1);
155
156 case SHORT:
157 case USHORT:
158 return(SZSHORT/SZCHAR);
159
160 case DOUBLE:
161 return(SZDOUBLE/SZCHAR);
162
163 case INT:
164 case UNSIGNED:
165 case LONG:
166 case ULONG:
167 return(SZINT/SZCHAR);
168
169 case LONGLONG:
170 case ULONGLONG:
171 return SZLONGLONG/SZCHAR;
172
173 default:
174 if (!ISPTR(p->n_type))
175 comperr("tlen type %d not pointer");
176 return SZPOINT(p->n_type)/SZCHAR;
177 }
178 }
179
180 int
181 fldexpand(NODE *p, int cookie, char **cp)
182 {
183 comperr("fldexpand");
184 return 0;
185 }
186
187 void
188 zzzcode(NODE *p, int c)
189 {
190 NODE *q;
191 int i;
192
193 switch (c) {
194 case 'A': /* print out label arg address */
195 printf("LA%d%d", prearg, curargs - p->n_rval);
196 break;
197
198 case 'B': /* put aside constant value for later use */
199 printf("LC%d", addicon(getlval(p), p->n_name));
200 break;
201
202 case 'C': /* print out space for label args */
203 for (i = 0; i < (int)p->n_qual; i++)
204 printf("LA%d%d: 0\n", prearg, i+1);
205 break;
206
207 case 'D': /* print out NOP if not pointer */
208 if (!ISPTR(p->n_type))
209 printf(" nop\n");
210 break;
211
212 case 'E': /* print a negative number used for comparison */
213 printf("LC%d", addicon(-getlval(getlr(p, 'R')) & 0777777, 0));
214 break;
215
216 case 'F': /* mask chars with 0777 */
217 printf("LC%d", addicon(0777, 0));
218 break;
219
220 case 'G': /* store byte to a name (with offset) */
221 q = getlr(p, 'L');
222 i = addicon(0, q->n_name);
223 printf(" dac .+5\n");
224 printf(" lac LC%d\n", i);
225 printf(" rcl\n");
226 printf(" tad LC%d\n", addicon(getlval(q), 0));
227 printf(" jms sbyt; ..\n");
228 break;
229
230 case 'H': /* memory position to save indirect refs */
231 indirs=1;
232 printf("Lindir");
233 break;
234
235 case 'I': /* Gen code to add constant to memory pos */
236 expand(p, FOREFF, " dac A1\n");
237 expand(p, FOREFF, " lac A1 i\n");
238 printf(" tad LC%d\n",
239 addicon(getlval(getlr(p, 'R')) & 0777777, 0));
240 expand(p, FOREFF, " dac A1 i\n");
241 break;
242
243 case 'J': /* add const */
244 printf("LC%d", addicon(getlval(getlr(p, 'R')) & 0777777, 0));
245 break;
246
247 case 'K':
248 if (regno(p->n_right) != AC)
249 expand(p, FOREFF, " lac AR\n");
250 break;
251
252 case 'L': /* put aside left constant value for later use */
253 printf("LC%d", addicon(getlval(p->n_left), p->n_left->n_name));
254 break;
255
256 default:
257 comperr("zzzcode %c", c);
258 }
259 }
260
261 int canaddr(NODE *);
262 int
263 canaddr(NODE *p)
264 {
265 int o = p->n_op;
266
267 if (o==NAME || o==REG || o==ICON || o==OREG ||
268 (o==UMUL && shumul(p->n_left, SOREG)))
269 return(1);
270 return(0);
271 }
272
273 /*
274 * Does the bitfield shape match?
275 */
276 int
277 flshape(NODE *p)
278 {
279 comperr("flshape");
280 return 0;
281 }
282
283 /* INTEMP shapes must not contain any temporary registers */
284 /* XXX should this go away now? */
285 int
286 shtemp(NODE *p)
287 {
288 return 0;
289 }
290
291 void
292 adrcon(CONSZ val)
293 {
294 printf("$" CONFMT, val);
295 }
296
297 void
298 conput(FILE *fp, NODE *p)
299 {
300 int val = (int)getlval(p);
301
302 switch (p->n_op) {
303 case ICON:
304 if (p->n_name[0] != '\0') {
305 fprintf(fp, "%s", p->n_name);
306 if (val)
307 fprintf(fp, "+%d", val);
308 } else
309 fprintf(fp, "%d", val);
310 return;
311
312 default:
313 comperr("illegal conput, p %p", p);
314 }
315 }
316
317 /*ARGSUSED*/
318 void
319 insput(NODE *p)
320 {
321 comperr("insput");
322 }
323
324 /*
325 * Write out the upper address, like the upper register of a 2-register
326 * reference, or the next memory location.
327 */
328 void
329 upput(NODE *p, int size)
330 {
331
332 size /= SZCHAR;
333 switch (p->n_op) {
334 case REG:
335 printf("%%%s", &rnames[p->n_rval][3]);
336 break;
337
338 case NAME:
339 case OREG:
340 setlval(p, getlval(p) + size);
341 adrput(stdout, p);
342 setlval(p, getlval(p) - size);
343 break;
344 case ICON:
345 printf("$" CONFMT, getlval(p) >> 32);
346 break;
347 default:
348 comperr("upput bad op %d size %d", p->n_op, size);
349 }
350 }
351
352 void
353 adrput(FILE *io, NODE *p)
354 {
355 int r;
356 /* output an address, with offsets, from p */
357
358 switch (p->n_op) {
359
360 case NAME:
361 if (p->n_name[0] != '\0') {
362 fputs(p->n_name, io);
363 if (getlval(p) != 0) {
364 OFFSZ l = getlval(p);
365 if (ISCHAR9 &&
366 (p->n_type == CHAR || p->n_type == UCHAR))
367 l >>= 1;
368 fprintf(io, "+" CONFMT, l);
369 }
370 } else
371 fprintf(io, CONFMT, getlval(p));
372 return;
373
374 case OREG:
375 r = p->n_rval;
376 if (p->n_name[0])
377 printf("%s%s", p->n_name, getlval(p) ? "+" : "");
378 if (getlval(p))
379 fprintf(io, "%d", (int)getlval(p));
380 if (R2TEST(r)) {
381 fprintf(io, "(%s,%s,4)", rnames[R2UPK1(r)],
382 rnames[R2UPK2(r)]);
383 } else
384 fprintf(io, "(%s)", rnames[p->n_rval]);
385 return;
386 case ICON:
387 #ifdef PCC_DEBUG
388 /* Sanitycheck for PIC, to catch adressable constants */
389 if (kflag && p->n_name[0] && 0) {
390 static int foo;
391
392 if (foo++ == 0) {
393 printf("\nfailing...\n");
394 fwalk(p, e2print, 0);
395 comperr("pass2 conput");
396 }
397 }
398 #endif
399 /* addressable value of the constant */
400 fputc('$', io);
401 conput(io, p);
402 return;
403
404 case REG:
405 switch (p->n_type) {
406 case LONGLONG:
407 case ULONGLONG:
408 fprintf(io, "%%%c%c%c", rnames[p->n_rval][0],
409 rnames[p->n_rval][1], rnames[p->n_rval][2]);
410 break;
411 case SHORT:
412 case USHORT:
413 fprintf(io, "%%%s", &rnames[p->n_rval][2]);
414 break;
415 default:
416 fprintf(io, "%s", rnames[p->n_rval]);
417 }
418 return;
419
420 case UMUL:
421 if (p->n_left->n_op == NAME) {
422 adrput(io, p->n_left);
423 break;
424 }
425 if (p->n_left->n_op != REG || regno(p->n_left) == 0)
426 comperr("adrput");
427 fprintf(io, "%s i", rnames[regno(p->n_left)]);
428 break;
429
430 default:
431 comperr("illegal address, op %d, node %p", p->n_op, p);
432 return;
433
434 }
435 }
436
437 static char *
438 ccbranches[] = {
439 "sna", /* jumpe */
440 "sza", /* jumpn */
441 "ERROR", /* jumple */
442 "spa", /* jumpl */
443 "sma", /* jumpge */
444 "sma sza", /* jumpg */
445 "jbe", /* jumple (jlequ) */
446 "jb", /* jumpl (jlssu) */
447 "jae", /* jumpge (jgequ) */
448 "ja", /* jumpg (jgtru) */
449 };
450
451
452 /* printf conditional and unconditional branches */
453 void
454 cbgen(int o, int lab)
455 {
456 if (o < EQ || o > UGT)
457 comperr("bad conditional branch: %s", opst[o]);
458 printf(" %s\n", ccbranches[o-EQ]);
459 printf(" jmp " LABFMT "\n", lab);
460 }
461
462 #if 0
463 static void
464 exttemp(NODE *p, void *arg)
465 {
466 struct ttemp *w;
467
468 if (p->n_op != TEMP)
469 return;
470 for (w = tpole; w; w = w->next) {
471 if (w->tno == regno(p))
472 break;
473 }
474 if (w == NULL) {
475 w = tmpcalloc(sizeof(struct ttemp));
476 w->tno = regno(p);
477 w->next = tpole;
478 tpole = w;
479 }
480 p->n_op = NAME;
481 p->n_name = tmpcalloc(10);
482 sprintf(p->n_name, "LT%d", w->tno);
483 }
484 #endif
485
486 void
487 myreader(struct interpass *ipole)
488 {
489
490 #if 0
491 struct interpass *ip;
492 DLIST_FOREACH(ip, ipole, qelem) {
493 if (ip->type != IP_NODE)
494 continue;
495 walkf(ip->ip_node, exttemp, 0);
496 }
497 #endif
498 if (x2debug)
499 printip(ipole);
500 }
501
502 void
503 mycanon(NODE *p)
504 {
505 NODE *q;
506
507 /* Avoid test for result > 0 */
508 if (p->n_op == CBRANCH && p->n_left->n_op == LE) {
509 /* Swap */
510 p = p->n_left;
511 q = p->n_left;
512 p->n_left = p->n_right;
513 p->n_right = q;
514 p->n_op = GT;
515 }
516 }
517
518 void
519 myoptim(struct interpass *ip)
520 {
521 }
522
523 void
524 rmove(int s, int d, TWORD t)
525 {
526 comperr("bad rmove: %d %d", s, d);
527 }
528
529 /*
530 * For class c, find worst-case displacement of the number of
531 * registers in the array r[] indexed by class.
532 */
533 int
534 COLORMAP(int c, int *r)
535 {
536 if (c == CLASSA)
537 return r[CLASSA] == 0;
538 return r[CLASSB] < 7;
539 }
540
541 char *rnames[] = {
542 "AC", "POS1", "POS1", "POS1", "POS1", "POS1", "POS1", "POS1",
543 "FP", "SP",
544 };
545
546 /*
547 * Return a class suitable for a specific type.
548 */
549 int
550 gclass(TWORD t)
551 {
552 return CLASSA;
553 }
554
555 /*
556 * Calculate argument sizes.
557 * Will count # of args before emitting them.
558 */
559 void
560 lastcall(NODE *p)
561 {
562 NODE *op = p;
563 int size = 0;
564
565 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
566 return;
567 p->n_qual = 0;
568 for (p = p->n_right; p->n_op == CM; p = p->n_left)
569 p->n_right->n_rval = size++;
570 p->n_rval = size++;
571 op->n_qual = size;
572 curargs = size;
573 prearg++;
574 }
575
576 /*
577 * Special shapes.
578 */
579 int
580 special(NODE *p, int shape)
581 {
582 switch (shape) {
583 case SLDFPSP:
584 return regno(p) == FP || regno(p) == SP;
585 }
586 return SRNOPE;
587 }
588
589 /*
590 * Target-dependent command-line options.
591 */
592 void
593 mflags(char *str)
594 {
595 if (strcmp(str, "char18") == 0)
596 msettings |= M_CHAR18;
597 else
598 uerror("bad mflag");
599 }
600
601 /*
602 * Do something target-dependent for xasm arguments.
603 */
604 int
605 myxasm(struct interpass *ip, NODE *p)
606 {
607 return 0;
608 }
609