2014-09-10 20:35:52 +02:00
|
|
|
module Builtin
|
|
|
|
module Kernel
|
|
|
|
module ClassMethods
|
2015-06-25 15:31:09 +02:00
|
|
|
# this is the really really first place the machine starts (apart from the jump here)
|
2014-09-10 20:35:52 +02:00
|
|
|
# it isn't really a function, ie it is jumped to (not called), exits and may not return
|
2015-06-25 15:31:09 +02:00
|
|
|
# so it is responsible for initial setup
|
2014-09-10 20:35:52 +02:00
|
|
|
def __init__ context
|
2015-05-31 17:34:18 +02:00
|
|
|
function = Virtual::CompiledMethodInfo.create_method(:Kernel,:__init__ , [])
|
2015-05-24 12:31:33 +02:00
|
|
|
function.info.return_type = Virtual::Integer
|
2015-06-25 15:31:09 +02:00
|
|
|
# no method enter or return (automatically added), remove
|
|
|
|
function.info.blocks.first.codes.pop # no Method enter
|
|
|
|
function.info.blocks.last.codes.pop # no Method return
|
|
|
|
#Set up the Space as self upon init
|
|
|
|
space = Parfait::Space.object_space
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::LoadConstant.new( space , Register::RegisterReference.self_reg)
|
2015-06-25 15:31:09 +02:00
|
|
|
message_ind = space.get_layout().index_of( :next_message )
|
|
|
|
# Load the message to message register (0)
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::GetSlot.new( Register::RegisterReference.self_reg , message_ind , Register::RegisterReference.new_message_reg)
|
|
|
|
# And store the space as the new self (so the call can move it back as self)
|
|
|
|
function.info.add_code Register::SetSlot.new( Register::RegisterReference.self_reg , Register::RegisterReference.new_message_reg , Virtual::SELF_INDEX)
|
2015-06-25 15:31:09 +02:00
|
|
|
# now we are set up to issue a call to the main
|
|
|
|
function.info.add_code Virtual::MethodCall.new(Virtual.machine.space.get_main)
|
|
|
|
emit_syscall( function , :exit )
|
2014-09-10 20:35:52 +02:00
|
|
|
return function
|
|
|
|
end
|
2015-05-13 11:15:14 +02:00
|
|
|
def putstring context
|
2015-05-31 17:34:18 +02:00
|
|
|
function = Virtual::CompiledMethodInfo.create_method(:Kernel , :putstring , [] )
|
2015-06-22 21:48:42 +02:00
|
|
|
emit_syscall( function , :putstring )
|
2014-09-10 20:35:52 +02:00
|
|
|
function
|
|
|
|
end
|
|
|
|
def exit context
|
2015-05-31 17:34:18 +02:00
|
|
|
function = Virtual::CompiledMethodInfo.create_method(:Kernel,:exit , [])
|
2015-05-24 12:31:33 +02:00
|
|
|
function.info.return_type = Virtual::Integer
|
2014-09-10 20:35:52 +02:00
|
|
|
return function
|
|
|
|
ret = Virtual::RegisterMachine.instance.exit(function)
|
|
|
|
function.set_return ret
|
|
|
|
function
|
|
|
|
end
|
2014-09-24 17:14:23 +02:00
|
|
|
def __send context
|
2015-05-31 17:34:18 +02:00
|
|
|
function = Virtual::CompiledMethodInfo.create_method(:Kernel ,:__send , [] )
|
2015-05-24 12:31:33 +02:00
|
|
|
function.info.return_type = Virtual::Integer
|
2014-09-23 19:16:05 +02:00
|
|
|
return function
|
|
|
|
end
|
2015-06-22 21:48:42 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
def emit_syscall function , name
|
|
|
|
save_message( function )
|
|
|
|
function.info.add_code Register::Syscall.new( name )
|
|
|
|
restore_message(function)
|
|
|
|
end
|
|
|
|
# save the current message, as the syscall destroys all context
|
|
|
|
#
|
|
|
|
# currently HACKED into the space as a temporary varaible. As the space is a globally
|
|
|
|
# unique object we can retrieve it from there
|
|
|
|
# TODO : fix this to use global (later per thread) variable
|
|
|
|
def save_message(function)
|
|
|
|
space_tmp = Register::RegisterReference.tmp_reg
|
|
|
|
ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message )
|
|
|
|
raise "index not found for :syscall_message" unless ind
|
2015-06-25 15:31:09 +02:00
|
|
|
function.info.add_code Register::LoadConstant.new( Parfait::Space.object_space , space_tmp)
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::SetSlot.new( Register::RegisterReference.message_reg , space_tmp , ind)
|
2015-06-22 21:48:42 +02:00
|
|
|
end
|
|
|
|
def restore_message(function)
|
|
|
|
# get the sys return out of the way
|
|
|
|
return_tmp = Register::RegisterReference.tmp_reg
|
|
|
|
# load the space into the base register
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::RegisterTransfer.new( return_tmp , Register::RegisterReference.message_reg )
|
2015-06-23 18:55:54 +02:00
|
|
|
slot = Virtual::Slot
|
2015-06-22 21:48:42 +02:00
|
|
|
# find the stored message
|
|
|
|
ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message )
|
|
|
|
raise "index not found for #{kind}.#{kind.class}" unless ind
|
|
|
|
# and load it into the base RegisterMachine
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , ind , Register::RegisterReference.message_reg )
|
2015-06-22 21:48:42 +02:00
|
|
|
# and "unroll" self and frame
|
2015-06-27 20:16:46 +02:00
|
|
|
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , Virtual::SELF_INDEX, Register::RegisterReference.self_reg )
|
|
|
|
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , Virtual::FRAME_INDEX, Register::RegisterReference.frame_reg )
|
2015-06-22 21:48:42 +02:00
|
|
|
end
|
2014-09-10 20:35:52 +02:00
|
|
|
end
|
|
|
|
extend ClassMethods
|
|
|
|
end
|
|
|
|
end
|