@@ -1,23 +1,1462 @@
1module IRTS.CodegenJava (codegenJava) where
1module IRTS.CodegenJava (codegenJava) where
3−import IRTS.BCImp
4+import IRTS.BCImp
5+import IRTS.CodegenCommon
4−import IRTS.Lang
6+import IRTS.Lang
5−import IRTS.Simplified
7+import IRTS.Simplified
7−import Paths_idris
8+import Paths_idris
8−import Util.System
9+import Util.System
9−import IRTS.CodegenCommon
10+
10−
11+import Control.Applicative
11−import Data.Char
14+import Data.Char
12−import System.Process
15+import Data.Maybe (fromJust)
16+import Language.Java.Parser
17+import Language.Java.Pretty
18+import Language.Java.Syntax hiding (Name)
19+import qualified Language.Java.Syntax as J
20+import System.Directory
13−import System.Exit
21+import System.Exit
22+import System.FilePath
14−import System.IO
23+import System.IO
15−import System.Directory
24+import System.Process
18codegenJava :: [(Name, SDecl)] ->
26codegenJava :: [(Name, SDecl)] ->
19− String -> -- output file name
27+ FilePath -> -- output file name
20 OutputType ->
28 OutputType ->
22−codegenJava defs out exec = putStrLn "Not implemented yet"
30+codegenJava defs out exec =
32+ (writeFile out . flatIndent . prettyPrint)
33+ (mkCompilationUnit defs out)
35+mkCompilationUnit :: [(Name, SDecl)] ->
37+ Either String CompilationUnit
38+mkCompilationUnit defs out =
39+ CompilationUnit Nothing [ ImportDecl False idrisRts True
40+ , ImportDecl True idrisForeign True
41+ , ImportDecl False bigInteger False
42+ , ImportDecl False stringBuffer False
43+ , ImportDecl False runtimeException False
44+ , ImportDecl False scanner False
45+ , ImportDecl False arrays False
49+ idrisRts = J.Name $ map Ident ["org", "idris", "rts"]
50+ idrisForeign = J.Name $ map Ident ["org", "idris", "rts", "ForeignPrimitives"]
51+ bigInteger = J.Name $ map Ident ["java", "math", "BigInteger"]
52+ stringBuffer = J.Name $ map Ident ["java", "lang", "StringBuffer"]
53+ runtimeException = J.Name $ map Ident ["java", "lang", "RuntimeException"]
54+ scanner = J.Name $ map Ident ["java", "util", "Scanner"]
55+ arrays = J.Name $ map Ident ["java", "util", "Arrays"]
57+flatIndent :: String -> String
58+flatIndent (' ' : ' ' : xs) = flatIndent xs
59+flatIndent (x:xs) = x:flatIndent xs
62+prefixCallNamespaces :: Ident -> SExp -> SExp
63+prefixCallNamespaces (Ident name) (SApp tail (NS n ns) args) = SApp tail (NS n (name:ns)) args
64+prefixCallNamespaces name (SLet var e1 e2) = SLet var (prefixCallNamespaces name e1) (prefixCallNamespaces name e2)
65+prefixCallNamespaces name (SUpdate var e) = SUpdate var (prefixCallNamespaces name e)
66+prefixCallNamespaces name (SCase var alts) = SCase var (map (prefixCallNamespacesCase name) alts)
67+prefixCallNamespaces name (SChkCase var alts) = SChkCase var (map (prefixCallNamespacesCase name) alts)
68+prefixCallNamespaces _ exp = exp
70+prefixCallNamespacesCase :: Ident -> SAlt -> SAlt
71+prefixCallNamespacesCase name (SConCase x y n ns e) = SConCase x y n ns (prefixCallNamespaces name e)
72+prefixCallNamespacesCase name (SConstCase c e) = SConstCase c (prefixCallNamespaces name e)
73+prefixCallNamespacesCase name (SDefaultCase e) = SDefaultCase (prefixCallNamespaces name e)
75+prefixCallNamespacesDecl :: Ident -> SDecl -> SDecl
76+prefixCallNamespacesDecl name (SFun fname args i e) = SFun fname args i (prefixCallNamespaces name e)
78+mkTypeDecl :: [(Name, SDecl)] ->
80+ Either String [TypeDecl]
82+ (\ name body -> [ClassTypeDecl $ ClassDecl [ Public
83+ , Annotation $ SingleElementAnnotation
84+ (J.Name [Ident "SuppressWarnings"])
85+ (EVVal . InitExp . Lit $ String "unchecked")
93+ <*> ( mkClassName out
94+ >>= (\ ident -> mkClassBody (map (second (prefixCallNamespacesDecl ident)) defs))
97+mkClassName :: FilePath -> Either String Ident
99+ left (\ err -> "Parser error in \"" ++ path ++ "\": " ++ (show err))
100+ (parser ident . takeBaseName $ takeFileName path)
102+mkClassBody :: [(Name, SDecl)] ->
103+ Either String ClassBody
104+mkClassBody defs = (ClassBody . addMainMethod . mergeInnerClasses)
105+ <$> mapM mkDecl defs
107+addMainMethod :: [Decl] -> [Decl]
109+ | findMain decls = mkMainMethod : decls
112+ findMain ((MemberDecl (MemberClassDecl (ClassDecl _ (Ident "Main") _ _ _ (ClassBody body)))):_) =
114+ findMain (_:decls) = findMain decls
117+ findMainMethod ((MemberDecl (MethodDecl _ _ _ (Ident "main") [] _ _)):_) = True
118+ findMainMethod (_:decls) = findMainMethod decls
119+ findMainMethod [] = False
123+ MemberDecl $ MethodDecl [Public, Static]
127+ [FormalParam [] stringArrayType False (VarId $ Ident "args")]
129+ (MethodBody . Just $ Block [ BlockStmt . ExpStmt . MethodInv $
130+ MethodCall (J.Name [Ident "idris_initArgs"])
131+ [ MethodInv $ TypeMethodCall
132+ (J.Name [Ident "Thread"])
134+ (Ident "currentThread")
136+ , ExpName $ J.Name [Ident "args"]
138+ , BlockStmt . ExpStmt . MethodInv $
139+ MethodCall (J.Name [Ident "runMain_0"])
144+mergeInnerClasses :: [Decl] -> [Decl]
145+mergeInnerClasses = foldl mergeInner []
147+ mergeInner ((decl@(MemberDecl (MemberClassDecl (ClassDecl priv name targs ext imp (ClassBody body))))):decls)
148+ decl'@(MemberDecl (MemberClassDecl (ClassDecl _ name' _ ext' imp' (ClassBody body'))))
150+ (MemberDecl $ MemberClassDecl $
156+ (ClassBody $ mergeInnerClasses (body ++ body')))
158+ | otherwise = decl:(mergeInner decls decl')
159+ mergeInner (decl:decls) decl' = decl:(mergeInner decls decl')
160+ mergeInner [] decl' = [decl']
163+mkIdentifier :: Name -> Either String Ident
164+mkIdentifier (NS name _) = mkIdentifier name
165+mkIdentifier (MN i name) = (\ (Ident x) -> Ident $ x ++ ('_' : show i))
166+ <$> mkIdentifier (UN name)
167+mkIdentifier (UN name) =
168+ left (\ err -> "Parser error in \"" ++ name ++ "\": " ++ (show err))
173+ $ cleanWs False name)
176+ | isNumber x = '_' : (x:xs)
177+ | otherwise = (x:xs)
179+ cleanNonLetter (x:xs)
180+ | x == '#' = "_Hash" ++ cleanNonLetter xs
181+ | x == '@' = "_At" ++ cleanNonLetter xs
182+ | x == '$' = "_Dollar" ++ cleanNonLetter xs
183+ | x == '!' = "_Bang" ++ cleanNonLetter xs
184+ | x == '.' = "_Dot" ++ cleanNonLetter xs
185+ | x == '\'' = "_Prime" ++ cleanNonLetter xs
186+ | x == '*' = "_Times" ++ cleanNonLetter xs
187+ | x == '+' = "_Plus" ++ cleanNonLetter xs
188+ | x == '/' = "_Divide" ++ cleanNonLetter xs
189+ | x == '-' = "_Minus" ++ cleanNonLetter xs
190+ | x == '%' = "_Mod" ++ cleanNonLetter xs
191+ | x == '<' = "_LessThan" ++ cleanNonLetter xs
192+ | x == '=' = "_Equals" ++ cleanNonLetter xs
193+ | x == '>' = "_MoreThan" ++ cleanNonLetter xs
194+ | x == '[' = "_LSBrace" ++ cleanNonLetter xs
195+ | x == ']' = "_RSBrace" ++ cleanNonLetter xs
196+ | x == '(' = "_LBrace" ++ cleanNonLetter xs
197+ | x == ')' = "_RBrace" ++ cleanNonLetter xs
198+ | x == '_' = "__" ++ cleanNonLetter xs
199+ | not (isAlphaNum x) = "_" ++ (show $ ord x) ++ xs
200+ | otherwise = x:cleanNonLetter xs
201+ cleanNonLetter [] = []
202+ cleanWs capitalize (x:xs)
203+ | isSpace x = cleanWs True xs
204+ | capitalize = (toUpper x) : (cleanWs False xs)
205+ | otherwise = x : (cleanWs False xs)
209+ cleanReserved "param" = "_param"
210+ cleanReserved "context" = "_context"
211+ cleanReserved "newcontext" = "_newcontext"
213+ cleanReserved "void" = "_void"
214+ cleanReserved "null" = "_null"
215+ cleanReserved "int" = "_int"
216+ cleanReserved "long" = "_long"
217+ cleanReserved "char" = "_char"
218+ cleanReserved "byte" = "_byte"
219+ cleanReserved "double" = "_double"
220+ cleanReserved "float" = "_float"
221+ cleanReserved "boolean" = "_boolean"
222+ cleanReserved "Object" = "_Object"
223+ cleanReserved "String" = "_String"
224+ cleanReserved "StringBuilder" = "_StringBuilder"
225+ cleanReserved "StringBuffer" = "_StringBuffer"
226+ cleanReserved "Scanner" = "_Scanner"
227+ cleanReserved "Integer" = "_Integer"
228+ cleanReserved "Double" = "_Double"
229+ cleanReserved "Byte" = "_Byte"
230+ cleanReserved "Character" = "_Character"
231+ cleanReserved "BigInteger" = "_BigInteger"
232+ cleanReserved "Boolean" = "_Boolean"
233+ cleanReserved "Closure" = "_Closure"
234+ cleanReserved "IdrisObject" = "_IdrisObject"
235+ cleanReserved "TailCallClosure" = "_TailCallClosure"
236+ cleanReserved "System" = "_System"
237+ cleanReserved "Math" = "_Math"
238+ cleanReserved "Arrays" = "_Arrays"
239+ cleanReserved "RuntimeException" = "_RuntimeException"
240+ cleanReserved "Comparable" = "_Comparable"
242+ cleanReserved "class" = "_class"
243+ cleanReserved "enum" = "_enum"
244+ cleanReserved "interface" = "_interface"
245+ cleanReserved "extends" = "_extends"
246+ cleanReserved "implements" = "_implements"
247+ cleanReserved "public" = "_public"
248+ cleanReserved "private" = "_private"
249+ cleanReserved "protected" = "_protected"
250+ cleanReserved "static" = "_static"
251+ cleanReserved "final" = "_final"
252+ cleanReserved "abstract" = "_abstract"
253+ cleanReserved "strict" = "_strict"
254+ cleanReserved "volatile" = "_volatile"
255+ cleanReserved "transient" = "_transient"
256+ cleanReserved "native" = "_native"
258+ cleanReserved "import" = "_import"
259+ cleanReserved "package" = "_package"
261+ cleanReserved "throw" = "_throw"
262+ cleanReserved "throws" = "_throws"
263+ cleanReserved "try" = "_try"
264+ cleanReserved "catch" = "_catch"
266+ cleanReserved "synchronized" = "_synchronized"
268+ cleanReserved "if" = "_if"
269+ cleanReserved "else" = "_else"
270+ cleanReserved "switch" = "_switch"
271+ cleanReserved "case" = "_case"
272+ cleanReserved "default" = "_default"
274+ cleanReserved "while" = "_while"
275+ cleanReserved "for" = "_for"
276+ cleanReserved "do" = "_do"
277+ cleanReserved "break" = "_break"
278+ cleanReserved "continue" = "_continue"
279+ cleanReserved "goto" = "_goto"
281+ cleanReserved "this" = "_this"
282+ cleanReserved "super" = "_super"
283+ cleanReserved "new" = "_new"
284+ cleanReserved "return" = "_return"
286+ cleanReserved "idris_initArgs" = "_idris_initArgs"
287+ cleanReserved "idris_numArgs" = "_idris_numArgs"
288+ cleanReserved "idris_getArg" = "_idris_getArg"
289+ cleanReserved "getenv" = "_getenv"
290+ cleanReserved "exit" = "_exit"
291+ cleanReserved "usleep" = "_usleep"
292+ cleanReserved "idris_sendMessage" = "_idris_sendMessage"
293+ cleanReserved "idris_checkMessage" = "_idris_checkMessage"
294+ cleanReserved "idris_recvMessage" = "_idris_recvMessage"
295+ cleanReserved "putStr" = "_putStr"
296+ cleanReserved "putchar" = "_putchar"
297+ cleanReserved "getchar" = "_getchar"
298+ cleanReserved "fileOpen" = "_fileOpen"
299+ cleanReserved "fileClose" = "_fileClose"
300+ cleanReserved "fputStr" = "_fputStr"
301+ cleanReserved "fileEOF" = "_fileEOF"
302+ cleanReserved "isNull" = "_isNull"
306+mkName :: Name -> Either String J.Name
307+mkName (NS name nss) = (\ n ns -> J.Name (n:ns))
308+ <$> mkIdentifier name
309+ <*> mapM (mkIdentifier . UN) nss
310+mkName n = J.Name . (:[]) <$> mkIdentifier n
312+voidType :: ClassType
313+voidType = ClassType [(Ident "Void", [])]
315+objectType :: ClassType
316+objectType = ClassType [(Ident "Object", [])]
318+objectArrayType :: Language.Java.Syntax.Type
319+objectArrayType = RefType . ArrayType . RefType . ClassRefType $ objectType
321+idrisClosureType :: ClassType
322+idrisClosureType = ClassType [(Ident "Closure", [])]
324+idrisTailCallClosureType :: ClassType
325+idrisTailCallClosureType = ClassType [(Ident "TailCallClosure", [])]
327+--idrisFunctionType :: ClassType
328+--idrisFunctionType = ClassType [(Ident "IdrisFunction", [])]
330+idrisObjectType :: ClassType
331+idrisObjectType = ClassType [(Ident "IdrisObject", [])]
334+contextArray = ExpName $ J.Name [Ident "context"]
336+charType :: ClassType
337+charType = ClassType [(Ident "Character", [])]
339+byteType :: ClassType
340+byteType = ClassType [(Ident "Byte", [])]
342+shortType :: ClassType
343+shortType = ClassType [(Ident "Short", [])]
345+integerType :: ClassType
346+integerType = ClassType [(Ident "Integer", [])]
348+longType :: ClassType
349+longType = ClassType [(Ident "Integer", [])]
351+bigIntegerType :: ClassType
352+bigIntegerType = ClassType [(Ident "BigInteger", [])]
354+doubleType :: ClassType
355+doubleType = ClassType [(Ident "Double", [])]
357+stringType :: ClassType
358+stringType = ClassType [(Ident "String", [])]
360+stringArrayType :: Language.Java.Syntax.Type
361+stringArrayType = RefType . ArrayType . RefType . ClassRefType $ stringType
363+exceptionType :: ClassType
364+exceptionType = ClassType [(Ident "Throwable", [])]
366+runtimeExceptionType :: ClassType
367+runtimeExceptionType = ClassType [(Ident "RuntimeException", [])]
369+comparableType :: ClassType
370+comparableType = ClassType [(Ident "Comparable", [])]
372+mkDecl :: (Name, SDecl) ->
374+mkDecl ((NS n (ns:nss)), decl) =
375+ (\ name body -> MemberDecl $ MemberClassDecl $ ClassDecl [Public, Static] name [] Nothing [] body)
376+ <$> mkIdentifier (UN ns)
377+ <*> mkClassBody [(NS n nss, decl)]
378+mkDecl (_, SFun name params stackSize body) =
379+ (\ name params cont ->
380+ MemberDecl $ MethodDecl [Public, Static]
382+ (Just . RefType $ ClassRefType objectType)
386+ (MethodBody . Just $ Block [ BlockStmt . Return . Just . MethodInv $
387+ PrimaryMethodCall cont [] (Ident "call") []])
389+ <$> mkIdentifier name
390+ <*> mapM mkFormalParam params
391+ <*> mkClosure params stackSize body
394+mkClosure :: [Name] -> Int -> SExp -> Either String Exp
395+mkClosure params stackSize body =
396+ (\ paramArray body ->
400+ (Just $ ClassBody [body])
402+ <$> mkStackInit params stackSize
403+ <*> mkClosureCall body
405+mkFormalParam :: Name -> Either String FormalParam
407+ (\ name -> FormalParam [Final] (RefType . ClassRefType $ objectType) False (VarId name))
408+ <$> mkIdentifier name
410+mkClosureCall :: SExp -> Either String Decl
412+ (\ body -> MemberDecl $ MethodDecl [Public] [] (Just . RefType $ ClassRefType objectType) (Ident "call") [] [] body)
413+ <$> mkMethodBody body
415+mkMethodBody :: SExp -> Either String MethodBody
417+ (\ exp -> MethodBody . Just . Block $ [BlockStmt . Return . Just $ exp])
420+mkStackInit :: [Name] -> Int -> Either String Exp
421+mkStackInit params stackSize =
422+ (\ localVars -> ArrayCreateInit objectArrayType 0 . ArrayInit $
423+ (map (InitExp . ExpName) localVars)
424+ ++ (replicate (stackSize) (InitExp $ Lit Null)))
425+ <$> mapM mkName params
427+mkAnonymousLetBinding :: LVar -> SExp -> SExp -> Either String Exp
428+mkAnonymousLetBinding var oldExp newExp =
429+ (\ bindingMethod newExp ->
430+ MethodInv $ PrimaryMethodCall
431+ ( MethodInv $ PrimaryMethodCall ( InstanceCreation []
434+ (Just $ ClassBody [MemberDecl $ bindingMethod])
445+ <$> mkLetBindingMethod var oldExp
449+mkContextParam :: FormalParam
451+ FormalParam [Final] (objectArrayType) False (VarId (Ident "context"))
453+mkLetBindingMethod :: LVar -> SExp -> Either String MemberDecl
454+mkLetBindingMethod (Loc i) oldExp =
455+ (\ param bindingStack oldCont ->
456+ MethodDecl [Final, Public]
458+ (Just . RefType $ ClassRefType idrisClosureType)
460+ [mkContextParam, param]
462+ (MethodBody . Just . Block $
463+ bindingStack ++ [BlockStmt . Return $ Just oldCont]
466+ <$> mkFormalParam (UN "param")
467+ <*> mkBindingStack True i [UN "param"]
468+ <*> mkBindingClosure oldExp
470+mkBindingClosure :: SExp -> Either String Exp
471+mkBindingClosure oldExp =
475+ [ ExpName $ J.Name [Ident "new_context"] ]
476+ (Just $ ClassBody [oldCall])
478+ <$> mkClosureCall oldExp
481+mkBindingStack :: Bool -> Int -> [Name] -> Either String [BlockStmt]
482+mkBindingStack checked parentStackStart params =
486+ [ VarDecl (VarDeclArray . VarId $ Ident "new_context")
487+ (Just . InitExp $ mkContextCopy checked parentStackStart params)
489+ : ( map (\ (param, pos) ->
490+ BlockStmt . ExpStmt $
491+ Assign (ArrayLhs $ ArrayIndex (ExpName $ J.Name [Ident "new_context"])
492+ (Lit $ Int (toInteger pos)))
494+ (ExpName param)) $ zip paramNames [parentStackStart..]
497+ <$> mapM mkName params
499+mkContextCopy :: Bool -> Int -> [Name] -> Exp
500+mkContextCopy True parentStackStart params =
501+ MethodInv $ PrimaryMethodCall (ExpName $ J.Name [Ident "context"])
505+mkContextCopy False parentStackStart params =
506+ MethodInv $ TypeMethodCall (J.Name [Ident "Arrays"])
509+ [ ExpName $ J.Name [Ident "context"]
511+ $ TypeMethodCall (J.Name [Ident "Math"])
514+ [ FieldAccess $ PrimaryFieldAccess (ExpName $ J.Name [Ident "context"])
516+ , Lit . Int $ toInteger (parentStackStart + length params)
520+reverseNameSpace :: J.Name -> J.Name
521+reverseNameSpace (J.Name ids) =
522+ J.Name ((tail ids) ++ [head ids])
524+mkCase :: Bool -> LVar -> [SAlt] -> Either String Exp
525+mkCase checked var ((SConCase parentStackPos consIndex _ params branchExpression):cases) =
533+mkCase checked var (c@(SConstCase constant branchExpression):cases) =
534+ (\ constant branchExpression alternative ->
535+ Cond ( MethodInv $ PrimaryMethodCall (constant)
538+ [mkVarAccess Nothing var]
543+ <$> mkExp (SConst constant)
544+ <*> mkExp branchExpression
545+ <*> mkCase checked var cases
546+mkCase checked var (SDefaultCase exp:cases) = mkExp exp
547+mkCase checked _ [] = mkExp (SError "Non-exhaustive pattern")
549+mkConsCase :: Bool -> LVar -> Int -> Int -> [Name] -> SExp -> SExp -> Either String Exp
557+ (\ caseBinding alternative->
558+ Cond (BinOp (InstanceOf (mkVarAccess Nothing toDeconstruct)
559+ (ClassRefType idrisObjectType)
563+ ( MethodInv $ PrimaryMethodCall (mkVarAccess (Just idrisObjectType) toDeconstruct)
565+ (Ident "getConstructorId")
569+ (Lit $ Int (toInteger consIndex))
575+ <$> mkCaseBinding checked toDeconstruct parentStackStart params branchExpression
576+ <*> mkExp alternative
579+mkCaseBinding :: Bool -> LVar -> Int -> [Name] -> SExp -> Either String Exp
580+mkCaseBinding checked var parentStackStart params branchExpression =
582+ MethodInv $ PrimaryMethodCall
583+ ( MethodInv $ PrimaryMethodCall (InstanceCreation []
584+ (ClassType [(Ident "Object", [])])
586+ (Just $ ClassBody [MemberDecl $ bindingMethod])
591+ : (mkCaseBindingDeconstruction var params)
598+ <$> mkCaseBindingMethod checked parentStackStart params branchExpression
602+mkCaseBindingMethod :: Bool -> Int -> [Name] -> SExp -> Either String MemberDecl
603+mkCaseBindingMethod checked parentStackStart params branchExpression =
604+ (\ formalParams caseBindingStack branchExpression ->
605+ MethodDecl [Final, Public]
607+ (Just . RefType $ ClassRefType idrisClosureType)
609+ (mkContextParam:formalParams)
611+ (MethodBody . Just . Block $
613+ [BlockStmt . Return $ Just branchExpression]))
614+ <$> mapM mkFormalParam params
615+ <*> mkBindingStack checked parentStackStart params
616+ <*> mkBindingClosure branchExpression
618+mkCaseBindingDeconstruction :: LVar -> [Name] -> [Exp]
619+mkCaseBindingDeconstruction var members =
620+ map (mkProjection var) ([0..(length members - 1)])
622+mkProjection :: LVar -> Int -> Exp
623+mkProjection var memberNr =
624+ ArrayAccess $ ArrayIndex ( MethodInv $ PrimaryMethodCall
625+ (mkVarAccess (Just idrisObjectType) var)
630+ (Lit $ Int (toInteger memberNr))
632+type ClassName = String
634+mkPrimitive :: ClassName -> Literal -> Exp
635+mkPrimitive className value =
636+ MethodInv $ TypeMethodCall (J.Name [Ident className])
641+mkClass :: ClassType -> Exp
643+ ClassLit . Just . RefType .ClassRefType $ classType
645+mkBinOpExp :: ClassType -> Op -> [LVar] -> Exp
646+mkBinOpExp castTo op (var:vars) =
647+ foldl (\ exp -> BinOp exp op . mkVarAccess (Just castTo))
648+ (mkVarAccess (Just castTo) var)
651+mkBinOpExpTrans :: (Exp -> Exp) -> (Exp -> Exp) -> ClassType -> Op -> [LVar] -> Exp
652+mkBinOpExpTrans opTransformation resultTransformation castTo op (var:vars) =
653+ foldl (\ exp -> resultTransformation
654+ . BinOp (opTransformation exp) op
656+ . mkVarAccess (Just castTo))
657+ (mkVarAccess (Just castTo) var)
660+mkBinOpExpConv :: String -> PrimType -> ClassType -> Op -> [LVar] -> Exp
661+mkBinOpExpConv fromMethodName toType fromType@(ClassType [(cls@(Ident _), [])]) op args =
662+ mkBinOpExpTrans (\ exp -> MethodInv $ TypeMethodCall (J.Name [cls])
664+ (Ident fromMethodName)
667+ (\ exp -> MethodInv $ TypeMethodCall (J.Name [cls])
670+ [Cast (PrimType $ toType) exp]
676+mkLogicalBinOpExp :: ClassType -> Op -> [LVar] -> Exp
677+mkLogicalBinOpExp castTo op (var:vars) =
678+ foldl (\ exp -> mkBoolToNumber castTo . BinOp exp op . mkVarAccess (Just castTo))
679+ (mkVarAccess (Just castTo) var)
682+mkMethodOpChain1 :: (Exp -> Exp) -> ClassType -> String -> [LVar] -> Exp
683+mkMethodOpChain1 = mkMethodOpChain id
685+mkMethodOpChain :: (Exp -> Exp) -> (Exp -> Exp) -> ClassType -> String -> [LVar] -> Exp
686+mkMethodOpChain initialTransformation resultTransformation castTo method (arg:args) =
687+ foldl (\ exp arg' ->
688+ resultTransformation . MethodInv $ PrimaryMethodCall exp
691+ [mkVarAccess (Just $ castTo) arg']
693+ (initialTransformation (mkVarAccess (Just $ castTo) arg))
696+mkBoolToNumber :: ClassType -> Exp -> Exp
697+mkBoolToNumber (ClassType [(Ident name, [])]) boolExp =
698+ Cond boolExp (mkPrimitive name (Int 1)) (mkPrimitive name (Int 0))
700+mkZeroExt :: String -> Int -> ClassType -> ClassType -> LVar -> Exp
701+mkZeroExt toMethod bits fromType toType@(ClassType [(toTypeName, [])]) var =
702+ MethodInv $ TypeMethodCall (J.Name [toTypeName])
705+ [ Cond ( BinOp (mkVarAccess (Just $ fromType) var)
709+ ( BinOp (Lit $ Int (2^bits))
711+ (mkSignedExt' toMethod fromType var)
713+ (mkSignedExt' toMethod fromType var)
716+mkSignedExt :: String -> ClassType -> ClassType -> LVar -> Exp
717+mkSignedExt toMethod fromType (ClassType [(toTypeName, [])]) var =
718+ MethodInv $ TypeMethodCall (J.Name [toTypeName])
721+ [ mkSignedExt' toMethod fromType var ]
723+mkSignedExt' :: String -> ClassType -> LVar -> Exp
724+mkSignedExt' toMethod fromType var =
725+ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ fromType) var)
737+mkPartialOrder :: SPartialOrder -> Exp -> Exp
738+mkPartialOrder SLt x = (BinOp (Lit $ Int (-1)) Equal x)
739+mkPartialOrder SLe x =
740+ BinOp (BinOp (Lit $ Int (-1)) Equal x)
742+ (BinOp (Lit $ Int 0) Equal x)
743+mkPartialOrder SEq x = BinOp (Lit $ Int 0) Equal x
744+mkPartialOrder SGe x =
745+ BinOp (BinOp (Lit $ Int 1) Equal x)
747+ (BinOp (Lit $ Int 0) Equal x)
748+mkPartialOrder SGt x = (BinOp (Lit $ Int 1) Equal x)
750+mkVarAccess :: Maybe ClassType -> LVar -> Exp
751+mkVarAccess Nothing (Loc i) =
752+ ArrayAccess $ ArrayIndex contextArray (Lit $ Int (toInteger i))
753+mkVarAccess (Just castTo) var =
754+ Cast (RefType . ClassRefType $ castTo) (mkVarAccess Nothing var)
756+mkPrimitiveCast :: ClassType -> ClassType -> LVar -> Exp
757+mkPrimitiveCast fromType (ClassType [(toType, [])]) var =
758+ MethodInv $ TypeMethodCall (J.Name [toType])
761+ [mkVarAccess (Just fromType) var]
763+mkToString :: ClassType -> LVar -> Exp
764+mkToString castTo var =
765+ MethodInv $ PrimaryMethodCall (mkVarAccess (Just castTo) var)
770+data Std = In | Out | Err
772+instance Show Std where
777+mkSystemStd :: Std -> Exp
778+mkSystemStd std = FieldAccess $ PrimaryFieldAccess (ExpName $ J.Name [Ident "System"]) (Ident $ show std)
780+mkSystemOutPrint :: Exp -> Exp
781+mkSystemOutPrint value =
782+ MethodInv $ PrimaryMethodCall (mkSystemStd Out)
787+mkMathFun :: String -> LVar -> Exp
788+mkMathFun funName var =
789+ MethodInv $ TypeMethodCall (J.Name [Ident "Double"])
792+ [ MethodInv $ TypeMethodCall (J.Name [Ident "Math"])
795+ [mkVarAccess (Just doubleType) var]
798+mkStringAtIndex :: LVar -> Exp -> Exp
799+mkStringAtIndex var indexExp =
800+ MethodInv $ TypeMethodCall (J.Name [Ident "Integer"])
803+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) var)
809+mkForeignType :: FType -> Maybe ClassType
810+mkForeignType FInt = return $ integerType
811+mkForeignType FChar = return $ integerType
812+mkForeignType FString = return $ stringType
813+mkForeignType FPtr = return $ objectType
814+mkForeignType FDouble = return $ doubleType
815+mkForeignType FAny = return $ objectType
816+mkForeignType FUnit = Nothing
818+mkForeignVarAccess :: FType -> LVar -> Exp
819+mkForeignVarAccess FInt var =
820+ MethodInv $ PrimaryMethodCall (mkVarAccess (Just integerType) var)
824+mkForeignVarAccess FChar var = Cast (PrimType CharT) (mkForeignVarAccess FInt var)
825+mkForeignVarAccess FDouble var =
826+ MethodInv $ PrimaryMethodCall (mkVarAccess (Just doubleType) var)
828+ (Ident "doubleValue")
830+mkForeignVarAccess otherType var = mkVarAccess (mkForeignType otherType) var
832+mkFromForeignType :: FType -> Exp -> Exp
833+mkFromForeignType FInt from =
834+ MethodInv $ TypeMethodCall (J.Name [Ident "Integer"])
838+mkFromForeignType FChar from = mkFromForeignType FInt from
839+mkFromForeignType FDouble from =
840+ MethodInv $ TypeMethodCall (J.Name [Ident "Double"])
844+mkFromForeignType _ from = from
846+mkForeignInvoke :: FType -> String -> [(FType, LVar)] -> Either String Exp
847+mkForeignInvoke fType method args =
848+ (\ foreignInvokeMeth ->
849+ MethodInv $ PrimaryMethodCall (InstanceCreation []
852+ (Just $ ClassBody [ MemberDecl $ foreignInvokeMeth ])
855+ (Ident "foreignInvoke")
858+ <$> mkForeignInvokeMethod fType method args
861+mkForeignInvokeMethod :: FType -> String -> [(FType, LVar)] -> Either String MemberDecl
862+mkForeignInvokeMethod fType method args =
864+ MethodDecl [Public, Final]
866+ (Just . RefType $ ClassRefType objectType)
867+ (Ident "foreignInvoke")
870+ (MethodBody . Just $ Block [ BlockStmt $
872+ [ Catch (FormalParam []
873+ (RefType $ ClassRefType exceptionType)
875+ (VarId $ Ident "ex")
877+ (Block [ BlockStmt . Throw $
879+ runtimeExceptionType
880+ [ExpName $ J.Name [Ident "ex"]]
889+ <$> mkForeignInvokeTryBlock fType method args
892+mkForeignInvokeTryBlock :: FType -> String -> [(FType, LVar)] -> Either String Block
893+mkForeignInvokeTryBlock FUnit method args =
894+ (\ method -> Block [ BlockStmt . ExpStmt . MethodInv $
896+ (map (uncurry mkForeignVarAccess) args)
897+ , BlockStmt $ Return (Just $ Lit Null)
900+ <$> ( left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
901+ (parser name method)
903+mkForeignInvokeTryBlock fType method args =
904+ (\ method -> Block [ BlockStmt . Return
906+ . mkFromForeignType fType
909+ (map (uncurry mkForeignVarAccess) args)
912+ <$> ( left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
913+ (parser name method)
916+mkMethodClosure :: Name -> [LVar] -> Either String Exp
917+mkMethodClosure name args =
921+ [ (ExpName $ J.Name [Ident "context"]) ]
924+ MethodDecl [Public, Final]
926+ (Just . RefType $ ClassRefType objectType)
930+ ( MethodBody . Just $
931+ Block [ BlockStmt . Return . Just . MethodInv $
932+ MethodCall (reverseNameSpace name)
940+ <*> mapM (mkExp . SV) args
942+mkThread :: LVar -> Either String Exp
944+ (\ eval -> MethodInv $
945+ PrimaryMethodCall (InstanceCreation []
946+ (ClassType [(Ident "Thread", [])])
949+ [ MemberDecl $ MethodDecl
952+ (Just . RefType $ ClassRefType objectType)
956+ ( MethodBody . Just $ Block
957+ [ BlockStmt . ExpStmt . MethodInv $ MethodCall
958+ (J.Name [Ident "start"])
960+ , BlockStmt . Return . Just $ This
970+ <$> mkThreadBinding arg
972+mkThreadBinding :: LVar -> Either String Exp
973+mkThreadBinding var =
975+ MethodInv $ PrimaryMethodCall ( InstanceCreation []
978+ (Just $ ClassBody [MemberDecl $ bindingMethod])
982+ [ mkVarAccess Nothing var ]
984+ <$> mkThreadBindingMethod
987+mkThreadBindingMethod :: Either String MemberDecl
988+mkThreadBindingMethod =
990+ MethodDecl [Final, Public]
992+ (Just . RefType $ ClassRefType idrisClosureType)
994+ [ FormalParam [Final] (RefType . ClassRefType $ objectType) False (VarId $ Ident "param") ]
996+ (MethodBody . Just $ Block
997+ [ mkThreadBindingStack
998+ , BlockStmt . Return $ Just compute
1002+ <$> mkBindingClosure (SUpdate (Loc 0) (SApp False (MN 0 "EVAL") [Loc 0]))
1005+mkThreadBindingStack :: BlockStmt
1006+mkThreadBindingStack =
1009+ [ VarDecl (VarDeclArray . VarId $ Ident "new_context")
1010+ (Just . InitArray $ ArrayInit [InitExp . ExpName $ J.Name [Ident "param"]])
1014+mkExp :: SExp -> Either String Exp
1016+ return $ mkVarAccess Nothing var
1017+mkExp (SApp False name args) =
1019+ MethodInv $ PrimaryMethodCall ( InstanceCreation []
1020+ idrisTailCallClosureType
1028+ <$> mkMethodClosure name args
1029+mkExp (SApp True name args) =
1031+ ( InstanceCreation []
1032+ idrisTailCallClosureType
1037+ <$> mkMethodClosure name args
1038+mkExp (SLet var new old) =
1039+ mkAnonymousLetBinding var old new
1040+mkExp (SUpdate (Loc i) exp) =
1041+ (\ rhs -> Assign (ArrayLhs $ ArrayIndex (contextArray) (Lit $ Int (toInteger i)))
1045+mkExp (SCon conId name args) =
1046+ (\ args -> InstanceCreation []
1048+ ((Lit $ Int (toInteger conId)):args)
1050+ <$> mapM (mkExp .SV) args
1051+mkExp (SCase var alts) = mkCase False var alts
1052+mkExp (SChkCase var alts) = mkCase True var alts
1053+mkExp (SProj var i) = return $ mkProjection var i
1054+mkExp (SConst (I x)) = return $ mkPrimitive "Integer" (Int (toInteger x))
1055+mkExp (SConst (BI x)) =
1056+ return $ InstanceCreation []
1057+ (ClassType [(Ident "BigInteger", [])])
1058+ [Lit $ String (show x)]
1060+mkExp (SConst (Fl x)) = return $ mkPrimitive "Double" (Double x)
1061+mkExp (SConst (Ch x)) = return $ mkPrimitive "Integer" (Char x)
1062+mkExp (SConst (Str x)) = return $ Lit $ String x
1063+mkExp (SConst IType) = return $ mkClass integerType
1064+mkExp (SConst BIType) = return $ mkClass bigIntegerType
1065+mkExp (SConst FlType) = return $ mkClass doubleType
1066+mkExp (SConst ChType) = return $ mkClass charType
1067+mkExp (SConst StrType) = return $ mkClass stringType
1068+mkExp (SConst (B8 x)) = return $ mkPrimitive "Byte" (Int (toInteger x))
1069+mkExp (SConst (B16 x)) = return $ mkPrimitive "Short" (Int (toInteger x))
1070+mkExp (SConst (B32 x)) = return $ mkPrimitive "Integer" (Int (toInteger x))
1071+mkExp (SConst (B64 x)) = return $ mkPrimitive "Long" (Int (toInteger x))
1072+mkExp (SConst (B8Type))= return $ mkClass byteType
1073+mkExp (SConst (B16Type)) = return $ mkClass shortType
1074+mkExp (SConst (B32Type)) = return $ mkClass integerType
1075+mkExp (SConst (B64Type)) = return $ mkClass longType
1076+mkExp (SConst (PtrType)) = return $ mkClass objectType
1077+mkExp (SConst (VoidType)) = return $ mkClass voidType
1078+mkExp (SConst (Forgot)) = return $ mkClass objectType
1079+mkExp (SForeign _ fType meth args) = mkForeignInvoke fType meth args
1080+mkExp (SOp LPlus args) = return $ mkBinOpExp integerType Add args
1081+mkExp (SOp LMinus args) = return $ mkBinOpExp integerType Sub args
1082+mkExp (SOp LTimes args) = return $ mkBinOpExp integerType Mult args
1083+mkExp (SOp LDiv args) = return $ mkBinOpExp integerType Div args
1084+mkExp (SOp LMod args) = return $ mkBinOpExp integerType Rem args
1085+mkExp (SOp LAnd args) = return $ mkBinOpExp integerType And args
1086+mkExp (SOp LOr args) = return $ mkBinOpExp integerType Or args
1087+mkExp (SOp LXOr args) = return $ mkBinOpExp integerType Xor args
1088+mkExp (SOp LCompl [var]) =
1089+ return $ PreBitCompl (mkVarAccess (Just $ integerType) var)
1090+mkExp (SOp LSHL args) = return $ mkBinOpExp integerType LShift args
1091+mkExp (SOp LSHR args) = return $ mkBinOpExp integerType RShift args
1092+mkExp (SOp LEq args) =
1093+ return $ mkMethodOpChain1 (mkBoolToNumber integerType) objectType "equals" args
1094+mkExp (SOp LLt args) = return $ mkLogicalBinOpExp integerType LThan args
1095+mkExp (SOp LLe args) = return $ mkLogicalBinOpExp integerType LThanE args
1096+mkExp (SOp LGt args) = return $ mkLogicalBinOpExp integerType GThan args
1097+mkExp (SOp LGe args) = return $ mkLogicalBinOpExp integerType GThanE args
1098+mkExp (SOp LFPlus args) = return $ mkBinOpExp doubleType Add args
1099+mkExp (SOp LFMinus args) = return $ mkBinOpExp doubleType Sub args
1100+mkExp (SOp LFTimes args) = return $ mkBinOpExp doubleType Mult args
1101+mkExp (SOp LFDiv args) = return $ mkBinOpExp doubleType Div args
1102+mkExp (SOp LFEq args) =
1103+ return $ mkMethodOpChain1 (mkBoolToNumber doubleType) doubleType "equals" args
1104+mkExp (SOp LFLt args) = return $ mkLogicalBinOpExp integerType LThan args
1105+mkExp (SOp LFLe args) = return $ mkLogicalBinOpExp integerType LThanE args
1106+mkExp (SOp LFGt args) = return $ mkLogicalBinOpExp integerType GThan args
1107+mkExp (SOp LFGe args) = return $ mkLogicalBinOpExp integerType GThanE args
1108+mkExp (SOp LBPlus args) = return $ mkMethodOpChain1 id bigIntegerType "add" args
1109+mkExp (SOp LBMinus args) = return $ mkMethodOpChain1 id bigIntegerType "subtract" args
1110+mkExp (SOp LBTimes args) = return $ mkMethodOpChain1 id bigIntegerType "multiply" args
1111+mkExp (SOp LBDiv args) = return $ mkMethodOpChain1 id bigIntegerType "divide" args
1112+mkExp (SOp LBMod args) = return $ mkMethodOpChain1 id bigIntegerType "mod" args
1113+mkExp (SOp LBEq args) =
1114+ return $ mkMethodOpChain1 (mkBoolToNumber bigIntegerType) bigIntegerType "equals" args
1115+mkExp (SOp LBLt args) =
1116+ return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1117+ . mkPartialOrder SLt
1122+mkExp (SOp LBLe args) =
1123+ return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1124+ . mkPartialOrder SLe
1129+mkExp (SOp LBGt args) =
1130+ return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1131+ . mkPartialOrder SGt
1136+mkExp (SOp LBGe args) =
1137+ return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1138+ . mkPartialOrder SGe
1143+mkExp (SOp LStrConcat args) =
1144+ return $ mkMethodOpChain (\ exp ->
1145+ InstanceCreation []
1146+ (ClassType [(Ident "StringBuilder", [])])
1150+ (\ exp -> MethodInv $ PrimaryMethodCall exp [] (Ident "toString") [])
1154+mkExp (SOp LStrLt args@[_, _]) =
1155+ return $ mkMethodOpChain1 ( mkBoolToNumber integerType
1156+ . mkPartialOrder SLt
1161+mkExp (SOp LStrEq args@[_, _]) =
1162+ return $ mkMethodOpChain1 ( mkBoolToNumber integerType)
1166+mkExp (SOp LStrLen [arg]) =
1167+ return . MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) arg) [] (Ident "length") []
1168+mkExp (SOp LIntFloat [arg]) =
1169+ return $ mkPrimitiveCast integerType doubleType arg
1170+mkExp (SOp LFloatInt [arg]) =
1171+ return $ mkPrimitiveCast doubleType integerType arg
1172+mkExp (SOp LIntStr [arg]) =
1173+ return $ mkToString integerType arg
1174+mkExp (SOp LStrInt [arg]) =
1175+ return $ mkPrimitiveCast stringType integerType arg
1176+mkExp (SOp LFloatStr [arg]) =
1177+ return $ mkToString doubleType arg
1178+mkExp (SOp LStrFloat [arg]) =
1179+ return $ mkPrimitiveCast doubleType stringType arg
1180+mkExp (SOp LIntBig [arg]) =
1181+ return $ mkPrimitiveCast integerType bigIntegerType arg
1182+mkExp (SOp LBigInt [arg]) =
1183+ return $ mkPrimitiveCast bigIntegerType integerType arg
1184+mkExp (SOp LStrBig [arg]) =
1185+ return $ InstanceCreation [] bigIntegerType [mkVarAccess (Just stringType) arg] Nothing
1186+mkExp (SOp LBigStr [arg]) =
1187+ return $ mkToString bigIntegerType arg
1188+mkExp (SOp LChInt [arg]) =
1189+ return $ mkVarAccess (Just integerType) arg
1190+mkExp (SOp LIntCh [arg]) =
1191+ return $ mkVarAccess (Just integerType) arg
1192+mkExp (SOp LPrintNum [arg]) =
1193+ return $ mkSystemOutPrint (mkVarAccess Nothing arg)
1194+mkExp (SOp LPrintStr [arg]) =
1195+ return $ mkSystemOutPrint (mkVarAccess (Just stringType) arg)
1196+mkExp (SOp LReadStr [arg]) = mkExp (SForeign LANG_C FString "idris_readStr" [(FPtr, arg)])
1197+mkExp (SOp LB8Lt args) = return $ mkLogicalBinOpExp byteType LThan args
1198+mkExp (SOp LB8Lte args) = return $ mkLogicalBinOpExp byteType LThanE args
1199+mkExp (SOp LB8Eq args) =
1200+ return $ mkMethodOpChain1 (mkBoolToNumber byteType) byteType "equals" args
1201+mkExp (SOp LB8Gt args) = return $ mkLogicalBinOpExp byteType GThan args
1202+mkExp (SOp LB8Gte args) = return $ mkLogicalBinOpExp byteType GThanE args
1203+mkExp (SOp LB8Plus args) = return $ mkBinOpExp byteType Add args
1204+mkExp (SOp LB8Minus args) = return $ mkBinOpExp byteType Sub args
1205+mkExp (SOp LB8Times args) = return $ mkBinOpExp byteType Mult args
1206+mkExp (SOp LB8UDiv args) = return $ mkBinOpExpConv "shortValue" ShortT byteType Div args
1207+mkExp (SOp LB8SDiv args) = return $ mkBinOpExp byteType Div args
1208+mkExp (SOp LB8URem args) = return $ mkBinOpExpConv "shortValue" ShortT byteType Rem args
1209+mkExp (SOp LB8SRem args) = return $ mkBinOpExp byteType Rem args
1210+mkExp (SOp LB8Shl args) = return $ mkBinOpExp byteType LShift args
1211+mkExp (SOp LB8LShr args) = return $ mkBinOpExp byteType RRShift args
1212+mkExp (SOp LB8AShr args) = return $ mkBinOpExp byteType RShift args
1213+mkExp (SOp LB8And args) = return $ mkBinOpExp byteType And args
1214+mkExp (SOp LB8Or args) = return $ mkBinOpExp byteType Or args
1215+mkExp (SOp LB8Xor args) = return $ mkBinOpExp byteType Xor args
1216+mkExp (SOp LB8Compl [var]) =
1217+ return $ PreBitCompl (mkVarAccess (Just $ byteType) var)
1218+mkExp (SOp LB8Z16 [var]) = return $ mkZeroExt "shortValue" 8 byteType shortType var
1219+mkExp (SOp LB8Z32 [var]) = return $ mkZeroExt "intValue" 8 byteType integerType var
1220+mkExp (SOp LB8Z64 [var]) = return $ mkZeroExt "longValue" 8 byteType longType var
1221+mkExp (SOp LB8S16 [var]) = return $ mkSignedExt "shortValue" byteType shortType var
1222+mkExp (SOp LB8S32 [var]) = return $ mkSignedExt "intValue" byteType integerType var
1223+mkExp (SOp LB8S64 [var]) = return $ mkSignedExt "longValue" byteType longType var
1224+mkExp (SOp LB16Lt args) = return $ mkLogicalBinOpExp shortType LThan args
1225+mkExp (SOp LB16Lte args) = return $ mkLogicalBinOpExp shortType LThanE args
1226+mkExp (SOp LB16Eq args) =
1227+ return $ mkMethodOpChain1 (mkBoolToNumber shortType) shortType "equals" args
1228+mkExp (SOp LB16Gt args) = return $ mkLogicalBinOpExp shortType GThan args
1229+mkExp (SOp LB16Gte args) = return $ mkLogicalBinOpExp shortType GThanE args
1230+mkExp (SOp LB16Plus args) = return $ mkBinOpExp shortType Add args
1231+mkExp (SOp LB16Minus args) = return $ mkBinOpExp shortType Sub args
1232+mkExp (SOp LB16Times args) = return $ mkBinOpExp shortType Mult args
1233+mkExp (SOp LB16UDiv args) = return $ mkBinOpExpConv "intValue" IntT shortType Div args
1234+mkExp (SOp LB16SDiv args) = return $ mkBinOpExp shortType Div args
1235+mkExp (SOp LB16URem args) = return $ mkBinOpExpConv "intValue" IntT shortType Rem args
1236+mkExp (SOp LB16SRem args) = return $ mkBinOpExp shortType Rem args
1237+mkExp (SOp LB16Shl args) = return $ mkBinOpExp shortType LShift args
1238+mkExp (SOp LB16LShr args) = return $ mkBinOpExp shortType RRShift args
1239+mkExp (SOp LB16AShr args) = return $ mkBinOpExp shortType RShift args
1240+mkExp (SOp LB16And args) = return $ mkBinOpExp shortType And args
1241+mkExp (SOp LB16Or args) = return $ mkBinOpExp shortType Or args
1242+mkExp (SOp LB16Xor args) = return $ mkBinOpExp shortType Xor args
1243+mkExp (SOp LB16Compl [var]) =
1244+ return $ PreBitCompl (mkVarAccess (Just $ shortType) var)
1245+mkExp (SOp LB16Z32 [var]) = return $ mkZeroExt "intValue" 16 shortType integerType var
1246+mkExp (SOp LB16Z64 [var]) = return $ mkZeroExt "longValue" 16 shortType longType var
1247+mkExp (SOp LB16S32 [var]) = return $ mkSignedExt "intValue" shortType integerType var
1248+mkExp (SOp LB16S64 [var]) = return $ mkSignedExt "longValue" shortType longType var
1249+mkExp (SOp LB16T8 [var]) =
1250+ return . MethodInv $
1251+ TypeMethodCall (J.Name [Ident "Byte"])
1254+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ shortType) var) [] (Ident "byteValue") [] ]
1255+mkExp (SOp LB32Lt args) = return $ mkLogicalBinOpExp integerType LThan args
1256+mkExp (SOp LB32Lte args) = return $ mkLogicalBinOpExp integerType LThanE args
1257+mkExp (SOp LB32Eq args) =
1258+ return $ mkMethodOpChain1 (mkBoolToNumber integerType) integerType "equals" args
1259+mkExp (SOp LB32Gt args) = return $ mkLogicalBinOpExp integerType GThan args
1260+mkExp (SOp LB32Gte args) = return $ mkLogicalBinOpExp integerType GThanE args
1261+mkExp (SOp LB32Plus args) = return $ mkBinOpExp integerType Add args
1262+mkExp (SOp LB32Minus args) = return $ mkBinOpExp integerType Sub args
1263+mkExp (SOp LB32Times args) = return $ mkBinOpExp integerType Mult args
1264+mkExp (SOp LB32UDiv args) = return $ mkBinOpExpConv "longValue" LongT integerType Div args
1265+mkExp (SOp LB32SDiv args) = return $ mkBinOpExp integerType Div args
1266+mkExp (SOp LB32URem args) = return $ mkBinOpExpConv "longValue" LongT integerType Rem args
1267+mkExp (SOp LB32SRem args) = return $ mkBinOpExp integerType Rem args
1268+mkExp (SOp LB32Shl args) = return $ mkBinOpExp integerType LShift args
1269+mkExp (SOp LB32LShr args) = return $ mkBinOpExp integerType RRShift args
1270+mkExp (SOp LB32AShr args) = return $ mkBinOpExp integerType RShift args
1271+mkExp (SOp LB32And args) = return $ mkBinOpExp integerType And args
1272+mkExp (SOp LB32Or args) = return $ mkBinOpExp integerType Or args
1273+mkExp (SOp LB32Xor args) = return $ mkBinOpExp integerType Xor args
1274+mkExp (SOp LB32Compl [var]) =
1275+ return $ PreBitCompl (mkVarAccess (Just $ integerType) var)
1276+mkExp (SOp LB32Z64 [var]) = return $ mkZeroExt "longValue" 32 integerType longType var
1277+mkExp (SOp LB32S64 [var]) = return $ mkSignedExt "longValue" integerType longType var
1278+mkExp (SOp LB32T8 [var]) =
1279+ return . MethodInv $
1280+ TypeMethodCall (J.Name [Ident "Byte"])
1283+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ integerType) var) [] (Ident "byteValue") [] ]
1284+mkExp (SOp LB32T16 [var]) =
1285+ return . MethodInv $
1286+ TypeMethodCall (J.Name [Ident "Short"])
1289+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ integerType) var) [] (Ident "shortValue") [] ]
1290+mkExp (SOp LB64Lt args) = return $ mkLogicalBinOpExp longType LThan args
1291+mkExp (SOp LB64Lte args) = return $ mkLogicalBinOpExp longType LThanE args
1292+mkExp (SOp LB64Eq args) =
1293+ return $ mkMethodOpChain1 (mkBoolToNumber longType) longType "equals" args
1294+mkExp (SOp LB64Gt args) = return $ mkLogicalBinOpExp longType GThan args
1295+mkExp (SOp LB64Gte args) = return $ mkLogicalBinOpExp longType GThanE args
1296+mkExp (SOp LB64Plus args) = return $ mkBinOpExp longType Add args
1297+mkExp (SOp LB64Minus args) = return $ mkBinOpExp longType Sub args
1298+mkExp (SOp LB64Times args) = return $ mkBinOpExp longType Mult args
1299+mkExp (SOp LB64UDiv (arg:args)) =
1300+ return $ foldl (\ exp arg ->
1301+ MethodInv $ PrimaryMethodCall
1302+ ( MethodInv $ PrimaryMethodCall
1303+ ( MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
1310+ [ MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
1313+ [ (mkVarAccess (Just longType) arg) ]
1317+ (Ident "longValue")
1320+ (mkVarAccess (Just longType) arg)
1322+mkExp (SOp LB64SDiv args) = return $ mkBinOpExp longType Div args
1323+mkExp (SOp LB64URem (arg:args)) =
1324+ return $ foldl (\ exp arg ->
1325+ MethodInv $ PrimaryMethodCall
1326+ ( MethodInv $ PrimaryMethodCall
1327+ ( MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
1333+ (Ident "remainder")
1334+ [ MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
1337+ [ (mkVarAccess (Just longType) arg) ]
1341+ (Ident "longValue")
1344+ (mkVarAccess (Just longType) arg)
1346+mkExp (SOp LB64SRem args) = return $ mkBinOpExp longType Rem args
1347+mkExp (SOp LB64Shl args) = return $ mkBinOpExp longType LShift args
1348+mkExp (SOp LB64LShr args) = return $ mkBinOpExp longType RRShift args
1349+mkExp (SOp LB64AShr args) = return $ mkBinOpExp longType RShift args
1350+mkExp (SOp LB64And args) = return $ mkBinOpExp longType And args
1351+mkExp (SOp LB64Or args) = return $ mkBinOpExp longType Or args
1352+mkExp (SOp LB64Xor args) = return $ mkBinOpExp longType Xor args
1353+mkExp (SOp LB64Compl [var]) =
1354+ return $ PreBitCompl (mkVarAccess (Just $ longType) var)
1355+mkExp (SOp LB64T8 [var]) =
1356+ return . MethodInv $
1357+ TypeMethodCall (J.Name [Ident "Byte"])
1360+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "byteValue") [] ]
1361+mkExp (SOp LB64T16 [var]) =
1362+ return . MethodInv $
1363+ TypeMethodCall (J.Name [Ident "Short"])
1366+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "shortValue") [] ]
1367+mkExp (SOp LB64T32 [var]) =
1368+ return . MethodInv $
1369+ TypeMethodCall (J.Name [Ident "Integer"])
1372+ [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "intValue") [] ]
1373+mkExp (SOp LIntB8 [arg]) = mkExp (SOp LB32T8 [arg])
1374+mkExp (SOp LIntB16 [arg]) = mkExp (SOp LB32T16 [arg])
1375+mkExp (SOp LIntB32 [arg]) = mkExp (SV arg)
1376+mkExp (SOp LIntB64 [arg]) = mkExp (SOp LB32S64 [arg])
1377+mkExp (SOp LB32Int [arg]) = mkExp (SV arg)
1378+mkExp (SOp LFExp [arg]) = return $ mkMathFun "exp" arg
1379+mkExp (SOp LFLog [arg]) = return $ mkMathFun "log" arg
1380+mkExp (SOp LFSin [arg]) = return $ mkMathFun "sin" arg
1381+mkExp (SOp LFCos [arg]) = return $ mkMathFun "cos" arg
1382+mkExp (SOp LFTan [arg]) = return $ mkMathFun "tan" arg
1383+mkExp (SOp LFASin [arg]) = return $ mkMathFun "asin" arg
1384+mkExp (SOp LFACos [arg]) = return $ mkMathFun "acos" arg
1385+mkExp (SOp LFATan [arg]) = return $ mkMathFun "atan" arg
1386+mkExp (SOp LFSqrt [arg]) = return $ mkMathFun "sqrt" arg
1387+mkExp (SOp LFFloor [arg]) = return $ mkMathFun "floor" arg
1388+mkExp (SOp LFCeil [arg]) = return $ mkMathFun "ceil" arg
1389+mkExp (SOp LStrHead [arg]) = return $ mkStringAtIndex arg (Lit $ Int 0)
1390+mkExp (SOp LStrTail [arg]) =
1391+ return . MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) arg)
1393+ (Ident "substring")
1395+mkExp (SOp LStrCons [c, cs]) =
1396+ return . MethodInv $
1397+ PrimaryMethodCall ( MethodInv $ PrimaryMethodCall (InstanceCreation []
1398+ (ClassType [(Ident "StringBuilder", [])])
1399+ [mkVarAccess (Just stringType) cs]
1405+ Cast (PrimType CharT)
1406+ (MethodInv $ PrimaryMethodCall
1407+ (mkVarAccess (Just integerType) c)
1417+mkExp (SOp LStrIndex [str, i]) = return $ mkStringAtIndex str (mkVarAccess (Just integerType) i)
1418+mkExp (SOp LStrRev [str]) =
1419+ return . MethodInv $
1420+ PrimaryMethodCall ( MethodInv $ PrimaryMethodCall (InstanceCreation []
1421+ (ClassType [(Ident "StringBuffer", [])])
1422+ [mkVarAccess (Just stringType) str]
1432+mkExp (SOp LStdIn []) = return $ mkSystemStd In
1433+mkExp (SOp LStdOut []) = return $ mkSystemStd Out
1434+mkExp (SOp LStdErr []) = return $ mkSystemStd Err
1435+mkExp (SOp LFork [arg]) = mkThread arg
1436+mkExp (SOp LPar [arg]) = mkExp (SV arg)
1437+mkExp (SOp LVMPtr []) =
1438+ return . MethodInv $ TypeMethodCall (J.Name [Ident "Thread"]) [] (Ident "currentThread") []
1439+mkExp (SOp LNoOp args) = mkExp . SV $ last args
1440+mkExp (SNothing) = return $ Lit Null
1441+mkExp (SError err) =
1442+ return . MethodInv $
1443+ PrimaryMethodCall (InstanceCreation []
1444+ runtimeExceptionType
1447+ [ MemberDecl $ MethodDecl [Public, Final]
1449+ (Just . RefType $ ClassRefType objectType)
1450+ (Ident "throwSelf")
1453+ ( MethodBody . Just $
1454+ Block [ BlockStmt (Throw This) ]
1460+ (Ident "throwSelf")
1462+mkExp other = error (show other)