2018-07-10 21:02:11 +02:00
|
|
|
module Risc
|
|
|
|
|
|
|
|
# CallableCompiler is used to generate risc instructions. It is an abstact base
|
|
|
|
# class shared by BlockCompiler and MethodCompiler
|
|
|
|
|
|
|
|
# - risc_instructions: The sequence of risc level instructions that mom was compiled to
|
|
|
|
# - cpu_instructions: The sequence of cpu specific instructions that the
|
|
|
|
# risc_instructions was compiled to
|
|
|
|
# Instructions derive from class Instruction and form a linked list
|
|
|
|
|
|
|
|
class CallableCompiler
|
|
|
|
|
2018-07-30 09:26:11 +02:00
|
|
|
def initialize( callable )
|
|
|
|
@callable = callable
|
2018-07-10 21:02:11 +02:00
|
|
|
@regs = []
|
|
|
|
@constants = []
|
|
|
|
@block_compilers = []
|
2018-08-02 16:36:39 +02:00
|
|
|
@risc_instructions = Risc.label(source_name, source_name)
|
2018-08-19 16:29:04 +02:00
|
|
|
@current = start = @risc_instructions
|
2018-08-19 12:16:07 +02:00
|
|
|
add_code Risc.label( source_name, "return_label")
|
|
|
|
Mom::ReturnSequence.new.to_risc(self)
|
|
|
|
add_code Risc.label( source_name, "unreachable")
|
2018-08-19 16:29:04 +02:00
|
|
|
@current = start
|
2018-08-02 16:36:39 +02:00
|
|
|
reset_regs
|
|
|
|
end
|
2018-08-19 12:16:07 +02:00
|
|
|
attr_reader :risc_instructions , :constants , :block_compilers , :callable , :current
|
2018-08-02 16:36:39 +02:00
|
|
|
|
|
|
|
def return_label
|
|
|
|
@risc_instructions.each do |ins|
|
|
|
|
next unless ins.is_a?(Label)
|
|
|
|
return ins if ins.name == "return_label"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:02:11 +02:00
|
|
|
# convert the given mom instruction to_risc and then add it (see add_code)
|
|
|
|
# continue down the instruction chain unti depleted
|
|
|
|
# (adding moves the insertion point so the whole mom chain is added as a risc chain)
|
|
|
|
def add_mom( instruction )
|
|
|
|
while( instruction )
|
|
|
|
raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction)
|
|
|
|
#puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}"
|
2018-08-19 12:16:07 +02:00
|
|
|
instruction.to_risc( self )
|
2018-07-10 21:02:11 +02:00
|
|
|
reset_regs
|
|
|
|
#puts "adding risc #{risc.to_s}:#{risc.next.to_s}"
|
|
|
|
instruction = instruction.next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# add a constant (which get created during compilation and need to be linked)
|
|
|
|
def add_constant(const)
|
|
|
|
raise "Must be Parfait #{const}" unless const.is_a?(Parfait::Object)
|
|
|
|
@constants << const
|
|
|
|
end
|
|
|
|
|
|
|
|
# add a risc instruction after the current (insertion point)
|
|
|
|
# the added instruction will become the new insertion point
|
|
|
|
def add_code( instruction )
|
|
|
|
raise "Not an instruction:#{instruction.to_s}" unless instruction.is_a?(Risc::Instruction)
|
|
|
|
raise instruction.to_s if( instruction.class.name.split("::").first == "Arm")
|
|
|
|
new_current = instruction.last #after insertion this point is lost
|
|
|
|
@current.insert(instruction) #insert after current
|
|
|
|
@current = new_current
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# require a (temporary) register. code must give this back with release_reg
|
2018-07-16 10:23:09 +02:00
|
|
|
# Second extra parameter may give extra info about the value, see RegisterValue
|
|
|
|
def use_reg( type , extra = {} )
|
2018-07-10 21:02:11 +02:00
|
|
|
raise "Not type #{type.inspect}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)
|
|
|
|
if @regs.empty?
|
2018-07-16 10:23:09 +02:00
|
|
|
reg = Risc.tmp_reg(type , extra)
|
2018-07-10 21:02:11 +02:00
|
|
|
else
|
2018-07-16 10:23:09 +02:00
|
|
|
reg = @regs.last.next_reg_use(type , extra)
|
2018-07-10 21:02:11 +02:00
|
|
|
end
|
|
|
|
@regs << reg
|
|
|
|
return reg
|
|
|
|
end
|
|
|
|
|
2018-07-16 10:46:18 +02:00
|
|
|
# resolve the type of the slot, by inferring from it's name, using the type
|
2018-07-17 09:37:33 +02:00
|
|
|
# scope related slots are resolved by the compiler by method/block
|
2018-07-16 10:46:18 +02:00
|
|
|
def slot_type( slot , type)
|
|
|
|
case slot
|
2018-07-16 11:03:40 +02:00
|
|
|
when :frame
|
|
|
|
new_type = self.frame_type
|
|
|
|
when :arguments
|
|
|
|
new_type = self.arg_type
|
|
|
|
when :receiver
|
|
|
|
new_type = self.receiver_type
|
2018-07-16 10:46:18 +02:00
|
|
|
when Symbol
|
|
|
|
new_type = type.type_for(slot)
|
|
|
|
raise "Not found object #{slot}: in #{type}" unless new_type
|
|
|
|
else
|
|
|
|
raise "Not implemented object #{slot}:#{slot.class}"
|
|
|
|
end
|
|
|
|
#puts "RESOLVE in #{@type.class_name} #{slot}->#{type}"
|
|
|
|
return new_type
|
|
|
|
end
|
|
|
|
|
2018-08-16 09:43:41 +02:00
|
|
|
# return the frame type, ie the blocks frame type
|
|
|
|
def frame_type
|
|
|
|
@callable.frame_type
|
|
|
|
end
|
|
|
|
# return the frame type, ie the blocks arguments type
|
|
|
|
def arg_type
|
|
|
|
@callable.arguments_type
|
|
|
|
end
|
|
|
|
# return the frame type, ie the blocks self_type
|
|
|
|
def receiver_type
|
|
|
|
@callable.self_type
|
|
|
|
end
|
|
|
|
|
2018-07-10 21:02:11 +02:00
|
|
|
def copy( reg , source )
|
|
|
|
copied = use_reg reg.type
|
|
|
|
add_code Register.transfer( source , reg , copied )
|
|
|
|
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 = @regs.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
|
|
|
|
@regs.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build with builder (see there), adding the created instructions
|
2018-08-16 09:43:41 +02:00
|
|
|
def build(source , &block)
|
2018-08-19 12:16:07 +02:00
|
|
|
builder(source).build(&block)
|
2018-07-10 21:02:11 +02:00
|
|
|
end
|
|
|
|
|
2018-08-19 12:16:07 +02:00
|
|
|
# return a Builder, that adds the generated code to this compiler
|
|
|
|
def builder( source)
|
|
|
|
Builder.new(self , source)
|
2018-07-10 21:02:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|