1

无法为此想出正确的标题,但是当我为我的程序设置要使用的 const 数据时,似乎总是会出现这个问题。我这里的程序基本上是一个轻量级的汇编器,作为我自定义代码编译器的一小部分。

CAssembler.h

struct sOperand
{
    bool used;
    eOperandType type;
    int size;
};

struct sOpcodeDefinition
{
    bool used;
    BYTE opcode;
    BYTE extended_opcode;
    sOperand operands[5];
};

struct sInstructionDefinition
{
    eMnemonicInstruction instruction;
    sOpcodeDefinition opcodes[16];
};

CAssembler.cpp

#define MNEMONIC(a,b) {a,b}
#define OPCODE(a,b,c) {true,OPCODE_##a,b,c}
#define OPERAND(a,b) {true,(eOperandType)OPERAND_##a,b}

sInstructionDefinition OpcodeDefinitionTable[NUM_X86_OPCODES] = {
    MNEMONIC(INC,
        OPCODE(INC_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(INC_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(INC_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(INC_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(INC_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(INC_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(INC_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(INC_DI, 0, OPERAND(REGISTER_DI, 4))
        ),
    MNEMONIC(DEC,
        OPCODE(DEC_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(DEC_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(DEC_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(DEC_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(DEC_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(DEC_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(DEC_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(DEC_DI, 0, OPERAND(REGISTER_DI, 4))
        ),
    MNEMONIC(PUSH,
        OPCODE(PUSH_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(PUSH_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(PUSH_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(PUSH_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(PUSH_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(PUSH_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(PUSH_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(PUSH_DI, 0, OPERAND(REGISTER_DI, 4))
        ),
    MNEMONIC(POP,
        OPCODE(POP_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(POP_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(POP_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(POP_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(POP_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(POP_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(POP_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(POP_DI, 0, OPERAND(REGISTER_DI, 4))
        ),
};

显然,我的目标是拥有一个 sInstructionDefinition 数组并填充它们的对象数组成员等。

不幸的是,每个 sInstructionDefinition 的第二个 sOpcodeDefinition 在编译代码中不存在。我认为同样的情况也可能开始发生在 sOperand 成员身上。任何想法为什么以及初始化这个大型嵌套结构的正确方法是什么?

4

2 回答 2

1

使用可变参数宏找到了一个很好的解决方案...奇怪的是,如何在 { } 中手动包装 OPCODE 用法并不起作用。但是现在,第 2、第 3、第 4 等 sOpcodeDefinition 定义现在已正确编译。

#define MNEMONIC(a,...) {a,{__VA_ARGS__}}
#define OPCODE(a,b,...) {true,OPCODE_##a,b,{__VA_ARGS__}}
#define OPERAND(a,b) {true,(eOperandType)OPERAND_##a,b}
#define NULL_OPERAND {false,BAD_OPERAND,0}

sInstructionDefinition OpcodeDefinitionTable[NUM_MNEMONICS] = {
    MNEMONIC(INC,
        OPCODE(INC_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(INC_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(INC_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(INC_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(INC_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(INC_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(INC_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(INC_DI, 0, OPERAND(REGISTER_DI, 4))
    ),
    MNEMONIC(DEC,
        OPCODE(DEC_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(DEC_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(DEC_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(DEC_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(DEC_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(DEC_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(DEC_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(DEC_DI, 0, OPERAND(REGISTER_DI, 4))
    ),
    MNEMONIC(PUSH,
        OPCODE(PUSH_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(PUSH_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(PUSH_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(PUSH_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(PUSH_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(PUSH_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(PUSH_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(PUSH_DI, 0, OPERAND(REGISTER_DI, 4))
    ),
    MNEMONIC(POP,
        OPCODE(POP_AX, 0, OPERAND(REGISTER_AX, 4)),
        OPCODE(POP_CX, 0, OPERAND(REGISTER_CX, 4)),
        OPCODE(POP_DX, 0, OPERAND(REGISTER_DX, 4)),
        OPCODE(POP_BX, 0, OPERAND(REGISTER_BX, 4)),
        OPCODE(POP_SP, 0, OPERAND(REGISTER_SP, 4)),
        OPCODE(POP_BP, 0, OPERAND(REGISTER_BP, 4)),
        OPCODE(POP_SI, 0, OPERAND(REGISTER_SI, 4)),
        OPCODE(POP_DI, 0, OPERAND(REGISTER_DI, 4))
    ),
};
于 2013-07-28T11:17:06.663 回答
1

您的MNEMONIC宏需要 2 个参数,但您提供 9 个。您可能打算写:

   MNEMONIC(INC,
        {
        OPCODE(INC_AX, 0, {OPERAND(REGISTER_AX, 4)}),
        OPCODE(INC_CX, 0, {OPERAND(REGISTER_CX, 4)}),
        // etc..
        }
     ) // etc

请注意,操作码宏中的花括号初始化结构,而上面代码中的额外花括号用于初始化数组。

编辑:请注意,上面的代码仍然不起作用,因为预处理器仍会将逗号视为参数分隔符。

于 2013-07-28T10:00:54.420 回答