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.
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
as if it were a "normal" function.
These functions return their code, ie a Virtual::CompiledMethod object, which can then be called by
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,
one of those chicken and egg things. C uses Assembler in this situation, we use Builtin functions.
A normal ruby function is one that is parsed and transformed to code. But not all functionality can
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)
The Builtin module is scattered into several files, but that is just so the file doesn't get too long.

View File

@ -1,4 +1,5 @@
module Builtin
module Register
module Builtin
class Array
module ClassMethods
def get context , index = Virtual::Integer
@ -16,4 +17,5 @@ module Builtin
end
extend ClassMethods
end
end
end

View File

@ -1,5 +1,6 @@
#integer related kernel functions
module Builtin
module Register
module Builtin
module Integer
module ClassMethods
# The conversion to base10 is quite a bit more complicated than i thought.
@ -95,4 +96,5 @@ module Builtin
end
extend ClassMethods
end
end
end

View File

@ -1,4 +1,5 @@
module Builtin
module Register
module Builtin
module Kernel
module ClassMethods
# this is the really really first place the machine starts (apart from the jump here)
@ -12,12 +13,12 @@ module Builtin
function.info.blocks.last.codes.pop # no Method return
#Set up the Space as self upon init
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 )
# 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)
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
function.info.add_code Virtual::MethodCall.new(Virtual.machine.space.get_main)
emit_syscall( function , :exit )
@ -45,7 +46,7 @@ module Builtin
private
def emit_syscall function , name
save_message( function )
function.info.add_code Register::Syscall.new( name )
function.info.add_code Syscall.new( name )
restore_message(function)
end
# save the current message, as the syscall destroys all context
@ -54,28 +55,28 @@ module Builtin
# 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
space_tmp = Register.tmp_reg
ind = Parfait::Space.object_space.get_layout().index_of( :syscall_message )
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 Register::SetSlot.new( Register::RegisterReference.message_reg , space_tmp , ind)
function.info.add_code LoadConstant.new( Parfait::Space.object_space , space_tmp)
function.info.add_code SetSlot.new( Register.message_reg , space_tmp , ind)
end
def restore_message(function)
# 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
function.info.add_code Register::RegisterTransfer.new( return_tmp , Register::RegisterReference.message_reg )
slot = Virtual::Slot
function.info.add_code RegisterTransfer.new( return_tmp , Register.message_reg )
# 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
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
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 )
function.info.add_code Register.get_slot(:message , :receiver, :self )
function.info.add_code Register.get_slot(:message , :frame , :frame)
end
end
extend ClassMethods
end
end
end

View File

@ -1,4 +1,5 @@
module Builtin
module Register
module Builtin
class Object
module ClassMethods
@ -24,16 +25,16 @@ module Builtin
return_to = get_function.return_type
index_function = ::Virtual.machine.space.get_class_by_name(:Object).resolve_method(:index_of)
# get_function.push( [me] )
# index = get_function.call( index_function )
# get_function.push( [me] )
# index = get_function.call( index_function )
after_body = get_function.new_block("after_index")
get_function.current after_body
# get_function.pop([me])
# return_to.at_index( get_function , me , return_to )
# get_function.pop([me])
# return_to.at_index( get_function , me , return_to )
# get_function.set_return return_to
# get_function.set_return return_to
return get_function
end
@ -68,8 +69,9 @@ module Builtin
end
extend ClassMethods
end
end
end
require_relative "integer"
require_relative "word"
require_relative "array"
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) }
layouts = { :Word => [] ,
:List => [] ,
:Message => [:next_message],
# Assumtion is that name is the last of message
:Message => [:next_message , :receiver , :frame , :return_address , :caller , :name ],
:MetaClass => [],
:BinaryCode => [],
: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
obj = @class_mappings[:Object ]
[: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
obj = @class_mappings[:Kernel ]
# create dummy main first, __init__ calls it
[: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
obj = @class_mappings[:Integer ]
[: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