Skip to content

Commit e267106

Browse files
committed
Use gimli to get the values of DWARF constants needed by codegen
The `gimli` crate is already a dependency of `thorin-dwp`, which is already a dependency of `rustc_codegen_ssa`.
1 parent 8417f83 commit e267106

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,7 @@ name = "rustc_codegen_llvm"
34993499
version = "0.0.0"
35003500
dependencies = [
35013501
"bitflags",
3502+
"gimli 0.30.0",
35023503
"itertools",
35033504
"libc",
35043505
"measureme",

Diff for: compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test = false
99
[dependencies]
1010
# tidy-alphabetical-start
1111
bitflags = "2.4.1"
12+
gimli = "0.30"
1213
itertools = "0.12"
1314
libc = "0.2"
1415
measureme = "11"
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Definitions of various DWARF-related constants.
2+
3+
use libc::c_uint;
4+
5+
/// Helper macro to let us redeclare gimli's constants as our own constants
6+
/// with a different type, with less risk of copy-paste errors.
7+
macro_rules! declare_constant {
8+
(
9+
$name:ident : $type:ty
10+
) => {
11+
#[allow(non_upper_case_globals)]
12+
pub(crate) const $name: $type = ::gimli::constants::$name.0 as $type;
13+
14+
// Assert that as-cast probably hasn't changed the value.
15+
const _: () = assert!($name as i128 == ::gimli::constants::$name.0 as i128);
16+
};
17+
}
18+
19+
declare_constant!(DW_TAG_const_type: c_uint);
20+
21+
// DWARF languages.
22+
declare_constant!(DW_LANG_Rust: c_uint);
23+
24+
// DWARF attribute type encodings.
25+
declare_constant!(DW_ATE_boolean: c_uint);
26+
declare_constant!(DW_ATE_float: c_uint);
27+
declare_constant!(DW_ATE_signed: c_uint);
28+
declare_constant!(DW_ATE_unsigned: c_uint);
29+
declare_constant!(DW_ATE_UTF: c_uint);

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_target::spec::DebuginfoKind;
2222
use smallvec::smallvec;
2323
use tracing::{debug, instrument};
2424

25+
pub(crate) use self::type_map::TypeMap;
2526
use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
2627
use super::CodegenUnitDebugContext;
2728
use super::namespace::mangled_name_of_instance;
@@ -30,6 +31,7 @@ use super::utils::{
3031
DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
3132
};
3233
use crate::common::{AsCCharPtr, CodegenCx};
34+
use crate::debuginfo::dwarf_const;
3335
use crate::debuginfo::metadata::type_map::build_type_with_children;
3436
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
3537
use crate::llvm::debuginfo::{
@@ -59,23 +61,6 @@ impl fmt::Debug for llvm::Metadata {
5961
}
6062
}
6163

62-
// From DWARF 5.
63-
// See http://www.dwarfstd.org/ShowIssue.php?issue=140129.1.
64-
const DW_LANG_RUST: c_uint = 0x1c;
65-
#[allow(non_upper_case_globals)]
66-
const DW_ATE_boolean: c_uint = 0x02;
67-
#[allow(non_upper_case_globals)]
68-
const DW_ATE_float: c_uint = 0x04;
69-
#[allow(non_upper_case_globals)]
70-
const DW_ATE_signed: c_uint = 0x05;
71-
#[allow(non_upper_case_globals)]
72-
const DW_ATE_unsigned: c_uint = 0x07;
73-
#[allow(non_upper_case_globals)]
74-
const DW_ATE_UTF: c_uint = 0x10;
75-
76-
#[allow(non_upper_case_globals)]
77-
const DW_TAG_const_type: c_uint = 0x26;
78-
7964
pub(super) const UNKNOWN_LINE_NUMBER: c_uint = 0;
8065
pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
8166

@@ -90,8 +75,6 @@ type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
9075
mod enums;
9176
mod type_map;
9277

93-
pub(crate) use type_map::TypeMap;
94-
9578
/// Returns from the enclosing function if the type debuginfo node with the given
9679
/// unique ID can be found in the type map.
9780
macro_rules! return_if_di_node_created_in_meantime {
@@ -522,7 +505,7 @@ fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll D
522505
name.as_c_char_ptr(),
523506
name.len(),
524507
cx.tcx.data_layout.pointer_size.bits(),
525-
DW_ATE_unsigned,
508+
dwarf_const::DW_ATE_unsigned,
526509
)
527510
}
528511
})
@@ -781,6 +764,8 @@ fn build_basic_type_di_node<'ll, 'tcx>(
781764
// .natvis visualizers (and perhaps other existing native debuggers?)
782765
let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
783766

767+
use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
768+
784769
let (name, encoding) = match t.kind() {
785770
ty::Never => ("!", DW_ATE_unsigned),
786771
ty::Tuple(elements) if elements.is_empty() => {
@@ -961,7 +946,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
961946

962947
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
963948
debug_context.builder,
964-
DW_LANG_RUST,
949+
dwarf_const::DW_LANG_Rust,
965950
compile_unit_file,
966951
producer.as_c_char_ptr(),
967952
producer.len(),

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use rustc_middle::ty::{self, AdtDef, CoroutineArgs, CoroutineArgsExt, Ty};
1212
use smallvec::smallvec;
1313

1414
use crate::common::{AsCCharPtr, CodegenCx};
15+
use crate::debuginfo::dwarf_const::DW_TAG_const_type;
1516
use crate::debuginfo::metadata::enums::DiscrResult;
1617
use crate::debuginfo::metadata::type_map::{self, Stub, UniqueTypeId};
1718
use crate::debuginfo::metadata::{
18-
DINodeCreationResult, DW_TAG_const_type, NO_GENERICS, NO_SCOPE_METADATA, SmallVec,
19-
UNKNOWN_LINE_NUMBER, build_field_di_node, file_metadata, file_metadata_from_def_id,
20-
size_and_align_of, type_di_node, unknown_file_metadata, visibility_di_flags,
19+
DINodeCreationResult, NO_GENERICS, NO_SCOPE_METADATA, SmallVec, UNKNOWN_LINE_NUMBER,
20+
build_field_di_node, file_metadata, file_metadata_from_def_id, size_and_align_of, type_di_node,
21+
unknown_file_metadata, visibility_di_flags,
2122
};
2223
use crate::debuginfo::utils::DIB;
2324
use crate::llvm::debuginfo::{DIFile, DIFlags, DIType};

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::llvm::debuginfo::{
3939
use crate::value::Value;
4040

4141
mod create_scope_map;
42+
mod dwarf_const;
4243
mod gdb;
4344
pub(crate) mod metadata;
4445
mod namespace;

0 commit comments

Comments
 (0)