use compiler to generate methods and their instructions

This commit is contained in:
Torsten Ruger 2015-10-28 21:37:42 +02:00
parent 99a695907c
commit 191be8d2f6
4 changed files with 33 additions and 33 deletions

View File

@ -12,7 +12,7 @@ module Register
# As we write before we recurse (save a push) we write the number backwards # As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer # arguments: string address , integer
# def utoa context # def utoa context
# utoa_function = MethodSource.create_method(:Integer ,:utoa , [ :Integer ] ) # compiler = Soml::Compiler.new.create_method(:Integer ,:utoa , [ :Integer ] ).init_method
# function.source.receiver = :Integer # function.source.receiver = :Integer
# return utoa_function # return utoa_function
# # str_addr = utoa_function.receiver # # str_addr = utoa_function.receiver
@ -31,8 +31,8 @@ module Register
# end # end
def putint context def putint context
putint_function = MethodSource.create_method(:Integer,:putint , [] ) compiler = Soml::Compiler.new.create_method(:Integer,:putint , [] ).init_method
return putint_function return compiler.method
# buffer = Parfait::Word.new(" ") # create a buffer # buffer = Parfait::Word.new(" ") # create a buffer
# context.object_space.add_object buffer # and save it (function local variable: a no no) # context.object_space.add_object buffer # and save it (function local variable: a no no)
# int = putint_function.receiver # int = putint_function.receiver
@ -58,8 +58,8 @@ module Register
# a hand coded version of the fibonachi numbers # a hand coded version of the fibonachi numbers
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
def fibo context def fibo context
fibo_function = MethodSource.create_method(:Integer,:fibo , [] ) compiler = Soml::Compiler.new.create_method(:Integer,:fibo , [] ).init_method
return fibo_function return compiler.method
# int = fibo_function.receiver # int = fibo_function.receiver
# #
# last = fibo_function.new_block("return") # last = fibo_function.new_block("return")

View File

@ -6,58 +6,58 @@ module Register
# it isn't really a function, ie it is jumped to (not called), exits and may not return # it isn't really a function, ie it is jumped to (not called), exits and may not return
# so it is responsible for initial setup # so it is responsible for initial setup
def __init__ context def __init__ context
function = MethodSource.create_method(:Kernel,:__init__ , []) compiler = Soml::Compiler.new.create_method(:Kernel,:__init__ , [])
# no method enter or return (automatically added), remove # no method enter or return (automatically added), remove
new_start = Label.new(function , "__init__" ) new_start = Label.new("__init__" , "__init__" )
function.instructions = new_start compiler.method.instructions = new_start
function.source.set_current new_start compiler.set_current new_start
#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
space_reg = Register.tmp_reg(:Space) space_reg = Register.tmp_reg(:Space)
function.source.add_code LoadConstant.new(function, space , space_reg) compiler.add_code LoadConstant.new("__init__", space , space_reg)
message_ind = Register.resolve_index( :space , :first_message ) message_ind = Register.resolve_index( :space , :first_message )
# Load the message to new message register (r1) # Load the message to new message register (r1)
function.source.add_code Register.get_slot( function , space_reg , message_ind , :new_message) compiler.add_code Register.get_slot( "__init__" , space_reg , message_ind , :new_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.source.add_code Register.set_slot( function, space_reg , :new_message , :receiver) compiler.add_code Register.set_slot( "__init__", space_reg , :new_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
Register.issue_call( function , Register.machine.space.get_main) Register.issue_call( compiler , Register.machine.space.get_main)
emit_syscall( function , :exit ) emit_syscall( compiler , :exit )
return function return compiler.method
end end
def exit context def exit context
function = MethodSource.create_method(:Kernel,:exit , []) compiler = Soml::Compiler.new.create_method(:Kernel,:exit , []).init_method
return function return compiler.method
ret = RegisterMachine.instance.exit(function) ret = RegisterMachine.instance.exit(function)
function.set_return ret function.set_return ret
function function
end end
def emit_syscall function , name def emit_syscall compiler , name
save_message( function ) save_message( compiler )
function.source.add_code Syscall.new(function, name ) compiler.add_code Syscall.new("emit_syscall", name )
restore_message(function) restore_message(compiler)
end end
# save the current message, as the syscall destroys all context # save the current message, as the syscall destroys all context
# #
# This relies on linux to save and restore all registers # This relies on linux to save and restore all registers
# #
def save_message(function) def save_message(compiler)
r8 = RegisterValue.new( :r8 , :Message) r8 = RegisterValue.new( :r8 , :Message)
function.source.add_code RegisterTransfer.new(function, Register.message_reg , r8 ) compiler.add_code RegisterTransfer.new("save_message", Register.message_reg , r8 )
end end
def restore_message(function) def restore_message(compiler)
r8 = RegisterValue.new( :r8 , :Message) r8 = RegisterValue.new( :r8 , :Message)
return_tmp = Register.tmp_reg :Integer return_tmp = Register.tmp_reg :Integer
# get the sys return out of the way # get the sys return out of the way
function.source.add_code RegisterTransfer.new(function, Register.message_reg , return_tmp ) compiler.add_code RegisterTransfer.new("restore_message", Register.message_reg , return_tmp )
# load the stored message into the base RegisterMachine # load the stored message into the base RegisterMachine
function.source.add_code RegisterTransfer.new(function, r8 , Register.message_reg ) compiler.add_code RegisterTransfer.new("restore_message", r8 , Register.message_reg )
# save the return value into the message # save the return value into the message
function.source.add_code Register.set_slot( function, return_tmp , :message , :return_value ) compiler.add_code Register.set_slot( "restore_message" , return_tmp , :message , :return_value )
# and "unroll" self and frame # and "unroll" self and frame
end end
end end

View File

@ -6,8 +6,8 @@ module Register
# main entry point, ie __init__ calls this # main entry point, ie __init__ calls this
# defined here as empty, to be redefined # defined here as empty, to be redefined
def main context def main context
function = MethodSource.create_method(:Object , :main , []) compiler = Soml::Compiler.new.create_method(:Object , :main , []).init_method
return function return compiler.method
end end
end end

View File

@ -3,10 +3,10 @@ module Register
module Word module Word
module ClassMethods module ClassMethods
def putstring context def putstring context
function = MethodSource.create_method(:Word , :putstring , [] ) compiler = Soml::Compiler.new.create_method(:Word , :putstring , [] ).init_method
function.source.add_code Register.get_slot( function , :message , :receiver , :new_message ) compiler.add_code Register.get_slot( "putstring" , :message , :receiver , :new_message )
Kernel.emit_syscall( function , :putstring ) Kernel.emit_syscall( compiler , :putstring )
function compiler.method
end end
end end
extend ClassMethods extend ClassMethods