qq to gfortran

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE QuasiQuotes #-}

import F2Hs
import Foreign
import Text.Printf

[f|
function a(b,c)

    integer(8) :: a, b(10,2), i
    real(8) :: c

    do i = 1,2
        a = a + sum(b(:,i))
    end do
    a = a + c

end function a
|]


main = do
    -- b <- new (listArray (1,10) [1..])
    let bn = [10..29]
    allocaArray (length bn) $ \b -> do
    pokeArray b bn
    c <- new 3
    a' <- a b c
    printf "fortran %d\n" a'
    printf "haskell %d\n" (3 + sum bn)

{-
t1.hs:1:1: Splicing declarations
"\nfunction a(b,c)\n\n    integer(8) :: a, b(10,2), i\n    real(8) :: c\n\n    do i = 1,2\n        a = a + sum(b(:,i))\n    end do\n    a = a + c\n\nend function a\n"
======>
[foreign import ccall safe "static a_" a
:: Ptr Int64 -> Ptr Double -> IO Int64]
Ok, modules loaded: Main, F2Hs, F, F.Type.
*Main> main
fortran 393
haskell 393
-}

qq to gfortran (annotation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module F2Hs where

import Prelude hiding (foldl,foldr,sequence,mapM)
import Control.Applicative
import Control.Monad hiding (mapM,sequence)
import Data.ByteString.Char8 ()
import Data.Char
import Data.Foldable
import Data.Int
import Data.Maybe
import Data.Monoid
import Data.String
import Data.Traversable
import Data.Foldable
import Foreign
import Foreign.C.Types
import Language.Haskell.TH as TH
import Language.Haskell.TH.Quote as TH
import Language.Haskell.TH.Syntax as TH
import qualified Data.ByteString as B
import qualified Data.Map as M
import qualified F
import qualified Text.Trifecta.CharSet as CharSet
import qualified Text.Trifecta.Parser.Step as T
import qualified Text.Trifecta.Highlight.Prim as T
import System.Process
import Text.Trifecta
import Text.Trifecta.CharSet (CharSet)
import Text.Trifecta.Parser.Token.Style
import Data.Array.Unboxed

-- | for now, run @ghc -L. -la@ twice
f = TH.QuasiQuoter { quoteDec = \fun -> do

        runIO $ do
            writeFile "liba.f90" fun
            system "gfortran -fPIC -shared -g -o liba.so liba.f90"

        join $ steppp pSubprogram' fun }


steppp :: P a -> String -> Q a
steppp (P p) s = case T.starve $ T.feed s $
    stepParser (fmap prettyTerm) (why prettyTerm) (release mempty *> p)
                mempty True mempty mempty
    of
        Success diag a -> return a
        Failure diag   -> do
                runIO (parseTest (fmap I p) s)
                fail "parse failure"

newtype Invisible a = I a
instance Show (Invisible a) where show _ = ""


parseTests = do
    parseTest (unP pType) "integer, parameter :: x, y = 3, z(4,32), a(y,3)"
    parseTest (unP pSubprogram) "function foo (x) real(8) :: x, foo \n foo = x end function foo"

instance MonadTokenParser P where
    whiteSpace = skipMany (oneOf " \t")
    -- buildWhiteSpaceParser emptyCommentStyle{ commentStart = "!" }
-- and strip out lines starting with c

newtype P a = P { unP :: Parser String a }
    deriving (Functor, Applicative, Monad, MonadParser, Alternative, MonadPlus)

wsp = skipMany (oneOf " \n\t")

pSubprogram'' f = do
    wsp
    spty <- highlight T.ReservedIdentifier $
            F.Function <> "function" <|> F.Subroutine <> "subroutine"
    r <- var
    args <- case spty of
        F.Function -> do
            parens ( commaSep var )
        F.Subroutine -> do
            fmap (Prelude.concat . maybeToList) $ optional (parens (many var))

    wsp

    tds <- optional pType `sepBy` (newline *> wsp)

    return $ f spty  r  args  (M.unions $ catMaybes tds)

pSubprogram = pSubprogram'' (,,,)

pSubprogram' = pSubprogram'' mkForeignD

mkForeignD F.Function fname args tds = do
    runIO $ print tds
    runIO $ print $ mapMaybe  (\x -> case x `M.lookup` tds of
                    Just {} -> Nothing
                    _ -> Just x)
        (fname : args)
    fname2 <- newName fname
    sequence
     [
        -- import without any wrapper
        TH.forImpD CCall Safe (fname++"_") -- or other mangling?
                            fname2 $
                foldr (\x y -> [t| $x -> $y |])
                        (addIO $ toTy $ tds M.! fname)
                        (map (addPtr . toTy . (tds M.!)) args),
        -- wrapper
        TH.funD (mkName fname)
            [ do
                as <- mapM newName args
                -- insert IO conversions?
                clause (map varP as)
                       (normalB (foldl appE (varE fname2) (map varE as)))
                       []
            ]
        ]
   where
    addPtr x = [t| Ptr $x |]
    addIO  x = [t| IO $x  |]
    toTy ((pty,F.Kind (F.ILit k)), Nothing, _dim , Nothing) = primTy pty k


    -- toTy ((pty,F.Kind (F.ILit k)), _ , Just dim, Nothing) = primTy pty k
    {-
    toTy ((pty,F.Kind (F.ILit k)), _ , Just dim, Nothing)
                = let nds = length dim
                in [t| $(Prelude.foldr appT (primTy pty k)
                                    $ replicate (nds-1) [t| Ptr |])
                                 |]
                                 -}
    {-
        | Just ds <- mapM (\x -> case x of
                        (F.Len (F.ILit l), F.Len (F.ILit u)) -> Just (l,u)
                        _ -> Nothing) -}


    primTy pty k = case (pty,k) of
            (F.Real,4)    -> [t| Float |]
            (F.Real,8)    -> [t| Double |]
            (F.Integer,8) -> [t| Int64 |]
            (F.Integer,4) -> [t| Int32 |]
            (F.Integer,2) -> [t| Int16 |]
            (F.Integer,1) -> [t| Int8  |]

pType = do
    t <- type_spec
    attrs <- traverse (\_ -> commaSep attr_spec) =<< optional comma

    (case attrs of Nothing -> optional; _ -> fmap Just) $ symbol "::"

    entities <- commaSep (some v)

    return (M.map (\(dim,assgn) -> (t,attrs,dim,assgn))
                $ M.unions $ Prelude.concat entities)

  where
    v = do
        r <- var
        dim <- optional dims
        assgn <- optional (char '=' *> some (noneOf ",\n"))
        return (M.singleton r (dim,assgn))



x <> y = x <$ symbol y

attr_spec :: P F.Attr
attr_spec = choice [

    F.Allocatable <> "allocatable",
    F.Intrinsic   <> "intrinsic",
    F.Parameter   <> "parameter",
    F.Pointer     <> "pointer",
    F.Pure        <> "pure",
    F.Target      <> "target",
    F.Dimension <$> (symbol "dimension" *> dims),
    F.Intent    <$> (symbol "intent" *> intent)

    ]

intent :: P F.Intent
intent = parens $
    choice [
            F.InOut <$ (symbol "in" *> symbol "out"),
            F.In <> "in",
            F.Out <> "out"
    ]

var = do
    a <- noneOfSet (digitChars `CharSet.union` symbolChars `CharSet.union` wsp)
        <?> "variable"
    rest <- many (noneOfSet (symbolChars `CharSet.union` wsp))
    whiteSpace
    return (a:rest)

    where wsp = CharSet.fromList " \n\t"

digitChars = CharSet.fromList "1234567890"
symbolChars = CharSet.fromList "-+@#$%^&*={}[]()\\|'\";:<>,./?"

int = (F.IVar <$> var <?> "integer variable") <* whiteSpace <|>
    F.ILit <$> integer

dims :: P [(F.Length, F.Length)]
dims = parens $ commaSep1 $ do
    n1 <- Right <$> int  <|> Left <$> l1

    either (\x -> return (dim0,x))
      (\n1 -> do
        c <- optional colon
        n2 <- traverse (\_ -> int <?> "upper bound") c
        return $ maybe (dim0,F.Len n1) ((,) (F.Len n1) . F.Len) n2
        )
        n1

    where
        l1 = choice[
            F.LDeferred <$ colon,
            F.LAssumed  <$ symbolic '*']
        dim0 = F.Len (F.ILit 1)

type_spec :: P (F.PrimType, F.Kind)
type_spec = liftA2 (,) type_spec' (kind (F.Kind (F.ILit 4)))
        <|>
            liftA2 (,) (F.Real <> "double precision")
                  (pure (F.Kind (F.ILit 8)))
    where
    type_spec' :: P F.PrimType
    type_spec' = choice [
            F.Character <> "character",
            F.Complex   <> "complex",
            F.Integer   <> "integer",
            F.Logical   <> "logical",
            F.Real      <> "real"
        ]

kind :: F.Kind -> P F.Kind
kind defaultKind = do
    oldKind <- optional (symbolic '*')
    case oldKind of
        Just {} -> F.Kind <$> int
        Nothing -> try (

                parens $ do
                    optional (symbol "kind" *> symbolic '=')
                    choice [
                        F.Kind <$> int,
                        fun2 F.SelectedReal "selected_real_kind",
                        fun2 F.SelectedInt  "selected_int_kind"
                        ]

            ) <|> pure defaultKind -- and support type_spec double precision

    where fun2 hsCon funName = symbol funName
                            *> parens (liftA2 hsCon int int)


qq to gfortran (annotation) (annotation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{-# LANGUAGE QuasiQuotes #-}
module Testabc where
import RlangQQ
import DwarfQQ

import qualified Data.Vector.Storable as V

[fortran|
subroutine test(a,b)
  real*8 b(3)
  b(:) = a+b
end

subroutine cspline(m,x,n,xs,ys)
  integer,       intent(in) :: m,n
  real (kind=8), intent(in) :: xs(n),ys(n)
  real (kind=8), intent(inout) :: x(m)

  integer, parameter :: maxK = 5
  real (kind=8) :: s(n,6), break(n), coef(n,maxK)

  call tautsp (xs,ys, n, 0, s, break, coef, l, k, iflag )
  do i=1,m
  x(i) = ppvalu (break, coef, l, k, x(i), 0)
  end do
end

|]

main = do
  so <- dlopen "./test.so" [RTLD_NOW]

  do
    b <- V.thaw (V.fromList [1,2,3])
    test so 3 b
    print =<< V.freeze b

  xs <- V.thaw (V.fromList [1,2,3,4,5,6])
  ys <- V.thaw (V.fromList [5,4,5,7,6,5.5])

  let xi = V.fromList [1, 1.01 .. 6]
  yi <- V.thaw xi

  cspline so (fromIntegral (V.length xi)) yi 6 xs ys
  yi <- V.freeze yi

  [r| library(ggplot2)
      df <- data.frame(x = $(xi), y = $(yi))
      ggplot(df, aes(x,y)) + geom_path()
      |]

  dlclose so

-----------------------------------------------------------------
--      DwarfQQ.hs                                             --
-----------------------------------------------------------------
{-# LANGUAGE TemplateHaskell #-}
{- | a quasiquote to contain a foreign language (gfortran) and generate
haskell functions that use libffi to call those functions/procedures.

-}
module DwarfQQ
  (fortran,
   module System.Posix.DynamicLinker,
  ) where

import Control.Applicative
import Control.Monad
import Data.Dwarf
import Data.Dwarf.ADT
import Data.Dwarf.Elf
import Data.Int
import Data.Maybe
import Data.Word
import Foreign.LibFFI
import Foreign.Marshal
import Foreign.Ptr
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
import qualified Data.Vector.Storable.Mutable as VM
import System.Cmd
import System.Posix.DynamicLinker

{- | includes
http://people.sc.fsu.edu/~%20jburkardt/f_src/pppack/pppack.html

-}
fortran = QuasiQuoter {
  quoteExp = error "fortran",
  quotePat = error "fortran",
  quoteType = error "fortran",
  quoteDec = \s -> do
  sps <- runIO $ do
    writeFile "test.f90" s
    system "test -e 'pppack.a' || gfortran -O3 -fPIC pppack.f90 -c -o pppack.a"
    system "gfortran -fbounds-check -fdefault-real-8 -O3 -fPIC -g -shared pppack.a test.f90 -o test.so"
    (Dwarf as,b) <- parseElfDwarfADT LittleEndian "test.so"
    return [ sp | Boxed _ a <- as, Boxed _ (DefSubprogram sp) <- cuDefs a,
                  subprogExternal sp ]
  sequence [ do
            dlName <- newName "dlName"
            funD (mkName (subprogName sp))
                [clause [varP dlName] (normalB (callSubprogram sp dlName)) []]
    | sp <- sps ]
  }


mangle :: String -> String
mangle x = x ++ "_"

callSubprogram :: Subprogram -> Name -> ExpQ
callSubprogram sp dlName = do
  ps <- zipWithM (\(Boxed _ x) n -> do
                  n' <- newName (fromMaybe ("x"++ show n) (formalParamName x))
                  return (n', x))
      (subprogFormalParameters sp)
      [1 .. ]
  lamE (map (varP . fst) ps) $
    [| do
        fn <- dlsym $(varE dlName) $(lift (mangle (subprogName sp)))
        $(foldr
            (\(n',FormalParameter { formalParamType = TypeRef (Boxed _ ty) }) accum ->
                  dtMarshal ty n' accum)
            [| callFFI fn retVoid $(listE (map (varE . fst) ps)) |]
            ps)
      |]


dtMarshal :: DefType -> Name -> ExpQ -> ExpQ
dtMarshal (DefArrayType (ArrayType { atSubrangeType = [Boxed _ (SubrangeType up rt)],
                   atType = TypeRef (Boxed _ (DefBaseType at)) }))
          n x =
      [| VM.unsafeWith $(varE n) ($(lamE [varP n] x)
          . argPtr . flip asTypeOf (undefined :: Ptr $(conT (btToHS at)))) |]
dtMarshal (DefBaseType dt) n x =
    [| new ($(varE n) :: $(conT (btToHS dt))) >>=
        \ m -> $(lamE [varP n] x) (argPtr m) <* free m |]

dtToHS :: DefType -> TypeQ
dtToHS (DefArrayType (ArrayType { atSubrangeType = [Boxed _ (SubrangeType up rt)],
                   atType = TypeRef (Boxed _ at) })) =
      [t| Ptr $(dtToHS at) |]
dtToHS (DefBaseType dt) = conT (btToHS dt)

btToHS :: BaseType -> Name
btToHS (BaseType { btByteSize = n, btEncoding = enc }) |
  enc `elem` [DW_ATE_unsigned, DW_ATE_unsigned_char] = case n of
    8 -> ''Word64
    4 -> ''Word32
    2 -> ''Word16
    1 -> ''Word8
btToHS (BaseType { btByteSize = n, btEncoding = enc }) |
  enc `elem` [DW_ATE_signed, DW_ATE_signed_char] = case n of
    8 -> ''Int64
    4 -> ''Int32
    2 -> ''Int16
    1 -> ''Int8
btToHS (BaseType { btByteSize = n, btEncoding = enc }) |
  enc `elem` [DW_ATE_float] = case n of
    8 -> ''Double
    4 -> ''Float
{-
  = DW_ATE_address
  | DW_ATE_boolean
  | DW_ATE_complex_float
  | DW_ATE_imaginary_float
  | DW_ATE_packed_decimal
  | DW_ATE_numeric_string
  | DW_ATE_edited
  | DW_ATE_signed_fixed
  | DW_ATE_unsigned_fixed
  | DW_ATE_decimal_float
-}