Skip to content

Commit 5ab690d

Browse files
authored
Merge pull request rust-lang#1351 from bjorn3/global_asm_const
Implement const and sym operands for global asm
2 parents e238ea6 + 178e267 commit 5ab690d

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/global_asm.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::process::{Command, Stdio};
77
use std::sync::Arc;
88

99
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
10-
use rustc_hir::ItemId;
10+
use rustc_hir::{InlineAsmOperand, ItemId};
1111
use rustc_session::config::{OutputFilenames, OutputType};
1212

1313
use crate::prelude::*;
@@ -23,7 +23,46 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
2323
for piece in asm.template {
2424
match *piece {
2525
InlineAsmTemplatePiece::String(ref s) => global_asm.push_str(s),
26-
InlineAsmTemplatePiece::Placeholder { .. } => todo!(),
26+
InlineAsmTemplatePiece::Placeholder { operand_idx, modifier: _, span: op_sp } => {
27+
match asm.operands[operand_idx].0 {
28+
InlineAsmOperand::Const { ref anon_const } => {
29+
let const_value =
30+
tcx.const_eval_poly(anon_const.def_id.to_def_id()).unwrap_or_else(
31+
|_| span_bug!(op_sp, "asm const cannot be resolved"),
32+
);
33+
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
34+
let string = rustc_codegen_ssa::common::asm_const_to_str(
35+
tcx,
36+
op_sp,
37+
const_value,
38+
RevealAllLayoutCx(tcx).layout_of(ty),
39+
);
40+
global_asm.push_str(&string);
41+
}
42+
InlineAsmOperand::SymFn { anon_const } => {
43+
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
44+
let instance = match ty.kind() {
45+
&ty::FnDef(def_id, substs) => Instance::new(def_id, substs),
46+
_ => span_bug!(op_sp, "asm sym is not a function"),
47+
};
48+
let symbol = tcx.symbol_name(instance);
49+
// FIXME handle the case where the function was made private to the
50+
// current codegen unit
51+
global_asm.push_str(symbol.name);
52+
}
53+
InlineAsmOperand::SymStatic { path: _, def_id } => {
54+
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
55+
let symbol = tcx.symbol_name(instance);
56+
global_asm.push_str(symbol.name);
57+
}
58+
InlineAsmOperand::In { .. }
59+
| InlineAsmOperand::Out { .. }
60+
| InlineAsmOperand::InOut { .. }
61+
| InlineAsmOperand::SplitInOut { .. } => {
62+
span_bug!(op_sp, "invalid operand type for global_asm!")
63+
}
64+
}
65+
}
2766
}
2867
}
2968
global_asm.push_str("\n.att_syntax\n\n");

0 commit comments

Comments
 (0)