Skip to content

Commit

Permalink
hmm
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Dec 5, 2023
1 parent 34d4071 commit 9ed5ee1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/JVM/Data/Analyse/StackMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ analyseBlockDiff current block = foldl' (flip analyseInstruction) current (takeW
if i > genericLength ba.locals
then error $ "ILoad index out of bounds. Given: " <> show i <> " Locals: " <> show ba.locals
else ba{stack = lvToStackEntry (ba.locals !! fromIntegral i) : ba.stack}
analyseInstruction (AStore i) ba = ba{locals = replaceAtOrGrow (fromIntegral (i - 1)) (stackEntryToLV $ head ba.stack) ba.locals, stack = tail ba.stack}
analyseInstruction (IStore i) ba = ba{locals = replaceAtOrGrow (fromIntegral (i - 1)) (stackEntryToLV $ head ba.stack) ba.locals, stack = tail ba.stack}
analyseInstruction (AStore i) ba = ba{locals = replaceAtOrGrow (fromIntegral i) (stackEntryToLV $ head ba.stack) ba.locals, stack = tail ba.stack}
analyseInstruction (IStore i) ba = ba{locals = replaceAtOrGrow (fromIntegral i) (stackEntryToLV $ head ba.stack) ba.locals, stack = tail ba.stack}
analyseInstruction AReturn ba = ba{stack = tail ba.stack}
analyseInstruction Return ba = ba
analyseInstruction (LDC (LDCInt _)) ba = ba{stack = StackEntry (PrimitiveFieldType Int) : ba.stack}
Expand Down
37 changes: 30 additions & 7 deletions test/Analyse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ genFieldType =
spec :: Spec
spec = describe "Analysis checks" $ do
describe "Does StackDiff concatenation correctly" $ do
it "Can identify incredibly simple blocks properly" $ do
let (_, code) = runCodeBuilder $ do
emit $ LDC (LDCInt 0) -- [0]
emit $ AStore 0
emit $ ALoad 0
emit AReturn

hedgehog $ do
let blocks = splitIntoBasicBlocks code

blocks
=== [ BasicBlock 0 [LDC (LDCInt 0), AStore 0, ALoad 0, AReturn] Nothing
]

let top = topFrame (MethodDescriptor [] (TypeReturn (PrimitiveFieldType Int)))
let nextFrame = analyseBlockDiff top (head blocks)

nextFrame
=== Frame
{ locals = [LocalVariable (PrimitiveFieldType Int)]
, stack = []
}

it "Can identify sameframe blocks properly" $ do
let (l, _, code) = runCodeBuilder' $ do
label <- newLabel
Expand Down Expand Up @@ -93,24 +116,24 @@ spec = describe "Analysis checks" $ do
let (l, _, code) = runCodeBuilder' $ do
label <- newLabel
emit $ LDC (LDCInt 0) -- [0]
emit $ IStore 1 -- []
emit $ IStore 0 -- []
emit $ LDC (LDCInt 0) -- [0]
emit $ IStore 2 -- []
emit $ ILoad 1 -- [0]
emit $ IStore 1 -- []
emit $ ILoad 0 -- [0]
emit $ IfLe label -- []
emit $ LDC (LDCInt 0) -- [0]
emit $ IStore 3 -- []
emit $ IStore 2 -- []
emit $ Label label -- []
emit $ LDC (LDCInt 0) -- [0]
emit $ IStore 3 -- []
emit $ IStore 2 -- []
emit Return -- []
pure label
hedgehog $ do
let blocks = splitIntoBasicBlocks code

blocks
=== [ BasicBlock 0 [LDC (LDCInt 0), IStore 1, LDC (LDCInt 0), IStore 2, ILoad 1, IfLe l, LDC (LDCInt 0), IStore 3] (Just l)
, BasicBlock 1 [LDC (LDCInt 0), IStore 3, Return] Nothing
=== [ BasicBlock 0 [LDC (LDCInt 0), IStore 0, LDC (LDCInt 0), IStore 1, ILoad 0, IfLe l, LDC (LDCInt 0), IStore 2] (Just l)
, BasicBlock 1 [LDC (LDCInt 0), IStore 2, Return] Nothing
]

let top = topFrame (MethodDescriptor [] VoidReturn)
Expand Down

0 comments on commit 9ed5ee1

Please sign in to comment.