diff --git a/lib/ast/while_expression.rb b/lib/ast/while_expression.rb index 99e2eebe..65071d52 100644 --- a/lib/ast/while_expression.rb +++ b/lib/ast/while_expression.rb @@ -14,15 +14,16 @@ module Ast [:condition, :body] end def compile context , into - cond_val = condition.compile(context , into) - #set up branches for bodies - # jump to end if done + ret = into.new_block "#{into.name}_return_#{hash}" + while_block = into.new_block "#{into.name}_while_#{hash}" + cond_val = condition.compile(context , while_block) + while_block.beq ret last = nil body.each do |part| - last = part.compile(context , into ) + last = part.compile(context , while_block ) puts "compiled in while #{last.inspect}" end - #jump back to test + while_block.b while_block return last end end diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index 2db73c2f..a243aa3e 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -21,8 +21,8 @@ module Elf binary = program.assemble(StringIO.new ) -# blocks = program.functions.collect{ |f| [f.entry , f.exit , f.body] } - blocks = program.functions.collect{ |f| [f.entry] } + blocks = [] + program.functions.each {|f| blocks += f.blocks } blocks += [program.entry , program.exit , program.main] blocks.flatten.each do |b| add_symbol b.name.to_s , b.position diff --git a/lib/vm/function.rb b/lib/vm/function.rb index b257fb6c..3639c019 100644 --- a/lib/vm/function.rb +++ b/lib/vm/function.rb @@ -73,6 +73,17 @@ module Vm block end + # return a list of the blocks that are addressable, ie entry and @blocks and all next + def blocks + ret = [] + (@blocks << @entry).each do |b| + while b + ret << b + b = b.next + end + end + ret + end # following id the Code interface # to link we link the entry and then any blocks. The entry links the straight line