Debian Codegraph @

lib/mlrisc-lib/MLRISC/ra/getreg.sml

raw
1 (* getreg.sml
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 *)
6
7 (** A simple round robin register allocator **)
8 functor GetReg(val first : int (* start from ``first'' *)
9 val nRegs : int (* n registers *)
10 val available : int list) : GETREG =
11 struct
12 exception GetReg
13 val size = first+nRegs
14 val allRegs = Array.array(size,false)
15 val preferred = Array.array(size,~1)
16
17 val lastReg = ref first
18
19 fun reset () = (lastReg := first; Array.modify(fn _ => ~1) preferred)
20
21 val _ = app (fn r => Array.update(allRegs,r,true)) available
22
23 fun getreg{pref,stamp:int,proh} =
24 let (* use preferred registers whenever possible *)
25 fun checkPreferred [] = find(!lastReg)
26 | checkPreferred(r::rs) =
27 if Array.sub(proh,r) <> stamp andalso
28 Array.sub(allRegs,r) then r
29 else checkPreferred rs
30
31 (* if not, use the round robin scheme to look for a register *)
32 and find(start) =
33 let val limit = Array.length allRegs
34 fun search r =
35 if Array.sub(proh,r) <> stamp andalso
36 Array.sub(allRegs,r) then r
37 else let val r = r+1
38 val r = if r >= limit then first else r
39 in if r = start then raise GetReg
40 else search r
41 end
42 val found = search(start)
43 val next = found + 1
44 val next = if next >= limit then first else next
45 in lastReg := next;
46 found
47 end
48 in checkPreferred pref
49 end
50
51 val lastRegPair = ref first
52
53 fun getpair{pref, stamp:int, proh} = let
54 (* if not, use the round robin scheme to look for a register *)
55 fun find(start) = let
56 val limit = Array.length allRegs
57 fun search r =
58 if Array.sub(proh,r) <> stamp
59 andalso Array.sub(proh,r+1) <> stamp
60 andalso Array.sub(allRegs,r)
61 andalso Array.sub(allRegs,r+1) then r
62 else let
63 val nxt = r+1
64 val nxtR = if nxt+1 >= limit then first else nxt
65 in
66 if nxtR = start then raise GetReg else search nxtR
67 end
68 val found = search(start)
69 val next = found + 1
70 val next = if next+1 >= limit then first else next
71 in
72 lastRegPair := next;
73 found
74 end
75 in find(!lastRegPair)
76 end
77
78 end
79
80
81
82