Skip to content

Commit df6b067

Browse files
committed
Implement const operands for global asm
1 parent e238ea6 commit df6b067

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/global_asm.rs

Lines changed: 27 additions & 2 deletions
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,32 @@ 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: _ } => todo!(),
43+
InlineAsmOperand::SymStatic { path: _, def_id: _ } => todo!(),
44+
InlineAsmOperand::In { .. }
45+
| InlineAsmOperand::Out { .. }
46+
| InlineAsmOperand::InOut { .. }
47+
| InlineAsmOperand::SplitInOut { .. } => {
48+
span_bug!(op_sp, "invalid operand type for global_asm!")
49+
}
50+
}
51+
}
2752
}
2853
}
2954
global_asm.push_str("\n.att_syntax\n\n");

0 commit comments

Comments
 (0)