@@ -688,11 +688,13 @@ class CFGVisitor : public ASTVisitor {
688
688
return createDstName (rtn);
689
689
}
690
690
691
- int addConst (Box* o) {
691
+ int addConst (STOLEN( Box*) o) {
692
692
// make sure all consts are unique
693
693
auto it = consts.find (o);
694
- if (it != consts.end ())
694
+ if (it != consts.end ()) {
695
+ Py_DECREF (o);
695
696
return it->second ;
697
+ }
696
698
int vreg = code_constants.createVRegEntryForConstant (o);
697
699
consts[o] = vreg;
698
700
return vreg;
@@ -707,20 +709,17 @@ class CFGVisitor : public ASTVisitor {
707
709
708
710
TmpValue remapNum (AST_Num* num) {
709
711
Box* o = createConstObject (num);
710
- AUTO_DECREF (o);
711
712
return TmpValue (addConst (o), num->lineno );
712
713
}
713
714
714
715
TmpValue makeStr (llvm::StringRef str, int lineno = 0 ) {
715
716
AST_Str ast_str (str.str ());
716
717
Box* o = createConstObject (&ast_str);
717
- AUTO_DECREF (o);
718
718
return TmpValue (addConst (o), lineno);
719
719
}
720
720
721
721
TmpValue remapStr (AST_Str* str) {
722
722
Box* o = createConstObject (str);
723
- AUTO_DECREF (o);
724
723
return TmpValue (addConst (o), str->lineno );
725
724
}
726
725
@@ -729,12 +728,11 @@ class CFGVisitor : public ASTVisitor {
729
728
ast_num.num_type = AST_Num::INT;
730
729
ast_num.n_int = n;
731
730
Box* o = createConstObject (&ast_num);
732
- AUTO_DECREF (o);
733
731
return TmpValue (addConst (o), lineno);
734
732
}
735
733
736
734
TmpValue makeNone (int lineno) {
737
- int vreg_const = addConst (Py_None);
735
+ int vreg_const = addConst (incref ( Py_None) );
738
736
return TmpValue (vreg_const, lineno);
739
737
}
740
738
@@ -1250,7 +1248,6 @@ class CFGVisitor : public ASTVisitor {
1250
1248
for (int i = 0 ; i < node->keywords .size (); ++i) {
1251
1249
tuple->elts [i] = incref (node->keywords [i]->arg .getBox ());
1252
1250
}
1253
- code_constants.addOwnedRef (tuple);
1254
1251
rtn_shared->index_keyword_names = addConst (tuple);
1255
1252
}
1256
1253
@@ -1360,7 +1357,7 @@ class CFGVisitor : public ASTVisitor {
1360
1357
}
1361
1358
1362
1359
TmpValue remapEllipsis (AST_Ellipsis* node) {
1363
- int vreg_const = addConst (Ellipsis);
1360
+ int vreg_const = addConst (incref ( Ellipsis) );
1364
1361
return TmpValue (vreg_const, node->lineno );
1365
1362
}
1366
1363
@@ -1438,8 +1435,7 @@ class CFGVisitor : public ASTVisitor {
1438
1435
BoxedCode* code = cfgizer->runRecursively (new_body, gen_name, node->lineno , genexp_args, node);
1439
1436
BST_MakeFunction* mkfunc = allocAndPush<BST_MakeFunction>(0 , 0 );
1440
1437
mkfunc->lineno = node->lineno ;
1441
- mkfunc->vreg_code_obj = code_constants.createVRegEntryForConstant (code);
1442
- code_constants.addOwnedRef (code);
1438
+ mkfunc->vreg_code_obj = addConst (code);
1443
1439
TmpValue func_var_name = createDstName (mkfunc);
1444
1440
1445
1441
return makeCall (func_var_name, { first });
@@ -1498,8 +1494,7 @@ class CFGVisitor : public ASTVisitor {
1498
1494
BoxedCode* code = cfgizer->runRecursively (new_body, comp_name, node->lineno , args, node);
1499
1495
BST_MakeFunction* mkfunc = allocAndPush<BST_MakeFunction>(0 , 0 );
1500
1496
mkfunc->lineno = node->lineno ;
1501
- mkfunc->vreg_code_obj = code_constants.createVRegEntryForConstant (code);
1502
- code_constants.addOwnedRef (code);
1497
+ mkfunc->vreg_code_obj = addConst (code);
1503
1498
TmpValue func_var_name = createDstName (mkfunc);
1504
1499
1505
1500
return makeCall (func_var_name, { first });
@@ -1554,8 +1549,7 @@ class CFGVisitor : public ASTVisitor {
1554
1549
1555
1550
auto name = getStaticString (" <lambda>" );
1556
1551
auto * code = cfgizer->runRecursively ({ stmt }, name, mkfn->lineno , node->args , node);
1557
- mkfn->vreg_code_obj = code_constants.createVRegEntryForConstant (code);
1558
- code_constants.addOwnedRef (code);
1552
+ mkfn->vreg_code_obj = addConst (code);
1559
1553
1560
1554
return createDstName (mkfn);
1561
1555
}
@@ -1666,18 +1660,18 @@ class CFGVisitor : public ASTVisitor {
1666
1660
if (str->str_type == AST_Str::STR) {
1667
1661
BoxedString*& o = str_constants[str->str_data ];
1668
1662
// we always intern the string
1669
- if (!o) {
1663
+ if (!o)
1670
1664
o = internStringMortal (str->str_data );
1671
- code_constants. addOwnedRef (o);
1672
- }
1673
- return incref (o) ;
1665
+ else
1666
+ incref (o);
1667
+ return o ;
1674
1668
} else if (str->str_type == AST_Str::UNICODE) {
1675
1669
Box*& r = unicode_constants[str->str_data ];
1676
- if (!r) {
1670
+ if (!r)
1677
1671
r = decodeUTF8StringPtr (str->str_data );
1678
- code_constants. addOwnedRef (r);
1679
- }
1680
- return incref (r) ;
1672
+ else
1673
+ incref (r);
1674
+ return r ;
1681
1675
} else
1682
1676
RELEASE_ASSERT (0 , " not implemented" );
1683
1677
} else if (node->type == AST_TYPE::Num) {
@@ -1688,18 +1682,18 @@ class CFGVisitor : public ASTVisitor {
1688
1682
return incref (code_constants.getFloatConstant (num->n_float ));
1689
1683
else if (num->num_type == AST_Num::LONG) {
1690
1684
Box*& r = long_constants[num->n_long ];
1691
- if (!r) {
1685
+ if (!r)
1692
1686
r = createLong (num->n_long );
1693
- code_constants. addOwnedRef (r);
1694
- }
1695
- return incref (r) ;
1687
+ else
1688
+ incref (r);
1689
+ return r ;
1696
1690
} else if (num->num_type == AST_Num::COMPLEX) {
1697
1691
Box*& r = imaginary_constants[getDoubleBits (num->n_float )];
1698
- if (!r) {
1692
+ if (!r)
1699
1693
r = createPureImaginary (num->n_float );
1700
- code_constants. addOwnedRef (r);
1701
- }
1702
- return incref (r) ;
1694
+ else
1695
+ incref (r);
1696
+ return r ;
1703
1697
} else
1704
1698
RELEASE_ASSERT (0 , " not implemented" );
1705
1699
} else if (node->type == AST_TYPE::Tuple) {
@@ -1720,8 +1714,7 @@ class CFGVisitor : public ASTVisitor {
1720
1714
1721
1715
// handle tuples where all elements are constants as a constant
1722
1716
if (isConstObject (node)) {
1723
- BoxedTuple* tuple = (BoxedTuple*)createConstObject (node);
1724
- code_constants.addOwnedRef (tuple);
1717
+ Box* tuple = createConstObject (node);
1725
1718
return TmpValue (addConst (tuple), node->lineno );
1726
1719
}
1727
1720
@@ -1968,8 +1961,7 @@ class CFGVisitor : public ASTVisitor {
1968
1961
unmapExpr (bases_name, &mkclass->vreg_bases_tuple );
1969
1962
1970
1963
auto * code = cfgizer->runRecursively (node->body , node->name .getBox (), node->lineno , NULL , node);
1971
- mkclass->vreg_code_obj = code_constants.createVRegEntryForConstant (code);
1972
- code_constants.addOwnedRef (code);
1964
+ mkclass->vreg_code_obj = addConst (code);
1973
1965
1974
1966
auto tmp = createDstName (mkclass);
1975
1967
pushAssign (TmpValue (scoping->mangleName (node->name ), node->lineno ), tmp);
@@ -2003,8 +1995,7 @@ class CFGVisitor : public ASTVisitor {
2003
1995
}
2004
1996
2005
1997
auto * code = cfgizer->runRecursively (node->body , node->name .getBox (), node->lineno , node->args , node);
2006
- mkfunc->vreg_code_obj = code_constants.createVRegEntryForConstant (code);
2007
- code_constants.addOwnedRef (code);
1998
+ mkfunc->vreg_code_obj = addConst (code);
2008
1999
auto tmp = createDstName (mkfunc);
2009
2000
pushAssign (TmpValue (scoping->mangleName (node->name ), node->lineno ), tmp);
2010
2001
@@ -2097,7 +2088,6 @@ class CFGVisitor : public ASTVisitor {
2097
2088
for (int i = 0 ; i < node->names .size (); i++) {
2098
2089
tuple->elts [i] = internStringMortal (node->names [i]->name .s ());
2099
2090
}
2100
- code_constants.addOwnedRef (tuple);
2101
2091
import ->vreg_from = addConst (tuple);
2102
2092
unmapExpr (makeStr (node->module .s ()), &import ->vreg_name );
2103
2093
@@ -3547,6 +3537,7 @@ static std::pair<CFG*, CodeConstants> computeCFG(llvm::ArrayRef<AST_stmt*> body,
3547
3537
3548
3538
pruneUnnecessaryBlocks (rtn);
3549
3539
3540
+ visitor.code_constants .optimizeSize ();
3550
3541
rtn->bytecode .optimizeSize ();
3551
3542
rtn->blocks .shrink_to_fit ();
3552
3543
0 commit comments