summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorAdam Perry <adam.n.perry@gmail.com>2019-10-09 08:25:41 -0700
committerAdam Perry <adam.n.perry@gmail.com>2019-10-27 12:50:52 -0700
commit743964ad3fe566ca2ce5c2de14f8733887d283fd (patch)
tree3f8e84bcab7700d4d5a7c25d330c3338888b9605 /src/librustc_codegen_llvm
parentfcf516d827300d21eb9f6312e799578b6f359d5d (diff)
downloadrust-743964ad3fe566ca2ce5c2de14f8733887d283fd.tar.gz
rust-743964ad3fe566ca2ce5c2de14f8733887d283fd.zip
Implement core::intrinsics::caller_location.
Returns a `&core::panic::Location` corresponding to where it was
called, also making `Location` a lang item.
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/builder.rs15
-rw-r--r--src/librustc_codegen_llvm/common.rs7
2 files changed, 22 insertions, 0 deletions
diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs
index 98be0ae4433..ffaf8050bcb 100644
--- a/src/librustc_codegen_llvm/builder.rs
+++ b/src/librustc_codegen_llvm/builder.rs
@@ -2,6 +2,7 @@ use crate::llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope};
 use crate::llvm::{self, False, BasicBlock};
 use crate::common::Funclet;
 use crate::context::CodegenCx;
+use crate::syntax_pos::Pos;
 use crate::type_::Type;
 use crate::type_of::LayoutLlvmExt;
 use crate::value::Value;
@@ -1068,6 +1069,20 @@ impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
         self.cx().get_static(def_id)
     }
 
+    fn static_panic_location(&mut self, loc: &syntax::source_map::Loc) -> Self::Value {
+        let filename = Symbol::intern(&loc.file.name.to_string());
+        let filename = self.const_str(filename);
+        let line = self.const_u32(loc.line as u32);
+        let col = self.const_u32(loc.col.to_usize() as u32 + 1);
+        let struct_ = self.const_struct(&[filename.0, filename.1, line, col], false);
+
+        let align = self.tcx.data_layout.aggregate_align.abi
+            .max(self.tcx.data_layout.i32_align.abi)
+            .max(self.tcx.data_layout.pointer_align.abi);
+        // FIXME(eddyb) move this into miri, it can be correct if e.g. field order changes
+        self.static_addr_of(struct_, align, Some("panic_loc"))
+    }
+
     fn static_panic_msg(
         &mut self,
         msg: Option<Symbol>,
diff --git a/src/librustc_codegen_llvm/common.rs b/src/librustc_codegen_llvm/common.rs
index a1a5232d588..b4b82f67c74 100644
--- a/src/librustc_codegen_llvm/common.rs
+++ b/src/librustc_codegen_llvm/common.rs
@@ -237,6 +237,13 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
         unsafe { llvm::LLVMConstReal(t, val) }
     }
 
+    fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
+        let len = s.as_str().len();
+        let cs = consts::ptrcast(self.const_cstr(s, false),
+            self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
+        (cs, self.const_usize(len as u64))
+    }
+
     fn const_struct(
         &self,
         elts: &[&'ll Value],