using the next field of blocks for a consecutive code line
This commit is contained in:
parent
a76ba577f2
commit
cf87c5323d
@ -25,7 +25,7 @@ module Ast
|
||||
if( l_val ) #variable existed, move data there
|
||||
l_val = l_val.move( into , r_val)
|
||||
else
|
||||
l_val = context.function.new_local.load( into , r_val )
|
||||
l_val = context.function.new_local.move( into , r_val )
|
||||
end
|
||||
context.locals[left.name] = l_val
|
||||
return l_val
|
||||
|
@ -20,18 +20,20 @@ module Vm
|
||||
|
||||
class Block < Code
|
||||
|
||||
def initialize(name , function)
|
||||
def initialize(name , function , next_block = nil)
|
||||
super()
|
||||
@function = function
|
||||
@name = name.to_sym
|
||||
@next = nil
|
||||
@next = next_block
|
||||
@codes = []
|
||||
end
|
||||
|
||||
attr_reader :name , :next , :codes , :function
|
||||
|
||||
def length
|
||||
@codes.inject(0) {| sum , item | sum + item.length}
|
||||
cods = @codes.inject(0) {| sum , item | sum + item.length}
|
||||
cods += @next.length if @next
|
||||
cods
|
||||
end
|
||||
|
||||
def add_code(kode)
|
||||
@ -55,6 +57,10 @@ module Vm
|
||||
code.link_at(pos , context)
|
||||
pos += code.length
|
||||
end
|
||||
if @next
|
||||
@next.link_at pos , context
|
||||
pos += @next.length
|
||||
end
|
||||
pos
|
||||
end
|
||||
|
||||
@ -62,8 +68,18 @@ module Vm
|
||||
@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
|
||||
def new_block name
|
||||
new_b = Block.new( name , @function , @next )
|
||||
@next = new_b
|
||||
return new_b
|
||||
end
|
||||
|
||||
# to use the assignment syntax (see method_missing) the scope must be set, so variables can be resolved
|
||||
# The scope you set should be a binding (literally, the kernel.binding)
|
||||
# The function return the block, so it can be chained into an assignment
|
||||
@ -74,13 +90,6 @@ module Vm
|
||||
@scope = where
|
||||
self
|
||||
end
|
||||
# set the next executed block after self.
|
||||
# why is this useful? if it's unconditional, why not merge them:
|
||||
# So the second block can be used as a jump target. You standard loop needs a block to setup
|
||||
# and at least one to do the calculation
|
||||
def set_next block
|
||||
@next = block
|
||||
end
|
||||
|
||||
# sugar to create instructions easily. Actually just got double sweet with two versions:
|
||||
# 1 for any method that ends in = we evaluate the method name in the current scope (see scope())
|
||||
|
@ -7,16 +7,24 @@ module Vm
|
||||
# Functions also have arguments and a return. These are Value subclass instances, ie specify
|
||||
# type (by class type) and register by instance
|
||||
|
||||
# Functions have a exactly three blocks, entry, exit and body, which are created for you
|
||||
# with straight branches between them.
|
||||
# They also have local variables. Args take up the first n regs, then locals the rest. No
|
||||
# direct manipulating of registers (ie specifying the number) should be done.
|
||||
|
||||
# Also remember that if your body exists of several blocks, they must be wrapped in a
|
||||
# block as the function really only has the one, and blocks only assemble their codes,
|
||||
# not their next links
|
||||
# This comes at zero runtime cost though, as the wrapper is just the sum of it's codes
|
||||
# Code-wise Functions are made up from a list of Blocks, in a similar way blocks are made up of codes
|
||||
# Four of the block have a special role:
|
||||
# - entry/exit: are usually system specific
|
||||
# - body: the logical start of the function
|
||||
# - return: the logical end, where ALL blocks must end
|
||||
|
||||
# Blocks can be linked in two ways:
|
||||
# -linear: flow continues from one to the next as they are sequential both logically and "physically"
|
||||
# use the block set_next for this.
|
||||
# This "the straight line", there must be a continuous sequence from body to return
|
||||
# Linear blocks may be created from an existing block with new_block
|
||||
# - branched: You create new blocks using function.new_block which gets added "after" return
|
||||
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
|
||||
# MUST have an uncoditional branch. And remember, all roads lead to return.
|
||||
|
||||
# If you change the body block to point elsewhere, remember to end up at exit
|
||||
|
||||
class Function < Code
|
||||
|
||||
def initialize(name , args = [] , return_type = nil)
|
||||
@ -37,12 +45,13 @@ module Vm
|
||||
else
|
||||
@return_type = @return_type.new(0)
|
||||
end
|
||||
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry" , self) ,name )
|
||||
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit", self) , name )
|
||||
@body = Block.new("#{name}_body", self)
|
||||
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit" , self) , name )
|
||||
@return = Block.new("#{name}_return", self , @exit)
|
||||
@body = Block.new("#{name}_body", self , @return)
|
||||
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry" , self , @body) ,name )
|
||||
@locals = []
|
||||
branch_body
|
||||
end
|
||||
|
||||
attr_reader :args , :entry , :exit , :body , :name
|
||||
attr_accessor :return_type
|
||||
|
||||
@ -62,28 +71,17 @@ module Vm
|
||||
super #just sets the position
|
||||
@entry.link_at address , context
|
||||
address += @entry.length
|
||||
@body.link_at(address , context)
|
||||
address += @body.length
|
||||
@exit.link_at(address,context)
|
||||
end
|
||||
def position
|
||||
@entry.position
|
||||
end
|
||||
def length
|
||||
@entry.length + @exit.length + @body.length
|
||||
@entry.length
|
||||
end
|
||||
|
||||
def assemble io
|
||||
@entry.assemble(io)
|
||||
@body.assemble(io)
|
||||
@exit.assemble(io)
|
||||
end
|
||||
|
||||
private
|
||||
# set up the braches from entry to body and body to exit (unless that exists, see set_body)
|
||||
def branch_body
|
||||
@entry.set_next(@body)
|
||||
@body.set_next(@exit) if @body and !@body.next
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user