Skip to content

Commit c681a83

Browse files
committed
Add Deref Mut cursor in L3 IR
1 parent b474e86 commit c681a83

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

gibbon-compiler/src/Gibbon/L3/Syntax.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ data E3Ext loc dec =
100100
| SSPop SSModality Var Var
101101
| Assert (PreExp E3Ext loc dec) -- ^ Translates to assert statements in C.
102102
-- ^ Analogous to L2's extensions.
103+
-- Instroduce some new IR for mutable cursors
104+
| DerefMutableCursor Var -- ^ Read a mutable cursor, maybe this should be names to ReadMutableCursor etc.
105+
-- Returns a CursorTy type
106+
107+
108+
103109
deriving (Show, Ord, Eq, Read, Generic, NFData)
104110

105111
instance FreeVars (E3Ext l d) where

gibbon-compiler/src/Gibbon/Passes/Cursorize.hs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,26 @@ cursorizeExp ddfs fundefs denv tenv senv ex =
406406
LetParRegionE reg sz _ bod -> do
407407
mkLets (regionToBinds True reg sz) <$> go bod
408408

409-
BoundsCheck i bound cur -> return $ Ext $ L3.BoundsCheck i (toLocVar bound) (toLocVar cur)
409+
BoundsCheck i bound cur -> do
410+
case (isMutableCursorTy bound, isMutableCursorTy cur) of
411+
(True, True) -> do
412+
bound' <- gensym "newDerefBound"
413+
cur' <- gensym "newDerefCur"
414+
let newBoundCheckExpr = Ext $ L3.BoundsCheck i bound' cur'
415+
exp' <- genReadMutableCursor bound newBoundCheckExpr bound'
416+
exp'' <- genReadMutableCursor cur exp' cur'
417+
return $ exp''
418+
(False, True) -> do
419+
cur' <- gensym "newDerefCur"
420+
let newBoundCheckExpr = Ext $ L3.BoundsCheck i (toLocVar bound) cur'
421+
exp' <- genReadMutableCursor cur newBoundCheckExpr cur'
422+
return $ exp'
423+
(True, False) -> do
424+
bound' <- gensym "newDerefBound"
425+
let newBoundCheckExpr = Ext $ L3.BoundsCheck i bound' (toLocVar cur)
426+
exp' <- genReadMutableCursor bound newBoundCheckExpr bound'
427+
return $ exp'
428+
(False, False) -> return $ Ext $ L3.BoundsCheck i (toLocVar bound) (toLocVar cur)
410429

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

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

432451

452+
-- genDerefOrVar :: LocArg -> Exp3 -> Var -> PassM (Either Var Exp3)
453+
-- genDerefOrVar locArg bod var = case isMutableCursorTy locArg of
454+
-- True -> genReadMutableCursor locArg bod
455+
-- False ->
456+
457+
isMutableCursorTy :: LocArg -> Bool
458+
isMutableCursorTy locArg = case locArg of
459+
Loc LREM{lremLoc, lremReg, lremEndReg, lremMode} -> case lremMode of
460+
OutputMutable -> True
461+
_ -> False
462+
_ -> False
463+
464+
genReadMutableCursor :: LocArg -> Exp3 -> Var -> PassM Exp3
465+
genReadMutableCursor locArg bod newCursor = do
466+
let derefCursorExp = Ext $ L3.DerefMutableCursor (toLocVar locArg)
467+
return $ LetE (newCursor, [], CursorTy, derefCursorExp) bod
468+
469+
433470
-- Cursorize expressions producing `Packed` values
434471
cursorizePackedExp :: DDefs Ty2 -> FunDefs2 -> DepEnv -> TyEnv Ty2 -> SyncEnv -> Exp2
435472
-> PassM (DiExp Exp3)

0 commit comments

Comments
 (0)