on 385dcbf help sign in
Idris-dev/src/IRTS/Portable.hs
363 lines 16 kB history raw
1{-|
2Module : IRTS.Portable
3Description : Serialise Idris' IR to JSON.
4Copyright :
5License : BSD3
6Maintainer : The Idris Community.
7-}
8{-# LANGUAGE OverloadedStrings #-}
9module IRTS.Portable (writePortable) where
11import Idris.Core.CaseTree
12import Idris.Core.Evaluate
13import Idris.Core.TT
14import IRTS.Bytecode
15import IRTS.CodegenCommon
16import IRTS.Defunctionalise
17import IRTS.Lang
18import IRTS.Simplified
20import Data.Aeson
21import qualified Data.ByteString.Lazy as B
22import qualified Data.Text as T
23import System.IO
25data CodegenFile = CGFile {
26 fileType :: String,
27 version :: Int,
28 cgInfo :: CodegenInfo
31-- Update the version when the format changes
32formatVersion :: Int
33formatVersion = 3
35writePortable :: Handle -> CodegenInfo -> IO ()
36writePortable file ci = do
37 let json = encode $ CGFile "idris-codegen" formatVersion ci
38 B.hPut file json
40instance ToJSON CodegenFile where
41 toJSON (CGFile ft v ci) = object ["file-type" .= ft,
42 "version" .= v,
43 "codegen-info" .= toJSON ci]
45instance ToJSON CodegenInfo where
46 toJSON ci = object ["output-file" .= (outputFile ci),
47 "includes" .= (includes ci),
48 "import-dirs" .= (importDirs ci),
49 "compile-objs" .= (compileObjs ci),
50 "compile-libs" .= (compileLibs ci),
51 "compiler-flags" .= (compilerFlags ci),
52 "interfaces" .= (interfaces ci),
53 "exports" .= (exportDecls ci),
54 "lift-decls" .= (liftDecls ci),
55 "defun-decls" .= (defunDecls ci),
56 "simple-decls" .= (simpleDecls ci),
57 "bytecode" .= (map toBC (simpleDecls ci)),
58 "tt-decls" .= (ttDecls ci)]
60instance ToJSON Name where
61 toJSON n = toJSON $ showCG n
63instance ToJSON ExportIFace where
64 toJSON (Export n f xs) = object ["ffi-desc" .= n,
65 "interface-file" .= f,
66 "exports" .= xs]
68instance ToJSON FDesc where
69 toJSON (FCon n) = object ["FCon" .= n]
70 toJSON (FStr s) = object ["FStr" .= s]
71 toJSON (FUnknown) = object ["FUnknown" .= Null]
72 toJSON (FIO fd) = object ["FIO" .= fd]
73 toJSON (FApp n xs) = object ["FApp" .= (n, xs)]
75instance ToJSON Export where
76 toJSON (ExportData fd) = object ["ExportData" .= fd]
77 toJSON (ExportFun n dsc ret args) = object ["ExportFun" .= (n, dsc, ret, args)]
79instance ToJSON LDecl where
80 toJSON (LFun opts name args def) = object ["LFun" .= (opts, name, args, def)]
81 toJSON (LConstructor name tag ar) = object ["LConstructor" .= (name, tag, ar)]
83instance ToJSON LOpt where
84 toJSON Inline = String "Inline"
85 toJSON NoInline = String "NoInline"
87instance ToJSON LExp where
88 toJSON (LV lv) = object ["LV" .= lv]
89 toJSON (LApp tail exp args) = object ["LApp" .= (tail, exp, args)]
90 toJSON (LLazyApp name exps) = object ["LLazyApp" .= (name, exps)]
91 toJSON (LLazyExp exp) = object ["LLazyExp" .= exp]
92 toJSON (LForce exp) = object ["LForce" .= exp]
93 toJSON (LLet name a b) = object ["LLet" .= (name, a, b)]
94 toJSON (LLam args exp) = object ["LLam" .= (args, exp)]
95 toJSON (LProj exp i) = object ["LProj" .= (exp, i)]
96 toJSON (LCon lv i n exps) = object ["LCon" .= (lv, i, n, exps)]
97 toJSON (LCase ct exp alts) = object ["LCase" .= (ct, exp, alts)]
98 toJSON (LConst c) = object ["LConst" .= c]
99 toJSON (LForeign fd ret exps) = object ["LForeign" .= (fd, ret, exps)]
100 toJSON (LOp prim exps) = object ["LOp" .= (prim, exps)]
101 toJSON LNothing = object ["LNothing" .= Null]
102 toJSON (LError s) = object ["LError" .= s]
104instance ToJSON LVar where
105 toJSON (Loc i) = object ["Loc" .= i]
106 toJSON (Glob n) = object ["Glob" .= n]
108instance ToJSON CaseType where
109 toJSON Updatable = String "Updatable"
110 toJSON Shared = String "Shared"
112instance ToJSON LAlt where
113 toJSON (LConCase i n ns exp) = object ["LConCase" .= (i, n, ns, exp)]
114 toJSON (LConstCase c exp) = object ["LConstCase" .= (c, exp)]
115 toJSON (LDefaultCase exp) = object ["LDefaultCase" .= exp]
117instance ToJSON Const where
118 toJSON (I i) = object ["int" .= i]
119 toJSON (BI i) = object ["bigint" .= (show i)]
120 toJSON (Fl d) = object ["double" .= d]
121 toJSON (Ch c) = object ["char" .= (show c)]
122 toJSON (Str s) = object ["string" .= s]
123 toJSON (B8 b) = object ["bits8" .= b]
124 toJSON (B16 b) = object ["bits16" .= b]
125 toJSON (B32 b) = object ["bits32" .= b]
126 toJSON (B64 b) = object ["bits64" .= b]
127 toJSON (AType at) = object ["atype" .= at]
128 toJSON StrType = object ["strtype" .= Null]
129 toJSON WorldType = object ["worldtype" .= Null]
130 toJSON TheWorld = object ["theworld" .= Null]
131 toJSON VoidType = object ["voidtype" .= Null]
132 toJSON Forgot = object ["forgot" .= Null]
134instance ToJSON ArithTy where
135 toJSON (ATInt it) = object ["ATInt" .= it]
136 toJSON ATFloat = object ["ATFloat" .= Null]
138instance ToJSON IntTy where
139 toJSON it = toJSON $ intTyName it
141instance ToJSON PrimFn where
142 toJSON (LPlus aty) = object ["LPlus" .= aty]
143 toJSON (LMinus aty) = object ["LMinus" .= aty]
144 toJSON (LTimes aty) = object ["LTimes" .= aty]
145 toJSON (LUDiv aty) = object ["LUDiv" .= aty]
146 toJSON (LSDiv aty) = object ["LSDiv" .= aty]
147 toJSON (LURem ity) = object ["LURem" .= ity]
148 toJSON (LSRem aty) = object ["LSRem" .= aty]
149 toJSON (LAnd ity) = object ["LAnd" .= ity]
150 toJSON (LOr ity) = object ["LOr" .= ity]
151 toJSON (LXOr ity) = object ["LXOr" .= ity]
152 toJSON (LCompl ity) = object ["LCompl" .= ity]
153 toJSON (LSHL ity) = object ["LSHL" .= ity]
154 toJSON (LLSHR ity) = object ["LLSHR" .= ity]
155 toJSON (LASHR ity) = object ["LASHR" .= ity]
156 toJSON (LEq aty) = object ["LEq" .= aty]
157 toJSON (LLt ity) = object ["LLt" .= ity]
158 toJSON (LLe ity) = object ["LLe" .= ity]
159 toJSON (LGt ity) = object ["LGt" .= ity]
160 toJSON (LGe ity) = object ["LGe" .= ity]
161 toJSON (LSLt aty) = object ["LSLt" .= aty]
162 toJSON (LSLe aty) = object ["LSLe" .= aty]
163 toJSON (LSGt aty) = object ["LSGt" .= aty]
164 toJSON (LSGe aty) = object ["LSGe" .= aty]
165 toJSON (LZExt from to) = object ["LZExt" .= (from, to)]
166 toJSON (LSExt from to) = object ["LSExt" .= (from, to)]
167 toJSON (LTrunc from to) = object ["LTrunc" .= (from, to)]
168 toJSON LStrConcat = object ["LStrConcat" .= Null]
169 toJSON LStrLt = object ["LStrLt" .= Null]
170 toJSON LStrEq = object ["LStrEq" .= Null]
171 toJSON LStrLen = object ["LStrLen" .= Null]
172 toJSON (LIntFloat ity) = object ["LIntFloat" .= ity]
173 toJSON (LFloatInt ity) = object ["LFloatInt" .= ity]
174 toJSON (LIntStr ity) = object ["LIntStr" .= ity]
175 toJSON (LStrInt ity) = object ["LStrInt" .= ity]
176 toJSON (LIntCh ity) = object ["LIntCh" .= ity]
177 toJSON (LChInt ity) = object ["LChInt" .= ity]
178 toJSON LFloatStr = object ["LFloatStr" .= Null]
179 toJSON LStrFloat = object ["LStrFloat" .= Null]
180 toJSON (LBitCast from to) = object ["LBitCast" .= (from, to)]
181 toJSON LFExp = object ["LFExp" .= Null]
182 toJSON LFLog = object ["LFLog" .= Null]
183 toJSON LFSin = object ["LFSin" .= Null]
184 toJSON LFCos = object ["LFCos" .= Null]
185 toJSON LFTan = object ["LFTan" .= Null]
186 toJSON LFASin = object ["LFASin" .= Null]
187 toJSON LFACos = object ["LFACos" .= Null]
188 toJSON LFATan = object ["LFATan" .= Null]
189 toJSON LFSqrt = object ["LFSqrt" .= Null]
190 toJSON LFFloor = object ["LFFloor" .= Null]
191 toJSON LFCeil = object ["LFCeil" .= Null]
192 toJSON LFNegate = object ["LFNegate" .= Null]
193 toJSON LStrHead = object ["LStrHead" .= Null]
194 toJSON LStrTail = object ["LStrTail" .= Null]
195 toJSON LStrCons = object ["LStrCons" .= Null]
196 toJSON LStrIndex = object ["LStrIndex" .= Null]
197 toJSON LStrRev = object ["LStrRev" .= Null]
198 toJSON LStrSubstr = object ["LStrSubstr" .= Null]
199 toJSON LReadStr = object ["LReadStr" .= Null]
200 toJSON LWriteStr = object ["LWriteStr" .= Null]
201 toJSON LSystemInfo = object ["LSystemInfo" .= Null]
202 toJSON LFork = object ["LFork" .= Null]
203 toJSON LPar = object ["LPar" .= Null]
204 toJSON (LExternal name) = object ["LExternal" .= name]
205 toJSON LCrash = object ["LCrash" .= Null]
206 toJSON LNoOp = object ["LNoOp" .= Null]
208instance ToJSON DDecl where
209 toJSON (DFun name args exp) = object ["DFun" .= (name, args, exp)]
210 toJSON (DConstructor name tag arity) = object ["DConstructor" .= (name, tag, arity)]
212instance ToJSON DExp where
213 toJSON (DV lv) = object ["DV" .= lv]
214 toJSON (DApp tail name exps) = object ["DApp" .= (tail, name, exps)]
215 toJSON (DLet name a b) = object ["DLet" .= (name, a, b)]
216 toJSON (DUpdate name exp) = object ["DUpdate" .= (name,exp)]
217 toJSON (DProj exp i) = object ["DProj" .= (exp, i)]
218 toJSON (DC lv i name exp) = object ["DC" .= (lv, i, name, exp)]
219 toJSON (DCase ct exp alts) = object ["DCase" .= (ct, exp, alts)]
220 toJSON (DChkCase exp alts) = object ["DChkCase" .= (exp, alts)]
221 toJSON (DConst c) = object ["DConst" .= c]
222 toJSON (DForeign fd ret exps) = object ["DForeign" .= (fd, ret, exps)]
223 toJSON (DOp prim exps) = object ["DOp" .= (prim, exps)]
224 toJSON DNothing = object ["DNothing" .= Null]
225 toJSON (DError s) = object ["DError" .= s]
227instance ToJSON DAlt where
228 toJSON (DConCase i n ns exp) = object ["DConCase" .= (i, n, ns, exp)]
229 toJSON (DConstCase c exp) = object ["DConstCase" .= (c, exp)]
230 toJSON (DDefaultCase exp) = object ["DDefaultCase" .= exp]
232instance ToJSON SDecl where
233 toJSON (SFun name args i exp) = object ["SFun" .= (name, args, i, exp)]
235instance ToJSON SExp where
236 toJSON (SV lv) = object ["SV" .= lv]
237 toJSON (SApp tail name exps) = object ["SApp" .= (tail, name, exps)]
238 toJSON (SLet lv a b) = object ["SLet" .= (lv, a, b)]
239 toJSON (SUpdate lv exp) = object ["SUpdate" .= (lv, exp)]
240 toJSON (SProj lv i) = object ["SProj" .= (lv, i)]
241 toJSON (SCon lv i name vars) = object ["SCon" .= (lv, i, name, vars)]
242 toJSON (SCase ct lv alts) = object ["SCase" .= (ct, lv, alts)]
243 toJSON (SChkCase lv alts) = object ["SChkCase" .= (lv, alts)]
244 toJSON (SConst c) = object ["SConst" .= c]
245 toJSON (SForeign fd ret exps) = object ["SForeign" .= (fd, ret, exps)]
246 toJSON (SOp prim vars) = object ["SOp" .= (prim, vars)]
247 toJSON SNothing = object ["SNothing" .= Null]
248 toJSON (SError s) = object ["SError" .= s]
250instance ToJSON SAlt where
251 toJSON (SConCase i j n ns exp) = object ["SConCase" .= (i, j, n, ns, exp)]
252 toJSON (SConstCase c exp) = object ["SConstCase" .= (c, exp)]
253 toJSON (SDefaultCase exp) = object ["SDefaultCase" .= exp]
255instance ToJSON BC where
256 toJSON (ASSIGN r1 r2) = object ["ASSIGN" .= (r1, r2)]
257 toJSON (ASSIGNCONST r c) = object ["ASSIGNCONST" .= (r, c)]
258 toJSON (UPDATE r1 r2) = object ["UPDATE" .= (r1, r2)]
259 toJSON (MKCON con mr i regs) = object ["MKCON" .= (con, mr, i, regs)]
260 toJSON (CASE b r alts def) = object ["CASE" .= (b, r, alts, def)]
261 toJSON (PROJECT r loc arity) = object ["PROJECT" .= (r, loc, arity)]
262 toJSON (PROJECTINTO r1 r2 loc) = object ["PROJECTINTO" .= (r1, r2, loc)]
263 toJSON (CONSTCASE r alts def) = object ["CONSTCASE" .= (r, alts, def)]
264 toJSON (CALL name) = object ["CALL" .= name]
265 toJSON (TAILCALL name) = object ["TAILCALL" .= name]
266 toJSON (FOREIGNCALL r fd ret exps) = object ["FOREIGNCALL" .= (r, fd, ret, exps)]
267 toJSON (SLIDE i) = object ["SLIDE" .= i]
268 toJSON (RESERVE i) = object ["RESERVE" .= i]
269 toJSON (ADDTOP i) = object ["ADDTOP" .= i]
270 toJSON (TOPBASE i) = object ["TOPBASE" .= i]
271 toJSON (BASETOP i) = object ["BASETOP" .= i]
272 toJSON REBASE = object ["REBASE" .= Null]
273 toJSON STOREOLD = object ["STOREOLD" .= Null]
274 toJSON (OP r prim args) = object ["OP" .= (r, prim, args)]
275 toJSON (NULL r) = object ["NULL" .= r]
276 toJSON (ERROR s) = object ["ERROR" .= s]
278instance ToJSON Reg where
279 toJSON RVal = object ["RVal" .= Null]
280 toJSON (T i) = object ["T" .= i]
281 toJSON (L i) = object ["L" .= i]
282 toJSON Tmp = object ["Tmp" .= Null]
284instance ToJSON RigCount where
285 toJSON r = object ["RigCount" .= show r]
287instance ToJSON Totality where
288 toJSON t = object ["Totality" .= show t]
290instance ToJSON MetaInformation where
291 toJSON m = object ["MetaInformation" .= show m]
293instance ToJSON Def where
294 toJSON (Function ty tm) = object ["Function" .= (ty, tm)]
295 toJSON (TyDecl nm ty) = object ["TyDecl" .= (nm, ty)]
296 toJSON (Operator ty n f) = Null -- Operator and CaseOp omits same values as in IBC.hs
297 toJSON (CaseOp info ty argTy _ _ cdefs) = object ["CaseOp" .= (info, ty, argTy, cdefs)]
299instance (ToJSON t) => ToJSON (TT t) where
300 toJSON (P nt name term) = object ["P" .= (nt, name, term)]
301 toJSON (V n) = object ["V" .= n]
302 toJSON (Bind n b tt) = object ["Bind" .= (n, b, tt)]
303 toJSON (App s t1 t2) = object ["App" .= (s, t1, t2)]
304 toJSON (Constant c) = object ["Constant" .= c]
305 toJSON (Proj tt n) = object ["Proj" .= (tt, n)]
306 toJSON Erased = object ["Erased" .= Null]
307 toJSON Impossible = object ["Impossible" .= Null]
308 toJSON (Inferred tt) = object ["Inferred" .= tt]
309 toJSON (TType u) = object ["TType" .= u]
310 toJSON (UType u) = object ["UType" .= (show u)]
312instance ToJSON UExp where
313 toJSON (UVar src n) = object ["UVar" .= (src, n)]
314 toJSON (UVal n) = object ["UVal" .= n]
317instance (ToJSON t) => ToJSON (AppStatus t) where
318 toJSON Complete = object ["Complete" .= Null]
319 toJSON MaybeHoles = object ["MaybeHoles" .= Null]
320 toJSON (Holes ns) = object ["Holes" .= ns]
322instance (ToJSON t) => ToJSON (Binder t) where
323 toJSON (Lam rc bty) = object ["Lam" .= (rc, bty)]
324 toJSON (Pi c i t k) = object ["Pi" .= (c, i, t, k)]
325 toJSON (Let t v) = object ["Let" .= (t, v)]
326 toJSON (NLet t v) = object ["NLet" .= (t, v)]
327 toJSON (Hole t) = object ["Hole" .= (t)]
328 toJSON (GHole l ns t) = object ["GHole" .= (l, ns, t)]
329 toJSON (Guess t v) = object ["Guess" .= (t, v)]
330 toJSON (PVar rc t) = object ["PVar" .= (rc, t)]
331 toJSON (PVTy t) = object ["PVTy" .= (t)]
333instance ToJSON ImplicitInfo where
334 toJSON (Impl a b c) = object ["Impl" .= (a, b, c)]
336instance ToJSON NameType where
337 toJSON Bound = object ["Bound" .= Null]
338 toJSON Ref = object ["Ref" .= Null]
339 toJSON (DCon a b c) = object ["DCon" .= (a, b, c)]
340 toJSON (TCon a b) = object ["TCon" .= (a, b)]
342instance ToJSON CaseDefs where
343 toJSON (CaseDefs rt ct) = object ["Runtime" .= rt, "Compiletime" .= ct]
345instance (ToJSON t) => ToJSON (SC' t) where
346 toJSON (Case ct n alts) = object ["Case" .= (ct, n, alts)]
347 toJSON (ProjCase t alts) = object ["ProjCase" .= (t, alts)]
348 toJSON (STerm t) = object ["STerm" .= t]
349 toJSON (UnmatchedCase s) = object ["UnmatchedCase" .= s]
350 toJSON ImpossibleCase = object ["ImpossibleCase" .= Null]
352instance (ToJSON t) => ToJSON (CaseAlt' t) where
353 toJSON (ConCase n c ns sc) = object ["ConCase" .= (n, c, ns, sc)]
354 toJSON (FnCase n ns sc) = object ["FnCase" .= (n, ns, sc)]
355 toJSON (ConstCase c sc) = object ["ConstCase" .= (c, sc)]
356 toJSON (SucCase n sc) = object ["SucCase" .= (n, sc)]
357 toJSON (DefaultCase sc) = object ["DefaultCase" .= sc]
359instance ToJSON CaseInfo where
360 toJSON (CaseInfo a b c) = object ["CaseInfo" .= (a, b, c)]
362instance ToJSON Accessibility where
363 toJSON a = object ["Accessibility" .= show a]