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-10-22 17:16:29 +02:00
|
|
|
function = MethodSource.create_method(:Kernel,:Integer,:__init__ , [])
|
2015-10-14 15:16:03 +02:00
|
|
|
function.source.set_return_type :Integer
|
2015-06-29 20:03:58 +02:00
|
|
|
# no method enter or return (automatically added), remove
|
2015-10-23 20:27:36 +02:00
|
|
|
new_start = Label.new(function , "__init__" )
|
|
|
|
function.source.instructions = new_start
|
|
|
|
function.source.current = new_start
|
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
#Set up the Space as self upon init
|
|
|
|
space = Parfait::Space.object_space
|
2015-10-18 16:20:19 +02:00
|
|
|
space_reg = Register.tmp_reg(:Space)
|
|
|
|
function.source.add_code LoadConstant.new(function, 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)
|
|
|
|
function.source.add_code Register.get_slot( function , space_reg , message_ind , :new_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-10-18 16:20:19 +02:00
|
|
|
function.source.add_code Register.set_slot( function, space_reg , :new_message , :receiver)
|
2015-06-29 20:03:58 +02:00
|
|
|
# now we are set up to issue a call to the main
|
2015-10-22 17:16:29 +02:00
|
|
|
Register.issue_call( function , Register.machine.space.get_main)
|
2015-06-29 20:03:58 +02:00
|
|
|
emit_syscall( function , :exit )
|
|
|
|
return function
|
|
|
|
end
|
|
|
|
def exit context
|
2015-10-22 17:16:29 +02:00
|
|
|
function = MethodSource.create_method(:Kernel,:Integer,:exit , [])
|
2015-10-14 15:16:03 +02:00
|
|
|
function.source.set_return_type :Integer
|
2015-06-29 20:03:58 +02:00
|
|
|
return function
|
2015-10-22 17:16:29 +02:00
|
|
|
ret = RegisterMachine.instance.exit(function)
|
2015-06-29 20:03:58 +02:00
|
|
|
function.set_return ret
|
|
|
|
function
|
|
|
|
end
|
2015-06-22 21:48:42 +02:00
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
def emit_syscall function , name
|
|
|
|
save_message( function )
|
2015-07-27 11:13:39 +02:00
|
|
|
function.source.add_code Syscall.new(function, name )
|
2015-06-29 20:03:58 +02:00
|
|
|
restore_message(function)
|
|
|
|
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-06-29 20:03:58 +02:00
|
|
|
def save_message(function)
|
2015-10-15 08:32:47 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
|
|
|
function.source.add_code RegisterTransfer.new(function, Register.message_reg , r8 )
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
2015-06-30 17:38:56 +02:00
|
|
|
|
2015-06-29 20:03:58 +02:00
|
|
|
def restore_message(function)
|
2015-10-15 08:32:47 +02:00
|
|
|
r8 = RegisterValue.new( :r8 , :Message)
|
2015-10-10 18:14:27 +02:00
|
|
|
return_tmp = Register.tmp_reg function.source.return_type
|
2015-07-02 12:48:32 +02:00
|
|
|
# get the sys return out of the way
|
2015-07-27 11:13:39 +02:00
|
|
|
function.source.add_code RegisterTransfer.new(function, Register.message_reg , return_tmp )
|
2015-07-02 12:48:32 +02:00
|
|
|
# load the stored message into the base RegisterMachine
|
2015-10-15 08:32:47 +02:00
|
|
|
function.source.add_code RegisterTransfer.new(function, r8 , Register.message_reg )
|
2015-06-30 17:38:56 +02:00
|
|
|
# save the return value into the message
|
2015-07-27 11:13:39 +02:00
|
|
|
function.source.add_code Register.set_slot( function, return_tmp , :message , :return_value )
|
2015-06-29 20:03:58 +02:00
|
|
|
# and "unroll" self and frame
|
|
|
|
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
|