Merge branch 'typeFamilies'
authorIlya V. Portnov <i.portnov@compassplus.ru>
Fri, 30 Sep 2011 08:11:17 +0000 (14:11 +0600)
committerIlya V. Portnov <i.portnov@compassplus.ru>
Fri, 30 Sep 2011 08:11:17 +0000 (14:11 +0600)
JVM/Generator.hs
TestGen.hs [new file with mode: 0644]

index 9db24e66cf5b5e9f69b93cc11c955738f0324f60..6291b068fcc4322d09068f99b4e556a93d686f52 100644 (file)
@@ -38,7 +38,7 @@ addItem :: Constant -> Generate Word16
 addItem c = do
   pool <- St.gets currentPool
   case lookupPool c pool of
-    Just i -> return i
+    Just i -> return (i+1)
     Nothing -> do
       let (pool', i) = appendPool c pool
       st <- St.get
diff --git a/TestGen.hs b/TestGen.hs
new file mode 100644 (file)
index 0000000..14c91f8
--- /dev/null
@@ -0,0 +1,64 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+import qualified Data.ByteString.Lazy as B
+
+import JVM.Types
+import JVM.ClassFile
+import JVM.Converter
+import JVM.Assembler
+import JVM.Generator
+import JVM.Dump
+
+initNT :: NameType Method
+initNT = NameType "<init>" $ MethodSignature [] ReturnsVoid
+
+helloNT :: NameType Method
+helloNT = NameType "hello" $ MethodSignature [IntType] ReturnsVoid
+
+printlnNT :: NameType Method
+printlnNT = NameType "println" $ MethodSignature [ObjectType "java/lang/String"] ReturnsVoid
+
+outNT :: NameType Field
+outNT = NameType "out" $ ObjectType "java/io/PrintStream"
+
+valueOfNT :: NameType Method
+valueOfNT = NameType "valueOf" $ MethodSignature [IntType] (Returns $ ObjectType "java/lang/Integer")
+
+printfNT :: NameType Method
+printfNT = NameType "printf" $ MethodSignature [ObjectType "java/lang/String",
+                                                Array Nothing $ ObjectType "java/lang/Object"] (Returns $ ObjectType "java/io/PrintStream")
+
+test :: Generate ()
+test = do
+  newMethod [ACC_PUBLIC] "<init>" [] ReturnsVoid $ do
+      i0 $ ALOAD_ I0
+      i1 INVOKESPECIAL (CMethod "java/lang/Object" initNT)
+      i0 RETURN
+
+  newMethod [ACC_PUBLIC, ACC_STATIC] "main" [Array Nothing $ ObjectType "java/lang/String"] ReturnsVoid $ do
+      i0 ICONST_5
+      i1 INVOKESTATIC (CMethod "Test" helloNT)
+      i0 RETURN
+
+  newMethod [ACC_PUBLIC, ACC_STATIC] "hello" [IntType] ReturnsVoid $ do
+      i1 GETSTATIC (CField "java/lang/System" outNT)
+      i8 LDC1 (CString "Здравствуй, мир!")
+      i1 INVOKEVIRTUAL (CMethod "java/io/PrintStream" printlnNT)
+      i1 GETSTATIC (CField "java/lang/System" outNT)
+      i8 LDC1 (CString "Argument: %d\n")
+      i0 ICONST_1
+      i1 ANEWARRAY (CClass "java/lang/Object")
+      i0 DUP
+      i0 ICONST_0
+      i0 (ILOAD_ I0)
+      i1 INVOKESTATIC (CMethod "java/lang/Integer" valueOfNT)
+      i0 AASTORE
+      i1 INVOKEVIRTUAL (CMethod "java/io/PrintStream" printfNT)
+      i0 POP
+      i0 RETURN
+
+testClass = generate "Test" test
+
+main = do
+  B.writeFile "Test.class" (encodeClass testClass)
+