2 * File types.c - datatype handling stuff for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 * This really doesn't do much at the moment, but it forms the framework
7 * upon which full support for datatype handling will eventually be hung.
29 #define NR_TYPE_HASH 521
33 struct en_values
* next
;
42 struct datatype
* type
;
50 struct datatype
* next
;
63 unsigned short bitoff
;
65 struct datatype
* basetype
;
70 struct datatype
* pointsto
;
74 struct datatype
* rettype
;
80 struct datatype
* basictype
;
85 struct member
* members
;
89 struct en_values
* members
;
99 #define BASIC_LONGLONG 6
100 #define BASIC_ULONGLONGI 7
101 #define BASIC_SHORT 8
102 #define BASIC_SHORTUI 9
103 #define BASIC_SCHAR 10
104 #define BASIC_UCHAR 11
106 #define BASIC_LONG_DOUBLE 13
107 #define BASIC_DOUBLE 14
108 #define BASIC_CMPLX_INT 15
109 #define BASIC_CMPLX_FLT 16
110 #define BASIC_CMPLX_DBL 17
111 #define BASIC_CMPLX_LONG_DBL 18
112 #define BASIC_VOID 19
114 struct datatype
* DEBUG_TypeInt
= NULL
;
115 struct datatype
* DEBUG_TypeIntConst
= NULL
;
116 struct datatype
* DEBUG_TypeUSInt
= NULL
;
117 struct datatype
* DEBUG_TypeString
= NULL
;
120 * All of the types that have been defined so far.
122 static struct datatype
* type_hash_table
[NR_TYPE_HASH
+ 1];
123 static struct datatype
* pointer_types
= NULL
;
125 static unsigned int type_hash( const char * name
)
127 unsigned int hash
= 0;
135 hash
= (hash
<< 4) + *p
++;
137 if( (tmp
= (hash
& 0xf0000000)) )
143 return hash
% NR_TYPE_HASH
;
147 static struct datatype
*
148 DEBUG_InitBasic(int type
, char * name
, int size
, int b_signed
,
149 char * output_format
)
153 struct datatype
* dt
;
154 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
160 hash
= type_hash(name
);
169 dt
->next
= type_hash_table
[hash
];
170 type_hash_table
[hash
] = dt
;
171 dt
->un
.basic
.basic_type
= type
;
172 dt
->un
.basic
.basic_size
= size
;
173 dt
->un
.basic
.b_signed
= b_signed
;
174 dt
->un
.basic
.output_format
= output_format
;
181 DEBUG_NewDataType(enum debug_type xtype
, const char * typename
)
183 struct datatype
* dt
= NULL
;
187 * The last bucket is special, and is used to hold typeless names.
189 if( typename
== NULL
)
195 hash
= type_hash(typename
);
198 if( typename
!= NULL
)
200 for( dt
= type_hash_table
[hash
]; dt
; dt
= dt
->next
)
202 if( xtype
!= dt
->type
|| dt
->name
== NULL
203 || dt
->name
[0] != typename
[0])
208 if( strcmp(dt
->name
, typename
) == 0 )
217 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
221 memset(dt
, 0, sizeof(*dt
));
224 if( typename
!= NULL
)
226 dt
->name
= xstrdup(typename
);
232 if( xtype
== POINTER
)
234 dt
->next
= pointer_types
;
239 dt
->next
= type_hash_table
[hash
];
240 type_hash_table
[hash
] = dt
;
249 DEBUG_FindOrMakePointerType(struct datatype
* reftype
)
251 struct datatype
* dt
= NULL
;
253 if( reftype
!= NULL
)
255 for( dt
= pointer_types
; dt
; dt
= dt
->next
)
257 if( dt
->type
!= POINTER
)
262 if( dt
->un
.pointer
.pointsto
== reftype
)
271 dt
= (struct datatype
*) xmalloc(sizeof(struct datatype
));
276 dt
->un
.pointer
.pointsto
= reftype
;
277 dt
->next
= pointer_types
;
288 static int beenhere
= 0;
289 struct datatype
* chartype
;
291 if( beenhere
++ != 0 )
297 * Special version of int used with constants of various kinds.
299 DEBUG_TypeIntConst
= DEBUG_InitBasic(BASIC_INT
,NULL
,4,1,"%d");
302 * Initialize a few builtin types.
306 DEBUG_TypeInt
= DEBUG_InitBasic(BASIC_INT
,"int",4,1,"%d");
307 chartype
= DEBUG_InitBasic(BASIC_CHAR
,"char",1,1,"'%c'");
308 DEBUG_InitBasic(BASIC_LONG
,"long int",4,1,"%d");
309 DEBUG_TypeUSInt
= DEBUG_InitBasic(BASIC_UINT
,"unsigned int",4,0,"%d");
310 DEBUG_InitBasic(BASIC_LUI
,"long unsigned int",4,0,"%d");
311 DEBUG_InitBasic(BASIC_LONGLONG
,"long long int",8,1,"%ld");
312 DEBUG_InitBasic(BASIC_ULONGLONGI
,"long long unsigned int",8,0,"%ld");
313 DEBUG_InitBasic(BASIC_SHORT
,"short int",2,1,"%d");
314 DEBUG_InitBasic(BASIC_SHORTUI
,"short unsigned int",2,0,"%d");
315 DEBUG_InitBasic(BASIC_SCHAR
,"signed char",1,1,"'%c'");
316 DEBUG_InitBasic(BASIC_UCHAR
,"unsigned char",1,0,"'%c'");
317 DEBUG_InitBasic(BASIC_FLT
,"float",4,0,"%f");
318 DEBUG_InitBasic(BASIC_LONG_DOUBLE
,"double",8,0,"%lf");
319 DEBUG_InitBasic(BASIC_DOUBLE
,"long double",12,0,NULL
);
320 DEBUG_InitBasic(BASIC_CMPLX_INT
,"complex int",8,1,NULL
);
321 DEBUG_InitBasic(BASIC_CMPLX_FLT
,"complex float",8,0,NULL
);
322 DEBUG_InitBasic(BASIC_CMPLX_DBL
,"complex double",16,0,NULL
);
323 DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL
,"complex long double",24,0,NULL
);
324 DEBUG_InitBasic(BASIC_VOID
,"void",0,0,NULL
);
326 DEBUG_TypeString
= DEBUG_NewDataType(POINTER
, NULL
);
327 DEBUG_SetPointerType(DEBUG_TypeString
, chartype
);
330 * Now initialize the builtins for codeview.
332 DEBUG_InitCVDataTypes();
337 DEBUG_GetExprValue(DBG_ADDR
* addr
, char ** format
)
340 struct datatype
* type2
= NULL
;
341 struct en_values
* e
;
342 char * def_format
= "0x%x";
345 assert(addr
->type
!= NULL
);
347 switch(addr
->type
->type
)
350 memcpy(&rtn
, (char *) addr
->off
, addr
->type
->un
.basic
.basic_size
);
351 if( (addr
->type
->un
.basic
.b_signed
)
352 && ((addr
->type
->un
.basic
.basic_size
& 3) != 0)
353 && ((rtn
>> (addr
->type
->un
.basic
.basic_size
* 8 - 1)) != 0) )
355 rtn
= rtn
| ((-1) << (addr
->type
->un
.basic
.basic_size
* 8));
357 if( addr
->type
->un
.basic
.output_format
!= NULL
)
359 def_format
= addr
->type
->un
.basic
.output_format
;
363 * Check for single character prints that are out of range.
365 if( addr
->type
->un
.basic
.basic_size
== 1
366 && strcmp(def_format
, "'%c'") == 0
367 && ((rtn
< 0x20) || (rtn
> 0x80)) )
373 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
374 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
375 type2
= addr
->type
->un
.pointer
.pointsto
;
376 if( type2
->type
== BASIC
&& type2
->un
.basic
.basic_size
== 1 )
378 def_format
= "\"%s\"";
383 def_format
= "0x%8.8x";
388 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
389 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
390 def_format
= "0x%8.8x";
393 rtn
= (unsigned int) *((unsigned char **)addr
->off
);
394 for(e
= addr
->type
->un
.enumeration
.members
; e
; e
= e
->next
)
396 if( e
->value
== rtn
)
419 *format
= def_format
;
425 DEBUG_TypeDerefPointer(DBG_ADDR
* addr
, struct datatype
** newtype
)
428 * Make sure that this really makes sense.
430 if( addr
->type
->type
!= POINTER
)
436 *newtype
= addr
->type
->un
.pointer
.pointsto
;
437 return *(unsigned int*) (addr
->off
);
441 DEBUG_FindStructElement(DBG_ADDR
* addr
, const char * ele_name
, int * tmpbuf
)
447 * Make sure that this really makes sense.
449 if( addr
->type
->type
!= STRUCT
)
455 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
457 if( strcmp(m
->name
, ele_name
) == 0 )
459 addr
->type
= m
->type
;
460 if( (m
->offset
& 7) != 0 || (m
->size
& 7) != 0)
463 * Bitfield operation. We have to extract the field and store
464 * it in a temporary buffer so that we get it all right.
466 *tmpbuf
= ((*(int* ) (addr
->off
+ (m
->offset
>> 3))) >> (m
->offset
& 7));
467 addr
->off
= (int) tmpbuf
;
469 mask
= 0xffffffff << (m
->size
);
472 * OK, now we have the correct part of the number.
473 * Check to see whether the basic type is signed or not, and if so,
474 * we need to sign extend the number.
476 if( m
->type
->type
== BASIC
&& m
->type
->un
.basic
.b_signed
!= 0
477 && (*tmpbuf
& (1 << (m
->size
- 1))) != 0 )
484 addr
->off
+= (m
->offset
>> 3);
495 DEBUG_SetStructSize(struct datatype
* dt
, int size
)
497 assert(dt
->type
== STRUCT
);
498 dt
->un
.structure
.size
= size
;
499 dt
->un
.structure
.members
= NULL
;
505 DEBUG_CopyFieldlist(struct datatype
* dt
, struct datatype
* dt2
)
508 assert( dt
->type
== dt2
->type
&& ((dt
->type
== STRUCT
) || (dt
->type
== ENUM
)));
510 if( dt
->type
== STRUCT
)
512 dt
->un
.structure
.members
= dt2
->un
.structure
.members
;
516 dt
->un
.enumeration
.members
= dt2
->un
.enumeration
.members
;
523 DEBUG_AddStructElement(struct datatype
* dt
, char * name
, struct datatype
* type
,
524 int offset
, int size
)
527 struct member
* last
;
528 struct en_values
* e
;
530 if( dt
->type
== STRUCT
)
532 for(last
= dt
->un
.structure
.members
; last
; last
= last
->next
)
534 if( (last
->name
[0] == name
[0])
535 && (strcmp(last
->name
, name
) == 0) )
539 if( last
->next
== NULL
)
544 m
= (struct member
*) xmalloc(sizeof(struct member
));
550 m
->name
= xstrdup(name
);
556 m
->next
= dt
->un
.structure
.members
;
557 dt
->un
.structure
.members
= m
;
565 * If the base type is bitfield, then adjust the offsets here so that we
566 * are able to look things up without lots of falter-all.
568 if( type
->type
== BITFIELD
)
570 m
->offset
+= m
->type
->un
.bitfield
.bitoff
;
571 m
->size
= m
->type
->un
.bitfield
.nbits
;
572 m
->type
= m
->type
->un
.bitfield
.basetype
;
575 else if( dt
->type
== ENUM
)
577 e
= (struct en_values
*) xmalloc(sizeof(struct en_values
));
583 e
->name
= xstrdup(name
);
585 e
->next
= dt
->un
.enumeration
.members
;
586 dt
->un
.enumeration
.members
= e
;
596 DEBUG_GetPointerType(struct datatype
* dt
)
598 if( dt
->type
== POINTER
)
600 return dt
->un
.pointer
.pointsto
;
607 DEBUG_SetPointerType(struct datatype
* dt
, struct datatype
* dt2
)
612 dt
->un
.pointer
.pointsto
= dt2
;
615 dt
->un
.funct
.rettype
= dt2
;
625 DEBUG_SetArrayParams(struct datatype
* dt
, int min
, int max
, struct datatype
* dt2
)
627 assert(dt
->type
== ARRAY
);
628 dt
->un
.array
.start
= min
;
629 dt
->un
.array
.end
= max
;
630 dt
->un
.array
.basictype
= dt2
;
636 DEBUG_SetBitfieldParams(struct datatype
* dt
, int offset
, int nbits
,
637 struct datatype
* dt2
)
639 assert(dt
->type
== BITFIELD
);
640 dt
->un
.bitfield
.bitoff
= offset
;
641 dt
->un
.bitfield
.nbits
= nbits
;
642 dt
->un
.bitfield
.basetype
= dt2
;
647 int DEBUG_GetObjectSize(struct datatype
* dt
)
657 return dt
->un
.basic
.basic_size
;
659 return sizeof(int *);
661 return dt
->un
.structure
.size
;
665 return (dt
->un
.array
.end
- dt
->un
.array
.start
)
666 * DEBUG_GetObjectSize(dt
->un
.array
.basictype
);
669 * Bitfields have to be handled seperately later on
670 * when we insert the element into the structure.
682 DEBUG_ArrayIndex(DBG_ADDR
* addr
, DBG_ADDR
* result
, int index
)
687 * Make sure that this really makes sense.
689 if( addr
->type
->type
== POINTER
)
692 * Get the base type, so we know how much to index by.
694 size
= DEBUG_GetObjectSize(addr
->type
->un
.pointer
.pointsto
);
695 result
->type
= addr
->type
->un
.pointer
.pointsto
;
696 result
->off
= (*(unsigned int*) (addr
->off
)) + size
* index
;
698 else if (addr
->type
->type
== ARRAY
)
700 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
701 result
->type
= addr
->type
->un
.array
.basictype
;
702 result
->off
= addr
->off
+ size
* (index
- addr
->type
->un
.array
.start
);
708 /***********************************************************************
711 * Implementation of the 'print' command.
713 void DEBUG_Print( const DBG_ADDR
*addr
, int count
, char format
, int level
)
724 fprintf( stderr
, "Count other than 1 is meaningless in 'print' command\n" );
728 if( addr
->type
== NULL
)
730 fprintf(stderr
, "Unable to evaluate expression\n");
734 if( format
== 'i' || format
== 's' || format
== 'w' || format
== 'b' )
736 fprintf( stderr
, "Format specifier '%c' is meaningless in 'print' command\n", format
);
740 switch(addr
->type
->type
)
746 DEBUG_PrintBasic(addr
, 1, format
);
749 fprintf(stderr
, "{");
750 for(m
= addr
->type
->un
.structure
.members
; m
; m
= m
->next
)
753 DEBUG_FindStructElement(&addr1
, m
->name
,
755 fprintf(stderr
, "%s=", m
->name
);
756 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
757 if( m
->next
!= NULL
)
759 fprintf(stderr
, ", ");
762 fprintf(stderr
, "}");
766 * Loop over all of the entries, printing stuff as we go.
768 size
= DEBUG_GetObjectSize(addr
->type
->un
.array
.basictype
);
772 * Special handling for character arrays.
774 pnt
= (char *) addr
->off
;
775 fprintf(stderr
, "\"");
776 for( i
=addr
->type
->un
.array
.start
; i
< addr
->type
->un
.array
.end
; i
++ )
778 fputc(*pnt
++, stderr
);
780 fprintf(stderr
, "\"");
784 addr1
.type
= addr
->type
->un
.array
.basictype
;
785 fprintf(stderr
, "{");
786 for( i
=addr
->type
->un
.array
.start
; i
<= addr
->type
->un
.array
.end
; i
++ )
788 DEBUG_Print(&addr1
, 1, format
, level
+ 1);
790 if( i
== addr
->type
->un
.array
.end
)
792 fprintf(stderr
, "}");
796 fprintf(stderr
, ", ");
807 fprintf(stderr
, "\n");