OSDN Git Service

2011-07-24 François Dumont <francois.cppdevs@free.fr>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2asm.c
1 /* Dwarf2 assembler output helper routines.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "output.h"
30 #include "target.h"
31 #include "dwarf2asm.h"
32 #include "dwarf2.h"
33 #include "splay-tree.h"
34 #include "ggc.h"
35 #include "tm_p.h"
36
37 \f
38 /* Output an unaligned integer with the given value and size.  Prefer not
39    to print a newline, since the caller may want to add a comment.  */
40
41 void
42 dw2_assemble_integer (int size, rtx x)
43 {
44   const char *op = integer_asm_op (size, FALSE);
45
46   if (op)
47     {
48       fputs (op, asm_out_file);
49       if (CONST_INT_P (x))
50         fprintf (asm_out_file, HOST_WIDE_INT_PRINT_HEX,
51                  (unsigned HOST_WIDE_INT) INTVAL (x));
52       else
53         output_addr_const (asm_out_file, x);
54     }
55   else
56     assemble_integer (x, size, BITS_PER_UNIT, 1);
57 }
58
59
60 /* Output a value of a given size in target byte order.  */
61
62 void
63 dw2_asm_output_data_raw (int size, unsigned HOST_WIDE_INT value)
64 {
65   unsigned char bytes[8];
66   int i;
67
68   for (i = 0; i < 8; ++i)
69     {
70       bytes[i] = value & 0xff;
71       value >>= 8;
72     }
73
74   if (BYTES_BIG_ENDIAN)
75     {
76       for (i = size - 1; i > 0; --i)
77         fprintf (asm_out_file, "%#x,", bytes[i]);
78       fprintf (asm_out_file, "%#x", bytes[0]);
79     }
80   else
81     {
82       for (i = 0; i < size - 1; ++i)
83         fprintf (asm_out_file, "%#x,", bytes[i]);
84       fprintf (asm_out_file, "%#x", bytes[i]);
85     }
86 }
87
88 /* Output an immediate constant in a given SIZE in bytes.  */
89
90 void
91 dw2_asm_output_data (int size, unsigned HOST_WIDE_INT value,
92                      const char *comment, ...)
93 {
94   va_list ap;
95   const char *op = integer_asm_op (size, FALSE);
96
97   va_start (ap, comment);
98
99   if (size * 8 < HOST_BITS_PER_WIDE_INT)
100     value &= ~(~(unsigned HOST_WIDE_INT) 0 << (size * 8));
101
102   if (op)
103     fprintf (asm_out_file, "%s" HOST_WIDE_INT_PRINT_HEX, op, value);
104   else
105     assemble_integer (GEN_INT (value), size, BITS_PER_UNIT, 1);
106
107   if (flag_debug_asm && comment)
108     {
109       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
110       vfprintf (asm_out_file, comment, ap);
111     }
112   fputc ('\n', asm_out_file);
113
114   va_end (ap);
115 }
116
117 /* Output the difference between two symbols in a given size.  */
118 /* ??? There appear to be assemblers that do not like such
119    subtraction, but do support ASM_SET_OP.  It's unfortunately
120    impossible to do here, since the ASM_SET_OP for the difference
121    symbol must appear after both symbols are defined.  */
122
123 void
124 dw2_asm_output_delta (int size, const char *lab1, const char *lab2,
125                       const char *comment, ...)
126 {
127   va_list ap;
128
129   va_start (ap, comment);
130
131 #ifdef ASM_OUTPUT_DWARF_DELTA
132   ASM_OUTPUT_DWARF_DELTA (asm_out_file, size, lab1, lab2);
133 #else
134   dw2_assemble_integer (size,
135                         gen_rtx_MINUS (Pmode,
136                                        gen_rtx_SYMBOL_REF (Pmode, lab1),
137                                        gen_rtx_SYMBOL_REF (Pmode, lab2)));
138 #endif
139   if (flag_debug_asm && comment)
140     {
141       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
142       vfprintf (asm_out_file, comment, ap);
143     }
144   fputc ('\n', asm_out_file);
145
146   va_end (ap);
147 }
148
149 /* Output the difference between two symbols in instruction units
150    in a given size.  */
151
152 void
153 dw2_asm_output_vms_delta (int size ATTRIBUTE_UNUSED,
154                           const char *lab1, const char *lab2,
155                           const char *comment, ...)
156 {
157   va_list ap;
158
159   va_start (ap, comment);
160
161 #ifndef ASM_OUTPUT_DWARF_VMS_DELTA
162   /* VMS Delta is only special on ia64-vms, but this funtion also gets
163      called on alpha-vms so it has to do something sane.  */
164   dw2_asm_output_delta (size, lab1, lab2, comment);
165 #else
166   ASM_OUTPUT_DWARF_VMS_DELTA (asm_out_file, size, lab1, lab2);
167   if (flag_debug_asm && comment)
168     {
169       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
170       vfprintf (asm_out_file, comment, ap);
171     }
172   fputc ('\n', asm_out_file);
173 #endif
174
175   va_end (ap);
176 }
177
178 /* Output a section-relative reference to a LABEL, which was placed in
179    BASE.  In general this can only be done for debugging symbols.
180    E.g. on most targets with the GNU linker, this is accomplished with
181    a direct reference and the knowledge that the debugging section
182    will be placed at VMA 0.  Some targets have special relocations for
183    this that we must use.  */
184
185 void
186 dw2_asm_output_offset (int size, const char *label,
187                        section *base ATTRIBUTE_UNUSED,
188                        const char *comment, ...)
189 {
190   va_list ap;
191
192   va_start (ap, comment);
193
194 #ifdef ASM_OUTPUT_DWARF_OFFSET
195   ASM_OUTPUT_DWARF_OFFSET (asm_out_file, size, label, base);
196 #else
197   dw2_assemble_integer (size, gen_rtx_SYMBOL_REF (Pmode, label));
198 #endif
199
200   if (flag_debug_asm && comment)
201     {
202       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
203       vfprintf (asm_out_file, comment, ap);
204     }
205   fputc ('\n', asm_out_file);
206
207   va_end (ap);
208 }
209
210 #if 0
211
212 /* Output a self-relative reference to a label, possibly in a
213    different section or object file.  */
214
215 void
216 dw2_asm_output_pcrel (int size ATTRIBUTE_UNUSED,
217                       const char *label ATTRIBUTE_UNUSED,
218                       const char *comment, ...)
219 {
220   va_list ap;
221
222   va_start (ap, comment);
223
224 #ifdef ASM_OUTPUT_DWARF_PCREL
225   ASM_OUTPUT_DWARF_PCREL (asm_out_file, size, label);
226 #else
227   dw2_assemble_integer (size,
228                         gen_rtx_MINUS (Pmode,
229                                        gen_rtx_SYMBOL_REF (Pmode, label),
230                                        pc_rtx));
231 #endif
232
233   if (flag_debug_asm && comment)
234     {
235       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
236       vfprintf (asm_out_file, comment, ap);
237     }
238   fputc ('\n', asm_out_file);
239
240   va_end (ap);
241 }
242 #endif /* 0 */
243
244 /* Output an absolute reference to a label.  */
245
246 void
247 dw2_asm_output_addr (int size, const char *label,
248                      const char *comment, ...)
249 {
250   va_list ap;
251
252   va_start (ap, comment);
253
254   dw2_assemble_integer (size, gen_rtx_SYMBOL_REF (Pmode, label));
255
256   if (flag_debug_asm && comment)
257     {
258       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
259       vfprintf (asm_out_file, comment, ap);
260     }
261   fputc ('\n', asm_out_file);
262
263   va_end (ap);
264 }
265
266 /* Similar, but use an RTX expression instead of a text label.  */
267
268 void
269 dw2_asm_output_addr_rtx (int size, rtx addr,
270                          const char *comment, ...)
271 {
272   va_list ap;
273
274   va_start (ap, comment);
275
276   dw2_assemble_integer (size, addr);
277
278   if (flag_debug_asm && comment)
279     {
280       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
281       vfprintf (asm_out_file, comment, ap);
282     }
283   fputc ('\n', asm_out_file);
284
285   va_end (ap);
286 }
287
288 /* Output the first ORIG_LEN characters of STR as a string.
289    If ORIG_LEN is equal to -1, ignore this parameter and output
290    the entire STR instead.
291    If COMMENT is not NULL and comments in the debug information
292    have been requested by the user, append the given COMMENT
293    to the generated output.  */
294
295 void
296 dw2_asm_output_nstring (const char *str, size_t orig_len,
297                         const char *comment, ...)
298 {
299   size_t i, len;
300   va_list ap;
301
302   va_start (ap, comment);
303
304   len = orig_len;
305
306   if (len == (size_t) -1)
307     len = strlen (str);
308
309   if (flag_debug_asm && comment)
310     {
311       fputs ("\t.ascii \"", asm_out_file);
312       for (i = 0; i < len; i++)
313         {
314           int c = str[i];
315           if (c == '\"' || c == '\\')
316             fputc ('\\', asm_out_file);
317           if (ISPRINT(c))
318             fputc (c, asm_out_file);
319           else
320             fprintf (asm_out_file, "\\%o", c);
321         }
322       fprintf (asm_out_file, "\\0\"\t%s ", ASM_COMMENT_START);
323       vfprintf (asm_out_file, comment, ap);
324       fputc ('\n', asm_out_file);
325     }
326   else
327     {
328       /* If an explicit length was given, we can't assume there
329          is a null termination in the string buffer.  */
330       if (orig_len == (size_t) -1)
331         len += 1;
332       ASM_OUTPUT_ASCII (asm_out_file, str, len);
333       if (orig_len != (size_t) -1)
334         assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
335     }
336
337   va_end (ap);
338 }
339 \f
340
341 /* Return the size of an unsigned LEB128 quantity.  */
342
343 int
344 size_of_uleb128 (unsigned HOST_WIDE_INT value)
345 {
346   int size = 0;
347
348   do
349     {
350       value >>= 7;
351       size += 1;
352     }
353   while (value != 0);
354
355   return size;
356 }
357
358 /* Return the size of a signed LEB128 quantity.  */
359
360 int
361 size_of_sleb128 (HOST_WIDE_INT value)
362 {
363   int size = 0, byte;
364
365   do
366     {
367       byte = (value & 0x7f);
368       value >>= 7;
369       size += 1;
370     }
371   while (!((value == 0 && (byte & 0x40) == 0)
372            || (value == -1 && (byte & 0x40) != 0)));
373
374   return size;
375 }
376
377 /* Given an encoding, return the number of bytes the format occupies.
378    This is only defined for fixed-size encodings, and so does not
379    include leb128.  */
380
381 int
382 size_of_encoded_value (int encoding)
383 {
384   if (encoding == DW_EH_PE_omit)
385     return 0;
386
387   switch (encoding & 0x07)
388     {
389     case DW_EH_PE_absptr:
390       return POINTER_SIZE / BITS_PER_UNIT;
391     case DW_EH_PE_udata2:
392       return 2;
393     case DW_EH_PE_udata4:
394       return 4;
395     case DW_EH_PE_udata8:
396       return 8;
397     default:
398       gcc_unreachable ();
399     }
400 }
401
402 /* Yield a name for a given pointer encoding.  */
403
404 const char *
405 eh_data_format_name (int format)
406 {
407 #if HAVE_DESIGNATED_INITIALIZERS
408 #define S(p, v)         [p] = v,
409 #else
410 #define S(p, v)         case p: return v;
411 #endif
412
413 #if HAVE_DESIGNATED_INITIALIZERS
414   __extension__ static const char * const format_names[256] = {
415 #else
416   switch (format) {
417 #endif
418
419   S(DW_EH_PE_absptr, "absolute")
420   S(DW_EH_PE_omit, "omit")
421   S(DW_EH_PE_aligned, "aligned absolute")
422
423   S(DW_EH_PE_uleb128, "uleb128")
424   S(DW_EH_PE_udata2, "udata2")
425   S(DW_EH_PE_udata4, "udata4")
426   S(DW_EH_PE_udata8, "udata8")
427   S(DW_EH_PE_sleb128, "sleb128")
428   S(DW_EH_PE_sdata2, "sdata2")
429   S(DW_EH_PE_sdata4, "sdata4")
430   S(DW_EH_PE_sdata8, "sdata8")
431
432   S(DW_EH_PE_absptr | DW_EH_PE_pcrel, "pcrel")
433   S(DW_EH_PE_uleb128 | DW_EH_PE_pcrel, "pcrel uleb128")
434   S(DW_EH_PE_udata2 | DW_EH_PE_pcrel, "pcrel udata2")
435   S(DW_EH_PE_udata4 | DW_EH_PE_pcrel, "pcrel udata4")
436   S(DW_EH_PE_udata8 | DW_EH_PE_pcrel, "pcrel udata8")
437   S(DW_EH_PE_sleb128 | DW_EH_PE_pcrel, "pcrel sleb128")
438   S(DW_EH_PE_sdata2 | DW_EH_PE_pcrel, "pcrel sdata2")
439   S(DW_EH_PE_sdata4 | DW_EH_PE_pcrel, "pcrel sdata4")
440   S(DW_EH_PE_sdata8 | DW_EH_PE_pcrel, "pcrel sdata8")
441
442   S(DW_EH_PE_absptr | DW_EH_PE_textrel, "textrel")
443   S(DW_EH_PE_uleb128 | DW_EH_PE_textrel, "textrel uleb128")
444   S(DW_EH_PE_udata2 | DW_EH_PE_textrel, "textrel udata2")
445   S(DW_EH_PE_udata4 | DW_EH_PE_textrel, "textrel udata4")
446   S(DW_EH_PE_udata8 | DW_EH_PE_textrel, "textrel udata8")
447   S(DW_EH_PE_sleb128 | DW_EH_PE_textrel, "textrel sleb128")
448   S(DW_EH_PE_sdata2 | DW_EH_PE_textrel, "textrel sdata2")
449   S(DW_EH_PE_sdata4 | DW_EH_PE_textrel, "textrel sdata4")
450   S(DW_EH_PE_sdata8 | DW_EH_PE_textrel, "textrel sdata8")
451
452   S(DW_EH_PE_absptr | DW_EH_PE_datarel, "datarel")
453   S(DW_EH_PE_uleb128 | DW_EH_PE_datarel, "datarel uleb128")
454   S(DW_EH_PE_udata2 | DW_EH_PE_datarel, "datarel udata2")
455   S(DW_EH_PE_udata4 | DW_EH_PE_datarel, "datarel udata4")
456   S(DW_EH_PE_udata8 | DW_EH_PE_datarel, "datarel udata8")
457   S(DW_EH_PE_sleb128 | DW_EH_PE_datarel, "datarel sleb128")
458   S(DW_EH_PE_sdata2 | DW_EH_PE_datarel, "datarel sdata2")
459   S(DW_EH_PE_sdata4 | DW_EH_PE_datarel, "datarel sdata4")
460   S(DW_EH_PE_sdata8 | DW_EH_PE_datarel, "datarel sdata8")
461
462   S(DW_EH_PE_absptr | DW_EH_PE_funcrel, "funcrel")
463   S(DW_EH_PE_uleb128 | DW_EH_PE_funcrel, "funcrel uleb128")
464   S(DW_EH_PE_udata2 | DW_EH_PE_funcrel, "funcrel udata2")
465   S(DW_EH_PE_udata4 | DW_EH_PE_funcrel, "funcrel udata4")
466   S(DW_EH_PE_udata8 | DW_EH_PE_funcrel, "funcrel udata8")
467   S(DW_EH_PE_sleb128 | DW_EH_PE_funcrel, "funcrel sleb128")
468   S(DW_EH_PE_sdata2 | DW_EH_PE_funcrel, "funcrel sdata2")
469   S(DW_EH_PE_sdata4 | DW_EH_PE_funcrel, "funcrel sdata4")
470   S(DW_EH_PE_sdata8 | DW_EH_PE_funcrel, "funcrel sdata8")
471
472   S(DW_EH_PE_indirect | DW_EH_PE_absptr, "indirect absolute")
473
474   S(DW_EH_PE_indirect | DW_EH_PE_absptr | DW_EH_PE_pcrel,
475     "indirect pcrel")
476   S(DW_EH_PE_indirect | DW_EH_PE_uleb128 | DW_EH_PE_pcrel,
477     "indirect pcrel uleb128")
478   S(DW_EH_PE_indirect | DW_EH_PE_udata2 | DW_EH_PE_pcrel,
479     "indirect pcrel udata2")
480   S(DW_EH_PE_indirect | DW_EH_PE_udata4 | DW_EH_PE_pcrel,
481     "indirect pcrel udata4")
482   S(DW_EH_PE_indirect | DW_EH_PE_udata8 | DW_EH_PE_pcrel,
483     "indirect pcrel udata8")
484   S(DW_EH_PE_indirect | DW_EH_PE_sleb128 | DW_EH_PE_pcrel,
485     "indirect pcrel sleb128")
486   S(DW_EH_PE_indirect | DW_EH_PE_sdata2 | DW_EH_PE_pcrel,
487     "indirect pcrel sdata2")
488   S(DW_EH_PE_indirect | DW_EH_PE_sdata4 | DW_EH_PE_pcrel,
489     "indirect pcrel sdata4")
490   S(DW_EH_PE_indirect | DW_EH_PE_sdata8 | DW_EH_PE_pcrel,
491     "indirect pcrel sdata8")
492
493   S(DW_EH_PE_indirect | DW_EH_PE_absptr | DW_EH_PE_textrel,
494     "indirect textrel")
495   S(DW_EH_PE_indirect | DW_EH_PE_uleb128 | DW_EH_PE_textrel,
496     "indirect textrel uleb128")
497   S(DW_EH_PE_indirect | DW_EH_PE_udata2 | DW_EH_PE_textrel,
498     "indirect textrel udata2")
499   S(DW_EH_PE_indirect | DW_EH_PE_udata4 | DW_EH_PE_textrel,
500     "indirect textrel udata4")
501   S(DW_EH_PE_indirect | DW_EH_PE_udata8 | DW_EH_PE_textrel,
502     "indirect textrel udata8")
503   S(DW_EH_PE_indirect | DW_EH_PE_sleb128 | DW_EH_PE_textrel,
504     "indirect textrel sleb128")
505   S(DW_EH_PE_indirect | DW_EH_PE_sdata2 | DW_EH_PE_textrel,
506     "indirect textrel sdata2")
507   S(DW_EH_PE_indirect | DW_EH_PE_sdata4 | DW_EH_PE_textrel,
508     "indirect textrel sdata4")
509   S(DW_EH_PE_indirect | DW_EH_PE_sdata8 | DW_EH_PE_textrel,
510     "indirect textrel sdata8")
511
512   S(DW_EH_PE_indirect | DW_EH_PE_absptr | DW_EH_PE_datarel,
513     "indirect datarel")
514   S(DW_EH_PE_indirect | DW_EH_PE_uleb128 | DW_EH_PE_datarel,
515     "indirect datarel uleb128")
516   S(DW_EH_PE_indirect | DW_EH_PE_udata2 | DW_EH_PE_datarel,
517     "indirect datarel udata2")
518   S(DW_EH_PE_indirect | DW_EH_PE_udata4 | DW_EH_PE_datarel,
519     "indirect datarel udata4")
520   S(DW_EH_PE_indirect | DW_EH_PE_udata8 | DW_EH_PE_datarel,
521     "indirect datarel udata8")
522   S(DW_EH_PE_indirect | DW_EH_PE_sleb128 | DW_EH_PE_datarel,
523     "indirect datarel sleb128")
524   S(DW_EH_PE_indirect | DW_EH_PE_sdata2 | DW_EH_PE_datarel,
525     "indirect datarel sdata2")
526   S(DW_EH_PE_indirect | DW_EH_PE_sdata4 | DW_EH_PE_datarel,
527     "indirect datarel sdata4")
528   S(DW_EH_PE_indirect | DW_EH_PE_sdata8 | DW_EH_PE_datarel,
529     "indirect datarel sdata8")
530
531   S(DW_EH_PE_indirect | DW_EH_PE_absptr | DW_EH_PE_funcrel,
532     "indirect funcrel")
533   S(DW_EH_PE_indirect | DW_EH_PE_uleb128 | DW_EH_PE_funcrel,
534     "indirect funcrel uleb128")
535   S(DW_EH_PE_indirect | DW_EH_PE_udata2 | DW_EH_PE_funcrel,
536     "indirect funcrel udata2")
537   S(DW_EH_PE_indirect | DW_EH_PE_udata4 | DW_EH_PE_funcrel,
538     "indirect funcrel udata4")
539   S(DW_EH_PE_indirect | DW_EH_PE_udata8 | DW_EH_PE_funcrel,
540     "indirect funcrel udata8")
541   S(DW_EH_PE_indirect | DW_EH_PE_sleb128 | DW_EH_PE_funcrel,
542     "indirect funcrel sleb128")
543   S(DW_EH_PE_indirect | DW_EH_PE_sdata2 | DW_EH_PE_funcrel,
544     "indirect funcrel sdata2")
545   S(DW_EH_PE_indirect | DW_EH_PE_sdata4 | DW_EH_PE_funcrel,
546     "indirect funcrel sdata4")
547   S(DW_EH_PE_indirect | DW_EH_PE_sdata8 | DW_EH_PE_funcrel,
548     "indirect funcrel sdata8")
549
550 #if HAVE_DESIGNATED_INITIALIZERS
551   };
552
553   gcc_assert (format >= 0 && format < 0x100 && format_names[format]);
554
555   return format_names[format];
556 #else
557   }
558   gcc_unreachable ();
559 #endif
560 }
561
562 /* Output an unsigned LEB128 quantity, but only the byte values.  */
563
564 void
565 dw2_asm_output_data_uleb128_raw (unsigned HOST_WIDE_INT value)
566 {
567   while (1)
568     {
569       int byte = (value & 0x7f);
570       value >>= 7;
571       if (value != 0)
572         /* More bytes to follow.  */
573         byte |= 0x80;
574
575       fprintf (asm_out_file, "%#x", byte);
576       if (value == 0)
577         break;
578       fputc (',', asm_out_file);
579     }
580 }
581
582 /* Output an unsigned LEB128 quantity.  */
583
584 void
585 dw2_asm_output_data_uleb128 (unsigned HOST_WIDE_INT value,
586                              const char *comment, ...)
587 {
588   va_list ap;
589
590   va_start (ap, comment);
591
592 #ifdef HAVE_AS_LEB128
593   fprintf (asm_out_file, "\t.uleb128 " HOST_WIDE_INT_PRINT_HEX , value);
594
595   if (flag_debug_asm && comment)
596     {
597       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
598       vfprintf (asm_out_file, comment, ap);
599     }
600 #else
601   {
602     unsigned HOST_WIDE_INT work = value;
603     const char *byte_op = targetm.asm_out.byte_op;
604
605     if (byte_op)
606       fputs (byte_op, asm_out_file);
607     do
608       {
609         int byte = (work & 0x7f);
610         work >>= 7;
611         if (work != 0)
612           /* More bytes to follow.  */
613           byte |= 0x80;
614
615         if (byte_op)
616           {
617             fprintf (asm_out_file, "%#x", byte);
618             if (work != 0)
619               fputc (',', asm_out_file);
620           }
621         else
622           assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
623       }
624     while (work != 0);
625
626   if (flag_debug_asm)
627     {
628       fprintf (asm_out_file, "\t%s uleb128 " HOST_WIDE_INT_PRINT_HEX,
629                ASM_COMMENT_START, value);
630       if (comment)
631         {
632           fputs ("; ", asm_out_file);
633           vfprintf (asm_out_file, comment, ap);
634         }
635     }
636   }
637 #endif
638   fputc ('\n', asm_out_file);
639
640   va_end (ap);
641 }
642
643 /* Output an signed LEB128 quantity, but only the byte values.  */
644
645 void
646 dw2_asm_output_data_sleb128_raw (HOST_WIDE_INT value)
647 {
648   int byte, more;
649
650   while (1)
651     {
652       byte = (value & 0x7f);
653       value >>= 7;
654       more = !((value == 0 && (byte & 0x40) == 0)
655                 || (value == -1 && (byte & 0x40) != 0));
656       if (more)
657         byte |= 0x80;
658
659       fprintf (asm_out_file, "%#x", byte);
660       if (!more)
661         break;
662       fputc (',', asm_out_file);
663     }
664 }
665
666 /* Output a signed LEB128 quantity.  */
667
668 void
669 dw2_asm_output_data_sleb128 (HOST_WIDE_INT value,
670                              const char *comment, ...)
671 {
672   va_list ap;
673
674   va_start (ap, comment);
675
676 #ifdef HAVE_AS_LEB128
677   fprintf (asm_out_file, "\t.sleb128 " HOST_WIDE_INT_PRINT_DEC, value);
678
679   if (flag_debug_asm && comment)
680     {
681       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
682       vfprintf (asm_out_file, comment, ap);
683     }
684 #else
685   {
686     HOST_WIDE_INT work = value;
687     int more, byte;
688     const char *byte_op = targetm.asm_out.byte_op;
689
690     if (byte_op)
691       fputs (byte_op, asm_out_file);
692     do
693       {
694         byte = (work & 0x7f);
695         /* arithmetic shift */
696         work >>= 7;
697         more = !((work == 0 && (byte & 0x40) == 0)
698                  || (work == -1 && (byte & 0x40) != 0));
699         if (more)
700           byte |= 0x80;
701
702         if (byte_op)
703           {
704             fprintf (asm_out_file, "%#x", byte);
705             if (more)
706               fputc (',', asm_out_file);
707           }
708         else
709           assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
710       }
711     while (more);
712
713   if (flag_debug_asm)
714     {
715       fprintf (asm_out_file, "\t%s sleb128 " HOST_WIDE_INT_PRINT_DEC,
716                ASM_COMMENT_START, value);
717       if (comment)
718         {
719           fputs ("; ", asm_out_file);
720           vfprintf (asm_out_file, comment, ap);
721         }
722     }
723   }
724 #endif
725   fputc ('\n', asm_out_file);
726
727   va_end (ap);
728 }
729
730 void
731 dw2_asm_output_delta_uleb128 (const char *lab1 ATTRIBUTE_UNUSED,
732                               const char *lab2 ATTRIBUTE_UNUSED,
733                               const char *comment, ...)
734 {
735   va_list ap;
736
737   va_start (ap, comment);
738
739 #ifdef HAVE_AS_LEB128
740   fputs ("\t.uleb128 ", asm_out_file);
741   assemble_name (asm_out_file, lab1);
742   fputc ('-', asm_out_file);
743   assemble_name (asm_out_file, lab2);
744 #else
745   gcc_unreachable ();
746 #endif
747
748   if (flag_debug_asm && comment)
749     {
750       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
751       vfprintf (asm_out_file, comment, ap);
752     }
753   fputc ('\n', asm_out_file);
754
755   va_end (ap);
756 }
757
758 #if 0
759
760 void
761 dw2_asm_output_delta_sleb128 (const char *lab1 ATTRIBUTE_UNUSED,
762                               const char *lab2 ATTRIBUTE_UNUSED,
763                               const char *comment, ...)
764 {
765   va_list ap;
766
767   va_start (ap, comment);
768
769 #ifdef HAVE_AS_LEB128
770   fputs ("\t.sleb128 ", asm_out_file);
771   assemble_name (asm_out_file, lab1);
772   fputc ('-', asm_out_file);
773   assemble_name (asm_out_file, lab2);
774 #else
775   gcc_unreachable ();
776 #endif
777
778   if (flag_debug_asm && comment)
779     {
780       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
781       vfprintf (asm_out_file, comment, ap);
782     }
783   fputc ('\n', asm_out_file);
784
785   va_end (ap);
786 }
787 #endif /* 0 */
788 \f
789 static int dw2_output_indirect_constant_1 (splay_tree_node, void *);
790
791 static GTY((param1_is (char *), param2_is (tree))) splay_tree indirect_pool;
792
793 static GTY(()) int dw2_const_labelno;
794
795 #if defined(HAVE_GAS_HIDDEN)
796 # define USE_LINKONCE_INDIRECT (SUPPORTS_ONE_ONLY)
797 #else
798 # define USE_LINKONCE_INDIRECT 0
799 #endif
800
801 /* Comparison function for a splay tree in which the keys are strings.
802    K1 and K2 have the dynamic type "const char *".  Returns <0, 0, or
803    >0 to indicate whether K1 is less than, equal to, or greater than
804    K2, respectively.  */
805
806 static int
807 splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2)
808 {
809   const char *s1 = (const char *)k1;
810   const char *s2 = (const char *)k2;
811   int ret;
812
813   if (s1 == s2)
814     return 0;
815
816   ret = strcmp (s1, s2);
817
818   /* The strings are always those from IDENTIFIER_NODEs, and,
819      therefore, we should never have two copies of the same
820      string.  */
821   gcc_assert (ret);
822
823   return ret;
824 }
825
826 /* Put X, a SYMBOL_REF, in memory.  Return a SYMBOL_REF to the allocated
827    memory.  Differs from force_const_mem in that a single pool is used for
828    the entire unit of translation, and the memory is not guaranteed to be
829    "near" the function in any interesting sense.  IS_PUBLIC controls whether
830    the symbol can be shared across the entire application (or DSO).  */
831
832 rtx
833 dw2_force_const_mem (rtx x, bool is_public)
834 {
835   splay_tree_node node;
836   const char *key;
837   tree decl_id;
838
839   if (! indirect_pool)
840     /* We use strcmp, rather than just comparing pointers, so that the
841        sort order will not depend on the host system.  */
842     indirect_pool = splay_tree_new_ggc (splay_tree_compare_strings,
843                                         ggc_alloc_splay_tree_str_tree_node_splay_tree_s,
844                                         ggc_alloc_splay_tree_str_tree_node_splay_tree_node_s);
845
846   gcc_assert (GET_CODE (x) == SYMBOL_REF);
847
848   key = XSTR (x, 0);
849   node = splay_tree_lookup (indirect_pool, (splay_tree_key) key);
850   if (node)
851     decl_id = (tree) node->value;
852   else
853     {
854       tree id;
855       const char *str = targetm.strip_name_encoding (key);
856
857       if (is_public && USE_LINKONCE_INDIRECT)
858         {
859           char *ref_name = XALLOCAVEC (char, strlen (str) + sizeof "DW.ref.");
860
861           sprintf (ref_name, "DW.ref.%s", str);
862           gcc_assert (!maybe_get_identifier (ref_name));
863           decl_id = get_identifier (ref_name);
864           TREE_PUBLIC (decl_id) = 1;
865         }
866       else
867         {
868           char label[32];
869
870           ASM_GENERATE_INTERNAL_LABEL (label, "LDFCM", dw2_const_labelno);
871           ++dw2_const_labelno;
872           gcc_assert (!maybe_get_identifier (label));
873           decl_id = get_identifier (label);
874         }
875
876       id = maybe_get_identifier (str);
877       if (id)
878         TREE_SYMBOL_REFERENCED (id) = 1;
879
880       splay_tree_insert (indirect_pool, (splay_tree_key) key,
881                          (splay_tree_value) decl_id);
882     }
883
884   return gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (decl_id));
885 }
886
887 /* A helper function for dw2_output_indirect_constants called through
888    splay_tree_foreach.  Emit one queued constant to memory.  */
889
890 static int
891 dw2_output_indirect_constant_1 (splay_tree_node node,
892                                 void *data ATTRIBUTE_UNUSED)
893 {
894   const char *sym;
895   rtx sym_ref;
896   tree id, decl;
897
898   sym = (const char *) node->key;
899   id = (tree) node->value;
900
901   decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id, ptr_type_node);
902   SET_DECL_ASSEMBLER_NAME (decl, id);
903   DECL_ARTIFICIAL (decl) = 1;
904   DECL_IGNORED_P (decl) = 1;
905   DECL_INITIAL (decl) = decl;
906   TREE_READONLY (decl) = 1;
907
908   if (TREE_PUBLIC (id))
909     {
910       TREE_PUBLIC (decl) = 1;
911       make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
912       if (USE_LINKONCE_INDIRECT)
913         DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
914     }
915   else
916     TREE_STATIC (decl) = 1;
917
918   sym_ref = gen_rtx_SYMBOL_REF (Pmode, sym);
919   assemble_variable (decl, 1, 1, 1);
920   assemble_integer (sym_ref, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
921
922   return 0;
923 }
924
925 /* Emit the constants queued through dw2_force_const_mem.  */
926
927 void
928 dw2_output_indirect_constants (void)
929 {
930   if (indirect_pool)
931     splay_tree_foreach (indirect_pool, dw2_output_indirect_constant_1, NULL);
932 }
933
934 /* Like dw2_asm_output_addr_rtx, but encode the pointer as directed.
935    If PUBLIC is set and the encoding is DW_EH_PE_indirect, the indirect
936    reference is shared across the entire application (or DSO).  */
937
938 void
939 dw2_asm_output_encoded_addr_rtx (int encoding, rtx addr, bool is_public,
940                                  const char *comment, ...)
941 {
942   int size;
943   va_list ap;
944
945   va_start (ap, comment);
946
947   size = size_of_encoded_value (encoding);
948
949   if (encoding == DW_EH_PE_aligned)
950     {
951       assemble_align (POINTER_SIZE);
952       assemble_integer (addr, size, POINTER_SIZE, 1);
953       va_end (ap);
954       return;
955     }
956
957   /* NULL is _always_ represented as a plain zero, as is 1 for Ada's
958      "all others".  */
959   if (addr == const0_rtx || addr == const1_rtx)
960     assemble_integer (addr, size, BITS_PER_UNIT, 1);
961   else
962     {
963     restart:
964       /* Allow the target first crack at emitting this.  Some of the
965          special relocations require special directives instead of
966          just ".4byte" or whatever.  */
967 #ifdef ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX
968       ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX (asm_out_file, encoding, size,
969                                          addr, done);
970 #endif
971
972       /* Indirection is used to get dynamic relocations out of a
973          read-only section.  */
974       if (encoding & DW_EH_PE_indirect)
975         {
976           /* It is very tempting to use force_const_mem so that we share data
977              with the normal constant pool.  However, we've already emitted
978              the constant pool for this function.  Moreover, we'd like to
979              share these constants across the entire unit of translation and
980              even, if possible, across the entire application (or DSO).  */
981           addr = dw2_force_const_mem (addr, is_public);
982           encoding &= ~DW_EH_PE_indirect;
983           goto restart;
984         }
985
986       switch (encoding & 0xF0)
987         {
988         case DW_EH_PE_absptr:
989           dw2_assemble_integer (size, addr);
990           break;
991
992         case DW_EH_PE_pcrel:
993           gcc_assert (GET_CODE (addr) == SYMBOL_REF);
994 #ifdef ASM_OUTPUT_DWARF_PCREL
995           ASM_OUTPUT_DWARF_PCREL (asm_out_file, size, XSTR (addr, 0));
996 #else
997           dw2_assemble_integer (size, gen_rtx_MINUS (Pmode, addr, pc_rtx));
998 #endif
999           break;
1000
1001         default:
1002           /* Other encodings should have been handled by
1003              ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX.  */
1004           gcc_unreachable ();
1005         }
1006
1007 #ifdef ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX
1008     done:;
1009 #endif
1010     }
1011
1012   if (flag_debug_asm && comment)
1013     {
1014       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
1015       vfprintf (asm_out_file, comment, ap);
1016     }
1017   fputc ('\n', asm_out_file);
1018
1019   va_end (ap);
1020 }
1021
1022 #include "gt-dwarf2asm.h"