2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2014-06-14 09:59:25 +02:00
|
|
|
|
2018-04-08 17:51:20 +02:00
|
|
|
# A Builder is used to generate code, either by using it's api, or dsl
|
|
|
|
#
|
2018-08-19 12:16:07 +02:00
|
|
|
# The code is added to the method_compiler.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-06 13:21:38 +02:00
|
|
|
class Builder
|
2014-06-14 09:59:25 +02:00
|
|
|
|
2018-04-06 23:14:02 +02:00
|
|
|
attr_reader :built , :compiler
|
2018-04-06 13:54:24 +02:00
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# pass a compiler, to which instruction are added (usually)
|
2018-04-08 17:51:20 +02:00
|
|
|
# second arg determines weather instructions are added (default true)
|
|
|
|
# call build with a block to build
|
2018-06-29 12:27:57 +02:00
|
|
|
def initialize(compiler, for_source)
|
2018-08-19 12:16:07 +02:00
|
|
|
raise "no compiler" unless compiler
|
2018-04-06 13:21:38 +02:00
|
|
|
@compiler = compiler
|
2018-04-18 18:12:30 +02:00
|
|
|
@source = for_source
|
|
|
|
@source_used = false
|
2018-04-06 19:55:21 +02:00
|
|
|
@names = {}
|
2015-06-01 07:34:17 +02:00
|
|
|
end
|
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
# make the magic: convert incoming names into registers that have the
|
|
|
|
# type set according to the name (using resolve_type)
|
2018-11-21 10:12:39 +01:00
|
|
|
# names are stored, so subsequent calls use the same register
|
2018-08-14 18:39:46 +02:00
|
|
|
def method_missing(name , *args)
|
|
|
|
super if args.length != 0
|
|
|
|
name = name.to_s
|
2018-04-06 19:55:21 +02:00
|
|
|
return @names[name] if @names.has_key?(name)
|
2018-08-14 18:39:46 +02:00
|
|
|
if name == "message"
|
2018-08-14 19:08:58 +02:00
|
|
|
return Risc.message_reg.set_builder(self)
|
2018-08-14 18:39:46 +02:00
|
|
|
end
|
|
|
|
if name.index("label")
|
2018-04-18 18:27:46 +02:00
|
|
|
reg = Risc.label( @source , "#{name}_#{object_id}")
|
|
|
|
@source_used = true
|
2018-04-06 21:54:54 +02:00
|
|
|
else
|
2018-08-15 18:59:17 +02:00
|
|
|
last_char = name[-1]
|
2018-08-14 18:39:46 +02:00
|
|
|
name = name[0 ... -1]
|
2018-08-15 18:59:17 +02:00
|
|
|
if last_char == "!" or last_char == "?"
|
|
|
|
if @names.has_key?(name)
|
|
|
|
return @names[name] if last_char == "?"
|
2018-11-22 08:16:56 +01:00
|
|
|
raise "Name exists (#{@names.keys})before creating it #{name}#{last_char}"
|
2018-08-15 18:59:17 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Must create (with ! or ?) before using #{name}#{last_char}"
|
|
|
|
end
|
2018-07-16 18:17:57 +02:00
|
|
|
type = infer_type(name )
|
2018-08-14 19:08:58 +02:00
|
|
|
reg = @compiler.use_reg( type.object_class.name ).set_builder(self)
|
2018-04-06 21:54:54 +02:00
|
|
|
end
|
2018-04-06 19:55:21 +02:00
|
|
|
@names[name] = reg
|
2018-04-06 13:21:38 +02:00
|
|
|
reg
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
2018-04-06 21:40:58 +02:00
|
|
|
|
2018-11-22 08:16:56 +01:00
|
|
|
# Infer the type from a symbol. In the simplest case the symbol is the class name.
|
2018-11-21 10:12:39 +01:00
|
|
|
# But in building, sometimes variations are needed, so next_message or caller work
|
|
|
|
# too (and both return "Message")
|
|
|
|
# A general "_reg"/"_obj"/"_const" or "_tmp" at the end of the name will be removed
|
2018-07-16 18:17:57 +02:00
|
|
|
# An error is raised if the symbol/object can not be inferred
|
|
|
|
def infer_type( name )
|
2018-08-06 09:11:12 +02:00
|
|
|
as_string = name.to_s
|
|
|
|
parts = as_string.split("_")
|
2018-08-15 17:18:21 +02:00
|
|
|
if( ["reg" , "obj" , "tmp" , "self" , "const", "1" , "2"].include?( parts.last ) )
|
2018-08-06 09:11:12 +02:00
|
|
|
parts.pop
|
|
|
|
as_string = parts.join("_")
|
|
|
|
end
|
|
|
|
as_string = "word" if as_string == "name"
|
|
|
|
as_string = "message" if as_string == "next_message"
|
|
|
|
as_string = "message" if as_string == "caller"
|
|
|
|
as_string = "named_list" if as_string == "arguments"
|
|
|
|
sym = as_string.camelise.to_sym
|
2018-07-16 18:17:57 +02:00
|
|
|
clazz = Parfait.object_space.get_class_by_name(sym)
|
|
|
|
raise "Not implemented/found object #{name}:#{sym}" unless clazz
|
|
|
|
return clazz.instance_type
|
|
|
|
end
|
|
|
|
|
2018-04-07 23:50:51 +02:00
|
|
|
def if_zero( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::IsZero.new(@source , label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
|
|
|
def if_not_zero( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::IsNotZero.new(@source , label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
2018-08-08 19:53:06 +02:00
|
|
|
def if_minus( label )
|
|
|
|
@source_used = true
|
|
|
|
add_code Risc::IsMinus.new(@source , label)
|
|
|
|
end
|
2018-04-07 23:50:51 +02:00
|
|
|
def branch( label )
|
2018-04-18 18:27:46 +02:00
|
|
|
@source_used = true
|
|
|
|
add_code Risc::Branch.new(@source, label)
|
2018-04-07 23:50:51 +02:00
|
|
|
end
|
|
|
|
|
2018-11-21 10:12:39 +01:00
|
|
|
# To avoid many an if, it can be handy to swap variable names.
|
|
|
|
# But since the names in the builder are not variables, we need this method.
|
|
|
|
# As it says, swap the two names around. Names must exist
|
2018-08-08 19:53:06 +02:00
|
|
|
def swap_names(left , right)
|
2018-08-14 18:39:46 +02:00
|
|
|
left , right = left.to_s , right.to_s
|
2018-08-08 19:53:06 +02:00
|
|
|
l = @names[left]
|
|
|
|
r = @names[right]
|
|
|
|
raise "No such name #{left}" unless l
|
|
|
|
raise "No such name #{right}" unless r
|
|
|
|
@names[left] = r
|
|
|
|
@names[right] = l
|
|
|
|
end
|
2018-08-13 17:02:34 +02:00
|
|
|
|
2018-11-21 10:12:39 +01:00
|
|
|
# Reset the names stored by the builder. The names are sort of variables names
|
|
|
|
# that can be used in the build block due to method_missing magic.
|
|
|
|
#
|
|
|
|
# But just as the compiler has reset_regs, the builder has this reset button, to
|
|
|
|
# start fresh. Quite crude for now, and only used in allocate_int
|
|
|
|
#
|
|
|
|
# Compiler regs are reset as well
|
|
|
|
def reset_names
|
|
|
|
@names = {}
|
|
|
|
compiler.reset_regs
|
|
|
|
end
|
|
|
|
|
2018-11-22 08:16:56 +01:00
|
|
|
# Build code using dsl (see __init__ or MessageSetup for examples).
|
|
|
|
# Names (that ruby would resolve to a variable/method) are converted
|
2018-04-07 21:35:40 +02:00
|
|
|
# to registers. << means assignment and [] is supported both on
|
|
|
|
# L and R values (but only one at a time). R values may also be constants.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-07 21:35:40 +02:00
|
|
|
# Basically this allows to create LoadConstant, RegToSlot, SlotToReg and
|
|
|
|
# Transfer instructions with extremely readable code.
|
|
|
|
# example:
|
|
|
|
# space << Parfait.object_space # load constant
|
2018-11-22 08:16:56 +01:00
|
|
|
# message[:receiver] << space #make current message's (r0) receiver the space
|
2018-04-07 21:35:40 +02:00
|
|
|
#
|
2018-08-19 12:16:07 +02:00
|
|
|
# build result is added to compiler directly
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-06 13:54:24 +02:00
|
|
|
def build(&block)
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
2018-04-06 23:35:54 +02:00
|
|
|
|
2018-08-19 12:16:07 +02:00
|
|
|
# add code straight to the compiler
|
2018-04-08 17:51:20 +02:00
|
|
|
def add_code(ins)
|
2018-08-19 12:16:07 +02:00
|
|
|
@compiler.add_code(ins)
|
|
|
|
return ins
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-08 17:51:20 +02:00
|
|
|
|
2018-11-22 08:16:56 +01:00
|
|
|
# for some methods that return an integer it is beneficial to pre allocate the
|
|
|
|
# integer and store it in the return value. That is what this function does.
|
|
|
|
#
|
|
|
|
# Those (builtin) methods, mostly syscall wrappers then go on to do this and that
|
|
|
|
# clobbering registers and so the allocate and even move would be difficult.
|
|
|
|
# We sidestep all that by pre-allocating.
|
|
|
|
def prepare_int_return
|
|
|
|
integer_tmp = allocate_int
|
2018-11-24 21:40:22 +01:00
|
|
|
reset_names
|
2018-11-22 08:16:56 +01:00
|
|
|
build do
|
|
|
|
message[:return_value] << integer_tmp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 10:12:39 +01:00
|
|
|
# allocate int fetches a new int, for sure. It is a builder method, rather than
|
|
|
|
# an inbuilt one, to avoid call overhead for 99.9%
|
|
|
|
# The factories allocate in 1k, so only when that runs out do we really need a call.
|
|
|
|
# Note:
|
|
|
|
# Unfortunately (or so me thinks), this creates code bloat, as the calling is
|
2018-11-22 08:16:56 +01:00
|
|
|
# included in 100%, but only needed in 0.1. Risc-level Blocks or Macros may be needed.
|
|
|
|
# as the calling in (the same) 40-50 instructions for every basic int op.
|
2018-11-21 10:12:39 +01:00
|
|
|
#
|
|
|
|
# The method
|
|
|
|
# - grabs a Integer instance from the Integer factory
|
|
|
|
# - checks for nil and calls (get_more) for more if needed
|
2018-11-22 08:16:56 +01:00
|
|
|
# - returns the RiscValue (Register) where the object is found
|
2018-11-21 10:12:39 +01:00
|
|
|
#
|
|
|
|
# The implicit condition is that the method is called at the entry of a method.
|
|
|
|
# It uses a fair few registers and resets all at the end. The returned object
|
2018-11-22 08:16:56 +01:00
|
|
|
# will always be in r1, because the method resets, and all others will be clobbered.
|
|
|
|
#
|
|
|
|
# Return RegisterValue(:r1) that will be named integer_tmp
|
2018-11-21 10:12:39 +01:00
|
|
|
def allocate_int
|
|
|
|
compiler.reset_regs
|
|
|
|
integer = self.integer!
|
|
|
|
build do
|
|
|
|
factory! << Parfait.object_space.get_factory_for(:Integer)
|
|
|
|
integer << factory[:next_object]
|
|
|
|
object! << Parfait.object_space.nil_object
|
|
|
|
object - integer
|
|
|
|
if_not_zero cont_label
|
|
|
|
integer_2! << factory[:reserve]
|
|
|
|
factory[:next_object] << integer_2
|
|
|
|
call_get_more
|
|
|
|
integer << factory[:next_object]
|
|
|
|
add_code cont_label
|
|
|
|
integer_2 << integer[:next_integer]
|
|
|
|
factory[:next_object] << integer_2
|
|
|
|
end
|
|
|
|
reset_names
|
|
|
|
integer_tmp!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Call_get_more calls the method get_more on the factory (see there).
|
|
|
|
# From the callers perspective the method ensures there is a next_object.
|
|
|
|
#
|
|
|
|
# Calling is three step process
|
|
|
|
# - setting up the next message
|
|
|
|
# - moving receiver (factory) and arguments (none)
|
|
|
|
# - issuing the call
|
|
|
|
# These steps shadow the MomInstructions MessageSetup, ArgumentTransfer and SimpleCall
|
|
|
|
def call_get_more
|
|
|
|
factory = Parfait.object_space.get_factory_for( :Integer )
|
|
|
|
calling = factory.get_type.get_method( :get_more )
|
|
|
|
calling = Parfait.object_space.get_main #until we actually parse Factory
|
|
|
|
Mom::MessageSetup.new( calling ).build_with( self )
|
|
|
|
self.build do
|
|
|
|
factory_reg! << factory
|
|
|
|
message[:receiver] << factory_reg
|
|
|
|
end
|
|
|
|
Mom::SimpleCall.new(calling).to_risc(compiler)
|
|
|
|
end
|
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|