Debian Codegraph @

lib/mlrisc-lib/MLRISC/flowgraph/pseudo-ops-big.sml

raw
1 (* pseudo-ops-big.sml
2 *
3 * COPYRIGHT (c) 2001 Lucent Technologies, Bell Laboratories.
4 *
5 * Subset of pseudo-ops functions that are little endian sensitive
6 *)
7
8 functor PseudoOpsBig
9 ( structure T : MLTREE
10 structure MLTreeEval : MLTREE_EVAL (* where T = T *)
11 where type T.Basis.cond = T.Basis.cond
12 and type T.Basis.div_rounding_mode = T.Basis.div_rounding_mode
13 and type T.Basis.ext = T.Basis.ext
14 and type T.Basis.fcond = T.Basis.fcond
15 and type T.Basis.rounding_mode = T.Basis.rounding_mode
16 and type T.Constant.const = T.Constant.const
17 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) T.Extension.ccx
18 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) T.Extension.fx
19 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) T.Extension.rx
20 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) T.Extension.sx
21 and type T.I.div_rounding_mode = T.I.div_rounding_mode
22 and type T.Region.region = T.Region.region
23 and type T.ccexp = T.ccexp
24 and type T.fexp = T.fexp
25 (* and type T.labexp = T.labexp *)
26 and type T.mlrisc = T.mlrisc
27 and type T.oper = T.oper
28 and type T.rep = T.rep
29 and type T.rexp = T.rexp
30 and type T.stm = T.stm
31 val icache_alignment : int (* cache line size *)
32 val max_alignment : int option (* maximum alignment for internal labels *)
33 val nop: {sz:int, en:Word32.word} (* encoding for noop *)
34 ) : PSEUDO_OPS_ENDIAN =
35 struct
36 structure W = Word
37 structure T = T
38 structure PB = PseudoOpsBasisTyp
39 type 'a pseudo_op = (T.labexp, 'a) PB.pseudo_op
40 fun error msg = MLRiscErrorMsg.error ("PseudoOpsBig.", msg)
41
42 val >> = Word.>>
43 val ~>> = Word.~>>
44 val & = Word.andb
45 infix >> ~>> &
46
47 (* return loc aligned at bndry *)
48 fun align(loc, bndry) = let
49 val mask = W.fromInt bndry - 0w1
50 in W.toIntX(W.andb(W.fromInt loc + mask, W.notb mask))
51 end
52
53 (* bytes of padding required *)
54 fun padding(loc, bndry) = align(loc, bndry) - loc
55
56 fun pow2(x, 0) = x
57 | pow2(x, n) = pow2(x * 2, n-1)
58
59 fun bytesIn sz = Int.quot(sz, 8)
60
61 fun sizeOf(pOp, loc) =
62 (case pOp
63 of PB.ALIGN_SZ n => padding(loc, pow2(1, n))
64 | PB.ALIGN_ENTRY => padding(loc, icache_alignment)
65 | PB.ALIGN_LABEL => let
66 val pad = padding(loc, icache_alignment)
67 in
68 case max_alignment
69 of NONE => pad
70 | SOME m => if pad <= m then pad else 0
71 end
72
73 | PB.INT{sz, i} => length(i) * bytesIn sz
74
75 | PB.ASCII s => String.size s
76 | PB.ASCIIZ s => String.size s + 1
77
78 | PB.SPACE(sz) => sz
79
80 | PB.FLOAT{sz, f} => length(f) * bytesIn sz
81
82 | PB.EXT _ => error "sizeOf: EXT"
83 | _ => 0
84 (*esac*))
85
86
87 fun emitValue{pOp, loc, emit} = let
88 val itow = W.fromInt
89 val toword8 = Word8.fromLargeWord o Word.toLargeWord o itow
90 fun emitByte n = emit (Word8.fromLargeWord (Word.toLargeWord n))
91 fun emitWord w = (emitByte((w >> 0w8) & 0w255); emitByte(w & 0w255))
92
93 fun emitLongX n = let val w = itow n
94 in emitWord(w ~>> 0w16); emitWord(w & 0w65535)
95 end
96
97 local
98 val {sz, en} = nop
99 val toWord = W.fromLargeInt o Word32.toLargeIntX
100 in
101 fun emitNop () =
102 case sz
103 of 1 => emitByte (toWord en)
104 | 2 => emitWord (toWord en)
105 | 4 => (emitWord(toWord(Word32.andb(en, 0w65535)));
106 emitWord(toWord(Word32.>>(en, 0w16))))
107 | n => error ("emitNop : sz = " ^ Int.toString n)
108
109 fun insertNops 0 = ()
110 | insertNops n =
111 if n >= sz then (emitNop(); insertNops(n-sz))
112 else error "insertNops"
113 end
114
115 fun align(loc, bndry) = let
116 val bndry = Word.fromInt bndry
117 val mask = bndry - 0w1
118 in
119 case W.andb(itow(loc), mask)
120 of 0w0 => ()
121 | w => let
122 val padSz = (bndry - w)
123 in insertNops(Word.toInt padSz)
124 end
125 (*esac*)
126 end
127
128 val {ccexp, rexp} =
129 MLTreeEval.eval
130 {const = IntInf.fromInt o T.Constant.valueOf,
131 label = Label.addrOf}
132 in
133 case pOp
134 of PB.ALIGN_SZ n => insertNops(sizeOf(pOp, loc))
135 | PB.ALIGN_ENTRY => insertNops(sizeOf(pOp, loc))
136 | PB.ALIGN_LABEL => insertNops(sizeOf(pOp, loc))
137
138 | PB.INT{sz, i} => let
139 val ints = map (IntInf.toInt o rexp) i
140 in
141 case sz
142 of 8 => app (emitByte o itow) ints
143 | 16 => app (emitWord o itow) ints
144 | 32 => app emitLongX ints
145 | _ => error "emitValue: INT 64"
146 (*esac*)
147 end
148
149 | PB.ASCII s => app (emit o Word8.fromInt o Char.ord) (String.explode s)
150 | PB.ASCIIZ s => (emitValue{pOp=PB.ASCII s, loc=loc, emit=emit}; emit 0w0)
151
152 | PB.FLOAT{sz, f} => error "emitValue: FLOAT - not implemented"
153 | PB.EXT _ => error "emitValue: EXT"
154 | PB.SPACE _ => error "emitValue: SPACE"
155 | _ => ()
156 end
157
158
159 end