@@ -106,59 +106,8 @@ unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
106106 Asm->OutStreamer ->hasRawTextSupport () ? 0 : getUniqueID ());
107107}
108108
109- DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE (
110- const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
111- // Check for pre-existence.
112- if (DIE *Die = getDIE (GV))
113- return Die;
114-
115- assert (GV);
116-
117- auto *GVContext = GV->getScope ();
118- auto *GTy = DD->resolve (GV->getType ());
119-
120- // Construct the context before querying for the existence of the DIE in
121- // case such construction creates the DIE.
122- DIE *ContextDIE = getOrCreateContextDIE (GVContext);
123-
124- // Add to map.
125- DIE *VariableDIE = &createAndAddDIE (GV->getTag (), *ContextDIE, GV);
126- DIScope *DeclContext;
127- if (auto *SDMDecl = GV->getStaticDataMemberDeclaration ()) {
128- DeclContext = resolve (SDMDecl->getScope ());
129- assert (SDMDecl->isStaticMember () && " Expected static member decl" );
130- assert (GV->isDefinition ());
131- // We need the declaration DIE that is in the static member's class.
132- DIE *VariableSpecDIE = getOrCreateStaticMemberDIE (SDMDecl);
133- addDIEEntry (*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
134- // If the global variable's type is different from the one in the class
135- // member type, assume that it's more specific and also emit it.
136- if (GTy != DD->resolve (SDMDecl->getBaseType ()))
137- addType (*VariableDIE, GTy);
138- } else {
139- DeclContext = GV->getScope ();
140- // Add name and type.
141- addString (*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName ());
142- addType (*VariableDIE, GTy);
143-
144- // Add scoping info.
145- if (!GV->isLocalToUnit ())
146- addFlag (*VariableDIE, dwarf::DW_AT_external);
147-
148- // Add line number info.
149- addSourceLine (*VariableDIE, GV);
150- }
151-
152- if (!GV->isDefinition ())
153- addFlag (*VariableDIE, dwarf::DW_AT_declaration);
154- else
155- addGlobalName (GV->getName (), *VariableDIE, DeclContext);
156-
157- if (uint32_t AlignInBytes = GV->getAlignInBytes ())
158- addUInt (*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
159- AlignInBytes);
160-
161- // Add location.
109+ void DwarfCompileUnit::addLocationAttribute (
110+ DIE *ToDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
162111 bool addToAccelTable = false ;
163112 DIELoc *Loc = nullptr ;
164113 std::unique_ptr<DIEDwarfExpression> DwarfExpr;
@@ -171,7 +120,7 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
171120 // DW_AT_const_value(X).
172121 if (GlobalExprs.size () == 1 && Expr && Expr->isConstant ()) {
173122 addToAccelTable = true ;
174- addConstantValue (*VariableDIE , /* Unsigned=*/ true , Expr->getElement (1 ));
123+ addConstantValue (*ToDIE , /* Unsigned=*/ true , Expr->getElement (1 ));
175124 break ;
176125 }
177126
@@ -231,19 +180,101 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
231180 }
232181 }
233182 if (Loc)
234- addBlock (*VariableDIE , dwarf::DW_AT_location, DwarfExpr->finalize ());
183+ addBlock (*ToDIE , dwarf::DW_AT_location, DwarfExpr->finalize ());
235184
236185 if (DD->useAllLinkageNames ())
237- addLinkageName (*VariableDIE , GV->getLinkageName ());
186+ addLinkageName (*ToDIE , GV->getLinkageName ());
238187
239188 if (addToAccelTable) {
240- DD->addAccelName (GV->getName (), *VariableDIE );
189+ DD->addAccelName (GV->getName (), *ToDIE );
241190
242191 // If the linkage name is different than the name, go ahead and output
243192 // that as well into the name table.
244193 if (GV->getLinkageName () != " " && GV->getName () != GV->getLinkageName ())
245- DD->addAccelName (GV->getLinkageName (), *VariableDIE);
194+ DD->addAccelName (GV->getLinkageName (), *ToDIE);
195+ }
196+ }
197+
198+ DIE *DwarfCompileUnit::getOrCreateCommonBlock (
199+ const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) {
200+ // Construct the context before querying for the existence of the DIE in case
201+ // such construction creates the DIE.
202+ DIE *ContextDIE = getOrCreateContextDIE (CB->getScope ());
203+
204+ if (DIE *NDie = getDIE (CB))
205+ return NDie;
206+ DIE &NDie = createAndAddDIE (dwarf::DW_TAG_common_block, *ContextDIE, CB);
207+ StringRef Name = CB->getName ().empty () ? " _BLNK_" : CB->getName ();
208+ addString (NDie, dwarf::DW_AT_name, Name);
209+ addGlobalName (Name, NDie, CB->getScope ());
210+ if (CB->getFile ())
211+ addSourceLine (NDie, CB->getLineNo (), CB->getFile ()->getFilename (),
212+ CB->getFile ()->getDirectory ());
213+ if (DIGlobalVariable *V = CB->getDecl ())
214+ getCU ().addLocationAttribute (&NDie, V, GlobalExprs);
215+ if (uint32_t AlignInBits = CB->getAlignInBits ()) {
216+ uint32_t AlignInBytes = AlignInBits >> 3 ;
217+ addUInt (NDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, AlignInBytes);
246218 }
219+ return &NDie;
220+ }
221+
222+ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE (
223+ const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
224+ // Check for pre-existence.
225+ if (DIE *Die = getDIE (GV))
226+ return Die;
227+
228+ assert (GV);
229+
230+ auto *GVContext = GV->getScope ();
231+ auto *GTy = DD->resolve (GV->getType ());
232+
233+ // Construct the context before querying for the existence of the DIE in
234+ // case such construction creates the DIE.
235+ auto *CB = dyn_cast<DICommonBlock>(GVContext);
236+ DIE *ContextDIE = CB ? getOrCreateCommonBlock (CB, GlobalExprs)
237+ : getOrCreateContextDIE (GVContext);
238+
239+ // Add to map.
240+ DIE *VariableDIE = &createAndAddDIE (GV->getTag (), *ContextDIE, GV);
241+ DIScope *DeclContext;
242+ if (auto *SDMDecl = GV->getStaticDataMemberDeclaration ()) {
243+ DeclContext = resolve (SDMDecl->getScope ());
244+ assert (SDMDecl->isStaticMember () && " Expected static member decl" );
245+ assert (GV->isDefinition ());
246+ // We need the declaration DIE that is in the static member's class.
247+ DIE *VariableSpecDIE = getOrCreateStaticMemberDIE (SDMDecl);
248+ addDIEEntry (*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
249+ // If the global variable's type is different from the one in the class
250+ // member type, assume that it's more specific and also emit it.
251+ if (GTy != DD->resolve (SDMDecl->getBaseType ()))
252+ addType (*VariableDIE, GTy);
253+ } else {
254+ DeclContext = GV->getScope ();
255+ // Add name and type.
256+ addString (*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName ());
257+ addType (*VariableDIE, GTy);
258+
259+ // Add scoping info.
260+ if (!GV->isLocalToUnit ())
261+ addFlag (*VariableDIE, dwarf::DW_AT_external);
262+
263+ // Add line number info.
264+ addSourceLine (*VariableDIE, GV);
265+ }
266+
267+ if (!GV->isDefinition ())
268+ addFlag (*VariableDIE, dwarf::DW_AT_declaration);
269+ else
270+ addGlobalName (GV->getName (), *VariableDIE, DeclContext);
271+
272+ if (uint32_t AlignInBytes = GV->getAlignInBytes ())
273+ addUInt (*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
274+ AlignInBytes);
275+
276+ // Add location.
277+ addLocationAttribute (VariableDIE, GV, GlobalExprs);
247278
248279 return VariableDIE;
249280}
0 commit comments