while coded

This commit is contained in:
Torsten Ruger 2014-05-22 14:56:31 +03:00
parent ccf88319e0
commit 8596fb312d
3 changed files with 19 additions and 7 deletions

View File

@ -14,15 +14,16 @@ module Ast
[:condition, :body] [:condition, :body]
end end
def compile context , into def compile context , into
cond_val = condition.compile(context , into) ret = into.new_block "#{into.name}_return_#{hash}"
#set up branches for bodies while_block = into.new_block "#{into.name}_while_#{hash}"
# jump to end if done cond_val = condition.compile(context , while_block)
while_block.beq ret
last = nil last = nil
body.each do |part| body.each do |part|
last = part.compile(context , into ) last = part.compile(context , while_block )
puts "compiled in while #{last.inspect}" puts "compiled in while #{last.inspect}"
end end
#jump back to test while_block.b while_block
return last return last
end end
end end

View File

@ -21,8 +21,8 @@ module Elf
binary = program.assemble(StringIO.new ) binary = program.assemble(StringIO.new )
# blocks = program.functions.collect{ |f| [f.entry , f.exit , f.body] } blocks = []
blocks = program.functions.collect{ |f| [f.entry] } program.functions.each {|f| blocks += f.blocks }
blocks += [program.entry , program.exit , program.main] blocks += [program.entry , program.exit , program.main]
blocks.flatten.each do |b| blocks.flatten.each do |b|
add_symbol b.name.to_s , b.position add_symbol b.name.to_s , b.position

View File

@ -73,6 +73,17 @@ module Vm
block block
end 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 # following id the Code interface
# to link we link the entry and then any blocks. The entry links the straight line # to link we link the entry and then any blocks. The entry links the straight line