1 | // Copyright 2019 The CC Authors. All rights reserved.
|
---|
2 | // Use of this source code is governed by a BSD-style
|
---|
3 | // license that can be found in the LICENSE file.
|
---|
4 |
|
---|
5 | package cc // import "modernc.org/cc/v3"
|
---|
6 |
|
---|
7 | import (
|
---|
8 | "fmt"
|
---|
9 | "math"
|
---|
10 | "math/big"
|
---|
11 | )
|
---|
12 |
|
---|
13 | var (
|
---|
14 | _ Value = (*Float128Value)(nil)
|
---|
15 | _ Value = (*InitializerValue)(nil)
|
---|
16 | _ Value = Complex128Value(0)
|
---|
17 | _ Value = Complex256Value{}
|
---|
18 | _ Value = Complex64Value(0)
|
---|
19 | _ Value = Float32Value(0)
|
---|
20 | _ Value = Float64Value(0)
|
---|
21 | _ Value = Int64Value(0)
|
---|
22 | _ Value = StringValue(0)
|
---|
23 | _ Value = Uint64Value(0)
|
---|
24 | _ Value = WideStringValue(0)
|
---|
25 |
|
---|
26 | _ Operand = (*funcDesignator)(nil)
|
---|
27 | _ Operand = (*lvalue)(nil)
|
---|
28 | _ Operand = (*operand)(nil)
|
---|
29 | _ Operand = noOperand
|
---|
30 |
|
---|
31 | noOperand = &operand{typ: noType}
|
---|
32 | )
|
---|
33 |
|
---|
34 | type Operand interface {
|
---|
35 | // IsAssingmentCompatible reports whether the operand can be
|
---|
36 | // assigned to lhs. [0], 6.5.16.1.
|
---|
37 | IsAssingmentCompatible(lhs Type) bool
|
---|
38 | ConvertTo(Type) Operand
|
---|
39 | Declarator() *Declarator
|
---|
40 | IsConst() bool
|
---|
41 | IsLValue() bool
|
---|
42 | IsNonZero() bool
|
---|
43 | IsZero() bool
|
---|
44 | Offset() uintptr // Valid only for non nil Declarator() value
|
---|
45 | Type() Type
|
---|
46 | Value() Value
|
---|
47 | convertFromInt(*context, Node, Type) Operand
|
---|
48 | convertTo(*context, Node, Type) Operand
|
---|
49 | convertToInt(*context, Node, Type) Operand
|
---|
50 | getABI() *ABI
|
---|
51 | integerPromotion(*context, Node) Operand
|
---|
52 | normalize(*context, Node) Operand
|
---|
53 | }
|
---|
54 |
|
---|
55 | type Value interface {
|
---|
56 | IsConst() bool
|
---|
57 | IsNonZero() bool
|
---|
58 | IsZero() bool
|
---|
59 | add(b Value) Value
|
---|
60 | and(b Value) Value
|
---|
61 | cpl() Value
|
---|
62 | div(b Value) Value
|
---|
63 | eq(b Value) Value
|
---|
64 | ge(b Value) Value
|
---|
65 | gt(b Value) Value
|
---|
66 | le(b Value) Value
|
---|
67 | lsh(b Value) Value
|
---|
68 | lt(b Value) Value
|
---|
69 | mod(b Value) Value
|
---|
70 | mul(b Value) Value
|
---|
71 | neg() Value
|
---|
72 | neq(b Value) Value
|
---|
73 | or(b Value) Value
|
---|
74 | rsh(b Value) Value
|
---|
75 | sub(b Value) Value
|
---|
76 | xor(b Value) Value
|
---|
77 | }
|
---|
78 |
|
---|
79 | type WideStringValue StringID
|
---|
80 |
|
---|
81 | func (v WideStringValue) add(b Value) Value { panic(todo("")) }
|
---|
82 | func (v WideStringValue) and(b Value) Value { panic(todo("")) }
|
---|
83 | func (v WideStringValue) cpl() Value { panic(todo("")) }
|
---|
84 | func (v WideStringValue) div(b Value) Value { panic(todo("")) }
|
---|
85 | func (v WideStringValue) eq(b Value) Value { return boolValue(v == b.(WideStringValue)) }
|
---|
86 | func (v WideStringValue) IsConst() bool { return true }
|
---|
87 | func (v WideStringValue) IsNonZero() bool { return true }
|
---|
88 | func (v WideStringValue) IsZero() bool { return false }
|
---|
89 | func (v WideStringValue) lsh(b Value) Value { panic(todo("")) }
|
---|
90 | func (v WideStringValue) mod(b Value) Value { panic(todo("")) }
|
---|
91 | func (v WideStringValue) mul(b Value) Value { panic(todo("")) }
|
---|
92 | func (v WideStringValue) neg() Value { panic(todo("")) }
|
---|
93 | func (v WideStringValue) neq(b Value) Value { return boolValue(v != b.(WideStringValue)) }
|
---|
94 | func (v WideStringValue) or(b Value) Value { panic(todo("")) }
|
---|
95 | func (v WideStringValue) rsh(b Value) Value { panic(todo("")) }
|
---|
96 | func (v WideStringValue) sub(b Value) Value { panic(todo("")) }
|
---|
97 | func (v WideStringValue) xor(b Value) Value { panic(todo("")) }
|
---|
98 |
|
---|
99 | func (v WideStringValue) le(b Value) Value {
|
---|
100 | return boolValue(StringID(v).String() <= StringID(b.(WideStringValue)).String())
|
---|
101 | }
|
---|
102 |
|
---|
103 | func (v WideStringValue) ge(b Value) Value {
|
---|
104 | return boolValue(StringID(v).String() >= StringID(b.(WideStringValue)).String())
|
---|
105 | }
|
---|
106 |
|
---|
107 | func (v WideStringValue) gt(b Value) Value {
|
---|
108 | return boolValue(StringID(v).String() > StringID(b.(WideStringValue)).String())
|
---|
109 | }
|
---|
110 |
|
---|
111 | func (v WideStringValue) lt(b Value) Value {
|
---|
112 | return boolValue(StringID(v).String() < StringID(b.(WideStringValue)).String())
|
---|
113 | }
|
---|
114 |
|
---|
115 | type StringValue StringID
|
---|
116 |
|
---|
117 | func (v StringValue) add(b Value) Value { panic(todo("")) }
|
---|
118 | func (v StringValue) and(b Value) Value { panic(todo("")) }
|
---|
119 | func (v StringValue) cpl() Value { panic(todo("")) }
|
---|
120 | func (v StringValue) div(b Value) Value { panic(todo("")) }
|
---|
121 | func (v StringValue) eq(b Value) Value { return boolValue(v == b.(StringValue)) }
|
---|
122 | func (v StringValue) IsConst() bool { return true }
|
---|
123 | func (v StringValue) IsNonZero() bool { return true }
|
---|
124 | func (v StringValue) IsZero() bool { return false }
|
---|
125 | func (v StringValue) lsh(b Value) Value { panic(todo("")) }
|
---|
126 | func (v StringValue) mod(b Value) Value { panic(todo("")) }
|
---|
127 | func (v StringValue) mul(b Value) Value { panic(todo("")) }
|
---|
128 | func (v StringValue) neg() Value { panic(todo("")) }
|
---|
129 | func (v StringValue) neq(b Value) Value { return boolValue(v != b.(StringValue)) }
|
---|
130 | func (v StringValue) or(b Value) Value { panic(todo("")) }
|
---|
131 | func (v StringValue) rsh(b Value) Value { panic(todo("")) }
|
---|
132 | func (v StringValue) sub(b Value) Value { panic(todo("")) }
|
---|
133 | func (v StringValue) xor(b Value) Value { panic(todo("")) }
|
---|
134 |
|
---|
135 | func (v StringValue) le(b Value) Value {
|
---|
136 | return boolValue(StringID(v).String() <= StringID(b.(StringValue)).String())
|
---|
137 | }
|
---|
138 |
|
---|
139 | func (v StringValue) ge(b Value) Value {
|
---|
140 | return boolValue(StringID(v).String() >= StringID(b.(StringValue)).String())
|
---|
141 | }
|
---|
142 |
|
---|
143 | func (v StringValue) gt(b Value) Value {
|
---|
144 | return boolValue(StringID(v).String() > StringID(b.(StringValue)).String())
|
---|
145 | }
|
---|
146 |
|
---|
147 | func (v StringValue) lt(b Value) Value {
|
---|
148 | return boolValue(StringID(v).String() < StringID(b.(StringValue)).String())
|
---|
149 | }
|
---|
150 |
|
---|
151 | type Int64Value int64
|
---|
152 |
|
---|
153 | func (v Int64Value) add(b Value) Value { return v + b.(Int64Value) }
|
---|
154 | func (v Int64Value) and(b Value) Value { return v & b.(Int64Value) }
|
---|
155 | func (v Int64Value) cpl() Value { return ^v }
|
---|
156 | func (v Int64Value) eq(b Value) Value { return boolValue(v == b.(Int64Value)) }
|
---|
157 | func (v Int64Value) ge(b Value) Value { return boolValue(v >= b.(Int64Value)) }
|
---|
158 | func (v Int64Value) gt(b Value) Value { return boolValue(v > b.(Int64Value)) }
|
---|
159 | func (v Int64Value) IsConst() bool { return true }
|
---|
160 | func (v Int64Value) IsNonZero() bool { return v != 0 }
|
---|
161 | func (v Int64Value) IsZero() bool { return v == 0 }
|
---|
162 | func (v Int64Value) le(b Value) Value { return boolValue(v <= b.(Int64Value)) }
|
---|
163 | func (v Int64Value) lt(b Value) Value { return boolValue(v < b.(Int64Value)) }
|
---|
164 | func (v Int64Value) mul(b Value) Value { return v * b.(Int64Value) }
|
---|
165 | func (v Int64Value) neg() Value { return -v }
|
---|
166 | func (v Int64Value) neq(b Value) Value { return boolValue(v != b.(Int64Value)) }
|
---|
167 | func (v Int64Value) or(b Value) Value { return v | b.(Int64Value) }
|
---|
168 | func (v Int64Value) sub(b Value) Value { return v - b.(Int64Value) }
|
---|
169 | func (v Int64Value) xor(b Value) Value { return v ^ b.(Int64Value) }
|
---|
170 |
|
---|
171 | func (v Int64Value) div(b Value) Value {
|
---|
172 | if b.IsZero() {
|
---|
173 | return nil
|
---|
174 | }
|
---|
175 |
|
---|
176 | return v / b.(Int64Value)
|
---|
177 | }
|
---|
178 |
|
---|
179 | func (v Int64Value) lsh(b Value) Value {
|
---|
180 | switch y := b.(type) {
|
---|
181 | case Int64Value:
|
---|
182 | return v << uint64(y)
|
---|
183 | case Uint64Value:
|
---|
184 | return v << y
|
---|
185 | default:
|
---|
186 | panic(todo(""))
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | func (v Int64Value) rsh(b Value) Value {
|
---|
191 | switch y := b.(type) {
|
---|
192 | case Int64Value:
|
---|
193 | return v >> uint64(y)
|
---|
194 | case Uint64Value:
|
---|
195 | return v >> y
|
---|
196 | default:
|
---|
197 | panic(todo(""))
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | func (v Int64Value) mod(b Value) Value {
|
---|
202 | if b.IsZero() {
|
---|
203 | return nil
|
---|
204 | }
|
---|
205 |
|
---|
206 | return v % b.(Int64Value)
|
---|
207 | }
|
---|
208 |
|
---|
209 | type Uint64Value uint64
|
---|
210 |
|
---|
211 | func (v Uint64Value) add(b Value) Value { return v + b.(Uint64Value) }
|
---|
212 | func (v Uint64Value) and(b Value) Value { return v & b.(Uint64Value) }
|
---|
213 | func (v Uint64Value) cpl() Value { return ^v }
|
---|
214 | func (v Uint64Value) eq(b Value) Value { return boolValue(v == b.(Uint64Value)) }
|
---|
215 | func (v Uint64Value) ge(b Value) Value { return boolValue(v >= b.(Uint64Value)) }
|
---|
216 | func (v Uint64Value) gt(b Value) Value { return boolValue(v > b.(Uint64Value)) }
|
---|
217 | func (v Uint64Value) IsConst() bool { return true }
|
---|
218 | func (v Uint64Value) IsNonZero() bool { return v != 0 }
|
---|
219 | func (v Uint64Value) IsZero() bool { return v == 0 }
|
---|
220 | func (v Uint64Value) le(b Value) Value { return boolValue(v <= b.(Uint64Value)) }
|
---|
221 | func (v Uint64Value) lt(b Value) Value { return boolValue(v < b.(Uint64Value)) }
|
---|
222 | func (v Uint64Value) mul(b Value) Value { return v * b.(Uint64Value) }
|
---|
223 | func (v Uint64Value) neg() Value { return -v }
|
---|
224 | func (v Uint64Value) neq(b Value) Value { return boolValue(v != b.(Uint64Value)) }
|
---|
225 | func (v Uint64Value) or(b Value) Value { return v | b.(Uint64Value) }
|
---|
226 | func (v Uint64Value) sub(b Value) Value { return v - b.(Uint64Value) }
|
---|
227 | func (v Uint64Value) xor(b Value) Value { return v ^ b.(Uint64Value) }
|
---|
228 |
|
---|
229 | func (v Uint64Value) div(b Value) Value {
|
---|
230 | if b.IsZero() {
|
---|
231 | return nil
|
---|
232 | }
|
---|
233 |
|
---|
234 | return v / b.(Uint64Value)
|
---|
235 | }
|
---|
236 |
|
---|
237 | func (v Uint64Value) lsh(b Value) Value {
|
---|
238 | switch y := b.(type) {
|
---|
239 | case Int64Value:
|
---|
240 | return v << uint64(y)
|
---|
241 | case Uint64Value:
|
---|
242 | return v << y
|
---|
243 | default:
|
---|
244 | panic(todo(""))
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | func (v Uint64Value) rsh(b Value) Value {
|
---|
249 | switch y := b.(type) {
|
---|
250 | case Int64Value:
|
---|
251 | return v >> uint64(y)
|
---|
252 | case Uint64Value:
|
---|
253 | return v >> y
|
---|
254 | default:
|
---|
255 | panic(todo(""))
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | func (v Uint64Value) mod(b Value) Value {
|
---|
260 | if b.IsZero() {
|
---|
261 | return nil
|
---|
262 | }
|
---|
263 |
|
---|
264 | return v % b.(Uint64Value)
|
---|
265 | }
|
---|
266 |
|
---|
267 | type Float32Value float32
|
---|
268 |
|
---|
269 | func (v Float32Value) add(b Value) Value { return v + b.(Float32Value) }
|
---|
270 | func (v Float32Value) and(b Value) Value { panic(todo("")) }
|
---|
271 | func (v Float32Value) cpl() Value { panic(todo("")) }
|
---|
272 | func (v Float32Value) div(b Value) Value { return v / b.(Float32Value) }
|
---|
273 | func (v Float32Value) eq(b Value) Value { return boolValue(v == b.(Float32Value)) }
|
---|
274 | func (v Float32Value) ge(b Value) Value { return boolValue(v >= b.(Float32Value)) }
|
---|
275 | func (v Float32Value) gt(b Value) Value { return boolValue(v > b.(Float32Value)) }
|
---|
276 | func (v Float32Value) IsConst() bool { return true }
|
---|
277 | func (v Float32Value) IsNonZero() bool { return v != 0 }
|
---|
278 | func (v Float32Value) IsZero() bool { return !math.Signbit(float64(v)) && v == 0 }
|
---|
279 | func (v Float32Value) le(b Value) Value { return boolValue(v <= b.(Float32Value)) }
|
---|
280 | func (v Float32Value) lsh(b Value) Value { panic(todo("")) }
|
---|
281 | func (v Float32Value) lt(b Value) Value { return boolValue(v < b.(Float32Value)) }
|
---|
282 | func (v Float32Value) mod(b Value) Value { panic(todo("")) }
|
---|
283 | func (v Float32Value) mul(b Value) Value { return v * b.(Float32Value) }
|
---|
284 | func (v Float32Value) neg() Value { return -v }
|
---|
285 | func (v Float32Value) neq(b Value) Value { return boolValue(v != b.(Float32Value)) }
|
---|
286 | func (v Float32Value) or(b Value) Value { panic(todo("")) }
|
---|
287 | func (v Float32Value) rsh(b Value) Value { panic(todo("")) }
|
---|
288 | func (v Float32Value) sub(b Value) Value { return v - b.(Float32Value) }
|
---|
289 | func (v Float32Value) xor(b Value) Value { panic(todo("")) }
|
---|
290 |
|
---|
291 | type Float64Value float64
|
---|
292 |
|
---|
293 | func (v Float64Value) add(b Value) Value { return v + b.(Float64Value) }
|
---|
294 | func (v Float64Value) and(b Value) Value { panic(todo("")) }
|
---|
295 | func (v Float64Value) cpl() Value { panic(todo("")) }
|
---|
296 | func (v Float64Value) div(b Value) Value { return v / b.(Float64Value) }
|
---|
297 | func (v Float64Value) eq(b Value) Value { return boolValue(v == b.(Float64Value)) }
|
---|
298 | func (v Float64Value) ge(b Value) Value { return boolValue(v >= b.(Float64Value)) }
|
---|
299 | func (v Float64Value) gt(b Value) Value { return boolValue(v > b.(Float64Value)) }
|
---|
300 | func (v Float64Value) IsConst() bool { return true }
|
---|
301 | func (v Float64Value) IsNonZero() bool { return v != 0 }
|
---|
302 | func (v Float64Value) IsZero() bool { return !math.Signbit(float64(v)) && v == 0 }
|
---|
303 | func (v Float64Value) le(b Value) Value { return boolValue(v <= b.(Float64Value)) }
|
---|
304 | func (v Float64Value) lsh(b Value) Value { panic(todo("")) }
|
---|
305 | func (v Float64Value) lt(b Value) Value { return boolValue(v < b.(Float64Value)) }
|
---|
306 | func (v Float64Value) mod(b Value) Value { panic(todo("")) }
|
---|
307 | func (v Float64Value) mul(b Value) Value { return v * b.(Float64Value) }
|
---|
308 | func (v Float64Value) neg() Value { return -v }
|
---|
309 | func (v Float64Value) neq(b Value) Value { return boolValue(v != b.(Float64Value)) }
|
---|
310 | func (v Float64Value) or(b Value) Value { panic(todo("")) }
|
---|
311 | func (v Float64Value) rsh(b Value) Value { panic(todo("")) }
|
---|
312 | func (v Float64Value) sub(b Value) Value { return v - b.(Float64Value) }
|
---|
313 | func (v Float64Value) xor(b Value) Value { panic(todo("")) }
|
---|
314 |
|
---|
315 | var float128Zero = &Float128Value{N: big.NewFloat(0)}
|
---|
316 |
|
---|
317 | type Float128Value struct {
|
---|
318 | N *big.Float
|
---|
319 | NaN bool
|
---|
320 | }
|
---|
321 |
|
---|
322 | func (v *Float128Value) add(b Value) Value { return v.safe(b, func(x, y *big.Float) { x.Add(x, y) }) }
|
---|
323 | func (v *Float128Value) and(b Value) Value { panic(todo("")) }
|
---|
324 | func (v *Float128Value) cpl() Value { panic(todo("")) }
|
---|
325 | func (v *Float128Value) div(b Value) Value { return v.safe(b, func(x, y *big.Float) { x.Quo(x, y) }) }
|
---|
326 | func (v *Float128Value) eq(b Value) Value { panic(todo("")) }
|
---|
327 | func (v *Float128Value) ge(b Value) Value { panic(todo("")) }
|
---|
328 | func (v *Float128Value) gt(b Value) Value { return boolValue(v.cmp(b, -1, 0)) }
|
---|
329 | func (v *Float128Value) IsNonZero() bool { panic(todo("")) }
|
---|
330 | func (v *Float128Value) IsConst() bool { return true }
|
---|
331 | func (v *Float128Value) IsZero() bool { return !v.NaN && !v.N.Signbit() && v.cmp(float128Zero, 0) }
|
---|
332 | func (v *Float128Value) le(b Value) Value { panic(todo("")) }
|
---|
333 | func (v *Float128Value) lsh(b Value) Value { panic(todo("")) }
|
---|
334 | func (v *Float128Value) lt(b Value) Value { panic(todo("")) }
|
---|
335 | func (v *Float128Value) mod(b Value) Value { panic(todo("")) }
|
---|
336 | func (v *Float128Value) mul(b Value) Value { return v.safe(b, func(x, y *big.Float) { x.Mul(x, y) }) }
|
---|
337 | func (v *Float128Value) neg() Value { return v.safe(nil, func(x, y *big.Float) { x.Neg(x) }) }
|
---|
338 | func (v *Float128Value) neq(b Value) Value { panic(todo("")) }
|
---|
339 | func (v *Float128Value) or(b Value) Value { panic(todo("")) }
|
---|
340 | func (v *Float128Value) rsh(b Value) Value { panic(todo("")) }
|
---|
341 | func (v *Float128Value) sub(b Value) Value { return v.safe(b, func(x, y *big.Float) { x.Sub(x, y) }) }
|
---|
342 | func (v *Float128Value) xor(b Value) Value { panic(todo("")) }
|
---|
343 |
|
---|
344 | func (v *Float128Value) cmp(b Value, accept ...int) bool {
|
---|
345 | w := b.(*Float128Value)
|
---|
346 | if v.NaN || w.NaN {
|
---|
347 | return false
|
---|
348 | }
|
---|
349 |
|
---|
350 | x := v.N.Cmp(w.N)
|
---|
351 | for _, v := range accept {
|
---|
352 | if v == x {
|
---|
353 | return true
|
---|
354 | }
|
---|
355 | }
|
---|
356 | return false
|
---|
357 | }
|
---|
358 |
|
---|
359 | func (v *Float128Value) String() string {
|
---|
360 | switch {
|
---|
361 | case v == nil:
|
---|
362 | return "<nil>"
|
---|
363 | case v.NaN:
|
---|
364 | return "NaN"
|
---|
365 | default:
|
---|
366 | return fmt.Sprint(v.N)
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | func (v *Float128Value) safe(b Value, f func(*big.Float, *big.Float)) (ret Value) {
|
---|
371 | var w *Float128Value
|
---|
372 | if b != nil {
|
---|
373 | w = b.(*Float128Value)
|
---|
374 | }
|
---|
375 | if v.NaN || w != nil && w.NaN {
|
---|
376 | return &Float128Value{NaN: true}
|
---|
377 | }
|
---|
378 |
|
---|
379 | r := &Float128Value{}
|
---|
380 |
|
---|
381 | defer func() {
|
---|
382 | switch x := recover().(type) {
|
---|
383 | case big.ErrNaN:
|
---|
384 | r.N = nil
|
---|
385 | r.NaN = true
|
---|
386 | ret = r
|
---|
387 | case nil:
|
---|
388 | // ok
|
---|
389 | default:
|
---|
390 | panic(x)
|
---|
391 | }
|
---|
392 | }()
|
---|
393 |
|
---|
394 | r.N = big.NewFloat(0).SetPrec(0).Set(v.N)
|
---|
395 | var wn *big.Float
|
---|
396 | if w != nil {
|
---|
397 | wn = w.N
|
---|
398 | }
|
---|
399 | f(r.N, wn)
|
---|
400 | return r
|
---|
401 | }
|
---|
402 |
|
---|
403 | type Complex64Value complex64
|
---|
404 |
|
---|
405 | func (v Complex64Value) add(b Value) Value { return v + b.(Complex64Value) }
|
---|
406 | func (v Complex64Value) and(b Value) Value { panic(todo("")) }
|
---|
407 | func (v Complex64Value) cpl() Value { panic(todo("")) }
|
---|
408 | func (v Complex64Value) div(b Value) Value { return v / b.(Complex64Value) }
|
---|
409 | func (v Complex64Value) eq(b Value) Value { return boolValue(v == b.(Complex64Value)) }
|
---|
410 | func (v Complex64Value) ge(b Value) Value { panic(todo("")) }
|
---|
411 | func (v Complex64Value) gt(b Value) Value { panic(todo("")) }
|
---|
412 | func (v Complex64Value) IsConst() bool { return true }
|
---|
413 | func (v Complex64Value) IsNonZero() bool { return v != 0 }
|
---|
414 | func (v Complex64Value) IsZero() bool { return v == 0 }
|
---|
415 | func (v Complex64Value) le(b Value) Value { panic(todo("")) }
|
---|
416 | func (v Complex64Value) lsh(b Value) Value { panic(todo("")) }
|
---|
417 | func (v Complex64Value) lt(b Value) Value { panic(todo("")) }
|
---|
418 | func (v Complex64Value) mod(b Value) Value { panic(todo("")) }
|
---|
419 | func (v Complex64Value) mul(b Value) Value { return v * b.(Complex64Value) }
|
---|
420 | func (v Complex64Value) neg() Value { return -v }
|
---|
421 | func (v Complex64Value) neq(b Value) Value { return boolValue(v != b.(Complex64Value)) }
|
---|
422 | func (v Complex64Value) or(b Value) Value { panic(todo("")) }
|
---|
423 | func (v Complex64Value) rsh(b Value) Value { panic(todo("")) }
|
---|
424 | func (v Complex64Value) sub(b Value) Value { return v - b.(Complex64Value) }
|
---|
425 | func (v Complex64Value) xor(b Value) Value { panic(todo("")) }
|
---|
426 |
|
---|
427 | type Complex128Value complex128
|
---|
428 |
|
---|
429 | func (v Complex128Value) add(b Value) Value { return v + b.(Complex128Value) }
|
---|
430 | func (v Complex128Value) and(b Value) Value { panic(todo("")) }
|
---|
431 | func (v Complex128Value) cpl() Value { panic(todo("")) }
|
---|
432 | func (v Complex128Value) div(b Value) Value { return v / b.(Complex128Value) }
|
---|
433 | func (v Complex128Value) eq(b Value) Value { return boolValue(v == b.(Complex128Value)) }
|
---|
434 | func (v Complex128Value) ge(b Value) Value { panic(todo("")) }
|
---|
435 | func (v Complex128Value) gt(b Value) Value { panic(todo("")) }
|
---|
436 | func (v Complex128Value) IsConst() bool { return true }
|
---|
437 | func (v Complex128Value) IsNonZero() bool { return v != 0 }
|
---|
438 | func (v Complex128Value) IsZero() bool { return v == 0 }
|
---|
439 | func (v Complex128Value) le(b Value) Value { panic(todo("")) }
|
---|
440 | func (v Complex128Value) lsh(b Value) Value { panic(todo("")) }
|
---|
441 | func (v Complex128Value) lt(b Value) Value { panic(todo("")) }
|
---|
442 | func (v Complex128Value) mod(b Value) Value { panic(todo("")) }
|
---|
443 | func (v Complex128Value) mul(b Value) Value { return v * b.(Complex128Value) }
|
---|
444 | func (v Complex128Value) neg() Value { return -v }
|
---|
445 | func (v Complex128Value) neq(b Value) Value { return boolValue(v != b.(Complex128Value)) }
|
---|
446 | func (v Complex128Value) or(b Value) Value { panic(todo("")) }
|
---|
447 | func (v Complex128Value) rsh(b Value) Value { panic(todo("")) }
|
---|
448 | func (v Complex128Value) sub(b Value) Value { return v - b.(Complex128Value) }
|
---|
449 | func (v Complex128Value) xor(b Value) Value { panic(todo("")) }
|
---|
450 |
|
---|
451 | type Complex256Value struct {
|
---|
452 | Re, Im *Float128Value
|
---|
453 | }
|
---|
454 |
|
---|
455 | func (v Complex256Value) add(b Value) Value {
|
---|
456 | w := b.(Complex256Value)
|
---|
457 | return Complex256Value{v.Re.add(w.Re).(*Float128Value), v.Im.add(w.Im).(*Float128Value)}
|
---|
458 | }
|
---|
459 |
|
---|
460 | func (v Complex256Value) and(b Value) Value { panic(todo("")) }
|
---|
461 | func (v Complex256Value) cpl() Value { panic(todo("")) }
|
---|
462 | func (v Complex256Value) div(b Value) Value { panic(todo("")) }
|
---|
463 | func (v Complex256Value) eq(b Value) Value { panic(todo("")) }
|
---|
464 | func (v Complex256Value) ge(b Value) Value { panic(todo("")) }
|
---|
465 | func (v Complex256Value) gt(b Value) Value { panic(todo("")) }
|
---|
466 | func (v Complex256Value) IsConst() bool { return true }
|
---|
467 | func (v Complex256Value) IsNonZero() bool { panic(todo("")) }
|
---|
468 | func (v Complex256Value) IsZero() bool { return v.Re.IsZero() && v.Im.IsZero() }
|
---|
469 | func (v Complex256Value) le(b Value) Value { panic(todo("")) }
|
---|
470 | func (v Complex256Value) lsh(b Value) Value { panic(todo("")) }
|
---|
471 | func (v Complex256Value) lt(b Value) Value { panic(todo("")) }
|
---|
472 | func (v Complex256Value) mod(b Value) Value { panic(todo("")) }
|
---|
473 | func (v Complex256Value) mul(b Value) Value { panic(todo("")) }
|
---|
474 | func (v Complex256Value) neg() Value { panic(todo("")) }
|
---|
475 | func (v Complex256Value) neq(b Value) Value { panic(todo("")) }
|
---|
476 | func (v Complex256Value) or(b Value) Value { panic(todo("")) }
|
---|
477 | func (v Complex256Value) rsh(b Value) Value { panic(todo("")) }
|
---|
478 | func (v Complex256Value) sub(b Value) Value { panic(todo("")) }
|
---|
479 | func (v Complex256Value) xor(b Value) Value { panic(todo("")) }
|
---|
480 |
|
---|
481 | type lvalue struct {
|
---|
482 | Operand
|
---|
483 | declarator *Declarator
|
---|
484 | }
|
---|
485 |
|
---|
486 | func (o *lvalue) ConvertTo(to Type) (r Operand) { return o.convertTo(nil, nil, to) }
|
---|
487 | func (o *lvalue) Declarator() *Declarator { return o.declarator }
|
---|
488 | func (o *lvalue) IsLValue() bool { return true }
|
---|
489 |
|
---|
490 | func (o *lvalue) IsConst() bool {
|
---|
491 | if v := o.Value(); v != nil {
|
---|
492 | return v.IsConst()
|
---|
493 | }
|
---|
494 |
|
---|
495 | d := o.Declarator()
|
---|
496 | return d != nil && (d.Linkage != None || d.IsStatic())
|
---|
497 | }
|
---|
498 |
|
---|
499 | func (o *lvalue) convertTo(ctx *context, n Node, to Type) (r Operand) {
|
---|
500 | return &lvalue{Operand: o.Operand.convertTo(ctx, n, to), declarator: o.declarator}
|
---|
501 | }
|
---|
502 |
|
---|
503 | type funcDesignator struct {
|
---|
504 | Operand
|
---|
505 | declarator *Declarator
|
---|
506 | }
|
---|
507 |
|
---|
508 | func (o *funcDesignator) ConvertTo(to Type) (r Operand) { return o.convertTo(nil, nil, to) }
|
---|
509 | func (o *funcDesignator) Declarator() *Declarator { return o.declarator }
|
---|
510 | func (o *funcDesignator) IsLValue() bool { return false }
|
---|
511 | func (o *funcDesignator) IsConst() bool { return true }
|
---|
512 |
|
---|
513 | func (o *funcDesignator) convertTo(ctx *context, n Node, to Type) (r Operand) {
|
---|
514 | return &lvalue{Operand: o.Operand.convertTo(ctx, n, to), declarator: o.declarator}
|
---|
515 | }
|
---|
516 |
|
---|
517 | type operand struct {
|
---|
518 | abi *ABI
|
---|
519 | typ Type
|
---|
520 | value Value
|
---|
521 | offset uintptr
|
---|
522 | }
|
---|
523 |
|
---|
524 | func (o *operand) ConvertTo(to Type) (r Operand) { return o.convertTo(nil, nil, to) }
|
---|
525 | func (o *operand) Declarator() *Declarator { return nil }
|
---|
526 | func (o *operand) Offset() uintptr { return o.offset }
|
---|
527 | func (o *operand) IsLValue() bool { return false }
|
---|
528 | func (o *operand) IsNonZero() bool { return o.value != nil && o.value.IsNonZero() }
|
---|
529 | func (o *operand) IsZero() bool { return o.value != nil && o.value.IsZero() }
|
---|
530 | func (o *operand) Type() Type { return o.typ }
|
---|
531 | func (o *operand) Value() Value { return o.value }
|
---|
532 | func (o *operand) getABI() *ABI { return o.abi }
|
---|
533 |
|
---|
534 | // IsAssingmentCompatible implements Operand.
|
---|
535 | func (o *operand) IsAssingmentCompatible(lhs Type) bool { return lhs.isAssingmentCompatibleOperand(o) }
|
---|
536 |
|
---|
537 | func (o *operand) IsConst() bool {
|
---|
538 | if v := o.Value(); v != nil {
|
---|
539 | return v.IsConst()
|
---|
540 | }
|
---|
541 |
|
---|
542 | d := o.Declarator()
|
---|
543 | return d != nil && (d.Linkage != None || d.IsStatic())
|
---|
544 | }
|
---|
545 |
|
---|
546 | // [0]6.3.1.8
|
---|
547 | //
|
---|
548 | // Many operators that expect operands of arithmetic type cause conversions and
|
---|
549 | // yield result types in a similar way. The purpose is to determine a common
|
---|
550 | // real type for the operands and result. For the specified operands, each
|
---|
551 | // operand is converted, without change of type domain, to a type whose
|
---|
552 | // corresponding real type is the common real type. Unless explicitly stated
|
---|
553 | // otherwise, the common real type is also the corresponding real type of the
|
---|
554 | // result, whose type domain is the type domain of the operands if they are the
|
---|
555 | // same, and complex otherwise. This pattern is called the usual arithmetic
|
---|
556 | // conversions:
|
---|
557 | func usualArithmeticConversions(ctx *context, n Node, a, b Operand, normalize bool) (Operand, Operand) {
|
---|
558 | if a.Type().Kind() == Invalid || b.Type().Kind() == Invalid {
|
---|
559 | return noOperand, noOperand
|
---|
560 | }
|
---|
561 |
|
---|
562 | abi := a.getABI()
|
---|
563 | if !a.Type().IsArithmeticType() {
|
---|
564 | if ctx != nil {
|
---|
565 | ctx.errNode(n, "not an arithmetic type: %s", a.Type())
|
---|
566 | }
|
---|
567 | return noOperand, noOperand
|
---|
568 | }
|
---|
569 |
|
---|
570 | if !b.Type().IsArithmeticType() {
|
---|
571 | if ctx != nil {
|
---|
572 | ctx.errNode(n, "not an arithmetic type: %s", b.Type())
|
---|
573 | }
|
---|
574 | return noOperand, noOperand
|
---|
575 | }
|
---|
576 |
|
---|
577 | if a.Type() == nil || b.Type() == nil {
|
---|
578 | return a, b
|
---|
579 | }
|
---|
580 |
|
---|
581 | if normalize {
|
---|
582 | a = a.normalize(ctx, n)
|
---|
583 | b = b.normalize(ctx, n)
|
---|
584 | }
|
---|
585 | if a == noOperand || b == noOperand {
|
---|
586 | return noOperand, noOperand
|
---|
587 | }
|
---|
588 |
|
---|
589 | at := a.Type()
|
---|
590 | bt := b.Type()
|
---|
591 | cplx := at.IsComplexType() || bt.IsComplexType()
|
---|
592 |
|
---|
593 | // First, if the corresponding real type of either operand is long
|
---|
594 | // double, the other operand is converted, without change of type
|
---|
595 | // domain, to a type whose corresponding real type is long double.
|
---|
596 | if at.Kind() == ComplexLongDouble || bt.Kind() == ComplexLongDouble || at.Kind() == LongDouble || bt.Kind() == LongDouble {
|
---|
597 | switch {
|
---|
598 | case cplx:
|
---|
599 | return a.convertTo(ctx, n, abi.Type(ComplexLongDouble)), b.convertTo(ctx, n, abi.Type(ComplexLongDouble))
|
---|
600 | default:
|
---|
601 | return a.convertTo(ctx, n, abi.Type(LongDouble)), b.convertTo(ctx, n, abi.Type(LongDouble))
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | // Otherwise, if the corresponding real type of either operand is
|
---|
606 | // double, the other operand is converted, without change of type
|
---|
607 | // domain, to a type whose corresponding real type is double.
|
---|
608 | if at.Kind() == ComplexDouble || bt.Kind() == ComplexDouble || at.Kind() == Double || bt.Kind() == Double {
|
---|
609 | switch {
|
---|
610 | case cplx:
|
---|
611 | return a.convertTo(ctx, n, abi.Type(ComplexDouble)), b.convertTo(ctx, n, abi.Type(ComplexDouble))
|
---|
612 | default:
|
---|
613 | return a.convertTo(ctx, n, abi.Type(Double)), b.convertTo(ctx, n, abi.Type(Double))
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | // Otherwise, if the corresponding real type of either operand is
|
---|
618 | // float, the other operand is converted, without change of type
|
---|
619 | // domain, to a type whose corresponding real type is float.
|
---|
620 | if at.Kind() == ComplexFloat || bt.Kind() == ComplexFloat || at.Kind() == Float || bt.Kind() == Float {
|
---|
621 | switch {
|
---|
622 | case cplx:
|
---|
623 | return a.convertTo(ctx, n, abi.Type(ComplexFloat)), b.convertTo(ctx, n, abi.Type(ComplexFloat))
|
---|
624 | default:
|
---|
625 | return a.convertTo(ctx, n, abi.Type(Float)), b.convertTo(ctx, n, abi.Type(Float))
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 | if cplx {
|
---|
630 | panic(internalErrorf("TODO %v, %v", at, bt))
|
---|
631 | }
|
---|
632 |
|
---|
633 | if !a.Type().IsIntegerType() || !b.Type().IsIntegerType() {
|
---|
634 | panic(todo(""))
|
---|
635 | }
|
---|
636 |
|
---|
637 | // Otherwise, the integer promotions are performed on both operands.
|
---|
638 | a = a.integerPromotion(ctx, n)
|
---|
639 | b = b.integerPromotion(ctx, n)
|
---|
640 | at = a.Type()
|
---|
641 | bt = b.Type()
|
---|
642 |
|
---|
643 | // Then the following rules are applied to the promoted operands:
|
---|
644 |
|
---|
645 | // If both operands have the same type, then no further conversion is
|
---|
646 | // needed.
|
---|
647 | if at.Kind() == bt.Kind() {
|
---|
648 | return a, b
|
---|
649 | }
|
---|
650 |
|
---|
651 | // Otherwise, if both operands have signed integer types or both have
|
---|
652 | // unsigned integer types, the operand with the type of lesser integer
|
---|
653 | // conversion rank is converted to the type of the operand with greater
|
---|
654 | // rank.
|
---|
655 | if abi.isSignedInteger(at.Kind()) == abi.isSignedInteger(bt.Kind()) {
|
---|
656 | t := a.Type()
|
---|
657 | if intConvRank[bt.Kind()] > intConvRank[at.Kind()] {
|
---|
658 | t = b.Type()
|
---|
659 | }
|
---|
660 | return a.convertTo(ctx, n, t), b.convertTo(ctx, n, t)
|
---|
661 |
|
---|
662 | }
|
---|
663 |
|
---|
664 | // Otherwise, if the operand that has unsigned integer type has rank
|
---|
665 | // greater or equal to the rank of the type of the other operand, then
|
---|
666 | // the operand with signed integer type is converted to the type of the
|
---|
667 | // operand with unsigned integer type.
|
---|
668 | switch {
|
---|
669 | case a.Type().IsSignedType(): // b is unsigned
|
---|
670 | if intConvRank[bt.Kind()] >= intConvRank[a.Type().Kind()] {
|
---|
671 | return a.convertTo(ctx, n, b.Type()), b
|
---|
672 | }
|
---|
673 | case b.Type().IsSignedType(): // a is unsigned
|
---|
674 | if intConvRank[at.Kind()] >= intConvRank[b.Type().Kind()] {
|
---|
675 | return a, b.convertTo(ctx, n, a.Type())
|
---|
676 | }
|
---|
677 | default:
|
---|
678 | panic(fmt.Errorf("TODO %v %v", a.Type(), b.Type()))
|
---|
679 | }
|
---|
680 |
|
---|
681 | // Otherwise, if the type of the operand with signed integer type can
|
---|
682 | // represent all of the values of the type of the operand with unsigned
|
---|
683 | // integer type, then the operand with unsigned integer type is
|
---|
684 | // converted to the type of the operand with signed integer type.
|
---|
685 | var signed Type
|
---|
686 | switch {
|
---|
687 | case abi.isSignedInteger(at.Kind()): // b is unsigned
|
---|
688 | signed = a.Type()
|
---|
689 | if at.Size() > bt.Size() {
|
---|
690 | return a, b.convertTo(ctx, n, a.Type())
|
---|
691 | }
|
---|
692 | case abi.isSignedInteger(bt.Kind()): // a is unsigned
|
---|
693 | signed = b.Type()
|
---|
694 | if bt.Size() > at.Size() {
|
---|
695 | return a.convertTo(ctx, n, b.Type()), b
|
---|
696 | }
|
---|
697 |
|
---|
698 | }
|
---|
699 |
|
---|
700 | // Otherwise, both operands are converted to the unsigned integer type
|
---|
701 | // corresponding to the type of the operand with signed integer type.
|
---|
702 | var typ Type
|
---|
703 | switch signed.Kind() {
|
---|
704 | case Int:
|
---|
705 | //TODO if a.IsEnumConst || b.IsEnumConst {
|
---|
706 | //TODO return a, b
|
---|
707 | //TODO }
|
---|
708 |
|
---|
709 | typ = abi.Type(UInt)
|
---|
710 | case Long:
|
---|
711 | typ = abi.Type(ULong)
|
---|
712 | case LongLong:
|
---|
713 | typ = abi.Type(ULongLong)
|
---|
714 | default:
|
---|
715 | panic(todo(""))
|
---|
716 | }
|
---|
717 | return a.convertTo(ctx, n, typ), b.convertTo(ctx, n, typ)
|
---|
718 | }
|
---|
719 |
|
---|
720 | // [0]6.3.1.1-2
|
---|
721 | //
|
---|
722 | // If an int can represent all values of the original type, the value is
|
---|
723 | // converted to an int; otherwise, it is converted to an unsigned int. These
|
---|
724 | // are called the integer promotions. All other types are unchanged by the
|
---|
725 | // integer promotions.
|
---|
726 | func (o *operand) integerPromotion(ctx *context, n Node) Operand {
|
---|
727 | t := o.Type()
|
---|
728 | if t2 := integerPromotion(o.abi, t); t2.Kind() != t.Kind() {
|
---|
729 | return o.convertTo(ctx, n, t2)
|
---|
730 | }
|
---|
731 |
|
---|
732 | return o
|
---|
733 | }
|
---|
734 |
|
---|
735 | // [0]6.3.1.1-2
|
---|
736 | //
|
---|
737 | // If an int can represent all values of the original type, the value is
|
---|
738 | // converted to an int; otherwise, it is converted to an unsigned int. These
|
---|
739 | // are called the integer promotions. All other types are unchanged by the
|
---|
740 | // integer promotions.
|
---|
741 | func integerPromotion(abi *ABI, t Type) Type {
|
---|
742 | // github.com/gcc-mirror/gcc/gcc/testsuite/gcc.c-torture/execute/bf-sign-2.c
|
---|
743 | //
|
---|
744 | // This test checks promotion of bitfields. Bitfields
|
---|
745 | // should be promoted very much like chars and shorts:
|
---|
746 | //
|
---|
747 | // Bitfields (signed or unsigned) should be promoted to
|
---|
748 | // signed int if their value will fit in a signed int,
|
---|
749 | // otherwise to an unsigned int if their value will fit
|
---|
750 | // in an unsigned int, otherwise we don't promote them
|
---|
751 | // (ANSI/ISO does not specify the behavior of bitfields
|
---|
752 | // larger than an unsigned int).
|
---|
753 | if t.IsBitFieldType() {
|
---|
754 | f := t.BitField()
|
---|
755 | intBits := int(abi.Types[Int].Size) * 8
|
---|
756 | switch {
|
---|
757 | case t.IsSignedType():
|
---|
758 | if f.BitFieldWidth() < intBits-1 {
|
---|
759 | return abi.Type(Int)
|
---|
760 | }
|
---|
761 | default:
|
---|
762 | if f.BitFieldWidth() < intBits {
|
---|
763 | return abi.Type(Int)
|
---|
764 | }
|
---|
765 | }
|
---|
766 | return t
|
---|
767 | }
|
---|
768 |
|
---|
769 | switch t.Kind() {
|
---|
770 | case Invalid:
|
---|
771 | return t
|
---|
772 | case Char, SChar, UChar, Short, UShort:
|
---|
773 | return abi.Type(Int)
|
---|
774 | default:
|
---|
775 | return t
|
---|
776 | }
|
---|
777 | }
|
---|
778 |
|
---|
779 | func (o *operand) convertTo(ctx *context, n Node, to Type) Operand {
|
---|
780 | if o.Type().Kind() == Invalid {
|
---|
781 | return o
|
---|
782 | }
|
---|
783 |
|
---|
784 | v := o.Value()
|
---|
785 | r := &operand{abi: o.abi, typ: to, offset: o.offset, value: v}
|
---|
786 | switch v.(type) {
|
---|
787 | case nil, *InitializerValue:
|
---|
788 | return r
|
---|
789 | }
|
---|
790 |
|
---|
791 | if o.Type().Kind() == to.Kind() {
|
---|
792 | return r.normalize(ctx, n)
|
---|
793 | }
|
---|
794 |
|
---|
795 | if o.Type().IsIntegerType() {
|
---|
796 | return o.convertFromInt(ctx, n, to)
|
---|
797 | }
|
---|
798 |
|
---|
799 | if to.IsIntegerType() {
|
---|
800 | return o.convertToInt(ctx, n, to)
|
---|
801 | }
|
---|
802 |
|
---|
803 | switch o.Type().Kind() {
|
---|
804 | case Array:
|
---|
805 | switch to.Kind() {
|
---|
806 | case Ptr:
|
---|
807 | return r
|
---|
808 | default:
|
---|
809 | panic(todo("", n.Position()))
|
---|
810 | }
|
---|
811 | case ComplexFloat:
|
---|
812 | v := v.(Complex64Value)
|
---|
813 | switch to.Kind() {
|
---|
814 | case ComplexDouble:
|
---|
815 | r.value = Complex128Value(v)
|
---|
816 | case Float:
|
---|
817 | r.value = Float32Value(real(v))
|
---|
818 | case Double:
|
---|
819 | r.value = Float64Value(real(v))
|
---|
820 | case ComplexLongDouble:
|
---|
821 | panic(todo("", n.Position()))
|
---|
822 | default:
|
---|
823 | panic(todo("", n.Position()))
|
---|
824 | }
|
---|
825 | case ComplexDouble:
|
---|
826 | v := v.(Complex128Value)
|
---|
827 | switch to.Kind() {
|
---|
828 | case ComplexFloat:
|
---|
829 | r.value = Complex64Value(v)
|
---|
830 | case ComplexLongDouble:
|
---|
831 | //TODO panic(todo("", n.Position()))
|
---|
832 | r.value = nil
|
---|
833 | case Float:
|
---|
834 | r.value = Float32Value(real(v))
|
---|
835 | case Double:
|
---|
836 | r.value = Float64Value(real(v))
|
---|
837 | default:
|
---|
838 | //TODO panic(todo("", n.Position(), o.Type(), to))
|
---|
839 | r.value = nil
|
---|
840 | }
|
---|
841 | case Float:
|
---|
842 | v := v.(Float32Value)
|
---|
843 | switch to.Kind() {
|
---|
844 | case ComplexFloat:
|
---|
845 | r.value = Complex64Value(complex(v, 0))
|
---|
846 | case ComplexDouble:
|
---|
847 | r.value = Complex128Value(complex(v, 0))
|
---|
848 | case Double:
|
---|
849 | r.value = Float64Value(v)
|
---|
850 | case ComplexLongDouble:
|
---|
851 | panic(todo("", n.Position()))
|
---|
852 | case LongDouble:
|
---|
853 | r.value = &Float128Value{N: big.NewFloat(float64(v))}
|
---|
854 | case Decimal32, Decimal64, Decimal128:
|
---|
855 | // ok
|
---|
856 | default:
|
---|
857 | panic(todo("695 %s", to.Kind()))
|
---|
858 | }
|
---|
859 | case Double:
|
---|
860 | v := v.(Float64Value)
|
---|
861 | switch to.Kind() {
|
---|
862 | case ComplexFloat:
|
---|
863 | r.value = Complex64Value(complex(v, 0))
|
---|
864 | case ComplexDouble:
|
---|
865 | r.value = Complex128Value(complex(v, 0))
|
---|
866 | case LongDouble:
|
---|
867 | f := float64(v)
|
---|
868 | switch {
|
---|
869 | case math.IsNaN(f):
|
---|
870 | r.value = &Float128Value{NaN: true}
|
---|
871 | default:
|
---|
872 | r.value = &Float128Value{N: big.NewFloat(f)}
|
---|
873 | }
|
---|
874 | case Float:
|
---|
875 | r.value = Float32Value(v)
|
---|
876 | case ComplexLongDouble:
|
---|
877 | panic(todo("", n.Position()))
|
---|
878 | case Vector:
|
---|
879 | r.value = nil
|
---|
880 | case Decimal32, Decimal64, Decimal128:
|
---|
881 | // ok
|
---|
882 | default:
|
---|
883 | panic(todo("", to.Kind()))
|
---|
884 | }
|
---|
885 | case LongDouble:
|
---|
886 | v := v.(*Float128Value)
|
---|
887 | switch to.Kind() {
|
---|
888 | case Double:
|
---|
889 | if v.NaN {
|
---|
890 | r.value = Float64Value(math.NaN())
|
---|
891 | break
|
---|
892 | }
|
---|
893 |
|
---|
894 | d, _ := v.N.Float64()
|
---|
895 | r.value = Float64Value(d)
|
---|
896 | case Float:
|
---|
897 | if v.NaN {
|
---|
898 | r.value = Float32Value(math.NaN())
|
---|
899 | break
|
---|
900 | }
|
---|
901 |
|
---|
902 | d, _ := v.N.Float64()
|
---|
903 | r.value = Float32Value(d)
|
---|
904 | case ComplexLongDouble:
|
---|
905 | if v.NaN {
|
---|
906 | r.value = Complex256Value{v, &Float128Value{NaN: true}}
|
---|
907 | break
|
---|
908 | }
|
---|
909 |
|
---|
910 | r.value = Complex256Value{v, &Float128Value{N: big.NewFloat(0)}}
|
---|
911 | case Decimal32, Decimal64, Decimal128:
|
---|
912 | // ok
|
---|
913 | default:
|
---|
914 | panic(todo("813 %v", to.Kind()))
|
---|
915 | }
|
---|
916 | case Ptr:
|
---|
917 | switch to.Kind() {
|
---|
918 | case Void:
|
---|
919 | return noOperand
|
---|
920 | default:
|
---|
921 | panic(internalErrorf("%v: %v y-> %v %v", n.Position(), o.Type(), to, to.Kind()))
|
---|
922 | }
|
---|
923 | default:
|
---|
924 | panic(internalErrorf("%v: %v -> %v %v", n.Position(), o.Type(), to, to.Kind()))
|
---|
925 | }
|
---|
926 | return r.normalize(ctx, n)
|
---|
927 | }
|
---|
928 |
|
---|
929 | type signedSaturationLimit struct {
|
---|
930 | fmin, fmax float64
|
---|
931 | min, max int64
|
---|
932 | }
|
---|
933 |
|
---|
934 | type unsignedSaturationLimit struct {
|
---|
935 | fmax float64
|
---|
936 | max uint64
|
---|
937 | }
|
---|
938 |
|
---|
939 | var (
|
---|
940 | signedSaturationLimits = [...]signedSaturationLimit{
|
---|
941 | 1: {math.Nextafter(math.MinInt32, 0), math.Nextafter(math.MaxInt32, 0), math.MinInt32, math.MaxInt32},
|
---|
942 | 2: {math.Nextafter(math.MinInt32, 0), math.Nextafter(math.MaxInt32, 0), math.MinInt32, math.MaxInt32},
|
---|
943 | 4: {math.Nextafter(math.MinInt32, 0), math.Nextafter(math.MaxInt32, 0), math.MinInt32, math.MaxInt32},
|
---|
944 | 8: {math.Nextafter(math.MinInt64, 0), math.Nextafter(math.MaxInt64, 0), math.MinInt64, math.MaxInt64},
|
---|
945 | }
|
---|
946 |
|
---|
947 | unsignedSaturationLimits = [...]unsignedSaturationLimit{
|
---|
948 | 1: {math.Nextafter(math.MaxUint32, 0), math.MaxUint32},
|
---|
949 | 2: {math.Nextafter(math.MaxUint32, 0), math.MaxUint32},
|
---|
950 | 4: {math.Nextafter(math.MaxUint32, 0), math.MaxUint32},
|
---|
951 | 8: {math.Nextafter(math.MaxUint64, 0), math.MaxUint64},
|
---|
952 | }
|
---|
953 | )
|
---|
954 |
|
---|
955 | func (o *operand) convertToInt(ctx *context, n Node, to Type) (r Operand) {
|
---|
956 | v := o.Value()
|
---|
957 | switch o.Type().Kind() {
|
---|
958 | case Float:
|
---|
959 | v := float64(v.(Float32Value))
|
---|
960 | switch {
|
---|
961 | case to.IsSignedType():
|
---|
962 | limits := &signedSaturationLimits[to.Size()]
|
---|
963 | if v > limits.fmax {
|
---|
964 | return (&operand{abi: o.abi, typ: to, value: Int64Value(limits.max)}).normalize(ctx, n)
|
---|
965 | }
|
---|
966 |
|
---|
967 | if v < limits.fmin {
|
---|
968 | return (&operand{abi: o.abi, typ: to, value: Int64Value(limits.min)}).normalize(ctx, n)
|
---|
969 | }
|
---|
970 |
|
---|
971 | return (&operand{abi: o.abi, typ: to, value: Int64Value(v)}).normalize(ctx, n)
|
---|
972 | default:
|
---|
973 | limits := &unsignedSaturationLimits[to.Size()]
|
---|
974 | if v > limits.fmax {
|
---|
975 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(limits.max)}).normalize(ctx, n)
|
---|
976 | }
|
---|
977 |
|
---|
978 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(v)}).normalize(ctx, n)
|
---|
979 | }
|
---|
980 | case Double:
|
---|
981 | v := float64(v.(Float64Value))
|
---|
982 | switch {
|
---|
983 | case to.IsSignedType():
|
---|
984 | limits := &signedSaturationLimits[to.Size()]
|
---|
985 | if v > limits.fmax {
|
---|
986 | return (&operand{abi: o.abi, typ: to, value: Int64Value(limits.max)}).normalize(ctx, n)
|
---|
987 | }
|
---|
988 |
|
---|
989 | if v < limits.fmin {
|
---|
990 | return (&operand{abi: o.abi, typ: to, value: Int64Value(limits.min)}).normalize(ctx, n)
|
---|
991 | }
|
---|
992 |
|
---|
993 | return (&operand{abi: o.abi, typ: to, value: Int64Value(v)}).normalize(ctx, n)
|
---|
994 | default:
|
---|
995 | limits := &unsignedSaturationLimits[to.Size()]
|
---|
996 | if v > limits.fmax {
|
---|
997 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(limits.max)}).normalize(ctx, n)
|
---|
998 | }
|
---|
999 |
|
---|
1000 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(v)}).normalize(ctx, n)
|
---|
1001 | }
|
---|
1002 | case LongDouble:
|
---|
1003 | panic(todo("", n.Position()))
|
---|
1004 | case Ptr:
|
---|
1005 | var v uint64
|
---|
1006 | switch x := o.Value().(type) {
|
---|
1007 | case Int64Value:
|
---|
1008 | v = uint64(x)
|
---|
1009 | case Uint64Value:
|
---|
1010 | v = uint64(x)
|
---|
1011 | case *InitializerValue:
|
---|
1012 | return (&operand{abi: o.abi, typ: to})
|
---|
1013 | default:
|
---|
1014 | panic(internalErrorf("%v: %T", n.Position(), x))
|
---|
1015 | }
|
---|
1016 | switch {
|
---|
1017 | case to.IsSignedType():
|
---|
1018 | return (&operand{abi: o.abi, typ: to, value: Int64Value(v)}).normalize(ctx, n)
|
---|
1019 | default:
|
---|
1020 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(v)}).normalize(ctx, n)
|
---|
1021 | }
|
---|
1022 | case Array:
|
---|
1023 | return &operand{abi: o.abi, typ: to}
|
---|
1024 | case Vector:
|
---|
1025 | if o.Type().Size() == to.Size() {
|
---|
1026 | return &operand{abi: o.abi, typ: to}
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | if ctx != nil {
|
---|
1030 | ctx.errNode(n, "cannot convert %s to %s", o.Type(), to)
|
---|
1031 | }
|
---|
1032 | return &operand{abi: o.abi, typ: to}
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | func (o *operand) convertFromInt(ctx *context, n Node, to Type) (r Operand) {
|
---|
1036 | var v uint64
|
---|
1037 | switch x := o.Value().(type) {
|
---|
1038 | case Int64Value:
|
---|
1039 | v = uint64(x)
|
---|
1040 | case Uint64Value:
|
---|
1041 | v = uint64(x)
|
---|
1042 | default:
|
---|
1043 | if ctx != nil {
|
---|
1044 | ctx.errNode(n, "conversion to integer: invalid value")
|
---|
1045 | }
|
---|
1046 | return &operand{abi: o.abi, typ: to}
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | if to.IsIntegerType() {
|
---|
1050 | switch {
|
---|
1051 | case to.IsSignedType():
|
---|
1052 | return (&operand{abi: o.abi, typ: to, value: Int64Value(v)}).normalize(ctx, n)
|
---|
1053 | default:
|
---|
1054 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(v)}).normalize(ctx, n)
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | switch to.Kind() {
|
---|
1059 | case ComplexFloat:
|
---|
1060 | switch {
|
---|
1061 | case o.Type().IsSignedType():
|
---|
1062 | return (&operand{abi: o.abi, typ: to, value: Complex64Value(complex(float64(int64(v)), 0))}).normalize(ctx, n)
|
---|
1063 | default:
|
---|
1064 | return (&operand{abi: o.abi, typ: to, value: Complex64Value(complex(float64(v), 0))}).normalize(ctx, n)
|
---|
1065 | }
|
---|
1066 | case ComplexDouble:
|
---|
1067 | switch {
|
---|
1068 | case o.Type().IsSignedType():
|
---|
1069 | return (&operand{abi: o.abi, typ: to, value: Complex128Value(complex(float64(int64(v)), 0))}).normalize(ctx, n)
|
---|
1070 | default:
|
---|
1071 | return (&operand{abi: o.abi, typ: to, value: Complex128Value(complex(float64(v), 0))}).normalize(ctx, n)
|
---|
1072 | }
|
---|
1073 | case Float:
|
---|
1074 | switch {
|
---|
1075 | case o.Type().IsSignedType():
|
---|
1076 | return (&operand{abi: o.abi, typ: to, value: Float32Value(float64(int64(v)))}).normalize(ctx, n)
|
---|
1077 | default:
|
---|
1078 | return (&operand{abi: o.abi, typ: to, value: Float32Value(float64(v))}).normalize(ctx, n)
|
---|
1079 | }
|
---|
1080 | case ComplexLongDouble:
|
---|
1081 | panic(todo("", n.Position()))
|
---|
1082 | case Double:
|
---|
1083 | switch {
|
---|
1084 | case o.Type().IsSignedType():
|
---|
1085 | return (&operand{abi: o.abi, typ: to, value: Float64Value(int64(v))}).normalize(ctx, n)
|
---|
1086 | default:
|
---|
1087 | return (&operand{abi: o.abi, typ: to, value: Float64Value(v)}).normalize(ctx, n)
|
---|
1088 | }
|
---|
1089 | case LongDouble:
|
---|
1090 | switch {
|
---|
1091 | case o.Type().IsSignedType():
|
---|
1092 | return (&operand{abi: o.abi, typ: to, value: &Float128Value{N: big.NewFloat(0).SetInt64(int64(v))}}).normalize(ctx, n)
|
---|
1093 | default:
|
---|
1094 | return (&operand{abi: o.abi, typ: to, value: &Float128Value{N: big.NewFloat(0).SetUint64(v)}}).normalize(ctx, n)
|
---|
1095 | }
|
---|
1096 | case Ptr:
|
---|
1097 | return (&operand{abi: o.abi, typ: to, value: Uint64Value(v)}).normalize(ctx, n)
|
---|
1098 | case Struct, Union, Array, Void, Int128, UInt128:
|
---|
1099 | return &operand{abi: o.abi, typ: to}
|
---|
1100 | case Vector:
|
---|
1101 | if o.Type().Size() == to.Size() {
|
---|
1102 | return &operand{abi: o.abi, typ: to}
|
---|
1103 | }
|
---|
1104 | }
|
---|
1105 | if ctx != nil {
|
---|
1106 | ctx.errNode(n, "cannot convert %s to %s", o.Type(), to)
|
---|
1107 | }
|
---|
1108 | return &operand{abi: o.abi, typ: to}
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | func (o *operand) normalize(ctx *context, n Node) (r Operand) {
|
---|
1112 | if o.Type() == nil {
|
---|
1113 | ctx.errNode(n, "operand has unsupported, invalid or incomplete type")
|
---|
1114 | return noOperand
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | if o.Type().IsIntegerType() {
|
---|
1118 | switch {
|
---|
1119 | case o.Type().IsSignedType():
|
---|
1120 | if x, ok := o.value.(Uint64Value); ok {
|
---|
1121 | o.value = Int64Value(x)
|
---|
1122 | }
|
---|
1123 | default:
|
---|
1124 | if x, ok := o.value.(Int64Value); ok {
|
---|
1125 | o.value = Uint64Value(x)
|
---|
1126 | }
|
---|
1127 | }
|
---|
1128 | switch x := o.Value().(type) {
|
---|
1129 | case Int64Value:
|
---|
1130 | if v := convertInt64(int64(x), o.Type(), o.abi); v != int64(x) {
|
---|
1131 | o.value = Int64Value(v)
|
---|
1132 | }
|
---|
1133 | case Uint64Value:
|
---|
1134 | v := uint64(x)
|
---|
1135 | switch o.Type().Size() {
|
---|
1136 | case 1:
|
---|
1137 | v &= 0xff
|
---|
1138 | case 2:
|
---|
1139 | v &= 0xffff
|
---|
1140 | case 4:
|
---|
1141 | v &= 0xffffffff
|
---|
1142 | }
|
---|
1143 | if v != uint64(x) {
|
---|
1144 | o.value = Uint64Value(v)
|
---|
1145 | }
|
---|
1146 | case *InitializerValue, nil:
|
---|
1147 | // ok
|
---|
1148 | default:
|
---|
1149 | panic(internalErrorf("%T %v", x, x))
|
---|
1150 | }
|
---|
1151 | return o
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | switch o.Type().Kind() {
|
---|
1155 | case ComplexFloat:
|
---|
1156 | switch o.Value().(type) {
|
---|
1157 | case Complex64Value, nil:
|
---|
1158 | return o
|
---|
1159 | default:
|
---|
1160 | panic(todo(""))
|
---|
1161 | }
|
---|
1162 | case ComplexDouble:
|
---|
1163 | switch o.Value().(type) {
|
---|
1164 | case Complex128Value, nil:
|
---|
1165 | return o
|
---|
1166 | default:
|
---|
1167 | panic(todo(""))
|
---|
1168 | }
|
---|
1169 | case ComplexLongDouble:
|
---|
1170 | switch o.Value().(type) {
|
---|
1171 | case Complex256Value, nil:
|
---|
1172 | return o
|
---|
1173 | default:
|
---|
1174 | panic(todo("934 %v", o.Type().Kind()))
|
---|
1175 | }
|
---|
1176 | case Float:
|
---|
1177 | switch o.Value().(type) {
|
---|
1178 | case Float32Value, *InitializerValue, nil:
|
---|
1179 | return o
|
---|
1180 | default:
|
---|
1181 | panic(todo(""))
|
---|
1182 | }
|
---|
1183 | case Double:
|
---|
1184 | switch x := o.Value().(type) {
|
---|
1185 | case Float64Value, *InitializerValue, nil:
|
---|
1186 | return o
|
---|
1187 | default:
|
---|
1188 | panic(internalErrorf("%T %v", x, x))
|
---|
1189 | }
|
---|
1190 | case LongDouble:
|
---|
1191 | switch x := o.Value().(type) {
|
---|
1192 | case *Float128Value, nil:
|
---|
1193 | return o
|
---|
1194 | default:
|
---|
1195 | panic(internalErrorf("%T %v TODO980 %v", x, x, n.Position()))
|
---|
1196 | }
|
---|
1197 | case Ptr:
|
---|
1198 | switch o.Value().(type) {
|
---|
1199 | case Int64Value, Uint64Value, *InitializerValue, StringValue, WideStringValue, nil:
|
---|
1200 | return o
|
---|
1201 | default:
|
---|
1202 | panic(todo(""))
|
---|
1203 | }
|
---|
1204 | case Array, Void, Function, Struct, Union, Vector, Decimal32, Decimal64, Decimal128:
|
---|
1205 | return o
|
---|
1206 | case ComplexChar, ComplexInt, ComplexLong, ComplexLongLong, ComplexShort, ComplexUInt, ComplexUShort:
|
---|
1207 | //TOD
|
---|
1208 | if ctx != nil {
|
---|
1209 | ctx.errNode(n, "unsupported type: %s", o.Type())
|
---|
1210 | }
|
---|
1211 | return noOperand
|
---|
1212 | }
|
---|
1213 | panic(internalErrorf("%v, %v", o.Type(), o.Type().Kind()))
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | func convertInt64(n int64, t Type, abi *ABI) int64 {
|
---|
1217 | k := t.Kind()
|
---|
1218 | if k == Enum {
|
---|
1219 | //TODO
|
---|
1220 | }
|
---|
1221 | signed := abi.isSignedInteger(k)
|
---|
1222 | switch sz := abi.size(k); sz {
|
---|
1223 | case 1:
|
---|
1224 | switch {
|
---|
1225 | case signed:
|
---|
1226 | switch {
|
---|
1227 | case int8(n) < 0:
|
---|
1228 | return n | ^math.MaxUint8
|
---|
1229 | default:
|
---|
1230 | return n & math.MaxUint8
|
---|
1231 | }
|
---|
1232 | default:
|
---|
1233 | return n & math.MaxUint8
|
---|
1234 | }
|
---|
1235 | case 2:
|
---|
1236 | switch {
|
---|
1237 | case signed:
|
---|
1238 | switch {
|
---|
1239 | case int16(n) < 0:
|
---|
1240 | return n | ^math.MaxUint16
|
---|
1241 | default:
|
---|
1242 | return n & math.MaxUint16
|
---|
1243 | }
|
---|
1244 | default:
|
---|
1245 | return n & math.MaxUint16
|
---|
1246 | }
|
---|
1247 | case 4:
|
---|
1248 | switch {
|
---|
1249 | case signed:
|
---|
1250 | switch {
|
---|
1251 | case int32(n) < 0:
|
---|
1252 | return n | ^math.MaxUint32
|
---|
1253 | default:
|
---|
1254 | return n & math.MaxUint32
|
---|
1255 | }
|
---|
1256 | default:
|
---|
1257 | return n & math.MaxUint32
|
---|
1258 | }
|
---|
1259 | default:
|
---|
1260 | return n
|
---|
1261 | }
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | func boolValue(b bool) Value {
|
---|
1265 | if b {
|
---|
1266 | return Int64Value(1)
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | return Int64Value(0)
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | type initializer interface {
|
---|
1273 | List() []*Initializer
|
---|
1274 | IsConst() bool
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | type InitializerValue struct {
|
---|
1278 | typ Type
|
---|
1279 | initializer initializer
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | func (v *InitializerValue) List() []*Initializer {
|
---|
1283 | if v == nil || v.initializer == nil {
|
---|
1284 | return nil
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | return v.initializer.List()
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | func (v *InitializerValue) IsConst() bool {
|
---|
1291 | return v != nil && v.initializer != nil && v.initializer.IsConst()
|
---|
1292 | }
|
---|
1293 | func (v *InitializerValue) Type() Type { return v.typ }
|
---|
1294 | func (v *InitializerValue) add(b Value) Value { return nil }
|
---|
1295 | func (v *InitializerValue) and(b Value) Value { return nil }
|
---|
1296 | func (v *InitializerValue) cpl() Value { return nil }
|
---|
1297 | func (v *InitializerValue) div(b Value) Value { return nil }
|
---|
1298 | func (v *InitializerValue) eq(b Value) Value { return nil }
|
---|
1299 | func (v *InitializerValue) ge(b Value) Value { return nil }
|
---|
1300 | func (v *InitializerValue) gt(b Value) Value { return nil }
|
---|
1301 | func (v *InitializerValue) le(b Value) Value { return nil }
|
---|
1302 | func (v *InitializerValue) lsh(b Value) Value { return nil }
|
---|
1303 | func (v *InitializerValue) lt(b Value) Value { return nil }
|
---|
1304 | func (v *InitializerValue) mod(b Value) Value { return nil }
|
---|
1305 | func (v *InitializerValue) mul(b Value) Value { return nil }
|
---|
1306 | func (v *InitializerValue) neg() Value { return nil }
|
---|
1307 | func (v *InitializerValue) neq(b Value) Value { return nil }
|
---|
1308 | func (v *InitializerValue) or(b Value) Value { return nil }
|
---|
1309 | func (v *InitializerValue) rsh(b Value) Value { return nil }
|
---|
1310 | func (v *InitializerValue) sub(b Value) Value { return nil }
|
---|
1311 | func (v *InitializerValue) xor(b Value) Value { return nil }
|
---|
1312 |
|
---|
1313 | func (v *InitializerValue) IsNonZero() bool {
|
---|
1314 | if v == nil {
|
---|
1315 | return false
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | for _, v := range v.List() {
|
---|
1319 | if !v.AssignmentExpression.Operand.IsZero() {
|
---|
1320 | return true
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 | return false
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | func (v *InitializerValue) IsZero() bool {
|
---|
1327 | if v == nil {
|
---|
1328 | return false
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | for _, v := range v.List() {
|
---|
1332 | if !v.AssignmentExpression.Operand.IsZero() {
|
---|
1333 | return false
|
---|
1334 | }
|
---|
1335 | }
|
---|
1336 | return true
|
---|
1337 | }
|
---|