-
Notifications
You must be signed in to change notification settings - Fork 133
/
lldebug.cpp
3517 lines (3226 loc) · 113 KB
/
lldebug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
/**
\file
\brief Main module to generate LLVM debug informations using metadata
*/
#include "lldebug.h"
#include "dtypeutl.h"
#include "global.h"
#include "symtab.h"
#include "ll_structure.h"
#include "ll_builder.h"
#include "dwarf2.h"
#include "error.h"
#include "version.h"
#include "fih.h"
#include "llassem.h"
#include "cgllvm.h"
#include "cgmain.h"
#include "flang/ADT/hash.h"
#include "symfun.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef HOST_WIN
#include <unistd.h>
#endif
#include "upper.h"
#ifdef __cplusplus
/* clang-format off */
inline SPTR GetParamSptr(int dpdsc, int i) {
return static_cast<SPTR>(aux.dpdsc_base[dpdsc + i]);
}
/* clang-format on */
#else
#define GetParamSptr(dpdsc, i) (aux.dpdsc_base[dpdsc + i])
#endif
#if !defined(DECLLINEG)
#define DECLLINEG(sptr) 0
#endif
#ifndef DW_TAG_auto_variable
#define DW_TAG_auto_variable 0x100
#endif
#ifndef DW_TAG_arg_variable
#define DW_TAG_arg_variable 0x101
#endif
#ifndef DW_TAG_return_variable
#define DW_TAG_return_variable 0x102
#endif
#ifndef DW_TAG_vector_type
#define DW_TAG_vector_type 0x103
#endif
const int DIFLAG_ARTIFICIAL = 1 << 6;
const int DIFLAG_ISMAINPGM = 1 << 21; // removed in release_90
static int DIFLAG_PURE; // removed in release_80
static int DIFLAG_ELEMENTAL; // removed in release_80
static int DIFLAG_RECURSIVE; // removed in release_80
const int DISPFLAG_LOCALTOUNIT = 1 << 2; // added in release_80
const int DISPFLAG_DEFINITION = 1 << 3; // added in release_80
const int DISPFLAG_OPTIMIZED = 1 << 4; // added in release_80
const int DISPFLAG_PURE = 1 << 5; // added in release_80
const int DISPFLAG_ELEMENTAL = 1 << 6; // added in release_80
const int DISPFLAG_RECURSIVE = 1 << 7; // added in release_80
const int DISPFLAG_MAINSUBPROGRAM = 1 << 8; // added in release_90
typedef struct {
LL_MDRef mdnode; /**< mdnode for block */
int sptr; /**< block sptr */
int startline;
int endline;
int keep;
LL_MDRef *line_mdnodes; /**< mdnodes for block lines */
LL_MDRef null_loc;
} BLKINFO;
typedef struct {
LL_MDRef mdnode;
INSTR_LIST *instr;
int sptr;
} PARAMINFO;
struct sptr_to_mdnode_map {
int sptr;
LL_MDRef mdnode;
struct sptr_to_mdnode_map *next;
};
typedef struct import_entity {
SPTR entity; /**< sptr of pending import entity */
IMPORT_TYPE entity_type; /**< 0 for DECLARATION; 1 for MODULE; 2 for UNIT */
SPTR func; /**< sptr of function import to */
struct import_entity *next; /**< pointer to the next node */
} import_entity;
#define BLK_STACK_SIZE 1024
#define PARAM_STACK_SIZE 1024
struct LL_DebugInfo {
LL_Module *module; /**< Pointer to the containing LL_Module */
LL_MDRef llvm_dbg_sp; /**< List of subprogram mdnodes */
LL_MDRef llvm_dbg_gv; /**< List of global variables mdnodes */
LL_MDRef llvm_dbg_retained; /**< List of retained type mdnodes */
LL_MDRef llvm_dbg_enum; /**< List of enum mdnodes */
LL_MDRef llvm_dbg_imported; /**< List of imported entity mdnodes */
LL_MDRef *llvm_dbg_lv_array; /**< List of formal parameters to routine */
char producer[1024];
LL_MDRef comp_unit_mdnode;
LL_MDRef *file_array;
int file_array_sz;
LL_MDRef cur_subprogram_mdnode;
unsigned cur_subprogram_func_ptr_offset;
LL_MDRef cur_parameters_mdnode;
LL_MDRef cur_module_mdnode;
LL_MDRef cur_cmnblk_mdnode;
int cur_subprogram_lineno;
LL_MDRef cur_subprogram_null_loc;
LL_MDRef cur_line_mdnode;
PARAMINFO param_stack[PARAM_STACK_SIZE];
LL_MDRef *dtype_array;
int dtype_array_sz;
LL_MDRef texture_type_mdnode;
BLKINFO cur_blk;
BLKINFO *blk_tab;
int blk_tab_size;
int blk_idx;
char *cur_module_name;
int param_idx;
int routine_count;
int routine_idx;
struct sptr_to_mdnode_map *sptrs_to_mdnodes;
hashmap_t subroutine_mdnodes;
hashset_t entity_func_added;
import_entity *import_entity_list; /**< list of entities to be imported to func */
bool need_dup_composite_type; /**< indicator of duplicate composite type when needed */
SPTR gbl_var_sptr; /**< current global variable symbol */
LL_MDRef gbl_obj_mdnode; /**< mdnode reference to the global object in STATICS */
LL_MDRef gbl_obj_exp_mdnode; /**< the expression mdnode of the above global object */
unsigned scope_is_global : 1;
};
static LL_MDRef lldbg_emit_modified_type(LL_DebugInfo *, DTYPE, SPTR, int);
static LL_MDRef lldbg_create_module_flag_mdnode(LL_DebugInfo *db, int severity,
char *name, int value);
static LL_MDRef lldbg_create_outlined_parameters_node(LL_DebugInfo *db);
static LL_MDRef lldbg_create_file_mdnode(LL_DebugInfo *db, char *filename,
char *sourcedir, LL_MDRef context,
int index);
static LL_MDRef lldbg_emit_type(LL_DebugInfo *db, DTYPE dtype, SPTR sptr,
int findex, bool is_reference,
bool skip_first_dim,
bool skipDataDependentTypes);
static LL_MDRef lldbg_fwd_local_variable(LL_DebugInfo *db, int sptr, int findex,
int emit_dummy_as_local);
static void lldbg_emit_imported_entity(LL_DebugInfo *db, SPTR entity_sptr,
SPTR func_sptr, IMPORT_TYPE entity_type);
/* ---------------------------------------------------------------------- */
void
InitializeDIFlags(const LL_IRFeatures *feature)
{
#ifdef FLANG_LLVM_EXTENSIONS
if (ll_feature_debug_info_ver70(feature)) {
DIFLAG_PURE = 1 << 27;
DIFLAG_ELEMENTAL = 1 << 28;
DIFLAG_RECURSIVE = 1 << 29;
} else {
DIFLAG_PURE = 1 << 22;
DIFLAG_ELEMENTAL = 1 << 23;
DIFLAG_RECURSIVE = 1 << 24;
}
#else
// do nothing
#endif
}
char *
lldbg_alloc(INT size)
{
char *p = (char *)getitem(LLVM_LONGTERM_AREA, size);
assert(p, "lldbg_alloc(), out of memory", 0, ERR_Fatal);
memset(p, 0, size);
return p;
}
static ISZ_T
lldbg_get_sizeof(int element)
{
ISZ_T sz;
#if defined(FLDSZG)
if (FIELDG(element) && FLDSZG(element))
return FLDSZG(element);
#endif
if (!element || !DTYPEG(element))
return 0;
sz = zsize_of(DTYPEG(element)) * 8;
if (sz < 0)
sz = 0;
return sz;
}
/**
\brief Make an i32 operand with a DWARF tag and possibly a version number.
LLVM 3.6 onwards do not encode the debug info version in the tag field.
*/
static int
make_dwtag(LL_DebugInfo *db, int tag)
{
if (ll_feature_versioned_dw_tag(&db->module->ir))
tag |= db->module->ir.debug_info_version << 16;
return tag;
}
static LL_MDRef
lldbg_create_module_flag_mdnode(LL_DebugInfo *db, int severity, char *name,
int value)
{
LLMD_Builder mdb = llmd_init(db->module);
llmd_add_i32(mdb, severity);
llmd_add_string(mdb, name);
llmd_add_i32(mdb, value);
return llmd_finish(mdb);
}
static LL_MDRef
get_file_mdnode(LL_DebugInfo *db, int index)
{
if (index * 2 < db->file_array_sz)
return db->file_array[index * 2];
return ll_get_md_null();
}
static LL_MDRef
get_filedesc_mdnode(LL_DebugInfo *db, int index)
{
if ((index * 2 + 1) < db->file_array_sz)
return db->file_array[index * 2 + 1];
return ll_get_md_null();
}
static LL_MDRef
lldbg_create_compile_unit_mdnode(LL_DebugInfo *db, int lang_tag, char *filename,
char *sourcedir, char *producer, int main,
int optimized, char *compflags, int vruntime,
LL_MDRef *enum_types_list,
LL_MDRef *retained_types_list,
LL_MDRef *subprograms_list, LL_MDRef *gv_list,
LL_MDRef *imported_entity_list)
{
LLMD_Builder mdb = llmd_init(db->module);
LL_MDRef cur_mdnode;
llmd_set_class(mdb, LL_DICompileUnit);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_compile_unit));
if (ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_i32(mdb, 0); /* Unused field. */
llmd_add_i32(mdb, lang_tag);
llmd_add_string(mdb, filename);
llmd_add_string(mdb, sourcedir);
} else {
LL_MDRef file_mdnode = get_filedesc_mdnode(db, 1);
if (LL_MDREF_IS_NULL(file_mdnode))
file_mdnode = lldbg_create_file_mdnode(db, filename, sourcedir,
ll_get_md_null(), 1);
llmd_add_md(mdb, file_mdnode);
llmd_add_i32(mdb, lang_tag);
}
llmd_add_string(mdb, producer);
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_i1(mdb, main);
llmd_add_i1(mdb, optimized);
llmd_add_string(mdb, compflags);
llmd_add_i32(mdb, vruntime);
*enum_types_list = ll_create_flexible_md_node(db->module);
*retained_types_list = ll_create_flexible_md_node(db->module);
*subprograms_list = ll_create_flexible_md_node(db->module);
*gv_list = ll_create_flexible_md_node(db->module);
*imported_entity_list = ll_create_flexible_md_node(db->module);
if (ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_md(mdb,
ll_get_md_node(db->module, LL_PlainMDNode, enum_types_list, 1));
llmd_add_md(mdb, ll_get_md_node(db->module, LL_PlainMDNode,
retained_types_list, 1));
llmd_add_md(
mdb, ll_get_md_node(db->module, LL_PlainMDNode, subprograms_list, 1));
llmd_add_md(mdb, ll_get_md_node(db->module, LL_PlainMDNode, gv_list, 1));
} else {
llmd_add_md(mdb, *enum_types_list);
llmd_add_md(mdb, *retained_types_list);
if (!ll_feature_subprogram_not_in_cu(&db->module->ir))
llmd_add_md(mdb, *subprograms_list);
llmd_add_md(mdb, *gv_list);
if (ll_feature_subprogram_not_in_cu(&db->module->ir))
llmd_add_i32(mdb, 1); /* emissionMode: FullDebug */
llmd_add_md(mdb, *imported_entity_list);
llmd_add_string(mdb, "");
}
llmd_set_distinct(mdb);
cur_mdnode = llmd_finish(mdb);
ll_extend_named_md_node(db->module, MD_llvm_dbg_cu, cur_mdnode);
return cur_mdnode;
}
static LL_MDRef
lldbg_create_module_mdnode(LL_DebugInfo *db, LL_MDRef _, char *name,
LL_MDRef scope, int lineno)
{
LLMD_Builder mdb;
char *module_name, *pname, *pmname;
unsigned tag = ll_feature_debug_info_pre34(&db->module->ir) ? DW_TAG_namespace
: DW_TAG_module;
if (name && db->cur_module_name && !strcmp(name, db->cur_module_name))
return db->cur_module_mdnode;
mdb = llmd_init(db->module);
module_name = (char *)lldbg_alloc(strlen(name) + 1);
pname = name;
pmname = module_name;
while (*pname != '\0') {
*pmname = tolower(*pname);
pname++;
pmname++;
}
*pmname = '\0'; /* append null char to end of string */
if (ll_feature_no_file_in_namespace(&db->module->ir)) {
// Use the DIModule template
llmd_set_class(mdb, LL_DIModule);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_module)); // tag
llmd_add_md(mdb, scope); // scope
llmd_add_string(mdb, module_name); // name
} else {
llmd_set_class(mdb, LL_DINamespace);
llmd_add_i32(mdb, make_dwtag(db, tag));
if (!ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb, scope);
llmd_add_null(mdb);
llmd_add_string(mdb, module_name);
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb, scope);
llmd_add_i32(mdb, lineno);
}
db->cur_module_name = module_name;
db->cur_module_mdnode = llmd_finish(mdb);
if (flg.debug && ll_feature_no_file_in_namespace(&db->module->ir))
ll_add_module_debug(cpu_llvm_module->module_debug_map, module_name,
db->cur_module_mdnode);
return db->cur_module_mdnode;
}
static LL_MDRef
lldbg_create_file_mdnode(LL_DebugInfo *db, char *filename, char *sourcedir,
LL_MDRef context, int index)
{
LLMD_Builder mdb = llmd_init(db->module);
LL_MDRef cur_mdnode;
llmd_set_class(mdb, LL_DIFile);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_file_type));
if (!ll_feature_debug_info_pre34(&db->module->ir)) {
LLMD_Builder pairmd = llmd_init(db->module);
llmd_set_class(pairmd, LL_DIFile);
llmd_add_string(pairmd, filename);
llmd_add_string(pairmd, sourcedir);
cur_mdnode = llmd_finish(pairmd);
llmd_add_md(mdb, cur_mdnode);
NEEDB(fihb.stg_avail * 2, db->file_array, LL_MDRef, db->file_array_sz,
fihb.stg_avail * 2);
db->file_array[2 * index] = llmd_finish(mdb);
db->file_array[2 * index + 1] = cur_mdnode;
} else {
llmd_add_string(mdb, filename);
llmd_add_string(mdb, sourcedir);
llmd_add_md(mdb, context);
cur_mdnode = llmd_finish(mdb);
NEEDB(fihb.stg_avail * 2, db->file_array, LL_MDRef, db->file_array_sz,
fihb.stg_avail * 2);
db->file_array[2 * index] = cur_mdnode;
db->file_array[2 * index + 1] = ll_get_md_null();
}
return cur_mdnode;
}
/**
* \brief Create a sub-program mdnode and store it in db->cur_subprogram_mdnode
*
* Don't set the function pointer field, but remember where it goes in
* cur_subprogram_func_ptr_offset.
*/
static void
lldbg_create_subprogram_mdnode(
LL_DebugInfo *db, LL_MDRef context, const char *routine,
const char *mips_linkage_name, LL_MDRef def_context, int line,
LL_MDRef type_mdnode, int is_local, int is_definition, int virtuality,
int vindex, int spFlags, int flags, int is_optimized,
LL_MDRef template_param_mdnode, LL_MDRef decl_desc_mdnode,
LL_MDRef lv_list_mdnode, int scope)
{
LLMD_Builder mdb = llmd_init(db->module);
llmd_set_class(mdb, LL_DISubprogram);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_subprogram));
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_i32(mdb, 0);
else
llmd_add_md(mdb, def_context);
llmd_add_md(mdb, context);
llmd_add_string(mdb, routine);
llmd_add_string(mdb, routine);
llmd_add_string(mdb, mips_linkage_name);
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb, def_context);
llmd_add_i32(mdb, line);
llmd_add_md(mdb, type_mdnode);
if (!ll_feature_debug_info_ver90(&db->module->ir)) {
llmd_add_i1(mdb, is_local); // removed in release_90
llmd_add_i1(mdb, is_definition);
}
llmd_add_i32(mdb, virtuality);
llmd_add_i32(mdb, vindex);
llmd_add_null(mdb);
llmd_add_i32(mdb, flags);
if (ll_feature_debug_info_ver90(&db->module->ir)) {
llmd_add_i32(mdb, spFlags);
} else {
llmd_add_i1(mdb, is_optimized);
}
/* The actual function pointer is inserted here later by
* lldbg_set_func_ptr(). */
db->cur_subprogram_func_ptr_offset = llmd_get_nelems(mdb);
llmd_add_null(mdb);
llmd_add_md(mdb, template_param_mdnode);
llmd_add_md(mdb, decl_desc_mdnode);
if (ll_feature_subprogram_not_in_cu(&db->module->ir))
llmd_add_md(mdb, db->comp_unit_mdnode);
/* Add extra layer of indirection before 3.4. */
if (!ll_feature_debug_info_ver70(&db->module->ir)) {
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb,
ll_get_md_node(db->module, LL_PlainMDNode, &lv_list_mdnode, 1));
else
llmd_add_md(mdb, lv_list_mdnode);
}
llmd_add_i32(mdb, scope);
/* Request a distinct mdnode so that it can be updated with a function pointer
* later. */
llmd_set_distinct(mdb);
db->cur_subprogram_mdnode = llmd_finish(mdb);
ll_extend_md_node(db->module, db->llvm_dbg_sp, db->cur_subprogram_mdnode);
}
void
lldbg_set_func_ptr(LL_DebugInfo *db, LL_Value *func_ptr)
{
LL_MDRef mdref = ll_get_md_value(db->module, func_ptr);
ll_update_md_node(db->module, db->cur_subprogram_mdnode,
db->cur_subprogram_func_ptr_offset, mdref);
}
LL_MDRef
lldbg_subprogram(LL_DebugInfo *db)
{
LL_MDRef rv = db->cur_subprogram_mdnode;
db->cur_subprogram_mdnode = (LL_MDRef)0;
return rv;
}
void
lldbg_reset_module(LL_DebugInfo *db)
{
db->cur_module_name = NULL;
db->cur_module_mdnode = ll_get_md_null();
}
static LL_MDRef
lldbg_create_global_variable_mdnode(LL_DebugInfo *db, LL_MDRef context,
const char *display_name, char *name,
char *mips_linkage_name,
LL_MDRef def_context, int line,
LL_MDRef type_mdnode, int is_local,
int is_definition, LL_Value *var_ptr,
int addrspace, int flags, ISZ_T off,
SPTR sptr, LL_MDRef fwd)
{
LLMD_Builder mdb = llmd_init(db->module);
LL_MDRef cur_mdnode;
llmd_set_class(mdb, LL_DIGlobalVariable);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_variable));
llmd_add_i32(mdb, 0);
llmd_add_md(mdb, context);
#ifdef FLANG_LLVM_EXTENSIONS
if (ll_feature_debug_info_ver70(&db->module->ir) &&
!XBIT(183, 0x40000000) &&
(flags & DIFLAG_ARTIFICIAL))
display_name = ""; // Do not expose the name of compiler created variable.
#endif
llmd_add_string(mdb, display_name);
llmd_add_string(mdb, name);
llmd_add_string(mdb, mips_linkage_name);
llmd_add_md(mdb, def_context);
llmd_add_i32(mdb, line);
llmd_add_md(mdb, type_mdnode);
llmd_add_i32(mdb, is_local);
llmd_add_i32(mdb, is_definition);
if (!ll_feature_from_global_to_md(&db->module->ir))
llmd_add_md(mdb, ll_get_md_value(db->module, var_ptr));
#ifdef FLANG_LLVM_EXTENSIONS
if (ll_feature_debug_info_ver70(&db->module->ir))
llmd_add_i32(mdb, flags);
#endif
if (addrspace >= 0)
llmd_add_i32(mdb, addrspace);
if (ll_feature_from_global_to_md(&db->module->ir))
llmd_set_distinct(mdb);
cur_mdnode = ll_finish_variable(mdb, fwd);
if (ll_feature_from_global_to_md(&db->module->ir)) {
LL_MDRef expr_mdnode;
const ISZ_T off0 = ((off >> 27) == 0) ? off : 0;
const unsigned v = lldbg_encode_expression_arg(LL_DW_OP_int, off0);
const unsigned cnt = (off0 > 0) ? 2 : 0;
LLMD_Builder mdb2 = llmd_init(db->module);
llmd_set_class(mdb2, LL_DIGlobalVariableExpression);
llmd_add_md(mdb2, cur_mdnode);
/* Handle the Fortran allocatable array cases. Emit expression mdnode with a
* sigle argument of DW_OP_deref because of using sptr array$p instead of
* sptr array for debugging purpose.
*/
if (ftn_array_need_debug_info(sptr)) {
const unsigned deref = lldbg_encode_expression_arg(LL_DW_OP_deref, 0);
expr_mdnode = lldbg_emit_expression_mdnode(db, 1, deref);
} else
if (ll_feature_use_5_diexpression(&db->module->ir)) {
const unsigned add = lldbg_encode_expression_arg(LL_DW_OP_plus_uconst, 0);
expr_mdnode = lldbg_emit_expression_mdnode(db, cnt, add, v);
} else {
const unsigned add = lldbg_encode_expression_arg(LL_DW_OP_plus, 0);
expr_mdnode = lldbg_emit_expression_mdnode(db, cnt, add, v);
}
llmd_add_md(mdb2, expr_mdnode);
cur_mdnode = llmd_finish(mdb2);
}
ll_extend_md_node(db->module, db->llvm_dbg_gv, cur_mdnode);
return cur_mdnode;
}
static LL_MDRef
lldbg_create_block_mdnode(LL_DebugInfo *db, LL_MDRef routine_context, int line,
int column, int findex, int ID)
{
LLMD_Builder mdb = llmd_init(db->module);
if (!line)
line = 1;
llmd_set_class(mdb, LL_DILexicalBlock);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_lexical_block));
if (!ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb, get_filedesc_mdnode(db, findex));
llmd_add_md(mdb, routine_context);
llmd_add_i32(mdb, line);
llmd_add_i32(mdb, column);
if (ll_feature_debug_info_pre34(&db->module->ir))
llmd_add_md(mdb, get_file_mdnode(db, findex));
llmd_add_i32(mdb, ID);
return llmd_finish(mdb);
}
INLINE static LL_MDRef
lldbg_create_string_type_mdnode(LL_DebugInfo *db, ISZ_T sz, DBLINT64 alignment,
const char *name, int encoding)
{
LLMD_Builder mdb = llmd_init(db->module);
if (ll_feature_has_diextensions(&db->module->ir)) {
llmd_set_class(mdb, LL_DIStringType);
} else {
llmd_set_class(mdb, LL_DIBasicType_string);
}
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_string_type));
llmd_add_string(mdb, name);
llmd_add_i64(mdb, sz);
llmd_add_INT64(mdb, alignment);
if (!ll_feature_has_diextensions(&db->module->ir)) {
llmd_add_i32(mdb, encoding);
}
return llmd_finish(mdb);
}
static LL_MDRef
lldbg_create_basic_type_mdnode(LL_DebugInfo *db, LL_MDRef context,
const char *name, LL_MDRef fileref, int line,
ISZ_T sz, DBLINT64 alignment, DBLINT64 offset,
int flags, int dwarf_encoding)
{
DBLINT64 size;
LLMD_Builder mdb = llmd_init(db->module);
ISZ_2_INT64(sz, size);
llmd_set_class(mdb, LL_DIBasicType);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_base_type));
if (ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_md(mdb, context);
llmd_add_string(mdb, name);
llmd_add_md(mdb, fileref);
} else {
llmd_add_null(mdb);
llmd_add_null(mdb);
llmd_add_string(mdb, name);
}
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_INT64(mdb, offset);
llmd_add_i32(mdb, flags);
llmd_add_i32(mdb, dwarf_encoding);
return llmd_finish(mdb);
}
static LL_MDRef
lldbg_create_pointer_type_mdnode(LL_DebugInfo *db, LL_MDRef context, char *name,
LL_MDRef fileref, int line, ISZ_T sz,
DBLINT64 alignment, DBLINT64 offset, int flags,
LL_MDRef pts_to)
{
DBLINT64 size;
LLMD_Builder mdb = llmd_init(db->module);
ISZ_2_INT64(sz, size);
llmd_set_class(mdb, LL_DIDerivedType);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_pointer_type));
if (!ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_null(mdb);
llmd_add_null(mdb);
llmd_add_string(mdb, name);
} else {
llmd_add_md(mdb, context);
llmd_add_string(mdb, name);
llmd_add_md(mdb, fileref);
}
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_INT64(mdb, offset);
llmd_add_i32(mdb, flags);
llmd_add_md(mdb, pts_to);
return llmd_finish(mdb);
}
static LL_MDRef
lldbg_create_ftn_array_type_mdnode(LL_DebugInfo *db, LL_MDRef context, int line,
ISZ_T sz, DBLINT64 alignment, LL_MDRef eleTy,
LL_MDRef subscripts)
{
LLMD_Builder mdb = llmd_init(db->module);
llmd_set_class(mdb, LL_DIFortranArrayType);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_array_type));
llmd_add_md(mdb, context);
llmd_add_i32(mdb, line);
llmd_add_i64(mdb, sz);
llmd_add_INT64(mdb, alignment);
llmd_add_md(mdb, eleTy);
llmd_add_md(mdb, subscripts);
return llmd_finish(mdb);
}
/**
\brief Create an array type, \c DW_TAG_array_type
\param db
\param context
\param line line number
\param sz size of array, must be in bits
\param alignment alignment of array
\param pts_to
\param subscripts
*/
static LL_MDRef
lldbg_create_array_type_mdnode(LL_DebugInfo *db, LL_MDRef context, int line,
ISZ_T sz, DBLINT64 alignment, LL_MDRef pts_to,
LL_MDRef subscripts)
{
DBLINT64 size;
LLMD_Builder mdb = llmd_init(db->module);
ISZ_2_INT64(sz, size);
llmd_set_class(mdb, LL_DICompositeType);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_array_type));
if (!ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_null(mdb);
llmd_add_null(mdb);
llmd_add_string(mdb, "");
} else {
llmd_add_md(mdb, context);
llmd_add_string(mdb, "");
llmd_add_md(mdb, context);
}
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_i32(mdb, 0);
llmd_add_i32(mdb, 0);
llmd_add_md(mdb, pts_to);
llmd_add_md(mdb, subscripts);
llmd_add_i32(mdb, 0);
if (ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_i32(mdb, 0);
} else {
llmd_add_null(mdb);
llmd_add_null(mdb);
}
return llmd_finish(mdb);
}
static LL_MDRef
lldbg_create_aggregate_type_mdnode(LL_DebugInfo *db, int dw_tag,
LL_MDRef context, const char *name,
LL_MDRef fileref, int line, ISZ_T sz,
DBLINT64 alignment, int flags, LL_MDRef members,
int runtime)
{
DBLINT64 size;
LLMD_Builder mdb = llmd_init(db->module);
ISZ_2_INT64(sz, size);
llmd_set_class(mdb, LL_DICompositeType);
llmd_add_i32(mdb, make_dwtag(db, dw_tag));
if (!ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_md(mdb, fileref);
llmd_add_null(mdb); /* Here should be non compile unit scope */
llmd_add_string(mdb, name);
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_i32(mdb, 0);
llmd_add_i32(mdb, flags);
llmd_add_null(mdb); /* Derived from ? */
llmd_add_md(mdb, members);
llmd_add_i32(mdb, runtime);
llmd_add_null(mdb); /* Virtual table holder ? */
llmd_add_null(mdb);
llmd_add_null(mdb); /* Unique identifier ? */
} else {
llmd_add_md(mdb, context);
llmd_add_string(mdb, name);
llmd_add_md(mdb, fileref);
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_i32(mdb, 0);
llmd_add_i32(mdb, flags);
llmd_add_i32(mdb, 0);
llmd_add_md(mdb, members);
llmd_add_i32(mdb, runtime);
llmd_add_i32(mdb, 0);
}
return llmd_finish(mdb);
}
static LL_MDRef
lldbg_create_structure_type_mdnode(LL_DebugInfo *db, LL_MDRef context,
const char *name, LL_MDRef fileref, int line,
ISZ_T sz, DBLINT64 alignment, int flags,
LL_MDRef members, int runtime)
{
return lldbg_create_aggregate_type_mdnode(db, DW_TAG_structure_type, context,
name, fileref, line, sz, alignment,
flags, members, runtime);
}
static LL_MDRef
lldbg_create_union_type_mdnode(LL_DebugInfo *db, LL_MDRef context,
const char *name, LL_MDRef fileref, int line,
ISZ_T sz, DBLINT64 alignment, int flags,
LL_MDRef members, int runtime)
{
return lldbg_create_aggregate_type_mdnode(db, DW_TAG_union_type, context,
name, fileref, line, sz, alignment,
flags, members, runtime);
}
static LL_MDRef
lldbg_create_member_mdnode(LL_DebugInfo *db, LL_MDRef fileref,
LL_MDRef parent_mdnode, const char *name, int line,
ISZ_T sz, DBLINT64 alignment, DBLINT64 offset, int flags,
LL_MDRef type_mdnode, LL_MDRef fwd)
{
DBLINT64 size;
LLMD_Builder mdb = llmd_init(db->module);
ISZ_2_INT64(sz, size);
llmd_set_class(mdb, LL_DIDerivedType);
llmd_add_i32(mdb, make_dwtag(db, DW_TAG_member));
llmd_add_md(mdb, fileref);
if (ll_feature_debug_info_pre34(&db->module->ir)) {
llmd_add_string(mdb, name);
llmd_add_md(mdb, fileref);
} else {
llmd_add_md(mdb, parent_mdnode);
llmd_add_string(mdb, name);
}
llmd_add_i32(mdb, line);
llmd_add_INT64(mdb, size);
llmd_add_INT64(mdb, alignment);
llmd_add_INT64(mdb, offset);
llmd_add_i32(mdb, flags);
llmd_add_md(mdb, type_mdnode);
return ll_finish_variable(mdb, fwd);
}
static void
lldbg_create_aggregate_members_type(LL_DebugInfo *db, SPTR first, int findex,
LL_MDRef file_mdnode,
LL_MDRef members_mdnode,
LL_MDRef parent_mdnode)
{
LL_MDRef member_mdnode, member_type_mdnode, fwd;
ISZ_T sz;
DBLINT64 align, offset;
SPTR element, member;
DTYPE elem_dtype;
hash_data_t val;
int skip_elem_num = 0;
bool is_desc_member = false;
bool contains_allocatable = false;
SPTR base_sptr = SPTR_NULL;
char* prefix = NULL;
if (!ll_feature_debug_info_pre34(&db->module->ir))
file_mdnode = get_filedesc_mdnode(db, findex);
for (element = first; element > NOSYM; element = SYMLKG(element)) {
if (CCSYMG(element))
continue;
if (SCG(element) == SC_BASED && POINTERG(element)) {
/* member syms linked list could contain descriptor(s):
* case #1: array type sym "foo" followed by "foo$p", "foo$o", "foo$sd";
* case #2: structure type sym "zar" followed by "zar$p", "zar$td";
* case #3: pointer type sym "bar" followed by "bar$p".
*/
base_sptr = element;
if (ALLOCATTRG(element) && SDSCG(element)) {
if (db->gbl_var_sptr) {
contains_allocatable = true;
db->need_dup_composite_type |= true;
}
} else {
element = SYMLKG(element);
assert(element > NOSYM,
"lldbg_create_aggregate_members_type: element not exists",
element, ERR_Fatal);
is_desc_member = true;
db->need_dup_composite_type = false;
}
}
elem_dtype = DTYPEG(element);
sz = lldbg_get_sizeof(element);
align[1] = ((alignment(elem_dtype) + 1) * 8);
align[0] = 0;
offset[1] = ((ADDRESSG(element)) * 8);
offset[0] = 0;
member_type_mdnode =
lldbg_emit_type(db, elem_dtype, element, findex, false, false, false);
if (hashmap_lookup(db->module->mdnodes_fwdvars, INT2HKEY(element), &val)) {
fwd = (LL_MDRef)(unsigned long)val;
hashmap_erase(db->module->mdnodes_fwdvars, INT2HKEY(element), NULL);
} else {
fwd = ll_get_md_null();
}
member = element;
if (is_desc_member) {
member = base_sptr;
is_desc_member = false;
}
if (contains_allocatable) {
db->need_dup_composite_type |= true;
}
if (base_sptr && SDSCG(element)) {
/* for case #1 and case #2, sdsc always appears as the last descriptor.
* skip all of the subsequent descriptors.
*/
element = SDSCG(element);
base_sptr = SPTR_NULL;
}
member_mdnode = lldbg_create_member_mdnode(
db, file_mdnode, parent_mdnode, CCSYMG(member) ? "" : SYMNAME(member),
0, sz, align, offset, 0, member_type_mdnode, fwd);
ll_extend_md_node(db->module, members_mdnode, member_mdnode);
}
}
static bool
map_sptr_to_mdnode(LL_MDRef *mdnode, LL_DebugInfo *db, int sptr)
{
struct sptr_to_mdnode_map *map = db->sptrs_to_mdnodes;
if (sptr > NOSYM) {
for (; map != NULL; map = map->next) {
if (map->sptr == sptr) {
if (mdnode != NULL)
*mdnode = map->mdnode;
return true;
}
}
}
return false;
}
/**
\brief Fill in extra data about a symbol
Probes any debug symbol information that may have been saved by the front-end
in order to generate a good display name and proper namespace or class scope
for a symbol. Falls back to the symbol table name and the compile unit's
outermost scope if better symbolic information cannot be found.
*/
static void
get_extra_info_for_sptr(const char **display_name, LL_MDRef *scope_mdnode,
LL_MDRef *type_mdnode, LL_DebugInfo *db, int sptr)
{
*display_name = SYMNAME(sptr);
if (scope_mdnode != NULL) {
if (db->cur_cmnblk_mdnode != ll_get_md_null())
*scope_mdnode = db->cur_cmnblk_mdnode;
else
if (db->cur_subprogram_mdnode != ll_get_md_null())
*scope_mdnode = db->cur_subprogram_mdnode;
else if (db->cur_module_mdnode != ll_get_md_null() &&
((STYPEG(sptr) != ST_ENTRY) ||
(STYPEG(sptr) == ST_ENTRY && INMODULEG(sptr))))
*scope_mdnode = db->cur_module_mdnode;
else
*scope_mdnode = lldbg_emit_compile_unit(db);
}
}
static LL_MDRef
lldbg_create_enumeration_type_mdnode(LL_DebugInfo *db, LL_MDRef context,
char *name, LL_MDRef fileref, int line,
ISZ_T sz, DBLINT64 alignment,
LL_MDRef elements)
{
return lldbg_create_aggregate_type_mdnode(
db, DW_TAG_enumeration_type, context, name, fileref, line, sz, alignment,
/*flags=*/0, elements, /*runtime=*/0);
}
static LL_MDRef
lldbg_create_enumerator_mdnode(LL_DebugInfo *db, int sptr, DBLINT64 value)
{