Skip to content

Commit

Permalink
Decided run a specific method, called 'RunMe' that has a polymorphic …
Browse files Browse the repository at this point in the history
…return type and capture and display its result. Currently the return type kind of has to be coerce to an integer, but overall still really good! I can get the return type and validate that now control flow is 100% working and LLVM is a god-send!
  • Loading branch information
LouisJenkinsCS committed Oct 28, 2017
1 parent 3d91162 commit f14ff0c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
26 changes: 24 additions & 2 deletions LLVMFrontend/MkGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module LLVMFrontend.MkGraph
, mkBlocks
, mkMethod
, pipeline
, jvm2llvmType
, returnType
) where

import qualified Data.List as L
Expand Down Expand Up @@ -121,6 +123,7 @@ module LLVMFrontend.MkGraph
hstarts :: S.Set Int32
hstarts = S.fromList $ map (fromIntegral . eHandlerPC)
$ codeExceptions decoded

initstate :: ParseState'
initstate = ParseState' {
-- We begin with only a single entry block
Expand All @@ -140,6 +143,24 @@ module LLVMFrontend.MkGraph
}
runAll prog = execStateT prog initstate

jvm2llvmType :: FieldType -> LT.Type
jvm2llvmType typ = case typ of
SignedByte -> LT.i8
CharByte -> LT.i8
DoubleType -> LT.double
FloatType -> LT.float
IntType -> LT.i32
LongInt -> LT.i64
ShortInt -> LT.i16
BoolType -> LT.i1
(ObjectType _) -> LT.i64
(Array dim typ) -> LT.ArrayType (maybe 0 fromIntegral dim) (jvm2llvmType typ)


returnType :: MethodSignature -> LT.Type
returnType (MethodSignature _ (Returns typ)) = jvm2llvmType typ
returnType (MethodSignature _ ReturnsVoid) = LT.void

prettyHeader :: String -> IO ()
prettyHeader str = do
let len = length str + 6
Expand Down Expand Up @@ -381,7 +402,7 @@ module LLVMFrontend.MkGraph
J.C_LT -> LIP.SLT
J.C_NE -> LIP.NE

appendInstruction $ cmpName LI.:= LI.ICmp cmpInstr op1 op2 []
appendInstruction $ cmpName LI.:= LI.ICmp cmpInstr op2 op1 []

-- branch conditionally based on result
setTerminator $ cbr cmp truejmp falsejmp
Expand Down Expand Up @@ -426,7 +447,8 @@ module LLVMFrontend.MkGraph
-- Return the top operand (after performing type-checking)
returnSomething t = do
r <- apop
error $ "Processing Type: " ++ show t ++ ", " ++ show r
setTerminator $ ret r
-- error $ "Processing Type: " ++ show t ++ ", " ++ show r
-- unless (varType r == t) $ error "toLast return: type mismatch"
-- return ([], IRReturn $ Just r)

Expand Down
11 changes: 7 additions & 4 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module Main where

import Misc.Logger

bootstrapMethod :: String
bootstrapMethod = "RunMe"

main :: IO ()
main = do
time "main" $ do
Expand Down Expand Up @@ -71,11 +74,11 @@ module Main where
--required on some platforms, initializes boehmgc. [todo bernhard: maybe this should be moved somewhere else - maybe at a global place where vm initialization takes place]
unless usePreciseGC initGC

case find ((==) (C8.pack "main") . methodName) (classMethods cls) of
case find ((==) (C8.pack bootstrapMethod) . methodName) (classMethods cls) of
Just m -> do
let mi = MethodInfo (C8.pack "main") bclspath $ methodSignature m
let mi = MethodInfo (C8.pack bootstrapMethod) bclspath $ methodSignature m
entry <- lookupMethodEntry mi
printfInfo "executing `main' now:\n"
printfInfo $ "executing '" ++ bootstrapMethod ++ "' now:\n"
executeFuncPtr (fromIntegral entry)
printfInfo "Well, goodbye Sir!\n"
Nothing -> error "main not found"
Nothing -> error $ bootstrapMethod ++ " not found"
6 changes: 4 additions & 2 deletions MateVMRuntime/MethodPool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import LLVM.Transforms
foreign import ccall "dynamic"
code_void :: FunPtr (IO ()) -> IO ()

foreign import ccall "dynamic"
code_int :: FunPtr (IO Int) -> IO Int

lookupMethodEntry:: MethodInfo -> IO CPtrdiff
lookupMethodEntry mi@(MethodInfo method cm sig) = do
Expand Down Expand Up @@ -103,7 +105,7 @@ compileMethod name sig cls = do

cfg <- pipeline cls meth (codeInstructions . decodeMethod $ code)
-- cfg <- parseCFG (decodeMethod code)
let mod = AST.defaultModule { AST.moduleDefinitions = [defineFn AST.VoidType "main" (M.elems $ basicBlocks cfg)], AST.moduleName = "Dummy" }
let mod = AST.defaultModule { AST.moduleDefinitions = [defineFn (returnType sig) "main" (M.elems $ basicBlocks cfg)], AST.moduleName = "Dummy" }
ast <- compileMethod' mod
error . show $ ast

Expand Down Expand Up @@ -132,7 +134,7 @@ compileMethod' mod =
mainfn <- EE.getFunction ee' "main"
case mainfn of
Just fn -> do
result <- code_void (castFunPtr fn :: FunPtr (IO ()))
result <- code_int (castFunPtr fn :: FunPtr (IO Int))
printfJit $ "Evaluated to: " ++ show result
Nothing -> return ()

Expand Down
9 changes: 8 additions & 1 deletion Unit/Basic/Addition.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Addition {
public static void main(String[] args) {

public static int RunMe() {
int x = 0x1000;
int y = 0x2000;
int z = 0x3000;
Expand All @@ -15,5 +16,11 @@ public static void main(String[] args) {
} else {
result = x;
}

return result;
}

public static void main(String[] args) {

}
}

0 comments on commit f14ff0c

Please sign in to comment.