move some compiler helpers to the compiler itself
This commit is contained in:
parent
168c2e862f
commit
5a861d4ed5
@ -3,26 +3,12 @@ module Risc
|
|||||||
module Builtin
|
module Builtin
|
||||||
module CompileHelper
|
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 = {})
|
def compiler_for( type , method_name , arguments , locals = {})
|
||||||
frame = Parfait::NamedList.type_for( locals ) #TODO fix locals passing/ using in builtin
|
frame = Parfait::NamedList.type_for( locals ) #TODO fix locals passing/ using in builtin
|
||||||
args = Parfait::NamedList.type_for( arguments )
|
args = Parfait::NamedList.type_for( arguments )
|
||||||
Risc::MethodCompiler.create_method(type , method_name , args, frame )
|
Risc::MethodCompiler.create_method(type , method_name , args, frame )
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,7 +18,7 @@ module Risc
|
|||||||
def +( context )
|
def +( context )
|
||||||
source = "plus"
|
source = "plus"
|
||||||
compiler = compiler_for(:Integer,:+ ,{other: :Integer})
|
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
|
# 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 + "2" , me , Parfait::Integer.integer_index , me)
|
||||||
compiler.add_slot_to_reg( source + "3", other , Parfait::Integer.integer_index , other)
|
compiler.add_slot_to_reg( source + "3", other , Parfait::Integer.integer_index , other)
|
||||||
@ -80,8 +80,8 @@ module Risc
|
|||||||
# return q + tmp
|
# return q + tmp
|
||||||
compiler.add_code Risc.op( s , :+ , q , tmp )
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
||||||
|
|
||||||
compiler.add_new_int(me , other)
|
# compiler.add_new_int(me , other)
|
||||||
compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
|
# compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
|
||||||
|
|
||||||
compiler.add_reg_to_slot( s , q , :message , :return_value)
|
compiler.add_reg_to_slot( s , q , :message , :return_value)
|
||||||
compiler.add_mom( Mom::ReturnSequence.new)
|
compiler.add_mom( Mom::ReturnSequence.new)
|
||||||
|
@ -7,10 +7,10 @@ module Risc
|
|||||||
# self[index] basically. Index is the first arg
|
# self[index] basically. Index is the first arg
|
||||||
# return is stored in return_value
|
# return is stored in return_value
|
||||||
# (this method returns a new method off course, like all builtin)
|
# (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})
|
compiler = compiler_for(:Object , :get_internal_word ,{at: :Integer})
|
||||||
source = "get_internal_word"
|
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]
|
# reduce me to me[index]
|
||||||
compiler.add_slot_to_reg( source , me , index , me)
|
compiler.add_slot_to_reg( source , me , index , me)
|
||||||
# and put it back into the return value
|
# and put it back into the return value
|
||||||
@ -23,8 +23,8 @@ module Risc
|
|||||||
def set_internal_word context
|
def set_internal_word context
|
||||||
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, :value => :Object} )
|
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, :value => :Object} )
|
||||||
source = "set_internal_word"
|
source = "set_internal_word"
|
||||||
me , index = self_and_int_arg(compiler,source)
|
me , index = compiler.self_and_int_arg(source)
|
||||||
value = load_int_arg_at(compiler,source , 2)
|
value = compiler.load_int_arg_at(source , 2)
|
||||||
|
|
||||||
# do the set
|
# do the set
|
||||||
compiler.add_reg_to_slot( source , value , me , index)
|
compiler.add_reg_to_slot( source , value , me , index)
|
||||||
|
@ -19,7 +19,7 @@ module Risc
|
|||||||
def get_internal_byte context
|
def get_internal_byte context
|
||||||
compiler = compiler_for(:Word , :get_internal_byte , {at: :Integer})
|
compiler = compiler_for(:Word , :get_internal_byte , {at: :Integer})
|
||||||
source = "get_internal_byte"
|
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]
|
# reduce me to me[index]
|
||||||
compiler.add_byte_to_reg( source , me , index , me)
|
compiler.add_byte_to_reg( source , me , index , me)
|
||||||
# and put it back into the return value
|
# and put it back into the return value
|
||||||
@ -36,8 +36,8 @@ module Risc
|
|||||||
len = args.instance_length
|
len = args.instance_length
|
||||||
raise "Compiler arg number mismatch, method=#{args} " if len != 3
|
raise "Compiler arg number mismatch, method=#{args} " if len != 3
|
||||||
source = "set_internal_byte"
|
source = "set_internal_byte"
|
||||||
me , index = self_and_int_arg(compiler,source)
|
me , index = compiler.self_and_int_arg(source)
|
||||||
value = load_int_arg_at(compiler , source , 2 )
|
value = compiler.load_int_arg_at(source , 2 )
|
||||||
# do the set
|
# do the set
|
||||||
compiler.add_reg_to_byte( source , value , me , index)
|
compiler.add_reg_to_byte( source , value , me , index)
|
||||||
return compiler.method
|
return compiler.method
|
||||||
|
@ -140,7 +140,7 @@ module Risc
|
|||||||
int = use_reg(:Integer)
|
int = use_reg(:Integer)
|
||||||
space_i = Risc.resolve_to_index(:Space, :next_integer)
|
space_i = Risc.resolve_to_index(:Space, :next_integer)
|
||||||
add_load_constant( source + "space" , Parfait.object_space , space )
|
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_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 link" , int , space , space_i )
|
||||||
add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index)
|
add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index)
|
||||||
@ -148,5 +148,23 @@ module Risc
|
|||||||
def add_constant(const)
|
def add_constant(const)
|
||||||
Risc.machine.add_constant(const)
|
Risc.machine.add_constant(const)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user