all: mate $(CLASS_FILES)
test: mate $(CLASS_FILES)
- ./$< tests/Fib.class | grep mainresult
+ ./$< tests/Fib | grep mainresult
@printf "should be: 0x%08x\n" 0x09de8d6d
- ./$< tests/Fac.class | grep mainresult
+ ./$< tests/Fac | grep mainresult
@printf "should be: 0x%08x\n" 0x58980
- ./$< tests/ArgumentPassing1.class | grep mainresult
+ ./$< tests/ArgumentPassing1 | grep mainresult
@printf "should be: 0x%08x\n" 0x92
@printf "should be: 0x%08x\n" $$(((0 - 0x1337) & 0xffffffff))
- ./$< tests/DifferentClass1.class | grep mainresult
+ ./$< tests/DifferentClass1 | grep mainresult
@printf "should be: 0x%08x\n" 8
@printf "should be: 0x%08x\n" 13
- ./$< tests/Native1.class | egrep -i -e '^printsomething: '
+ ./$< tests/Native1 | egrep -i -e '^printsomething: '
+ ./$< tests/Static1
%.class: %.java
$(JAVAC) $<
import Mate.X86CodeGen
import Mate.MethodPool
import Mate.Types
+import Mate.ClassPool
main :: IO ()
main = do
initMethodPool
case args of
[clspath] -> do
- cls <- parseClassFile clspath
+ let bclspath = B.pack $ map (fromIntegral . ord) clspath
+ cls <- getClassFile bclspath
dumpClass cls
hmap <- parseMethod cls "main"
printMapBB hmap
let method = find (\x -> (methodName x) == "main") methods
case method of
Just m -> do
- let bclspath = B.pack $ map (fromIntegral . ord) (replace ".class" "" clspath)
entry <- compileBB hmap' (MethodInfo "main" bclspath (methodSignature m))
printf "executing `main' now:\n"
executeFuncPtr entry
--- /dev/null
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ForeignFunctionInterface #-}
+module Mate.ClassPool where
+
+import Data.Binary
+import Data.String.Utils
+import qualified Data.Map as M
+import qualified Data.Set as S
+import qualified Data.ByteString.Lazy as B
+import System.Plugins
+
+import Text.Printf
+
+import Foreign.Ptr
+import Foreign.C.Types
+import Foreign.C.String
+import Foreign.StablePtr
+import Foreign.Marshal.Alloc
+
+import JVM.ClassFile
+import JVM.Converter
+
+import Harpy
+import Harpy.X86Disassembler
+
+import Mate.BasicBlocks
+import Mate.Types
+import Mate.Utilities
+
+
+getClassFile :: B.ByteString -> IO (Class Resolved)
+getClassFile path = do
+ let rpath = toString $ path `B.append` ".class"
+ parseClassFile rpath
import Mate.Types
import Mate.X86CodeGen
import Mate.Utilities
+import Mate.ClassPool
foreign import ccall "get_mmap"
let w32_from = fromIntegral signal_from
let mi@(MethodInfo method cm sig) = cmap M.! w32_from
-- TODO(bernhard): replace parsing with some kind of classpool
- cls <- parseClassFile $ toString $ cm `B.append` ".class"
+ cls <- getClassFile cm
case M.lookup mi mmap of
Nothing -> do
printf "getMethodEntry(from 0x%08x): no method \"%s\" found. compile it\n" w32_from (show mi)
cmap <- get_cmap >>= ptr2cmap
-- TODO(bernhard): replace parsing with some kind of classpool
- cls <- parseClassFile $ toString $ (cName methodinfo) `B.append` ".class"
+ cls <- getClassFile (cName methodinfo)
let ebb = emitFromBB cls hmap
(_, Right ((entry, _, _, new_cmap), disasm)) <- runCodeGen ebb () ()
let w32_entry = ((fromIntegral $ ptrToIntPtr entry) :: Word32)
import Mate.BasicBlocks
import Mate.Types
import Mate.Utilities
+import Mate.ClassPool
foreign import ccall "dynamic"
code_int :: FunPtr (CInt -> CInt -> IO CInt) -> (CInt -> CInt -> IO CInt)
test_01, test_02, test_03 :: IO ()
test_01 = do
register_signal
- (entry, end) <- testCase "./tests/Fib.class" "fib"
+ (entry, end) <- testCase "./tests/Fib" "fib"
let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
mapM_ (\x -> do
test_02 = do
- (entry,_) <- testCase "./tests/While.class" "f"
+ (entry,_) <- testCase "./tests/While" "f"
let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
result <- code_int entryFuncPtr 5 4
let iresult :: Int; iresult = fromIntegral result
test_03 = do
- (entry,_) <- testCase "./tests/While.class" "g"
+ (entry,_) <- testCase "./tests/While" "g"
let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
result <- code_int entryFuncPtr 5 4
let iresult :: Int; iresult = fromIntegral result
printf "result of g(4,3): %3d\t\t%s\n" iresult2 kk2
-testCase :: String -> B.ByteString -> IO (Ptr Word8, Int)
+testCase :: B.ByteString -> B.ByteString -> IO (Ptr Word8, Int)
testCase cf method = do
- cls <- parseClassFile cf
+ cls <- getClassFile cf
hmap <- parseMethod cls method
printMapBB hmap
case hmap of
--- /dev/null
+package tests;
+
+public class Static1 {
+ public static int a;
+ public static int b;
+
+ public static void main(String []args) {
+ Static1.a = 0x11;
+ Static1.b = 0x22;
+ addnumbers();
+ }
+
+ public static int addnumbers() {
+ return Static1.a + Static1.b;
+ }
+}