finally scoping builtin to register

had put this off because it breaks history
but now the references to register stuff which
builtin is off course full of, become much shorter
This commit is contained in:
Torsten Ruger 2015-06-29 21:03:58 +03:00
parent a03dcecbbd
commit 0f2c8e4201
7 changed files with 268 additions and 266 deletions

View File

@ -3,15 +3,16 @@
The Builtin module contains functions that can not be coded in ruby. The Builtin module contains functions that can not be coded in ruby.
It is the other side of the parfait coin, part of the runtime. It is the other side of the parfait coin, part of the runtime.
The functions are organized by their respective class and get loaded in boot_classes! , right at the start. The functions are organized by their respective class and get loaded in boot_classes! ,
right at the start. (see virtual/boot.rb)
These functions return their code, ie a Virtual::CompiledMethod object, which can then be called by ruby code These functions return their code, ie a Virtual::CompiledMethod object, which can then be called by
as if it were a "normal" function. ruby code as if it were a "normal" function.
A normal ruby function is one that is parsed and transformed to code. But not all functionality can be written in ruby, A normal ruby function is one that is parsed and transformed to code. But not all functionality can
one of those chicken and egg things. C uses Assembler in this situation, we use Builtin functions. be written in ruby, one of those chicken and egg things.
C uses Assembler in this situation, we use Builtin functions.
Slightly more here : http://salama.github.io/2014/06/10/more-clarity.html (then still called Kernel) Slightly more here : http://salama.github.io/2014/06/10/more-clarity.html (then still called Kernel)
The Builtin module is scattered into several files, but that is just so the file doesn't get too long. The Builtin module is scattered into several files, but that is just so the file doesn't get too long.

View File

@ -1,3 +1,4 @@
module Register
module Builtin module Builtin
class Array class Array
module ClassMethods module ClassMethods
@ -17,3 +18,4 @@ module Builtin
extend ClassMethods extend ClassMethods
end end
end end
end

View File

@ -1,4 +1,5 @@
#integer related kernel functions #integer related kernel functions
module Register
module Builtin module Builtin
module Integer module Integer
module ClassMethods module ClassMethods
@ -96,3 +97,4 @@ module Builtin
extend ClassMethods extend ClassMethods
end end
end end
end

View File

@ -1,3 +1,4 @@
module Register
module Builtin module Builtin
module Kernel module Kernel
module ClassMethods module ClassMethods
@ -12,12 +13,12 @@ module Builtin
function.info.blocks.last.codes.pop # no Method return function.info.blocks.last.codes.pop # no Method return
#Set up the Space as self upon init #Set up the Space as self upon init
space = Parfait::Space.object_space space = Parfait::Space.object_space
function.info.add_code Register::LoadConstant.new( space , Register::RegisterReference.self_reg) function.info.add_code LoadConstant.new( space , Register.self_reg)
message_ind = space.get_layout().index_of( :next_message ) message_ind = space.get_layout().index_of( :next_message )
# Load the message to message register (0) # Load the message to message register (0)
function.info.add_code Register::GetSlot.new( Register::RegisterReference.self_reg , message_ind , Register::RegisterReference.new_message_reg) function.info.add_code Register.get_slot( :self , message_ind , :message)
# And store the space as the new self (so the call can move it back as self) # 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) function.info.add_code Register.set_slot( :self , :message , :receiver)
# now we are set up to issue a call to the main # now we are set up to issue a call to the main
function.info.add_code Virtual::MethodCall.new(Virtual.machine.space.get_main) function.info.add_code Virtual::MethodCall.new(Virtual.machine.space.get_main)
emit_syscall( function , :exit ) emit_syscall( function , :exit )
@ -45,7 +46,7 @@ module Builtin
private private
def emit_syscall function , name def emit_syscall function , name
save_message( function ) save_message( function )
function.info.add_code Register::Syscall.new( name ) function.info.add_code Syscall.new( name )
restore_message(function) restore_message(function)
end end
# save the current message, as the syscall destroys all context # save the current message, as the syscall destroys all context
@ -54,28 +55,28 @@ module Builtin
# unique object we can retrieve it from there # unique object we can retrieve it from there
# TODO : fix this to use global (later per thread) variable # TODO : fix this to use global (later per thread) variable
def save_message(function) def save_message(function)
space_tmp = Register::RegisterReference.tmp_reg space_tmp = Register.tmp_reg
ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message ) ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message )
raise "index not found for :syscall_message" unless ind raise "index not found for :syscall_message" unless ind
function.info.add_code Register::LoadConstant.new( Parfait::Space.object_space , space_tmp) function.info.add_code LoadConstant.new( Parfait::Space.object_space , space_tmp)
function.info.add_code Register::SetSlot.new( Register::RegisterReference.message_reg , space_tmp , ind) function.info.add_code SetSlot.new( Register.message_reg , space_tmp , ind)
end end
def restore_message(function) def restore_message(function)
# get the sys return out of the way # get the sys return out of the way
return_tmp = Register::RegisterReference.tmp_reg return_tmp = Register.tmp_reg
# load the space into the base register # load the space into the base register
function.info.add_code Register::RegisterTransfer.new( return_tmp , Register::RegisterReference.message_reg ) function.info.add_code RegisterTransfer.new( return_tmp , Register.message_reg )
slot = Virtual::Slot
# find the stored message # find the stored message
ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message ) ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message )
raise "index not found for #{kind}.#{kind.class}" unless ind raise "index not found for #{kind}.#{kind.class}" unless ind
# and load it into the base RegisterMachine # and load it into the base RegisterMachine
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , ind , Register::RegisterReference.message_reg ) function.info.add_code Register.get_slot :message , ind , :message
# and "unroll" self and frame # and "unroll" self and frame
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , Virtual::SELF_INDEX, Register::RegisterReference.self_reg ) function.info.add_code Register.get_slot(:message , :receiver, :self )
function.info.add_code Register::GetSlot.new( Register::RegisterReference.message_reg , Virtual::FRAME_INDEX, Register::RegisterReference.frame_reg ) function.info.add_code Register.get_slot(:message , :frame , :frame)
end end
end end
extend ClassMethods extend ClassMethods
end end
end end
end

View File

@ -1,3 +1,4 @@
module Register
module Builtin module Builtin
class Object class Object
module ClassMethods module ClassMethods
@ -69,7 +70,8 @@ module Builtin
extend ClassMethods extend ClassMethods
end end
end end
end
require_relative "integer" require_relative "integer"
require_relative "word"
require_relative "array" require_relative "array"
require_relative "kernel" require_relative "kernel"

View File

@ -1,7 +0,0 @@
module Builtin
class Word
module ClassMethods
end
extend ClassMethods
end
end

View File

@ -25,7 +25,8 @@ module Virtual
value_classes = values.collect { |cl| @space.create_class(cl,nil) } value_classes = values.collect { |cl| @space.create_class(cl,nil) }
layouts = { :Word => [] , layouts = { :Word => [] ,
:List => [] , :List => [] ,
:Message => [:next_message], # Assumtion is that name is the last of message
:Message => [:next_message , :receiver , :frame , :return_address , :caller , :name ],
:MetaClass => [], :MetaClass => [],
:BinaryCode => [], :BinaryCode => [],
:Space => [:classes ,:next_message ,:next_frame, :syscall_message], :Space => [:classes ,:next_message ,:next_frame, :syscall_message],
@ -99,17 +100,17 @@ module Virtual
# TODO go through the virtual parfait layer and adjust function names to what they really are # TODO go through the virtual parfait layer and adjust function names to what they really are
obj = @class_mappings[:Object ] obj = @class_mappings[:Object ]
[:main , :_get_instance_variable , :_set_instance_variable].each do |f| [:main , :_get_instance_variable , :_set_instance_variable].each do |f|
obj.add_instance_method Builtin::Object.send(f , nil) obj.add_instance_method Register::Builtin::Object.send(f , nil)
end end
obj = @class_mappings[:Kernel ] obj = @class_mappings[:Kernel ]
# create dummy main first, __init__ calls it # create dummy main first, __init__ calls it
[:putstring,:exit,:__send , :__init__ ].each do |f| [:putstring,:exit,:__send , :__init__ ].each do |f|
obj.add_instance_method Builtin::Kernel.send(f , nil) obj.add_instance_method Register::Builtin::Kernel.send(f , nil)
end end
obj = @class_mappings[:Integer ] obj = @class_mappings[:Integer ]
[:putint,:fibo].each do |f| [:putint,:fibo].each do |f|
obj.add_instance_method Builtin::Integer.send(f , nil) obj.add_instance_method Register::Builtin::Integer.send(f , nil)
end end
end end