tombagby / llvmruby

LLVM bindings for Ruby

This URL has Read+Write access

llvmruby / test / test_ruby_vm.rb
100644 173 lines (150 sloc) 3.535 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'test/unit'
require 'llvm'
require 'ruby_vm'
 
include LLVM
RubyVM.start
 
class RubyVMTests < Test::Unit::TestCase
  def test_getinstancevariable
    bytecode = [
      [:putobject, nil.immediate],
      [:getinstancevariable, :@shaka]
    ]
 
    obj = Object.new
    obj.instance_variable_set(:@shaka, 'khan')
    compiled_method = RubyVM.compile_bytecode(bytecode)
    assert_equal('khan', RubyVM.method_send(obj, compiled_method))
  end
 
  def test_setinstancevariable
    bytecode = [
      [:putobject, 'puter'],
      [:setinstancevariable, :@fem]
    ]
 
    obj = Object.new
    compiled_method = RubyVM.compile_bytecode(bytecode)
    RubyVM.method_send(obj, compiled_method)
    assert_equal('puter', obj.instance_variable_get(:@fem))
  end
 
  def test_opt_aset
    bytecode = [
      [:newarray],
      [:dup],
      [:putobject, 0.immediate],
      [:putobject, 'shaka'.immediate],
      [:opt_aset],
      [:pop]
    ]
  
    ret = RubyVM.call_bytecode(bytecode, nil)
    assert_equal(ret, ['shaka'])
  end
 
  def opt_cmp_tester(op, truth_table)
    truth_table.each do |x, y, z|
      bytecode = [
        [:putobject, x.immediate],
        [:putobject, y.immediate],
        [op]
      ]
      ret = RubyVM.call_bytecode(bytecode, nil)
      assert_equal(z, ret)
    end
  end
 
  def test_opt_lt
    opt_cmp_tester(:opt_lt, [
      [0, 1, true],
      [1, 0, false],
      [1, 1, false]
    ])
  end
 
  def test_opt_gt
    opt_cmp_tester(:opt_gt, [
      [0, 1, false],
      [1, 0, true],
      [1, 1, false]
    ])
  end
 
  def test_opt_ge
    opt_cmp_tester(:opt_ge, [
      [0, 1, false],
      [1, 0, true],
      [1, 1, true]
    ])
  end
 
  def test_opt_length
    bytecode = [
      [:opt_length]
    ]
 
    ret1 = RubyVM.call_bytecode(bytecode, [])
    assert_equal(0, ret1)
 
    ret2 = RubyVM.call_bytecode(bytecode, [1,2,3,4,5])
    assert_equal(5, ret2)
  end
 
  def test_simple_loop
    bytecode = [
      [:putobject, 1.immediate],
      [:opt_plus],
      [:dup],
      [:putobject, 10.immediate],
      [:opt_lt],
      [:branchif, 0]
    ]
 
    ret = RubyVM.call_bytecode(bytecode, 6)
    assert_equal(10, ret)
  end
 
  def test_array_loop
    bytecode = [
      [:dup],
      [:setlocal, 0],
      [:opt_length],
      [:putobject, 1.immediate],
      [:opt_minus],
      [:dup],
      [:getlocal, 0],
      [:swap],
      [:opt_aref],
      [:putobject, 2.immediate],
      [:opt_mult],
      [:setlocal, 1],
      [:dup],
      [:getlocal, 0],
      [:swap],
      [:getlocal, 1],
      [:opt_aset],
      [:pop],
      [:dup],
      [:putobject, 0.immediate],
      [:opt_gt],
      [:branchif, 3],
      [:getlocal, 0]
    ]
 
    ret = RubyVM.call_bytecode(bytecode, [1,2,3,4,5,6])
    assert_equal([2,4,6,8,10,12], ret)
  end
 
  def test_send
    bytecode = [
      [:send]
    ]
 
    ret = RubyVM.call_bytecode(bytecode, nil)
    assert_equal('nil', ret)
  end
 
  def test_putself
    bytecode = [
      [:putobject, 5.immediate],
      [:putself]
    ]
    compiled_method = RubyVM.compile_bytecode(bytecode)
    ret = RubyVM.method_send('me', compiled_method)
    assert_equal('me', ret)
  end
 
  def test_newhash
    bytecode = [
      [:putobject, 'one'.immediate],
      [:putobject, 1.immediate],
      [:putobject, 'two'.immediate],
      [:putobject, 2.immediate],
      [:putobject, 'three'.immediate],
      [:putobject, 3.immediate],
      [:newhash, 6]
    ]
    ret = RubyVM.call_bytecode(bytecode, nil)
    assert_equal({'one' => 1, 'two' => 2, 'three' => 3}, ret)
  end
end