On Thursday, 20 July 2017 at 22:02:32 UTC, Walter Bright wrote:
On 7/20/2017 2:21 PM, Stefan Koch wrote:
Please tell me this is not going to get into dmd :)
templates are so much more expensive then macros.
(Well, for now :) )

Those templates can and should be replaced by CTFE.

If you like, present the CTFE solution. Should be fun!

My pleasure :)

string itos(uint n)
{
    char[] result = [];
    immutable len = 10;
    result.length = len;
    uint i = len - 1;

    while (n > 10)
    {
        result[i--] = cast(char) ('0' + (n % 10));
        n /= 10;
    }
    result[i] = cast(char) ('0' + (n % 10));

    return cast(string) result[i .. $];
}


mixin((){
    static struct X
    {
        string id;
        ubyte reg;
        uint mask;
        ubyte ty;
    }

    enum Y = [
        //  id   reg  mask   ty
        X("AH",   4, mAX, TYuchar),
        X("AL",   0, mAX, TYuchar),
        X("AX",   8, mAX, TYushort),
        X("BH",   7, mBX, TYuchar),
        X("BL",   3, mBX, TYuchar),
        X("BP",  13,   0, TYushort),
        X("BX",  11, mBX, TYushort),
        X("CH",   5, mCX, TYuchar),
        X("CL",   1, mCX, TYuchar),
        X("CX",   9, mCX, TYushort),
        X("DH",   6, mDX, TYuchar),
        X("DI",  15, mDI, TYushort),
        X("DL",   2, mDX, TYuchar),
        X("DX",  10, mDX, TYushort),
        X("EAX", 16, mAX, TYulong),
        X("EBP", 21,   0, TYulong),
        X("EBX", 19, mBX, TYulong),
        X("ECX", 17, mCX, TYulong),
        X("EDI", 23, mDI, TYulong),
        X("EDX", 18, mDX, TYulong),
        X("ESI", 22, mSI, TYulong),
        X("ESP", 20,   0, TYulong),
        X("SI",  14, mSI, TYushort),
        X("SP",  12,   0, TYushort),
    ];


    enum lns = itos(Y.length);

string pseudotab = "\nprivate __gshared static immutable string[" ~ lns ~ "] pseudotab = ["; string pseudoreg = "\nprivate __gshared static immutable ubyte[" ~ lns ~ "] pseudoreg = ["; string pseudomask = "\nprivate __gshared static immutable regm_t[" ~ lns ~ "] pseudomask = ["; string pseudoty = "\nprivate __gshared static immutable ubyte[" ~ lns ~ "] pseudoty = [";

    foreach(i, r; Y)
    {
        pseudotab ~= `"` ~ r.id ~ `", `;
        pseudoreg ~= itos(r.reg) ~ `, `;
        pseudomask ~= itos(r.mask) ~ `, `;
        pseudoty ~=  itos(r.ty) ~ `, `;
    }


    pseudotab ~= "];\n";
    pseudoreg ~= "];\n";
    pseudomask ~= "];\n";
    pseudoty ~=  "];\n";


    return pseudotab ~ pseudoreg ~ pseudomask ~ pseudoty;
}());

Reply via email to