Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1 | /* This file contains code shared by the compiler and the peephole |
| 2 | optimizer. |
| 3 | */ |
| 4 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 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 */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 13 | static int |
| 14 | instrsize(unsigned int oparg) |
| 15 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 16 | return oparg <= 0xff ? 1 : |
| 17 | oparg <= 0xffff ? 2 : |
| 18 | oparg <= 0xffffff ? 3 : |
| 19 | 4; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 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 */ |
| 24 | static void |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 25 | write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 26 | unsigned int oparg, int ilen) |
| 27 | { |
| 28 | switch (ilen) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 29 | case 4: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 30 | *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 24) & 0xff); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 31 | /* fall through */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 32 | case 3: |
| 33 | *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 16) & 0xff); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 34 | /* fall through */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 35 | case 2: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 36 | *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 8) & 0xff); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 37 | /* fall through */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 38 | case 1: |
| 39 | *codestr++ = PACKOPARG(opcode, oparg & 0xff); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 40 | break; |
| 41 | default: |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 42 | Py_UNREACHABLE(); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 43 | } |
| 44 | } |