From 17a7f29b0ce46338f8a6240ba3dac350cf6f8cea Mon Sep 17 00:00:00 2001 From: Torsten Date: Mon, 16 Mar 2020 17:31:14 +0200 Subject: [PATCH] define and use syscall_XX registers rather than use hardcoded r0 etc use syscall_X change the syscalls and interpreter to use them later use platform to map from syscall_X to actually used register (like r0 in arm) --- lib/risc/instructions/syscall.rb | 7 +++++++ lib/risc/interpreter.rb | 4 ++-- lib/slot_machine/macro/macro.rb | 7 +++---- lib/slot_machine/macro/putstring.rb | 8 ++++++-- test/support/risc_interpreter.rb | 2 +- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/risc/instructions/syscall.rb b/lib/risc/instructions/syscall.rb index b612004c..c2701c3e 100644 --- a/lib/risc/instructions/syscall.rb +++ b/lib/risc/instructions/syscall.rb @@ -19,6 +19,13 @@ module Risc def to_s class_source name end + end + # return the nth register that a sycall needs + # these map to different physical registers in the Platform + # first arg is (running 1..) integer, second (optional) type + def self.syscall_reg( number , type = nil) + type = :Integer unless type + RegisterValue.new("syscall_#{number}".to_sym , type) end end diff --git a/lib/risc/interpreter.rb b/lib/risc/interpreter.rb index f063de04..64adffdd 100644 --- a/lib/risc/interpreter.rb +++ b/lib/risc/interpreter.rb @@ -257,12 +257,12 @@ module Risc else raise "un-implemented syscall #{name}" end - set_register( :r0 , ret_value ) # syscalls return into r0 , usually some int + set_register( :syscall_1 , ret_value ) # syscalls return into syscall_1 true end def handle_putstring - str = get_register( :r1 ) # should test length, ie r2 + str = get_register( :"syscall_1" ) # should test length, ie r2 case str when Symbol @stdout << str.to_s diff --git a/lib/slot_machine/macro/macro.rb b/lib/slot_machine/macro/macro.rb index b5f6cbab..e2699015 100644 --- a/lib/slot_machine/macro/macro.rb +++ b/lib/slot_machine/macro/macro.rb @@ -20,10 +20,9 @@ module SlotMachine # Assumes int return value and extracts the fixnum for process exit code def self.exit_sequence(builder) save_message( builder ) + unix_return = Risc.syscall_reg(1) builder.build do - message << message[:return_value] - int = message.reduce_int(false) #hack, noo type check - message << int + unix_return << message[:return_value].reduce_int(false) #hack, noo type check add_code Risc::Syscall.new("emit_syscall(exit)", :exit ) end end @@ -46,7 +45,7 @@ module SlotMachine r8 = Risc::RegisterValue.new( :saved_message , :Message).set_compiler(builder.compiler) tmp = Risc::RegisterValue.new( :integer_tmp , :Integer).set_compiler(builder.compiler) builder.build do - tmp << message + tmp << Risc.syscall_reg(1) message << r8 message[:return_value][Parfait::Integer.integer_index] << tmp end diff --git a/lib/slot_machine/macro/putstring.rb b/lib/slot_machine/macro/putstring.rb index d746949b..9371d620 100644 --- a/lib/slot_machine/macro/putstring.rb +++ b/lib/slot_machine/macro/putstring.rb @@ -3,9 +3,13 @@ module SlotMachine def to_risc(compiler) builder = compiler.builder(compiler.source) integer = builder.prepare_int_return # makes integer_tmp variable as return + word = Risc.syscall_reg(1).set_compiler(compiler) + length = Risc.syscall_reg(2).set_compiler(compiler) builder.build do - word = message[:receiver].to_reg.known_type(:Word) - integer << word[:char_length] + string = message[:receiver].to_reg.known_type(:Word) + integer << string[:char_length] + word << string # into right + length << integer # registers end SlotMachine::Macro.emit_syscall( builder , :putstring ) compiler diff --git a/test/support/risc_interpreter.rb b/test/support/risc_interpreter.rb index 288001ac..80f2b0d0 100644 --- a/test/support/risc_interpreter.rb +++ b/test/support/risc_interpreter.rb @@ -49,7 +49,7 @@ module Risc def get_return assert_equal Parfait::Message , @interpreter.get_register(:saved_message).class - @interpreter.get_register(:message) + @interpreter.get_register(:syscall_1) end # do as many as given ticks in the main, ie main_at more