2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-06-29 20:03:58 +02:00
|
|
|
module Builtin
|
|
|
|
class Object
|
|
|
|
module ClassMethods
|
2016-12-15 18:20:54 +01:00
|
|
|
include CompileHelper
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2015-11-07 16:36:28 +01:00
|
|
|
# self[index] basically. Index is the first arg
|
|
|
|
# return is stored in return_value
|
2015-11-08 16:10:36 +01:00
|
|
|
# (this method returns a new method off course, like all builtin)
|
2018-04-01 13:09:30 +02:00
|
|
|
def get_internal_word( context )
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Object , :get_internal_word ,{at: :Integer})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2015-11-18 14:36:43 +01:00
|
|
|
source = "get_internal_word"
|
2018-04-08 17:52:17 +02:00
|
|
|
me , index = builder.self_and_int_arg(source)
|
2015-11-07 16:36:28 +01:00
|
|
|
# reduce me to me[index]
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_slot_to_reg( source , me , index , me)
|
2015-11-07 16:36:28 +01:00
|
|
|
# and put it back into the return value
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_reg_to_slot( source , me , :message , :return_value)
|
2018-04-01 14:17:16 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2015-11-07 16:36:28 +01:00
|
|
|
end
|
|
|
|
|
2016-12-06 10:38:09 +01:00
|
|
|
# self[index] = val basically. Index is the first arg , value the second
|
2015-11-08 16:10:36 +01:00
|
|
|
# no return
|
2018-04-01 14:17:16 +02:00
|
|
|
def set_internal_word( context )
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, :value => :Object} )
|
2015-11-18 14:36:43 +01:00
|
|
|
source = "set_internal_word"
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-08 17:52:17 +02:00
|
|
|
me , index = builder.self_and_int_arg(source)
|
2018-05-14 14:17:04 +02:00
|
|
|
value = builder.load_int_arg_at(source , 1)
|
2015-11-08 16:10:36 +01:00
|
|
|
# do the set
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_reg_to_slot( source , value , me , index)
|
2018-04-01 14:17:16 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2015-11-08 16:10:36 +01:00
|
|
|
end
|
|
|
|
|
2018-04-02 15:49:30 +02:00
|
|
|
# every object needs a method missing.
|
|
|
|
# Even if it's just this one, sys_exit (later raise)
|
|
|
|
def _method_missing( context )
|
|
|
|
compiler = compiler_for(:Object,:method_missing ,{})
|
2018-06-29 12:27:57 +02:00
|
|
|
emit_syscall( compiler.compiler_builder(compiler.method) , :exit )
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-04-02 15:49:30 +02:00
|
|
|
end
|
2018-06-29 12:03:19 +02:00
|
|
|
|
2018-04-02 16:06:31 +02:00
|
|
|
# this is the really really first place the machine starts (apart from the jump here)
|
|
|
|
# it isn't really a function, ie it is jumped to (not called), exits and may not return
|
|
|
|
# so it is responsible for initial setup
|
|
|
|
def __init__ context
|
2018-06-30 22:26:28 +02:00
|
|
|
compiler = MethodCompiler.compiler_for_class(:Object,:__init__ ,
|
2018-04-02 16:06:31 +02:00
|
|
|
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-07 21:35:40 +02:00
|
|
|
builder.build do
|
2018-04-06 23:14:02 +02:00
|
|
|
space << Parfait.object_space
|
2018-07-02 14:49:51 +02:00
|
|
|
message << space[:next_message]
|
2018-04-06 23:14:02 +02:00
|
|
|
next_message << message[:next_message]
|
2018-07-02 14:49:51 +02:00
|
|
|
space[:next_message] << next_message
|
2018-04-06 23:14:02 +02:00
|
|
|
end
|
|
|
|
|
2018-04-08 17:52:17 +02:00
|
|
|
Mom::MessageSetup.new(Parfait.object_space.get_main).build_with( builder )
|
2018-04-06 23:14:02 +02:00
|
|
|
|
2018-04-07 21:35:40 +02:00
|
|
|
builder.build do
|
2018-04-06 23:14:02 +02:00
|
|
|
message << message[:next_message]
|
|
|
|
message[:receiver] << space
|
|
|
|
end
|
|
|
|
|
2018-07-06 19:01:17 +02:00
|
|
|
exit_label = Risc.label(compiler.method , "#{compiler.method.self_type.object_class.name}.#{compiler.method.name}" )
|
2018-04-02 16:06:31 +02:00
|
|
|
ret_tmp = compiler.use_reg(:Label)
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.build do
|
|
|
|
add_load_constant("__init__ load return", exit_label , ret_tmp)
|
|
|
|
add_reg_to_slot("__init__ store return", ret_tmp , :message , :return_address)
|
2018-05-19 11:21:20 +02:00
|
|
|
add_function_call( "__init__ issue call" , Parfait.object_space.get_main)
|
2018-04-08 17:52:17 +02:00
|
|
|
add_code exit_label
|
|
|
|
end
|
2018-04-08 21:29:08 +02:00
|
|
|
compiler.reset_regs
|
2018-06-19 17:55:47 +02:00
|
|
|
exit_sequence(builder)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-04-02 16:06:31 +02:00
|
|
|
end
|
|
|
|
|
2018-06-19 17:55:47 +02:00
|
|
|
# a sort of inline version of exit method.
|
|
|
|
# Used by exit and __init__ (so it doesn't have to call it)
|
|
|
|
def exit_sequence(builder)
|
2018-06-19 18:52:06 +02:00
|
|
|
save_message( builder )
|
|
|
|
builder.add_slot_to_reg "get return" , :message , :return_value , :message
|
|
|
|
builder.reduce_int( "reduce return" , :message)
|
|
|
|
builder.add_code Syscall.new("emit_syscall(exit)", :exit )
|
2018-06-19 17:55:47 +02:00
|
|
|
end
|
|
|
|
|
2018-04-02 16:06:31 +02:00
|
|
|
def exit( context )
|
|
|
|
compiler = compiler_for(:Object,:exit ,{})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-06-19 17:55:47 +02:00
|
|
|
exit_sequence(builder)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-04-02 16:06:31 +02:00
|
|
|
end
|
|
|
|
|
2018-04-08 17:52:17 +02:00
|
|
|
def emit_syscall( builder , name )
|
|
|
|
save_message( builder )
|
|
|
|
builder.add_code Syscall.new("emit_syscall(#{name})", name )
|
|
|
|
restore_message(builder)
|
2018-04-02 16:06:31 +02:00
|
|
|
return unless (@clazz and @method)
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.label( "#{@clazz.name}.#{@message.name}" , "return_syscall" )
|
2018-04-02 16:06:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# save the current message, as the syscall destroys all context
|
|
|
|
#
|
|
|
|
# This relies on linux to save and restore all registers
|
|
|
|
#
|
2018-04-08 17:52:17 +02:00
|
|
|
def save_message(builder)
|
2018-06-29 10:39:07 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.transfer("save_message", Risc.message_reg , r8 )
|
2018-04-02 16:06:31 +02:00
|
|
|
end
|
|
|
|
|
2018-04-08 17:52:17 +02:00
|
|
|
def restore_message(builder)
|
2018-06-29 10:39:07 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
2018-04-08 17:52:17 +02:00
|
|
|
return_tmp = builder.compiler.use_reg :fixnum
|
2018-04-02 16:06:31 +02:00
|
|
|
source = "_restore_message"
|
|
|
|
# get the sys return out of the way
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.transfer(source, Risc.message_reg , return_tmp )
|
2018-04-02 16:06:31 +02:00
|
|
|
# load the stored message into the base register
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.transfer(source, r8 , Risc.message_reg )
|
|
|
|
int = builder.compiler.use_reg(:Integer)
|
|
|
|
builder.add_new_int(source , return_tmp , int )
|
2018-04-02 16:06:31 +02:00
|
|
|
# save the return value into the message
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.reg_to_slot( source , int , :message , :return_value )
|
2018-04-02 16:06:31 +02:00
|
|
|
end
|
|
|
|
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
2015-06-29 20:03:58 +02:00
|
|
|
extend ClassMethods
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|