create a load on the compiler

thus removing the need for << with objects on RegisterValue
This commit is contained in:
2020-03-01 10:52:21 +02:00
parent 95f3eec043
commit 64d860b2bf
7 changed files with 48 additions and 92 deletions

View File

@ -90,18 +90,16 @@ module Risc
copied
end
# releasing a register (accuired by use_reg) makes it available for use again
# thus avoiding possibly using too many registers
def release_reg( reg )
last = @allocator.pop
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
end
# reset the registers to be used. Start at r4 for next usage.
# Every statement starts with this, meaning each statement may use all registers, but none
# get saved. Statements have affect on objects.
def reset_regs
@allocator.clear_regs
# Load a constant, meaning create a LoadConstant instruction for the constant
# add the instruction to the code and return the register_value that was created
# for further use
def load_object( object )
raise "must be Parfait, not #{object.class}" unless object.is_a?(Parfait::Object)
ins = Risc.load_constant("load to #{object.type}" , object)
add_code ins
# todo for constants (not objects)
# add_constant(right) if compiler
ins.register
end
# Build with builder (see there), adding the created instructions

View File

@ -14,10 +14,10 @@ module Risc
# or r-vlalue respectively.
class RegisterSlot
attr_reader :register , :index , :builder
attr_reader :register , :index , :compiler
def initialize(register, index , builder)
@register , @index , @builder = register , index , builder
def initialize(register, index , compiler)
@register , @index , @compiler = register , index , compiler
end
# fullfil the objects purpose by creating a RegToSlot instruction from
@ -25,7 +25,7 @@ module Risc
def <<( reg )
raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
reg_to_slot = Risc.reg_to_slot("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
builder.add_code(reg_to_slot) if builder
compiler.add_code(reg_to_slot) if compiler
reg_to_slot
end
@ -34,7 +34,7 @@ module Risc
def <=( reg )
raise "not reg #{reg}" unless reg.is_a?(RegisterValue)
reg_to_byte = Risc.reg_to_byte("#{reg.class_name} -> #{register.class_name}[#{index}]" , reg , register, index)
builder.add_code(reg_to_byte) if builder
compiler.add_code(reg_to_byte) if compiler
reg_to_byte
end

View File

@ -5,13 +5,13 @@ module Risc
# The type is always known, and sometimes the value too
# Or something about the value, like some instances types
#
# When participating in the builder dsl, a builder may be set to get the
# results of dsl operations (like <<) back to the builder
# When participating in the compiler dsl, a compiler may be set to get the
# results of dsl operations (like <<) back to the compiler
class RegisterValue
attr_reader :symbol , :type , :extra
attr_reader :builder
attr_reader :compiler
# The first arg is a symbol :r0 - :r12
# Second arg is the type, which may be given as the symbol of the class name
@ -34,11 +34,11 @@ module Risc
@type.class_name
end
# allows to set the builder, which is mainly done by the builder
# allows to set the compiler, which is mainly done by the compiler
# but sometimes, eg in exit, one nneds to create the reg by hand and set
# return the RegisterValue for chaining in assignment
def set_builder( builder )
@builder = builder
def set_compiler( compiler )
@compiler = compiler
self
end
@ -64,10 +64,10 @@ module Risc
type.type_at(index)
end
# reduce integer to fixnum and add instruction if builder is used
# reduce integer to fixnum and add instruction if compiler is used
def reduce_int
reduce = Risc::SlotToReg.new( "int -> fix" , self , Parfait::Integer.integer_index , self)
builder.add_code(reduce) if builder
compiler.add_code(reduce) if compiler
reduce
end
@ -136,25 +136,19 @@ module Risc
# - an RegisterSlot, resulting in an SlotToReg
def <<( right )
case right
when Symbol
ins = Risc::LoadConstant.new("#{right.class} to #{self.type}" , right , self)
when Parfait::Object
ins = Risc::LoadConstant.new("#{right.class} to #{self.type}" , right , self)
builder.compiler.add_constant(right) if builder
when Label
ins = Risc::LoadConstant.new("#{right.class} to #{self.type}" , right , self)
builder.compiler.add_constant(right.address) if builder
compiler.compiler.add_constant(right.address) if compiler
when ::Integer
ins = Risc.load_data("#{right.class} to #{self.type}" , right , self)
when RegisterValue
ins = Risc.transfer("#{right.type} to #{self.type}" , right , self)
when RegisterSlot
puts right.to_s
ins = Risc::SlotToReg.new("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
else
raise "not implemented for #{right.class}:#{right}"
end
builder.add_code(ins) if builder
compiler.add_code(ins) if compiler
return ins
end
@ -163,14 +157,14 @@ module Risc
def <=( right )
raise "not implemented for #{right.class}:#{right}" unless right.is_a?( RegisterSlot )
ins = Risc.byte_to_reg("#{right.register.type}[#{right.index}] -> #{self.type}" , right.register , right.index , self)
builder.add_code(ins) if builder
compiler.add_code(ins) if compiler
return ins
end
def -( right )
raise "operators only on registers, not #{right.class}" unless right.is_a? RegisterValue
op = Risc.op("#{self.type} - #{right.type}", :- , self , right )
builder.add_code(op) if builder
compiler.add_code(op) if compiler
op
end
@ -178,7 +172,7 @@ module Risc
# doesn't read quite as smoothly as one would like, but better than the compiler version
def op( operator , right)
ret = Risc.op( "operator #{operator}" , operator , self , right)
builder.add_code(ret) if builder
compiler.add_code(ret) if compiler
ret
end
@ -186,7 +180,7 @@ module Risc
# The RegisterSlot then gets used in a RegToSlot or SlotToReg, where
# the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg
def []( index )
RegisterSlot.new( self , index , builder)
RegisterSlot.new( self , index , compiler)
end
end