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-06-29 12:27:57 +02:00
|
|
|
# There are two subclasses of Builder, depending of what one wants to do with the
|
|
|
|
# generated code.
|
|
|
|
#
|
|
|
|
# CompilerBuilder: The code is added to the method_compiler.
|
2018-04-08 17:51:20 +02:00
|
|
|
# This is used to generate the builtin methods.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
|
|
|
# CodeBuilder: The code can be stored up and returned.
|
|
|
|
# This is used in Mom::to_risc methods
|
|
|
|
#
|
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-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)
|
|
|
|
# anmes 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-14 18:39:46 +02:00
|
|
|
raise "Must create (with !) before using #{name}" unless name[-1] == "!"
|
|
|
|
name = name[0 ... -1]
|
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-07-16 18:17:57 +02:00
|
|
|
# infer the type from a symbol. In the simplest case the sybbol is the class name
|
|
|
|
# But in building sometimes variations are needed, so next_message or caller work
|
|
|
|
# too (and return Message)
|
2018-08-13 17:02:34 +02:00
|
|
|
# A general "_reg"/"_obj" 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-08-08 19:53:06 +02:00
|
|
|
# to avoid many an if, it can be candy 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
|
|
|
|
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-04-07 21:35:40 +02:00
|
|
|
# build code using dsl (see __init__ or MessageSetup for examples)
|
|
|
|
# names (that ruby would resolve to a variable/method) are converted
|
|
|
|
# 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
|
|
|
|
# message[:receiver] << space #make current message (r0) receiver the space
|
|
|
|
#
|
2018-04-08 17:51:20 +02:00
|
|
|
# build result is available as built, but also gets added to compiler, if the
|
|
|
|
# builder is created with default args
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
2018-04-06 13:54:24 +02:00
|
|
|
def build(&block)
|
|
|
|
instance_eval(&block)
|
2018-04-08 17:51:20 +02:00
|
|
|
@built
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-06 23:35:54 +02:00
|
|
|
|
2018-04-08 17:51:20 +02:00
|
|
|
def add_code(ins)
|
2018-06-29 12:27:57 +02:00
|
|
|
raise "Must be implemented in subclass #{self}"
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-08 17:51:20 +02:00
|
|
|
|
|
|
|
# move a machine int from register "from" to a Parfait::Integer in register "to"
|
|
|
|
# have to grab an integer from space and stick it in the "to" register first.
|
|
|
|
def add_new_int( source , from, to )
|
2018-08-14 19:08:58 +02:00
|
|
|
to.set_builder( self ) # esecially div10 comes in without having used builder
|
|
|
|
from.set_builder( self ) # not named regs, different regs ==> silent errors
|
2018-08-13 17:02:34 +02:00
|
|
|
build do
|
2018-08-14 18:39:46 +02:00
|
|
|
space! << Parfait.object_space
|
2018-08-13 17:02:34 +02:00
|
|
|
to << space[:next_integer]
|
2018-08-14 18:39:46 +02:00
|
|
|
integer_tmp! << to[:next_integer]
|
2018-08-13 17:02:34 +02:00
|
|
|
space[:next_integer] << integer_tmp
|
|
|
|
to[Parfait::Integer.integer_index] << from
|
|
|
|
end
|
2018-04-08 17:51:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# for computationally building code (ie writing assembler) these short cuts
|
|
|
|
# help to instantiate risc instructions and add them immediately
|
2018-08-14 19:24:48 +02:00
|
|
|
[ :slot_to_reg , :load_constant, :load_data,
|
|
|
|
:transfer ].each do |method|
|
2018-04-08 17:51:20 +02:00
|
|
|
define_method("add_#{method}".to_sym) do |*args|
|
2018-04-18 18:27:46 +02:00
|
|
|
if not @source_used
|
|
|
|
args[0] = @source
|
|
|
|
@source_used = true
|
|
|
|
end
|
2018-04-08 17:51:20 +02:00
|
|
|
add_code Risc.send( method , *args )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|
|
|
|
|
2015-06-29 09:55:22 +02:00
|
|
|
|
2018-06-29 12:27:57 +02:00
|
|
|
class CodeBuilder < Builder
|
|
|
|
|
|
|
|
attr_reader :built
|
|
|
|
def initialize(compiler, for_source)
|
|
|
|
super
|
|
|
|
@built = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(&block)
|
|
|
|
super
|
|
|
|
@built
|
|
|
|
end
|
|
|
|
|
|
|
|
# CodeBuilder stores the code.
|
|
|
|
# The code can be access through the @built instance, and is returned
|
|
|
|
# from build method
|
|
|
|
def add_code(ins)
|
|
|
|
if(@built)
|
|
|
|
@built << ins
|
|
|
|
else
|
|
|
|
@built = ins
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-30 22:26:28 +02:00
|
|
|
# A CompilerBuilder adds the generated code to the MethodCompiler.
|
2018-06-29 12:27:57 +02:00
|
|
|
#
|
|
|
|
class CompilerBuilder < Builder
|
|
|
|
# add code straight to the compiler
|
|
|
|
def add_code(ins)
|
|
|
|
return @compiler.add_code(ins)
|
|
|
|
end
|
|
|
|
end
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2014-06-14 09:59:25 +02:00
|
|
|
end
|