Skip to content

Commit 8bc6eab

Browse files
committed
Make sure output locations are re-written to in redirection case
1 parent d9a25a4 commit 8bc6eab

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

gibbon-compiler/examples/simple_tests/tree.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ sumTree tr =
3030

3131
gibbon_main =
3232
let tree = mkTree 10
33-
-- _ = printPacked tree
33+
--_ = printPacked tree
3434
tree' = add1Tree tree
3535
--_ = printPacked tree'
3636
val = sumTree tree'
@@ -40,4 +40,4 @@ gibbon_main =
4040
rmost = rightmost tree'
4141
_ = printint rmost
4242
_ = printsym (quote ")\n\n")
43-
in sumTree tree'
43+
in () --sumTree tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2707,7 +2707,7 @@ unpackDataCon dcon_var freeVarToVarEnv lenv ddfs fundefs denv1 tenv1 senv isPack
27072707
(toEndFromTaggedV redirection_var_dcon, [], CursorTy, Ext $ AddCursor redirection_var_dcon (VarE (toTagV redirection_var_dcon)))]
27082708

27092709
--generate binds for all fields.
2710-
binds_flields = L.foldr (\((_, idx), var) (index, res) -> let read_cursor_f = if isIndirectionTag dcon || isRedirectionTag dcon
2710+
binds_flields = L.foldl (\(index, res) ((_, idx), var) -> let read_cursor_f = if isIndirectionTag dcon || isRedirectionTag dcon
27112711
then Ext (ReadTaggedCursor (vars_next_fields !! index))
27122712
else error $ "unpackRegularDataCon: cursorty without indirection/redirection."
27132713
tmpf = tmp_flds !! index

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ module Gibbon.Passes.FollowPtrs
55
import qualified Data.Map as M
66
-- import qualified Data.Set as S
77
import qualified Data.List as L
8-
-- import Data.Foldable ( foldrM )
8+
import Data.Foldable ( foldrM )
99
import Data.Maybe ( fromJust )
1010

1111
import Gibbon.Common
1212
import Gibbon.Language
1313
import Gibbon.L2.Syntax as L2
1414
import Gibbon.DynFlags
15+
import Gibbon.L3.Syntax (E3Ext(AddCursor))
1516

1617
--------------------------------------------------------------------------------
1718

@@ -66,12 +67,44 @@ followPtrs (Prog ddefs fundefs mainExp) = do
6667
-- because bounds checking may have updated the value of the location.
6768
-- Note that we only need to update the non packed locations + the data constructor buffer.
6869
-- Other packed types will be updated by the function that traverses it.
70+
(new_out_locs, new_loc_bnds) <- foldrM (\locvar (nl, bnds) -> case locvar of
71+
SoA dloc flocs -> do
72+
-- unpack all locations in the SoA location.
73+
let unpack_dcon = LetLocE (singleLocVar dloc) (GetDataConLocSoA locvar)
74+
let unpack_flds = map (\((dcon, idx), floc) -> do
75+
let flet = LetLocE floc (GetFieldLocSoA (dcon, idx) locvar)
76+
in flet
77+
) flocs
78+
-- make a new name for this loc_var
79+
new_locvar <- freshCommonLoc "copy" locvar
80+
let new_don_loc = getDconLoc new_locvar
81+
-- The data con loc should be unpacked and updated by bounds check.
82+
-- from design of the compiler
83+
let new_don_let = LetLocE new_don_loc (AfterConstantLE 0 (singleLocVar dloc))
84+
let new_fld_locs = getAllFieldLocsSoA new_locvar
85+
new_fld_lets <- foldrM (\((dcon, idx), nfloc) flts -> do
86+
let ty = (lookupDataCon ddefs dcon) !! idx
87+
in case (ty) of
88+
PackedTy{} -> do
89+
let let_for_fld = LetLocE nfloc (GetFieldLocSoA (dcon, idx) locvar)
90+
in pure $ flts ++ [let_for_fld]
91+
_ -> do
92+
let let_for_fld = LetLocE nfloc (AfterConstantLE 0 (getFieldLoc (dcon, idx) locvar))
93+
in pure $ flts ++ [let_for_fld]
94+
) [] new_fld_locs
95+
let new_soa_loc_let = LetLocE new_locvar (GenSoALoc new_don_loc new_fld_locs)
96+
return $ (nl ++ [new_locvar], bnds ++ [unpack_dcon] ++ unpack_flds ++ [new_don_let] ++ new_fld_lets ++ [new_soa_loc_let])
97+
98+
-- no need to update single location variables
99+
Single{} -> return $ (nl ++ [locvar], bnds)
100+
) ([], []) out_locs
69101

70102
let redir_dcon = fst $ fromJust $ L.find (isRedirectionTag . fst) dataCons
71103
let redir_bod = (if isPrinterName funName then LetE (wc,[],ProdTy[],PrimAppE PrintSym [LitSymE (toVar " ->r ")]) else id) $
72-
LetE (callv,endofs,out_ty,AppE funName (in_locs ++ out_locs) args) $
104+
LetE (callv,endofs,out_ty,AppE funName (in_locs ++ new_out_locs) args) $
73105
Ext (RetE endofs callv)
74-
let redir_br = (redir_dcon,[(indir_ptrv,(indir_ptrloc))],redir_bod)
106+
let redir_bod' = foldr (\bnd bod -> Ext $ bnd bod) redir_bod new_loc_bnds
107+
let redir_br = (redir_dcon,[(indir_ptrv,(indir_ptrloc))],redir_bod')
75108
----------------------------------------
76109
(pure (CaseE scrt (brs ++ [redir_br])))
77110
else do

0 commit comments

Comments
 (0)