Skip to content

IRToASTVisitor: sign-extend from the source width #345

Open

Description

@humdrum00001010

Reproduced with Rellic v0.2.3 (0cccc98a78d985bd0d1343f82b123dda03f71fcb), LLVM 15. The corresponding lowering remains in current master (370abaad86ae5adad82f85b81aff14557462b7f2); current-master behavior has only been source-inspected.

LLVM sext widens an unsigned C source directly to the signed destination, losing the source sign bit.

Save as repro.ll:

define i64 @extend32(i32 %source) {
  %result = sext i32 %source to i64
  ret i64 %result
}
define i64 @extend1(i1 %source) {
  %result = sext i1 %source to i64
  ret i64 %result
}
rellic-decomp --input repro.ll --output repro.c

For extend32, the emitted widening is equivalent to (long long)source with an unsigned 32-bit source. 0xffffffff consequently becomes 4294967295, whereas LLVM requires -1.

For sext i1, true must become all-one bits (-1), rather than integer 1.

Interpret the source at its signed LLVM width before widening, including the special one-bit case. Casting only the destination to a signed type is insufficient.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions