wag 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitlab-ci.yml +15 -0
- data/.rspec +3 -0
- data/.rubocop.yml +32 -0
- data/.travis.yml +6 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +40 -0
- data/README.md +170 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/wag.rb +42 -0
- data/lib/wag/data.rb +18 -0
- data/lib/wag/element.rb +18 -0
- data/lib/wag/encodable.rb +23 -0
- data/lib/wag/export.rb +39 -0
- data/lib/wag/f32_instructions.rb +21 -0
- data/lib/wag/f64_instructions.rb +21 -0
- data/lib/wag/function.rb +33 -0
- data/lib/wag/function_type.rb +16 -0
- data/lib/wag/global.rb +18 -0
- data/lib/wag/global_instructions.rb +19 -0
- data/lib/wag/i32_instructions.rb +22 -0
- data/lib/wag/i64_instructions.rb +23 -0
- data/lib/wag/import.rb +42 -0
- data/lib/wag/indices.rb +14 -0
- data/lib/wag/indices/base.rb +16 -0
- data/lib/wag/indices/func.rb +8 -0
- data/lib/wag/indices/global.rb +8 -0
- data/lib/wag/indices/label.rb +8 -0
- data/lib/wag/indices/local.rb +8 -0
- data/lib/wag/indices/mem.rb +8 -0
- data/lib/wag/indices/table.rb +8 -0
- data/lib/wag/indices/type.rb +8 -0
- data/lib/wag/inflector.rb +13 -0
- data/lib/wag/instructable.rb +65 -0
- data/lib/wag/instruction.rb +193 -0
- data/lib/wag/instructions/base.rb +24 -0
- data/lib/wag/instructions/block.rb +21 -0
- data/lib/wag/instructions/br.rb +18 -0
- data/lib/wag/instructions/br_if.rb +18 -0
- data/lib/wag/instructions/br_table.rb +18 -0
- data/lib/wag/instructions/call.rb +18 -0
- data/lib/wag/instructions/call_indirect.rb +18 -0
- data/lib/wag/instructions/drop.rb +8 -0
- data/lib/wag/instructions/else.rb +9 -0
- data/lib/wag/instructions/end.rb +8 -0
- data/lib/wag/instructions/f32/abs.rb +11 -0
- data/lib/wag/instructions/f32/add.rb +11 -0
- data/lib/wag/instructions/f32/base.rb +13 -0
- data/lib/wag/instructions/f32/ceil.rb +11 -0
- data/lib/wag/instructions/f32/const.rb +20 -0
- data/lib/wag/instructions/f32/convert_i32_s.rb +11 -0
- data/lib/wag/instructions/f32/convert_i32_u.rb +11 -0
- data/lib/wag/instructions/f32/convert_i64_s.rb +11 -0
- data/lib/wag/instructions/f32/convert_i64_u.rb +11 -0
- data/lib/wag/instructions/f32/copysign.rb +11 -0
- data/lib/wag/instructions/f32/demote_f64.rb +11 -0
- data/lib/wag/instructions/f32/div.rb +11 -0
- data/lib/wag/instructions/f32/eq.rb +11 -0
- data/lib/wag/instructions/f32/floor.rb +11 -0
- data/lib/wag/instructions/f32/ge.rb +11 -0
- data/lib/wag/instructions/f32/gt.rb +11 -0
- data/lib/wag/instructions/f32/le.rb +11 -0
- data/lib/wag/instructions/f32/load.rb +11 -0
- data/lib/wag/instructions/f32/lt.rb +11 -0
- data/lib/wag/instructions/f32/max.rb +11 -0
- data/lib/wag/instructions/f32/min.rb +11 -0
- data/lib/wag/instructions/f32/mul.rb +11 -0
- data/lib/wag/instructions/f32/ne.rb +11 -0
- data/lib/wag/instructions/f32/nearest.rb +11 -0
- data/lib/wag/instructions/f32/neg.rb +11 -0
- data/lib/wag/instructions/f32/reinterpret_i32.rb +11 -0
- data/lib/wag/instructions/f32/sqrt.rb +11 -0
- data/lib/wag/instructions/f32/store.rb +11 -0
- data/lib/wag/instructions/f32/sub.rb +11 -0
- data/lib/wag/instructions/f32/trunc.rb +11 -0
- data/lib/wag/instructions/f64/abs.rb +11 -0
- data/lib/wag/instructions/f64/add.rb +11 -0
- data/lib/wag/instructions/f64/base.rb +13 -0
- data/lib/wag/instructions/f64/ceil.rb +11 -0
- data/lib/wag/instructions/f64/const.rb +20 -0
- data/lib/wag/instructions/f64/convert_i32_s.rb +11 -0
- data/lib/wag/instructions/f64/convert_i32_u.rb +11 -0
- data/lib/wag/instructions/f64/convert_i64_s.rb +11 -0
- data/lib/wag/instructions/f64/convert_i64_u.rb +11 -0
- data/lib/wag/instructions/f64/copysign.rb +11 -0
- data/lib/wag/instructions/f64/div.rb +11 -0
- data/lib/wag/instructions/f64/eq.rb +11 -0
- data/lib/wag/instructions/f64/floor.rb +11 -0
- data/lib/wag/instructions/f64/ge.rb +11 -0
- data/lib/wag/instructions/f64/gt.rb +11 -0
- data/lib/wag/instructions/f64/le.rb +11 -0
- data/lib/wag/instructions/f64/load.rb +11 -0
- data/lib/wag/instructions/f64/lt.rb +11 -0
- data/lib/wag/instructions/f64/max.rb +11 -0
- data/lib/wag/instructions/f64/min.rb +11 -0
- data/lib/wag/instructions/f64/mul.rb +11 -0
- data/lib/wag/instructions/f64/ne.rb +11 -0
- data/lib/wag/instructions/f64/nearest.rb +11 -0
- data/lib/wag/instructions/f64/neg.rb +11 -0
- data/lib/wag/instructions/f64/promote_f32.rb +11 -0
- data/lib/wag/instructions/f64/reinterpret_i64.rb +11 -0
- data/lib/wag/instructions/f64/sqrt.rb +11 -0
- data/lib/wag/instructions/f64/store.rb +11 -0
- data/lib/wag/instructions/f64/sub.rb +11 -0
- data/lib/wag/instructions/f64/trunc.rb +11 -0
- data/lib/wag/instructions/global/base.rb +13 -0
- data/lib/wag/instructions/global/get.rb +20 -0
- data/lib/wag/instructions/global/set.rb +20 -0
- data/lib/wag/instructions/i32/add.rb +11 -0
- data/lib/wag/instructions/i32/and.rb +11 -0
- data/lib/wag/instructions/i32/base.rb +13 -0
- data/lib/wag/instructions/i32/clz.rb +11 -0
- data/lib/wag/instructions/i32/const.rb +20 -0
- data/lib/wag/instructions/i32/ctz.rb +11 -0
- data/lib/wag/instructions/i32/div_s.rb +11 -0
- data/lib/wag/instructions/i32/div_u.rb +11 -0
- data/lib/wag/instructions/i32/eq.rb +11 -0
- data/lib/wag/instructions/i32/eqz.rb +11 -0
- data/lib/wag/instructions/i32/ge_s.rb +11 -0
- data/lib/wag/instructions/i32/ge_u.rb +11 -0
- data/lib/wag/instructions/i32/gt_s.rb +11 -0
- data/lib/wag/instructions/i32/gt_u.rb +11 -0
- data/lib/wag/instructions/i32/le_s.rb +11 -0
- data/lib/wag/instructions/i32/le_u.rb +11 -0
- data/lib/wag/instructions/i32/load.rb +11 -0
- data/lib/wag/instructions/i32/load16_s.rb +11 -0
- data/lib/wag/instructions/i32/load16_u.rb +11 -0
- data/lib/wag/instructions/i32/load8_s.rb +11 -0
- data/lib/wag/instructions/i32/load8_u.rb +11 -0
- data/lib/wag/instructions/i32/lt_s.rb +11 -0
- data/lib/wag/instructions/i32/lt_u.rb +11 -0
- data/lib/wag/instructions/i32/mul.rb +11 -0
- data/lib/wag/instructions/i32/ne.rb +11 -0
- data/lib/wag/instructions/i32/or.rb +11 -0
- data/lib/wag/instructions/i32/popcnt.rb +11 -0
- data/lib/wag/instructions/i32/reinterpret_f32.rb +11 -0
- data/lib/wag/instructions/i32/rem_s.rb +11 -0
- data/lib/wag/instructions/i32/rem_u.rb +11 -0
- data/lib/wag/instructions/i32/rotl.rb +11 -0
- data/lib/wag/instructions/i32/rotr.rb +11 -0
- data/lib/wag/instructions/i32/shl.rb +11 -0
- data/lib/wag/instructions/i32/shr_s.rb +11 -0
- data/lib/wag/instructions/i32/shr_u.rb +11 -0
- data/lib/wag/instructions/i32/store.rb +11 -0
- data/lib/wag/instructions/i32/store16.rb +11 -0
- data/lib/wag/instructions/i32/store8.rb +11 -0
- data/lib/wag/instructions/i32/sub.rb +11 -0
- data/lib/wag/instructions/i32/trunc_f32_s.rb +11 -0
- data/lib/wag/instructions/i32/trunc_f32_u.rb +11 -0
- data/lib/wag/instructions/i32/trunc_f64_s.rb +11 -0
- data/lib/wag/instructions/i32/trunc_f64_u.rb +11 -0
- data/lib/wag/instructions/i32/wrap_i64.rb +11 -0
- data/lib/wag/instructions/i32/xor.rb +11 -0
- data/lib/wag/instructions/i64/add.rb +11 -0
- data/lib/wag/instructions/i64/and.rb +11 -0
- data/lib/wag/instructions/i64/base.rb +13 -0
- data/lib/wag/instructions/i64/clz.rb +11 -0
- data/lib/wag/instructions/i64/const.rb +20 -0
- data/lib/wag/instructions/i64/ctz.rb +11 -0
- data/lib/wag/instructions/i64/div_s.rb +11 -0
- data/lib/wag/instructions/i64/div_u.rb +11 -0
- data/lib/wag/instructions/i64/eq.rb +11 -0
- data/lib/wag/instructions/i64/eqz.rb +11 -0
- data/lib/wag/instructions/i64/extend_i32_s.rb +11 -0
- data/lib/wag/instructions/i64/extend_i32_u.rb +11 -0
- data/lib/wag/instructions/i64/ge_s.rb +11 -0
- data/lib/wag/instructions/i64/ge_u.rb +11 -0
- data/lib/wag/instructions/i64/gt_s.rb +11 -0
- data/lib/wag/instructions/i64/gt_u.rb +11 -0
- data/lib/wag/instructions/i64/le_s.rb +11 -0
- data/lib/wag/instructions/i64/le_u.rb +11 -0
- data/lib/wag/instructions/i64/load.rb +11 -0
- data/lib/wag/instructions/i64/load16_s.rb +11 -0
- data/lib/wag/instructions/i64/load16_u.rb +11 -0
- data/lib/wag/instructions/i64/load32_s.rb +11 -0
- data/lib/wag/instructions/i64/load32_u.rb +11 -0
- data/lib/wag/instructions/i64/load8_s.rb +11 -0
- data/lib/wag/instructions/i64/load8_u.rb +11 -0
- data/lib/wag/instructions/i64/lt_s.rb +11 -0
- data/lib/wag/instructions/i64/lt_u.rb +11 -0
- data/lib/wag/instructions/i64/mul.rb +11 -0
- data/lib/wag/instructions/i64/ne.rb +11 -0
- data/lib/wag/instructions/i64/or.rb +11 -0
- data/lib/wag/instructions/i64/popcnt.rb +11 -0
- data/lib/wag/instructions/i64/reinterpret_f64.rb +11 -0
- data/lib/wag/instructions/i64/rem_s.rb +11 -0
- data/lib/wag/instructions/i64/rem_u.rb +11 -0
- data/lib/wag/instructions/i64/rotl.rb +11 -0
- data/lib/wag/instructions/i64/rotr.rb +11 -0
- data/lib/wag/instructions/i64/shl.rb +11 -0
- data/lib/wag/instructions/i64/shr_s.rb +11 -0
- data/lib/wag/instructions/i64/shr_u.rb +11 -0
- data/lib/wag/instructions/i64/store.rb +11 -0
- data/lib/wag/instructions/i64/store16.rb +11 -0
- data/lib/wag/instructions/i64/store32.rb +11 -0
- data/lib/wag/instructions/i64/store8.rb +11 -0
- data/lib/wag/instructions/i64/sub.rb +11 -0
- data/lib/wag/instructions/i64/trunc_f32_s.rb +11 -0
- data/lib/wag/instructions/i64/trunc_f32_u.rb +11 -0
- data/lib/wag/instructions/i64/trunc_f64_s.rb +11 -0
- data/lib/wag/instructions/i64/trunc_f64_u.rb +11 -0
- data/lib/wag/instructions/i64/xor.rb +11 -0
- data/lib/wag/instructions/if.rb +26 -0
- data/lib/wag/instructions/local/base.rb +13 -0
- data/lib/wag/instructions/local/get.rb +20 -0
- data/lib/wag/instructions/local/set.rb +20 -0
- data/lib/wag/instructions/local/tee.rb +20 -0
- data/lib/wag/instructions/loop.rb +9 -0
- data/lib/wag/instructions/memory/base.rb +13 -0
- data/lib/wag/instructions/memory/grow.rb +11 -0
- data/lib/wag/instructions/memory/size.rb +11 -0
- data/lib/wag/instructions/nop.rb +8 -0
- data/lib/wag/instructions/return.rb +9 -0
- data/lib/wag/instructions/select.rb +9 -0
- data/lib/wag/instructions/unreachable.rb +8 -0
- data/lib/wag/label.rb +32 -0
- data/lib/wag/local.rb +19 -0
- data/lib/wag/local_instructions.rb +19 -0
- data/lib/wag/memory.rb +26 -0
- data/lib/wag/memory_instructions.rb +19 -0
- data/lib/wag/module.rb +100 -0
- data/lib/wag/param.rb +20 -0
- data/lib/wag/result.rb +17 -0
- data/lib/wag/table.rb +17 -0
- data/lib/wag/then.rb +13 -0
- data/lib/wag/type.rb +19 -0
- data/lib/wag/types/base.rb +16 -0
- data/lib/wag/types/f32.rb +8 -0
- data/lib/wag/types/f64.rb +8 -0
- data/lib/wag/types/i32.rb +8 -0
- data/lib/wag/types/i64.rb +8 -0
- data/lib/wag/version.rb +5 -0
- data/lib/wag/wabt.rb +45 -0
- data/lib/wag/wasm.rb +22 -0
- data/lib/wag/wat.rb +18 -0
- data/wag.gemspec +35 -0
- metadata +299 -0
data/lib/wag/data.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Data
|
5
|
+
include WAG::Encodable
|
6
|
+
|
7
|
+
attr_reader :offset, :value
|
8
|
+
|
9
|
+
def initialize(offset, value)
|
10
|
+
@offset = offset.to_i
|
11
|
+
@value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_sexpr
|
15
|
+
[:data, [:"i32.const", offset], value]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/wag/element.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Element
|
5
|
+
include WAG::Encodable
|
6
|
+
|
7
|
+
attr_reader :table_id, :labels
|
8
|
+
|
9
|
+
def initialize(table_id, *labels)
|
10
|
+
@table_id = table_id.to_i
|
11
|
+
@labels = labels.map { |l| WAG::Label.from(l) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_sexpr
|
15
|
+
[:elem, [:"i32.const", table_id]].concat(labels.map(&:to_sexpr))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
# A simple mixin which formats s-expressions as WAT.
|
5
|
+
module Encodable
|
6
|
+
def to_wat
|
7
|
+
WAG::WAT.new(wat_encode(to_sexpr))
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_wasm
|
11
|
+
to_wat.to_wasm
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def wat_encode(data)
|
17
|
+
return "(#{data.map { |e| wat_encode(e) }.join(' ')})" if data.is_a?(Enumerable)
|
18
|
+
return data.inspect if data.is_a?(String)
|
19
|
+
|
20
|
+
data.to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/wag/export.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Export
|
5
|
+
include WAG::Encodable
|
6
|
+
|
7
|
+
attr_reader :name, :desc
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
end
|
12
|
+
|
13
|
+
def func(label, &block)
|
14
|
+
@desc = Function.new(label)
|
15
|
+
@desc.instance_exec(&block) if block
|
16
|
+
@desc
|
17
|
+
end
|
18
|
+
|
19
|
+
def global(label, type)
|
20
|
+
@desc = Global.new(label, type)
|
21
|
+
end
|
22
|
+
|
23
|
+
def memory(number, min = nil, max = nil, &block)
|
24
|
+
@desc = Memory.new(number, min, max)
|
25
|
+
@desc.instance_exec(&block) if block
|
26
|
+
@desc
|
27
|
+
end
|
28
|
+
|
29
|
+
def table(number, type = :anyfunc, &block)
|
30
|
+
@desc = Table.new(number, type)
|
31
|
+
@desc.instance_exec(&block) if block
|
32
|
+
@desc
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_sexpr
|
36
|
+
[:export, name, desc.to_sexpr]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class F32Instructions
|
5
|
+
def initialize(instructions)
|
6
|
+
@instructions = instructions
|
7
|
+
end
|
8
|
+
|
9
|
+
%i[load store const eq ne lt gt le ge abs neg ceil floor trunc nearest
|
10
|
+
sqrt add sub mul div min max copysign convert_i32_s convert_i32_u
|
11
|
+
convert_i64_s convert_i64_u reinterpret_i32].each do |instruction_name|
|
12
|
+
camelised_name = "F32#{WAG::Inflector.inflector.camelize(instruction_name)}"
|
13
|
+
define_method(instruction_name) do |*args, &block|
|
14
|
+
instruction = WAG::Instruction.const_get(camelised_name).new(*args)
|
15
|
+
@instructions << instruction
|
16
|
+
instruction.instance_exec(&block) if block
|
17
|
+
instruction
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class F64Instructions
|
5
|
+
def initialize(instructions)
|
6
|
+
@instructions = instructions
|
7
|
+
end
|
8
|
+
|
9
|
+
%i[load store const eq ne lt gt le ge abs neg ceil floor trunc nearest sqrt
|
10
|
+
add sub mul div min max copysign convert_i32_s convert_i32_u
|
11
|
+
convert_i64_s convert_i64_u promote_f32 reinterpret_i64].each do |instruction_name|
|
12
|
+
camelised_name = "F64#{WAG::Inflector.inflector.camelize(instruction_name)}"
|
13
|
+
define_method(instruction_name) do |*args, &block|
|
14
|
+
instruction = WAG::Instruction.const_get(camelised_name).new(*args)
|
15
|
+
@instructions << instruction
|
16
|
+
instruction.instance_exec(&block) if block
|
17
|
+
instruction
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/wag/function.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Function
|
5
|
+
include WAG::Encodable
|
6
|
+
prepend WAG::Instructable
|
7
|
+
|
8
|
+
attr_reader :label, :params
|
9
|
+
|
10
|
+
def initialize(label = nil)
|
11
|
+
@label = WAG::Label.from(label) if label
|
12
|
+
@params = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def param(label = nil, type)
|
16
|
+
@params.push(WAG::Param.new(type, label))
|
17
|
+
end
|
18
|
+
|
19
|
+
def result(*types)
|
20
|
+
return @result if types.empty?
|
21
|
+
|
22
|
+
@result = WAG::Result.new(*types)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_sexpr
|
26
|
+
[:func].tap do |func|
|
27
|
+
func.push(@label.to_sexpr) if @label
|
28
|
+
func.concat(@params.map(&:to_sexpr)) unless @params.empty?
|
29
|
+
func.push(@result.to_sexpr) if @result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class FunctionType < Function
|
5
|
+
def to_sexpr
|
6
|
+
[:type].tap do |type|
|
7
|
+
type.push(@label.to_sexpr) if @label
|
8
|
+
[:func].tap do |func|
|
9
|
+
func.concat(@params.map(&:to_sexpr)) unless @params.empty?
|
10
|
+
func.push(@result.to_sexpr) if @result
|
11
|
+
type.push(func)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/wag/global.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Global
|
5
|
+
include WAG::Encodable
|
6
|
+
|
7
|
+
attr_reader :label, :type
|
8
|
+
|
9
|
+
def initialize(label, type)
|
10
|
+
@label = WAG::Label.from(label)
|
11
|
+
@type = WAG::Type.from(type)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_sexpr
|
15
|
+
[:global, label.to_sexpr, type.to_sexpr]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class GlobalInstructions
|
5
|
+
def initialize(instructions)
|
6
|
+
@instructions = instructions
|
7
|
+
end
|
8
|
+
|
9
|
+
%i[get set].each do |instruction_name|
|
10
|
+
camelised_name = "Global::#{WAG::Inflector.inflector.camelize(instruction_name)}"
|
11
|
+
define_method(instruction_name) do |*args, &block|
|
12
|
+
instruction = WAG::Instruction.const_get(camelised_name).new(*args)
|
13
|
+
@instructions << instruction
|
14
|
+
instruction.instance_exec(&block) if block
|
15
|
+
instruction
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class I32Instructions
|
5
|
+
def initialize(instructions)
|
6
|
+
@instructions = instructions
|
7
|
+
end
|
8
|
+
|
9
|
+
%i[load load8_s load8_u load16_s load16_u store store8 store16 const eqz eq
|
10
|
+
ne lt_s lt_u gt_s gt_u le_s le_u ge_s ge_u clz ctz popcnt add sub mul
|
11
|
+
div_s div_u rem_s rem_u and or xor shl shr_s shr_u rotl rotr trunc_f32_s
|
12
|
+
trunc_f32_u trunc_f64_s trunc_f64_u reinterpret_f32].each do |instruction_name|
|
13
|
+
camelised_name = "I32::#{WAG::Inflector.inflector.camelize(instruction_name)}"
|
14
|
+
define_method(instruction_name) do |*args, &block|
|
15
|
+
instruction = WAG::Instruction.const_get(camelised_name).new(*args)
|
16
|
+
@instructions << instruction
|
17
|
+
instruction.instance_exec(&block) if block
|
18
|
+
instruction
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class I64Instructions
|
5
|
+
def initialize(instructions)
|
6
|
+
@instructions = instructions
|
7
|
+
end
|
8
|
+
|
9
|
+
%i[load load8_s load8_u load16_s load16_u load32_s load32_u store store8
|
10
|
+
store16 store32 const eqz eq ne lt_s lt_u gt_s gt_u le_s le_u ge_s ge_u
|
11
|
+
clz ctz popcnt add sub mul div_s div_u rem_s rem_u and or xor shl shr_s
|
12
|
+
shr_u rotl rotr extend_i32_s extend_i32_u trunc_f32_s trunc_f32_u
|
13
|
+
trunc_f64_s trunc_f64_u reinterpret_f64].each do |instruction_name|
|
14
|
+
camelised_name = "I64::#{WAG::Inflector.inflector.camelize(instruction_name)}"
|
15
|
+
define_method(instruction_name) do |*args, &block|
|
16
|
+
instruction = WAG::Instruction.const_get(camelised_name).new(*args)
|
17
|
+
@instructions << instruction
|
18
|
+
instruction.instance_exec(&block) if block
|
19
|
+
instruction
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/wag/import.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
class Import
|
5
|
+
include WAG::Encodable
|
6
|
+
|
7
|
+
attr_reader :module_name, :name, :desc
|
8
|
+
|
9
|
+
def initialize(module_name, name)
|
10
|
+
@module_name = module_name
|
11
|
+
@name = name
|
12
|
+
end
|
13
|
+
|
14
|
+
def func(label, &block)
|
15
|
+
@desc = Function.new(label)
|
16
|
+
@desc.instance_exec(&block) if block
|
17
|
+
@desc
|
18
|
+
end
|
19
|
+
|
20
|
+
def global(label, type, &block)
|
21
|
+
@desc = Global.new(label, type)
|
22
|
+
@desc.instance_exec(&block) if block
|
23
|
+
@desc
|
24
|
+
end
|
25
|
+
|
26
|
+
def memory(number, min = nil, max = nil, &block)
|
27
|
+
@desc = Memory.new(number, min, max)
|
28
|
+
@desc.instance_exec(&block) if block
|
29
|
+
@desc
|
30
|
+
end
|
31
|
+
|
32
|
+
def table(number, type = :anyfunc, &block)
|
33
|
+
@desc = Table.new(number, type)
|
34
|
+
@desc.instance_exec(&block) if block
|
35
|
+
@desc
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_sexpr
|
39
|
+
[:import, module_name, name, desc.to_sexpr]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/wag/indices.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
module Indices
|
5
|
+
require 'wag/indices/base'
|
6
|
+
require 'wag/indices/func'
|
7
|
+
require 'wag/indices/global'
|
8
|
+
require 'wag/indices/label'
|
9
|
+
require 'wag/indices/local'
|
10
|
+
require 'wag/indices/mem'
|
11
|
+
require 'wag/indices/table'
|
12
|
+
require 'wag/indices/type'
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WAG
|
4
|
+
module Indices
|
5
|
+
class Base
|
6
|
+
RANGE = [0..0xFFFFFFFF].freeze
|
7
|
+
attr_reader :value
|
8
|
+
|
9
|
+
def initialize(value)
|
10
|
+
raise "Index 0x#{value.to_s(16)} does not fit in an i32" unless RANGE.cover?(value)
|
11
|
+
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|