Skip to content

Commit 14a46d4

Browse files
Static field support now implemented
1 parent 175b99b commit 14a46d4

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Documentation/
44
*.dyn_hi*
55
*.dyn_o*
66
*.log
7+
*.h
8+
*.hi-boot
79
*.json
810
*.exe
911
Main

LLVM-JVM.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ executable LLVM-JVM
2020
main-is: Main.hs
2121
-- other-modules:
2222
other-extensions: OverloadedStrings, FlexibleContexts, GADTs, ForeignFunctionInterface, MultiParamTypeClasses, FunctionalDependencies, ScopedTypeVariables, TemplateHaskell, CPP, GeneralizedNewtypeDeriving
23-
build-depends: base >=4.8 && <4.9, hs-java >=0.3 && <0.4, split >=0.2 && <0.3, bytestring >=0.10 && <0.11, hs-boehmgc >=0.1 && <0.2, binary >=0.7 && <0.8, containers >=0.5 && <0.6, MissingH >=1.4 && <1.5, directory >=1.2 && <1.3, plugins >=1.5 && <1.6, mtl >=2.2 && <2.3, QuickCheck >=2.8 && <2.9, IntervalMap >=0.5 && <0.6, harpy >=0.6 && <0.7, vector >=0.11 && <0.12, lens >=4.15 && <4.16, llvm-hs-pure >=5.0 && <5.1, llvm-hs -any, extra -any
23+
build-depends: base -any, hs-java >=0.3 && <0.4, split -any, bytestring -any, hs-boehmgc -any, binary -any, containers -any, MissingH -any, directory -any, plugins -any, mtl -any, QuickCheck -any, IntervalMap -any, harpy -any, vector -any, lens -any, llvm-hs-pure -any, llvm-hs -any, extra -any
2424
-- hs-source-dirs:
2525
default-language: Haskell2010

LLVMFrontend/MkGraph.hs

+28-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module LLVMFrontend.MkGraph
2323
import Data.Int
2424
import Data.Word
2525
import Data.Maybe
26+
import Unsafe.Coerce
2627

2728
import Control.Applicative hiding ((<*>))
2829
import Control.Monad
@@ -264,6 +265,7 @@ module LLVMFrontend.MkGraph
264265
forM_ (S.toList hs ++ [0]) addLabel
265266
return ()
266267

268+
267269
mkBlocks :: ParseState ()
268270
mkBlocks = do
269271
pc <- pcOffset <$> get
@@ -619,14 +621,33 @@ module LLVMFrontend.MkGraph
619621
-- apush nv
620622
-- return [IRLoad (RTPool x) obj nv]
621623
tir (GETSTATIC x) = do
622-
error "Not Supported"
623-
-- cls <- classf <$> get
624-
-- nv <- newvar (fieldType cls x)
625-
-- apush nv
626-
-- return [IRLoad (RTPool x) LT.ptrNull nv]
624+
cls <- gets classf
625+
(fname, ftype) <- case constsPool cls M.! x of
626+
(CField _ (NameType fname ftype)) -> return (fname, ftype)
627+
_ -> error $ "Bad index: " ++ show x
628+
629+
let fieldRef = cons $ global (LT.ptr . jvm2llvmType $ ftype) (mkName' fname)
630+
tmp <- newvar
631+
pushOperand $ LO.LocalReference (jvm2llvmType ftype) tmp
632+
appendInstruction $ tmp LI.:= load fieldRef
633+
where
634+
mkName' :: B.ByteString -> LN.Name
635+
mkName' = LN.mkName . map unsafeCoerce . B.unpack
627636
tir (PUTSTATIC x) = do
628-
error "Not Supported"
629-
-- y <- apop
637+
y <- popOperand
638+
639+
cls <- gets classf
640+
(fname, ftype) <- case constsPool cls M.! x of
641+
(CField _ (NameType fname ftype)) -> return (fname, ftype)
642+
_ -> error $ "Bad index: " ++ show x
643+
644+
let fieldRef = cons $ global (LT.ptr . jvm2llvmType $ ftype) (mkName' fname)
645+
appendInstruction $ LI.Do $ store fieldRef y
646+
where
647+
mkName' :: B.ByteString -> LN.Name
648+
mkName' = LN.mkName . map unsafeCoerce . B.unpack
649+
650+
-- appendInstruction $ LI.Do $ load (global LT.i32 )
630651
-- return [IRStore (RTPool x) LT.ptrNull y]
631652
tir (LDC1 x) = tir (LDC2 (fromIntegral x))
632653
tir (LDC2 x) = do

MateVMRuntime/MethodPool.hs

+21-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Data.Maybe (fromJust)
1414

1515
import Foreign
1616
import Foreign.C.Types
17+
import Unsafe.Coerce
1718

1819
import JVM.ClassFile
1920
import JVM.Assembler
@@ -27,7 +28,10 @@ import MateVMRuntime.ClassPool
2728
import MateVMRuntime.NativeMethods
2829

2930
import qualified LLVM.AST as AST
31+
import qualified LLVM.AST.Global as LG
32+
import qualified LLVM.AST.Linkage as LL
3033
import qualified LLVM.ExecutionEngine as EE
34+
import qualified LLVM.AST.Constant as LC
3135
import LLVM.Context
3236
import LLVM.CodeModel
3337
import LLVM.Target
@@ -96,6 +100,20 @@ insertCompiledMethod entry (MethodInfo mmname _ msig) clsnames = do
96100
initModule :: AST.Module
97101
initModule = AST.defaultModule { AST.moduleName = "dummy" }
98102

103+
mkName' :: B.ByteString -> AST.Name
104+
mkName' = AST.mkName . map unsafeCoerce . B.unpack
105+
106+
parseFields :: Class Direct -> [AST.Definition]
107+
parseFields cls =
108+
flip map (classFields cls) $ \field ->
109+
let nameType = fieldNameType field in
110+
AST.GlobalDefinition $ LG.globalVariableDefaults {
111+
LG.name = mkName' $ ntName nameType,
112+
LG.type' = jvm2llvmType $ ntSignature nameType,
113+
LG.linkage = LL.Private,
114+
LG.initializer = Just $ LC.Int 32 0
115+
}
116+
99117
-- Stubbed for now...
100118
-- TODO: Do LLVM
101119
compileMethod :: B.ByteString -> MethodSignature -> Class Direct -> IO (a,b)
@@ -105,7 +123,7 @@ compileMethod name sig cls = do
105123

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

@@ -135,7 +153,8 @@ compileMethod' mod =
135153
case mainfn of
136154
Just fn -> do
137155
result <- code_int (castFunPtr fn :: FunPtr (IO Int))
138-
printfJit $ "Evaluated to: " ++ show result
156+
printfJit . show $ result
157+
139158
Nothing -> return ()
140159

141160
-- Optimized module used only in next pass...

Test.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ for d in */; do
2929
continue
3030
fi
3131

32-
bootstrap=$(locate -r '/rt.jar$')
32+
bootstrap=$(java -verbose 2>/dev/null | sed -ne '1 s/\[Opened \(.*\)\]/\1/p')
33+
echo "bootstrap: $bootstrap, file: $f"
3334
javac -target 1.7 -source 1.7 -bootclasspath $bootstrap $f
3435
../../Main.exe -cp ../../rt:./ "$filename" 1> tmp.out 2> tmp.err
3536

Unit/Basic/Addition.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Addition {
22

3+
public static int result;
4+
35
public static int RunMe() {
46
int x = 0x1000;
57
int y = 0x2000;
68
int z = 0x3000;
79
int w = x + y + z;
810

9-
int result;
1011
if (w > x) {
1112
result = w;
1213
} else if (y > w) {

example.hs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
main :: IO ()
2+
main = putStrLn "Hello World"
3+
a 1 2a
4+
prelude

0 commit comments

Comments
 (0)