removed blocks and moved to labels
somewhat easier to understand the code as a linked list relatively painless change, considering
This commit is contained in:
@ -10,8 +10,6 @@ module Interpreter
|
||||
attr_reader :clock # current instruction or pc
|
||||
# an (arm style) link register. store the return address to return to
|
||||
attr_reader :link
|
||||
# current executing block. since this is not a hardware simulator this is luxury
|
||||
attr_reader :block
|
||||
attr_reader :registers # the registers, 16 (a hash, sym -> contents)
|
||||
attr_reader :stdout # collect the output
|
||||
attr_reader :state # running etc
|
||||
@ -27,13 +25,12 @@ module Interpreter
|
||||
(0...12).each do |r|
|
||||
set_register "r#{r}".to_sym , "r#{r}:unknown"
|
||||
end
|
||||
@block = nil
|
||||
end
|
||||
|
||||
def start bl
|
||||
def start instruction
|
||||
@clock = 0
|
||||
set_state(:running)
|
||||
set_block bl
|
||||
set_instruction instruction
|
||||
end
|
||||
|
||||
def set_state state
|
||||
@ -43,20 +40,6 @@ module Interpreter
|
||||
trigger(:state_changed , old , state )
|
||||
end
|
||||
|
||||
def set_block bl
|
||||
return if @block == bl
|
||||
raise "Error, nil block" unless bl
|
||||
old = @block
|
||||
if bl.codes.empty?
|
||||
next_b = @block.method.source.blocks.index(bl) + 1
|
||||
bl = @block.method.source.blocks[next_b]
|
||||
end
|
||||
raise "Block #{bl.codes.empty?}" if bl.codes.empty? #just fixed, leave for next time
|
||||
@block = bl
|
||||
trigger(:block_changed , old , bl)
|
||||
set_instruction bl.codes.first
|
||||
end
|
||||
|
||||
def set_instruction i
|
||||
return if @instruction == i
|
||||
old = @instruction
|
||||
@ -88,7 +71,6 @@ module Interpreter
|
||||
def tick
|
||||
return unless @instruction
|
||||
@clock += 1
|
||||
#puts @instruction
|
||||
name = @instruction.class.name.split("::").last
|
||||
fetch = send "execute_#{name}"
|
||||
return unless fetch
|
||||
@ -96,12 +78,7 @@ module Interpreter
|
||||
end
|
||||
|
||||
def fetch_next_intruction
|
||||
if(@instruction != @block.codes.last)
|
||||
set_instruction @block.codes[ @block.codes.index(@instruction) + 1]
|
||||
else
|
||||
next_b = @block.method.source.blocks.index(@block) + 1
|
||||
set_block @block.method.source.blocks[next_b]
|
||||
end
|
||||
set_instruction @instruction.next
|
||||
end
|
||||
|
||||
def object_for reg
|
||||
@ -110,10 +87,14 @@ module Interpreter
|
||||
object.nil? ? id : object
|
||||
end
|
||||
|
||||
# Label is a noop.
|
||||
def execute_Label
|
||||
true
|
||||
end
|
||||
# Instruction interpretation starts here
|
||||
def execute_Branch
|
||||
target = @instruction.block
|
||||
set_block target
|
||||
label = @instruction.label
|
||||
set_instruction label
|
||||
false
|
||||
end
|
||||
|
||||
@ -161,10 +142,9 @@ module Interpreter
|
||||
end
|
||||
|
||||
def execute_FunctionCall
|
||||
@link = [@block , @instruction]
|
||||
@link = @instruction
|
||||
#puts "Call link #{@link}"
|
||||
next_block = @instruction.method.source.blocks.first
|
||||
set_block next_block
|
||||
set_instruction @instruction.method.source.instructions
|
||||
false
|
||||
end
|
||||
|
||||
@ -182,7 +162,7 @@ module Interpreter
|
||||
object = object_for( @instruction.register )
|
||||
link = object.internal_object_get( @instruction.index )
|
||||
#puts "FunctionReturn link #{@link}"
|
||||
@block , @instruction = link
|
||||
@instruction = link
|
||||
# we jump back to the call instruction. so it is as if the call never happened and we continue
|
||||
true
|
||||
end
|
||||
|
@ -1,61 +0,0 @@
|
||||
module Register
|
||||
|
||||
# Think flowcharts: blocks are the boxes. The smallest unit of linear code
|
||||
|
||||
# Blocks must end in control instructions (jump/call/return).
|
||||
# And the only valid argument for a jump is a Block
|
||||
|
||||
# Blocks form a graph, which is managed by the method
|
||||
|
||||
class Block
|
||||
|
||||
def initialize(name , method )
|
||||
super()
|
||||
@method = method
|
||||
raise "Method is not Method, but #{method.class}" unless method == :__init__ or method.is_a?(Parfait::Method)
|
||||
@name = name.to_sym
|
||||
@codes = []
|
||||
end
|
||||
|
||||
attr_reader :name , :codes , :method , :position
|
||||
|
||||
def add_code kode
|
||||
@codes << kode
|
||||
self
|
||||
end
|
||||
|
||||
# replace a code with an array of new codes. This is what happens in passes all the time
|
||||
def replace code , new_codes
|
||||
index = @codes.index code
|
||||
raise "Code not found #{code} in #{self}" unless index
|
||||
@codes.delete_at(index)
|
||||
if( new_codes.is_a? Array)
|
||||
new_codes.reverse.each {|c| @codes.insert(index , c)}
|
||||
else
|
||||
@codes.insert(index , new_codes)
|
||||
end
|
||||
end
|
||||
|
||||
# position is what another block uses to jump to. this is determined by the assembler
|
||||
# the assembler allso assembles and assumes a linear instruction sequence
|
||||
# Note: this will have to change for plocks and maybe anyway.
|
||||
def set_position at
|
||||
@position = at
|
||||
@codes.each do |code|
|
||||
begin
|
||||
code.set_position( at)
|
||||
rescue => e
|
||||
puts "BLOCK #{self.to_s[0..5000]}"
|
||||
raise e
|
||||
end
|
||||
raise code.inspect unless code.byte_length
|
||||
at += code.byte_length
|
||||
end
|
||||
end
|
||||
|
||||
def byte_length
|
||||
@codes.inject(0){|count , instruction| count += instruction.byte_length }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -9,8 +9,10 @@ module Register
|
||||
function = MethodSource.create_method(:Kernel,:Integer,:__init__ , [])
|
||||
function.source.set_return_type :Integer
|
||||
# no method enter or return (automatically added), remove
|
||||
function.source.blocks.first.codes.pop # no Method enter
|
||||
function.source.blocks.last.codes.pop # no Method return
|
||||
new_start = Label.new(function , "__init__" )
|
||||
function.source.instructions = new_start
|
||||
function.source.current = new_start
|
||||
|
||||
#Set up the Space as self upon init
|
||||
space = Parfait::Space.object_space
|
||||
space_reg = Register.tmp_reg(:Space)
|
||||
|
@ -7,29 +7,54 @@ module Register
|
||||
# but we keep the names for better understanding, r4/5 are temporary/scratch
|
||||
# there is no direct memory access, only through registers
|
||||
# constants can/must be loaded into registers before use
|
||||
|
||||
# Instructions form a graph.
|
||||
# Linear instructions form a linked list
|
||||
# Branches fan out, Labels collect
|
||||
# Labels are the only valid branch targets
|
||||
class Instruction
|
||||
|
||||
def initialize source
|
||||
def initialize source , nekst = nil
|
||||
@source = source
|
||||
@next = nekst
|
||||
end
|
||||
attr_reader :source
|
||||
|
||||
# returns an array of registers (RegisterValues) that this instruction uses.
|
||||
# ie for r1 = r2 + r3
|
||||
# which in assembler is add r1 , r2 , r3
|
||||
# it would return [r2,r3]
|
||||
# for pushes the list may be longer, whereas for a jump empty
|
||||
def uses
|
||||
raise "abstract called for #{self.class}"
|
||||
# set the next instruction (also aliased as <<)
|
||||
# throw an error if that is set, use insert for that use case
|
||||
# return the instruction, so chaining works as one wants (not backwards)
|
||||
def set_next nekst
|
||||
raise "Next already set #{@next}" if @next
|
||||
@next = nekst
|
||||
nekst
|
||||
end
|
||||
# returns an array of registers (RegisterValues) that this instruction assigns to.
|
||||
# ie for r1 = r2 + r3
|
||||
# which in assembler is add r1 , r2 , r3
|
||||
# it would return [r1]
|
||||
# for most instruction this is one, but comparisons and jumps 0 , and pop's as long as 16
|
||||
def assigns
|
||||
raise "abstract called for #{self.class}"
|
||||
alias :<< :set_next
|
||||
|
||||
# get the next instruction (without arg given )
|
||||
# when given an interger, advance along the line that many time and return.
|
||||
def next( amount = 1)
|
||||
(amount == 1) ? @next : @next.next(amount-1)
|
||||
end
|
||||
# set the give instruction as the next, while moving any existing
|
||||
# instruction along to the given ones's next.
|
||||
# ie insert into the linked list that the instructions form
|
||||
def insert instruction
|
||||
instruction.set_next @next
|
||||
@next = instruction
|
||||
end
|
||||
|
||||
def length labels = []
|
||||
ret = 1
|
||||
ret += self.next.length( labels ) if self.next
|
||||
ret
|
||||
end
|
||||
|
||||
def to_ac labels = []
|
||||
ret = [self.class]
|
||||
ret += self.next.to_ac(labels) if self.next
|
||||
ret
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@ -42,5 +67,6 @@ require_relative "instructions/function_call"
|
||||
require_relative "instructions/function_return"
|
||||
require_relative "instructions/save_return"
|
||||
require_relative "instructions/register_transfer"
|
||||
require_relative "instructions/label"
|
||||
require_relative "instructions/branch"
|
||||
require_relative "instructions/operator_instruction"
|
||||
|
@ -6,14 +6,22 @@ module Register
|
||||
def initialize source , to
|
||||
super(source)
|
||||
raise "No block" unless to
|
||||
@block = to
|
||||
@label = to
|
||||
end
|
||||
attr_reader :block
|
||||
attr_reader :label
|
||||
|
||||
def to_s
|
||||
"#{self.class.name}: #{block.name}"
|
||||
"#{self.class.name}: #{label.name}"
|
||||
end
|
||||
alias :inspect :to_s
|
||||
|
||||
def length labels = []
|
||||
super(labels) + self.label.length(labels)
|
||||
end
|
||||
|
||||
def to_ac labels = []
|
||||
super(labels) + self.label.to_ac(labels)
|
||||
end
|
||||
end
|
||||
|
||||
class IsZero < Branch
|
||||
|
34
lib/register/instructions/label.rb
Normal file
34
lib/register/instructions/label.rb
Normal file
@ -0,0 +1,34 @@
|
||||
module Register
|
||||
|
||||
# A label is a placeholder for it's next Instruction
|
||||
# It's function is not to turn into code, but to be a valid brnch target
|
||||
#
|
||||
# So branches and Labels are pairs, fan out, fan in
|
||||
#
|
||||
#
|
||||
|
||||
class Label < Instruction
|
||||
def initialize source , name , nekst = nil
|
||||
super(source , nekst)
|
||||
@name = name
|
||||
end
|
||||
attr_reader :name
|
||||
|
||||
def to_s
|
||||
"Label: #{@name} (#{self.next.class})"
|
||||
end
|
||||
|
||||
def to_ac labels = []
|
||||
return [] if labels.include?(self)
|
||||
labels << self
|
||||
self.next.to_ac(labels)
|
||||
end
|
||||
|
||||
def length labels = []
|
||||
return 0 if labels.include?(self)
|
||||
labels << self
|
||||
1 + self.next.length(labels)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -117,9 +117,7 @@ module Register
|
||||
|
||||
def boot
|
||||
boot_parfait!
|
||||
@init = Block.new("init", :__init__ )
|
||||
branch = Branch.new( "__init__" , self.space.get_init.source.blocks.first )
|
||||
@init.add_code branch
|
||||
@init = Branch.new( "__init__" , self.space.get_init.source.instructions )
|
||||
@booted = true
|
||||
self
|
||||
end
|
||||
|
@ -1,18 +1,8 @@
|
||||
require_relative "block"
|
||||
|
||||
module Register
|
||||
# the static info of a method (with its compiled code, argument names etc ) is part of the
|
||||
# runtime, ie found in Parfait::Method
|
||||
|
||||
# the source we create here is injected into the method and used only at compile-time
|
||||
|
||||
#
|
||||
# Methods are one step up from to VM::Blocks. Where Blocks can be jumped to, Methods can be called.
|
||||
|
||||
# Methods also have arguments and a return. These are typed by subclass instances of Value
|
||||
|
||||
# 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)
|
||||
|
||||
@ -55,23 +45,21 @@ module Register
|
||||
end
|
||||
|
||||
def init method , return_type = nil
|
||||
# first block we have to create with .new , as new_block assumes a current
|
||||
enter = Block.new( "enter" , method )
|
||||
enter.add_code Register.save_return(self, :message , :return_address)
|
||||
set_return_type( return_type )
|
||||
@blocks = [enter]
|
||||
@current = enter
|
||||
ret = new_block("return")
|
||||
@instructions = @current = Label.new(self, "Method_#{method.name}")
|
||||
add_code enter = Register.save_return(self, :message , :return_address)
|
||||
add_code Label.new( method, "return")
|
||||
# move the current message to new_message
|
||||
ret.add_code RegisterTransfer.new(self, Register.message_reg , Register.new_message_reg )
|
||||
add_code RegisterTransfer.new(self, Register.message_reg , Register.new_message_reg )
|
||||
# and restore the message from saved value in new_message
|
||||
ret.add_code Register.get_slot(self,:new_message , :caller , :message )
|
||||
add_code Register.get_slot(self,:new_message , :caller , :message )
|
||||
#load the return address into pc, affecting return. (other cpus have commands for this, but not arm)
|
||||
ret.add_code FunctionReturn.new( self , Register.new_message_reg , Register.resolve_index(:message , :return_address) )
|
||||
add_code FunctionReturn.new( self , Register.new_message_reg , Register.resolve_index(:message , :return_address) )
|
||||
@current = enter
|
||||
@constants = []
|
||||
end
|
||||
attr_reader :blocks , :constants , :return_type
|
||||
attr_accessor :current , :receiver
|
||||
attr_reader :constants , :return_type
|
||||
attr_accessor :current , :receiver , :instructions
|
||||
|
||||
def set_return_type type
|
||||
return if type.nil?
|
||||
@ -84,85 +72,25 @@ module Register
|
||||
unless instruction.is_a?(Instruction)
|
||||
raise instruction.to_s
|
||||
end
|
||||
@current.add_code(instruction) #insert after current
|
||||
@current.insert(instruction) #insert after current
|
||||
@current = instruction
|
||||
self
|
||||
end
|
||||
|
||||
# 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.
|
||||
# assigned = [ RegisterValue.new(RegisterMachine.instance.return_register) ]
|
||||
# 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 statement creates the extra blocks, adds them and the code and then "moves"
|
||||
# the insertion point along
|
||||
def current block
|
||||
@current = block
|
||||
# set the insertion point (where code is added with add_code)
|
||||
def current ins
|
||||
@current = ins
|
||||
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
|
||||
new_b = Block.new( new_name , @blocks.first.method )
|
||||
index = @blocks.index( @current )
|
||||
@blocks.insert( index + 1 , new_b ) # + one because we want the ne after the insert_at
|
||||
return new_b
|
||||
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 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, *arguments, &block)
|
||||
# add_code ::Arm::ArmMachine.send(meth , *arguments)
|
||||
# end
|
||||
|
||||
def byte_length
|
||||
@blocks.inject(0) { |c , block| c += block.byte_length }
|
||||
@instructions.byte_length
|
||||
end
|
||||
|
||||
# position of the function is the position of the entry block, is where we call
|
||||
def set_position at
|
||||
at += 8 #for the 2 header words
|
||||
@blocks.each do |block|
|
||||
block.set_position at
|
||||
at = at + block.byte_length
|
||||
end
|
||||
@instructions.set_position at
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,39 +1,35 @@
|
||||
module Soml
|
||||
Compiler.class_eval do
|
||||
# if - attr_reader :cond, :if_true, :if_false
|
||||
|
||||
# an if evaluates the condition and jumps to the true block if true
|
||||
# so the else block is automatically after that.
|
||||
# But then the else needs to jump over the true block unconditionally.
|
||||
def on_if_statement statement
|
||||
branch_type , condition , if_true , if_false = *statement
|
||||
condition = condition.first
|
||||
# to execute the logic as the if states it, the blocks are the other way around
|
||||
# so we can the jump over the else if true ,
|
||||
# and the else joins unconditionally after the true_block
|
||||
merge_block = @method.source.new_block "if_merge" # last one, created first
|
||||
true_block = @method.source.new_block "if_true" # second, linked in after current, before merge
|
||||
false_block = @method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here
|
||||
|
||||
reset_regs
|
||||
is = process(condition)
|
||||
process(condition)
|
||||
|
||||
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
||||
true_block = Register::Label.new(statement, "if_true")
|
||||
add_code branch_class.new( condition , true_block )
|
||||
|
||||
# compile the true block (as we think of it first, even it is second in sequential order)
|
||||
@method.source.current true_block
|
||||
|
||||
reset_regs
|
||||
last = process_all(if_true).last
|
||||
|
||||
# compile the false block
|
||||
@method.source.current false_block
|
||||
reset_regs
|
||||
last = process_all(if_false).last if if_false
|
||||
add_code Register::Branch.new(statement, merge_block )
|
||||
process_all(if_false) if if_false
|
||||
merge = Register::Label.new(statement , "if_merge")
|
||||
add_code Register::Branch.new(statement, merge )
|
||||
|
||||
# compile the true block
|
||||
add_code true_block
|
||||
reset_regs
|
||||
process_all(if_true)
|
||||
|
||||
#puts "compiled if: end"
|
||||
@method.source.current merge_block
|
||||
add_code merge
|
||||
|
||||
#TODO should return the union of the true and false types
|
||||
last
|
||||
nil # statements don't return anything
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -6,25 +6,25 @@ module Soml
|
||||
branch_type , condition , statements = *statement
|
||||
condition = condition.first
|
||||
|
||||
# this is where the while ends and both branches meet
|
||||
merge = @method.source.new_block("while merge")
|
||||
# this comes after the current and beofre the merge
|
||||
start = @method.source.new_block("while_start" )
|
||||
@method.source.current start
|
||||
add_code start = Register::Label.new(statement , "while_start" )
|
||||
|
||||
cond = process(condition)
|
||||
reset_regs
|
||||
process(condition)
|
||||
|
||||
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
||||
# this is where the while ends and both branches meet
|
||||
merge = Register::Label.new(statement , "while_merge")
|
||||
add_code branch_class.new( condition , merge )
|
||||
|
||||
last = process_all(statements).last
|
||||
reset_regs
|
||||
process_all(statements)
|
||||
|
||||
# unconditionally branch to the start
|
||||
add_code Register::Branch.new(statement,start)
|
||||
|
||||
# continue execution / compiling at the merge block
|
||||
@method.source.current merge
|
||||
last
|
||||
add_code merge
|
||||
nil # statements don't return anything
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user