2015-06-29 20:03:58 +02:00
|
|
|
module Register
|
|
|
|
module Builtin
|
|
|
|
module Kernel
|
|
|
|
module ClassMethods
|
|
|
|
# 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
|
2015-11-01 18:13:40 +01:00
|
|
|
source = "__init__"
|
2016-12-08 14:25:20 +01:00
|
|
|
compiler = Typed::Compiler.new.create_method(:Kernel,:__init__ )
|
2015-06-29 20:03:58 +02:00
|
|
|
# no method enter or return (automatically added), remove
|
2015-10-29 21:31:28 +01:00
|
|
|
new_start = Label.new(source , source )
|
2015-10-28 20:37:42 +01:00
|
|
|
compiler.method.instructions = new_start
|
|
|
|
compiler.set_current new_start
|
2015-10-28 11:19:10 +01:00
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
#Set up the Space as self upon init
|
|
|
|
space = Parfait::Space.object_space
|
2015-11-07 20:58:19 +01:00
|
|
|
space_reg = compiler.use_reg(:Space)
|
2015-10-29 21:31:28 +01:00
|
|
|
compiler.add_code LoadConstant.new(source, space , space_reg)
|
2015-07-01 18:27:18 +02:00
|
|
|
message_ind = Register.resolve_index( :space , :first_message )
|
2015-10-18 16:20:19 +02:00
|
|
|
# Load the message to new message register (r1)
|
2015-11-07 20:58:19 +01:00
|
|
|
compiler.add_code Register.get_slot( source , space_reg , message_ind , :message)
|
2015-06-29 20:03:58 +02:00
|
|
|
# And store the space as the new self (so the call can move it back as self)
|
2015-11-07 20:58:19 +01:00
|
|
|
compiler.add_code Register.set_slot( source, space_reg , :message , :receiver)
|
2016-12-14 12:23:46 +01:00
|
|
|
exit_label = Label.new("_exit_label" , "#{compiler.type.object_class.name}.#{compiler.method.name}" )
|
2015-11-07 20:58:19 +01:00
|
|
|
ret_tmp = compiler.use_reg(:Label)
|
|
|
|
compiler.add_code Register::LoadConstant.new(source, exit_label , ret_tmp)
|
|
|
|
compiler.add_code Register.set_slot(source, ret_tmp , :message , :return_address)
|
|
|
|
# do the register call
|
|
|
|
compiler.add_code FunctionCall.new( source , Register.machine.space.get_main )
|
|
|
|
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
|
2016-12-08 14:25:20 +01:00
|
|
|
compiler = Typed::Compiler.new.create_method(:Kernel,:exit ).init_method
|
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)
|
|
|
|
compiler.add_code Label.new( "#{@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)
|
2015-10-15 08:32:47 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
2015-10-28 20:37:42 +01:00
|
|
|
compiler.add_code RegisterTransfer.new("save_message", Register.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)
|
2015-10-15 08:32:47 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
2015-10-28 11:19:10 +01:00
|
|
|
return_tmp = Register.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
|
2015-11-01 18:13:40 +01:00
|
|
|
compiler.add_code RegisterTransfer.new(source, Register.message_reg , return_tmp )
|
2015-07-02 12:48:32 +02:00
|
|
|
# load the stored message into the base RegisterMachine
|
2015-11-01 18:13:40 +01:00
|
|
|
compiler.add_code RegisterTransfer.new(source, r8 , Register.message_reg )
|
2015-06-30 17:38:56 +02:00
|
|
|
# save the return value into the message
|
2015-11-01 18:13:40 +01:00
|
|
|
compiler.add_code Register.set_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
|