1 /*	$Id: code.c,v 1.28 2012/06/06 09:28:01 ragge Exp $	*/
2 /*	$OpenBSD: code.c,v 1.2 2007/11/22 15:06:43 stefan Exp $	*/
3 
4 /*
5  * Copyright (c) 2007 Michael Shalayeff
6  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 # include "pass1.h"
34 
35 NODE *funarg(NODE *, int *);
36 int argreg(TWORD, int *);
37 
38 static const char *const loctbl[] = { "text", "data", "section .rodata" };
39 
40 /*
41  * Define everything needed to print out some data (or text).
42  * This means segment, alignment, visibility, etc.
43  */
44 void
defloc(struct symtab * sp)45 defloc(struct symtab *sp)
46 {
47 	extern char *nextsect;
48 	static int lastloc = -1;
49 	TWORD t;
50 	char *n;
51 	int s;
52 
53 	if (sp == NULL) {
54 		lastloc = -1;
55 		return;
56 	}
57 	t = sp->stype;
58 	s = ISFTN(t) ? PROG : ISCON(cqual(t, sp->squal)) ? RDATA : DATA;
59 	if (nextsect) {
60 		printf("\t.section %s\n", nextsect);
61 		nextsect = NULL;
62 		s = -1;
63 	} else if (s != lastloc)
64 		printf("\t.%s\n", loctbl[s]);
65 	lastloc = s;
66 	while (ISARY(t))
67 		t = DECREF(t);
68 	s = ISFTN(t) ? ALINT : talign(t, sp->ssue);
69 	if (s > ALCHAR)
70 		printf("\t.align\t%d\n", s / ALCHAR);
71 	n = sp->soname ? sp->soname : sp->sname;
72 	if (sp->sclass == EXTDEF)
73 		printf("\t.export %s, %s\n", n,
74 		    ISFTN(t)? "code" : "data");
75 	if (sp->slevel == 0)
76 		printf("\t.type\t%s, @%s\n\t.label %s\n",
77 		    n, ISFTN(t)? "function" : "object", n);
78 	else
79 		printf("\t.type\t" LABFMT ", @%s\n\t.label\t" LABFMT "\n",
80 		    sp->soffset, ISFTN(t)? "function" : "object", sp->soffset);
81 }
82 
83 /*
84  * code for the end of a function
85  * deals with struct return here
86  */
87 void
efcode(void)88 efcode(void)
89 {
90 	NODE *p, *q;
91 	int sz;
92 
93 	if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
94 		return;
95 	/* address of return struct is in %ret0 */
96 	/* create a call to memcpy() */
97 	/* will get the result in %ret0 */
98 	p = block(REG, NIL, NIL, CHAR+PTR, 0, 0);
99 	p->n_rval = RET0;
100 	q = block(OREG, NIL, NIL, CHAR+PTR, 0, 0);
101 	q->n_rval = FP;
102 	q->n_lval = 8; /* return buffer offset */
103 	p = block(CM, q, p, INT, 0, 0);
104 	sz = (tsize(STRTY, cftnsp->sdf, cftnsp->ssue)+SZCHAR-1)/SZCHAR;
105 	p = block(CM, p, bcon(sz), INT, 0, 0);
106 	p->n_right->n_name = "";
107 	p = block(CALL, bcon(0), p, CHAR+PTR, 0, 0);
108 	p->n_left->n_name = "memcpy";
109 	p = clocal(p);
110 	send_passt(IP_NODE, p);
111 }
112 
113 int
argreg(TWORD t,int * n)114 argreg(TWORD t, int *n)
115 {
116 	switch (t) {
117 	case FLOAT:
118 		return FR7L - 2 * (*n)++;
119 	case DOUBLE:
120 	case LDOUBLE:
121 		*n += 2;
122 		return FR6 - *n - 2;
123 	case LONGLONG:
124 	case ULONGLONG:
125 		*n += 2;
126 		return AD1 - (*n - 2) / 2;
127 	default:
128 		return ARG0 - (*n)++;
129 	}
130 }
131 
132 /*
133  * code for the beginning of a function; 'a' is an array of
134  * indices in symtab for the arguments; n is the number
135  */
136 void
bfcode(struct symtab ** a,int cnt)137 bfcode(struct symtab **a, int cnt)
138 {
139 	struct symtab *sp;
140 	NODE *p, *q;
141 	int i, n, sz;
142 
143 	if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
144 		/* Function returns struct, adjust arg offset */
145 		for (i = 0; i < cnt; i++)
146 			a[i]->soffset += SZPOINT(LONG);
147 	}
148 
149 	/* recalculate the arg offset and create TEMP moves */
150 	for (n = 0, i = 0; i < cnt; i++) {
151 		sp = a[i];
152 
153 		sz = szty(sp->stype);
154 		if (n % sz)
155 			n++;	/* XXX LDOUBLE */
156 
157 		if (n < 4) {
158 			p = tempnode(0, sp->stype, sp->sdf, sp->ssue);
159 			/* TODO p->n_left->n_lval = -(32 + n * 4); */
160 			q = block(REG, NIL, NIL, sp->stype, sp->sdf, sp->ssue);
161 			q->n_rval = argreg(sp->stype, &n);
162 			p = buildtree(ASSIGN, p, q);
163 			sp->soffset = regno(p->n_left);
164 			sp->sflags |= STNODE;
165 			ecomp(p);
166 		} else {
167 			sp->soffset += SZINT * n;
168 			if (xtemps) {
169 				/* put stack args in temps if optimizing */
170 				p = tempnode(0, sp->stype, sp->sdf, sp->ssue);
171 				p = buildtree(ASSIGN, p, buildtree(NAME, 0, 0));
172 				sp->soffset = regno(p->n_left);
173 				sp->sflags |= STNODE;
174 				ecomp(p);
175 			}
176 		}
177 	}
178 }
179 
180 
181 /* called just before final exit */
182 /* flag is 1 if errors, 0 if none */
183 void
ejobcode(int flag)184 ejobcode(int flag)
185 {
186 	if (flag)
187 		return;
188 
189 	printf("\t.end\n");
190 }
191 
192 void
bjobcode(void)193 bjobcode(void)
194 {
195 	printf("\t.level\t1.1\n"
196 	    "\t.import $global$, data\n"
197 	    "\t.import $$dyncall, millicode\n");
198 }
199 
200 /* fix up type of field p */
201 void
fldty(struct symtab * p)202 fldty(struct symtab *p)
203 {
204 }
205 
206 /*
207  * XXX - fix genswitch.
208  */
209 int
mygenswitch(int num,TWORD type,struct swents ** p,int n)210 mygenswitch(int num, TWORD type, struct swents **p, int n)
211 {
212 	return 0;
213 }
214 
215 NODE *
funarg(NODE * p,int * n)216 funarg(NODE *p, int *n)
217 {
218 	NODE *r;
219 	int sz;
220 
221 	if (p->n_op == CM) {
222 		p->n_left = funarg(p->n_left, n);
223 		p->n_right = funarg(p->n_right, n);
224 		return p;
225 	}
226 
227 	sz = szty(p->n_type);
228 	if (*n % sz)
229 		(*n)++;	/* XXX LDOUBLE */
230 
231 	if (*n >= 4) {
232 		*n += sz;
233 		r = block(OREG, NIL, NIL, p->n_type|PTR, 0, 0);
234 		r->n_rval = SP;
235 		r->n_lval = -(32 + *n * 4);
236 	} else {
237 		r = block(REG, NIL, NIL, p->n_type, 0, 0);
238 		r->n_lval = 0;
239 		r->n_rval = argreg(p->n_type, n);
240 	}
241 	p = block(ASSIGN, r, p, p->n_type, 0, 0);
242 	clocal(p);
243 
244 	return p;
245 }
246 
247 /*
248  * Called with a function call with arguments as argument.
249  * This is done early in buildtree() and only done once.
250  */
251 NODE *
funcode(NODE * p)252 funcode(NODE *p)
253 {
254 	int n = 0;
255 
256 	p->n_right = funarg(p->n_right, &n);
257 	return p;
258 }
259