/
variables.go
170 lines (153 loc) · 4.13 KB
/
variables.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
package main
import (
"fmt"
"go/ast"
"github.com/droundy/go/x86"
)
type Variable interface {
InMemory() x86.Memory
Type() *ast.Type
Name() string
}
type StackVariable struct {
T *ast.Type
N string
Offset int
}
func (v *StackVariable) InMemory() x86.Memory {
return x86.Memory{ x86.Imm32(v.Offset), x86.ESP, nil, nil }
}
func (v *StackVariable) Type() *ast.Type {
return v.T
}
func (v *StackVariable) Name() string {
return v.N
}
type GlobalVariable struct {
T *ast.Type
N string
}
func (g *GlobalVariable) InMemory() x86.Memory {
return x86.Memory{x86.Symbol(g.N),nil,nil,nil}
}
func (v *GlobalVariable) Type() *ast.Type {
return v.T
}
func (v *GlobalVariable) Name() string {
return v.N
}
// All global variables are accessible via Globals.
var Globals = make(map[string]GlobalVariable)
func DefineGlobal(name string, t *ast.Type) {
Globals[name] = GlobalVariable{ t, name }
}
// Stack variable scope is visible through type.
type Stack struct {
Parent *Stack
Vars map[string]StackVariable
Size int
ReturnSize int
Name string
}
// DefineVariable returns the offset to be subtracted from the stack
// pointer
func (s *Stack) DefineVariable(name string, t *ast.Type, synonymns ...string) int {
if _,ok := s.Vars[name]; ok && name != "_" {
panic(fmt.Sprintf("Cannot define already existing variable %s", name))
}
off := SizeOnStack(t)
s.Size += off
s.Vars[name] = StackVariable{ t, name, s.Size }
for _,n := range synonymns {
s.Vars[n] = StackVariable{ t, name, s.Size }
}
return off
}
// Pop returns the offset to be added to the stack pointer
func (s *Stack) Pop(t *ast.Type) int {
off := SizeOnStack(t)
s.Size -= off
return off
}
// Pop returns the offset to be added to the stack pointer
func (s *Stack) Push(t *ast.Type) int {
off := SizeOnStack(t)
s.Size += off
return off
}
// PopTo returns code to save data from the stack into the variable.
// It also changes the stack size accordingly.
func (s *Stack) PopTo(name string) x86.X86 {
v := s.Lookup(name)
off := SizeOnStack(v.Type())
if TypeToSize(v.Type()) != off {
panic("I can't yet handle types with sizes that aren't a multiple of 4")
}
comment := "Popping to variable "+v.Name()
if v.Name() == "_" {
comment = fmt.Sprint("Popping to ",name," of type ",PrettyType(v.Type()), " at ", v.InMemory())
}
code := []x86.X86{x86.Comment(s.PrettyComments())}
switch off {
case 4:
s.Size -= off
vnew := s.Lookup(name) // Its location relative to stack may have changed!
return x86.RawAssembly(x86.Assembly([]x86.X86{
x86.PopL(x86.EAX),
x86.Commented(x86.MovL(x86.EAX, vnew.InMemory()), comment),
}))
case 8:
s.Size -= 4
vnew := s.Lookup(name) // Its location relative to stack may have changed!
code = append(code,
x86.PopL(x86.EAX),
x86.Commented(x86.MovL(x86.EAX, vnew.InMemory()), comment))
s.Size -= 4
vnew = s.Lookup(name) // Its location relative to stack may have changed again!
code = append(code,
x86.PopL(x86.EAX),
x86.Commented(x86.MovL(x86.EAX, vnew.InMemory().Add(4)), comment))
return x86.RawAssembly(x86.Assembly(code))
default:
panic(fmt.Sprintf("I don't pop variables with length %s", SizeOnStack(v.Type())))
}
panic("This can't happen")
}
func (s *Stack) Lookup(name string) (out Variable) {
offtotal := 0
for {
if s == nil {
if v,ok := Globals[name]; ok {
return &v
}
panic("There is no variable named "+name)
}
offtotal += s.Size
if v,ok := s.Vars[name]; ok {
fmt.Println("For", name, "offtotal is", offtotal, "and v.Offset is", v.Offset)
v.Offset = offtotal - v.Offset
// Because we do this just here, we can't save this Variable for
// later, when the stack pointer might have changed.
return &v
}
s = s.Parent
}
panic("This can never happen")
return
}
func (s *Stack) New(name string) *Stack {
n := Stack{ s, make(map[string]StackVariable), 0, 0, name }
return &n
}
func (s *Stack) PrettyComments() string {
if s == nil {
return ""
}
out := "\n# Stack: " + s.Name
for vn,v := range s.Vars {
out += fmt.Sprint("\n# ", v.Name(), " (", vn, ") size: ", SizeOnStack(v.Type()),
" type: ", PrettyType(v.Type()), " offset: ", v.Offset)
}
out += s.Parent.PrettyComments()
return out
}