1 | /* $Id: code.c,v 1.5 2008/01/01 17:31:00 ragge Exp $ */
|
---|
2 | /*
|
---|
3 | * Copyright (c) 2006 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 | * cause the alignment to become a multiple of n
|
---|
34 | * never called for text segment.
|
---|
35 | */
|
---|
36 | void
|
---|
37 | defalign(int n)
|
---|
38 | {
|
---|
39 | /* alignment are always correct */
|
---|
40 | }
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * define the current location as the name p->soname
|
---|
44 | * never called for text segment.
|
---|
45 | */
|
---|
46 | void
|
---|
47 | defnam(struct symtab *p)
|
---|
48 | {
|
---|
49 | char *c = p->soname;
|
---|
50 |
|
---|
51 | if (p->sclass == EXTDEF)
|
---|
52 | printf(" .globl %s\n", c);
|
---|
53 | printf("%s:\n", c);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * code for the end of a function
|
---|
59 | * deals with struct return here
|
---|
60 | */
|
---|
61 | void
|
---|
62 | efcode()
|
---|
63 | {
|
---|
64 | NODE *p, *q;
|
---|
65 | int sz;
|
---|
66 |
|
---|
67 | if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
|
---|
68 | return;
|
---|
69 | cerror("efcode");
|
---|
70 | /* address of return struct is in eax */
|
---|
71 | /* create a call to memcpy() */
|
---|
72 | /* will get the result in eax */
|
---|
73 | p = block(REG, NIL, NIL, CHAR+PTR, 0, MKSUE(CHAR+PTR));
|
---|
74 | // p->n_rval = EAX;
|
---|
75 | q = block(OREG, NIL, NIL, CHAR+PTR, 0, MKSUE(CHAR+PTR));
|
---|
76 | // q->n_rval = EBP;
|
---|
77 | q->n_lval = 8; /* return buffer offset */
|
---|
78 | p = block(CM, q, p, INT, 0, MKSUE(INT));
|
---|
79 | sz = (tsize(STRTY, cftnsp->sdf, cftnsp->ssue)+SZCHAR-1)/SZCHAR;
|
---|
80 | p = block(CM, p, bcon(sz), INT, 0, MKSUE(INT));
|
---|
81 | p->n_right->n_name = "";
|
---|
82 | p = block(CALL, bcon(0), p, CHAR+PTR, 0, MKSUE(CHAR+PTR));
|
---|
83 | p->n_left->n_name = "memcpy";
|
---|
84 | p = clocal(p);
|
---|
85 | send_passt(IP_NODE, p);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * code for the beginning of a function; a is an array of
|
---|
90 | * indices in symtab for the arguments; n is the number
|
---|
91 | */
|
---|
92 | void
|
---|
93 | bfcode(struct symtab **a, int n)
|
---|
94 | {
|
---|
95 | int i;
|
---|
96 |
|
---|
97 | if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
|
---|
98 | return;
|
---|
99 | cerror("bfcode");
|
---|
100 | /* Function returns struct, adjust arg offset */
|
---|
101 | for (i = 0; i < n; i++)
|
---|
102 | a[i]->soffset += SZPOINT(INT);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * by now, the automatics and register variables are allocated
|
---|
108 | */
|
---|
109 | void
|
---|
110 | bccode()
|
---|
111 | {
|
---|
112 | SETOFF(autooff, SZINT);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* called just before final exit */
|
---|
116 | /* flag is 1 if errors, 0 if none */
|
---|
117 | void
|
---|
118 | ejobcode(int flag )
|
---|
119 | {
|
---|
120 | }
|
---|
121 |
|
---|
122 | void
|
---|
123 | bjobcode()
|
---|
124 | {
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Print character t at position i in one string, until t == -1.
|
---|
129 | * Locctr & label is already defined.
|
---|
130 | */
|
---|
131 | void
|
---|
132 | bycode(int t, int i)
|
---|
133 | {
|
---|
134 | static int lastoctal = 0;
|
---|
135 |
|
---|
136 | /* put byte i+1 in a string */
|
---|
137 |
|
---|
138 | if (t < 0) {
|
---|
139 | if (i != 0)
|
---|
140 | puts("\"");
|
---|
141 | } else {
|
---|
142 | if (i == 0)
|
---|
143 | printf("\t.ascii \"");
|
---|
144 | if (t == '\\' || t == '"') {
|
---|
145 | lastoctal = 0;
|
---|
146 | putchar('\\');
|
---|
147 | putchar(t);
|
---|
148 | } else if (t < 040 || t >= 0177) {
|
---|
149 | lastoctal++;
|
---|
150 | printf("\\%o",t);
|
---|
151 | } else if (lastoctal && '0' <= t && t <= '9') {
|
---|
152 | lastoctal = 0;
|
---|
153 | printf("\"\n\t.ascii \"%c", t);
|
---|
154 | } else {
|
---|
155 | lastoctal = 0;
|
---|
156 | putchar(t);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * return the alignment of field of type t
|
---|
163 | */
|
---|
164 | int
|
---|
165 | fldal(unsigned int t)
|
---|
166 | {
|
---|
167 | uerror("illegal field type");
|
---|
168 | return(ALINT);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* fix up type of field p */
|
---|
172 | void
|
---|
173 | fldty(struct symtab *p)
|
---|
174 | {
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * XXX - fix genswitch.
|
---|
179 | */
|
---|
180 | int
|
---|
181 | mygenswitch(int num, TWORD type, struct swents **p, int n)
|
---|
182 | {
|
---|
183 | return 0;
|
---|
184 | }
|
---|
185 | /*
|
---|
186 | * Called with a function call with arguments as argument.
|
---|
187 | * This is done early in buildtree() and only done once.
|
---|
188 | */
|
---|
189 | NODE *
|
---|
190 | funcode(NODE *p)
|
---|
191 | {
|
---|
192 | return p;
|
---|
193 | }
|
---|