Skip to content

Commit f19b38d

Browse files
authored
Merge pull request #245 from ncaklovic/master
Changes in 3.10 under BPO-27129: "Use instruction offset, rather than bytecode offset"
2 parents 9fe5463 + 3039d8b commit f19b38d

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

ASTree.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
11581158
|| opcode == Pyc::POP_JUMP_IF_TRUE_A;
11591159

11601160
int offs = operand;
1161+
if (mod->verCompare(3, 10) >= 0)
1162+
offs *= sizeof(uint16_t); // // BPO-27129
11611163
if (opcode == Pyc::JUMP_IF_FALSE_A
11621164
|| opcode == Pyc::JUMP_IF_TRUE_A) {
11631165
/* Offset is relative in these cases */
@@ -1240,7 +1242,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
12401242
break;
12411243
case Pyc::JUMP_ABSOLUTE_A:
12421244
{
1243-
if (operand < pos) {
1245+
int offs = operand;
1246+
if (mod->verCompare(3, 10) >= 0)
1247+
offs *= sizeof(uint16_t); // // BPO-27129
1248+
1249+
if (offs < pos) {
12441250
if (curblock->blktype() == ASTBlock::BLK_FOR
12451251
&& curblock.cast<ASTIterBlock>()->isComprehension()) {
12461252
PycRef<ASTNode> top = stack.top();
@@ -1339,13 +1345,17 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
13391345
break;
13401346
case Pyc::JUMP_FORWARD_A:
13411347
{
1348+
int offs = operand;
1349+
if (mod->verCompare(3, 10) >= 0)
1350+
offs *= sizeof(uint16_t); // // BPO-27129
1351+
13421352
if (curblock->blktype() == ASTBlock::BLK_CONTAINER) {
13431353
PycRef<ASTContainerBlock> cont = curblock.cast<ASTContainerBlock>();
13441354
if (cont->hasExcept()) {
13451355
stack_hist.push(stack);
13461356

1347-
curblock->setEnd(pos+operand);
1348-
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
1357+
curblock->setEnd(pos+offs);
1358+
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+offs, NULL, false);
13491359
except->init();
13501360
blocks.push(except);
13511361
curblock = blocks.top();
@@ -1381,31 +1391,31 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
13811391

13821392
if (prev->blktype() == ASTBlock::BLK_IF
13831393
|| prev->blktype() == ASTBlock::BLK_ELIF) {
1384-
if (operand == 0) {
1394+
if (offs == 0) {
13851395
prev = nil;
13861396
continue;
13871397
}
13881398

13891399
if (push) {
13901400
stack_hist.push(stack);
13911401
}
1392-
PycRef<ASTBlock> next = new ASTBlock(ASTBlock::BLK_ELSE, pos+operand);
1402+
PycRef<ASTBlock> next = new ASTBlock(ASTBlock::BLK_ELSE, pos+offs);
13931403
if (prev->inited() == ASTCondBlock::PRE_POPPED) {
13941404
next->init(ASTCondBlock::PRE_POPPED);
13951405
}
13961406

13971407
blocks.push(next.cast<ASTBlock>());
13981408
prev = nil;
13991409
} else if (prev->blktype() == ASTBlock::BLK_EXCEPT) {
1400-
if (operand == 0) {
1410+
if (offs == 0) {
14011411
prev = nil;
14021412
continue;
14031413
}
14041414

14051415
if (push) {
14061416
stack_hist.push(stack);
14071417
}
1408-
PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
1418+
PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+offs, NULL, false);
14091419
next->init();
14101420

14111421
blocks.push(next.cast<ASTBlock>());
@@ -1424,7 +1434,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
14241434
prev = nil;
14251435
}
14261436
} else if (prev->blktype() == ASTBlock::BLK_TRY
1427-
&& prev->end() < pos+operand) {
1437+
&& prev->end() < pos+offs) {
14281438
/* Need to add an except/finally block */
14291439
stack = stack_hist.top();
14301440
stack.pop();
@@ -1436,7 +1446,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
14361446
stack_hist.push(stack);
14371447
}
14381448

1439-
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
1449+
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+offs, NULL, false);
14401450
except->init();
14411451
blocks.push(except);
14421452
}
@@ -1453,7 +1463,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
14531463
curblock = blocks.top();
14541464

14551465
if (curblock->blktype() == ASTBlock::BLK_EXCEPT) {
1456-
curblock->setEnd(pos+operand);
1466+
curblock->setEnd(pos+offs);
14571467
}
14581468
}
14591469
break;

bytecode.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ bool Pyc::IsCellArg(int opcode)
137137
(opcode == Pyc::STORE_DEREF_A);
138138
}
139139

140+
bool Pyc::IsJumpArg(int opcode)
141+
{
142+
return (opcode == Pyc::POP_JUMP_IF_FALSE_A) || (opcode == Pyc::POP_JUMP_IF_TRUE_A) ||
143+
(opcode == Pyc::JUMP_IF_FALSE_OR_POP_A) || (opcode == JUMP_IF_TRUE_OR_POP_A);
144+
}
145+
140146
bool Pyc::IsJumpOffsetArg(int opcode)
141147
{
142148
return (opcode == Pyc::JUMP_FORWARD_A) || (opcode == Pyc::JUMP_IF_FALSE_A) ||
@@ -385,7 +391,16 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
385391
fprintf(pyc_output, "%d <INVALID>", operand);
386392
}
387393
} else if (Pyc::IsJumpOffsetArg(opcode)) {
388-
fprintf(pyc_output, "%d (to %d)", operand, pos+operand);
394+
int offs = operand;
395+
if (mod->verCompare(3, 10) >= 0)
396+
offs *= sizeof(uint16_t); // BPO-27129
397+
fprintf(pyc_output, "%d (to %d)", operand, pos+offs);
398+
}
399+
else if (Pyc::IsJumpArg(opcode)) {
400+
if (mod->verCompare(3, 10) >= 0) // BPO-27129
401+
fprintf(pyc_output, "%d (to %d)", operand, int(operand * sizeof(uint16_t)));
402+
else
403+
fprintf(pyc_output, "%d", operand);
389404
} else if (Pyc::IsCompareArg(opcode)) {
390405
if (static_cast<size_t>(operand) < cmp_strings_len)
391406
fprintf(pyc_output, "%d (%s)", operand, cmp_strings[operand]);

bytecode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bool IsNameArg(int opcode);
2525
bool IsVarNameArg(int opcode);
2626
bool IsCellArg(int opcode);
2727
bool IsJumpOffsetArg(int opcode);
28+
bool IsJumpArg(int opcode);
2829
bool IsCompareArg(int opcode);
2930

3031
}

0 commit comments

Comments
 (0)