2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-06-29 20:03:58 +02:00
|
|
|
module Builtin
|
|
|
|
module Kernel
|
|
|
|
module ClassMethods
|
2018-03-18 17:38:35 +01:00
|
|
|
include CompileHelper
|
2015-06-29 20:03:58 +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-03-18 17:38:35 +01:00
|
|
|
compiler = Risc::MethodCompiler.create_method(:Kernel,:__init__ ,
|
|
|
|
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
|
2017-01-19 08:02:29 +01:00
|
|
|
new_start = Risc.label("__init__ start" , "__init__" )
|
2016-12-29 17:45:32 +01:00
|
|
|
compiler.method.set_instructions( new_start)
|
2018-03-23 17:55:23 +01:00
|
|
|
compiler.set_current( new_start ) #thus abandoning standard method setup
|
2015-10-28 11:19:10 +01:00
|
|
|
|
2016-12-30 13:10:49 +01:00
|
|
|
space = Parfait.object_space
|
2016-12-27 19:39:39 +01:00
|
|
|
space_reg = compiler.use_reg(:Space) #Set up the Space as self upon init
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant("__init__ load Space", space , space_reg)
|
2017-01-19 08:02:29 +01:00
|
|
|
message_ind = Risc.resolve_to_index( :space , :first_message )
|
2018-03-23 17:55:23 +01:00
|
|
|
#load the first_message (instance of space)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_slot_to_reg( "__init__ load 1st message" , space_reg , message_ind , :message)
|
2018-03-24 14:59:54 +01:00
|
|
|
compiler.add_mom( Mom::MessageSetup.new(compiler.method))
|
2018-03-23 17:55:23 +01:00
|
|
|
# but use it's next message, so main can return normally
|
|
|
|
compiler.add_slot_to_reg( "__init__ load 2nd message" , :message , :next_message , :message)
|
2018-03-24 14:59:54 +01:00
|
|
|
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_reg_to_slot( "__init__ store Space in message", space_reg , :message , :receiver)
|
2017-01-04 20:34:51 +01:00
|
|
|
#fixme: should add arg type here, as done in call_site (which this sort of is)
|
2017-01-19 08:02:29 +01:00
|
|
|
exit_label = Risc.label("_exit_label for __init__" , "#{compiler.type.object_class.name}.#{compiler.method.name}" )
|
2015-11-07 20:58:19 +01:00
|
|
|
ret_tmp = compiler.use_reg(:Label)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant("__init__ load return", exit_label , ret_tmp)
|
|
|
|
compiler.add_reg_to_slot("__init__ store return", ret_tmp , :message , :return_address)
|
2018-03-23 17:55:23 +01:00
|
|
|
compiler.add_function_call( "__init__ issue call" , Parfait.object_space.get_main , ret_tmp)
|
2015-11-07 20:58:19 +01:00
|
|
|
compiler.add_code exit_label
|
2015-10-28 20:37:42 +01:00
|
|
|
emit_syscall( compiler , :exit )
|
|
|
|
return compiler.method
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-11-01 18:13:40 +01:00
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
def exit context
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Kernel,:exit ,{})
|
2015-11-01 18:13:40 +01:00
|
|
|
emit_syscall( compiler , :exit )
|
2015-10-28 20:37:42 +01:00
|
|
|
return compiler.method
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-06-22 21:48:42 +02:00
|
|
|
|
2015-10-28 20:37:42 +01:00
|
|
|
def emit_syscall compiler , name
|
|
|
|
save_message( compiler )
|
2015-10-29 21:31:28 +01:00
|
|
|
compiler.add_code Syscall.new("emit_syscall(#{name})", name )
|
2015-10-28 20:37:42 +01:00
|
|
|
restore_message(compiler)
|
2015-11-01 18:13:40 +01:00
|
|
|
return unless (@clazz and @method)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_label( "#{@clazz.name}.#{@message.name}" , "return_syscall" )
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-07-02 12:48:32 +02:00
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
# save the current message, as the syscall destroys all context
|
|
|
|
#
|
2015-07-02 12:48:32 +02:00
|
|
|
# This relies on linux to save and restore all registers
|
2015-07-27 11:13:39 +02:00
|
|
|
#
|
2015-10-28 20:37:42 +01:00
|
|
|
def save_message(compiler)
|
2017-01-19 08:02:29 +01:00
|
|
|
r8 = RiscValue.new( :r8 , :Message)
|
|
|
|
compiler.add_transfer("save_message", Risc.message_reg , r8 )
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-06-30 17:38:56 +02:00
|
|
|
|
2015-10-28 20:37:42 +01:00
|
|
|
def restore_message(compiler)
|
2017-01-19 08:02:29 +01:00
|
|
|
r8 = RiscValue.new( :r8 , :Message)
|
|
|
|
return_tmp = Risc.tmp_reg :Integer
|
2015-11-01 18:13:40 +01:00
|
|
|
source = "_restore_message"
|
2015-07-02 12:48:32 +02:00
|
|
|
# get the sys return out of the way
|
2017-01-19 08:02:29 +01:00
|
|
|
compiler.add_transfer(source, Risc.message_reg , return_tmp )
|
|
|
|
# load the stored message into the base RiscMachine
|
|
|
|
compiler.add_transfer(source, r8 , Risc.message_reg )
|
2015-06-30 17:38:56 +02:00
|
|
|
# save the return value into the message
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_reg_to_slot( source , return_tmp , :message , :return_value )
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-06-22 21:48:42 +02:00
|
|
|
end
|
2015-06-29 20:03:58 +02:00
|
|
|
extend ClassMethods
|
2014-09-10 20:35:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|