diff --git a/lib/vm/block.rb b/lib/vm/block.rb index 2b0003a0..7fd93eae 100644 --- a/lib/vm/block.rb +++ b/lib/vm/block.rb @@ -30,12 +30,6 @@ module Vm attr_reader :name , :next , :codes , :function - def length - cods = @codes.inject(0) {| sum , item | sum + item.length} - cods += @next.length if @next - cods - end - def add_code(kode) if kode.is_a? Hash raise "Hack only for 1 element #{inspect} #{kode.inspect}" unless kode.length == 1 @@ -51,26 +45,6 @@ module Vm alias :<< :add_code alias :a :add_code - def link_at pos , context - @position = pos - @codes.each do |code| - code.link_at(pos , context) - pos += code.length - end - if @next - @next.link_at pos , context - pos += @next.length - end - pos - end - - def assemble(io) - @codes.each do |obj| - obj.assemble io - end - @next.assemble(io) if @next - end - # create a new linear block after this block. Linear means there is no brach needed from this one # to the new one. Usually the new one just serves as jump address for a control statement # In code generation (assembly) , new new_block is written after this one, ie zero runtime cost @@ -121,6 +95,36 @@ module Vm add_code RegisterMachine.instance.send(meth , *args) end - end + # Code interface follows. Note position is inheitted as is from Code + # length of the block is the length of it's codes, plus any next block (ie no branch follower) + # Note, the next is in effect a linked list and as such may have many blocks behind it. + def length + cods = @codes.inject(0) {| sum , item | sum + item.length} + cods += @next.length if @next + cods + end + + # to link we link the codes (instructions), plus any next in line block (non- branched) + def link_at pos , context + super(pos , context) + @codes.each do |code| + code.link_at(pos , context) + pos += code.length + end + if @next + @next.link_at pos , context + pos += @next.length + end + pos + end + + # assemble the codes (instructions) and any next in line block + def assemble(io) + @codes.each do |obj| + obj.assemble io + end + @next.assemble(io) if @next + end + end end \ No newline at end of file diff --git a/lib/vm/code.rb b/lib/vm/code.rb index dfc146d5..869756ff 100644 --- a/lib/vm/code.rb +++ b/lib/vm/code.rb @@ -1,10 +1,10 @@ module Vm # Base class for anything that we can assemble - # Derived classes include instructions and data(values) + # Derived classes include instructions and constants(data) # The commonality abstracted here is the length and position - # and the ability to assemble itself into the stream + # and the ability to assemble itself into the stream(io) # All code is position independant once assembled. # But for jumps and calls two passes are neccessary. @@ -39,8 +39,7 @@ module Vm raise "Not implemented #{inspect}" end - # so currently the interface passes the io (usually string_io) in for the code to assemble itself. - # this may change as the writing is still done externally (or that will change) + # we pass the io (usually string_io) in for the code to assemble itself. def assemble(io) raise "Not implemented #{self.inspect}" end diff --git a/lib/vm/function.rb b/lib/vm/function.rb index 84a2aee6..b257fb6c 100644 --- a/lib/vm/function.rb +++ b/lib/vm/function.rb @@ -50,6 +50,7 @@ module Vm @body = Block.new("#{name}_body", self , @return) @entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry" , self , @body) ,name ) @locals = [] + @blocks = [] end attr_reader :args , :entry , :exit , :body , :name @@ -66,21 +67,42 @@ module Vm l end + def new_block name + block = Block.new(name , self) + @blocks << block + block + end + + # following id the Code interface + + # to link we link the entry and then any blocks. The entry links the straight line def link_at address , context - raise "undefined code #{inspect}" if @body.nil? super #just sets the position @entry.link_at address , context address += @entry.length + @blocks.each do |block| + block.link_at(pos , context) + pos += block.length + end end + + # position of the function is the position of the entry block def position @entry.position end + + # length of a function is the entry block length (includes the straight line behind it) + # plus any out of line blocks that have been added def length - @entry.length + @blocks.inject(@entry.length) {| sum , item | sum + item.length} end + # assembling assembles the entry (straight line/ no branch line) + any additional branches def assemble io @entry.assemble(io) + @blocks.each do |block| + block.assemble io + end end end diff --git a/test/fragments/test_while.rb b/test/fragments/test_while.rb new file mode 100644 index 00000000..cf87b98a --- /dev/null +++ b/test/fragments/test_while.rb @@ -0,0 +1,27 @@ +require_relative 'helper' + +class TestWhile < MiniTest::Test + include Fragments + + def test_while + @string_input = < 1 ) do #BUG comment lines + comments behind function calls + tmp = a # r3 <- r1 + a = b # r1 <- r2 + b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4 + n = n - 1 # r0 <- r2 for call, #call ok + end #r5 <- r0 - 1 n=n-1 through r5 tmp + putint(b) + end # r0 <- r5 + + fibonaccit( 10 ) +HERE + @should = [0x0,0xb0,0xa0,0xe3,0xe,0x0,0x2d,0xe9,0xa,0x0,0xa0,0xe3,0x2,0x0,0x0,0xeb,0xe,0x0,0xbd,0xe8,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe3,0x1,0x0,0x50,0xe3,0x1,0x30,0xa0,0xe1,0x2,0x10,0xa0,0xe1,0x2,0x40,0x83,0xe0,0x4,0x20,0xa0,0xe1,0x1,0x50,0x40,0xe2,0x5,0x0,0xa0,0xe1,0xe,0x0,0x2d,0xe9,0x2,0x0,0xa0,0xe1,0x12,0x0,0x0,0xeb,0xe,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x20,0x41,0xe2,0x21,0x11,0x41,0xe0,0x21,0x12,0x81,0xe0,0x21,0x14,0x81,0xe0,0x21,0x18,0x81,0xe0,0xa1,0x11,0xa0,0xe1,0x1,0x31,0x81,0xe0,0x83,0x20,0x52,0xe0,0x1,0x10,0x81,0x52,0xa,0x20,0x82,0x42,0x30,0x20,0x82,0xe2,0x0,0x20,0xc0,0xe5,0x1,0x0,0x40,0xe2,0x0,0x0,0x51,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe1,0x24,0x0,0x8f,0xe2,0x9,0x0,0x80,0xe2,0xe9,0xff,0xff,0xeb,0x18,0x0,0x8f,0xe2,0xc,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe1,0x0,0x10,0xa0,0xe1,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0] + parse + write "while" + end +end +