Class: Wardite::Op

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, code, operand) ⇒ Op

Returns a new instance of Op.



15
16
17
18
19
# File 'lib/wardite/instruction.rb', line 15

def initialize(namespace, code, operand)
  @namespace = namespace      
  @code = code
  @operand = operand
end

Instance Attribute Details

#codeObject

: Symbol



7
8
9
# File 'lib/wardite/instruction.rb', line 7

def code
  @code
end

#namespaceObject

: Symbol



5
6
7
# File 'lib/wardite/instruction.rb', line 5

def namespace
  @namespace
end

#operandObject

TODO: add types of potential operands



10
11
12
# File 'lib/wardite/instruction.rb', line 10

def operand
  @operand
end

Class Method Details

.i2type(code) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wardite/instruction.rb', line 86

def self.i2type(code)
  case code
  when 0x7f
    :i32
  when 0x7e
    :i64
  when 0x7d
    :f32
  when 0x7c
    :f64
  else
    raise "unknown type code #{code}"
  end
end

.operand_of(code) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wardite/instruction.rb', line 68

def self.operand_of(code)
  case code
  when :local_get, :local_set, :call
    [:u32]
  when :i32_const
    [:i32]
  when :i32_store
    [:u32, :u32]
  when :if
    [:u8_block]
  else
    []
  end
end

.to_sym(chr) ⇒ Object



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
# File 'lib/wardite/instruction.rb', line 23

def self.to_sym(chr)
  code = case chr
    when "\u0004"
      :if
    when "\u000b"
      :end
    when "\u000f"
      :return
    when "\u0010"
      :call
    when "\u0020"
      :local_get
    when "\u0021"
      :local_set
    when "\u0036"
      :i32_store
    when "\u0041"
      :i32_const
    when "\u0048"
      :i32_lts
    when "\u004d"
      :i32_leu
    when "\u006a"
      :i32_add
    when "\u006b"
      :i32_sub
    else
      raise NotImplementedError, "unimplemented: #{"%04x" % chr.ord}"
    end
  # opcodes equal to or larger than are "convert" ops
  if chr.ord >= 0xa7
    return [:convert, code]
  end

  prefix = code.to_s.split("_")[0]
  case prefix
  when "i32", "i64", "f32", "f64"
    [prefix.to_sym, code]
  else
    [:default, code]
  end
end