Skip to content

Commit

Permalink
Fix parsing of branch targets in control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
em-eight committed Nov 9, 2023
1 parent 9291dc0 commit 346b160
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions include/ppc2cpp/common/insn_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ inline bool isBranchUncoditional(uint32_t insn) {
// Returns the LI field of an I form instruction
inline int32_t branchValue(uint32_t insn) {
int32_t addr_val = (insn >> 2) & 0xffffff;
return addr_val;
// Sign extend if the value is negative
if (addr_val & 0x00800000) {
addr_val |= 0xFF000000;
}
return addr_val * 4;
}

inline bool isBranchConditional(uint32_t insn) {
Expand All @@ -49,7 +53,11 @@ inline bool isBranchConditional(uint32_t insn) {

// Returns the BD field of a B form instruction
inline int32_t branchConditionalValue(uint32_t insn) {
int32_t addr_val = (insn >> 2) & 0x3fff;
int32_t addr_val = 0xfffc;
// Sign extend if the value is negative
if (addr_val & 0x00008000) {
addr_val |= 0xFFFF0000;
}
return addr_val;
}

Expand Down

0 comments on commit 346b160

Please sign in to comment.