source: trunk/emx/src/emxomf/stabshll.c@ 3021

Last change on this file since 3021 was 3021, checked in by bird, 18 years ago

Implemented non-standard enum (size) based on a patch from Froloff. Fixes #145.

  • Property cvs2svn:cvs-rev set to 1.35
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 118.6 KB
Line 
1/* stabshll.c -- Convert GNU-style a.out debug information into
2 IBM OS/2 HLL 3 debug information
3 Copyright (c) 1993-1998 Eberhard Mattes
4
5This file is part of emxomf.
6
7emxomf is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12emxomf is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with emxomf; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21//#define HLL_DEBUG 1
22
23#define DEMANGLE_PROC_NAMES 1
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <time.h>
33#include <alloca.h>
34#include <ctype.h>
35#ifdef DEMANGLE_PROC_NAMES
36# include <demangle.h>
37#endif
38#include "defs.h"
39#include "emxomf.h"
40#include "grow.h"
41#include "stabshll.h"
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/* Field ID values for the type table. */
48
49#define FID_nil 0x80 /* No data */
50#define FID_void 0x81 /* Type void */
51#define FID_string 0x82 /* A length-prefixed string follows */
52#define FID_index 0x83 /* A type index follows */
53#define FID_span_16u 0x85 /* An unsigned 16-bit number follows */
54#define FID_span_32u 0x86 /* An unsigned 32-bit number follows */
55#define FID_span_8s 0x88 /* A signed 8-bit number follows */
56#define FID_span_16s 0x89 /* A signed 16-bit number follows */
57#define FID_span_32s 0x8a /* A signed 32-bit number follows */
58#define FID_span_8u 0x8b /* An unsigned 8-bit number follows */
59
60/* Static scope table subrecord types. */
61
62#define SST_begin 0x00 /* Begin block */
63#define SST_proc 0x01 /* Procedure */
64#define SST_end 0x02 /* End block or end procedure */
65#define SST_auto 0x04 /* Scoped variable */
66#define SST_static 0x05 /* Static variable */
67#define SST_reg 0x0d /* Register variable */
68#define SST_changseg 0x11 /* Change default segment */
69#define SST_tag 0x16 /* Structure, union, enum tags */
70#define SST_tag2 0x19 /* Extended tag for long names (C++ class). */
71#define SST_memfunc 0x1a /* C++ member function */
72#define SST_CPPproc 0x1d /* C++ function */
73#define SST_CPPstatic 0x1e /* C++ static variable */
74#define SST_cuinfo 0x40 /* Compile unit information */
75
76/* Class visibility. */
77
78#define VIS_PRIVATE 0x00 /* private */
79#define VIS_PROTECTED 0x01 /* protected */
80#define VIS_PUBLIC 0x02 /* public */
81
82#define MEMBER_VIS 0x03 /* Member visibility mask */
83#define MEMBER_FUNCTION 0x08 /* It's a member function */
84#define MEMBER_VIRTUAL 0x10 /* Member function / base class is virtual */
85#define MEMBER_VOLATILE 0x20 /* Member function is volatile */
86#define MEMBER_CONST 0x40 /* Member function is const */
87#define MEMBER_STATIC 0x80 /* Member is static */
88#define MEMBER_CTOR 0x100 /* Member function is a constructor */
89#define MEMBER_DTOR 0x200 /* Member function is a destructor */
90#define MEMBER_BASE 0x400 /* It's a base class */
91
92/* Flags for ty_struc. */
93
94#define STRUC_FORWARD 0x01 /* Structure declared forward */
95
96
97/*******************************************************************************
98* Structures and Typedefs *
99*******************************************************************************/
100enum type_tag
101{
102 ty_stabs_ref, /* Reference to stabs type */
103 ty_prim, /* Primitive type */
104 ty_alias, /* Equivalent type */
105 ty_pointer, /* Pointer type */
106 ty_func, /* Function */
107 ty_args, /* Function arguments */
108 ty_struc, /* Structure or union */
109 ty_types, /* Type list for structure/union */
110 ty_fields, /* Field list for structure/union */
111 ty_enu, /* Enumeration type */
112 ty_values, /* Enumeration values */
113 ty_array, /* Array */
114 ty_bits, /* Bit field / bit string */
115 ty_ref, /* Reference type (C++) */
116 ty_member, /* Member of a C++ class */
117 ty_class, /* A C++ class */
118 ty_memfunc, /* A C++ member function */
119 ty_baseclass, /* A C++ base class */
120 ty_max
121};
122
123/* A structure field. */
124
125struct field
126{
127 dword offset; /* Offset, in bytes */
128 const char *name; /* Field name */
129};
130
131/* An enumeration value. */
132
133struct value
134{
135 long index; /* The value */
136 const char *name; /* The name */
137};
138
139/* Temporary representation of a structure field. */
140
141struct tmp_field
142{
143 struct type *type; /* The internal type */
144 dword offset; /* The offset of the field */
145 const char *name; /* The field name */
146 const char *sname; /* Static name */
147 const char *mnglname; /* Mangled name for methods. (#456) */
148 int flags; /* Member flags (visibility) */
149};
150
151
152/* Internal representation of a type. */
153
154struct type
155{
156 enum type_tag tag; /* The type of this type */
157 int index; /* The HLL index for this type */
158 struct type *next; /* All types are chained in a single list */
159 struct type *nexthash; /* All types are chained in the hash list */
160 union
161 {
162 int stabs_ref; /* Reference a stabs type */
163 struct type *pointer; /* Pointer to another type */
164 struct type *alias; /* Alias for another type */
165 struct type *ref; /* Reference to another type */
166 struct
167 { /* Structure */
168 struct type *types; /* Types of the fields */
169 struct type *fields; /* Field names and offsets */
170 dword size; /* Size of the structure, in bytes */
171 word count; /* Number of fields */
172 word flags; /* Flags (STRUC_FORWARD) */
173 const char *name; /* Name of the structure */
174 } struc;
175 struct
176 { /* Class */
177 struct type *members; /* Members */
178 dword size; /* Size of the class, in bytes */
179 word count; /* Number of fields */
180 const char *name; /* Name of the class */
181 } class;
182 struct
183 { /* Enumeration */
184 struct type *type; /* Base type */
185 struct type *values; /* Values */
186 long first; /* Smallest value */
187 long last; /* Biggest value */
188 const char *name; /* Name of the enum */
189 } enu;
190 struct
191 { /* Array */
192 struct type *itype; /* Index type */
193 struct type *etype; /* Element type */
194 long first; /* First index */
195 long last; /* Last index */
196 } array;
197 struct
198 { /* Bitfield */
199 struct type *type; /* Base type */
200 long start; /* First bit */
201 long count; /* Number of bits */
202 } bits;
203 struct
204 { /* Function */
205 struct type *ret; /* Return type */
206 struct type *args; /* Argument types */
207 struct type *domain; /* Domain type (C++ member) (#456) */
208 int arg_count; /* Number of arguments */
209 } func;
210 struct
211 { /* Function arguments */
212 struct type **list; /* Array of argument types */
213 int count; /* Number of arguments */
214 } args;
215 struct
216 { /* List of types */
217 struct type **list; /* Array of types */
218 int count; /* Number of types */
219 } types;
220 struct
221 { /* Structure fields */
222 struct field *list; /* The fields */
223 int count; /* Number of fields */
224 } fields;
225 struct
226 { /* Class member */
227 struct type *type; /* Type of the member */
228 dword offset; /* Offset of the member, in bytes */
229 int flags; /* Member flags (visibility etc.) */
230 const char *name; /* Name of the member */
231 const char *sname; /* Static name */
232 } member;
233 struct
234 { /* Enumeration values */
235 struct value *list; /* Array of values */
236 int count; /* Number of values */
237 } values;
238 struct
239 { /* Member function */
240 struct type *type; /* Function type */
241 dword offset; /* Offset in the virtual table */
242 int flags; /* Member flags (visibility etc.) */
243 const char *name; /* Name of the member function */
244 const char *mnglname; /* Mangled Name of the member function. (#456) */
245 } memfunc;
246 struct
247 { /* Base class */
248 struct type *type; /* The base class */
249 dword offset; /* Offset of the base class */
250 int flags; /* Visibility, virtual */
251 } baseclass;
252 } d;
253};
254
255
256/* A block. */
257
258struct block
259{
260 dword addr; /* Start address of the block */
261 size_t patch_ptr; /* Index into SST buffer for patching */
262};
263
264/* Timestamp for the compiler unit information SST subrecord. */
265
266#pragma pack(1)
267
268struct timestamp
269{
270 byte hours;
271 byte minutes;
272 byte seconds;
273 byte hundredths;
274 byte day;
275 byte month;
276 word year;
277};
278
279#pragma pack()
280
281
282/*******************************************************************************
283* Global Variables *
284*******************************************************************************/
285/* This variable points to the next character of a stabs type being
286 parsed. */
287
288static const char *parse_ptr;
289
290/* Start of the stabs string we're parsing. Useful when printing
291 messages. */
292static const char *parse_start;
293
294/* Current stabs index. Useful when printing messages. */
295static int *parse_pindex;
296
297/* The next HLL type index. When creating a complex type, this index
298 is used. Then, this variable is incremented. */
299
300static int hll_type_index;
301
302/* We keep all internal types in a single list. When creating a new
303 type, we first look through the list to find out whether an
304 identical type already exists. Doing a linear search is
305 inefficient, but that doesn't matter here, according to
306 experience. This variable points to the first type of the list. */
307
308static struct type *type_head;
309#define TYPE_HASH_MAX 211
310static struct type *atype_tag[TYPE_HASH_MAX];
311#ifdef HASH_DEBUG
312static int ctypes = 0;
313static int ctype_tag[TYPE_HASH_MAX];
314#endif
315
316/* This growing array keeps track of stabs vs. internal types. It is
317 indexed by stabs type numbers. */
318
319static struct type **stype_list;
320static struct grow stype_grow;
321
322/* We collect function arguments in this growing array. */
323
324static struct type **args_list;
325static struct grow args_grow;
326
327/* This stack (implemented with a growing array) keeps track of
328 begin/end block nesting. */
329
330static struct block *block_stack;
331static struct grow block_grow;
332
333/* This string pool contains field names etc. */
334
335static struct strpool *str_pool;
336
337/* This variable holds the sym_ptr index of the most recently
338 encountered N_LBRAC symbol. */
339
340static int lbrac_index;
341
342/* The index (in sst) of the most recently started SST subrecord. */
343
344static size_t subrec_start;
345
346/* The proc_patch_base contains the index where the base address and
347 function length are to be patched into the SST. */
348
349static size_t proc_patch_base;
350
351/* The base address used by the most recently proc record in the
352 SST. */
353
354static size_t proc_start_addr;
355
356/* The internal type of the most recently defined function. Valid
357 only if last_fun_valid is TRUE. */
358
359static struct type last_fun_type;
360
361/* This variable is TRUE if a function has recently been defined. */
362
363static int last_fun_valid;
364
365/* The length of the prologue of the most recently defined
366 function. */
367
368static int prologue_length;
369
370/* The address of the most recently defined function. */
371
372static dword last_fun_addr;
373
374/* This variable is non-zero when converting a translated C++
375 program. */
376
377static int cplusplus_flag;
378
379/* Unnamed structures are numbered sequentially, using this
380 counter. */
381
382static int unnamed_struct_number;
383
384/* kso #456 2003-06-11: Reversed quiet workaround. */
385#define no_warning warning_parse
386
387
388/*******************************************************************************
389* Internal Functions *
390*******************************************************************************/
391static void parse_typedef (int *index);
392static void warning_parse (const char *pszFormat, ...);
393
394
395/**
396 * Report an warning in which might be connecting to parsing.
397 *
398 * @param pszFormat Message string.
399 * @param ... More arguments
400 */
401static void warning_parse (const char *pszFormat, ...)
402{
403 va_list args;
404
405 va_start (args, pszFormat);
406 fprintf (stderr, "emxomf warning: ");
407 vfprintf (stderr, pszFormat, args);
408 va_end (args);
409 fputc ('\n', stderr);
410
411
412 if (parse_ptr && parse_start && parse_ptr >= parse_start)
413 {
414 if (parse_pindex && *parse_pindex >= 0 && *parse_pindex < sym_count)
415 fprintf (stderr, "emxomf info: parsing sym no.%d type=%d at char '%c' in position %d:\n%s\n",
416 *parse_pindex, sym_ptr[*parse_pindex].n_type,
417 *parse_ptr, parse_ptr - parse_start, parse_start);
418 else
419 fprintf (stderr, "emxomf info: parsing '%c' at position %d:\n%s\n",
420 *parse_ptr, parse_ptr - parse_start, parse_start);
421 }
422}
423
424
425/* Start a type table record of type TYPE, with type qualifier QUAL.
426 tt_end() must be called to finish the record after writing the
427 record contents. */
428
429static void tt_start (byte type, byte qual)
430{
431 subrec_start = tt.size;
432 grow_by (&tt_boundary_grow, 1);
433 tt_boundary[tt_boundary_grow.count++] = tt.size;
434 buffer_word (&tt, 0); /* Length of sub-record */
435 buffer_byte (&tt, type); /* Type of sub-record */
436 buffer_byte (&tt, qual); /* Type qualifier */
437}
438
439
440/* Finish a type table record. This function must be called after
441 tt_start(). */
442
443static void tt_end (void)
444{
445 *(word *)(tt.buf + subrec_start) = tt.size - subrec_start - 2;
446}
447
448
449/* Put the type index TI into the type table. TI = -1 encodes the
450 `nil' type (no type), 0x97 encodes the `void' type. All other
451 values are type indices. */
452
453static void tt_index (int ti)
454{
455 if (ti == -1) /* nil */
456 buffer_byte (&tt, FID_nil);
457 else if (ti == 0x97) /* void */
458 buffer_byte (&tt, FID_void);
459 else
460 {
461 buffer_byte (&tt, FID_index);
462 buffer_word (&tt, ti);
463 }
464}
465
466
467/* Put an 8-bit byte into the type table. */
468
469static void tt_byte (byte x)
470{
471 buffer_byte (&tt, x);
472}
473
474
475/* Put a 16-bit word into the type table. */
476
477static void tt_word (word x)
478{
479 buffer_word (&tt, x);
480}
481
482
483/* Put a 32-bit double word into the type table. */
484
485static void tt_dword (dword x)
486{
487 buffer_dword (&tt, x);
488}
489
490
491/* Put the name S into the type table. */
492
493static void tt_name (const char *s)
494{
495 buffer_byte (&tt, FID_string);
496 buffer_nstr (&tt, s);
497}
498
499
500/* Put the mangled name S into the type table. */
501
502static void tt_enc (const char *s)
503{
504 buffer_enc (&tt, s);
505}
506
507
508/* Put the unsigned number N into the type table. Depending on the
509 magnitude of N, different encodings are used. */
510
511static void tt_unsigned_span (dword n)
512{
513 if (n <= 0xff)
514 {
515 buffer_byte (&tt, FID_span_8u);
516 buffer_byte (&tt, n);
517 }
518 else if (n <= 0xffff)
519 {
520 buffer_byte (&tt, FID_span_16u);
521 buffer_word (&tt, n);
522 }
523 else
524 {
525 buffer_byte (&tt, FID_span_32u);
526 buffer_dword (&tt, n);
527 }
528}
529
530
531/* Put the signed number N into the type table. Depending on the
532 magnitude of N, different encodings are used. */
533
534static void tt_signed_span (long n)
535{
536 if (-127 <= n && n <= 127)
537 {
538 buffer_byte (&tt, FID_span_8s);
539 buffer_byte (&tt, n);
540 }
541 else if (-32767 <= n && n <= 32767)
542 {
543 buffer_byte (&tt, FID_span_16s);
544 buffer_word (&tt, n);
545 }
546 else
547 {
548 buffer_byte (&tt, FID_span_32s);
549 buffer_dword (&tt, n);
550 }
551}
552
553
554/* Start a static scope table subrecord of type TYPE. sst_end() must
555 be called to finish the subrecord after writing the subrecord
556 data. */
557
558static void sst_start (byte type)
559{
560 subrec_start = sst.size;
561 grow_by (&sst_boundary_grow, 1);
562 sst_boundary[sst_boundary_grow.count++] = sst.size;
563 /* Length of sub-record - bird: not sure if HL03 likes this. */
564 buffer_byte (&sst, 0x80);
565 buffer_byte (&sst, 0);
566 /* Type of sub-record */
567 buffer_byte (&sst, type);
568}
569
570
571/* Finish a static scope table subrecord. This function must be
572 called after sst_start(). */
573
574static void sst_end (void)
575{
576 int len = sst.size - subrec_start - 2;
577 /* -- experimental...
578 if (len > 1000)
579 error ("sst_end: Record too big");
580 */
581 sst.buf[subrec_start] = (len >> 8) | 0x80;
582 sst.buf[subrec_start + 1] = 0xff & len;
583}
584
585
586/* Define a static variable in the static scope table. NAME is the
587 name of the variable, ADDR is the address of the variable (segment
588 offset), TYPE_INDEX is the HLL type index. If EXT is zero, SYM_SEG
589 is the segment (N_DATA, for instance). If EXT is non-zero, SYM_SEG
590 is the stabs symbol index (for the relocation table). This
591 function also creates appropriate relocation table entries. */
592
593static void sst_static (const char *name, dword addr, int type_index,
594 int sym_seg, int ext, const struct nlist *psym)
595{
596 struct relocation_info r;
597 size_t len = strlen (name);
598
599 if (!psym)
600 {
601 psym = find_symbol_ex (name, -1, ext);
602 if (!psym)
603 {
604 char *psz = alloca (len + 2);
605 *psz = '_';
606 memcpy(psz + 1, name, len + 1);
607 psym = find_symbol_ex (psz, -1, ext);
608 }
609 }
610 if (psym)
611 set_hll_type (psym - sym_ptr, type_index);
612
613 if (len > 255)
614 sst_start (SST_CPPstatic);
615 else
616 sst_start (SST_static);
617 r.r_address = sst.size;
618 buffer_dword (&sst, addr); /* Segment offset */
619 buffer_word (&sst, 0); /* Segment address */
620 buffer_word (&sst, type_index); /* Type index */
621 if (len > 255)
622 buffer_enc (&sst, name); /* Symbol name - encoded. */
623 else
624 buffer_nstr (&sst, name); /* Symbol name */
625 sst_end ();
626 r.r_extern = ext;
627 r.r_length = 2;
628 r.r_symbolnum = sym_seg;
629 r.r_pcrel = 0;
630 r.r_pad = 0;
631 buffer_mem (&sst_reloc, &r, sizeof (r));
632 r.r_address += 4;
633 r.r_length = 1;
634 buffer_mem (&sst_reloc, &r, sizeof (r));
635}
636
637/**
638 * Try calculate a good hash (yeah sure).
639 *
640 * @returns hash index.
641 * @param t type
642 */
643static inline unsigned type_hash (struct type *t)
644{
645 register unsigned uhash = 0;
646 switch (t->tag)
647 {
648 case ty_alias: uhash = (unsigned)t->d.alias; break;
649 case ty_stabs_ref: uhash = (unsigned)t->d.stabs_ref; break;
650 case ty_prim: uhash = (unsigned)t->index; break;
651 case ty_pointer: uhash = (unsigned)t->d.pointer; break;
652 case ty_ref: uhash = (unsigned)t->d.ref; break;
653 case ty_struc: uhash = (unsigned)t->d.struc.name; break;
654 case ty_class: uhash = (unsigned)t->d.class.name; break;
655 case ty_enu: uhash = (unsigned)t->d.enu.name; break;
656 case ty_array: uhash = (unsigned)t->d.array.etype; break;
657 case ty_bits: uhash = (unsigned)t->d.bits.type; break;
658 case ty_func: uhash = (unsigned)t->d.func.args ^ (unsigned)t->d.func.arg_count ^ (unsigned)t->d.func.ret ^ (unsigned)t->d.func.domain; break;
659 case ty_types: uhash = (unsigned)t->d.types.count << 3; break;
660 case ty_args: uhash = (unsigned)t->d.args.count << 3; break;
661 case ty_fields: uhash = (unsigned)t->d.fields.count << 3; break;
662 case ty_member: uhash = (unsigned)t->d.member.type ^ (unsigned)t->d.member.name >> 7; break;
663 case ty_values: uhash = (unsigned)t->d.values.count << 3; break;
664 case ty_memfunc: uhash = (unsigned)t->d.memfunc.type; break;
665 case ty_baseclass: uhash = (unsigned)t->d.baseclass.type; break;
666 default:break; /* shut up warnings. */
667 }
668 uhash += t->tag;
669 return (uhash ^ uhash >> 13) % TYPE_HASH_MAX;
670}
671
672
673/**
674 * Search a list for a type.
675 * @returns pointer to the type we found.
676 * @returns NULL if not found.
677 * @param p Pointer to the head list.
678 * @param src Type to find.
679 * @param fhash Whether or not to follow the hash list or the main list.
680 */
681#ifdef HASH_DEBUG
682static struct type *type_find (struct type *p, const struct type *src, int fhash)
683#else
684static struct type *type_find (struct type *p, const struct type *src)
685#endif
686{
687 int i;
688 for (; p != NULL;
689#ifdef HASH_DEBUG
690 p = fhash ? p->nexthash : p->next )
691#else
692 p = p->nexthash )
693#endif
694 if (p->tag == src->tag)
695 switch (p->tag)
696 {
697 case ty_alias:
698 if (p->d.alias == src->d.alias)
699 return p;
700 break;
701 case ty_stabs_ref:
702 if (p->d.stabs_ref == src->d.stabs_ref)
703 return p;
704 break;
705 case ty_prim:
706 if (p->index == src->index)
707 return p;
708 break;
709 case ty_pointer:
710 if (p->d.pointer == src->d.pointer)
711 return p;
712 break;
713 case ty_ref:
714 if (p->d.ref == src->d.ref)
715 return p;
716 break;
717 case ty_struc:
718 if (p->d.struc.fields == src->d.struc.fields
719 && p->d.struc.types == src->d.struc.types
720 && p->d.struc.name == src->d.struc.name
721 && p->d.struc.flags == src->d.struc.flags)
722 return p;
723 break;
724 case ty_class:
725 if (p->d.class.members == src->d.class.members
726 && p->d.class.name == src->d.class.name)
727 return p;
728 break;
729 case ty_enu:
730 if (p->d.enu.type == src->d.enu.type
731 && p->d.enu.values == src->d.enu.values
732 && p->d.enu.name == src->d.enu.name)
733 return p;
734 break;
735 case ty_array:
736 if (p->d.array.etype == src->d.array.etype
737 && p->d.array.itype == src->d.array.itype
738 && p->d.array.first == src->d.array.first
739 && p->d.array.last == src->d.array.last)
740 return p;
741 break;
742 case ty_bits:
743 if (p->d.bits.type == src->d.bits.type
744 && p->d.bits.start == src->d.bits.start
745 && p->d.bits.count == src->d.bits.count)
746 return p;
747 break;
748 case ty_func:
749 if (p->d.func.ret == src->d.func.ret
750 && p->d.func.args == src->d.func.args
751 && p->d.func.domain == src->d.func.domain
752 && p->d.func.arg_count == src->d.func.arg_count)
753 return p;
754 break;
755 case ty_args:
756 if (p->d.args.count == src->d.args.count)
757 {
758 for (i = 0; i < p->d.args.count; ++i)
759 if (p->d.args.list[i] != src->d.args.list[i])
760 break;
761 if (i >= p->d.args.count)
762 return p;
763 }
764 break;
765 case ty_types:
766 if (p->d.types.count == src->d.types.count)
767 {
768 for (i = 0; i < p->d.types.count; ++i)
769 if (p->d.types.list[i] != src->d.types.list[i])
770 break;
771 if (i >= p->d.types.count)
772 return p;
773 }
774 break;
775 case ty_fields:
776 if (p->d.fields.count == src->d.fields.count)
777 {
778 for (i = 0; i < p->d.fields.count; ++i)
779 if (p->d.fields.list[i].offset != src->d.fields.list[i].offset
780 || p->d.fields.list[i].name != src->d.fields.list[i].name)
781 break;
782 if (i >= p->d.fields.count)
783 return p;
784 }
785 break;
786 case ty_member:
787 if (p->d.member.type == src->d.member.type
788 && p->d.member.offset == src->d.member.offset
789 && p->d.member.flags == src->d.member.flags
790 && p->d.member.name == src->d.member.name
791 && p->d.member.sname == src->d.member.sname)
792 return p;
793 break;
794 case ty_values:
795 if (p->d.values.count == src->d.values.count)
796 {
797 for (i = 0; i < p->d.values.count; ++i)
798 if (p->d.values.list[i].index != src->d.values.list[i].index
799 || p->d.values.list[i].name != src->d.values.list[i].name)
800 break;
801 if (i >= p->d.values.count)
802 return p;
803 }
804 break;
805 case ty_memfunc:
806 if (p->d.memfunc.type == src->d.memfunc.type
807 && p->d.memfunc.offset == src->d.memfunc.offset
808 && p->d.memfunc.flags == src->d.memfunc.flags
809 && p->d.memfunc.name == src->d.memfunc.name
810 && p->d.memfunc.mnglname == src->d.memfunc.mnglname)
811 return p;
812 break;
813 case ty_baseclass:
814 if (p->d.baseclass.type == src->d.baseclass.type
815 && p->d.baseclass.offset == src->d.baseclass.offset
816 && p->d.baseclass.flags == src->d.baseclass.flags)
817 return p;
818 break;
819 default:
820 abort ();
821 }
822 return NULL;
823}
824
825
826/* Add a new internal type to the list of internal types. If an
827 identical type already exists, a pointer to that type is returned.
828 Otherwise, a copy of the new type is added to the list and a
829 pointer to that type is returned. It isn't worth my while to
830 improve the speed of this function, for instance by using
831 hashing.
832
833 Actually it is worth while optimizing it as it takes 85% of the
834 time here... or rather it was.
835
836 ctypes=25301 */
837
838static struct type *type_add (struct type *src)
839{
840 struct type *p;
841 unsigned ihash;
842 int i;
843
844 ihash = type_hash(src);
845#ifdef HASH_DEBUG
846 p = type_find (atype_tag[ihash], src, 1);
847#else
848 p = type_find (atype_tag[ihash], src);
849#endif
850 if (p)
851 {
852 return p;
853 }
854
855#ifdef HASH_DEBUG
856/* {
857 struct type *pcheck = type_find (type_head, src, 0);
858 if (pcheck)
859 {
860 printf("\n\thash algorithm is borked! tag=%d\n", pcheck->tag);
861 return pcheck;
862 }
863 } */
864 ctype_tag[ihash]++;
865 ctypes++;
866#endif
867
868 p = xmalloc (sizeof (*p));
869 *p = *src;
870 p->next = type_head;
871 type_head = p;
872 p->nexthash = atype_tag[ihash];
873 atype_tag[ihash] = p;
874 switch (src->tag)
875 {
876 case ty_args:
877 i = src->d.args.count * sizeof (*(src->d.args.list));
878 p->d.args.list = xmalloc (i);
879 memcpy (p->d.args.list, src->d.args.list, i);
880 break;
881 case ty_types:
882 i = src->d.types.count * sizeof (*(src->d.types.list));
883 p->d.types.list = xmalloc (i);
884 memcpy (p->d.types.list, src->d.types.list, i);
885 break;
886 case ty_fields:
887 i = src->d.fields.count * sizeof (*(src->d.fields.list));
888 p->d.fields.list = xmalloc (i);
889 memcpy (p->d.fields.list, src->d.fields.list, i);
890 break;
891 case ty_values:
892 i = src->d.values.count * sizeof (*(src->d.values.list));
893 p->d.values.list = xmalloc (i);
894 memcpy (p->d.values.list, src->d.values.list, i);
895 break;
896 default:
897 break;
898 }
899 return p;
900}
901
902
903/* Associate the stabs type number NUMBER with the internal type
904 HLL. */
905
906static void stype_add (int number, struct type *hll)
907{
908 int i;
909
910 /* Stabs type numbers are greater than zero. */
911
912 if (number < 1)
913 error ("stype_add: Invalid type index %d", number);
914
915 /* Remember the current size of the table and grow the table as
916 required. */
917
918 i = stype_grow.alloc;
919 grow_to (&stype_grow, number);
920
921 /* Initialize the new table entries. */
922
923 while (i < stype_grow.alloc)
924 stype_list[i++] = NULL;
925
926 /* Put the type into the table. */
927
928#ifdef HLL_DEBUG
929 printf (" type: stabs %d => HLL 0x%x\n", number, hll->index);
930#endif
931#if 0 /* not good test... as we do some types twice. */
932 if (stype_list[number-1] != NULL && stype_list[number-1] != hll)
933 warning ("Type stabs %d allready mapped to HLL 0x%x, new HLL 0x%x!!\n", number, stype_list[number-1]->index, hll->index);
934#endif
935
936 stype_list[number-1] = hll;
937}
938
939
940/* Find the internal type associated with the stabs type number
941 NUMBER. If there is no such type, return NULL. */
942
943static struct type *stype_find (int number)
944{
945 /* Stabs type numbers are greater than zero. */
946
947 if (number < 1)
948 error ("stype_find: Invalid type index %d", number);
949
950 /* Adjust the index and check if it is within the current size of
951 the table. */
952
953 --number;
954 if (number >= stype_grow.alloc)
955 return NULL;
956 else
957 return stype_list[number];
958}
959
960
961/* Dereference stabs references until finding a non-reference. */
962
963static const struct type *follow_refs (const struct type *t)
964{
965 const struct type *t2;
966
967 while (t->tag == ty_stabs_ref)
968 {
969 t2 = stype_find (t->d.stabs_ref);
970 if (t2 == NULL)
971 break;
972 t = t2;
973 }
974
975 return t;
976}
977
978
979#if defined (HLL_DEBUG)
980
981/* This function is used to print an internal type for debugging. */
982
983static void show_type (struct type *tp)
984{
985 /* precaution so we don't iterate for ever here when showing classes. */
986 static int fShowClass;
987 int i;
988
989 /* Currently we crash when showing FILE (stdio.h). So, better not crash here.. */
990 if (!tp)
991 {
992 printf("!!! error! NULL type pointer! !!!");
993 return;
994 }
995
996 switch (tp->tag)
997 {
998 case ty_prim:
999 printf ("%#x", tp->index);
1000 break;
1001 case ty_stabs_ref:
1002 printf ("t%d", tp->d.stabs_ref);
1003 break;
1004 case ty_alias:
1005 show_type (tp->d.alias);
1006 break;
1007 case ty_pointer:
1008 printf ("(");
1009 show_type (tp->d.pointer);
1010 printf (")*");
1011 break;
1012 case ty_ref:
1013 printf ("(");
1014 show_type (tp->d.ref);
1015 printf (")&");
1016 break;
1017 case ty_struc:
1018 printf ("{");
1019 show_type (tp->d.struc.types);
1020 printf ("}");
1021 break;
1022 case ty_class:
1023 printf ("{");
1024 if (!fShowClass)
1025 {
1026 fShowClass = 1;
1027 show_type (tp->d.class.members);
1028 fShowClass = 0;
1029 }
1030 else
1031 printf ("<-!-!->");
1032 printf ("}");
1033 break;
1034 case ty_enu:
1035 printf ("{");
1036 show_type (tp->d.enu.values);
1037 printf ("}");
1038 break;
1039 case ty_array:
1040 printf ("(");
1041 show_type (tp->d.array.etype);
1042 printf ("[%ld]", tp->d.array.last + 1 - tp->d.array.first);
1043 break;
1044 case ty_bits:
1045 show_type (tp->d.bits.type);
1046 printf (":%ld:%ld", tp->d.bits.start, tp->d.bits.count);
1047 break;
1048 case ty_func:
1049 show_type (tp->d.func.ret);
1050 printf ("(");
1051 if (tp->d.func.args != NULL)
1052 show_type (tp->d.func.args);
1053 printf (")");
1054 break;
1055 case ty_args:
1056 for (i = 0; i < tp->d.args.count; ++i)
1057 {
1058 if (i != 0)
1059 printf (", ");
1060 show_type (tp->d.args.list[i]);
1061 }
1062 break;
1063 case ty_types:
1064 for (i = 0; i < tp->d.types.count; ++i)
1065 {
1066 if (i != 0)
1067 printf (", ");
1068 show_type (tp->d.types.list[i]);
1069 }
1070 break;
1071 case ty_fields:
1072 for (i = 0; i < tp->d.fields.count; ++i)
1073 {
1074 if (i != 0)
1075 printf (", ");
1076 printf ("%s", tp->d.fields.list[i].name);
1077 }
1078 break;
1079 case ty_member:
1080 printf ("%s", tp->d.member.name);
1081 break;
1082 case ty_values:
1083 for (i = 0; i < tp->d.values.count; ++i)
1084 {
1085 if (i != 0)
1086 printf (", ");
1087 printf ("%s", tp->d.values.list[i].name);
1088 }
1089 break;
1090 case ty_memfunc:
1091 show_type (tp->d.memfunc.type);
1092 break;
1093 case ty_baseclass:
1094 printf ("{");
1095 show_type (tp->d.baseclass.type);
1096 printf ("}");
1097 break;
1098 default:
1099 error ("show_type: Unknown type: %d", tp->tag);
1100 }
1101}
1102
1103#endif
1104
1105
1106/**
1107 * Tries to complete a struct forward reference.
1108 *
1109 * @returns 0 on success
1110 * @returns 1 on failure.
1111 * @param tp Pointer to the struct/class to complete.
1112 * @remark Nasy hack for strstreambuf.
1113 */
1114static int try_complete_struct(const struct type *tp)
1115{
1116 int cch;
1117 const char *pszName;
1118 int i;
1119
1120 if (tp->tag == ty_struc)
1121 return 0;
1122 if (tp->d.struc.flags != STRUC_FORWARD)
1123 return 1;
1124
1125 pszName = tp->d.struc.name;
1126 cch = strlen(pszName);
1127 for (i = 0; i < sym_count; ++i)
1128 switch (sym_ptr[i].n_type)
1129 {
1130 case N_LSYM:
1131 case N_LCSYM:
1132 case N_GSYM:
1133 case N_PSYM:
1134 case N_RSYM:
1135 case N_STSYM:
1136 case N_FUN:
1137 if ( !memcmp(str_ptr + sym_ptr[i].n_un.n_strx, pszName, cch)
1138 && *(char*)(str_ptr + sym_ptr[i].n_un.n_strx + cch) == ':'
1139 )
1140 {
1141 parse_typedef(&i);
1142 return 1;
1143 } /* if */
1144 } /* switch */
1145
1146 return 0;
1147}
1148
1149
1150
1151/* Return the size of the internal type TP, in bytes. */
1152
1153static dword type_size (const struct type *tp)
1154{
1155 switch (tp->tag)
1156 {
1157 case ty_prim:
1158
1159 /* Primitive types. */
1160
1161 if (tp->index >= 0xa0 && tp->index <= 0xbf)
1162 return 4; /* Near pointer */
1163 switch (tp->index)
1164 {
1165 case 0x97: /* void */
1166 return 0;
1167 case 0x80: /* 8 bit signed */
1168 case 0x84: /* 8 bit unsigned */
1169 case 0x90: /* 8 bit boolean */
1170 return 1;
1171 case 0x81: /* 16 bit signed */
1172 case 0x85: /* 16 bit unsigned */
1173 return 2;
1174 case 0x82: /* 32 bit signed */
1175 case 0x86: /* 32 bit unsigned */
1176 case 0x88: /* 32 bit real */
1177 return 4;
1178 case 0x89: /* 64 bit real */
1179 case 0x8c: /* 64 bit complex */
1180 return 8;
1181 case 0x8a: /* 80 bit real (96 bits of storage) */
1182 return 12;
1183 case 0x8d: /* 128 bit complex */
1184 return 16;
1185 case 0x8e: /* 160 bit complex */
1186 return 24;
1187 case 0x8f: /* 256 bit complex (birds invention) */
1188 return 32;
1189 }
1190 error ("type_size: Unknown primitive type: %d", tp->index);
1191
1192 case ty_stabs_ref:
1193
1194 #if 0
1195 /* This should not happen. */
1196
1197 no_warning ("stabs type %d not defined", tp->d.stabs_ref);
1198
1199 return 4;
1200 #else
1201 /* This seems to happen although it shouldn't... Try do something at least. */
1202
1203 tp = follow_refs (tp);
1204 if (!tp || tp->tag == ty_stabs_ref)
1205 {
1206 no_warning ("stabs type %d not defined", tp->d.stabs_ref);
1207 return 4;
1208 }
1209 return type_size (tp);
1210 #endif
1211
1212 case ty_alias:
1213
1214 /* An alias. Return the size of the referenced type. */
1215
1216 return type_size (tp->d.alias);
1217
1218 case ty_pointer:
1219 case ty_ref:
1220
1221 /* Pointers and references are 32-bit values. */
1222
1223 return 4;
1224
1225 case ty_struc:
1226
1227 /* The size of a structure is stored in the type structure. */
1228
1229 if ( tp->d.struc.flags & STRUC_FORWARD
1230 && try_complete_struct(tp))
1231 {
1232 no_warning ("size of incomplete structure %s is unknown (off %ld)\n stabs: %s",
1233 tp->d.struc.name, parse_ptr - parse_start, parse_start);
1234 return 0;
1235 }
1236 return tp->d.struc.size;
1237
1238 case ty_class:
1239
1240 /* The size of a class is stored in the type structure. */
1241
1242 return tp->d.class.size;
1243
1244 case ty_enu:
1245
1246 /* Return the size of the base type for an enumeration type. */
1247
1248 return type_size (tp->d.enu.type);
1249
1250 case ty_array:
1251
1252 /* Multiply the element size with the number of array elements
1253 for arrays. */
1254
1255 return ((tp->d.array.last + 1 - tp->d.array.first) *
1256 type_size (tp->d.array.etype));
1257
1258 case ty_bits:
1259
1260 /* Use the size of the base type for bitfields. */
1261
1262 return type_size (tp->d.bits.type);
1263
1264 case ty_member:
1265
1266 /* Use the size of the member's type for members. */
1267
1268 return type_size (tp->d.member.type);
1269
1270 case ty_baseclass:
1271
1272 /* The size of a base class is the size of the base class :-) */
1273
1274 return type_size (tp->d.baseclass.type);
1275
1276 case ty_func:
1277 case ty_args:
1278 case ty_types:
1279 case ty_fields:
1280 case ty_values:
1281 case ty_memfunc:
1282
1283 /* This cannot not happen. */
1284
1285 error ("type_size: cannot compute size for tag %d", tp->tag);
1286
1287 default:
1288
1289 /* This cannot happen. */
1290
1291 error ("type_size: unknown type: %d", tp->tag);
1292 }
1293}
1294
1295
1296/* Turn a struct into a class. This needs to be done when a struct is
1297 referenced as base class (unfortunately, a simple class looks like
1298 a struct). */
1299
1300static void struct_to_class (struct type *tp)
1301{
1302 struct type t, *t1, **list, **types;
1303 struct field *fields;
1304 int i, n;
1305
1306 if (tp->index != -1)
1307 {
1308 warning ("Base class cannot be converted from struct");
1309 return;
1310 }
1311 n = tp->d.struc.count;
1312 if (n == 0)
1313 list = NULL;
1314 else
1315 {
1316 if (tp->d.struc.fields->tag != ty_fields
1317 || tp->d.struc.fields->d.fields.count != n)
1318 {
1319 warning ("Invalid struct field list");
1320 return;
1321 }
1322 fields = tp->d.struc.fields->d.fields.list;
1323 if (tp->d.struc.types->tag != ty_types
1324 || tp->d.struc.types->d.types.count != n)
1325 {
1326 warning ("Invalid struct types list");
1327 return;
1328 }
1329 types = tp->d.struc.types->d.types.list;
1330 list = xmalloc (n * sizeof (*list));
1331 for (i = 0; i < n; ++i)
1332 {
1333 t.tag = ty_member;
1334 t.index = -1;
1335 t.d.member.type = types[i];
1336 t.d.member.offset = fields[i].offset;
1337 t.d.member.flags = VIS_PUBLIC;
1338 t.d.member.name = fields[i].name;
1339 t.d.member.sname = NULL;
1340 list[i] = type_add (&t);
1341 }
1342 }
1343
1344 t.tag = ty_types;
1345 t.index = -1;
1346 t.d.types.count = n;
1347 t.d.types.list = list;
1348 t1 = type_add (&t);
1349 free (list);
1350
1351 t = *tp;
1352 tp->tag = ty_class;
1353 tp->d.class.members = t1;
1354 tp->d.class.size = t.d.struc.size;
1355 tp->d.class.count = t.d.struc.count;
1356 tp->d.class.name = t.d.struc.name;
1357}
1358
1359
1360/* Build and return the internal representation of the `long long'
1361 type. As IPMD doesn't know about `long long' we use the following
1362 structure instead:
1363
1364 struct _long_long
1365 {
1366 unsigned long lo, hi;
1367 };
1368
1369 This function could be optimized by remembering the type in a
1370 global variable. */
1371
1372static struct type *make_long_long (void)
1373{
1374 struct type t, *t1, *t2, *t3, *types[2];
1375 struct field fields[2];
1376
1377 /* Let t1 be `unsigned long'. */
1378
1379 t.tag = ty_prim;
1380 t.index = 0x86; /* 32 bit unsigned */
1381 t1 = type_add (&t);
1382
1383 /* Let t2 be the field types: unsigned long, unsigned long. */
1384
1385 types[0] = t1;
1386 types[1] = t1;
1387 t.tag = ty_types;
1388 t.index = -1;
1389 t.d.types.count = 2;
1390 t.d.types.list = types;
1391 t2 = type_add (&t);
1392
1393 /* Let t3 be the fields: lo (at 0), hi (at 4). */
1394
1395 fields[0].offset = 0; fields[0].name = strpool_add (str_pool, "lo");
1396 fields[1].offset = 4; fields[1].name = strpool_add (str_pool, "hi");
1397 t.tag = ty_fields;
1398 t.index = -1;
1399 t.d.fields.count = 2;
1400 t.d.fields.list = fields;
1401 t3 = type_add (&t);
1402
1403 /* Build the structure type from t1, t2 and t3. */
1404
1405 t.tag = ty_struc;
1406 t.d.struc.count = 2;
1407 t.d.struc.size = 8;
1408 t.d.struc.types = t2;
1409 t.d.struc.fields = t3;
1410 t.d.struc.name = strpool_add (str_pool, "_long_long");
1411 t.d.struc.flags = 0;
1412 return type_add (&t);
1413}
1414
1415
1416/* Parse a (signed) long long number in a stabs type. The number is
1417 given in octal or decimal notation. The result is stored to
1418 *NUMBER. This function returns TRUE iff successful. We don't
1419 check for overflow. */
1420
1421static int parse_long_long (long long *number)
1422{
1423 long long n;
1424 int neg, base;
1425
1426 n = 0; neg = FALSE; base = 10;
1427 if (*parse_ptr == '-')
1428 {
1429 neg = TRUE;
1430 ++parse_ptr;
1431 }
1432 if (*parse_ptr == '0')
1433 base = 8;
1434 if (!(*parse_ptr >= '0' && *parse_ptr < base + '0'))
1435 {
1436 *number = 0;
1437 return FALSE;
1438 }
1439 while (*parse_ptr >= '0' && *parse_ptr < base + '0')
1440 {
1441 n = n * base + (*parse_ptr - '0');
1442 ++parse_ptr;
1443 }
1444 *number = (neg ? -n : n);
1445 return TRUE;
1446}
1447
1448
1449/* Parse a (signed) number in a stabs type. The number is given in
1450 octal or decimal notation. The result is stored to *NUMBER. This
1451 function returns TRUE iff successful. We don't check for
1452 overflow. */
1453
1454static int parse_number (long *number)
1455{
1456 long long n;
1457 int result;
1458
1459 result = parse_long_long (&n);
1460 *number = (long)n;
1461 return result;
1462}
1463
1464
1465/* Check the next character in a stabs type for C. If the next
1466 character matches C, the character is skipped and TRUE is
1467 returned. Otherwise, a warning message is displayed and FALSE is
1468 returned. */
1469
1470static int parse_char (char c)
1471{
1472 if (*parse_ptr != c)
1473 {
1474 no_warning ("Invalid symbol data: `%c' expected, found '%c' (off %ld)\n stabs: %s",
1475 c, *parse_ptr, parse_ptr - parse_start, parse_start);
1476 return FALSE;
1477 }
1478 ++parse_ptr;
1479 return TRUE;
1480}
1481
1482
1483/* Parse mangled arguments of a member functions. For instance, ic
1484 means an `int' argument and a `char' argument. Return FALSE on
1485 failure. */
1486
1487static int parse_mangled_args (void)
1488{
1489 while (*parse_ptr != ';')
1490 {
1491 switch (*parse_ptr)
1492 {
1493 case 0:
1494 warning ("Missing semicolon after mangled arguments");
1495 return FALSE;
1496 default:
1497 /* TODO */
1498 break;
1499 }
1500 ++parse_ptr;
1501 }
1502 ++parse_ptr; /* Skip semicolon */
1503 return TRUE;
1504}
1505
1506
1507/* Parse the visibility of a class or member. */
1508
1509static int parse_visibility (void)
1510{
1511 switch (*parse_ptr)
1512 {
1513 case '0':
1514 ++parse_ptr;
1515 return VIS_PRIVATE;
1516 case '1':
1517 ++parse_ptr;
1518 return VIS_PROTECTED;
1519 case '2':
1520 ++parse_ptr;
1521 return VIS_PUBLIC;
1522 default:
1523 ++parse_ptr;
1524 no_warning ("Invalid visibility: %c", *parse_ptr);
1525 if (*parse_ptr != 0)
1526 ++parse_ptr;
1527 return VIS_PUBLIC;
1528 }
1529}
1530
1531
1532/* Add TYPE_NAME to `str_pool', replacing a NULL string with a unique
1533 string. */
1534
1535static const char *add_struct_name (const char *type_name)
1536{
1537 char tmp[32];
1538
1539 /* Some names have only spaces for gdb safety, let's make an
1540 fake name for those. */
1541 if (type_name != NULL)
1542 {
1543 const char *psz = type_name;
1544 while (*psz == ' ' || *psz == '\t')
1545 psz++;
1546 if (!*psz)
1547 type_name = NULL;
1548 }
1549
1550 if (type_name == NULL)
1551 {
1552 sprintf (tmp, "__%d", unnamed_struct_number++);
1553 type_name = tmp;
1554 }
1555 return strpool_add (str_pool, type_name);
1556}
1557
1558/**
1559 * Finds the next colon in the string and returns pointer to it.
1560 * For C++ template support, colons within <> are not considered.
1561 *
1562 * @returns Pointer to next colon within psz.
1563 * @returns NULL if not found.
1564 * @param pszString String to search.
1565 * @param chType Type we're parsing. Use '\0' if unknown or it
1566 * should be ignored
1567 * @remark Still a bad hack this.
1568 */
1569#ifdef COLON_DEBUG
1570static const char *nextcolon2(const char *pszString, char chType)
1571{
1572 static const char *_nextcolon2(const char *pszString, char chType);
1573 const char *psz = _nextcolon2(pszString, chType);
1574 fprintf(stderr, "COLON: %.100s\n%.32s\n", pszString, psz);
1575 return psz;
1576}
1577static const char *_nextcolon2(const char *pszString, char chType)
1578#else
1579static const char *nextcolon2(const char *pszString, char chType)
1580#endif
1581{
1582 const char *pszColon;
1583 const char *psz;
1584
1585 /* permanent operator hack */
1586 if ( pszString[0] == 'o'
1587 && pszString[1] == 'p'
1588 && pszString[2] == 'e'
1589 && pszString[3] == 'r'
1590 && pszString[4] == 'a'
1591 && pszString[5] == 't'
1592 && pszString[6] == 'o'
1593 && pszString[7] == 'r')
1594 {
1595 psz = pszString + 8;
1596 while (*psz && !isalnum(*psz) && *psz != '_') /* potential operator chars */
1597 {
1598 if (*psz == ':')
1599 {
1600 if ( psz - pszString >= 1 + 8
1601 && psz - pszString <= 3 + 8)
1602 return psz;
1603 break;
1604 }
1605 psz++;
1606 }
1607
1608 }
1609
1610 /* normal life */
1611 pszColon = strchr(pszString, ':');
1612 if (!pszColon)
1613 return NULL;
1614 psz = strchr(pszString, '<');
1615
1616 if (psz && psz < pszColon && pszColon[1] == ':')
1617 {
1618 int cNesting = 0; /* <> level */
1619
1620 for (;;psz++)
1621 {
1622 switch (*psz)
1623 {
1624 case '<': cNesting++; break;
1625 case '>': cNesting--; break;
1626 case ':':
1627 if (cNesting > 0)
1628 break;
1629 /* hack: continue on ">::" (but not on "operator...") */
1630 if ( chType != 'x'
1631 && psz[1] == ':'
1632 && psz > pszString && psz[-1] == '>'
1633 )
1634 {
1635 psz++;
1636 break;
1637 }
1638 return psz;
1639 case '\0':
1640 return NULL;
1641 }
1642 }
1643 }
1644 else
1645 {
1646 if (chType != 'x')
1647 {
1648 /* skip '::' (hack:) if not followed by number. */
1649 while (*pszColon && pszColon[1] == ':' && (pszColon[2] > '9' || pszColon[2] < '0'))
1650 {
1651 pszColon += 2;
1652 while (*pszColon != ':' && *pszColon)
1653 pszColon++;
1654 }
1655 }
1656 }
1657
1658 return pszColon;
1659}
1660
1661/* old version */
1662static inline const char *nextcolon(const char *pszString)
1663{
1664 return nextcolon2(pszString, '\0');
1665}
1666
1667
1668
1669/* Parse a stabs type (which is named TYPE_NAME; TYPE_NAME is NULL for
1670 an unnamed type) and return the internal representation of that
1671 type. */
1672
1673static struct type *parse_type (const char *type_name)
1674{
1675 const char *saved_ptr, *p1;
1676 char ch;
1677 struct type t, *result, *t1, *t2, *t3, **tlist;
1678 int i, n, class_flag, is_void;
1679 long num1, num2, size, lo, hi, valid, offset, width, code;
1680 long long range_lo, range_hi;
1681 struct tmp_field *tmp_fields, *tf;
1682 struct value *tmp_values, *tv;
1683 struct grow g1 = GROW_INIT;
1684 enum type_tag tt;
1685
1686 valid = TRUE; result = NULL;
1687 t.index = -1;
1688 switch (*parse_ptr)
1689 {
1690 case '0': case '1': case '2': case '3': case '4':
1691 case '5': case '6': case '7': case '8': case '9':
1692 if (!parse_number (&num1))
1693 goto syntax;
1694
1695 /* Special case: self reference => void */
1696 is_void = FALSE;
1697 if (*parse_ptr == '=' && parse_ptr[1] >= '0' && parse_ptr[1] <= '9')
1698 {
1699 saved_ptr = parse_ptr++;
1700 if (!parse_number (&num2))
1701 goto syntax;
1702 if (num2 == num1)
1703 is_void = TRUE;
1704 else
1705 parse_ptr = saved_ptr;
1706 }
1707 if (is_void)
1708 {
1709 t.tag = ty_prim;
1710 t.index = 0x97; /* void */
1711 result = type_add (&t);
1712 stype_add (num1, result);
1713 }
1714 else if (*parse_ptr == '=')
1715 {
1716 /* Nested definition */
1717 ++parse_ptr;
1718 result = parse_type (type_name);
1719 stype_add (num1, result);
1720 }
1721 else
1722 {
1723 result = stype_find (num1);
1724 if (result == NULL)
1725 {
1726 t.tag = ty_stabs_ref;
1727 t.d.stabs_ref = num1;
1728 }
1729 }
1730 break;
1731
1732 case 'r':
1733
1734 /* A range type: r<base_type>;<lower_bound>;<upper_bound>;
1735
1736 Special cases:
1737
1738 lower_bound | upper_bound | type
1739 ------------+-------------+------------------------
1740 n > 0 | 0 | floating point, n bytes
1741
1742 */
1743
1744 ++parse_ptr;
1745 if (!parse_number (&num1) || !parse_char (';')
1746 || !parse_long_long (&range_lo) || !parse_char (';')
1747 || !parse_long_long (&range_hi) || !parse_char (';'))
1748 goto syntax;
1749 t.tag = ty_prim;
1750 if (range_lo == -128 && range_hi == 127)
1751 t.index = 0x80; /* 8 bit signed */
1752 else if (range_lo == -32768 && range_hi == 32767)
1753 t.index = 0x81; /* 16 bit signed */
1754 else if (range_lo == -2147483647 - 1 && range_hi == 2147483647)
1755 t.index = 0x82; /* 32 bit signed */
1756 else if (range_lo == 0 && range_hi == 127)
1757 t.index = 0x84; /* 8 bit character */
1758 else if (range_lo == 0 && range_hi == 255)
1759 t.index = 0x84; /* 8 bit unsigned */
1760 else if (range_lo == 0 && range_hi == 65535)
1761 t.index = 0x85; /* 16 bit unsigned */
1762 else if (range_lo == 0 && range_hi == 0xffffffff)
1763 t.index = 0x86; /* 32 bit unsigned */
1764 else if (range_lo == 0 && range_hi == -1)
1765 {
1766 if (type_name != NULL
1767 && (strcmp (type_name, "long long unsigned int") == 0
1768 || strcmp (type_name, "long long int") == 0))
1769 result = make_long_long ();
1770 else
1771 t.index = 0x86; /* 32 bit unsigned */
1772 }
1773 else if ((unsigned long long)range_lo == 0x8000000000000000ULL
1774 && range_hi == 0x7fffffffffffffffLL)
1775 result = make_long_long ();
1776 else if (range_lo == 4 && range_hi == 0)
1777 t.index = 0x88; /* 32 bit real */
1778 else if (range_lo == 8 && range_hi == 0)
1779 t.index = 0x89; /* 64 bit real */
1780 else if (range_lo == 12 && range_hi == 0)
1781 t.index = 0x8a; /* 80 bit real */
1782 else
1783 {
1784 no_warning ("Unknown range type: %lld..%lld", range_lo, range_hi);
1785 goto syntax;
1786 }
1787 break;
1788
1789 case 'R':
1790
1791 /* Complex number: R<?base_type?>;<size bytes>;<0>; */
1792
1793 ++parse_ptr;
1794 if (!parse_number (&num1) || !parse_char (';')
1795 || !parse_number (&width) || !parse_char (';')
1796 || !parse_number (&code) || !parse_char (';'))
1797 goto syntax;
1798
1799 /*
1800 * The 192bit type is not defined in the HLL specs, but we assume it for
1801 * convenince right now.
1802 *
1803 * It seems like no debugger acutally supports these types. So, we should,
1804 * when somebody requires it for debugging complex math, make structs out
1805 * of these declarations like GCC does for the 'complex int' type.
1806 *
1807 * .stabs "FakeComplexInt:t21=22=s8real:1,0,32;imag:1,32,32;;",128,0,0,0
1808 * .stabs "FakeComplexFloat:t23=24=s8real:12,0,32;imag:12,32,32;;",128,0,0,0
1809 * .stabs "FakeComplexDouble:t25=26=s16real:13,0,64;imag:13,64,64;;",128,0,0,0
1810 * .stabs "FakeComplexLongDouble:t27=28=s24real:14,0,96;imag:14,96,96;;",128,0,0,0
1811 *
1812 */
1813 t.tag = ty_prim;
1814 if (width == 8 && code == 0)
1815 t.index = 0x8c;
1816 else if (width == 16 && code == 0)
1817 t.index = 0x8d ;
1818 else if (width == 20 && code == 0)
1819 t.index = 0x8e;
1820 else if (width == 24 && code == 0)
1821 t.index = 0x8f; //TODO: bogus!!!
1822 else
1823 {
1824 no_warning ("Unknown complex type: %ld/%ld", width, code);
1825 goto syntax;
1826 }
1827 break;
1828
1829 case '*':
1830
1831 /* A pointer type: *<type> */
1832
1833 ++parse_ptr;
1834 t1 = parse_type (NULL);
1835 t.tag = ty_pointer;
1836 t.d.pointer = t1;
1837 break;
1838
1839 case 'k':
1840
1841 /* A const type: k<type>
1842 HLL don't have const typedefs afaik, so we just make it an alias. */
1843
1844 ++parse_ptr;
1845 t1 = parse_type (NULL);
1846 t.tag = ty_alias;
1847 t.d.alias = t1;
1848 break;
1849
1850 case 'B':
1851
1852 /* A volatile type: B<type>
1853 This is a SUN extension, and right now we don't care to generate
1854 specific HLL for it and stick with an alias. */
1855
1856 ++parse_ptr;
1857 t1 = parse_type (NULL);
1858 t.tag = ty_alias;
1859 t.d.alias = t1;
1860 break;
1861
1862
1863 case '&':
1864
1865 /* A reference type (C++): &<type> */
1866
1867 ++parse_ptr;
1868 t1 = parse_type (NULL);
1869 t.tag = ty_ref;
1870 t.d.ref = t1;
1871 break;
1872
1873 case '#':
1874
1875 /* A member function:
1876
1877 short format: ##<return_type>;
1878
1879 long format: #<domain_type>,<return_type>{,<arg_type>};
1880
1881 */
1882
1883 ++parse_ptr;
1884 args_grow.count = 0; /* ASSUMES we can safely use args_grow/args_list. */
1885 t2 = NULL; /* t2=Domain type; t1=Return type; */
1886 if (*parse_ptr == '#')
1887 {
1888 ++parse_ptr;
1889 t1 = parse_type (NULL);
1890 if (!parse_char (';'))
1891 goto syntax;
1892 no_warning ("We don't understand '##' methods.");
1893 }
1894 else
1895 {
1896 t2 = parse_type (NULL); /* Domain type */
1897 if (!parse_char (','))
1898 goto syntax;
1899 t1 = parse_type (NULL); /* Return type */
1900 /* Arguments */
1901 while (*parse_ptr != ';')
1902 {
1903 if (!parse_char (','))
1904 goto syntax;
1905 grow_by (&args_grow, 1);
1906 args_list[args_grow.count++] = parse_type (NULL); /* Argument type */
1907 }
1908 ++parse_ptr;
1909 }
1910
1911 t3 = NULL;
1912 #if 0 /* this doesn't really seems to be required, but can just as well leave it in. */
1913 if (args_grow.count)
1914 {
1915 t.tag = ty_args;
1916 t.index = -1;
1917 t.d.args.count = args_grow.count;
1918 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
1919 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
1920 t3 = type_add (&t);
1921 free (t.d.args.list);
1922 }
1923 #endif
1924
1925 /* Make a function type from the return type t1 */
1926 t.tag = ty_func;
1927 t.d.func.ret = t1;
1928 t.d.func.domain = t2;
1929 t.d.func.args = t3;
1930 t.d.func.arg_count = args_grow.count;
1931 args_grow.count = 0;
1932 t1 = type_add(&t);
1933
1934 break;
1935
1936 case '@':
1937
1938 /* Special GNU-defined types:
1939
1940 @s<bits>;<code>;
1941
1942 BITS: Number of bits
1943 CODE: Identifies the type:
1944 -16 bool
1945 -20 char
1946
1947 @s<bits>;<range record>
1948 Used for 64 bit ints.
1949
1950 @s<bits>;<num records>
1951 Used for non-standard enums.
1952
1953 @<basetype>,<membertype>
1954 Used for addressing class/struct/union members.
1955 */
1956
1957 ++parse_ptr;
1958 switch (*parse_ptr)
1959 {
1960 case 's':
1961 ++parse_ptr;
1962 if (!parse_number (&size) || !parse_char (';'))
1963 goto syntax;
1964 if (*parse_ptr == 'r')
1965 { /* nested */
1966 return parse_type(type_name);
1967 }
1968 if (*parse_ptr == 'e')
1969 goto l_parse_enum;
1970
1971 if (!parse_number (&code))
1972 goto syntax;
1973 if (size == 8 && code == -16)
1974 {
1975#if 0 /* debugger doesn't understand this game */
1976 t.tag = ty_prim;
1977 t.index = 0x90; /* 8 bit boolean */
1978#else
1979 t.tag = ty_values;
1980 t.d.values.count = 2;
1981 t.d.values.list = xmalloc (2 * sizeof (*t.d.values.list));
1982 t.d.values.list[0].index = 0;
1983 t.d.values.list[0].name = strpool_add (str_pool, "false");
1984 t.d.values.list[1].index = 1;
1985 t.d.values.list[1].name = strpool_add (str_pool, "true");
1986 t1 = type_add (&t);
1987 free (t.d.values.list);
1988
1989 t.tag = ty_prim;
1990 t.index = 0x80;
1991 t2 = type_add (&t);
1992
1993 t.tag = ty_enu;
1994 t.index = -1;
1995 t.d.enu.values = t1;
1996 t.d.enu.type = t2;
1997 t.d.enu.name = add_struct_name (NULL);
1998 t.d.enu.first = 0;
1999 t.d.enu.last = 1;
2000#endif
2001 }
2002 else
2003 {
2004 warning ("Unknown GNU extension type code: %ld, %ld bits",
2005 code, size);
2006 goto syntax;
2007 }
2008 if (!parse_char (';'))
2009 goto syntax;
2010 break;
2011
2012 case '0': case '1': case '2': case '3': case '4':
2013 case '5': case '6': case '7': case '8': case '9':
2014 {
2015 /* just make it an integer - see dbxout.c OFFSET_TYPE case. */
2016 if ( parse_number (&num1)
2017 && *parse_ptr == ','
2018 && ++parse_ptr
2019 && parse_number (&num2)
2020 )
2021 {
2022 t.tag = ty_prim;
2023 t.index = 0x82; /* 32 bit signed */
2024 }
2025 else
2026 {
2027 warning_parse ("Invalid BASE_OFFSET GNU extension format!");
2028 goto syntax;
2029 }
2030 break;
2031 }
2032
2033 default:
2034 warning ("Unknown GNU extension type: @%c", *parse_ptr);
2035 goto syntax;
2036 }
2037 break;
2038
2039 case 'x':
2040
2041 /* Incomplete type: xs<name>:
2042 xu<name>:
2043 xe<name>: */
2044
2045 ++parse_ptr;
2046 ch = *parse_ptr++;
2047 p1 = nextcolon2 (parse_ptr, 'x');
2048 if (p1 == NULL)
2049 {
2050 warning ("Invalid forward reference: missing colon");
2051 goto syntax;
2052 }
2053
2054 switch (ch)
2055 {
2056 case 's':
2057 case 'u':
2058 {
2059 /* make a fake structure. */
2060 t.tag = ty_prim;
2061 t.index = 0x80;
2062 tlist = xmalloc (1 * sizeof (*t.d.types.list));
2063 tlist[0] = type_add (&t);
2064 t.tag = ty_types;
2065 t.index = -1;
2066 t.d.types.count = 1;
2067 t.d.types.list = tlist;
2068 t1 = type_add (&t);
2069 free (tlist);
2070
2071 t.tag = ty_fields;
2072 t.index = -1;
2073 t.d.fields.count = 1;
2074 t.d.fields.list = xmalloc (1 * sizeof (*t.d.fields.list));
2075 t.d.fields.list[0].offset = 0;
2076 t.d.fields.list[0].name = strpool_add (str_pool, "This_Is_an_Incomplete_Structure_SORRY");
2077 t2 = type_add (&t);
2078 free (t.d.fields.list);
2079
2080 t.tag = ty_struc;
2081 t.index = -1;
2082 t.d.struc.types = t1;
2083 t.d.struc.fields = t2;
2084 t.d.struc.size = 1;
2085 t.d.struc.count = 1;
2086 t.d.struc.name = strpool_addn (str_pool, parse_ptr, p1 - parse_ptr);
2087 t.d.struc.flags = STRUC_FORWARD;
2088
2089 /* Avoid adding an incomplete type if the complete one is
2090 already defined. We're ignoring scoping (TODO). */
2091
2092 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2093 if ((t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2094 && !(t3->d.struc.flags & STRUC_FORWARD))
2095 || (t3->tag == ty_class && t3->d.class.name == t.d.struc.name))
2096 {
2097 result = t3;
2098 break;
2099 }
2100 break;
2101 }
2102
2103 case 'e':
2104 { /* just make it a primary. */
2105 /** @todo make an enum perhaps? */
2106 t.tag = ty_prim;
2107 t.index = 0x82; /* 32 bit signed */
2108 break;
2109 }
2110
2111 default:
2112 warning ("Unknown forward reference: %c", ch);
2113 parse_ptr = p1 + 1;
2114 goto syntax;
2115 }
2116 parse_ptr = p1 + 1;
2117 break;
2118
2119 case 's': /* struct, class */
2120 case 'u': /* union */
2121
2122 /* Structures, unions and classes:
2123
2124 s<size>[<baseclasses>]{<field>}{<method>}
2125
2126 BASECLASSES: !<#baseclasses>,{<baseclass>;}
2127 BASECLASS: <virtuality><visibility><offset>,<type>
2128 FIELD: <name>:[/0|1|2]<type>,<offset>,<width>;
2129 FIELD: <name>:[/0|1|2]<type>:<static_name>;
2130 METHOD: <name>::{<type>:<args>};
2131
2132 */
2133
2134 ++parse_ptr;
2135 class_flag = FALSE;
2136 if (!parse_number (&size))
2137 goto syntax;
2138 grow_init (&g1, &tmp_fields, sizeof (*tmp_fields), 8);
2139
2140 /* Parse the base classes, if present. */
2141
2142 if (*parse_ptr == '!')
2143 {
2144 long i, n;
2145
2146 ++parse_ptr;
2147 class_flag = TRUE;
2148 if (!parse_number (&n) || !parse_char (','))
2149 goto syntax;
2150 for (i = 0; i < n; ++i)
2151 {
2152 grow_by (&g1, 1);
2153 tf = &tmp_fields[g1.count++];
2154 tf->flags = MEMBER_BASE;
2155 tf->name = NULL;
2156 tf->sname = NULL;
2157 switch (*parse_ptr)
2158 {
2159 case '0':
2160 ++parse_ptr;
2161 break;
2162 case '1':
2163 tf->flags |= MEMBER_VIRTUAL;
2164 ++parse_ptr;
2165 break;
2166 default:
2167 warning ("Invalid base class visibility: %c", *parse_ptr);
2168 if (*parse_ptr != 0)
2169 ++parse_ptr;
2170 }
2171
2172 tf->flags |= parse_visibility ();
2173 if (!parse_number (&offset) || !parse_char (','))
2174 goto syntax;
2175 tf->offset = offset >> 3;
2176 t1 = parse_type (NULL);
2177 switch (t1->tag)
2178 {
2179 case ty_class:
2180 break;
2181 case ty_struc:
2182 /* kso #456 2003-06-11: Don't mess with forward defines! */
2183 if (!( (t1->d.struc.flags & STRUC_FORWARD)
2184 /*&& t1->d.struc.types == NULL
2185 && t1->d.struc.fields == NULL*/))
2186 struct_to_class (t1);
2187 break;
2188 default:
2189 warning ("Invalid base class type");
2190 }
2191 tf->type = t1;
2192 if (!parse_char (';'))
2193 goto syntax;
2194 }
2195 }
2196
2197 while (*parse_ptr != ';')
2198 {
2199 p1 = nextcolon (parse_ptr);
2200 if (p1 == NULL)
2201 {
2202 warning ("Invalid structure type: missing colon");
2203 goto syntax;
2204 }
2205 n = p1 - parse_ptr;
2206 grow_by (&g1, 1);
2207 tf = &tmp_fields[g1.count++];
2208 tf->flags = VIS_PUBLIC;
2209 tf->name = strpool_addn (str_pool, parse_ptr, n);
2210 tf->mnglname = NULL;
2211 parse_ptr = p1 + 1;
2212 if (*parse_ptr == ':')
2213 {
2214 class_flag = TRUE;
2215 ++parse_ptr;
2216 do
2217 {
2218 const char * pszMangled;
2219
2220 t1 = parse_type (NULL);
2221 offset = 0;
2222 if (t1 == NULL || t1->tag != ty_func)
2223 {
2224 no_warning ("Invalid member function type");
2225 goto syntax;
2226 }
2227
2228 if (*parse_ptr != ':')
2229 {
2230 no_warning ("Arguments for member function missing");
2231 goto syntax;
2232 }
2233 ++parse_ptr;
2234 pszMangled = parse_ptr;
2235 if (!parse_mangled_args ())
2236 goto syntax;
2237 tf->mnglname = strpool_addn(str_pool, pszMangled, parse_ptr - pszMangled - 1); /* ASSUMES no extra spaces! */
2238 tf->flags = parse_visibility () | MEMBER_FUNCTION;
2239 switch (*parse_ptr)
2240 {
2241 case 'A':
2242 ++parse_ptr;
2243 break;
2244 case 'B':
2245 tf->flags |= MEMBER_CONST;
2246 ++parse_ptr;
2247 break;
2248 case 'C':
2249 tf->flags |= MEMBER_VOLATILE;
2250 ++parse_ptr;
2251 break;
2252 case 'D':
2253 tf->flags |= MEMBER_CONST | MEMBER_VOLATILE;
2254 ++parse_ptr;
2255 break;
2256 default:
2257 no_warning ("Unknown member function qualifier: %c",
2258 *parse_ptr);
2259 if (*parse_ptr != 0)
2260 ++parse_ptr;
2261 }
2262 switch (*parse_ptr)
2263 {
2264 case '.':
2265 ++parse_ptr;
2266 break;
2267 case '*':
2268 tf->flags |= MEMBER_VIRTUAL;
2269 ++parse_ptr;
2270 if (!parse_number (&offset) || !parse_char (';'))
2271 {
2272 warning ("Invalid data for virtual member function");
2273 goto syntax;
2274 }
2275 parse_type (NULL);
2276 if (!parse_char (';'))
2277 {
2278 warning ("Invalid data for virtual member function");
2279 goto syntax;
2280 }
2281 break;
2282 case '?':
2283 tf->flags |= MEMBER_STATIC;
2284 ++parse_ptr;
2285 break;
2286 default:
2287 no_warning ("Unknown member function qualifier: %c",
2288 *parse_ptr);
2289 if (*parse_ptr != 0)
2290 ++parse_ptr;
2291 }
2292 /* Contstructor / Destructor hack -
2293 * TODO: verify that this doesn't really work with overloaded constructors and fix it.
2294 */
2295 if (!(tf->flags & MEMBER_STATIC))
2296 {
2297 /* using the strpool is much faster! */
2298 static const char *pszCompCtor, *pszBaseCtor, *pszCompDtor, *pszBaseDtor = NULL;
2299 if (!pszBaseDtor)
2300 {
2301 pszCompCtor = strpool_addn(str_pool, "__comp_ctor", 11);
2302 pszBaseCtor = strpool_addn(str_pool, "__base_ctor", 11);
2303 pszCompDtor = strpool_addn(str_pool, "__comp_dtor", 11);
2304 pszBaseDtor = strpool_addn(str_pool, "__base_dtor", 11);
2305 }
2306
2307 if (tf->name == pszCompCtor || tf->name == pszBaseCtor)
2308 tf->flags |= MEMBER_CTOR;
2309 else if (tf->name == pszCompDtor || tf->name == pszBaseDtor)
2310 tf->flags |= MEMBER_DTOR;
2311 }
2312 tf->type = t1;
2313 tf->offset = offset;
2314 if (*parse_ptr != ';')
2315 {
2316 /* Another member function with the same name
2317 follows. */
2318
2319 grow_by (&g1, 1);
2320 tf = &tmp_fields[g1.count++];
2321 tf->flags = VIS_PUBLIC | MEMBER_FUNCTION;
2322 tf->name = tf[-1].name;
2323 }
2324 } while (*parse_ptr != ';');
2325 ++parse_ptr;
2326 }
2327 else
2328 {
2329 if (*parse_ptr == '/')
2330 {
2331 class_flag = TRUE;
2332 ++parse_ptr;
2333 tf->flags = parse_visibility ();
2334 }
2335 t1 = parse_type (NULL);
2336 if (*parse_ptr == ':')
2337 {
2338 /* Static member */
2339
2340 ++parse_ptr;
2341 tf->flags |= MEMBER_STATIC;
2342 p1 = strchr (parse_ptr, ';');
2343 if (p1 == NULL)
2344 {
2345 warning ("Invalid static member: missing semicolon");
2346 goto syntax;
2347 }
2348 n = p1 - parse_ptr;
2349 tf->sname = strpool_addn (str_pool, parse_ptr, n);
2350 parse_ptr = p1 + 1;
2351 tf->offset = 0;
2352 }
2353 else
2354 {
2355 if (!parse_char (',') || !parse_number (&offset))
2356 goto syntax;
2357 if (*parse_ptr == ',')
2358 {
2359 ++parse_ptr;
2360 if (!parse_number (&width))
2361 goto syntax;
2362 }
2363 else
2364 width = 0; /* vtable pointer field */
2365 if (!parse_char (';'))
2366 goto syntax;
2367 if ((offset & 7) != 0 || width != 8 * type_size (t1))
2368 {
2369 t.tag = ty_bits;
2370 t.d.bits.type = t1;
2371 t.d.bits.start = offset & 7;
2372 t.d.bits.count = width;
2373 t1 = type_add (&t);
2374 }
2375 tf->offset = offset >> 3;
2376 }
2377 tf->type = t1;
2378 }
2379 }
2380 ++parse_ptr;
2381 if (*parse_ptr == '~')
2382 {
2383 /* Type reference to the first base class. This field is
2384 present for classes which contain virtual functions. */
2385
2386 ++parse_ptr;
2387 if (!parse_char ('%'))
2388 goto syntax;
2389 t1 = parse_type (NULL);
2390 if (!parse_char (';'))
2391 goto syntax;
2392 }
2393
2394 if (class_flag)
2395 {
2396 tlist = xmalloc (g1.count * sizeof (*t.d.types.list));
2397 for (i = 0; i < g1.count; ++i)
2398 {
2399 if (tmp_fields[i].flags & MEMBER_FUNCTION)
2400 {
2401 t2 = tmp_fields[i].type;
2402 tt = follow_refs (t2)->tag;
2403 /* TODO: check later if ty_stabs_ref */
2404 if (tt != ty_func && tt != ty_stabs_ref)
2405 error ("Invalid type for member function %s",
2406 tmp_fields[i].name);
2407 t.tag = ty_memfunc;
2408 t.d.memfunc.type = t2;
2409 t.d.memfunc.name = tmp_fields[i].name;
2410 t.d.memfunc.mnglname = tmp_fields[i].mnglname;
2411 t.d.memfunc.flags = tmp_fields[i].flags;
2412 t.d.memfunc.offset = tmp_fields[i].offset;
2413 }
2414 else if (tmp_fields[i].flags & MEMBER_BASE)
2415 {
2416 t.tag = ty_baseclass;
2417 t.d.baseclass.type = tmp_fields[i].type;
2418 t.d.baseclass.flags = tmp_fields[i].flags;
2419 t.d.baseclass.offset = tmp_fields[i].offset;
2420 }
2421 else
2422 {
2423 t.tag = ty_member;
2424 t.d.member.type = tmp_fields[i].type;
2425 t.d.member.offset = tmp_fields[i].offset;
2426 t.d.member.flags = tmp_fields[i].flags;
2427 t.d.member.name = tmp_fields[i].name;
2428 t.d.member.sname = tmp_fields[i].sname;
2429 }
2430 t2 = type_add (&t);
2431 tlist[i] = t2;
2432 }
2433 t.tag = ty_types;
2434 t.d.types.count = g1.count;
2435 t.d.types.list = tlist;
2436 t1 = type_add (&t);
2437 free (tlist);
2438
2439 t.tag = ty_class;
2440 t.d.class.members = t1;
2441 t.d.class.size = size;
2442 t.d.class.count = g1.count;
2443 t.d.class.name = add_struct_name (type_name);
2444
2445 /* Check if there is an incomplete type waiting to be filled
2446 in (forward reference). We're ignoring scoping (TODO). */
2447
2448 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2449 if (t3->tag == ty_struc && t3->d.struc.name == t.d.class.name
2450 /*&& t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2451 && (t3->d.struc.flags & STRUC_FORWARD))
2452 {
2453 if (t3->index != -1)
2454 warning ("This cannot happen, case 1");
2455 result = t3;
2456 t3->tag = ty_class;
2457 t3->d.class = t.d.class;
2458 break;
2459 }
2460 }
2461 else
2462 {
2463 t.tag = ty_types;
2464 t.d.types.count = g1.count;
2465 t.d.types.list = xmalloc (g1.count * sizeof (*t.d.types.list));
2466 for (i = 0; i < g1.count; ++i)
2467 t.d.types.list[i] = tmp_fields[i].type;
2468 t1 = type_add (&t);
2469 free (t.d.types.list);
2470
2471 t.tag = ty_fields;
2472 t.d.fields.count = g1.count;
2473 t.d.fields.list = xmalloc (g1.count * sizeof (*t.d.fields.list));
2474 for (i = 0; i < g1.count; ++i)
2475 {
2476 t.d.fields.list[i].offset = tmp_fields[i].offset;
2477 t.d.fields.list[i].name = tmp_fields[i].name;
2478 }
2479 t2 = type_add (&t);
2480 free (t.d.fields.list);
2481
2482 t.tag = ty_struc;
2483 t.d.struc.count = g1.count;
2484 t.d.struc.size = size;
2485 t.d.struc.types = t1;
2486 t.d.struc.fields = t2;
2487 t.d.struc.name = add_struct_name (type_name);
2488 t.d.struc.flags = 0;
2489
2490 /* Check if there is an incomplete type waiting to be filled
2491 in (forward reference). We're ignoring scoping (TODO). */
2492
2493 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2494 if (t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2495 /* && t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2496 && (t3->d.struc.flags & STRUC_FORWARD))
2497 {
2498 if (t3->index != -1)
2499 warning ("This cannot happen, case 1");
2500 result = t3;
2501 t3->d.struc = t.d.struc;
2502 break;
2503 }
2504 }
2505 grow_free (&g1);
2506 break;
2507
2508 case 'e':
2509 size = 32;
2510l_parse_enum:
2511
2512 /* Enumeration type: e{<name>:<value>,}; */
2513
2514 ++parse_ptr;
2515 grow_init (&g1, &tmp_values, sizeof (*tmp_values), 8);
2516 while (*parse_ptr != ';')
2517 {
2518 p1 = nextcolon (parse_ptr);
2519 if (p1 == NULL)
2520 {
2521 warning ("Invalid enum type: missing colon");
2522 goto syntax;
2523 }
2524 n = p1 - parse_ptr;
2525 grow_by (&g1, 1);
2526 tv = &tmp_values[g1.count++];
2527 tv->name = strpool_addn (str_pool, parse_ptr, n);
2528 parse_ptr = p1 + 1;
2529 if (!parse_number (&tv->index) || !parse_char (','))
2530 goto syntax;
2531 }
2532 ++parse_ptr;
2533 t.tag = ty_values;
2534 t.d.values.count = g1.count;
2535 t.d.values.list = xmalloc (g1.count * sizeof (*t.d.values.list));
2536 if (g1.count == 0)
2537 lo = hi = 0;
2538 else
2539 lo = hi = tmp_values[0].index;
2540 for (i = 0; i < g1.count; ++i)
2541 {
2542 t.d.values.list[i].index = tmp_values[i].index;
2543 t.d.values.list[i].name = tmp_values[i].name;
2544 if (tmp_values[i].index < lo)
2545 lo = tmp_values[i].index;
2546 if (tmp_values[i].index > hi)
2547 hi = tmp_values[i].index;
2548 }
2549 t1 = type_add (&t);
2550 free (t.d.values.list);
2551
2552 t.tag = ty_prim;
2553 switch (size)
2554 {
2555 case 8: t.index = 0x80; break; /* 8 bit signed */
2556 case 16: t.index = 0x81; break; /* 16 bit signed */
2557 case 32: t.index = 0x82; break; /* 32 bit signed */
2558 case 64: result = t2 = make_long_long (); break;
2559 default: goto syntax;
2560 }
2561 if (!result)
2562 t2 = type_add (&t);
2563
2564 t.tag = ty_enu;
2565 t.index = -1;
2566 t.d.enu.values = t1;
2567 t.d.enu.type = t2;
2568 t.d.enu.name = add_struct_name (type_name);
2569 t.d.enu.first = lo;
2570 t.d.enu.last = hi;
2571 grow_free (&g1);
2572 break;
2573
2574 case 'a':
2575 /* An array: ar<index_type>;<lower_bound>;<upper_bound>;<el_type> */
2576
2577 ++parse_ptr;
2578 if (!parse_char ('r'))
2579 goto syntax;
2580 if (strncmp (parse_ptr, "0;", 2) == 0)
2581 {
2582 /* This invalid type index seems to be caused by a bug of
2583 GCC 2.8.1. Replace with a 32-bit signed integer. */
2584
2585 t.tag = ty_prim;
2586 t.index = 0x82; /* 32 bit signed */
2587 t1 = type_add (&t);
2588 ++parse_ptr;
2589 }
2590 else
2591 t1 = parse_type (NULL); /* Index type */
2592 if (!parse_char (';')
2593 || !parse_number (&lo) || !parse_char (';') /* Lower bound */
2594 || !parse_number (&hi) || !parse_char (';')) /* Upper bound */
2595 goto syntax;
2596 t2 = parse_type (NULL); /* Elements type */
2597 if (lo == 0 && hi == 0) /* Variable-length array */
2598 {
2599 t.tag = ty_pointer; /* Turn into pointer */
2600 t.d.pointer = t2;
2601 }
2602 else
2603 {
2604 t.tag = ty_array;
2605 t.d.array.first = lo;
2606 t.d.array.last = hi;
2607 t.d.array.itype = t1;
2608 t.d.array.etype = t2;
2609 }
2610 break;
2611
2612 case 'f':
2613
2614 /* A function: f<return_type> */
2615
2616 ++parse_ptr;
2617 t1 = parse_type (NULL); /* Return type */
2618 t.tag = ty_func;
2619 t.d.func.ret = t1;
2620 t.d.func.domain = NULL;
2621 t.d.func.args = NULL;
2622 t.d.func.arg_count = 0;
2623 break;
2624
2625 default:
2626 no_warning ("Unknown type: %c", *parse_ptr);
2627 goto syntax;
2628 }
2629
2630 /* Add the type to the tables. */
2631
2632 if (result == NULL)
2633 result = type_add (&t);
2634 return result;
2635
2636syntax:
2637 no_warning ("syntax error in stabs: %c (off %ld)\n stabs: %s",
2638 *parse_ptr, parse_ptr - parse_start, parse_start);
2639
2640 grow_free (&g1);
2641 t.tag = ty_prim;
2642 t.index = 0x82; /* 32 bit signed */
2643 return type_add (&t);
2644}
2645
2646
2647/* Parse a stabs type (the name of the type is TYPE_NAME, which is
2648 NULL for an unnamed type) and check for end of the string. If not
2649 all characters of the string have been parsed, a warning message is
2650 displayed. Return the internal representation of the type. */
2651
2652static struct type *parse_complete_type (const char *type_name)
2653{
2654 struct type *tp;
2655
2656 tp = parse_type (type_name);
2657 if (*parse_ptr != 0)
2658 no_warning ("unexpected character at end of stabs type: %c (off %ld)\n stabs: %s",
2659 *parse_ptr, parse_ptr - parse_start, parse_start);
2660 return tp;
2661}
2662
2663
2664/* Create an HLL type number for type TP. If a type number has already
2665 been assigned to TP, use that number. Otherwise, allocate a new
2666 (complex) type number and define the type in the $$TYPES segment.
2667 The HLL type number us stored to *HLL.
2668
2669 Note: This function must not be called between sst_start() and
2670 sst_end() or tt_start() and tt_end() as entries are made both in
2671 the SST ($$SYMBOLS) and the type table ($$TYPES). */
2672
2673static void make_type (struct type *tp, int *hll)
2674{
2675 struct type *t1;
2676 int ti_1, ti_2, ti_3, i, qual;
2677 int patch1;
2678
2679#define RETURN(X) do {*hll = (X); return;} while (0)
2680
2681 if (tp->index <= -2)
2682 {
2683 /* bird: A hack where we allow a 2nd recursion of rectain types.
2684 * The normal issue is C++ class references. */
2685 if (tp->tag != ty_alias || tp->index <= -3)
2686 {
2687 warning_parse ("Cycle detected by make_type.");
2688 RETURN (0);
2689 }
2690 }
2691 if (tp->index > -1)
2692 RETURN (tp->index);
2693 tp->index--;
2694 switch (tp->tag)
2695 {
2696 case ty_stabs_ref:
2697 t1 = stype_find (tp->d.stabs_ref);
2698 if (t1 == NULL)
2699 {
2700 warning_parse ("Undefined stabs type %d referenced", tp->d.stabs_ref);
2701 RETURN (0x82); /* 32 bit signed */
2702 }
2703 make_type (t1, &tp->index);
2704 *hll = tp->index;
2705 break;
2706
2707 case ty_alias:
2708 make_type (tp->d.alias, &tp->index);
2709 *hll = tp->index;
2710 break;
2711
2712 case ty_pointer:
2713 ti_1 = tp->d.pointer->index;
2714 if (ti_1 >= 0x80 && ti_1 <= 0xa0)
2715 {
2716 tp->index = ti_1 + 0x20;
2717 RETURN (tp->index);
2718 }
2719 *hll = tp->index = hll_type_index++;
2720 tt_start (0x7a, 0x01); /* 32-bit near pointer */
2721 patch1 = tt.size;
2722 tt_index (0);
2723 tt_end ();
2724 make_type (tp->d.pointer, &ti_1);
2725 buffer_patch_word (&tt, patch1+1, ti_1);
2726 break;
2727
2728 case ty_ref:
2729 *hll = tp->index = hll_type_index++;
2730 tt_start (0x48, 0x00); /* Reference type (C++) */
2731 patch1 = tt.size;
2732 tt_word (0);
2733 tt_end ();
2734 make_type (tp->d.ref, &ti_1);
2735 buffer_patch_word (&tt, patch1, ti_1);
2736 break;
2737
2738 case ty_struc:
2739 {
2740 struct field *fields = NULL;
2741 struct type **types = NULL;
2742 int cFields = tp->d.struc.count;
2743
2744 if ( tp->d.struc.fields
2745 && tp->d.struc.fields->tag == ty_fields
2746 && tp->d.struc.fields->d.fields.count == cFields)
2747 fields = tp->d.struc.fields->d.fields.list;
2748 if ( tp->d.struc.types
2749 && tp->d.struc.types->tag == ty_types
2750 && tp->d.struc.types->d.types.count == cFields)
2751 types = tp->d.struc.types->d.types.list;
2752
2753 if (cplusplus_flag && ((types && fields) || !cFields))
2754 {
2755 struct type t;
2756 struct type **list;
2757 int i;
2758 *hll = tp->index = hll_type_index++;
2759 tt_start (0x40, 0x01); /* Class is-struct */
2760 tt_dword (tp->d.struc.size);
2761 tt_word (tp->d.struc.count);
2762 patch1 = tt.size;
2763 tt_word (0); /* Members */
2764 tt_enc (tp->d.struc.name); /* Class name */
2765 tt_end ();
2766
2767 list = xmalloc (cFields * sizeof (*list));
2768 for (i = 0; i < cFields; ++i)
2769 {
2770 t.tag = ty_member;
2771 t.index = -1;
2772 t.d.member.type = types[i];
2773 t.d.member.offset = fields[i].offset;
2774 t.d.member.flags = VIS_PUBLIC;
2775 t.d.member.name = fields[i].name;
2776 t.d.member.sname = NULL;
2777 list[i] = type_add (&t);
2778 }
2779
2780 t.tag = ty_types;
2781 t.index = -1;
2782 t.d.types.count = cFields;
2783 t.d.types.list = list;
2784 t1 = type_add (&t);
2785 free (list);
2786
2787 make_type (t1, &ti_1);
2788 buffer_patch_word (&tt, patch1, ti_1);
2789 }
2790 else
2791 {
2792 tt_start (0x79, 0x00); /* Structure/Union */
2793 tt_dword (tp->d.struc.size);
2794 tt_word (tp->d.struc.count);
2795 patch1 = tt.size;
2796 tt_index (0);
2797 tt_index (0);
2798 tt_name (tp->d.struc.name);
2799 tt_end ();
2800 *hll = tp->index = hll_type_index++;
2801 if (tp->d.struc.types == NULL && tp->d.struc.fields == NULL)
2802 {
2803 ti_1 = 0;
2804 ti_2 = 1;
2805 }
2806 else
2807 {
2808 make_type (tp->d.struc.types, &ti_1);
2809 make_type (tp->d.struc.fields, &ti_2);
2810 }
2811 buffer_patch_word (&tt, patch1+1, ti_1);
2812 buffer_patch_word (&tt, patch1+4, ti_2);
2813 }
2814 break;
2815 }
2816
2817 case ty_class:
2818 *hll = tp->index = hll_type_index++;
2819 tt_start (0x40, 0x00); /* Class */
2820 tt_dword (tp->d.class.size);
2821 tt_word (tp->d.class.count);
2822 patch1 = tt.size;
2823 tt_word (0); /* Members */
2824 tt_enc (tp->d.class.name); /* Class name */
2825 tt_end ();
2826 make_type (tp->d.class.members, &ti_1);
2827 buffer_patch_word (&tt, patch1, ti_1);
2828 break;
2829
2830 case ty_member:
2831 qual = 0;
2832 if (tp->d.member.flags & MEMBER_STATIC)
2833 qual |= 0x01;
2834 tt_start (0x46, qual);
2835 tt_byte (tp->d.member.flags & MEMBER_VIS);
2836 patch1 = tt.size;
2837 tt_word (0);
2838 tt_unsigned_span (tp->d.member.offset);
2839 if (tp->d.member.flags & MEMBER_STATIC)
2840 tt_enc (tp->d.member.sname);
2841 else
2842 tt_enc ("");
2843 tt_enc (tp->d.member.name);
2844 tt_end ();
2845 *hll = tp->index = hll_type_index++;
2846
2847 make_type (tp->d.member.type, &ti_1);
2848 buffer_patch_word (&tt, patch1, ti_1);
2849 break;
2850
2851 case ty_enu:
2852 make_type (tp->d.enu.type, &ti_1);
2853 make_type (tp->d.enu.values, &ti_2);
2854 tt_start (0x7b, 0x00); /* Enum */
2855 tt_index (ti_1); /* Element's data type */
2856 tt_index (ti_2); /* List of values */
2857 tt_signed_span (tp->d.enu.first); /* Minimum index */
2858 tt_signed_span (tp->d.enu.last); /* Maximum index */
2859 tt_name (tp->d.enu.name);
2860 tt_end ();
2861 *hll = tp->index = hll_type_index++;
2862 break;
2863
2864 case ty_array:
2865 make_type (tp->d.array.itype, &ti_1);
2866 make_type (tp->d.array.etype, &ti_2);
2867 tt_start (0x78, 0x00); /* Array */
2868 tt_dword (type_size (tp));
2869 tt_index (ti_1);
2870 tt_index (ti_2);
2871 tt_name (""); /* Name */
2872 tt_end ();
2873 *hll = tp->index = hll_type_index++;
2874 break;
2875
2876 case ty_bits:
2877 tt_start (0x5c, 0x0a); /* Bit string */
2878 tt_byte (tp->d.bits.start);
2879 tt_unsigned_span (tp->d.bits.count);
2880 tt_end ();
2881 *hll = tp->index = hll_type_index++;
2882 break;
2883
2884 case ty_func:
2885 tt_start (0x54, 0x05); /* Function */
2886 tt_word (tp->d.func.arg_count); /* Number of parameters */
2887 tt_word (tp->d.func.arg_count); /* Max. Number of parameters */
2888 patch1 = tt.size;
2889 tt_index (0); /* Return value */
2890 tt_index (0); /* Argument list */
2891 tt_end ();
2892 *hll = tp->index = hll_type_index++;
2893
2894 if (tp->d.func.domain)
2895 make_type (tp->d.func.domain, &ti_3);
2896 make_type (tp->d.func.ret, &ti_1);
2897 if (tp->d.func.args == NULL)
2898 ti_2 = 0;
2899 else
2900 make_type (tp->d.func.args, &ti_2);
2901 buffer_patch_word (&tt, patch1+1, ti_1);
2902 buffer_patch_word (&tt, patch1+4, ti_2);
2903 break;
2904
2905 case ty_args:
2906 if (tp->d.args.count == 0)
2907 {
2908 tp->index = 0;
2909 RETURN (tp->index);
2910 }
2911 for (i = 0; i < tp->d.args.count; ++i)
2912 make_type (tp->d.args.list[i], &ti_1);
2913 tt_start (0x7f, 0x04); /* List (function parameters) */
2914 for (i = 0; i < tp->d.args.count; ++i)
2915 {
2916 tt_byte (0x01); /* Flag: pass by value, no descriptor */
2917 tt_index (tp->d.args.list[i]->index);
2918 }
2919 tt_end ();
2920 *hll = tp->index = hll_type_index++;
2921 break;
2922
2923 case ty_types:
2924 if (tp->d.types.count == 0)
2925 {
2926 tp->index = 0;
2927 RETURN (tp->index);
2928 }
2929 *hll = tp->index = hll_type_index++;
2930 tt_start (0x7f, 0x01); /* List (structure field types) */
2931 patch1 = tt.size;
2932 for (i = 0; i < tp->d.types.count; ++i)
2933 tt_index (0);
2934 tt_end ();
2935 for (i = 0; i < tp->d.types.count; ++i)
2936 {
2937 make_type (tp->d.types.list[i], &ti_1);
2938 buffer_patch_word (&tt, patch1 + 1 + 3*i, ti_1);
2939 }
2940 break;
2941
2942 case ty_fields:
2943 if (tp->d.fields.count == 0)
2944 {
2945 tp->index = 0;
2946 RETURN (tp->index);
2947 }
2948 tt_start (0x7f, 0x02); /* List (structure field names) */
2949 for (i = 0; i < tp->d.fields.count; ++i)
2950 {
2951 tt_name (tp->d.fields.list[i].name);
2952 tt_unsigned_span (tp->d.fields.list[i].offset);
2953 }
2954 tt_end ();
2955 *hll = tp->index = hll_type_index++;
2956 break;
2957
2958 case ty_values:
2959 if (tp->d.values.count == 0)
2960 {
2961 tp->index = -1;
2962 RETURN (tp->index);
2963 }
2964 tt_start (0x7f, 0x03); /* Enum values */
2965 for (i = 0; i < tp->d.values.count; ++i)
2966 {
2967 tt_name (tp->d.values.list[i].name);
2968 tt_unsigned_span (tp->d.values.list[i].index);
2969 }
2970 tt_end ();
2971 *hll = tp->index = hll_type_index++;
2972 break;
2973
2974 case ty_memfunc:
2975 make_type (tp->d.memfunc.type, &ti_1);
2976 qual = 0;
2977 if (tp->d.memfunc.flags & MEMBER_STATIC)
2978 qual |= 0x01;
2979 if (tp->d.memfunc.flags & MEMBER_CONST)
2980 qual |= 0x04;
2981 if (tp->d.memfunc.flags & MEMBER_VOLATILE)
2982 qual |= 0x08;
2983 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2984 qual |= 0x10;
2985 tt_start (0x45, qual); /* Member Function */
2986 tt_byte (tp->d.memfunc.flags & MEMBER_VIS); /* Protection */
2987 if (tp->d.memfunc.flags & MEMBER_CTOR)
2988 tt_byte (1); /* Constructor */
2989 else if (tp->d.memfunc.flags & MEMBER_DTOR)
2990 tt_byte (2); /* Destructor */
2991 else
2992 tt_byte (0); /* Regular member function */
2993 tt_word (ti_1); /* Type index of function */
2994 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2995 tt_unsigned_span (tp->d.memfunc.offset); /* Index into vtable */
2996 tt_enc (tp->d.memfunc.name); /* Name of the function */
2997 tt_end ();
2998 *hll = tp->index = hll_type_index++;
2999 break;
3000
3001 case ty_baseclass:
3002 make_type (tp->d.baseclass.type, &ti_1);
3003 qual = 0;
3004 if (tp->d.baseclass.flags & MEMBER_VIRTUAL)
3005 qual |= 0x01;
3006 tt_start (0x41, qual); /* Base Class */
3007 tt_byte (tp->d.baseclass.flags & MEMBER_VIS);
3008 tt_word (ti_1); /* Type */
3009 tt_unsigned_span (tp->d.baseclass.offset);
3010 tt_end ();
3011 *hll = tp->index = hll_type_index++;
3012 break;
3013
3014 default:
3015 error ("make_type: unknown tag %d", tp->tag);
3016 }
3017}
3018
3019
3020/* Parse an unnamed stabs type and return an HLL type index for it. */
3021
3022static int hll_type (void)
3023{
3024 struct type *tp;
3025 int ti;
3026
3027 tp = parse_complete_type (NULL);
3028 make_type (tp, &ti);
3029 return ti;
3030}
3031
3032
3033/* Return the symbol name of symbol *INDEX, concatenating the names of
3034 successive symbol table entries if necessary. A pointer to the
3035 string is stored to *STR. If symbols are concatenated, *INDEX is
3036 adjusted to refer to the last symbol table entry used, in order to
3037 refer to the next symbol after incrementing by one. If TRUE is
3038 returned, the caller should free the string when it is no longer
3039 needed. If FALSE is returned, the string must not be freed. */
3040
3041static int concat_symbols (int *index, const char **str)
3042{
3043 int i, n, len;
3044 char *new;
3045 const char *p;
3046
3047 len = 0;
3048 for (i = *index; i < sym_count; ++i)
3049 {
3050 p = str_ptr + sym_ptr[i].n_un.n_strx;
3051 n = strlen (p);
3052 if (n > 0 && p[n-1] == '\\')
3053 len += n - 1;
3054 else
3055 {
3056 len += n;
3057 break;
3058 }
3059 }
3060 if (i == *index)
3061 {
3062 *str = str_ptr + sym_ptr[i].n_un.n_strx;
3063 return FALSE;
3064 }
3065 else
3066 {
3067 new = xmalloc (len + 1);
3068 *str = new;
3069 for (i = *index; i < sym_count; ++i)
3070 {
3071 p = str_ptr + sym_ptr[i].n_un.n_strx;
3072 n = strlen (p);
3073 if (n > 0 && p[n-1] == '\\')
3074 {
3075 memcpy (new, p, n - 1);
3076 new += n - 1;
3077 }
3078 else
3079 {
3080 memcpy (new, p, n);
3081 new += n;
3082 break;
3083 }
3084 }
3085 *new = 0;
3086 *index = i;
3087 return TRUE;
3088 }
3089}
3090
3091
3092/* Parse a stabs type definition and store the internal representation
3093 of that type. The sym_ptr index is passed in INDEX. As we might
3094 have to concatenate successive symbol table entries, *INDEX is
3095 updated to refer to the next symbol. */
3096
3097static void parse_typedef (int *index)
3098{
3099 char *name;
3100 const char *str, *p;
3101 struct type *t;
3102 int n, alloc_flag;
3103
3104 alloc_flag = concat_symbols (index, &str);
3105 parse_start = str;
3106 parse_pindex = index;
3107 p = nextcolon (str);
3108 if (p != NULL)
3109 {
3110 n = p - str;
3111 name = alloca (n + 1);
3112 memcpy (name, str, n);
3113 name[n] = 0;
3114#if defined (HLL_DEBUG)
3115 printf ("LSYM/LCSYM/GSYM/PSYM/RSYM/STSYM/FUN %s\n", str);
3116#endif
3117 parse_ptr = p + 1;
3118 switch (*parse_ptr)
3119 {
3120 case 'f':
3121 case 'F':
3122 ++parse_ptr;
3123 /* Note: don't call parse_complete_type() as there's a comma
3124 at the end for nested functions. */
3125 t = parse_type (NULL);
3126 break;
3127
3128 case 't':
3129 case 'T':
3130 ++parse_ptr;
3131 if (*parse_ptr == 't')
3132 ++parse_ptr; /* synonymous type */
3133 t = parse_complete_type (name);
3134#if defined (HLL_DEBUG)
3135 printf (" type: ");
3136 show_type (t);
3137 printf ("\n");
3138#endif
3139 break;
3140
3141 case 'p':
3142 case 'r':
3143 case 'G':
3144 case 'S':
3145 case 'V':
3146 ++parse_ptr;
3147 t = parse_complete_type (name);
3148#if defined (HLL_DEBUG)
3149 printf (" type: ");
3150 show_type (t);
3151 printf ("\n");
3152#endif
3153 break;
3154
3155 case '0': case '1': case '2': case '3': case '4':
3156 case '5': case '6': case '7': case '8': case '9':
3157 t = parse_complete_type (name);
3158#if defined (HLL_DEBUG)
3159 printf (" type: ");
3160 show_type (t);
3161 printf ("\n");
3162#endif
3163 break;
3164 }
3165 }
3166 if (alloc_flag)
3167 free ((void *)str); /* remove const attribute */
3168}
3169
3170
3171/* Write a `begin' subrecord to the SST. The block begins at ADDR in
3172 the text segment. */
3173
3174static size_t hll_begin (dword addr)
3175{
3176 size_t ptr;
3177 struct relocation_info r;
3178
3179 sst_start (SST_begin);
3180 r.r_address = sst.size;
3181 ptr = sst.size;
3182 buffer_dword (&sst, addr); /* Segment offset */
3183 buffer_dword (&sst, 0); /* Length of block -- patched later */
3184#if 1
3185 buffer_nstr (&sst, ""); /* Name of block (optional) */
3186#endif
3187 sst_end ();
3188
3189 r.r_extern = 0;
3190 r.r_length = 2;
3191 r.r_symbolnum = N_TEXT;
3192 r.r_pcrel = 0;
3193 r.r_pad = 0;
3194 buffer_mem (&sst_reloc, &r, sizeof (r));
3195 return ptr;
3196}
3197
3198
3199/* Start of a block. Write a `begin' subrecord to the SST. The block
3200 begins at ADDR in the text segment. Push the block onto the block
3201 stack. */
3202
3203static void block_begin (dword addr)
3204{
3205 size_t ptr;
3206
3207 ptr = hll_begin (addr);
3208 grow_by (&block_grow, 1);
3209 block_stack[block_grow.count].patch_ptr = ptr;
3210 block_stack[block_grow.count].addr = addr;
3211 ++block_grow.count;
3212}
3213
3214
3215/* Write an `end' subrecord to the SST. PATCH_PTR is the sst index of
3216 the `begin' subrecord for this block. Add ADD_START to the start
3217 address of the block and set the length of the block to LENGTH, in
3218 bytes. */
3219
3220static void hll_end (size_t patch_ptr, int add_start, size_t length)
3221{
3222 *(dword *)(sst.buf+patch_ptr+0) += add_start;
3223 *(dword *)(sst.buf+patch_ptr+4) = length;
3224 sst_start (SST_end);
3225 sst_end ();
3226}
3227
3228
3229/* Return TRUE iff symbol table entry S defines a function. */
3230
3231static int is_fun (const struct nlist *s)
3232{
3233 const char *p;
3234
3235 if (s->n_type != N_FUN)
3236 return FALSE;
3237 p = nextcolon (str_ptr + s->n_un.n_strx);
3238 if (p == NULL)
3239 return FALSE;
3240 return (p[1] == 'F' || p[1] == 'f');
3241}
3242
3243
3244/* Approximate a function prologue length by examining the code. The
3245 function starts at START and ends at END. */
3246
3247static int get_prologue_length (dword start, dword end)
3248{
3249 dword i;
3250
3251 if (end > text_size)
3252 end = text_size;
3253 i = start;
3254 if (i < end && text_ptr[i] == 0x55) ++i; /* PUSH EBP */
3255 if (i + 1 < end && text_ptr[i+0] == 0x89
3256 && text_ptr[i+1] == 0xe5) /* MOV EBP, ESP */
3257 i += 2;
3258 if (i + 2 < end && text_ptr[i+0] == 0x83
3259 && text_ptr[i+1] == 0xec) /* SUB ESP, n8 */
3260 i += 3;
3261 return i - start;
3262}
3263
3264
3265/* End of a function reached. Try to find the function length. I is
3266 the sym_ptr index of the current symbol -- we search the symbol
3267 table starting at I to find the next function for estimating the
3268 function length. */
3269
3270static void fun_end (int i)
3271{
3272 int p_len;
3273 dword addr;
3274
3275 addr = text_size;
3276 while (i < sym_count)
3277 {
3278 if (is_fun (&sym_ptr[i]))
3279 {
3280 addr = sym_ptr[i].n_value;
3281 break;
3282 }
3283 ++i;
3284 }
3285 if (prologue_length != -1)
3286 p_len = prologue_length;
3287 else
3288 p_len = get_prologue_length (proc_start_addr, addr);
3289 *(dword *)(sst.buf+proc_patch_base + 0) = addr - proc_start_addr;
3290 *(word *)(sst.buf+proc_patch_base + 4) = p_len;
3291 *(dword *)(sst.buf+proc_patch_base + 6) = addr - proc_start_addr;
3292 proc_patch_base = 0;
3293 sst_start (SST_end);
3294 sst_end ();
3295}
3296
3297
3298/* End of a block (N_RBRAC encountered). I is the sym_ptr index of
3299 the N_RBRAC symbol. If we reach the outmost block level, we call
3300 fun_end(). */
3301
3302static void block_end (int i)
3303{
3304 if (block_grow.count == 0)
3305 {
3306 /** @todo fix this - broken after my last change (bird 2003-10-06) */
3307 /* warning ("Invalid N_RBRAC without N_LBRAC"); */
3308 }
3309 else
3310 {
3311 --block_grow.count;
3312 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3313 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3314 if (block_grow.count == 1) /* we make one extra begin */
3315 {
3316 --block_grow.count;
3317 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3318 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3319 }
3320 if (block_grow.count == 0 && proc_patch_base != 0)
3321 fun_end (i);
3322 }
3323}
3324
3325
3326/* Define a function. SYMBOL points to the sym_ptr entry. The
3327 function arguments are taken from the args_list array. Several
3328 fields of the SST entry are filled-in later, when reaching the end
3329 of the function. proc_patch_base is used for remembering the place
3330 where to patch the SST entry. */
3331
3332static void define_fun (const struct nlist *symbol)
3333{
3334 struct type t, *t1, *t2, *t3;
3335 struct relocation_info r;
3336 const char *str, *p, *p2;
3337 const char *name, *mnglname;
3338#ifdef DEMANGLE_PROC_NAMES
3339 char *pszFree;
3340#endif
3341 size_t cchName, cchMnglName;
3342 int ti, ticlass;
3343
3344 str = str_ptr + symbol->n_un.n_strx;
3345 p = nextcolon (str);
3346 if (p == NULL)
3347 abort ();
3348 cchName = cchMnglName = p - str;
3349 name = mnglname = strpool_addn (str_pool, str, cchMnglName);
3350 proc_start_addr = symbol->n_value;
3351
3352#ifdef DEMANGLE_PROC_NAMES
3353 /* demangle the name */
3354 pszFree = cplus_demangle (mnglname, DMGL_ANSI | DMGL_PARAMS);
3355 if (!pszFree && (*mnglname == '_' || *mnglname == '*'))
3356 pszFree = cplus_demangle (mnglname + 1, DMGL_ANSI | DMGL_PARAMS);
3357 if (pszFree)
3358 {
3359 cchName = strlen (pszFree);
3360 name = pszFree;
3361 }
3362 else if (*mnglname == '*')
3363 {
3364 cchName--;
3365 name++;
3366 }
3367#endif
3368
3369 /* now let's see if there is a memfunc for this name. */
3370 for (t1 = type_head; t1; t1 = t1->next)
3371 if ( t1->tag == ty_memfunc
3372 && t1->d.memfunc.mnglname == mnglname)
3373 break;
3374
3375 if (t1)
3376 { /* C++ member function */
3377 t2 = t1->d.memfunc.type;
3378 if (t2 && t2->tag == ty_func && t2->d.func.domain)
3379 make_type (t2->d.func.domain, &ticlass);
3380 else
3381 { /* the hard way - search for the freaking string in suitable .stabs.
3382 * I think this is rather slow when done for many members.. :/ */
3383 int i;
3384 for (i = 0, t3 = NULL, ticlass = -1; !t3 && i < sym_count; ++i)
3385 switch (sym_ptr[i].n_type)
3386 {
3387 case N_LSYM:
3388 {
3389 str = str_ptr + sym_ptr[i].n_un.n_strx;
3390 p = nextcolon (str);
3391 if (p && p[1] == 'T' && p[2] == 't')
3392 {
3393 /* Now, there can be several name which NAME is a
3394 * substring in.
3395 */
3396 /** @todo: the string parsing still is a bit spooky. */
3397 for (p2 = p; (p2 = strstr (p2, mnglname)) != NULL; p2 += cchMnglName)
3398 if (p2[-1] == ':' && p2[cchMnglName] == ';')
3399 {
3400 int stab = atoi (&p[3]);
3401 t3 = stype_find (stab);
3402 break;
3403 }
3404 }
3405 break;
3406 }
3407 }
3408 if (t3)
3409 make_type (t3, &ticlass);
3410 if (ticlass < 0)
3411 {
3412 warning (" Can't figure out which class method '%s' is a member of!", mnglname);
3413 ticlass = 0;
3414 }
3415 }
3416
3417 make_type (t1, &ti);
3418 sst_start (SST_memfunc);
3419 r.r_address = sst.size;
3420 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3421 buffer_word (&sst, ti); /* Type index */
3422 proc_patch_base = sst.size;
3423 buffer_dword (&sst, 0); /* Length of proc */
3424 buffer_word (&sst, 0); /* Length of prologue */
3425 buffer_dword (&sst, 0); /* Length of prologue and body */
3426 buffer_word (&sst, ticlass); /* Class type (for member functions) */
3427 buffer_byte (&sst, 8); /* 32-bit near */
3428 buffer_enc (&sst, name); /* Proc name */
3429 sst_end ();
3430 }
3431 else
3432 {
3433 if (args_grow.count == 0)
3434 t1 = NULL;
3435 else
3436 {
3437 t.tag = ty_args;
3438 t.index = -1;
3439 t.d.args.count = args_grow.count;
3440 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
3441 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
3442 t1 = type_add (&t);
3443 free (t.d.args.list);
3444 }
3445 last_fun_type.d.func.args = t1;
3446 last_fun_type.d.func.arg_count = args_grow.count;
3447 t2 = type_add (&last_fun_type);
3448 make_type (t2, &ti);
3449
3450 sst_start (cplusplus_flag || cchName > 255 ? SST_CPPproc : SST_proc);
3451 r.r_address = sst.size;
3452 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3453 buffer_word (&sst, ti); /* Type index */
3454 proc_patch_base = sst.size;
3455 buffer_dword (&sst, 0); /* Length of proc */
3456 buffer_word (&sst, 0); /* Length of prologue */
3457 buffer_dword (&sst, 0); /* Length of prologue and body */
3458 buffer_word (&sst, 0); /* Class type (for member functions) */
3459 buffer_byte (&sst, 8); /* 32-bit near */
3460 if (cplusplus_flag || cchName > 255)
3461 buffer_enc (&sst, name); /* Proc name */
3462 else
3463 buffer_nstr (&sst, name); /* Proc name */
3464 sst_end ();
3465 }
3466 r.r_extern = 0;
3467 r.r_length = 2;
3468 r.r_symbolnum = N_TEXT;
3469 r.r_pcrel = 0;
3470 r.r_pad = 0;
3471 buffer_mem (&sst_reloc, &r, sizeof (r));
3472 set_hll_type (symbol - sym_ptr, ti);
3473
3474#ifdef DEMANGLE_PROC_NAMES
3475 /* free the demangled name. */
3476 free (pszFree);
3477#endif
3478}
3479
3480
3481/* Locate the next N_LBRAC starting at symbol I. If found, set
3482 lbrac_index to the index of N_LBRAC and start a new block. If
3483 there is no N_LBRAC, set lbrac_index to -1. */
3484
3485static void next_lbrac (int i)
3486{
3487 lbrac_index = -1;
3488 for (; i < sym_count; ++i)
3489 if (sym_ptr[i].n_type == N_RBRAC || is_fun (&sym_ptr[i]))
3490 return;
3491 else if (sym_ptr[i].n_type == N_LBRAC)
3492 {
3493 lbrac_index = i;
3494 block_begin (sym_ptr[i].n_value);
3495 return;
3496 }
3497}
3498
3499
3500/* Parse a stabs symbol. The sym_ptr index is passed in INDEX. As we
3501 might have to concatenate successive symbol table entries, *INDEX
3502 is updated to refer to the next symbol. WHERE is the segment where
3503 the symbol is defined (N_DATA, for instance). If WHERE is -1,
3504 there is no segment. MSG is used for warning messages. This
3505 function is called recursively for static functions (F) There's up
3506 to one level of recursion. The arguments of a function are read
3507 twice: If FLAG is TRUE, we turn the arguments into local symbols;
3508 if FLAG is FALSE, we put arguments into the args_list array. */
3509
3510static void parse_symbol (int *index, int where, const char *msg, int flag)
3511{
3512 char *name;
3513 const char *str, *p;
3514 const struct nlist *symbol;
3515 struct type *t1;
3516 int i, n, ti, alloc_flag;
3517
3518 symbol = &sym_ptr[*index];
3519 alloc_flag = concat_symbols (index, &str);
3520 parse_start = str;
3521 parse_pindex = index;
3522 p = nextcolon (str);
3523 if (p != NULL)
3524 {
3525 n = p - str;
3526 name = alloca (n + 1);
3527 memcpy (name, str, n);
3528 name[n] = 0;
3529#if defined (HLL_DEBUG)
3530 printf ("%s %s\n", msg, str);
3531#endif
3532
3533 parse_ptr = p + 1;
3534 switch (*parse_ptr)
3535 {
3536 case 'G':
3537 /* Static storage, global scope */
3538 {
3539 const struct nlist *sym2 = NULL;
3540 ++parse_ptr;
3541 ti = hll_type ();
3542#if defined (HLL_DEBUG)
3543 printf (" type=%#x\n", ti);
3544#endif
3545
3546 /* Check for a ugly hack in dbxout.c which allow us to resolve
3547 communal and global variables more accuratly. The n_value is -12357
3548 and the previous entry is 0xfe. What the previous entry contains
3549 is the assembly name of the symbol.
3550
3551 Another part of that hack is that n_value is contains the
3552 symbol value so we don't need to look up the symbol for that
3553 reason (but as it turns out we have to look it up to see where
3554 it is in most cases, bah). */
3555 if ( symbol->n_value == -12357
3556 && *index >= 1
3557 && symbol[-1].n_type == 0xfe)
3558 {
3559 sym2 = find_symbol_ex (symbol[-1].n_un.n_strx + str_ptr, *index - 1, 1);
3560 if (!sym2)
3561 {
3562 warning ("Cannot find address of communal/external variable %s", name);
3563 return;
3564 }
3565 }
3566 else if (where == -1)
3567 { /* This is not very nice, we have to look up the symbol
3568 like we used to do in order to the the segment right.
3569 Should probably just use the external/communal hack above
3570 for everything or change the n_type of the 'G' stab... However,
3571 this will work for most cases and it will be compatible with
3572 the unmodified dbxout.c code. */
3573 if (symbol->n_type == N_FUN) /* in case GCC change... */
3574 where = N_TEXT;
3575 else
3576 {
3577 size_t cch = strlen (name);
3578 char *psz = alloca (cch + 2);
3579 *psz = '_';
3580 memcpy (psz + 1, name, cch + 1);
3581 sym2 = find_symbol (psz);
3582 if (!sym2)
3583 sym2 = find_symbol (psz + 1);
3584 where = N_DATA;
3585 }
3586 }
3587 /* if we're lucky we've got a symbol... */
3588 if (sym2)
3589 {
3590 if (sym2->n_type == N_EXT)
3591 sst_static (name, 0, ti, sym2 - sym_ptr, 1, sym2);
3592 else
3593 sst_static (name, sym2->n_value, ti, sym2->n_type, 0, sym2);
3594 }
3595 else
3596 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3597 break;
3598 }
3599
3600 case 'S':
3601 case 'V':
3602
3603 /* S = static storage, file scope,
3604 V = static storage, local scope */
3605
3606 if (where == -1)
3607 {
3608 warning ("Cannot use symbol type %c with N_%s", *parse_ptr, msg);
3609 return;
3610 }
3611 ++parse_ptr;
3612 ti = hll_type ();
3613#if defined (HLL_DEBUG)
3614 printf (" type=%#x\n", ti);
3615#endif
3616 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3617 break;
3618
3619 case 'p':
3620
3621 /* Function argument. */
3622
3623 if (where != -1)
3624 {
3625 warning ("Cannot use argument symbol with N_%s", msg);
3626 return;
3627 }
3628 ++parse_ptr;
3629 if (flag)
3630 {
3631 ti = hll_type ();
3632#if defined (HLL_DEBUG)
3633 printf (" type=%#x\n", ti);
3634#endif
3635 sst_start (SST_auto);
3636 buffer_dword (&sst, symbol->n_value);/* Offset into stk frame */
3637 buffer_word (&sst, ti); /* Type index */
3638 buffer_nstr (&sst, name); /* Symbol name */
3639 sst_end ();
3640 }
3641 else
3642 {
3643 t1 = parse_complete_type (NULL);
3644 grow_by (&args_grow, 1);
3645 args_list[args_grow.count++] = t1;
3646 }
3647 break;
3648
3649 case 'r':
3650
3651 /* Register variable. */
3652
3653 if (where != -1)
3654 {
3655 warning ("Cannot use register symbol with N_%s", msg);
3656 return;
3657 }
3658 ++parse_ptr;
3659 ti = hll_type ();
3660 switch (symbol->n_value)
3661 {
3662 case 0: /* EAX */
3663 i = 0x10;
3664 break;
3665 case 1: /* ECX */
3666 i = 0x11;
3667 break;
3668 case 2: /* EDX */
3669 i = 0x12;
3670 break;
3671 case 3: /* EBX */
3672 i = 0x13;
3673 break;
3674 case 4: /* ESP */
3675 i = 0x14;
3676 break;
3677 case 5: /* EBP */
3678 i = 0x15;
3679 break;
3680 case 6: /* ESI */
3681 i = 0x16;
3682 break;
3683 case 7: /* EDI */
3684 i = 0x17;
3685 break;
3686 case 12: /* ST(0) */
3687 i = 0x80;
3688 break;
3689 case 13: /* ST(1) */
3690 i = 0x81;
3691 break;
3692 case 14: /* ST(2) */
3693 i = 0x82;
3694 break;
3695 case 15: /* ST(3) */
3696 i = 0x83;
3697 break;
3698 case 16: /* ST(4) */
3699 i = 0x84;
3700 break;
3701 case 17: /* ST(5) */
3702 i = 0x85;
3703 break;
3704 case 18: /* ST(6) */
3705 i = 0x86;
3706 break;
3707 case 19: /* ST(7) */
3708 i = 0x87;
3709 break;
3710 default:
3711 warning ("unknown register %lu", symbol->n_value);
3712 return;
3713 }
3714 sst_start (SST_reg); /* Register variable */
3715 buffer_word (&sst, ti); /* Type index */
3716 buffer_byte (&sst, i); /* Register number */
3717 buffer_nstr (&sst, name); /* Symbol name */
3718 sst_end ();
3719 break;
3720
3721 case '0': case '1': case '2': case '3': case '4':
3722 case '5': case '6': case '7': case '8': case '9':
3723
3724 /* Local symbol. */
3725
3726 if (where != -1)
3727 {
3728 warning ("Cannot use local symbol with N_%s", msg);
3729 return;
3730 }
3731 ti = hll_type ();
3732#if defined (HLL_DEBUG)
3733 printf (" type=%#x\n", ti);
3734#endif
3735 sst_start (SST_auto);
3736 buffer_dword (&sst, symbol->n_value); /* Offset into stk frame */
3737 buffer_word (&sst, ti); /* Type index */
3738 buffer_nstr (&sst, name); /* Symbol name */
3739 sst_end ();
3740 break;
3741
3742 case 'F':
3743 case 'f':
3744
3745 /* Function. */
3746
3747 ++parse_ptr;
3748 if (where != N_TEXT)
3749 {
3750 warning ("Cannot use function with N_%s", msg);
3751 return;
3752 }
3753 last_fun_type.tag = ty_func;
3754 last_fun_type.index = -1;
3755 last_fun_type.d.func.domain = NULL;
3756 last_fun_type.d.func.ret = parse_type (NULL);
3757
3758 /* Don't check for end of string -- parse_ptr points to a
3759 comma for nested functions! */
3760
3761 last_fun_type.d.func.args = NULL;
3762 last_fun_type.d.func.arg_count = 0;
3763 last_fun_valid = TRUE;
3764 args_grow.count = 0;
3765 last_fun_addr = symbol->n_value;
3766 prologue_length = -1;
3767
3768 for (i = *index + 1; i < sym_count && sym_ptr[i].n_type == N_PSYM; ++i)
3769 parse_symbol (&i, -1, "PSYM", FALSE);
3770
3771 define_fun (symbol);
3772
3773 /* Now look for N_LBRAC */
3774 next_lbrac (i);
3775 if (lbrac_index != -1)
3776 {
3777 if (prologue_length == -1)
3778 prologue_length = sym_ptr[lbrac_index].n_value - last_fun_addr;
3779 }
3780 else
3781 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3782 prologue_length = 0;
3783 block_begin (proc_start_addr);
3784 }
3785
3786 /* Parameters */
3787 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_PSYM)
3788 {
3789 ++(*index);
3790 parse_symbol (index, -1, "PSYM", TRUE);
3791 }
3792
3793 /* Start another block for the local variables. This is to work
3794 around some missing bracets in stabs and to fix problem with
3795 C++ bool type. */
3796 dword addr = proc_start_addr + prologue_length;
3797 if (lbrac_index >= 0 && sym_ptr[lbrac_index].n_value > addr)
3798 addr = sym_ptr[lbrac_index].n_value;
3799 block_begin (addr);
3800
3801 /* Local variables (fix for boolparam testcase too). */
3802 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_LSYM)
3803 {
3804 ++(*index);
3805 parse_symbol (index, -1, "LSYM", TRUE);
3806 }
3807
3808 /* If there's no N_LBRAC, write SST_end now */
3809 if (lbrac_index == -1)
3810 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3811 while (block_grow.count > 0)
3812 block_end (*index+1);
3813 }
3814 break;
3815 }
3816 }
3817 if (alloc_flag)
3818 free ((void *)str); /* Remove the const attribute */
3819}
3820
3821
3822/* Create a type tag for HLL type index INDEX. TYPE is either SST_tag
3823 (for structures and unions) or SST_tag2 (for classes). NAME is
3824 the name of the structure, union or class. */
3825
3826static void type_tag (int type, int index, const char *name)
3827{
3828 if (name != NULL)
3829 {
3830 size_t cch;
3831 if (type == SST_tag2 || (cch = strlen(name)) > 250)
3832 {
3833 sst_start (SST_tag2);
3834 buffer_word (&sst, index);
3835 buffer_enc (&sst, name);
3836 }
3837 else
3838 {
3839 char achName[256];
3840 memcpy (achName, name, cch);
3841 achName[cch] = '\0';
3842
3843 sst_start (type);
3844 buffer_word (&sst, index);
3845 buffer_nstr (&sst, achName);
3846 }
3847 sst_end ();
3848 }
3849}
3850
3851
3852/* Write a CuInfo SST entry to tell the debugger what compiler (C or
3853 C++) has been used. */
3854
3855static void write_cuinfo (void)
3856{
3857 struct timestamp ts;
3858 time_t now;
3859 struct tm *tp;
3860 byte lang;
3861
3862 if (cplusplus_flag)
3863 lang = 0x02; /* C++ */
3864 else
3865 lang = 0x01; /* C */
3866
3867 time (&now);
3868 tp = localtime (&now);
3869 ts.year = tp->tm_year + 1900;
3870 ts.month = tp->tm_mon + 1;
3871 ts.day = tp->tm_mday;
3872 ts.hours = tp->tm_hour;
3873 ts.minutes = tp->tm_min;
3874 ts.seconds = tp->tm_sec;
3875 ts.hundredths = 0;
3876 sst_start (SST_cuinfo);
3877 buffer_byte (&sst, lang);
3878 buffer_nstr (&sst, " "); /* Compiler options */
3879 buffer_nstr (&sst, __DATE__); /* Compiler date */
3880 buffer_mem (&sst, &ts, sizeof (ts));
3881 sst_end ();
3882}
3883
3884
3885/* Write a ChangSeg SST entry to define the text segment. */
3886
3887static void write_changseg (void)
3888{
3889 struct relocation_info r;
3890
3891 sst_start (SST_changseg);
3892 r.r_address = sst.size;
3893 buffer_word (&sst, 0); /* Segment number */
3894 buffer_word (&sst, 0); /* Reserved */
3895 sst_end ();
3896 r.r_extern = 0;
3897 r.r_length = 1;
3898 r.r_symbolnum = N_TEXT;
3899 r.r_pcrel = 0;
3900 r.r_pad = 0;
3901 buffer_mem (&sst_reloc, &r, sizeof (r));
3902}
3903
3904
3905/* Convert debug information. The data is taken from the symbol and
3906 string tables of the current a.out module. Output is put into the
3907 tt, sst etc. buffers. */
3908
3909void convert_debug (void)
3910{
3911 int i;
3912 struct type *t1, *t2;
3913#if defined (HLL_DEBUG)
3914 setvbuf(stdout, NULL, _IONBF, 256);
3915#endif
3916
3917 str_pool = strpool_init ();
3918 grow_init (&stype_grow, &stype_list, sizeof (*stype_list), 32);
3919 type_head = NULL; hll_type_index = 512;
3920 grow_init (&block_grow, &block_stack, sizeof (*block_stack), 16);
3921 grow_init (&args_grow, &args_list, sizeof (*args_list), 16);
3922 last_fun_valid = FALSE; prologue_length = -1;
3923 unnamed_struct_number = 1;
3924
3925 /* Check whether converting a translated C++ program. */
3926#if 1 /* kso #465 2003-06-04: pretend everything is C++, that symbols is no longer present. */
3927 cplusplus_flag = 1;
3928#else
3929 cplusplus_flag = (find_symbol ("__gnu_compiled_cplusplus") != NULL);
3930#endif
3931
3932
3933 /* Parse the typedefs to avoid forward references. */
3934
3935 for (i = 0; i < sym_count; ++i)
3936 switch (sym_ptr[i].n_type)
3937 {
3938 case N_LSYM:
3939 case N_LCSYM:
3940 case N_GSYM:
3941 case N_PSYM:
3942 case N_RSYM:
3943 case N_STSYM:
3944 case N_FUN:
3945 parse_typedef (&i);
3946 break;
3947 }
3948
3949 /* Provide information about the compiler. */
3950
3951 write_cuinfo ();
3952
3953 /* Change the default segment. */
3954
3955 write_changseg ();
3956
3957 /* Parse the other symbol table entries. */
3958
3959 lbrac_index = -1; block_grow.count = 0; proc_patch_base = 0;
3960 for (i = 0; i < sym_count; ++i)
3961 switch (sym_ptr[i].n_type)
3962 {
3963 case N_LSYM:
3964 parse_symbol (&i, -1, "LSYM", TRUE);
3965 break;
3966
3967 case N_GSYM:
3968 parse_symbol (&i, -1, "GSYM", TRUE);
3969 break;
3970
3971 case N_LCSYM:
3972 parse_symbol (&i, N_BSS, "LCSYM", TRUE);
3973 break;
3974
3975 case N_STSYM:
3976 parse_symbol (&i, N_DATA, "STSYM", TRUE);
3977 break;
3978
3979 case N_RSYM:
3980 parse_symbol (&i, -1, "RSYM", TRUE);
3981 break;
3982
3983 case N_LBRAC:
3984 if (lbrac_index == -1) /* N_LBRAC without local symbols */
3985 block_begin (sym_ptr[i].n_value);
3986 else if (i != lbrac_index)
3987 warning ("Invalid N_LBRAC");
3988 next_lbrac (i + 1);
3989 break;
3990
3991 case N_RBRAC:
3992 block_end (i);
3993 next_lbrac (i + 1);
3994 break;
3995
3996 case N_FUN:
3997 parse_symbol (&i, N_TEXT, "FUN", TRUE);
3998 break;
3999 }
4000
4001#ifdef HASH_DEBUG
4002fprintf(stderr, "\n");
4003for (i = 0; i < TYPE_HASH_MAX; i++)
4004 if (i % 7 == 6)
4005 fprintf(stderr, "%3d: %4d\n", i, ctype_tag[i]);
4006 else
4007 fprintf(stderr, "%3d: %4d ", i, ctype_tag[i]);
4008fprintf(stderr, "\nctypes=%d\n", ctypes);
4009for (i = 0; i < TYPE_HASH_MAX; i++)
4010 if (ctype_tag[i] > 1000)
4011 {
4012 int j;
4013 unsigned au[ty_max];
4014 memset(&au[0], 0, sizeof(au));
4015 for (t1 = atype_tag[i]; t1 != NULL; t1 = t1->nexthash)
4016 au[t1->tag]++;
4017
4018 fprintf(stderr, "\nIndex %d is overpopulated (%d):\n", i, ctype_tag[i]);
4019 for (j = 0; j < 18; j++)
4020 if (j % 7 == 6)
4021 fprintf(stderr, "%2d: %4d\n", j, au[j]);
4022 else
4023 fprintf(stderr, "%2d: %4d ", j, au[j]);
4024 }
4025#endif
4026
4027 /* Create tags for structures, unions and classes. */
4028
4029 for (t1 = type_head; t1 != NULL; t1 = t1->next)
4030 if (t1->index != -1)
4031 switch (t1->tag)
4032 {
4033 case ty_struc:
4034 type_tag (SST_tag, t1->index, t1->d.struc.name);
4035 break;
4036 case ty_class:
4037 type_tag (SST_tag2, t1->index, t1->d.class.name);
4038 break;
4039 case ty_enu:
4040 type_tag (SST_tag, t1->index, t1->d.enu.name);
4041 break;
4042 default:
4043 break;
4044 }
4045
4046#ifdef HLL_DEBUG
4047 printf ("Stabs to HLL type mappings: %d/%d\n", stype_grow.count, stype_grow.alloc);
4048 for (i = 0; i < stype_grow.alloc; i++)
4049 if (stype_list[i])
4050 {
4051 printf (" %3d => 0x%03x/%-3d ", i + 1, stype_list[i]->index, stype_list[i]->index);
4052 show_type (stype_list[i]);
4053 printf("\n");
4054 }
4055#endif
4056
4057
4058 /* Deallocate memory. */
4059
4060 grow_free (&stype_grow);
4061 grow_free (&args_grow);
4062 grow_free (&block_grow);
4063 for (t1 = type_head; t1 != NULL; t1 = t2)
4064 {
4065 t2 = t1->next;
4066 switch (t1->tag)
4067 {
4068 case ty_args:
4069 if (t1->d.args.list != NULL)
4070 free (t1->d.args.list);
4071 break;
4072 case ty_types:
4073 if (t1->d.types.list != NULL)
4074 free (t1->d.types.list);
4075 break;
4076 case ty_fields:
4077 if (t1->d.fields.list != NULL)
4078 free (t1->d.fields.list);
4079 break;
4080 case ty_values:
4081 if (t1->d.values.list != NULL)
4082 free (t1->d.values.list);
4083 break;
4084 default:
4085 break;
4086 }
4087 free (t1);
4088 }
4089 type_head = NULL;
4090 memset (&atype_tag, 0, sizeof(atype_tag));
4091 strpool_free (str_pool);
4092 str_pool = NULL;
4093}
Note: See TracBrowser for help on using the repository browser.