0

I have a list of opcodes declared like this:

enum OpCode {
    PUSH = 0x01, 
    POP  = 0x02,
    DUP  = 0x03,
    //...etc (the list is too long to copy-paste it here and it wouldn't make a lot of sense)
}

What I want is to "automatically" (via macros):

  • create an array of their string representations, like: static const char *OpCodeStr[] = {"PUSH","POP","DUP",...};
  • create an array of some labels (for dispatch) using the different enum values, like: static void* dispatchTable[] = {&&PUSH_, &&POP_, &&DUP_, ...};

I believe it should be possible using C macros, but I still have not figured out how.

Any ideas?

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 1
    you asked 2 questions. The first bullet can probably be solved with the duplicate I linked to. The other one is unclear. What is `&&PUSH_` ? an address of an address? Honestly you'd be better off with a script generating this stuff for you – Jean-François Fabre Jan 06 '20 at 07:30
  • @Jean-FrançoisFabre This for a computed-goto implementation (dispatch table for a bytecode interpreter). – Dr.Kameleon Jan 06 '20 at 07:32
  • 1
    the answer: https://stackoverflow.com/a/10966395/6451573 using x-macros can be applied to your problem, with 3 different blocks (the latter for your dispatch table) – Jean-François Fabre Jan 06 '20 at 07:35
  • @Jean-FrançoisFabre I just solved it! Thanks! – Dr.Kameleon Jan 06 '20 at 08:14

0 Answers0