11# # Instruction and TapedFunction
22
33abstract type AbstractInstruction end
4- const RawTape = Vector{AbstractInstruction}
54
65"""
76 Instruction
@@ -29,7 +28,7 @@ mutable struct TapedFunction{F}
2928 func:: F # maybe a function, a constructor, or a callable object
3029 arity:: Int
3130 ir:: Core.CodeInfo
32- tape:: RawTape
31+ tape:: Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}
3332 counter:: Int
3433 bindings:: Dict{Symbol, Any}
3534 retval:: Symbol
@@ -46,7 +45,7 @@ mutable struct TapedFunction{F}
4645 end
4746
4847 ir = CodeInfoTools. code_inferred (f, args_type... )
49- tape = RawTape ()
48+ tape = Vector {FunctionWrapper{Nothing, Tuple{TapedFunction}}} ()
5049 bindings = translate! (tape, ir)
5150
5251 tf = new {F} (f, length (args), ir, tape, 1 , bindings, :none )
@@ -83,12 +82,15 @@ function (tf::TapedFunction)(args...; callback=nothing)
8382 ins = tf. tape[tf. counter]
8483 ins (tf)
8584 callback != = nothing && callback ()
86- isa (ins, ReturnInstruction) && break
85+ tf . retval != = :none && break
8786 end
8887 return result (tf)
8988end
9089
9190function 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
9294 buf = IOBuffer ()
9395 println (buf, " TapedFunction:" )
9496 println (buf, " * .func => $(tf. func) " )
@@ -103,22 +105,6 @@ function Base.show(io::IO, tf::TapedFunction)
103105 print (io, String (take! (buf)))
104106end
105107
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-
122108# # methods for Instruction
123109Base. show (io:: IO , instr:: AbstractInstruction ) = println (io, " A " , typeof (instr))
124110
@@ -191,7 +177,8 @@ function bind_var!(var, bindings::Dict{Symbol, Any}) # for literal constants
191177 bindings[id] = var
192178 return id
193179end
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)
195182bind_var! (var:: Core.TypedSlot , bindings:: Dict{Symbol, Any} ) =
196183 bind_var! (Symbol (:_ , var. id), bindings)
197184bind_var! (var:: Core.SlotNumber , bindings:: Dict{Symbol, Any} ) =
@@ -201,13 +188,13 @@ function bind_var!(var::Symbol, bindings::Dict{Symbol, Any})
201188 return var
202189end
203190
204- function translate! (tape:: RawTape , ir:: Core.CodeInfo )
191+ function translate! (tape:: Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}} , ir:: Core.CodeInfo )
205192 bindings = Dict {Symbol, Any} ()
206193
207194 for (idx, line) in enumerate (ir. code)
208195 isa (line, Core. Const) && (line = line. val) # unbox Core.Const
209196 ins = translate!! (Core. SSAValue (idx), line, bindings, ir)
210- push! (tape, ins)
197+ push! (tape, FunctionWrapper {Nothing, Tuple{TapedFunction}} ( ins) )
211198 end
212199 return bindings
213200end
0 commit comments