1/* This file contains code shared by the compiler and the peephole
2   optimizer.
3 */
4
5#ifdef WORDS_BIGENDIAN
6#  define PACKOPARG(opcode, oparg) ((_Py_CODEUNIT)(((opcode) << 8) | (oparg)))
7#else
8#  define PACKOPARG(opcode, oparg) ((_Py_CODEUNIT)(((oparg) << 8) | (opcode)))
9#endif
10
11/* Minimum number of code units necessary to encode instruction with
12   EXTENDED_ARGs */
13static int
14instrsize(unsigned int oparg)
15{
16    return oparg <= 0xff ? 1 :
17        oparg <= 0xffff ? 2 :
18        oparg <= 0xffffff ? 3 :
19        4;
20}
21
22/* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the
23   desired location of the first EXTENDED_ARG */
24static void
25write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
26    unsigned int oparg, int ilen)
27{
28    switch (ilen) {
29        case 4:
30            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 24) & 0xff);
31        case 3:
32            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 16) & 0xff);
33        case 2:
34            *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 8) & 0xff);
35        case 1:
36            *codestr++ = PACKOPARG(opcode, oparg & 0xff);
37            break;
38        default:
39            assert(0);
40    }
41}
42