DIBuilder.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the DIBuilder.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/DIBuilder.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/DebugInfo.h"
  17. #include "llvm/IntrinsicInst.h"
  18. #include "llvm/Module.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/Dwarf.h"
  21. using namespace llvm;
  22. using namespace llvm::dwarf;
  23. static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
  24. assert((Tag & LLVMDebugVersionMask) == 0 &&
  25. "Tag too large for debug encoding!");
  26. return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
  27. }
  28. DIBuilder::DIBuilder(Module &m)
  29. : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
  30. TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
  31. ValueFn(0)
  32. {}
  33. /// finalize - Construct any deferred debug info descriptors.
  34. void DIBuilder::finalize() {
  35. DIArray Enums = getOrCreateArray(AllEnumTypes);
  36. DIType(TempEnumTypes).replaceAllUsesWith(Enums);
  37. DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
  38. DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
  39. DIArray SPs = getOrCreateArray(AllSubprograms);
  40. DIType(TempSubprograms).replaceAllUsesWith(SPs);
  41. for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
  42. DISubprogram SP(SPs.getElement(i));
  43. SmallVector<Value *, 4> Variables;
  44. if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
  45. for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
  46. Variables.push_back(NMD->getOperand(ii));
  47. NMD->eraseFromParent();
  48. }
  49. if (MDNode *Temp = SP.getVariablesNodes()) {
  50. DIArray AV = getOrCreateArray(Variables);
  51. DIType(Temp).replaceAllUsesWith(AV);
  52. }
  53. }
  54. DIArray GVs = getOrCreateArray(AllGVs);
  55. DIType(TempGVs).replaceAllUsesWith(GVs);
  56. }
  57. /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
  58. /// N.
  59. static MDNode *getNonCompileUnitScope(MDNode *N) {
  60. if (DIDescriptor(N).isCompileUnit())
  61. return NULL;
  62. return N;
  63. }
  64. /// createCompileUnit - A CompileUnit provides an anchor for all debugging
  65. /// information generated during this instance of compilation.
  66. void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
  67. StringRef Directory, StringRef Producer,
  68. bool isOptimized, StringRef Flags,
  69. unsigned RunTimeVer) {
  70. assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
  71. (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
  72. "Invalid Language tag");
  73. assert(!Filename.empty() &&
  74. "Unable to create compile unit without filename");
  75. Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
  76. TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
  77. Value *THElts[] = { TempEnumTypes };
  78. MDNode *EnumHolder = MDNode::get(VMContext, THElts);
  79. TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
  80. Value *TRElts[] = { TempRetainTypes };
  81. MDNode *RetainHolder = MDNode::get(VMContext, TRElts);
  82. TempSubprograms = MDNode::getTemporary(VMContext, TElts);
  83. Value *TSElts[] = { TempSubprograms };
  84. MDNode *SPHolder = MDNode::get(VMContext, TSElts);
  85. TempGVs = MDNode::getTemporary(VMContext, TElts);
  86. Value *TVElts[] = { TempGVs };
  87. MDNode *GVHolder = MDNode::get(VMContext, TVElts);
  88. Value *Elts[] = {
  89. GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
  90. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  91. ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
  92. MDString::get(VMContext, Filename),
  93. MDString::get(VMContext, Directory),
  94. MDString::get(VMContext, Producer),
  95. // Deprecate isMain field.
  96. ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
  97. ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
  98. MDString::get(VMContext, Flags),
  99. ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
  100. EnumHolder,
  101. RetainHolder,
  102. SPHolder,
  103. GVHolder
  104. };
  105. TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
  106. // Create a named metadata so that it is easier to find cu in a module.
  107. NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
  108. NMD->addOperand(TheCU);
  109. }
  110. /// createFile - Create a file descriptor to hold debugging information
  111. /// for a file.
  112. DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
  113. assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
  114. assert(!Filename.empty() && "Unable to create file without name");
  115. Value *Elts[] = {
  116. GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
  117. MDString::get(VMContext, Filename),
  118. MDString::get(VMContext, Directory),
  119. NULL // TheCU
  120. };
  121. return DIFile(MDNode::get(VMContext, Elts));
  122. }
  123. /// createEnumerator - Create a single enumerator value.
  124. DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
  125. assert(!Name.empty() && "Unable to create enumerator without name");
  126. Value *Elts[] = {
  127. GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
  128. MDString::get(VMContext, Name),
  129. ConstantInt::get(Type::getInt64Ty(VMContext), Val)
  130. };
  131. return DIEnumerator(MDNode::get(VMContext, Elts));
  132. }
  133. /// createNullPtrType - Create C++0x nullptr type.
  134. DIType DIBuilder::createNullPtrType(StringRef Name) {
  135. assert(!Name.empty() && "Unable to create type without name");
  136. // nullptr is encoded in DIBasicType format. Line number, filename,
  137. // ,size, alignment, offset and flags are always empty here.
  138. Value *Elts[] = {
  139. GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
  140. NULL, //TheCU,
  141. MDString::get(VMContext, Name),
  142. NULL, // Filename
  143. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  144. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  145. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  146. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  147. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
  148. ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
  149. };
  150. return DIType(MDNode::get(VMContext, Elts));
  151. }
  152. /// createBasicType - Create debugging information entry for a basic
  153. /// type, e.g 'char'.
  154. DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
  155. uint64_t AlignInBits,
  156. unsigned Encoding) {
  157. assert(!Name.empty() && "Unable to create type without name");
  158. // Basic types are encoded in DIBasicType format. Line number, filename,
  159. // offset and flags are always empty here.
  160. Value *Elts[] = {
  161. GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
  162. NULL, //TheCU,
  163. MDString::get(VMContext, Name),
  164. NULL, // Filename
  165. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  166. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  167. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  168. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  169. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
  170. ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
  171. };
  172. return DIType(MDNode::get(VMContext, Elts));
  173. }
  174. /// createQualifiedType - Create debugging information entry for a qualified
  175. /// type, e.g. 'const int'.
  176. DIType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
  177. // Qualified types are encoded in DIDerivedType format.
  178. Value *Elts[] = {
  179. GetTagConstant(VMContext, Tag),
  180. NULL, //TheCU,
  181. MDString::get(VMContext, StringRef()), // Empty name.
  182. NULL, // Filename
  183. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  184. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  185. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  186. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  187. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
  188. FromTy
  189. };
  190. return DIType(MDNode::get(VMContext, Elts));
  191. }
  192. /// createPointerType - Create debugging information entry for a pointer.
  193. DIType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
  194. uint64_t AlignInBits, StringRef Name) {
  195. // Pointer types are encoded in DIDerivedType format.
  196. Value *Elts[] = {
  197. GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
  198. NULL, //TheCU,
  199. MDString::get(VMContext, Name),
  200. NULL, // Filename
  201. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  202. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  203. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  204. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  205. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
  206. PointeeTy
  207. };
  208. return DIType(MDNode::get(VMContext, Elts));
  209. }
  210. /// createReferenceType - Create debugging information entry for a reference
  211. /// type.
  212. DIType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
  213. assert(RTy.Verify() && "Unable to create reference type");
  214. // References are encoded in DIDerivedType format.
  215. Value *Elts[] = {
  216. GetTagConstant(VMContext, Tag),
  217. NULL, // TheCU,
  218. NULL, // Name
  219. NULL, // Filename
  220. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  221. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  222. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  223. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  224. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
  225. RTy
  226. };
  227. return DIType(MDNode::get(VMContext, Elts));
  228. }
  229. /// createTypedef - Create debugging information entry for a typedef.
  230. DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
  231. unsigned LineNo, DIDescriptor Context) {
  232. // typedefs are encoded in DIDerivedType format.
  233. assert(Ty.Verify() && "Invalid typedef type!");
  234. Value *Elts[] = {
  235. GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
  236. getNonCompileUnitScope(Context),
  237. MDString::get(VMContext, Name),
  238. File,
  239. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
  240. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  241. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  242. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  243. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
  244. Ty
  245. };
  246. return DIType(MDNode::get(VMContext, Elts));
  247. }
  248. /// createFriend - Create debugging information entry for a 'friend'.
  249. DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
  250. // typedefs are encoded in DIDerivedType format.
  251. assert(Ty.Verify() && "Invalid type!");
  252. assert(FriendTy.Verify() && "Invalid friend type!");
  253. Value *Elts[] = {
  254. GetTagConstant(VMContext, dwarf::DW_TAG_friend),
  255. Ty,
  256. NULL, // Name
  257. Ty.getFile(),
  258. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  259. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  260. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  261. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
  262. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
  263. FriendTy
  264. };
  265. return DIType(MDNode::get(VMContext, Elts));
  266. }
  267. /// createInheritance - Create debugging information entry to establish
  268. /// inheritance relationship between two types.
  269. DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
  270. uint64_t BaseOffset, unsigned Flags) {
  271. assert(Ty.Verify() && "Unable to create inheritance");
  272. // TAG_inheritance is encoded in DIDerivedType format.
  273. Value *Elts[] = {
  274. GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
  275. Ty,
  276. NULL, // Name
  277. Ty.getFile(),
  278. ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
  279. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
  280. ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
  281. ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
  282. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  283. BaseTy
  284. };
  285. return DIType(MDNode::get(VMContext, Elts));
  286. }
  287. /// createMemberType - Create debugging information entry for a member.
  288. DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
  289. DIFile File, unsigned LineNumber,
  290. uint64_t SizeInBits, uint64_t AlignInBits,
  291. uint64_t OffsetInBits, unsigned Flags,
  292. DIType Ty) {
  293. // TAG_member is encoded in DIDerivedType format.
  294. Value *Elts[] = {
  295. GetTagConstant(VMContext, dwarf::DW_TAG_member),
  296. getNonCompileUnitScope(Scope),
  297. MDString::get(VMContext, Name),
  298. File,
  299. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  300. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  301. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  302. ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
  303. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  304. Ty
  305. };
  306. return DIType(MDNode::get(VMContext, Elts));
  307. }
  308. /// createObjCIVar - Create debugging information entry for Objective-C
  309. /// instance variable.
  310. DIType DIBuilder::createObjCIVar(StringRef Name,
  311. DIFile File, unsigned LineNumber,
  312. uint64_t SizeInBits, uint64_t AlignInBits,
  313. uint64_t OffsetInBits, unsigned Flags,
  314. DIType Ty, StringRef PropertyName,
  315. StringRef GetterName, StringRef SetterName,
  316. unsigned PropertyAttributes) {
  317. // TAG_member is encoded in DIDerivedType format.
  318. Value *Elts[] = {
  319. GetTagConstant(VMContext, dwarf::DW_TAG_member),
  320. getNonCompileUnitScope(File),
  321. MDString::get(VMContext, Name),
  322. File,
  323. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  324. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  325. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  326. ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
  327. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  328. Ty,
  329. MDString::get(VMContext, PropertyName),
  330. MDString::get(VMContext, GetterName),
  331. MDString::get(VMContext, SetterName),
  332. ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
  333. };
  334. return DIType(MDNode::get(VMContext, Elts));
  335. }
  336. /// createObjCIVar - Create debugging information entry for Objective-C
  337. /// instance variable.
  338. DIType DIBuilder::createObjCIVar(StringRef Name,
  339. DIFile File, unsigned LineNumber,
  340. uint64_t SizeInBits, uint64_t AlignInBits,
  341. uint64_t OffsetInBits, unsigned Flags,
  342. DIType Ty, MDNode *PropertyNode) {
  343. // TAG_member is encoded in DIDerivedType format.
  344. Value *Elts[] = {
  345. GetTagConstant(VMContext, dwarf::DW_TAG_member),
  346. getNonCompileUnitScope(File),
  347. MDString::get(VMContext, Name),
  348. File,
  349. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  350. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  351. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  352. ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
  353. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  354. Ty,
  355. PropertyNode
  356. };
  357. return DIType(MDNode::get(VMContext, Elts));
  358. }
  359. /// createObjCProperty - Create debugging information entry for Objective-C
  360. /// property.
  361. DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
  362. DIFile File, unsigned LineNumber,
  363. StringRef GetterName,
  364. StringRef SetterName,
  365. unsigned PropertyAttributes,
  366. DIType Ty) {
  367. Value *Elts[] = {
  368. GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
  369. MDString::get(VMContext, Name),
  370. File,
  371. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  372. MDString::get(VMContext, GetterName),
  373. MDString::get(VMContext, SetterName),
  374. ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
  375. Ty
  376. };
  377. return DIObjCProperty(MDNode::get(VMContext, Elts));
  378. }
  379. /// createTemplateTypeParameter - Create debugging information for template
  380. /// type parameter.
  381. DITemplateTypeParameter
  382. DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
  383. DIType Ty, MDNode *File, unsigned LineNo,
  384. unsigned ColumnNo) {
  385. Value *Elts[] = {
  386. GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
  387. getNonCompileUnitScope(Context),
  388. MDString::get(VMContext, Name),
  389. Ty,
  390. File,
  391. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
  392. ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
  393. };
  394. return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
  395. }
  396. /// createTemplateValueParameter - Create debugging information for template
  397. /// value parameter.
  398. DITemplateValueParameter
  399. DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
  400. DIType Ty, uint64_t Val,
  401. MDNode *File, unsigned LineNo,
  402. unsigned ColumnNo) {
  403. Value *Elts[] = {
  404. GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
  405. getNonCompileUnitScope(Context),
  406. MDString::get(VMContext, Name),
  407. Ty,
  408. ConstantInt::get(Type::getInt64Ty(VMContext), Val),
  409. File,
  410. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
  411. ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
  412. };
  413. return DITemplateValueParameter(MDNode::get(VMContext, Elts));
  414. }
  415. /// createClassType - Create debugging information entry for a class.
  416. DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
  417. DIFile File, unsigned LineNumber,
  418. uint64_t SizeInBits, uint64_t AlignInBits,
  419. uint64_t OffsetInBits, unsigned Flags,
  420. DIType DerivedFrom, DIArray Elements,
  421. MDNode *VTableHolder,
  422. MDNode *TemplateParams) {
  423. // TAG_class_type is encoded in DICompositeType format.
  424. Value *Elts[] = {
  425. GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
  426. getNonCompileUnitScope(Context),
  427. MDString::get(VMContext, Name),
  428. File,
  429. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  430. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  431. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  432. ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
  433. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  434. DerivedFrom,
  435. Elements,
  436. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  437. VTableHolder,
  438. TemplateParams
  439. };
  440. return DIType(MDNode::get(VMContext, Elts));
  441. }
  442. /// createStructType - Create debugging information entry for a struct.
  443. DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
  444. DIFile File, unsigned LineNumber,
  445. uint64_t SizeInBits, uint64_t AlignInBits,
  446. unsigned Flags, DIArray Elements,
  447. unsigned RunTimeLang) {
  448. // TAG_structure_type is encoded in DICompositeType format.
  449. Value *Elts[] = {
  450. GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
  451. getNonCompileUnitScope(Context),
  452. MDString::get(VMContext, Name),
  453. File,
  454. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  455. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  456. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  457. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  458. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  459. NULL,
  460. Elements,
  461. ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
  462. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  463. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  464. };
  465. return DIType(MDNode::get(VMContext, Elts));
  466. }
  467. /// createUnionType - Create debugging information entry for an union.
  468. DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
  469. DIFile File,
  470. unsigned LineNumber, uint64_t SizeInBits,
  471. uint64_t AlignInBits, unsigned Flags,
  472. DIArray Elements, unsigned RunTimeLang) {
  473. // TAG_union_type is encoded in DICompositeType format.
  474. Value *Elts[] = {
  475. GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
  476. getNonCompileUnitScope(Scope),
  477. MDString::get(VMContext, Name),
  478. File,
  479. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  480. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  481. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  482. ConstantInt::get(Type::getInt64Ty(VMContext), 0),
  483. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  484. NULL,
  485. Elements,
  486. ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
  487. Constant::getNullValue(Type::getInt32Ty(VMContext))
  488. };
  489. return DIType(MDNode::get(VMContext, Elts));
  490. }
  491. /// createSubroutineType - Create subroutine type.
  492. DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
  493. // TAG_subroutine_type is encoded in DICompositeType format.
  494. Value *Elts[] = {
  495. GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
  496. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  497. MDString::get(VMContext, ""),
  498. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  499. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  500. ConstantInt::get(Type::getInt64Ty(VMContext), 0),
  501. ConstantInt::get(Type::getInt64Ty(VMContext), 0),
  502. ConstantInt::get(Type::getInt64Ty(VMContext), 0),
  503. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  504. NULL,
  505. ParameterTypes,
  506. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  507. Constant::getNullValue(Type::getInt32Ty(VMContext))
  508. };
  509. return DIType(MDNode::get(VMContext, Elts));
  510. }
  511. /// createEnumerationType - Create debugging information entry for an
  512. /// enumeration.
  513. DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
  514. DIFile File, unsigned LineNumber,
  515. uint64_t SizeInBits,
  516. uint64_t AlignInBits,
  517. DIArray Elements,
  518. DIType ClassType) {
  519. // TAG_enumeration_type is encoded in DICompositeType format.
  520. Value *Elts[] = {
  521. GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
  522. getNonCompileUnitScope(Scope),
  523. MDString::get(VMContext, Name),
  524. File,
  525. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  526. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  527. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  528. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  529. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  530. ClassType,
  531. Elements,
  532. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  533. Constant::getNullValue(Type::getInt32Ty(VMContext))
  534. };
  535. MDNode *Node = MDNode::get(VMContext, Elts);
  536. AllEnumTypes.push_back(Node);
  537. return DIType(Node);
  538. }
  539. /// createArrayType - Create debugging information entry for an array.
  540. DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
  541. DIType Ty, DIArray Subscripts) {
  542. // TAG_array_type is encoded in DICompositeType format.
  543. Value *Elts[] = {
  544. GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
  545. NULL, //TheCU,
  546. MDString::get(VMContext, ""),
  547. NULL, //TheCU,
  548. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  549. ConstantInt::get(Type::getInt64Ty(VMContext), Size),
  550. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  551. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  552. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  553. Ty,
  554. Subscripts,
  555. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  556. Constant::getNullValue(Type::getInt32Ty(VMContext))
  557. };
  558. return DIType(MDNode::get(VMContext, Elts));
  559. }
  560. /// createVectorType - Create debugging information entry for a vector.
  561. DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
  562. DIType Ty, DIArray Subscripts) {
  563. // TAG_vector_type is encoded in DICompositeType format.
  564. Value *Elts[] = {
  565. GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
  566. NULL, //TheCU,
  567. MDString::get(VMContext, ""),
  568. NULL, //TheCU,
  569. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  570. ConstantInt::get(Type::getInt64Ty(VMContext), Size),
  571. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  572. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  573. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  574. Ty,
  575. Subscripts,
  576. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  577. Constant::getNullValue(Type::getInt32Ty(VMContext))
  578. };
  579. return DIType(MDNode::get(VMContext, Elts));
  580. }
  581. /// createArtificialType - Create a new DIType with "artificial" flag set.
  582. DIType DIBuilder::createArtificialType(DIType Ty) {
  583. if (Ty.isArtificial())
  584. return Ty;
  585. SmallVector<Value *, 9> Elts;
  586. MDNode *N = Ty;
  587. assert (N && "Unexpected input DIType!");
  588. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  589. if (Value *V = N->getOperand(i))
  590. Elts.push_back(V);
  591. else
  592. Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
  593. }
  594. unsigned CurFlags = Ty.getFlags();
  595. CurFlags = CurFlags | DIType::FlagArtificial;
  596. // Flags are stored at this slot.
  597. Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
  598. return DIType(MDNode::get(VMContext, Elts));
  599. }
  600. /// createArtificialType - Create a new DIType with "artificial" flag set.
  601. DIType DIBuilder::createObjectPointerType(DIType Ty) {
  602. if (Ty.isObjectPointer())
  603. return Ty;
  604. SmallVector<Value *, 9> Elts;
  605. MDNode *N = Ty;
  606. assert (N && "Unexpected input DIType!");
  607. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  608. if (Value *V = N->getOperand(i))
  609. Elts.push_back(V);
  610. else
  611. Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
  612. }
  613. unsigned CurFlags = Ty.getFlags();
  614. CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
  615. // Flags are stored at this slot.
  616. Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
  617. return DIType(MDNode::get(VMContext, Elts));
  618. }
  619. /// retainType - Retain DIType in a module even if it is not referenced
  620. /// through debug info anchors.
  621. void DIBuilder::retainType(DIType T) {
  622. AllRetainTypes.push_back(T);
  623. }
  624. /// createUnspecifiedParameter - Create unspeicified type descriptor
  625. /// for the subroutine type.
  626. DIDescriptor DIBuilder::createUnspecifiedParameter() {
  627. Value *Elts[] = {
  628. GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
  629. };
  630. return DIDescriptor(MDNode::get(VMContext, Elts));
  631. }
  632. /// createTemporaryType - Create a temporary forward-declared type.
  633. DIType DIBuilder::createTemporaryType() {
  634. // Give the temporary MDNode a tag. It doesn't matter what tag we
  635. // use here as long as DIType accepts it.
  636. Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
  637. MDNode *Node = MDNode::getTemporary(VMContext, Elts);
  638. return DIType(Node);
  639. }
  640. /// createTemporaryType - Create a temporary forward-declared type.
  641. DIType DIBuilder::createTemporaryType(DIFile F) {
  642. // Give the temporary MDNode a tag. It doesn't matter what tag we
  643. // use here as long as DIType accepts it.
  644. Value *Elts[] = {
  645. GetTagConstant(VMContext, DW_TAG_base_type),
  646. TheCU,
  647. NULL,
  648. F
  649. };
  650. MDNode *Node = MDNode::getTemporary(VMContext, Elts);
  651. return DIType(Node);
  652. }
  653. /// createForwardDecl - Create a temporary forward-declared type that
  654. /// can be RAUW'd if the full type is seen.
  655. DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
  656. DIDescriptor Scope, DIFile F,
  657. unsigned Line, unsigned RuntimeLang,
  658. uint64_t SizeInBits,
  659. uint64_t AlignInBits) {
  660. // Create a temporary MDNode.
  661. Value *Elts[] = {
  662. GetTagConstant(VMContext, Tag),
  663. getNonCompileUnitScope(Scope),
  664. MDString::get(VMContext, Name),
  665. F,
  666. ConstantInt::get(Type::getInt32Ty(VMContext), Line),
  667. ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
  668. ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
  669. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  670. ConstantInt::get(Type::getInt32Ty(VMContext),
  671. DIDescriptor::FlagFwdDecl),
  672. NULL,
  673. DIArray(),
  674. ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
  675. };
  676. MDNode *Node = MDNode::getTemporary(VMContext, Elts);
  677. return DIType(Node);
  678. }
  679. /// getOrCreateArray - Get a DIArray, create one if required.
  680. DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
  681. if (Elements.empty()) {
  682. Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
  683. return DIArray(MDNode::get(VMContext, Null));
  684. }
  685. return DIArray(MDNode::get(VMContext, Elements));
  686. }
  687. /// getOrCreateSubrange - Create a descriptor for a value range. This
  688. /// implicitly uniques the values returned.
  689. DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
  690. Value *Elts[] = {
  691. GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
  692. ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
  693. ConstantInt::get(Type::getInt64Ty(VMContext), Count)
  694. };
  695. return DISubrange(MDNode::get(VMContext, Elts));
  696. }
  697. /// createGlobalVariable - Create a new descriptor for the specified global.
  698. DIGlobalVariable DIBuilder::
  699. createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
  700. DIType Ty, bool isLocalToUnit, Value *Val) {
  701. Value *Elts[] = {
  702. GetTagConstant(VMContext, dwarf::DW_TAG_variable),
  703. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  704. NULL, // TheCU,
  705. MDString::get(VMContext, Name),
  706. MDString::get(VMContext, Name),
  707. MDString::get(VMContext, Name),
  708. F,
  709. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  710. Ty,
  711. ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
  712. ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
  713. Val
  714. };
  715. MDNode *Node = MDNode::get(VMContext, Elts);
  716. AllGVs.push_back(Node);
  717. return DIGlobalVariable(Node);
  718. }
  719. /// createStaticVariable - Create a new descriptor for the specified static
  720. /// variable.
  721. DIGlobalVariable DIBuilder::
  722. createStaticVariable(DIDescriptor Context, StringRef Name,
  723. StringRef LinkageName, DIFile F, unsigned LineNumber,
  724. DIType Ty, bool isLocalToUnit, Value *Val) {
  725. Value *Elts[] = {
  726. GetTagConstant(VMContext, dwarf::DW_TAG_variable),
  727. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  728. getNonCompileUnitScope(Context),
  729. MDString::get(VMContext, Name),
  730. MDString::get(VMContext, Name),
  731. MDString::get(VMContext, LinkageName),
  732. F,
  733. ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
  734. Ty,
  735. ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
  736. ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
  737. Val
  738. };
  739. MDNode *Node = MDNode::get(VMContext, Elts);
  740. AllGVs.push_back(Node);
  741. return DIGlobalVariable(Node);
  742. }
  743. /// createVariable - Create a new descriptor for the specified variable.
  744. DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
  745. StringRef Name, DIFile File,
  746. unsigned LineNo, DIType Ty,
  747. bool AlwaysPreserve, unsigned Flags,
  748. unsigned ArgNo) {
  749. Value *Elts[] = {
  750. GetTagConstant(VMContext, Tag),
  751. getNonCompileUnitScope(Scope),
  752. MDString::get(VMContext, Name),
  753. File,
  754. ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
  755. Ty,
  756. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  757. Constant::getNullValue(Type::getInt32Ty(VMContext))
  758. };
  759. MDNode *Node = MDNode::get(VMContext, Elts);
  760. if (AlwaysPreserve) {
  761. // The optimizer may remove local variable. If there is an interest
  762. // to preserve variable info in such situation then stash it in a
  763. // named mdnode.
  764. DISubprogram Fn(getDISubprogram(Scope));
  765. NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
  766. FnLocals->addOperand(Node);
  767. }
  768. return DIVariable(Node);
  769. }
  770. /// createComplexVariable - Create a new descriptor for the specified variable
  771. /// which has a complex address expression for its address.
  772. DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
  773. StringRef Name, DIFile F,
  774. unsigned LineNo,
  775. DIType Ty, ArrayRef<Value *> Addr,
  776. unsigned ArgNo) {
  777. SmallVector<Value *, 15> Elts;
  778. Elts.push_back(GetTagConstant(VMContext, Tag));
  779. Elts.push_back(getNonCompileUnitScope(Scope)),
  780. Elts.push_back(MDString::get(VMContext, Name));
  781. Elts.push_back(F);
  782. Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
  783. (LineNo | (ArgNo << 24))));
  784. Elts.push_back(Ty);
  785. Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
  786. Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
  787. Elts.append(Addr.begin(), Addr.end());
  788. return DIVariable(MDNode::get(VMContext, Elts));
  789. }
  790. /// createFunction - Create a new descriptor for the specified function.
  791. DISubprogram DIBuilder::createFunction(DIDescriptor Context,
  792. StringRef Name,
  793. StringRef LinkageName,
  794. DIFile File, unsigned LineNo,
  795. DIType Ty,
  796. bool isLocalToUnit, bool isDefinition,
  797. unsigned ScopeLine,
  798. unsigned Flags, bool isOptimized,
  799. Function *Fn,
  800. MDNode *TParams,
  801. MDNode *Decl) {
  802. Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
  803. MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
  804. Value *TVElts[] = { Temp };
  805. MDNode *THolder = MDNode::get(VMContext, TVElts);
  806. Value *Elts[] = {
  807. GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
  808. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  809. getNonCompileUnitScope(Context),
  810. MDString::get(VMContext, Name),
  811. MDString::get(VMContext, Name),
  812. MDString::get(VMContext, LinkageName),
  813. File,
  814. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
  815. Ty,
  816. ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
  817. ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
  818. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  819. ConstantInt::get(Type::getInt32Ty(VMContext), 0),
  820. NULL,
  821. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  822. ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
  823. Fn,
  824. TParams,
  825. Decl,
  826. THolder,
  827. ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
  828. };
  829. MDNode *Node = MDNode::get(VMContext, Elts);
  830. // Create a named metadata so that we do not lose this mdnode.
  831. AllSubprograms.push_back(Node);
  832. return DISubprogram(Node);
  833. }
  834. /// createMethod - Create a new descriptor for the specified C++ method.
  835. DISubprogram DIBuilder::createMethod(DIDescriptor Context,
  836. StringRef Name,
  837. StringRef LinkageName,
  838. DIFile F,
  839. unsigned LineNo, DIType Ty,
  840. bool isLocalToUnit,
  841. bool isDefinition,
  842. unsigned VK, unsigned VIndex,
  843. MDNode *VTableHolder,
  844. unsigned Flags,
  845. bool isOptimized,
  846. Function *Fn,
  847. MDNode *TParam) {
  848. Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
  849. MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
  850. Value *TVElts[] = { Temp };
  851. MDNode *THolder = MDNode::get(VMContext, TVElts);
  852. Value *Elts[] = {
  853. GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
  854. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  855. getNonCompileUnitScope(Context),
  856. MDString::get(VMContext, Name),
  857. MDString::get(VMContext, Name),
  858. MDString::get(VMContext, LinkageName),
  859. F,
  860. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
  861. Ty,
  862. ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
  863. ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
  864. ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
  865. ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
  866. VTableHolder,
  867. ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
  868. ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
  869. Fn,
  870. TParam,
  871. Constant::getNullValue(Type::getInt32Ty(VMContext)),
  872. THolder,
  873. // FIXME: Do we want to use different scope/lines?
  874. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
  875. };
  876. MDNode *Node = MDNode::get(VMContext, Elts);
  877. return DISubprogram(Node);
  878. }
  879. /// createNameSpace - This creates new descriptor for a namespace
  880. /// with the specified parent scope.
  881. DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
  882. DIFile File, unsigned LineNo) {
  883. Value *Elts[] = {
  884. GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
  885. getNonCompileUnitScope(Scope),
  886. MDString::get(VMContext, Name),
  887. File,
  888. ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
  889. };
  890. return DINameSpace(MDNode::get(VMContext, Elts));
  891. }
  892. /// createLexicalBlockFile - This creates a new MDNode that encapsulates
  893. /// an existing scope with a new filename.
  894. DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
  895. DIFile File) {
  896. Value *Elts[] = {
  897. GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
  898. Scope,
  899. File
  900. };
  901. return DILexicalBlockFile(MDNode::get(VMContext, Elts));
  902. }
  903. DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
  904. unsigned Line, unsigned Col) {
  905. // Defeat MDNode uniqing for lexical blocks by using unique id.
  906. static unsigned int unique_id = 0;
  907. Value *Elts[] = {
  908. GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
  909. getNonCompileUnitScope(Scope),
  910. ConstantInt::get(Type::getInt32Ty(VMContext), Line),
  911. ConstantInt::get(Type::getInt32Ty(VMContext), Col),
  912. File,
  913. ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
  914. };
  915. return DILexicalBlock(MDNode::get(VMContext, Elts));
  916. }
  917. /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
  918. Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
  919. Instruction *InsertBefore) {
  920. assert(Storage && "no storage passed to dbg.declare");
  921. assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
  922. if (!DeclareFn)
  923. DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
  924. Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
  925. return CallInst::Create(DeclareFn, Args, "", InsertBefore);
  926. }
  927. /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
  928. Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
  929. BasicBlock *InsertAtEnd) {
  930. assert(Storage && "no storage passed to dbg.declare");
  931. assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
  932. if (!DeclareFn)
  933. DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
  934. Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
  935. // If this block already has a terminator then insert this intrinsic
  936. // before the terminator.
  937. if (TerminatorInst *T = InsertAtEnd->getTerminator())
  938. return CallInst::Create(DeclareFn, Args, "", T);
  939. else
  940. return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
  941. }
  942. /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
  943. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
  944. DIVariable VarInfo,
  945. Instruction *InsertBefore) {
  946. assert(V && "no value passed to dbg.value");
  947. assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
  948. if (!ValueFn)
  949. ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
  950. Value *Args[] = { MDNode::get(V->getContext(), V),
  951. ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
  952. VarInfo };
  953. return CallInst::Create(ValueFn, Args, "", InsertBefore);
  954. }
  955. /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
  956. Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
  957. DIVariable VarInfo,
  958. BasicBlock *InsertAtEnd) {
  959. assert(V && "no value passed to dbg.value");
  960. assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
  961. if (!ValueFn)
  962. ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
  963. Value *Args[] = { MDNode::get(V->getContext(), V),
  964. ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
  965. VarInfo };
  966. return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
  967. }