/
cost.go
208 lines (189 loc) · 4.71 KB
/
cost.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
package sexp
import (
"cfg"
"vmm"
)
// CostOfList calls each form Cost method and returns
// accumulated cost sum.
func CostOfList(forms []Form) int {
total := 0
for _, form := range forms {
total += form.Cost()
}
return total
}
// CostOf is a convenience wrapper over CostOfList.
func CostOf(forms ...Form) int {
return CostOfList(forms)
}
// costOfCall returns function invocation cost.
// Does not account called function complexity.
func costOfCall(args []Form) int {
return CostOfList(args) + len(args)/2 + cfg.CostFnCallBase
}
func max2(a, b int) int {
if a > b {
return a
}
return b
}
func maxClauseCost(b *SwitchBody) int {
res := 0
for _, cc := range b.Clauses {
res = max2(res, cc.Expr.Cost()+cc.Body.Cost()+1)
}
res = max2(res, b.DefaultBody.Cost())
return res
}
func costOfSpan(span Span) int {
cost := 0
if span.Low != nil {
cost += span.Low.Cost()
}
if span.High != nil {
cost += span.High.Cost()
}
return cost
}
func costOfBindList(binds []*Bind) int {
cost := 0
for _, bind := range binds {
cost += bind.Init.Cost()
}
return cost
}
func (atom Bool) Cost() int { return 1 }
func (atom Int) Cost() int { return 1 }
func (atom Float) Cost() int { return 1 }
func (atom Str) Cost() int { return 1 }
func (atom Symbol) Cost() int { return 1 }
func (atom Var) Cost() int { return 1 }
func (atom Local) Cost() int { return 1 }
func (lit *ArrayLit) Cost() int {
return CostOfList(lit.Vals) + 2
}
func (lit *SparseArrayLit) Cost() int {
return CostOfList(lit.Vals)*3 + 2
}
func (lit *SliceLit) Cost() int {
// #REFS: 76.
// "10" -> "rt.FnMakeSlice.Complexity".
return CostOfList(lit.Vals) + 10
}
func (lit *StructLit) Cost() int {
// #REFS: 77.
return CostOfList(lit.Vals) + 2
}
func (form *ArrayUpdate) Cost() int {
return CostOf(form.Array, form.Expr, form.Index) + 2
}
func (form *SliceUpdate) Cost() int {
// #REFS: 76.
// "6" -> "rt.FnSliceSet.Complexity".
return CostOf(form.Slice, form.Expr, form.Index) + 6
}
func (form *StructUpdate) Cost() int {
// #REFS: 77.
return CostOf(form.Struct, form.Expr) + 2
}
func (form *Bind) Cost() int {
return CostOf(form.Init)
}
func (form *Rebind) Cost() int {
return CostOf(form.Expr) + 1
}
func (form *VarUpdate) Cost() int {
return CostOf(form.Expr) + 1
}
func (form FormList) Cost() int {
return CostOfList(form)
}
func (form Block) Cost() int {
return CostOfList(form) + 1
}
func (form *If) Cost() int {
maxBranchCost := max2(form.Then.Cost(), form.Else.Cost())
return form.Cond.Cost() + maxBranchCost + 2
}
func (form *Switch) Cost() int {
// #REFS: 90, 91.
// return form.Expr.Cost() + maxClauseCost(&form.SwitchBody) + 2
return -1
}
func (form *SwitchTrue) Cost() int {
// #REFS: 90, 91.
// return maxClauseCost(&form.SwitchBody) + 2
return -1
}
func (form *Return) Cost() int {
return CostOfList(form.Results) + len(form.Results)
}
func (form *ExprStmt) Cost() int {
return form.Expr.Cost() + 1
}
func (form *Goto) Cost() int { return 1 }
func (form *Label) Cost() int { return 0 }
func (form *Repeat) Cost() int {
return form.Body.Cost() * int(form.N)
}
func (form *DoTimes) Cost() int { return -1 }
func (form *Loop) Cost() int { return -1 }
func (form *While) Cost() int { return -1 }
func (form *ArrayIndex) Cost() int {
return CostOf(form.Array, form.Index) + 1
}
func (form *SliceIndex) Cost() int {
// #REFS: 76.
// "4" -> "rt.FnSliceGet.Complexity".
return CostOf(form.Slice, form.Index) + 4
}
func (form *StructIndex) Cost() int {
switch vmm.StructReprOf(form.Typ) {
case vmm.StructUnit:
return 1 + form.Struct.Cost()
case vmm.StructCons:
return 1 + form.Index + form.Struct.Cost()
case vmm.StructVec:
return 3 + form.Struct.Cost()
default:
return 0
}
}
func (form *ArraySlice) Cost() int {
return form.Array.Cost() + costOfSpan(form.Span) + 4
}
func (form *SliceSlice) Cost() int {
return form.Slice.Cost() + costOfSpan(form.Span) + 6
}
func (form *TypeAssert) Cost() int {
return form.Expr.Cost() + 4
}
func (call *Call) Cost() int {
return costOfCall(call.Args)
}
func (call *LispCall) Cost() int {
if cost := vmm.InstrCallCost(call.Fn.Name); cost != 0 {
return cost + CostOfList(call.Args)
}
if vmm.FuncIsThrowing(call.Fn.Name) {
return costOfCall(call.Args) + cfg.CostThrowFuncPenalty
}
return costOfCall(call.Args)
}
func (call *LambdaCall) Cost() int {
return call.Body.Cost() + costOfBindList(call.Args) + 1
}
func (call *DynCall) Cost() int {
return call.Callable.Cost() + costOfCall(call.Args)
}
func (form *Let) Cost() int {
return form.Expr.Cost() + costOfBindList(form.Bindings)
}
func (form *TypeCast) Cost() int { return 0 }
func (form *And) Cost() int {
return CostOf(form.X, form.Y) + 3
}
func (form *Or) Cost() int {
return CostOf(form.X, form.Y) + 3
}
func (form *emptyForm) Cost() int { return 0 }