wordcode_helpers.h 1.25 KB
Newer Older
1 2 3 4
/* This file contains code shared by the compiler and the peephole
   optimizer.
 */

5 6 7 8 9 10 11 12
#ifdef WORDS_BIGENDIAN
#  define PACKOPARG(opcode, oparg) ((_Py_CODEUNIT)(((opcode) << 8) | (oparg)))
#else
#  define PACKOPARG(opcode, oparg) ((_Py_CODEUNIT)(((oparg) << 8) | (opcode)))
#endif

/* Minimum number of code units necessary to encode instruction with
   EXTENDED_ARGs */
13 14 15
static int
instrsize(unsigned int oparg)
{
16 17 18 19
    return oparg <= 0xff ? 1 :
        oparg <= 0xffff ? 2 :
        oparg <= 0xffffff ? 3 :
        4;
20 21 22 23 24
}

/* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the
   desired location of the first EXTENDED_ARG */
static void
25
write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
26 27 28 29
    unsigned int oparg, int ilen)
{
    switch (ilen) {
        case 4:
30
            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 24) & 0xff);
31
            /* fall through */
32 33
        case 3:
            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 16) & 0xff);
34
            /* fall through */
35
        case 2:
36
            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 8) & 0xff);
37
            /* fall through */
38 39
        case 1:
            *codestr++ = PACKOPARG(opcode, oparg & 0xff);
40 41
            break;
        default:
Barry Warsaw's avatar
Barry Warsaw committed
42
            Py_UNREACHABLE();
43 44
    }
}