@@ -34,22 +34,32 @@ module Feldspar.Compiler.Imperative.ArrayOps (arrayOps) where
3434import Feldspar.Compiler.Imperative.Representation
3535import Feldspar.Compiler.Imperative.Frontend
3636 (litI32 , deepCopy , fun , call , for , toBlock , mkIf , isShallow , variant , arrayFun , freeArrayE ,
37- lowerCopy , mkSequence , elemTyAwL , isAwLType )
37+ lowerCopy , mkSequence , elemTyAwL , isAwLType , encodeType )
3838import Feldspar.Range (fullRange )
3939import Feldspar.Core.Types (Length )
4040import Feldspar.Compiler.Backend.C.Options (Options )
4141
42- import Data.List (nub , isPrefixOf , concatMap )
42+ import Data.List (nub , isPrefixOf , concatMap , sortBy )
4343import Control.Monad.Writer (Writer (.. ), runWriter , tell , censor )
44+ import Data.Bifunctor (bimap )
4445
4546-- | Main interface for adding needed array operations to a module.
4647arrayOps :: Options -> Module () -> Module ()
47- arrayOps opts (Module ents) = Module $ concatMap mkArrayOps dts ++ ents'
48+ arrayOps opts (Module ents) = Module $ concatMap mkArrayOps sdts ++ ents'
4849 where dts = filter (not . either isShallow isShallow) lrts
50+ sdts = map (bimap snd snd ) $ sortBy compFst $ map (bimap rank rank) dts
4951 (ents',lrts) = lower opts ents
5052 mkArrayOps (Left t) = [mkInitArray opts t, mkFreeArray opts t]
5153 mkArrayOps (Right t) = [mkCopyArrayPos opts t, mkCopyArray opts t, mkInitCopyArray opts t]
5254
55+ -- | Annotate a type with a measure of the size of the type
56+ rank :: Type -> (Int , Type )
57+ rank t = (length $ encodeType t, t)
58+
59+ -- | Compare two pairs only on their first components
60+ compFst :: Ord a => Either (a ,b ) (a ,b ) -> Either (a ,c ) (a ,c ) -> Ordering
61+ compFst x y = compare (bimap fst fst x) (bimap fst fst y)
62+
5363-- | Copying an array to a given position in the destination
5464mkCopyArrayPos :: Options -> Type -> Entity ()
5565mkCopyArrayPos opts t = Proc name False [dstVar, dstLVar, srcVar, srcLVar, posVar] (typeof dstVar) (Just body)
0 commit comments