cleanup a bit and reuse instrsize (instruction size). working towards fixing problems with EXTENDED_ARG

This commit is contained in:
Neal Norwitz 2005-10-23 22:40:47 +00:00
parent 1e86beb3f8
commit 7d37f2ff40
1 changed files with 24 additions and 30 deletions

View File

@ -3696,13 +3696,11 @@ assemble_free(struct assembler *a)
static int static int
instrsize(struct instr *instr) instrsize(struct instr *instr)
{ {
int size = 1; if (!instr->i_hasarg)
if (instr->i_hasarg) { return 1;
size += 2; if (instr->i_oparg > 0xffff)
if (instr->i_oparg >> 16) return 6;
size += 2; return 3;
}
return size;
} }
static int static int
@ -3851,42 +3849,40 @@ assemble_lnotab(struct assembler *a, struct instr *i)
static int static int
assemble_emit(struct assembler *a, struct instr *i) assemble_emit(struct assembler *a, struct instr *i)
{ {
int arg = 0, size = 0, ext = i->i_oparg >> 16; int size, arg = 0, ext = 0;
int len = PyString_GET_SIZE(a->a_bytecode); int len = PyString_GET_SIZE(a->a_bytecode);
char *code; char *code;
if (!i->i_hasarg) size = instrsize(i);
size = 1; if (i->i_hasarg) {
else {
if (ext)
size = 6;
else
size = 3;
arg = i->i_oparg; arg = i->i_oparg;
ext = arg >> 16;
} }
if (i->i_lineno && !assemble_lnotab(a, i)) if (i->i_lineno && !assemble_lnotab(a, i))
return 0; return 0;
if (a->a_offset + size >= len) { if (a->a_offset + size >= len) {
if (_PyString_Resize(&a->a_bytecode, len * 2) < 0) if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
return 0; return 0;
} }
code = PyString_AS_STRING(a->a_bytecode) + a->a_offset; code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
a->a_offset += size; a->a_offset += size;
if (ext > 0) { if (size == 6) {
*code++ = (char)EXTENDED_ARG; assert(i->i_hasarg);
*code++ = ext & 0xff; *code++ = (char)EXTENDED_ARG;
*code++ = ext >> 8; *code++ = ext & 0xff;
arg &= 0xffff; *code++ = ext >> 8;
arg &= 0xffff;
} }
*code++ = i->i_opcode; *code++ = i->i_opcode;
if (size == 1) if (i->i_hasarg) {
return 1; assert(size == 3 || size == 6);
*code++ = arg & 0xff; *code++ = arg & 0xff;
*code++ = arg >> 8; *code++ = arg >> 8;
}
return 1; return 1;
} }
static int static void
assemble_jump_offsets(struct assembler *a, struct compiler *c) assemble_jump_offsets(struct assembler *a, struct compiler *c)
{ {
basicblock *b; basicblock *b;
@ -3896,7 +3892,7 @@ assemble_jump_offsets(struct assembler *a, struct compiler *c)
/* Compute the size of each block and fixup jump args. /* Compute the size of each block and fixup jump args.
Replace block pointer with position in bytecode. */ Replace block pointer with position in bytecode. */
for (i = a->a_nblocks - 1; i >= 0; i--) { for (i = a->a_nblocks - 1; i >= 0; i--) {
basicblock *b = a->a_postorder[i]; b = a->a_postorder[i];
bsize = blocksize(b); bsize = blocksize(b);
b->b_offset = totsize; b->b_offset = totsize;
totsize += bsize; totsize += bsize;
@ -3918,7 +3914,6 @@ assemble_jump_offsets(struct assembler *a, struct compiler *c)
} }
} }
} }
return 1;
} }
static PyObject * static PyObject *
@ -4079,8 +4074,7 @@ assemble(struct compiler *c, int addNone)
dfs(c, entryblock, &a); dfs(c, entryblock, &a);
/* Can't modify the bytecode after computing jump offsets. */ /* Can't modify the bytecode after computing jump offsets. */
if (!assemble_jump_offsets(&a, c)) assemble_jump_offsets(&a, c);
goto error;
/* Emit code in reverse postorder from dfs. */ /* Emit code in reverse postorder from dfs. */
for (i = a.a_nblocks - 1; i >= 0; i--) { for (i = a.a_nblocks - 1; i >= 0; i--) {