first tests comping through after ast/compile change
This commit is contained in:
@@ -14,20 +14,20 @@ module Virtual
|
||||
|
||||
# Methods also have arguments and a return. These are typed by subclass instances of Value
|
||||
|
||||
# They also have local variables.
|
||||
# They also have local variables.
|
||||
|
||||
# Code-wise Methods are made up from a list of Blocks, in a similar way blocks are made up of Instructions
|
||||
# The function starts with one block, and that has a start and end (return)
|
||||
|
||||
|
||||
# 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.
|
||||
# "physically" use the block set_next for this.
|
||||
# This "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
|
||||
# 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.
|
||||
|
||||
|
||||
class CompiledMethod < Virtual::Object
|
||||
#return the main function (the top level) into which code is compiled
|
||||
def CompiledMethod.main
|
||||
@@ -63,10 +63,10 @@ module Virtual
|
||||
def locals_at l_block
|
||||
used =[]
|
||||
# call assigns the return register, but as it is in l_block, it is not asked.
|
||||
assigned = [ RegisterReference.new(Register::RegisterMachine.instance.return_register) ]
|
||||
assigned = [ RegisterReference.new(Virtual::RegisterMachine.instance.return_register) ]
|
||||
l_block.reachable.each do |b|
|
||||
b.uses.each {|u|
|
||||
(used << u) unless assigned.include?(u)
|
||||
(used << u) unless assigned.include?(u)
|
||||
}
|
||||
assigned += b.assigns
|
||||
end
|
||||
@@ -77,15 +77,15 @@ module Virtual
|
||||
# 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
|
||||
# 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
|
||||
# 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
|
||||
@@ -93,8 +93,8 @@ module Virtual
|
||||
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.
|
||||
# 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)
|
||||
@@ -109,7 +109,7 @@ module Virtual
|
||||
|
||||
# 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
|
||||
# used to determine if a send must be issued
|
||||
def has_var name
|
||||
name = name.to_sym
|
||||
var = @arg_names.find {|a| a.name == name }
|
||||
@@ -151,23 +151,23 @@ module Virtual
|
||||
def layout
|
||||
Virtual::Object.layout
|
||||
end
|
||||
# sugar to create instructions easily.
|
||||
# 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,
|
||||
# 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
|
||||
# mov and add will be called on Machine and generate Instructions 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, *arg_names, &block)
|
||||
add_code ::Register::RegisterMachine.instance.send(meth , *arg_names)
|
||||
add_code ::Arm::ArmMachine.send(meth , *arg_names)
|
||||
end
|
||||
|
||||
def mem_length
|
||||
l = @blocks.inject(0) { |c , block| c += block.mem_length }
|
||||
padded(l)
|
||||
padded(l)
|
||||
end
|
||||
# position of the function is the position of the entry block, is where we call
|
||||
def set_position at
|
||||
|
@@ -50,7 +50,8 @@ module Virtual
|
||||
# read all the files needed for a minimal system at compile
|
||||
classes = ["object"]
|
||||
classes.each do |clazz|
|
||||
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
|
||||
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
|
||||
bytes = 0 #shuts up my atom linter
|
||||
# expression = compile_main(bytes)
|
||||
end
|
||||
end
|
||||
@@ -59,7 +60,7 @@ module Virtual
|
||||
syntax = @parser.parse_with_debug(bytes)
|
||||
parts = Parser::Transform.new.apply(syntax)
|
||||
main = Virtual::CompiledMethod.main
|
||||
parts.compile( main , self.message )
|
||||
Compiler.compile( parts , main , self.message )
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user