gives function the list of blocks it needs for branching and documents also the blocks better

This commit is contained in:
Torsten Ruger 2014-05-22 14:18:22 +03:00
parent cf87c5323d
commit ccf88319e0
4 changed files with 85 additions and 33 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,27 @@
require_relative 'helper'
class TestWhile < MiniTest::Test
include Fragments
def test_while
@string_input = <<HERE
def fibonaccit(n) # n == r0
a = 0 # a == r1
b = 1 # b = r2
while( n > 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