| author | |
| committer | |
| log | 31b6d14bf76fa6b78013a6a32d3066e882d64043 |
| tree | 7a31da91ca6515f1fbb2bcea8023e6b4c9aea63a |
| parent | d80e6ca5a6843cb3911ec2d0b2da8b4ae515202a |
| signature |
* llvm: emit metadata for global variable
One use case is to genearte BTF information from global variable's metadata.
Signed-off-by: Tw <weii.tan>4 files changed, 39 insertions(+), 10 deletions(-)
src/codegen/llvm.zig+3-2| ... | ... | @@ -2549,7 +2549,7 @@ pub const DeclGen = struct { |
| 2549 | 2549 | |
| 2550 | 2550 | const line_number = decl.src_line + 1; |
| 2551 | 2551 | const is_internal_linkage = !dg.module.decl_exports.contains(decl_index); |
| 2552 | const di_global = dib.createGlobalVariable( | |
| 2552 | const di_global = dib.createGlobalVariableExpression( | |
| 2553 | 2553 | di_file.toScope(), |
| 2554 | 2554 | decl.name, |
| 2555 | 2555 | global.getValueName(), |
| ... | ... | @@ -2559,7 +2559,8 @@ pub const DeclGen = struct { |
| 2559 | 2559 | is_internal_linkage, |
| 2560 | 2560 | ); |
| 2561 | 2561 | |
| 2562 | try dg.object.di_map.put(dg.gpa, dg.decl, di_global.toNode()); | |
| 2562 | try dg.object.di_map.put(dg.gpa, dg.decl, di_global.getVariable().toNode()); | |
| 2563 | if (!is_internal_linkage) global.attachMetaData(di_global); | |
| 2563 | 2564 | } |
| 2564 | 2565 | } |
| 2565 | 2566 | } |
src/codegen/llvm/bindings.zig+14-3| ... | ... | @@ -256,6 +256,9 @@ pub const Value = opaque { |
| 256 | 256 | |
| 257 | 257 | pub const addByValAttr = ZigLLVMAddByValAttr; |
| 258 | 258 | extern fn ZigLLVMAddByValAttr(Fn: *Value, ArgNo: c_uint, type: *Type) void; |
| 259 | ||
| 260 | pub const attachMetaData = ZigLLVMAttachMetaData; | |
| 261 | extern fn ZigLLVMAttachMetaData(GlobalVar: *Value, DIG: *DIGlobalVariableExpression) void; | |
| 259 | 262 | }; |
| 260 | 263 | |
| 261 | 264 | pub const Type = opaque { |
| ... | ... | @@ -1588,6 +1591,7 @@ pub const address_space = struct { |
| 1588 | 1591 | pub const DIEnumerator = opaque {}; |
| 1589 | 1592 | pub const DILocalVariable = opaque {}; |
| 1590 | 1593 | pub const DILocation = opaque {}; |
| 1594 | pub const DIGlobalExpression = opaque {}; | |
| 1591 | 1595 | |
| 1592 | 1596 | pub const DIGlobalVariable = opaque { |
| 1593 | 1597 | pub const toNode = ZigLLVMGlobalVariableToNode; |
| ... | ... | @@ -1596,6 +1600,13 @@ pub const DIGlobalVariable = opaque { |
| 1596 | 1600 | pub const replaceLinkageName = ZigLLVMGlobalVariableReplaceLinkageName; |
| 1597 | 1601 | extern fn ZigLLVMGlobalVariableReplaceLinkageName(global_variable: *DIGlobalVariable, linkage_name: *MDString) void; |
| 1598 | 1602 | }; |
| 1603 | pub const DIGlobalVariableExpression = opaque { | |
| 1604 | pub const getVariable = ZigLLVMGlobalGetVariable; | |
| 1605 | extern fn ZigLLVMGlobalGetVariable(global_variable: *DIGlobalVariableExpression) *DIGlobalVariable; | |
| 1606 | ||
| 1607 | pub const getExpression = ZigLLVMGlobalGetExpression; | |
| 1608 | extern fn ZigLLVMGlobalGetExpression(global_variable: *DIGlobalVariableExpression) *DIGlobalExpression; | |
| 1609 | }; | |
| 1599 | 1610 | pub const DIType = opaque { |
| 1600 | 1611 | pub const toScope = ZigLLVMTypeToScope; |
| 1601 | 1612 | extern fn ZigLLVMTypeToScope(ty: *DIType) *DIScope; |
| ... | ... | @@ -1803,8 +1814,8 @@ pub const DIBuilder = opaque { |
| 1803 | 1814 | flags: c_uint, |
| 1804 | 1815 | ) *DILocalVariable; |
| 1805 | 1816 | |
| 1806 | pub const createGlobalVariable = ZigLLVMCreateGlobalVariable; | |
| 1807 | extern fn ZigLLVMCreateGlobalVariable( | |
| 1817 | pub const createGlobalVariableExpression = ZigLLVMCreateGlobalVariableExpression; | |
| 1818 | extern fn ZigLLVMCreateGlobalVariableExpression( | |
| 1808 | 1819 | dib: *DIBuilder, |
| 1809 | 1820 | scope: *DIScope, |
| 1810 | 1821 | name: [*:0]const u8, |
| ... | ... | @@ -1813,7 +1824,7 @@ pub const DIBuilder = opaque { |
| 1813 | 1824 | line_no: c_uint, |
| 1814 | 1825 | di_type: *DIType, |
| 1815 | 1826 | is_local_to_unit: bool, |
| 1816 | ) *DIGlobalVariable; | |
| 1827 | ) *DIGlobalVariableExpression; | |
| 1817 | 1828 | |
| 1818 | 1829 | pub const createParameterVariable = ZigLLVMCreateParameterVariable; |
| 1819 | 1830 | extern fn ZigLLVMCreateParameterVariable( |
src/zig_llvm.cpp+15-4| ... | ... | @@ -870,19 +870,18 @@ ZigLLVMDILocalVariable *ZigLLVMCreateAutoVariable(ZigLLVMDIBuilder *dbuilder, |
| 870 | 870 | return reinterpret_cast<ZigLLVMDILocalVariable*>(result); |
| 871 | 871 | } |
| 872 | 872 | |
| 873 | ZigLLVMDIGlobalVariable *ZigLLVMCreateGlobalVariable(ZigLLVMDIBuilder *dbuilder, | |
| 873 | ZigLLVMDIGlobalVariableExpression *ZigLLVMCreateGlobalVariableExpression(ZigLLVMDIBuilder *dbuilder, | |
| 874 | 874 | ZigLLVMDIScope *scope, const char *name, const char *linkage_name, ZigLLVMDIFile *file, |
| 875 | 875 | unsigned line_no, ZigLLVMDIType *di_type, bool is_local_to_unit) |
| 876 | 876 | { |
| 877 | DIGlobalVariableExpression *result = reinterpret_cast<DIBuilder*>(dbuilder)->createGlobalVariableExpression( | |
| 877 | return reinterpret_cast<ZigLLVMDIGlobalVariableExpression*>(reinterpret_cast<DIBuilder*>(dbuilder)->createGlobalVariableExpression( | |
| 878 | 878 | reinterpret_cast<DIScope*>(scope), |
| 879 | 879 | name, |
| 880 | 880 | linkage_name, |
| 881 | 881 | reinterpret_cast<DIFile*>(file), |
| 882 | 882 | line_no, |
| 883 | 883 | reinterpret_cast<DIType*>(di_type), |
| 884 | is_local_to_unit); | |
| 885 | return reinterpret_cast<ZigLLVMDIGlobalVariable*>(result->getVariable()); | |
| 884 | is_local_to_unit)); | |
| 886 | 885 | } |
| 887 | 886 | |
| 888 | 887 | ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilder, |
| ... | ... | @@ -1448,6 +1447,18 @@ void ZigLLVMTakeName(LLVMValueRef new_owner, LLVMValueRef victim) { |
| 1448 | 1447 | unwrap(new_owner)->takeName(unwrap(victim)); |
| 1449 | 1448 | } |
| 1450 | 1449 | |
| 1450 | ZigLLVMDIGlobalVariable* ZigLLVMGlobalGetVariable(ZigLLVMDIGlobalVariableExpression *global_variable_expression) { | |
| 1451 | 	return reinterpret_cast<ZigLLVMDIGlobalVariable*>(reinterpret_cast<DIGlobalVariableExpression*>(global_variable_expression)->getVariable()); | |
| 1452 | } | |
| 1453 | ||
| 1454 | ZigLLVMDIGlobalExpression* ZigLLVMGlobalGetExpression(ZigLLVMDIGlobalVariableExpression *global_variable_expression) { | |
| 1455 | 	return reinterpret_cast<ZigLLVMDIGlobalExpression*>(reinterpret_cast<DIGlobalVariableExpression*>(global_variable_expression)->getExpression()); | |
| 1456 | } | |
| 1457 | ||
| 1458 | void ZigLLVMAttachMetaData(LLVMValueRef Val, ZigLLVMDIGlobalVariableExpression *global_variable_expression) { | |
| 1459 | 	unwrap<GlobalVariable>(Val)->addDebugInfo(reinterpret_cast<DIGlobalVariableExpression*>(global_variable_expression)); | |
| 1460 | } | |
| 1461 | ||
| 1451 | 1462 | static_assert((Triple::ArchType)ZigLLVM_UnknownArch == Triple::UnknownArch, ""); |
| 1452 | 1463 | static_assert((Triple::ArchType)ZigLLVM_arm == Triple::arm, ""); |
| 1453 | 1464 | static_assert((Triple::ArchType)ZigLLVM_armeb == Triple::armeb, ""); |
src/zig_llvm.h+7-1| ... | ... | @@ -34,7 +34,9 @@ struct ZigLLVMDILexicalBlock; |
| 34 | 34 | struct ZigLLVMDISubprogram; |
| 35 | 35 | struct ZigLLVMDISubroutineType; |
| 36 | 36 | struct ZigLLVMDILocalVariable; |
| 37 | struct ZigLLVMDIGlobalVariableExpression; | |
| 37 | 38 | struct ZigLLVMDIGlobalVariable; |
| 39 | struct ZigLLVMDIGlobalExpression; | |
| 38 | 40 | struct ZigLLVMDILocation; |
| 39 | 41 | struct ZigLLVMDIEnumerator; |
| 40 | 42 | struct ZigLLVMInsertionPoint; |
| ... | ... | @@ -271,7 +273,7 @@ ZIG_EXTERN_C struct ZigLLVMDILocalVariable *ZigLLVMCreateAutoVariable(struct Zig |
| 271 | 273 | struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_no, |
| 272 | 274 | struct ZigLLVMDIType *type, bool always_preserve, unsigned flags); |
| 273 | 275 | |
| 274 | ZIG_EXTERN_C struct ZigLLVMDIGlobalVariable *ZigLLVMCreateGlobalVariable(struct ZigLLVMDIBuilder *dbuilder, | |
| 276 | ZIG_EXTERN_C struct ZigLLVMDIGlobalVariableExpression *ZigLLVMCreateGlobalVariableExpression(struct ZigLLVMDIBuilder *dbuilder, | |
| 275 | 277 | struct ZigLLVMDIScope *scope, const char *name, const char *linkage_name, struct ZigLLVMDIFile *file, |
| 276 | 278 | unsigned line_no, struct ZigLLVMDIType *di_type, bool is_local_to_unit); |
| 277 | 279 | |
| ... | ... | @@ -334,6 +336,10 @@ ZIG_EXTERN_C void ZigLLVMAddFunctionAttrCold(LLVMValueRef fn); |
| 334 | 336 | |
| 335 | 337 | ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv); |
| 336 | 338 | |
| 339 | ZIG_EXTERN_C ZigLLVMDIGlobalVariable* ZigLLVMGlobalGetVariable(ZigLLVMDIGlobalVariableExpression *global_variable_expression); | |
| 340 | ZIG_EXTERN_C ZigLLVMDIGlobalExpression* ZigLLVMGlobalGetExpression(ZigLLVMDIGlobalVariableExpression *global_variable_expression); | |
| 341 | ZIG_EXTERN_C void ZigLLVMAttachMetaData(LLVMValueRef Val, ZigLLVMDIGlobalVariableExpression *global_variable_expression); | |
| 342 | ||
| 337 | 343 | |
| 338 | 344 | // synchronize with llvm/include/ADT/Triple.h::ArchType |
| 339 | 345 | // synchronize with std.Target.Cpu.Arch |