Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d37f2f

Browse files
committed
cleanup a bit and reuse instrsize (instruction size). working towards fixing problems with EXTENDED_ARG
1 parent 1e86beb commit 7d37f2f

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

Python/compile.c

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,13 +3696,11 @@ assemble_free(struct assembler *a)
36963696
static int
36973697
instrsize(struct instr *instr)
36983698
{
3699-
int size = 1;
3700-
if (instr->i_hasarg) {
3701-
size += 2;
3702-
if (instr->i_oparg >> 16)
3703-
size += 2;
3704-
}
3705-
return size;
3699+
if (!instr->i_hasarg)
3700+
return 1;
3701+
if (instr->i_oparg > 0xffff)
3702+
return 6;
3703+
return 3;
37063704
}
37073705

37083706
static int
@@ -3851,42 +3849,40 @@ assemble_lnotab(struct assembler *a, struct instr *i)
38513849
static int
38523850
assemble_emit(struct assembler *a, struct instr *i)
38533851
{
3854-
int arg = 0, size = 0, ext = i->i_oparg >> 16;
3852+
int size, arg = 0, ext = 0;
38553853
int len = PyString_GET_SIZE(a->a_bytecode);
38563854
char *code;
38573855

3858-
if (!i->i_hasarg)
3859-
size = 1;
3860-
else {
3861-
if (ext)
3862-
size = 6;
3863-
else
3864-
size = 3;
3856+
size = instrsize(i);
3857+
if (i->i_hasarg) {
38653858
arg = i->i_oparg;
3859+
ext = arg >> 16;
38663860
}
38673861
if (i->i_lineno && !assemble_lnotab(a, i))
3868-
return 0;
3862+
return 0;
38693863
if (a->a_offset + size >= len) {
38703864
if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
38713865
return 0;
38723866
}
38733867
code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
38743868
a->a_offset += size;
3875-
if (ext > 0) {
3876-
*code++ = (char)EXTENDED_ARG;
3877-
*code++ = ext & 0xff;
3878-
*code++ = ext >> 8;
3879-
arg &= 0xffff;
3869+
if (size == 6) {
3870+
assert(i->i_hasarg);
3871+
*code++ = (char)EXTENDED_ARG;
3872+
*code++ = ext & 0xff;
3873+
*code++ = ext >> 8;
3874+
arg &= 0xffff;
38803875
}
38813876
*code++ = i->i_opcode;
3882-
if (size == 1)
3883-
return 1;
3884-
*code++ = arg & 0xff;
3885-
*code++ = arg >> 8;
3877+
if (i->i_hasarg) {
3878+
assert(size == 3 || size == 6);
3879+
*code++ = arg & 0xff;
3880+
*code++ = arg >> 8;
3881+
}
38863882
return 1;
38873883
}
38883884

3889-
static int
3885+
static void
38903886
assemble_jump_offsets(struct assembler *a, struct compiler *c)
38913887
{
38923888
basicblock *b;
@@ -3896,7 +3892,7 @@ assemble_jump_offsets(struct assembler *a, struct compiler *c)
38963892
/* Compute the size of each block and fixup jump args.
38973893
Replace block pointer with position in bytecode. */
38983894
for (i = a->a_nblocks - 1; i >= 0; i--) {
3899-
basicblock *b = a->a_postorder[i];
3895+
b = a->a_postorder[i];
39003896
bsize = blocksize(b);
39013897
b->b_offset = totsize;
39023898
totsize += bsize;
@@ -3918,7 +3914,6 @@ assemble_jump_offsets(struct assembler *a, struct compiler *c)
39183914
}
39193915
}
39203916
}
3921-
return 1;
39223917
}
39233918

39243919
static PyObject *
@@ -4079,8 +4074,7 @@ assemble(struct compiler *c, int addNone)
40794074
dfs(c, entryblock, &a);
40804075

40814076
/* Can't modify the bytecode after computing jump offsets. */
4082-
if (!assemble_jump_offsets(&a, c))
4083-
goto error;
4077+
assemble_jump_offsets(&a, c);
40844078

40854079
/* Emit code in reverse postorder from dfs. */
40864080
for (i = a.a_nblocks - 1; i >= 0; i--) {

0 commit comments

Comments
 (0)