2016-05-24 08:15:14 +02:00
|
|
|
/* This file contains code shared by the compiler and the peephole
|
|
|
|
optimizer.
|
|
|
|
*/
|
|
|
|
|
2016-09-11 12:48:15 +02:00
|
|
|
#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 */
|
2016-05-24 08:15:14 +02:00
|
|
|
static int
|
|
|
|
instrsize(unsigned int oparg)
|
|
|
|
{
|
2016-09-11 12:48:15 +02:00
|
|
|
return oparg <= 0xff ? 1 :
|
|
|
|
oparg <= 0xffff ? 2 :
|
|
|
|
oparg <= 0xffffff ? 3 :
|
|
|
|
4;
|
2016-05-24 08:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the
|
|
|
|
desired location of the first EXTENDED_ARG */
|
|
|
|
static void
|
2016-09-11 12:48:15 +02:00
|
|
|
write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
|
2016-05-24 08:15:14 +02:00
|
|
|
unsigned int oparg, int ilen)
|
|
|
|
{
|
|
|
|
switch (ilen) {
|
|
|
|
case 4:
|
2016-09-11 12:48:15 +02:00
|
|
|
*codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 24) & 0xff);
|
2017-08-21 13:09:59 +02:00
|
|
|
/* fall through */
|
2016-09-11 12:48:15 +02:00
|
|
|
case 3:
|
|
|
|
*codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 16) & 0xff);
|
2017-08-21 13:09:59 +02:00
|
|
|
/* fall through */
|
2016-05-24 08:15:14 +02:00
|
|
|
case 2:
|
2016-09-11 12:48:15 +02:00
|
|
|
*codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 8) & 0xff);
|
2017-08-21 13:09:59 +02:00
|
|
|
/* fall through */
|
2016-09-11 12:48:15 +02:00
|
|
|
case 1:
|
|
|
|
*codestr++ = PACKOPARG(opcode, oparg & 0xff);
|
2016-05-24 08:15:14 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-15 03:13:16 +02:00
|
|
|
Py_UNREACHABLE();
|
2016-05-24 08:15:14 +02:00
|
|
|
}
|
|
|
|
}
|