1
Fork 0

Auto merge of #94468 - Amanieu:global_asm_sym, r=nagisa

Implement sym operands for global_asm!

Tracking issue: #93333

This PR is pretty much a complete rewrite of `sym` operand support for inline assembly so that the same implementation can be shared by `asm!` and `global_asm!`. The main changes are:
- At the AST level, `sym` is represented as a special `InlineAsmSym` AST node containing a path instead of an `Expr`.
- At the HIR level, `sym` is split into `SymStatic` and `SymFn` depending on whether the path resolves to a static during AST lowering (defaults to `SynFn` if `get_early_res` fails).
  - `SymFn` is just an `AnonConst`. It runs through typeck and we just collect the resulting type at the end. An error is emitted if the type is not a `FnDef`.
  - `SymStatic` directly holds a path and the `DefId` of the `static` that it is pointing to.
- The representation at the MIR level is mostly unchanged. There is a minor change to THIR where `SymFn` is a constant instead of an expression.
- At the codegen level we need to apply the target's symbol mangling to the result of `tcx.symbol_name()` depending on the target. This is done by calling the LLVM name mangler, which handles all of the details.
  - On Mach-O, all symbols have a leading underscore.
  - On x86 Windows, different mangling is used for cdecl, stdcall, fastcall and vectorcall.
  - No mangling is needed on other platforms.

r? `@nagisa`
cc `@eddyb`
This commit is contained in:
bors 2022-04-16 04:46:01 +00:00
commit 080d5452e1
50 changed files with 654 additions and 245 deletions

View file

@ -2425,8 +2425,12 @@ pub enum InlineAsmOperand<'hir> {
Const {
anon_const: AnonConst,
},
Sym {
expr: Expr<'hir>,
SymFn {
anon_const: AnonConst,
},
SymStatic {
path: QPath<'hir>,
def_id: DefId,
},
}
@ -2437,7 +2441,7 @@ impl<'hir> InlineAsmOperand<'hir> {
| Self::Out { reg, .. }
| Self::InOut { reg, .. }
| Self::SplitInOut { reg, .. } => Some(reg),
Self::Const { .. } | Self::Sym { .. } => None,
Self::Const { .. } | Self::SymFn { .. } | Self::SymStatic { .. } => None,
}
}

View file

@ -484,6 +484,9 @@ pub trait Visitor<'v>: Sized {
fn visit_defaultness(&mut self, defaultness: &'v Defaultness) {
walk_defaultness(self, defaultness);
}
fn visit_inline_asm(&mut self, asm: &'v InlineAsm<'v>, id: HirId) {
walk_inline_asm(self, asm, id);
}
}
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
@ -588,7 +591,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
}
ItemKind::GlobalAsm(asm) => {
visitor.visit_id(item.hir_id());
walk_inline_asm(visitor, asm);
visitor.visit_inline_asm(asm, item.hir_id());
}
ItemKind::TyAlias(ref ty, ref generics) => {
visitor.visit_id(item.hir_id());
@ -648,12 +651,12 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
}
}
fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>) {
for (op, _op_sp) in asm.operands {
pub fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>, id: HirId) {
for (op, op_sp) in asm.operands {
match op {
InlineAsmOperand::In { expr, .. }
| InlineAsmOperand::InOut { expr, .. }
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
visitor.visit_expr(expr)
}
InlineAsmOperand::Out { expr, .. } => {
if let Some(expr) = expr {
visitor.visit_expr(expr);
@ -665,7 +668,9 @@ fn walk_inline_asm<'v, V: Visitor<'v>>(visitor: &mut V, asm: &'v InlineAsm<'v>)
visitor.visit_expr(out_expr);
}
}
InlineAsmOperand::Const { anon_const } => visitor.visit_anon_const(anon_const),
InlineAsmOperand::Const { anon_const, .. }
| InlineAsmOperand::SymFn { anon_const, .. } => visitor.visit_anon_const(anon_const),
InlineAsmOperand::SymStatic { path, .. } => visitor.visit_qpath(path, id, *op_sp),
}
}
}
@ -1221,7 +1226,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
walk_list!(visitor, visit_expr, optional_expression);
}
ExprKind::InlineAsm(ref asm) => {
walk_inline_asm(visitor, asm);
visitor.visit_inline_asm(asm, expression.hir_id);
}
ExprKind::Yield(ref subexpression, _) => {
visitor.visit_expr(subexpression);