是否有专门设计用于以跨平台方式使用汇编指令对位操作进行 1 到 1 编译的 C 函数或宏? [英] Are there C functions or macros specifically designed to compile 1 to 1 with assembly instructions for bit manipulations in a cross-platform manner?

查看:12
本文介绍了是否有专门设计用于以跨平台方式使用汇编指令对位操作进行 1 到 1 编译的 C 函数或宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及仿真的项目(如果您查看我的帖子历史记录,您会看到我走了多远!)并且我正在寻找使用 C 进行伪二进制翻译并使用优化器和/或编译器使用 C 代码将我的 switch 语句内容编译为单个汇编指令,主要用于非常标准的指令,例如 movs、addSR 和其他简单的位操作和算术指令.我希望同时为 ARM 和 x86-64 执行此操作,并在两个程序集中尽可能少地编写它.

I've got a project involving emulation (If you look at my post history, you'll see how far I've come!) and I'm looking to do pseudo-binary-translation using C and playing with the optimizers and/or compilers to use C code that compiles my switch statement contents to a single assembly instruction, primarily for very standard instructions such as movs, add, SR and other simple bit manipulations and arithmetic instructions. I'm hoping to do this for ARM and x86-64 at the same time, writing as little of it in both assemblies as possible.

如果我描述的东西不存在,那么我想知道是否有某种汇编语言"可以用来编写我的代码,然后将该程序集编译为 x86-64 和 ARM.

If the thing I'm describing doesn't exist, then I wonder if there's some sort of "assembly language" that I can use to write my code and then compile that assembly into x86-64 and ARM.

推荐答案

明确回答这部分:

...然后我想知道是否有某种我可以使用的汇编语言"用于编写我的代码,然后将该程序集编译为 x86-64 和ARM.

... then I wonder if there's some sort of "assembly language" that I can use to write my code and then compile that assembly into x86-64 and ARM.

这正是 LLVM IR 的目标.

LLVM 表示的目标是轻量级和低级,同时同时具有表现力、类型化和可扩展性.它旨在通过处于足够低的水平,成为某种通用 IR"高层次的想法可以清晰地映射到它(类似于如何微处理器是通用 IR",允许使用多种源语言映射到它们).

The LLVM representation aims to be light-weight and low-level while being expressive, typed, and extensible at the same time. It aims to be a "universal IR" of sorts, by being at a low enough level that high-level ideas may be cleanly mapped to it (similar to how microprocessors are "universal IR’s", allowing many source languages to be mapped to them).

对于示例:

你可以代表这个C函数

int mul_add(int x, int y, int z) {
  return x * y + z;
}

使用这个 LLVM IR

with this LLVM IR

define i32 @mul_add(i32 %x, i32 %y, i32 %z) {
entry:
  %tmp = mul i32 %x, %y
  %tmp2 = add i32 %tmp, %z
  ret i32 %tmp2
}

这篇关于是否有专门设计用于以跨平台方式使用汇编指令对位操作进行 1 到 1 编译的 C 函数或宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆