rename register to risc
seems to fit the layer much better as we really have a very reduced instruction set
This commit is contained in:
23
test/risc/interpreter/helper.rb
Normal file
23
test/risc/interpreter/helper.rb
Normal file
@ -0,0 +1,23 @@
|
||||
require_relative "../helper"
|
||||
require "risc/interpreter"
|
||||
|
||||
module Risc
|
||||
module Ticker
|
||||
include AST::Sexp
|
||||
include InterpreterHelpers
|
||||
|
||||
def setup
|
||||
Risc.machine.boot
|
||||
do_clean_compile
|
||||
Vm.compile_ast( @input )
|
||||
Collector.collect_space
|
||||
@interpreter = Interpreter.new
|
||||
@interpreter.start Risc.machine.init
|
||||
end
|
||||
|
||||
# must be after boot, but before main compile, to define method
|
||||
def do_clean_compile
|
||||
end
|
||||
|
||||
end
|
||||
end
|
64
test/risc/interpreter/test_add.rb
Normal file
64
test/risc/interpreter/test_add.rb
Normal file
@ -0,0 +1,64 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class AddTest < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
return 5 + 7
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
end
|
||||
|
||||
def test_get
|
||||
assert_equal SlotToReg , ticks(4).class
|
||||
assert @interpreter.get_register( :r2 )
|
||||
assert Integer , @interpreter.get_register( :r2 ).class
|
||||
end
|
||||
def test_transfer
|
||||
transfer = ticks 19
|
||||
assert_equal RiscTransfer , transfer.class
|
||||
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
|
||||
end
|
||||
|
||||
def test_call
|
||||
ret = ticks(18)
|
||||
assert_equal FunctionReturn , ret.class
|
||||
|
||||
object = @interpreter.get_register( ret.register )
|
||||
link = object.get_internal_word( ret.index )
|
||||
|
||||
assert_equal Label , link.class
|
||||
end
|
||||
def test_adding
|
||||
done_op = ticks(12)
|
||||
assert_equal OperatorInstruction , done_op.class
|
||||
left = @interpreter.get_register(done_op.left)
|
||||
rr = done_op.right
|
||||
right = @interpreter.get_register(rr)
|
||||
assert_equal Fixnum , left.class
|
||||
assert_equal Fixnum , right.class
|
||||
assert_equal 7 , right
|
||||
assert_equal 12 , left
|
||||
done_tr = ticks(1)
|
||||
assert_equal RegToSlot , done_tr.class
|
||||
result = @interpreter.get_register(done_op.left)
|
||||
assert_equal result , 12
|
||||
end
|
||||
end
|
||||
end
|
9
test/risc/interpreter/test_all.rb
Normal file
9
test/risc/interpreter/test_all.rb
Normal file
@ -0,0 +1,9 @@
|
||||
require_relative "test_add"
|
||||
require_relative "test_change"
|
||||
require_relative "test_byte_to_reg"
|
||||
require_relative "test_called_if"
|
||||
require_relative "test_simple_if"
|
||||
require_relative "test_puts"
|
||||
require_relative "test_plus"
|
||||
require_relative "test_mult"
|
||||
require_relative "test_reg_to_byte"
|
67
test/risc/interpreter/test_byte_to_reg.rb
Normal file
67
test/risc/interpreter/test_byte_to_reg.rb
Normal file
@ -0,0 +1,67 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class TestInterpretRegToByte < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
"Hello".set_internal_byte(1,104)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:call,
|
||||
:set_internal_byte ,
|
||||
s(:arguments, s(:int, 1), s(:int, 104)),
|
||||
s(:receiver, s(:string, "Hello"))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
|
||||
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
|
||||
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot,
|
||||
RiscTransfer, FunctionCall, Label, LoadConstant, SlotToReg,
|
||||
RegToSlot, SlotToReg, SlotToReg, SlotToReg, SlotToReg,
|
||||
SlotToReg, RegToByte, Label, FunctionReturn, RiscTransfer,
|
||||
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot,
|
||||
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
|
||||
end
|
||||
|
||||
def test_branch
|
||||
was = @interpreter.instruction
|
||||
assert_equal Branch , ticks(1).class
|
||||
assert was != @interpreter.instruction
|
||||
assert @interpreter.instruction , "should have gone to next instruction"
|
||||
end
|
||||
def test_load
|
||||
assert_equal LoadConstant , ticks(3).class
|
||||
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
|
||||
assert_equal :r2, @interpreter.instruction.array.symbol
|
||||
end
|
||||
def test_get
|
||||
assert_equal SlotToReg , ticks(4).class
|
||||
assert @interpreter.get_register( :r1 )
|
||||
assert Integer , @interpreter.get_register( :r1 ).class
|
||||
end
|
||||
def test_call
|
||||
assert_equal FunctionCall , ticks(8).class
|
||||
end
|
||||
def test_exit
|
||||
done = ticks(50)
|
||||
assert_equal NilClass , done.class
|
||||
end
|
||||
|
||||
def test_reg_to_byte
|
||||
done = ticks(37)
|
||||
assert_equal RegToByte , done.class
|
||||
assert_equal "h".ord , @interpreter.get_register(done.register)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
55
test/risc/interpreter/test_called_if.rb
Normal file
55
test/risc/interpreter/test_called_if.rb
Normal file
@ -0,0 +1,55 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class IfCalledTest < MiniTest::Test
|
||||
include Ticker
|
||||
include Compiling
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int itest(int n)
|
||||
if_zero( n - 12)
|
||||
"then".putstring()
|
||||
else
|
||||
"else".putstring()
|
||||
end
|
||||
end
|
||||
|
||||
int main()
|
||||
itest(20)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:call, :itest , s(:arguments, s(:int, 20))))
|
||||
super
|
||||
end
|
||||
|
||||
# must be after boot, but before main compile, to define method
|
||||
def do_clean_compile
|
||||
clean_compile :Space , :itest , {:n => :Integer} ,
|
||||
s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:arg, :n), s(:int, 12))),
|
||||
s(:true_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "then")))),
|
||||
s(:false_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "else"))))))
|
||||
end
|
||||
def test_if
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
|
||||
SlotToReg, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
|
||||
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label,
|
||||
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
|
||||
LoadConstant, OperatorInstruction, IsZero, SlotToReg, LoadConstant,
|
||||
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall,
|
||||
Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg,
|
||||
SlotToReg, RiscTransfer, Syscall, RiscTransfer, RiscTransfer,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, SlotToReg,
|
||||
SlotToReg, Branch, Label, Label, FunctionReturn,
|
||||
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
end
|
||||
end
|
||||
end
|
48
test/risc/interpreter/test_change.rb
Normal file
48
test/risc/interpreter/test_change.rb
Normal file
@ -0,0 +1,48 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class AddChange < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 5), s(:int, 7))))
|
||||
@state_events = {}
|
||||
@instruction_events = []
|
||||
super
|
||||
end
|
||||
|
||||
def state_changed( a , b)
|
||||
@state_events[:state_changed] = [a , b]
|
||||
end
|
||||
|
||||
def instruction_changed(was , is )
|
||||
@instruction_events << was
|
||||
end
|
||||
|
||||
def test_state_change
|
||||
@interpreter.register_event :state_changed , self
|
||||
ticks 30
|
||||
assert @state_events[:state_changed]
|
||||
assert_equal 2 , @state_events[:state_changed].length
|
||||
assert_equal :running, @state_events[:state_changed][0]
|
||||
@interpreter.unregister_event :state_changed , self
|
||||
end
|
||||
|
||||
def test_instruction_events
|
||||
@interpreter.register_event :instruction_changed , self
|
||||
ticks 30
|
||||
assert_equal 20 , @instruction_events.length
|
||||
@interpreter.unregister_event :instruction_changed , self
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
40
test/risc/interpreter/test_mult.rb
Normal file
40
test/risc/interpreter/test_mult.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class MultTest < MiniTest::Test
|
||||
include Ticker
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
return #{2**31} * #{2**31}
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:return, s(:operator_value, :*, s(:int, 2147483648), s(:int, 2147483648))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_mult
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
check_return 0
|
||||
end
|
||||
def test_overflow
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:overflow]
|
||||
end
|
||||
|
||||
def test_zero
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:zero]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
39
test/risc/interpreter/test_plus.rb
Normal file
39
test/risc/interpreter/test_plus.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class PlusTest < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
return #{2**62 - 1} + 1
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:return, s(:operator_value, :+, s(:int, 4611686018427387903), s(:int, 1))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_add
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
LoadConstant, OperatorInstruction, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
check_return 0
|
||||
end
|
||||
|
||||
def test_overflow
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:overflow]
|
||||
end
|
||||
|
||||
def test_zero
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:zero]
|
||||
end
|
||||
end
|
||||
end
|
71
test/risc/interpreter/test_puts.rb
Normal file
71
test/risc/interpreter/test_puts.rb
Normal file
@ -0,0 +1,71 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class TestPuts < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
"Hello again".putstring()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "Hello again"))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
|
||||
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
|
||||
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer,
|
||||
FunctionCall, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
SlotToReg, SlotToReg, RiscTransfer, Syscall, RiscTransfer,
|
||||
RiscTransfer, RegToSlot, Label, FunctionReturn, RiscTransfer,
|
||||
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot,
|
||||
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
|
||||
end
|
||||
|
||||
def test_branch
|
||||
was = @interpreter.instruction
|
||||
assert_equal Branch , ticks(1).class
|
||||
assert was != @interpreter.instruction
|
||||
assert @interpreter.instruction , "should have gone to next instruction"
|
||||
end
|
||||
def test_load
|
||||
assert_equal LoadConstant , ticks(3).class
|
||||
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
|
||||
assert_equal :r2, @interpreter.instruction.array.symbol
|
||||
end
|
||||
def test_get
|
||||
assert_equal SlotToReg , ticks(4).class
|
||||
assert @interpreter.get_register( :r1 )
|
||||
assert Integer , @interpreter.get_register( :r1 ).class
|
||||
end
|
||||
def test_call
|
||||
assert_equal FunctionCall , ticks(8).class
|
||||
end
|
||||
|
||||
def test_putstring
|
||||
done = ticks(29)
|
||||
assert_equal Syscall , done.class
|
||||
assert_equal "Hello again" , @interpreter.stdout
|
||||
end
|
||||
|
||||
def test_return
|
||||
done = ticks(34)
|
||||
assert_equal FunctionReturn , done.class
|
||||
assert Label , @interpreter.instruction.class
|
||||
assert @interpreter.instruction.is_a?(Instruction) , "not instruction #{@interpreter.instruction}"
|
||||
end
|
||||
|
||||
def test_exit
|
||||
done = ticks(45)
|
||||
assert_equal NilClass , done.class
|
||||
assert_equal "Hello again" , @interpreter.stdout
|
||||
end
|
||||
end
|
||||
end
|
67
test/risc/interpreter/test_reg_to_byte.rb
Normal file
67
test/risc/interpreter/test_reg_to_byte.rb
Normal file
@ -0,0 +1,67 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class TestInterpretByteToReg < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
"Hello".get_internal_byte(1)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:call,
|
||||
:get_internal_byte,
|
||||
s(:arguments, s(:int, 1)),
|
||||
s(:receiver, s(:string, "Hello"))))
|
||||
super
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
|
||||
LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant,
|
||||
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label,
|
||||
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg,
|
||||
SlotToReg, ByteToReg, RegToSlot, Label, FunctionReturn,
|
||||
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
end
|
||||
|
||||
def test_branch
|
||||
was = @interpreter.instruction
|
||||
assert_equal Branch , ticks(1).class
|
||||
assert was != @interpreter.instruction
|
||||
assert @interpreter.instruction , "should have gone to next instruction"
|
||||
end
|
||||
def test_load
|
||||
assert_equal LoadConstant , ticks(3).class
|
||||
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
|
||||
assert_equal :r2, @interpreter.instruction.array.symbol
|
||||
end
|
||||
def test_get
|
||||
assert_equal SlotToReg , ticks(4).class
|
||||
assert @interpreter.get_register( :r1 )
|
||||
assert Integer , @interpreter.get_register( :r1 ).class
|
||||
end
|
||||
def test_call
|
||||
assert_equal FunctionCall , ticks(8).class
|
||||
end
|
||||
def test_exit
|
||||
done = ticks(46)
|
||||
assert_equal NilClass , done.class
|
||||
end
|
||||
|
||||
def test_byte_to_reg
|
||||
done = ticks(32)
|
||||
assert_equal ByteToReg , done.class
|
||||
assert_equal "H".ord , @interpreter.get_register(done.register)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
42
test/risc/interpreter/test_simple_if.rb
Normal file
42
test/risc/interpreter/test_simple_if.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Risc
|
||||
class IfSimpleTest < MiniTest::Test
|
||||
include Ticker
|
||||
include Compiling
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
if_zero( 10 - 12)
|
||||
"then".putstring()
|
||||
else
|
||||
"else".putstring()
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))),
|
||||
s(:true_statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "then")))),
|
||||
s(:false_statements, s(:call, :putstring, s(:arguments), s(:receiver, s(:string, "else"))))))
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
def test_if
|
||||
#show_ticks # get output of what is
|
||||
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
|
||||
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
|
||||
LoadConstant, OperatorInstruction, IsZero, SlotToReg, LoadConstant,
|
||||
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg,
|
||||
RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall,
|
||||
Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg,
|
||||
SlotToReg, RiscTransfer, Syscall, RiscTransfer, RiscTransfer,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, SlotToReg,
|
||||
SlotToReg, Branch, Label, LoadConstant, SlotToReg,
|
||||
RegToSlot, Label, FunctionReturn, RiscTransfer, Syscall,
|
||||
NilClass]
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user