1 |
48 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2012-2018 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// CC64 - 'C' derived language compiler
|
9 |
|
|
// - 64 bit CPU
|
10 |
|
|
//
|
11 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
12 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
13 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
14 |
|
|
// (at your option) any later version.
|
15 |
|
|
//
|
16 |
|
|
// This source file is distributed in the hope that it will be useful,
|
17 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
// GNU General Public License for more details.
|
20 |
|
|
//
|
21 |
|
|
// You should have received a copy of the GNU General Public License
|
22 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
23 |
|
|
//
|
24 |
|
|
// ============================================================================
|
25 |
|
|
//
|
26 |
|
|
#include "stdafx.h"
|
27 |
|
|
|
28 |
|
|
// ----------------------------------------------------------------------------
|
29 |
|
|
// forcefit will coerce the nodes passed into compatible
|
30 |
|
|
// types and return the type of the resulting expression.
|
31 |
|
|
//
|
32 |
|
|
// Generally if the promote flag is set then the whichever type is larger
|
33 |
|
|
// is returned. Code is emitted to convert the shorter type to the longer type.
|
34 |
|
|
// ----------------------------------------------------------------------------
|
35 |
|
|
TYP *forcefit(ENODE **srcnode, TYP *srctp, ENODE **dstnode, TYP *dsttp, bool promote)
|
36 |
|
|
{
|
37 |
|
|
ENODE *n2;
|
38 |
|
|
|
39 |
|
|
if (dstnode)
|
40 |
|
|
n2 = *dstnode;
|
41 |
|
|
else
|
42 |
|
|
n2 = (ENODE *)NULL;
|
43 |
|
|
switch (srctp->type) {
|
44 |
|
|
case bt_byte:
|
45 |
|
|
case bt_ubyte:
|
46 |
|
|
case bt_char:
|
47 |
|
|
case bt_uchar:
|
48 |
|
|
case bt_short:
|
49 |
|
|
case bt_ushort:
|
50 |
|
|
case bt_long:
|
51 |
|
|
case bt_ulong:
|
52 |
|
|
case bt_bitfield:
|
53 |
|
|
case bt_ubitfield:
|
54 |
|
|
case bt_exception:
|
55 |
|
|
switch (dsttp->type) {
|
56 |
|
|
case bt_long: return &stdlong;
|
57 |
|
|
case bt_ulong: return &stdulong;
|
58 |
|
|
case bt_short: return &stdlong;
|
59 |
|
|
case bt_ushort: return &stdulong;
|
60 |
|
|
case bt_char: return &stdlong;
|
61 |
|
|
case bt_uchar: return &stdulong;
|
62 |
|
|
case bt_byte: return &stdlong;
|
63 |
|
|
case bt_ubyte: return &stdulong;
|
64 |
|
|
case bt_enum: return &stdlong;
|
65 |
|
|
// If we have a pointer involved we likely want a pointer result.
|
66 |
|
|
case bt_pointer:return (dsttp);
|
67 |
|
|
case bt_float:
|
68 |
|
|
case bt_double: *srcnode = makenode(en_i2d, *srcnode, *dstnode); (*srcnode)->esize = 8; return (dsttp);
|
69 |
|
|
case bt_triple: *srcnode = makenode(en_i2t, *srcnode, *dstnode); (*srcnode)->esize = 12; return (dsttp);
|
70 |
|
|
case bt_quad: *srcnode = makenode(en_i2q, *srcnode, *dstnode); (*srcnode)->esize = 16; return (dsttp);
|
71 |
|
|
case bt_exception: return &stdexception;
|
72 |
|
|
case bt_vector: return (dsttp);
|
73 |
|
|
case bt_union: return (dsttp);
|
74 |
|
|
case bt_struct:
|
75 |
|
|
case bt_class:
|
76 |
|
|
error(ERR_MISMATCH);
|
77 |
|
|
return (dsttp);
|
78 |
|
|
}
|
79 |
|
|
return (srctp);
|
80 |
|
|
|
81 |
|
|
case bt_enum:
|
82 |
|
|
switch (dsttp->type) {
|
83 |
|
|
case bt_long: /**srcnode = makenode(en_ccw, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdlong;
|
84 |
|
|
case bt_ulong: /**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdulong;
|
85 |
|
|
case bt_short: /**srcnode = makenode(en_ccw, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdlong;
|
86 |
|
|
case bt_ushort: /**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdulong;
|
87 |
|
|
case bt_char: /**srcnode = makenode(en_ccw, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdlong;
|
88 |
|
|
case bt_uchar: /**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdulong;
|
89 |
|
|
case bt_byte: /**srcnode = makenode(en_ccw, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdlong;
|
90 |
|
|
case bt_ubyte: /**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdulong;
|
91 |
|
|
case bt_enum: /**srcnode = makenode(en_ccw, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdlong;
|
92 |
|
|
case bt_pointer:/**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return dsttp;
|
93 |
|
|
case bt_exception: /**srcnode = makenode(en_ccu, *srcnode, *dstnode);*/ (*srcnode)->esize = 8; return &stdexception;
|
94 |
|
|
case bt_float:
|
95 |
|
|
case bt_double: *srcnode = makenode(en_i2d, *srcnode, *dstnode); (*srcnode)->esize = 8; return (dsttp);
|
96 |
|
|
case bt_triple: *srcnode = makenode(en_i2t, *srcnode, *dstnode); (*srcnode)->esize = 12; return (dsttp);
|
97 |
|
|
case bt_quad: *srcnode = makenode(en_i2q, *srcnode, *dstnode); (*srcnode)->esize = 16; return (dsttp);
|
98 |
|
|
case bt_vector: return (dsttp);
|
99 |
|
|
case bt_union: return (dsttp);
|
100 |
|
|
case bt_struct:
|
101 |
|
|
case bt_class:
|
102 |
|
|
error(ERR_MISMATCH);
|
103 |
|
|
return (dsttp);
|
104 |
|
|
}
|
105 |
|
|
return srctp;
|
106 |
|
|
|
107 |
|
|
case bt_pointer:
|
108 |
|
|
switch (dsttp->type)
|
109 |
|
|
{
|
110 |
|
|
case bt_byte:
|
111 |
|
|
case bt_ubyte:
|
112 |
|
|
case bt_char:
|
113 |
|
|
case bt_uchar:
|
114 |
|
|
case bt_short:
|
115 |
|
|
case bt_ushort:
|
116 |
|
|
case bt_long:
|
117 |
|
|
case bt_ulong:
|
118 |
|
|
case bt_pointer: return (srctp);
|
119 |
|
|
// pointer to function was really desired.
|
120 |
|
|
case bt_func:
|
121 |
|
|
case bt_ifunc:
|
122 |
|
|
return (srctp);
|
123 |
|
|
case bt_struct:
|
124 |
|
|
if ((*dstnode)->nodetype == en_list || (*dstnode)->nodetype == en_aggregate)
|
125 |
|
|
return (srctp);
|
126 |
|
|
break;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
case bt_float:
|
130 |
|
|
switch (dsttp->type) {
|
131 |
|
|
case bt_byte:
|
132 |
|
|
case bt_ubyte:
|
133 |
|
|
case bt_char:
|
134 |
|
|
case bt_uchar:
|
135 |
|
|
case bt_short:
|
136 |
|
|
case bt_ushort:
|
137 |
|
|
case bt_long:
|
138 |
|
|
case bt_ulong:
|
139 |
|
|
case bt_exception:
|
140 |
|
|
case bt_enum: *srcnode = makenode(en_i2d, *srcnode, *dstnode); return (dsttp);
|
141 |
|
|
case bt_pointer: return(dsttp);
|
142 |
|
|
case bt_double: return (dsttp);
|
143 |
|
|
case bt_triple: *srcnode = makenode(en_d2t, *srcnode, *dstnode); return (dsttp);
|
144 |
|
|
case bt_quad: *srcnode = makenode(en_d2q, *srcnode, *dstnode); return (dsttp);
|
145 |
|
|
case bt_vector: return (dsttp);
|
146 |
|
|
case bt_union: return (dsttp);
|
147 |
|
|
case bt_struct:
|
148 |
|
|
case bt_class:
|
149 |
|
|
error(ERR_MISMATCH);
|
150 |
|
|
return (dsttp);
|
151 |
|
|
}
|
152 |
|
|
return srctp;
|
153 |
|
|
|
154 |
|
|
case bt_double:
|
155 |
|
|
switch (dsttp->type) {
|
156 |
|
|
case bt_byte:
|
157 |
|
|
case bt_ubyte:
|
158 |
|
|
case bt_char:
|
159 |
|
|
case bt_uchar:
|
160 |
|
|
case bt_short:
|
161 |
|
|
case bt_ushort:
|
162 |
|
|
case bt_long:
|
163 |
|
|
case bt_ulong:
|
164 |
|
|
case bt_exception:
|
165 |
|
|
case bt_enum: *dstnode = makenode(en_i2d, *dstnode, *srcnode); return (srctp);
|
166 |
|
|
case bt_pointer: return(dsttp);
|
167 |
|
|
case bt_double: return (dsttp);
|
168 |
|
|
case bt_triple: *srcnode = makenode(en_d2t, *srcnode, *dstnode); return (dsttp);
|
169 |
|
|
case bt_quad: *srcnode = makenode(en_d2q, *srcnode, *dstnode); return (dsttp);
|
170 |
|
|
case bt_vector: return (dsttp);
|
171 |
|
|
case bt_union: return (dsttp);
|
172 |
|
|
case bt_struct:
|
173 |
|
|
case bt_class:
|
174 |
|
|
error(ERR_MISMATCH);
|
175 |
|
|
return (dsttp);
|
176 |
|
|
}
|
177 |
|
|
return srctp;
|
178 |
|
|
|
179 |
|
|
case bt_triple:
|
180 |
|
|
switch (dsttp->type) {
|
181 |
|
|
case bt_byte:
|
182 |
|
|
case bt_ubyte:
|
183 |
|
|
case bt_char:
|
184 |
|
|
case bt_uchar:
|
185 |
|
|
case bt_short:
|
186 |
|
|
case bt_ushort:
|
187 |
|
|
case bt_long:
|
188 |
|
|
case bt_ulong:
|
189 |
|
|
case bt_exception:
|
190 |
|
|
case bt_enum: *dstnode = makenode(en_i2t, *dstnode, *srcnode); return (srctp);
|
191 |
|
|
case bt_pointer: return(dsttp);
|
192 |
|
|
case bt_double: *dstnode = makenode(en_d2t, *dstnode, *srcnode); return (srctp);
|
193 |
|
|
case bt_triple: return (dsttp);
|
194 |
|
|
case bt_quad: *srcnode = makenode(en_t2q, *srcnode, *dstnode); return (dsttp);
|
195 |
|
|
case bt_vector: return (dsttp);
|
196 |
|
|
case bt_union: return (dsttp);
|
197 |
|
|
case bt_struct:
|
198 |
|
|
case bt_class:
|
199 |
|
|
error(ERR_MISMATCH);
|
200 |
|
|
return (dsttp);
|
201 |
|
|
}
|
202 |
|
|
return srctp;
|
203 |
|
|
|
204 |
|
|
case bt_quad:
|
205 |
|
|
switch (dsttp->type) {
|
206 |
|
|
case bt_byte:
|
207 |
|
|
case bt_ubyte:
|
208 |
|
|
case bt_char:
|
209 |
|
|
case bt_uchar:
|
210 |
|
|
case bt_short:
|
211 |
|
|
case bt_ushort:
|
212 |
|
|
case bt_long:
|
213 |
|
|
case bt_ulong:
|
214 |
|
|
case bt_exception:
|
215 |
|
|
case bt_enum: *dstnode = makenode(en_i2t, *dstnode, *srcnode); return (srctp);
|
216 |
|
|
case bt_pointer: return(dsttp);
|
217 |
|
|
case bt_double: *dstnode = makenode(en_d2q, *dstnode, *srcnode); return (srctp);
|
218 |
|
|
case bt_triple: *dstnode = makenode(en_t2q, *dstnode, *srcnode); return (srctp);
|
219 |
|
|
case bt_quad: return (dsttp);
|
220 |
|
|
case bt_vector: return (dsttp);
|
221 |
|
|
case bt_union: return (dsttp);
|
222 |
|
|
case bt_struct:
|
223 |
|
|
case bt_class:
|
224 |
|
|
error(ERR_MISMATCH);
|
225 |
|
|
return (dsttp);
|
226 |
|
|
}
|
227 |
|
|
return srctp;
|
228 |
|
|
|
229 |
|
|
case bt_class:
|
230 |
|
|
case bt_struct:
|
231 |
|
|
case bt_union:
|
232 |
|
|
if (dsttp->size > srctp->size)
|
233 |
|
|
return dsttp;
|
234 |
|
|
return srctp;
|
235 |
|
|
// Really working with pointers to functions.
|
236 |
|
|
case bt_func:
|
237 |
|
|
case bt_ifunc:
|
238 |
|
|
return srctp;
|
239 |
|
|
}
|
240 |
|
|
error(ERR_MISMATCH);
|
241 |
|
|
return srctp;
|
242 |
|
|
}
|
243 |
|
|
|