1 | /* $Id: code.c,v 1.39 2008/07/29 13:25:58 ragge Exp $ */
|
---|
2 | /*
|
---|
3 | * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
|
---|
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 | * Define everything needed to print out some data (or text).
|
---|
34 | * This means segment, alignment, visibility, etc.
|
---|
35 | */
|
---|
36 | void
|
---|
37 | defloc(struct symtab *sp)
|
---|
38 | {
|
---|
39 | char *nextsect = NULL; /* notyet */
|
---|
40 | static char *loctbl[] = { "text", "data", "section .rodata" };
|
---|
41 | static int lastloc = -1;
|
---|
42 | TWORD t;
|
---|
43 | int s;
|
---|
44 |
|
---|
45 | if (sp == NULL) {
|
---|
46 | lastloc = -1;
|
---|
47 | return;
|
---|
48 | }
|
---|
49 | t = sp->stype;
|
---|
50 | s = ISFTN(t) ? PROG : ISCON(cqual(t, sp->squal)) ? RDATA : DATA;
|
---|
51 | if (nextsect) {
|
---|
52 | printf(" .section %s\n", nextsect);
|
---|
53 | nextsect = NULL;
|
---|
54 | s = -1;
|
---|
55 | } else if (s != lastloc)
|
---|
56 | printf(" .%s\n", loctbl[s]);
|
---|
57 | lastloc = s;
|
---|
58 | if (sp->sclass == EXTDEF)
|
---|
59 | printf(" .globl %s\n", sp->soname);
|
---|
60 | if (sp->slevel == 0)
|
---|
61 | printf("%s:\n", sp->soname);
|
---|
62 | else
|
---|
63 | printf(LABFMT ":\n", sp->soffset);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * code for the end of a function
|
---|
68 | */
|
---|
69 | void
|
---|
70 | efcode()
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * code for the beginning of a function; a is an array of
|
---|
76 | * indices in stab for the arguments; n is the number
|
---|
77 | */
|
---|
78 | void
|
---|
79 | bfcode(struct symtab **sp, int cnt)
|
---|
80 | {
|
---|
81 | NODE *p, *q;
|
---|
82 | int i, n;
|
---|
83 |
|
---|
84 | if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
|
---|
85 | uerror("no struct return yet");
|
---|
86 | }
|
---|
87 | /* recalculate the arg offset and create TEMP moves */
|
---|
88 | for (n = 1, i = 0; i < cnt; i++) {
|
---|
89 | if (n < 8) {
|
---|
90 | p = tempnode(0, sp[i]->stype, sp[i]->sdf, sp[i]->ssue);
|
---|
91 | q = block(REG, NIL, NIL,
|
---|
92 | sp[i]->stype, sp[i]->sdf, sp[i]->ssue);
|
---|
93 | q->n_rval = n;
|
---|
94 | p = buildtree(ASSIGN, p, q);
|
---|
95 | sp[i]->soffset = regno(p->n_left);
|
---|
96 | sp[i]->sflags |= STNODE;
|
---|
97 | ecomp(p);
|
---|
98 | } else {
|
---|
99 | sp[i]->soffset += SZINT * n;
|
---|
100 | if (xtemps) {
|
---|
101 | /* put stack args in temps if optimizing */
|
---|
102 | p = tempnode(0, sp[i]->stype,
|
---|
103 | sp[i]->sdf, sp[i]->ssue);
|
---|
104 | p = buildtree(ASSIGN, p, nametree(sp[i]));
|
---|
105 | sp[i]->soffset = regno(p->n_left);
|
---|
106 | sp[i]->sflags |= STNODE;
|
---|
107 | ecomp(p);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | n += szty(sp[i]->stype);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * by now, the automatics and register variables are allocated
|
---|
117 | */
|
---|
118 | void
|
---|
119 | bccode()
|
---|
120 | {
|
---|
121 | SETOFF(autooff, SZINT);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void
|
---|
125 | bjobcode()
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* called just before final exit */
|
---|
130 | /* flag is 1 if errors, 0 if none */
|
---|
131 | void
|
---|
132 | ejobcode(int flag )
|
---|
133 | {
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Make a register node, helper for funcode.
|
---|
138 | */
|
---|
139 | static NODE *
|
---|
140 | mkreg(NODE *p, int n)
|
---|
141 | {
|
---|
142 | NODE *r;
|
---|
143 |
|
---|
144 | r = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_sue);
|
---|
145 | if (szty(p->n_type) == 2)
|
---|
146 | n += 16;
|
---|
147 | r->n_rval = n;
|
---|
148 | return r;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static int regnum;
|
---|
152 | /*
|
---|
153 | * Move args to registers and emit expressions bottom-up.
|
---|
154 | */
|
---|
155 | static void
|
---|
156 | fixargs(NODE *p)
|
---|
157 | {
|
---|
158 | NODE *r;
|
---|
159 |
|
---|
160 | if (p->n_op == CM) {
|
---|
161 | fixargs(p->n_left);
|
---|
162 | r = p->n_right;
|
---|
163 | if (r->n_op == STARG)
|
---|
164 | regnum = 9; /* end of register list */
|
---|
165 | else if (regnum + szty(r->n_type) > 8)
|
---|
166 | p->n_right = block(FUNARG, r, NIL, r->n_type,
|
---|
167 | r->n_df, r->n_sue);
|
---|
168 | else
|
---|
169 | p->n_right = buildtree(ASSIGN, mkreg(r, regnum), r);
|
---|
170 | } else {
|
---|
171 | if (p->n_op == STARG) {
|
---|
172 | regnum = 9; /* end of register list */
|
---|
173 | } else {
|
---|
174 | r = talloc();
|
---|
175 | *r = *p;
|
---|
176 | r = buildtree(ASSIGN, mkreg(r, regnum), r);
|
---|
177 | *p = *r;
|
---|
178 | nfree(r);
|
---|
179 | }
|
---|
180 | r = p;
|
---|
181 | }
|
---|
182 | regnum += szty(r->n_type);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Called with a function call with arguments as argument.
|
---|
188 | * This is done early in buildtree() and only done once.
|
---|
189 | */
|
---|
190 | NODE *
|
---|
191 | funcode(NODE *p)
|
---|
192 | {
|
---|
193 |
|
---|
194 | regnum = 1;
|
---|
195 |
|
---|
196 | fixargs(p->n_right);
|
---|
197 | return p;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * return the alignment of field of type t
|
---|
202 | */
|
---|
203 | int
|
---|
204 | fldal(unsigned int t)
|
---|
205 | {
|
---|
206 | uerror("illegal field type");
|
---|
207 | return(ALINT);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* fix up type of field p */
|
---|
211 | void
|
---|
212 | fldty(struct symtab *p)
|
---|
213 | {
|
---|
214 | }
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * XXX - fix genswitch.
|
---|
218 | */
|
---|
219 | int
|
---|
220 | mygenswitch(int num, TWORD type, struct swents **p, int n)
|
---|
221 | {
|
---|
222 | return 0;
|
---|
223 | }
|
---|