我有一个想法,可以简化使用GDC的扩展asm语法创建D+ asm代码的过程。我想消除在整个地方插入\n\t标记的需要,比方说,使用单独的字符串并让D编译器将它们连接起来。但我愿意接受其他建议。我的尝试失败了,因为连接D字符串不幸不能在编译时在GDC中工作,我需要CTFE。正如你所预料的,这是一项要求,这片糖的成本是零的。
我想我需要用混合蛋白做点什么。有什么关于在CTFE内去哪里和如何停留的建议?
发布于 2018-05-27 12:26:31
GDC是个缺陷,因为扩展内联AssemblerTemplate应该是编译时生成字符串,但实际上不是,你可以做的是生成字符串,把所有的ASM东西放在里面,然后把它混合起来。我一直在使用类似的东西来定制syscall实现(它仍然是内联的)。
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和最不发达国家上工作,如果您愿意的话。你可以用一点编译时间魔法来解决这一切。(编辑)这实际上是有效的。
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或类似的输入操作数。
顺便说一句,谢谢你让我想到这个!我现在需要为我自己的东西实现它。
https://stackoverflow.com/questions/39030201
复制相似问题