You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

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