summaryrefslogtreecommitdiff
path: root/Python/wordcode_helpers.h
diff options
context:
space:
mode:
authorMariatta Wijaya <mariatta.wijaya@gmail.com>2017-02-06 20:16:58 -0800
committerMariatta Wijaya <mariatta.wijaya@gmail.com>2017-02-06 20:16:58 -0800
commitda79bcf8ac7ae72218ab023e1ed54390bc1a3a27 (patch)
tree74845e2dbd9521d9748b9c32f1922f4123083bf3 /Python/wordcode_helpers.h
parente3c7e835bdfc97750eb9b7fc0ad2493108c2d438 (diff)
parent1fe806ac56f8b83694d24ab604eb695d00bc8497 (diff)
downloadcpython-da79bcf8ac7ae72218ab023e1ed54390bc1a3a27.tar.gz
Issue #29371: merge with 3.5
Diffstat (limited to 'Python/wordcode_helpers.h')
-rw-r--r--Python/wordcode_helpers.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/Python/wordcode_helpers.h b/Python/wordcode_helpers.h
new file mode 100644
index 0000000000..b0e3a91776
--- /dev/null
+++ b/Python/wordcode_helpers.h
@@ -0,0 +1,41 @@
+/* This file contains code shared by the compiler and the peephole
+ optimizer.
+ */
+
+#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 */
+static int
+instrsize(unsigned int oparg)
+{
+ return oparg <= 0xff ? 1 :
+ oparg <= 0xffff ? 2 :
+ oparg <= 0xffffff ? 3 :
+ 4;
+}
+
+/* Spits out op/oparg pair using ilen bytes. codestr should be pointed at the
+ desired location of the first EXTENDED_ARG */
+static void
+write_op_arg(_Py_CODEUNIT *codestr, unsigned char opcode,
+ unsigned int oparg, int ilen)
+{
+ switch (ilen) {
+ case 4:
+ *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 24) & 0xff);
+ case 3:
+ *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 16) & 0xff);
+ case 2:
+ *codestr++ = PACKOPARG(EXTENDED_ARG, (oparg >> 8) & 0xff);
+ case 1:
+ *codestr++ = PACKOPARG(opcode, oparg & 0xff);
+ break;
+ default:
+ assert(0);
+ }
+}