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 | |
| 5 | /* Minimum number of bytes necessary to encode instruction with EXTENDED_ARGs */ |
| 6 | static int |
| 7 | instrsize(unsigned int oparg) |
| 8 | { |
| 9 | return oparg <= 0xff ? 2 : |
| 10 | oparg <= 0xffff ? 4 : |
| 11 | oparg <= 0xffffff ? 6 : |
| 12 | 8; |
| 13 | } |
| 14 | |
| 15 | /* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the |
| 16 | desired location of the first EXTENDED_ARG */ |
| 17 | static void |
| 18 | write_op_arg(unsigned char *codestr, unsigned char opcode, |
| 19 | unsigned int oparg, int ilen) |
| 20 | { |
| 21 | switch (ilen) { |
| 22 | case 8: |
| 23 | *codestr++ = EXTENDED_ARG; |
| 24 | *codestr++ = (oparg >> 24) & 0xff; |
| 25 | case 6: |
| 26 | *codestr++ = EXTENDED_ARG; |
| 27 | *codestr++ = (oparg >> 16) & 0xff; |
| 28 | case 4: |
| 29 | *codestr++ = EXTENDED_ARG; |
| 30 | *codestr++ = (oparg >> 8) & 0xff; |
| 31 | case 2: |
| 32 | *codestr++ = opcode; |
| 33 | *codestr++ = oparg & 0xff; |
| 34 | break; |
| 35 | default: |
| 36 | assert(0); |
| 37 | } |
| 38 | } |