2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2019-07-22 14:21:16 +02:00
|
|
|
|
2019-10-03 20:07:55 +02:00
|
|
|
# CallableCompiler is used to generate slot instructions. It is an abstact base
|
2019-08-06 17:33:27 +02:00
|
|
|
# class shared by BlockCompiler and MethodCompiler
|
2019-07-22 14:21:16 +02:00
|
|
|
|
2019-10-03 20:07:55 +02:00
|
|
|
# - slot_instructions: The sequence of slot level instructions that was compiled to
|
2019-08-06 17:33:27 +02:00
|
|
|
# Instructions derive from class Instruction and form a linked list
|
2019-07-22 14:21:16 +02:00
|
|
|
|
2020-02-26 18:01:01 +01:00
|
|
|
# Since we have many different compilers, and they are kept in lists, the
|
|
|
|
# Util CompilerList encapsulates the list behaviour
|
|
|
|
#
|
2019-07-22 14:21:16 +02:00
|
|
|
class CallableCompiler
|
2019-09-28 16:24:10 +02:00
|
|
|
include Util::CompilerList
|
2019-07-22 14:21:16 +02:00
|
|
|
|
|
|
|
def initialize( callable )
|
|
|
|
@callable = callable
|
|
|
|
@constants = []
|
2019-10-03 20:07:55 +02:00
|
|
|
@slot_instructions = Label.new(source_name, source_name)
|
|
|
|
@current = start = @slot_instructions
|
2019-08-06 17:33:27 +02:00
|
|
|
add_code Label.new( source_name, "return_label")
|
2019-10-03 20:07:55 +02:00
|
|
|
add_code SlotMachine::ReturnSequence.new(source_name)
|
2019-08-06 17:33:27 +02:00
|
|
|
add_code Label.new( source_name, "unreachable")
|
2019-07-22 14:21:16 +02:00
|
|
|
@current = start
|
|
|
|
end
|
2019-10-03 20:07:55 +02:00
|
|
|
attr_reader :slot_instructions , :constants , :callable , :current
|
2019-07-22 14:21:16 +02:00
|
|
|
|
2020-02-26 18:01:01 +01:00
|
|
|
# find the return_label, every method should have exactly one (see constructor)
|
|
|
|
# used during return sequence code generation
|
2019-07-22 14:21:16 +02:00
|
|
|
def return_label
|
2019-10-03 20:07:55 +02:00
|
|
|
@slot_instructions.each do |ins|
|
2019-07-22 14:21:16 +02:00
|
|
|
next unless ins.is_a?(Label)
|
|
|
|
return ins if ins.name == "return_label"
|
|
|
|
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
|
|
|
|
|
2019-08-08 11:19:27 +02:00
|
|
|
# translate to Risc, ie a Risc level CallableCompiler
|
|
|
|
# abstract functon that needs to be implemented by Method/BlockCompiler
|
|
|
|
def to_risc
|
|
|
|
raise "abstract in #{self.class}"
|
|
|
|
end
|
|
|
|
|
2019-07-22 14:21:16 +02:00
|
|
|
# add a risc instruction after the current (insertion point)
|
|
|
|
# the added instruction will become the new insertion point
|
|
|
|
def add_code( instruction )
|
2019-10-03 20:07:55 +02:00
|
|
|
raise "Not an instruction:#{instruction.to_s}:#{instruction.class.name}" unless instruction.is_a?(SlotMachine::Instruction)
|
2019-07-22 14:21:16 +02:00
|
|
|
new_current = instruction.last #after insertion this point is lost
|
|
|
|
@current.insert(instruction) #insert after current
|
|
|
|
@current = new_current
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the frame type, ie the blocks self_type
|
|
|
|
def receiver_type
|
|
|
|
@callable.self_type
|
|
|
|
end
|
|
|
|
|
2019-08-13 18:32:17 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
# convert al instruction to risc
|
2019-08-18 09:19:52 +02:00
|
|
|
# method is called by Method/BlockCompiler from to_risc
|
2019-08-13 18:32:17 +02:00
|
|
|
def instructions_to_risc(risc_compiler)
|
2019-10-03 20:07:55 +02:00
|
|
|
instruction = slot_instructions.next
|
2019-08-13 18:32:17 +02:00
|
|
|
while( instruction )
|
2019-10-03 20:07:55 +02:00
|
|
|
raise "whats this a #{instruction}" unless instruction.is_a?(SlotMachine::Instruction)
|
|
|
|
#puts "adding slot #{instruction.to_s}:#{instruction.next.to_s}"
|
2019-08-13 18:32:17 +02:00
|
|
|
risc_compiler.reset_regs
|
2019-08-23 18:22:27 +02:00
|
|
|
instruction.to_risc( risc_compiler )
|
2019-08-13 18:32:17 +02:00
|
|
|
#puts "adding risc #{risc.to_s}:#{risc.next.to_s}"
|
|
|
|
instruction = instruction.next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-22 14:21:16 +02:00
|
|
|
end
|
|
|
|
end
|