2014-08-13 10:59:51 +02:00
|
|
|
require_relative "block"
|
2014-07-14 13:06:09 +02:00
|
|
|
|
2014-07-10 16:14:38 +02:00
|
|
|
module Virtual
|
|
|
|
# static description of a method
|
|
|
|
# name
|
2015-03-28 19:20:01 +01:00
|
|
|
# arg_names (with defaults)
|
|
|
|
# receiver
|
2014-07-10 16:14:38 +02:00
|
|
|
# code
|
|
|
|
# return arg (usually mystery, but for coded ones can be more specific)
|
|
|
|
# known local variable names
|
|
|
|
# temp variables (numbered)
|
|
|
|
#
|
2015-03-28 19:20:01 +01:00
|
|
|
# Methods are one step up from to VM::Blocks. Where Blocks can be jumped to, Methods can be called.
|
2014-08-13 10:59:51 +02:00
|
|
|
|
2015-03-28 19:20:01 +01:00
|
|
|
# Methods also have arguments and a return. These are typed by subclass instances of Value
|
2014-08-13 10:59:51 +02:00
|
|
|
|
2015-03-28 19:20:01 +01:00
|
|
|
# They also have local variables.
|
2014-08-13 10:59:51 +02:00
|
|
|
|
2015-03-28 19:20:01 +01:00
|
|
|
# Code-wise Methods are made up from a list of Blocks, in a similar way blocks are made up of Instructions
|
2014-08-13 10:59:51 +02:00
|
|
|
# The function starts with one block, and that has a start and end (return)
|
|
|
|
|
|
|
|
# Blocks can be linked in two ways:
|
2015-03-28 19:20:01 +01:00
|
|
|
# -linear: flow continues from one to the next as they are sequential both logically and
|
|
|
|
# "physically" use the block set_next for this.
|
|
|
|
# This "straight line", there must be a continuous sequence from body to return
|
2014-08-13 10:59:51 +02:00
|
|
|
# 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-08-28 07:10:33 +02:00
|
|
|
class CompiledMethod < Virtual::Object
|
2014-07-10 16:14:38 +02:00
|
|
|
#return the main function (the top level) into which code is compiled
|
2014-08-28 07:10:33 +02:00
|
|
|
def CompiledMethod.main
|
|
|
|
CompiledMethod.new(:main , [] )
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2015-03-28 19:20:01 +01:00
|
|
|
def initialize name , arg_names , receiver = Virtual::Self.new , return_type = Virtual::Mystery
|
2014-07-14 10:29:38 +02:00
|
|
|
@name = name.to_sym
|
2014-08-22 08:21:59 +02:00
|
|
|
@class_name = :Object
|
2015-03-28 19:20:01 +01:00
|
|
|
@arg_names = arg_names
|
2014-07-10 16:14:38 +02:00
|
|
|
@locals = []
|
2014-07-14 23:00:00 +02:00
|
|
|
@tmps = []
|
2014-07-12 20:59:17 +02:00
|
|
|
@receiver = receiver
|
|
|
|
@return_type = return_type
|
2014-08-13 10:59:51 +02:00
|
|
|
# first block we have to create with .new , as new_block assumes a current
|
2014-08-30 19:55:22 +02:00
|
|
|
enter = Block.new( "enter" , self ).add_code(MethodEnter.new())
|
2014-08-30 15:57:56 +02:00
|
|
|
@blocks = [enter]
|
2014-08-13 10:59:51 +02:00
|
|
|
@current = enter
|
|
|
|
new_block("return").add_code(MethodReturn.new)
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2015-03-28 19:20:01 +01:00
|
|
|
attr_reader :name , :arg_names , :receiver , :blocks
|
2014-08-22 08:21:59 +02:00
|
|
|
attr_accessor :return_type , :current , :class_name
|
2014-07-10 16:14:38 +02:00
|
|
|
|
2014-07-18 10:56:46 +02:00
|
|
|
# add an instruction after the current (insertion point)
|
|
|
|
# the added instruction will become the new insertion point
|
2014-08-13 10:59:51 +02:00
|
|
|
def add_code instruction
|
2014-10-04 11:52:47 +02:00
|
|
|
raise instruction.inspect unless (instruction.is_a?(Instruction) or instruction.is_a?(Register::Instruction))
|
2014-08-13 10:59:51 +02:00
|
|
|
@current.add_code(instruction) #insert after current
|
|
|
|
self
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
|
|
|
|
2014-08-13 10:59:51 +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 =[]
|
|
|
|
# call assigns the return register, but as it is in l_block, it is not asked.
|
2014-08-22 17:00:23 +02:00
|
|
|
assigned = [ RegisterReference.new(Register::RegisterMachine.instance.return_register) ]
|
2014-08-13 10:59:51 +02:00
|
|
|
l_block.reachable.each do |b|
|
|
|
|
b.uses.each {|u|
|
|
|
|
(used << u) unless assigned.include?(u)
|
|
|
|
}
|
|
|
|
assigned += b.assigns
|
|
|
|
end
|
|
|
|
used.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
# control structures need to see blocks as a graph, but they are stored as a list with implict branches
|
|
|
|
# So when creating a new block (with new_block), it is only added to the list, but instructions
|
|
|
|
# still go to the current one
|
|
|
|
# With this function one can change the current block, to actually code it.
|
|
|
|
# This juggling is (unfortunately) neccessary, as all compile functions just keep puring their code into the
|
|
|
|
# method and don't care what other compiles (like if's) do.
|
|
|
|
|
|
|
|
# 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 current block
|
|
|
|
@current = 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 , the 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
|
2014-08-30 19:55:22 +02:00
|
|
|
new_b = Block.new( new_name , self )
|
2014-08-13 10:59:51 +02:00
|
|
|
index = @blocks.index( @current )
|
|
|
|
@blocks.insert( index + 1 , new_b ) # + one because we want the ne after the insert_at
|
|
|
|
return new_b
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-10 16:14:38 +02:00
|
|
|
# determine whether this method has a variable by the given name
|
|
|
|
# variables are locals and and arguments
|
|
|
|
# used to determine if a send must be issued
|
|
|
|
def has_var name
|
2014-07-16 12:20:47 +02:00
|
|
|
name = name.to_sym
|
2015-03-28 19:20:01 +01:00
|
|
|
var = @arg_names.find {|a| a.name == name }
|
2014-07-16 12:20:47 +02:00
|
|
|
var = @locals.find {|a| a.name == name } unless var
|
|
|
|
var = @tmps.find {|a| a.name == name } unless var
|
|
|
|
var
|
|
|
|
end
|
|
|
|
|
2014-07-25 19:28:38 +02:00
|
|
|
# determine whether this method has an argument by the name
|
|
|
|
def has_arg name
|
|
|
|
name = name.to_sym
|
2015-03-28 19:20:01 +01:00
|
|
|
var = @arg_names.find {|a| a.name == name }
|
2014-07-25 19:28:38 +02:00
|
|
|
var
|
|
|
|
end
|
|
|
|
|
2014-07-16 12:20:47 +02:00
|
|
|
def set_var name , var
|
|
|
|
v = has_var name
|
|
|
|
if( v )
|
2014-07-27 09:09:31 +02:00
|
|
|
v.type = var
|
2014-07-16 12:20:47 +02:00
|
|
|
else
|
|
|
|
v = Local.new(name , var)
|
|
|
|
@locals << v
|
|
|
|
end
|
|
|
|
v
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_var name
|
|
|
|
var = has_var name
|
2015-03-28 19:20:01 +01:00
|
|
|
raise "no var #{name} in method #{self.name} , #{@locals} #{@arg_names}" unless var
|
2014-07-10 16:14:38 +02:00
|
|
|
var
|
|
|
|
end
|
2014-07-15 09:35:29 +02:00
|
|
|
|
2014-07-14 23:00:00 +02:00
|
|
|
def get_tmp
|
|
|
|
name = "__tmp__#{@tmps.length}"
|
|
|
|
@tmps << name
|
|
|
|
Ast::NameExpression.new(name)
|
|
|
|
end
|
2014-08-22 16:26:49 +02:00
|
|
|
|
2014-09-16 15:06:56 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
|
|
|
end
|
2014-08-22 16:26:49 +02:00
|
|
|
# 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)
|
2015-03-28 19:20:01 +01:00
|
|
|
def method_missing(meth, *arg_names, &block)
|
|
|
|
add_code ::Register::RegisterMachine.instance.send(meth , *arg_names)
|
2014-08-22 16:26:49 +02:00
|
|
|
end
|
|
|
|
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
|
|
|
l = @blocks.inject(0) { |c , block| c += block.mem_length }
|
|
|
|
padded(l)
|
|
|
|
end
|
2014-08-30 12:47:51 +02:00
|
|
|
# position of the function is the position of the entry block, is where we call
|
|
|
|
def set_position at
|
2014-09-17 11:04:54 +02:00
|
|
|
super
|
2014-09-11 14:19:29 +02:00
|
|
|
at += 8 #for the 2 header words
|
2014-08-30 12:47:51 +02:00
|
|
|
@blocks.each do |block|
|
2014-08-30 16:08:30 +02:00
|
|
|
block.set_position at
|
2014-09-16 16:16:56 +02:00
|
|
|
at = at + block.mem_length
|
2014-08-30 12:47:51 +02:00
|
|
|
end
|
2014-08-22 16:26:49 +02:00
|
|
|
end
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2014-08-22 16:26:49 +02:00
|
|
|
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|