Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /MLRISC/trunk/flowgraph/gasPseudoOps.sml
ViewVC logotype

Annotation of /MLRISC/trunk/flowgraph/gasPseudoOps.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2778 - (view) (download)

1 : george 991 (* gasPseudoOps.sml
2 :     *
3 : jhr 1921 * COPYRIGHT (c) 2006 The Fellowship of SML/NJ (www.smlnj.org)
4 :     * All rights reserved.
5 : george 991 *
6 :     * Implements the string related functions to emit pseudo-ops
7 : jhr 1921 * in the standard GAS syntax (see http://www.gnu.org/software/binutils/manual/gas-2.9.1/)
8 : george 991 *)
9 : jhr 1921
10 :     functor GasPseudoOps (
11 : george 984 structure T : MLTREE
12 :     val labFmt : {gPrefix: string, aPrefix: string}
13 : jhr 1921 ) : AS_PSEUDO_OPS =
14 :     struct
15 :     structure T = T
16 :     structure PB = PseudoOpsBasisTyp
17 :     structure Fmt = Format
18 : george 984
19 : jhr 1921 fun error msg = MLRiscErrorMsg.error ("GasPseudoOps.", msg)
20 : george 984
21 : jhr 1921 fun prIntInf i = if IntInf.sign i < 0 then "-"^IntInf.toString(IntInf.~ i)
22 :     else IntInf.toString i
23 : george 984
24 : jhr 1921 fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i
25 : george 984
26 : jhr 1921 (* 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 : george 984
34 : jhr 1921 (* 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 : george 984
38 : jhr 1921 fun lexpToString le = toStr(le, 0)
39 : george 984
40 : jhr 1921 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 : george 984
70 : jhr 1921 fun defineLabel lab = lexpToString (T.LABEL lab) ^ ":"
71 : george 984
72 : jhr 1921 fun decls (fmt, labs) =
73 :     String.concat
74 :     (map (fn lab => (Fmt.format fmt [Fmt.STR (lexpToString(T.LABEL lab))])) labs)
75 : george 984
76 : jhr 1921 fun toString(PB.ALIGN_SZ n) = Fmt.format "\t.align\t%d" [Fmt.INT n]
77 :     | toString(PB.ALIGN_ENTRY) = "\t.align\t4" (* 16 byte boundary *)
78 :     | toString(PB.ALIGN_LABEL) = "\t.p2align\t4,,7"
79 : george 984
80 : jhr 1921 | toString(PB.DATA_LABEL lab) = Label.fmt labFmt lab ^ ":"
81 :     | toString(PB.DATA_READ_ONLY) = "\t.section\t.rodata"
82 :     | toString(PB.DATA) = "\t.data"
83 :     | toString(PB.BSS) = "\t.section\t.bss"
84 :     | toString(PB.TEXT) = "\t.text"
85 :     | toString(PB.SECTION at) = "\t.section\t" ^ Atom.toString at
86 : george 984
87 : jhr 1921 | toString(PB.REORDER) = ""
88 :     | toString(PB.NOREORDER) = ""
89 : george 984
90 : jhr 1921 | toString(PB.INT{sz, i}) = let
91 :     fun join [] = []
92 :     | join [lexp] = [lexpToString lexp]
93 :     | join (lexp::r) = lexpToString lexp :: "," :: join r
94 :     val pop = (case sz
95 :     of 8 => "\t.byte\t"
96 :     | 16 => "\t.short\t"
97 : jhr 2778 | 32 => "\t.long\t"
98 : jhr 1921 | 64 => "\t.quad\t"
99 :     | n => error ("unexpected INT size: " ^ Int.toString n)
100 :     (* end case *))
101 :     in
102 :     String.concat (pop :: join i)
103 :     end
104 : george 984
105 : jhr 1921 | toString(PB.ASCII s) =
106 :     Fmt.format "\t.ascii\t\"%s\"" [Fmt.STR(String.toCString s)]
107 :     | toString(PB.ASCIIZ s) =
108 : jhr 2778 Fmt.format "\t.asciz\t\"%s\"" [Fmt.STR(String.toCString s)]
109 : george 984
110 : jhr 1921 | toString(PB.SPACE sz) = Fmt.format "\t.space\t%d" [Fmt.INT sz]
111 : george 1012
112 : jhr 1921 | toString(PB.FLOAT{sz, f}) = let
113 :     fun join [] = []
114 :     | join [f] = [f]
115 :     | join (f::r) = f :: "," :: join r
116 :     val pop = (case sz
117 :     of 32 => "\t.single "
118 :     | 64 => "\t.double "
119 :     | 128 => "\t.extended "
120 :     | n => error ("unexpected FLOAT size: " ^ Int.toString n)
121 :     (* end case *))
122 :     in
123 :     String.concat (pop :: join f)
124 :     end
125 : george 984
126 : jhr 1921 | toString(PB.IMPORT labs) = decls("\t.extern\t%s", labs)
127 : jhr 2775 | toString(PB.EXPORT labs) = decls("\t.globl\t%s", labs)
128 : jhr 1921 | toString(PB.COMMENT txt) = Fmt.format "/* %s */" [Fmt.STR txt]
129 :     | toString(PB.EXT _) = error "EXT"
130 :    
131 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0