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