Debian Codegraph @

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

raw
1 (* darwin-pseudo-ops.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 Darwin (Mac OS X) assembler syntax.
8 *)
9
10 functor DarwinPseudoOps (T : MLTREE) : AS_PSEUDO_OPS =
11 struct
12 structure T = T
13 structure PB = PseudoOpsBasisTyp
14 structure Fmt = Format
15
16 fun error msg = MLRiscErrorMsg.error ("DarwinPseudoOps.", msg)
17
18 fun prIntInf i =
19 if IntInf.sign i < 0
20 then "-"^IntInf.toString(IntInf.~ i)
21 else IntInf.toString i
22
23 fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i
24
25 (* operator precedences follow C (which is different from gas!):
26 *
27 * 4 NEG, NOTB (unary)
28 * 3 MULS, DIVS
29 * 2 PLUS, MINUS
30 * 1 ANDB, ORB, XORB
31 * 0 LSHIFT, RSHIFT
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 concat["(", str, ")"] else str
37 fun parensBop (s1, s2, s3, prec, op_prec) =
38 if prec >= op_prec
39 then concat["(", s1, s2, s3, ")"]
40 else concat[s1, s2, s3]
41
42 val fmtLabel = Label.fmt {gPrefix="_",aPrefix="L"}
43
44 fun lexpToString le = toStr(le, 0)
45
46 and toStr(T.LABEL lab, _) = fmtLabel lab
47 | toStr(T.LABEXP le, p) = toStr(le, p)
48 | toStr(T.NEG(_, T.CONST c), _) =
49 (prInt(~(T.Constant.valueOf c)) handle _ => "-"^T.Constant.toString c)
50 | toStr(T.NEG(_, T.LI i), _) = prIntInf(~i)
51 | toStr(T.NEG(_, lexp), prec) = "-" ^ parens(toStr(lexp, 4), prec, 4)
52 | toStr(T.NOTB(_, lexp), prec) = "~" ^ parens(toStr(lexp, 4), prec, 4)
53 | toStr(T.CONST c, _) =
54 (prInt(T.Constant.valueOf c) handle _ => T.Constant.toString c)
55 | toStr(T.LI i, _) = prIntInf i
56 | toStr(T.MULS(_, lexp1, lexp2), prec) =
57 parensBop(toStr(lexp1, 3), "*", toStr(lexp2, 3), prec, 3)
58 | toStr(T.DIVS(T.DIV_TO_ZERO, _, lexp1, lexp2), prec) =
59 parensBop(toStr(lexp1, 3), "/", toStr(lexp2, 3), prec, 3)
60 | toStr(T.ADD(_, lexp1, lexp2), prec) =
61 parensBop(toStr(lexp1, 2), "+", toStr(lexp2, 2), prec, 2)
62 | toStr(T.SUB(_, lexp1, lexp2), prec) =
63 parensBop(toStr(lexp1, 2), "-", toStr(lexp2, 2), prec, 2)
64 | toStr(T.ANDB(_, lexp, mask), prec) =
65 parensBop(toStr(lexp, 1), "&", toStr(mask, 1), prec, 1)
66 | toStr(T.ORB(_, lexp, mask), prec) =
67 parensBop(toStr(lexp, 1), "|", toStr(mask, 1), prec, 1)
68 | toStr(T.XORB(_, lexp, mask), prec) =
69 parensBop(toStr(lexp, 1), "^", toStr(mask, 1), prec, 1)
70 | toStr(T.SLL(_, lexp, cnt), prec) =
71 parensBop(toStr(lexp, 0), "<<", toStr(cnt, 0), prec, 0)
72 | toStr(T.SRA(_, lexp, cnt), prec) =
73 parensBop(toStr(lexp, 0), ">>", toStr(cnt, 0), prec, 0)
74 | toStr _ = error "toStr"
75
76 fun defineLabel lab = lexpToString (T.LABEL lab) ^ ":"
77
78 fun decls (fmt, labs) =
79 String.concat
80 (map (fn lab => (Fmt.format fmt [Fmt.STR (lexpToString(T.LABEL lab))])) labs)
81
82 fun toString(PB.ALIGN_SZ n) = Fmt.format "\t.align\t%d" [Fmt.INT n]
83 | toString(PB.ALIGN_ENTRY) = "\t.align\t4" (* 16 byte boundary *)
84 | toString(PB.ALIGN_LABEL) = "\t.p2align\t4,,7"
85
86 | toString(PB.DATA_LABEL lab) = fmtLabel lab ^ ":"
87 | toString(PB.DATA_READ_ONLY) = "\t.const_data"
88 | toString(PB.DATA) = "\t.data"
89 | toString(PB.BSS) = raise Fail "BSS not supported; use DATA instead"
90 | toString(PB.TEXT) = "\t.text"
91 | toString(PB.SECTION at) = "\t.section\t" ^ Atom.toString at
92
93 | toString(PB.REORDER) = ""
94 | toString(PB.NOREORDER) = ""
95
96 | toString(PB.INT{sz, i}) = let
97 fun join [] = []
98 | join [lexp] = [lexpToString lexp]
99 | join (lexp::r) = lexpToString lexp :: "," :: join r
100 val pop = (case sz
101 of 8 => "\t.byte\t"
102 | 16 => "\t.short\t"
103 | 32 => "\t.long\t" (* NOTE: ".int" doesn't work on Mac OS X! *)
104 | 64 => "\t.quad\t"
105 | n => error ("unexpected INT size: " ^ Int.toString n)
106 (* end case *))
107 in
108 String.concat (pop :: join i)
109 end
110
111 | toString(PB.ASCII s) =
112 Fmt.format "\t.ascii\t\"%s\"" [Fmt.STR(String.toCString s)]
113 | toString(PB.ASCIIZ s) =
114 Fmt.format "\t.asciz\t\"%s\"" [Fmt.STR(String.toCString s)]
115
116 | toString(PB.SPACE sz) = Fmt.format "\t.space\t%d" [Fmt.INT sz]
117
118 | toString(PB.FLOAT{sz, f}) = let
119 fun join [] = []
120 | join [f] = [f]
121 | join (f::r) = f :: "," :: join r
122 val pop = (case sz
123 of 32 => "\t.single "
124 | 64 => "\t.double "
125 | n => error ("unexpected FLOAT size: " ^ Int.toString n)
126 (* end case *))
127 in
128 String.concat (pop :: join f)
129 end
130
131 | toString(PB.IMPORT labs) = decls("\t.extern\t%s", labs)
132 | toString(PB.EXPORT labs) = decls("\t.globl\t%s", labs)
133 | toString(PB.COMMENT txt) = Fmt.format "/* %s */" [Fmt.STR txt]
134
135
136 | toString(PB.EXT _) = error "EXT"
137
138 end