-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstructions.cpp
More file actions
1279 lines (1203 loc) Β· 37 KB
/
instructions.cpp
File metadata and controls
1279 lines (1203 loc) Β· 37 KB
1
2
3
4
5
6
7
8
9
10
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "instructions.h"
#include <cmath>
#include <cstring>
#include "../Memory/mem.h"
#include "../Utils/macros.h"
#include "../Utils/util.h"
#include "../WARDuino/CallbackHandler.h"
// performs proxy calls to an MCU
bool proxy_call(Module *m, uint32_t fidx) {
dbg_info("Remote Function Call %d\n", fidx);
ProxySupervisor *supervisor = m->warduino->debugger->supervisor;
RFC *rfc;
Type *type = m->functions[fidx].type;
if (type->param_count > 0) {
m->sp -= type->param_count;
StackValue *args = &m->stack[m->sp + 1];
rfc = new RFC(m, fidx, type, args);
} else {
rfc = new RFC(m, fidx, type);
}
if (!supervisor->call(rfc)) {
dbg_info(": FAILED TO SEND\n", fidx);
return false;
}
if (!rfc->success) {
// TODO exception bugger might be too small and msg not null terminated?
memcpy(&exception, rfc->exception, strlen(rfc->exception));
return false;
}
if (rfc->type->result_count > 0) {
m->stack[++m->sp] = *rfc->result;
}
return true;
}
/*
* WebAssembly Instructions
*
* i_instr_**** functions
*
* Functions here are called from a dispatching loop
* in WARDuino.cpp.
*
* The given module `m` is the currently executing module
*
* Returning false breaks this loop and marks the
* execution as crashed. The exception variable can
* be filled with an explanation.
*
* Returning true continues the loop with the
* notable exception of end which can terminate
* the program successfully by setting program_done
*
*/
/*
Control Instructions :=
| nop
| unreachable
| block resulttype instr* end
| loop resulttype instr* end
| if resulttype instr* else instr* end
| br labelidx
| br_if labelidx
| br_table vec(labelidx) labelidx
| return
| call funcidx
| call_indirect typeidx
*/
/*
Instruction: call funcidx
Webassembly Description:
1. Let πΉ be the current frame.
2. Assert: due to validation, πΉ.module.funcaddrs[π₯] exists.
3. Let π be the function address πΉ.module.funcaddrs[π₯].
4. Invoke the function instance at address π.
Formal specification:
πΉ.module.funcaddrs[π₯] = π
-----------------------------------
πΉ; (call π₯) -> πΉ; (invoke π)
*/
/**
* 0x02
*/
bool i_instr_block(Module *m, uint8_t *block_ptr) {
read_LEB_32(&m->pc_ptr); // ignore block type
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
auto block_itr = m->block_lookup.find(block_ptr);
ASSERT(block_itr != m->block_lookup.end(), "could not find block");
m->warduino->interpreter->push_block(m, block_itr->second, m->sp);
return true;
}
/**
* 0x03
*/
bool i_instr_loop(Module *m, uint8_t *block_ptr) {
read_LEB_32(&m->pc_ptr); // ignore block type
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
m->warduino->interpreter->push_block(m, m->block_lookup[block_ptr], m->sp);
return true;
}
/**
* 0x04 if
*/
bool i_instr_if(Module *m, uint8_t *block_ptr) {
read_LEB_32(&m->pc_ptr); // ignore block type
Block *block = m->block_lookup[block_ptr];
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
m->warduino->interpreter->push_block(m, block, m->sp);
uint32_t cond = m->stack[m->sp--].value.uint32;
if (cond == 0) { // if false (I32)
// branch to else block or after end of if
if (block->else_ptr == nullptr) {
// no else block, pop if block and skip end
m->csp -= 1;
m->pc_ptr = block->br_ptr + 1;
} else {
m->pc_ptr = block->else_ptr;
}
}
// if true, keep going
#if TRACE
debug(" - cond: 0x%x jump to 0x%x, block: %s\n", cond,
(uint32_t)(m->pc_ptr - m->bytes), block_repr(block));
#endif
return true;
}
/**
* 0x05 else
*/
bool i_instr_else(Module *m) {
Block *block = m->callstack[m->csp].block;
m->pc_ptr = block->br_ptr;
#if TRACE
debug(" - of %s jump to 0x%p\n", block_repr(block), m->pc_ptr);
#endif
return true;
}
/**
* 0x0b end
*/
bool i_instr_end(Module *m, bool *prog_done) {
Block *block = m->warduino->interpreter->pop_block(m);
if (block == nullptr) {
return false; // an exception (set by pop_block)
}
#if TRACE
debug(" - of %s\n", block_repr(block));
#endif
if (block->block_type == 0x00) { // Function
#if TRACE
dbg_warn(
" << fn0x%x(%d) %s = %s\n", block->fidx, block->fidx,
block->export_name ? block->export_name : "",
block->type->result_count > 0 ? value_repr(&m->stack[m->sp]) : "_");
#endif
if (m->csp == -1) {
// Return to top-level
*prog_done = true;
return true; // continue execution but brake dispatch loop
} else {
// Keep going at return address
}
} else if (block->block_type == 0x01) { // init_expr
*prog_done = true;
return true; // continue execution but brake dispatch loop
} else { // Block
// End of block/loop/if, keep going
}
return true; // continue execution
}
/**
* 0x0c br
*/
bool i_instr_br(Module *m) {
uint32_t depth = read_LEB_32(&m->pc_ptr);
m->csp -= depth;
// set to end for pop_block
m->pc_ptr = m->callstack[m->csp].block->br_ptr;
#if TRACE
debug(" - to: 0x%p\n", m->pc_ptr);
#endif
return true;
}
/**
* 0x0d br_if
*/
bool i_instr_br_if(Module *m) {
uint32_t depth = read_LEB_32(&m->pc_ptr);
uint32_t cond = m->stack[m->sp--].value.uint32;
if (cond) { // if true
m->csp -= depth;
// set to end for pop_block
m->pc_ptr = m->callstack[m->csp].block->br_ptr;
}
#if TRACE
debug(" - depth: 0x%x, cond: 0x%x, to: 0x%p\n", depth, cond,
m->pc_ptr);
#endif
return true;
}
/**
* 0x0e br_table
*/
bool i_instr_br_table(Module *m) {
uint32_t count = read_LEB_32(&m->pc_ptr);
if (count > BR_TABLE_SIZE) {
// TODO: check this prior to runtime
sprintf(exception, "br_table size %" PRIu32 " exceeds max %d\n", count,
BR_TABLE_SIZE);
return false;
}
for (uint32_t i = 0; i < count; i++) {
m->br_table[i] = read_LEB_32(&m->pc_ptr);
}
uint32_t depth = read_LEB_32(&m->pc_ptr);
int32_t didx = m->stack[m->sp--].value.int32;
if (didx >= 0 && didx < (int32_t)count) {
depth = m->br_table[didx];
}
m->csp -= depth;
// set to end for pop_block
m->pc_ptr = m->callstack[m->csp].block->br_ptr;
#if TRACE
debug(" - count: %d, didx: %d, to: 0x%p\n", count, didx, m->pc_ptr);
#endif
return true;
}
/**
* 0x0f return
*/
bool i_instr_return(Module *m) {
while (m->csp >= 0 && m->callstack[m->csp].block->block_type != 0x00) {
m->csp--;
}
// Set the program count to the end of the function
// The actual pop_block and return is handled by the end opcode.
m->pc_ptr = m->callstack[0].block->end_ptr;
#if TRACE
debug(" - to: 0x%p\n", m->pc_ptr);
#endif
return true;
}
/**
* 0x10 call
*/
bool i_instr_call(Module *m) {
uint32_t fidx = read_LEB_32(&m->pc_ptr);
if (m->warduino->debugger->isProxied(m, fidx)) {
return proxy_call(m, fidx);
}
if (fidx < m->import_count) {
// Mocking only works on primitives, no need to check for it otherwise.
if (m->sp >= 0) {
uint32_t arg = m->stack[m->sp].value.uint32;
if (m->warduino->debugger->isMocked(fidx, arg)) {
m->stack[m->sp].value.uint32 =
m->warduino->debugger->getMockedValue(fidx, arg);
return true;
}
}
return ((Primitive)m->functions[fidx].func_ptr)(m);
} else {
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
m->warduino->interpreter->setup_call(m, fidx); // regular function call
#if TRACE
debug(" - calling function fidx: %d at: 0x%p\n", fidx, m->pc_ptr);
#endif
}
return true;
}
/**
* 0x11 call_indirect
*/
bool i_instr_call_indirect(Module *m) {
uint32_t tidx = read_LEB_32(&m->pc_ptr); // TODO: use tidx?
(void)tidx;
read_LEB(&m->pc_ptr, 1); // reserved immediate
uint32_t val = m->stack[m->sp--].value.uint32;
if (m->options.mangle_table_index) {
// val is the table address + the index (not sized for the
// pointer size) so get the actual (sized) index
#if TRACE
debug(" - entries: %p, original val: 0x%x, new val: 0x%x\n",
m->table.entries, val,
(uint32_t)((uint64_t)m->table.entries) - val);
#endif
// val = val - (uint32_t)((uint64_t)m->table.entries & 0xFFFFFFFF);
val = val - (uint32_t)((uint64_t)m->table.entries);
}
if (val >= m->table.maximum) {
sprintf(exception,
"undefined element 0x%" PRIx32 " (max: 0x%" PRIx32 ") in table",
val, m->table.maximum);
return false;
}
uint32_t fidx = m->table.entries[val];
#if TRACE
debug(" - call_indirect tidx: %d, val: 0x%x, fidx: 0x%x\n", tidx, val,
fidx);
#endif
if (fidx < m->import_count) {
// THUNK thunk_out(m, fidx); // import/thunk call
} else {
Block *func = &m->functions[fidx];
Type *ftype = func->type;
if (m->csp >= CALLSTACK_SIZE) {
sprintf(exception, "call stack exhausted");
return false;
}
if (ftype->mask != m->types[tidx].mask) {
sprintf(exception,
"indirect call type mismatch (call type and function type "
"differ)");
return false;
}
m->warduino->interpreter->setup_call(m, fidx); // regular function call
// Validate signatures match
if ((int)(ftype->param_count + func->local_count) !=
m->sp - m->fp + 1) {
sprintf(exception,
"indirect call type mismatch (param counts differ)");
return false;
}
for (uint32_t f = 0; f < ftype->param_count; f++) {
if (ftype->params[f] != m->stack[m->fp + f].value_type) {
sprintf(exception,
"indirect call type mismatch (param types differ)");
return false;
}
}
#if TRACE
debug(
" - tidx: %d, table idx: %d, "
"calling function fidx: %d at: 0x%p\n",
tidx, val, fidx, m->pc_ptr);
#endif
}
return true;
}
/**
* 0x1a drop
* remove a value from the stack
*/
bool i_instr_drop(Module *m) {
m->sp--;
return true;
}
/**
* 0x1b select
*
* pop the c from the stack,
* pop val_2 form the stack
* pop val_1 form the stack
* if c : push val_1 to the stack
* else : push val_2 to the stack
*/
bool i_instr_select(Module *m) {
uint32_t cond = m->stack[m->sp--].value.uint32;
m->sp--;
if (!cond) { // use a instead of b
m->stack[m->sp] = m->stack[m->sp + 1];
}
return true;
}
/**
* 0x20 get_local
* move the i-th local to the top of the stack
*/
bool i_instr_get_local(Module *m) {
int32_t arg = read_LEB_32(&m->pc_ptr);
#if TRACE
debug(" - arg: 0x%x, got %s\n", arg,
value_repr(&m->stack[m->fp + arg]));
#endif
m->stack[++m->sp] = m->stack[m->fp + arg];
return true;
}
/**
* 0x21 set_local
*/
bool i_instr_set_local(Module *m) {
int32_t arg = read_LEB_32(&m->pc_ptr);
m->stack[m->fp + arg] = m->stack[m->sp--];
#if TRACE
debug(" - arg: 0x%x, to %s (stack loc: %d)\n", arg,
value_repr(&m->stack[m->sp + 1]), m->fp + arg);
#endif
return true;
}
/**
* 0x0d tee_local
*/
bool i_instr_tee_local(Module *m) {
int32_t arg = read_LEB_32(&m->pc_ptr);
m->stack[m->fp + arg] = m->stack[m->sp];
#if TRACE
debug(" - arg: 0x%x, to %s\n", arg, value_repr(&m->stack[m->sp]));
#endif
return true;
}
/**
* 0x23 get_global
*/
bool i_instr_get_global(Module *m) {
int32_t arg = read_LEB_32(&m->pc_ptr);
#if TRACE
debug(" - arg: 0x%x, got %s\n", arg, value_repr(&m->globals[arg]));
#endif
m->stack[++m->sp] = m->globals[arg];
return true;
}
/**
* 0x24 set_global
*/
bool i_instr_set_global(Module *m) {
uint32_t arg = read_LEB_32(&m->pc_ptr);
m->globals[arg] = m->stack[m->sp--];
#if TRACE
debug(" - arg: 0x%x, got %s\n", arg, value_repr(&m->stack[m->sp + 1]));
#endif
return true;
}
/**
* 0x3f current_memory
*/
bool i_instr_current_memory(Module *m) {
read_LEB_32(&m->pc_ptr); // ignore reserved
m->stack[++m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = m->memory.pages;
return true;
}
/**
* 0x40 grow_memory
*/
bool i_instr_grow_memory(Module *m) {
read_LEB_32(&m->pc_ptr); // ignore reserved
uint32_t prev_pages = m->memory.pages;
uint32_t delta = m->stack[m->sp].value.uint32;
m->stack[m->sp].value.uint32 = prev_pages;
if (delta == 0) {
return true; // No change
} else if (delta + prev_pages > m->memory.maximum) {
m->stack[m->sp].value.uint32 = static_cast<uint32_t>(-1);
return true;
}
m->memory.pages += delta;
m->memory.bytes = (uint8_t *)arecalloc(
m->memory.bytes, prev_pages * PAGE_SIZE, m->memory.pages * PAGE_SIZE,
1 /*sizeof(uint32_t)*/, "Module->memory.bytes", true);
return true;
}
/**
* 0x0d XXX
*/
bool i_instr_mem_load(Module *m, uint8_t opcode) {
uint32_t flags = read_LEB_32(&m->pc_ptr);
uint32_t offset = read_LEB_32(&m->pc_ptr);
uint32_t addr = m->stack[m->sp--].value.uint32;
if (flags != 2 && TRACE) {
dbg_info(
" - unaligned load - flags: 0x%x,"
" offset: 0x%x, addr: 0x%x\n",
flags, offset, addr);
}
return m->warduino->interpreter->load(m, I32 + (0x28 - opcode), addr,
offset);
}
bool i_instr_mem_store(Module *m, uint8_t opcode) {
StackValue *sval = &m->stack[m->sp--];
uint32_t flags = read_LEB_32(&m->pc_ptr);
uint32_t offset = read_LEB_32(&m->pc_ptr);
uint32_t addr = m->stack[m->sp--].value.uint32;
if (flags != 2 && TRACE) {
dbg_info(
" - unaligned store - flags: 0x%x,"
" offset: 0x%x, addr: 0x%x, val: %s\n",
flags, offset, addr, value_repr(sval));
}
if (offset + addr < addr && !m->options.disable_memory_bounds) {
m->warduino->interpreter->report_overflow(
m, m->memory.bytes + offset + addr);
}
addr += offset;
return m->warduino->interpreter->store(m, I32 + (0x36 - opcode), addr,
*sval);
}
/**
* 0x41...0x44 const
*/
bool i_instr_const(Module *m, uint8_t opcode) {
StackValue *target = &m->stack[++m->sp];
switch (opcode) {
case 0x41: // i32.const
target->value_type = I32;
target->value.uint32 = read_LEB_signed(&m->pc_ptr, 32);
break;
case 0x42: // i64.const
target->value_type = I64;
target->value.int64 = read_LEB_signed(&m->pc_ptr, 64);
break;
case 0x43: // f32.const
target->value_type = F32;
memcpy(&target->value.uint32, m->pc_ptr, 4);
m->pc_ptr += 4;
break;
case 0x44: // f64.const
target->value_type = F64;
memcpy(&target->value.uint64, m->pc_ptr, 8);
m->pc_ptr += 8;
break;
default:
return false;
}
return true;
}
/**
* 0x45 eqz
*/
bool i_instr_unary_u32(Module *m, uint8_t opcode) {
switch (opcode) {
case 0x45: // i32.eqz
m->stack[m->sp].value.uint32 =
static_cast<uint32_t>(m->stack[m->sp].value.uint32 == 0);
break;
case 0x50: // i64.eqz
m->stack[m->sp].value_type = I32;
m->stack[m->sp].value.uint32 =
static_cast<uint32_t>(m->stack[m->sp].value.uint64 == 0);
break;
default:
return false;
}
return true;
}
/**
* 0x0d binop32
*/
bool i_instr_math_u32(Module *m, uint8_t opcode) {
uint32_t a = m->stack[m->sp - 1].value.uint32;
uint32_t b = m->stack[m->sp].value.uint32;
uint32_t c;
m->sp -= 1;
switch (opcode) {
case 0x46:
c = static_cast<uint32_t>(a == b);
break; // i32.eq
case 0x47:
c = static_cast<uint32_t>(a != b);
break; // i32.ne
case 0x48:
c = static_cast<uint32_t>((int32_t)a < (int32_t)b);
break; // i32.lt_s
case 0x49:
c = static_cast<uint32_t>(a < b);
break; // i32.lt_u
case 0x4a:
c = static_cast<uint32_t>((int32_t)a > (int32_t)b);
break; // i32.gt_s
case 0x4b:
c = static_cast<uint32_t>(a > b);
break; // i32.gt_u
case 0x4c:
c = static_cast<uint32_t>((int32_t)a <= (int32_t)b);
break; // i32.le_s
case 0x4d:
c = static_cast<uint32_t>(a <= b);
break; // i32.le_u
case 0x4e:
c = static_cast<uint32_t>((int32_t)a >= (int32_t)b);
break; // i32.ge_s
case 0x4f:
c = static_cast<uint32_t>(a >= b);
break; // i32.ge_u
default:
return false;
}
m->stack[m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = c;
return true;
}
/**
* 0x0d binop64
*/
bool i_instr_math_u64(Module *m, uint8_t opcode) {
uint64_t d = m->stack[m->sp - 1].value.uint64;
uint64_t e = m->stack[m->sp].value.uint64;
uint32_t c;
m->sp -= 1;
switch (opcode) {
case 0x51:
c = static_cast<uint32_t>(d == e);
break; // i64.eq
case 0x52:
c = static_cast<uint32_t>(d != e);
break; // i64.ne
case 0x53:
c = static_cast<uint32_t>((int64_t)d < (int64_t)e);
break; // i64.lt_s
case 0x54:
c = static_cast<uint32_t>(d < e);
break; // i64.lt_u
case 0x55:
c = static_cast<uint32_t>((int64_t)d > (int64_t)e);
break; // i64.gt_s
case 0x56:
c = static_cast<uint32_t>(d > e);
break; // i64.gt_u
case 0x57:
c = static_cast<uint32_t>((int64_t)d <= (int64_t)e);
break; // i64.le_s
case 0x58:
c = static_cast<uint32_t>(d <= e);
break; // i64.le_u
case 0x59:
c = static_cast<uint32_t>((int64_t)d >= (int64_t)e);
break; // i64.ge_s
case 0x5a:
c = static_cast<uint32_t>(d >= e);
break; // i64.ge_u
default:
return false;
}
m->stack[m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = c;
return true;
}
/**
* 0x0d binop64
*/
bool i_instr_math_f32(Module *m, uint8_t opcode) {
float g = m->stack[m->sp - 1].value.f32;
float h = m->stack[m->sp].value.f32;
uint32_t c;
m->sp -= 1;
switch (opcode) {
case 0x5b:
c = static_cast<uint32_t>(g == h);
break; // f32.eq
case 0x5c:
c = static_cast<uint32_t>(g != h);
break; // f32.ne
case 0x5d:
c = static_cast<uint32_t>(g < h);
break; // f32.lt
case 0x5e:
c = static_cast<uint32_t>(g > h);
break; // f32.gt
case 0x5f:
c = static_cast<uint32_t>(g <= h);
break; // f32.le
case 0x60:
c = static_cast<uint32_t>(g >= h);
break; // f32.ge
default:
return false;
}
m->stack[m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = c;
return true;
}
/**
* 0x0d binopf64
*/
bool i_instr_math_f64(Module *m, uint8_t opcode) {
double j = m->stack[m->sp - 1].value.f64;
double k = m->stack[m->sp].value.f64;
uint32_t c;
m->sp -= 1;
switch (opcode) {
case 0x61:
c = static_cast<uint32_t>(j == k);
break; // f64.eq
case 0x62:
c = static_cast<uint32_t>(j != k);
break; // f64.ne
case 0x63:
c = static_cast<uint32_t>(j < k);
break; // f64.lt
case 0x64:
c = static_cast<uint32_t>(j > k);
break; // f64.gt
case 0x65:
c = static_cast<uint32_t>(j <= k);
break; // f64.le
case 0x66:
c = static_cast<uint32_t>(j >= k);
break; // f64.ge
default:
return false;
}
m->stack[m->sp].value_type = I32;
m->stack[m->sp].value.uint32 = c;
return true;
}
bool i_instr_unary_i32(Module *m, uint8_t opcode) {
uint32_t a = m->stack[m->sp].value.uint32;
uint32_t c;
switch (opcode) {
case 0x67:
c = a == 0 ? 32 : __builtin_clz(a);
break; // i32.clz
case 0x68:
c = a == 0 ? 32 : __builtin_ctz(a);
break; // i32.ctz
case 0x69:
c = __builtin_popcount(a);
break; // i32.popcnt
default:
return false;
}
m->stack[m->sp].value.uint32 = c;
return true;
}
bool i_instr_unary_i64(Module *m, uint8_t opcode) {
uint64_t d = m->stack[m->sp].value.uint64;
uint64_t f;
switch (opcode) {
case 0x79:
f = d == 0 ? 64 : __builtin_clzll(d);
break; // i64.clz
case 0x7a:
f = d == 0 ? 64 : __builtin_ctzll(d);
break; // i64.ctz
case 0x7b:
f = __builtin_popcountll(d);
break; // i64.popcnt
default:
return false;
}
m->stack[m->sp].value.uint64 = f;
return true;
}
/**
* 0x0d XXX
*/
bool i_instr_unary_floating(Module *m, uint8_t opcode) {
switch (opcode) {
// unary f32
case 0x8b:
m->stack[m->sp].value.f32 = fabs(m->stack[m->sp].value.f32);
break; // f32.abs
case 0x8c:
m->stack[m->sp].value.f32 = -m->stack[m->sp].value.f32;
break; // f32.neg
case 0x8d:
m->stack[m->sp].value.f32 = ceil(m->stack[m->sp].value.f32);
break; // f32.ceil
case 0x8e:
m->stack[m->sp].value.f32 = floor(m->stack[m->sp].value.f32);
break; // f32.floor
case 0x8f:
m->stack[m->sp].value.f32 = trunc(m->stack[m->sp].value.f32);
break; // f32.trunc
case 0x90:
m->stack[m->sp].value.f32 = rint(m->stack[m->sp].value.f32);
break; // f32.nearest
case 0x91:
m->stack[m->sp].value.f32 = sqrt(m->stack[m->sp].value.f32);
break; // f32.sqrt
// unary f64
case 0x99:
m->stack[m->sp].value.f64 = fabs(m->stack[m->sp].value.f64);
break; // f64.abs
case 0x9a:
m->stack[m->sp].value.f64 = -m->stack[m->sp].value.f64;
break; // f64.neg
case 0x9b:
m->stack[m->sp].value.f64 = ceil(m->stack[m->sp].value.f64);
break; // f64.ceil
case 0x9c:
m->stack[m->sp].value.f64 = floor(m->stack[m->sp].value.f64);
break; // f64.floor
case 0x9d:
m->stack[m->sp].value.f64 = trunc(m->stack[m->sp].value.f64);
break; // f64.trunc
case 0x9e:
m->stack[m->sp].value.f64 = rint(m->stack[m->sp].value.f64);
break; // f64.nearest
case 0x9f:
m->stack[m->sp].value.f64 = sqrt(m->stack[m->sp].value.f64);
break; // f64.sqrt
default:
return false;
}
return true;
}
/**
* 0x0d binary_i32
*/
bool i_instr_binary_i32(Module *m, uint8_t opcode) {
// TODO: verify if this should not be done with int32_t instead
uint32_t a = m->stack[m->sp - 1].value.uint32;
uint32_t b = m->stack[m->sp].value.uint32;
uint32_t c;
m->sp -= 1;
if (opcode >= 0x6d && opcode <= 0x70 && b == 0) {
sprintf(exception, "integer divide by zero");
return false;
}
switch (opcode) {
// case 0x6a: o = __builtin_add_overflow(a, b, &c); break;
// // i32.add case 0x6b: o = __builtin_sub_overflow(a, b,
// &c); break; // i32.sub
case 0x6a:
c = a + b;
break; // i32.add
case 0x6b:
c = a - b;
break; // i32.sub
case 0x6c:
c = a * b;
break; // i32.mul
case 0x6d:
if (a == 0x80000000 && b == (uint32_t)-1) {
sprintf(exception, "integer overflow");
return false;
}
c = (int32_t)a / (int32_t)b;
break; // i32.div_s
case 0x6e:
c = a / b;
break; // i32.div_u
case 0x6f:
if (a == 0x80000000 && b == (uint32_t)-1) {
c = 0;
} else {
c = (int32_t)a % (int32_t)b;
};
break; // i32.rem_s
case 0x70:
c = a % b;
break; // i32.rem_u
case 0x71:
c = a & b;
break; // i32.and
case 0x72:
c = a | b;
break; // i32.or
case 0x73:
c = a ^ b;
break; // i32.xor
case 0x74:
c = a << b;
break; // i32.shl
case 0x75:
c = (int32_t)a >> b; // NOLINT(hicpp-signed-bitwise)
break; // i32.shr_s
case 0x76:
c = a >> b;
break; // i32.shr_u
case 0x77:
c = rotl32(a, b);
break; // i32.rotl
case 0x78:
c = rotr32(a, b);
break; // i32.rotr
default:
return false;
}
// if (o == 1) {
// sprintf(exception, "integer overflow");
// return false;
//}
m->stack[m->sp].value.uint32 = c;
return true;
}
/**
* 0x0d XXX
*/
bool i_instr_binary_i64(Module *m, uint8_t opcode) {
uint64_t d = m->stack[m->sp - 1].value.uint64;
uint64_t e = m->stack[m->sp].value.uint64;
uint64_t f;
m->sp -= 1;
if (opcode >= 0x7f && opcode <= 0x82 && e == 0) {
sprintf(exception, "integer divide by zero");
return false;
}
switch (opcode) {
case 0x7c:
f = d + e;
break; // i64.add
case 0x7d:
f = d - e;
break; // i64.sub
case 0x7e:
f = d * e;
break; // i64.mul
case 0x7f:
if (d == 0x8000000000000000 && e == (uint32_t)-1) {
sprintf(exception, "integer overflow");
return false;
}
f = (int64_t)d / (int64_t)e;
break; // i64.div_s
case 0x80:
f = d / e;
break; // i64.div_u
case 0x81:
if (d == 0x8000000000000000 && e == (uint32_t)-1) {
f = 0;
} else {
f = (int64_t)d % (int64_t)e;
}
break; // i64.rem_s
case 0x82:
f = d % e;
break; // i64.rem_u
case 0x83:
f = d & e;
break; // i64.and
case 0x84:
f = d | e;
break; // i64.or
case 0x85:
f = d ^ e;
break; // i64.xor
case 0x86:
f = d << e;
break; // i64.shl
case 0x87: