text
stringlengths 0
1.05M
|
---|
internaliseExp desc (E.LetFun ofname (tparams, params, retdecl, Info rettype, body) letbody _ loc) = do
|
internaliseValBind $
|
E.ValBind Nothing ofname retdecl (Info (rettype, [])) tparams params body Nothing mempty loc
|
internaliseExp desc letbody
|
internaliseExp desc (E.DoLoop sparams mergepat mergeexp form loopbody (Info (ret, retext)) loc) = do
|
ses <- internaliseExp "loop_init" mergeexp
|
((loopbody', (form', shapepat, mergepat', mergeinit')), initstms) <-
|
collectStms $ handleForm ses form
|
addStms initstms
|
mergeinit_ts' <- mapM subExpType mergeinit'
|
ctxinit <- argShapes (map I.paramName shapepat) mergepat' mergeinit_ts'
|
let ctxmerge = zip shapepat ctxinit
|
valmerge = zip mergepat' mergeinit'
|
dropCond = case form of
|
E.While {} -> drop 1
|
_ -> id
|
-- Ensure that the result of the loop matches the shapes of the
|
-- merge parameters. XXX: Ideally they should already match (by
|
-- the source language type rules), but some of our
|
-- transformations (esp. defunctionalisation) strips out some size
|
-- information. For a type-correct source program, these reshapes
|
-- should simplify away.
|
let merge = ctxmerge ++ valmerge
|
merge_ts = map (I.paramType . fst) merge
|
loopbody'' <-
|
localScope (scopeOfFParams $ map fst merge) $
|
inScopeOf form' $
|
insertStmsM $
|
resultBodyM
|
=<< ensureArgShapes
|
"shape of loop result does not match shapes in loop parameter"
|
loc
|
(map (I.paramName . fst) ctxmerge)
|
merge_ts
|
=<< bodyBind loopbody'
|
attrs <- asks envAttrs
|
loop_res <-
|
map I.Var . dropCond
|
<$> attributing
|
attrs
|
(letTupExp desc (I.DoLoop ctxmerge valmerge form' loopbody''))
|
bindExtSizes (E.toStruct ret) retext loop_res
|
return loop_res
|
where
|
sparams' = map (`TypeParamDim` mempty) sparams
|
forLoop mergepat' shapepat mergeinit form' =
|
bodyFromStms $
|
inScopeOf form' $ do
|
ses <- internaliseExp "loopres" loopbody
|
sets <- mapM subExpType ses
|
shapeargs <- argShapes (map I.paramName shapepat) mergepat' sets
|
return
|
( shapeargs ++ ses,
|
( form',
|
shapepat,
|
mergepat',
|
mergeinit
|
)
|
)
|
handleForm mergeinit (E.ForIn x arr) = do
|
arr' <- internaliseExpToVars "for_in_arr" arr
|
arr_ts <- mapM lookupType arr'
|
let w = arraysSize 0 arr_ts
|
i <- newVName "i"
|
bindingLoopParams sparams' mergepat $
|
\shapepat mergepat' ->
|
bindingLambdaParams [x] (map rowType arr_ts) $ \x_params -> do
|
let loopvars = zip x_params arr'
|
forLoop mergepat' shapepat mergeinit $
|
I.ForLoop i Int32 w loopvars
|
handleForm mergeinit (E.For i num_iterations) = do
|
num_iterations' <- internaliseExp1 "upper_bound" num_iterations
|
i' <- internaliseIdent i
|
num_iterations_t <- I.subExpType num_iterations'
|
it <- case num_iterations_t of
|
I.Prim (IntType it) -> return it
|
_ -> error "internaliseExp DoLoop: invalid type"
|
bindingLoopParams sparams' mergepat $
|
\shapepat mergepat' ->
|
forLoop mergepat' shapepat mergeinit $
|
I.ForLoop i' it num_iterations' []
|
handleForm mergeinit (E.While cond) =
|
bindingLoopParams sparams' mergepat $ \shapepat mergepat' -> do
|
mergeinit_ts <- mapM subExpType mergeinit
|
-- We need to insert 'cond' twice - once for the initial
|
-- condition (do we enter the loop at all?), and once with the
|
-- result values of the loop (do we continue into the next
|
-- iteration?). This is safe, as the type rules for the
|
-- external language guarantees that 'cond' does not consume
|
-- anything.
|