Skip to content

Commit 7c0562b

Browse files
authored
Merge pull request #618 from wasmx/immediates_merge
Drop immediates array
2 parents 9abcdfb + 1bb954b commit 7c0562b

10 files changed

+263
-366
lines changed

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ jobs:
390390
sanitizers-gcc:
391391
executor: linux-gcc-latest
392392
environment:
393-
CMAKE_BUILD_PARALLEL_LEVEL: 6
393+
CMAKE_BUILD_PARALLEL_LEVEL: 2
394394
# TODO: Enable detect_stack_use_after_return=1 when https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97414 is fixed.
395395
ASAN_OPTIONS: detect_invalid_pointer_pairs=2:check_initialization_order=1
396396
UBSAN_OPTIONS: halt_on_error=1

lib/fizzy/execute.cpp

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace fizzy
1818
{
1919
namespace
2020
{
21-
// code_offset + imm_offset + stack_height
22-
constexpr auto BranchImmediateSize = 3 * sizeof(uint32_t);
21+
// code_offset + stack_drop
22+
constexpr auto BranchImmediateSize = 2 * sizeof(uint32_t);
2323

2424
constexpr uint32_t F32AbsMask = 0x7fffffff;
2525
constexpr uint32_t F32SignMask = ~F32AbsMask;
@@ -453,15 +453,12 @@ __attribute__((no_sanitize("float-cast-overflow"))) inline constexpr float demot
453453
return static_cast<float>(value);
454454
}
455455

456-
void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, const uint8_t*& immediates,
457-
uint32_t arity) noexcept
456+
void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, uint32_t arity) noexcept
458457
{
459-
const auto code_offset = read<uint32_t>(immediates);
460-
const auto imm_offset = read<uint32_t>(immediates);
461-
const auto stack_drop = read<uint32_t>(immediates);
458+
const auto code_offset = read<uint32_t>(pc);
459+
const auto stack_drop = read<uint32_t>(pc);
462460

463461
pc = code.instructions.data() + code_offset;
464-
immediates = code.immediates.data() + imm_offset;
465462

466463
// When branch is taken, additional stack items must be dropped.
467464
assert(static_cast<int>(stack_drop) >= 0);
@@ -523,7 +520,6 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
523520
static_cast<size_t>(code.max_stack_height));
524521

525522
const uint8_t* pc = code.instructions.data();
526-
const uint8_t* immediates = code.immediates.data();
527523

528524
while (true)
529525
{
@@ -539,27 +535,20 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
539535
case Instr::if_:
540536
{
541537
if (stack.pop().as<uint32_t>() != 0)
542-
immediates += 2 * sizeof(uint32_t); // Skip the immediates for else instruction.
538+
pc += sizeof(uint32_t); // Skip the immediate for else instruction.
543539
else
544540
{
545-
const auto target_pc = read<uint32_t>(immediates);
546-
const auto target_imm = read<uint32_t>(immediates);
547-
541+
const auto target_pc = read<uint32_t>(pc);
548542
pc = code.instructions.data() + target_pc;
549-
immediates = code.immediates.data() + target_imm;
550543
}
551544
break;
552545
}
553546
case Instr::else_:
554547
{
555548
// We reach else only after executing if block ("then" part),
556549
// so we need to skip else block now.
557-
const auto target_pc = read<uint32_t>(immediates);
558-
const auto target_imm = read<uint32_t>(immediates);
559-
550+
const auto target_pc = read<uint32_t>(pc);
560551
pc = code.instructions.data() + target_pc;
561-
immediates = code.immediates.data() + target_imm;
562-
563552
break;
564553
}
565554
case Instr::end:
@@ -573,36 +562,36 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
573562
case Instr::br_if:
574563
case Instr::return_:
575564
{
576-
const auto arity = read<uint32_t>(immediates);
565+
const auto arity = read<uint32_t>(pc);
577566

578567
// Check condition for br_if.
579568
if (instruction == Instr::br_if && stack.pop().as<uint32_t>() == 0)
580569
{
581-
immediates += BranchImmediateSize;
570+
pc += BranchImmediateSize;
582571
break;
583572
}
584573

585-
branch(code, stack, pc, immediates, arity);
574+
branch(code, stack, pc, arity);
586575
break;
587576
}
588577
case Instr::br_table:
589578
{
590-
const auto br_table_size = read<uint32_t>(immediates);
591-
const auto arity = read<uint32_t>(immediates);
579+
const auto br_table_size = read<uint32_t>(pc);
580+
const auto arity = read<uint32_t>(pc);
592581

593582
const auto br_table_idx = stack.pop().as<uint32_t>();
594583

595584
const auto label_idx_offset = br_table_idx < br_table_size ?
596585
br_table_idx * BranchImmediateSize :
597586
br_table_size * BranchImmediateSize;
598-
immediates += label_idx_offset;
587+
pc += label_idx_offset;
599588

600-
branch(code, stack, pc, immediates, arity);
589+
branch(code, stack, pc, arity);
601590
break;
602591
}
603592
case Instr::call:
604593
{
605-
const auto called_func_idx = read<uint32_t>(immediates);
594+
const auto called_func_idx = read<uint32_t>(pc);
606595
const auto& called_func_type = instance.module->get_function_type(called_func_idx);
607596

608597
if (!invoke_function(called_func_type, called_func_idx, instance, stack, depth))
@@ -613,7 +602,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
613602
{
614603
assert(instance.table != nullptr);
615604

616-
const auto expected_type_idx = read<uint32_t>(immediates);
605+
const auto expected_type_idx = read<uint32_t>(pc);
617606
assert(expected_type_idx < instance.module->typesec.size());
618607

619608
const auto elem_idx = stack.pop().as<uint32_t>();
@@ -655,25 +644,25 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
655644
}
656645
case Instr::local_get:
657646
{
658-
const auto idx = read<uint32_t>(immediates);
647+
const auto idx = read<uint32_t>(pc);
659648
stack.push(stack.local(idx));
660649
break;
661650
}
662651
case Instr::local_set:
663652
{
664-
const auto idx = read<uint32_t>(immediates);
653+
const auto idx = read<uint32_t>(pc);
665654
stack.local(idx) = stack.pop();
666655
break;
667656
}
668657
case Instr::local_tee:
669658
{
670-
const auto idx = read<uint32_t>(immediates);
659+
const auto idx = read<uint32_t>(pc);
671660
stack.local(idx) = stack.top();
672661
break;
673662
}
674663
case Instr::global_get:
675664
{
676-
const auto idx = read<uint32_t>(immediates);
665+
const auto idx = read<uint32_t>(pc);
677666
assert(idx < instance.imported_globals.size() + instance.globals.size());
678667
if (idx < instance.imported_globals.size())
679668
{
@@ -689,7 +678,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
689678
}
690679
case Instr::global_set:
691680
{
692-
const auto idx = read<uint32_t>(immediates);
681+
const auto idx = read<uint32_t>(pc);
693682
if (idx < instance.imported_globals.size())
694683
{
695684
assert(instance.imported_globals[idx].type.is_mutable);
@@ -706,129 +695,129 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
706695
}
707696
case Instr::i32_load:
708697
{
709-
if (!load_from_memory<uint32_t>(*memory, stack, immediates))
698+
if (!load_from_memory<uint32_t>(*memory, stack, pc))
710699
goto trap;
711700
break;
712701
}
713702
case Instr::i64_load:
714703
{
715-
if (!load_from_memory<uint64_t>(*memory, stack, immediates))
704+
if (!load_from_memory<uint64_t>(*memory, stack, pc))
716705
goto trap;
717706
break;
718707
}
719708
case Instr::f32_load:
720709
{
721-
if (!load_from_memory<float>(*memory, stack, immediates))
710+
if (!load_from_memory<float>(*memory, stack, pc))
722711
goto trap;
723712
break;
724713
}
725714
case Instr::f64_load:
726715
{
727-
if (!load_from_memory<double>(*memory, stack, immediates))
716+
if (!load_from_memory<double>(*memory, stack, pc))
728717
goto trap;
729718
break;
730719
}
731720
case Instr::i32_load8_s:
732721
{
733-
if (!load_from_memory<uint32_t, int8_t>(*memory, stack, immediates))
722+
if (!load_from_memory<uint32_t, int8_t>(*memory, stack, pc))
734723
goto trap;
735724
break;
736725
}
737726
case Instr::i32_load8_u:
738727
{
739-
if (!load_from_memory<uint32_t, uint8_t>(*memory, stack, immediates))
728+
if (!load_from_memory<uint32_t, uint8_t>(*memory, stack, pc))
740729
goto trap;
741730
break;
742731
}
743732
case Instr::i32_load16_s:
744733
{
745-
if (!load_from_memory<uint32_t, int16_t>(*memory, stack, immediates))
734+
if (!load_from_memory<uint32_t, int16_t>(*memory, stack, pc))
746735
goto trap;
747736
break;
748737
}
749738
case Instr::i32_load16_u:
750739
{
751-
if (!load_from_memory<uint32_t, uint16_t>(*memory, stack, immediates))
740+
if (!load_from_memory<uint32_t, uint16_t>(*memory, stack, pc))
752741
goto trap;
753742
break;
754743
}
755744
case Instr::i64_load8_s:
756745
{
757-
if (!load_from_memory<uint64_t, int8_t>(*memory, stack, immediates))
746+
if (!load_from_memory<uint64_t, int8_t>(*memory, stack, pc))
758747
goto trap;
759748
break;
760749
}
761750
case Instr::i64_load8_u:
762751
{
763-
if (!load_from_memory<uint64_t, uint8_t>(*memory, stack, immediates))
752+
if (!load_from_memory<uint64_t, uint8_t>(*memory, stack, pc))
764753
goto trap;
765754
break;
766755
}
767756
case Instr::i64_load16_s:
768757
{
769-
if (!load_from_memory<uint64_t, int16_t>(*memory, stack, immediates))
758+
if (!load_from_memory<uint64_t, int16_t>(*memory, stack, pc))
770759
goto trap;
771760
break;
772761
}
773762
case Instr::i64_load16_u:
774763
{
775-
if (!load_from_memory<uint64_t, uint16_t>(*memory, stack, immediates))
764+
if (!load_from_memory<uint64_t, uint16_t>(*memory, stack, pc))
776765
goto trap;
777766
break;
778767
}
779768
case Instr::i64_load32_s:
780769
{
781-
if (!load_from_memory<uint64_t, int32_t>(*memory, stack, immediates))
770+
if (!load_from_memory<uint64_t, int32_t>(*memory, stack, pc))
782771
goto trap;
783772
break;
784773
}
785774
case Instr::i64_load32_u:
786775
{
787-
if (!load_from_memory<uint64_t, uint32_t>(*memory, stack, immediates))
776+
if (!load_from_memory<uint64_t, uint32_t>(*memory, stack, pc))
788777
goto trap;
789778
break;
790779
}
791780
case Instr::i32_store:
792781
{
793-
if (!store_into_memory<uint32_t>(*memory, stack, immediates))
782+
if (!store_into_memory<uint32_t>(*memory, stack, pc))
794783
goto trap;
795784
break;
796785
}
797786
case Instr::i64_store:
798787
{
799-
if (!store_into_memory<uint64_t>(*memory, stack, immediates))
788+
if (!store_into_memory<uint64_t>(*memory, stack, pc))
800789
goto trap;
801790
break;
802791
}
803792
case Instr::f32_store:
804793
{
805-
if (!store_into_memory<float>(*memory, stack, immediates))
794+
if (!store_into_memory<float>(*memory, stack, pc))
806795
goto trap;
807796
break;
808797
}
809798
case Instr::f64_store:
810799
{
811-
if (!store_into_memory<double>(*memory, stack, immediates))
800+
if (!store_into_memory<double>(*memory, stack, pc))
812801
goto trap;
813802
break;
814803
}
815804
case Instr::i32_store8:
816805
case Instr::i64_store8:
817806
{
818-
if (!store_into_memory<uint8_t>(*memory, stack, immediates))
807+
if (!store_into_memory<uint8_t>(*memory, stack, pc))
819808
goto trap;
820809
break;
821810
}
822811
case Instr::i32_store16:
823812
case Instr::i64_store16:
824813
{
825-
if (!store_into_memory<uint16_t>(*memory, stack, immediates))
814+
if (!store_into_memory<uint16_t>(*memory, stack, pc))
826815
goto trap;
827816
break;
828817
}
829818
case Instr::i64_store32:
830819
{
831-
if (!store_into_memory<uint32_t>(*memory, stack, immediates))
820+
if (!store_into_memory<uint32_t>(*memory, stack, pc))
832821
goto trap;
833822
break;
834823
}
@@ -861,14 +850,14 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
861850
case Instr::i32_const:
862851
case Instr::f32_const:
863852
{
864-
const auto value = read<uint32_t>(immediates);
853+
const auto value = read<uint32_t>(pc);
865854
stack.push(value);
866855
break;
867856
}
868857
case Instr::i64_const:
869858
case Instr::f64_const:
870859
{
871-
const auto value = read<uint64_t>(immediates);
860+
const auto value = read<uint64_t>(pc);
872861
stack.push(value);
873862
break;
874863
}

0 commit comments

Comments
 (0)