1
1
# # Instruction and TapedFunction
2
2
3
3
abstract type AbstractInstruction end
4
- const RawTape = Vector{AbstractInstruction}
5
4
6
5
"""
7
6
Instruction
@@ -29,7 +28,7 @@ mutable struct TapedFunction{F}
29
28
func:: F # maybe a function, a constructor, or a callable object
30
29
arity:: Int
31
30
ir:: Core.CodeInfo
32
- tape:: RawTape
31
+ tape:: Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}
33
32
counter:: Int
34
33
bindings:: Dict{Symbol, Any}
35
34
retval:: Symbol
@@ -46,7 +45,7 @@ mutable struct TapedFunction{F}
46
45
end
47
46
48
47
ir = CodeInfoTools. code_inferred (f, args_type... )
49
- tape = RawTape ()
48
+ tape = Vector {FunctionWrapper{Nothing, Tuple{TapedFunction}}} ()
50
49
bindings = translate! (tape, ir)
51
50
52
51
tf = new {F} (f, length (args), ir, tape, 1 , bindings, :none )
@@ -83,12 +82,15 @@ function (tf::TapedFunction)(args...; callback=nothing)
83
82
ins = tf. tape[tf. counter]
84
83
ins (tf)
85
84
callback != = nothing && callback ()
86
- isa (ins, ReturnInstruction) && break
85
+ tf . retval != = :none && break
87
86
end
88
87
return result (tf)
89
88
end
90
89
91
90
function Base. show (io:: IO , tf:: TapedFunction )
91
+ # we use an extra IOBuffer to collect all the data and then
92
+ # output it once to avoid output interrupt during task context
93
+ # switching
92
94
buf = IOBuffer ()
93
95
println (buf, " TapedFunction:" )
94
96
println (buf, " * .func => $(tf. func) " )
@@ -103,22 +105,6 @@ function Base.show(io::IO, tf::TapedFunction)
103
105
print (io, String (take! (buf)))
104
106
end
105
107
106
- function Base. show (io:: IO , rtape:: RawTape )
107
- # we use an extra IOBuffer to collect all the data and then
108
- # output it once to avoid output interrupt during task context
109
- # switching
110
- buf = IOBuffer ()
111
- print (buf, length (rtape), " -element RawTape" )
112
- isempty (rtape) || println (buf, " :" )
113
- i = 1
114
- for instr in rtape
115
- print (buf, " \t " , i, " => " )
116
- show (buf, instr)
117
- i += 1
118
- end
119
- print (io, String (take! (buf)))
120
- end
121
-
122
108
# # methods for Instruction
123
109
Base. show (io:: IO , instr:: AbstractInstruction ) = println (io, " A " , typeof (instr))
124
110
@@ -191,7 +177,8 @@ function bind_var!(var, bindings::Dict{Symbol, Any}) # for literal constants
191
177
bindings[id] = var
192
178
return id
193
179
end
194
- bind_var! (var:: Core.SSAValue , bindings:: Dict{Symbol, Any} ) = bind_var! (Symbol (var. id), bindings)
180
+ bind_var! (var:: Core.SSAValue , bindings:: Dict{Symbol, Any} ) =
181
+ bind_var! (Symbol (var. id), bindings)
195
182
bind_var! (var:: Core.TypedSlot , bindings:: Dict{Symbol, Any} ) =
196
183
bind_var! (Symbol (:_ , var. id), bindings)
197
184
bind_var! (var:: Core.SlotNumber , bindings:: Dict{Symbol, Any} ) =
@@ -201,13 +188,13 @@ function bind_var!(var::Symbol, bindings::Dict{Symbol, Any})
201
188
return var
202
189
end
203
190
204
- function translate! (tape:: RawTape , ir:: Core.CodeInfo )
191
+ function translate! (tape:: Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}} , ir:: Core.CodeInfo )
205
192
bindings = Dict {Symbol, Any} ()
206
193
207
194
for (idx, line) in enumerate (ir. code)
208
195
isa (line, Core. Const) && (line = line. val) # unbox Core.Const
209
196
ins = translate!! (Core. SSAValue (idx), line, bindings, ir)
210
- push! (tape, ins)
197
+ push! (tape, FunctionWrapper {Nothing, Tuple{TapedFunction}} ( ins) )
211
198
end
212
199
return bindings
213
200
end
0 commit comments