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

@ -426,9 +426,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
value: Box::new(Constant { span, user_ty: None, literal: value }),
}
}
thir::InlineAsmOperand::SymFn { expr } => mir::InlineAsmOperand::SymFn {
value: Box::new(this.as_constant(&this.thir[expr])),
},
thir::InlineAsmOperand::SymFn { value, span } => {
mir::InlineAsmOperand::SymFn {
value: Box::new(Constant {
span,
user_ty: None,
literal: value.into(),
}),
}
}
thir::InlineAsmOperand::SymStatic { def_id } => {
mir::InlineAsmOperand::SymStatic { def_id }
}

View file

@ -462,94 +462,55 @@ impl<'tcx> Cx<'tcx> {
operands: asm
.operands
.iter()
.map(|(op, _op_sp)| {
match *op {
hir::InlineAsmOperand::In { reg, ref expr } => {
InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
}
hir::InlineAsmOperand::Out { reg, late, ref expr } => {
InlineAsmOperand::Out {
reg,
late,
expr: expr.as_ref().map(|expr| self.mirror_expr(expr)),
}
}
hir::InlineAsmOperand::InOut { reg, late, ref expr } => {
InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
}
hir::InlineAsmOperand::SplitInOut {
.map(|(op, _op_sp)| match *op {
hir::InlineAsmOperand::In { reg, ref expr } => {
InlineAsmOperand::In { reg, expr: self.mirror_expr(expr) }
}
hir::InlineAsmOperand::Out { reg, late, ref expr } => {
InlineAsmOperand::Out {
reg,
late,
ref in_expr,
ref out_expr,
} => InlineAsmOperand::SplitInOut {
reg,
late,
in_expr: self.mirror_expr(in_expr),
out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
},
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id =
self.tcx.hir().local_def_id(anon_const.hir_id);
let value = mir::ConstantKind::from_anon_const(
self.tcx,
anon_const_def_id,
self.param_env,
);
let span = self.tcx.hir().span(anon_const.hir_id);
InlineAsmOperand::Const { value, span }
expr: expr.as_ref().map(|expr| self.mirror_expr(expr)),
}
hir::InlineAsmOperand::Sym { ref expr } => {
let hir::ExprKind::Path(ref qpath) = expr.kind else {
span_bug!(
expr.span,
"asm `sym` operand should be a path, found {:?}",
expr.kind
);
};
let temp_lifetime =
self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
let ty;
match res {
Res::Def(DefKind::Fn, _) | Res::Def(DefKind::AssocFn, _) => {
ty = self.typeck_results().node_type(expr.hir_id);
let user_ty =
self.user_substs_applied_to_res(expr.hir_id, res);
InlineAsmOperand::SymFn {
expr: self.thir.exprs.push(Expr {
ty,
temp_lifetime,
span: expr.span,
kind: ExprKind::zero_sized_literal(user_ty),
}),
}
}
}
hir::InlineAsmOperand::InOut { reg, late, ref expr } => {
InlineAsmOperand::InOut { reg, late, expr: self.mirror_expr(expr) }
}
hir::InlineAsmOperand::SplitInOut {
reg,
late,
ref in_expr,
ref out_expr,
} => InlineAsmOperand::SplitInOut {
reg,
late,
in_expr: self.mirror_expr(in_expr),
out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
},
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = mir::ConstantKind::from_anon_const(
self.tcx,
anon_const_def_id,
self.param_env,
);
let span = self.tcx.hir().span(anon_const.hir_id);
Res::Def(DefKind::Static(_), def_id) => {
InlineAsmOperand::SymStatic { def_id }
}
InlineAsmOperand::Const { value, span }
}
hir::InlineAsmOperand::SymFn { ref anon_const } => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = mir::ConstantKind::from_anon_const(
self.tcx,
anon_const_def_id,
self.param_env,
);
let span = self.tcx.hir().span(anon_const.hir_id);
_ => {
self.tcx.sess.span_err(
expr.span,
"asm `sym` operand must point to a fn or static",
);
// Not a real fn, but we're not reaching codegen anyways...
ty = self.tcx.ty_error();
InlineAsmOperand::SymFn {
expr: self.thir.exprs.push(Expr {
ty,
temp_lifetime,
span: expr.span,
kind: ExprKind::zero_sized_literal(None),
}),
}
}
}
}
InlineAsmOperand::SymFn { value, span }
}
hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
InlineAsmOperand::SymStatic { def_id }
}
})
.collect(),