on master help sign in
Merge pull request #1294 from JanBessai/JavaBackendFixes
2b183c8Edwin Brady
CONTRIBUTORS+1
@@ -3,6 +3,7 @@ Thanks to the following for their help and contributions:
3Ozgur Akgun
3Ozgur Akgun
4Ahmad Salim Al-Sibahi
4Ahmad Salim Al-Sibahi
5Edward Chadwick Amsden
5Edward Chadwick Amsden
6+Jan Bessai
6Nicola Botta
7Nicola Botta
7Edwin Brady
8Edwin Brady
8David Raymond Christiansen
9David Raymond Christiansen
src/IRTS/CodegenJava.hs+8−5
@@ -169,7 +169,7 @@ jarHeader =
169 ++ "if test -n \"$JAVA_HOME\"; then\n"
169 ++ "if test -n \"$JAVA_HOME\"; then\n"
170 ++ " java=\"$JAVA_HOME/bin/java\"\n"
170 ++ " java=\"$JAVA_HOME/bin/java\"\n"
171 ++ "fi\n"
171 ++ "fi\n"
172− ++ "exec \"$java\" $java_args -jar $MYSELF \"$@\""
172+ ++ "exec \"$java\" $java_args -jar $MYSELF \"$@\"\n"
173 ++ "exit 1\n"
173 ++ "exit 1\n"
174
174
175-----------------------------------------------------------------------
175-----------------------------------------------------------------------
@@ -312,6 +312,7 @@ mkCompilationUnit globalInit defs hdrs out = do
312 , ImportDecl True idrisPrelude True
312 , ImportDecl True idrisPrelude True
313 , ImportDecl False bigInteger False
313 , ImportDecl False bigInteger False
314 , ImportDecl False runtimeException False
314 , ImportDecl False runtimeException False
315+ , ImportDecl False byteBuffer False
315 ] ++ otherHdrs
316 ] ++ otherHdrs
316 )
317 )
317 <$> mkTypeDecl clsName globalInit defs
318 <$> mkTypeDecl clsName globalInit defs
@@ -320,6 +321,7 @@ mkCompilationUnit globalInit defs hdrs out = do
320 idrisPrelude = J.Name $ map Ident ["org", "idris", "rts", "Prelude"]
321 idrisPrelude = J.Name $ map Ident ["org", "idris", "rts", "Prelude"]
321 bigInteger = J.Name $ map Ident ["java", "math", "BigInteger"]
322 bigInteger = J.Name $ map Ident ["java", "math", "BigInteger"]
322 runtimeException = J.Name $ map Ident ["java", "lang", "RuntimeException"]
323 runtimeException = J.Name $ map Ident ["java", "lang", "RuntimeException"]
324+ byteBuffer = J.Name $ map Ident ["java", "nio", "ByteBuffer"]
323 otherHdrs = map ( (\ name -> ImportDecl False name False)
325 otherHdrs = map ( (\ name -> ImportDecl False name False)
324 . J.Name
326 . J.Name
325 . map (Ident . T.unpack)
327 . map (Ident . T.unpack)
@@ -386,9 +388,7 @@ mkMainMethod =
386 "main"
388 "main"
387 [FormalParam [] (array stringType) False (VarId $ Ident "args")]
389 [FormalParam [] (array stringType) False (VarId $ Ident "args")]
388 $ Block [ BlockStmt . ExpStmt
390 $ Block [ BlockStmt . ExpStmt
389− $ call "idris_initArgs" [ (threadType ~> "currentThread") []
391+ $ call "idris_initArgs" [ jConst "args" ]
390− , jConst "args"
391− ]
392 , BlockStmt . ExpStmt $ call (mangle' (sMN 0 "runMain")) []
392 , BlockStmt . ExpStmt $ call (mangle' (sMN 0 "runMain")) []
393 ]
393 ]
394
394
@@ -484,6 +484,8 @@ mkExp pp (SOp LFork [arg]) =
484 (mkThread arg) >>= ppExp pp
484 (mkThread arg) >>= ppExp pp
485mkExp pp (SOp LPar [arg]) =
485mkExp pp (SOp LPar [arg]) =
486 (Nothing <>@! arg) >>= ppExp pp
486 (Nothing <>@! arg) >>= ppExp pp
487+mkExp pp (SOp LRegisterPtr [ptr, i]) =
488+ (Nothing <>@! ptr) >>= ppExp pp
487mkExp pp (SOp LNoOp args) =
489mkExp pp (SOp LNoOp args) =
488 (Nothing <>@! (last args)) >>= ppExp pp
490 (Nothing <>@! (last args)) >>= ppExp pp
489mkExp pp (SOp LNullPtr args) =
491mkExp pp (SOp LNullPtr args) =
@@ -707,6 +709,7 @@ mkConstant c@(AType x) = ClassLit (Just $ box (constType c))
707mkConstant c@(StrType ) = ClassLit (Just $ stringType)
709mkConstant c@(StrType ) = ClassLit (Just $ stringType)
708mkConstant c@(PtrType ) = ClassLit (Just $ objectType)
710mkConstant c@(PtrType ) = ClassLit (Just $ objectType)
709mkConstant c@(ManagedPtrType) = ClassLit (Just $ objectType)
711mkConstant c@(ManagedPtrType) = ClassLit (Just $ objectType)
712+mkConstant c@(BufferType) = ClassLit (Just $ bufferType)
710mkConstant c@(VoidType ) = ClassLit (Just $ voidType)
713mkConstant c@(VoidType ) = ClassLit (Just $ voidType)
711mkConstant c@(Forgot ) = ClassLit (Just $ objectType)
714mkConstant c@(Forgot ) = ClassLit (Just $ objectType)
712
715
@@ -744,7 +747,7 @@ mkForeign pp (LANG_JAVA callType) resTy text params
744
747
745mkPrimitiveFunction :: PrimFn -> [LVar] -> CodeGeneration Exp
748mkPrimitiveFunction :: PrimFn -> [LVar] -> CodeGeneration Exp
746mkPrimitiveFunction op args =
749mkPrimitiveFunction op args =
747− (\ args -> (primFnType ~> opName op) args)
750+ (\ args -> (primFnType ~> opName op) (endiannessArguments op ++ args))
748 <$> sequence (zipWith (\ a t -> (Just t) <>@! a) args (sourceTypes op))
751 <$> sequence (zipWith (\ a t -> (Just t) <>@! a) args (sourceTypes op))
749
752
750mkThread :: LVar -> CodeGeneration Exp
753mkThread :: LVar -> CodeGeneration Exp
src/IRTS/Java/JTypes.hs+31
@@ -33,6 +33,9 @@ doubleType = PrimType DoubleT
33array :: J.Type -> J.Type
33array :: J.Type -> J.Type
34array t = RefType . ArrayType $ t
34array t = RefType . ArrayType $ t
35
35
36+addressType :: J.Type
37+addressType = longType
38+
36-----------------------------------------------------------------------
39-----------------------------------------------------------------------
37-- Boxed types
40-- Boxed types
38
41
@@ -48,6 +51,10 @@ stringType :: J.Type
48stringType =
51stringType =
49 RefType . ClassRefType $ ClassType [(Ident "String", [])]
52 RefType . ClassRefType $ ClassType [(Ident "String", [])]
50
53
54+bufferType :: J.Type
55+bufferType =
56+ RefType . ClassRefType $ ClassType [(Ident "ByteBuffer", [])]
57+
51threadType :: J.Type
58threadType :: J.Type
52threadType =
59threadType =
53 RefType . ClassRefType $ ClassType [(Ident "Thread", [])]
60 RefType . ClassRefType $ ClassType [(Ident "Thread", [])]
@@ -132,6 +139,9 @@ runtimeExceptionType :: J.Type
132runtimeExceptionType =
139runtimeExceptionType =
133 RefType . ClassRefType $ ClassType [(Ident "RuntimeException", [])]
140 RefType . ClassRefType $ ClassType [(Ident "RuntimeException", [])]
134
141
142+-----------------------------------------------------------------------
143+-- Integer types
144+
135nativeTyToJType :: NativeTy -> J.Type
145nativeTyToJType :: NativeTy -> J.Type
136nativeTyToJType IT8 = byteType
146nativeTyToJType IT8 = byteType
137nativeTyToJType IT16 = shortType
147nativeTyToJType IT16 = shortType
@@ -219,6 +229,7 @@ opName x
219 | (LFloatInt to) <- x = "LFloatInt" ++ (suffixFor to)
229 | (LFloatInt to) <- x = "LFloatInt" ++ (suffixFor to)
220 | (LStrInt to) <- x = "LStrInt" ++ (suffixFor to)
230 | (LStrInt to) <- x = "LStrInt" ++ (suffixFor to)
221 | (LChInt to) <- x = "LChInt" ++ (suffixFor to)
231 | (LChInt to) <- x = "LChInt" ++ (suffixFor to)
232+ | (LPeek to _) <- x = "LPeek" ++ (suffixFor to)
222 | otherwise = takeWhile ((/=) ' ') $ show x
233 | otherwise = takeWhile ((/=) ' ') $ show x
223 where
234 where
224 suffixFor (ITFixed nt) = show nt
235 suffixFor (ITFixed nt) = show nt
@@ -291,10 +302,30 @@ sourceTypes (LStrRev) = [stringType]
291sourceTypes (LStdIn) = []
302sourceTypes (LStdIn) = []
292sourceTypes (LStdOut) = []
303sourceTypes (LStdOut) = []
293sourceTypes (LStdErr) = []
304sourceTypes (LStdErr) = []
305+sourceTypes (LAllocate) = [addressType]
306+sourceTypes (LAppendBuffer) =
307+ [bufferType, addressType, addressType, addressType, addressType, bufferType]
308+sourceTypes (LSystemInfo) = [integerType]
309+sourceTypes (LAppend nt _) = [bufferType, addressType, addressType, intTyToJType nt]
310+sourceTypes (LPeek _ _) = [bufferType, addressType]
294sourceTypes (LFork) = [objectType]
311sourceTypes (LFork) = [objectType]
295sourceTypes (LPar) = [objectType]
312sourceTypes (LPar) = [objectType]
296sourceTypes (LVMPtr) = []
313sourceTypes (LVMPtr) = []
297sourceTypes (LNullPtr) = [objectType]
314sourceTypes (LNullPtr) = [objectType]
298sourceTypes (LNoOp) = repeat objectType
315sourceTypes (LNoOp) = repeat objectType
299
316
317+-----------------------------------------------------------------------
318+-- Endianess markers
300
319
320+endiannessConstant :: Endianness -> Exp
321+endiannessConstant c =
322+ ExpName . Name . map Ident $ ["java", "nio", "ByteOrder", endiannessConstant' c]
323+ where
324+ endiannessConstant' BE = "BIG_ENDIAN"
325+ endiannessConstant' LE = "LITTLE_ENDIAN"
326+ endiannessConstant' (IRTS.Lang.Native) = endiannessConstant' BE
327+
328+endiannessArguments :: PrimFn -> [Exp]
329+endiannessArguments (LAppend _ end) = [endiannessConstant end]
330+endiannessArguments (LPeek _ end) = [endiannessConstant end]
331+endiannessArguments _ = []
src/IRTS/Java/Pom.hs+7−1
@@ -3,6 +3,9 @@ module IRTS.Java.Pom (pomString) where
3import Data.List (unfoldr)
3import Data.List (unfoldr)
4import Text.XML.Light
4import Text.XML.Light
5
5
6+-----------------------------------------------------------------------
7+-- String <-> XML processing
8+
6uattr :: String -> String -> Attr
9uattr :: String -> String -> Attr
7uattr k v = Attr (QName k Nothing Nothing) v
10uattr k v = Attr (QName k Nothing Nothing) v
8
11
@@ -31,6 +34,9 @@ groupArtifactVersion g a v = [
31pomString :: String -> String -> [String] -> String
34pomString :: String -> String -> [String] -> String
32pomString c a d = ppElement $ pom c a d
35pomString c a d = ppElement $ pom c a d
33
36
37+-----------------------------------------------------------------------
38+-- The pom template for idris projects
39+
34pom :: String -> String -> [String] -> Element
40pom :: String -> String -> [String] -> Element
35pom clsName artifactName dependencies = unode "project" ([
41pom clsName artifactName dependencies = unode "project" ([
36 uattr "xmlns" "http://maven.apache.org/POM/4.0.0",
42 uattr "xmlns" "http://maven.apache.org/POM/4.0.0",
@@ -49,7 +55,7 @@ pom clsName artifactName dependencies = unode "project" ([
49 unode "skipTest" "true"
55 unode "skipTest" "true"
50 ],
56 ],
51 unode "dependencies" (
57 unode "dependencies" (
52− dependency "org.idris-lang" "idris" "0.9.10-alpha-2" :
58+ dependency "org.idris-lang" "idris" "0.9.14" :
53 map parseToDep dependencies
59 map parseToDep dependencies
54 ),
60 ),
55 unode "build" [
61 unode "build" [
test/Makefile+1−1
@@ -8,7 +8,7 @@ test: $(TESTS)
8 @perl ./runtest.pl $(patsubst %.test,%,$@)
8 @perl ./runtest.pl $(patsubst %.test,%,$@)
9
9
10test_java:
10test_java:
11− perl ./runtest.pl without sugar004 buffer001 --codegen Java
11+ @perl ./runtest.pl without --codegen Java
12
12
13test_js:
13test_js:
14 @perl ./runtest.pl without sugar004 reg029 io001 dsl002 io003 effects001 effects002 buffer001 --codegen node
14 @perl ./runtest.pl without sugar004 reg029 io001 dsl002 io003 effects001 effects002 buffer001 --codegen node
test/reg029/Reg029Wrapper.javanew file+17
@@ -0,0 +1,17 @@
1+import java.util.Map;
2+
3+/**
4+ * The Java back end uses properties
5+ * instead of environment variables.
6+ * This wrapper loads the env variables
7+ * into the standard set of properties.
8+ */
9+public class Reg029Wrapper {
10+ public static void main(String args[]) {
11+ Map<String, String> env = System.getenv();
12+ for (String key : env.keySet()) {
13+ System.setProperty(key, env.get(key));
14+ }
15+ reg029.main(args);
16+ }
17+}
test/reg029/reg029.idr+1
@@ -2,6 +2,7 @@ module Main
2
2
3import System
3import System
4
4
5+-- necessary to find the getenv symbol
5%dynamic "libm"
6%dynamic "libm"
6
7
7main : IO ()
8main : IO ()
test/reg029/run+12−2
@@ -1,7 +1,17 @@
1#!/usr/bin/env bash
1#!/usr/bin/env bash
2+
2idris $@ reg029.idr -o reg029
3idris $@ reg029.idr -o reg029
3unset IDRIS_REG029_NONEXISTENT_VAR
4unset IDRIS_REG029_NONEXISTENT_VAR
4export IDRIS_REG029_EXISTENT_VAR='exists!'
5export IDRIS_REG029_EXISTENT_VAR='exists!'
6+
7+IsJava=`echo "$@" | grep -E "\-\-codegen([[:space:]]+)Java\>"`
8+if [ -z "$IsJava" ]
9+then
5−./reg029
10+ ./reg029
11+else
12+ javac -cp reg029:. Reg029Wrapper.java
13+ java -cp reg029:. Reg029Wrapper
14+fi
6idris $@ reg029.idr --execute
15idris $@ reg029.idr --execute
7−rm -f reg029 *.ibc
16+rm -f reg029 *.ibc *.class
17+