Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    Copyright (c) 1993 Open Software Foundation, Inc.
3
 
4
 
5
    All Rights Reserved
6
 
7
 
8
    Permission to use, copy, modify, and distribute this software
9
    and its documentation for any purpose and without fee is hereby
10
    granted, provided that the above copyright notice appears in all
11
    copies and that both the copyright notice and this permission
12
    notice appear in supporting documentation.
13
 
14
 
15
    OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
16
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17
    PARTICULAR PURPOSE.
18
 
19
 
20
    IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
21
    CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
22
    LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
23
    NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
*/
26
 
27
/*
28
    		 Crown Copyright (c) 1997
29
 
30
    This TenDRA(r) Computer Program is subject to Copyright
31
    owned by the United Kingdom Secretary of State for Defence
32
    acting through the Defence Evaluation and Research Agency
33
    (DERA).  It is made available to Recipients with a
34
    royalty-free licence for its use, reproduction, transfer
35
    to other parties and amendment for any purpose not excluding
36
    product development provided that any such use et cetera
37
    shall be deemed to be acceptance of the following conditions:-
38
 
39
        (1) Its Recipients shall ensure that this Notice is
40
        reproduced upon any copies or amended versions of it;
41
 
42
        (2) Any amended version of it shall be clearly marked to
43
        show both the nature of and the organisation responsible
44
        for the relevant amendment or amendments;
45
 
46
        (3) Its onward transfer from a recipient to another
47
        party shall be deemed to be that party's acceptance of
48
        these conditions;
49
 
50
        (4) DERA gives no warranty or assurance as to its
51
        quality or suitability for any purpose and DERA accepts
52
        no liability whatsoever in relation to any use to which
53
        it may be put.
54
*/
55
 
56
 
57
 
58
/**********************************************************************
59
$Author: release $
60
$Date: 1998/02/04 15:48:42 $
61
$Revision: 1.3 $
62
$Log: diagout.c,v $
63
 * Revision 1.3  1998/02/04  15:48:42  release
64
 * Added OSF copyright message.
65
 *
66
 * Revision 1.2  1998/02/04  10:43:52  release
67
 * Changes during testing.
68
 *
69
 * Revision 1.1.1.1  1998/01/17  15:55:56  release
70
 * First version to be checked into rolling release.
71
 *
72
 * Revision 1.2  1996/10/04  16:00:10  pwe
73
 * add banners and mod for PWE ownership
74
 *
75
**********************************************************************/
76
 
77
 
78
#include "config.h"
79
#include "cross_config.h"
80
#include <time.h>
81
 
82
#ifdef CROSS_INCLUDE
83
#include CROSS_INCLUDE/dbxstclass.h>
84
#include CROSS_INCLUDE/sys/debug.h>
85
#else
86
#include <dbxstclass.h>
87
#include <sys/debug.h>
88
#endif
89
 
90
#include "common_types.h"
91
#include "memtdf.h"
92
#include "installglob.h"
93
#include "li_types.h"
94
#include "diagtypes.h"
95
#include "toktypes.h"
96
#include "diag_fns.h"
97
#include "diagglob.h"
98
#include "mark_scope.h"
99
#include "externs.h"		/* for f_floating() */
100
#include "translat.h"
101
#include "machine.h"
102
#include "szs_als.h"
103
#include "install_fns.h"
104
#include "xalloc.h"
105
 
106
#include "maxminmacs.h"
107
#include "proc.h"
108
 
109
#include "comment.h"
110
#include "myassert.h"
111
#include "stack.h"
112
#include "diagout.h"
113
 
114
 
115
 
116
/* configuration options */
117
 
118
/* maximum .bb/.eb nesting depth in output, negative value implies no limit */
119
#ifndef MAX_BLOCK_DEPTH
120
#define MAX_BLOCK_DEPTH		1	/* more than one seems to upset dbx */
121
#endif
122
 
123
 
124
 
125
 
126
/* special line number codes */
127
#define UNKNOWN			(-9999)
128
#define NOT_IN_PROC		(-9998)
129
 
130
 
131
/* TypeId INTEGER number for standard TDF shapes */
132
#define TYPEID_SCHAR		1
133
#define TYPEID_UCHAR		2
134
#define TYPEID_SWORD		3
135
#define TYPEID_UWORD		4
136
#define TYPEID_SLONG		5
137
#define TYPEID_ULONG		6
138
#define TYPEID_SHREAL		7
139
#define TYPEID_REAL		8
140
#define TYPEID_DOUBLE		9
141
#define TYPEID_VOID		10
142
#define TYPEID_TOKEN		11
143
 
144
 
145
 
146
/* tdfstring -> nul terminated C string (char *) */
147
#define CSTRING(tdfstring)	((tdfstring).ints.chars)
148
 
149
/* tdf exp -> C int */
150
#define EXPINT(exp)		(ASSERT(name(exp) == val_tag) , no(exp))
151
 
152
/* tdf nat -> C int */
153
#define NATINT(n)		((n).nat_val.small_nat)
154
 
155
 
156
 
157
/*
158
 * Query and set "been_outed" field, which may hold
159
 * stabstring TypeNo if type has already been output.
160
 */
161
 
162
#define	CLR_OUTED_NO(dt)	((dt)->been_outed = (OUTPUT_REC)0)
163
#define	SET_OUTED_NO(dt,i)	((dt)->been_outed = (OUTPUT_REC)(i))
164
#define	OUTED_NO(dt)		((int)(dt)->been_outed)
165
#define	IS_OUTED(dt)		((dt)->been_outed != 0)
166
 
167
 
168
/*
169
 * Params for stab_relativeline().
170
 */
171
 
172
static CONST char line_stab[] = ".line";	/* line number */
173
static CONST char bf_stab[] = ".bf";	/* begin function */
174
static CONST char ef_stab[] = ".ef";	/* end function */
175
static CONST char bb_stab[] = ".bb";	/* begin block */
176
static CONST char eb_stab[] = ".eb";	/* end block */
177
 
178
 
179
/*
180
 * Location in the source program
181
 */
182
 
183
static int current_fileno;	/* file number of last file output */
184
static int current_lineno;	/* the last line number output */
185
static int files_stabbed;	/* number of .file or .bi output */
186
static int first_fileno;
187
 
188
static int current_procstart_lineno;	/* line proc started on */
189
 
190
static int block_depth;		/* .bb/.eb nesting depth */
191
 
192
static filename *fds;		/* known files numbered from 0 */
193
static int mainfile_fd = -1;	/* index into fds of 'main source file' */
194
static int szfds = 0;		/* space malloc'd for fds */
195
static int nofds = 0;		/* how many are known */
196
 
197
 
198
/*
199
 * Struct/union type information.
200
 * (See function OUTPUT_DIAG_TAGS)
201
 */
202
 
203
static diag_tagdef **su_diags = (diag_tagdef **) 0;
204
static int no_of_sus = 0;
205
static int leng_sus = 0;
206
 
207
/*
208
 * Typedef information
209
 * (See function OUTPUT_GLOBALS_TAB)
210
 */
211
 
212
static diag_descriptor **typedef_diags = (diag_descriptor ** ) 0;
213
static int no_of_typedefs = 0;
214
static int leng_typedefs = 0;
215
 
216
/*
217
 * Shapes we need, set in init_diag.
218
 */
219
 
220
static shape voidsh;		/* shape for void */
221
static shape tokensh;		/* shape for TDF tokenised type */
222
 
223
 
224
/*
225
 * Static procs.
226
 */
227
 
228
static diag_descriptor *find_dd PROTO_S ((exp));
229
static int find_file PROTO_S ((char *));
230
static void stab_relativeline PROTO_S ((CONST char *));
231
static void stab_begin_block PROTO_S ((void));
232
static void stab_end_block PROTO_S ((void));
233
static void stabn PROTO_S ((int, int));
234
static void stab_file PROTO_S ((int));
235
static void diagbr_open PROTO_S ((int));
236
static void diagbr_close PROTO_S ((int));
237
static int next_typen PROTO_S ((void));
238
static int TypeNo_of_shape PROTO_S ((shape));
239
static bool eq_sutype PROTO_S ((diag_type, diag_type));
240
static bool eq_typedef_type PROTO_S ((diag_descriptor *,diag_descriptor *));
241
static int size_dt PROTO_S ((diag_type));
242
static void out_dt_TypeDef_no_recurse PROTO_S ((diag_type));
243
static void out_dt_TypeDef PROTO_S ((diag_type));
244
static void out_dt_NewTypeId PROTO_S ((diag_type, int));
245
static void out_dt_TypeId PROTO_S ((diag_type));
246
static void stab_internal_types PROTO_S ((diag_type, bool));
247
static void stab_types PROTO_S ((void));
248
 
249
static void number_and_stab_basicshapes PROTO_S ((void));
250
static void number_structs_and_unions PROTO_S ((void));
251
static void stab_structs_and_unions PROTO_S ((void));
252
static void number_typedefs PROTO_S ((void));
253
static void stab_typedefs PROTO_S ((void));
254
static void stab_basicshape PROTO_S ((shape,char * ,int,int));
255
 
256
 
257
/*(See diag_config.h)
258
 * remember a filename so that find_file() can map onto filename number
259
 */
260
void INSPECT_FILENAME PROTO_N ((f)) PROTO_T (filename f)
261
{
262
  FULLCOMMENT2("INSPECT_FILENAME %d: '%s'", nofds, (int) CSTRING(f->file));
263
 
264
  if (fds == (filename *) 0)
265
  {
266
    fds = (filename *) xmalloc((szfds += 5) * sizeof (filename));
267
  }
268
  else if (nofds >= szfds)
269
  {
270
    fds = (filename *) xrealloc(fds, (szfds += 5) * sizeof (filename));
271
  }
272
 
273
  fds[nofds] = f;
274
 
275
  /*
276
   * The following is taken from trans386/dwarf/dwarf_out.c.
277
   * It works out the main source filename. There must be a better way. +++
278
   */
279
  if (mainfile_fd < 0)
280
  {
281
    /* use heuristic to see if this is 'main source file' */
282
    char *lastdot = strrchr(CSTRING(f->file), '.');
283
 
284
    /* XXX.h not considered 'main source file' */
285
    if (lastdot != 0 && lastdot[1] != 'h')
286
      mainfile_fd = nofds;
287
  }
288
 
289
  nofds++;
290
  return;
291
}
292
 
293
/*(See diag_config.h)
294
 * outputs structs & unions from global level types
295
 */
296
void OUTPUT_DIAG_TAGS PROTO_Z ()
297
{
298
  diag_tagdef **di = unit_ind_diagtags;
299
  unsigned int n = unit_no_of_diagtags;
300
  int i;
301
  int il = no_of_sus; /* There should be no clashes within a single unit so we save a bit of time here */
302
 
303
  for (i = 0; i < n; ++i)
304
  {
305
    diag_type d = di[i]->d_type;
306
 
307
    switch (d->key)
308
    {
309
     case DIAG_TYPE_STRUCT:
310
     case DIAG_TYPE_UNION:
311
      {
312
	int j;
313
 
314
	for (j = 0; j < il; j++)
315
	{
316
	  /* Check we have not done already */
317
	  if (eq_sutype(d, su_diags[j]->d_type))
318
	  {
319
	    break;
320
	  }
321
	}
322
	if (j != il)
323
	{
324
	  break;
325
	}
326
	if (no_of_sus >= leng_sus)
327
	{
328
	  if (su_diags == (diag_tagdef **) 0)
329
	  {
330
	    su_diags=(diag_tagdef**)xmalloc((leng_sus+=20)*sizeof(diag_tagdef*));
331
	  }
332
	  else
333
	  {
334
	    su_diags=(diag_tagdef**)xrealloc(su_diags,(leng_sus+=20)*sizeof(diag_tagdef*));
335
	  }
336
	}
337
	/* Set up the appropriate member of the su_diags array */
338
	su_diags[no_of_sus++] = di[i];
339
	break;
340
      }
341
    default:
342
      break;
343
    }
344
  }
345
  return;
346
}
347
/*
348
 * Collects information
349
 */
350
void OUTPUT_GLOBALS_TAB PROTO_Z ()
351
{
352
  diag_descriptor * unit_typedef_array = unit_diagvar_tab.array;
353
  unsigned int no_of_typedefs_in_unit =unit_diagvar_tab.lastused;
354
  int i;
355
  int il = no_of_typedefs;
356
 
357
  for(i=0 ; i<=no_of_typedefs_in_unit ; i++)
358
  {
359
    diag_descriptor * dd = unit_typedef_array + i;
360
 
361
    switch (dd->key)
362
    {
363
     case DIAG_TYPEDEF_KEY:
364
      {
365
	int j;
366
 
367
	/* Check that we have not done already */
368
	for(j=0; j < il ; j++)
369
	{
370
	  if(eq_typedef_type(dd,typedef_diags[j]))
371
	  {
372
	    break;
373
	  }
374
	}
375
	if( j != il )
376
	{
377
	  break;
378
	}
379
 
380
	/* We have a new one */
381
	if (no_of_typedefs >= leng_typedefs)
382
	{ 
383
	  if(typedef_diags == (diag_descriptor **) 0)
384
	  {
385
	    typedef_diags = (diag_descriptor **)xmalloc ((leng_typedefs+=20)*sizeof(diag_descriptor *));
386
	  }
387
	  else
388
	  {
389
	    typedef_diags = (diag_descriptor **)xrealloc(typedef_diags,(leng_typedefs+=20)*sizeof(diag_descriptor *));
390
	  }
391
	}
392
	typedef_diags[no_of_typedefs++]=dd;
393
      }
394
      break;
395
     default:
396
      break;
397
    }
398
  }
399
}
400
 
401
 
402
static void number_and_stab_basicshapes PROTO_Z ()
403
{
404
  /* NOTE: char is unsigned char */
405
  stab_basicshape(scharsh ,"signed char"   , TYPEID_SCHAR , TP_SCHAR);
406
  stab_basicshape(ucharsh ,"char"          , TYPEID_UCHAR , TP_CHAR);
407
  stab_basicshape(swordsh ,"short"         , TYPEID_SWORD , TP_SHORT);
408
  stab_basicshape(uwordsh ,"unsigned short", TYPEID_UWORD , TP_USHORT);
409
  stab_basicshape(slongsh ,"int"           , TYPEID_SLONG , TP_LONG);
410
  stab_basicshape(ulongsh ,"unsigned int"  , TYPEID_ULONG , TP_ULONG);
411
  stab_basicshape(shrealsh,"float"         , TYPEID_SHREAL, TP_FLOAT);
412
  stab_basicshape(realsh  ,"double"        , TYPEID_REAL  , TP_DOUBLE);
413
  stab_basicshape(doublesh,"long double"   , TYPEID_DOUBLE, TP_LDOUBLE);
414
  stab_basicshape(voidsh  ,"void"          , TYPEID_VOID  , TP_VOID);
415
 
416
#if 0
417
  /* this work fine with dbx and gdb, the type is given as "TOKENISED_TYPE" */
418
  stab_basicshape(tokensh, "TOKENISED_TYPE", TYPEID_TOKEN, TP_VOID);
419
#else
420
  /*
421
   * ... but this is better
422
   * dbx gives type as "@TOKENISED_TYPE", gdb "struct <unknown>",
423
   * when value is printed, dbx says "[typeref]", gdb "<incomplete type>"
424
   */
425
  {
426
    int n = next_typen();	/* use up the typen TYPEID_TOKEN */
427
 
428
    ASSERT(TYPEID_TOKEN == n);
429
 
430
    /*
431
     * TypeId:	    INTEGER = TypeDef	"New type number described by TypeDef"
432
     * TypeDef:	    o NAME ;		"Opaque type"
433
     */
434
    fprintf(as_file, "\t.stabx\t\":t%d=oTOKENISED_TYPE;", TYPEID_TOKEN);
435
    fprintf(as_file, "\",%d,%d,%d\n", 0, C_DECL, 0);
436
  }
437
#endif
438
}
439
 
440
static void number_structs_and_unions PROTO_Z ()
441
{
442
  int i;
443
 
444
  for (i = 0; i < no_of_sus; ++i)
445
  {
446
    SET_OUTED_NO(su_diags[i]->d_type, next_typen());
447
  }
448
}
449
static void number_typedefs PROTO_Z ()
450
{
451
  int i;
452
 
453
  for (i=0;i<no_of_typedefs; i++)
454
  {
455
    if (!IS_OUTED(typedef_diags[i]->data.typ.new_type))
456
    {
457
      SET_OUTED_NO(typedef_diags[i]->data.typ.new_type,next_typen());
458
    }
459
  }
460
}
461
 
462
static void stab_structs_and_unions PROTO_Z ()
463
{
464
  int i;
465
 
466
  for (i = 0; i < no_of_sus; ++i)
467
  {
468
    int non;
469
    diag_type dt = su_diags[i]->d_type;
470
    char *nme = (dt->key == DIAG_TYPE_STRUCT)
471
			? CSTRING(dt->data.t_struct.nme)
472
			: CSTRING(dt->data.t_union.nme);
473
 
474
    COMMENT2("su_diags: is_struct=%d nme='%s'", dt->key == DIAG_TYPE_STRUCT, (int) nme);
475
 
476
    stab_internal_types(dt, 0);
477
 
478
    if (nme == (char *) 0 || *nme == (char) 0)
479
    {
480
      /*
481
       * Output unnamed object here for 2 reasons:
482
       *	1) gdb (but not dbx) is confused by Typeid which includes a
483
       *	   full TypeDef and treats it as an anonymous struct.
484
       *	   It prefers TypeDef to refer to a previous TypeId INTEGER.
485
       *	2) It avoids very long stabstrings from unnamed object
486
       *	   being expanded on the fly.
487
       */
488
      fprintf(as_file, "\t.stabx\t\":T");	/* Unnamed object */
489
    }
490
    else
491
    {
492
      fprintf(as_file, "\t.stabx\t\"%s:T", nme);
493
    }
494
 
495
    non = OUTED_NO(dt);
496
    CLR_OUTED_NO(dt);			/* avoid identity */
497
    out_dt_NewTypeId(dt, non);
498
 
499
    fprintf(as_file, "\",%d,%d,%d\n", 0, C_DECL, 0);
500
  }
501
}  
502
static void stab_typedefs PROTO_Z ()
503
{
504
  int i;
505
  for(i=0;i<no_of_typedefs;i++)
506
  {
507
    diag_descriptor * dd = typedef_diags[i];
508
    int non;
509
    diag_type dt = dd->data.typ.new_type;
510
    stab_internal_types(dt,0);
511
    ASSERT(CSTRING(dd->data.typ.nme)[0]!=0);/* Not an empty string */
512
 
513
    fprintf(as_file, "\t.stabx\t\"%s:t", CSTRING(dd->data.typ.nme));
514
 
515
    non = OUTED_NO(dt);
516
    CLR_OUTED_NO(dt);			/* avoid identity */
517
    out_dt_NewTypeId(dt, non);
518
 
519
    fprintf(as_file, "\",%d,%d,%d\n", 0, C_DECL, 0);
520
  }
521
}
522
 
523
/*
524
 * init_diag - used by translate_capsule() to initialise diagnostics.
525
 */
526
void init_diag PROTO_Z ()
527
{
528
  /* 
529
   * Initialisation of diagnostics 
530
   */
531
  current_fileno = UNKNOWN;
532
  current_procstart_lineno = NOT_IN_PROC;
533
  current_lineno = NOT_IN_PROC;
534
  files_stabbed = 0;
535
  first_fileno = -100;
536
  stab_file(mainfile_fd);
537
  voidsh = getshape(0, 0, 0, TOP_ALIGN, TOP_SZ, tophd);/* very dubious */
538
  tokensh = getshape(0, 0, 0, TOP_ALIGN, TOP_SZ, bothd);/* very dubious */
539
  stab_types();
540
  return;
541
}
542
 
543
 
544
/*
545
 * Correct the name for non externs (eg C static) to keep dbx happy.
546
 * gdb happily uses the stab info to override the assembler label,
547
 * but dbx does not.  local_prefix names (eg S.999) upset dbx,
548
 * partcularly on stack tracebacks from 'where'.  If we find that
549
 * the name is already in use, eg possibly from nested C static
550
 * we change the name to "S.NNN.name" to add meaning, but stay unique.
551
 */
552
void fixup_name PROTO_N ((global,top_def,crt_def)) PROTO_T (exp global X dec * top_def X dec * crt_def)
553
{
554
  diag_descriptor *dd;
555
  dec *d;
556
  char *id = crt_def->dec_u.dec_val.dec_id;	/* assembler label */
557
  char *nm;			/* real name, or uniqued real name */
558
 
559
  /*
560
   * Externs have correct name currently.
561
   * Any name not starting with local_prefix is special, leave it.
562
   */
563
  if (crt_def->dec_u.dec_val.extnamed ||
564
      id[0] != local_prefix[0] || id[1] != local_prefix[1])
565
  {
566
    return;			/* externs have correct name currently */
567
  }
568
 
569
  dd = find_dd(global);
570
 
571
  if (dd == (diag_descriptor *) 0)
572
  {
573
    /* eg string constant or currently static within proc */
574
    FULLCOMMENT("correct_name: no descriptor");
575
    return;
576
  }
577
 
578
  nm = CSTRING(dd->data.id.nme);
579
 
580
  /* search def chain to see if name is already used as assembler label */
581
  for (d = top_def; d != crt_def && d != (dec *) 0; d = d->def_next)
582
  {
583
    if (strcmp(nm, d->dec_u.dec_val.dec_id) == 0)
584
    {
585
 
586
      /*
587
       * Name already used, create new name to add meaning but stay unique:
588
       * "S.NNNN.name".
589
       */
590
      int len1 = strlen(id);
591
      int len2 = strlen(nm);
592
      char *newid;
593
 
594
      newid = (char *)xmalloc(len1 + 1 + len2 + 1);
595
      strcpy(newid, id);
596
      newid[len1] = '.';
597
      strcpy(&newid[len1 + 1], nm);
598
      nm = newid;
599
      break;
600
    }
601
  }
602
 
603
  crt_def->dec_u.dec_val.dec_id = nm;	/* change label to real identifier
604
					 * name */
605
}
606
 
607
 
608
/*
609
 * output_diag - used by make_code() to implement "diagnose_tag".
610
 */
611
void output_diag PROTO_N ((d,proc_no,e)) PROTO_T (diag_info * d X int proc_no X exp e)
612
{
613
  exp id;
614
 
615
  FULLCOMMENT1("output_diag: key=%d", d->key);
616
 
617
  if (d->key == DIAG_INFO_SOURCE)
618
  {
619
    sourcemark *s = &d->data.source.beg;
620
    int f = find_file(CSTRING(s->file->file));
621
 
622
    stabn(f, NATINT(s->line_no));
623
    return;
624
  }
625
 
626
  if (d->key != DIAG_INFO_ID)
627
  {
628
    FULLCOMMENT1("output_diag: unsupported key=%d", d->key);
629
    return;
630
  }
631
 
632
  id = son(d->data.id_scope.access);
633
 
634
  FULLCOMMENT3("output_diag: DIAG_INFO_ID %s isglob(id)=%d no(id)=%d",
635
	       (int) CSTRING(d->data.id_scope.nme), isglob(id), no(id));
636
 
637
  /* can't output global values as local names */
638
  if (isglob(id))
639
  {
640
    return;
641
  }
642
 
643
  ASSERT(name(id) == ident_tag);
644
 
645
  mark_scope(e);
646
  FULLCOMMENT1("output_diag: DIAG_INFO_ID mark_scope props(e)=%#x", props(e));
647
 
648
  if (props(e) & 0x80)
649
  {
650
    diagbr_open(current_fileno);
651
    stabn(current_fileno, current_lineno + 1);	/* don't have proper lineno */
652
  }
653
 
654
  /* dbx is upset if a function does not have a begin block, make sure it does */
655
  if (!isparam(id) && block_depth == 0)
656
  {
657
    stab_begin_block();
658
  }
659
  stab_local(CSTRING(d->data.id_scope.nme), d->data.id_scope.typ,
660
	     id, 0, current_fileno);
661
 
662
  if (isparam(id) && last_caller_param(id))
663
  {
664
    if (block_depth == 0)
665
    {
666
      stab_begin_block();
667
    }
668
#if 0
669
    stabn(current_fileno, current_lineno + 1);	/* don't have proper lineno */
670
#endif
671
  }
672
  return;
673
}
674
 
675
 
676
/*
677
 * output_end_scope - used by make_code() to make end of diagnose_tag scope.
678
 */
679
void output_end_scope PROTO_N ((d,e)) PROTO_T (diag_info * d X exp e)
680
{
681
  FULLCOMMENT1("output_end_scope key=%d", d->key);
682
  if (d->key == DIAG_INFO_SOURCE)
683
  {
684
    sourcemark *s = &d->data.source.end;
685
    int f = find_file(CSTRING(s->file->file));
686
    int lno = NATINT(s->line_no);
687
 
688
    stabn(f, lno);
689
    return;
690
  }
691
  if (d->key == DIAG_INFO_ID && props(e) & 0x80)
692
  {
693
    diagbr_close(current_fileno);
694
    return;
695
  }
696
}
697
/*
698
 * find_dd
699
 */
700
static diag_descriptor *find_dd PROTO_N ((e)) PROTO_T (exp e)
701
{
702
  if (diag_def==NULL) 
703
  {
704
    return NULL;
705
  }
706
  return diag_def->dec_u.dec_val.diag_info;
707
}
708
 
709
 
710
 
711
 
712
/*
713
 * get filename number
714
 */
715
static int find_file PROTO_N ((f)) PROTO_T (char *f)
716
{
717
  int i;
718
 
719
  for (i = 0; i < nofds; i++)
720
  {
721
    if (strcmp(f, CSTRING(fds[i]->file)) == 0)
722
    {
723
      return i;
724
    }
725
  }
726
  return 0;
727
}
728
 
729
 
730
 
731
/*
732
 * ============================================================================
733
 *		Procs to generate line number related stabs
734
 */
735
 
736
 
737
/*
738
 * output directive using proc start relative linenos
739
 */
740
static void stab_relativeline PROTO_N ((directive)) PROTO_T (CONST char *directive)
741
{
742
  int lineinproc = current_lineno - current_procstart_lineno;
743
 
744
  /* avoid nonsense line nos from unusual header or #line usage */
745
  if (lineinproc >= 0)
746
  {
747
    fprintf(as_file, "\t%s\t%d\n", directive, lineinproc + 1);
748
  }
749
  else if (directive != line_stab)
750
  {
751
    fprintf(as_file, "\t%s\t%d\n", directive, 1);	/* must put out
752
							 * something to match
753
							 * begin/end */
754
  }
755
  return;
756
}
757
 
758
 
759
/*
760
 * output begin block directive
761
 */
762
static void stab_begin_block PROTO_Z ()
763
{
764
#if 0
765
  if (MAX_BLOCK_DEPTH < 0 || block_depth < MAX_BLOCK_DEPTH)
766
#endif
767
  {
768
    stab_relativeline(bb_stab);
769
  }
770
  block_depth++;
771
  return;
772
}
773
 
774
/*
775
 * output end block directive
776
 */
777
static void stab_end_block PROTO_Z ()
778
{
779
  if (block_depth >= 0)
780
  {
781
#if 0
782
    if (MAX_BLOCK_DEPTH < 0 || block_depth <= MAX_BLOCK_DEPTH)
783
#endif
784
    {
785
      stab_relativeline(eb_stab);
786
    }
787
    block_depth--;
788
  }
789
  return;
790
}
791
 
792
 
793
/*
794
 * Generate stabs for current file and line number.
795
 * Note that line number stabs are relative to start of current proc.
796
 */
797
static void stabn PROTO_N ((findex,lno)) PROTO_T (int findex X int lno)
798
{
799
  if (findex == current_fileno && lno == current_lineno)
800
  {
801
    return;
802
  }
803
  if (findex != current_fileno)
804
  {
805
    stab_file(findex);
806
  }
807
  if (current_procstart_lineno == NOT_IN_PROC)
808
  {
809
    return;
810
  }
811
 
812
  current_lineno = lno;
813
  stab_relativeline(line_stab);
814
  return;
815
}
816
/*
817
 * stab_end_file ties up lose ends 
818
 */
819
void stab_end_file PROTO_Z ()
820
{
821
  if (current_fileno != first_fileno)
822
  {
823
    COMMENT("stab_end_file: close the last include file with  a .ei");
824
    /* close the last include file */
825
    fprintf(as_file, "\t.ei\t\"%s\"\n", CSTRING(fds[current_fileno]->file));
826
  }
827
  return;
828
}
829
 
830
/*
831
 * output file name if changed
832
 */
833
static void stab_file PROTO_N ((findex)) PROTO_T (int findex)
834
{
835
  bool stabbed = 0;
836
 
837
  if ((findex == current_fileno) || (findex < 0) || (findex >= szfds))
838
  {
839
    return;
840
  }
841
 
842
  if (files_stabbed == 0)
843
  {
844
    /* .file */
845
    fprintf(as_file, "\n\t.file\t\"%s\"\n", CSTRING(fds[findex]->file));
846
    stabbed = 1;
847
    first_fileno = findex;
848
  }
849
  else
850
  {
851
    /* .bi (begin include) if appropriate */
852
 
853
    /*
854
     * +++ .bi/.ei causes dbx to core dump, see IBM problem fix (APAR)
855
     * +++ IX26109 bosadt: .bi/.ei placement doesn't match xlc, debuggers suffer
856
     * +++ so we leave them commented out currently.
857
     */
858
 
859
    /* first end previous .bi if needed */
860
    if (current_fileno != first_fileno)
861
    {  
862
      fprintf(as_file, "\t.ei\t\"%s\"\n", CSTRING(fds[current_fileno]->file));
863
    }
864
 
865
    /* .bi if not same as original .file */
866
    if (findex != first_fileno)
867
    {
868
      fprintf(as_file, "\n\t.bi\t\"%s\"\n", CSTRING(fds[findex]->file));
869
      stabbed = 1;
870
    }
871
    else
872
    {
873
      /* output a comment to indicate back to .file level */
874
      fprintf(as_file, "\n#\t.file\t\"%s\"\n", CSTRING(fds[findex]->file));
875
    }
876
  }
877
 
878
  files_stabbed++;
879
 
880
  if (stabbed)
881
  {
882
    /* output TDF file machine */
883
    char *mach = CSTRING(fds[findex]->machine);
884
    time_t t = NATINT(fds[findex]->date);
885
 
886
    /* output machine name if given */
887
    if (mach != 0 && *mach != '\0')
888
    {
889
      fprintf(as_file, "#\tMachine: \"%s\"\n", mach);
890
    }
891
 
892
    /* output TDF file time (ctime appends extra '\n') */
893
    if (t != 0)
894
    {
895
      fprintf(as_file, "#\tSource file date: %s\n", ctime(&t));
896
    }
897
  }
898
  current_fileno = findex;
899
  return;
900
}
901
 
902
 
903
/*
904
 * start of a new lex level
905
 */
906
static void diagbr_open PROTO_N ((findex)) PROTO_T (int findex)
907
{
908
  stab_file(findex);
909
  stab_begin_block();
910
  return;
911
}
912
 
913
/*
914
 * end of a lex level
915
 */
916
static void diagbr_close PROTO_N ((findex)) PROTO_T (int findex)
917
{
918
  stab_file(findex);
919
  stab_end_block();
920
  return;
921
}
922
 
923
 
924
 
925
/*
926
 * ============================================================================
927
 *		Procs to generate type or object related stabs
928
 */
929
 
930
 
931
/*
932
 * get the next type number to be used as a stabstring TypeId
933
 */
934
static int next_typen PROTO_Z ()
935
{
936
  static int typeno = 0;	/* types are numbered from 1 */
937
 
938
  return ++typeno;
939
}
940
 
941
 
942
/*
943
 * return standard TypeNo output by stab_types() for simple shapes
944
 */
945
static int TypeNo_of_shape PROTO_N ((s)) PROTO_T (shape s)
946
{
947
  /* tokensh is special to this module, cannot use name(tokensh) */
948
  if (s == tokensh)
949
  {
950
    return TYPEID_TOKEN;
951
  }
952
 
953
  switch (name(s))
954
  {
955
   case bothd:		/*FALLTHROUGH*/
956
   case tophd:		return TYPEID_VOID;
957
   case scharhd:	return TYPEID_SCHAR;
958
   case ucharhd:       	return TYPEID_UCHAR;
959
   case swordhd:       	return TYPEID_SWORD;
960
   case uwordhd:       	return TYPEID_UWORD;
961
   case sizehd:		/*FALLTHROUGH*/
962
   case slonghd:       	return TYPEID_SLONG;
963
   case ulonghd:	return TYPEID_ULONG;
964
   case shrealhd:	return TYPEID_SHREAL;
965
   case realhd:		return TYPEID_REAL;
966
   case doublehd:	return TYPEID_DOUBLE;
967
 
968
   default:
969
    {
970
      ASSERT(0);		/* fail if debugging */
971
      return TYPEID_VOID;	/* return something that will work */
972
    }
973
    /* NOTREACHED */ 
974
  }
975
}
976
 
977
 
978
/*
979
 * Are two struct/union types structurally equivalent?
980
 * That is, the same stab TypeDef would be generated.
981
 */
982
static bool eq_sutype PROTO_N ((a,b)) PROTO_T (diag_type a X diag_type b)
983
{
984
  diag_field_list fa;
985
  diag_field_list fb;
986
  int j;
987
 
988
  if (a == b)
989
  {
990
    return 1;
991
  }
992
  if (a->key != b->key)
993
  {
994
    return 0;
995
  }
996
  if (a->key != DIAG_TYPE_STRUCT && a->key != DIAG_TYPE_UNION)
997
  {
998
    return 0;
999
  }
1000
 
1001
  if (strcmp(CSTRING(a->data.t_struct.nme), CSTRING(b->data.t_struct.nme)))
1002
  {
1003
    return 0;
1004
  }
1005
  fa = a->data.t_struct.fields;
1006
  fb = b->data.t_struct.fields;
1007
  if (fa->lastused != fb->lastused)
1008
  {
1009
    return 0;
1010
  }
1011
  for (j = fa->lastused - 1; j >= 0; j--)
1012
  {
1013
    diag_field sfa = (fa->array)[j];
1014
    diag_field sfb = (fb->array)[j];
1015
 
1016
    if (strcmp(CSTRING(sfa->field_name), CSTRING(sfb->field_name)))
1017
    {
1018
      return 0;
1019
    }
1020
  }
1021
  return eq_shape(a->data.t_struct.tdf_shape, b->data.t_struct.tdf_shape);
1022
}
1023
static bool eq_typedef_type PROTO_N ((a,b)) PROTO_T (diag_descriptor * a X diag_descriptor * b)
1024
{
1025
  if(a==b)
1026
  {
1027
    return 1;
1028
  }
1029
 
1030
  if (strcmp(CSTRING(a->data.typ.nme),CSTRING(b->data.typ.nme))==0)
1031
  {
1032
    return 1;
1033
  }
1034
  return 0;
1035
}
1036
 
1037
 
1038
 
1039
/*
1040
 * size in bits of object represented by dt, or negative for unknown size
1041
 */
1042
static int size_dt PROTO_N ((dt)) PROTO_T (diag_type dt)
1043
{
1044
  switch (dt->key)
1045
  {
1046
   case DIAG_TYPE_PTR:
1047
    {
1048
      return 32;
1049
    }
1050
   case DIAG_TYPE_ARRAY:
1051
    {
1052
      int stride = EXPINT(dt->data.array.stride);
1053
      int lwb = EXPINT(dt->data.array.lower_b);
1054
      int upb = EXPINT(dt->data.array.upper_b);
1055
      int nelements = upb - lwb + 1;
1056
 
1057
      if (nelements < 0)
1058
      {
1059
	return 0;		/* avoid negative size from "super-flat" arrays */
1060
      }
1061
 
1062
      if (stride > 0)
1063
      {
1064
	ASSERT(stride >= size_dt(dt->data.array.element_type));
1065
	return nelements * stride;
1066
      }
1067
      else
1068
      {
1069
	/* stride not expected to be <= 0, but just in case ... */
1070
	return nelements * size_dt(dt->data.array.element_type);
1071
      }
1072
      /* NOTREACHED */
1073
    }
1074
   case DIAG_TYPE_STRUCT:
1075
    {
1076
      return shape_size(dt->data.t_struct.tdf_shape);
1077
    }
1078
   case DIAG_TYPE_UNION:
1079
    {
1080
      return shape_size(dt->data.t_union.tdf_shape);
1081
    }
1082
   case DIAG_TYPE_ENUM:
1083
    {
1084
      return size_dt(dt->data.t_enum.base_type);
1085
    }
1086
   case DIAG_TYPE_VARIETY:
1087
    {
1088
      return shape_size(dt->data.var);
1089
    }
1090
   case DIAG_TYPE_PROC:
1091
    {
1092
      return 32;		/* ptr to proc */
1093
    }
1094
   case DIAG_TYPE_LOC:
1095
    {
1096
      return size_dt(dt->data.loc.object);
1097
    }
1098
   case DIAG_TYPE_FLOAT:
1099
    {
1100
      return shape_size(f_floating(dt->data.f_var));	/* shape for f_var */
1101
    }
1102
   case DIAG_TYPE_NULL:
1103
    {
1104
      return 0;			/* void has no size */
1105
    }
1106
   case DIAG_TYPE_BITFIELD:
1107
    {
1108
      return NATINT(dt->data.bitfield.no_of_bits);
1109
    }
1110
   case DIAG_TYPE_UNINIT:	/* from tokenised type */
1111
   case DIAG_TYPE_INITED:
1112
    {
1113
      return -1;		/* +++ we should try to do better somehow */
1114
    }
1115
   default:
1116
    {
1117
      ASSERT(0);		/* fail if in debug mode */
1118
      return -1;
1119
    }
1120
  }
1121
}
1122
 
1123
 
1124
/*
1125
 * output TypeDef, except as a INTEGER TypeId to avoid recursion
1126
 */
1127
static void out_dt_TypeDef_no_recurse PROTO_N ((dt)) PROTO_T (diag_type dt)
1128
{
1129
  switch (dt->key)
1130
  {
1131
   case DIAG_TYPE_PTR:
1132
    {
1133
      /* TypeDef:		* TypeId	"Pointer of type TypeId" */
1134
      fprintf(as_file, "*");
1135
      out_dt_TypeId(dt->data.ptr.object);
1136
      break;
1137
    }
1138
   case DIAG_TYPE_ARRAY:
1139
    {
1140
      int stride = EXPINT(dt->data.array.stride);
1141
      int lwb = EXPINT(dt->data.array.lower_b);
1142
      int upb = EXPINT(dt->data.array.upper_b);
1143
      diag_type index_type = dt->data.array.index_type;
1144
      diag_type element_type = dt->data.array.element_type;
1145
 
1146
      ASSERT(stride >= size_dt(element_type));
1147
 
1148
#if 0
1149
      /* +++ maybe this works better thab Packed array with dbx/gdb, try it */
1150
      if (stride == 1 && lwb == 0 && upb < 32)
1151
      {
1152
	/* represent as bitfield */
1153
	fprintf(as_file, "r%d;0;4294967295;", TYPEID_SLONG);
1154
	break;
1155
      }
1156
#endif
1157
 
1158
      /*
1159
       * TypeDef:		 Array
1160
       * Array:		a TypeDef ; TypeId		"Array"
1161
       *		|	P TypeDef ; TypeId		"Packed array"
1162
       *		|	A TypeId			"Open array of TypeId"
1163
       *	where	TypeDef:	Subrange
1164
       * Subrange:	r TypeId ; Bound ; Bound
1165
       * Bound:		INTEGER				"Constant bound"
1166
       */
1167
      if (stride == 1)
1168
      {
1169
	fprintf(as_file, "Pr");		/* Packed Array - should never happen for C */	
1170
      }
1171
      else
1172
      {
1173
	fprintf(as_file, "ar");
1174
      }
1175
      out_dt_TypeId(index_type);
1176
      fprintf(as_file, ";%d;%d;", lwb, upb);
1177
      out_dt_TypeId(element_type);
1178
      break;
1179
    }
1180
   case DIAG_TYPE_STRUCT:
1181
   case DIAG_TYPE_UNION:
1182
    {
1183
      int i;
1184
      char su;
1185
      diag_field_list fields;
1186
      shape s;
1187
 
1188
      /* TypeDef:	Record */
1189
 
1190
      if (dt->key == DIAG_TYPE_STRUCT)
1191
      {
1192
	fields = dt->data.t_struct.fields;
1193
	s = dt->data.t_struct.tdf_shape;
1194
 
1195
	/* Record:	s NumBytes FieldList ;		"Structure or record definition" */
1196
	su = 's';
1197
      }
1198
      else
1199
      {				/* dt->key == DIAG_TYPE_UNION */
1200
	fields = (diag_field_list) dt->data.t_union.fields;
1201
	s = dt->data.t_union.tdf_shape;
1202
	/* Record:	u NumBytes FieldList ;		"Union" */
1203
	su = 'u';
1204
      }
1205
 
1206
      /* NumBytes:	INTEGER */
1207
      fprintf(as_file, "%c%d", su, shape_size(s) / 8);
1208
 
1209
      /* FieldList:	Field | FieldList Field */
1210
      for (i = fields->lastused - 1; i >= 0; i--)
1211
      {
1212
	int size;
1213
	diag_field sf = (fields->array)[i];
1214
	int offset = EXPINT(sf->where);
1215
 
1216
	/* Field:	NAME : TypeId , BitOffset , NumBits ; */
1217
	ASSERT(CSTRING(sf->field_name)[0]!=0);
1218
	fprintf(as_file, "%s:", CSTRING(sf->field_name));
1219
	out_dt_TypeId(sf->field_type);
1220
 
1221
	size = size_dt(sf->field_type);
1222
 
1223
#ifdef DO_ASSERT
1224
	/* check object size <= field size */
1225
	if (size > 0)
1226
	{
1227
	  int next_start;
1228
	  int sizetonext;
1229
 
1230
	  if (dt->key == DIAG_TYPE_UNION || i == 0)
1231
	  {
1232
	    next_start = shape_size(s);
1233
	  }
1234
	  else
1235
	  {
1236
	    next_start = EXPINT(((fields->array)[i - 1])->where);
1237
	  }
1238
	  sizetonext = next_start - offset;
1239
 
1240
	  ASSERT(size <= sizetonext);
1241
	}
1242
#endif
1243
 
1244
	if (size < 0)		/* eg from tokenised type */
1245
	{
1246
	  /* guess: space to next field, or end */
1247
	  int next_start;
1248
 
1249
	  if (dt->key == DIAG_TYPE_UNION || i == 0)
1250
	    next_start = shape_size(s);
1251
	  else
1252
	    next_start = EXPINT(((fields->array)[i - 1])->where);
1253
	  size = next_start - offset;
1254
	}
1255
 
1256
	if (size < 0)
1257
	{
1258
	  size = 32;		/* desperate guess */
1259
	}
1260
 
1261
	/*
1262
	 * BitOffset:	INTEGER		"Offset in bits from beginning of structure"
1263
	 * NumBits:	INTEGER		"Number of bits in item"
1264
	 */
1265
	fprintf(as_file, ",%d,%d;", offset, size);	/* bitoff,bitsz */
1266
      }
1267
      fprintf(as_file, ";");
1268
      break;
1269
    }
1270
 
1271
  case DIAG_TYPE_ENUM:
1272
    {
1273
#if 1
1274
      /* simply output the base integer type */
1275
      out_dt_TypeDef(dt->data.t_enum.base_type);
1276
#else
1277
 
1278
      /*
1279
       * +++ currently tdfc (Jan 93) does not generate DIAG_TYPE_ENUM
1280
       * +++ enable and test this when DIAG_TYPE_ENUM is generated
1281
       */
1282
 
1283
      /*
1284
       * TypeDef:	e EnumList ;		"Enumerated type (default size, 32 bits)"
1285
       * EnumList:	Enum | EnumList Enum
1286
       * Enum:		NAME : OrdValue ,	"Enumerated scalar description"
1287
       * OrdValue:	INTEGER			"Associated numeric value"
1288
       */
1289
      enum_values_list enumvals = dt->data.t_enum.values;
1290
      enum_values enumarr = *(enumvals->array);
1291
      int nvals = enumvals->len;
1292
      int i;
1293
 
1294
      ASSERT(size_dt(dt->data.t_enum.base_type) == 32);
1295
 
1296
      fprintf(as_file, "e");
1297
      for (i = 0; i < nvals; i++)
1298
      {
1299
	fprintf(as_file, "%s:%d,", CSTRING(enumarr[i].nme), EXPINT(enumarr[i].val));
1300
      }
1301
      fprintf(as_file, ";");
1302
#endif
1303
      break;
1304
    }
1305
 
1306
  case DIAG_TYPE_VARIETY:
1307
    {
1308
      int i = TypeNo_of_shape(dt->data.var);
1309
 
1310
      /*
1311
       * TypeDef:	INTEGER		"Type number of a previously defined type"
1312
       */
1313
      fprintf(as_file, "%d", i);
1314
      SET_OUTED_NO(dt, i);
1315
      break;
1316
    }
1317
 
1318
  case DIAG_TYPE_PROC:
1319
    {
1320
      diag_type result_type = dt->data.proc.result_type;
1321
      int non;
1322
 
1323
      /*
1324
       * In general DIAG_TYPE_PROC in C terms means a pointer to a proc:
1325
       * TypeDef:		* TypeId	"Pointer of type TypeId"
1326
       */
1327
 
1328
      fprintf(as_file, "*");
1329
 
1330
      /* TypeId:		INTEGER = TypeDef */
1331
      /* we need a new typeno to match the TypeId syntax */
1332
      non = next_typen();
1333
      fprintf(as_file, "%d=", non);
1334
 
1335
      /*
1336
       * TypeDef:	ProcedureType	"For function types rather than declarations"
1337
       * ProcedureType:	f TypeId ;	"Function returning type TypeId"
1338
       * +++ generate parameter info as well, though IBM stabsyntax says this is for Modula-2:
1339
       * +++ ProcedureType:	f TypeId , NumParams ; TParamList ;
1340
       *				"Function of N parameters returning type TypeId"
1341
       */
1342
      fprintf(as_file, "f");
1343
      out_dt_TypeId(result_type);
1344
      break;
1345
    }
1346
 
1347
  case DIAG_TYPE_LOC:
1348
    {
1349
      /* simply output type of object */
1350
 
1351
      /* +++ use data.loc.qualifier.is_const and data.loc.qualifier.is_volatile if useful */
1352
      out_dt_TypeDef(dt->data.loc.object);
1353
      break;
1354
    }
1355
 
1356
  case DIAG_TYPE_FLOAT:
1357
    {
1358
      /* simply output standard type number */
1359
      int i = TypeNo_of_shape(f_floating(dt->data.f_var));	/* shape for f_var */
1360
 
1361
      /*
1362
       * TypeDef:		INTEGER		"Type number of a previously defined type"
1363
       */
1364
      fprintf(as_file, "%d", i);
1365
      SET_OUTED_NO(dt, i);
1366
      break;
1367
    }
1368
 
1369
  case DIAG_TYPE_NULL:
1370
    {
1371
 
1372
      /*
1373
       * TypeDef:		INTEGER		"Type number of a previously defined type"
1374
       */
1375
      fprintf(as_file, "%d", TYPEID_VOID);	/* use "void" for the null
1376
						 * type */
1377
      SET_OUTED_NO(dt, TYPEID_VOID);
1378
      break;
1379
    }
1380
 
1381
  case DIAG_TYPE_BITFIELD:
1382
    {
1383
#if 1
1384
 
1385
      /*
1386
       * Rely on struct field processing to provide the bitfield size,
1387
       * which works very well for C.  Simply output the base type.
1388
       */
1389
      out_dt_TypeDef(dt->data.bitfield.result_type);
1390
#else
1391
      /* +++ this is wrong, TypeId where TypeDef expected, fix and try again */
1392
      /* this breaks gdb 4.9, and dbx ignores TypeAttr */
1393
 
1394
      /*
1395
       * TypeId:	INTEGER = TypeAttrs TypeDef
1396
       *					"New type with special type attributes"
1397
       * TypeAttrs:	@ TypeAttrList ;	"Any additional information; ignored by dbx"
1398
       * TypeAttrList:	TypeAttrList ; @ TypeAttr | TypeAttr
1399
       * TypeAttr:	s INTEGER		"Size in bits"
1400
       */
1401
      fprintf(as_file, "@s%d;", NATINT(dt->data.bitfield.no_of_bits));
1402
      out_dt_TypeDef(dt->data.bitfield.result_type);
1403
#endif
1404
      break;
1405
    }
1406
 
1407
  case DIAG_TYPE_UNINIT:	/* from tokenised type */
1408
  case DIAG_TYPE_INITED:
1409
    {
1410
 
1411
      /*
1412
       * TypeDef:		INTEGER		"Type number of a previously defined type"
1413
       */
1414
      fprintf(as_file, "%d", TYPEID_TOKEN);
1415
      break;
1416
    }
1417
  default:
1418
    {
1419
      /* nothing expected now, but maybe there will be extensions */
1420
      ASSERT(0);		/* fail if in debug mode */
1421
      /* We must output something here to satisfy the syntax */
1422
 
1423
      /*
1424
       * TypeDef:		INTEGER		"Type number of a previously defined type"
1425
       */
1426
      fprintf(as_file, "%d", TYPEID_VOID);
1427
      break;
1428
    }
1429
  }
1430
}
1431
 
1432
 
1433
/*
1434
 * generate TypeDef stabstring
1435
 */
1436
static void out_dt_TypeDef PROTO_N ((dt)) PROTO_T (diag_type dt)
1437
{
1438
  if (IS_OUTED(dt))
1439
  {
1440
 
1441
    /*
1442
     * TypeDef:		INTEGER		"Type number of a previously defined type"
1443
     */
1444
    fprintf(as_file, "%d", OUTED_NO(dt));
1445
  }
1446
  else
1447
  {
1448
    /* any of the other expansions of TypeDef */
1449
    out_dt_TypeDef_no_recurse(dt);
1450
  }
1451
}
1452
 
1453
 
1454
/*
1455
 * generate TypeId stabstring defining new type number
1456
 * TypeId:	INTEGER = TypeDef	"New type number described by TypeDef"
1457
 */
1458
static void out_dt_NewTypeId PROTO_N ((dt,non)) PROTO_T (diag_type dt X int non)
1459
{
1460
  fprintf(as_file, "%d=", non);
1461
 
1462
  if (IS_OUTED(dt))
1463
  {
1464
    out_dt_TypeDef(dt);
1465
  }
1466
  else
1467
  {
1468
    /*
1469
     * We have to take care to handle recursive type correctly. Record the new
1470
     * type number so indirect refs (by PTR of PROC) do not lead to infinite
1471
     * recursion.  But we need to avoid simply identifying whole type to
1472
     * itself; out_dt_TypeDef_no_recurse() avoids this problem.
1473
     */
1474
    SET_OUTED_NO(dt, non);	/* record typeno for future use */
1475
    out_dt_TypeDef_no_recurse(dt);
1476
  }
1477
}
1478
 
1479
 
1480
/*
1481
 * generate TypeId stabstring
1482
 */
1483
static void out_dt_TypeId PROTO_N ((dt)) PROTO_T (diag_type dt)
1484
{
1485
  if (IS_OUTED(dt))
1486
  {
1487
 
1488
    /*
1489
     * TypeId:		INTEGER		"Type number of previously defined type"
1490
     */
1491
    fprintf(as_file, "%d", OUTED_NO(dt));
1492
    return;
1493
  }
1494
 
1495
  switch (dt->key)
1496
  {
1497
  case DIAG_TYPE_VARIETY:
1498
  case DIAG_TYPE_FLOAT:
1499
  case DIAG_TYPE_NULL:
1500
  case DIAG_TYPE_UNINIT:	/* from tokenised type */
1501
  case DIAG_TYPE_INITED:	/* from tokenised type */
1502
    {
1503
 
1504
      /*
1505
       * Simple types have preallocated TypeId INTEGER, out_dt_TypeDef()
1506
       * or for these cases out_dt_TypeDef_no_recurse() will output it.
1507
       */
1508
 
1509
      /*
1510
       * TypeId:		INTEGER		"Type number of previously defined type"
1511
       */
1512
      out_dt_TypeDef_no_recurse(dt);	/* for these keys, we assume TypeDef
1513
					 * expands to INTEGER */
1514
      break;
1515
    }
1516
 
1517
  case DIAG_TYPE_LOC:
1518
    {
1519
 
1520
      /* look down to object, so maybe avoiding allocating another TypeId INTEGER */
1521
      out_dt_TypeId(dt->data.loc.object);
1522
      break;
1523
    }
1524
 
1525
  default:
1526
    {
1527
      /* no existing TypeNo that we know of, stabsyntax requires a new one */
1528
 
1529
      /*
1530
       * TypeId:	INTEGER = TypeDef	"New type number described by TypeDef"
1531
       */
1532
      out_dt_NewTypeId(dt, next_typen());
1533
      break;
1534
    }
1535
  }
1536
}
1537
 
1538
/*
1539
 * Output .bs (begin static block) which must precede stab_global
1540
 * for tags which have data initialisers.  If such stabs are output
1541
 * outside .bs/.be gdb and dbx cannot locate tag.
1542
 */
1543
void stab_bs PROTO_N ((sectname)) PROTO_T (char *sectname)
1544
{
1545
  fprintf(as_file, "\t.bs\t%s\n", sectname);
1546
}
1547
 
1548
/*
1549
 * Output .es (end static block).
1550
 */
1551
void stab_es PROTO_N ((sectname)) PROTO_T (char *sectname)
1552
{
1553
  fprintf(as_file, "\t.es\n");
1554
}
1555
 
1556
 
1557
/*
1558
 * Produce diagnostic for ident_tag variable "id" defined by "global";
1559
 * called from translat().  "ext" tells whether "id" is "static".
1560
 */
1561
void stab_global PROTO_N ((global,id,ext)) PROTO_T (exp global X char *id X bool ext)
1562
{
1563
  diag_descriptor *dd = find_dd(global);
1564
 
1565
  if (dd == (diag_descriptor *) 0)
1566
    return;
1567
 
1568
  /* +++ inefficient */
1569
  stab_file(find_file(CSTRING(dd->data.id.whence.file->file)));
1570
 
1571
  /*
1572
   * Stabstring:	NAME : Class	"Name of object followed by object classification"
1573
   * Class:		Variable	"Variable in program"
1574
   * Variable:		G TypeId	"Global (external) variable of type TypeId"
1575
   *		|	S TypeId	"Module variable of type TypeId (C static global)"
1576
   */
1577
  ASSERT(CSTRING(dd->data.id.nme)[0]!=0);
1578
  fprintf(as_file, "\t.stabx\t\"%s:%c",
1579
	  CSTRING(dd->data.id.nme),
1580
	  (ext ? 'G' : 'S'));
1581
  out_dt_TypeId(dd->data.id.new_type);
1582
  fprintf(as_file, "\",%s,%d,%d\n",
1583
	  id,
1584
	  (ext ? C_GSYM : C_STSYM),
1585
	  0);
1586
}
1587
 
1588
 
1589
/*
1590
 * switch to correct file prior to proc prelude
1591
 */
1592
void stab_proc1 PROTO_N ((proc,id,ext)) PROTO_T (exp proc X char *id X bool ext)
1593
{
1594
  diag_descriptor *dd = find_dd(proc);
1595
 
1596
  block_depth = 0;
1597
 
1598
  if (dd == (diag_descriptor *) 0)
1599
  {
1600
    COMMENT("stab_proc1: no descriptor");	/* should never happen */
1601
    current_procstart_lineno = NOT_IN_PROC;
1602
    current_lineno = NOT_IN_PROC;
1603
    return;
1604
  }
1605
 
1606
  current_procstart_lineno = NATINT(dd->data.id.whence.line_no);
1607
  current_lineno = current_procstart_lineno;
1608
 
1609
  /* +++ inefficient */
1610
  stab_file(find_file(CSTRING(dd->data.id.whence.file->file)));
1611
}
1612
 
1613
 
1614
/*
1615
 * stap proc, after label defined
1616
 */
1617
void stab_proc2 PROTO_N ((proc,id,ext)) PROTO_T (exp proc X char *id X bool ext)
1618
{
1619
  diag_descriptor *dd = find_dd(proc);
1620
  char *nm;
1621
  diag_type dt;
1622
 
1623
  if (dd == (diag_descriptor *) 0)
1624
  {
1625
    COMMENT("stab_proc2: no descriptor");	/* should never happen */
1626
    return;
1627
  }
1628
 
1629
  dt = dd->data.id.new_type;
1630
  nm = CSTRING(dd->data.id.nme);	/* source proc name, id is just the
1631
					 * assembler label */
1632
  ASSERT(nm[0]!=0);
1633
 
1634
  /* first a .stabx for the proc descriptor */
1635
 
1636
  /*
1637
   * Stabstring:	NAME : Class
1638
   * Class:		Procedure	"Subprogram declaration"
1639
   * Procedure:		Proc		"Procedure at current scoping level"
1640
   * Proc:		F TypeID	"External function of type TypeID"
1641
   *		|	f TypeID	"Private function of type TypeID"
1642
   *		|	P		"External procedure"
1643
   *		|	Q		"Private procedure"
1644
   */
1645
 
1646
  /* +++ when gdb understands, maybe use "P" or "Q" for proc returning void */
1647
 
1648
  fprintf(as_file, "\t.stabx\t\"%s:%c", nm, (ext ? 'F' : 'f'));
1649
 
1650
  /*
1651
   * The meaning of TypeId is not clear from the stabstring syntax document,
1652
   * dbx and gdb work best if it is the result type.
1653
   */
1654
  switch (dt->key)
1655
  {
1656
  case DIAG_TYPE_PROC:
1657
    {
1658
      out_dt_TypeId(dt->data.proc.result_type);
1659
      break;
1660
    }
1661
  default:
1662
    {
1663
      /* should never happen, but if it does ... */
1664
      fail("stab_proc2: Should never happen");
1665
      out_dt_TypeId(dt);
1666
      break;
1667
    }
1668
  }
1669
 
1670
  fprintf(as_file, "\",.%s,%d,%d\n", id, C_FUN, 0);
1671
 
1672
#if 1
1673
 
1674
  /*
1675
   * Then a .function for the proc body. The 5th arg (function length) is not
1676
   * documented in assembler manual, but gcc generates it and it enable gdb to
1677
   * trace down stack properly.
1678
   */
1679
  fprintf(as_file, "\t.function\t.%s,.%s,16,044,E.%s-.%s\n", id, id, id, id);
1680
#else
1681
  fprintf(as_file, "\t.function\t.%s,.%s,16,044\n", id, id);
1682
#endif
1683
 
1684
  /* the proc start line number  */
1685
  fprintf(as_file, "\t%s\t%d\n", bf_stab ,current_procstart_lineno);
1686
 
1687
  /* now mork line 1 */
1688
  /*stab_relativeline(line_stab);*/
1689
}
1690
 
1691
 
1692
/*
1693
 * diagnostics for proc end
1694
 */
1695
void stab_endproc PROTO_N ((proc,id,ext)) PROTO_T (exp proc X char *id X bool ext)
1696
{
1697
  /* end all open blocks */
1698
  while (block_depth > 0)
1699
    stab_end_block();
1700
 
1701
  stab_relativeline(ef_stab);
1702
  fprintf(as_file, "E.%s:\n", id);	/* proc end label */
1703
 
1704
  current_procstart_lineno = NOT_IN_PROC;
1705
 
1706
  /* output AIX traceback table, see header file sys/debug.h */
1707
  {
1708
    static struct tbtable_short zero_tbtable_short;
1709
    struct tbtable_short tbtable_sht;
1710
    diag_descriptor *dd = find_dd(proc);
1711
    char *nm;
1712
    int i;
1713
 
1714
    if (dd == (diag_descriptor *) 0)
1715
    {
1716
      COMMENT("stab_endproc: no descriptor");	/* should never happen */
1717
      return;
1718
 
1719
    }
1720
 
1721
    nm = CSTRING(dd->data.id.nme);	/* source proc name, id is just the
1722
					 * assembler label */
1723
    ASSERT(nm[0]!=0);
1724
    tbtable_sht = zero_tbtable_short;
1725
 
1726
    /* +++ set up tbtable_sht more fully */
1727
 
1728
    tbtable_sht.lang = TB_C;	/* lang C +++ */
1729
    tbtable_sht.has_tboff = 1;	/* optional tb_offset always given */
1730
    tbtable_sht.name_present = 1;	/* optional proc name always given */
1731
    tbtable_sht.saves_lr =
1732
      !p_leaf;	/* non-leaf procs store link reg */
1733
    tbtable_sht.stores_bc =
1734
      !p_leaf;	/* non-leaf procs store stack backchain */
1735
 
1736
    /* number of float regs saved */
1737
    if (p_sfreg_first_save != FR_NO_REG)
1738
      tbtable_sht.fpr_saved = FR_31 + 1 - p_sfreg_first_save;
1739
 
1740
    /* number of fixed regs saved */
1741
    if (p_sreg_first_save != R_NO_REG)
1742
      tbtable_sht.gpr_saved = R_31 + 1 - p_sreg_first_save;
1743
 
1744
    /* number of fixed and float params passed in regs */
1745
    tbtable_sht.fixedparms = p_fixed_params;
1746
    tbtable_sht.floatparms = p_float_params;
1747
 
1748
    tbtable_sht.parmsonstk = 1;	/* -g always stores parameters on stack */
1749
 
1750
    /* 0 signifies start of traceback table */
1751
    fprintf(as_file, "#\ttraceback table\n");
1752
    fprintf(as_file, "\t.long\t0\n");
1753
 
1754
    /* tbtable_sht as bytes */
1755
    fprintf(as_file, "\t.byte\t");
1756
    for (i = 0; i < sizeof (tbtable_sht) - 1; i++)
1757
      fprintf(as_file, "%#x,", ((unsigned char *) (&tbtable_sht))[i]);
1758
    fprintf(as_file, "%#x\n", ((unsigned char *) (&tbtable_sht))[i]);
1759
 
1760
    /* optional portions of traceback table */
1761
 
1762
    /* parminfo */
1763
    if (tbtable_sht.fixedparms || tbtable_sht.floatparms)
1764
      fprintf(as_file, "\t.long\t0\n");	/* +++ */
1765
 
1766
    /* tb_offset */
1767
    fprintf(as_file, "\t.long\tE.%s-.%s\n", id, id);
1768
 
1769
    /* we never use hand_mask, ctl_info and ctl_info_disp optional components */
1770
    ASSERT(!tbtable_sht.int_hndl);
1771
    ASSERT(!tbtable_sht.has_ctl);
1772
 
1773
    /* proc name */
1774
    fprintf(as_file, "\t.short\t%d\n", (int)strlen(nm));
1775
    fprintf(as_file, "\t.byte\t\"%s\"\n", nm);
1776
 
1777
    /* alloca_reg */
1778
    if (tbtable_sht.uses_alloca)
1779
      fprintf(as_file, "\t.byte\t0\n");	/* +++ */
1780
 
1781
    /* keep program area [PR] word aligned */
1782
    fprintf(as_file, "\t.align\t2\n");
1783
  }
1784
}
1785
 
1786
 
1787
/*
1788
 * Output appropriate info to symbol table to indicate the declaration of
1789
 * a local identifier, nm, defined by id, displaced by disp; findex is the
1790
 * index of the file containing the declaration. The code below gives does
1791
 * not allow for identifiers allocated in registers; in fact, with
1792
 * current translator, none are used by stab_local. I don't even know
1793
 * whether dbx can actually use them.
1794
 */
1795
void stab_local PROTO_N ((nm,dt,id,disp,findex)) PROTO_T (char *nm X diag_type dt X exp id X int disp X int findex)
1796
{
1797
  FULLCOMMENT3("stab_local: %s disp=%d boff(id).offset=%d", (long) nm, disp, boff(id).offset);
1798
  disp += boff(id).offset;
1799
again:
1800
  if (name(id) == ident_tag)
1801
  {
1802
    FULLCOMMENT2("stab_local ident_tag: %s disp=%d", (long) nm, disp);
1803
    if ((props(id) & defer_bit) == 0)
1804
    {
1805
      if (isparam(id))
1806
      {
1807
	fprintf(as_file, "\t.stabx\t\"%s:p", nm);
1808
	out_dt_TypeId(dt);
1809
	fprintf(as_file, "\",%d,%d,%d\n", disp, C_PSYM, 0);
1810
	return;
1811
      }
1812
      else
1813
      {
1814
	fprintf(as_file, "\t.stabx\t\"%s:", nm);
1815
	out_dt_TypeId(dt);
1816
	fprintf(as_file, "\",%d,%d,%d\n", disp, C_LSYM, 0);
1817
	return;
1818
      }
1819
    }
1820
    else
1821
    {
1822
      exp sn = son(id);
1823
      int d = disp;
1824
 
1825
      while (sn != nilexp)
1826
      {
1827
	switch (name(sn))
1828
	{
1829
	case name_tag:
1830
	  {
1831
	    disp = d + no(sn);
1832
	    id = son(sn);
1833
	    if (isvar(id))
1834
	      dt = dt->data.ptr.object;
1835
	    goto again;
1836
	  }
1837
	case reff_tag:
1838
	  {
1839
	    d += no(sn);
1840
	    sn = son(sn);
1841
	    break;
1842
	  }
1843
	case cont_tag:
1844
	  {
1845
	    sn = son(sn);
1846
	    break;
1847
	  }
1848
	default:
1849
	  return;
1850
	}
1851
      }
1852
    }
1853
  }
1854
}
1855
 
1856
 
1857
/*
1858
 * Output a .stabx for all internal struct/unions not already processed.
1859
 * This avoids extremely long stabstrings from internal struct/unions being
1860
 * output on the fly for the top level struct/union.
1861
 * It also avoids dbx going recursive printing some types.
1862
 */
1863
static void stab_internal_types PROTO_N ((dt,stabthislevel)) PROTO_T (diag_type dt X bool stabthislevel)
1864
{
1865
  if (IS_OUTED(dt))
1866
    return;				/* already been here */
1867
 
1868
  switch (dt->key)
1869
  {
1870
  case DIAG_TYPE_STRUCT:
1871
  case DIAG_TYPE_UNION:
1872
    {
1873
      int i;
1874
      diag_field_list fields;
1875
      int non;
1876
 
1877
      if (stabthislevel && !IS_OUTED(dt))
1878
      {
1879
	non = next_typen();
1880
	SET_OUTED_NO(dt, non);		/* record typeno for future use,
1881
					 * use before definition allowed */
1882
      }
1883
      else
1884
      {
1885
	non = 0;
1886
      }
1887
 
1888
      if (dt->key == DIAG_TYPE_STRUCT)
1889
	fields = dt->data.t_struct.fields;
1890
      else
1891
	fields = (diag_field_list) dt->data.t_union.fields;
1892
 
1893
      for (i = fields->lastused - 1; i >= 0; i--)
1894
      {
1895
	diag_field sf = (fields->array)[i];
1896
 
1897
	stab_internal_types(sf->field_type, 1);
1898
      }
1899
 
1900
      if (non != 0)
1901
      {
1902
	/*
1903
	 * Generate NamedType stabstring:
1904
	 * NamedType:	T TypeId		"Struct, union, or enumeration tag"
1905
	 * TypeId:	INTEGER = TypeDef	"New type number described by TypeDef"
1906
	 */
1907
	fprintf(as_file, "\t.stabx\t\":");	/* Unnamed object
1908
						 * classification */
1909
	fprintf(as_file, "T%d=", non);
1910
	out_dt_TypeDef_no_recurse(dt);
1911
 
1912
	fprintf(as_file, "\",%d,%d,%d\n", 0, C_DECL, 0);
1913
      }
1914
 
1915
      break;
1916
    }
1917
 
1918
  case DIAG_TYPE_PTR:
1919
    {
1920
      stab_internal_types(dt->data.ptr.object, 1);
1921
      break;
1922
    }
1923
 
1924
  case DIAG_TYPE_ARRAY:
1925
    {
1926
      stab_internal_types(dt->data.array.index_type, 1);
1927
      stab_internal_types(dt->data.array.element_type, 1);
1928
      break;
1929
    }
1930
 
1931
  case DIAG_TYPE_PROC:
1932
    {
1933
      stab_internal_types(dt->data.proc.result_type, 1);
1934
      break;
1935
    }
1936
 
1937
  default:
1938
    {
1939
      /* simple types without internal types */
1940
      break;
1941
    }
1942
  }
1943
}
1944
 
1945
 
1946
/*
1947
 * Generate stab for a basic type, for which there is a standard IBM AIX StabNo.
1948
 */
1949
static void stab_basicshape
1950
    PROTO_N ((sha,typename,tdf_typeidnum,ibm_typeidnum))
1951
    PROTO_T (shape sha X char *typename X int tdf_typeidnum X int ibm_typeidnum)
1952
{
1953
  int n = next_typen();
1954
 
1955
  ASSERT(tdf_typeidnum == n);
1956
 
1957
  fprintf(as_file, "\t.stabx\t\"%s:t%d=%d", typename, tdf_typeidnum, ibm_typeidnum);
1958
  fprintf(as_file, "\",%d,%d,%d\n", 0, C_DECL, 0);
1959
}
1960
 
1961
 
1962
/*
1963
 * Output diagnostic stabs for built in types, and for structs and unions.
1964
 * Information about procedures is ignored for present;
1965
 * it is output by stab_procN().
1966
 *
1967
 * Must be called before any ".stabx" directive attempted.
1968
 */
1969
static void stab_types PROTO_Z ()
1970
{
1971
  /* Numbering and outputing of basicshapes,structs,unions and typedefs */
1972
  (void)number_and_stab_basicshapes();
1973
  (void)number_structs_and_unions();
1974
  (void)number_typedefs();
1975
  (void)stab_structs_and_unions();
1976
  (void)stab_typedefs();
1977
}
1978
 
1979