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.
|
|
|
|
|
|
|
|
# Functions also have arguments, though they are handled differently (in register allocation)
|
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
# Functions have a exactly three blocks, entry, exit and body, which are created for you
|
|
|
|
# with straight branches between them.
|
|
|
|
|
2014-05-15 15:54:23 +02:00
|
|
|
# Also remember that if your den body exists of several blocks, they must be wrapped in a
|
2014-05-06 20:36:28 +02:00
|
|
|
# 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
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
# If you change the body block to point elsewhere, remember to end up at exit
|
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
|
|
|
|
|
|
|
def initialize(name , args = [])
|
2014-05-06 20:36:28 +02:00
|
|
|
super()
|
|
|
|
@name = name
|
2014-05-03 14:13:44 +02:00
|
|
|
@args = args
|
2014-05-14 21:04:03 +02:00
|
|
|
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry") ,name )
|
|
|
|
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit") , name )
|
2014-05-13 17:21:24 +02:00
|
|
|
@body = Block.new("#{name}_body")
|
|
|
|
@reg_count = 0
|
2014-05-06 20:36:28 +02:00
|
|
|
branch_body
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-06 20:36:28 +02:00
|
|
|
attr_reader :args , :entry , :exit , :body , :name
|
2014-05-13 20:06:12 +02:00
|
|
|
attr_accessor :return_type
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
def arity
|
|
|
|
@args.length
|
|
|
|
end
|
|
|
|
|
2014-05-13 17:21:24 +02:00
|
|
|
def next_register
|
|
|
|
next_free = @reg_count
|
|
|
|
@reg_count += 1
|
|
|
|
next_free + args.length
|
|
|
|
end
|
|
|
|
|
2014-05-05 23:12:04 +02:00
|
|
|
def link_at address , context
|
2014-05-06 20:36:28 +02:00
|
|
|
raise "undefined code #{inspect}" if @body.nil?
|
|
|
|
super #just sets the position
|
2014-05-05 23:12:04 +02:00
|
|
|
@entry.link_at address , context
|
|
|
|
address += @entry.length
|
2014-05-06 20:36:28 +02:00
|
|
|
@body.link_at(address , context)
|
2014-05-14 21:04:03 +02:00
|
|
|
address += @body.length
|
2014-05-05 23:12:04 +02:00
|
|
|
@exit.link_at(address,context)
|
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def length
|
2014-05-06 20:36:28 +02:00
|
|
|
@entry.length + @exit.length + @body.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 23:12:04 +02:00
|
|
|
def assemble io
|
2014-05-06 20:36:28 +02:00
|
|
|
@entry.assemble(io)
|
|
|
|
@body.assemble(io)
|
2014-05-05 23:12:04 +02:00
|
|
|
@exit.assemble(io)
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
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
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|