- notarget = (off, Just Return, x):next
- onetarget w16 = (off, Just $ OneTarget (off `addW16Signed` w16), x):next
- twotargets w16 = (off, Just $ TwoTarget (off + 3) (off `addW16Signed` w16), x):next
- next = cio' (newoffset x off) xs
+ notarget = ((off, Just Return, x):) <$> next
+ onetarget w16 = do
+ let jump = off `addW16Signed` w16
+ modify (jump:)
+ ((off, Just $ OneTarget jump, x):) <$> next
+ twotargets w16 = do
+ let nojump = off + 3
+ modify (nojump:)
+ let jump = off `addW16Signed` w16
+ modify (jump:)
+ ((off, Just $ TwoTarget nojump jump, x):) <$> next
+ next = cio' newoffset xs
+ newoffset = (off + insnLength x, Nothing, NOP)
+
+-- TODO(bernhard): does GHC memomize results? i.e. does it calculate the size
+-- of `NOP' only once?
+insnLength :: Num a => Instruction -> a
+insnLength = fromIntegral . B.length . encodeInstructions . (:[])