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