on master help sign in
Merge pull request #1294 from JanBessai/JavaBackendFixes

Java back-end fixes

commit2b183c8e27cd44145fb0a7ecd93b8a963d2a91b0 authorEdwin Brady <edwin.brady@gmail.com> dateMon 9 Jun 2014, 12:51 (12 years ago) parent9815da85cad34b
8 files changed+78−9
CONTRIBUTORS+1 src/IRTS/CodegenJava.hs+8−5 src/IRTS/Java/JTypes.hs+31 src/IRTS/Java/Pom.hs+7−1 test/Makefile+1−1 test/reg029/Reg029Wrapper.java+17 test/reg029/reg029.idr+1 test/reg029/run+12−2
CONTRIBUTORS+1
@@ -3,6 +3,7 @@ Thanks to the following for their help and contributions:
33Ozgur Akgun
44Ahmad Salim Al-Sibahi
55Edward Chadwick Amsden
6+Jan Bessai
67Nicola Botta
78Edwin Brady
89David Raymond Christiansen
src/IRTS/CodegenJava.hs+8−5
@@ -169,7 +169,7 @@ jarHeader =
169169 ++ "if test -n \"$JAVA_HOME\"; then\n"
170170 ++ " java=\"$JAVA_HOME/bin/java\"\n"
171171 ++ "fi\n"
172− ++ "exec \"$java\" $java_args -jar $MYSELF \"$@\""
172+ ++ "exec \"$java\" $java_args -jar $MYSELF \"$@\"\n"
173173 ++ "exit 1\n"
174174
175175-----------------------------------------------------------------------
@@ -312,6 +312,7 @@ mkCompilationUnit globalInit defs hdrs out = do
312312 , ImportDecl True idrisPrelude True
313313 , ImportDecl False bigInteger False
314314 , ImportDecl False runtimeException False
315+ , ImportDecl False byteBuffer False
315316 ] ++ otherHdrs
316317 )
317318 <$> mkTypeDecl clsName globalInit defs
@@ -320,6 +321,7 @@ mkCompilationUnit globalInit defs hdrs out = do
320321 idrisPrelude = J.Name $ map Ident ["org", "idris", "rts", "Prelude"]
321322 bigInteger = J.Name $ map Ident ["java", "math", "BigInteger"]
322323 runtimeException = J.Name $ map Ident ["java", "lang", "RuntimeException"]
324+ byteBuffer = J.Name $ map Ident ["java", "nio", "ByteBuffer"]
323325 otherHdrs = map ( (\ name -> ImportDecl False name False)
324326 . J.Name
325327 . map (Ident . T.unpack)
@@ -386,9 +388,7 @@ mkMainMethod =
386388 "main"
387389 [FormalParam [] (array stringType) False (VarId $ Ident "args")]
388390 $ Block [ BlockStmt . ExpStmt
389− $ call "idris_initArgs" [ (threadType ~> "currentThread") []
390− , jConst "args"
391− ]
391+ $ call "idris_initArgs" [ jConst "args" ]
392392 , BlockStmt . ExpStmt $ call (mangle' (sMN 0 "runMain")) []
393393 ]
394394
@@ -484,6 +484,8 @@ mkExp pp (SOp LFork [arg]) =
484484 (mkThread arg) >>= ppExp pp
485485mkExp pp (SOp LPar [arg]) =
486486 (Nothing <>@! arg) >>= ppExp pp
487+mkExp pp (SOp LRegisterPtr [ptr, i]) =
488+ (Nothing <>@! ptr) >>= ppExp pp
487489mkExp pp (SOp LNoOp args) =
488490 (Nothing <>@! (last args)) >>= ppExp pp
489491mkExp pp (SOp LNullPtr args) =
@@ -707,6 +709,7 @@ mkConstant c@(AType x) = ClassLit (Just $ box (constType c))
707709mkConstant c@(StrType ) = ClassLit (Just $ stringType)
708710mkConstant c@(PtrType ) = ClassLit (Just $ objectType)
709711mkConstant c@(ManagedPtrType) = ClassLit (Just $ objectType)
712+mkConstant c@(BufferType) = ClassLit (Just $ bufferType)
710713mkConstant c@(VoidType ) = ClassLit (Just $ voidType)
711714mkConstant c@(Forgot ) = ClassLit (Just $ objectType)
712715
@@ -744,7 +747,7 @@ mkForeign pp (LANG_JAVA callType) resTy text params
744747
745748mkPrimitiveFunction :: PrimFn -> [LVar] -> CodeGeneration Exp
746749mkPrimitiveFunction op args =
747− (\ args -> (primFnType ~> opName op) args)
750+ (\ args -> (primFnType ~> opName op) (endiannessArguments op ++ args))
748751 <$> sequence (zipWith (\ a t -> (Just t) <>@! a) args (sourceTypes op))
749752
750753mkThread :: LVar -> CodeGeneration Exp
src/IRTS/Java/JTypes.hs+31
@@ -33,6 +33,9 @@ doubleType = PrimType DoubleT
3333array :: J.Type -> J.Type
3434array t = RefType . ArrayType $ t
3535
36+addressType :: J.Type
37+addressType = longType
38+
3639-----------------------------------------------------------------------
3740-- Boxed types
3841
@@ -48,6 +51,10 @@ stringType :: J.Type
4851stringType =
4952 RefType . ClassRefType $ ClassType [(Ident "String", [])]
5053
54+bufferType :: J.Type
55+bufferType =
56+ RefType . ClassRefType $ ClassType [(Ident "ByteBuffer", [])]
57+
5158threadType :: J.Type
5259threadType =
5360 RefType . ClassRefType $ ClassType [(Ident "Thread", [])]
@@ -132,6 +139,9 @@ runtimeExceptionType :: J.Type
132139runtimeExceptionType =
133140 RefType . ClassRefType $ ClassType [(Ident "RuntimeException", [])]
134141
142+-----------------------------------------------------------------------
143+-- Integer types
144+
135145nativeTyToJType :: NativeTy -> J.Type
136146nativeTyToJType IT8 = byteType
137147nativeTyToJType IT16 = shortType
@@ -219,6 +229,7 @@ opName x
219229 | (LFloatInt to) <- x = "LFloatInt" ++ (suffixFor to)
220230 | (LStrInt to) <- x = "LStrInt" ++ (suffixFor to)
221231 | (LChInt to) <- x = "LChInt" ++ (suffixFor to)
232+ | (LPeek to _) <- x = "LPeek" ++ (suffixFor to)
222233 | otherwise = takeWhile ((/=) ' ') $ show x
223234 where
224235 suffixFor (ITFixed nt) = show nt
@@ -291,10 +302,30 @@ sourceTypes (LStrRev) = [stringType]
291302sourceTypes (LStdIn) = []
292303sourceTypes (LStdOut) = []
293304sourceTypes (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]
294311sourceTypes (LFork) = [objectType]
295312sourceTypes (LPar) = [objectType]
296313sourceTypes (LVMPtr) = []
297314sourceTypes (LNullPtr) = [objectType]
298315sourceTypes (LNoOp) = repeat objectType
299316
317+-----------------------------------------------------------------------
318+-- Endianess markers
300319
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
33import Data.List (unfoldr)
44import Text.XML.Light
55
6+-----------------------------------------------------------------------
7+-- String <-> XML processing
8+
69uattr :: String -> String -> Attr
710uattr k v = Attr (QName k Nothing Nothing) v
811
@@ -31,6 +34,9 @@ groupArtifactVersion g a v = [
3134pomString :: String -> String -> [String] -> String
3235pomString c a d = ppElement $ pom c a d
3336
37+-----------------------------------------------------------------------
38+-- The pom template for idris projects
39+
3440pom :: String -> String -> [String] -> Element
3541pom clsName artifactName dependencies = unode "project" ([
3642 uattr "xmlns" "http://maven.apache.org/POM/4.0.0",
@@ -49,7 +55,7 @@ pom clsName artifactName dependencies = unode "project" ([
4955 unode "skipTest" "true"
5056 ],
5157 unode "dependencies" (
52− dependency "org.idris-lang" "idris" "0.9.10-alpha-2" :
58+ dependency "org.idris-lang" "idris" "0.9.14" :
5359 map parseToDep dependencies
5460 ),
5561 unode "build" [
test/Makefile+1−1
@@ -8,7 +8,7 @@ test: $(TESTS)
88 @perl ./runtest.pl $(patsubst %.test,%,$@)
99
1010test_java:
11− perl ./runtest.pl without sugar004 buffer001 --codegen Java
11+ @perl ./runtest.pl without --codegen Java
1212
1313test_js:
1414 @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
22
33import System
44
5+-- necessary to find the getenv symbol
56%dynamic "libm"
67
78main : IO ()
test/reg029/run+12−2
@@ -1,7 +1,17 @@
11#!/usr/bin/env bash
2+
23idris $@ reg029.idr -o reg029
34unset IDRIS_REG029_NONEXISTENT_VAR
45export IDRIS_REG029_EXISTENT_VAR='exists!'
5−./reg029
6+
7+IsJava=`echo "$@" | grep -E "\-\-codegen([[:space:]]+)Java\>"`
8+if [ -z "$IsJava" ]
9+then
10+ ./reg029
11+else
12+ javac -cp reg029:. Reg029Wrapper.java
13+ java -cp reg029:. Reg029Wrapper
14+fi
615idris $@ reg029.idr --execute
7−rm -f reg029 *.ibc
16+rm -f reg029 *.ibc *.class
17+