2019-08-12 12:16:15 +02:00
|
|
|
require_relative "get_internal_word"
|
|
|
|
require_relative "set_internal_word"
|
|
|
|
require_relative "method_missing"
|
|
|
|
require_relative "init"
|
|
|
|
require_relative "exit"
|
|
|
|
|
2019-08-12 11:36:32 +02:00
|
|
|
module Mom
|
2015-06-29 20:03:58 +02:00
|
|
|
module Builtin
|
|
|
|
class Object
|
|
|
|
module ClassMethods
|
2016-12-15 18:20:54 +01:00
|
|
|
include CompileHelper
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2015-11-07 16:36:28 +01:00
|
|
|
# self[index] basically. Index is the first arg
|
|
|
|
# return is stored in return_value
|
2018-04-01 13:09:30 +02:00
|
|
|
def get_internal_word( context )
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Object , :get_internal_word ,{at: :Integer})
|
2019-08-11 13:31:00 +02:00
|
|
|
compiler.add_code GetInternalWord.new("get_internal_word")
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2015-11-07 16:36:28 +01:00
|
|
|
end
|
2016-12-06 10:38:09 +01:00
|
|
|
# self[index] = val basically. Index is the first arg , value the second
|
2018-08-14 10:23:19 +02:00
|
|
|
# return the value passed in
|
2018-04-01 14:17:16 +02:00
|
|
|
def set_internal_word( context )
|
2018-11-21 19:29:22 +01:00
|
|
|
compiler = compiler_for(:Object , :set_internal_word , {at: :Integer, value: :Object} )
|
2019-08-11 19:36:10 +02:00
|
|
|
compiler.add_code SetInternalWord.new("set_internal_word")
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2015-11-08 16:10:36 +01:00
|
|
|
end
|
|
|
|
|
2018-04-02 15:49:30 +02:00
|
|
|
# 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 ,{})
|
2019-08-11 19:36:10 +02:00
|
|
|
compiler.add_code MethodMissing.new("missing")
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-04-02 15:49:30 +02:00
|
|
|
end
|
2018-06-29 12:03:19 +02:00
|
|
|
|
2018-04-02 16:06:31 +02:00
|
|
|
# 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
|
2018-08-14 10:23:19 +02:00
|
|
|
# so it is responsible for initial setup:
|
|
|
|
# - load fist message, set up Space as receiver
|
|
|
|
# - call main, ie set up message for that etc
|
|
|
|
# - exit (exit_sequence) which passes a machine int out to c
|
|
|
|
def __init__( context )
|
2019-08-11 19:36:10 +02:00
|
|
|
compiler = Mom::MethodCompiler.compiler_for_class(:Object,:__init__ ,
|
2018-04-02 16:06:31 +02:00
|
|
|
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
|
2019-08-14 10:11:26 +02:00
|
|
|
compiler.add_code Init.new("missing")
|
2019-08-11 19:36:10 +02:00
|
|
|
return compiler
|
|
|
|
end
|
|
|
|
|
2018-11-22 08:16:56 +01:00
|
|
|
# the exit function
|
|
|
|
# mainly calls exit_sequence
|
|
|
|
def exit( context )
|
|
|
|
compiler = compiler_for(:Object,:exit ,{})
|
2019-08-11 19:36:10 +02:00
|
|
|
compiler.add_code Exit.new("exit")
|
2018-11-22 08:16:56 +01:00
|
|
|
return compiler
|
|
|
|
end
|
|
|
|
|
2019-08-11 19:36:10 +02:00
|
|
|
end
|
|
|
|
extend ClassMethods
|
|
|
|
end
|
2018-06-19 17:55:47 +02:00
|
|
|
|
2019-08-11 19:36:10 +02:00
|
|
|
# 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 )
|
2019-08-12 11:36:32 +02:00
|
|
|
builder.add_code Risc::Syscall.new("emit_syscall(#{name})", name )
|
2019-08-11 19:36:10 +02:00
|
|
|
restore_message(builder)
|
|
|
|
return unless (@clazz and @method)
|
|
|
|
builder.add_code Risc.label( "#{@clazz.name}.#{@message.name}" , "return_syscall" )
|
|
|
|
end
|
2018-04-02 16:06:31 +02:00
|
|
|
|
2019-08-11 19:36:10 +02:00
|
|
|
# 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 self.exit_sequence(builder)
|
|
|
|
save_message( builder )
|
|
|
|
builder.build do
|
|
|
|
message << message[:return_value]
|
|
|
|
message.reduce_int
|
2019-08-12 11:36:32 +02:00
|
|
|
add_code Risc::Syscall.new("emit_syscall(exit)", :exit )
|
2019-08-11 19:36:10 +02:00
|
|
|
end
|
|
|
|
end
|
2018-04-02 16:06:31 +02:00
|
|
|
|
2019-08-11 19:36:10 +02:00
|
|
|
# save the current message, as the syscall destroys all context
|
|
|
|
#
|
|
|
|
# This relies on linux to save and restore all registers
|
|
|
|
#
|
|
|
|
def self.save_message(builder)
|
2019-08-12 11:36:32 +02:00
|
|
|
r8 = Risc::RegisterValue.new( :r8 , :Message).set_builder(builder)
|
2019-08-11 19:36:10 +02:00
|
|
|
builder.build {r8 << message}
|
|
|
|
end
|
2018-04-02 16:06:31 +02:00
|
|
|
|
2019-08-11 19:36:10 +02:00
|
|
|
# restore the message that we save in r8
|
|
|
|
# before th restore, the syscall return, a fixnum, is saved
|
|
|
|
# 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 self.restore_message(builder)
|
2019-08-12 11:36:32 +02:00
|
|
|
r8 = Risc::RegisterValue.new( :r8 , :Message)
|
2019-08-11 19:36:10 +02:00
|
|
|
builder.build do
|
|
|
|
integer_reg! << message
|
|
|
|
message << r8
|
|
|
|
integer_2! << message[:return_value]
|
|
|
|
integer_2[Parfait::Integer.integer_index] << integer_reg
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|