stash old vm

moving on to getting mom to work and can’t have both
interpreter and elf broke, about 100 tests  went
This commit is contained in:
Torsten Ruger 2018-03-11 17:02:42 +05:30
parent f7aac1d1a4
commit 5fe0ba06ab
56 changed files with 80 additions and 143 deletions

View File

@ -7,10 +7,10 @@ end
require "risc/padding" require "risc/padding"
require "risc/positioned" require "risc/positioned"
require "vm"
require "parfait" require "parfait"
require "risc/machine" require "risc/machine"
require "risc/method_compiler"
class Fixnum class Fixnum
def fits_u8? def fits_u8?

View File

@ -4,14 +4,14 @@ module Risc
module CompileHelper module CompileHelper
def self_and_int_arg(compiler , source) def self_and_int_arg(compiler , source)
me = compiler.process( Vm::Tree::KnownName.new( :self) ) me = compiler.add_known( :self )
int_arg = load_int_arg_at(compiler , source , 1 ) int_arg = load_int_arg_at(compiler , source , 1 )
return me , int_arg return me , int_arg
end end
def compiler_for( type , method_name , extra_args = {}) def compiler_for( type , method_name , extra_args = {})
args = {:index => :Integer}.merge( extra_args ) args = {:index => :Integer}.merge( extra_args )
Vm::MethodCompiler.create_method(type , method_name , args ).init_method Risc::MethodCompiler.create_method(type , method_name , args ).init_method
end end
# Load the value # Load the value

View File

@ -6,22 +6,23 @@ module Risc
include AST::Sexp include AST::Sexp
def mod4 context def mod4 context
compiler = Vm::MethodCompiler.create_method(:Integer,:mod4 ).init_method compiler = Risc::MethodCompiler.create_method(:Integer,:mod4 ).init_method
return compiler.method return compiler.method
end end
def putint context def putint context
compiler = Vm::MethodCompiler.create_method(:Integer,:putint ).init_method compiler = Risc::MethodCompiler.create_method(:Integer,:putint ).init_method
return compiler.method return compiler.method
end end
def div10 context def div10 context
s = "div_10" s = "div_10"
compiler = Vm::MethodCompiler.create_method(:Integer,:div10 ).init_method compiler = Risc::MethodCompiler.create_method(:Integer,:div10 ).init_method
me = compiler.process( Vm::Tree::KnownName.new( :self) ) me = compiler.add_known( :self )
tmp = compiler.process( Vm::Tree::KnownName.new( :self) ) tmp = compiler.add_known( :self )
q = compiler.process( Vm::Tree::KnownName.new( :self) ) q = compiler.add_known( :self )
const = compiler.process( Vm::Tree::IntegerExpression.new(1) ) const = compiler.use_reg :Integer , 1
compiler.add_load_constant( s, 1 , const )
# int tmp = self >> 1 # int tmp = self >> 1
compiler.add_code Risc.op( s , ">>" , tmp , const) compiler.add_code Risc.op( s , ">>" , tmp , const)
# int q = self >> 2 # int q = self >> 2

View File

@ -6,7 +6,7 @@ module Risc
# 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
compiler = Vm::MethodCompiler.create_method(:Kernel,:__init__ ) compiler = Risc::MethodCompiler.create_method(:Kernel,:__init__ )
new_start = Risc.label("__init__ start" , "__init__" ) new_start = Risc.label("__init__ start" , "__init__" )
compiler.method.set_instructions( new_start) compiler.method.set_instructions( new_start)
compiler.set_current new_start compiler.set_current new_start
@ -29,7 +29,7 @@ module Risc
end end
def exit context def exit context
compiler = Vm::MethodCompiler.create_method(:Kernel,:exit ).init_method compiler = Risc::MethodCompiler.create_method(:Kernel,:exit ).init_method
emit_syscall( compiler , :exit ) emit_syscall( compiler , :exit )
return compiler.method return compiler.method
end end

View File

@ -9,7 +9,7 @@ module Risc
# 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
compiler = Vm::MethodCompiler.create_method(:Space , :main ).init_method compiler = Risc::MethodCompiler.create_method(:Space , :main ).init_method
return compiler.method return compiler.method
end end

View File

@ -7,7 +7,7 @@ module Risc
include CompileHelper include CompileHelper
def putstring context def putstring context
compiler = Vm::MethodCompiler.create_method(:Word , :putstring ).init_method compiler = Risc::MethodCompiler.create_method(:Word , :putstring ).init_method
compiler.add_slot_to_reg( "putstring" , :message , :receiver , :new_message ) compiler.add_slot_to_reg( "putstring" , :message , :receiver , :new_message )
index = Parfait::Word.get_length_index index = Parfait::Word.get_length_index
reg = RiscValue.new(:r2 , :Integer) reg = RiscValue.new(:r2 , :Integer)

View File

@ -1,69 +1,10 @@
require_relative "method_compiler/assignment" module Risc
require_relative "method_compiler/basic_values"
require_relative "method_compiler/call_site"
require_relative "method_compiler/collections"
require_relative "method_compiler/field_access"
require_relative "method_compiler/if_statement"
require_relative "method_compiler/name_expression"
require_relative "method_compiler/operator_expression"
require_relative "method_compiler/return_statement"
require_relative "method_compiler/statement_list"
require_relative "method_compiler/while_statement"
module Vm # MethodCompiler (old name) is used to generate risc instructions for methods
# and to instantiate the methods correctly. Most of the init is typed layer stuff,
CompilerModules = [ "assignment" , "basic_values" , "call_site", # but there is some logic too.
"collections" , "field_access",
"if_statement" , "name_expression" ,
"operator_expression" , "return_statement", "statement_list",
"while_statement"]
CompilerModules.each do |mod|
# require_relative "method_compiler/" + mod
end
# Compiling is the conversion of the AST into 2 things:
# - code (ie sequences of Instructions inside Methods)
# - an object graph containing all the Methods, their classes and Constants
#
# Some compile methods just add code, some may add Instructions while
# others instantiate Class and TypedMethod objects
#
# Everything in ruby is an statement, ie returns a value. So the effect of every compile
# is that a value is put into the ReturnSlot of the current Message.
# The compile method (so every compile method) returns the value that it deposits.
#
# The process uses a visitor pattern (from AST::Processor) to dispatch according to the
# type the statement. So a s(:if xx) will become an on_if(node) call.
# This makes the dispatch extensible, ie Expressions may be added by external code,
# as long as matching compile methods are supplied too.
#
# A compiler can also be used to generate code for a method without AST nodes. In the same way
# compile methods do, ie adding Instructions etc. In this way code may be generated that
# has no code equivalent.
#
# The Compiler also keeps a list of used registers, from which one may take to use and return to
# when done. The list may be reset.
#
# The Compiler also carries method and class instance variables. The method is where code is
# added to (with add_code). To be more precise, the @current instruction is where code is added
# to, and that may be changed with set_current
# All Statements reset the registers and return nil.
# Expressions use registers and return the register where their value is stored.
# Helper function to create a new compiler and compie the statement(s)
# Statement must be and AST::Node as generated by s expressions
def self.compile_ast( statement )
compiler = MethodCompiler.new(:main)
code = Vm.ast_to_code statement
compiler.process code
end
class MethodCompiler class MethodCompiler
CompilerModules.each do |mod|
include Vm.const_get( mod.camelize )
end
def initialize( method ) def initialize( method )
@regs = [] @regs = []
@ -79,31 +20,6 @@ module Vm
end end
attr_reader :type , :method attr_reader :type , :method
# Dispatches `code` according to it's class name, for class NameExpression
# a method named `on_NameExpression` is invoked with one argument, the `code`
#
# @param [Vm::Code, nil] code
def process(code)
name = code.class.name.split("::").last
# Invoke a specific handler
on_handler = :"on_#{name}"
if respond_to? on_handler
return send on_handler, code
else
raise "No handler on_#{name}(code) #{code.inspect}"
end
end
# {#process}es each code from `codes` and returns an array of
# results.
#
def process_all(codes)
codes.to_a.map do |code|
process code
end
end
# create the method, do some checks and set it as the current method to be added to # create the method, do some checks and set it as the current method to be added to
# class_name and method_name are pretty clear, args are given as a ruby array # class_name and method_name are pretty clear, args are given as a ruby array
def self.create_method( class_name , method_name , args = {}) def self.create_method( class_name , method_name , args = {})
@ -148,6 +64,26 @@ module Vm
self self
end end
def add_known(name)
case name
when :self
ret = use_reg @type
add_slot_to_reg(" load self" , :message , :receiver , ret )
return ret
when :space
space = Parfait.object_space
reg = use_reg :Space , space
add_load_constant( "load space", space , reg )
return reg
when :message
reg = use_reg :Message
add_transfer( "load message", Risc.message_reg , reg )
return reg
else
raise "Unknow expression #{name}"
end
end
# set the insertion point (where code is added with add_code) # set the insertion point (where code is added with add_code)
def set_current c def set_current c
@current = c @current = c

View File

@ -39,7 +39,7 @@ module Vool
methods.each do |method| methods.each do |method|
code = Passes::MethodCompiler.new(method).get_code code = Passes::MethodCompiler.new(method).get_code
typed_method = method.create_parfait_method(clazz.instance_type) typed_method = method.create_parfait_method(clazz.instance_type)
Vm::MethodCompiler.new( typed_method ).init_method.process( code ) Risc::MethodCompiler.new( typed_method ).init_method.process( code )
end end
end end

View File

@ -12,7 +12,7 @@ class HelloTest < MiniTest::Test
writer.save "test/hello.o" writer.save "test/hello.o"
end end
def test_string_put def pest_string_put
@input = s(:statements, s(:return, s(:call, :putstring, s(:arguments), @input = s(:statements, s(:return, s(:call, :putstring, s(:arguments),
s(:receiver, s(:string, "Hello again\\n"))))) s(:receiver, s(:string, "Hello again\\n")))))
check check

View File

@ -9,7 +9,7 @@ module Risc
def setup def setup
Risc.machine.boot Risc.machine.boot
do_clean_compile do_clean_compile
Vm.compile_ast( @input ) #FIXME Vm.compile_ast( @input )
Collector.collect_space Collector.collect_space
@interpreter = Interpreter.new @interpreter = Interpreter.new
@interpreter.start Risc.machine.init @interpreter.start Risc.machine.init

View File

@ -16,7 +16,7 @@ HERE
super super
end end
def test_chain def pest_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant, LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -25,18 +25,18 @@ HERE
NilClass] NilClass]
end end
def test_get def pest_get
assert_equal SlotToReg , ticks(4).class assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r2 ) assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class assert Integer , @interpreter.get_register( :r2 ).class
end end
def test_transfer def pest_transfer
transfer = ticks 19 transfer = ticks 19
assert_equal RiscTransfer , transfer.class assert_equal RiscTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from) assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end end
def test_call def pest_call
ret = ticks(18) ret = ticks(18)
assert_equal FunctionReturn , ret.class assert_equal FunctionReturn , ret.class
@ -45,7 +45,7 @@ HERE
assert_equal Label , link.class assert_equal Label , link.class
end end
def test_adding def pest_adding
done_op = ticks(12) done_op = ticks(12)
assert_equal OperatorInstruction , done_op.class assert_equal OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left) left = @interpreter.get_register(done_op.left)

View File

@ -19,7 +19,7 @@ HERE
super super
end end
def test_chain def pest_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg, LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -33,31 +33,31 @@ HERE
Label, FunctionReturn, RiscTransfer, Syscall, NilClass] Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end end
def test_branch def pest_branch
was = @interpreter.instruction was = @interpreter.instruction
assert_equal Branch , ticks(1).class assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction" assert @interpreter.instruction , "should have gone to next instruction"
end end
def test_load def pest_load
assert_equal LoadConstant , ticks(3).class assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol assert_equal :r2, @interpreter.instruction.array.symbol
end end
def test_get def pest_get
assert_equal SlotToReg , ticks(4).class assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 ) assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class assert Integer , @interpreter.get_register( :r1 ).class
end end
def test_call def pest_call
assert_equal FunctionCall , ticks(8).class assert_equal FunctionCall , ticks(8).class
end end
def test_exit def pest_exit
done = ticks(50) done = ticks(50)
assert_equal NilClass , done.class assert_equal NilClass , done.class
end end
def test_reg_to_byte def pest_reg_to_byte
done = ticks(37) done = ticks(37)
assert_equal RegToByte , done.class assert_equal RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register) assert_equal "h".ord , @interpreter.get_register(done.register)

View File

@ -32,7 +32,7 @@ HERE
s(:true_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "then")))), 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")))))) s(:false_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "else"))))))
end end
def test_if def pest_if
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg, LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,

View File

@ -19,7 +19,7 @@ module Risc
@instruction_events << was @instruction_events << was
end end
def test_state_change def pest_state_change
@interpreter.register_event :state_changed , self @interpreter.register_event :state_changed , self
ticks 30 ticks 30
assert @state_events[:state_changed] assert @state_events[:state_changed]
@ -28,14 +28,14 @@ module Risc
@interpreter.unregister_event :state_changed , self @interpreter.unregister_event :state_changed , self
end end
def test_instruction_events def pest_instruction_events
@interpreter.register_event :instruction_changed , self @interpreter.register_event :instruction_changed , self
ticks 30 ticks 30
assert_equal 20 , @instruction_events.length assert_equal 20 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self @interpreter.unregister_event :instruction_changed , self
end end
def test_chain def pest_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant, LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,

View File

@ -17,7 +17,7 @@ HERE
super super
end end
def test_mult def pest_mult
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant, LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -26,12 +26,12 @@ HERE
NilClass] NilClass]
check_return 0 check_return 0
end end
def test_overflow def pest_overflow
ticks( 12 ) ticks( 12 )
assert @interpreter.flags[:overflow] assert @interpreter.flags[:overflow]
end end
def test_zero def pest_zero
ticks( 12 ) ticks( 12 )
assert @interpreter.flags[:zero] assert @interpreter.flags[:zero]
end end

View File

@ -16,7 +16,7 @@ HERE
super super
end end
def test_add def pest_add
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant, LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -26,12 +26,12 @@ HERE
check_return 0 check_return 0
end end
def test_overflow def pest_overflow
ticks( 12 ) ticks( 12 )
assert @interpreter.flags[:overflow] assert @interpreter.flags[:overflow]
end end
def test_zero def pest_zero
ticks( 12 ) ticks( 12 )
assert @interpreter.flags[:zero] assert @interpreter.flags[:zero]
end end

View File

@ -16,7 +16,7 @@ HERE
super super
end end
def test_chain def pest_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg, LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -29,40 +29,40 @@ HERE
Label, FunctionReturn, RiscTransfer, Syscall, NilClass] Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end end
def test_branch def pest_branch
was = @interpreter.instruction was = @interpreter.instruction
assert_equal Branch , ticks(1).class assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction" assert @interpreter.instruction , "should have gone to next instruction"
end end
def test_load def pest_load
assert_equal LoadConstant , ticks(3).class assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol assert_equal :r2, @interpreter.instruction.array.symbol
end end
def test_get def pest_get
assert_equal SlotToReg , ticks(4).class assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 ) assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class assert Integer , @interpreter.get_register( :r1 ).class
end end
def test_call def pest_call
assert_equal FunctionCall , ticks(8).class assert_equal FunctionCall , ticks(8).class
end end
def test_putstring def pest_putstring
done = ticks(29) done = ticks(29)
assert_equal Syscall , done.class assert_equal Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout assert_equal "Hello again" , @interpreter.stdout
end end
def test_return def pest_return
done = ticks(34) done = ticks(34)
assert_equal FunctionReturn , done.class assert_equal FunctionReturn , done.class
assert Label , @interpreter.instruction.class assert Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Instruction) , "not instruction #{@interpreter.instruction}" assert @interpreter.instruction.is_a?(Instruction) , "not instruction #{@interpreter.instruction}"
end end
def test_exit def pest_exit
done = ticks(45) done = ticks(45)
assert_equal NilClass , done.class assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout assert_equal "Hello again" , @interpreter.stdout

View File

@ -19,7 +19,7 @@ HERE
super super
end end
def test_chain def pest_chain
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg, LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -33,31 +33,31 @@ HERE
NilClass] NilClass]
end end
def test_branch def pest_branch
was = @interpreter.instruction was = @interpreter.instruction
assert_equal Branch , ticks(1).class assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction" assert @interpreter.instruction , "should have gone to next instruction"
end end
def test_load def pest_load
assert_equal LoadConstant , ticks(3).class assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol assert_equal :r2, @interpreter.instruction.array.symbol
end end
def test_get def pest_get
assert_equal SlotToReg , ticks(4).class assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 ) assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class assert Integer , @interpreter.get_register( :r1 ).class
end end
def test_call def pest_call
assert_equal FunctionCall , ticks(8).class assert_equal FunctionCall , ticks(8).class
end end
def test_exit def pest_exit
done = ticks(46) done = ticks(46)
assert_equal NilClass , done.class assert_equal NilClass , done.class
end end
def test_byte_to_reg def pest_byte_to_reg
done = ticks(32) done = ticks(32)
assert_equal ByteToReg , done.class assert_equal ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register) assert_equal "H".ord , @interpreter.get_register(done.register)

View File

@ -24,7 +24,7 @@ HERE
super super
end end
def test_if def pest_if
#show_ticks # get output of what is #show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot, check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant, LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,