named the first intruction constructor argument

This commit is contained in:
Torsten Ruger 2014-05-15 19:41:51 +03:00
parent b4c79d218f
commit a0f0d08e81
13 changed files with 78 additions and 77 deletions

View File

@ -11,46 +11,46 @@ module Arm
class ArmMachine < Vm::CMachine
def integer_less_or_equal block , left , right
block.add_code cmp(:left => left , :right => right )
block.add_code cmp( left , :right => right )
Vm::Bool.new
end
def integer_plus block , result , left , right
block.add_code add(:left => result , :right => left , :extra => right )
block.add_code add( result , :right => left , :extra => right )
result
end
def integer_minus block , result , left , right
block.add_code sub(:left => result , :right => left , :extra => right )
block.add_code sub( result , :right => left , :extra => right )
result
end
def integer_load block , left , right
block.add_code mov( :left => left , :right => right )
block.add_code mov( left , :right => right )
left
end
def integer_move block , left , right
block.add_code mov( :left => left , :right => right )
block.add_code mov( left , :right => right )
left
end
def string_load block , str_lit , reg
block.add_code add( :left => "r#{reg}".to_sym , :extra => str_lit ) #right is pc, implicit
block.add_code add( "r#{reg}".to_sym , :extra => str_lit ) #right is pc, implicit
#second arg is a hack to get the stringlength without coding
block.add_code mov( :left => "r#{reg+1}".to_sym , :right => str_lit.length )
block.add_code mov( "r#{reg+1}".to_sym , :right => str_lit.length )
str_lit
end
def function_call into , call
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
raise "Not linked #{call.inspect}" unless call.function
into.add_code call( :left => call.function )
into.add_code call( call.function )
call.function.return_type
end
def main_start entry
entry.add_code mov( :left => :fp , :right => 0 )
entry.add_code mov( :fp , :right => 0 )
end
def main_exit exit
syscall(exit , 1)
@ -61,14 +61,14 @@ module Arm
block
end
def function_exit entry , f_name
entry.add_code mov( :left => :pc , :right => :lr )
entry.add_code mov( :pc , :right => :lr )
end
# assumes string in r0 and r1 and moves them along for the syscall
def write_stdout block
block.add_code mov( :left => :r2 , :right => :r1 )
block.add_code mov( :left => :r1 , :right => :r0 )
block.add_code mov( :left => :r0 , :right => 1 ) # 1 == stdout
block.add_code mov( :r2 , :right => :r1 )
block.add_code mov( :r1 , :right => :r0 )
block.add_code mov( :r0 , :right => 1 ) # 1 == stdout
syscall( block , 4 )
end
@ -110,7 +110,7 @@ module Arm
# takes argument in r1
# returns quotient in r1, remainder in r2
# SUB r2, r1, #10 # keep (x-10) for later
block.add_code sub(:left => remainder , :right => number , :extra => 10 )
block.add_code sub( remainder , :right => number , :extra => 10 )
# SUB r1, r1, r1, lsr #2
# ADD r1, r1, r1, lsr #4
# ADD r1, r1, r1, lsr #8
@ -124,8 +124,8 @@ module Arm
end
def syscall block , num
block.add_code mov( :left => :r7 , :right => num )
block.add_code swi( :left => 0 )
block.add_code mov( :r7 , :right => num )
block.add_code swi( 0 , {})
Vm::Integer.new(0) #small todo, is this actually correct for all (that they return int)
end

View File

@ -20,8 +20,8 @@ module Arm
4
end
def initialize(attributes)
super(attributes)
def initialize(left, attributes)
super(left , attributes)
@attributes[:update_status_flag] = 0
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
end
@ -29,7 +29,7 @@ module Arm
def assemble(io)
case @attributes[:opcode]
when :b, :call
arg = @attributes[:left]
arg = @left
#puts "BLAB #{arg.inspect}"
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
arg = Vm::IntegerConstant.new( arg )
@ -49,7 +49,7 @@ module Arm
end
io.write_uint8 op_bit_code | (COND_CODES[@attributes[:condition_code]] << 4)
when :swi
arg = @attributes[:left]
arg = @left
if( arg.is_a? Fixnum ) #HACK to not have to change the code just now
arg = Vm::IntegerConstant.new( arg )
end

View File

@ -5,13 +5,13 @@ module Arm
include Arm::Constants
include LogicHelper
def initialize(attributes)
super(attributes)
def initialize(left , attributes)
super(left , attributes)
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@i = 0
@attributes[:update_status_flag] = 1
@rn = attributes[:left]
@rn = left
@rd = :r0
end
def build

View File

@ -6,15 +6,15 @@ module Arm
include Arm::Constants
include LogicHelper
def initialize(attributes)
super(attributes)
def initialize(left , attributes)
super(left , attributes)
@attributes[:update_status_flag] = 0 if @attributes[:update_status_flag] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@rn = nil
@i = 0
@rd = @attributes[:left]
@rd = @left
end
attr_accessor :i, :rn, :rd
# Build representation for source value

View File

@ -6,8 +6,8 @@ module Arm
class MemoryInstruction < Vm::MemoryInstruction
include Arm::Constants
def initialize(attributes)
super(attributes)
def initialize(left , attributes)
super(left , attributes)
@attributes[:update_status_flag] = 0 if @attributes[:update_status_flag] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@ -28,11 +28,11 @@ module Arm
# Build representation for target address
def build
if( @is_load )
@rd = @attributes[:left]
@rd = @left
arg = @attributes[:right]
else #store
@rd = @attributes[:right]
arg = @attributes[:left]
arg = @left
end
#str / ldr are _serious instructions. With BIG possibilities not half are implemented
if (arg.is_a?(Symbol)) #symbol is register

View File

@ -6,15 +6,15 @@ module Arm
include Arm::Constants
include LogicHelper
def initialize(attributes)
super(attributes)
def initialize(left , attributes)
super(left , attributes)
@attributes[:update_status_flag] = 0 if @attributes[:update_status_flag] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@attributes[:opcode] = attributes[:opcode]
@operand = 0
@i = 0
@rd = @attributes[:left]
@rd = @left
@rn = :r0 # register zero = zero bit pattern
end

View File

@ -11,8 +11,8 @@ module Arm
4
end
def initialize(attributes)
super(attributes)
def initialize(left , attributes)
super(left , attributes)
@attributes[:update_status_flag] = 0 if @attributes[:update_status_flag] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@attributes[:opcode] = attributes[:opcode]
@ -55,7 +55,7 @@ module Arm
private
# Build representation for source value
def build
regs = @attributes[:regs]
regs = @left
if (regs.is_a?(Array))
@operand = 0
regs.each do |reg |

View File

@ -66,7 +66,7 @@ module Vm
# sugar to create instructions easily. Any method with one arg is sent to the machine and the result
# (hopefully an instruction) added as code
def method_missing(meth, *args, &block)
if args.length == 1
if args.length == 2
add_code CMachine.instance.send(meth , *args)
else
super

View File

@ -81,11 +81,11 @@ module Vm
# defaults gets merged into the instructions options hash, ie passed on to the (machine specific)
# Instruction constructor and as such can be used to influence that classes behaviour
def define_instruction(inst , clazz , defaults = {} )
create_method(inst) do |options|
create_method(inst) do |left , options|
options = {} if options == nil
options.merge defaults
options[:opcode] = inst
clazz.new(options)
clazz.new(left , options)
end
end

View File

@ -25,7 +25,8 @@ module Vm
# Make hash attributes to object attributes
include Support::HashAttributes
def initialize options
def initialize left , options
@left = left
@attributes = options
end
end
@ -43,7 +44,7 @@ module Vm
class MoveInstruction < Instruction
end
class CallInstruction < Instruction
def initialize options
def initialize left , options
super
opcode = @attributes[:opcode].to_s
if opcode.length == 3 and opcode[0] == "b"

View File

@ -38,8 +38,8 @@ def foo(x) #here
end #anywhere
foo( 3 ) #and more
HERE
@parse_output =[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}, {:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}]
@transform_output = [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))] ), Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(3)] )]
@parse_output = [{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"<", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"putstring"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}, {:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}]
@transform_output = [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0)),Ast::OperatorExpression.new("=", Ast::NameExpression.new("b"),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new("<", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")), Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")), Ast::OperatorExpression.new("=", Ast::NameExpression.new("b"),Ast::OperatorExpression.new("+", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("b"))), Ast::CallSiteExpression.new(:putstring, [Ast::NameExpression.new("b")] ), Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )] ), Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(3)] )]
end
def test_fibo1

View File

@ -27,110 +27,110 @@ class TestArmAsm < MiniTest::Test
end
end
def test_adc
code = @machine.adc :left => :r1, :right => :r3, :extra => :r5
code = @machine.adc :r1, :right => :r3, :extra => :r5
assert_code code , :adc , [0x05,0x10,0xa3,0xe0] #e0 a3 10 05
end
def test_add
code = @machine.add :left => :r1 , :right => :r1, :extra => :r3
code = @machine.add :r1 , :right => :r1, :extra => :r3
assert_code code , :add , [0x03,0x10,0x81,0xe0] #e0 81 10 03
end
def test_and # inst eval doesn't really work with and
code = @machine.and( :left => :r1 , :right => :r2 , :extra => :r3)
code = @machine.and( :r1 , :right => :r2 , :extra => :r3)
assert_code code , :and , [0x03,0x10,0x02,0xe0] #e0 01 10 03
end
def test_b
# the address is what an assembler calculates (a signed number for the amount of instructions),
# ie the relative (to pc) address -8 (pipeline) /4 so save space
# so the cpu adds the value*4 and starts loading that (load, decode, execute)
code = @machine.b :left => -4 #this jumps to the next instruction
code = @machine.b -4 , {} #this jumps to the next instruction
assert_code code , :b , [0xff,0xff,0xff,0xea] #ea ff ff fe
end
def test_call #see comment above. bx not implemented (as it means into thumb, and no thumb here)
code = @machine.call :left => -4 #this jumps to the next instruction
code = @machine.call -4 ,{} #this jumps to the next instruction
assert_code code , :call, [0xff,0xff,0xff,0xeb] #ea ff ff fe
end
def test_bic
code = @machine.bic :left => :r2 , :right => :r2 , :extra => :r3
code = @machine.bic :r2 , :right => :r2 , :extra => :r3
assert_code code , :bic , [0x03,0x20,0xc2,0xe1] #e3 c2 20 44
end
def test_cmn
code = @machine.cmn :left => :r1 , :right => :r2
code = @machine.cmn :r1 , :right => :r2
assert_code code , :cmn , [0x02,0x00,0x71,0xe1] #e1 71 00 02
end
def test_cmp
code = @machine.cmp :left => :r1 , :right => :r2
code = @machine.cmp :r1 , :right => :r2
assert_code code , :cmp , [0x02,0x00,0x51,0xe1] #e1 51 00 02
end
def test_eor
code = @machine.eor :left => :r2 , :right => :r2 , :extra => :r3
code = @machine.eor :r2 , :right => :r2 , :extra => :r3
assert_code code , :eor , [0x03,0x20,0x22,0xe0] #e0 22 20 03
end
def test_ldr
code = @machine.ldr :left => :r0, :right => :r0
code = @machine.ldr :r0, :right => :r0
assert_code code, :ldr , [0x00,0x00,0x90,0xe5] #e5 90 00 00
end
def test_ldr2
code = @machine.ldr :left => :r0, :right => :r0 , :offset => 4
code = @machine.ldr :r0, :right => :r0 , :offset => 4
assert_code code, :ldr , [0x04,0x00,0x90,0xe5] #e5 90 00 04
end
def test_ldrb
code = @machine.ldrb :left => :r0, :right => :r0
code = @machine.ldrb :r0, :right => :r0
assert_code code, :ldrb , [0x00,0x00,0xd0,0xe5] #e5 d0 00 00
end
def test_orr
code = @machine.orr :left => :r2 , :right => :r2 , :extra => :r3
code = @machine.orr :r2 , :right => :r2 , :extra => :r3
assert_code code , :orr , [0x03,0x20,0x82,0xe1] #e1 82 20 03
end
def test_push
code = @machine.push :regs => [:lr]
code = @machine.push [:lr] , {}
assert_code code , :push , [0x00,0x40,0x2d,0xe9] #e9 2d 40 00
end
def test_pop
code = @machine.pop :regs => [:pc]
code = @machine.pop [:pc] , {}
assert_code code , :pop , [0x00,0x80,0xbd,0xe8] #e8 bd 80 00
end
def test_rsb
code = @machine.rsb :left => :r1 , :right => :r2 , :extra => :r3
code = @machine.rsb :r1 , :right => :r2 , :extra => :r3
assert_code code , :rsb , [0x03,0x10,0x62,0xe0]#e0 62 10 03
end
def test_rsc
code = @machine.rsc :left => :r2 , :right => :r3 , :extra => :r4
code = @machine.rsc :r2 , :right => :r3 , :extra => :r4
assert_code code , :rsc , [0x04,0x20,0xe3,0xe0]#e0 e3 20 04
end
def test_sbc
code = @machine.sbc :left => :r3, :right => :r4 , :extra => :r5
code = @machine.sbc :r3, :right => :r4 , :extra => :r5
assert_code code , :sbc , [0x05,0x30,0xc4,0xe0]#e0 c4 30 05
end
def test_str
code = @machine.str :left => :r0, :right => :r0
code = @machine.str :r0, :right => :r0
assert_code code, :str , [0x00,0x00,0x80,0xe5] #e5 81 00 00
end
def test_strb
code = @machine.strb :left => :r0, :right => :r0
code = @machine.strb :r0, :right => :r0
assert_code code, :strb , [0x00,0x00,0xc0,0xe5] #e5 c0 00 00
end
def test_sub
code = @machine.sub :left => :r2, :right => :r0, :extra => 1
code = @machine.sub :r2, :right => :r0, :extra => 1
assert_code code, :sub , [0x01,0x20,0x40,0xe2] #e2 40 20 01
end
def test_swi
code = @machine.swi :left => 0x05
code = @machine.swi 0x05 , {}
assert_code code , :swi , [0x05,0x00,0x00,0xef]#ef 00 00 05
end
def test_teq
code = @machine.teq :left => :r1 , :right => :r2
code = @machine.teq :r1 , :right => :r2
assert_code code , :teq , [0x02,0x00,0x31,0xe1] #e1 31 00 02
end
def test_tst
code = @machine.tst :left => :r1 , :right => :r2
code = @machine.tst :r1 , :right => :r2
assert_code code , :tst , [0x02,0x00,0x11,0xe1] #e1 11 00 02
end
def test_mov
code = @machine.mov :left => :r0, :right => 5
code = @machine.mov :r0, :right => 5
assert_code code , :mov , [0x05,0x00,0xa0,0xe3] #e3 a0 10 05
end
def test_mvn
code = @machine.mvn :left => :r1, :right => 5
code = @machine.mvn :r1, :right => 5
assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05
end
end

View File

@ -14,12 +14,12 @@ class TestSmallProg < MiniTest::Test
def test_loop
@program.main.instance_eval do
mov :left => :r0, :right => 5 #1
mov :r0, :right => 5 #1
start = Vm::Block.new("start")
add_code start
start.instance_eval do
sub :left => :r0, :right => :r0, :extra => 1 , :update_status_flag => 1 #2
bne :left => start #3
sub :r0, :right => :r0, :extra => 1 , :update_status_flag => 1 #2
bne start ,{} #3
end
end
write( 6 , "loop" )
@ -29,11 +29,11 @@ class TestSmallProg < MiniTest::Test
hello = Vm::StringConstant.new "Hello Raisa\n"
@program.add_object hello
@program.main.instance_eval do
mov :left =>:r7, :right => 4 # 4 == write
mov :left =>:r0 , :right => 1 # stdout
add :left =>:r1 , :extra => hello # address of "hello Raisa"
mov :left =>:r2 , :right => hello.length
swi :left => 0 #software interupt, ie kernel syscall
mov :r7, :right => 4 # 4 == write
mov :r0 , :right => 1 # stdout
add :r1 , :extra => hello # address of "hello Raisa"
mov :r2 , :right => hello.length
swi 0 , {} #software interupt, ie kernel syscall
end
write(7 + hello.length/4 + 1 , 'hello')
end