2014-05-03 14:13:44 +02:00
|
|
|
require_relative "block"
|
|
|
|
|
|
|
|
module Vm
|
|
|
|
|
|
|
|
# Functions are similar to Blocks. Where Blocks can be jumped to, Functions can be called.
|
|
|
|
|
2014-05-19 10:28:13 +02:00
|
|
|
# Functions also have arguments and a return. These are Value subclass instances, ie specify
|
|
|
|
# type (by class type) and register by instance
|
|
|
|
|
2014-05-22 12:59:47 +02:00
|
|
|
# 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.
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-22 12:59:47 +02:00
|
|
|
# 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.
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
class Function < Code
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-19 10:28:13 +02:00
|
|
|
def initialize(name , args = [] , return_type = nil)
|
2014-05-06 20:36:28 +02:00
|
|
|
super()
|
|
|
|
@name = name
|
2014-05-19 11:18:01 +02:00
|
|
|
@args = Array.new(args.length)
|
|
|
|
args.each_with_index do |arg , i|
|
|
|
|
if arg.is_a?(Value)
|
|
|
|
@args[i] = arg
|
2014-05-25 09:57:56 +02:00
|
|
|
raise "arg in non std register #{arg.inspect}" unless (i+1) == arg.register
|
2014-05-19 11:18:01 +02:00
|
|
|
else
|
2014-05-25 09:57:56 +02:00
|
|
|
@args[i] = arg.new(i+1)
|
2014-05-19 11:18:01 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-25 07:43:07 +02:00
|
|
|
set_return return_type
|
|
|
|
@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 = []
|
|
|
|
@blocks = []
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-22 12:59:47 +02:00
|
|
|
|
2014-05-25 07:43:07 +02:00
|
|
|
attr_reader :args , :entry , :exit , :body , :name , :return_type
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-25 07:43:07 +02:00
|
|
|
def set_return type_or_value
|
|
|
|
@return_type = type_or_value || Vm::Integer
|
|
|
|
if @return_type.is_a?(Value)
|
2014-05-25 09:57:56 +02:00
|
|
|
raise "return in non std register #{@return_type.inspect}" unless 7 == @return_type.register
|
2014-05-25 07:43:07 +02:00
|
|
|
else
|
2014-05-25 09:57:56 +02:00
|
|
|
@return_type = @return_type.new(7)
|
2014-05-25 07:43:07 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
def arity
|
|
|
|
@args.length
|
|
|
|
end
|
|
|
|
|
2014-05-21 15:42:36 +02:00
|
|
|
def new_local type = Vm::Integer
|
|
|
|
register = args.length + @locals.length
|
2014-05-25 09:57:56 +02:00
|
|
|
l = type.new(register + 1) # one for the type register 0, TODO add type as arg0 implicitly
|
|
|
|
raise "the red flag #{inspect}" if l.register > 6
|
2014-05-21 15:42:36 +02:00
|
|
|
@locals << l
|
|
|
|
l
|
2014-05-13 17:21:24 +02:00
|
|
|
end
|
|
|
|
|
2014-05-22 20:55:17 +02:00
|
|
|
def save_locals context , into
|
|
|
|
save = args.collect{|a| a.register } + @locals.collect{|l| l.register}
|
2014-05-25 09:57:56 +02:00
|
|
|
into.push(save) unless save.empty?
|
2014-05-22 20:55:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore_locals context , into
|
2014-05-25 07:43:07 +02:00
|
|
|
#TODO assumes allocation in order, as the pop must be get regs in ascending order (also push)
|
2014-05-22 20:55:17 +02:00
|
|
|
restore = args.collect{|a| a.register } + @locals.collect{|l| l.register}
|
2014-05-25 09:57:56 +02:00
|
|
|
into.pop(restore) unless restore.empty?
|
2014-05-22 20:55:17 +02:00
|
|
|
end
|
|
|
|
|
2014-05-22 13:18:22 +02:00
|
|
|
def new_block name
|
|
|
|
block = Block.new(name , self)
|
|
|
|
@blocks << block
|
|
|
|
block
|
|
|
|
end
|
|
|
|
|
2014-05-22 13:56:31 +02:00
|
|
|
# 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
|
2014-05-22 13:18:22 +02:00
|
|
|
# following id the Code interface
|
|
|
|
|
|
|
|
# to link we link the entry and then any blocks. The entry links the straight line
|
2014-05-05 23:12:04 +02:00
|
|
|
def link_at address , context
|
2014-05-06 20:36:28 +02:00
|
|
|
super #just sets the position
|
2014-05-05 23:12:04 +02:00
|
|
|
@entry.link_at address , context
|
|
|
|
address += @entry.length
|
2014-05-22 13:18:22 +02:00
|
|
|
@blocks.each do |block|
|
|
|
|
block.link_at(pos , context)
|
|
|
|
pos += block.length
|
|
|
|
end
|
2014-05-05 23:12:04 +02:00
|
|
|
end
|
2014-05-22 13:18:22 +02:00
|
|
|
|
|
|
|
# position of the function is the position of the entry block
|
2014-05-16 18:56:13 +02:00
|
|
|
def position
|
|
|
|
@entry.position
|
|
|
|
end
|
2014-05-22 13:18:22 +02:00
|
|
|
|
|
|
|
# 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
|
2014-05-05 08:35:40 +02:00
|
|
|
def length
|
2014-05-22 13:18:22 +02:00
|
|
|
@blocks.inject(@entry.length) {| sum , item | sum + item.length}
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
|
|
|
|
2014-05-22 13:18:22 +02:00
|
|
|
# assembling assembles the entry (straight line/ no branch line) + any additional branches
|
2014-05-05 23:12:04 +02:00
|
|
|
def assemble io
|
2014-05-06 20:36:28 +02:00
|
|
|
@entry.assemble(io)
|
2014-05-22 13:18:22 +02:00
|
|
|
@blocks.each do |block|
|
|
|
|
block.assemble io
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|