1
Fork 0

Use declare_anonymous_data for anonymous_str

This commit is contained in:
bjorn3 2021-05-11 14:25:56 +02:00
parent 7c40338ba1
commit 24c459c2e5
3 changed files with 4 additions and 10 deletions

View file

@ -870,7 +870,7 @@ pub(crate) fn codegen_operand<'tcx>(
pub(crate) fn codegen_panic<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, msg_str: &str, span: Span) {
let location = fx.get_caller_location(span).load_scalar(fx);
let msg_ptr = fx.anonymous_str("assert", msg_str);
let msg_ptr = fx.anonymous_str(msg_str);
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
let args = [msg_ptr, msg_len, location];

View file

@ -345,18 +345,12 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
self.module.isa().triple()
}
pub(crate) fn anonymous_str(&mut self, prefix: &str, msg: &str) -> Value {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
msg.hash(&mut hasher);
let msg_hash = hasher.finish();
pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value {
let mut data_ctx = DataContext::new();
data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice());
let msg_id = self
.module
.declare_data(&format!("__{}_{:08x}", prefix, msg_hash), Linkage::Local, false, false)
.declare_anonymous_data(false, false)
.unwrap();
// Ignore DuplicateDefinition error, as the data will be the same

View file

@ -21,7 +21,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
}
let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, fx.symbol_name, msg);
let msg_ptr = fx.anonymous_str("trap", &real_msg);
let msg_ptr = fx.anonymous_str(&real_msg);
fx.bcx.ins().call(puts, &[msg_ptr]);
}