move some compiler helpers to the compiler itself

This commit is contained in:
Torsten Ruger 2018-04-01 14:09:30 +03:00
parent 168c2e862f
commit 5a861d4ed5
5 changed files with 29 additions and 25 deletions

View File

@ -3,26 +3,12 @@ module Risc
module Builtin
module CompileHelper
def self_and_int_arg(compiler , source)
me = compiler.add_known( :receiver )
int_arg = load_int_arg_at(compiler , source , 1 )
return me , int_arg
end
def compiler_for( type , method_name , arguments , locals = {})
frame = Parfait::NamedList.type_for( locals ) #TODO fix locals passing/ using in builtin
args = Parfait::NamedList.type_for( arguments )
Risc::MethodCompiler.create_method(type , method_name , args, frame )
end
# Load the value
def load_int_arg_at(compiler, source , at)
int_arg = compiler.use_reg :Integer
compiler.add_slot_to_reg(source , :message , :arguments , int_arg )
compiler.add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type
return int_arg
end
end
end
end

View File

@ -18,7 +18,7 @@ module Risc
def +( context )
source = "plus"
compiler = compiler_for(:Integer,:+ ,{other: :Integer})
me , other = self_and_int_arg(compiler,source + "1")
me , other = compiler.self_and_int_arg(source + "1")
# reduce me and other to integers
compiler.add_slot_to_reg( source + "2" , me , Parfait::Integer.integer_index , me)
compiler.add_slot_to_reg( source + "3", other , Parfait::Integer.integer_index , other)
@ -80,8 +80,8 @@ module Risc
# return q + tmp
compiler.add_code Risc.op( s , :+ , q , tmp )
compiler.add_new_int(me , other)
compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
# compiler.add_new_int(me , other)
# compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
compiler.add_reg_to_slot( s , q , :message , :return_value)
compiler.add_mom( Mom::ReturnSequence.new)

View File

@ -7,10 +7,10 @@ module Risc
# self[index] basically. Index is the first arg
# return is stored in return_value
# (this method returns a new method off course, like all builtin)
def get_internal_word context
def get_internal_word( context )
compiler = compiler_for(:Object , :get_internal_word ,{at: :Integer})
source = "get_internal_word"
me , index = self_and_int_arg(compiler,source)
me , index = compiler.self_and_int_arg(source)
# reduce me to me[index]
compiler.add_slot_to_reg( source , me , index , me)
# and put it back into the return value
@ -23,8 +23,8 @@ module Risc
def set_internal_word context
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, :value => :Object} )
source = "set_internal_word"
me , index = self_and_int_arg(compiler,source)
value = load_int_arg_at(compiler,source , 2)
me , index = compiler.self_and_int_arg(source)
value = compiler.load_int_arg_at(source , 2)
# do the set
compiler.add_reg_to_slot( source , value , me , index)

View File

@ -19,7 +19,7 @@ module Risc
def get_internal_byte context
compiler = compiler_for(:Word , :get_internal_byte , {at: :Integer})
source = "get_internal_byte"
me , index = self_and_int_arg(compiler,source)
me , index = compiler.self_and_int_arg(source)
# reduce me to me[index]
compiler.add_byte_to_reg( source , me , index , me)
# and put it back into the return value
@ -36,8 +36,8 @@ module Risc
len = args.instance_length
raise "Compiler arg number mismatch, method=#{args} " if len != 3
source = "set_internal_byte"
me , index = self_and_int_arg(compiler,source)
value = load_int_arg_at(compiler , source , 2 )
me , index = compiler.self_and_int_arg(source)
value = compiler.load_int_arg_at(source , 2 )
# do the set
compiler.add_reg_to_byte( source , value , me , index)
return compiler.method

View File

@ -140,7 +140,7 @@ module Risc
int = use_reg(:Integer)
space_i = Risc.resolve_to_index(:Space, :next_integer)
add_load_constant( source + "space" , Parfait.object_space , space )
add_slot_to_reg( source + "next_i1" , space , space_i , register)
add_slot_to_reg( source + "next_i1" , space , space_i , to)
add_slot_to_reg( source + "next_i2" , to , Risc.resolve_to_index(:Integer, :next_integer) , int)
add_reg_to_slot( source + "store link" , int , space , space_i )
add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index)
@ -148,5 +148,23 @@ module Risc
def add_constant(const)
Risc.machine.add_constant(const)
end
# load receiver and the first argument (int)
# return both registers
def self_and_int_arg( source )
me = add_known( :receiver )
int_arg = load_int_arg_at(source , 1 )
return me , int_arg
end
# Load the first argument, assumed to be integer
def load_int_arg_at( source , at)
int_arg = use_reg :Integer
add_slot_to_reg(source , :message , :arguments , int_arg )
add_slot_to_reg(source , int_arg , at + 1, int_arg ) #1 for type
return int_arg
end
end
end