Rust 编译器对中断的支持

Rust compiler support for interrupts

我正在用 Rust 为 AMR 板编写裸机应用程序,其中涉及中断服务例程。目前,我在自己的汇编程序 prolog/epilog 中使用 #naked 函数。但是,我想知道是否有我错过的更好(并且希望更便携)的方式,也许是 Rust nightly 中的 #interrupt-like 属性或任何其他编译器支持。我认为与 GCC 的 __attribute__ ((interrupt ("IRQ"))) 一致,因为 Rust 的后端 LLVM 提供了这样一个属性。

复制这里的资料,不要脸;)

https://github.com/nix-rust/nix

https://users.rust-lang.org/t/unix-signals-in-rust/733/3

use nix::sys::signal;

extern fn handle_sigint(_:i32) {
  println!("Interrupted!");
  panic!();
}

fn main() {
  let sig_action = signal::SigAction::new(handle_sigint,
                                          signal::SockFlag::empty(),
                                          signal::SigSet::empty());
  signal::sigaction(signal::SIGINT, &sig_action);
}

中断只是另一种调用约定。对于 Rust 的 AVR 端口,我们添加了两种新的调用约定,一种用于 AVR 支持的每种中断。

调用约定的权威列表is the source code。 Rust 1.16 列出了这些:

#[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
pub enum Abi {
    // NB: This ordering MUST match the AbiDatas array below.
    // (This is ensured by the test indices_are_correct().)

    // Single platform ABIs
    Cdecl,
    Stdcall,
    Fastcall,
    Vectorcall,
    Aapcs,
    Win64,
    SysV64,
    PtxKernel,
    Msp430Interrupt,

    // Multiplatform / generic ABIs
    Rust,
    C,
    System,
    RustIntrinsic,
    RustCall,
    PlatformIntrinsic,
    Unadjusted
}

unstable book also mentions that the different calling conventions exist.

要使用这些,您需要用它声明您的函数:

#![feature(abi_msp430_interrupt)] 

extern "msp430-interrupt" fn handler() {}

将函数注册为中断向量的异常处理程序仍然取决于您table(或等效)。

当然,您可能需要提交一个 PR,告知 Rust 前端要使用的特定 LLVM 调用约定,如果您的调用约定不在此列表中的话。