Debian Codegraph @

arch/pdp7/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.8 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
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 #endif
39
40 /* this file contains code which is dependent on the target machine */
41
42 /* clocal() is called to do local transformations on
43 * an expression tree preparitory to its being
44 * written out in intermediate code.
45 *
46 * the major essential job is rewriting the
47 * automatic variables and arguments in terms of
48 * REG and OREG nodes
49 * conversion ops which are not necessary are also clobbered here
50 * in addition, any special features (such as rewriting
51 * exclusive or) are easily handled here as well
52 */
53 P1ND *
54 clocal(P1ND *p)
55 {
56
57 P1ND *r;
58 register struct symtab *q;
59 register int o;
60
61 #ifdef PCC_DEBUG
62 if (xdebug) {
63 printf("clocal: %p\n", p);
64 p1fwalk(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 AUTO:
76 /* fake up a structure reference */
77 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
78 slval(r, 0);
79 r->n_rval = FPREG;
80 p = stref(block(STREF, r, p, 0, 0, 0));
81 break;
82
83 case PARAM:
84 cerror("PARAM");
85 break;
86
87 case USTATIC:
88 break;
89
90 case STATIC:
91 break;
92
93 case REGISTER:
94 p->n_op = REG;
95 slval(p, 0);
96 p->n_rval = q->soffset;
97 break;
98
99 case EXTERN:
100 case EXTDEF:
101 break;
102 }
103 break;
104
105 case PCONV:
106 if (coptype(p->n_left->n_op) == LTYPE) {
107 p->n_left->n_type = p->n_type;
108 p = p1nfree(p);
109 }
110 break;
111
112 case SCONV:
113 break;
114
115 case PMCONV:
116 case PVCONV:
117 r = buildtree(o==PMCONV?MUL:DIV, p->n_left, p->n_right);
118 p1nfree(p);
119 p = r;
120 break;
121
122 case FORCE:
123 /* put return value in return reg */
124 p->n_op = ASSIGN;
125 p->n_right = p->n_left;
126 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
127 p->n_left->n_rval = p->n_left->n_type == BOOL ?
128 RETREG(CHAR) : RETREG(p->n_type);
129 break;
130
131 }
132 #ifdef PCC_DEBUG
133 if (xdebug) {
134 printf("clocal end: %p\n", p);
135 p1fwalk(p, eprint, 0);
136 }
137 #endif
138 return(p);
139 }
140
141 P1ND *
142 offcon(OFFSZ o, TWORD t, union dimfun *d, struct attr *ap)
143 {
144 P1ND *p;
145
146 if (t == (PTR|CHAR) || t == (PTR|UCHAR))
147 p = xbcon((o/SZCHAR), NULL, INTPTR);
148 else
149 p = xbcon((o/SZINT), NULL, INTPTR);
150 return p;
151 }
152
153 void
154 myp2tree(P1ND *p)
155 {
156 }
157
158 /*ARGSUSED*/
159 int
160 andable(P1ND *p)
161 {
162 return(1); /* all names can have & taken on them */
163 }
164
165 /*
166 * Return 1 if a variable of type type is OK to put in register.
167 */
168 int
169 cisreg(TWORD t)
170 {
171 return 1;
172 }
173
174 /*
175 * Allocate off bits on the stack. p is a tree that when evaluated
176 * is the multiply count for off, t is a storeable node where to write
177 * the allocated address.
178 */
179 void
180 spalloc(P1ND *t, P1ND *p, OFFSZ off)
181 {
182 P1ND *sp;
183
184 p = buildtree(MUL, p, bcon(off/SZCHAR)); /* XXX word alignment? */
185
186 /* sub the size from sp */
187 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
188 slval(sp, 0);
189 sp->n_rval = STKREG;
190 ecomp(buildtree(MINUSEQ, sp, p));
191
192 /* save the address of sp */
193 sp = block(REG, NIL, NIL, PTR+INT, t->n_df, t->n_ap);
194 slval(sp, 0);
195 sp->n_rval = STKREG;
196 t->n_type = sp->n_type;
197 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
198
199 }
200
201 static int chalv, curbits;
202 /*
203 * print out a constant node, may be associated with a label.
204 * Do not free the node after use.
205 * off is bit offset from the beginning of the aggregate
206 * fsz is the number of bits this is referring to
207 */
208 int
209 ninval(CONSZ off, int fsz, P1ND *p)
210 {
211 struct symtab *sp = p->n_sp;
212 long l = glval(p);
213
214 if (p->n_type != CHAR && p->n_type != UCHAR && chalv)
215 printf(" 0%o\n", curbits), chalv = 0;
216
217 switch (p->n_type) {
218 case CHAR:
219 case UCHAR:
220 if ((msettings & M_CHAR18) == 0) {
221 if (chalv) {
222 printf(" 0%o\n",
223 curbits | ((int)l & 0777));
224 chalv = 0;
225 } else
226 curbits = (l & 0777) << 9, chalv = 1;
227 break;
228 }
229 /* FALLTHROUGH */
230 case INT:
231 case UNSIGNED:
232 printf("0%o", (int)l);
233 if (sp != NULL) {
234 if ((sp->sclass == STATIC && sp->slevel > 0)) {
235 printf("+" LABFMT, (int)sp->soffset);
236 } else
237 printf("+%s", getexname(sp));
238 }
239 printf("\n");
240 break;
241 case PTR+CHAR:
242 case PTR+UCHAR:
243 if (ISCHAR9)
244 printf(".byteptr ");
245 if (l)
246 printf("0%o%s", (int)l, sp ? "+" : "");
247 if (sp != NULL) {
248 if ((sp->sclass == STATIC && sp->slevel > 0)) {
249 printf(LABFMT, (int)sp->soffset);
250 } else
251 printf("%s", getexname(sp));
252 }
253 printf("\n");
254 break;
255
256 default:
257 return 0;
258 }
259 return 1;
260 }
261
262 void
263 myendinit()
264 {
265 if (chalv)
266 printf(" 0%o\n", curbits), chalv = 0;
267 }
268
269 void
270 instring(struct symtab *sp)
271 {
272 char *s = sp->sname;
273 unsigned short word;
274
275 defloc(sp);
276 printf("\n");
277 for (; ; ) {
278 word = (*s == '\\' ? esccon(&s) : (unsigned)*s++) << 9;
279 if (word == 0 || *s == 0)
280 break;
281 word |= (*s == '\\' ? esccon(&s) : (unsigned)*s++);
282 printf(" 0%o\n", word);
283 }
284 printf(" 0%o\n", word);
285 }
286
287 /* make a name look like an external name in the local machine */
288 char *
289 exname(char *p)
290 {
291 static char b[100] = "_";
292 strcpy(b+1, p);
293 return b;
294 }
295
296 /*
297 * map types which are not defined on the local machine
298 */
299 TWORD
300 ctype(TWORD type)
301 {
302 switch (BTYPE(type)) {
303 case SHORT:
304 MODTYPE(type,INT);
305 break;
306
307 case USHORT:
308 MODTYPE(type,UNSIGNED);
309
310 }
311 return (type);
312 }
313
314 void
315 calldec(P1ND *p, P1ND *q)
316 {
317 }
318
319 void
320 extdec(struct symtab *q)
321 {
322 }
323
324 /* make a common declaration for id, if reasonable */
325 void
326 defzero(struct symtab *sp)
327 {
328 int off;
329 char *name;
330
331 name = getexname(sp);
332 off = (int)tsize(sp->stype, sp->sdf, sp->sap);
333 SETOFF(off,SZINT);
334 off /= SZINT;
335 if (ISCHAR18 && ISARY(sp->stype) &&
336 (BTYPE(sp->stype) == CHAR || BTYPE(sp->stype) == UCHAR))
337 off <<= 1;
338 if (sp->slevel == 0) {
339 if (sp->slevel == 0)
340 printf(PRTPREF "\t.local %s\n", name);
341 printf("%s:\n", name);
342 } else
343 printf(LABFMT ":", sp->soffset);
344 printf(" .=.+0%o\n", off);
345 }
346
347 /*
348 * Give target the opportunity of handling pragmas.
349 */
350 int
351 mypragma(char *str)
352 {
353 return 0;
354 }
355
356 struct lab { struct lab *next; int lab; } *lpole;
357 void dellab(int lab);
358 void printlab(void);
359 void addlab(int lab);
360
361 void
362 addlab(int lab)
363 {
364 struct lab *l;
365
366 for (l = lpole; l; l = l->next) {
367 if (l->lab == lab)
368 return;
369 }
370 l = permalloc(sizeof(struct lab));
371 l->lab = lab;
372 l->next = lpole;
373 lpole = l;
374 }
375
376 void
377 dellab(int lab)
378 {
379 struct lab *l;
380
381 for (l = lpole; l; l = l->next) {
382 if (l->lab == lab) {
383 l->lab = 0;
384 return;
385 }
386 }
387 }
388
389 void
390 printlab()
391 {
392 struct lab *l;
393
394 for (l = lpole; l; l = l->next) {
395 if (l->lab != 0)
396 printf(LABFMT ": 0\n", l->lab);
397 }
398 }
399
400 /*
401 * Called when a identifier has been declared.
402 */
403 void
404 fixdef(struct symtab *sp)
405 {
406 if (sp->sflags & STNODE)
407 return;
408 #if 0
409 if (sp->sclass == AUTO) {
410 sp->sclass = STATIC;
411 sp->soffset = getlab();
412 addlab(sp->soffset);
413 }
414 #endif
415 }
416
417 void
418 pass1_lastchance(struct interpass *ip)
419 {
420 }
421
422 #ifdef PASS1
423 void
424 mflags(char *s)
425 {
426 }
427 #endif