2 * Expression and value printing.
4 * ----------------------------------------------------------------
7 * Copyright (C) 2001 Jason Hickey, Caltech
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Author: Jason Hickey
24 * @email{jyh@cs.caltech.edu}
37 let pp_print_label buf v
=
38 Format.pp_print_string buf
(string_of_symbol v
)
43 let pp_print_value buf v
=
46 Format.fprintf buf
"abs(%s)" (Int32.format
"0x%08x" i
)
47 | ValSection
(sect
, off
) ->
48 Format.fprintf buf
"section(%s+%s)" (**)
49 (string_of_symbol sect
)
50 (Int32.format
"0x%08x" off
)
51 | ValLabel
(v
, off
) ->
52 Format.fprintf buf
"label(%s+%s)" (string_of_symbol v
) (Int32.format
"0x%08x" off
)
53 | ValRelative
(v1
, v2
, off
) ->
54 Format.fprintf buf
"relative(%s-%s+%s)" (**)
57 (Int32.format
"0x%08x" off
)
59 Format.fprintf buf
"undefined"
62 * Print an expression.
64 let rec pp_print_exp buf e
=
67 Format.pp_print_string buf
(Int32.format
"0x%08x" i
)
69 Format.pp_print_string buf
(string_of_symbol v
)
71 Format.pp_print_string buf
"-";
74 Format.pp_print_string buf
"~";
77 Format.pp_print_string buf
"abs(";
79 Format.pp_print_string buf
")"
81 Format.pp_print_string buf
"log2(";
83 Format.pp_print_string buf
")"
85 Format.pp_print_string buf
"pow2(";
87 Format.pp_print_string buf
")"
89 pp_print_binary buf
"+" e1 e2
91 pp_print_binary buf
"-" e1 e2
93 pp_print_rel buf
".rel." e l
95 pp_print_binary buf
"*" e1 e2
97 pp_print_binary buf
"/" e1 e2
99 pp_print_binary buf
"<<" e1 e2
101 pp_print_binary buf
">>a" e1 e2
103 pp_print_binary buf
">>l" e1 e2
105 pp_print_binary buf
"&" e1 e2
107 pp_print_binary buf
"|" e1 e2
109 pp_print_binary buf
"^" e1 e2
110 | ExpAlign
(e1
, e2
) ->
111 pp_print_binary buf
".bfd_align." e1 e2
113 and pp_print_binary buf op e1 e2
=
114 Format.pp_open_hvbox buf
1;
115 Format.pp_print_string buf
"(";
117 Format.pp_print_space buf
();
118 Format.pp_print_string buf op
;
119 Format.pp_print_string buf
" ";
121 Format.pp_print_string buf
")";
122 Format.pp_close_box buf
()
124 and pp_print_rel buf op e l
=
125 Format.pp_open_hvbox buf
1;
126 Format.pp_print_string buf
"(";
128 Format.pp_print_space buf
();
129 Format.pp_print_string buf op
;
130 Format.pp_print_string buf
" ";
131 Format.pp_print_string buf
(string_of_symbol l
);
132 Format.pp_print_string buf
")";
133 Format.pp_close_box buf
()
136 * Print a relocation.
138 let pp_print_reloc_type buf reloc_type
=
140 match reloc_type
with
142 | RelocInt16
-> "int16"
143 | RelocInt24
-> "int24"
144 | RelocInt32
-> "int32"
145 | RelocInt64
-> "int64"
147 Format.pp_print_string buf
s
149 let pp_print_reloc buf reloc
=
150 let { reloc_type
= reloc_type
;
156 Format.pp_open_hvbox buf
3;
157 Format.fprintf buf
"%a,@ %d, %d,@ %a" (**)
158 pp_print_reloc_type reloc_type
162 Format.pp_close_box buf
()
167 let pp_print_interval buf
= function
169 Format.pp_print_string buf
"_"
170 | IntRange
(lower
, upper
) ->
171 Format.pp_print_string buf
"[";
172 Format.pp_print_string buf
(Int32.format
"0x%08x" lower
);
173 Format.pp_print_string buf
", ";
174 Format.pp_print_string buf
(Int32.format
"0x%08x" upper
);
175 Format.pp_print_string buf
"]"
178 * Print a symbol type.
180 let pp_print_sym_type buf t
=
182 SymGlobal
-> "global"
183 | SymUndefined
-> "undefined"
184 | SymLocal
-> "local"
187 Format.pp_print_string buf
s
192 let rec pp_print_item buf item
=
195 Format.fprintf buf
"bfd_align(%d)" i
196 | BufSkip
(bfd_skip
, fill
) ->
197 Format.fprintf buf
"bfd_skip(%d,%d)" bfd_skip fill
198 | BufChoice
(e
, choices
) ->
199 Format.pp_open_hvbox buf
3;
200 Format.pp_print_string buf
"choice(";
202 Format.pp_print_string buf
";";
203 List.iter
(fun { choice_enabled
= enabled
;
204 choice_interval
= interval
;
205 choice_buffer
= buffer
207 Format.pp_print_space buf
();
208 Format.pp_open_hvbox buf
3;
209 pp_print_interval buf interval
;
210 Format.pp_print_string buf
" ->";
211 Format.pp_print_space buf
();
212 pp_print_buffer buf buffer
;
213 Format.pp_close_box buf
()) choices
;
214 Format.pp_print_string buf
")";
215 Format.pp_close_box buf
()
216 | BufLabel
(sym_type
, label
) ->
217 Format.fprintf buf
"%a: %a" (**)
219 pp_print_sym_type sym_type
220 | BufEqu
(sym_type
, label
, e
) ->
221 Format.fprintf buf
".equ(%a,%a,%a)" (**)
222 pp_print_sym_type sym_type
225 | BufCommon
(sym_type
, label
, i
) ->
226 Format.fprintf buf
".comm(%a,%a,%d)" (**)
227 pp_print_sym_type sym_type
231 Format.fprintf buf
".reloc(%a)" (**)
235 Format.fprintf buf
".data %s" (String.escaped
s)
237 Format.fprintf buf
".ascii \"%s\"" (String.escaped
s)
239 Format.fprintf buf
"\t.int8 0x%02x" i
241 Format.fprintf buf
".int16 %d" i
243 Format.fprintf buf
".int24 %d" i
245 Format.fprintf buf
".int32 %s" (Int32.to_string i
)
247 Format.fprintf buf
".int64 %s" (Int64.to_string i
)
249 Format.fprintf buf
".float32 %s" (Float80.to_string x
)
251 Format.fprintf buf
".float64 %s" (Float80.to_string x
)
253 Format.fprintf buf
".float80 %s" (Float80.to_string x
)
258 and pp_print_buffer buf buffer
=
259 let items = buffer
.buf_forward_list
@ List.rev buffer
.buf_reverse_list
in
260 ignore
(List.fold_left
(fun first item
->
262 pp_print_space buf
();
263 pp_print_item buf item
;
269 let pp_print_sect buf sect
=
270 let { sect_name
= name
;
274 Format.pp_open_vbox buf
3;
275 Format.fprintf buf
".section %a" pp_print_label name
;
276 Format.pp_print_space buf
();
277 pp_print_buffer buf buffer
;
278 Format.pp_close_box buf
()
282 * Print all the sections.
284 let pp_print_bfd buf bfd
=
285 let { bfd_sections
= sects
} = bfd
in
286 Format.pp_open_hvbox buf
3;
287 Format.pp_print_string buf
"BFD:";
288 SymbolTable.iter
(fun _ sect
->
289 Format.pp_print_space buf
();
292 pp_print_sect buf sect
294 Format.pp_print_string buf
".section <value>") sects
;
295 Format.pp_close_box buf
()
302 * Caml-master: "compile"