1module IRTS.CodegenC (codegenC) where
7import IRTS.Defunctionalise
9import IRTS.CodegenCommon
16import Data.List (intercalate, nubBy)
20import System.Directory
21import System.FilePath ((</>), (<.>))
26codegenC :: CodeGenerator
27codegenC ci = do codegenC' (simpleDecls ci)
32 (map mkLib (compileLibs ci) ++
33 map incdir (importDirs ci))
38 when (interfaces ci) $
39 codegenH (exportDecls ci)
41 where mkLib l = "-l" ++ l
44codegenC' :: [(Name, SDecl)] ->
45 String -> -- output file name
46 OutputType -> -- generate executable if True, only .o if False
47 [FilePath] -> -- include files
48 [String] -> -- extra object files
49 [String] -> -- extra compiler flags (libraries)
50 [String] -> -- extra compiler flags (anything)
52 Bool -> -- interfaces too (so make a .o instead)
55codegenC' defs out exec incs objs libs flags exports iface dbg
57 let bc = map toBC defs
58 let wrappers = genWrappers bc
59 let h = concatMap toDecl (map fst bc)
60 let cc = concatMap (uncurry toC) bc
61 let hi = concatMap ifaceC (concatMap getExp exports)
63 mprog <- readFile (d </> "rts" </> "idris_main" <.> "c")
64 let cout = headers incs ++ debug dbg ++ h ++ wrappers ++ cc ++
65 (if (exec == Executable) then mprog else hi)
67 MavenProject -> putStrLn ("FAILURE: output type not supported")
68 Raw -> writeSource out cout
70 (tmpn, tmph) <- tempfile ".c"
75 libFlags <- getLibFlags
76 incFlags <- getIncFlags
77 envFlags <- getEnvFlags
78 let args = [gccDbg dbg] ++
80 -- # Any flags defined here which alter the RTS API must also be added to config.mk
81 ["-DHAS_PTHREAD", "-DIDRIS_ENABLE_STATS",
82 "-I."] ++ objs ++ envFlags ++
83 (if (exec == Executable) then [] else ["-c"]) ++
85 (if not iface then libFlags else []) ++
87 (if not iface then libs else []) ++
90-- putStrLn (show args)
91 exit <- rawSystem comp args
92 when (exit /= ExitSuccess) $
93 putStrLn ("FAILURE: " ++ show comp ++ " " ++ show args)
95 getExp (Export _ _ exp) = exp
99 (\h -> "#include \"" ++ h ++ "\"\n")
100 (xs ++ ["idris_rts.h", "idris_bitstring.h", "idris_stdfgn.h"])
102debug TRACE = "#define IDRIS_TRACE\n\n"
105-- We're using signed integers now. Make sure we get consistent semantics
106-- out of them from gcc. See e.g. http://thiemonagel.de/2010/01/signed-integer-overflow/
107gccFlags i = if i then ["-fwrapv"]
108 else ["-fwrapv", "-fno-strict-overflow"]
114cname :: Name -> String
115cname n = "_idris_" ++ concatMap cchar (showCG n)
116 where cchar x | isAlpha x || isDigit x = [x]
117 | otherwise = "_" ++ show (fromEnum x) ++ "_"
119indent :: Int -> String
120indent n = replicate (n*4) ' '
123creg (L i) = "LOC(" ++ show i ++ ")"
124creg (T i) = "TOP(" ++ show i ++ ")"
127toDecl :: Name -> String
128toDecl f = "void " ++ cname f ++ "(VM*, VAL*);\n"
130toC :: Name -> [BC] -> String
132 = -- "/* " ++ show code ++ "*/\n\n" ++
133 "void " ++ cname f ++ "(VM* vm, VAL* oldbase) {\n" ++
134 indent 1 ++ "INITFRAME;\n" ++
135 concatMap (bcc 1) code ++ "}\n\n"
137showCStr :: String -> String
138showCStr s = '"' : foldr ((++) . showChar) "\"" s
140 showChar :: Char -> String
141 showChar '"' = "\\\""
142 showChar '\\' = "\\\\"
144 -- Note: we need the double quotes around the codes because otherwise
145 -- "\n3" would get encoded as "\x0a3", which is incorrect.
146 -- Instead, we opt for "\x0a""3" and let the C compiler deal with it.
147 | ord c < 0x10 = "\"\"\\x0" ++ showHex (ord c) "\"\""
148 | ord c < 0x20 = "\"\"\\x" ++ showHex (ord c) "\"\""
149 | ord c < 0x7f = [c] -- 0x7f = \DEL
150 | otherwise = showHexes (utf8bytes (ord c))
152 utf8bytes :: Int -> [Int]
153 utf8bytes x = let (h : bytes) = split [] x in
154 headHex h (length bytes) : map toHex bytes
157 split acc x = let xbits = x .&. 0x3f
158 xrest = shiftR x 6 in
159 split (xbits : acc) xrest
161 headHex h 1 = h + 0xc0
162 headHex h 2 = h + 0xe0
163 headHex h 3 = h + 0xf0
164 headHex h n = error "Can't happen: Invalid UTF8 character"
168 showHexes = foldr ((++) . showUTF8) ""
169 showUTF8 c = "\"\"\\x" ++ showHex c "\"\""
171bcc :: Int -> BC -> String
172bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
173bcc i (ASSIGNCONST l c)
174 = indent i ++ creg l ++ " = " ++ mkConst c ++ ";\n"
176 mkConst (I i) = "MKINT(" ++ show i ++ ")"
177 mkConst (BI i) | i < (2^30) = "MKINT(" ++ show i ++ ")"
178 | otherwise = "MKBIGC(vm,\"" ++ show i ++ "\")"
179 mkConst (Fl f) = "MKFLOAT(vm, " ++ show f ++ ")"
180 mkConst (Ch c) = "MKINT(" ++ show (fromEnum c) ++ ")"
181 mkConst (Str s) = "MKSTR(vm, " ++ showCStr s ++ ")"
182 mkConst (B8 x) = "idris_b8const(vm, " ++ show x ++ "U)"
183 mkConst (B16 x) = "idris_b16const(vm, " ++ show x ++ "U)"
184 mkConst (B32 x) = "idris_b32const(vm, " ++ show x ++ "UL)"
185 mkConst (B64 x) = "idris_b64const(vm, " ++ show x ++ "ULL)"
186 -- if it's a type constant, we won't use it, but equally it shouldn't
187 -- report an error. These might creep into generated for various reasons
188 -- (especially if erasure is disabled).
189 mkConst c | isTypeConst c = "MKINT(42424242)"
190 mkConst c = error $ "mkConst of (" ++ show c ++ ") not implemented"
192bcc i (UPDATE l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
193bcc i (MKCON l loc tag []) | tag < 256
194 = indent i ++ creg l ++ " = NULL_CON(" ++ show tag ++ ");\n"
195bcc i (MKCON l loc tag args)
196 = indent i ++ alloc loc tag ++
197 indent i ++ setArgs 0 args ++ "\n" ++
198 indent i ++ creg l ++ " = " ++ creg Tmp ++ ";\n"
200-- "MKCON(vm, " ++ creg l ++ ", " ++ show tag ++ ", " ++
201-- show (length args) ++ concatMap showArg args ++ ");\n"
202 where showArg r = ", " ++ creg r
204 setArgs i (x : xs) = "SETARG(" ++ creg Tmp ++ ", " ++ show i ++ ", " ++ creg x ++
205 "); " ++ setArgs (i + 1) xs
207 = "allocCon(" ++ creg Tmp ++ ", vm, " ++ show tag ++ ", " ++
208 show (length args) ++ ", 0);\n"
210 = "updateCon(" ++ creg Tmp ++ ", " ++ creg old ++ ", " ++ show tag ++ ", " ++
211 show (length args) ++ ");\n"
213bcc i (PROJECT l loc a) = indent i ++ "PROJECT(vm, " ++ creg l ++ ", " ++ show loc ++
214 ", " ++ show a ++ ");\n"
215bcc i (PROJECTINTO r t idx)
216 = indent i ++ creg r ++ " = GETARG(" ++ creg t ++ ", " ++ show idx ++ ");\n"
217bcc i (CASE True r code def)
218 | length code < 4 = showCase i def code
220 showCode :: Int -> [BC] -> String
221 showCode i bc = "{\n" ++ concatMap (bcc (i + 1)) bc ++
224 showCase :: Int -> Maybe [BC] -> [(Int, [BC])] -> String
225 showCase i Nothing [(t, c)] = indent i ++ showCode i c
226 showCase i (Just def) [] = indent i ++ showCode i def
227 showCase i def ((t, c) : cs)
228 = indent i ++ "if (CTAG(" ++ creg r ++ ") == " ++ show t ++ ") " ++ showCode i c
229 ++ indent i ++ "else\n" ++ showCase i def cs
231bcc i (CASE safe r code def)
232 = indent i ++ "switch(" ++ ctag safe ++ "(" ++ creg r ++ ")) {\n" ++
233 concatMap (showCase i) code ++
240 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
241 ++ concatMap (bcc (i+1)) bc ++ indent (i + 1) ++ "break;\n"
242 showDef i Nothing = ""
243 showDef i (Just c) = indent i ++ "default:\n"
244 ++ concatMap (bcc (i+1)) c ++ indent (i + 1) ++ "break;\n"
245bcc i (CONSTCASE r code def)
247-- = indent i ++ "switch(GETINT(" ++ creg r ++ ")) {\n" ++
248-- concatMap (showCase i) code ++
251 = concatMap (iCase (creg r)) code ++
252 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
254 = concatMap (strCase ("GETSTR(" ++ creg r ++ ")")) code ++
255 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
257 = concatMap (biCase (creg r)) code ++
258 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
259 | otherwise = error $ "Can't happen: Can't compile const case " ++ show code
261 intConsts ((I _, _ ) : _) = True
262 intConsts ((Ch _, _ ) : _) = True
263 intConsts ((B8 _, _ ) : _) = True
264 intConsts ((B16 _, _ ) : _) = True
265 intConsts ((B32 _, _ ) : _) = True
266 intConsts ((B64 _, _ ) : _) = True
269 bigintConsts ((BI _, _ ) : _) = True
270 bigintConsts _ = False
272 strConsts ((Str _, _ ) : _) = True
276 indent i ++ "if (strcmp(" ++ sv ++ ", " ++ show s ++ ") == 0) {\n" ++
277 concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
278 biCase bv (BI b, bc) =
279 indent i ++ "if (bigEqConst(" ++ bv ++ ", " ++ show b ++ ")) {\n"
280 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
282 indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
283 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
285 indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show (fromEnum b) ++ ") {\n"
286 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
288 indent i ++ "if (GETBITS8(" ++ v ++ ") == " ++ show (fromEnum w) ++ ") {\n"
289 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
290 iCase v (B16 w, bc) =
291 indent i ++ "if (GETBITS16(" ++ v ++ ") == " ++ show (fromEnum w) ++ ") {\n"
292 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
293 iCase v (B32 w, bc) =
294 indent i ++ "if (GETBITS32(" ++ v ++ ") == " ++ show (fromEnum w) ++ ") {\n"
295 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
296 iCase v (B64 w, bc) =
297 indent i ++ "if (GETBITS64(" ++ v ++ ") == " ++ show (fromEnum w) ++ ") {\n"
298 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
299 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
300 ++ concatMap (bcc (i+1)) bc ++
301 indent (i + 1) ++ "break;\n"
302 showDef i Nothing = ""
303 showDef i (Just c) = indent i ++ "default:\n"
304 ++ concatMap (bcc (i+1)) c ++
305 indent (i + 1) ++ "break;\n"
306 showDefS i Nothing = ""
307 showDefS i (Just c) = concatMap (bcc (i+1)) c
309bcc i (CALL n) = indent i ++ "CALL(" ++ cname n ++ ");\n"
310bcc i (TAILCALL n) = indent i ++ "TAILCALL(" ++ cname n ++ ");\n"
311bcc i (SLIDE n) = indent i ++ "SLIDE(vm, " ++ show n ++ ");\n"
312bcc i REBASE = indent i ++ "REBASE;\n"
313bcc i (RESERVE 0) = ""
314bcc i (RESERVE n) = indent i ++ "RESERVE(" ++ show n ++ ");\n"
316bcc i (ADDTOP n) = indent i ++ "ADDTOP(" ++ show n ++ ");\n"
317bcc i (TOPBASE n) = indent i ++ "TOPBASE(" ++ show n ++ ");\n"
318bcc i (BASETOP n) = indent i ++ "BASETOP(" ++ show n ++ ");\n"
319bcc i STOREOLD = indent i ++ "STOREOLD;\n"
320bcc i (OP l fn args) = indent i ++ doOp (creg l ++ " = ") fn args ++ ";\n"
321bcc i (FOREIGNCALL l rty (FStr fn@('&':name)) [])
323 c_irts (toFType rty) (creg l ++ " = ") fn ++ ";\n"
324bcc i (FOREIGNCALL l rty (FStr fn) (x:xs)) | fn == "%wrapper"
326 c_irts (toFType rty) (creg l ++ " = ")
327 ("_idris_get_wrapper(" ++ creg (snd x) ++ ")") ++ ";\n"
328bcc i (FOREIGNCALL l rty (FStr fn) (x:xs)) | fn == "%dynamic"
329 = indent i ++ c_irts (toFType rty) (creg l ++ " = ")
330 ("(*(" ++ cFnSig "" rty xs ++ ") GETPTR(" ++ creg (snd x) ++ "))" ++
331 "(" ++ showSep "," (map fcall xs) ++ ")") ++ ";\n"
332bcc i (FOREIGNCALL l rty (FStr fn) args)
334 c_irts (toFType rty) (creg l ++ " = ")
335 (fn ++ "(" ++ showSep "," (map fcall args) ++ ")") ++ ";\n"
336bcc i (NULL r) = indent i ++ creg r ++ " = NULL;\n" -- clear, so it'll be GCed
337bcc i (ERROR str) = indent i ++ "fprintf(stderr, " ++ show str ++ "); fprintf(stderr, \"\\n\"); exit(-1);\n"
338-- bcc i c = error (show c) -- indent i ++ "// not done yet\n"
340fcall (t, arg) = irts_c (toFType t) (creg arg)
341-- Deconstruct the Foreign type in the defunctionalised expression and build
342-- a foreign type description for c_irts and irts_c
344 | i == sUN "C_IntChar" = ATInt ITChar
345 | i == sUN "C_IntNative" = ATInt ITNative
346 | i == sUN "C_IntBits8" = ATInt (ITFixed IT8)
347 | i == sUN "C_IntBits16" = ATInt (ITFixed IT16)
348 | i == sUN "C_IntBits32" = ATInt (ITFixed IT32)
349 | i == sUN "C_IntBits64" = ATInt (ITFixed IT64)
350toAType t = error (show t ++ " not defined in toAType")
353 | c == sUN "C_Str" = FString
354 | c == sUN "C_Float" = FArith ATFloat
355 | c == sUN "C_Ptr" = FPtr
356 | c == sUN "C_MPtr" = FManagedPtr
357 | c == sUN "C_Unit" = FUnit
358toFType (FApp c [_,ity])
359 | c == sUN "C_IntT" = FArith (toAType ity)
360 | c == sUN "C_FnT" = toFunType ity
362 | c == sUN "C_Any" = FAny
365toFunType (FApp c [_,ity])
366 | c == sUN "C_FnBase" = FFunction
367 | c == sUN "C_FnIO" = FFunctionIO
368toFunType (FApp c [_,_,_,ity])
369 | c == sUN "C_Fn" = toFunType ity
372c_irts (FArith (ATInt ITNative)) l x = l ++ "MKINT((i_int)(" ++ x ++ "))"
373c_irts (FArith (ATInt ITChar)) l x = c_irts (FArith (ATInt ITNative)) l x
374c_irts (FArith (ATInt (ITFixed ity))) l x
375 = l ++ "idris_b" ++ show (nativeTyWidth ity) ++ "const(vm, " ++ x ++ ")"
376c_irts FString l x = l ++ "MKSTR(vm, " ++ x ++ ")"
378c_irts FPtr l x = l ++ "MKPTR(vm, " ++ x ++ ")"
379c_irts FManagedPtr l x = l ++ "MKMPTR(vm, " ++ x ++ ")"
380c_irts (FArith ATFloat) l x = l ++ "MKFLOAT(vm, " ++ x ++ ")"
381c_irts FAny l x = l ++ x
382c_irts FFunction l x = error "Return of function from foreign call is not supported"
383c_irts FFunctionIO l x = error "Return of function from foreign call is not supported"
385irts_c (FArith (ATInt ITNative)) x = "GETINT(" ++ x ++ ")"
386irts_c (FArith (ATInt ITChar)) x = irts_c (FArith (ATInt ITNative)) x
387irts_c (FArith (ATInt (ITFixed ity))) x
388 = "(" ++ x ++ "->info.bits" ++ show (nativeTyWidth ity) ++ ")"
389irts_c FString x = "GETSTR(" ++ x ++ ")"
391irts_c FPtr x = "GETPTR(" ++ x ++ ")"
392irts_c FManagedPtr x = "GETMPTR(" ++ x ++ ")"
393irts_c (FArith ATFloat) x = "GETFLOAT(" ++ x ++ ")"
395irts_c FFunctionIO x = wrapped x
396irts_c FFunction x = wrapped x
398cFnSig name rty [] = ctype rty ++ " (*" ++ name ++ ")(void) "
399cFnSig name rty args = ctype rty ++ " (*" ++ name ++ ")("
400 ++ showSep "," (map (ctype . fst) args) ++ ") "
402wrapped x = "_idris_get_wrapper(" ++ x ++ ")"
404bitOp v op ty args = v ++ "idris_b" ++ show (nativeTyWidth ty) ++ op ++ "(vm, " ++ intercalate ", " (map creg args) ++ ")"
406bitCoerce v op input output arg
407 = v ++ "idris_b" ++ show (nativeTyWidth input) ++ op ++ show (nativeTyWidth output) ++ "(vm, " ++ creg arg ++ ")"
409signedTy :: NativeTy -> String
410signedTy t = "int" ++ show (nativeTyWidth t) ++ "_t"
412doOp v (LPlus (ATInt ITNative)) [l, r] = v ++ "ADD(" ++ creg l ++ ", " ++ creg r ++ ")"
413doOp v (LMinus (ATInt ITNative)) [l, r] = v ++ "INTOP(-," ++ creg l ++ ", " ++ creg r ++ ")"
414doOp v (LTimes (ATInt ITNative)) [l, r] = v ++ "MULT(" ++ creg l ++ ", " ++ creg r ++ ")"
415doOp v (LUDiv ITNative) [l, r] = v ++ "UINTOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
416doOp v (LSDiv (ATInt ITNative)) [l, r] = v ++ "INTOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
417doOp v (LURem ITNative) [l, r] = v ++ "UINTOP(%," ++ creg l ++ ", " ++ creg r ++ ")"
418doOp v (LSRem (ATInt ITNative)) [l, r] = v ++ "INTOP(%," ++ creg l ++ ", " ++ creg r ++ ")"
419doOp v (LAnd ITNative) [l, r] = v ++ "INTOP(&," ++ creg l ++ ", " ++ creg r ++ ")"
420doOp v (LOr ITNative) [l, r] = v ++ "INTOP(|," ++ creg l ++ ", " ++ creg r ++ ")"
421doOp v (LXOr ITNative) [l, r] = v ++ "INTOP(^," ++ creg l ++ ", " ++ creg r ++ ")"
422doOp v (LSHL ITNative) [l, r] = v ++ "INTOP(<<," ++ creg l ++ ", " ++ creg r ++ ")"
423doOp v (LLSHR ITNative) [l, r] = v ++ "UINTOP(>>," ++ creg l ++ ", " ++ creg r ++ ")"
424doOp v (LASHR ITNative) [l, r] = v ++ "INTOP(>>," ++ creg l ++ ", " ++ creg r ++ ")"
425doOp v (LCompl ITNative) [x] = v ++ "INTOP(~," ++ creg x ++ ")"
426doOp v (LEq (ATInt ITNative)) [l, r] = v ++ "INTOP(==," ++ creg l ++ ", " ++ creg r ++ ")"
427doOp v (LSLt (ATInt ITNative)) [l, r] = v ++ "INTOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
428doOp v (LSLe (ATInt ITNative)) [l, r] = v ++ "INTOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
429doOp v (LSGt (ATInt ITNative)) [l, r] = v ++ "INTOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
430doOp v (LSGe (ATInt ITNative)) [l, r] = v ++ "INTOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
431doOp v (LLt ITNative) [l, r] = v ++ "UINTOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
432doOp v (LLe ITNative) [l, r] = v ++ "UINTOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
433doOp v (LGt ITNative) [l, r] = v ++ "UINTOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
434doOp v (LGe ITNative) [l, r] = v ++ "UINTOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
436doOp v (LPlus (ATInt ITChar)) [l, r] = doOp v (LPlus (ATInt ITNative)) [l, r]
437doOp v (LMinus (ATInt ITChar)) [l, r] = doOp v (LMinus (ATInt ITNative)) [l, r]
438doOp v (LTimes (ATInt ITChar)) [l, r] = doOp v (LTimes (ATInt ITNative)) [l, r]
439doOp v (LUDiv ITChar) [l, r] = doOp v (LUDiv ITNative) [l, r]
440doOp v (LSDiv (ATInt ITChar)) [l, r] = doOp v (LSDiv (ATInt ITNative)) [l, r]
441doOp v (LURem ITChar) [l, r] = doOp v (LURem ITNative) [l, r]
442doOp v (LSRem (ATInt ITChar)) [l, r] = doOp v (LSRem (ATInt ITNative)) [l, r]
443doOp v (LAnd ITChar) [l, r] = doOp v (LAnd ITNative) [l, r]
444doOp v (LOr ITChar) [l, r] = doOp v (LOr ITNative) [l, r]
445doOp v (LXOr ITChar) [l, r] = doOp v (LXOr ITNative) [l, r]
446doOp v (LSHL ITChar) [l, r] = doOp v (LSHL ITNative) [l, r]
447doOp v (LLSHR ITChar) [l, r] = doOp v (LLSHR ITNative) [l, r]
448doOp v (LASHR ITChar) [l, r] = doOp v (LASHR ITNative) [l, r]
449doOp v (LCompl ITChar) [x] = doOp v (LCompl ITNative) [x]
450doOp v (LEq (ATInt ITChar)) [l, r] = doOp v (LEq (ATInt ITNative)) [l, r]
451doOp v (LSLt (ATInt ITChar)) [l, r] = doOp v (LSLt (ATInt ITNative)) [l, r]
452doOp v (LSLe (ATInt ITChar)) [l, r] = doOp v (LSLe (ATInt ITNative)) [l, r]
453doOp v (LSGt (ATInt ITChar)) [l, r] = doOp v (LSGt (ATInt ITNative)) [l, r]
454doOp v (LSGe (ATInt ITChar)) [l, r] = doOp v (LSGe (ATInt ITNative)) [l, r]
455doOp v (LLt ITChar) [l, r] = doOp v (LLt ITNative) [l, r]
456doOp v (LLe ITChar) [l, r] = doOp v (LLe ITNative) [l, r]
457doOp v (LGt ITChar) [l, r] = doOp v (LGt ITNative) [l, r]
458doOp v (LGe ITChar) [l, r] = doOp v (LGe ITNative) [l, r]
460doOp v (LPlus ATFloat) [l, r] = v ++ "FLOATOP(+," ++ creg l ++ ", " ++ creg r ++ ")"
461doOp v (LMinus ATFloat) [l, r] = v ++ "FLOATOP(-," ++ creg l ++ ", " ++ creg r ++ ")"
462doOp v (LTimes ATFloat) [l, r] = v ++ "FLOATOP(*," ++ creg l ++ ", " ++ creg r ++ ")"
463doOp v (LSDiv ATFloat) [l, r] = v ++ "FLOATOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
464doOp v (LEq ATFloat) [l, r] = v ++ "FLOATBOP(==," ++ creg l ++ ", " ++ creg r ++ ")"
465doOp v (LSLt ATFloat) [l, r] = v ++ "FLOATBOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
466doOp v (LSLe ATFloat) [l, r] = v ++ "FLOATBOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
467doOp v (LSGt ATFloat) [l, r] = v ++ "FLOATBOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
468doOp v (LSGe ATFloat) [l, r] = v ++ "FLOATBOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
470doOp v (LIntFloat ITBig) [x] = v ++ "idris_castBigFloat(vm, " ++ creg x ++ ")"
471doOp v (LFloatInt ITBig) [x] = v ++ "idris_castFloatBig(vm, " ++ creg x ++ ")"
472doOp v (LPlus (ATInt ITBig)) [l, r] = v ++ "idris_bigPlus(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
473doOp v (LMinus (ATInt ITBig)) [l, r] = v ++ "idris_bigMinus(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
474doOp v (LTimes (ATInt ITBig)) [l, r] = v ++ "idris_bigTimes(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
475doOp v (LSDiv (ATInt ITBig)) [l, r] = v ++ "idris_bigDivide(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
476doOp v (LSRem (ATInt ITBig)) [l, r] = v ++ "idris_bigMod(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
477doOp v (LAnd ITBig) [l, r] = v ++ "idris_bigAnd(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
478doOp v (LOr ITBig) [l, r] = v ++ "idris_bigOr(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
479doOp v (LSHL ITBig) [l, r] = v ++ "idris_bigShiftLeft(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
480doOp v (LLSHR ITBig) [l, r] = v ++ "idris_bigLShiftRight(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
481doOp v (LASHR ITBig) [l, r] = v ++ "idris_bigAShiftRight(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
482doOp v (LEq (ATInt ITBig)) [l, r] = v ++ "idris_bigEq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
483doOp v (LSLt (ATInt ITBig)) [l, r] = v ++ "idris_bigLt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
484doOp v (LSLe (ATInt ITBig)) [l, r] = v ++ "idris_bigLe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
485doOp v (LSGt (ATInt ITBig)) [l, r] = v ++ "idris_bigGt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
486doOp v (LSGe (ATInt ITBig)) [l, r] = v ++ "idris_bigGe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
488doOp v (LIntFloat ITNative) [x] = v ++ "idris_castIntFloat(" ++ creg x ++ ")"
489doOp v (LFloatInt ITNative) [x] = v ++ "idris_castFloatInt(" ++ creg x ++ ")"
490doOp v (LSExt ITNative ITBig) [x] = v ++ "idris_castIntBig(vm, " ++ creg x ++ ")"
491doOp v (LTrunc ITBig ITNative) [x] = v ++ "idris_castBigInt(vm, " ++ creg x ++ ")"
492doOp v (LStrInt ITBig) [x] = v ++ "idris_castStrBig(vm, " ++ creg x ++ ")"
493doOp v (LIntStr ITBig) [x] = v ++ "idris_castBigStr(vm, " ++ creg x ++ ")"
494doOp v (LIntStr ITNative) [x] = v ++ "idris_castIntStr(vm, " ++ creg x ++ ")"
495doOp v (LStrInt ITNative) [x] = v ++ "idris_castStrInt(vm, " ++ creg x ++ ")"
496doOp v (LIntStr (ITFixed _)) [x] = v ++ "idris_castBitsStr(vm, " ++ creg x ++ ")"
497doOp v LFloatStr [x] = v ++ "idris_castFloatStr(vm, " ++ creg x ++ ")"
498doOp v LStrFloat [x] = v ++ "idris_castStrFloat(vm, " ++ creg x ++ ")"
500doOp v (LSLt (ATInt (ITFixed ty))) [x, y] = bitOp v "SLt" ty [x, y]
501doOp v (LSLe (ATInt (ITFixed ty))) [x, y] = bitOp v "SLte" ty [x, y]
502doOp v (LEq (ATInt (ITFixed ty))) [x, y] = bitOp v "Eq" ty [x, y]
503doOp v (LSGe (ATInt (ITFixed ty))) [x, y] = bitOp v "SGte" ty [x, y]
504doOp v (LSGt (ATInt (ITFixed ty))) [x, y] = bitOp v "SGt" ty [x, y]
505doOp v (LLt (ITFixed ty)) [x, y] = bitOp v "Lt" ty [x, y]
506doOp v (LLe (ITFixed ty)) [x, y] = bitOp v "Lte" ty [x, y]
507doOp v (LGe (ITFixed ty)) [x, y] = bitOp v "Gte" ty [x, y]
508doOp v (LGt (ITFixed ty)) [x, y] = bitOp v "Gt" ty [x, y]
510doOp v (LSHL (ITFixed ty)) [x, y] = bitOp v "Shl" ty [x, y]
511doOp v (LLSHR (ITFixed ty)) [x, y] = bitOp v "LShr" ty [x, y]
512doOp v (LASHR (ITFixed ty)) [x, y] = bitOp v "AShr" ty [x, y]
513doOp v (LAnd (ITFixed ty)) [x, y] = bitOp v "And" ty [x, y]
514doOp v (LOr (ITFixed ty)) [x, y] = bitOp v "Or" ty [x, y]
515doOp v (LXOr (ITFixed ty)) [x, y] = bitOp v "Xor" ty [x, y]
516doOp v (LCompl (ITFixed ty)) [x] = bitOp v "Compl" ty [x]
518doOp v (LPlus (ATInt (ITFixed ty))) [x, y] = bitOp v "Plus" ty [x, y]
519doOp v (LMinus (ATInt (ITFixed ty))) [x, y] = bitOp v "Minus" ty [x, y]
520doOp v (LTimes (ATInt (ITFixed ty))) [x, y] = bitOp v "Times" ty [x, y]
521doOp v (LUDiv (ITFixed ty)) [x, y] = bitOp v "UDiv" ty [x, y]
522doOp v (LSDiv (ATInt (ITFixed ty))) [x, y] = bitOp v "SDiv" ty [x, y]
523doOp v (LURem (ITFixed ty)) [x, y] = bitOp v "URem" ty [x, y]
524doOp v (LSRem (ATInt (ITFixed ty))) [x, y] = bitOp v "SRem" ty [x, y]
526doOp v (LSExt (ITFixed from) ITBig) [x]
527 = v ++ "MKBIGSI(vm, (" ++ signedTy from ++ ")" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
528doOp v (LSExt ITNative (ITFixed to)) [x]
529 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, GETINT(" ++ creg x ++ "))"
530doOp v (LSExt ITChar (ITFixed to)) [x]
531 = doOp v (LSExt ITNative (ITFixed to)) [x]
532doOp v (LSExt (ITFixed from) ITNative) [x]
533 = v ++ "MKINT((i_int)((" ++ signedTy from ++ ")" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ "))"
534doOp v (LSExt (ITFixed from) ITChar) [x]
535 = doOp v (LSExt (ITFixed from) ITNative) [x]
536doOp v (LSExt (ITFixed from) (ITFixed to)) [x]
537 | nativeTyWidth from < nativeTyWidth to = bitCoerce v "S" from to x
538doOp v (LZExt ITNative (ITFixed to)) [x]
539 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, (uintptr_t)GETINT(" ++ creg x ++ "))"
540doOp v (LZExt ITChar (ITFixed to)) [x]
541 = doOp v (LZExt ITNative (ITFixed to)) [x]
542doOp v (LZExt (ITFixed from) ITNative) [x]
543 = v ++ "MKINT((i_int)" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
544doOp v (LZExt (ITFixed from) ITChar) [x]
545 = doOp v (LZExt (ITFixed from) ITNative) [x]
546doOp v (LZExt (ITFixed from) ITBig) [x]
547 = v ++ "MKBIGUI(vm, " ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
548doOp v (LZExt ITNative ITBig) [x]
549 = v ++ "MKBIGUI(vm, (uintptr_t)GETINT(" ++ creg x ++ "))"
550doOp v (LZExt (ITFixed from) (ITFixed to)) [x]
551 | nativeTyWidth from < nativeTyWidth to = bitCoerce v "Z" from to x
552doOp v (LTrunc ITNative (ITFixed to)) [x]
553 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, GETINT(" ++ creg x ++ "))"
554doOp v (LTrunc ITChar (ITFixed to)) [x]
555 = doOp v (LTrunc ITNative (ITFixed to)) [x]
556doOp v (LTrunc (ITFixed from) ITNative) [x]
557 = v ++ "MKINT((i_int)" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
558doOp v (LTrunc (ITFixed from) ITChar) [x]
559 = doOp v (LTrunc (ITFixed from) ITNative) [x]
560doOp v (LTrunc ITBig (ITFixed IT64)) [x]
561 = v ++ "idris_b64const(vm, ISINT(" ++ creg x ++ ") ? GETINT(" ++ creg x ++ ") : idris_truncBigB64(GETMPZ(" ++ creg x ++ ")))"
562doOp v (LTrunc ITBig (ITFixed to)) [x]
563 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, ISINT(" ++ creg x ++ ") ? GETINT(" ++ creg x ++ ") : mpz_get_ui(GETMPZ(" ++ creg x ++ ")))"
564doOp v (LTrunc (ITFixed from) (ITFixed to)) [x]
565 | nativeTyWidth from > nativeTyWidth to = bitCoerce v "T" from to x
567doOp v LFExp [x] = v ++ flUnOp "exp" (creg x)
568doOp v LFLog [x] = v ++ flUnOp "log" (creg x)
569doOp v LFSin [x] = v ++ flUnOp "sin" (creg x)
570doOp v LFCos [x] = v ++ flUnOp "cos" (creg x)
571doOp v LFTan [x] = v ++ flUnOp "tan" (creg x)
572doOp v LFASin [x] = v ++ flUnOp "asin" (creg x)
573doOp v LFACos [x] = v ++ flUnOp "acos" (creg x)
574doOp v LFATan [x] = v ++ flUnOp "atan" (creg x)
575doOp v LFSqrt [x] = v ++ flUnOp "sqrt" (creg x)
576doOp v LFFloor [x] = v ++ flUnOp "floor" (creg x)
577doOp v LFCeil [x] = v ++ flUnOp "ceil" (creg x)
578doOp v LFNegate [x] = v ++ "MKFLOAT(vm, -GETFLOAT(" ++ (creg x) ++ "))"
580-- String functions which don't need to know we're UTF8
581doOp v LStrConcat [l,r] = v ++ "idris_concat(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
582doOp v LStrLt [l,r] = v ++ "idris_strlt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
583doOp v LStrEq [l,r] = v ++ "idris_streq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
585doOp v LReadStr [_] = v ++ "idris_readStr(vm, stdin)"
586doOp v LWriteStr [_,s]
587 = v ++ "MKINT((i_int)(idris_writeStr(stdout"
592-- String functions which need to know we're UTF8
593doOp v LStrHead [x] = v ++ "idris_strHead(vm, " ++ creg x ++ ")"
594doOp v LStrTail [x] = v ++ "idris_strTail(vm, " ++ creg x ++ ")"
595doOp v LStrCons [x, y] = v ++ "idris_strCons(vm, " ++ creg x ++ "," ++ creg y ++ ")"
596doOp v LStrIndex [x, y] = v ++ "idris_strIndex(vm, " ++ creg x ++ "," ++ creg y ++ ")"
597doOp v LStrRev [x] = v ++ "idris_strRev(vm, " ++ creg x ++ ")"
598doOp v LStrLen [x] = v ++ "idris_strlen(vm, " ++ creg x ++ ")"
599doOp v LStrSubstr [x,y,z] = v ++ "idris_substr(vm, " ++ creg x ++ "," ++ creg y ++ "," ++ creg z ++ ")"
601doOp v LFork [x] = v ++ "MKPTR(vm, vmThread(vm, " ++ cname (sMN 0 "EVAL") ++ ", " ++ creg x ++ "))"
602doOp v LPar [x] = v ++ creg x -- "MKPTR(vm, vmThread(vm, " ++ cname (MN 0 "EVAL") ++ ", " ++ creg x ++ "))"
603doOp v (LChInt ITNative) args = v ++ creg (last args)
604doOp v (LChInt ITChar) args = doOp v (LChInt ITNative) args
605doOp v (LIntCh ITNative) args = v ++ creg (last args)
606doOp v (LIntCh ITChar) args = doOp v (LIntCh ITNative) args
608doOp v LSystemInfo [x] = v ++ "idris_systemInfo(vm, " ++ creg x ++ ")"
609doOp v LNoOp args = v ++ creg (last args)
611-- Pointer primitives (declared as %extern in Builtins.idr)
612doOp v (LExternal rf) [_,x]
613 | rf == sUN "prim__readFile"
614 = v ++ "idris_readStr(vm, GETPTR(" ++ creg x ++ "))"
615doOp v (LExternal wf) [_,x,s]
616 | wf == sUN "prim__writeFile"
617 = v ++ "MKINT((i_int)(idris_writeStr(GETPTR(" ++ creg x
620doOp v (LExternal vm) [] | vm == sUN "prim__vm" = v ++ "MKPTR(vm, vm)"
621doOp v (LExternal si) [] | si == sUN "prim__stdin" = v ++ "MKPTR(vm, stdin)"
622doOp v (LExternal so) [] | so == sUN "prim__stdout" = v ++ "MKPTR(vm, stdout)"
623doOp v (LExternal se) [] | se == sUN "prim__stderr" = v ++ "MKPTR(vm, stderr)"
625doOp v (LExternal nul) [] | nul == sUN "prim__null" = v ++ "MKPTR(vm, NULL)"
626doOp v (LExternal eqp) [x, y] | eqp == sUN "prim__eqPtr"
627 = v ++ "MKINT((i_int)(GETPTR(" ++ creg x ++ ") == GETPTR(" ++ creg y ++ ")))"
628doOp v (LExternal eqp) [x, y] | eqp == sUN "prim__eqManagedPtr"
629 = v ++ "MKINT((i_int)(GETMPTR(" ++ creg x ++ ") == GETMPTR(" ++ creg y ++ ")))"
630doOp v (LExternal rp) [p, i] | rp == sUN "prim__registerPtr"
631 = v ++ "MKMPTR(vm, GETPTR(" ++ creg p ++ "), GETINT(" ++ creg i ++ "))"
632doOp v (LExternal pk) [_, p, o] | pk == sUN "prim__peek8"
633 = v ++ "idris_peekB8(vm," ++ creg p ++ "," ++ creg o ++")"
634doOp v (LExternal pk) [_, p, o, x] | pk == sUN "prim__poke8"
635 = v ++ "idris_pokeB8(" ++ creg p ++ "," ++ creg o ++ "," ++ creg x ++ ")"
636doOp v (LExternal pk) [_, p, o] | pk == sUN "prim__peek16"
637 = v ++ "idris_peekB16(vm," ++ creg p ++ "," ++ creg o ++")"
638doOp v (LExternal pk) [_, p, o, x] | pk == sUN "prim__poke16"
639 = v ++ "idris_pokeB16(" ++ creg p ++ "," ++ creg o ++ "," ++ creg x ++ ")"
640doOp v (LExternal pk) [_, p, o] | pk == sUN "prim__peek32"
641 = v ++ "idris_peekB32(vm," ++ creg p ++ "," ++ creg o ++")"
642doOp v (LExternal pk) [_, p, o, x] | pk == sUN "prim__poke32"
643 = v ++ "idris_pokeB32(" ++ creg p ++ "," ++ creg o ++ "," ++ creg x ++ ")"
644doOp v (LExternal pk) [_, p, o] | pk == sUN "prim__peek64"
645 = v ++ "idris_peekB64(vm," ++ creg p ++ "," ++ creg o ++")"
646doOp v (LExternal pk) [_, p, o, x] | pk == sUN "prim__poke64"
647 = v ++ "idris_pokeB64(" ++ creg p ++ "," ++ creg o ++ "," ++ creg x ++ ")"
648doOp v (LExternal pk) [_, p, o] | pk == sUN "prim__peekPtr"
649 = v ++ "idris_peekPtr(vm," ++ creg p ++ "," ++ creg o ++")"
650doOp v (LExternal pk) [_, p, o, x] | pk == sUN "prim__pokePtr"
651 = v ++ "idris_pokePtr(" ++ creg p ++ "," ++ creg o ++ "," ++ creg x ++ ")"
652doOp v (LExternal pk) [] | pk == sUN "prim__sizeofPtr"
653 = v ++ "MKINT(sizeof(void*))"
654doOp v (LExternal mpt) [p] | mpt == sUN "prim__asPtr" = v ++ "MKPTR(vm, GETMPTR("++ creg p ++"))"
655doOp _ op args = error $ "doOp not implemented (" ++ show (op, args) ++ ")"
658flUnOp :: String -> String -> String
659flUnOp name val = "MKFLOAT(vm, " ++ name ++ "(GETFLOAT(" ++ val ++ ")))"
661-------------------- Interface file generation
663-- First, the wrappers in the C file
665ifaceC :: Export -> String
666ifaceC (ExportData n) = "typedef VAL " ++ cdesc n ++ ";\n"
667ifaceC (ExportFun n cn ret args)
668 = ctype ret ++ " " ++ cdesc cn ++
669 "(VM* vm" ++ showArgs (zip argNames args) ++ ") {\n"
670 ++ mkBody n (zip argNames args) ret ++ "}\n\n"
671 where showArgs [] = ""
672 showArgs ((n, t) : ts) = ", " ++ ctype t ++ " " ++ n ++
675 argNames = zipWith (++) (repeat "arg") (map show [0..])
677mkBody n as t = indent 1 ++ "INITFRAME;\n" ++
678 indent 1 ++ "RESERVE(" ++ show (max (length as) 3) ++ ");\n" ++
679 push 0 as ++ call n ++ retval t
681 push i ((n, t) : ts) = indent 1 ++ c_irts (toFType t)
682 ("TOP(" ++ show i ++ ") = ") n
683 ++ ";\n" ++ push (i + 1) ts
685 call _ = indent 1 ++ "STOREOLD;\n" ++
686 indent 1 ++ "BASETOP(0);\n" ++
687 indent 1 ++ "ADDTOP(" ++ show (length as) ++ ");\n" ++
688 indent 1 ++ "CALL(" ++ cname n ++ ");\n"
691 = indent 1 ++ "TOP(0) = NULL;\n" ++
692 indent 1 ++ "TOP(1) = NULL;\n" ++
693 indent 1 ++ "TOP(2) = RVAL;\n" ++
694 indent 1 ++ "STOREOLD;\n" ++
695 indent 1 ++ "BASETOP(0);\n" ++
696 indent 1 ++ "ADDTOP(3);\n" ++
697 indent 1 ++ "CALL(" ++ cname (sUN "call__IO") ++ ");\n" ++
699 retval t = indent 1 ++ "return " ++ irts_c (toFType t) "RVAL" ++ ";\n"
702 | c == sUN "C_Str" = "char*"
703 | c == sUN "C_Float" = "float"
704 | c == sUN "C_Ptr" = "void*"
705 | c == sUN "C_MPtr" = "void*"
706 | c == sUN "C_Unit" = "void"
707ctype (FApp c [_,ity])
708 | c == sUN "C_IntT" = carith ity
710 | c == sUN "C_Any" = "VAL"
712ctype FUnknown = "void*"
713ctype (FIO t) = ctype t
714ctype t = error "Can't happen: Not a valid interface type " ++ show t
717 | i == sUN "C_IntChar" = "char"
718 | i == sUN "C_IntNative" = "int"
719carith t = error "Can't happen: Not an exportable arithmetic type"
722cdesc s = error "Can't happen: Not a valid C name"
724-- Then, the header files
726codegenH :: [ExportIFace] -> IO ()
727codegenH es = mapM_ writeIFace es
729writeIFace :: ExportIFace -> IO ()
730writeIFace (Export ffic hdr exps)
731 | ffic == sNS (sUN "FFI_C") ["FFI_C"]
732 = do let hfile = "#ifndef " ++ hdr_guard hdr ++ "\n" ++
733 "#define " ++ hdr_guard hdr ++ "\n\n" ++
734 "#include <idris_rts.h>\n\n" ++
735 concatMap hdr_export exps ++ "\n" ++
738 | otherwise = return ()
740hdr_guard x = "__" ++ map hchar x
741 where hchar x | isAlphaNum x = toUpper x
744hdr_export :: Export -> String
745hdr_export (ExportData n) = "typedef VAL " ++ cdesc n ++ ";\n"
746hdr_export (ExportFun n cn ret args)
747 = ctype ret ++ " " ++ cdesc cn ++
748 "(VM* vm" ++ showArgs (zip argNames args) ++ ");\n"
749 where showArgs [] = ""
750 showArgs ((n, t) : ts) = ", " ++ ctype t ++ " " ++ n ++
753 argNames = zipWith (++) (repeat "arg") (map show [0..])
755------------------ Callback wrapper generation ----------------
756-- Generate callback wrappers and a function to select the
757-- correct wrapper function to pass.
758-- TODO: This is limited to functions that are specified in
759-- the foreign call. Otherwise we would have to generate wrappers for all
760-- functions with correct arity or do flow analysis
761-- to find all possible inputs to the foreign call.
762genWrappers :: [(Name, [BC])] -> String
764 tags = nubBy (\x y -> snd x == snd y) $ concatMap (getCallback . snd) bcs
768 t -> concatMap genWrapper t ++ genDispatcher t
770genDispatcher :: [(FDesc, Int)] -> String
771genDispatcher tags = "void* _idris_get_wrapper(VAL con)\n" ++
773 indent 1 ++ "switch(TAG(con)) {\n" ++
774 concatMap makeSwitch tags ++
776 indent 1 ++ "fprintf(stderr, \"No wrapper for callback\");\n" ++
777 indent 1 ++ "exit(-1);\n" ++
780 makeSwitch (_, tag) =
781 indent 1 ++ "case " ++ show tag ++ ":\n" ++
782 indent 2 ++ "return (void*) &" ++ wrapperName tag ++ ";\n"
784genWrapper :: (FDesc, Int) -> String
785genWrapper (desc, tag) | (toFType desc) == FFunctionIO =
786 error $ "Cannot create C callbacks for IO functions, wrap them with unsafePerformIO.\n"
787genWrapper (desc, tag) = ret ++ " " ++ wrapperName tag ++ "(" ++
788 renderArgs argList ++")\n" ++
790 (if ret /= "void" then indent 1 ++ ret ++ " ret;\n" else "") ++
791 indent 1 ++ "VM* vm = get_vm();\n" ++
792 indent 1 ++ "if (vm == NULL) {\n" ++
793 indent 2 ++ "fprintf(stderr, \"No vm available in callback.\");\n" ++
794 indent 2 ++ "exit(-1);\n" ++
796 indent 1 ++ "INITFRAME;\n" ++
797 indent 1 ++ "RESERVE(" ++ show (len + 1) ++ ");\n" ++
798 indent 1 ++ "allocCon(REG1, vm, " ++ show tag ++ ",0 , 0);\n" ++
799 indent 1 ++ "TOP(0) = REG1;\n" ++
802 indent 1 ++ "STOREOLD;\n" ++
803 indent 1 ++ "BASETOP(0);\n" ++
804 indent 1 ++ "ADDTOP(" ++ show (len + 1) ++ ");\n" ++
805 indent 1 ++ "CALL(_idris__123_APPLY0_125_);\n" ++
807 then indent 1 ++ "ret = " ++ irts_c (toFType ft) "RVAL" ++ ";\n"
808 ++ indent 1 ++ "return ret;\n}\n\n"
812 argList = zip (args desc) [0..]
814 renderArgs [] = "void"
815 renderArgs [((s, _), n)] = s ++ " a" ++ (show n)
816 renderArgs (((s, _), n):xs) = s ++ " a" ++ (show n) ++ ", " ++
819 | c == sUN "C_FnBase" = (ctype ty, ty)
820 | c == sUN "C_FnIO" = (ctype ty, ty)
821 | c == sUN "C_FnT" = rty ty
822 rty (FApp c [_,_,ty,fn])
823 | c == sUN "C_Fn" = rty fn
826 | c == sUN "C_FnBase" = []
827 | c == sUN "C_FnIO" = []
828 | c == sUN "C_FnT" = args ty
829 args (FApp c [_,_,ty,fn])
830 | toFType ty == FUnit = []
831 | c == sUN "C_Fn" = (ctype ty, ty) : args fn
834 push i (((c, t), n) : ts) = indent 1 ++ c_irts (toFType t)
835 ("TOP(" ++ show i ++ ") = ") ("a" ++ show n)
836 ++ ";\n" ++ push (i + 1) ts
838wrapperName :: Int -> String
839wrapperName tag = "_idris_wrapper_" ++ show tag
841getCallback :: [BC] -> [(FDesc, Int)]
842getCallback bc = getCallback' (reverse bc)
844 getCallback' (x:xs) = case hasCallback x of
845 [] -> getCallback' xs
846 cbs -> case findCons cbs xs of
847 [] -> error "Idris function couldn't be wrapped."
850 findCons (c:cs) xs = findCon c xs ++ findCons cs xs
852 findCon c ((MKCON l loc tag args):xs) | snd c == l = [(fst c, tag)]
853 findCon c (_:xs) = findCon c xs
856hasCallback :: BC -> [(FDesc, Reg)]
857hasCallback (FOREIGNCALL l rty (FStr fn) args) = filter isFn args
859 isFn (desc,_) = case toFType desc of
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list