wardite 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,76 @@
1
+ require "stringio"
2
+
3
+ module GenConv
4
+ def self.execute(path, defined_ops: {})
5
+ parent_dir = File.dirname(path)
6
+ system "mkdir -p #{parent_dir}"
7
+
8
+ basic_module = File.read(
9
+ File.expand_path("../templates/conv_module.rb.tmpl", __FILE__)
10
+ )
11
+
12
+ ope_defs = generate_ops(defined_ops: defined_ops)
13
+ basic_module.gsub!(/\$\{DEFS\}/, ope_defs)
14
+
15
+ dest = File.open(path, "w")
16
+ dest.puts basic_module
17
+
18
+ $stderr.puts "generated: #{path}"
19
+ end
20
+
21
+ def self.generate_ops(defined_ops:)
22
+ result = StringIO.new("")
23
+ defined_ops.each_pair do |to, ops|
24
+ ops.each_pair do |op, argtypes|
25
+ argtypes.each do |from|
26
+ code = generate_one_branch(op: op, to: to, from: from)
27
+ result << "\n"
28
+ code.each_line do |ln|
29
+ result << " " * 6 << ln
30
+ end
31
+ result << "\n"
32
+ end
33
+ end
34
+ end
35
+ result.string
36
+ end
37
+
38
+ def self.generate_one_branch(op:, to:, from:)
39
+ code = DEF.dup
40
+ method = op.to_s
41
+ symbol = "#{to.to_s}_#{method}_#{from.to_sym}"
42
+
43
+ if method == "extend"
44
+ method = "extend_"
45
+ elsif method.end_with?("_s") or method.end_with?("_u")
46
+ core, suffix = *(method.split("_")[0..1])
47
+ symbol = "#{to.to_s}_#{core}_#{from.to_sym}_#{suffix}"
48
+ end
49
+ # NOTE to is as namespace
50
+ code.gsub!(/\$\{SYMBOL\}/, symbol)
51
+ code.gsub!(/\$\{METHOD\}/, method)
52
+ code.gsub!(/\$\{TO\}/, to.to_s)
53
+ code.gsub!(/\$\{TO_CLASS\}/, to_class(to.to_sym))
54
+ code.gsub!(/\$\{FROM_CLASS\}/, to_class(from.to_sym))
55
+ return code
56
+ end
57
+
58
+ def self.to_class(typ)
59
+ {
60
+ i32: "I32",
61
+ i64: "I64",
62
+ f32: "F32",
63
+ f64: "F64",
64
+ }[typ]
65
+ end
66
+
67
+ # ope_templates
68
+ DEF = <<~RUBY
69
+ when :${SYMBOL}
70
+ from = runtime.stack.pop
71
+ raise EvalError, "maybe empty or invalid stack" if !from.is_a?(${FROM_CLASS})
72
+ to = from.${METHOD}(to: :${TO})
73
+ raise EvalError, "failed to convert type" if !to.is_a?(${TO_CLASS})
74
+ runtime.stack.push(to)
75
+ RUBY
76
+ end
@@ -0,0 +1,18 @@
1
+ # rbs_inline: enabled
2
+ require_relative "value"
3
+
4
+ module Wardite
5
+ module Evaluator
6
+ # @rbs runtime: Runtime
7
+ # @rbs frame: Frame
8
+ # @rbs insn: Op
9
+ # @rbs return: void
10
+ def self.convert_eval_insn(runtime, frame, insn)
11
+ case insn.code
12
+ ${DEFS}
13
+ else
14
+ raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ # Generated from lib/wardite/alu_f32.generated.rb with RBS::Inline
2
+
3
+ module Wardite
4
+ module Evaluator
5
+ # @rbs runtime: Runtime
6
+ # @rbs frame: Frame
7
+ # @rbs insn: Op
8
+ # @rbs return: void
9
+ def self.f32_eval_insn: (Runtime runtime, Frame frame, Op insn) -> void
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # Generated from lib/wardite/alu_f64.generated.rb with RBS::Inline
2
+
3
+ module Wardite
4
+ module Evaluator
5
+ # @rbs runtime: Runtime
6
+ # @rbs frame: Frame
7
+ # @rbs insn: Op
8
+ # @rbs return: void
9
+ def self.f64_eval_insn: (Runtime runtime, Frame frame, Op insn) -> void
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # Generated from lib/wardite/alu_i64.generated.rb with RBS::Inline
2
+
3
+ module Wardite
4
+ module Evaluator
5
+ # @rbs runtime: Runtime
6
+ # @rbs frame: Frame
7
+ # @rbs insn: Op
8
+ # @rbs return: void
9
+ def self.i64_eval_insn: (Runtime runtime, Frame frame, Op insn) -> void
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # Generated from lib/wardite/convert.generated.rb with RBS::Inline
2
+
3
+ module Wardite
4
+ module Evaluator
5
+ # @rbs runtime: Runtime
6
+ # @rbs frame: Frame
7
+ # @rbs insn: Op
8
+ # @rbs return: void
9
+ def self.convert_eval_insn: (Runtime runtime, Frame frame, Op insn) -> void
10
+ end
11
+ end
@@ -2,6 +2,12 @@
2
2
 
3
3
  module Wardite
4
4
  class Op
5
+ # @see https://pengowray.github.io/wasm-ops/
6
+ SYMS: Array[Symbol]
7
+
8
+ # @rbs return: Hash[Integer, Symbol]
9
+ def self.table: () -> Hash[Integer, Symbol]
10
+
5
11
  attr_accessor namespace: Symbol
6
12
 
7
13
  attr_accessor code: Symbol
@@ -1,58 +1,302 @@
1
1
  # Generated from lib/wardite/value.rb with RBS::Inline
2
2
 
3
3
  module Wardite
4
+ module ValueHelper
5
+ # @rbs value: Integer
6
+ # @rbs return: I32
7
+ def I32: (Integer value) -> I32
8
+
9
+ # @rbs value: Integer
10
+ # @rbs return: I64
11
+ def I64: (Integer value) -> I64
12
+
13
+ # @rbs value: Float
14
+ # @rbs return: F32
15
+ def F32: (Float value) -> F32
16
+
17
+ # @rbs value: Float
18
+ # @rbs return: F64
19
+ def F64: (Float value) -> F64
20
+
21
+ private
22
+
23
+ # @rbs value: Integer
24
+ # @rbs return: Integer
25
+ def as_u32: (Integer value) -> Integer
26
+
27
+ # @rbs value: Integer
28
+ # @rbs return: Integer
29
+ def as_u64: (Integer value) -> Integer
30
+ end
31
+
32
+ extend ValueHelper
33
+
4
34
  class I32
35
+ include ValueHelper
36
+
37
+ I32_MAX: untyped
38
+
39
+ # value should be stored as unsigned Integer, even in I32/I64
40
+ # when we want to access signed value, it'd be done via #value_s
5
41
  attr_accessor value: Integer
6
42
 
43
+ # @rbs str: String
44
+ # @rbs size: Integer|nil
45
+ # @rbs signed: bool
46
+ # @rbs return: I32
47
+ def self.from_bytes: (String str, ?size: Integer | nil, ?signed: bool) -> I32
48
+
49
+ # @rbs return: Integer
50
+ def memsize: () -> Integer
51
+
52
+ # returns a value interpreted as signed integer
53
+ # @rbs return: Integer
54
+ def value_s: () -> Integer
55
+
7
56
  # TODO: eliminate use of pack, to support mruby - in this file!
57
+ # @rbs size: Integer|nil
8
58
  # @rbs return: String
9
- def packed: () -> String
59
+ def packed: (?size: Integer | nil) -> String
60
+
61
+ # @rbs to: Symbol
62
+ # @rbs return: I32|I64|F32|F64
63
+ def wrap: (to: Symbol) -> (I32 | I64 | F32 | F64)
64
+
65
+ # @rbs to: Symbol
66
+ # @rbs return: I32|I64|F32|F64
67
+ def extend_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
68
+
69
+ # @rbs to: Symbol
70
+ # @rbs return: I32|I64|F32|F64
71
+ def extend_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
72
+
73
+ # @rbs to: Symbol
74
+ # @rbs return: I32|I64|F32|F64
75
+ def trunc_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
76
+
77
+ # @rbs to: Symbol
78
+ # @rbs return: I32|I64|F32|F64
79
+ def trunc_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
80
+
81
+ # @rbs to: Symbol
82
+ # @rbs return: I32|I64|F32|F64
83
+ def convert_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
84
+
85
+ # @rbs to: Symbol
86
+ # @rbs return: I32|I64|F32|F64
87
+ def convert_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
10
88
 
89
+ # @rbs to: Symbol
90
+ # @rbs return: I32|I64|F32|F64
91
+ def demote: (to: Symbol) -> (I32 | I64 | F32 | F64)
92
+
93
+ # @rbs to: Symbol
94
+ # @rbs return: I32|I64|F32|F64
95
+ def promote: (to: Symbol) -> (I32 | I64 | F32 | F64)
96
+
97
+ # @rbs to: Symbol
98
+ # @rbs return: I32|I64|F32|F64
99
+ def reinterpret: (to: Symbol) -> (I32 | I64 | F32 | F64)
100
+
101
+ # I32#inspect shows signed value for convinience
11
102
  def inspect: () -> untyped
12
103
  end
13
104
 
14
105
  class I64
106
+ include ValueHelper
107
+
108
+ I64_MAX: untyped
109
+
15
110
  attr_accessor value: Integer
16
111
 
112
+ # @rbs str: String
113
+ # @rbs size: Integer|nil
114
+ # @rbs signed: bool
115
+ # @rbs return: I64
116
+ def self.from_bytes: (String str, ?size: Integer | nil, ?signed: bool) -> I64
117
+
118
+ # @rbs return: Integer
119
+ def memsize: () -> Integer
120
+
121
+ # returns a value interpreted as signed integer
122
+ # @rbs return: Integer
123
+ def value_s: () -> Integer
124
+
125
+ # @rbs size: Integer|nil
17
126
  # @rbs return: String
18
- def packed: () -> String
127
+ def packed: (?size: Integer | nil) -> String
128
+
129
+ # @rbs to: Symbol
130
+ # @rbs return: I32|I64|F32|F64
131
+ def wrap: (to: Symbol) -> (I32 | I64 | F32 | F64)
132
+
133
+ # @rbs to: Symbol
134
+ # @rbs return: I32|I64|F32|F64
135
+ def extend_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
136
+
137
+ # @rbs to: Symbol
138
+ # @rbs return: I32|I64|F32|F64
139
+ def extend_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
19
140
 
141
+ # @rbs to: Symbol
142
+ # @rbs return: I32|I64|F32|F64
143
+ def trunc_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
144
+
145
+ # @rbs to: Symbol
146
+ # @rbs return: I32|I64|F32|F64
147
+ def trunc_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
148
+
149
+ # @rbs to: Symbol
150
+ # @rbs return: I32|I64|F32|F64
151
+ def convert_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
152
+
153
+ # @rbs to: Symbol
154
+ # @rbs return: I32|I64|F32|F64
155
+ def convert_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
156
+
157
+ # @rbs to: Symbol
158
+ # @rbs return: I32|I64|F32|F64
159
+ def demote: (to: Symbol) -> (I32 | I64 | F32 | F64)
160
+
161
+ # @rbs to: Symbol
162
+ # @rbs return: I32|I64|F32|F64
163
+ def promote: (to: Symbol) -> (I32 | I64 | F32 | F64)
164
+
165
+ # @rbs to: Symbol
166
+ # @rbs return: I32|I64|F32|F64
167
+ def reinterpret: (to: Symbol) -> (I32 | I64 | F32 | F64)
168
+
169
+ # I64#inspect shows signed value
20
170
  def inspect: () -> untyped
21
171
  end
22
172
 
23
173
  class F32
174
+ include ValueHelper
175
+
24
176
  attr_accessor value: Float
25
177
 
178
+ # @rbs str: String
179
+ # @rbs return: F32
180
+ def self.from_bytes: (String str) -> F32
181
+
182
+ # @rbs return: Integer
183
+ def memsize: () -> Integer
184
+
185
+ # @rbs return: :positive|:negative
186
+ def sign: () -> (:positive | :negative)
187
+
188
+ # @rbs size: Integer|nil
26
189
  # @rbs return: String
27
- def packed: () -> String
190
+ def packed: (?size: Integer | nil) -> String
191
+
192
+ # @rbs to: Symbol
193
+ # @rbs return: I32|I64|F32|F64
194
+ def wrap: (to: Symbol) -> (I32 | I64 | F32 | F64)
195
+
196
+ # @rbs to: Symbol
197
+ # @rbs return: I32|I64|F32|F64
198
+ def extend_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
199
+
200
+ # @rbs to: Symbol
201
+ # @rbs return: I32|I64|F32|F64
202
+ def extend_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
203
+
204
+ # @todo need more testcase...
205
+ # @see https://webassembly.github.io/spec/core/exec/numerics.html#xref-exec-numerics-op-trunc-s-mathrm-trunc-mathsf-s-m-n-z
206
+ # @rbs to: Symbol
207
+ # @rbs return: I32|I64|F32|F64
208
+ def trunc_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
209
+
210
+ # @see https://webassembly.github.io/spec/core/exec/numerics.html#xref-exec-numerics-op-trunc-u-mathrm-trunc-mathsf-u-m-n-z
211
+ # @rbs to: Symbol
212
+ # @rbs return: I32|I64|F32|F64
213
+ def trunc_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
214
+
215
+ # @rbs to: Symbol
216
+ # @rbs return: I32|I64|F32|F64
217
+ def convert_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
218
+
219
+ # @rbs to: Symbol
220
+ # @rbs return: I32|I64|F32|F64
221
+ def convert_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
222
+
223
+ # @rbs to: Symbol
224
+ # @rbs return: I32|I64|F32|F64
225
+ def demote: (to: Symbol) -> (I32 | I64 | F32 | F64)
226
+
227
+ # @rbs to: Symbol
228
+ # @rbs return: I32|I64|F32|F64
229
+ def promote: (to: Symbol) -> (I32 | I64 | F32 | F64)
230
+
231
+ # @rbs to: Symbol
232
+ # @rbs return: I32|I64|F32|F64
233
+ def reinterpret: (to: Symbol) -> (I32 | I64 | F32 | F64)
28
234
 
29
235
  def inspect: () -> untyped
30
236
  end
31
237
 
32
238
  class F64
239
+ include ValueHelper
240
+
33
241
  attr_accessor value: Float
34
242
 
243
+ # @rbs str: String
244
+ # @rbs return: F64
245
+ def self.from_bytes: (String str) -> F64
246
+
247
+ # @rbs return: Integer
248
+ def memsize: () -> Integer
249
+
250
+ # @rbs return: :positive|:negative
251
+ def sign: () -> (:positive | :negative)
252
+
253
+ # @rbs size: Integer|nil
35
254
  # @rbs return: String
36
- def packed: () -> String
255
+ def packed: (?size: Integer | nil) -> String
37
256
 
38
- def inspect: () -> untyped
39
- end
257
+ # @rbs to: Symbol
258
+ # @rbs return: I32|I64|F32|F64
259
+ def wrap: (to: Symbol) -> (I32 | I64 | F32 | F64)
40
260
 
41
- module ValueHelper
42
- # @rbs value: Integer
43
- # @rbs return: I32
44
- def I32: (Integer value) -> I32
261
+ # @rbs to: Symbol
262
+ # @rbs return: I32|I64|F32|F64
263
+ def extend_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
45
264
 
46
- # @rbs value: Integer
47
- # @rbs return: I64
48
- def I64: (Integer value) -> I64
265
+ # @rbs to: Symbol
266
+ # @rbs return: I32|I64|F32|F64
267
+ def extend_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
49
268
 
50
- # @rbs value: Float
51
- # @rbs return: F32
52
- def F32: (Float value) -> F32
269
+ # @see the same as F32
270
+ # @rbs to: Symbol
271
+ # @rbs return: I32|I64|F32|F64
272
+ def trunc_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
53
273
 
54
- # @rbs value: Float
55
- # @rbs return: F64
56
- def F64: (Float value) -> F64
274
+ # @see the same as F32
275
+ # @rbs to: Symbol
276
+ # @rbs return: I32|I64|F32|F64
277
+ def trunc_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
278
+
279
+ # @rbs to: Symbol
280
+ # @rbs return: I32|I64|F32|F64
281
+ def convert_s: (to: Symbol) -> (I32 | I64 | F32 | F64)
282
+
283
+ # @rbs to: Symbol
284
+ # @rbs return: I32|I64|F32|F64
285
+ def convert_u: (to: Symbol) -> (I32 | I64 | F32 | F64)
286
+
287
+ # @todo no loss of digits...
288
+ # @rbs to: Symbol
289
+ # @rbs return: I32|I64|F32|F64
290
+ def demote: (to: Symbol) -> (I32 | I64 | F32 | F64)
291
+
292
+ # @rbs to: Symbol
293
+ # @rbs return: I32|I64|F32|F64
294
+ def promote: (to: Symbol) -> (I32 | I64 | F32 | F64)
295
+
296
+ # @rbs to: Symbol
297
+ # @rbs return: I32|I64|F32|F64
298
+ def reinterpret: (to: Symbol) -> (I32 | I64 | F32 | F64)
299
+
300
+ def inspect: () -> untyped
57
301
  end
58
302
  end
@@ -215,7 +215,7 @@ module Wardite
215
215
  include ValueHelper
216
216
 
217
217
  # TODO: add types of class that the stack accomodates
218
- attr_accessor stack: Array[I32 | I64 | F32 | F64 | Block]
218
+ attr_accessor stack: Array[I32 | I64 | F32 | F64]
219
219
 
220
220
  attr_accessor call_stack: Array[Frame]
221
221
 
@@ -265,8 +265,8 @@ module Wardite
265
265
  def stack_unwind: (Integer sp, Integer arity) -> void
266
266
 
267
267
  # @rbs finish: Integer
268
- # @rbs return: Array[I32|I64|F32|F64|Block]
269
- def drained_stack: (Integer finish) -> Array[I32 | I64 | F32 | F64 | Block]
268
+ # @rbs return: Array[I32|I64|F32|F64]
269
+ def drained_stack: (Integer finish) -> Array[I32 | I64 | F32 | F64]
270
270
 
271
271
  # @rbs name: Symbol
272
272
  # @rbs args: Array[Object]
@@ -333,6 +333,8 @@ module Wardite
333
333
  class Memory
334
334
  attr_accessor data: String
335
335
 
336
+ attr_accessor current: Integer
337
+
336
338
  attr_accessor max: Integer | nil
337
339
 
338
340
  # @rbs min: Integer
@@ -340,6 +342,10 @@ module Wardite
340
342
  # @rbs return: void
341
343
  def initialize: (Integer min, Integer | nil max) -> void
342
344
 
345
+ # @rbs delta: Integer
346
+ # @rbs return: Integer
347
+ def grow: (Integer delta) -> Integer
348
+
343
349
  def inspect: () -> untyped
344
350
  end
345
351
 
@@ -437,6 +443,9 @@ module Wardite
437
443
  class EvalError < StandardError
438
444
  end
439
445
 
446
+ class Unreachable < StandardError
447
+ end
448
+
440
449
  # @rbs path: String|nil
441
450
  # @rbs buffer: File|StringIO|nil
442
451
  # @rbs **options: Hash[Symbol, Object]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wardite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio Kondo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-02 00:00:00.000000000 Z
11
+ date: 2024-11-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A pure-ruby webassembly runtime
14
14
  email:
@@ -25,6 +25,7 @@ files:
25
25
  - examples/add.wasm
26
26
  - examples/add.wat
27
27
  - examples/call.wat
28
+ - examples/consts.wat
28
29
  - examples/fib.wat
29
30
  - examples/fib2.wat
30
31
  - examples/helloworld.wat
@@ -34,18 +35,28 @@ files:
34
35
  - examples/memory.wat
35
36
  - exe/wardite
36
37
  - lib/wardite.rb
38
+ - lib/wardite/alu_f32.generated.rb
39
+ - lib/wardite/alu_f64.generated.rb
37
40
  - lib/wardite/alu_i32.generated.rb
41
+ - lib/wardite/alu_i64.generated.rb
38
42
  - lib/wardite/const.rb
43
+ - lib/wardite/convert.generated.rb
39
44
  - lib/wardite/instruction.rb
40
45
  - lib/wardite/leb128.rb
41
46
  - lib/wardite/value.rb
42
47
  - lib/wardite/version.rb
43
48
  - lib/wardite/wasi.rb
44
49
  - scripts/gen_alu.rb
50
+ - scripts/gen_conv.rb
45
51
  - scripts/templates/alu_module.rb.tmpl
52
+ - scripts/templates/conv_module.rb.tmpl
46
53
  - sig/generated/wardite.rbs
54
+ - sig/generated/wardite/alu_f32.generated.rbs
55
+ - sig/generated/wardite/alu_f64.generated.rbs
47
56
  - sig/generated/wardite/alu_i32.generated.rbs
57
+ - sig/generated/wardite/alu_i64.generated.rbs
48
58
  - sig/generated/wardite/const.rbs
59
+ - sig/generated/wardite/convert.generated.rbs
49
60
  - sig/generated/wardite/instruction.rbs
50
61
  - sig/generated/wardite/leb128.rbs
51
62
  - sig/generated/wardite/value.rbs