-
Notifications
You must be signed in to change notification settings - Fork 9
/
variable.go
366 lines (335 loc) · 7.96 KB
/
variable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"bytes"
"fmt"
"strings"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
"github.com/llir/llvm/ir/value"
)
// VariableName returns the name to use for a local variable or parameter.
func VariableName(v value.Named) string {
name := v.Name()
if name == "" {
return "v" + strings.TrimPrefix(v.Ident(), "%")
}
if c := name[0]; '0' <= c && c <= '9' {
name = "v" + name
}
name = strings.Replace(name, ".", "_", -1)
if invalidNames[name] {
name = "_" + name
}
return name
}
func BlockName(v value.Value) string {
block := v.(*ir.Block)
name := block.Name()
if name == "" {
return "block" + strings.TrimPrefix(block.Ident(), "%")
}
if c := name[0]; '0' <= c && c <= '9' {
name = "block" + name
}
name = strings.Replace(name, ".", "_", -1)
if invalidNames[name] {
name = "_" + name
}
return name
}
var invalidNames = map[string]bool{
"return": true,
"init": true,
}
// FormatValue formats a constant or variable as it should appear in an expression.
func FormatValue(v value.Value) (string, error) {
switch v := v.(type) {
case *ir.Global:
name := VariableName(v)
if types.IsFunc(v.ContentType) {
return name, nil
}
if renamed, ok := libraryGlobals[name]; ok {
name = renamed
}
return "&" + name, nil
case value.Named:
return VariableName(v), nil
case *ir.Arg:
return FormatValue(v.Value)
case *constant.Array:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
b := new(bytes.Buffer)
if len(v.Elems) < 16 {
b.WriteString(t)
b.WriteByte('{')
for i, c := range v.Elems {
if i > 0 {
b.WriteString(", ")
}
e, err := FormatValue(c)
if err != nil {
return "", fmt.Errorf("error translating element %d (%v): %v", i, c, err)
}
fmt.Fprint(b, e)
}
b.WriteByte('}')
} else {
b.WriteString(t)
b.WriteString("{\n\t")
for i, c := range v.Elems {
if i > 0 {
if i%16 == 0 {
b.WriteString(",\n\t")
} else {
b.WriteString(", ")
}
}
e, err := FormatValue(c)
if err != nil {
return "", fmt.Errorf("error translating element %d (%v): %v", i, c, err)
}
fmt.Fprint(b, e)
}
b.WriteString(",\n}")
}
return b.String(), nil
case *constant.CharArray:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
b := new(bytes.Buffer)
if len(v.X) < 16 {
b.WriteString(t)
b.WriteByte('{')
for i, c := range v.X {
if i > 0 {
b.WriteString(", ")
}
fmt.Fprintf(b, "%d", c)
}
b.WriteByte('}')
} else {
b.WriteString(t)
b.WriteString("{\n\t")
for i, c := range v.X {
if i > 0 {
if i%16 == 0 {
b.WriteString(",\n\t")
} else {
b.WriteString(", ")
}
}
fmt.Fprintf(b, "%d", c)
}
b.WriteString(",\n}")
}
return b.String(), nil
case *constant.ExprBitCast:
from, err := FormatValue(v.From)
if err != nil {
return "", fmt.Errorf("error translating source (%v): %v", v.From, err)
}
to, err := TypeSpec(v.To)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.To, err)
}
return fmt.Sprintf("(%s)(unsafe.Pointer(%s))", to, from), nil
case *constant.ExprGetElementPtr:
indices := make([]value.Value, len(v.Indices))
for i, index := range v.Indices {
indices[i] = index
}
return GetElementPtr(v.ElemType, v.Src, indices)
case *constant.Float:
result := v.X.String()
special := false
switch result {
case "+Inf":
result = "math.Inf(1)"
special = true
case "-Inf":
result = "math.Inf(-1)"
special = true
case "NaN":
result = "math.NaN()"
special = true
}
if special && v.Typ.Kind == types.FloatKindFloat {
result = fmt.Sprintf("float32(%s)", result)
}
return result, nil
case *constant.Index:
return FormatValue(v.Constant)
case *constant.Int:
var value int64
switch {
case v.X.IsInt64():
value = v.X.Int64()
case v.X.IsUint64():
value = int64(v.X.Uint64())
default:
return "", fmt.Errorf("integer constant too large: %v", v.X)
}
switch v.Typ.BitSize {
case 1:
if value != 0 {
return "true", nil
}
return "false", nil
case 8:
return fmt.Sprint(byte(value)), nil
case 16:
return fmt.Sprint(int16(value)), nil
case 32:
return fmt.Sprint(int32(value)), nil
default:
return fmt.Sprint(value), nil
}
case *constant.Null:
return "nil", nil
case *constant.Struct:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
b := new(bytes.Buffer)
b.WriteString(t)
b.WriteByte('{')
for i, c := range v.Fields {
if i > 0 {
b.WriteString(", ")
}
e, err := FormatValue(c)
if err != nil {
return "", fmt.Errorf("error translating field %d (%v): %v", i, c, err)
}
fmt.Fprint(b, e)
}
b.WriteByte('}')
return b.String(), nil
case *constant.Undef:
switch v.Typ.(type) {
case *types.ArrayType, *types.StructType, *types.VectorType:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
return t + "{}", nil
case *types.IntType, *types.FloatType:
return "0", nil
case *types.PointerType:
return "nil", nil
default:
return "", fmt.Errorf("unsupported type for undefined constant: %v", v.Typ)
}
case *constant.Vector:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
b := new(bytes.Buffer)
b.WriteString(t)
b.WriteByte('{')
for i, c := range v.Elems {
if i > 0 {
b.WriteString(", ")
}
e, err := FormatValue(c)
if err != nil {
return "", fmt.Errorf("error translating element %d (%v): %v", i, c, err)
}
fmt.Fprint(b, e)
}
b.WriteByte('}')
return b.String(), nil
case *constant.ZeroInitializer:
t, err := TypeSpec(v.Typ)
if err != nil {
return "", fmt.Errorf("error translating type (%v): %v", v.Typ, err)
}
return t + "{}", nil
default:
return "", fmt.Errorf("unsupported type of value to translate: %T", v)
}
}
var libraryGlobals = map[string]string{
"stdin": "os.Stdin",
"stdout": "os.Stdout",
"stderr": "os.Stderr",
}
// FormatSigned is like FormatValue, except that it converts "byte" to "int8".
func FormatSigned(v value.Value) (string, error) {
result, err := FormatValue(v)
if err != nil {
return "", err
}
if ci, ok := v.(*constant.Int); ok {
if ci.Typ.BitSize == 8 {
return fmt.Sprint(int8(ci.X.Int64())), nil
}
return result, nil
}
if t, ok := v.Type().(*types.IntType); ok && t.BitSize == 8 {
return fmt.Sprintf("int8(%s)", result), nil
}
return result, nil
}
// FormatUnsigned is like FormatValue, except that it converts integer types to
// unsigned.
func FormatUnsigned(v value.Value) (string, error) {
result, err := FormatValue(v)
if err != nil {
return "", err
}
if ci, ok := v.(*constant.Int); ok {
var value uint64
switch {
case ci.X.IsUint64():
value = ci.X.Uint64()
case ci.X.IsInt64():
value = uint64(ci.X.Int64())
switch ci.Typ.BitSize {
case 8:
return fmt.Sprintf("byte(%d)", byte(value)), nil
case 16:
return fmt.Sprintf("uint16(%d)", uint16(value)), nil
case 32:
return fmt.Sprintf("uint32(%d)", uint32(value)), nil
default:
return fmt.Sprint(value), nil
}
default:
return "", fmt.Errorf("integer constant too large: %v", ci.X)
}
switch ci.Typ.BitSize {
case 1:
if value != 0 {
return "true", nil
}
return "false", nil
case 8:
return fmt.Sprint(byte(value)), nil
case 16:
return fmt.Sprint(uint16(value)), nil
case 32:
return fmt.Sprint(uint32(value)), nil
default:
return fmt.Sprint(value), nil
}
}
switch t := v.Type().(type) {
case *types.IntType:
if t.BitSize > 8 {
return fmt.Sprintf("uint%d(%s)", t.BitSize, result), nil
}
case *types.PointerType:
return fmt.Sprintf("uintptr(unsafe.Pointer(%s))", result), nil
}
return result, nil
}