wardite 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,314 @@
1
+ (module
2
+ ;; Export function that performs basic 4-operation arithmetic loop 10000 times
3
+ (func $arithmetic_loop (export "arithmetic_loop") (result i32)
4
+ (local $i i32) ;; Loop counter
5
+ (local $a i32) ;; Arithmetic variable a
6
+ (local $b i32) ;; Arithmetic variable b
7
+ (local $result i32) ;; Result storage
8
+
9
+ ;; Initialize values
10
+ i32.const 0
11
+ local.set $i ;; i = 0
12
+
13
+ i32.const 10
14
+ local.set $a ;; a = 10
15
+
16
+ i32.const 3
17
+ local.set $b ;; b = 3
18
+
19
+ i32.const 0
20
+ local.set $result ;; result = 0
21
+
22
+ ;; Start loop
23
+ (loop $main_loop
24
+ ;; Addition: result += a + b
25
+ local.get $result
26
+ local.get $a
27
+ local.get $b
28
+ i32.add
29
+ i32.add
30
+ local.set $result
31
+
32
+ ;; Subtraction: result += a - b
33
+ local.get $result
34
+ local.get $a
35
+ local.get $b
36
+ i32.sub
37
+ i32.add
38
+ local.set $result
39
+
40
+ ;; Multiplication: result += a * b
41
+ local.get $result
42
+ local.get $a
43
+ local.get $b
44
+ i32.mul
45
+ i32.add
46
+ local.set $result
47
+
48
+ ;; Division: result += a / b
49
+ local.get $result
50
+ local.get $a
51
+ local.get $b
52
+ i32.div_s
53
+ i32.add
54
+ local.set $result
55
+
56
+ ;; Increment counter
57
+ local.get $i
58
+ i32.const 1
59
+ i32.add
60
+ local.set $i
61
+
62
+ ;; Check loop continuation condition (i < 100000)
63
+ local.get $i
64
+ i32.const 100000
65
+ i32.lt_s
66
+ br_if $main_loop
67
+ )
68
+
69
+ ;; Return result
70
+ local.get $result
71
+ )
72
+
73
+ ;; Detailed 4-operation arithmetic function (execute each operation individually)
74
+ (func $detailed_arithmetic (export "detailed_arithmetic") (param $x i32) (param $y i32) (result i32)
75
+ (local $sum i32)
76
+ (local $diff i32)
77
+ (local $product i32)
78
+ (local $quotient i32)
79
+ (local $final_result i32)
80
+
81
+ ;; Addition
82
+ local.get $x
83
+ local.get $y
84
+ i32.add
85
+ local.set $sum
86
+
87
+ ;; Subtraction
88
+ local.get $x
89
+ local.get $y
90
+ i32.sub
91
+ local.set $diff
92
+
93
+ ;; Multiplication
94
+ local.get $x
95
+ local.get $y
96
+ i32.mul
97
+ local.set $product
98
+
99
+ ;; Division (with zero division check)
100
+ local.get $y
101
+ i32.const 0
102
+ i32.eq
103
+ (if
104
+ (then
105
+ i32.const 0
106
+ local.set $quotient
107
+ )
108
+ (else
109
+ local.get $x
110
+ local.get $y
111
+ i32.div_s
112
+ local.set $quotient
113
+ )
114
+ )
115
+
116
+ ;; Sum all results
117
+ local.get $sum
118
+ local.get $diff
119
+ i32.add
120
+ local.get $product
121
+ i32.add
122
+ local.get $quotient
123
+ i32.add
124
+ local.set $final_result
125
+
126
+ local.get $final_result
127
+ )
128
+
129
+ ;; Execute detailed_arithmetic with changing numbers 10000 times
130
+ (func $detailed_arithmetic_loop (export "detailed_arithmetic_loop") (result i32)
131
+ (local $i i32) ;; Loop counter
132
+ (local $x i32) ;; First argument
133
+ (local $y i32) ;; Second argument
134
+ (local $total_result i32) ;; Accumulated result
135
+ (local $current_result i32) ;; Current arithmetic result
136
+
137
+ ;; Initialize values
138
+ i32.const 0
139
+ local.set $i ;; i = 0
140
+
141
+ i32.const 5
142
+ local.set $x ;; x = 5 (initial value)
143
+
144
+ i32.const 2
145
+ local.set $y ;; y = 2 (initial value)
146
+
147
+ i32.const 0
148
+ local.set $total_result ;; total_result = 0
149
+
150
+ ;; Start loop
151
+ (loop $detailed_loop
152
+ ;; Call detailed_arithmetic function
153
+ local.get $x
154
+ local.get $y
155
+ call $detailed_arithmetic
156
+ local.set $current_result
157
+
158
+ ;; Accumulate result
159
+ local.get $total_result
160
+ local.get $current_result
161
+ i32.add
162
+ local.set $total_result
163
+
164
+ ;; Change x and y values (add variation)
165
+ ;; x = x + 1
166
+ local.get $x
167
+ i32.const 1
168
+ i32.add
169
+ local.set $x
170
+
171
+ ;; y = (y * 2) % 17 + 1 (cycle in range 1-17, avoid zero division)
172
+ local.get $y
173
+ i32.const 2
174
+ i32.mul
175
+ i32.const 17
176
+ i32.rem_s
177
+ i32.const 1
178
+ i32.add
179
+ local.set $y
180
+
181
+ ;; Limit x to prevent overflow (overflow protection)
182
+ local.get $x
183
+ i32.const 1000
184
+ i32.gt_s
185
+ (if
186
+ (then
187
+ i32.const 1
188
+ local.set $x
189
+ )
190
+ )
191
+
192
+ ;; Increment counter
193
+ local.get $i
194
+ i32.const 1
195
+ i32.add
196
+ local.set $i
197
+
198
+ ;; Check loop continuation condition (i < 100000)
199
+ local.get $i
200
+ i32.const 100000
201
+ i32.lt_s
202
+ br_if $detailed_loop
203
+ )
204
+
205
+ ;; Return accumulated result
206
+ local.get $total_result
207
+ )
208
+
209
+ ;; Version with more complex pattern for number changes
210
+ (func $detailed_arithmetic_complex_loop (export "detailed_arithmetic_complex_loop") (result i32)
211
+ (local $i i32) ;; Loop counter
212
+ (local $x i32) ;; First argument
213
+ (local $y i32) ;; Second argument
214
+ (local $total_result i32) ;; Accumulated result
215
+ (local $current_result i32) ;; Current arithmetic result
216
+
217
+ ;; Initialize values
218
+ i32.const 0
219
+ local.set $i ;; i = 0
220
+
221
+ i32.const 10
222
+ local.set $x ;; x = 10 (initial value)
223
+
224
+ i32.const 3
225
+ local.set $y ;; y = 3 (initial value)
226
+
227
+ i32.const 0
228
+ local.set $total_result ;; total_result = 0
229
+
230
+ ;; Start loop
231
+ (loop $complex_loop
232
+ ;; Call detailed_arithmetic function
233
+ local.get $x
234
+ local.get $y
235
+ call $detailed_arithmetic
236
+ local.set $current_result
237
+
238
+ ;; Accumulate result
239
+ local.get $total_result
240
+ local.get $current_result
241
+ i32.add
242
+ local.set $total_result
243
+
244
+ ;; Change numbers with more complex pattern
245
+ ;; x = (x + i) % 100 + 1 (range 1-100)
246
+ local.get $x
247
+ local.get $i
248
+ i32.add
249
+ i32.const 100
250
+ i32.rem_s
251
+ i32.const 1
252
+ i32.add
253
+ local.set $x
254
+
255
+ ;; y = (i * 3 + 7) % 13 + 1 (range 1-13, avoid zero division)
256
+ local.get $i
257
+ i32.const 3
258
+ i32.mul
259
+ i32.const 7
260
+ i32.add
261
+ i32.const 13
262
+ i32.rem_s
263
+ i32.const 1
264
+ i32.add
265
+ local.set $y
266
+
267
+ ;; Increment counter
268
+ local.get $i
269
+ i32.const 1
270
+ i32.add
271
+ local.set $i
272
+
273
+ ;; Check loop continuation condition (i < 100000)
274
+ local.get $i
275
+ i32.const 100000
276
+ i32.lt_s
277
+ br_if $complex_loop
278
+ )
279
+
280
+ ;; Return accumulated result
281
+ local.get $total_result
282
+ )
283
+
284
+ ;; Floating-point 4-operation arithmetic (f32)
285
+ (func $float_arithmetic (export "float_arithmetic") (param $x f32) (param $y f32) (result f32)
286
+ (local $result f32)
287
+
288
+ ;; Addition
289
+ local.get $x
290
+ local.get $y
291
+ f32.add
292
+
293
+ ;; Add subtraction
294
+ local.get $x
295
+ local.get $y
296
+ f32.sub
297
+ f32.add
298
+
299
+ ;; Add multiplication
300
+ local.get $x
301
+ local.get $y
302
+ f32.mul
303
+ f32.add
304
+
305
+ ;; Add division
306
+ local.get $x
307
+ local.get $y
308
+ f32.div
309
+ f32.add
310
+
311
+ local.set $result
312
+ local.get $result
313
+ )
314
+ )
@@ -0,0 +1,24 @@
1
+ require "wardite"
2
+ require "optparse"
3
+ require "ostruct"
4
+
5
+ $options = OpenStruct.new
6
+
7
+ opt = OptionParser.new
8
+ opt.on('--wasm-file [FILE]') {|v| $options.wasm_file = v }
9
+ opt.parse!
10
+
11
+ f = File.open($options.wasm_file)
12
+
13
+ require "vernier"
14
+ RubyVM::YJIT.enable
15
+ puts "YJIT enabled: #{RubyVM::YJIT.enabled?}"
16
+
17
+ Vernier.profile(out: "./tmp/load_perf.json") do
18
+ start = Time.now
19
+ _instance = Wardite::BinaryLoader::load_from_buffer(f);
20
+ puts "Profile saved to ./tmp/load_perf.json"
21
+ puts "Load time: #{Time.now.to_f - start.to_f} seconds"
22
+ end
23
+
24
+ p "OK"
@@ -5,14 +5,15 @@ module Wardite
5
5
  module Evaluator
6
6
  # @rbs runtime: Runtime
7
7
  # @rbs frame: Frame
8
- # @rbs insn: Op
9
- # @rbs return: void
10
- def self.f32_eval_insn(runtime, frame, insn)
11
- case insn.code
8
+ # @rbs code: Symbol
9
+ # @rbs operand: Array[operandItem]
10
+ # @rbs return: bool?
11
+ def self.f32_eval_insn(runtime, frame, code, operand)
12
+ case code
12
13
 
13
14
  when :f32_load
14
- _align = insn.operand[0] # TODO: alignment support?
15
- offset = insn.operand[1]
15
+ _align = operand[0] # TODO: alignment support?
16
+ offset = operand[1]
16
17
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
17
18
 
18
19
  addr = runtime.stack.pop
@@ -31,8 +32,8 @@ module Wardite
31
32
 
32
33
 
33
34
  when :f32_store
34
- _align = insn.operand[0] # TODO: alignment support?
35
- offset = insn.operand[1]
35
+ _align = operand[0] # TODO: alignment support?
36
+ offset = operand[1]
36
37
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
37
38
 
38
39
  value = runtime.stack.pop
@@ -48,7 +49,7 @@ module Wardite
48
49
 
49
50
 
50
51
  when :f32_const
51
- const = insn.operand[0]
52
+ const = operand[0]
52
53
  if !const.is_a?(Float)
53
54
  raise EvalError, "invalid type of operand"
54
55
  end
@@ -243,8 +244,9 @@ module Wardite
243
244
 
244
245
 
245
246
  else
246
- raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
247
+ return
247
248
  end
249
+ return true
248
250
  end
249
251
  end
250
252
  end
@@ -5,14 +5,15 @@ module Wardite
5
5
  module Evaluator
6
6
  # @rbs runtime: Runtime
7
7
  # @rbs frame: Frame
8
- # @rbs insn: Op
9
- # @rbs return: void
10
- def self.f64_eval_insn(runtime, frame, insn)
11
- case insn.code
8
+ # @rbs code: Symbol
9
+ # @rbs operand: Array[operandItem]
10
+ # @rbs return: bool?
11
+ def self.f64_eval_insn(runtime, frame, code, operand)
12
+ case code
12
13
 
13
14
  when :f64_load
14
- _align = insn.operand[0] # TODO: alignment support?
15
- offset = insn.operand[1]
15
+ _align = operand[0] # TODO: alignment support?
16
+ offset = operand[1]
16
17
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
17
18
 
18
19
  addr = runtime.stack.pop
@@ -31,8 +32,8 @@ module Wardite
31
32
 
32
33
 
33
34
  when :f64_store
34
- _align = insn.operand[0] # TODO: alignment support?
35
- offset = insn.operand[1]
35
+ _align = operand[0] # TODO: alignment support?
36
+ offset = operand[1]
36
37
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
37
38
 
38
39
  value = runtime.stack.pop
@@ -48,7 +49,7 @@ module Wardite
48
49
 
49
50
 
50
51
  when :f64_const
51
- const = insn.operand[0]
52
+ const = operand[0]
52
53
  if !const.is_a?(Float)
53
54
  raise EvalError, "invalid type of operand"
54
55
  end
@@ -243,8 +244,9 @@ module Wardite
243
244
 
244
245
 
245
246
  else
246
- raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
247
+ return
247
248
  end
249
+ return true
248
250
  end
249
251
  end
250
252
  end
@@ -5,14 +5,15 @@ module Wardite
5
5
  module Evaluator
6
6
  # @rbs runtime: Runtime
7
7
  # @rbs frame: Frame
8
- # @rbs insn: Op
9
- # @rbs return: void
10
- def self.i32_eval_insn(runtime, frame, insn)
11
- case insn.code
8
+ # @rbs code: Symbol
9
+ # @rbs operand: Array[operandItem]
10
+ # @rbs return: bool?
11
+ def self.i32_eval_insn(runtime, frame, code, operand)
12
+ case code
12
13
 
13
14
  when :i32_load
14
- _align = insn.operand[0] # TODO: alignment support?
15
- offset = insn.operand[1]
15
+ _align = operand[0] # TODO: alignment support?
16
+ offset = operand[1]
16
17
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
17
18
 
18
19
  addr = runtime.stack.pop
@@ -31,8 +32,8 @@ module Wardite
31
32
 
32
33
 
33
34
  when :i32_load8_s
34
- _align = insn.operand[0] # TODO: alignment support?
35
- offset = insn.operand[1]
35
+ _align = operand[0] # TODO: alignment support?
36
+ offset = operand[1]
36
37
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
37
38
 
38
39
  addr = runtime.stack.pop
@@ -51,8 +52,8 @@ module Wardite
51
52
 
52
53
 
53
54
  when :i32_load8_u
54
- _align = insn.operand[0] # TODO: alignment support?
55
- offset = insn.operand[1]
55
+ _align = operand[0] # TODO: alignment support?
56
+ offset = operand[1]
56
57
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
57
58
 
58
59
  addr = runtime.stack.pop
@@ -71,8 +72,8 @@ module Wardite
71
72
 
72
73
 
73
74
  when :i32_load16_s
74
- _align = insn.operand[0] # TODO: alignment support?
75
- offset = insn.operand[1]
75
+ _align = operand[0] # TODO: alignment support?
76
+ offset = operand[1]
76
77
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
77
78
 
78
79
  addr = runtime.stack.pop
@@ -91,8 +92,8 @@ module Wardite
91
92
 
92
93
 
93
94
  when :i32_load16_u
94
- _align = insn.operand[0] # TODO: alignment support?
95
- offset = insn.operand[1]
95
+ _align = operand[0] # TODO: alignment support?
96
+ offset = operand[1]
96
97
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
97
98
 
98
99
  addr = runtime.stack.pop
@@ -111,8 +112,8 @@ module Wardite
111
112
 
112
113
 
113
114
  when :i32_store
114
- _align = insn.operand[0] # TODO: alignment support?
115
- offset = insn.operand[1]
115
+ _align = operand[0] # TODO: alignment support?
116
+ offset = operand[1]
116
117
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
117
118
 
118
119
  value = runtime.stack.pop
@@ -128,8 +129,8 @@ module Wardite
128
129
 
129
130
 
130
131
  when :i32_store8
131
- _align = insn.operand[0] # TODO: alignment support?
132
- offset = insn.operand[1]
132
+ _align = operand[0] # TODO: alignment support?
133
+ offset = operand[1]
133
134
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
134
135
 
135
136
  value = runtime.stack.pop
@@ -145,8 +146,8 @@ module Wardite
145
146
 
146
147
 
147
148
  when :i32_store16
148
- _align = insn.operand[0] # TODO: alignment support?
149
- offset = insn.operand[1]
149
+ _align = operand[0] # TODO: alignment support?
150
+ offset = operand[1]
150
151
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
151
152
 
152
153
  value = runtime.stack.pop
@@ -162,7 +163,7 @@ module Wardite
162
163
 
163
164
 
164
165
  when :i32_const
165
- const = insn.operand[0]
166
+ const = operand[0]
166
167
  if !const.is_a?(Integer)
167
168
  raise EvalError, "invalid type of operand"
168
169
  end
@@ -464,8 +465,9 @@ module Wardite
464
465
 
465
466
 
466
467
  else
467
- raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
468
+ return
468
469
  end
470
+ return true
469
471
  end
470
472
  end
471
473
  end
@@ -5,14 +5,15 @@ module Wardite
5
5
  module Evaluator
6
6
  # @rbs runtime: Runtime
7
7
  # @rbs frame: Frame
8
- # @rbs insn: Op
9
- # @rbs return: void
10
- def self.i64_eval_insn(runtime, frame, insn)
11
- case insn.code
8
+ # @rbs code: Symbol
9
+ # @rbs operand: Array[operandItem]
10
+ # @rbs return: bool?
11
+ def self.i64_eval_insn(runtime, frame, code, operand)
12
+ case code
12
13
 
13
14
  when :i64_load
14
- _align = insn.operand[0] # TODO: alignment support?
15
- offset = insn.operand[1]
15
+ _align = operand[0] # TODO: alignment support?
16
+ offset = operand[1]
16
17
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
17
18
 
18
19
  addr = runtime.stack.pop
@@ -31,8 +32,8 @@ module Wardite
31
32
 
32
33
 
33
34
  when :i64_load8_s
34
- _align = insn.operand[0] # TODO: alignment support?
35
- offset = insn.operand[1]
35
+ _align = operand[0] # TODO: alignment support?
36
+ offset = operand[1]
36
37
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
37
38
 
38
39
  addr = runtime.stack.pop
@@ -51,8 +52,8 @@ module Wardite
51
52
 
52
53
 
53
54
  when :i64_load8_u
54
- _align = insn.operand[0] # TODO: alignment support?
55
- offset = insn.operand[1]
55
+ _align = operand[0] # TODO: alignment support?
56
+ offset = operand[1]
56
57
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
57
58
 
58
59
  addr = runtime.stack.pop
@@ -71,8 +72,8 @@ module Wardite
71
72
 
72
73
 
73
74
  when :i64_load16_s
74
- _align = insn.operand[0] # TODO: alignment support?
75
- offset = insn.operand[1]
75
+ _align = operand[0] # TODO: alignment support?
76
+ offset = operand[1]
76
77
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
77
78
 
78
79
  addr = runtime.stack.pop
@@ -91,8 +92,8 @@ module Wardite
91
92
 
92
93
 
93
94
  when :i64_load16_u
94
- _align = insn.operand[0] # TODO: alignment support?
95
- offset = insn.operand[1]
95
+ _align = operand[0] # TODO: alignment support?
96
+ offset = operand[1]
96
97
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
97
98
 
98
99
  addr = runtime.stack.pop
@@ -111,8 +112,8 @@ module Wardite
111
112
 
112
113
 
113
114
  when :i64_load32_s
114
- _align = insn.operand[0] # TODO: alignment support?
115
- offset = insn.operand[1]
115
+ _align = operand[0] # TODO: alignment support?
116
+ offset = operand[1]
116
117
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
117
118
 
118
119
  addr = runtime.stack.pop
@@ -131,8 +132,8 @@ module Wardite
131
132
 
132
133
 
133
134
  when :i64_load32_u
134
- _align = insn.operand[0] # TODO: alignment support?
135
- offset = insn.operand[1]
135
+ _align = operand[0] # TODO: alignment support?
136
+ offset = operand[1]
136
137
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
137
138
 
138
139
  addr = runtime.stack.pop
@@ -151,8 +152,8 @@ module Wardite
151
152
 
152
153
 
153
154
  when :i64_store
154
- _align = insn.operand[0] # TODO: alignment support?
155
- offset = insn.operand[1]
155
+ _align = operand[0] # TODO: alignment support?
156
+ offset = operand[1]
156
157
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
157
158
 
158
159
  value = runtime.stack.pop
@@ -168,8 +169,8 @@ module Wardite
168
169
 
169
170
 
170
171
  when :i64_store8
171
- _align = insn.operand[0] # TODO: alignment support?
172
- offset = insn.operand[1]
172
+ _align = operand[0] # TODO: alignment support?
173
+ offset = operand[1]
173
174
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
174
175
 
175
176
  value = runtime.stack.pop
@@ -185,8 +186,8 @@ module Wardite
185
186
 
186
187
 
187
188
  when :i64_store16
188
- _align = insn.operand[0] # TODO: alignment support?
189
- offset = insn.operand[1]
189
+ _align = operand[0] # TODO: alignment support?
190
+ offset = operand[1]
190
191
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
191
192
 
192
193
  value = runtime.stack.pop
@@ -202,8 +203,8 @@ module Wardite
202
203
 
203
204
 
204
205
  when :i64_store32
205
- _align = insn.operand[0] # TODO: alignment support?
206
- offset = insn.operand[1]
206
+ _align = operand[0] # TODO: alignment support?
207
+ offset = operand[1]
207
208
  raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
208
209
 
209
210
  value = runtime.stack.pop
@@ -219,7 +220,7 @@ module Wardite
219
220
 
220
221
 
221
222
  when :i64_const
222
- const = insn.operand[0]
223
+ const = operand[0]
223
224
  if !const.is_a?(Integer)
224
225
  raise EvalError, "invalid type of operand"
225
226
  end
@@ -521,8 +522,9 @@ module Wardite
521
522
 
522
523
 
523
524
  else
524
- raise "Unknown opcode for namespace #{insn.namespace}: #{insn.code}"
525
+ return
525
526
  end
527
+ return true
526
528
  end
527
529
  end
528
530
  end