-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_assembly_test.go
More file actions
128 lines (119 loc) · 3.56 KB
/
Copy pathfunction_assembly_test.go
File metadata and controls
128 lines (119 loc) · 3.56 KB
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
package ember
import (
"reflect"
"testing"
)
var functionAssemblyProtoSink *Proto
func TestFunctionAssemblyOwnsCodeMappingLinesAndWordcode(t *testing.T) {
source := "local value = 1\nvalue = value + 1\nreturn value\n"
ir := []bytecodeIRInstruction{
lowerInstructionToBytecodeIR(
instruction{op: opLoadConst, a: 0, b: 0},
sourceRange{start: 0, end: 15},
),
lowerInstructionToBytecodeIR(
instruction{op: opJump, b: 2},
sourceRange{start: 16, end: 33},
),
lowerInstructionToBytecodeIR(
instruction{op: opReturnOne, a: 0},
sourceRange{start: 34, end: 46},
),
}
assembly := assembleFunctionBytecode(newSourceLineMap(source), ir)
wantCode := []instruction{
{op: opLoadConst, a: 0, b: 0},
{op: opReturnOne, a: 0},
}
if !reflect.DeepEqual(assembly.code, wantCode) {
t.Fatalf("assembled code is %#v, want %#v", assembly.code, wantCode)
}
if want := []int{0, 1, 1, 2}; !reflect.DeepEqual(assembly.oldToNew, want) {
t.Fatalf("old-to-new PC map is %#v, want %#v", assembly.oldToNew, want)
}
if want := []sourceRange{{start: 0, end: 15}, {start: 34, end: 46}}; !reflect.DeepEqual(assembly.sources, want) {
t.Fatalf("source anchors are %#v, want %#v", assembly.sources, want)
}
if want := []int{1, 3}; !reflect.DeepEqual(assembly.lines, want) {
t.Fatalf("source lines are %#v, want %#v", assembly.lines, want)
}
words, err := encodeWordcode(assembly.code, 1, 1)
if err != nil {
t.Fatalf("encodeWordcode returned error: %v", err)
}
decoded, err := decodeWordcode(words)
if err != nil {
t.Fatalf("decodeWordcode returned error: %v", err)
}
if !reflect.DeepEqual(decoded, assembly.code) {
t.Fatalf("decoded wordcode is %#v, want %#v", decoded, assembly.code)
}
boundaries, err := wordcodeBoundaries(assembly.code)
if err != nil {
t.Fatalf("wordcodeBoundaries returned error: %v", err)
}
if got := wordcodeLogicalLineMap(assembly.lines, boundaries); len(got) != len(words) {
t.Fatalf("word line map has %d entries for %d words", len(got), len(words))
}
}
func TestSourceLineMapMatchesSourceRangeLines(t *testing.T) {
source := "first\nsecond\nthird"
lines := newSourceLineMap(source)
tests := []struct {
span sourceRange
want int
}{
{span: sourceRange{start: 0, end: 5}, want: 1},
{span: sourceRange{start: 6, end: 12}, want: 2},
{span: sourceRange{start: 13, end: 18}, want: 3},
{span: sourceRange{}, want: -1},
{span: sourceRange{start: -1, end: 1}, want: -1},
{span: sourceRange{start: len(source), end: len(source) + 1}, want: -1},
}
for _, test := range tests {
if got := lines.line(test.span); got != test.want {
t.Errorf("line(%#v) = %d, want %d", test.span, got, test.want)
}
}
}
func TestCompileFinalAssemblyAllocationBudget(t *testing.T) {
if allocationInstrumentedTest() {
t.Skip("allocation budgets run only with the normal compiler/runtime instrumentation")
}
tests := []struct {
name string
source string
maxAllocs int
}{
{
name: "tiny_arithmetic",
source: `local x = 1
local y = 2
return (x + y) * 3 - 4 / 2`,
maxAllocs: 158,
},
{
name: "closure_upvalue",
source: `local base = 4
local function add(x)
return base + x
end
return add(3)`,
maxAllocs: 205,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
allocs := testing.AllocsPerRun(25, func() {
proto, err := Compile(test.source)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
functionAssemblyProtoSink = proto
})
if allocs > float64(test.maxAllocs) {
t.Fatalf("Compile used %.0f allocs/op, want at most %d", allocs, test.maxAllocs)
}
})
}
}