Debian Codegraph @

arch/vax/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.28 2018/04/03 14:49:46 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 # include "pass1.h"
37
38 #ifndef LANG_CXX
39 #define NODE P1ND
40 #undef NIL
41 #define NIL NULL
42 #define tfree p1tfree
43 #define talloc p1alloc
44 #define ccopy p1tcopy
45 #endif
46
47 /*
48 * Print out assembler segment name.
49 */
50 void
51 setseg(int seg, char *name)
52 {
53 switch (seg) {
54 case PROG: name = ".text"; break;
55
56 case DATA:
57 case LDATA: name = ".data"; break;
58
59 case STRNG:
60 case RDATA: name = ".section .rodata"; break;
61
62 case UDATA: break;
63
64 case DTORS:
65 name = ".section .dtors,\"aw\",@progbits";
66 break;
67 case CTORS:
68 name = ".section .ctors,\"aw\",@progbits";
69 break;
70
71 case TLSDATA:
72 case TLSUDATA:
73 uerror("FIXME: unsupported segment %d", seg);
74 break;
75
76 case PICRDATA:
77 name = ".section .data.rel.ro.local,\"aw\",@progbits";
78 break;
79
80 case PICDATA:
81 name = ".section .data.rel,\"aw\",@progbits";
82 break;
83 case PICLDATA:
84 name = ".section .data.rel.local,\"aw\",@progbits";
85 break;
86
87 case NMSEG:
88 printf("\t.section %s,\"a%c\",@progbits\n", name,
89 cftnsp ? 'x' : 'w');
90 return;
91 }
92 printf("\t%s\n", name);
93 }
94
95 /*
96 * Define everything needed to print out some data (or text).
97 * This means segment, alignment, visibility, etc.
98 */
99 void
100 defloc(struct symtab *sp)
101 {
102 char *name;
103
104 name = getexname(sp);
105
106 if (sp->sclass == EXTDEF) {
107 printf("\t.globl %s\n", name);
108 if (ISFTN(sp->stype)) {
109 printf("\t.type %s,@function\n", name);
110 } else {
111 printf("\t.type %s,@object\n", name);
112 printf("\t.size %s,%d\n", name,
113 (int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR);
114 }
115 }
116 if (sp->slevel == 0)
117 printf("%s:\n", name);
118 else
119 printf(LABFMT ":\n", sp->soffset);
120 }
121
122 static int strtemp;
123
124 void
125 efcode(void)
126 {
127 TWORD t;
128 NODE *p, *q;
129
130 /* code for the end of a function */
131 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
132 return;
133
134 t = PTR+BTYPE(cftnsp->stype);
135 /* Create struct assignment */
136 q = tempnode(strtemp, t, 0, cftnsp->sap);
137 q = buildtree(UMUL, q, NIL);
138 p = block(REG, NIL, NIL, t, 0, cftnsp->sap);
139 regno(p) = R0;
140 p = buildtree(UMUL, p, NIL);
141 p = buildtree(ASSIGN, q, p);
142 ecomp(p);
143
144 /* put hidden arg in r0 on return */
145 q = tempnode(strtemp, INT, 0, 0);
146 p = block(REG, NIL, NIL, INT, 0, 0);
147 regno(p) = R0;
148 ecomp(buildtree(ASSIGN, p, q));
149 }
150
151 void
152 bfcode(struct symtab **sp, int n)
153 {
154 struct symtab *sp2;
155 NODE *p, *q;
156 int i, argbase, sz;
157
158 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
159 /* Move return address into temporary */
160 p = tempnode(0, INT, 0, 0);
161 strtemp = regno(p);
162 q = block(REG, 0, 0, INT, 0, 0);
163 regno(q) = R1;
164 ecomp(buildtree(ASSIGN, p, q));
165 }
166
167 /* correct arg alignment XXX should be done somewhere else */
168 argbase = ARGINIT;
169 for (i = 0; i < n; i++) {
170 sp2 = sp[i];
171 sz = tsize(sp2->stype, sp2->sdf, sp2->sap);
172
173 SETOFF(sz, SZINT);
174 sp2->soffset = argbase;
175 argbase += sz;
176 }
177
178 if (xtemps == 0)
179 return;
180
181 /* put arguments in temporaries */
182 for (i = 0; i < n; i++) {
183 if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY ||
184 cisreg(sp[i]->stype) == 0)
185 continue;
186 if (cqual(sp[i]->stype, sp[i]->squal) & VOL)
187 continue;
188 sp2 = sp[i];
189 p = tempnode(0, sp[i]->stype, sp[i]->sdf, sp[i]->sap);
190 p = buildtree(ASSIGN, p, nametree(sp2));
191 sp[i]->soffset = regno(p->n_left);
192 sp[i]->sflags |= STNODE;
193 ecomp(p);
194 }
195
196 }
197
198 void
199 ejobcode(int flag)
200 {
201 /* called just before final exit */
202 /* flag is 1 if errors, 0 if none */
203 }
204
205 void
206 bjobcode(void)
207 {
208 astypnames[INT] = astypnames[UNSIGNED] = "\t.long";
209 astypnames[SHORT] = astypnames[USHORT] = "\t.word";
210 }
211
212 #if 0
213 aobeg(void)
214 {
215 /* called before removing automatics from stab */
216 }
217
218 aocode(struct symtab *p)
219 {
220 /* called when automatic p removed from stab */
221 }
222
223 aoend(void)
224 {
225 /* called after removing all automatics from stab */
226 }
227 #endif
228
229 void
230 fldty(struct symtab *p)
231 {
232 /* fix up type of field p */
233 }
234
235 /*
236 * XXX - fix genswitch.
237 */
238 int
239 mygenswitch(int num, TWORD type, struct swents **p, int n)
240 {
241 return 0;
242 }
243
244 #ifdef notyet
245 struct sw heapsw[SWITSZ]; /* heap for switches */
246
247 genswitch(register struct sw *p, int n)
248 {
249 /* p points to an array of structures, each consisting
250 of a constant value and a label.
251 The first is >=0 if there is a default label;
252 its value is the label number
253 The entries p[1] to p[n] are the nontrivial cases
254 */
255 register i;
256 register CONSZ j, range;
257 register dlab, swlab;
258
259 range = p[n].sval-p[1].sval;
260
261 if( range>0 && range <= 3*n && n>=4 ){ /* implement a direct switch */
262
263 swlab = getlab();
264 dlab = p->slab >= 0 ? p->slab : getlab();
265
266 /* already in r0 */
267 printf(" casel r0,$%ld,$%ld\n", p[1].sval, range);
268 deflab1(swlab);
269 for( i=1,j=p[1].sval; i<=n; j++) {
270 printf(" .word " LABFMT "-" LABFMT "\n",
271 (j == p[i].sval ? ((j=p[i++].sval), p[i-1].slab) : dlab),
272 swlab);
273 }
274
275 if( p->slab >= 0 ) branch( dlab );
276 else deflab1(dlab);
277 return;
278
279 }
280
281 if( n>8 ) { /* heap switch */
282
283 heapsw[0].slab = dlab = p->slab >= 0 ? p->slab : getlab();
284 makeheap(p, n, 1); /* build heap */
285
286 walkheap(1, n); /* produce code */
287
288 if( p->slab >= 0 )
289 branch( dlab );
290 else
291 deflab1(dlab);
292 return;
293 }
294
295 /* debugging code */
296
297 /* out for the moment
298 if( n >= 4 ) werror( "inefficient switch: %d, %d", n, (int) (range/n) );
299 */
300
301 /* simple switch code */
302
303 for( i=1; i<=n; ++i ){
304 /* already in r0 */
305
306 printf( " cmpl r0,$" );
307 printf( CONFMT, p[i].sval );
308 printf( "\n jeql " LBLFMT "\n", p[i].slab );
309 }
310
311 if( p->slab>=0 ) branch( p->slab );
312 }
313
314 makeheap(register struct sw *p, int m, int n)
315 {
316 register int q;
317
318 q = select(m);
319 heapsw[n] = p[q];
320 if( q>1 ) makeheap(p, q-1, 2*n);
321 if( q<m ) makeheap(p+q, m-q, 2*n+1);
322 }
323
324 select(int m)
325 {
326 register int l,i,k;
327
328 for(i=1; ; i*=2)
329 if( (i-1) > m ) break;
330 l = ((k = i/2 - 1) + 1)/2;
331 return( l + (m-k < l ? m-k : l));
332 }
333
334 walkheap(int start, int limit)
335 {
336 int label;
337
338 if( start > limit ) return;
339 printf(" cmpl r0,$%d\n", heapsw[start].sval);
340 printf(" jeql " LBLFMT "\n", heapsw[start].slab);
341 if( (2*start) > limit ) {
342 printf(" jbr " LBLFMT "\n", heapsw[0].slab);
343 return;
344 }
345 if( (2*start+1) <= limit ) {
346 label = getlab();
347 printf(" jgtr " LBLFMT "\n", label);
348 } else
349 printf(" jgtr " LBLFMT "\n", heapsw[0].slab);
350 walkheap( 2*start, limit);
351 if( (2*start+1) <= limit ) {
352 deflab1(label);
353 walkheap( 2*start+1, limit);
354 }
355 }
356 #endif
357
358 /*
359 * Called with a function call with arguments as argument.
360 * This is done early in buildtree() and only done once.
361 */
362 NODE *
363 funcode(NODE *p)
364 {
365 NODE *r, *l;
366
367 /* Fix function call arguments. On vax, just add funarg */
368 for (r = p->n_right; r->n_op == CM; r = r->n_left) {
369 if (r->n_right->n_op != STARG) {
370 r->n_right = intprom(r->n_right);
371 r->n_right = block(FUNARG, r->n_right, NIL,
372 r->n_right->n_type, r->n_right->n_df,
373 r->n_right->n_ap);
374 }
375 }
376 if (r->n_op != STARG) {
377 l = talloc();
378 *l = *r;
379 r->n_op = FUNARG;
380 r->n_left = l;
381 r->n_left = intprom(r->n_left);
382 r->n_type = r->n_left->n_type;
383 }
384 return p;
385 }
386
387 /*
388 * Generate the builtin code for FFS.
389 */
390 NODE *
391 builtin_ffs(const struct bitable *bt, NODE *a)
392 {
393 NODE *p, *q, *r;
394
395 p = tempnode(0, bt->rt, 0, 0);
396 r = block(XARG, ccopy(p), NIL, INT, 0, 0);
397 r->n_name = "=&r";
398 q = block(XARG, a, NIL, INT, 0, 0);
399 q->n_name = "g";
400 q = block(CM, r, q, INT, 0, 0);
401 q = block(XASM, q, block(ICON, 0, 0, STRTY, 0, 0), INT, 0, 0);
402 q->n_name = "ffs $0,$32,%1,%0;bneq 1f;mnegl $1,%0;1:;incl %0";
403 p = block(COMOP, q, p, bt->rt, 0, 0);
404 return p;
405 }
406
407 NODE *
408 builtin_ffsl(const struct bitable *bt, NODE *a)
409 {
410 return builtin_ffs(bt, a);
411 }
412
413 NODE *
414 builtin_ffsll(const struct bitable *bt, NODE *a)
415 {
416 cerror("builtin_ffsll unimplemented");
417 return NIL;
418 }
419
420 NODE *
421 builtin_return_address(const struct bitable *bt, NODE *a)
422 {
423 NODE *f;
424 int v;
425
426 if (a->n_op != ICON)
427 goto bad;
428 v = glval(a);
429 tfree(a);
430
431 if (v != 0) {
432 werror("unsupported argument");
433 return xbcon(0, NULL, VOID|PTR);
434 }
435
436 f = block(REG, NIL, NIL, INCREF(PTR+CHAR), 0, 0);
437 regno(f) = FPREG;
438 f = block(UMUL,
439 block(PLUS, f,
440 bcon(16), INCREF(PTR+CHAR), 0, 0), NIL, PTR+CHAR, 0, 0);
441 f = makety(f, PTR+VOID, 0, 0, 0);
442
443 return f;
444 bad:
445 uerror("bad argument to __builtin_return_address");
446 return bcon(0);
447 }
448
449 NODE *
450 builtin_frame_address(const struct bitable *bt, NODE *a)
451 {
452 int nframes;
453 NODE *f;
454
455 if (a->n_op != ICON)
456 goto bad;
457
458 nframes = glval(a);
459
460 tfree(a);
461
462 f = block(REG, NIL, NIL, PTR+CHAR, 0, 0);
463 regno(f) = FPREG;
464
465 while (nframes--) {
466 f = block(UMUL,
467 block(PLUS, f,
468 bcon(12), INCREF(PTR+CHAR), 0, 0),
469 NIL, PTR+CHAR, 0, 0);
470 f = makety(f, PTR+CHAR, 0, 0, 0);
471 }
472
473 return f;
474 bad:
475 uerror("bad argument to __builtin_frame_address");
476 return bcon(0);
477 }
478
479 /*
480 * Return "canonical frame address".
481 */
482 NODE *
483 builtin_cfa(const struct bitable *bt, NODE *a)
484 {
485 uerror("missing builtin_cfa");
486 return bcon(0);
487 }
488