@@ -1429,28 +1429,19 @@ public void setUpFrame(ArgumentsTy args, Builder b) {
14291429 }
14301430 }
14311431
1432- String [] cellVariables = orderedKeys (cellvars , new String [0 ]);
1433- BytecodeLocal [] cellVariableLocals = new BytecodeLocal [cellVariables .length ];
1434- for (int i = 0 ; i < cellVariables .length ; i ++) {
1435- BytecodeLocal local = b .createLocal ();
1436- cellLocals .put (cellVariables [i ], local );
1437- cellVariableLocals [i ] = local ;
1438- }
1439-
1440- String [] freeVariables = orderedKeys (freevars , new String [0 ]);
1441- BytecodeLocal [] freeVariableLocals = new BytecodeLocal [freeVariables .length ];
1442- for (int i = 0 ; i < freeVariables .length ; i ++) {
1443- BytecodeLocal local = b .createLocal ();
1444- freeLocals .put (freeVariables [i ], local );
1445- freeVariableLocals [i ] = local ;
1446- }
1447-
14481432 // 2. Copy arguments, initialize cells, and copy free variables.
14491433 copyArguments (args , b );
14501434
1451- if (cellVariableLocals .length > 0 ) {
1435+ if (!cellvars .isEmpty ()) {
1436+ String [] cellVariables = orderedKeys (cellvars , new String [0 ]);
1437+ BytecodeLocal [] cellVariableLocals = new BytecodeLocal [cellVariables .length ];
1438+ for (int i = 0 ; i < cellVariables .length ; i ++) {
1439+ BytecodeLocal local = b .createLocal ();
1440+ cellLocals .put (cellVariables [i ], local );
1441+ cellVariableLocals [i ] = local ;
1442+ }
14521443 b .emitCreateCells (cellVariableLocals );
1453- for (int i = 0 ; i < cellVariableLocals .length ; i ++) {
1444+ for (int i = 0 ; i < cellVariables .length ; i ++) {
14541445 if (scope .getUseOfName (cellVariables [i ]).contains (DefUse .DefParam )) {
14551446 /*
14561447 * To simplify the argument copying performed above, we copy cell params into
@@ -1467,10 +1458,15 @@ public void setUpFrame(ArgumentsTy args, Builder b) {
14671458 }
14681459 }
14691460
1470- if (freeVariableLocals .length > 0 ) {
1471- b .beginStoreRange (freeVariableLocals );
1472- b .emitLoadClosure ();
1473- b .endStoreRange ();
1461+ if (!freevars .isEmpty ()) {
1462+ String [] freeVariables = orderedKeys (freevars , new String [0 ]);
1463+ BytecodeLocal [] freeVariableLocals = new BytecodeLocal [freeVariables .length ];
1464+ for (int i = 0 ; i < freeVariables .length ; i ++) {
1465+ BytecodeLocal local = b .createLocal ();
1466+ freeLocals .put (freeVariables [i ], local );
1467+ freeVariableLocals [i ] = local ;
1468+ }
1469+ b .emitInitFreeVars (freeVariableLocals );
14741470 }
14751471 }
14761472
@@ -1479,50 +1475,42 @@ private void copyArguments(ArgumentsTy args, Builder b) {
14791475 return ;
14801476 }
14811477
1482- int argIdx = PArguments .USER_ARGUMENTS_OFFSET ;
1483- if (args .posOnlyArgs != null ) {
1484- for (int i = 0 ; i < args .posOnlyArgs .length ; i ++) {
1485- BytecodeLocal local = getLocal (args .posOnlyArgs [i ].arg );
1486- assert local != null ;
1487- b .beginStoreLocal (local );
1488- b .emitLoadArgument (argIdx ++);
1489- b .endStoreLocal ();
1478+ int idx = 0 ;
1479+ int posOnlyArgsCount = args .posOnlyArgs != null ? args .posOnlyArgs .length : 0 ;
1480+ int argsCount = args .args != null ? args .args .length : 0 ;
1481+ int kwOnlyArgsLength = args .kwOnlyArgs != null ? args .kwOnlyArgs .length : 0 ;
1482+ int totalLocals = posOnlyArgsCount + argsCount + kwOnlyArgsLength ;
1483+ if (totalLocals > 0 ) {
1484+ BytecodeLocal [] locals = new BytecodeLocal [totalLocals ];
1485+
1486+ for (int i = 0 ; i < posOnlyArgsCount ; i ++) {
1487+ locals [idx ++] = getLocal (args .posOnlyArgs [i ].arg );
14901488 }
1491- }
14921489
1493- if (args .args != null ) {
1494- for (int i = 0 ; i < args .args .length ; i ++) {
1495- BytecodeLocal local = getLocal (args .args [i ].arg );
1496- assert local != null ;
1497- b .beginStoreLocal (local );
1498- b .emitLoadArgument (argIdx ++);
1499- b .endStoreLocal ();
1490+ for (int i = 0 ; i < argsCount ; i ++) {
1491+ locals [idx ++] = getLocal (args .args [i ].arg );
15001492 }
1501- }
15021493
1503- if (args .kwOnlyArgs != null ) {
1504- for (int i = 0 ; i < args .kwOnlyArgs .length ; i ++) {
1505- BytecodeLocal local = getLocal (args .kwOnlyArgs [i ].arg );
1506- assert local != null ;
1507- b .beginStoreLocal (local );
1508- b .emitLoadArgument (argIdx ++);
1509- b .endStoreLocal ();
1494+ for (int i = 0 ; i < kwOnlyArgsLength ; i ++) {
1495+ locals [idx ++] = getLocal (args .kwOnlyArgs [i ].arg );
15101496 }
1497+
1498+ b .emitCopyArguments (locals );
15111499 }
15121500
15131501 if (args .varArg != null ) {
15141502 BytecodeLocal local = getLocal (args .varArg .arg );
15151503 assert local != null ;
15161504 b .beginStoreLocal (local );
1517- b .emitLoadVariableArguments (argIdx ++);
1505+ b .emitLoadVariableArguments (idx ++);
15181506 b .endStoreLocal ();
15191507 }
15201508
15211509 if (args .kwArg != null ) {
15221510 BytecodeLocal local = getLocal (args .kwArg .arg );
15231511 assert local != null ;
15241512 b .beginStoreLocal (local );
1525- b .emitLoadKeywordArguments (argIdx );
1513+ b .emitLoadKeywordArguments (idx );
15261514 b .endStoreLocal ();
15271515 }
15281516 }
0 commit comments