transformed object builtins

This commit is contained in:
Torsten Rüger 2019-08-11 20:36:10 +03:00
parent 0725f02e9a
commit 3282e0ae06
5 changed files with 190 additions and 93 deletions

View File

@ -37,9 +37,8 @@ module Risc
end
obj_type = space.get_type_by_class_name(:Object)
[ :get_internal_word , #:set_internal_word , :_method_missing,
#:exit , :__init__
].each do |f|
[ :get_internal_word , :set_internal_word , :_method_missing,
:exit , :__init__ ].each do |f|
compilers << compiler_for( obj_type , Object , f)
end

View File

@ -28,6 +28,12 @@ module Risc
# return the value passed in
def set_internal_word( context )
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, value: :Object} )
compiler.add_code SetInternalWord.new("set_internal_word")
return compiler
end
class SetInternalWord < ::Mom::Instruction
def to_risc(compiler)
compiler.builder(compiler.source).build do
object! << message[:receiver]
integer! << message[:arguments]
@ -37,19 +43,26 @@ module Risc
object[integer] << object_reg
message[:return_value] << object_reg
end
compiler.add_mom( Mom::ReturnSequence.new)
return compiler
end
end
# every object needs a method missing.
# Even if it's just this one, sys_exit (later raise)
def _method_missing( context )
compiler = compiler_for(:Object,:method_missing ,{})
compiler.add_code MethodMissing.new("missing")
return compiler
end
class MethodMissing < ::Mom::Instruction
def to_risc(compiler)
builder = compiler.builder(compiler.source)
builder.prepare_int_return # makes integer_tmp variable as return
emit_syscall( builder , :exit )
Builtin.emit_syscall( builder , :exit )
return compiler
end
end
# 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
@ -58,8 +71,14 @@ module Risc
# - call main, ie set up message for that etc
# - exit (exit_sequence) which passes a machine int out to c
def __init__( context )
compiler = MethodCompiler.compiler_for_class(:Object,:__init__ ,
compiler = Mom::MethodCompiler.compiler_for_class(:Object,:__init__ ,
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
compiler.add_code MethodMissing.new("missing")
return compiler
end
class Init < ::Mom::Instruction
def to_risc(compiler)
builder = compiler.builder(compiler.source)
builder.build do
factory! << Parfait.object_space.get_factory_for(:Message)
@ -88,21 +107,42 @@ module Risc
exit_sequence(builder)
return compiler
end
end
# the exit function
# mainly calls exit_sequence
def exit( context )
compiler = compiler_for(:Object,:exit ,{})
compiler.add_code Exit.new("exit")
return compiler
end
class Exit < ::Mom::Instruction
def to_risc(compiler)
builder = compiler.builder(compiler.source)
builder.prepare_int_return # makes integer_tmp variable as return
exit_sequence(builder)
Builtin.exit_sequence(builder)
return compiler
end
end
end
extend ClassMethods
end
# emit the syscall with given name
# there is a Syscall instruction, but the message has to be saved and restored
def self.emit_syscall( builder , name )
save_message( builder )
builder.add_code Syscall.new("emit_syscall(#{name})", name )
restore_message(builder)
return unless (@clazz and @method)
builder.add_code Risc.label( "#{@clazz.name}.#{@message.name}" , "return_syscall" )
end
# a sort of inline version of exit method.
# Used by exit and __init__ (so it doesn't have to call it)
# Assumes int return value and extracts the fixnum for process exit code
def exit_sequence(builder)
def self.exit_sequence(builder)
save_message( builder )
builder.build do
message << message[:return_value]
@ -111,21 +151,11 @@ module Risc
end
end
# emit the syscall with given name
# there is a Syscall instruction, but the message has to be saved and restored
def emit_syscall( builder , name )
save_message( builder )
builder.add_code Syscall.new("emit_syscall(#{name})", name )
restore_message(builder)
return unless (@clazz and @method)
builder.add_code Risc.label( "#{@clazz.name}.#{@message.name}" , "return_syscall" )
end
# save the current message, as the syscall destroys all context
#
# This relies on linux to save and restore all registers
#
def save_message(builder)
def self.save_message(builder)
r8 = RegisterValue.new( :r8 , :Message).set_builder(builder)
builder.build {r8 << message}
end
@ -135,7 +165,7 @@ module Risc
# The caller of this method is assumed to caal prepare_int_return
# so that the return value already has an integer instance
# This instance is filled with os return value
def restore_message(builder)
def self.restore_message(builder)
r8 = RegisterValue.new( :r8 , :Message)
builder.build do
integer_reg! << message
@ -144,9 +174,5 @@ module Risc
integer_2[Parfait::Integer.integer_index] << integer_reg
end
end
end
extend ClassMethods
end
end
end

View File

@ -39,7 +39,7 @@ module Vool
check = Mom::NotSameCheck.new(compile_method , runtime_method, ok_label)
# TODO? Maybe create mom instructions for this
#builder = compiler.builder("yield")
#Risc::Builtin::Object.exit_sequence(builder)
#Risc::Builtin.exit_sequence(builder)
#check << builder.built
check << ok_label
end

View File

@ -2,7 +2,7 @@ require_relative "helper"
module Risc
module Builtin
class TestObjectFunction1 < BootTest
class TestObjectFunctionGet < BootTest
def setup
super
@method = get_compiler(:get_internal_word)
@ -20,5 +20,77 @@ module Risc
assert_equal 20 , @method.to_risc.risc_instructions.length
end
end
class TestObjectFunctionSet < BootTest
def setup
super
@method = get_compiler(:set_internal_word)
end
def test_has_get_internal
assert_equal Mom::MethodCompiler , @method.class
end
def test_mom_length
assert_equal 5 , @method.mom_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 21 , @method.to_risc.risc_instructions.length
end
end
class TestObjectFunctionMissing < BootTest
def setup
super
@method = get_compiler(:method_missing)
end
def test_has_get_internal
assert_equal Mom::MethodCompiler , @method.class
end
def test_mom_length
assert_equal 5 , @method.mom_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 48 , @method.to_risc.risc_instructions.length
end
end
class TestObjectFunctionExit < BootTest
def setup
super
@method = get_compiler(:exit)
end
def test_has_get_internal
assert_equal Mom::MethodCompiler , @method.class
end
def test_mom_length
assert_equal 5 , @method.mom_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 46 , @method.to_risc.risc_instructions.length
end
end
class TestObjectFunctionInit < BootTest
def setup
super
@method = get_compiler(:__init__)
end
def test_has_get_internal
assert_equal Mom::MethodCompiler , @method.class
end
def test_mom_length
assert_equal 5 , @method.mom_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 48 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -14,7 +14,7 @@ module Risc
assert_equal Array, @functions.class
end
def test_boot_function_length
assert_equal 2, @functions.length
assert_equal 6, @functions.length
end
def test_boot_function_first
assert_equal Mom::MethodCompiler, @functions.first.class