-
Notifications
You must be signed in to change notification settings - Fork 13
/
function.go
145 lines (111 loc) · 3.04 KB
/
function.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
package frontend
import (
"fmt"
_ "strconv"
)
type Variables map[string]Var
func (self Variables) String() (ret string) {
for k, v := range self {
ret += fmt.Sprintf(" %-14s : %d\n", k, v.Offset())
}
return
}
type ArgList []Var
type Function struct {
CallTarget *IntInstr
Id string
ArgCount int
Ret bool
VarTable Variables
ArgTable ArgList
frame, offset int
}
func NewFunction(id string, target *IntInstr, ac int, ret bool) *Function {
return &Function{target, id, ac, ret, make(map[string]Var), nil, 0, 0}
}
func (self *Function) String() string {
return fmt.Sprintf(`
### %s ###
Arguments: %d
Var Table:
%v
`, self.Id, self.ArgCount, self.VarTable)
}
func (self *Function) GenOffset() int {
return self.Size()
}
func (self *Function) NewVar(id string, typ varType) (Var, error) {
if self.VarTable[id] != nil {
return nil, fmt.Errorf("redeclaration of '%v'", id)
}
var v *Variable
switch typ {
case varNumTy:
v = NewNumeric(id, self.Size())
// TODO other types
default:
v = NewNumeric(id, self.Size())
}
v.offset = self.Size()
v.typ = typ
self.VarTable[id] = v
return v, nil
}
func (self *Function) PushArg(v Var) {
self.ArgTable = append(self.ArgTable, v)
}
func (self *Function) SetVar(v Var) {
self.VarTable[v.Id()] = v
}
func (self *Function) GetVar(id string) Var {
return self.VarTable[id]
}
func (self *Function) Size() (size int) {
for _, variable := range self.VarTable {
size += variable.Size()
}
return
}
func (self *Function) Call(args []*SyntaxTree, gen *IntGen, scope Scope) *IntInstr {
if len(args) != self.ArgCount {
gen.addError(fmt.Errorf("%s takes %d arguments. Received %d arguments", self.Id, self.ArgCount, len(args)))
return newIntInstr(IntIgnore, "")
}
// Push all arguments on to the local VM stack
argsPush := newIntInstr(IntIgnore, "")
for _, arg := range args {
push := gen.MakeIntCode(arg)
concat(argsPush, push)
}
// Push new stack frame
stack := gen.PushStack()
// Pop stack values and assign to memory
argsPop := newIntInstr(IntIgnore, "")
for i, _ := range args {
pop := gen.assignMemory(self.ArgTable[i].Offset())
concat(argsPop, pop)
}
pc := newIntInstr(IntPc, "") // 1
push := gen.makePush("17") // 2
add := newIntInstr(IntAdd, "") // 1
ret := gen.loadStackPtr() // 3
offset := gen.makePush("32") // 2
add2 := newIntInstr(IntAdd, "") // 1
retStore := newIntInstr(IntMStore, "") // 1
p, jmp := newJumpInstr(IntJump) // 6
jmp.Target = self.CallTarget
retTarget := newIntInstr(IntTarget, "")
cc(argsPush, stack, argsPop, pc, push, add, ret, offset, add2, retStore, p, retTarget)
return argsPush
}
func (self *Function) MakeReturn(expr *SyntaxTree, gen *IntGen) *IntInstr {
retVal := gen.MakeIntCode(expr)
rPos := gen.loadStackPtr()
stack := gen.PopStack()
offset := gen.makePush("32")
add := newIntInstr(IntAdd, "")
rPosLoad := newIntInstr(IntMLoad, "")
jumpBack := newIntInstr(IntJump, "")
cc(retVal, rPos, stack, offset, add, rPosLoad, jumpBack)
return retVal
}