2019-07-22 14:21:16 +02:00
|
|
|
module Mom
|
|
|
|
|
2019-08-06 17:33:27 +02:00
|
|
|
# CallableCompiler is used to generate mom instructions. It is an abstact base
|
|
|
|
# class shared by BlockCompiler and MethodCompiler
|
2019-07-22 14:21:16 +02:00
|
|
|
|
2019-08-06 17:33:27 +02:00
|
|
|
# - mom_instructions: The sequence of mom level instructions that mom was compiled to
|
|
|
|
# Instructions derive from class Instruction and form a linked list
|
2019-07-22 14:21:16 +02:00
|
|
|
|
|
|
|
class CallableCompiler
|
|
|
|
|
|
|
|
def initialize( callable )
|
|
|
|
@callable = callable
|
|
|
|
@constants = []
|
|
|
|
@block_compilers = []
|
2019-08-06 17:33:27 +02:00
|
|
|
@mom_instructions = Label.new(source_name, source_name)
|
2019-08-07 11:06:06 +02:00
|
|
|
@current = start = @mom_instructions
|
2019-08-06 17:33:27 +02:00
|
|
|
add_code Label.new( source_name, "return_label")
|
2019-08-07 11:06:06 +02:00
|
|
|
add_code Mom::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-08-07 11:06:06 +02:00
|
|
|
attr_reader :mom_instructions , :constants , :block_compilers , :callable , :current
|
2019-07-22 14:21:16 +02:00
|
|
|
|
|
|
|
def return_label
|
2019-08-07 11:06:06 +02:00
|
|
|
@mom_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-08-06 17:33:27 +02:00
|
|
|
raise "Not an instruction:#{instruction.to_s}:#{instruction.class.name}" unless instruction.is_a?(Mom::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)
|
|
|
|
instruction = mom_instructions.next
|
|
|
|
while( instruction )
|
|
|
|
raise "whats this a #{instruction}" unless instruction.is_a?(Mom::Instruction)
|
|
|
|
#puts "adding mom #{instruction.to_s}:#{instruction.next.to_s}"
|
|
|
|
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
|