Skip to content

Commit

Permalink
fix confusion between GEP base type and return type
Browse files Browse the repository at this point in the history
The LLVM GEP instruction is notoriously confusing, and looks like this
was yet another victim.

The base type of computation is the underlying pointee type of the
pointer from which operations are being done.

The return type can be the same or something else, depending on what
exactly is computed by the GEP operation.

The two were conflated here which was creating bugs.  They are now
distinct, with the base type being obtained from the operand (as it is
easy to do), and the return type being provided by the caller (as it is
otherwise annoying to computed from the operands).
  • Loading branch information
Ptival committed Oct 18, 2024
1 parent 359da26 commit c636f42
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Reopt/CFG/LLVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,17 +1167,21 @@ bvSubPtrPtr x y = do

-- | This emits a getElementPointer in the special case where the value argument is a pointer.
llvmGEPFromPtr ::
HasCallStack =>
L.Type ->
Int ->
L.Typed L.Value ->
BBLLVM arch (L.Typed L.Value)
llvmGEPFromPtr pointeeType ofs ptrV = do
let pointerType = L.PtrTo pointeeType
llvmGEPFromPtr returnType ofs ptrV = do
let
pointeeType =
case L.typedType ptrV of
L.PtrTo ty -> ty
ty -> error $ "llvmGEPFromPtr: expecter pointer type, got: " <> show ty
zeroV = L.Typed (L.iT 32) (L.int 0)
ofsV = L.Typed (L.iT 32) (L.int ofs)
-- https://llvm.org/docs/GetElementPtr.html#what-is-the-first-index-of-the-gep-instruction
L.Typed pointerType <$> evalInstr (L.GEP False pointeeType ptrV [zeroV, ofsV])
L.Typed returnType <$> evalInstr (L.GEP False pointeeType ptrV [zeroV, ofsV])

-- | Truncate and log.
llvmTrunc ::
Expand Down

0 comments on commit c636f42

Please sign in to comment.