Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better C++ compatibility and fixes to conditional branch/switch handling #10

Merged
merged 7 commits into from
Nov 1, 2024
Prev Previous commit
Next Next commit
Cleanup the code by keeping original OpLabel locations. Handling of b…
…ranch instructions becomes much simpler this way.
  • Loading branch information
mtehver committed Apr 20, 2022
commit 463d73375d042b4b3bbf5832f876f45dd9903adb
25 changes: 5 additions & 20 deletions src/opcode_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1916,19 +1916,13 @@ void spvm_execute_OpPhi(spvm_word word_count, spvm_state_t state)
void spvm_execute_OpLabel(spvm_word word_count, spvm_state_t state)
{
spvm_word id = SPVM_READ_WORD(state->code_current);

// Make sure we do not overwrite parent block id if we encounter an explicit label after a jump
if (state->function_stack_cfg[state->function_stack_current] != id) {
state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
state->function_stack_cfg[state->function_stack_current] = id;
}
state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
state->function_stack_cfg[state->function_stack_current] = id;
}
void spvm_execute_OpBranch(spvm_word word_count, spvm_state_t state)
{
spvm_word id = SPVM_READ_WORD(state->code_current);
state->code_current = state->results[id].source_location;
state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
state->function_stack_cfg[state->function_stack_current] = id;

state->did_jump = 1;
}
Expand All @@ -1938,14 +1932,10 @@ void spvm_execute_OpBranchConditional(spvm_word word_count, spvm_state_t state)
spvm_word true_branch = SPVM_READ_WORD(state->code_current);
spvm_word false_branch = SPVM_READ_WORD(state->code_current);

state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
if (state->results[cond].members[0].value.b) {
if (state->results[cond].members[0].value.b)
state->code_current = state->results[true_branch].source_location;
state->function_stack_cfg[state->function_stack_current] = true_branch;
} else {
else
state->code_current = state->results[false_branch].source_location;
state->function_stack_cfg[state->function_stack_current] = false_branch;
}

state->did_jump = 1;
}
Expand All @@ -1965,18 +1955,13 @@ void spvm_execute_OpSwitch(spvm_word word_count, spvm_state_t state)

if (val == lit) {
state->code_current = state->results[lbl].source_location;
state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
state->function_stack_cfg[state->function_stack_current] = lbl;
found = 1;
break;
}
}

if (!found) {
if (!found)
state->code_current = state->results[def_lbl].source_location;
state->function_stack_cfg_parent[state->function_stack_current] = state->function_stack_cfg[state->function_stack_current];
state->function_stack_cfg[state->function_stack_current] = def_lbl;
}

state->did_jump = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/opcode_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ void spvm_setup_OpLabel(spvm_word word_count, spvm_state_t state)
spvm_word id = SPVM_READ_WORD(state->code_current);

state->results[id].type = spvm_result_type_label;
state->results[id].source_location = state->code_current;
state->results[id].source_location = state->code_current - 2; // store original label location (instruction has length 2)
}


Expand Down