Skip to content

Commit

Permalink
Add Deref Mut cursor in L3 IR
Browse files Browse the repository at this point in the history
  • Loading branch information
vidsinghal committed Apr 25, 2024
1 parent b474e86 commit c681a83
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions gibbon-compiler/src/Gibbon/L3/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ data E3Ext loc dec =
| SSPop SSModality Var Var
| Assert (PreExp E3Ext loc dec) -- ^ Translates to assert statements in C.
-- ^ Analogous to L2's extensions.
-- Instroduce some new IR for mutable cursors
| DerefMutableCursor Var -- ^ Read a mutable cursor, maybe this should be names to ReadMutableCursor etc.
-- Returns a CursorTy type



deriving (Show, Ord, Eq, Read, Generic, NFData)

instance FreeVars (E3Ext l d) where
Expand Down
39 changes: 38 additions & 1 deletion gibbon-compiler/src/Gibbon/Passes/Cursorize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,26 @@ cursorizeExp ddfs fundefs denv tenv senv ex =
LetParRegionE reg sz _ bod -> do
mkLets (regionToBinds True reg sz) <$> go bod

BoundsCheck i bound cur -> return $ Ext $ L3.BoundsCheck i (toLocVar bound) (toLocVar cur)
BoundsCheck i bound cur -> do
case (isMutableCursorTy bound, isMutableCursorTy cur) of
(True, True) -> do
bound' <- gensym "newDerefBound"
cur' <- gensym "newDerefCur"
let newBoundCheckExpr = Ext $ L3.BoundsCheck i bound' cur'
exp' <- genReadMutableCursor bound newBoundCheckExpr bound'
exp'' <- genReadMutableCursor cur exp' cur'
return $ exp''
(False, True) -> do
cur' <- gensym "newDerefCur"
let newBoundCheckExpr = Ext $ L3.BoundsCheck i (toLocVar bound) cur'
exp' <- genReadMutableCursor cur newBoundCheckExpr cur'
return $ exp'
(True, False) -> do
bound' <- gensym "newDerefBound"
let newBoundCheckExpr = Ext $ L3.BoundsCheck i bound' (toLocVar cur)
exp' <- genReadMutableCursor bound newBoundCheckExpr bound'
return $ exp'
(False, False) -> return $ Ext $ L3.BoundsCheck i (toLocVar bound) (toLocVar cur)

FromEndE{} -> error $ "cursorizeExp: TODO FromEndE" ++ sdoc ext

Expand All @@ -430,6 +449,24 @@ cursorizeExp ddfs fundefs denv tenv senv ex =
go = cursorizeExp ddfs fundefs denv tenv senv


-- genDerefOrVar :: LocArg -> Exp3 -> Var -> PassM (Either Var Exp3)
-- genDerefOrVar locArg bod var = case isMutableCursorTy locArg of
-- True -> genReadMutableCursor locArg bod
-- False ->

isMutableCursorTy :: LocArg -> Bool
isMutableCursorTy locArg = case locArg of
Loc LREM{lremLoc, lremReg, lremEndReg, lremMode} -> case lremMode of
OutputMutable -> True
_ -> False
_ -> False

genReadMutableCursor :: LocArg -> Exp3 -> Var -> PassM Exp3
genReadMutableCursor locArg bod newCursor = do
let derefCursorExp = Ext $ L3.DerefMutableCursor (toLocVar locArg)
return $ LetE (newCursor, [], CursorTy, derefCursorExp) bod


-- Cursorize expressions producing `Packed` values
cursorizePackedExp :: DDefs Ty2 -> FunDefs2 -> DepEnv -> TyEnv Ty2 -> SyncEnv -> Exp2
-> PassM (DiExp Exp3)
Expand Down

0 comments on commit c681a83

Please sign in to comment.