From: Ilya Portnov Date: Fri, 30 Sep 2011 19:14:22 +0000 (+0600) Subject: Allow setting stack size and locals number for each method. X-Git-Tag: v0.3.2~10^2~23 X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=hs-java.git;a=commitdiff_plain;h=5575269b75aaaa3813d0bbe9dc7e8b44aa109288 Allow setting stack size and locals number for each method. --- diff --git a/JVM/Builder/Monad.hs b/JVM/Builder/Monad.hs index 85916e2..2ab3f7a 100644 --- a/JVM/Builder/Monad.hs +++ b/JVM/Builder/Monad.hs @@ -17,14 +17,18 @@ data GState = GState { generated :: [Instruction], currentPool :: Pool Resolved, doneMethods :: [Method Resolved], - currentMethod :: Maybe (Method Resolved)} + currentMethod :: Maybe (Method Resolved), + stackSize :: Word16, + locals :: Word16 } deriving (Eq,Show) emptyGState = GState { generated = [], currentPool = M.empty, doneMethods = [], - currentMethod = Nothing } + currentMethod = Nothing, + stackSize = 496, + locals = 0 } type Generate a = State GState a @@ -106,10 +110,22 @@ i8 fn c = do ix <- addToPool c i0 (fn $ fromIntegral ix) +setStackSize :: Word16 -> Generate () +setStackSize n = do + st <- St.get + St.put $ st {stackSize = n} + +setMaxLocals :: Word16 -> Generate () +setMaxLocals n = do + st <- St.get + St.put $ st {locals = n} + startMethod :: [AccessFlag] -> B.ByteString -> MethodSignature -> Generate () startMethod flags name sig = do addToPool (CString name) addSig sig + setStackSize 4096 + setMaxLocals 100 st <- St.get let method = Method { methodAccessFlags = S.fromList flags, @@ -144,8 +160,8 @@ newMethod flags name args ret gen = do genCode :: GState -> Code genCode st = Code { - codeStackSize = 4096, - codeMaxLocals = 100, + codeStackSize = stackSize st, + codeMaxLocals = locals st, codeLength = len, codeInstructions = generated st, codeExceptionsN = 0, diff --git a/TestGen.hs b/TestGen.hs index 175e10b..886751d 100644 --- a/TestGen.hs +++ b/TestGen.hs @@ -13,11 +13,15 @@ import qualified Java.IO test :: Generate () test = do newMethod [ACC_PUBLIC] "" [] ReturnsVoid $ do + setStackSize 1 + aload_ I0 invokeSpecial Java.Lang.object Java.Lang.objectInit i0 RETURN hello <- newMethod [ACC_PUBLIC, ACC_STATIC] "hello" [IntType] ReturnsVoid $ do + setStackSize 8 + getStaticField Java.Lang.system Java.IO.out loadString "Здравствуй, мир!" invokeVirtual Java.IO.printStream Java.IO.println @@ -35,6 +39,8 @@ test = do i0 RETURN newMethod [ACC_PUBLIC, ACC_STATIC] "main" [arrayOf Java.Lang.stringClass] ReturnsVoid $ do + setStackSize 1 + iconst_5 invokeStatic "Test" hello i0 RETURN