text
stringlengths 0
1.05M
|
---|
used_free_params <- forM (namesToList free_in_fun) $ \v -> do
|
v_t <- lookupType v
|
return $ Param v $ toDecl v_t Nonunique
|
let free_shape_params =
|
map (`Param` I.Prim int32) $
|
concatMap (I.shapeVars . I.arrayShape . I.paramType) used_free_params
|
free_params = nub $ free_shape_params ++ used_free_params
|
all_params = free_params ++ shapeparams ++ concat params'
|
let fd =
|
I.FunDef
|
Nothing
|
(internaliseAttrs attrs)
|
fname'
|
rettype'
|
all_params
|
body'
|
if null params'
|
then bindConstant fname fd
|
else
|
bindFunction
|
fname
|
fd
|
( fname',
|
map I.paramName free_params,
|
shapenames,
|
map declTypeOf $ concat params',
|
all_params,
|
applyRetType rettype' all_params
|
)
|
case entry of
|
Just (Info entry') -> generateEntryPoint entry' fb
|
Nothing -> return ()
|
where
|
zeroExts ts = generaliseExtTypes ts ts
|
allDimsFreshInType :: MonadFreshNames m => E.PatternType -> m E.PatternType
|
allDimsFreshInType = bitraverse onDim pure
|
where
|
onDim (E.NamedDim v) =
|
E.NamedDim . E.qualName <$> newVName (baseString $ E.qualLeaf v)
|
onDim _ =
|
E.NamedDim . E.qualName <$> newVName "size"
|
-- | Replace all named dimensions with a fresh name, and remove all
|
-- constant dimensions. The point is to remove the constraints, but
|
-- keep the names around. We use this for constructing the entry
|
-- point parameters.
|
allDimsFreshInPat :: MonadFreshNames m => E.Pattern -> m E.Pattern
|
allDimsFreshInPat (PatternAscription p _ _) =
|
allDimsFreshInPat p
|
allDimsFreshInPat (PatternParens p _) =
|
allDimsFreshInPat p
|
allDimsFreshInPat (Id v (Info t) loc) =
|
Id v <$> (Info <$> allDimsFreshInType t) <*> pure loc
|
allDimsFreshInPat (TuplePattern ps loc) =
|
TuplePattern <$> mapM allDimsFreshInPat ps <*> pure loc
|
allDimsFreshInPat (RecordPattern ps loc) =
|
RecordPattern <$> mapM (traverse allDimsFreshInPat) ps <*> pure loc
|
allDimsFreshInPat (Wildcard (Info t) loc) =
|
Wildcard <$> (Info <$> allDimsFreshInType t) <*> pure loc
|
allDimsFreshInPat (PatternLit e (Info t) loc) =
|
PatternLit e <$> (Info <$> allDimsFreshInType t) <*> pure loc
|
allDimsFreshInPat (PatternConstr c (Info t) pats loc) =
|
PatternConstr c <$> (Info <$> allDimsFreshInType t)
|
<*> mapM allDimsFreshInPat pats
|
<*> pure loc
|
generateEntryPoint :: E.EntryPoint -> E.ValBind -> InternaliseM ()
|
generateEntryPoint (E.EntryPoint e_paramts e_rettype) vb = localConstsScope $ do
|
let (E.ValBind _ ofname _ (Info (rettype, _)) _ params _ _ attrs loc) = vb
|
-- We replace all shape annotations, so there should be no constant
|
-- parameters here.
|
params_fresh <- mapM allDimsFreshInPat params
|
let tparams =
|
map (`E.TypeParamDim` mempty) $
|
S.toList $
|
mconcat $ map E.patternDimNames params_fresh
|
bindingParams tparams params_fresh $ \shapeparams params' -> do
|
entry_rettype <- internaliseEntryReturnType $ anySizes rettype
|
let entry' = entryPoint (zip e_paramts params') (e_rettype, entry_rettype)
|
args = map (I.Var . I.paramName) $ concat params'
|
entry_body <- insertStmsM $ do
|
-- Special case the (rare) situation where the entry point is
|
-- not a function.
|
maybe_const <- lookupConst ofname
|
vals <- case maybe_const of
|
Just ses ->
|
return ses
|
Nothing ->
|
fst <$> funcall "entry_result" (E.qualName ofname) args loc
|
ctx <-
|
extractShapeContext (concat entry_rettype)
|
<$> mapM (fmap I.arrayDims . subExpType) vals
|
resultBodyM (ctx ++ vals)
|