-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloop.go
More file actions
134 lines (113 loc) · 2.97 KB
/
loop.go
File metadata and controls
134 lines (113 loc) · 2.97 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
129
130
131
132
133
134
// Copyright (c) 2013 Ostap Cherkashin. You can use this source code
// under the terms of the MIT License found in the LICENSE file.
package main
/*
The following comprehension:
[i * j | i <- [1, 2, 3], j <- [10, 20], i == j / 10]
Will be compiled as follows:
// list [1, 2, 3]
loop +L1
// list [10, 20]
loop +L2
// i == j / 10
test -T1 // back to L2
// R: append(res, i * j)
next -N1 // back to L2
next -N2 // back to L1
Where L1, L2, T1, N1, N2 are relative code offsets (jumps):
+-------->----------->----------+
| +-->----------->-------+ |
| | +-------->-+ | |
L1 -> L2 -> T1 -> R -> N2 -> N1 v
| | | |
| +-<----------->-+ |
+--------<----------->-------+
*/
type Loop struct {
lid int
inner *Loop
resAddr int
varAddr int
list Expr
sel []Expr
ret Expr
parallel bool
}
func ForEach(lid int, varAddr int, list Expr, parallel bool) *Loop {
return &Loop{lid, nil, -1, varAddr, list, nil, BadExpr, parallel}
}
func (l *Loop) Code() []Op {
code := l.list.Code()
clen := l.codeLen(-1)
// jump over the loop
loopJump := clen + 1 /* OpNext */ + 1 /* next instruction after the loop */
// jump back to beginning
nextJump := -clen
parallel := 0
if l.parallel {
parallel = 1
}
code = append(code, OpArg(loopJump))
code = append(code, OpArg(parallel))
code = append(code, OpLoop(l.lid))
code = append(code, OpStore(l.varAddr))
for i, s := range l.sel { // select(s)
for _, c := range s.Code() {
code = append(code, c)
}
code = append(code, OpTest(l.codeLen(i)))
}
if l.inner != nil { // nested loop(s)
for _, c := range l.inner.Code() {
code = append(code, c)
}
} else { // return
code = append(code, OpLoad(l.resAddr))
for _, c := range l.ret.Code() {
code = append(code, c)
}
code = append(code, OpAppend())
code = append(code, OpStore(l.resAddr))
}
code = append(code, OpArg(nextJump))
return append(code, OpNext(l.lid))
}
func (l *Loop) Nest(lid int, varAddr int, list Expr, parallel bool) *Loop {
l.innermost().inner = &Loop{lid, nil, -1, varAddr, list, nil, BadExpr, parallel}
return l
}
func (l *Loop) Select(expr Expr) *Loop {
i := l.innermost()
i.sel = append(i.sel, expr)
return l
}
func (l *Loop) Return(expr Expr, resAddr int) *Loop {
l.innermost().ret = expr
l.innermost().resAddr = resAddr
return l
}
func (l *Loop) innermost() *Loop {
i := l
for i.inner != nil {
i = i.inner
}
return i
}
// codeLen calculates the jump from a test instruction. selPos is the index
// of a select statement (left to right). passing -1 will calculate the length
// of the whole loop.
func (l *Loop) codeLen(selPos int) int {
jump := 0
if selPos < 0 {
jump++ /* OpStore */
}
for i := selPos + 1; i < len(l.sel); i++ {
jump += len(l.sel[i].Code()) + 1 /* OpTest */
}
if l.inner != nil {
jump += len(l.inner.Code())
} else {
jump += 1 /* OpLoad */ + len(l.ret.Code()) + 1 /* OpAppend */ + 1 /* OpStore */
}
return jump + 1 /* OpArg */
}