work on branches

the concept is different in bosl, move appropriate to virtual
This commit is contained in:
Torsten Ruger 2015-10-07 10:02:51 +03:00
parent 4e26166dff
commit f0611e52db
7 changed files with 17 additions and 35 deletions

View File

@ -12,11 +12,10 @@ module Bosl
true_block = @method.source.new_block "if_true" # second, linked in after current, before merge true_block = @method.source.new_block "if_true" # second, linked in after current, before merge
false_block = @method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here false_block = @method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here
is = process(condition)
is = process(condition )
# TODO should/will use different branches for different conditions. # TODO should/will use different branches for different conditions.
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition
@method.source.add_code Virtual::IsTrueBranch.new( true_block ) @method.source.add_code Register::IsZeroBranch.new( condition , true_block )
# compile the true block (as we think of it first, even it is second in sequential order) # compile the true block (as we think of it first, even it is second in sequential order)
@method.source.current true_block @method.source.current true_block
@ -26,7 +25,7 @@ module Bosl
# compile the false block # compile the false block
@method.source.current false_block @method.source.current false_block
last = process_all(if_false).last if if_false last = process_all(if_false).last if if_false
@method.source.add_code Virtual::UnconditionalBranch.new( merge_block ) @method.source.add_code Register::AlwaysBranch.new(expression, merge_block )
#puts "compiled if: end" #puts "compiled if: end"
@method.source.current merge_block @method.source.current merge_block

View File

@ -14,12 +14,12 @@ module Bosl
cond = process(condition) cond = process(condition)
@method.source.add_code Virtual::IsTrueBranch.new(merge) @method.source.add_code Register::IsZeroBranch.new(condition,merge)
last = process_all(expressions).last last = process_all(expressions).last
# unconditionally branch to the start # unconditionally branch to the start
@method.source.add_code Virtual::UnconditionalBranch.new(start) @method.source.add_code Register::AlwaysBranch.new(expression,start)
# continue execution / compiling at the merge block # continue execution / compiling at the merge block
@method.source.current merge @method.source.current merge

View File

@ -13,7 +13,19 @@ module Register
def to_s def to_s
"Branch: #{block.name}" "Branch: #{block.name}"
end end
alias :inspect :to_s
end
class IsZeroBranch < Branch
end
class IsNegativeBranch < Branch
end
class IsPositiveBranch < Branch
end
class AlwaysBranch < Branch
end end
end end

View File

@ -13,7 +13,6 @@ module Virtual
end end
require_relative "instructions/branch"
require_relative "instructions/halt" require_relative "instructions/halt"
require_relative "instructions/instance_get" require_relative "instructions/instance_get"
require_relative "instructions/method_call" require_relative "instructions/method_call"

View File

@ -1,14 +0,0 @@
module Virtual
# a branch must branch to a block. This is an abstract class, names indicate the actual test
class Branch < Instruction
def initialize to
@to = to
end
attr_reader :to
end
end
require_relative "is_true_branch"
require_relative "unconditional_branch"

View File

@ -1,8 +0,0 @@
module Virtual
# implicit means there is no explcit test involved.
# normal ruby rules are false and nil are false, EVERYTHING else is true (and that includes 0)
class IsTrueBranch < Branch
end
end

View File

@ -1,6 +0,0 @@
module Virtual
class UnconditionalBranch < Branch
end
end