我有一个想法,可以使用 GDC 的扩展 asm 语法来简化创建 D plus asm 代码的过程。我想摆脱在各处插入\n\t 标记的需要,例如,通过使用单独的字符串并让 D 编译器连接它们。但我愿意接受其他建议。我的尝试失败了,因为不幸的是,连接 D 字符串在编译时在 GDC 中不起作用,而我需要 CTFE。正如您所料,这 block 糖的成本为零。
我怀疑我需要用 mixin 做点什么。关于去哪里以及如何留在 CTFE 内有什么建议吗?
最佳答案
GDC 存在缺陷,因为扩展内联 ASM 中的AssemblerTemplate应该是编译时生成字符串,但实际上不是。您可以做的是生成字符串,将所有 ASM 内容放在它周围,然后将其混合在一起。我一直在使用类似的东西来实现自定义系统调用(仍然是内联的)。
module test;
template joinASMStatements(T...) {
static if (T.length == 0) enum joinASMStatements = "";
else static if (T.length == 1) enum joinASMStatements = T[0];
else enum joinASMStatements = joinASMStatements!(T[0..$/2]) ~ "\\n\\t" ~ joinASMStatements!(T[$/2..$]);
}
void main() {
ulong dst, src = 20;
enum statements = joinASMStatements!("mov %[dst], %[src]");
mixin("asm {" ~ statements ~ " : [dst] \"rm,r\" dst : [src] \"ri,rmi\" src }");
}
但坦率地说,这看起来很可怕。创建一个模板来为您处理所有这些事情会更容易、更美观,它需要一个字符串数组。您可以在模板中实现额外的内容来处理某些操作码并根据它们自动添加约束。如果您愿意的话,这将使代码也能够在 DMD 和 LDC 上运行。您可以使用一些编译时魔法来解决这一切。 (编辑)这确实有效。
module test2;
import std.traits: AliasSeq;
// Input operand type
struct IOp(string _name) {
string constraints; // A set of constraints. This is the whole thing.
string asmName; // A label to be given to the operand (the "[<name>]" thing)
enum name = _name; // Inner usage, to ease accessing `_name`.
}
// Output operand type
struct OOp(string _name) {
// For variable details, see IOp comments.
string constraints;
string asmName;
enum name = _name;
}
// type for register (and "cc" and "memory") clobbers
struct Clobber(string _reg) {enum reg = _reg;}
// type for goto labels
struct Goto(string _goto) {enum name = _goto;} // note that `goto` is a D keyword.
// filters out types as S!(string blah)
template filterOp(alias S, T...) {
static if (T.length == 0) alias filterOp = AliasSeq!();
else static if (T.length == 1) {
static if (is(typeof(T[0]) : S!(N), string N))
alias filterOp = AliasSeq!(T[0]);
else
alias filterOp = AliasSeq!();
} else
alias filterOp = AliasSeq!(filterOp!(S, T[0..$/2]), filterOp!(S, T[$/2..$]));
}
// joiner function for input and output operands.
template joinOps(T...) {
static if (T.length == 0) enum joinOps = "";
else static if (T.length == 1) enum joinOps = ((T[0].asmName != "")?"[" ~ T[0].asmName ~ "] ":"") ~ "\"" ~ T[0].constraints ~ "\" " ~ T[0].name; // The .name unescapes the name
else enum joinOps = joinOps!(T[0..$/2]) ~ ", " ~ joinOps!(T[$/2..$]);
}
// joiner function for clobbers
template joinClobbers(T...) {
static if (T.length == 0) enum joinClobbers = "";
else static if (T.length == 1) enum joinClobbers = "\"" ~ T[0].reg ~ "\"";
else enum joinClobbers = joinClobbers!(T[0..$/2]) ~ ", " ~ joinClobbers!(T[$/2..$]);
}
// joiner function for goto labels
template joinGotos(T...) {
static if (T.length == 0) enum joinGotos = "";
else static if (T.length == 1) enum joinGotos = T[0].name; // Here the label is unescaped
else enum joinGotos = joinGotos!(T[0..$/2]) ~ ", " ~ joinGotos!(T[$/2..$]); // Recursively executes itself on halves of the input. Eventually, the halves become lengths of `1` or `0`, and they are actually evaluated.
}
// joiner function for instructions.
template joinInstrs(string[] instrs) {
static if (instrs.length == 0) enum joinInstrs = "";
else static if (instrs.length == 1) enum joinInstrs = instrs[0];
else enum joinInstrs = joinInstrs!(instrs[0..$/2]) ~ "\\n\\t" ~ joinInstrs!(instrs[$/2..$]);
}
// complete assembly generator function. Output is to be mixed in.
template ASM(string[] ops, T...) {
enum iops = joinOps!(filterOp!(IOp, T));
enum oops = joinOps!(filterOp!(OOp, T));
enum clobbers = joinClobbers!(filterOp!(Clobber, T));
enum gotos = joinGotos!(filterOp!(Goto, T));
enum instrs = "\"" ~ joinInstrs!(ops) ~ "\"";
enum ASM = "asm { " ~ instrs ~ " : " ~ oops ~ " : " ~ iops ~ " : " ~ clobbers ~ " : " ~ gotos ~ "; }";
}
void main() {
ulong src = 24, dst;
mixin(ASM!(["mov %[dst], %[src]"], IOp!"src"("ri,rmi", "src"), OOp!"dst"("=rm,r", "dst")));
}
注释:
- 您可能希望结合
IOp
和OOp
并根据约束区分输入和输出,而不是向输出约束添加=
(请参阅GCC 文档中有效的输出约束)。其他所有内容都将构成共享struct Op
或类似下的输入操作数。- 这是我的第一次尝试,有更好的方法来做到这一点并简化我忽略的代码。
顺便说一句,谢谢你让我想到这一点!我现在需要为我自己的东西实现它。
关于D - GDC 更友好的 asm 语法糖 (Dlang),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39030201/