Module: Wardite::Evaluator

Extended by:
ValueHelper
Defined in:
lib/wardite.rb,
lib/wardite/alu_f32.generated.rb,
lib/wardite/alu_f64.generated.rb,
lib/wardite/alu_i32.generated.rb,
lib/wardite/alu_i64.generated.rb,
lib/wardite/convert.generated.rb

Class Method Summary collapse

Methods included from ValueHelper

F32, F64, I32, I64

Class Method Details

.convert_eval_insn(runtime, frame, code, operand) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
# File 'lib/wardite/convert.generated.rb', line 11

def self.convert_eval_insn(runtime, frame, code, operand)
  case code

  when :i32_wrap_i64
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.wrap(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_f32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_s(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_f64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_s(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_f32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_u(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_f64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_u(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_reinterpret_f32
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.reinterpret(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_extend8_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extendN_s(to: :i32, from: :i8)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_extend16_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extendN_s(to: :i32, from: :i16)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_sat_f32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_sat_s(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_sat_f64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_sat_s(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_sat_f32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_sat_u(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i32_trunc_sat_f64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_sat_u(to: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I32)
    runtime.stack.push(to)


  when :i64_extend_i32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extend_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend_i64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.extend_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend_i32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extend_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend_i64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.extend_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_f32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_f64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_f32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_f64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_reinterpret_f64
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.reinterpret(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend8_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extendN_s(to: :i64, from: :i8)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend16_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extendN_s(to: :i64, from: :i16)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_extend32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.extendN_s(to: :i64, from: :i32)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_sat_f32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_sat_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_sat_f64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_sat_s(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_sat_f32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.trunc_sat_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :i64_trunc_sat_f64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.trunc_sat_u(to: :i64)
    raise EvalError, "failed to convert type" if !to.is_a?(I64)
    runtime.stack.push(to)


  when :f32_convert_i32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.convert_s(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f32_convert_i64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.convert_s(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f32_convert_i32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.convert_u(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f32_convert_i64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.convert_u(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f32_demote_f64
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F64)
    to = from.demote(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f32_reinterpret_i32
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.reinterpret(to: :f32)
    raise EvalError, "failed to convert type" if !to.is_a?(F32)
    runtime.stack.push(to)


  when :f64_convert_i32_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.convert_s(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  when :f64_convert_i64_s
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.convert_s(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  when :f64_convert_i32_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I32)
    to = from.convert_u(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  when :f64_convert_i64_u
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.convert_u(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  when :f64_promote_f32
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(F32)
    to = from.promote(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  when :f64_reinterpret_i64
    from = runtime.stack.pop
    raise EvalError, "maybe empty or invalid stack" if !from.is_a?(I64)
    to = from.reinterpret(to: :f64)
    raise EvalError, "failed to convert type" if !to.is_a?(F64)
    runtime.stack.push(to)


  else
    return
  end
  return true
end

.f32_eval_insn(runtime, frame, code, operand) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/wardite/alu_f32.generated.rb', line 11

def self.f32_eval_insn(runtime, frame, code, operand)
  case code

  when :f32_load
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + F32.new.memsize / 8
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(F32.from_bytes(buf))


  when :f32_store
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(F32) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + value.packed.size
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed


  when :f32_const
    const = operand[0]
    if !const.is_a?(Float)
      raise EvalError, "invalid type of operand"
    end
    runtime.stack.push(F32(const))


  when :f32_eqz
    target = runtime.stack.pop
    if !target.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = target.value.zero? ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_eq
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value == right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_ne
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value != right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_lt
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value < right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_gt
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value > right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_le
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value <= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_ge
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value >= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f32_abs
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value.abs))


  when :f32_neg
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(-(x.value)))


  when :f32_ceil
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value.ceil.to_f))


  when :f32_floor
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value.floor.to_f))


  when :f32_trunc
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value.to_i.to_f))


  when :f32_nearest
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value.round.to_f))


  when :f32_sqrt
    x = runtime.stack.pop
    if !x.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(x.value ** 0.5))


  when :f32_add
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(left.value + right.value))


  when :f32_sub
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(left.value - right.value))


  when :f32_mul
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(left.value * right.value))


  when :f32_div
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F32(left.value / right.value))


  when :f32_min
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    if right.value.nan? || left.value.nan?
      runtime.stack.push(F32(Float::NAN))
      return
    end
    runtime.stack.push(F32([left.value, right.value].min))


  when :f32_max
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    if right.value.nan? || left.value.nan?
      runtime.stack.push(F32(Float::NAN))
      return
    end
    runtime.stack.push(F32([left.value, right.value].max))


  when :f32_copysign
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F32) || !left.is_a?(F32)
      raise EvalError, "maybe empty or invalid stack"
    end
    if left.sign == right.sign
      runtime.stack.push(F32(left.value))
    else
      runtime.stack.push(F32(-left.value))
    end


  else
    return
  end
  return true
end

.f64_eval_insn(runtime, frame, code, operand) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/wardite/alu_f64.generated.rb', line 11

def self.f64_eval_insn(runtime, frame, code, operand)
  case code

  when :f64_load
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + F64.new.memsize / 8
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(F64.from_bytes(buf))


  when :f64_store
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(F64) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + value.packed.size
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed


  when :f64_const
    const = operand[0]
    if !const.is_a?(Float)
      raise EvalError, "invalid type of operand"
    end
    runtime.stack.push(F64(const))


  when :f64_eqz
    target = runtime.stack.pop
    if !target.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = target.value.zero? ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_eq
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value == right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_ne
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value != right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_lt
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value < right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_gt
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value > right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_le
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value <= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_ge
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value >= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :f64_abs
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value.abs))


  when :f64_neg
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(-(x.value)))


  when :f64_ceil
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value.ceil.to_f))


  when :f64_floor
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value.floor.to_f))


  when :f64_trunc
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value.to_i.to_f))


  when :f64_nearest
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value.round.to_f))


  when :f64_sqrt
    x = runtime.stack.pop
    if !x.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(x.value ** 0.5))


  when :f64_add
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(left.value + right.value))


  when :f64_sub
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(left.value - right.value))


  when :f64_mul
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(left.value * right.value))


  when :f64_div
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(F64(left.value / right.value))


  when :f64_min
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    if right.value.nan? || left.value.nan?
      runtime.stack.push(F64(Float::NAN))
      return
    end
    runtime.stack.push(F64([left.value, right.value].min))


  when :f64_max
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    if right.value.nan? || left.value.nan?
      runtime.stack.push(F64(Float::NAN))
      return
    end
    runtime.stack.push(F64([left.value, right.value].max))


  when :f64_copysign
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(F64) || !left.is_a?(F64)
      raise EvalError, "maybe empty or invalid stack"
    end
    if left.sign == right.sign
      runtime.stack.push(F64(left.value))
    else
      runtime.stack.push(F64(-left.value))
    end


  else
    return
  end
  return true
end

.i32_eval_insn(runtime, frame, code, operand) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
463
464
465
466
467
468
469
470
471
# File 'lib/wardite/alu_i32.generated.rb', line 11

def self.i32_eval_insn(runtime, frame, code, operand)
  case code

  when :i32_load
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + I32.new.memsize / 8
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I32.from_bytes(buf))


  when :i32_load8_s
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I32.from_bytes(buf, size: 8, signed: true))


  when :i32_load8_u
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I32.from_bytes(buf, size: 8, signed: false))


  when :i32_load16_s
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I32.from_bytes(buf, size: 16, signed: true))


  when :i32_load16_u
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I32.from_bytes(buf, size: 16, signed: false))


  when :i32_store
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I32) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + value.packed.size
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed


  when :i32_store8
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I32) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed(size: 8)


  when :i32_store16
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I32) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed(size: 16)


  when :i32_const
    const = operand[0]
    if !const.is_a?(Integer)
      raise EvalError, "invalid type of operand"
    end
    runtime.stack.push(I32(const))


  when :i32_eqz
    target = runtime.stack.pop
    if !target.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = target.value.zero? ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_eq
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value == right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_ne
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value != right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_lts
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s < right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_ltu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value < right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_gts
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s > right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_gtu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value > right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_les
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s <= right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_leu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value <= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_ges
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s >= right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_geu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value >= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i32_clz
    target = runtime.stack.pop
    if !target.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    start = target.memsize - 1
    count = 0
    while start > -1
      if (target.value >> start).zero?
        count += 1
        start -= 1
      else
        break
      end
    end
    runtime.stack.push(I32(count))


  when :i32_ctz
    target = runtime.stack.pop
    if !target.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    finish = target.memsize
    count = 0
    while count < finish
      if (target.value & (1 << count)).zero?
        count += 1
      else
        break
      end
    end
    runtime.stack.push(I32(count))


  when :i32_popcnt
    target = runtime.stack.pop
    if !target.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    digits = target.memsize
    count = 0
    digits.times do |i|
      if (target.value & (1 << i)).nonzero?
        count += 1
      end
    end
    runtime.stack.push(I32(count))


  when :i32_add
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value + right.value))


  when :i32_sub
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value - right.value))


  when :i32_mul
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value * right.value))


  when :i32_div_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    result = left.value_s / right.value_s.to_f
    iresult = (result >= 0 ? result.floor : result.ceil).to_i
    if iresult >= (1 << (left.memsize - 1))
      raise IntegerOverflow, "integer overflow"
    end
  
    runtime.stack.push(I32(iresult))


  when :i32_div_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value / right.value))


  when :i32_rem_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    result = left.value_s % right.value_s
    if result > 0 && left.value_s < 0
      result = result - right.value_s
    elsif result < 0 && left.value_s > 0
      result = result - right.value_s
    end
    runtime.stack.push(I32(result))


  when :i32_rem_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value % right.value))


  when :i32_and
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value & right.value))


  when :i32_or
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value | right.value))


  when :i32_xor
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I32(left.value ^ right.value))


  when :i32_shl
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value << (right.value % right.memsize)
    value %= 1 << right.memsize
  
    runtime.stack.push(I32(value))


  when :i32_shr_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value_s >> (right.value % right.memsize)
    runtime.stack.push(I32(value))


  when :i32_shr_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value >> (right.value % right.memsize)
    runtime.stack.push(I32(value))


  when :i32_rotl
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    k = (right.value % right.memsize)
    rotated = left.value << k
    rest = left.value & (I32::I32_MAX << (right.memsize - k))
    value = rotated | (rest >> (right.memsize - k))
    runtime.stack.push(I32(value))


  when :i32_rotr
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I32) || !left.is_a?(I32)
      raise EvalError, "maybe empty or invalid stack"
    end
    k = (right.value % right.memsize)
    rotated = left.value >> k
    rest = left.value & (I32::I32_MAX >> (right.memsize - k))
    value = rotated | (rest << (right.memsize - k))
    runtime.stack.push(I32(value))


  else
    return
  end
  return true
end

.i64_eval_insn(runtime, frame, code, operand) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/wardite/alu_i64.generated.rb', line 11

def self.i64_eval_insn(runtime, frame, code, operand)
  case code

  when :i64_load
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + I64.new.memsize / 8
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf))


  when :i64_load8_s
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 8, signed: true))


  when :i64_load8_u
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 8, signed: false))


  when :i64_load16_s
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 16, signed: true))


  when :i64_load16_u
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 16, signed: false))


  when :i64_load32_s
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 4
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 32, signed: true))


  when :i64_load32_u
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    addr = runtime.stack.pop
    if !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 4
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    buf = memory.data[at...data_end]
    if !buf
      raise EvalError, "invalid memory range"
    end
    runtime.stack.push(I64.from_bytes(buf, size: 32, signed: false))


  when :i64_store
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I64) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + value.packed.size
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed


  when :i64_store8
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I64) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 1
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed(size: 8)


  when :i64_store16
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I64) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 2
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed(size: 16)


  when :i64_store32
    _align = operand[0] # TODO: alignment support?
    offset = operand[1]
    raise EvalError, "[BUG] invalid type of operand" if !offset.is_a?(Integer)
  
    value = runtime.stack.pop
    addr = runtime.stack.pop
    if !value.is_a?(I64) || !addr.is_a?(I32)
      raise EvalError, "maybe stack too short"
    end
  
    at = addr.value + offset
    data_end = at + 4
    memory = runtime.instance.store.memories[0] || raise("[BUG] no memory")
    memory.data[at...data_end] = value.packed(size: 32)


  when :i64_const
    const = operand[0]
    if !const.is_a?(Integer)
      raise EvalError, "invalid type of operand"
    end
    runtime.stack.push(I64(const))


  when :i64_eqz
    target = runtime.stack.pop
    if !target.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = target.value.zero? ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_eq
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value == right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_ne
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value != right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_lts
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s < right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_ltu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value < right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_gts
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s > right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_gtu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value > right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_les
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s <= right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_leu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value <= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_ges
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value_s >= right.value_s) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_geu
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = (left.value >= right.value) ? 1 : 0
    runtime.stack.push(I32(value))


  when :i64_clz
    target = runtime.stack.pop
    if !target.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    start = target.memsize - 1
    count = 0
    while start > -1
      if (target.value >> start).zero?
        count += 1
        start -= 1
      else
        break
      end
    end
    runtime.stack.push(I64(count))


  when :i64_ctz
    target = runtime.stack.pop
    if !target.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    finish = target.memsize
    count = 0
    while count < finish
      if (target.value & (1 << count)).zero?
        count += 1
      else
        break
      end
    end
    runtime.stack.push(I64(count))


  when :i64_popcnt
    target = runtime.stack.pop
    if !target.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    digits = target.memsize
    count = 0
    digits.times do |i|
      if (target.value & (1 << i)).nonzero?
        count += 1
      end
    end
    runtime.stack.push(I64(count))


  when :i64_add
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value + right.value))


  when :i64_sub
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value - right.value))


  when :i64_mul
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value * right.value))


  when :i64_div_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    result = left.value_s / right.value_s.to_f
    iresult = (result >= 0 ? result.floor : result.ceil).to_i
    if iresult >= (1 << (left.memsize - 1))
      raise IntegerOverflow, "integer overflow"
    end
  
    runtime.stack.push(I64(iresult))


  when :i64_div_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value / right.value))


  when :i64_rem_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    result = left.value_s % right.value_s
    if result > 0 && left.value_s < 0
      result = result - right.value_s
    elsif result < 0 && left.value_s > 0
      result = result - right.value_s
    end
    runtime.stack.push(I64(result))


  when :i64_rem_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value % right.value))


  when :i64_and
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value & right.value))


  when :i64_or
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value | right.value))


  when :i64_xor
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    runtime.stack.push(I64(left.value ^ right.value))


  when :i64_shl
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value << (right.value % right.memsize)
    value %= 1 << right.memsize
  
    runtime.stack.push(I64(value))


  when :i64_shr_s
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value_s >> (right.value % right.memsize)
    runtime.stack.push(I64(value))


  when :i64_shr_u
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    value = left.value >> (right.value % right.memsize)
    runtime.stack.push(I64(value))


  when :i64_rotl
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    k = (right.value % right.memsize)
    rotated = left.value << k
    rest = left.value & (I64::I64_MAX << (right.memsize - k))
    value = rotated | (rest >> (right.memsize - k))
    runtime.stack.push(I64(value))


  when :i64_rotr
    right, left = runtime.stack.pop, runtime.stack.pop
    if !right.is_a?(I64) || !left.is_a?(I64)
      raise EvalError, "maybe empty or invalid stack"
    end
    k = (right.value % right.memsize)
    rotated = left.value >> k
    rest = left.value & (I64::I64_MAX >> (right.memsize - k))
    value = rotated | (rest << (right.memsize - k))
    runtime.stack.push(I64(value))


  else
    return
  end
  return true
end