Initial snarf.
[shack.git] / arch / bfd / util / bfd_print.ml
blobf6f308deffe1721b810758764e7d633f26e05f5a
1 (*
2 * Expression and value printing.
4 * ----------------------------------------------------------------
6 * @begin[license]
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}
25 * @end[license]
27 open Format
29 open Symbol
31 open Bfd
32 open Bfd_type
35 * Print a label.
37 let pp_print_label buf v =
38 Format.pp_print_string buf (string_of_symbol v)
41 * Print a value.
43 let pp_print_value buf v =
44 match v with
45 ValAbsolute i ->
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)" (**)
55 (string_of_symbol v1)
56 (string_of_symbol v2)
57 (Int32.format "0x%08x" off)
58 | ValUndefined ->
59 Format.fprintf buf "undefined"
62 * Print an expression.
64 let rec pp_print_exp buf e =
65 match e with
66 ExpInt32 i ->
67 Format.pp_print_string buf (Int32.format "0x%08x" i)
68 | ExpLabel v ->
69 Format.pp_print_string buf (string_of_symbol v)
70 | ExpUMinus e ->
71 Format.pp_print_string buf "-";
72 pp_print_exp buf e
73 | ExpUNot e ->
74 Format.pp_print_string buf "~";
75 pp_print_exp buf e
76 | ExpUAbs e ->
77 Format.pp_print_string buf "abs(";
78 pp_print_exp buf e;
79 Format.pp_print_string buf ")"
80 | ExpULog2 e ->
81 Format.pp_print_string buf "log2(";
82 pp_print_exp buf e;
83 Format.pp_print_string buf ")"
84 | ExpUPow2 e ->
85 Format.pp_print_string buf "pow2(";
86 pp_print_exp buf e;
87 Format.pp_print_string buf ")"
88 | ExpAdd (e1, e2) ->
89 pp_print_binary buf "+" e1 e2
90 | ExpSub (e1, e2) ->
91 pp_print_binary buf "-" e1 e2
92 | ExpRel (e, l) ->
93 pp_print_rel buf ".rel." e l
94 | ExpMul (e1, e2) ->
95 pp_print_binary buf "*" e1 e2
96 | ExpDiv (e1, e2) ->
97 pp_print_binary buf "/" e1 e2
98 | ExpSal (e1, e2) ->
99 pp_print_binary buf "<<" e1 e2
100 | ExpSar (e1, e2) ->
101 pp_print_binary buf ">>a" e1 e2
102 | ExpShr (e1, e2) ->
103 pp_print_binary buf ">>l" e1 e2
104 | ExpAnd (e1, e2) ->
105 pp_print_binary buf "&" e1 e2
106 | ExpOr (e1, e2) ->
107 pp_print_binary buf "|" e1 e2
108 | ExpXor (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 "(";
116 pp_print_exp buf e1;
117 Format.pp_print_space buf ();
118 Format.pp_print_string buf op;
119 Format.pp_print_string buf " ";
120 pp_print_exp buf e2;
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 "(";
127 pp_print_exp buf e;
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 =
139 let s =
140 match reloc_type with
141 RelocInt8 -> "int8"
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;
151 reloc_pos = pos;
152 reloc_offset = off;
153 reloc_exp = e
154 } = reloc
156 Format.pp_open_hvbox buf 3;
157 Format.fprintf buf "%a,@ %d, %d,@ %a" (**)
158 pp_print_reloc_type reloc_type
161 pp_print_exp e;
162 Format.pp_close_box buf ()
165 * Print an interval.
167 let pp_print_interval buf = function
168 IntAll ->
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 =
181 let s = match t with
182 SymGlobal -> "global"
183 | SymUndefined -> "undefined"
184 | SymLocal -> "local"
185 | SymTemp -> "temp"
187 Format.pp_print_string buf s
190 * Print an item.
192 let rec pp_print_item buf item =
193 match item with
194 BufAlign i ->
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(";
201 pp_print_exp buf e;
202 Format.pp_print_string buf ";";
203 List.iter (fun { choice_enabled = enabled;
204 choice_interval = interval;
205 choice_buffer = buffer
206 } ->
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" (**)
218 pp_print_label label
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
223 pp_print_label label
224 pp_print_exp e
225 | BufCommon (sym_type, label, i) ->
226 Format.fprintf buf ".comm(%a,%a,%d)" (**)
227 pp_print_sym_type sym_type
228 pp_print_label label
230 | BufReloc reloc ->
231 Format.fprintf buf ".reloc(%a)" (**)
232 pp_print_reloc reloc
234 | BufData s ->
235 Format.fprintf buf ".data %s" (String.escaped s)
236 | BufAscii s ->
237 Format.fprintf buf ".ascii \"%s\"" (String.escaped s)
238 | BufInt8 i ->
239 Format.fprintf buf "\t.int8 0x%02x" i
240 | BufInt16 i ->
241 Format.fprintf buf ".int16 %d" i
242 | BufInt24 i ->
243 Format.fprintf buf ".int24 %d" i
244 | BufInt32 i ->
245 Format.fprintf buf ".int32 %s" (Int32.to_string i)
246 | BufInt64 i ->
247 Format.fprintf buf ".int64 %s" (Int64.to_string i)
248 | BufFloat32 x ->
249 Format.fprintf buf ".float32 %s" (Float80.to_string x)
250 | BufFloat64 x ->
251 Format.fprintf buf ".float64 %s" (Float80.to_string x)
252 | BufFloat80 x ->
253 Format.fprintf buf ".float80 %s" (Float80.to_string x)
256 * Print a buffer.
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 ->
261 if not first then
262 pp_print_space buf ();
263 pp_print_item buf item;
264 false) true items)
267 * Print a section.
269 let pp_print_sect buf sect =
270 let { sect_name = name;
271 sect_data = buffer
272 } = sect
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 ()
281 * Print the bfd.
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 ();
290 match sect with
291 RawSection sect ->
292 pp_print_sect buf sect
293 | ValueSection _ ->
294 Format.pp_print_string buf ".section <value>") sects;
295 Format.pp_close_box buf ()
298 * @docoff
300 * -*-
301 * Local Variables:
302 * Caml-master: "compile"
303 * End:
304 * -*-