on 4e6adeb help sign in
Idris-dev/src/IRTS/CodegenC.hs
459 lines 23 kB history raw
1module IRTS.CodegenC (codegenC) where
3import Idris.AbsSyntax
4import IRTS.Bytecode
5import IRTS.Lang
6import IRTS.Simplified
7import IRTS.System
8import IRTS.CodegenCommon
9import Idris.Core.TT
10import Paths_idris
11import Util.System
13import Data.Char
14import Data.List (intercalate)
15import System.Process
16import System.Exit
17import System.IO
18import System.Directory
19import System.FilePath ((</>), (<.>))
20import Control.Monad
22codegenC :: [(Name, SDecl)] ->
23 String -> -- output file name
24 OutputType -> -- generate executable if True, only .o if False
25 [FilePath] -> -- include files
26 String -> -- extra object files
27 String -> -- extra compiler flags (libraries)
28 String -> -- extra compiler flags (anything)
29 DbgLevel ->
30 IO ()
31codegenC defs out exec incs objs libs flags dbg
32 = do -- print defs
33 let bc = map toBC defs
34 let h = concatMap toDecl (map fst bc)
35 let cc = concatMap (uncurry toC) bc
36 d <- getDataDir
37 mprog <- readFile (d </> "rts" </> "idris_main" <.> "c")
38 let cout = headers incs ++ debug dbg ++ h ++ cc ++
39 (if (exec == Executable) then mprog else "")
40 case exec of
41 MavenProject -> putStrLn ("FAILURE: output type not supported")
42 Raw -> writeFile out cout
43 _ -> do
44 (tmpn, tmph) <- tempfile
45 hPutStr tmph cout
46 hFlush tmph
47 hClose tmph
48 let useclang = False
49 comp <- getCC
50 libFlags <- getLibFlags
51 incFlags <- getIncFlags
52 let gcc = comp ++ " " ++
53 gccDbg dbg ++ " " ++
54 gccFlags ++
55 " -I. " ++ objs ++ " -x c " ++
56 (if (exec == Executable) then "" else " -c ") ++
57 " " ++ tmpn ++
58 " " ++ libFlags ++
59 " " ++ incFlags ++
60 " " ++ libs ++
61 " " ++ flags ++
62 " -o " ++ out
63-- putStrLn gcc
64 exit <- system gcc
65 when (exit /= ExitSuccess) $
66 putStrLn ("FAILURE: " ++ gcc)
68headers xs =
69 concatMap
70 (\h -> "#include <" ++ h ++ ">\n")
71 (xs ++ ["idris_rts.h", "idris_bitstring.h", "idris_stdfgn.h", "assert.h"])
73debug TRACE = "#define IDRIS_TRACE\n\n"
74debug _ = ""
76-- We're using signed integers now. Make sure we get consistent semantics
77-- out of them from gcc. See e.g. http://thiemonagel.de/2010/01/signed-integer-overflow/
78gccFlags = " -fwrapv -fno-strict-overflow"
80gccDbg DEBUG = "-g"
81gccDbg TRACE = "-O2"
82gccDbg _ = "-O2"
84cname :: Name -> String
85cname n = "_idris_" ++ concatMap cchar (showCG n)
86 where cchar x | isAlpha x || isDigit x = [x]
87 | otherwise = "_" ++ show (fromEnum x) ++ "_"
89indent :: Int -> String
90indent n = replicate (n*4) ' '
92creg RVal = "RVAL"
93creg (L i) = "LOC(" ++ show i ++ ")"
94creg (T i) = "TOP(" ++ show i ++ ")"
95creg Tmp = "REG1"
97toDecl :: Name -> String
98toDecl f = "void " ++ cname f ++ "(VM*, VAL*);\n"
100toC :: Name -> [BC] -> String
101toC f code
102 = -- "/* " ++ show code ++ "*/\n\n" ++
103 "void " ++ cname f ++ "(VM* vm, VAL* oldbase) {\n" ++
104 indent 1 ++ "INITFRAME;\n" ++
105 concatMap (bcc 1) code ++ "}\n\n"
107bcc :: Int -> BC -> String
108bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
109bcc i (ASSIGNCONST l c)
110 = indent i ++ creg l ++ " = " ++ mkConst c ++ ";\n"
111 where
112 mkConst (I i) = "MKINT(" ++ show i ++ ")"
113 mkConst (BI i) | i < (2^30) = "MKINT(" ++ show i ++ ")"
114 | otherwise = "MKBIGC(vm,\"" ++ show i ++ "\")"
115 mkConst (Fl f) = "MKFLOAT(vm, " ++ show f ++ ")"
116 mkConst (Ch c) = "MKINT(" ++ show (fromEnum c) ++ ")"
117 mkConst (Str s) = "MKSTR(vm, " ++ show s ++ ")"
118 mkConst (B8 x) = "idris_b8const(vm, " ++ show x ++ ")"
119 mkConst (B16 x) = "idris_b16const(vm, " ++ show x ++ ")"
120 mkConst (B32 x) = "idris_b32const(vm, " ++ show x ++ ")"
121 mkConst (B64 x) = "idris_b64const(vm, " ++ show x ++ ")"
122 mkConst _ = "MKINT(42424242)"
123bcc i (UPDATE l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
124bcc i (MKCON l tag args)
125 = indent i ++ "allocCon(" ++ creg Tmp ++ ", vm, " ++ show tag ++ "," ++
126 show (length args) ++ ", 0);\n" ++
127 indent i ++ setArgs 0 args ++ "\n" ++
128 indent i ++ creg l ++ " = " ++ creg Tmp ++ ";\n"
130-- "MKCON(vm, " ++ creg l ++ ", " ++ show tag ++ ", " ++
131-- show (length args) ++ concatMap showArg args ++ ");\n"
132 where showArg r = ", " ++ creg r
133 setArgs i [] = ""
134 setArgs i (x : xs) = "SETARG(" ++ creg Tmp ++ ", " ++ show i ++ ", " ++ creg x ++
135 "); " ++ setArgs (i + 1) xs
137bcc i (PROJECT l loc a) = indent i ++ "PROJECT(vm, " ++ creg l ++ ", " ++ show loc ++
138 ", " ++ show a ++ ");\n"
139bcc i (PROJECTINTO r t idx)
140 = indent i ++ creg r ++ " = GETARG(" ++ creg t ++ ", " ++ show idx ++ ");\n"
141bcc i (CASE True r code def)
142 | length code < 4 = showCase i def code
143 where
144 showCode :: Int -> [BC] -> String
145 showCode i bc = "{\n" ++ indent i ++ concatMap (bcc (i + 1)) bc ++
146 indent i ++ "}\n"
148 showCase :: Int -> Maybe [BC] -> [(Int, [BC])] -> String
149 showCase i Nothing [(t, c)] = showCode i c
150 showCase i (Just def) [] = showCode i def
151 showCase i def ((t, c) : cs)
152 = "if (CTAG(" ++ creg r ++ ") == " ++ show t ++ ") " ++ showCode i c
153 ++ "else " ++ showCase i def cs
155bcc i (CASE safe r code def)
156 = indent i ++ "switch(" ++ ctag safe ++ "(" ++ creg r ++ ")) {\n" ++
157 concatMap (showCase i) code ++
158 showDef i def ++
159 indent i ++ "}\n"
160 where
161 ctag True = "CTAG"
162 ctag False = "TAG"
164 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
165 ++ concatMap (bcc (i+1)) bc ++ indent (i + 1) ++ "break;\n"
166 showDef i Nothing = ""
167 showDef i (Just c) = indent i ++ "default:\n"
168 ++ concatMap (bcc (i+1)) c ++ indent (i + 1) ++ "break;\n"
169bcc i (CONSTCASE r code def)
170 | intConsts code
171-- = indent i ++ "switch(GETINT(" ++ creg r ++ ")) {\n" ++
172-- concatMap (showCase i) code ++
173-- showDef i def ++
174-- indent i ++ "}\n"
175 = concatMap (iCase (creg r)) code ++
176 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
177 | strConsts code
178 = concatMap (strCase ("GETSTR(" ++ creg r ++ ")")) code ++
179 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
180 | bigintConsts code
181 = concatMap (biCase (creg r)) code ++
182 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
183 | otherwise = error $ "Can't happen: Can't compile const case " ++ show code
184 where
185 intConsts ((I _, _ ) : _) = True
186 intConsts ((Ch _, _ ) : _) = True
187 intConsts _ = False
189 bigintConsts ((BI _, _ ) : _) = True
190 bigintConsts _ = False
192 strConsts ((Str _, _ ) : _) = True
193 strConsts _ = False
195 strCase sv (s, bc) =
196 indent i ++ "if (strcmp(" ++ sv ++ ", " ++ show s ++ ") == 0) {\n" ++
197 concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
198 biCase bv (BI b, bc) =
199 indent i ++ "if (bigEqConst(" ++ bv ++ ", " ++ show b ++ ")) {\n"
200 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
201 iCase v (I b, bc) =
202 indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
203 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
204 iCase v (Ch b, bc) =
205 indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show (fromEnum b) ++ ") {\n"
206 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
208 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
209 ++ concatMap (bcc (i+1)) bc ++
210 indent (i + 1) ++ "break;\n"
211 showDef i Nothing = ""
212 showDef i (Just c) = indent i ++ "default:\n"
213 ++ concatMap (bcc (i+1)) c ++
214 indent (i + 1) ++ "break;\n"
215 showDefS i Nothing = ""
216 showDefS i (Just c) = concatMap (bcc (i+1)) c
218bcc i (CALL n) = indent i ++ "CALL(" ++ cname n ++ ");\n"
219bcc i (TAILCALL n) = indent i ++ "TAILCALL(" ++ cname n ++ ");\n"
220bcc i (SLIDE n) = indent i ++ "SLIDE(vm, " ++ show n ++ ");\n"
221bcc i REBASE = indent i ++ "REBASE;\n"
222bcc i (RESERVE 0) = ""
223bcc i (RESERVE n) = indent i ++ "RESERVE(" ++ show n ++ ");\n"
224bcc i (ADDTOP 0) = ""
225bcc i (ADDTOP n) = indent i ++ "ADDTOP(" ++ show n ++ ");\n"
226bcc i (TOPBASE n) = indent i ++ "TOPBASE(" ++ show n ++ ");\n"
227bcc i (BASETOP n) = indent i ++ "BASETOP(" ++ show n ++ ");\n"
228bcc i STOREOLD = indent i ++ "STOREOLD;\n"
229bcc i (OP l fn args) = indent i ++ doOp (creg l ++ " = ") fn args ++ ";\n"
230bcc i (FOREIGNCALL l LANG_C rty fn args)
231 = indent i ++
232 c_irts rty (creg l ++ " = ")
233 (fn ++ "(" ++ showSep "," (map fcall args) ++ ")") ++ ";\n"
234 where fcall (t, arg) = irts_c t (creg arg)
235bcc i (NULL r) = indent i ++ creg r ++ " = NULL;\n" -- clear, so it'll be GCed
236bcc i (ERROR str) = indent i ++ "fprintf(stderr, " ++ show str ++ "); assert(0); exit(-1);"
237-- bcc i _ = indent i ++ "// not done yet\n"
241c_irts (FArith (ATInt ITNative)) l x = l ++ "MKINT((i_int)(" ++ x ++ "))"
242c_irts (FArith (ATInt ITChar)) l x = c_irts (FArith (ATInt ITNative)) l x
243c_irts (FArith (ATInt (ITFixed ity))) l x
244 = l ++ "idris_b" ++ show (nativeTyWidth ity) ++ "const(vm, " ++ x ++ ")"
245c_irts FString l x = l ++ "MKSTR(vm, " ++ x ++ ")"
246c_irts FUnit l x = x
247c_irts FPtr l x = l ++ "MKPTR(vm, " ++ x ++ ")"
248c_irts (FArith ATFloat) l x = l ++ "MKFLOAT(vm, " ++ x ++ ")"
249c_irts FAny l x = l ++ x
251irts_c (FArith (ATInt ITNative)) x = "GETINT(" ++ x ++ ")"
252irts_c (FArith (ATInt ITChar)) x = irts_c (FArith (ATInt ITNative)) x
253irts_c (FArith (ATInt (ITFixed ity))) x
254 = "(" ++ x ++ "->info.bits" ++ show (nativeTyWidth ity) ++ ")"
255irts_c FString x = "GETSTR(" ++ x ++ ")"
256irts_c FUnit x = x
257irts_c FPtr x = "GETPTR(" ++ x ++ ")"
258irts_c (FArith ATFloat) x = "GETFLOAT(" ++ x ++ ")"
259irts_c FAny x = x
261bitOp v op ty args = v ++ "idris_b" ++ show (nativeTyWidth ty) ++ op ++ "(vm, " ++ intercalate ", " (map creg args) ++ ")"
263bitCoerce v op input output arg
264 = v ++ "idris_b" ++ show (nativeTyWidth input) ++ op ++ show (nativeTyWidth output) ++ "(vm, " ++ creg arg ++ ")"
266signedTy :: NativeTy -> String
267signedTy t = "int" ++ show (nativeTyWidth t) ++ "_t"
269doOp v (LPlus (ATInt ITNative)) [l, r] = v ++ "ADD(" ++ creg l ++ ", " ++ creg r ++ ")"
270doOp v (LMinus (ATInt ITNative)) [l, r] = v ++ "INTOP(-," ++ creg l ++ ", " ++ creg r ++ ")"
271doOp v (LTimes (ATInt ITNative)) [l, r] = v ++ "MULT(" ++ creg l ++ ", " ++ creg r ++ ")"
272doOp v (LUDiv ITNative) [l, r] = v ++ "UINTOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
273doOp v (LSDiv (ATInt ITNative)) [l, r] = v ++ "INTOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
274doOp v (LURem ITNative) [l, r] = v ++ "UINTOP(%," ++ creg l ++ ", " ++ creg r ++ ")"
275doOp v (LSRem (ATInt ITNative)) [l, r] = v ++ "INTOP(%," ++ creg l ++ ", " ++ creg r ++ ")"
276doOp v (LAnd ITNative) [l, r] = v ++ "INTOP(&," ++ creg l ++ ", " ++ creg r ++ ")"
277doOp v (LOr ITNative) [l, r] = v ++ "INTOP(|," ++ creg l ++ ", " ++ creg r ++ ")"
278doOp v (LXOr ITNative) [l, r] = v ++ "INTOP(^," ++ creg l ++ ", " ++ creg r ++ ")"
279doOp v (LSHL ITNative) [l, r] = v ++ "INTOP(<<," ++ creg l ++ ", " ++ creg r ++ ")"
280doOp v (LLSHR ITNative) [l, r] = v ++ "UINTOP(>>," ++ creg l ++ ", " ++ creg r ++ ")"
281doOp v (LASHR ITNative) [l, r] = v ++ "INTOP(>>," ++ creg l ++ ", " ++ creg r ++ ")"
282doOp v (LCompl ITNative) [x] = v ++ "INTOP(~," ++ creg x ++ ")"
283doOp v (LEq (ATInt ITNative)) [l, r] = v ++ "INTOP(==," ++ creg l ++ ", " ++ creg r ++ ")"
284doOp v (LSLt (ATInt ITNative)) [l, r] = v ++ "INTOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
285doOp v (LSLe (ATInt ITNative)) [l, r] = v ++ "INTOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
286doOp v (LSGt (ATInt ITNative)) [l, r] = v ++ "INTOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
287doOp v (LSGe (ATInt ITNative)) [l, r] = v ++ "INTOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
288doOp v (LLt ITNative) [l, r] = v ++ "UINTOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
289doOp v (LLe ITNative) [l, r] = v ++ "UINTOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
290doOp v (LGt ITNative) [l, r] = v ++ "UINTOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
291doOp v (LGe ITNative) [l, r] = v ++ "UINTOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
293doOp v (LPlus (ATInt ITChar)) [l, r] = doOp v (LPlus (ATInt ITNative)) [l, r]
294doOp v (LMinus (ATInt ITChar)) [l, r] = doOp v (LMinus (ATInt ITNative)) [l, r]
295doOp v (LTimes (ATInt ITChar)) [l, r] = doOp v (LTimes (ATInt ITNative)) [l, r]
296doOp v (LUDiv ITChar) [l, r] = doOp v (LUDiv ITNative) [l, r]
297doOp v (LSDiv (ATInt ITChar)) [l, r] = doOp v (LSDiv (ATInt ITNative)) [l, r]
298doOp v (LURem ITChar) [l, r] = doOp v (LURem ITNative) [l, r]
299doOp v (LSRem (ATInt ITChar)) [l, r] = doOp v (LSRem (ATInt ITNative)) [l, r]
300doOp v (LAnd ITChar) [l, r] = doOp v (LAnd ITNative) [l, r]
301doOp v (LOr ITChar) [l, r] = doOp v (LOr ITNative) [l, r]
302doOp v (LXOr ITChar) [l, r] = doOp v (LXOr ITNative) [l, r]
303doOp v (LSHL ITChar) [l, r] = doOp v (LSHL ITNative) [l, r]
304doOp v (LLSHR ITChar) [l, r] = doOp v (LLSHR ITNative) [l, r]
305doOp v (LASHR ITChar) [l, r] = doOp v (LASHR ITNative) [l, r]
306doOp v (LCompl ITChar) [x] = doOp v (LCompl ITNative) [x]
307doOp v (LEq (ATInt ITChar)) [l, r] = doOp v (LEq (ATInt ITNative)) [l, r]
308doOp v (LSLt (ATInt ITChar)) [l, r] = doOp v (LSLt (ATInt ITNative)) [l, r]
309doOp v (LSLe (ATInt ITChar)) [l, r] = doOp v (LSLe (ATInt ITNative)) [l, r]
310doOp v (LSGt (ATInt ITChar)) [l, r] = doOp v (LSGt (ATInt ITNative)) [l, r]
311doOp v (LSGe (ATInt ITChar)) [l, r] = doOp v (LSGe (ATInt ITNative)) [l, r]
312doOp v (LLt ITChar) [l, r] = doOp v (LLt ITNative) [l, r]
313doOp v (LLe ITChar) [l, r] = doOp v (LLe ITNative) [l, r]
314doOp v (LGt ITChar) [l, r] = doOp v (LGt ITNative) [l, r]
315doOp v (LGe ITChar) [l, r] = doOp v (LGe ITNative) [l, r]
317doOp v (LPlus ATFloat) [l, r] = v ++ "FLOATOP(+," ++ creg l ++ ", " ++ creg r ++ ")"
318doOp v (LMinus ATFloat) [l, r] = v ++ "FLOATOP(-," ++ creg l ++ ", " ++ creg r ++ ")"
319doOp v (LTimes ATFloat) [l, r] = v ++ "FLOATOP(*," ++ creg l ++ ", " ++ creg r ++ ")"
320doOp v (LSDiv ATFloat) [l, r] = v ++ "FLOATOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
321doOp v (LEq ATFloat) [l, r] = v ++ "FLOATBOP(==," ++ creg l ++ ", " ++ creg r ++ ")"
322doOp v (LSLt ATFloat) [l, r] = v ++ "FLOATBOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
323doOp v (LSLe ATFloat) [l, r] = v ++ "FLOATBOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
324doOp v (LSGt ATFloat) [l, r] = v ++ "FLOATBOP(>," ++ creg l ++ ", " ++ creg r ++ ")"
325doOp v (LSGe ATFloat) [l, r] = v ++ "FLOATBOP(>=," ++ creg l ++ ", " ++ creg r ++ ")"
327doOp v (LIntFloat ITBig) [x] = v ++ "idris_castBigFloat(vm, " ++ creg x ++ ")"
328doOp v (LFloatInt ITBig) [x] = v ++ "idris_castFloatBig(vm, " ++ creg x ++ ")"
329doOp v (LPlus (ATInt ITBig)) [l, r] = v ++ "idris_bigPlus(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
330doOp v (LMinus (ATInt ITBig)) [l, r] = v ++ "idris_bigMinus(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
331doOp v (LTimes (ATInt ITBig)) [l, r] = v ++ "idris_bigTimes(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
332doOp v (LSDiv (ATInt ITBig)) [l, r] = v ++ "idris_bigDivide(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
333doOp v (LSRem (ATInt ITBig)) [l, r] = v ++ "idris_bigMod(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
334doOp v (LEq (ATInt ITBig)) [l, r] = v ++ "idris_bigEq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
335doOp v (LSLt (ATInt ITBig)) [l, r] = v ++ "idris_bigLt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
336doOp v (LSLe (ATInt ITBig)) [l, r] = v ++ "idris_bigLe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
337doOp v (LSGt (ATInt ITBig)) [l, r] = v ++ "idris_bigGt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
338doOp v (LSGe (ATInt ITBig)) [l, r] = v ++ "idris_bigGe(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
340doOp v LStrConcat [l,r] = v ++ "idris_concat(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
341doOp v LStrLt [l,r] = v ++ "idris_strlt(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
342doOp v LStrEq [l,r] = v ++ "idris_streq(vm, " ++ creg l ++ ", " ++ creg r ++ ")"
343doOp v LStrLen [x] = v ++ "idris_strlen(vm, " ++ creg x ++ ")"
345doOp v (LIntFloat ITNative) [x] = v ++ "idris_castIntFloat(" ++ creg x ++ ")"
346doOp v (LFloatInt ITNative) [x] = v ++ "idris_castFloatInt(" ++ creg x ++ ")"
347doOp v (LSExt ITNative ITBig) [x] = v ++ "idris_castIntBig(vm, " ++ creg x ++ ")"
348doOp v (LTrunc ITBig ITNative) [x] = v ++ "idris_castBigInt(vm, " ++ creg x ++ ")"
349doOp v (LStrInt ITBig) [x] = v ++ "idris_castStrBig(vm, " ++ creg x ++ ")"
350doOp v (LIntStr ITBig) [x] = v ++ "idris_castBigStr(vm, " ++ creg x ++ ")"
351doOp v (LIntStr ITNative) [x] = v ++ "idris_castIntStr(vm, " ++ creg x ++ ")"
352doOp v (LStrInt ITNative) [x] = v ++ "idris_castStrInt(vm, " ++ creg x ++ ")"
353doOp v LFloatStr [x] = v ++ "idris_castFloatStr(vm, " ++ creg x ++ ")"
354doOp v LStrFloat [x] = v ++ "idris_castStrFloat(vm, " ++ creg x ++ ")"
356doOp v LReadStr [x] = v ++ "idris_readStr(vm, GETPTR(" ++ creg x ++ "))"
357doOp _ LPrintNum [x] = "printf(\"%ld\\n\", GETINT(" ++ creg x ++ "))"
358doOp _ LPrintStr [x] = "fputs(GETSTR(" ++ creg x ++ "), stdout)"
360doOp v (LSLt (ATInt (ITFixed ty))) [x, y] = bitOp v "SLt" ty [x, y]
361doOp v (LSLe (ATInt (ITFixed ty))) [x, y] = bitOp v "SLte" ty [x, y]
362doOp v (LEq (ATInt (ITFixed ty))) [x, y] = bitOp v "Eq" ty [x, y]
363doOp v (LSGe (ATInt (ITFixed ty))) [x, y] = bitOp v "SGte" ty [x, y]
364doOp v (LSGt (ATInt (ITFixed ty))) [x, y] = bitOp v "SGt" ty [x, y]
365doOp v (LLt (ITFixed ty)) [x, y] = bitOp v "Lt" ty [x, y]
366doOp v (LLe (ITFixed ty)) [x, y] = bitOp v "Lte" ty [x, y]
367doOp v (LGe (ITFixed ty)) [x, y] = bitOp v "Gte" ty [x, y]
368doOp v (LGt (ITFixed ty)) [x, y] = bitOp v "Gt" ty [x, y]
370doOp v (LSHL (ITFixed ty)) [x, y] = bitOp v "Shl" ty [x, y]
371doOp v (LLSHR (ITFixed ty)) [x, y] = bitOp v "LShr" ty [x, y]
372doOp v (LASHR (ITFixed ty)) [x, y] = bitOp v "AShr" ty [x, y]
373doOp v (LAnd (ITFixed ty)) [x, y] = bitOp v "And" ty [x, y]
374doOp v (LOr (ITFixed ty)) [x, y] = bitOp v "Or" ty [x, y]
375doOp v (LXOr (ITFixed ty)) [x, y] = bitOp v "Xor" ty [x, y]
376doOp v (LCompl (ITFixed ty)) [x] = bitOp v "Compl" ty [x]
378doOp v (LPlus (ATInt (ITFixed ty))) [x, y] = bitOp v "Plus" ty [x, y]
379doOp v (LMinus (ATInt (ITFixed ty))) [x, y] = bitOp v "Minus" ty [x, y]
380doOp v (LTimes (ATInt (ITFixed ty))) [x, y] = bitOp v "Times" ty [x, y]
381doOp v (LUDiv (ITFixed ty)) [x, y] = bitOp v "UDiv" ty [x, y]
382doOp v (LSDiv (ATInt (ITFixed ty))) [x, y] = bitOp v "SDiv" ty [x, y]
383doOp v (LURem (ITFixed ty)) [x, y] = bitOp v "URem" ty [x, y]
384doOp v (LSRem (ATInt (ITFixed ty))) [x, y] = bitOp v "SRem" ty [x, y]
386doOp v (LSExt (ITFixed from) ITBig) [x]
387 = v ++ "MKBIGSI(vm, (" ++ signedTy from ++ ")" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
388doOp v (LSExt ITNative (ITFixed to)) [x]
389 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, GETINT(" ++ creg x ++ "))"
390doOp v (LSExt ITChar (ITFixed to)) [x]
391 = doOp v (LSExt ITNative (ITFixed to)) [x]
392doOp v (LSExt (ITFixed from) ITNative) [x]
393 = v ++ "MKINT((i_int)((" ++ signedTy from ++ ")" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ "))"
394doOp v (LSExt (ITFixed from) ITChar) [x]
395 = doOp v (LSExt (ITFixed from) ITNative) [x]
396doOp v (LSExt (ITFixed from) (ITFixed to)) [x]
397 | nativeTyWidth from < nativeTyWidth to = bitCoerce v "S" from to x
398doOp v (LZExt ITNative (ITFixed to)) [x]
399 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, (uintptr_t)GETINT(" ++ creg x ++ ")"
400doOp v (LZExt ITChar (ITFixed to)) [x]
401 = doOp v (LZExt ITNative (ITFixed to)) [x]
402doOp v (LZExt (ITFixed from) ITNative) [x]
403 = v ++ "MKINT((i_int)" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
404doOp v (LZExt (ITFixed from) ITChar) [x]
405 = doOp v (LZExt (ITFixed from) ITNative) [x]
406doOp v (LZExt (ITFixed from) ITBig) [x]
407 = v ++ "MKBIGUI(vm, " ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
408doOp v (LZExt ITNative ITBig) [x]
409 = v ++ "MKBIGUI(vm, (uintptr_t)GETINT(" ++ creg x ++ "))"
410doOp v (LZExt (ITFixed from) (ITFixed to)) [x]
411 | nativeTyWidth from < nativeTyWidth to = bitCoerce v "Z" from to x
412doOp v (LTrunc ITNative (ITFixed to)) [x]
413 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, GETINT(" ++ creg x ++ "))"
414doOp v (LTrunc ITChar (ITFixed to)) [x]
415 = doOp v (LTrunc ITNative (ITFixed to)) [x]
416doOp v (LTrunc (ITFixed from) ITNative) [x]
417 = v ++ "MKINT((i_int)" ++ creg x ++ "->info.bits" ++ show (nativeTyWidth from) ++ ")"
418doOp v (LTrunc (ITFixed from) ITChar) [x]
419 = doOp v (LTrunc (ITFixed from) ITNative) [x]
420doOp v (LTrunc ITBig (ITFixed to)) [x]
421 = v ++ "idris_b" ++ show (nativeTyWidth to) ++ "const(vm, ISINT(" ++ creg x ++ ") ? GETINT(" ++ creg x ++ ") : mpz_get_ui(GETMPZ(" ++ creg x ++ ")))"
422doOp v (LTrunc (ITFixed from) (ITFixed to)) [x]
423 | nativeTyWidth from > nativeTyWidth to = bitCoerce v "T" from to x
425doOp v LFExp [x] = v ++ flUnOp "exp" (creg x)
426doOp v LFLog [x] = v ++ flUnOp "log" (creg x)
427doOp v LFSin [x] = v ++ flUnOp "sin" (creg x)
428doOp v LFCos [x] = v ++ flUnOp "cos" (creg x)
429doOp v LFTan [x] = v ++ flUnOp "tan" (creg x)
430doOp v LFASin [x] = v ++ flUnOp "asin" (creg x)
431doOp v LFACos [x] = v ++ flUnOp "acos" (creg x)
432doOp v LFATan [x] = v ++ flUnOp "atan" (creg x)
433doOp v LFSqrt [x] = v ++ flUnOp "sqrt" (creg x)
434doOp v LFFloor [x] = v ++ flUnOp "floor" (creg x)
435doOp v LFCeil [x] = v ++ flUnOp "ceil" (creg x)
437doOp v LStrHead [x] = v ++ "idris_strHead(vm, " ++ creg x ++ ")"
438doOp v LStrTail [x] = v ++ "idris_strTail(vm, " ++ creg x ++ ")"
439doOp v LStrCons [x, y] = v ++ "idris_strCons(vm, " ++ creg x ++ "," ++ creg y ++ ")"
440doOp v LStrIndex [x, y] = v ++ "idris_strIndex(vm, " ++ creg x ++ "," ++ creg y ++ ")"
441doOp v LStrRev [x] = v ++ "idris_strRev(vm, " ++ creg x ++ ")"
443doOp v LStdIn [] = v ++ "MKPTR(vm, stdin)"
444doOp v LStdOut [] = v ++ "MKPTR(vm, stdout)"
445doOp v LStdErr [] = v ++ "MKPTR(vm, stderr)"
447doOp v LFork [x] = v ++ "MKPTR(vm, vmThread(vm, " ++ cname (MN 0 "EVAL") ++ ", " ++ creg x ++ "))"
448doOp v LPar [x] = v ++ creg x -- "MKPTR(vm, vmThread(vm, " ++ cname (MN 0 "EVAL") ++ ", " ++ creg x ++ "))"
449doOp v LVMPtr [] = v ++ "MKPTR(vm, vm)"
450doOp v LNullPtr [] = v ++ "MKPTR(vm, NULL)"
451doOp v (LChInt ITNative) args = v ++ creg (last args)
452doOp v (LChInt ITChar) args = doOp v (LChInt ITNative) args
453doOp v (LIntCh ITNative) args = v ++ creg (last args)
454doOp v (LIntCh ITChar) args = doOp v (LIntCh ITNative) args
455doOp v LNoOp args = v ++ creg (last args)
456doOp _ op _ = "FAIL /* " ++ show op ++ " */"
458flUnOp :: String -> String -> String
459flUnOp name val = "MKFLOAT(vm, " ++ name ++ "(GETFLOAT(" ++ val ++ ")))"