From 5a861d4ed5725b8f81eb98d84811630fbffa5aa3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 1 Apr 2018 14:09:30 +0300 Subject: [PATCH] move some compiler helpers to the compiler itself --- lib/risc/builtin/compile_helper.rb | 14 -------------- lib/risc/builtin/integer.rb | 6 +++--- lib/risc/builtin/object.rb | 8 ++++---- lib/risc/builtin/word.rb | 6 +++--- lib/risc/method_compiler.rb | 20 +++++++++++++++++++- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/risc/builtin/compile_helper.rb b/lib/risc/builtin/compile_helper.rb index 7e218e2d..e970e3e4 100644 --- a/lib/risc/builtin/compile_helper.rb +++ b/lib/risc/builtin/compile_helper.rb @@ -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 diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index ea87c804..b7c5fd55 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -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) diff --git a/lib/risc/builtin/object.rb b/lib/risc/builtin/object.rb index 9d0c206f..0936b4bf 100644 --- a/lib/risc/builtin/object.rb +++ b/lib/risc/builtin/object.rb @@ -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) diff --git a/lib/risc/builtin/word.rb b/lib/risc/builtin/word.rb index 6dc140f9..02817786 100644 --- a/lib/risc/builtin/word.rb +++ b/lib/risc/builtin/word.rb @@ -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 diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 4da45ecb..b76e0e6e 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -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