2014-05-03 14:13:44 +02:00
|
|
|
require_relative "block"
|
2014-06-11 10:41:50 +02:00
|
|
|
require_relative "passes"
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
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-06-06 20:49:03 +02:00
|
|
|
def initialize(name , receiver = Vm::Integer , args = [] , return_type = Vm::Integer)
|
2014-05-06 20:36:28 +02:00
|
|
|
super()
|
2014-05-31 15:43:03 +02:00
|
|
|
@name = name.to_sym
|
2014-06-06 20:49:03 +02:00
|
|
|
if receiver.is_a?(Value)
|
|
|
|
@receiver = receiver
|
2014-06-10 12:29:01 +02:00
|
|
|
raise "arg in non std register #{receiver.inspect}" unless RegisterMachine.instance.receiver_register == receiver.register_symbol
|
2014-06-06 20:49:03 +02:00
|
|
|
else
|
2014-06-10 12:29:01 +02:00
|
|
|
@receiver = receiver.new(RegisterMachine.instance.receiver_register)
|
2014-06-06 20:49:03 +02:00
|
|
|
end
|
|
|
|
|
2014-05-19 11:18:01 +02:00
|
|
|
@args = Array.new(args.length)
|
|
|
|
args.each_with_index do |arg , i|
|
2014-06-10 12:29:01 +02:00
|
|
|
shouldda = RegisterUse.new(RegisterMachine.instance.receiver_register).next_reg_use(i + 1)
|
2014-05-19 11:18:01 +02:00
|
|
|
if arg.is_a?(Value)
|
|
|
|
@args[i] = arg
|
2014-06-10 12:29:01 +02:00
|
|
|
raise "arg #{i} in non std register #{arg.used_register}, expecting #{shouldda}" unless shouldda == arg.used_register
|
2014-05-19 11:18:01 +02:00
|
|
|
else
|
2014-06-10 12:29:01 +02:00
|
|
|
@args[i] = arg.new(shouldda)
|
2014-05-19 11:18:01 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-25 07:43:07 +02:00
|
|
|
set_return return_type
|
2014-06-10 23:38:46 +02:00
|
|
|
@exit = RegisterMachine.instance.function_exit( Vm::Block.new("exit" , self , nil) , name )
|
2014-05-31 16:02:55 +02:00
|
|
|
@return = Block.new("return", self , @exit)
|
|
|
|
@body = Block.new("body", self , @return)
|
2014-06-10 22:57:56 +02:00
|
|
|
@insert_at = @body
|
2014-06-10 23:38:46 +02:00
|
|
|
@entry = RegisterMachine.instance.function_entry( Vm::Block.new("entry" , self , @body) ,name )
|
2014-05-25 07:43:07 +02:00
|
|
|
@locals = []
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-22 12:59:47 +02:00
|
|
|
|
2014-06-10 22:57:56 +02:00
|
|
|
attr_reader :args , :entry , :exit , :body , :name , :return_type , :receiver
|
|
|
|
|
|
|
|
def insertion_point
|
|
|
|
@insert_at
|
|
|
|
end
|
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-06-10 12:29:01 +02:00
|
|
|
raise "return in non std register #{@return_type.inspect}" unless RegisterMachine.instance.return_register == @return_type.register_symbol
|
2014-05-25 07:43:07 +02:00
|
|
|
else
|
2014-06-10 12:29:01 +02:00
|
|
|
@return_type = @return_type.new(RegisterMachine.instance.return_register)
|
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
|
2014-06-12 08:07:03 +02:00
|
|
|
register = args.length + 3 + @locals.length # three for the receiver, return and type regs
|
|
|
|
l = type.new(register) #so start at r3
|
2014-06-07 16:59:44 +02:00
|
|
|
puts "new local #{l.register_symbol}"
|
2014-06-12 08:07:03 +02:00
|
|
|
raise "Register overflow in function #{name}" if register >= 13 # yep, 13 is bad luck
|
2014-05-21 15:42:36 +02:00
|
|
|
@locals << l
|
|
|
|
l
|
2014-05-13 17:21:24 +02:00
|
|
|
end
|
2014-06-06 20:49:03 +02:00
|
|
|
|
2014-06-09 18:24:09 +02:00
|
|
|
# return a list of registers that are still in use after the given block
|
|
|
|
# a call_site uses pushes and pops these to make them available for code after a call
|
|
|
|
def locals_at l_block
|
|
|
|
used =[]
|
|
|
|
assigned = []
|
|
|
|
l_block.reachable.each do |b|
|
|
|
|
b.uses.each {|u|
|
|
|
|
(used << u) unless assigned.include?(u)
|
|
|
|
}
|
|
|
|
assigned += b.assigns
|
|
|
|
end
|
|
|
|
used.uniq
|
2014-05-22 13:18:22 +02:00
|
|
|
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 = []
|
2014-06-09 18:24:09 +02:00
|
|
|
b = @entry
|
|
|
|
while b
|
|
|
|
ret << b
|
|
|
|
b = b.next
|
|
|
|
end
|
2014-05-22 13:56:31 +02:00
|
|
|
ret
|
|
|
|
end
|
2014-06-09 18:24:09 +02:00
|
|
|
|
2014-06-10 22:57:56 +02:00
|
|
|
# when control structures create new blocks (with new_block) control continues at some new block the
|
|
|
|
# the control structure creates.
|
|
|
|
# Example: while, needs 2 extra blocks
|
|
|
|
# 1 condition code, must be its own blockas we jump back to it
|
|
|
|
# - the body, can actually be after the condition as we don't need to jump there
|
|
|
|
# 2 after while block. Condition jumps here
|
|
|
|
# After block 2, the function is linear again and the calling code does not need to know what happened
|
|
|
|
|
|
|
|
# But subsequent statements are still using the original block (self) to add code to
|
|
|
|
# So the while expression creates the extra blocks, adds them and the code and then "moves" the insertion point along
|
|
|
|
def insert_at block
|
|
|
|
@insert_at = block
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# create a new linear block after the current insertion block.
|
|
|
|
# Linear means there is no brach needed from that 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
|
|
|
|
# This does _not_ change the insertion point, that has do be done with insert_at(block)
|
|
|
|
def new_block new_name
|
|
|
|
block_name = "#{@insert_at.name}_#{new_name}"
|
2014-06-11 20:36:22 +02:00
|
|
|
new_b = Block.new( block_name , self , @insert_at.next )
|
2014-06-10 22:57:56 +02:00
|
|
|
@insert_at.set_next new_b
|
|
|
|
return new_b
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_code(kode)
|
|
|
|
raise "alarm #{kode}" if kode.is_a? Word
|
|
|
|
raise "alarm #{kode.class} #{kode}" unless kode.is_a? Code
|
|
|
|
@insert_at.do_add kode
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# sugar to create instructions easily.
|
|
|
|
# any method will be passed on to the RegisterMachine and the result added to the insertion block
|
|
|
|
# With this trick we can write what looks like assembler,
|
|
|
|
# Example func.instance_eval
|
|
|
|
# mov( r1 , r2 )
|
|
|
|
# add( r1 , r2 , 4)
|
|
|
|
# end
|
|
|
|
# mov and add will be called on Machine and generate Inststuction that are then added
|
|
|
|
# to the current block
|
|
|
|
# also symbols are supported and wrapped as register usages (for bare metal programming)
|
|
|
|
def method_missing(meth, *args, &block)
|
|
|
|
add_code RegisterMachine.instance.send(meth , *args)
|
|
|
|
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
|
|
|
|
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-06-09 18:24:09 +02:00
|
|
|
@entry.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-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|