| 1 |
(* gasPseudoOps.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2006 The Fellowship of SML/NJ (www.smlnj.org) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Implements the string related functions to emit pseudo-ops |
| 7 |
* in the standard GAS syntax (see http://www.gnu.org/software/binutils/manual/gas-2.9.1/) |
| 8 |
*) |
| 9 |
|
| 10 |
functor GasPseudoOps ( |
| 11 |
structure T : MLTREE |
| 12 |
val labFmt : {gPrefix: string, aPrefix: string} |
| 13 |
) : AS_PSEUDO_OPS = |
| 14 |
struct |
| 15 |
structure T = T |
| 16 |
structure PB = PseudoOpsBasisTyp |
| 17 |
structure Fmt = Format |
| 18 |
|
| 19 |
fun error msg = MLRiscErrorMsg.error ("GasPseudoOps.", msg) |
| 20 |
|
| 21 |
fun prIntInf i = if IntInf.sign i < 0 then "-"^IntInf.toString(IntInf.~ i) |
| 22 |
else IntInf.toString i |
| 23 |
|
| 24 |
fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i |
| 25 |
|
| 26 |
(* operator precedences: |
| 27 |
Note: these differ from C's precedences |
| 28 |
3 NEG, NOTB |
| 29 |
2 MULS, DIVS, SLL, SRA |
| 30 |
1 ANDB, ORB, XORB |
| 31 |
0 PLUS, MINUS |
| 32 |
*) |
| 33 |
|
| 34 |
(* NOTE: we use ">=" here instead of ">" so that we don't have to worry about associativity *) |
| 35 |
fun parens (str, prec, op_prec) = |
| 36 |
if prec >= op_prec then "(" ^ str ^ ")" else str |
| 37 |
|
| 38 |
fun lexpToString le = toStr(le, 0) |
| 39 |
|
| 40 |
and toStr(T.LABEL lab, _) = Label.fmt labFmt lab |
| 41 |
| toStr(T.LABEXP le, p) = toStr(le, p) |
| 42 |
| toStr(T.NEG(_, T.CONST c), _) = |
| 43 |
(prInt(~(T.Constant.valueOf c)) handle _ => "-"^T.Constant.toString c) |
| 44 |
| toStr(T.NEG(_, T.LI i), _) = prIntInf(~i) |
| 45 |
| toStr(T.NEG(_, lexp), prec) = "-" ^ parens(toStr(lexp, 3), prec, 3) |
| 46 |
| toStr(T.NOTB(_, lexp), prec) = "~" ^ parens(toStr(lexp, 3), prec, 3) |
| 47 |
| toStr(T.CONST c, _) = |
| 48 |
(prInt(T.Constant.valueOf c) handle _ => T.Constant.toString c) |
| 49 |
| toStr(T.LI i, _) = prIntInf i |
| 50 |
| toStr(T.MULS(_,lexp1, lexp2), prec) = |
| 51 |
parens(toStr(lexp1, 2) ^ "*" ^ toStr(lexp2,2), prec, 2) |
| 52 |
| toStr(T.DIVS(T.DIV_TO_ZERO, _, lexp1, lexp2), prec) = |
| 53 |
parens(toStr(lexp1, 2) ^ "/" ^ toStr(lexp2,2), prec, 2) |
| 54 |
| toStr(T.SLL(_,lexp, cnt), prec) = |
| 55 |
parens(toStr(lexp,2) ^ "<<" ^ toStr(cnt,2), prec, 2) |
| 56 |
| toStr(T.SRA(_,lexp, cnt), prec) = |
| 57 |
parens(toStr(lexp,2) ^ ">>" ^ toStr(cnt,2), prec, 2) |
| 58 |
| toStr(T.ANDB(_,lexp, mask), prec) = |
| 59 |
parens(toStr(lexp,1) ^ "&" ^ toStr(mask, 1), prec, 1) |
| 60 |
| toStr(T.ORB(_,lexp, mask), prec) = |
| 61 |
parens(toStr(lexp, 1) ^ "|" ^ toStr(mask, 1), prec, 1) |
| 62 |
| toStr(T.XORB(_,lexp, mask), prec) = |
| 63 |
parens(toStr(lexp, 1) ^ "^" ^ toStr(mask, 1), prec, 1) |
| 64 |
| toStr(T.ADD(_,lexp1, lexp2), prec) = |
| 65 |
parens(toStr(lexp1, 0) ^ "+" ^ toStr(lexp2, 0), prec, 0) |
| 66 |
| toStr(T.SUB(_,lexp1, lexp2), prec) = |
| 67 |
parens(toStr(lexp1, 0) ^ "-" ^ toStr(lexp2, 0), prec, 0) |
| 68 |
| toStr _ = error "toStr" |
| 69 |
|
| 70 |
fun defineLabel lab = lexpToString (T.LABEL lab) ^ ":" |
| 71 |
|
| 72 |
fun decls (fmt, labs) = |
| 73 |
String.concat |
| 74 |
(map (fn lab => (Fmt.format fmt [Fmt.STR (lexpToString(T.LABEL lab))])) labs) |
| 75 |
|
| 76 |
(* if i is a power of two, return the lg of i, otherwise return nothing *) |
| 77 |
fun lg i = let |
| 78 |
fun loop (0, k) = SOME k |
| 79 |
| loop (i, k) = if i mod 2 = 0 |
| 80 |
then loop (i div 2, k+1) |
| 81 |
else NONE |
| 82 |
in |
| 83 |
loop (i, 0) |
| 84 |
end |
| 85 |
|
| 86 |
fun toString(PB.ALIGN_SZ n) = |
| 87 |
(* always favor p2align because it is guaranteed to be consistent across assemblers *) |
| 88 |
(case lg n |
| 89 |
of NONE => Fmt.format "\t.align\t%d" [Fmt.INT n] |
| 90 |
| SOME k => Fmt.format "\t.p2align\t%d" [Fmt.INT k] |
| 91 |
(* end case *)) |
| 92 |
| toString(PB.ALIGN_ENTRY) = "\t.align\t4" (* 16 byte boundary *) |
| 93 |
| toString(PB.ALIGN_LABEL) = "\t.p2align\t4,,7" |
| 94 |
|
| 95 |
| toString(PB.DATA_LABEL lab) = Label.fmt labFmt lab ^ ":" |
| 96 |
| toString(PB.DATA_READ_ONLY) = "\t.section\t.rodata" |
| 97 |
| toString(PB.DATA) = "\t.data" |
| 98 |
| toString(PB.BSS) = "\t.section\t.bss" |
| 99 |
| toString(PB.TEXT) = "\t.text" |
| 100 |
| toString(PB.SECTION at) = "\t.section\t" ^ Atom.toString at |
| 101 |
|
| 102 |
| toString(PB.REORDER) = "" |
| 103 |
| toString(PB.NOREORDER) = "" |
| 104 |
|
| 105 |
| toString(PB.INT{sz, i}) = let |
| 106 |
fun join [] = [] |
| 107 |
| join [lexp] = [lexpToString lexp] |
| 108 |
| join (lexp::r) = lexpToString lexp :: "," :: join r |
| 109 |
val pop = (case sz |
| 110 |
of 8 => "\t.byte\t" |
| 111 |
| 16 => "\t.short\t" |
| 112 |
| 32 => "\t.long\t" |
| 113 |
| 64 => "\t.quad\t" |
| 114 |
| n => error ("unexpected INT size: " ^ Int.toString n) |
| 115 |
(* end case *)) |
| 116 |
in |
| 117 |
String.concat (pop :: join i) |
| 118 |
end |
| 119 |
|
| 120 |
| toString(PB.ASCII s) = |
| 121 |
Fmt.format "\t.ascii\t\"%s\"" [Fmt.STR(String.toCString s)] |
| 122 |
| toString(PB.ASCIIZ s) = |
| 123 |
Fmt.format "\t.asciz\t\"%s\"" [Fmt.STR(String.toCString s)] |
| 124 |
|
| 125 |
| toString(PB.SPACE sz) = Fmt.format "\t.space\t%d" [Fmt.INT sz] |
| 126 |
|
| 127 |
| toString(PB.FLOAT{sz, f}) = let |
| 128 |
fun join [] = [] |
| 129 |
| join [f] = [f] |
| 130 |
| join (f::r) = f :: "," :: join r |
| 131 |
val pop = (case sz |
| 132 |
of 32 => "\t.single " |
| 133 |
| 64 => "\t.double " |
| 134 |
| 128 => "\t.extended " |
| 135 |
| n => error ("unexpected FLOAT size: " ^ Int.toString n) |
| 136 |
(* end case *)) |
| 137 |
in |
| 138 |
String.concat (pop :: join f) |
| 139 |
end |
| 140 |
|
| 141 |
| toString(PB.IMPORT labs) = decls("\t.extern\t%s", labs) |
| 142 |
| toString(PB.EXPORT labs) = decls("\t.global\t%s", labs) |
| 143 |
| toString(PB.COMMENT txt) = Fmt.format "/* %s */" [Fmt.STR txt] |
| 144 |
| toString(PB.EXT _) = error "EXT" |
| 145 |
|
| 146 |
end |