C++ - 如何消除宏? (将应用程序移植到 ie c# 中)
我创建了简单的虚拟机,为了创建指令字节和开关,我使用 C++ 宏,它看起来像这样(这是简化的示例):
typedef t_sint_b32 int;
typedef t_sint_b16 short;
(...)
typedef t_sfloat_b32 float;
enum InstructionOpSize{b8,b16,b32,b64};
enum InstructionOpSign{s, u};
enum InstructionOpType{int_, float_};
enum InstructionFunc{MOV, ADD, SUB, ...};
#define CreateInstruction(size1, sign1, type1, size2, ...) ((size1) | (sign1 << 3) | (type1 << 4) | ...)
#define CASE_INSTRUCTION(size1, sign1, type1, size2, ..., operation) case CreateInstruction(size1, sign1, type1, size2, ...): ((t_##sign1##type1##size1) ARG1) operation ((t_##sign2##type2##size2) ARG2); break;
#define MULTI_CASE(size1, sign1, type1, ...)\
CASE_INSTRUCTION(size1, sign1, type1, b8, u, int_,...);\
(...)
CASE_INSTRUCTION(size1, sign1, type1, b64, s, float_,...);
#define MULTI_MULTI_CASE(...)\
MULTI_CASE(b8, u, int_,..);\
(...)
MULTI_CASE(b64, s, float_,..);
然后有一个巨大的开关:
switch(opcode){
MULTI_MULTI_CASE(ADD, +=);
MULTI_MULTI_CASE(SUB, -=);
MULTI_MULTI_CASE(MUL, *=);
(...)
}
您如何看到毫不费力地生成了大量代码(只有 10 种类型的变量 - 8 个整数和 2 个浮点数 - 和 4 个函数,其 10*10*4 行代码),但将来我想将其移至不支持宏的语言(我正在考虑 c#或者爪哇)。我想到的唯一想法是制作代码生成器,它将生成代码以粘贴到虚拟机代码中,但代码将不可读,并且很难更改和维护。也许您有一些好主意要分享? :)
I've created simple virtual machine, and for creating instructions bytes and switch I use c++ macros, it looks like this (thats simplified example):
typedef t_sint_b32 int;
typedef t_sint_b16 short;
(...)
typedef t_sfloat_b32 float;
enum InstructionOpSize{b8,b16,b32,b64};
enum InstructionOpSign{s, u};
enum InstructionOpType{int_, float_};
enum InstructionFunc{MOV, ADD, SUB, ...};
#define CreateInstruction(size1, sign1, type1, size2, ...) ((size1) | (sign1 << 3) | (type1 << 4) | ...)
#define CASE_INSTRUCTION(size1, sign1, type1, size2, ..., operation) case CreateInstruction(size1, sign1, type1, size2, ...): ((t_##sign1##type1##size1) ARG1) operation ((t_##sign2##type2##size2) ARG2); break;
#define MULTI_CASE(size1, sign1, type1, ...)\
CASE_INSTRUCTION(size1, sign1, type1, b8, u, int_,...);\
(...)
CASE_INSTRUCTION(size1, sign1, type1, b64, s, float_,...);
#define MULTI_MULTI_CASE(...)\
MULTI_CASE(b8, u, int_,..);\
(...)
MULTI_CASE(b64, s, float_,..);
And then there is huge switch:
switch(opcode){
MULTI_MULTI_CASE(ADD, +=);
MULTI_MULTI_CASE(SUB, -=);
MULTI_MULTI_CASE(MUL, *=);
(...)
}
How you can see there is lots of code generated with little effort (with only 10 types of variables - 8 ints and 2 floats - and 4 functions its 10*10*4 lines of code), but in future I'd like to move it to language not supporting macros (I'm thinking of c# or Java). Only idea I came up was to make code generator, which will be generating code to paste into vm code, but then code won't be readable and it will be hard to change sth and maintain. Maybe you have some great ideas to share? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是转移到外部宏处理器(例如 M4),然后使其成为构建过程的一部分,以便在编译之前生成宏扩展版本。基本上,您只需从 C 预处理器移动到外部预处理器(例如 M4)。
另一种选择是手动重构代码:)
One option is to move to an external macro processor (e.g. M4) and then make it part of your build process to produce the macro-expanded version before it is compiled. Basically you just move from the C preprocessor to an external preprocessor (e.g. M4).
Another alternative is to restructure the code by hand :)