[vm/bytecode] Cleanup after switching to compact bytecode instructions

Change-Id: Ie6ecdd88e8d1740c53cfb3fbc4b43f9e41c592b1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101491
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/pkg/vm/lib/bytecode/assembler.dart b/pkg/vm/lib/bytecode/assembler.dart
index c9a9fea..b71650b 100644
--- a/pkg/vm/lib/bytecode/assembler.dart
+++ b/pkg/vm/lib/bytecode/assembler.dart
@@ -357,23 +357,22 @@
     _emitInstructionX(Opcode.kPopLocal, rx);
   }
 
-  // TODO(alexmarkov): swap operands.
-  void emitDirectCall(int rf, int rd) {
+  void emitDirectCall(int rd, int rf) {
     emitSourcePosition();
     _emitInstructionDF(Opcode.kDirectCall, rd, rf);
   }
 
-  void emitInterfaceCall(int rf, int rd) {
+  void emitInterfaceCall(int rd, int rf) {
     emitSourcePosition();
     _emitInstructionDF(Opcode.kInterfaceCall, rd, rf);
   }
 
-  void emitUncheckedInterfaceCall(int rf, int rd) {
+  void emitUncheckedInterfaceCall(int rd, int rf) {
     emitSourcePosition();
     _emitInstructionDF(Opcode.kUncheckedInterfaceCall, rd, rf);
   }
 
-  void emitDynamicCall(int rf, int rd) {
+  void emitDynamicCall(int rd, int rf) {
     emitSourcePosition();
     _emitInstructionDF(Opcode.kDynamicCall, rd, rf);
   }
diff --git a/pkg/vm/lib/bytecode/gen_bytecode.dart b/pkg/vm/lib/bytecode/gen_bytecode.dart
index c2d1bdb..52b325b 100644
--- a/pkg/vm/lib/bytecode/gen_bytecode.dart
+++ b/pkg/vm/lib/bytecode/gen_bytecode.dart
@@ -790,7 +790,7 @@
         : (isSet ? InvocationKind.setter : InvocationKind.method);
     final cpIndex = cp.addDirectCall(kind, target, argDesc);
 
-    asm.emitDirectCall(totalArgCount, cpIndex);
+    asm.emitDirectCall(cpIndex, totalArgCount);
   }
 
   void _genDirectCallWithArgs(Member target, Arguments args,
@@ -1069,7 +1069,7 @@
       final argDesc = objectTable.getArgDescHandle(2);
       final cpIndex = cp.addInterfaceCall(
           InvocationKind.method, objectSimpleInstanceOf, argDesc);
-      asm.emitInterfaceCall(2, cpIndex);
+      asm.emitInterfaceCall(cpIndex, 2);
       return;
     }
 
@@ -1083,7 +1083,7 @@
     final argDesc = objectTable.getArgDescHandle(4);
     final cpIndex =
         cp.addInterfaceCall(InvocationKind.method, objectInstanceOf, argDesc);
-    asm.emitInterfaceCall(4, cpIndex);
+    asm.emitInterfaceCall(cpIndex, 4);
   }
 
   void start(Member node) {
@@ -2301,11 +2301,11 @@
       int totalArgCount, int callCpIndex, bool isDynamic, bool isUnchecked) {
     if (isDynamic) {
       assert(!isUnchecked);
-      asm.emitDynamicCall(totalArgCount, callCpIndex);
+      asm.emitDynamicCall(callCpIndex, totalArgCount);
     } else if (isUnchecked) {
-      asm.emitUncheckedInterfaceCall(totalArgCount, callCpIndex);
+      asm.emitUncheckedInterfaceCall(callCpIndex, totalArgCount);
     } else {
-      asm.emitInterfaceCall(totalArgCount, callCpIndex);
+      asm.emitInterfaceCall(callCpIndex, totalArgCount);
     }
   }
 
@@ -2798,9 +2798,9 @@
     // Front-end inserts implicit cast (type check) which ensures that
     // result of iterable expression is Iterable<dynamic>.
     asm.emitInterfaceCall(
-        1,
         cp.addInterfaceCall(InvocationKind.getter, iterableIterator,
-            objectTable.getArgDescHandle(1)));
+            objectTable.getArgDescHandle(1)),
+        1);
 
     final iteratorTemp = locals.tempIndexInFrame(node);
     asm.emitPopLocal(iteratorTemp);
@@ -2832,9 +2832,9 @@
     }
 
     asm.emitInterfaceCall(
-        1,
         cp.addInterfaceCall(InvocationKind.method, iteratorMoveNext,
-            objectTable.getArgDescHandle(1)));
+            objectTable.getArgDescHandle(1)),
+        1);
     _genJumpIfFalse(/* negated = */ false, done);
 
     _enterScope(node);
@@ -2843,9 +2843,9 @@
 
     asm.emitPush(iteratorTemp);
     asm.emitInterfaceCall(
-        1,
         cp.addInterfaceCall(InvocationKind.getter, iteratorCurrent,
-            objectTable.getArgDescHandle(1)));
+            objectTable.getArgDescHandle(1)),
+        1);
 
     _genStoreVar(node.variable);
 
@@ -3002,9 +3002,9 @@
           asm.emitPush(temp);
           _genPushConstExpr(expr);
           asm.emitInterfaceCall(
-              2,
-              cp.addInterfaceCall(InvocationKind.method, coreTypes.objectEquals,
-                  equalsArgDesc));
+              cp.addInterfaceCall(
+                  InvocationKind.method, coreTypes.objectEquals, equalsArgDesc),
+              2);
           _genJumpIfTrue(/* negated = */ false, caseLabel);
         }
       }
diff --git a/runtime/vm/compiler/frontend/bytecode_flow_graph_builder.cc b/runtime/vm/compiler/frontend/bytecode_flow_graph_builder.cc
index a0ad951..0a6df8f 100644
--- a/runtime/vm/compiler/frontend/bytecode_flow_graph_builder.cc
+++ b/runtime/vm/compiler/frontend/bytecode_flow_graph_builder.cc
@@ -26,7 +26,6 @@
 
 namespace kernel {
 
-// 8-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandA() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -36,7 +35,6 @@
   }
 }
 
-// 8-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandB() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -46,7 +44,6 @@
   }
 }
 
-// 8-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandC() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -56,7 +53,6 @@
   }
 }
 
-// 8/16-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandD() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -66,7 +62,6 @@
   }
 }
 
-// 8/16-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandE() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -76,7 +71,6 @@
   }
 }
 
-// 8-bit unsigned operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandF() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -86,7 +80,6 @@
   }
 }
 
-// 8/16-bit signed operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandX() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -96,7 +89,6 @@
   }
 }
 
-// 8/16-bit signed operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandY() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
@@ -106,7 +98,6 @@
   }
 }
 
-// 8/24-bit signed operand.
 BytecodeFlowGraphBuilder::Operand BytecodeFlowGraphBuilder::DecodeOperandT() {
   if (is_generating_interpreter()) {
     UNIMPLEMENTED();  // TODO(alexmarkov): interpreter
diff --git a/runtime/vm/interpreter.cc b/runtime/vm/interpreter.cc
index 2bdb64d..e8f1419 100644
--- a/runtime/vm/interpreter.cc
+++ b/runtime/vm/interpreter.cc
@@ -933,6 +933,8 @@
   BYTECODE_IMPL_LABEL(Name)
 
 #define BYTECODE_HEADER_A_WITH_OLD(Name)                                       \
+  uint32_t rA;                                                                 \
+  USE(rA);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
   rA = pc[1];                                                                  \
   pc += 4;                                                                     \
@@ -960,24 +962,23 @@
   pc += 2;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
-// TODO(alexmarkov): Rename rD to rX.
 #define BYTECODE_HEADER_X_WITH_OLD(Name)                                       \
-  int32_t rD;                                                                  \
-  USE(rD);                                                                     \
+  int32_t rX;                                                                  \
+  USE(rX);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
-  rD = static_cast<int16_t>(static_cast<uint16_t>(pc[2]) |                     \
+  rX = static_cast<int16_t>(static_cast<uint16_t>(pc[2]) |                     \
                             (static_cast<uint16_t>(pc[3]) << 8));              \
   pc += 4;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_WIDE_ENTRY_LABEL(Name)                                              \
-  rD = static_cast<int32_t>(static_cast<uint32_t>(pc[1]) |                     \
+  rX = static_cast<int32_t>(static_cast<uint32_t>(pc[1]) |                     \
                             (static_cast<uint32_t>(pc[2]) << 8) |              \
                             (static_cast<uint32_t>(pc[3]) << 16) |             \
                             (static_cast<uint32_t>(pc[4]) << 24));             \
   pc += 5;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_ENTRY_LABEL(Name)                                                   \
-  rD = static_cast<int8_t>(pc[1]);                                             \
+  rX = static_cast<int8_t>(pc[1]);                                             \
   pc += 2;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
@@ -1003,41 +1004,42 @@
   pc += 2;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
-// TODO(alexmarkov): Rename rD to rE.
 #define BYTECODE_HEADER_A_E_WITH_OLD(Name)                                     \
-  uint32_t rD;                                                                 \
-  USE(rD);                                                                     \
+  uint32_t rA, rE;                                                             \
+  USE(rA);                                                                     \
+  USE(rE);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
   rA = pc[1];                                                                  \
-  rD = static_cast<uint32_t>(pc[2]) | (static_cast<uint32_t>(pc[3]) << 8);     \
+  rE = static_cast<uint32_t>(pc[2]) | (static_cast<uint32_t>(pc[3]) << 8);     \
   pc += 4;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_WIDE_ENTRY_LABEL(Name)                                              \
   rA = pc[1];                                                                  \
-  rD = static_cast<uint32_t>(pc[2]) | (static_cast<uint32_t>(pc[3]) << 8) |    \
+  rE = static_cast<uint32_t>(pc[2]) | (static_cast<uint32_t>(pc[3]) << 8) |    \
        (static_cast<uint32_t>(pc[4]) << 16) |                                  \
        (static_cast<uint32_t>(pc[5]) << 24);                                   \
   pc += 6;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_ENTRY_LABEL(Name)                                                   \
   rA = pc[1];                                                                  \
-  rD = pc[2];                                                                  \
+  rE = pc[2];                                                                  \
   pc += 3;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
-// TODO(alexmarkov): Rename rD to rY.
 #define BYTECODE_HEADER_A_Y_WITH_OLD(Name)                                     \
-  int32_t rD;                                                                  \
-  USE(rD);                                                                     \
+  uint32_t rA;                                                                 \
+  int32_t rY;                                                                  \
+  USE(rA);                                                                     \
+  USE(rY);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
   rA = pc[1];                                                                  \
-  rD = static_cast<int16_t>(static_cast<uint16_t>(pc[2]) |                     \
+  rY = static_cast<int16_t>(static_cast<uint16_t>(pc[2]) |                     \
                             (static_cast<uint16_t>(pc[3]) << 8));              \
   pc += 4;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_WIDE_ENTRY_LABEL(Name)                                              \
   rA = pc[1];                                                                  \
-  rD = static_cast<int32_t>(static_cast<uint32_t>(pc[2]) |                     \
+  rY = static_cast<int32_t>(static_cast<uint32_t>(pc[2]) |                     \
                             (static_cast<uint32_t>(pc[3]) << 8) |              \
                             (static_cast<uint32_t>(pc[4]) << 16) |             \
                             (static_cast<uint32_t>(pc[5]) << 24));             \
@@ -1045,16 +1047,16 @@
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_ENTRY_LABEL(Name)                                                   \
   rA = pc[1];                                                                  \
-  rD = static_cast<int8_t>(pc[2]);                                             \
+  rY = static_cast<int8_t>(pc[2]);                                             \
   pc += 3;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
-// TODO(alexmarkov): Rename rA to rF.
 #define BYTECODE_HEADER_D_F_WITH_OLD(Name)                                     \
-  uint32_t rD;                                                                 \
+  uint32_t rD, rF;                                                             \
   USE(rD);                                                                     \
+  USE(rF);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
-  rA = pc[1];                                                                  \
+  rF = pc[1];                                                                  \
   rD = static_cast<uint32_t>(pc[2]) | (static_cast<uint32_t>(pc[3]) << 8);     \
   pc += 4;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
@@ -1062,17 +1064,18 @@
   rD = static_cast<uint32_t>(pc[1]) | (static_cast<uint32_t>(pc[2]) << 8) |    \
        (static_cast<uint32_t>(pc[3]) << 16) |                                  \
        (static_cast<uint32_t>(pc[4]) << 24);                                   \
-  rA = pc[5];                                                                  \
+  rF = pc[5];                                                                  \
   pc += 6;                                                                     \
   GOTO_BYTECODE_IMPL(Name);                                                    \
   BYTECODE_ENTRY_LABEL(Name)                                                   \
   rD = pc[1];                                                                  \
-  rA = pc[2];                                                                  \
+  rF = pc[2];                                                                  \
   pc += 3;                                                                     \
   BYTECODE_IMPL_LABEL(Name)
 
 #define BYTECODE_HEADER_A_B_C_WITH_OLD(Name)                                   \
-  uint16_t rB, rC;                                                             \
+  uint32_t rA, rB, rC;                                                         \
+  USE(rA);                                                                     \
   USE(rB);                                                                     \
   USE(rC);                                                                     \
   BYTECODE_OLD_ENTRY_LABEL(Name)                                               \
@@ -1483,7 +1486,6 @@
   RawObject** SP;  // Stack Pointer.
 
   uint32_t op;  // Currently executing op.
-  uint32_t rA;  // A component of the currently executing op.
 
   bool reentering = fp_ != NULL;
   if (!reentering) {
@@ -1596,7 +1598,7 @@
   // KernelBytecode handlers (see constants_kbc.h for bytecode descriptions).
   {
     BYTECODE(Entry, D);
-    const uint16_t num_locals = rD;
+    const intptr_t num_locals = rD;
 
     // Initialize locals with null & set SP.
     for (intptr_t i = 0; i < num_locals; i++) {
@@ -1609,8 +1611,8 @@
 
   {
     BYTECODE(EntryFixed, A_E);
-    const uint16_t num_fixed_params = rA;
-    const uint16_t num_locals = rD;
+    const intptr_t num_fixed_params = rA;
+    const intptr_t num_locals = rE;
 
     const intptr_t arg_count = InterpreterHelpers::ArgDescArgCount(argdesc_);
     const intptr_t pos_count = InterpreterHelpers::ArgDescPosCount(argdesc_);
@@ -1629,9 +1631,9 @@
 
   {
     BYTECODE(EntryOptional, A_B_C);
-    const uint16_t num_fixed_params = rA;
-    const uint16_t num_opt_pos_params = rB;
-    const uint16_t num_opt_named_params = rC;
+    const intptr_t num_fixed_params = rA;
+    const intptr_t num_opt_pos_params = rB;
+    const intptr_t num_opt_named_params = rC;
     const intptr_t min_num_pos_args = num_fixed_params;
     const intptr_t max_num_pos_args = num_fixed_params + num_opt_pos_params;
 
@@ -1742,7 +1744,7 @@
   {
     BYTECODE(Frame, D);
     // Initialize locals with null and increment SP.
-    const uint16_t num_locals = rD;
+    const intptr_t num_locals = rD;
     for (intptr_t i = 1; i <= num_locals; i++) {
       SP[i] = null_value;
     }
@@ -1785,8 +1787,8 @@
 
   {
     BYTECODE(CheckFunctionTypeArgs, A_E);
-    const uint16_t declared_type_args_len = rA;
-    const uint16_t first_stack_local_index = rD;
+    const intptr_t declared_type_args_len = rA;
+    const intptr_t first_stack_local_index = rE;
 
     // Decode arguments descriptor's type args len.
     const intptr_t type_args_len =
@@ -1825,7 +1827,7 @@
     BYTECODE(InstantiateTypeArgumentsTOS, A_E);
     // Stack: instantiator type args, function type args
     RawTypeArguments* type_arguments =
-        static_cast<RawTypeArguments*>(LOAD_CONSTANT(rD));
+        static_cast<RawTypeArguments*>(LOAD_CONSTANT(rE));
 
     RawObject* instantiator_type_args = SP[-1];
     RawObject* function_type_args = SP[0];
@@ -1886,7 +1888,7 @@
 
   {
     BYTECODE(LoadConstant, A_E);
-    FP[rA] = LOAD_CONSTANT(rD);
+    FP[rA] = LOAD_CONSTANT(rE);
     DISPATCH();
   }
 
@@ -1916,32 +1918,32 @@
 
   {
     BYTECODE(PushInt, X);
-    *++SP = Smi::New(rD);
+    *++SP = Smi::New(rX);
     DISPATCH();
   }
 
   {
     BYTECODE(Push, X);
-    *++SP = FP[rD];
+    *++SP = FP[rX];
     DISPATCH();
   }
 
   {
     BYTECODE(StoreLocal, X);
-    FP[rD] = *SP;
+    FP[rX] = *SP;
     DISPATCH();
   }
 
   {
     BYTECODE(PopLocal, X);
-    FP[rD] = *SP--;
+    FP[rX] = *SP--;
     DISPATCH();
   }
 
   {
     BYTECODE(MoveSpecial, A_Y);
     ASSERT(rA < KernelBytecode::kSpecialIndexCount);
-    FP[rD] = special_[rA];
+    FP[rY] = special_[rA];
     DISPATCH();
   }
 
@@ -1965,8 +1967,8 @@
 
     // Invoke target function.
     {
-      const uint16_t argc = rA;
-      const uint16_t kidx = rD;
+      const uint32_t argc = rF;
+      const uint32_t kidx = rD;
 
       InterpreterHelpers::IncrementUsageCounter(FrameFunction(FP));
       *++SP = LOAD_CONSTANT(kidx);
@@ -1994,8 +1996,8 @@
 #endif  // !PRODUCT
 
     {
-      const uint16_t argc = rA;
-      const uint16_t kidx = rD;
+      const uint32_t argc = rF;
+      const uint32_t kidx = rD;
 
       RawObject** call_base = SP - argc + 1;
       RawObject** call_top = SP + 1;
@@ -2026,8 +2028,8 @@
 #endif  // !PRODUCT
 
     {
-      const uint16_t argc = rA;
-      const uint16_t kidx = rD;
+      const uint32_t argc = rF;
+      const uint32_t kidx = rD;
 
       RawObject** call_base = SP - argc + 1;
       RawObject** call_top = SP + 1;
@@ -2058,8 +2060,8 @@
 #endif  // !PRODUCT
 
     {
-      const uint16_t argc = rA;
-      const uint16_t kidx = rD;
+      const uint32_t argc = rF;
+      const uint32_t kidx = rD;
 
       RawObject** call_base = SP - argc + 1;
       RawObject** call_top = SP + 1;
@@ -2440,11 +2442,11 @@
   {
     BYTECODE(StoreContextVar, A_E);
     const uword offset_in_words =
-        static_cast<uword>(Context::variable_offset(rD) / kWordSize);
+        static_cast<uword>(Context::variable_offset(rE) / kWordSize);
     RawContext* instance = reinterpret_cast<RawContext*>(SP[-1]);
     RawObject* value = reinterpret_cast<RawContext*>(SP[0]);
     SP -= 2;  // Drop instance and value.
-    ASSERT(rD < static_cast<uint32_t>(instance->ptr()->num_variables_));
+    ASSERT(rE < static_cast<uint32_t>(instance->ptr()->num_variables_));
     instance->StorePointer(
         reinterpret_cast<RawObject**>(instance->ptr()) + offset_in_words, value,
         thread);
@@ -2492,9 +2494,9 @@
   {
     BYTECODE(LoadContextVar, A_E);
     const uword offset_in_words =
-        static_cast<uword>(Context::variable_offset(rD) / kWordSize);
+        static_cast<uword>(Context::variable_offset(rE) / kWordSize);
     RawContext* instance = static_cast<RawContext*>(SP[0]);
-    ASSERT(rD < static_cast<uint32_t>(instance->ptr()->num_variables_));
+    ASSERT(rE < static_cast<uint32_t>(instance->ptr()->num_variables_));
     SP[0] = reinterpret_cast<RawObject**>(instance->ptr())[offset_in_words];
     DISPATCH();
   }
@@ -2502,7 +2504,7 @@
   {
     BYTECODE(AllocateContext, A_E);
     ++SP;
-    const uint16_t num_context_variables = rD;
+    const uint32_t num_context_variables = rE;
     if (!AllocateContext(thread, num_context_variables, pc, FP, SP)) {
       HANDLE_EXCEPTION;
     }
@@ -2604,7 +2606,7 @@
     const bool smi_ok = is_smi && may_be_smi;
     if (!smi_ok && (args[0] != null_value)) {
       RawSubtypeTestCache* cache =
-          static_cast<RawSubtypeTestCache*>(LOAD_CONSTANT(rD));
+          static_cast<RawSubtypeTestCache*>(LOAD_CONSTANT(rE));
 
       if (!AssertAssignable(thread, pc, FP, SP, args, cache)) {
         HANDLE_EXCEPTION;