12 -- Begin Signature ----------------------------------------------------------
15 We have used Transactions to represent instructions w/ their data.
16 These have been particularly useful in pipelined and out-of-order
21 data Trans i c = Trans [c] i [c] [c]
22 deriving (Eq,Show,Read)
24 -- Convention: if Trans d op s i
25 -- we say that d is the destination, op is the instruction
26 -- s is the source, and i is the information
28 -- return a nop-like transaction
29 nop :: (Instruction i,Register r) => Trans i (c r w)
31 -- return a PC transaction
32 pcTrans :: (Cell c,Instruction i,Register r, Word w) =>
35 isNop :: (Instruction i,Register r) => Trans i (c r w) -> Bool
36 isAdd :: (Instruction i,Register r) => Trans i (c r w) -> Bool
37 isAlu :: (Instruction i,Register r) => Trans i (c r w) -> Bool
38 isCmp :: (Instruction i,Register r) => Trans i (c r w) -> Bool
39 isBool :: (Instruction i,Register r) => Trans i (c r w) -> Bool
40 isSub :: (Instruction i,Register r) => Trans i (c r w) -> Bool
41 isMul :: (Instruction i,Register r) => Trans i (c r w) -> Bool
42 isDiv :: (Instruction i,Register r) => Trans i (c r w) -> Bool
43 isJump :: (Instruction i,Register r) => Trans i (c r w) -> Bool
44 isMove :: (Instruction i,Register r) => Trans i (c r w) -> Bool
45 isMem :: (Instruction i,Register r) => Trans i (c r w) -> Bool
46 isLoad :: (Instruction i,Register r) => Trans i (c r w) -> Bool
47 isStore :: (Instruction i,Register r) => Trans i (c r w) -> Bool
48 isBranch :: (Cell c,Register r,Word w) => Trans i (c r w) -> Bool
49 isComputable :: (Cell c,Register r,Word w) => Trans i (c r w) -> Bool
51 -- update destination fields
52 updDst :: (Cell c,Register r,Word w) =>
53 Trans i (c r w) -> [c r w] -> Trans i (c r w)
55 -- apply a function to the destination fields
56 repDst :: Register r => (c r w -> c r w -> Bool) ->
57 Trans i (c r w) -> [c r w] -> Trans i (c r w)
59 -- add to the destination
60 addDst :: Register r => c r w -> Trans i (c r w) -> Trans i (c r w)
62 -- get the destination
63 getDst :: Register r => Trans i (c r w) -> [c r w]
65 -- replace the dest fields
66 putDst :: Register r => Trans i (c r w) -> [c r w] -> Trans i (c r w)
69 updSrc :: (Cell c,Register r,Word w) =>
70 Trans i (c r w) -> [c r w] -> Trans i (c r w)
71 addSrc :: Register r => c r w -> Trans i (c r w) -> Trans i (c r w)
72 getSrc :: Register r => Trans i (c r w) -> [c r w]
73 putSrc :: Register r => Trans i (c r w) -> [c r w] -> Trans i (c r w)
75 addInfo :: Register r => c r w -> Trans i (c r w) -> Trans i (c r w)
76 getInfo :: Register r => Trans i (c r w) -> [c r w]
77 putInfo :: Register r => Trans i (c r w) -> [c r w] -> Trans i (c r w)
79 getOp :: Trans i (c r w) -> i
80 putOp :: Trans i (c r w) -> i -> Trans i (c r w)
83 -- return the speculative PC from the info area
84 getSpecPC :: (Cell c,Register r,Word w) =>
85 Trans i (c r w) -> Maybe (c r w)
87 -- return the PC from the destination area
88 getDstPC :: (Cell c,Register r,Word w) =>
89 Trans i (c r w) -> Maybe (c r w)
90 getSrcPC :: (Cell c,Register r,Word w) =>
91 Trans i (c r w) -> Maybe (c r w)
93 -- return the instructions location from memory from the destination
95 getLoc :: (Cell c,Register r,Word w) =>
96 Trans i (c r w) -> Maybe (c r w)
98 -- get register references
99 getSrcRegs :: (Cell c,Register r,Word w) => Trans i (c r w) -> [r]
100 getDstRegs :: (Cell c,Register r,Word w) => Trans i (c r w) -> [r]
102 -- get register reference values
103 getSrcRegVals :: (Cell c,Register r,Word w) => Trans i (c r w) -> [w]
104 putDstRegVal :: (Cell c,Register r,Word w) =>
105 Trans i (c r w) -> w -> Trans i (c r w)
107 -- evalTrans t (c,w) update the destination fields in t with w if they match
109 evalTrans :: (Cell c,Register r,Word w) =>
110 Trans i (c r w) -> (c r w,Maybe w) -> Trans i (c r w)
112 -- is there a Read-After-Write hazard between two transactions?
113 rawHazard :: (Cell c,Register r,Word w) =>
114 (Trans i (c r w),Trans i (c r w)) -> Bool
116 -- bypass t t2 source operands of t with the dest operands of t2
117 -- if the references match.
119 bypass :: (Cell c,Register r,Word w) =>
120 Trans i (c r w) -> Trans i (c r w) -> Trans i (c r w)
122 -- bypass the dest. operands instead of the source operands.
123 bypassDst :: (Cell c,Register r,Word w) =>
124 Trans i (c r w) -> Trans i (c r w) -> Trans i (c r w)
126 -- bypass with multiple transactions
127 bypassMany :: (Cell c,Register r,Word w) =>
128 Trans i (c r w) -> [Trans i (c r w)] -> Trans i (c r w)
129 bypassDstMany :: (Cell c,Register r,Word w) =>
130 Trans i (c r w) -> [Trans i (c r w)] -> Trans i (c r w)
132 -- bypass to multiple transaction with multiple transactions
133 broadcast :: (Cell a, Register b, Word c) =>
134 [Trans e (a b c)] -> [Trans e (a b c)] -> [Trans e (a b c)]
136 --source operands and dest operands all filled in?
137 complete :: (Cell c,Register r,Word w) =>
138 Trans i (c r w) -> Bool
140 -- if (x,y) = readyToRetire z, then
141 -- x is the lift of transactions that are "complete"
142 readyToRetire :: (Cell c,Register r,Word w) =>
143 [Trans i (c r w)] -> ([Trans i (c r w)],[Trans i (c r w)])
146 -- if (x,y) = readyToCompute z, then
147 -- x is the lift of transactions with all of their source operands filled in
148 readyToCompute :: (Cell c,Register r,Word w) =>
149 [Trans i (c r w)] -> ([Trans i (c r w)],[Trans i (c r w)])
152 updatePC :: Register r => c r w -> Trans i (c r w) -> Trans i (c r w)
155 getPredicate :: (Cell c,Register r,Word w) => Trans i (c r w) -> c r w
156 isPredicated :: (Cell c,Register r,Word w) => Trans i (c r w) -> Bool
158 evalPredicate :: (Cell c,Register r,Word w) => Trans i (c r w) -> w
160 -- End Signature ----------------------------------------------------------
163 updCells :: (Cell c,Register r,Word w) => [c r w] -> [c r w] -> [c r w]
164 repCells :: Register r => (c r w -> c r w -> Bool) ->
165 [c r w] -> [c r w] -> [c r w]
167 -- perhaps these functions can go?
168 filterDst :: Register r => (c r w -> Bool) -> Trans i (c r w) -> [c r w]
170 fillInCells :: (Cell c,Register r,Word w) => [c r w] -> [c r w] -> [c r w]
172 fillInSrcCells :: (Cell c,Register r,Word w) =>
173 Trans i (c r w) -> [c r w] -> Trans i (c r w)
175 filterOut :: (Register r,Functor m) =>
176 (Trans i (c r w) -> Bool) -> m [Trans i (c r w)] ->
181 nop = Trans [] noOp [] []
183 pcTrans addr = Trans [putVal pcNothing (Just addr)] noOp [] []
184 isNop t = isNoOp (getOp t)
185 isAdd t = isAddOp (getOp t)
186 isAlu t = isAluOp (getOp t)
187 isCmp t = isCmpOp (getOp t)
188 isBool t = isBoolOp (getOp t)
189 isSub t = isSubOp (getOp t)
190 isMul t = isMultOp (getOp t)
191 isDiv t = isDivOp (getOp t)
192 isJump t = isJumpOp (getOp t)
193 isMem t = isMemOp (getOp t)
194 isMove t = isMoveOp (getOp t)
195 isLoad t = isLoadOp (getOp t)
196 isStore t = isStoreOp (getOp t)
198 isBranch (Trans d _ _ _) = any search d where
199 search r = if isReg r then ispc (getReg r)
202 isComputable = and . map isComputed . getSrc
207 repCells replFunc cells replacements
208 = map (\cell -> foldr bypassCell cell replacements) cells
210 bypassCell bypassed argCell
211 = if replFunc bypassed argCell
216 updCells cells bypassCells = repCells cellHazard cells bypassCells
219 repDst repFunc (Trans d o s i) cells = Trans (repCells repFunc d cells) o s i
220 updDst = repDst cellHazard
221 addDst c t = putDst t (c:getDst t)
222 getDst (Trans d o s i) = d
223 putDst (Trans _ o s i) d = Trans d o s i
225 updSrc (Trans d o s i) cells = Trans d o (updCells s cells) i
226 addSrc c t = putSrc t (c:getSrc t)
227 getSrc (Trans d o s i) = s
228 putSrc (Trans d o _ i) s = Trans d o s i
230 addInfo c t = putInfo t (c:getInfo t)
231 getInfo (Trans d o s i) = i
232 putInfo (Trans d o s _) i = Trans d o s i
234 getOp (Trans d o s i) = o
235 putOp (Trans d _ s i) o = Trans d o s i
237 getSpecPC = find isSpecPC . getInfo
238 getDstPC = find isPC . getDst
239 getSrcPC = find isPC . getSrc
240 getLoc = find isLoc . getInfo
242 getSrcRegs t = map getReg $ filter isReg $ getSrc t
243 getDstRegs t = map getReg $ filter isReg $ getDst t
245 getSrcRegVals t = map getVal $
246 filter isReg $ getSrc t
249 putDstRegVal (Trans [Reg r _] o s i) n
250 = Trans [Reg r (Val n)] o s i
252 putDstRegVal (Trans [r] o s i) n
253 = Trans [putVal r (Just n)] o s i
255 getPredicate (Trans _ _ l _) = last (filter isPred l)
257 getPredicate' t = if isPredicated t then Just (getPredicate t)
260 isPredicated (Trans _ _ x _)
261 = case filter isPred x of
267 case getPredicate' t of
268 Just c -> if isAss c then getVal c
269 -- else error $ "evalPredicate" ++ show t
270 else error "evalPredicate"
273 bypass tran bypassT = --updSrc tran $ getDst bypassT
274 if evalPredicate bypassT /= 0
275 then updSrc tran $ getDst bypassT
278 bypassDst tran bypassT = if evalPredicate bypassT /= 0
279 then updDst tran $ getDst bypassT
282 bypassMany tran bypassT = foldr (\a b -> b `bypass` a) tran bypassT
284 bypassDstMany tran bypassT = foldr (\a b -> b `bypassDst` a) tran bypassT
286 broadcast xs ys = map (`bypassMany` ys) xs
289 bypass tran bypassT = updSrc tran $ getDst bypassT
291 bypassDst tran bypassT = updDst tran $ getDst bypassT
293 bypassTrans tran bypassT = foldr (\a b -> b `bypass` a) tran bypassT
295 broadcast xs ys = map (`bypassTrans` ys) xs
299 readyToRetire = partition $ and . map isComputed . getDst
301 complete = and . map isComputed . getDst
303 readyToCompute = partition $ and . map isComputed . getSrc
305 tran `evalTrans` (dest,val) = repDst sameLoc tran [putVal dest val]
307 rawHazard (preceeding,following)
308 = or [ cellHazard precCell followCell |
309 precCell <- getDst preceeding,
310 followCell <- getSrc following]
312 filterDst f (Trans d _ _ _) = filter f d
315 filterOut f = fmap (filter $ not . f)
317 fillInCells cells bypassCells
318 = repCells (\x y -> (not $ isAss y) && cellHazard x y) cells bypassCells
320 fillInSrcCells (Trans d o s i) cells = Trans d o (fillInCells s cells) i
322 fillInCells' cells bypassCells
323 = repCells cellHazard cells bypassCells
325 fillInSrcCells' (Trans d o s i) cells = Trans d o (fillInCells' s cells) i
327 -- TEMPORARY --- NOT ROBUST!
328 updatePC c (Trans _ o s i) = Trans [c] o s i