tests: add class with {do-,}while keyword
authorBernhard Urban <lewurm@gmail.com>
Mon, 2 Apr 2012 00:00:49 +0000 (02:00 +0200)
committerBernhard Urban <lewurm@gmail.com>
Mon, 2 Apr 2012 00:00:49 +0000 (02:00 +0200)
... which unfortunately fails to parse due to a bug in hs-java.

the bogus instructions are goto and if<cond>, which hs-java
misses to parse their immediate values from the instructionstream.

fix:
$ git clone git@wien.tomnetworks.com:hs-java.git || git clone git://wien.tomnetworks.com/hs-java.git
$ cd hs-java; cabal configure; cabal build; cabal install

src/BasicBlocks.hs
tests/While.java [new file with mode: 0644]

index 92ad81bcbb98b3d5ca1b24e63d96064302e07fda..e37979bce264403c51d63680810753ef5d2f14ed 100644 (file)
@@ -14,23 +14,40 @@ import JVM.Assembler
 
 import Utilities
 
+type Name       = String -- use "virtual register id" instead?
+data Type       = JInt | JFloat -- add more
+type Variable   = (Type,Name)
+
+-- Represents a CFG node
+data BasicBlock = BasicBlock {
+                     inputs  :: [Variable],
+                     outputs :: [Variable],
+                     code    :: [Instruction] }
+
+-- Represents a Control-Flow-Graph as
+-- Adjacency list (add matrix representation if appropriate)
+type CFList     = [(BasicBlock, [BasicBlock])]
+
+
 main = do
   args <- getArgs
   case args of
-    [clspath] -> parseFile clspath
+    [clspath] -> parseFile clspath "fib" -- TODO
     _ -> error "Synopsis: dump-class File.class"
 
 
-parseFile :: String -> IO ()
-parseFile clspath = do
+parseFile :: String -> B.ByteString -> IO ()
+parseFile clspath method = do
                      --clsFile <- decodeFile clspath
                      --putStrLn $ showListIx $ M.assocs $ constsPool (clsFile :: Class Pointers)
                      cls <- parseClassFile clspath
                      --dumpClass cls    
-                     let mainmethod = lookupMethod "fib" cls -- "main|([Ljava/lang/String;)V" cf
+                     let mainmethod = lookupMethod method cls -- "main|([Ljava/lang/String;)V" cf
                      mapM_ putStrLn (testCFG mainmethod)
 
-test = parseFile "./tests/Fib.class"
+test_01 = parseFile "./tests/Fib.class" "fib"
+test_02 = parseFile "./tests/While.class" "f"
+test_03 = parseFile "./tests/While.class" "g"
 
 
 testCFG :: Maybe (Method Resolved) -> [String]
diff --git a/tests/While.java b/tests/While.java
new file mode 100644 (file)
index 0000000..745b378
--- /dev/null
@@ -0,0 +1,17 @@
+public class While {
+       public static int f(int a, int b) {
+               do {
+                       a += b;
+                       b--;
+               } while (b > 0);
+               return a;
+       }
+
+       public static int g(int a, int b) {
+               while (b > 0) {
+                       a += b;
+                       b--;
+               }
+               return a;
+       }
+}