D - 更友好的asm语法糖用于GDC(Dlang)

时间:2016-08-19 02:24:14

标签: d inline-assembly gdc

我有一个想法是使用GDC扩展的asm语法来简化创建D plus asm代码的过程。我想摆脱在整个地方插入\ n \ t标记的需要,比如说,有单独的字符串并让D编译器连接它们。但我对其他建议持开放态度。我的尝试失败了,因为连接D字符串遗憾的是在编译时没有在GDC 中工作我需要CTFE。正如您所预料的那样,这块糖的成本是零的。

我怀疑,我需要用mixin做点什么。关于去哪里以及如何留在CTFE的任何提示?

1 个答案:

答案 0 :(得分:2)

GDC的错误在于扩展内联ASM中的 AssemblerTemplate 假设是编译时生成字符串,但实际上并非如此。您可以做的是生成字符串,将所有ASM内容放在其周围,然后将其混合。我已经使用类似的自定义系统调用实现(仍然内联)。< / p>

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或类似内容下的输入操作数。
    • 这是我的第一次尝试尝试,并且更好的方法来完成此操作并简化我忽略的代码。
顺便说一下,谢谢你让我想起这个!我现在需要为自己的东西实现它。