Skip to content

Commit

Permalink
Store load type separately in Load
Browse files Browse the repository at this point in the history
With opaque pointers, one cannot tell what the type being loaded is by
inspecting the pointer in the `load` instruction. As a result, we now store the
load type separately in the `Load` instruction so that it can be determined
regardless of whether opaque pointers are used or not.

See #102.
  • Loading branch information
RyanGlScott committed Apr 6, 2023
1 parent 257a906 commit 37cbb71
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/Text/LLVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,8 @@ alloca ty mb align = observe (PtrTo ty) (Alloca ty es align)
where
es = fmap toValue `fmap` mb

load :: IsValue a => Typed a -> Maybe Align -> BB (Typed Value)
load tv ma =
case typedType tv of
PtrTo ty -> observe ty (Load (toValue `fmap` tv) Nothing ma)
_ -> error "load not given a pointer"
load :: IsValue a => Type -> Typed a -> Maybe Align -> BB (Typed Value)
load ty ptr ma = observe ty (Load ty (toValue `fmap` ptr) Nothing ma)

store :: (IsValue a, IsValue b) => a -> Typed b -> Maybe Align -> BB ()
store a ptr ma =
Expand Down
3 changes: 2 additions & 1 deletion src/Text/LLVM/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,9 @@ data Instr' lab
* Middle of basic block.
* Returns a pointer to hold the given number of elements. -}

| Load (Typed (Value' lab)) (Maybe AtomicOrdering) (Maybe Align)
| Load Type (Typed (Value' lab)) (Maybe AtomicOrdering) (Maybe Align)
{- ^ * Read a value from the given address:
type being loaded;
address to read from;
atomic ordering;
assumptions about alignment of the given pointer.
Expand Down
5 changes: 4 additions & 1 deletion src/Text/LLVM/Labels.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ instance HasLabel Instr' where
relabel f (Alloca t n a) = Alloca t
<$> traverse (traverse (relabel f)) n
<*> pure a
relabel f (Load a mo ma) = Load <$> traverse (relabel f) a <*> pure mo <*> pure ma
relabel f (Load t a mo ma) = Load t
<$> traverse (relabel f) a
<*> pure mo
<*> pure ma
relabel f (Store d v mo ma) = Store
<$> traverse (relabel f) d
<*> traverse (relabel f) v
Expand Down
11 changes: 4 additions & 7 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ ppInstr instr = case instr of
Call tc ty f args -> ppCall tc ty f args
CallBr ty f args u es -> ppCallBr ty f args u es
Alloca ty len align -> ppAlloca ty len align
Load ptr mo ma -> ppLoad ptr mo ma
Load ty ptr mo ma -> ppLoad ty ptr mo ma
Store a ptr mo ma -> ppStore a ptr mo ma
Fence scope order -> "fence" <+> ppScope scope <+> ppAtomicOrdering order
CmpXchg w v p a n s o o' -> "cmpxchg" <+> opt w "weak"
Expand Down Expand Up @@ -597,8 +597,8 @@ ppInstr instr = case instr of
Resume tv -> "resume" <+> ppTyped ppValue tv
Freeze tv -> "freeze" <+> ppTyped ppValue tv

ppLoad :: LLVM => Typed (Value' BlockLabel) -> Maybe AtomicOrdering -> Maybe Align -> Doc
ppLoad ptr mo ma =
ppLoad :: LLVM => Type -> Typed (Value' BlockLabel) -> Maybe AtomicOrdering -> Maybe Align -> Doc
ppLoad ty ptr mo ma =
"load" <+> (if isAtomic then "atomic" else empty)
<+> (if isImplicit then empty else explicit)
<+> ppTyped ppValue ptr
Expand All @@ -615,10 +615,7 @@ ppLoad ptr mo ma =
Just ao -> ppAtomicOrdering ao
_ -> empty

explicit =
case typedType ptr of
PtrTo ty -> ppType ty <> comma
ty -> ppType ty <> comma
explicit = ppType ty <> comma

ppStore :: LLVM
=> Typed (Value' BlockLabel)
Expand Down

0 comments on commit 37cbb71

Please sign in to comment.