Debian Codegraph @

arch/m68k/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.8 2016/01/30 17:26:19 ragge Exp $ */
2 /*
3 * Copyright (c) 2014 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 #ifdef LANG_CXX
31 #define p1listf listf
32 #define p1tfree tfree
33 #else
34 #define NODE P1ND
35 #define talloc p1alloc
36 #define tfree p1tfree
37 #endif
38
39 extern int gotnr;
40
41 /*
42 * Print out assembler segment name.
43 */
44 void
45 setseg(int seg, char *name)
46 {
47 switch (seg) {
48 case PROG: name = ".text"; break;
49 case DATA:
50 case LDATA: name = ".data"; break;
51 case STRNG:
52 case RDATA: name = ".section .rodata"; break;
53 case UDATA: break;
54 case PICLDATA:
55 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
56 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
57 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
58 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
59 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
60 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
61 case NMSEG:
62 printf("\t.section %s,\"a%c\",@progbits\n", name,
63 cftnsp ? 'x' : 'w');
64 return;
65 }
66 printf("\t%s\n", name);
67 }
68
69 /*
70 * Define everything needed to print out some data (or text).
71 * This means segment, alignment, visibility, etc.
72 */
73 void
74 defloc(struct symtab *sp)
75 {
76 char *name;
77
78 name = getexname(sp);
79 if (sp->sclass == EXTDEF) {
80 printf("\t.globl %s\n", name);
81 if (ISFTN(sp->stype)) {
82 printf("\t.type %s,@function\n", name);
83 } else {
84 printf("\t.type %s,@object\n", name);
85 printf("\t.size %s,%d\n", name,
86 (int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR);
87 }
88 }
89 if (sp->slevel == 0)
90 printf("%s:\n", name);
91 else
92 printf(LABFMT ":\n", sp->soffset);
93 }
94
95 /*
96 * code for the end of a function
97 * deals with struct return here
98 * The return value is in (or pointed to by) RETREG.
99 */
100 int sttemp;
101
102 void
103 efcode(void)
104 {
105 NODE *p, *q;
106
107 gotnr = 0;
108 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
109 return;
110 /* use stasg to get call to memcpy() */
111 p = buildtree(UMUL, tempnode(sttemp, INCREF(STRTY),
112 cftnsp->sdf, cftnsp->sap), NIL);
113 q = block(REG, 0, 0, INCREF(STRTY), cftnsp->sdf, cftnsp->sap);
114 regno(q) = A0;
115 q = buildtree(UMUL, q, NIL);
116 p = buildtree(ASSIGN, p, q);
117 ecomp(p);
118 }
119
120 /*
121 * code for the beginning of a function; a is an array of
122 * indices in symtab for the arguments; n is the number
123 */
124 void
125 bfcode(struct symtab **s, int cnt)
126 {
127 struct symtab *sp2;
128 NODE *n, *p;
129 int i;
130
131 if (kflag) { /* PIC code */
132 /* Generate extended assembler for PIC prolog */
133 p = tempnode(0, CHAR|PTR, 0, 0);
134 gotnr = regno(p);
135 p = block(XARG, p, NIL, INT, 0, 0);
136 p->n_name = "=r";
137 p = block(XASM, p, bcon(0), INT, 0, 0);
138
139 p->n_name = "lea (%%pc,_GLOBAL_OFFSET_TABLE_@GOTPC),%0\n";
140 p->n_right->n_type = STRTY;
141 ecomp(p);
142 }
143
144 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
145 n = tempnode(0, INCREF(CHAR), 0, 0);
146 p = block(REG, 0, 0, INCREF(CHAR), 0, 0);
147 regno(p) = A0;
148 sttemp = regno(n);
149 ecomp(buildtree(ASSIGN, n, p));
150 }
151
152 if (xtemps == 0)
153 return;
154
155 /* put arguments in temporaries */
156 for (i = 0; i < cnt; i++) {
157 if (s[i]->stype == STRTY || s[i]->stype == UNIONTY ||
158 cisreg(s[i]->stype) == 0)
159 continue;
160 if (cqual(s[i]->stype, s[i]->squal) & VOL)
161 continue;
162 sp2 = s[i];
163 n = tempnode(0, s[i]->stype, s[i]->sdf, s[i]->sap);
164 n = buildtree(ASSIGN, n, nametree(sp2));
165 s[i]->soffset = regno(n->n_left);
166 s[i]->sflags |= STNODE;
167 ecomp(n);
168 }
169 }
170
171
172 /* called just before final exit */
173 /* flag is 1 if errors, 0 if none */
174 void
175 ejobcode(int flag)
176 {
177 if (flag)
178 return;
179
180 printf("\t.ident \"PCC: %s\"\n", VERSSTR);
181 }
182
183 void
184 bjobcode(void)
185 {
186 /* Set correct names for our types */
187 astypnames[SHORT] = astypnames[USHORT] = "\t.word";
188 astypnames[INT] = astypnames[UNSIGNED] = "\t.long";
189 }
190
191 /*
192 * Called with a function call with arguments as argument.
193 * This is done early in buildtree() and only done once.
194 * Returns p.
195 */
196 NODE *
197 funcode(NODE *p)
198 {
199 NODE *r, *l;
200
201 /* Fix function call arguments. On m68k, just add funarg */
202 for (r = p->n_right; r->n_op == CM; r = r->n_left) {
203 if (r->n_right->n_op != STARG) {
204 r->n_right = intprom(r->n_right);
205 r->n_right = block(FUNARG, r->n_right, NIL,
206 r->n_right->n_type, r->n_right->n_df,
207 r->n_right->n_ap);
208 }
209 }
210 if (r->n_op != STARG) {
211 l = talloc();
212 *l = *r;
213 r->n_op = FUNARG;
214 r->n_left = l;
215 r->n_left = intprom(r->n_left);
216 r->n_type = r->n_left->n_type;
217 }
218 return p;
219
220 }
221
222 /* fix up type of field p */
223 void
224 fldty(struct symtab *p)
225 {
226 }
227
228 /*
229 * XXX - fix genswitch.
230 */
231 int
232 mygenswitch(int num, TWORD type, struct swents **p, int n)
233 {
234 return 0;
235 }
236
237 /*
238 * Return return as given by a.
239 */
240 NODE *
241 builtin_return_address(const struct bitable *bt, NODE *a)
242 {
243 int nframes;
244 NODE *f;
245
246 cerror((char *)__func__);
247 nframes = glval(a);
248 tfree(a);
249
250 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
251 regno(f) = FPREG;
252
253 while (nframes--)
254 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
255
256 f = block(PLUS, f, bcon(8), INCREF(PTR+VOID), 0, 0);
257 f = buildtree(UMUL, f, NIL);
258
259 return f;
260 }
261
262 /*
263 * Return frame as given by a.
264 */
265 NODE *
266 builtin_frame_address(const struct bitable *bt, NODE *a)
267 {
268 int nframes;
269 NODE *f;
270
271 cerror((char *)__func__);
272 nframes = glval(a);
273 tfree(a);
274
275 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
276 regno(f) = FPREG;
277
278 while (nframes--)
279 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
280
281 return f;
282 }
283
284 /*
285 * Return "canonical frame address".
286 */
287 NODE *
288 builtin_cfa(const struct bitable *bt, NODE *a)
289 {
290 NODE *f;
291
292 cerror((char *)__func__);
293 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
294 regno(f) = FPREG;
295 return block(PLUS, f, bcon(16), INCREF(PTR+VOID), 0, 0);
296 }