Debian Codegraph @

arch/m16c/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.25 2014/05/29 19:20:02 plunky 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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 # include "pass1.h"
31
32 /*
33 * cause the alignment to become a multiple of n
34 */
35 void
36 defalign(int n)
37 {
38 #if 0
39 char *s;
40
41 n /= SZCHAR;
42 if (lastloc == PROG || n == 1)
43 return;
44 s = (isinlining ? permalloc(40) : tmpalloc(40));
45 sprintf(s, "\t.align %d\n", n);
46 send_passt(IP_ASM, s);
47 #endif
48 }
49
50 /*
51 * define the current location as the name p->soname
52 */
53 void
54 defnam(struct symtab *p)
55 {
56 char *c = p->soname;
57
58 if (p->sclass == EXTDEF)
59 printf(" PUBLIC %s\n", c);
60 printf("%s:\n", c);
61 }
62
63
64 /*
65 * code for the end of a function
66 * deals with struct return here
67 */
68 void
69 efcode(void)
70 {
71 NODE *p, *q;
72 int sz;
73
74 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
75 return;
76 /* address of return struct is in eax */
77 /* create a call to memcpy() */
78 /* will get the result in eax */
79 p = block(REG, NIL, NIL, CHAR+PTR, 0, 0);
80 p->n_rval = R0;
81 q = block(OREG, NIL, NIL, CHAR+PTR, 0, 0);
82 q->n_rval = FB;
83 q->n_lval = 8; /* return buffer offset */
84 p = block(CM, q, p, INT, 0, 0);
85 sz = (tsize(STRTY, cftnsp->sdf, cftnsp->ssue)+SZCHAR-1)/SZCHAR;
86 p = block(CM, p, bcon(sz), INT, 0, 0);
87 p->n_right->n_name = "";
88 p = block(CALL, bcon(0), p, CHAR+PTR, 0, 0);
89 p->n_left->n_name = "memcpy";
90 send_passt(IP_NODE, p);
91 }
92
93 /*
94 * helper for bfcode() to put register arguments on stack.
95 */
96 static void
97 argmove(struct symtab *s, int regno)
98 {
99 NODE *p, *r;
100
101 s->sclass = AUTO;
102 s->soffset = NOOFFSET;
103 oalloc(s, &autooff);
104 p = nametree(s);
105 r = bcon(0);
106 r->n_op = REG;
107 r->n_rval = regno;
108 r->n_type = p->n_type;
109 r->n_sue = p->n_sue;
110 r->n_df = p->n_df;
111 ecode(buildtree(ASSIGN, p, r));
112 }
113
114 /*
115 * code for the beginning of a function; a is an array of
116 * indices in symtab for the arguments; n is the number
117 * On m16k, space is allocated on stack for register arguments,
118 * arguments are moved to the stack and symtab is updated accordingly.
119 */
120 void
121 bfcode(struct symtab **a, int n)
122 {
123 struct symtab *s;
124 int i, r0l, r0h, a0, r2, sz, hasch, stk;
125 int argoff = ARGINIT;
126
127 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
128 /* Function returns struct, adjust arg offset */
129 for (i = 0; i < n; i++)
130 a[i]->soffset += SZPOINT(INT);
131 }
132 /* first check if there are 1-byte parameters */
133 for (hasch = i = 0; i < n && i < 6; i++)
134 if (DEUNSIGN(a[i]->stype) == CHAR)
135 hasch = 1;
136
137 stk = r0l = r0h = a0 = r2 = 0;
138 for (i = 0; i < n; i++) {
139 s = a[i];
140 sz = tsize(s->stype, s->sdf, s->ssue);
141 if (ISPTR(s->stype) && ISFTN(DECREF(s->stype)))
142 sz = SZLONG; /* function pointers are always 32 */
143 if (stk == 0)
144 switch (sz) {
145 case SZCHAR:
146 if (r0l) {
147 if (r0h)
148 break;
149 argmove(s, 1);
150 r0h = 1;
151 } else {
152 argmove(s, 0);
153 r0l = 1;
154 }
155 continue;
156
157 case SZINT:
158 if (s->stype > BTMASK) {
159 /* is a pointer */
160 if (a0) {
161 if (r0l || hasch) {
162 if (r2)
163 break;
164 argmove(s, R2);
165 r2 = 1;
166 } else {
167 argmove(s, R0);
168 r0l = r0h = 1;
169 }
170 } else {
171 argmove(s, A0);
172 a0 = 1;
173 }
174 } else if (r0l || hasch) {
175 if (r2) {
176 if (a0)
177 break;
178 argmove(s, A0);
179 a0 = 1;
180 } else {
181 argmove(s, R2);
182 r2 = 1;
183 }
184 } else {
185 argmove(s, R0);
186 r0l = r0h = 1;
187 }
188 continue;
189 case SZLONG:
190 if (r0l||r0h||r2)
191 break;
192 argmove(s, R0);
193 r0l = r0h = r2 = 1;
194 continue;
195
196 default:
197 break;
198 }
199 stk = 1;
200 s->soffset = argoff;
201 argoff += sz;
202 }
203 }
204
205 /*
206 * Add a symbol to an internal list printed out at the end.
207 */
208 void addsym(struct symtab *);
209 static struct symlst {
210 struct symlst *next;
211 struct symtab *sp;
212 } *sympole;
213
214 void
215 addsym(struct symtab *q)
216 {
217 struct symlst *w = sympole;
218
219 if (q == NULL)
220 return;
221
222 while (w) {
223 if (q == w->sp)
224 return; /* exists */
225 w = w->next;
226 }
227 w = permalloc(sizeof(struct symlst));
228 w->sp = q;
229 w->next = sympole;
230 sympole = w;
231 }
232
233 struct caps {
234 char *cap, *stat;
235 } caps[] = {
236 { "__64bit_doubles", "Disabled" },
237 { "__calling_convention", "Normal" },
238 { "__constant_data", "near" },
239 { "__data_alignment", "2" },
240 { "__data_model", "near" },
241 { "__processor", "M16C" },
242 { "__rt_version", "1" },
243 { "__variable_data", "near" },
244 { NULL, NULL },
245 };
246
247 /*
248 * Called before parsing begins.
249 */
250 void
251 bjobcode(void)
252 {
253 struct caps *c;
254
255 printf(" NAME gurka.c\n"); /* Don't have the name */
256 for (c = caps; c->cap; c++)
257 printf(" RTMODEL \"%s\", \"%s\"\n", c->cap, c->stat);
258 //printf(" RSEG CODE:CODE:REORDER:NOROOT(0)\n");
259 }
260
261 /* called just before final exit */
262 /* flag is 1 if errors, 0 if none */
263 void
264 ejobcode(int flag)
265 {
266 struct symlst *w = sympole;
267
268 for (w = sympole; w; w = w->next) {
269 if (w->sp->sclass != EXTERN)
270 continue;
271 printf(" EXTERN %s\n", w->sp->soname);
272 }
273
274 printf(" END\n");
275 }
276
277 /*
278 * Print character t at position i in one string, until t == -1.
279 * Locctr & label is already defined.
280 */
281 void
282 bycode(int t, int i)
283 {
284 static int lastoctal = 0;
285
286 /* put byte i+1 in a string */
287
288 if (t < 0) {
289 if (i != 0)
290 puts("\"");
291 } else {
292 if (i == 0)
293 printf("\t.ascii \"");
294 if (t == '\\' || t == '"') {
295 lastoctal = 0;
296 putchar('\\');
297 putchar(t);
298 } else if (t < 040 || t >= 0177) {
299 lastoctal++;
300 printf("\\%o",t);
301 } else if (lastoctal && '0' <= t && t <= '9') {
302 lastoctal = 0;
303 printf("\"\n\t.ascii \"%c", t);
304 } else {
305 lastoctal = 0;
306 putchar(t);
307 }
308 }
309 }
310
311 /* fix up type of field p */
312 void
313 fldty(struct symtab *p)
314 {
315 }
316
317 /*
318 * XXX - fix genswitch.
319 */
320 int
321 mygenswitch(int num, TWORD type, struct swents **p, int n)
322 {
323 return 0;
324 }
325 /*
326 * Called with a function call with arguments as argument.
327 * This is done early in buildtree() and only done once.
328 */
329 NODE *
330 funcode(NODE *p)
331 {
332 return p;
333 }