Can someone explain this short segment of C ++ code, I can’t make heads or tails out of it

#include <opcodes.h>
const char *getOpcodeName(
    uint8_t op
)
{
    #define OPCODE(x, y) if((0x##y)==op) return "OP_" #x;
        OPCODES
    #undef OPCODE
    return "OP_UNKNOWN";
}

Link to the code here: https://github.com/znort987/blockparser/blob/master/opcodes.cpp

Here is the link to opcodes.h included

I understand that this is just a weirdly formatted function, however I wonder what exactly it means *at the beginning of the function name. I assume this has something to do with pointers?

Also, how do operators #undefand #define? After one of them, there is no semicolon, and one of them is apparently defined as a single-line function. What does it mean (0x##y)? What does it mean return "OP_" #x? I have never come across syntax before.

++, , , . ?

+1
2

++. g++ -Wall -C -E opcodes.cpp > opcodes.i, opcodes.i

#define , .

OPCODES , OPCODE( NOP, 61), -

if ((0x61)==op) return "OP_" "NOP";

, "OP_NOP" .

GCC cpp. stringification ( #, #x; OPCODE), concatenation ( ## (0x##y) OPCODE).

+4

: .

* ! , const char *. char ( ), C-String. C- " " (-, , + ), 0 ( '\0'), !

( ) . , :

  • 0x01 → OP_CODE1
  • 0x02 → OP_CODE2
  • 0x03 → OP_CODE3

:

const char *getOpcodeName( uint8_t op )
{
    if((0x01)==op) return "OP_X";
    if((0x02)==op) return "OP_Y";
    if((0x03)==op) return "OP_Z";
    ...
    if((0x??)==op) return "OP_?";
    return "OP_UNKNOWN";
}

houndreds IF...

#define OPCODE(x, y) if((0x##y)==op) return "OP_" #x;

, OPCODES :

#define OPCODES \
OPCODE( 01, "X" ) \
OPCODE( 02, "Y" ) \
OPCODE( 03, "Z" ) \
...
OPCODE( ??, "?" )

- , ( ). ( Intel):

  inc eax      ; opcode = 0x40
  pusha        ; opcode = 0x60
  nop          ; opcode = 0x90

, :

#define OPCODES \
OPCODE( 40, "INCEAX" ) \
OPCODE( 60, "PUSHA" ) \
OPCODE( 90, "NOP" )
+2

Source: https://habr.com/ru/post/1569423/


All Articles