From: Bernhard Urban Date: Mon, 2 Apr 2012 19:40:00 +0000 (+0200) Subject: basicblock: get jump offsets from instructions X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mate.git;a=commitdiff_plain;h=85f798b95510de7cae7c48984c223e03a8b37f92 basicblock: get jump offsets from instructions --- diff --git a/Mate/BasicBlocks.hs b/Mate/BasicBlocks.hs index 68d8cb9..a15c5e9 100644 --- a/Mate/BasicBlocks.hs +++ b/Mate/BasicBlocks.hs @@ -2,6 +2,7 @@ module Mate.BasicBlocks where import Data.Binary +import Data.Int import System.Environment import qualified Data.Map as M import qualified Data.ByteString.Lazy as B @@ -64,11 +65,18 @@ buildCFG xs = map (\(x,y) -> show x ++ ", " ++ show y) xs' where xs' = calculateInstructionOffset xs -type Offset = Int +type Offset = (Int, Maybe Int16) -- (offset in bytecode, offset to jump target) + calculateInstructionOffset :: [Instruction] -> [(Offset, Instruction)] -calculateInstructionOffset = cio' 0 +calculateInstructionOffset = cio' (0, Nothing) where + newoffset :: Instruction -> Int -> Offset + newoffset x off = (off + (fromIntegral $ B.length $ encodeInstructions [x]), Nothing) cio' :: Offset -> [Instruction] -> [(Offset, Instruction)] cio' _ [] = [] - cio' off (x:xs) = (off,x):(cio' newoffset xs) - where newoffset = off + (fromIntegral $ B.length $ encodeInstructions [x]) + -- TODO(bernhard): add more instruction with offset (IF_ACMP, JSR, ...) + -- TODO(bernhard): beautiful code please (BCP) + cio' (off,_) (x@(IF _ w16):xs) = ((off, Just $ fromIntegral w16), x):(cio' (newoffset x off) xs) + cio' (off,_) (x@(IF_ICMP _ w16):xs) = ((off, Just $ fromIntegral w16), x):(cio' (newoffset x off) xs) + cio' (off,_) (x@(GOTO w16):xs) = ((off, Just $ fromIntegral w16), x):(cio' (newoffset x off) xs) + cio' (off,_) (x:xs) = ((off, Nothing), x):(cio' (newoffset x off) xs)