/
operand.go
148 lines (122 loc) · 2.96 KB
/
operand.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
// Copyright (c) 2016 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package operand
import (
"fmt"
"gate.computer/wag/internal/gen/condition"
"gate.computer/wag/internal/gen/debug"
"gate.computer/wag/internal/gen/reg"
"gate.computer/wag/internal/gen/storage"
"gate.computer/wag/wa"
)
const (
payloadDebug = uint64(0x0c7a0c707e7a7e55) // "octa octo teta tess"
)
type O struct {
Storage storage.Storage
Type wa.Type
UnreachableFallback bool
payload uint64
}
func Placeholder(t wa.Type) (o O) {
o.Storage = storage.Imm // Least dangerous
o.Type = t
if debug.Enabled {
o.payload = payloadDebug
}
return
}
func Imm(t wa.Type, value uint64) O {
return O{
Storage: storage.Imm,
Type: t,
payload: value,
}
}
func Reg(t wa.Type, r reg.R) O {
return O{
Storage: storage.Reg,
Type: t,
payload: uint64(byte(r)),
}
}
func Flags(cond condition.C) O {
return O{
Storage: storage.Flags,
Type: wa.I32,
payload: uint64(cond),
}
}
func UnreachableSentinel() O {
return O{
Storage: storage.Unreachable,
}
}
func UnreachableFallback(t wa.Type) O {
return O{
Storage: storage.Imm,
Type: t,
UnreachableFallback: true,
}
}
func (o O) Size() wa.Size {
return o.Type.Size()
}
func (o *O) SetPlaceholder() {
o.Storage = storage.Imm // Least dangerous
if debug.Enabled {
o.payload = payloadDebug
}
}
func (o *O) SetReg(r reg.R) {
o.Storage = storage.Reg
o.payload = uint64(r)
}
func (o *O) SetStack() {
o.Storage = storage.Stack
}
func (o O) ImmValue() int64 {
if o.Size() == wa.Size32 {
return int64(int32(uint32(o.payload)))
} else {
return int64(o.payload)
}
}
func (o O) ImmValue8() int8 { return int8(uint8(o.payload)) }
func (o O) Reg() reg.R { return reg.R(byte(o.payload)) }
func (o O) FlagsCond() condition.C { return condition.C(int(o.payload)) }
func (o O) String() string {
if o.UnreachableFallback {
return fmt.Sprintf("unreachable fallback %s", o.Type)
}
if debug.Enabled && o.payload == payloadDebug {
if o.Storage == storage.Imm {
return fmt.Sprintf("%s placeholder", o.Type)
} else {
return fmt.Sprintf("magic %s value 0x%08x surprisingly in %s storage", o.Type, o.payload, o.Storage)
}
}
switch o.Storage {
case storage.Imm:
if o.Type.Category() == wa.Int {
return fmt.Sprintf("constant %s 0x%x", o.Type, o.payload)
} else {
return fmt.Sprintf("constant %s bits 0x%x", o.Type, o.payload)
}
case storage.Stack:
return fmt.Sprintf("saved %s in stack", o.Type)
case storage.Reg:
if o.Reg() == reg.Result {
return fmt.Sprintf("volatile %s in result register", o.Type)
} else {
return fmt.Sprintf("allocated %s in %s", o.Type, o.Reg())
}
case storage.Flags:
return fmt.Sprintf("volatile %s comparison result", o.FlagsCond())
case storage.Unreachable:
return "unreachable sentinel"
default:
return "<invalid operand>"
}
}