Skip to content

Commit 35b4c36

Browse files
committed
Move br_table immediate values to the instructions array
1 parent f9c2aba commit 35b4c36

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

lib/fizzy/execute.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
596596
}
597597
case Instr::br_table:
598598
{
599-
const auto br_table_size = read<uint32_t>(immediates);
600-
const auto arity = read<uint32_t>(immediates);
599+
const auto br_table_size = read<uint32_t>(pc);
600+
const auto arity = read<uint32_t>(pc);
601601

602602
const auto br_table_idx = stack.pop().as<uint32_t>();
603603

lib/fizzy/parser_expr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,16 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
634634
if (default_label_idx >= control_stack.size())
635635
throw validation_error{"invalid label index"};
636636

637-
push(code.immediates, static_cast<uint32_t>(label_indices.size()));
637+
code.instructions.push_back(opcode);
638+
push(code.instructions, static_cast<uint32_t>(label_indices.size()));
638639

639640
auto& default_branch_frame = control_stack[default_label_idx];
640641
const auto default_branch_type = get_branch_frame_type(default_branch_frame);
641642

642643
update_branch_stack(frame, default_branch_frame, operand_stack);
643644

644645
// arity is the same for all indices, so we push it once
645-
push(code.immediates, get_branch_arity(default_branch_frame));
646+
push(code.instructions, get_branch_arity(default_branch_frame));
646647

647648
// Remember immediates offset for all br items to fill them at end instruction.
648649
for (const auto idx : label_indices)
@@ -662,7 +663,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
662663

663664
mark_frame_unreachable(frame, operand_stack);
664665

665-
break;
666+
continue;
666667
}
667668

668669
case Instr::return_:

test/unittests/parser_expr_test.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,12 @@ TEST(parser_expr, instr_br_table)
334334

335335
EXPECT_THAT(code.instructions,
336336
ElementsAre(Instr::block, Instr::block, Instr::block, Instr::block, Instr::block,
337-
Instr::local_get, 0, 0, 0, 0, Instr::br_table, Instr::i32_const, 0x41, 0, 0, 0,
338-
Instr::return_, Instr::end, Instr::i32_const, 0x42, 0, 0, 0, Instr::return_, Instr::end,
339-
Instr::i32_const, 0x43, 0, 0, 0, Instr::return_, Instr::end, Instr::i32_const, 0x44, 0,
340-
0, 0, Instr::return_, Instr::end, Instr::i32_const, 0x45, 0, 0, 0, Instr::return_,
341-
Instr::end, Instr::i32_const, 0x46, 0, 0, 0, Instr::end));
337+
Instr::local_get, 0, 0, 0, 0, Instr::br_table, /*label_count:*/ 4, 0, 0, 0,
338+
/*arity:*/ 0, 0, 0, 0, Instr::i32_const, 0x41, 0, 0, 0, Instr::return_, Instr::end,
339+
Instr::i32_const, 0x42, 0, 0, 0, Instr::return_, Instr::end, Instr::i32_const, 0x43, 0,
340+
0, 0, Instr::return_, Instr::end, Instr::i32_const, 0x44, 0, 0, 0, Instr::return_,
341+
Instr::end, Instr::i32_const, 0x45, 0, 0, 0, Instr::return_, Instr::end,
342+
Instr::i32_const, 0x46, 0, 0, 0, Instr::end));
342343

343344
// br_imm_size = 12
344345
// return_imm_size = br_imm_size + arity_size = 16
@@ -348,27 +349,24 @@ TEST(parser_expr, instr_br_table)
348349
// br_3_offset = br_2_offset + 4 + return_imm_size = 0x98
349350
// br_4_offset = br_3_offset + 4 + return_imm_size = 0xac
350351
const auto expected_br_imm =
351-
"04000000" // label_count
352-
"00000000" // arity
353-
354-
"27000000" // code_offset
355-
"84000000" // imm_offset
352+
"2f000000" // code_offset
353+
"7c000000" // imm_offset
356354
"00000000" // stack_drop
357355

358-
"20000000" // code_offset
359-
"74000000" // imm_offset
356+
"28000000" // code_offset
357+
"6c000000" // imm_offset
360358
"00000000" // stack_drop
361359

362-
"19000000" // code_offset
363-
"64000000" // imm_offset
360+
"21000000" // code_offset
361+
"5c000000" // imm_offset
364362
"00000000" // stack_drop
365363

366-
"12000000" // code_offset
367-
"54000000" // imm_offset
364+
"1a000000" // code_offset
365+
"4c000000" // imm_offset
368366
"00000000" // stack_drop
369367

370-
"2e000000" // code_offset
371-
"94000000" // imm_offset
368+
"36000000" // code_offset
369+
"8c000000" // imm_offset
372370
"00000000"_bytes; // stack_drop
373371

374372
EXPECT_EQ(code.immediates.substr(0, expected_br_imm.size()), expected_br_imm);
@@ -393,16 +391,14 @@ TEST(parser_expr, instr_br_table_empty_vector)
393391
ASSERT_EQ(module->codesec.size(), 1);
394392
const auto& code = module->codesec[0];
395393

396-
EXPECT_THAT(
397-
code.instructions, ElementsAre(Instr::block, Instr::local_get, 0, 0, 0, 0, Instr::br_table,
398-
Instr::i32_const, 0x63, 0, 0, 0, Instr::return_, Instr::end,
399-
Instr::i32_const, 0x64, 0, 0, 0, Instr::end));
394+
EXPECT_THAT(code.instructions,
395+
ElementsAre(Instr::block, Instr::local_get, 0, 0, 0, 0, Instr::br_table,
396+
/*label_count:*/ 0, 0, 0, 0, /*arity:*/ 0, 0, 0, 0, Instr::i32_const, 0x63, 0, 0, 0,
397+
Instr::return_, Instr::end, Instr::i32_const, 0x64, 0, 0, 0, Instr::end));
400398

401399
const auto expected_br_imm =
402-
"00000000" // label_count
403-
"00000000" // arity
404-
"0e000000" // code_offset
405-
"24000000" // imm_offset
400+
"16000000" // code_offset
401+
"1c000000" // imm_offset
406402
"00000000"_bytes; // stack_drop
407403
EXPECT_EQ(code.immediates.substr(0, expected_br_imm.size()), expected_br_imm);
408404
EXPECT_EQ(code.max_stack_height, 1);
@@ -417,8 +413,9 @@ TEST(parser_expr, instr_br_table_as_return)
417413

418414
const auto code_bin = i32_const(0) + "0e00000b"_bytes;
419415
const auto [code, _] = parse_expr(code_bin);
420-
EXPECT_THAT(
421-
code.instructions, ElementsAre(Instr::i32_const, 0, 0, 0, 0, Instr::br_table, Instr::end));
416+
EXPECT_THAT(code.instructions,
417+
ElementsAre(Instr::i32_const, 0, 0, 0, 0, Instr::br_table, /*label_count:*/ 0, 0, 0, 0,
418+
/*arity:*/ 0, 0, 0, 0, Instr::end));
422419
EXPECT_EQ(code.max_stack_height, 1);
423420
}
424421

0 commit comments

Comments
 (0)