Class: OpCodeBuilder

Inherits:
Object show all
Defined in:
lib/ruby_vm.rb

Instance Method Summary collapse

Constructor Details

#initialize(mod, func, rb_funcs) ⇒ OpCodeBuilder

Returns a new instance of OpCodeBuilder.



77
78
79
80
81
# File 'lib/ruby_vm.rb', line 77

def initialize(mod, func, rb_funcs)
  @mod = mod
  @func = func
  @rb_funcs = rb_funcs
end

Instance Method Details

#dup(b, oprnd) ⇒ Object



106
107
108
# File 'lib/ruby_vm.rb', line 106

def dup(b, oprnd)
  b.push(b.peek)
end

#get_selfObject



83
84
85
# File 'lib/ruby_vm.rb', line 83

def get_self
  @func.arguments.first
end

#getinstancevariable(b, oprnd) ⇒ Object



205
206
207
208
209
# File 'lib/ruby_vm.rb', line 205

def getinstancevariable(b, oprnd)
  id = b.call(@rb_funcs[:rb_to_id], oprnd.llvm)
  v = b.call(@rb_funcs[:rb_ivar_get], get_self, id)
  b.push(v)
end

#getlocal(b, oprnd) ⇒ Object



123
124
125
126
127
# File 'lib/ruby_vm.rb', line 123

def getlocal(b, oprnd)
  local_slot = b.gep(b.locals, oprnd.llvm)
  val = b.load(local_slot)
  b.push(val)
end

#newarray(b, oprnd) ⇒ Object



217
218
219
220
# File 'lib/ruby_vm.rb', line 217

def newarray(b, oprnd)
  ary = b.call(@rb_funcs[:rb_ary_new])
  b.push(ary)
end

#newhash(b, oprnd) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ruby_vm.rb', line 222

def newhash(b, oprnd)
  hash = b.call(@rb_funcs[:rb_hash_new])
  i = oprnd.llvm(Type::Int32Ty) # This is an integer not a fixnum

  entry_block = @func.create_block
  loop_block = @func.create_block
  exit_block = @func.create_block

  b.br(entry_block)
  b.set_insert_point(entry_block)
  cmp = b.icmp_sgt(i, 0.llvm(Type::Int32Ty)) 
  b.cond_br(cmp, loop_block, exit_block)

  b.set_insert_point(loop_block)
  idx = b.phi(Type::Int32Ty)
  idx.add_incoming(i, entry_block)
  next_idx = b.sub(idx, 2.llvm(Type::Int32Ty))
  idx.add_incoming(next_idx, loop_block)

  n_1 = b.sub(idx, 1.llvm(Type::Int32Ty))
  n_2 = b.sub(idx, 2.llvm(Type::Int32Ty))
  b.call(@rb_funcs[:rb_hash_aset], hash, b.topn(n_1), b.topn(n_2))
  
  cmp = b.icmp_sgt(next_idx, 0.llvm(Type::Int32Ty))
  b.cond_br(cmp, loop_block, exit_block)

  b.set_insert_point(exit_block)
  b.push(hash)
end

#nop(b, oprnd) ⇒ Object



87
88
# File 'lib/ruby_vm.rb', line 87

def nop(b, oprnd)
end

#opt_aref(b, oprnd) ⇒ Object



150
151
152
153
154
155
# File 'lib/ruby_vm.rb', line 150

def opt_aref(b, oprnd)
  idx = b.fix2int(b.pop)
  ary = b.pop
  out = b.aref(ary, idx)
  b.push(out)
end

#opt_aset(b, oprnd) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/ruby_vm.rb', line 157

def opt_aset(b, oprnd)
  set = b.pop
  idx = b.fix2int(b.pop)
  ary = b.pop
  b.call(@rb_funcs[:rb_ary_store], ary, idx, set)
  b.push(set)
end

#opt_ge(b, oprnd) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/ruby_vm.rb', line 194

def opt_ge(b, oprnd)
  obj = b.pop
  recv = b.pop
  x = b.fix2int(recv)
  y = b.fix2int(obj)
  val = b.icmp_sge(x, y)
  val = b.int_cast(val, LONG, false)
  val = b.mul(val, 2.llvm)
  b.push(val)
end

#opt_gt(b, oprnd) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby_vm.rb', line 183

def opt_gt(b, oprnd)
  obj = b.pop
  recv = b.pop
  x = b.fix2int(recv)
  y = b.fix2int(obj)
  val = b.icmp_sgt(x, y)
  val = b.int_cast(val, LONG, false)
  val = b.mul(val, 2.llvm)
  b.push(val)
end

#opt_length(b, oprnd) ⇒ Object



165
166
167
168
169
170
# File 'lib/ruby_vm.rb', line 165

def opt_length(b, oprnd)
  recv  = b.pop
  len = b.alen(recv)
  len = b.num2fix(len)
  b.push(len)
end

#opt_lt(b, oprnd) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/ruby_vm.rb', line 172

def opt_lt(b, oprnd)
  obj = b.pop
  recv = b.pop
  x = b.fix2int(recv)
  y = b.fix2int(obj)
  val = b.icmp_slt(x, y)
  val = b.int_cast(val, LONG, false)
  val = b.mul(val, 2.llvm)
  b.push(val)
end

#opt_minus(b, oprnd) ⇒ Object



136
137
138
139
140
141
# File 'lib/ruby_vm.rb', line 136

def opt_minus(b, oprnd)
  v1 = b.fix2int(b.pop)
  v2 = b.fix2int(b.pop)
  sum = b.sub(v2, v1)
  b.push(b.num2fix(sum))
end

#opt_mult(b, oprnd) ⇒ Object



143
144
145
146
147
148
# File 'lib/ruby_vm.rb', line 143

def opt_mult(b, oprnd)
  v1 = b.fix2int(b.pop)
  v2 = b.fix2int(b.pop)
  mul = b.mul(v1, v2)
  b.push(b.num2fix(mul))
end

#opt_plus(b, oprnd) ⇒ Object



129
130
131
132
133
134
# File 'lib/ruby_vm.rb', line 129

def opt_plus(b, oprnd)
  v1 = b.fix2int(b.pop)
  v2 = b.fix2int(b.pop)
  sum = b.add(v1, v2)
  b.push(b.num2fix(sum))
end

#pop(b, oprnd) ⇒ Object



102
103
104
# File 'lib/ruby_vm.rb', line 102

def pop(b, oprnd)
  b.pop
end

#putnil(b, oprnd) ⇒ Object



90
91
92
# File 'lib/ruby_vm.rb', line 90

def putnil(b, oprnd)
  b.push(nil.immediate)
end

#putobject(b, oprnd) ⇒ Object



98
99
100
# File 'lib/ruby_vm.rb', line 98

def putobject(b, oprnd)
  b.push(oprnd.llvm)
end

#putself(b, oprnd) ⇒ Object



94
95
96
# File 'lib/ruby_vm.rb', line 94

def putself(b, oprnd)
  b.push(get_self)
end

#send(b, oprnd) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/ruby_vm.rb', line 252

def send(b, oprnd)
  recv = nil.immediate
  id = b.call(@rb_funcs[:rb_to_id], :inspect.immediate)
  argc = 0.llvm(Type::Int32Ty)
  val = b.call(@rb_funcs[:rb_funcall2], recv, id, argc, b.stack)
  b.push(val)
end

#setinstancevariable(b, oprnd) ⇒ Object



211
212
213
214
215
# File 'lib/ruby_vm.rb', line 211

def setinstancevariable(b, oprnd)
  new_val = b.peek
  id = b.call(@rb_funcs[:rb_to_id], oprnd.llvm)
  b.call(@rb_funcs[:rb_ivar_set], get_self, id, new_val)
end

#setlocal(b, oprnd) ⇒ Object



117
118
119
120
121
# File 'lib/ruby_vm.rb', line 117

def setlocal(b, oprnd)
  v = b.pop
  local_slot = b.gep(b.locals, oprnd.llvm)
  b.store(v, local_slot)
end

#swap(b, oprn) ⇒ Object



110
111
112
113
114
115
# File 'lib/ruby_vm.rb', line 110

def swap(b, oprn) 
  v1 = b.pop
  v2 = b.pop
  b.push(v1)
  b.push(v2) 
end