@@ -11,8 +11,13 @@ import Util.System
1111import Control.Applicative
1212import Control.Arrow
1313import Control.Monad
14+import qualified Control.Monad.Trans as T
15+import Control.Monad.Trans.State
1416import Data.Char
1517import Data.Maybe (fromJust)
18+import Data.List (isPrefixOf, intercalate)
19+import qualified Data.Text as T
20+import qualified Data.Text.IO as TIO
1621import Language.Java.Parser
1722import Language.Java.Pretty
1823import Language.Java.Syntax hiding (Name)
@@ -23,19 +28,83 @@ import System.FilePath
2328import System.IO
2429import System.Process
2530
26−codegenJava :: [(Name, SDecl)] ->
31+data CodeGenerationEnv = CodeGenerationEnv { globalVariablePositions :: [(Name, Integer)] }
32+
33+type CodeGeneration = StateT (CodeGenerationEnv) (Either String)
34+
35+codegenJava :: [(Name, SExp)] -> -- initialization of globals
36+ [(Name, SDecl)] ->
2737 FilePath -> -- output file name
2838 OutputType ->
2939 IO ()
30−codegenJava defs out exec =
31− either (error)
32− (writeFile out . flatIndent . prettyPrint)
33− (mkCompilationUnit defs out)
34−
35−mkCompilationUnit :: [(Name, SDecl)] ->
36− FilePath ->
37− Either String CompilationUnit
38−mkCompilationUnit defs out =
40+codegenJava globalInit defs out exec = do
41+ withTempdir (takeBaseName out) $ \ tmpDir -> do
42+ let srcdir = tmpDir </> "src" </> "main" </> "java"
43+ createDirectoryIfMissing True srcdir
44+ let (Ident clsName) =
45+ either error id (evalStateT (mkClassName out) (mkCodeGenEnv globalInit))
46+ let outjava = srcdir </> clsName <.> "java"
47+ let jout = either error
48+ (flatIndent . prettyPrint)
49+ (evalStateT (mkCompilationUnit globalInit defs out) (mkCodeGenEnv globalInit))
50+ writeFile outjava jout
51+ if (exec == Raw)
52+ then copyFile outjava (takeDirectory out </> clsName <.> "java")
53+ else do
54+ execPom <- getExecutablePom
55+ execPomTemplate <- TIO.readFile execPom
56+ let execPom = T.replace (T.pack "$MAIN-CLASS$")
57+ (T.pack clsName)
58+ (T.replace (T.pack "$ARTIFACT-NAME$")
59+ (T.pack $ takeBaseName out)
60+ execPomTemplate)
61+ TIO.writeFile (tmpDir </> "pom.xml") execPom
62+ mvnCmd <- getMvn
63+ let args = ["-f", (tmpDir </> "pom.xml")]
64+ (exit, _, err) <- readProcessWithExitCode mvnCmd (args ++ ["compile"]) ""
65+ when (exit /= ExitSuccess) $ error ("FAILURE: " ++ mvnCmd ++ " compile\n" ++ err)
66+ if (exec == Object)
67+ then do
68+ classFiles <-
69+ map (\ clsFile -> tmpDir </> "target" </> "classes" </> clsFile)
70+ . filter ((".class" ==) . takeExtension)
71+ <$> getDirectoryContents (tmpDir </> "target" </> "classes")
72+ mapM_ (\ clsFile -> copyFile clsFile (takeDirectory out </> takeFileName clsFile))
73+ classFiles
74+ else do
75+ (exit, _, err) <- readProcessWithExitCode mvnCmd (args ++ ["package"]) ""
76+ when (exit /= ExitSuccess) (error ("FAILURE: " ++ mvnCmd ++ " package\n" ++ err))
77+ copyFile (tmpDir </> "target" </> (takeBaseName out) <.> "jar") out
78+ handle <- openBinaryFile out ReadMode
79+ contents <- TIO.hGetContents handle
80+ hClose handle
81+ handle <- openBinaryFile out WriteMode
82+ TIO.hPutStr handle (T.append (T.pack jarHeader) contents)
83+ hFlush handle
84+ hClose handle
85+ perms <- getPermissions out
86+ setPermissions out (setOwnerExecutable True perms)
87+ readProcess mvnCmd (args ++ ["clean"]) ""
88+ removeFile (tmpDir </> "pom.xml")
89+
90+jarHeader :: String
91+jarHeader =
92+ "#!/bin/sh\n"
93+ ++ "MYSELF=`which \"$0\" 2>/dev/null`\n"
94+ ++ "[ $? -gt 0 -a -f \"$0\" ] && MYSELF=\"./$0\"\n"
95+ ++ "java=java\n"
96+ ++ "if test -n \"$JAVA_HOME\"; then\n"
97+ ++ " java=\"$JAVA_HOME/bin/java\"\n"
98+ ++ "fi\n"
99+ ++ "exec \"$java\" $java_args -jar $MYSELF \"$@\""
100+ ++ "exit 1\n"
101+
102+mkCodeGenEnv :: [(Name, SExp)] -> CodeGenerationEnv
103+mkCodeGenEnv globalInit =
104+ CodeGenerationEnv $ zipWith (\ (name, _) pos -> (name, pos)) globalInit [0..]
105+
106+mkCompilationUnit :: [(Name, SExp)] -> [(Name, SDecl)] -> FilePath -> CodeGeneration CompilationUnit
107+mkCompilationUnit globalInit defs out =
39108 CompilationUnit Nothing [ ImportDecl False idrisRts True
40109 , ImportDecl True idrisForeign True
41110 , ImportDecl False bigInteger False
@@ -44,7 +113,7 @@ mkCompilationUnit defs out =
44113 , ImportDecl False scanner False
45114 , ImportDecl False arrays False
46115 ] <$>
47− mkTypeDecl defs out
116+ mkTypeDecl globalInit defs out
48117 where
49118 idrisRts = J.Name $ map Ident ["org", "idris", "rts"]
50119 idrisForeign = J.Name $ map Ident ["org", "idris", "rts", "ForeignPrimitives"]
@@ -75,10 +144,8 @@ prefixCallNamespacesCase name (SDefaultCase e) = SDefaultCase (prefixCallNamespa
75144prefixCallNamespacesDecl :: Ident -> SDecl -> SDecl
76145prefixCallNamespacesDecl name (SFun fname args i e) = SFun fname args i (prefixCallNamespaces name e)
77146
78−mkTypeDecl :: [(Name, SDecl)] ->
79− FilePath ->
80− Either String [TypeDecl]
81−mkTypeDecl defs out =
147+mkTypeDecl :: [(Name, SExp)] -> [(Name, SDecl)] -> FilePath -> CodeGeneration [TypeDecl]
148+mkTypeDecl globalInit defs out =
82149 (\ name body -> [ClassTypeDecl $ ClassDecl [ Public
83150 , Annotation $ SingleElementAnnotation
84151 (J.Name [Ident "SuppressWarnings"])
@@ -91,18 +158,33 @@ mkTypeDecl defs out =
91158 body])
92159 <$> mkClassName out
93160 <*> ( mkClassName out
94− >>= (\ ident -> mkClassBody (map (second (prefixCallNamespacesDecl ident)) defs))
161+ >>= (\ ident -> mkClassBody globalInit (map (second (prefixCallNamespacesDecl ident)) defs))
95162 )
96163
97−mkClassName :: FilePath -> Either String Ident
164+mkClassName :: FilePath -> CodeGeneration Ident
98165mkClassName path =
99− left (\ err -> "Parser error in \"" ++ path ++ "\": " ++ (show err))
100− (parser ident . takeBaseName $ takeFileName path)
166+ T.lift $ left (\ err -> "Parser error in \"" ++ path ++ "\": " ++ (show err))
167+ (parser ident . takeBaseName $ takeFileName path)
168+
169+mkClassBody :: [(Name, SExp)] -> [(Name, SDecl)] -> CodeGeneration ClassBody
170+mkClassBody globalInit defs =
171+ (\ globals defs -> ClassBody . (globals++) . addMainMethod . mergeInnerClasses $ defs)
172+ <$> mkGlobalContext globalInit
173+ <*> mapM mkDecl defs
174+
175+mkGlobalContext :: [(Name, SExp)] -> CodeGeneration [Decl]
176+mkGlobalContext [] = return []
177+mkGlobalContext initExps =
178+ (\ exps -> [ MemberDecl $ FieldDecl [Private, Static, Final]
179+ objectArrayType
180+ [VarDecl (VarId $ Ident "globalContext")
181+ (Just . InitArray $ ArrayInit exps)
182+ ]
183+ ]
184+ )
185+ <$> mapM (\ (_, exp) -> InitExp <$> mkExp exp) initExps
101186
102−mkClassBody :: [(Name, SDecl)] ->
103− Either String ClassBody
104−mkClassBody defs = (ClassBody . addMainMethod . mergeInnerClasses)
105− <$> mapM mkDecl defs
187+
106188
107189addMainMethod :: [Decl] -> [Decl]
108190addMainMethod decls
@@ -160,17 +242,17 @@ mergeInnerClasses = foldl mergeInner []
160242 mergeInner [] decl' = [decl']
161243
162244
163−mkIdentifier :: Name -> Either String Ident
245+mkIdentifier :: Name -> CodeGeneration Ident
164246mkIdentifier (NS name _) = mkIdentifier name
165247mkIdentifier (MN i name) = (\ (Ident x) -> Ident $ x ++ ('_' : show i))
166248 <$> mkIdentifier (UN name)
167249mkIdentifier (UN name) =
168− left (\ err -> "Parser error in \"" ++ name ++ "\": " ++ (show err))
169− ( parser ident
170− . cleanReserved
171− . cleanNonLetter
172− . cleanStart
173− $ cleanWs False name)
250+ T.lift $ left (\ err -> "Parser error in \"" ++ name ++ "\": " ++ (show err))
251+ ( parser ident
252+ . cleanReserved
253+ . cleanNonLetter
254+ . cleanStart
255+ $ cleanWs False name)
174256 where
175257 cleanStart (x:xs)
176258 | isNumber x = '_' : (x:xs)
@@ -207,6 +289,7 @@ mkIdentifier (UN name) =
207289
208290
209291 cleanReserved "param" = "_param"
292+ cleanReserved "globalContext" = "_globalContext"
210293 cleanReserved "context" = "_context"
211294 cleanReserved "newcontext" = "_newcontext"
212295
@@ -300,10 +383,13 @@ mkIdentifier (UN name) =
300383 cleanReserved "fputStr" = "_fputStr"
301384 cleanReserved "fileEOF" = "_fileEOF"
302385 cleanReserved "isNull" = "_isNull"
386+ cleanReserved "idris_K" = "_idris_K"
387+ cleanReserved "idris_flipK" = "_idris_flipK"
388+ cleanReserved "idris_assignStack" = "_idris_assignStack"
303389
304390 cleanReserved x = x
305391
306−mkName :: Name -> Either String J.Name
392+mkName :: Name -> CodeGeneration J.Name
307393mkName (NS name nss) = (\ n ns -> J.Name (n:ns))
308394 <$> mkIdentifier name
309395 <*> mapM (mkIdentifier . UN) nss
@@ -330,8 +416,9 @@ idrisTailCallClosureType = ClassType [(Ident "TailCallClosure", [])]
330416idrisObjectType :: ClassType
331417idrisObjectType = ClassType [(Ident "IdrisObject", [])]
332418
333−contextArray :: Exp
334−contextArray = ExpName $ J.Name [Ident "context"]
419+contextArray :: LVar -> Exp
420+contextArray (Loc _) = ExpName $ J.Name [Ident "context"]
421+contextArray (Glob _) = ExpName $ J.Name [Ident "globalContext"]
335422
336423charType :: ClassType
337424charType = ClassType [(Ident "Character", [])]
@@ -369,29 +456,38 @@ runtimeExceptionType = ClassType [(Ident "RuntimeException", [])]
369456comparableType :: ClassType
370457comparableType = ClassType [(Ident "Comparable", [])]
371458
372−mkDecl :: (Name, SDecl) ->
373− Either String Decl
459+mkDecl :: (Name, SDecl) -> CodeGeneration Decl
374460mkDecl ((NS n (ns:nss)), decl) =
375461 (\ name body -> MemberDecl $ MemberClassDecl $ ClassDecl [Public, Static] name [] Nothing [] body)
376462 <$> mkIdentifier (UN ns)
377− <*> mkClassBody [(NS n nss, decl)]
463+ <*> mkClassBody [] [(NS n nss, decl)]
378464mkDecl (_, SFun name params stackSize body) =
379− (\ name params cont ->
465+ (\ name params paramNames methodBody ->
380466 MemberDecl $ MethodDecl [Public, Static]
381467 []
382468 (Just . RefType $ ClassRefType objectType)
383469 name
384470 params
385471 []
386− (MethodBody . Just $ Block [ BlockStmt . Return . Just . MethodInv $
387− PrimaryMethodCall cont [] (Ident "call") []])
472+ (MethodBody . Just $ Block
473+ [ LocalVars [Final]
474+ objectArrayType
475+ [ VarDecl (VarDeclArray . VarId $ Ident "context")
476+ (Just . InitArray . ArrayInit $
477+ paramNames
478+ ++ replicate stackSize (InitExp . Lit $ Null))
479+ ]
480+ , BlockStmt . Return $ Just methodBody
481+ ]
482+ )
388483 )
389484 <$> mkIdentifier name
390485 <*> mapM mkFormalParam params
391− <*> mkClosure params stackSize body
486+ <*> mapM (\ p -> (InitExp . ExpName) <$> mkName p) params
487+ <*> mkExp body
392488
393489
394−mkClosure :: [Name] -> Int -> SExp -> Either String Exp
490+mkClosure :: [Name] -> Int -> SExp -> CodeGeneration Exp
395491mkClosure params stackSize body =
396492 (\ paramArray body ->
397493 InstanceCreation []
@@ -402,126 +498,55 @@ mkClosure params stackSize body =
402498 <$> mkStackInit params stackSize
403499 <*> mkClosureCall body
404500
405−mkFormalParam :: Name -> Either String FormalParam
501+mkFormalParam :: Name -> CodeGeneration FormalParam
406502mkFormalParam name =
407503 (\ name -> FormalParam [Final] (RefType . ClassRefType $ objectType) False (VarId name))
408504 <$> mkIdentifier name
409505
410−mkClosureCall :: SExp -> Either String Decl
506+mkClosureCall :: SExp -> CodeGeneration Decl
411507mkClosureCall body =
412508 (\ body -> MemberDecl $ MethodDecl [Public] [] (Just . RefType $ ClassRefType objectType) (Ident "call") [] [] body)
413509 <$> mkMethodBody body
414510
415−mkMethodBody :: SExp -> Either String MethodBody
511+mkMethodBody :: SExp -> CodeGeneration MethodBody
416512mkMethodBody exp =
417513 (\ exp -> MethodBody . Just . Block $ [BlockStmt . Return . Just $ exp])
418514 <$> mkExp exp
419515
420−mkStackInit :: [Name] -> Int -> Either String Exp
516+mkStackInit :: [Name] -> Int -> CodeGeneration Exp
421517mkStackInit params stackSize =
422518 (\ localVars -> ArrayCreateInit objectArrayType 0 . ArrayInit $
423519 (map (InitExp . ExpName) localVars)
424520 ++ (replicate (stackSize) (InitExp $ Lit Null)))
425521 <$> mapM mkName params
426522
427−mkAnonymousLetBinding :: LVar -> SExp -> SExp -> Either String Exp
428−mkAnonymousLetBinding var oldExp newExp =
429− (\ bindingMethod newExp ->
430− MethodInv $ PrimaryMethodCall
431− ( MethodInv $ PrimaryMethodCall ( InstanceCreation []
432− objectType
433− []
434− (Just $ ClassBody [MemberDecl $ bindingMethod])
435− )
436− []
437− (Ident "apply")
438− [ contextArray
439− , newExp]
440− )
441− []
442− (Ident "call")
443− []
444− )
445− <$> mkLetBindingMethod var oldExp
446− <*> mkExp newExp
447−
448−
449−mkContextParam :: FormalParam
450−mkContextParam =
451− FormalParam [Final] (objectArrayType) False (VarId (Ident "context"))
452−
453−mkLetBindingMethod :: LVar -> SExp -> Either String MemberDecl
454−mkLetBindingMethod (Loc i) oldExp =
455− (\ param bindingStack oldCont ->
456− MethodDecl [Final, Public]
457− []
458− (Just . RefType $ ClassRefType idrisClosureType)
459− (Ident "apply")
460− [mkContextParam, param]
461− []
462− (MethodBody . Just . Block $
463− bindingStack ++ [BlockStmt . Return $ Just oldCont]
464− )
465− )
466− <$> mkFormalParam (UN "param")
467− <*> mkBindingStack True i [UN "param"]
468− <*> mkBindingClosure oldExp
469−
470−mkBindingClosure :: SExp -> Either String Exp
471−mkBindingClosure oldExp =
472− (\ oldCall ->
473− InstanceCreation []
474− idrisClosureType
475− [ ExpName $ J.Name [Ident "new_context"] ]
476− (Just $ ClassBody [oldCall])
523+mkK :: Exp -> Exp -> Exp
524+mkK result drop =
525+ MethodInv $ MethodCall (J.Name [Ident "idris_K"]) [ result, drop ]
526+
527+mkFlipK :: Exp -> Exp -> Exp
528+mkFlipK drop result =
529+ MethodInv $ MethodCall (J.Name [Ident "idris_flipK"]) [ drop, result ]
530+
531+mkLet :: LVar -> SExp -> SExp -> CodeGeneration Exp
532+mkLet (Loc pos) oldExp newExp =
533+ (\ oldExp newExp ->
534+ mkFlipK ( Assign ( ArrayLhs $ ArrayIndex (ExpName $ J.Name [Ident "context"])
535+ (Lit $ Int (toInteger pos)))
536+ EqualA
537+ newExp
538+ )
539+ oldExp
477540 )
478− <$> mkClosureCall oldExp
479−
480−
481−mkBindingStack :: Bool -> Int -> [Name] -> Either String [BlockStmt]
482−mkBindingStack checked parentStackStart params =
483− (\ paramNames ->
484− ( LocalVars [Final]
485− objectArrayType
486− [ VarDecl (VarDeclArray . VarId $ Ident "new_context")
487− (Just . InitExp $ mkContextCopy checked parentStackStart params)
488− ])
489− : ( map (\ (param, pos) ->
490− BlockStmt . ExpStmt $
491− Assign (ArrayLhs $ ArrayIndex (ExpName $ J.Name [Ident "new_context"])
492− (Lit $ Int (toInteger pos)))
493− EqualA
494− (ExpName param)) $ zip paramNames [parentStackStart..]
495− )
496− )
497− <$> mapM mkName params
541+ <$> mkExp oldExp
542+ <*> mkExp newExp
543+mkLet (Glob _) _ _ = T.lift $ Left "Cannot let bind to global variable"
498544
499−mkContextCopy :: Bool -> Int -> [Name] -> Exp
500−mkContextCopy True parentStackStart params =
501− MethodInv $ PrimaryMethodCall (ExpName $ J.Name [Ident "context"])
502− []
503− (Ident "clone")
504− []
505−mkContextCopy False parentStackStart params =
506− MethodInv $ TypeMethodCall (J.Name [Ident "Arrays"])
507− []
508− (Ident "copyOf")
509− [ ExpName $ J.Name [Ident "context"]
510− , MethodInv
511− $ TypeMethodCall (J.Name [Ident "Math"])
512− []
513− (Ident "max")
514− [ FieldAccess $ PrimaryFieldAccess (ExpName $ J.Name [Ident "context"])
515− (Ident "length")
516− , Lit . Int $ toInteger (parentStackStart + length params)
517− ]
518− ]
519−
520545reverseNameSpace :: J.Name -> J.Name
521546reverseNameSpace (J.Name ids) =
522547 J.Name ((tail ids) ++ [head ids])
523548
524−mkCase :: Bool -> LVar -> [SAlt] -> Either String Exp
549+mkCase :: Bool -> LVar -> [SAlt] -> CodeGeneration Exp
525550mkCase checked var ((SConCase parentStackPos consIndex _ params branchExpression):cases) =
526551 mkConsCase checked
527552 var
@@ -531,11 +556,11 @@ mkCase checked var ((SConCase parentStackPos consIndex _ params branchExpression
531556 branchExpression
532557 (SCase var cases)
533558mkCase checked var (c@(SConstCase constant branchExpression):cases) =
534− (\ constant branchExpression alternative ->
559+ (\ constant branchExpression alternative var->
535560 Cond ( MethodInv $ PrimaryMethodCall (constant)
536561 []
537562 (Ident "equals")
538− [mkVarAccess Nothing var]
563+ [var]
539564 )
540565 branchExpression
541566 alternative
@@ -543,10 +568,11 @@ mkCase checked var (c@(SConstCase constant branchExpression):cases) =
543568 <$> mkExp (SConst constant)
544569 <*> mkExp branchExpression
545570 <*> mkCase checked var cases
571+ <*> mkVarAccess Nothing var
546572mkCase checked var (SDefaultCase exp:cases) = mkExp exp
547573mkCase checked _ [] = mkExp (SError "Non-exhaustive pattern")
548574
549−mkConsCase :: Bool -> LVar -> Int -> Int -> [Name] -> SExp -> SExp -> Either String Exp
575+mkConsCase :: Bool -> LVar -> Int -> Int -> [Name] -> SExp -> SExp -> CodeGeneration Exp
550576mkConsCase checked
551577 toDeconstruct
552578 parentStackStart
@@ -554,13 +580,13 @@ mkConsCase checked
554580 params
555581 branchExpression
556582 alternative =
557− (\ caseBinding alternative->
558− Cond (BinOp (InstanceOf (mkVarAccess Nothing toDeconstruct)
583+ (\ caseBinding alternative var varCasted->
584+ Cond (BinOp (InstanceOf (var)
559585 (ClassRefType idrisObjectType)
560586 )
561587 CAnd
562588 ( BinOp
563− ( MethodInv $ PrimaryMethodCall (mkVarAccess (Just idrisObjectType) toDeconstruct)
589+ ( MethodInv $ PrimaryMethodCall (varCasted)
564590 []
565591 (Ident "getConstructorId")
566592 []
@@ -574,11 +600,24 @@ mkConsCase checked
574600 )
575601 <$> mkCaseBinding checked toDeconstruct parentStackStart params branchExpression
576602 <*> mkExp alternative
577−
578−
579−mkCaseBinding :: Bool -> LVar -> Int -> [Name] -> SExp -> Either String Exp
580−mkCaseBinding checked var parentStackStart params branchExpression =
581− (\ bindingMethod ->
603+ <*> mkVarAccess (Nothing) toDeconstruct
604+ <*> mkVarAccess (Just idrisObjectType) toDeconstruct
605+
606+
607+mkCaseBinding :: Bool -> LVar -> Int -> [Name] -> SExp -> CodeGeneration Exp
608+mkCaseBinding True var parentStackStart params branchExpression =
609+ (\ branchExpression deconstruction ->
610+ mkFlipK (MethodInv $ MethodCall (J.Name [Ident "idris_assignStack"])
611+ ( (ExpName $ J.Name [Ident "context"])
612+ : (Lit $ Int (toInteger parentStackStart))
613+ : deconstruction
614+ ))
615+ (branchExpression)
616+ )
617+ <$> mkExp branchExpression
618+ <*> mkCaseBindingDeconstruction var params
619+mkCaseBinding False var parentStackStart params branchExpression =
620+ (\ bindingMethod deconstruction ->
582621 MethodInv $ PrimaryMethodCall
583622 ( MethodInv $ PrimaryMethodCall (InstanceCreation []
584623 (ClassType [(Ident "Object", [])])
@@ -587,20 +626,21 @@ mkCaseBinding checked var parentStackStart params branchExpression =
587626 )
588627 []
589628 (Ident "apply")
590− ( contextArray
591− : (mkCaseBindingDeconstruction var params)
592− )
629+ ( contextArray (Loc undefined) : deconstruction )
593630 )
594631 []
595632 (Ident "call")
596633 []
597634 )
598− <$> mkCaseBindingMethod checked parentStackStart params branchExpression
599−
635+ <$> mkCaseBindingMethod parentStackStart params branchExpression
636+ <*> mkCaseBindingDeconstruction var params
600637
638+mkCaseBindingDeconstruction :: LVar -> [Name] -> CodeGeneration [Exp]
639+mkCaseBindingDeconstruction var members =
640+ mapM (mkProjection var) ([0..(length members - 1)])
601641
602−mkCaseBindingMethod :: Bool -> Int -> [Name] -> SExp -> Either String MemberDecl
603−mkCaseBindingMethod checked parentStackStart params branchExpression =
642+mkCaseBindingMethod :: Int -> [Name] -> SExp -> CodeGeneration MemberDecl
643+mkCaseBindingMethod parentStackStart params branchExpression =
604644 (\ formalParams caseBindingStack branchExpression ->
605645 MethodDecl [Final, Public]
606646 []
@@ -612,22 +652,74 @@ mkCaseBindingMethod checked parentStackStart params branchExpression =
612652 caseBindingStack ++
613653 [BlockStmt . Return $ Just branchExpression]))
614654 <$> mapM mkFormalParam params
615− <*> mkBindingStack checked parentStackStart params
655+ <*> mkBindingStack False parentStackStart params
616656 <*> mkBindingClosure branchExpression
617657
618−mkCaseBindingDeconstruction :: LVar -> [Name] -> [Exp]
619−mkCaseBindingDeconstruction var members =
620− map (mkProjection var) ([0..(length members - 1)])
658+mkContextParam :: FormalParam
659+mkContextParam =
660+ FormalParam [Final] (objectArrayType) False (VarId (Ident "context"))
661+
662+mkBindingClosure :: SExp -> CodeGeneration Exp
663+mkBindingClosure oldExp =
664+ (\ oldCall ->
665+ InstanceCreation []
666+ idrisClosureType
667+ [ ExpName $ J.Name [Ident "new_context"] ]
668+ (Just $ ClassBody [oldCall])
669+ )
670+ <$> mkClosureCall oldExp
671+
672+
673+mkBindingStack :: Bool -> Int -> [Name] -> CodeGeneration [BlockStmt]
674+mkBindingStack checked parentStackStart params =
675+ (\ paramNames ->
676+ ( LocalVars [Final]
677+ objectArrayType
678+ [ VarDecl (VarDeclArray . VarId $ Ident "new_context")
679+ (Just . InitExp $ mkContextCopy checked parentStackStart params)
680+ ])
681+ : ( map (\ (param, pos) ->
682+ BlockStmt . ExpStmt $
683+ Assign (ArrayLhs $ ArrayIndex (ExpName $ J.Name [Ident "new_context"])
684+ (Lit $ Int (toInteger pos)))
685+ EqualA
686+ (ExpName param)) $ zip paramNames [parentStackStart..]
687+ )
688+ )
689+ <$> mapM mkName params
621690
622−mkProjection :: LVar -> Int -> Exp
691+mkContextCopy :: Bool -> Int -> [Name] -> Exp
692+mkContextCopy True parentStackStart params =
693+ MethodInv $ PrimaryMethodCall (ExpName $ J.Name [Ident "context"])
694+ []
695+ (Ident "clone")
696+ []
697+mkContextCopy False parentStackStart params =
698+ MethodInv $ TypeMethodCall (J.Name [Ident "Arrays"])
699+ []
700+ (Ident "copyOf")
701+ [ ExpName $ J.Name [Ident "context"]
702+ , MethodInv
703+ $ TypeMethodCall (J.Name [Ident "Math"])
704+ []
705+ (Ident "max")
706+ [ FieldAccess $ PrimaryFieldAccess (ExpName $ J.Name [Ident "context"])
707+ (Ident "length")
708+ , Lit . Int $ toInteger (parentStackStart + length params)
709+ ]
710+ ]
711+
712+mkProjection :: LVar -> Int -> CodeGeneration Exp
623713mkProjection var memberNr =
624− ArrayAccess $ ArrayIndex ( MethodInv $ PrimaryMethodCall
625− (mkVarAccess (Just idrisObjectType) var)
626− []
627− (Ident "getData")
628− []
629− )
630− (Lit $ Int (toInteger memberNr))
714+ (\ var -> ArrayAccess $ ArrayIndex ( MethodInv $ PrimaryMethodCall
715+ (var)
716+ []
717+ (Ident "getData")
718+ []
719+ )
720+ (Lit $ Int (toInteger memberNr))
721+ )
722+ <$> mkVarAccess (Just idrisObjectType) var
631723
632724type ClassName = String
633725
@@ -642,22 +734,22 @@ mkClass :: ClassType -> Exp
642734mkClass classType =
643735 ClassLit . Just . RefType .ClassRefType $ classType
644736
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)
737+mkBinOpExp :: ClassType -> Op -> [LVar] -> CodeGeneration Exp
738+mkBinOpExp castTo op (var:vars) = do
739+ start <- mkVarAccess (Just castTo) var
740+ foldM (\ exp var -> BinOp exp op <$> mkVarAccess (Just castTo) var) start vars
741+
742+mkBinOpExpTrans :: (Exp -> Exp) -> (Exp -> Exp) -> ClassType -> Op -> [LVar] -> CodeGeneration Exp
743+mkBinOpExpTrans opTransformation resultTransformation castTo op (var:vars) = do
744+ start <- (mkVarAccess (Just castTo) var)
745+ foldM (\ exp var -> resultTransformation
746+ . BinOp (opTransformation exp) op
747+ . opTransformation
748+ <$> mkVarAccess (Just castTo) var)
749+ start
649750 vars
650751
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
655− . opTransformation
656− . mkVarAccess (Just castTo))
657− (mkVarAccess (Just castTo) var)
658− vars
659−
660−mkBinOpExpConv :: String -> PrimType -> ClassType -> Op -> [LVar] -> Exp
752+mkBinOpExpConv :: String -> PrimType -> ClassType -> Op -> [LVar] -> CodeGeneration Exp
661753mkBinOpExpConv fromMethodName toType fromType@(ClassType [(cls@(Ident _), [])]) op args =
662754 mkBinOpExpTrans (\ exp -> MethodInv $ TypeMethodCall (J.Name [cls])
663755 []
@@ -673,59 +765,70 @@ mkBinOpExpConv fromMethodName toType fromType@(ClassType [(cls@(Ident _), [])])
673765 op
674766 args
675767
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)
768+mkLogicalBinOpExp :: ClassType -> Op -> [LVar] -> CodeGeneration Exp
769+mkLogicalBinOpExp castTo op (var:vars) = do
770+ start <- mkVarAccess (Just castTo) var
771+ foldM (\ exp var -> mkBoolToNumber castTo . BinOp exp op <$> mkVarAccess (Just castTo) var)
772+ start
680773 vars
681774
682−mkMethodOpChain1 :: (Exp -> Exp) -> ClassType -> String -> [LVar] -> Exp
775+mkMethodOpChain1 :: (Exp -> Exp) -> ClassType -> String -> [LVar] -> CodeGeneration Exp
683776mkMethodOpChain1 = mkMethodOpChain id
684777
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
689− []
690− (Ident method)
691− [mkVarAccess (Just $ castTo) arg']
778+mkMethodOpChain :: (Exp -> Exp) -> (Exp -> Exp) -> ClassType -> String -> [LVar] -> CodeGeneration Exp
779+mkMethodOpChain initialTransformation resultTransformation castTo method (arg:args) = do
780+ start <- initialTransformation <$> mkVarAccess (Just $ castTo) arg
781+ foldM (\ exp arg' ->
782+ resultTransformation
783+ . MethodInv
784+ . PrimaryMethodCall exp [] (Ident method)
785+ . (:[])
786+ <$> mkVarAccess (Just $ castTo) arg'
692787 )
693− (initialTransformation (mkVarAccess (Just $ castTo) arg))
788+ start
694789 args
695790
696791mkBoolToNumber :: ClassType -> Exp -> Exp
697792mkBoolToNumber (ClassType [(Ident name, [])]) boolExp =
698793 Cond boolExp (mkPrimitive name (Int 1)) (mkPrimitive name (Int 0))
699794
700−mkZeroExt :: String -> Int -> ClassType -> ClassType -> LVar -> Exp
701−mkZeroExt toMethod bits fromType toType@(ClassType [(toTypeName, [])]) var =
702− MethodInv $ TypeMethodCall (J.Name [toTypeName])
703− []
704− (Ident "valueOf")
705− [ Cond ( BinOp (mkVarAccess (Just $ fromType) var)
706− LThan
707− (Lit $ Int 0)
708− )
709− ( BinOp (Lit $ Int (2^bits))
710− Add
711− (mkSignedExt' toMethod fromType var)
712− )
713− (mkSignedExt' toMethod fromType var)
714− ]
795+mkZeroExt :: String -> Int -> ClassType -> ClassType -> LVar -> CodeGeneration Exp
796+mkZeroExt toMethod bits fromType toType@(ClassType [(toTypeName, [])]) var = do
797+ (\ var sext ->
798+ MethodInv $ TypeMethodCall (J.Name [toTypeName])
799+ []
800+ (Ident "valueOf")
801+ [ Cond ( BinOp (var)
802+ LThan
803+ (Lit $ Int 0)
804+ )
805+ ( BinOp (Lit $ Int (2^bits))
806+ Add
807+ (sext)
808+ )
809+ sext
810+ ]
811+ )
812+ <$> mkVarAccess (Just $ fromType) var
813+ <*> mkSignedExt' toMethod fromType var
715814
716−mkSignedExt :: String -> ClassType -> ClassType -> LVar -> Exp
815+mkSignedExt :: String -> ClassType -> ClassType -> LVar -> CodeGeneration Exp
717816mkSignedExt toMethod fromType (ClassType [(toTypeName, [])]) var =
718− MethodInv $ TypeMethodCall (J.Name [toTypeName])
719− []
720− (Ident "valueOf")
721− [ mkSignedExt' toMethod fromType var ]
817+ (\ sext -> MethodInv $ TypeMethodCall (J.Name [toTypeName])
818+ []
819+ (Ident "valueOf")
820+ [ sext ]
821+ )
822+ <$> mkSignedExt' toMethod fromType var
722823
723−mkSignedExt' :: String -> ClassType -> LVar -> Exp
824+mkSignedExt' :: String -> ClassType -> LVar -> CodeGeneration Exp
724825mkSignedExt' toMethod fromType var =
725− MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ fromType) var)
726− []
727− (Ident toMethod)
728− []
826+ (\ var -> MethodInv $ PrimaryMethodCall (var)
827+ []
828+ (Ident toMethod)
829+ []
830+ )
831+ <$> mkVarAccess (Just $ fromType) var
729832
730833data SPartialOrder
731834 = SLt
@@ -747,26 +850,39 @@ mkPartialOrder SGe x =
747850 (BinOp (Lit $ Int 0) Equal x)
748851mkPartialOrder SGt x = (BinOp (Lit $ Int 1) Equal x)
749852
750−mkVarAccess :: Maybe ClassType -> LVar -> Exp
751−mkVarAccess Nothing (Loc i) =
752− ArrayAccess $ ArrayIndex contextArray (Lit $ Int (toInteger i))
853+varPos :: LVar -> CodeGeneration Integer
854+varPos (Loc i) = return (toInteger i)
855+varPos (Glob name) = do
856+ positions <- globalVariablePositions <$> get
857+ case lookup name positions of
858+ (Just pos) -> return pos
859+ Nothing -> T.lift . Left $ "Invalid global variable id: " ++ show name
860+
861+mkVarAccess :: Maybe ClassType -> LVar -> CodeGeneration Exp
862+mkVarAccess Nothing var =
863+ (\ pos -> ArrayAccess $ ArrayIndex (contextArray var) (Lit $ Int pos))
864+ <$> varPos var
753865mkVarAccess (Just castTo) var =
754− Cast (RefType . ClassRefType $ castTo) (mkVarAccess Nothing var)
866+ Cast (RefType . ClassRefType $ castTo) <$> (mkVarAccess Nothing var)
755867
756−mkPrimitiveCast :: ClassType -> ClassType -> LVar -> Exp
868+mkPrimitiveCast :: ClassType -> ClassType -> LVar -> CodeGeneration Exp
757869mkPrimitiveCast fromType (ClassType [(toType, [])]) var =
758− MethodInv $ TypeMethodCall (J.Name [toType])
759− []
760− (Ident "valueOf")
761− [mkVarAccess (Just fromType) var]
762−
763−mkToString :: ClassType -> LVar -> Exp
764−mkToString castTo var =
765− MethodInv $ PrimaryMethodCall (mkVarAccess (Just castTo) var)
766− []
767− (Ident "toString")
870+ (\ var ->
871+ MethodInv $ TypeMethodCall (J.Name [toType])
768872 []
873+ (Ident "valueOf")
874+ [var]
875+ )
876+ <$> mkVarAccess (Just fromType) var
769877
878+mkToString :: ClassType -> LVar -> CodeGeneration Exp
879+mkToString castTo var =
880+ (\ var -> MethodInv $ PrimaryMethodCall (var)
881+ []
882+ (Ident "toString")
883+ []
884+ )
885+ <$> mkVarAccess (Just castTo) var
770886data Std = In | Out | Err
771887
772888instance Show Std where
@@ -784,49 +900,57 @@ mkSystemOutPrint value =
784900 (Ident "print")
785901 [value]
786902
787−mkMathFun :: String -> LVar -> Exp
903+mkMathFun :: String -> LVar -> CodeGeneration Exp
788904mkMathFun funName var =
789− MethodInv $ TypeMethodCall (J.Name [Ident "Double"])
790− []
791− (Ident "valueOf")
792− [ MethodInv $ TypeMethodCall (J.Name [Ident "Math"])
793− []
794− (Ident funName)
795− [mkVarAccess (Just doubleType) var]
796− ]
905+ (\ var -> MethodInv $ TypeMethodCall (J.Name [Ident "Double"])
906+ []
907+ (Ident "valueOf")
908+ [ MethodInv $ TypeMethodCall (J.Name [Ident "Math"])
909+ []
910+ (Ident funName)
911+ [var]
912+ ]
913+ )
914+ <$> mkVarAccess (Just doubleType) var
797915
798−mkStringAtIndex :: LVar -> Exp -> Exp
916+mkStringAtIndex :: LVar -> Exp -> CodeGeneration Exp
799917mkStringAtIndex var indexExp =
800− MethodInv $ TypeMethodCall (J.Name [Ident "Integer"])
801− []
802− (Ident "valueOf")
803− [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) var)
804− []
805− (Ident "charAt")
806− [indexExp]
807− ]
918+ (\ var -> MethodInv $ TypeMethodCall (J.Name [Ident "Integer"])
919+ []
920+ (Ident "valueOf")
921+ [ MethodInv $ PrimaryMethodCall (var)
922+ []
923+ (Ident "charAt")
924+ [indexExp]
925+ ]
926+ )
927+ <$> mkVarAccess (Just stringType) var
808928
809929mkForeignType :: 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
930+mkForeignType FInt = return integerType
931+mkForeignType FChar = return integerType
932+mkForeignType FString = return stringType
933+mkForeignType FPtr = return objectType
934+mkForeignType FDouble = return doubleType
935+mkForeignType FAny = return objectType
816936mkForeignType FUnit = Nothing
817937
818−mkForeignVarAccess :: FType -> LVar -> Exp
938+mkForeignVarAccess :: FType -> LVar -> CodeGeneration Exp
819939mkForeignVarAccess FInt var =
820− MethodInv $ PrimaryMethodCall (mkVarAccess (Just integerType) var)
821− []
822− (Ident "intValue")
823− []
824−mkForeignVarAccess FChar var = Cast (PrimType CharT) (mkForeignVarAccess FInt var)
940+ (\ var -> MethodInv $ PrimaryMethodCall (var)
941+ []
942+ (Ident "intValue")
943+ []
944+ )
945+ <$> mkVarAccess (Just integerType) var
946+mkForeignVarAccess FChar var = Cast (PrimType CharT) <$> mkForeignVarAccess FInt var
825947mkForeignVarAccess FDouble var =
826− MethodInv $ PrimaryMethodCall (mkVarAccess (Just doubleType) var)
827− []
828− (Ident "doubleValue")
829− []
948+ (\ var -> MethodInv $ PrimaryMethodCall (var)
949+ []
950+ (Ident "doubleValue")
951+ []
952+ )
953+ <$> mkVarAccess (Just doubleType) var
830954mkForeignVarAccess otherType var = mkVarAccess (mkForeignType otherType) var
831955
832956mkFromForeignType :: FType -> Exp -> Exp
@@ -843,7 +967,7 @@ mkFromForeignType FDouble from =
843967 [from]
844968mkFromForeignType _ from = from
845969
846−mkForeignInvoke :: FType -> String -> [(FType, LVar)] -> Either String Exp
970+mkForeignInvoke :: FType -> String -> [(FType, LVar)] -> CodeGeneration Exp
847971mkForeignInvoke fType method args =
848972 (\ foreignInvokeMeth ->
849973 MethodInv $ PrimaryMethodCall (InstanceCreation []
@@ -858,7 +982,7 @@ mkForeignInvoke fType method args =
858982 <$> mkForeignInvokeMethod fType method args
859983
860984
861−mkForeignInvokeMethod :: FType -> String -> [(FType, LVar)] -> Either String MemberDecl
985+mkForeignInvokeMethod :: FType -> String -> [(FType, LVar)] -> CodeGeneration MemberDecl
862986mkForeignInvokeMethod fType method args =
863987 (\ tryBlock ->
864988 MethodDecl [Public, Final]
@@ -867,111 +991,124 @@ mkForeignInvokeMethod fType method args =
867991 (Ident "foreignInvoke")
868992 []
869993 []
870− (MethodBody . Just $ Block [ BlockStmt $
871− Try tryBlock
872− [ Catch (FormalParam []
873− (RefType $ ClassRefType exceptionType)
874− False
875− (VarId $ Ident "ex")
876− )
877− (Block [ BlockStmt . Throw $
878− InstanceCreation []
879− runtimeExceptionType
880− [ExpName $ J.Name [Ident "ex"]]
881− Nothing
882− ]
883− )
884− ]
885− Nothing
886− ]
994+ (MethodBody . Just $ Block
995+ [ BlockStmt
996+ $ Try tryBlock
997+ [ Catch (FormalParam []
998+ (RefType $ ClassRefType exceptionType)
999+ False
1000+ (VarId $ Ident "ex")
1001+ )
1002+ (Block [ BlockStmt
1003+ . Throw
1004+ $ InstanceCreation []
1005+ runtimeExceptionType
1006+ [ExpName $ J.Name [Ident "ex"]]
1007+ Nothing
1008+ ]
1009+ )
1010+ ]
1011+ Nothing
1012+ ]
8871013 )
888− )
1014+ )
8891015 <$> mkForeignInvokeTryBlock fType method args
8901016
8911017
892−mkForeignInvokeTryBlock :: FType -> String -> [(FType, LVar)] -> Either String Block
1018+mkForeignInvokeTryBlock :: FType -> String -> [(FType, LVar)] -> CodeGeneration Block
8931019mkForeignInvokeTryBlock FUnit method args =
894− (\ method -> Block [ BlockStmt . ExpStmt . MethodInv $
895− MethodCall method
896− (map (uncurry mkForeignVarAccess) args)
897− , BlockStmt $ Return (Just $ Lit Null)
898− ]
1020+ (\ method args -> Block [ BlockStmt . ExpStmt . MethodInv $ MethodCall method args
1021+ , BlockStmt $ Return (Just $ Lit Null)
1022+ ]
8991023 )
900− <$> ( left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
901− (parser name method)
1024+ <$> ( T.lift $ left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
1025+ (parser name method)
9021026 )
1027+ <*> mapM (uncurry mkForeignVarAccess) args
9031028mkForeignInvokeTryBlock fType method args =
904− (\ method -> Block [ BlockStmt . Return
1029+ (\ method args -> Block [ BlockStmt . Return
9051030 . Just
9061031 . mkFromForeignType fType
907− . MethodInv $
908− MethodCall method
909− (map (uncurry mkForeignVarAccess) args)
910− ]
1032+ . MethodInv
1033+ $ MethodCall method args
1034+ ]
9111035 )
912− <$> ( left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
913− (parser name method)
1036+ <$> ( T.lift $ left (\ err -> "Error parsing name \"" ++ method ++ "\" :" ++ (show err))
1037+ (parser name method)
9141038 )
1039+ <*> mapM (uncurry mkForeignVarAccess) args
9151040
916−mkMethodClosure :: Name -> [LVar] -> Either String Exp
1041+mkMethodClosure :: Name -> [LVar] -> CodeGeneration Exp
9171042mkMethodClosure name args =
9181043 (\ name args ->
9191044 InstanceCreation []
9201045 idrisClosureType
9211046 [ (ExpName $ J.Name [Ident "context"]) ]
922− ( Just $ ClassBody
923− [ MemberDecl $
924− MethodDecl [Public, Final]
925− []
926− (Just . RefType $ ClassRefType objectType)
927− (Ident "call")
928− []
929− []
930− ( MethodBody . Just $
931− Block [ BlockStmt . Return . Just . MethodInv $
932− MethodCall (reverseNameSpace name)
933− args
934− ]
935− )
936− ]
937− )
1047+ ( Just
1048+ $ ClassBody
1049+ [ MemberDecl
1050+ $ MethodDecl [Public, Final]
1051+ []
1052+ (Just . RefType $ ClassRefType objectType)
1053+ (Ident "call")
1054+ []
1055+ []
1056+ ( MethodBody
1057+ . Just
1058+ $ Block [ BlockStmt
1059+ . Return
1060+ . Just
1061+ . MethodInv
1062+ $ MethodCall (reverseNameSpace name)
1063+ args
1064+ ]
1065+ )
1066+ ]
1067+ )
9381068 )
9391069 <$> mkName name
9401070 <*> mapM (mkExp . SV) args
9411071
942−mkThread :: LVar -> Either String Exp
1072+mkThread :: LVar -> CodeGeneration Exp
9431073mkThread arg =
944− (\ eval -> MethodInv $
945− PrimaryMethodCall (InstanceCreation []
946− (ClassType [(Ident "Thread", [])])
947− [ eval ]
948− ( Just $ ClassBody
949− [ MemberDecl $ MethodDecl
950− [Public, Final]
951− []
952− (Just . RefType $ ClassRefType objectType)
953− (Ident "_start")
954− []
955− []
956− ( MethodBody . Just $ Block
957− [ BlockStmt . ExpStmt . MethodInv $ MethodCall
958− (J.Name [Ident "start"])
959− []
960− , BlockStmt . Return . Just $ This
1074+ (\ eval ->
1075+ MethodInv
1076+ $ PrimaryMethodCall (InstanceCreation []
1077+ (ClassType [(Ident "Thread", [])])
1078+ [ eval ]
1079+ ( Just
1080+ $ ClassBody [ MemberDecl
1081+ $ MethodDecl [Public, Final]
1082+ []
1083+ (Just . RefType $ ClassRefType objectType)
1084+ (Ident "_start")
1085+ []
1086+ []
1087+ ( MethodBody
1088+ . Just
1089+ $ Block [ BlockStmt
1090+ . ExpStmt
1091+ . MethodInv
1092+ $ MethodCall (J.Name [Ident "start"])
1093+ []
1094+ , BlockStmt
1095+ . Return
1096+ . Just
1097+ $ This
9611098 ]
962− )
963− ]
964− )
965− )
966− []
967− (Ident "_start")
968− []
1099+ )
1100+ ]
1101+ )
1102+ )
1103+ []
1104+ (Ident "_start")
1105+ []
9691106 )
9701107 <$> mkThreadBinding arg
9711108
972−mkThreadBinding :: LVar -> Either String Exp
1109+mkThreadBinding :: LVar -> CodeGeneration Exp
9731110mkThreadBinding var =
974− (\ bindingMethod ->
1111+ (\ bindingMethod var ->
9751112 MethodInv $ PrimaryMethodCall ( InstanceCreation []
9761113 objectType
9771114 []
@@ -979,12 +1116,13 @@ mkThreadBinding var =
9791116 )
9801117 []
9811118 (Ident "apply")
982− [ mkVarAccess Nothing var ]
983− )
1119+ [ var ]
1120+ )
9841121 <$> mkThreadBindingMethod
1122+ <*> mkVarAccess Nothing var
9851123
9861124
987−mkThreadBindingMethod :: Either String MemberDecl
1125+mkThreadBindingMethod :: CodeGeneration MemberDecl
9881126mkThreadBindingMethod =
9891127 (\ compute ->
9901128 MethodDecl [Final, Public]
@@ -1011,9 +1149,8 @@ mkThreadBindingStack =
10111149 ]
10121150
10131151
1014−mkExp :: SExp -> Either String Exp
1015−mkExp (SV var) =
1016− return $ mkVarAccess Nothing var
1152+mkExp :: SExp -> CodeGeneration Exp
1153+mkExp (SV var) = mkVarAccess Nothing var
10171154mkExp (SApp False name args) =
10181155 (\ methClosure ->
10191156 MethodInv $ PrimaryMethodCall ( InstanceCreation []
@@ -1036,12 +1173,14 @@ mkExp (SApp True name args) =
10361173 )
10371174 <$> mkMethodClosure name args
10381175mkExp (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)))
1042− EqualA
1043− rhs)
1176+ mkLet var old new
1177+mkExp (SUpdate var exp) =
1178+ (\ rhs varPos -> Assign (ArrayLhs $ ArrayIndex (contextArray var) (Lit $ Int varPos))
1179+ EqualA
1180+ rhs
1181+ )
10441182 <$> mkExp exp
1183+ <*> varPos var
10451184mkExp (SCon conId name args) =
10461185 (\ args -> InstanceCreation []
10471186 idrisObjectType
@@ -1050,7 +1189,7 @@ mkExp (SCon conId name args) =
10501189 <$> mapM (mkExp .SV) args
10511190mkExp (SCase var alts) = mkCase False var alts
10521191mkExp (SChkCase var alts) = mkCase True var alts
1053−mkExp (SProj var i) = return $ mkProjection var i
1192+mkExp (SProj var i) = mkProjection var i
10541193mkExp (SConst (I x)) = return $ mkPrimitive "Integer" (Int (toInteger x))
10551194mkExp (SConst (BI x)) =
10561195 return $ InstanceCreation []
@@ -1077,226 +1216,235 @@ mkExp (SConst (PtrType)) = return $ mkClass objectType
10771216mkExp (SConst (VoidType)) = return $ mkClass voidType
10781217mkExp (SConst (Forgot)) = return $ mkClass objectType
10791218mkExp (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
1219+mkExp (SOp LPlus args) = mkBinOpExp integerType Add args
1220+mkExp (SOp LMinus args) = mkBinOpExp integerType Sub args
1221+mkExp (SOp LTimes args) = mkBinOpExp integerType Mult args
1222+mkExp (SOp LDiv args) = mkBinOpExp integerType Div args
1223+mkExp (SOp LMod args) = mkBinOpExp integerType Rem args
1224+mkExp (SOp LAnd args) = mkBinOpExp integerType And args
1225+mkExp (SOp LOr args) = mkBinOpExp integerType Or args
1226+mkExp (SOp LXOr args) = mkBinOpExp integerType Xor args
10881227mkExp (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
1228+ PreBitCompl <$> mkVarAccess (Just $ integerType) var
1229+mkExp (SOp LSHL args) = mkBinOpExp integerType LShift args
1230+mkExp (SOp LSHR args) = mkBinOpExp integerType RShift args
10921231mkExp (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
1232+ mkMethodOpChain1 (mkBoolToNumber integerType) objectType "equals" args
1233+mkExp (SOp LLt args) = mkLogicalBinOpExp integerType LThan args
1234+mkExp (SOp LLe args) = mkLogicalBinOpExp integerType LThanE args
1235+mkExp (SOp LGt args) = mkLogicalBinOpExp integerType GThan args
1236+mkExp (SOp LGe args) = mkLogicalBinOpExp integerType GThanE args
1237+mkExp (SOp LFPlus args) = mkBinOpExp doubleType Add args
1238+mkExp (SOp LFMinus args) = mkBinOpExp doubleType Sub args
1239+mkExp (SOp LFTimes args) = mkBinOpExp doubleType Mult args
1240+mkExp (SOp LFDiv args) = mkBinOpExp doubleType Div args
11021241mkExp (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
1242+ mkMethodOpChain1 (mkBoolToNumber doubleType) doubleType "equals" args
1243+mkExp (SOp LFLt args) = mkLogicalBinOpExp integerType LThan args
1244+mkExp (SOp LFLe args) = mkLogicalBinOpExp integerType LThanE args
1245+mkExp (SOp LFGt args) = mkLogicalBinOpExp integerType GThan args
1246+mkExp (SOp LFGe args) = mkLogicalBinOpExp integerType GThanE args
1247+mkExp (SOp LBPlus args) = mkMethodOpChain1 id bigIntegerType "add" args
1248+mkExp (SOp LBMinus args) = mkMethodOpChain1 id bigIntegerType "subtract" args
1249+mkExp (SOp LBTimes args) = mkMethodOpChain1 id bigIntegerType "multiply" args
1250+mkExp (SOp LBDiv args) = mkMethodOpChain1 id bigIntegerType "divide" args
1251+mkExp (SOp LBMod args) = mkMethodOpChain1 id bigIntegerType "mod" args
11131252mkExp (SOp LBEq args) =
1114− return $ mkMethodOpChain1 (mkBoolToNumber bigIntegerType) bigIntegerType "equals" args
1253+ mkMethodOpChain1 (mkBoolToNumber bigIntegerType) bigIntegerType "equals" args
11151254mkExp (SOp LBLt args) =
1116− return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1117− . mkPartialOrder SLt
1118− )
1119− bigIntegerType
1120− "compare"
1121− args
1255+ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1256+ . mkPartialOrder SLt
1257+ )
1258+ bigIntegerType
1259+ "compare"
1260+ args
11221261mkExp (SOp LBLe args) =
1123− return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1124− . mkPartialOrder SLe
1125− )
1126− bigIntegerType
1127− "compare"
1128− args
1262+ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1263+ . mkPartialOrder SLe
1264+ )
1265+ bigIntegerType
1266+ "compare"
1267+ args
11291268mkExp (SOp LBGt args) =
1130− return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1131− . mkPartialOrder SGt
1132− )
1133− bigIntegerType
1134− "compare"
1135− args
1269+ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1270+ . mkPartialOrder SGt
1271+ )
1272+ bigIntegerType
1273+ "compare"
1274+ args
11361275mkExp (SOp LBGe args) =
1137− return $ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1138− . mkPartialOrder SGe
1139− )
1140− bigIntegerType
1141− "compare"
1142− args
1276+ mkMethodOpChain1 ( mkBoolToNumber bigIntegerType
1277+ . mkPartialOrder SGe
1278+ )
1279+ bigIntegerType
1280+ "compare"
1281+ args
11431282mkExp (SOp LStrConcat args) =
1144− return $ mkMethodOpChain (\ exp ->
1145− InstanceCreation []
1146− (ClassType [(Ident "StringBuilder", [])])
1147− [exp]
1148− Nothing
1149− )
1150− (\ exp -> MethodInv $ PrimaryMethodCall exp [] (Ident "toString") [])
1151− stringType
1152− "append"
1153− args
1283+ mkMethodOpChain (\ exp -> InstanceCreation []
1284+ (ClassType [(Ident "StringBuilder", [])])
1285+ [exp]
1286+ Nothing
1287+ )
1288+ (\ exp -> MethodInv $ PrimaryMethodCall exp [] (Ident "toString") [])
1289+ stringType
1290+ "append"
1291+ args
11541292mkExp (SOp LStrLt args@[_, _]) =
1155− return $ mkMethodOpChain1 ( mkBoolToNumber integerType
1156− . mkPartialOrder SLt
1157− )
1158− stringType
1159− "compare"
1160− args
1293+ mkMethodOpChain1 ( mkBoolToNumber integerType
1294+ . mkPartialOrder SLt
1295+ )
1296+ stringType
1297+ "compare"
1298+ args
11611299mkExp (SOp LStrEq args@[_, _]) =
1162− return $ mkMethodOpChain1 ( mkBoolToNumber integerType)
1163− stringType
1164− "equals"
1165− args
1300+ mkMethodOpChain1 ( mkBoolToNumber integerType)
1301+ stringType
1302+ "equals"
1303+ args
11661304mkExp (SOp LStrLen [arg]) =
1167− return . MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) arg) [] (Ident "length") []
1305+ (\ var -> MethodInv $ PrimaryMethodCall var [] (Ident "length") [])
1306+ <$> mkVarAccess (Just stringType) arg
11681307mkExp (SOp LIntFloat [arg]) =
1169− return $ mkPrimitiveCast integerType doubleType arg
1308+ mkPrimitiveCast integerType doubleType arg
11701309mkExp (SOp LFloatInt [arg]) =
1171− return $ mkPrimitiveCast doubleType integerType arg
1310+ mkPrimitiveCast doubleType integerType arg
11721311mkExp (SOp LIntStr [arg]) =
1173− return $ mkToString integerType arg
1312+ mkToString integerType arg
11741313mkExp (SOp LStrInt [arg]) =
1175− return $ mkPrimitiveCast stringType integerType arg
1314+ mkPrimitiveCast stringType integerType arg
11761315mkExp (SOp LFloatStr [arg]) =
1177− return $ mkToString doubleType arg
1316+ mkToString doubleType arg
11781317mkExp (SOp LStrFloat [arg]) =
1179− return $ mkPrimitiveCast doubleType stringType arg
1318+ mkPrimitiveCast doubleType stringType arg
11801319mkExp (SOp LIntBig [arg]) =
1181− return $ mkPrimitiveCast integerType bigIntegerType arg
1320+ mkPrimitiveCast integerType bigIntegerType arg
11821321mkExp (SOp LBigInt [arg]) =
1183− return $ mkPrimitiveCast bigIntegerType integerType arg
1322+ mkPrimitiveCast bigIntegerType integerType arg
11841323mkExp (SOp LStrBig [arg]) =
1185− return $ InstanceCreation [] bigIntegerType [mkVarAccess (Just stringType) arg] Nothing
1324+ (\ var -> InstanceCreation [] bigIntegerType [var] Nothing)
1325+ <$> mkVarAccess (Just stringType) arg
11861326mkExp (SOp LBigStr [arg]) =
1187− return $ mkToString bigIntegerType arg
1327+ mkToString bigIntegerType arg
11881328mkExp (SOp LChInt [arg]) =
1189− return $ mkVarAccess (Just integerType) arg
1329+ mkVarAccess (Just integerType) arg
11901330mkExp (SOp LIntCh [arg]) =
1191− return $ mkVarAccess (Just integerType) arg
1331+ mkVarAccess (Just integerType) arg
11921332mkExp (SOp LPrintNum [arg]) =
1193− return $ mkSystemOutPrint (mkVarAccess Nothing arg)
1333+ mkSystemOutPrint <$> (mkVarAccess Nothing arg)
11941334mkExp (SOp LPrintStr [arg]) =
1195− return $ mkSystemOutPrint (mkVarAccess (Just stringType) arg)
1335+ mkSystemOutPrint <$> (mkVarAccess (Just stringType) arg)
11961336mkExp (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
1337+mkExp (SOp LB8Lt args) = mkLogicalBinOpExp byteType LThan args
1338+mkExp (SOp LB8Lte args) = mkLogicalBinOpExp byteType LThanE args
11991339mkExp (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
1340+ mkMethodOpChain1 (mkBoolToNumber byteType) byteType "equals" args
1341+mkExp (SOp LB8Gt args) = mkLogicalBinOpExp byteType GThan args
1342+mkExp (SOp LB8Gte args) = mkLogicalBinOpExp byteType GThanE args
1343+mkExp (SOp LB8Plus args) = mkBinOpExp byteType Add args
1344+mkExp (SOp LB8Minus args) = mkBinOpExp byteType Sub args
1345+mkExp (SOp LB8Times args) = mkBinOpExp byteType Mult args
1346+mkExp (SOp LB8UDiv args) = mkBinOpExpConv "shortValue" ShortT byteType Div args
1347+mkExp (SOp LB8SDiv args) = mkBinOpExp byteType Div args
1348+mkExp (SOp LB8URem args) = mkBinOpExpConv "shortValue" ShortT byteType Rem args
1349+mkExp (SOp LB8SRem args) = mkBinOpExp byteType Rem args
1350+mkExp (SOp LB8Shl args) = mkBinOpExp byteType LShift args
1351+mkExp (SOp LB8LShr args) = mkBinOpExp byteType RRShift args
1352+mkExp (SOp LB8AShr args) = mkBinOpExp byteType RShift args
1353+mkExp (SOp LB8And args) = mkBinOpExp byteType And args
1354+mkExp (SOp LB8Or args) = mkBinOpExp byteType Or args
1355+mkExp (SOp LB8Xor args) = mkBinOpExp byteType Xor args
12161356mkExp (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
1357+ PreBitCompl <$> mkVarAccess (Just $ byteType) var
1358+mkExp (SOp LB8Z16 [var]) = mkZeroExt "shortValue" 8 byteType shortType var
1359+mkExp (SOp LB8Z32 [var]) = mkZeroExt "intValue" 8 byteType integerType var
1360+mkExp (SOp LB8Z64 [var]) = mkZeroExt "longValue" 8 byteType longType var
1361+mkExp (SOp LB8S16 [var]) = mkSignedExt "shortValue" byteType shortType var
1362+mkExp (SOp LB8S32 [var]) = mkSignedExt "intValue" byteType integerType var
1363+mkExp (SOp LB8S64 [var]) = mkSignedExt "longValue" byteType longType var
1364+mkExp (SOp LB16Lt args) = mkLogicalBinOpExp shortType LThan args
1365+mkExp (SOp LB16Lte args) = mkLogicalBinOpExp shortType LThanE args
12261366mkExp (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
1367+ mkMethodOpChain1 (mkBoolToNumber shortType) shortType "equals" args
1368+mkExp (SOp LB16Gt args) = mkLogicalBinOpExp shortType GThan args
1369+mkExp (SOp LB16Gte args) = mkLogicalBinOpExp shortType GThanE args
1370+mkExp (SOp LB16Plus args) = mkBinOpExp shortType Add args
1371+mkExp (SOp LB16Minus args) = mkBinOpExp shortType Sub args
1372+mkExp (SOp LB16Times args) = mkBinOpExp shortType Mult args
1373+mkExp (SOp LB16UDiv args) = mkBinOpExpConv "intValue" IntT shortType Div args
1374+mkExp (SOp LB16SDiv args) = mkBinOpExp shortType Div args
1375+mkExp (SOp LB16URem args) = mkBinOpExpConv "intValue" IntT shortType Rem args
1376+mkExp (SOp LB16SRem args) = mkBinOpExp shortType Rem args
1377+mkExp (SOp LB16Shl args) = mkBinOpExp shortType LShift args
1378+mkExp (SOp LB16LShr args) = mkBinOpExp shortType RRShift args
1379+mkExp (SOp LB16AShr args) = mkBinOpExp shortType RShift args
1380+mkExp (SOp LB16And args) = mkBinOpExp shortType And args
1381+mkExp (SOp LB16Or args) = mkBinOpExp shortType Or args
1382+mkExp (SOp LB16Xor args) = mkBinOpExp shortType Xor args
12431383mkExp (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
1384+ PreBitCompl <$> mkVarAccess (Just $ shortType) var
1385+mkExp (SOp LB16Z32 [var]) = mkZeroExt "intValue" 16 shortType integerType var
1386+mkExp (SOp LB16Z64 [var]) = mkZeroExt "longValue" 16 shortType longType var
1387+mkExp (SOp LB16S32 [var]) = mkSignedExt "intValue" shortType integerType var
1388+mkExp (SOp LB16S64 [var]) = mkSignedExt "longValue" shortType longType var
12491389mkExp (SOp LB16T8 [var]) =
1250− return . MethodInv $
1251− TypeMethodCall (J.Name [Ident "Byte"])
1252− []
1253− (Ident "valueOf")
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
1390+ (\ var -> MethodInv $
1391+ TypeMethodCall (J.Name [Ident "Byte"])
1392+ []
1393+ (Ident "valueOf")
1394+ [ MethodInv
1395+ $ PrimaryMethodCall var [] (Ident "byteValue") [] ]
1396+ )
1397+ <$> mkVarAccess (Just $ shortType) var
1398+mkExp (SOp LB32Lt args) = mkLogicalBinOpExp integerType LThan args
1399+mkExp (SOp LB32Lte args) = mkLogicalBinOpExp integerType LThanE args
12571400mkExp (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
1401+ mkMethodOpChain1 (mkBoolToNumber integerType) integerType "equals" args
1402+mkExp (SOp LB32Gt args) = mkLogicalBinOpExp integerType GThan args
1403+mkExp (SOp LB32Gte args) = mkLogicalBinOpExp integerType GThanE args
1404+mkExp (SOp LB32Plus args) = mkBinOpExp integerType Add args
1405+mkExp (SOp LB32Minus args) = mkBinOpExp integerType Sub args
1406+mkExp (SOp LB32Times args) = mkBinOpExp integerType Mult args
1407+mkExp (SOp LB32UDiv args) = mkBinOpExpConv "longValue" LongT integerType Div args
1408+mkExp (SOp LB32SDiv args) = mkBinOpExp integerType Div args
1409+mkExp (SOp LB32URem args) = mkBinOpExpConv "longValue" LongT integerType Rem args
1410+mkExp (SOp LB32SRem args) = mkBinOpExp integerType Rem args
1411+mkExp (SOp LB32Shl args) = mkBinOpExp integerType LShift args
1412+mkExp (SOp LB32LShr args) = mkBinOpExp integerType RRShift args
1413+mkExp (SOp LB32AShr args) = mkBinOpExp integerType RShift args
1414+mkExp (SOp LB32And args) = mkBinOpExp integerType And args
1415+mkExp (SOp LB32Or args) = mkBinOpExp integerType Or args
1416+mkExp (SOp LB32Xor args) = mkBinOpExp integerType Xor args
12741417mkExp (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
1418+ PreBitCompl <$> mkVarAccess (Just $ integerType) var
1419+mkExp (SOp LB32Z64 [var]) = mkZeroExt "longValue" 32 integerType longType var
1420+mkExp (SOp LB32S64 [var]) = mkSignedExt "longValue" integerType longType var
12781421mkExp (SOp LB32T8 [var]) =
1279− return . MethodInv $
1280− TypeMethodCall (J.Name [Ident "Byte"])
1281− []
1282− (Ident "valueOf")
1283− [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ integerType) var) [] (Ident "byteValue") [] ]
1422+ (\ var -> MethodInv
1423+ $ TypeMethodCall (J.Name [Ident "Byte"])
1424+ []
1425+ (Ident "valueOf")
1426+ [ MethodInv $ PrimaryMethodCall var [] (Ident "byteValue") [] ]
1427+ )
1428+ <$> mkVarAccess (Just $ integerType) var
12841429mkExp (SOp LB32T16 [var]) =
1285− return . MethodInv $
1286− TypeMethodCall (J.Name [Ident "Short"])
1287− []
1288− (Ident "valueOf")
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
1430+ (\ var -> MethodInv
1431+ $ TypeMethodCall (J.Name [Ident "Short"])
1432+ []
1433+ (Ident "valueOf")
1434+ [ MethodInv $ PrimaryMethodCall var [] (Ident "shortValue") [] ]
1435+ )
1436+ <$> mkVarAccess (Just $ integerType) var
1437+mkExp (SOp LB64Lt args) = mkLogicalBinOpExp longType LThan args
1438+mkExp (SOp LB64Lte args) = mkLogicalBinOpExp longType LThanE args
12921439mkExp (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)) =
1440+ mkMethodOpChain1 (mkBoolToNumber longType) longType "equals" args
1441+mkExp (SOp LB64Gt args) = mkLogicalBinOpExp longType GThan args
1442+mkExp (SOp LB64Gte args) = mkLogicalBinOpExp longType GThanE args
1443+mkExp (SOp LB64Plus args) = mkBinOpExp longType Add args
1444+mkExp (SOp LB64Minus args) = mkBinOpExp longType Sub args
1445+mkExp (SOp LB64Times args) = mkBinOpExp longType Mult args
1446+mkExp (SOp LB64UDiv (arg:args)) = do
1447+ (arg:args) <- mapM (mkVarAccess (Just longType)) (arg:args)
13001448 return $ foldl (\ exp arg ->
13011449 MethodInv $ PrimaryMethodCall
13021450 ( MethodInv $ PrimaryMethodCall
@@ -1310,17 +1458,18 @@ mkExp (SOp LB64UDiv (arg:args)) =
13101458 [ MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
13111459 []
13121460 (Ident "valueOf")
1313− [ (mkVarAccess (Just longType) arg) ]
1461+ [ arg ]
13141462 ]
13151463 )
13161464 []
13171465 (Ident "longValue")
13181466 []
13191467 )
1320− (mkVarAccess (Just longType) arg)
1468+ arg
13211469 args
1322−mkExp (SOp LB64SDiv args) = return $ mkBinOpExp longType Div args
1323−mkExp (SOp LB64URem (arg:args)) =
1470+mkExp (SOp LB64SDiv args) = mkBinOpExp longType Div args
1471+mkExp (SOp LB64URem (arg:args)) = do
1472+ (arg:args) <- mapM (mkVarAccess (Just longType)) (arg:args)
13241473 return $ foldl (\ exp arg ->
13251474 MethodInv $ PrimaryMethodCall
13261475 ( MethodInv $ PrimaryMethodCall
@@ -1334,69 +1483,79 @@ mkExp (SOp LB64URem (arg:args)) =
13341483 [ MethodInv $ TypeMethodCall (J.Name [Ident "BigInteger"])
13351484 []
13361485 (Ident "valueOf")
1337− [ (mkVarAccess (Just longType) arg) ]
1486+ [ arg ]
13381487 ]
13391488 )
13401489 []
13411490 (Ident "longValue")
13421491 []
13431492 )
1344− (mkVarAccess (Just longType) arg)
1493+ arg
13451494 args
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
1495+mkExp (SOp LB64SRem args) = mkBinOpExp longType Rem args
1496+mkExp (SOp LB64Shl args) = mkBinOpExp longType LShift args
1497+mkExp (SOp LB64LShr args) = mkBinOpExp longType RRShift args
1498+mkExp (SOp LB64AShr args) = mkBinOpExp longType RShift args
1499+mkExp (SOp LB64And args) = mkBinOpExp longType And args
1500+mkExp (SOp LB64Or args) = mkBinOpExp longType Or args
1501+mkExp (SOp LB64Xor args) = mkBinOpExp longType Xor args
13531502mkExp (SOp LB64Compl [var]) =
1354− return $ PreBitCompl (mkVarAccess (Just $ longType) var)
1503+ PreBitCompl <$> mkVarAccess (Just $ longType) var
13551504mkExp (SOp LB64T8 [var]) =
1356− return . MethodInv $
1357− TypeMethodCall (J.Name [Ident "Byte"])
1358− []
1359− (Ident "valueOf")
1360− [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "byteValue") [] ]
1505+ (\ var -> MethodInv
1506+ $ TypeMethodCall (J.Name [Ident "Byte"])
1507+ []
1508+ (Ident "valueOf")
1509+ [ MethodInv
1510+ $ PrimaryMethodCall var [] (Ident "byteValue") []
1511+ ]
1512+ )
1513+ <$> mkVarAccess (Just $ longType) var
13611514mkExp (SOp LB64T16 [var]) =
1362− return . MethodInv $
1363− TypeMethodCall (J.Name [Ident "Short"])
1364− []
1365− (Ident "valueOf")
1366− [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "shortValue") [] ]
1515+ (\ var -> MethodInv
1516+ $ TypeMethodCall (J.Name [Ident "Short"])
1517+ []
1518+ (Ident "valueOf")
1519+ [ MethodInv $ PrimaryMethodCall var [] (Ident "shortValue") [] ]
1520+ )
1521+ <$> mkVarAccess (Just $ longType) var
13671522mkExp (SOp LB64T32 [var]) =
1368− return . MethodInv $
1369− TypeMethodCall (J.Name [Ident "Integer"])
1370− []
1371− (Ident "valueOf")
1372− [ MethodInv $ PrimaryMethodCall (mkVarAccess (Just $ longType) var) [] (Ident "intValue") [] ]
1523+ (\ var -> MethodInv
1524+ $ TypeMethodCall (J.Name [Ident "Integer"])
1525+ []
1526+ (Ident "valueOf")
1527+ [ MethodInv $ PrimaryMethodCall var [] (Ident "intValue") [] ]
1528+ )
1529+ <$> mkVarAccess (Just $ longType) var
13731530mkExp (SOp LIntB8 [arg]) = mkExp (SOp LB32T8 [arg])
13741531mkExp (SOp LIntB16 [arg]) = mkExp (SOp LB32T16 [arg])
13751532mkExp (SOp LIntB32 [arg]) = mkExp (SV arg)
13761533mkExp (SOp LIntB64 [arg]) = mkExp (SOp LB32S64 [arg])
13771534mkExp (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)
1535+mkExp (SOp LFExp [arg]) = mkMathFun "exp" arg
1536+mkExp (SOp LFLog [arg]) = mkMathFun "log" arg
1537+mkExp (SOp LFSin [arg]) = mkMathFun "sin" arg
1538+mkExp (SOp LFCos [arg]) = mkMathFun "cos" arg
1539+mkExp (SOp LFTan [arg]) = mkMathFun "tan" arg
1540+mkExp (SOp LFASin [arg]) = mkMathFun "asin" arg
1541+mkExp (SOp LFACos [arg]) = mkMathFun "acos" arg
1542+mkExp (SOp LFATan [arg]) = mkMathFun "atan" arg
1543+mkExp (SOp LFSqrt [arg]) = mkMathFun "sqrt" arg
1544+mkExp (SOp LFFloor [arg]) = mkMathFun "floor" arg
1545+mkExp (SOp LFCeil [arg]) = mkMathFun "ceil" arg
1546+mkExp (SOp LStrHead [arg]) = mkStringAtIndex arg (Lit $ Int 0)
13901547mkExp (SOp LStrTail [arg]) =
1391− return . MethodInv $ PrimaryMethodCall (mkVarAccess (Just stringType) arg)
1548+ (\ var -> MethodInv $ PrimaryMethodCall (var)
13921549 []
13931550 (Ident "substring")
13941551 [Lit $ Int 1]
1552+ )
1553+ <$> mkVarAccess (Just stringType) arg
13951554mkExp (SOp LStrCons [c, cs]) =
1396− return . MethodInv $
1555+ (\ cVar csVar -> MethodInv $
13971556 PrimaryMethodCall ( MethodInv $ PrimaryMethodCall (InstanceCreation []
13981557 (ClassType [(Ident "StringBuilder", [])])
1399− [mkVarAccess (Just stringType) cs]
1558+ [csVar]
14001559 Nothing
14011560 )
14021561 []
@@ -1404,7 +1563,7 @@ mkExp (SOp LStrCons [c, cs]) =
14041563 [ Lit $ Int 0,
14051564 Cast (PrimType CharT)
14061565 (MethodInv $ PrimaryMethodCall
1407− (mkVarAccess (Just integerType) c)
1566+ (cVar)
14081567 []
14091568 (Ident "intValue")
14101569 []
@@ -1414,12 +1573,15 @@ mkExp (SOp LStrCons [c, cs]) =
14141573 []
14151574 (Ident "toString")
14161575 []
1417−mkExp (SOp LStrIndex [str, i]) = return $ mkStringAtIndex str (mkVarAccess (Just integerType) i)
1576+ )
1577+ <$> mkVarAccess (Just integerType) c
1578+ <*> mkVarAccess (Just stringType) cs
1579+mkExp (SOp LStrIndex [str, i]) = mkVarAccess (Just integerType) i >>= mkStringAtIndex str
14181580mkExp (SOp LStrRev [str]) =
1419− return . MethodInv $
1581+ (\ var -> MethodInv $
14201582 PrimaryMethodCall ( MethodInv $ PrimaryMethodCall (InstanceCreation []
14211583 (ClassType [(Ident "StringBuffer", [])])
1422− [mkVarAccess (Just stringType) str]
1584+ [var]
14231585 Nothing
14241586 )
14251587 []
@@ -1429,13 +1591,15 @@ mkExp (SOp LStrRev [str]) =
14291591 []
14301592 (Ident "toString")
14311593 []
1594+ )
1595+ <$> mkVarAccess (Just stringType) str
14321596mkExp (SOp LStdIn []) = return $ mkSystemStd In
14331597mkExp (SOp LStdOut []) = return $ mkSystemStd Out
14341598mkExp (SOp LStdErr []) = return $ mkSystemStd Err
14351599mkExp (SOp LFork [arg]) = mkThread arg
14361600mkExp (SOp LPar [arg]) = mkExp (SV arg)
14371601mkExp (SOp LVMPtr []) =
1438− return . MethodInv $ TypeMethodCall (J.Name [Ident "Thread"]) [] (Ident "currentThread") []
1602+ return $ MethodInv $ TypeMethodCall (J.Name [Ident "Thread"]) [] (Ident "currentThread") []
14391603mkExp (SOp LNoOp args) = mkExp . SV $ last args
14401604mkExp (SNothing) = return $ Lit Null
14411605mkExp (SError err) =