| 1 |
(* ppcDarwinPseudoOps.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2002 Bell labs, Lucent Technologies. |
| 4 |
* |
| 5 |
* PPC/Darwin (aka MacOS X) pseudo operations. |
| 6 |
*) |
| 7 |
|
| 8 |
functor PPCDarwinPseudoOps ( |
| 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 |
) : PSEUDO_OPS_BASIS = struct |
| 32 |
|
| 33 |
structure T = T |
| 34 |
structure PB = PseudoOpsBasisTyp |
| 35 |
structure Fmt = Format |
| 36 |
|
| 37 |
structure Endian = PseudoOpsBig ( |
| 38 |
structure T = T |
| 39 |
structure MLTreeEval=MLTreeEval |
| 40 |
val icache_alignment = 16 |
| 41 |
val max_alignment = SOME 7 |
| 42 |
val nop = {sz=4, en=0wx60000000: Word32.word}) (* FIX:: ori 0, 0, 0 *) |
| 43 |
|
| 44 |
(* EXPAND |
| 45 |
structure GasPseudoOps = |
| 46 |
GasPseudoOps(structure T = T |
| 47 |
val labFmt = {gPrefix="", aPrefix="L"}) |
| 48 |
*) |
| 49 |
|
| 50 |
type 'a pseudo_op = (T.labexp, 'a) PB.pseudo_op |
| 51 |
|
| 52 |
fun error msg = MLRiscErrorMsg.error ("PPCDarwinPseudoOps.", msg) |
| 53 |
|
| 54 |
val sizeOf = Endian.sizeOf |
| 55 |
val emitValue = Endian.emitValue |
| 56 |
|
| 57 |
val labelToString = Label.fmt {gPrefix="", aPrefix="L"} |
| 58 |
|
| 59 |
fun prIntInf i = |
| 60 |
if IntInf.sign i < 0 then "-"^IntInf.toString(IntInf.~ i) |
| 61 |
else IntInf.toString i |
| 62 |
|
| 63 |
fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i |
| 64 |
|
| 65 |
(* operator precedences: |
| 66 |
Note: these differ from C's precedences |
| 67 |
2 MULT, DIV, LSHIFT, RSHIFT |
| 68 |
1 AND, OR |
| 69 |
0 PLUS, MINUS |
| 70 |
*) |
| 71 |
|
| 72 |
fun parens (str, prec, op_prec) = |
| 73 |
if prec > op_prec then "(" ^ str ^ ")" else str |
| 74 |
|
| 75 |
fun lexpToString le = toStr(le, 0) |
| 76 |
|
| 77 |
and toStr(T.LABEL lab, _) = labelToString lab |
| 78 |
| toStr(T.LABEXP le, p) = toStr(le, p) |
| 79 |
| toStr(T.CONST c, _) = |
| 80 |
(prInt(T.Constant.valueOf c) handle _ => T.Constant.toString c) |
| 81 |
| toStr(T.LI i, _) = prIntInf i |
| 82 |
| toStr(T.MULS(_,lexp1, lexp2), _) = toStr(lexp1, 2) ^ "*" ^ toStr(lexp2,2) |
| 83 |
| toStr(T.DIVS(T.DIV_TO_ZERO,_,lexp1, lexp2), _) = |
| 84 |
toStr(lexp1, 2) ^ "/" ^ toStr(lexp2,2) |
| 85 |
| toStr(T.SLL(_,lexp, cnt), prec) = toStr(lexp,2) ^ "<<" ^ toStr(cnt,2) |
| 86 |
| toStr(T.SRL(_,lexp, cnt), prec) = toStr(lexp,2) ^ ">>" ^ toStr(cnt,2) |
| 87 |
| toStr(T.ANDB(_,lexp, mask), prec) = |
| 88 |
parens(toStr(lexp,1) ^ "&" ^ toStr(mask, 1), prec, 1) |
| 89 |
| toStr(T.ORB(_,lexp, mask), prec) = |
| 90 |
parens(toStr(lexp, 1) ^ "|" ^ toStr(mask, 1), prec, 1) |
| 91 |
| toStr(T.ADD(_,lexp1, lexp2), prec) = |
| 92 |
parens(toStr(lexp1, 0) ^ "+" ^ toStr(lexp2, 0), prec, 0) |
| 93 |
| toStr(T.SUB(_,lexp1, lexp2), prec) = |
| 94 |
parens(toStr(lexp1, 0) ^ "-" ^ toStr(lexp2, 0), prec, 0) |
| 95 |
| toStr _ = error "toStr" |
| 96 |
|
| 97 |
fun decls (fmt, labs) = |
| 98 |
String.concat |
| 99 |
(map (fn lab => (Fmt.format fmt [Fmt.STR (lexpToString(T.LABEL lab))])) labs) |
| 100 |
|
| 101 |
fun toString(PB.ALIGN_SZ n) = Fmt.format "\t.align\t%d" [Fmt.INT n] |
| 102 |
| toString(PB.ALIGN_ENTRY) = "\t.align\t4" (* 16 byte boundary *) |
| 103 |
| toString(PB.ALIGN_LABEL) = "\t.align\t2" |
| 104 |
|
| 105 |
| toString(PB.DATA_LABEL lab) = labelToString lab ^ ":" |
| 106 |
| toString(PB.DATA_READ_ONLY) = "\t.const_data" |
| 107 |
| toString(PB.DATA) = "\t.data" |
| 108 |
| toString(PB.BSS) = "\t.section\t__DATA,__BSS" |
| 109 |
| toString(PB.TEXT) = "\t.text" |
| 110 |
| toString(PB.SECTION at) = "\t.section\t" ^ Atom.toString at |
| 111 |
| toString(PB.REORDER) = "" |
| 112 |
| toString(PB.NOREORDER) = "" |
| 113 |
| toString(PB.INT{sz, i}) = let |
| 114 |
fun join [] = [] |
| 115 |
| join [lexp] = [lexpToString lexp] |
| 116 |
| join (lexp::r) = lexpToString lexp :: "," :: join r |
| 117 |
val pop = (case sz |
| 118 |
of 8 => "\t.byte\t" |
| 119 |
| 16 => "\t.short\t" |
| 120 |
| 32 => "\t.long\t" |
| 121 |
| 64 => error "INT64" |
| 122 |
| _ => error ("pop: INT sz = " ^ Int.toString sz) |
| 123 |
(* end case *)) |
| 124 |
in |
| 125 |
String.concat (pop :: join i) |
| 126 |
end |
| 127 |
| toString(PB.ASCII s) = |
| 128 |
Fmt.format "\t.ascii\t\"%s\"" [Fmt.STR(String.toCString s)] |
| 129 |
| toString(PB.ASCIIZ s) = |
| 130 |
Fmt.format "\t.asciz \"%s\"" [Fmt.STR(String.toCString s)] |
| 131 |
| toString(PB.SPACE sz) = Fmt.format "\t.space\t%d" [Fmt.INT sz] |
| 132 |
| toString(PB.FLOAT{sz, f}) = let |
| 133 |
fun join [] = [] |
| 134 |
| join [f] = [f] |
| 135 |
| join (f::r) = f :: "," :: join r |
| 136 |
val pop = (case sz |
| 137 |
of 32 => "\t.single " |
| 138 |
| 64 => "\t.double " |
| 139 |
| _ => error ("pop: FLOAT sz = " ^ Int.toString sz) |
| 140 |
(* end case *)) |
| 141 |
in |
| 142 |
String.concat (pop :: join f) |
| 143 |
end |
| 144 |
| toString(PB.IMPORT labs) = decls("\t.extern\t%s", labs) |
| 145 |
| toString(PB.EXPORT labs) = decls("\t.globl\t%s", labs) |
| 146 |
| toString(PB.COMMENT txt) = Fmt.format "; %s" [Fmt.STR txt] |
| 147 |
| toString(PB.EXT _) = error "EXT" |
| 148 |
|
| 149 |
fun defineLabel lab = labelToString lab ^ ":" |
| 150 |
|
| 151 |
val wordSize = 32 |
| 152 |
end |