Debian Codegraph @

arch/m16c/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.22 2015/08/18 10:15:08 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 NODE *
33 clocal(NODE *p)
34 {
35 /* this is called to do local transformations on
36 an expression tree preparitory to its being
37 written out in intermediate code.
38 */
39
40 /* the major essential job is rewriting the
41 automatic variables and arguments in terms of
42 REG and OREG nodes */
43 /* conversion ops which are not necessary are also clobbered here */
44 /* in addition, any special features (such as rewriting
45 exclusive or) are easily handled here as well */
46
47 struct symtab *q;
48 NODE *l, *r;
49 int o;
50 TWORD ml;
51
52 switch( o = p->n_op ){
53
54 case NAME:
55 if ((q = p->n_sp) == NULL)
56 return p; /* Nothing to care about */
57
58 switch (q->sclass) {
59
60 case PARAM:
61 case AUTO:
62 /* fake up a structure reference */
63 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
64 r->n_lval = 0;
65 r->n_rval = FPREG;
66 p = stref(block(STREF, r, p, 0, 0, 0));
67 break;
68
69 case STATIC:
70 if (q->slevel == 0)
71 break;
72 p->n_lval = 0;
73 p->n_sp = q;
74 break;
75
76 case REGISTER:
77 p->n_op = REG;
78 p->n_lval = 0;
79 p->n_rval = q->soffset;
80 break;
81
82 }
83 break;
84
85 case PCONV:
86 ml = p->n_left->n_type;
87 l = p->n_left;
88 if ((ml == CHAR || ml == UCHAR) && l->n_op != ICON)
89 break;
90 l->n_type = p->n_type;
91 l->n_qual = p->n_qual;
92 l->n_df = p->n_df;
93 l->n_sue = p->n_sue;
94 nfree(p);
95 p = l;
96 break;
97
98 case SCONV:
99 l = p->n_left;
100 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT) {
101 nfree(p);
102 return l;
103 }
104 if (l->n_op == ICON) {
105 CONSZ val = l->n_lval;
106 switch (p->n_type) {
107 case CHAR:
108 l->n_lval = (char)val;
109 break;
110 case UCHAR:
111 l->n_lval = val & 0377;
112 break;
113 case SHORT:
114 case INT:
115 l->n_lval = (short)val;
116 break;
117 case USHORT:
118 case UNSIGNED:
119 l->n_lval = val & 0177777;
120 break;
121 case ULONG:
122 case ULONGLONG:
123 l->n_lval = val & 0xffffffff;
124 break;
125 case LONG:
126 case LONGLONG:
127 l->n_lval = (int)val;
128 break;
129 case VOID:
130 break;
131 case LDOUBLE:
132 case DOUBLE:
133 case FLOAT:
134 l->n_op = FCON;
135 l->n_dcon = val;
136 break;
137 default:
138 cerror("unknown type %d", p->n_type);
139 }
140 l->n_type = p->n_type;
141 nfree(p);
142 return l;
143 }
144 break;
145
146
147 }
148
149 return(p);
150 }
151
152 /*ARGSUSED*/
153 int
154 andable(NODE *p)
155 {
156 return(1); /* all names can have & taken on them */
157 }
158
159 /*
160 * is an automatic variable of type t OK for a register variable
161 */
162 int
163 cisreg(TWORD t)
164 {
165 if (t == INT || t == UNSIGNED || t == CHAR || t == UCHAR ||
166 ISPTR(t))
167 return(1);
168 return 0; /* XXX - fix reg assignment in pftn.c */
169 }
170
171
172 /*
173 * Allocate off bits on the stack. p is a tree that when evaluated
174 * is the multiply count for off, t is a NAME node where to write
175 * the allocated address.
176 */
177 void
178 spalloc(NODE *t, NODE *p, OFFSZ off)
179 {
180 NODE *sp;
181
182 if ((off % SZINT) == 0)
183 p = buildtree(MUL, p, bcon(off/SZINT));
184 else if ((off % SZSHORT) == 0) {
185 p = buildtree(MUL, p, bcon(off/SZSHORT));
186 p = buildtree(PLUS, p, bcon(1));
187 p = buildtree(RS, p, bcon(1));
188 } else if ((off % SZCHAR) == 0) {
189 p = buildtree(MUL, p, bcon(off/SZCHAR));
190 p = buildtree(PLUS, p, bcon(3));
191 p = buildtree(RS, p, bcon(2));
192 } else
193 cerror("roundsp");
194
195 /* save the address of sp */
196 sp = block(REG, NIL, NIL, PTR+INT, t->n_df, t->n_sue);
197 sp->n_lval = 0;
198 sp->n_rval = STKREG;
199 t->n_type = sp->n_type;
200 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
201
202 /* add the size to sp */
203 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
204 sp->n_lval = 0;
205 sp->n_rval = STKREG;
206 ecomp(buildtree(PLUSEQ, sp, p));
207 }
208
209 /*
210 * print out a constant node
211 * mat be associated with a label
212 */
213 int
214 ninval(NODE *p)
215 {
216 struct symtab *q;
217 TWORD t;
218
219 p = p->n_left;
220 t = p->n_type;
221 if (t > BTMASK)
222 t = p->n_type = INT; /* pointer */
223
224 switch (t) {
225 case LONGLONG:
226 case ULONGLONG:
227 inval(p->n_lval & 0xffffffff);
228 inval(p->n_lval >> 32);
229 break;
230 case LONG:
231 case ULONG:
232 case INT:
233 case UNSIGNED:
234 printf("\t.long 0x%x", (int)p->n_lval);
235 if ((q = p->n_sp) != NULL) {
236 if ((q->sclass == STATIC && q->slevel > 0)) {
237 printf("+" LABFMT, q->soffset);
238 } else
239 printf("+%s", exname(q->soname));
240 }
241 printf("\n");
242 break;
243 default:
244 fwalk(p, eprint, 0);
245 return 0;
246 }
247 return 1;
248 }
249
250 /* make a name look like an external name in the local machine */
251 char *
252 exname(char *p)
253 {
254 if (p == NULL)
255 return "";
256 return p;
257 }
258
259 /*
260 * map types which are not defined on the local machine
261 */
262 TWORD
263 ctype(TWORD type)
264 {
265 switch (BTYPE(type)) {
266 case SHORT:
267 MODTYPE(type,INT);
268 break;
269
270 case USHORT:
271 MODTYPE(type,UNSIGNED);
272 break;
273
274 case LONGLONG:
275 MODTYPE(type,LONG);
276 break;
277
278 case ULONGLONG:
279 MODTYPE(type,ULONG);
280 break;
281
282 case LDOUBLE:
283 MODTYPE(type,DOUBLE);
284 break;
285 }
286 return (type);
287 }
288
289 /* curid is a variable which is defined but
290 * is not initialized (and not a function );
291 * This routine returns the storage class for an uninitialized declaration
292 */
293 int
294 noinit()
295 {
296 return(EXTERN);
297 }
298
299 /*
300 * Extern variable not necessary common.
301 */
302 void
303 extdec(struct symtab *q)
304 {
305 extern void addsym(struct symtab *);
306 addsym(q);
307 }
308
309 /*
310 * Call to a function
311 */
312 void
313 calldec(NODE *p, NODE *r)
314 {
315 struct symtab *q = p->n_sp;
316 extern void addsym(struct symtab *);
317 addsym(q);
318 }
319
320 /* make a common declaration for id, if reasonable */
321 void
322 commdec(struct symtab *q)
323 {
324 int off;
325 char *c = q->soname;
326
327 off = tsize(q->stype, q->sdf, q->ssue);
328 off = (off+(SZCHAR-1))/SZCHAR;
329
330 printf(" PUBLIC %s\n", c);
331 /* XXX - NOROOT??? */
332 printf(" RSEG DATA16_Z:NEARDATA:SORT:NOROOT(1)\n");
333 printf("%s:\n", c);
334 printf(" DS8 %d\n", off);
335 printf(" REQUIRE __data16_zero\n");
336 }
337
338 /* make a local common declaration for id, if reasonable */
339 void
340 lcommdec(struct symtab *q)
341 {
342 int off;
343
344 off = tsize(q->stype, q->sdf, q->ssue);
345 off = (off+(SZCHAR-1))/SZCHAR;
346 if (q->slevel == 0)
347 printf(" .lcomm %s,0%o\n", exname(q->soname), off);
348 else
349 printf(" .lcomm " LABFMT ",0%o\n", q->soffset, off);
350 }
351
352 /*
353 * print a (non-prog) label.
354 */
355 void
356 deflab1(int label)
357 {
358 printf(LABFMT ":\n", label);
359 }
360
361 void
362 setloc1(int locc)
363 {
364 if (locc == lastloc)
365 return;
366 lastloc = locc;
367 }
368
369 /*
370 * special handling before tree is written out.
371 */
372 void
373 myp2tree(NODE *p)
374 {
375 struct symtab *sp;
376 union dimfun *df;
377 union arglist *al;
378 NODE *q;
379 int i;
380
381 switch (p->n_op) {
382 case MOD:
383 case DIV:
384 if (p->n_type == LONG || p->n_type == ULONG) {
385 /* Swap arguments for hardops() later */
386 q = p->n_left;
387 p->n_left = p->n_right;
388 p->n_right = q;
389 }
390 break;
391
392 case CALL:
393 case STCALL:
394 /*
395 * inform pass2 about varargs.
396 * store first variadic argument number in n_stalign
397 * in the CM node.
398 */
399 if (p->n_right->n_op != CM)
400 break; /* nothing to care about */
401 df = p->n_left->n_df;
402 if (df && (al = df->dfun)) {
403 for (i = 0; i < 6; i++, al++) {
404 if (al->type == TELLIPSIS || al->type == TNULL)
405 break;
406 }
407 p->n_right->n_stalign = al->type == TELLIPSIS ? i : 0;
408 } else
409 p->n_right->n_stalign = 0;
410 break;
411
412 case FCON:
413 /* Write float constants to memory */
414 sp = tmpalloc(sizeof(struct symtab));
415 sp->sclass = STATIC;
416 sp->ssue = 0;
417 sp->slevel = 1; /* fake numeric label */
418 sp->soffset = getlab();
419 sp->sflags = 0;
420 sp->stype = p->n_type;
421 sp->squal = (CON >> TSHIFT);
422
423 defloc(sp);
424 ninval(0, sp->ssue->suesize, p);
425
426 p->n_op = NAME;
427 p->n_lval = 0;
428 p->n_sp = sp;
429 break;
430 }
431
432 }
433
434 /*
435 * Give target the opportunity of handling pragmas.
436 */
437 int
438 mypragma(char **ary)
439 {
440 return 0;
441 }
442
443 /*
444 * Called when a identifier has been declared, to give target last word.
445 */
446 void
447 fixdef(struct symtab *sp)
448 {
449 }
450
451 void
452 pass1_lastchance(struct interpass *ip)
453 {
454 }