无法为此想出正确的标题,但是当我为我的程序设置要使用的 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 成员身上。任何想法为什么以及初始化这个大型嵌套结构的正确方法是什么?