Class: Wardite::Runtime

Inherits:
Object
  • Object
show all
Includes:
ValueHelper
Defined in:
lib/wardite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Constructor Details

#initialize(inst) ⇒ Runtime

Returns a new instance of Runtime.



157
158
159
160
161
# File 'lib/wardite.rb', line 157

def initialize(inst)
  @stack = []
  @call_stack = []
  @instance = inst
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



525
526
527
528
529
530
531
# File 'lib/wardite.rb', line 525

def method_missing(name, *args)
  if callable? name
    call(name, args)
  else
    super
  end
end

Instance Attribute Details

#call_stackObject

: Array



152
153
154
# File 'lib/wardite.rb', line 152

def call_stack
  @call_stack
end

#instanceObject (readonly)

: Instance



154
155
156
# File 'lib/wardite.rb', line 154

def instance
  @instance
end

#stackObject

TODO: add types of class that the stack accomodates



150
151
152
# File 'lib/wardite.rb', line 150

def stack
  @stack
end

Instance Method Details

#call(name, args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/wardite.rb', line 172

def call(name, args)
  if !callable?(name)
    raise ::NoMethodError, "function #{name} not found"
  end
  kind, fn = @instance.exports[name.to_s]
  if kind != 0
    raise ::NoMethodError, "#{name} is not a function"
  end
  if fn.callsig.size != args.size
    raise ArgumentError, "unmatch arg size"
  end
  args.each_with_index do |arg, idx|
    case fn.callsig[idx]
    when :i32
      raise "type mismatch: i32(#{arg})" unless arg.is_a?(Integer)
      stack.push I32(arg)
    else
      raise "TODO: add me"
    end
  end

  case fn
  when WasmFunction
    invoke_internal(fn)
  when ExternalFunction
    invoke_external(fn)
  else
    raise GenericError, "registered pointer is not to a function"
  end
end

#callable?(name) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/wardite.rb', line 165

def callable?(name)
  !! @instance.exports[name.to_s]
end

#drained_stack(finish) ⇒ Object



513
514
515
516
517
518
519
520
# File 'lib/wardite.rb', line 513

def drained_stack(finish)
  drained = stack[0...finish]
  if ! drained
    $stderr.puts "warning: state of stack: #{stack.inspect}"
    raise EvalError, "stack too short"
  end
  return drained
end

#eval_insn(frame, insn) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/wardite.rb', line 308

def eval_insn(frame, insn)
  case insn.namespace
  when :convert
    return Evaluator.convert_eval_insn(self, frame, insn)
  when :i32
    return Evaluator.i32_eval_insn(self, frame, insn)
  when :i64
    return Evaluator.i64_eval_insn(self, frame, insn)
  when :f32
    return Evaluator.f32_eval_insn(self, frame, insn)
  when :f64
    return Evaluator.f64_eval_insn(self, frame, insn)
  end

  # unmached namespace...
  case insn.code
  when :unreachable
    raise Unreachable, "unreachable op"
  when :nop
    return

  when :if
    block = insn.operand[0]
    raise EvalError, "if op without block" if !block.is_a?(Block)
    cond = stack.pop 
    raise EvalError, "cond not found" if !cond.is_a?(I32)
    next_pc = fetch_ops_while_end(frame.body, frame.pc)
    if cond.value.zero?
      frame.pc = next_pc
    end

    label = Label.new(:if, next_pc, stack.size, block.result_size)
    frame.labels.push(label)

  when :call
    idx = insn.operand[0]
    raise EvalError, "[BUG] local operand not found" if !idx.is_a?(Integer)
    fn = self.instance.store.funcs[idx]
    case fn
    when WasmFunction
      push_frame(fn)
    when ExternalFunction
      ret = invoke_external(fn)
      self.stack.push ret if ret
    else
      raise GenericError, "got a non-function pointer"
    end

  when :return
    old_frame = call_stack.pop
    if !old_frame
      raise EvalError, "maybe empty call stack"
    end

    stack_unwind(old_frame.sp, old_frame.arity)

  when :end
    if old_label = frame.labels.pop
      frame.pc = old_label.pc
      stack_unwind(old_label.sp, old_label.arity)
    else
      old_frame = call_stack.pop
      if !old_frame
        raise EvalError, "maybe empty call stack"
      end
      stack_unwind(old_frame.sp, old_frame.arity)
    end

  when :drop
    stack.pop

  when :select
    cond, right, left = stack.pop, stack.pop, stack.pop
    if !cond.is_a?(I32)
      raise EvalError, "invalid stack for select"
    end
    if !right || !left
      raise EvalError, "stack too short"
    end
    stack.push(cond.value != 0 ? left : right)

  when :local_get
    idx = insn.operand[0]
    if !idx.is_a?(Integer)
      raise EvalError, "[BUG] invalid type of operand"
    end
    local = frame.locals[idx]
    if !local
      raise EvalError, "local not found"
    end
    stack.push(local)

  when :local_set
    idx = insn.operand[0]
    if !idx.is_a?(Integer)
      raise EvalError, "[BUG] invalid type of operand"
    end
    value = stack.pop
    if !value
      raise EvalError, "value should be pushed"
    end
    frame.locals[idx] = value

  when :global_get
    idx = insn.operand[0]
    if !idx.is_a?(Integer)
      raise EvalError, "[BUG] invalid type of operand"
    end
    global = instance.store.globals[idx]
    if !global
      raise EvalError, "global not found"
    end
    stack.push(global.value)

  when :global_set
    idx = insn.operand[0]
    if !idx.is_a?(Integer)
      raise EvalError, "[BUG] invalid type of operand"
    end
    current_global = instance.store.globals[idx]
    if !current_global
      raise EvalError, "global index not valid"
    end
    if !current_global.mutable?
      raise EvalError, "global not mutable"
    end
    value = stack.pop
    if !value
      raise EvalError, "value should be pushed"
    end
    current_global.value = value
    instance.store.globals[idx] = current_global

  when :memory_size
    memory = instance.store.memories[0] || raise("[BUG] no memory")
    stack.push(I32(memory.current))

  when :memory_grow
    delta = stack.pop
    if !delta.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
    memory = instance.store.memories[0] || raise("[BUG] no memory")
    stack.push(I32(memory.grow(delta.value)))

  else
    raise "TODO! unsupported #{insn.inspect}"
  end

rescue => e
  require "pp"
  $stderr.puts "frame:::\n#{frame.pretty_inspect}"
  $stderr.puts "stack:::\n#{stack.pretty_inspect}"
  raise e
end

#execute!Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/wardite.rb', line 290

def execute!
  loop do
    cur_frame = self.call_stack.last #: Frame
    if !cur_frame
      break
    end
    cur_frame.pc += 1
    insn = cur_frame.body[cur_frame.pc]
    if !insn
      break
    end
    eval_insn(cur_frame, insn)
  end
end

#fetch_ops_while_end(ops, pc_start) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/wardite.rb', line 467

def fetch_ops_while_end(ops, pc_start)
  cursor = pc_start
  depth = 0
  loop {
    cursor += 1
    inst = ops[cursor]
    case inst&.code
    when nil
      raise EvalError, "end op not found"
    when :i
      depth += 1
    when :end
      if depth == 0
        return cursor
      else
        depth -= 1
      end
    else
      # nop
    end
  }
  raise "[BUG] unreachable"
end

#invoke_external(external_function) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/wardite.rb', line 253

def invoke_external(external_function)
  local_start = stack.size - external_function.callsig.size
  args = stack[local_start..]
  if !args
    raise LoadError, "stack too short"
  end
  self.stack = drained_stack(local_start)

  proc = external_function.callable
  val = proc[self.instance.store, args]
  if !val
    return
  end

  case val
  when I32, I64, F32, F64
    return val
  when Integer
    case external_function.retsig[0]
    when :i32
      return I32(val)
    when :i64
      return I64(val)
    end
  when Float
    case external_function.retsig[0]
    when :f32
      return F32(val)
    when :f64
      return F64(val)
    end
  end

  raise "invalid type of value returned in proc. val: #{val.inspect}"
end

#invoke_internal(wasm_function) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/wardite.rb', line 232

def invoke_internal(wasm_function)
  arity = wasm_function.retsig.size
  push_frame(wasm_function)
  execute!

  if arity > 0
    if arity > 1
      raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;"
    end
    if self.stack.empty?
      raise "[BUG] stack empry"
    end
    v = self.stack.pop
    return v
  end

  return nil
end

#push_frame(wasm_function) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/wardite.rb', line 205

def push_frame(wasm_function)
  local_start = stack.size - wasm_function.callsig.size
  locals = stack[local_start..]
  if !locals
    raise LoadError, "stack too short"
  end
  self.stack = drained_stack(local_start)

  wasm_function.locals_type.each_with_index do |typ, i|
    case typ
    when :i32, :u32
      locals.push I32(0)
    when :i64, :u64
      locals.push I64(0)
    else
      $stderr.puts "warning: unknown type #{typ.inspect}. default to I32"
      locals.push I32(0)
    end
  end

  arity = wasm_function.retsig.size
  frame = Frame.new(-1, stack.size, wasm_function.body, arity, locals)
  self.call_stack.push(frame)
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


535
536
537
# File 'lib/wardite.rb', line 535

def respond_to? name
  callable?(name) || super
end

#stack_unwind(sp, arity) ⇒ Object

unwind the stack and put return value if exists



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/wardite.rb', line 495

def stack_unwind(sp, arity)
  if arity > 0
    if arity > 1
      raise ::NotImplementedError, "return artiy >= 2 not yet supported ;;"
    end
    value = stack.pop
    if !value
      raise EvalError, "cannot obtain return value"
    end
    self.stack = drained_stack(sp)
    stack.push value
  else
    self.stack = drained_stack(sp)
  end
end