[vm/ffi] Refactor Native* Zone usage

This CL refactors all dart::Zone usage in Native* classes in
vm/compiler/ffi to be explicit, so that they are never looked up from
Thread::Current().

Also, this CL changes the ToCString printing to take a Zone as argument,
and uses the ZoneTextBuffer.

Finally, it clears up imports.

TEST=Covered by existing tests in `tests/ffi` and `tests/ffi_2`.

Change-Id: I8655cfc98a3fcc783e6ea8fc954ca50070fa2b53
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-win-release-ia32-try,vm-kernel-win-debug-ia32-try,vm-kernel-linux-debug-ia32-try,vm-kernel-mac-debug-x64-try,vm-kernel-precomp-android-release-arm_x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171724
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Clement Skau <cskau@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
diff --git a/runtime/vm/compiler/backend/flow_graph_compiler.cc b/runtime/vm/compiler/backend/flow_graph_compiler.cc
index 535b34c..9f3015f 100644
--- a/runtime/vm/compiler/backend/flow_graph_compiler.cc
+++ b/runtime/vm/compiler/backend/flow_graph_compiler.cc
@@ -2846,7 +2846,7 @@
     }
     const auto& intermediate =
         *new (zone_) compiler::ffi::NativeRegistersLocation(
-            dst_payload_type, dst_container_type, scratch);
+            zone_, dst_payload_type, dst_container_type, scratch);
     EmitNativeMove(intermediate, source, temp);
     EmitNativeMove(destination, intermediate, temp);
     if (TMP == kNoRegister) {
diff --git a/runtime/vm/compiler/ffi/native_calling_convention.cc b/runtime/vm/compiler/ffi/native_calling_convention.cc
index 2ac1445..fe3881c 100644
--- a/runtime/vm/compiler/ffi/native_calling_convention.cc
+++ b/runtime/vm/compiler/ffi/native_calling_convention.cc
@@ -4,11 +4,10 @@
 
 #include "vm/compiler/ffi/native_calling_convention.h"
 
-#include "vm/compiler/ffi/marshaller.h"
 #include "vm/compiler/ffi/native_location.h"
 #include "vm/compiler/ffi/native_type.h"
-#include "vm/log.h"
-#include "vm/symbols.h"
+#include "vm/cpu.h"
+#include "vm/zone_text_buffer.h"
 
 namespace dart {
 
@@ -82,13 +81,13 @@
           const Register register_1 = AllocateCpuRegister();
           const Register register_2 = AllocateCpuRegister();
           return *new (zone_) NativeRegistersLocation(
-              payload_type, container_type, register_1, register_2);
+              zone_, payload_type, container_type, register_1, register_2);
         }
       } else {
         ASSERT(payload_type.SizeInBytes() <= target::kWordSize);
         if (cpu_regs_used + 1 <= CallingConventions::kNumArgRegs) {
           return *new (zone_) NativeRegistersLocation(
-              payload_type, container_type, AllocateCpuRegister());
+              zone_, payload_type, container_type, AllocateCpuRegister());
         } else {
           // Transfer on stack.
         }
@@ -232,12 +231,12 @@
   ASSERT(container_type.IsInt() || container_type.IsVoid());
   if (container_type.SizeInBytes() == 8 && target::kWordSize == 4) {
     return *new (zone) NativeRegistersLocation(
-        payload_type, container_type, CallingConventions::kReturnReg,
+        zone, payload_type, container_type, CallingConventions::kReturnReg,
         CallingConventions::kSecondReturnReg);
   }
 
   ASSERT(container_type.SizeInBytes() <= target::kWordSize);
-  return *new (zone) NativeRegistersLocation(payload_type, container_type,
+  return *new (zone) NativeRegistersLocation(zone, payload_type, container_type,
                                              CallingConventions::kReturnReg);
 }
 
@@ -261,23 +260,41 @@
   return Utils::RoundUp(max_height_in_bytes, compiler::target::kWordSize);
 }
 
-const char* NativeCallingConvention::ToCString() const {
-  char buffer[1024];
-  BufferFormatter bf(buffer, 1024);
-  PrintTo(&bf);
-  return Thread::Current()->zone()->MakeCopyOfString(buffer);
-}
-
-void NativeCallingConvention::PrintTo(BaseTextBuffer* f) const {
-  f->AddString("(");
+void NativeCallingConvention::PrintTo(BaseTextBuffer* f,
+                                      bool multi_line) const {
+  if (!multi_line) {
+    f->AddString("(");
+  }
   for (intptr_t i = 0; i < argument_locations_.length(); i++) {
     if (i > 0) {
-      f->AddString(", ");
+      if (multi_line) {
+        f->AddString("\n");
+      } else {
+        f->AddString(", ");
+      }
     }
     argument_locations_[i]->PrintTo(f);
   }
-  f->AddString(") => ");
+  if (multi_line) {
+    f->AddString("\n=>\n");
+  } else {
+    f->AddString(") => ");
+  }
   return_location_.PrintTo(f);
+  if (multi_line) {
+    f->AddString("\n");
+  }
+}
+
+const char* NativeCallingConvention::ToCString(Zone* zone,
+                                               bool multi_line) const {
+  ZoneTextBuffer textBuffer(zone);
+  PrintTo(&textBuffer, multi_line);
+  return textBuffer.buffer();
+}
+
+const char* NativeCallingConvention::ToCString(bool multi_line) const {
+  return ToCString(Thread::Current()->zone(), multi_line);
 }
 
 }  // namespace ffi
diff --git a/runtime/vm/compiler/ffi/native_calling_convention.h b/runtime/vm/compiler/ffi/native_calling_convention.h
index 4948910..bb10887 100644
--- a/runtime/vm/compiler/ffi/native_calling_convention.h
+++ b/runtime/vm/compiler/ffi/native_calling_convention.h
@@ -9,9 +9,7 @@
 #error "AOT runtime should not use compiler sources (including header files)"
 #endif  // defined(DART_PRECOMPILED_RUNTIME)
 
-#include <platform/globals.h>
-
-#include "vm/compiler/backend/locations.h"
+#include "platform/globals.h"
 #include "vm/compiler/ffi/native_location.h"
 #include "vm/compiler/ffi/native_type.h"
 
@@ -41,8 +39,10 @@
 
   intptr_t StackTopInBytes() const;
 
-  void PrintTo(BaseTextBuffer* f) const;
-  const char* ToCString() const;
+  void PrintTo(BaseTextBuffer* f, bool multi_line = false) const;
+  void PrintToMultiLine(BaseTextBuffer* f) const;
+  const char* ToCString(Zone* zone, bool multi_line = false) const;
+  const char* ToCString(bool multi_line = false) const;
 
  private:
   NativeCallingConvention(const NativeLocations& argument_locations,
diff --git a/runtime/vm/compiler/ffi/native_location.cc b/runtime/vm/compiler/ffi/native_location.cc
index f954e9f..dce51c1 100644
--- a/runtime/vm/compiler/ffi/native_location.cc
+++ b/runtime/vm/compiler/ffi/native_location.cc
@@ -4,7 +4,7 @@
 
 #include "vm/compiler/ffi/native_location.h"
 
-#include "vm/compiler/backend/il_printer.h"
+#include "vm/zone_text_buffer.h"
 
 namespace dart {
 
@@ -41,7 +41,7 @@
   switch (loc.kind()) {
     case Location::Kind::kRegister:
       return *new (zone)
-          NativeRegistersLocation(native_rep, native_rep, loc.reg());
+          NativeRegistersLocation(zone, native_rep, native_rep, loc.reg());
     case Location::Kind::kFpuRegister:
       return *new (zone)
           NativeFpuRegistersLocation(native_rep, native_rep, loc.fpu_reg());
@@ -126,12 +126,13 @@
   }
   UNREACHABLE();
 }
+
 NativeRegistersLocation& NativeRegistersLocation::Split(Zone* zone,
                                                         intptr_t index) const {
   ASSERT(num_regs() == 2);
   return *new (zone) NativeRegistersLocation(
-      payload_type().Split(zone, index), container_type().Split(zone, index),
-      reg_at(index));
+      zone, payload_type().Split(zone, index),
+      container_type().Split(zone, index), reg_at(index));
 }
 
 NativeStackLocation& NativeStackLocation::Split(Zone* zone,
@@ -264,11 +265,14 @@
   PrintRepresentations(f, *this);
 }
 
+const char* NativeLocation::ToCString(Zone* zone) const {
+  ZoneTextBuffer textBuffer(zone);
+  PrintTo(&textBuffer);
+  return textBuffer.buffer();
+}
+
 const char* NativeLocation::ToCString() const {
-  char buffer[1024];
-  BufferFormatter bf(buffer, 1024);
-  PrintTo(&bf);
-  return Thread::Current()->zone()->MakeCopyOfString(buffer);
+  return ToCString(Thread::Current()->zone());
 }
 
 intptr_t SizeFromFpuRegisterKind(enum FpuRegisterKind kind) {
diff --git a/runtime/vm/compiler/ffi/native_location.h b/runtime/vm/compiler/ffi/native_location.h
index 6c7e980..c3cce14 100644
--- a/runtime/vm/compiler/ffi/native_location.h
+++ b/runtime/vm/compiler/ffi/native_location.h
@@ -12,8 +12,8 @@
 #include "platform/assert.h"
 #include "vm/compiler/backend/locations.h"
 #include "vm/compiler/ffi/native_type.h"
+#include "vm/constants.h"
 #include "vm/growable_array.h"
-#include "vm/thread.h"
 
 namespace dart {
 
@@ -97,6 +97,7 @@
   }
 
   virtual void PrintTo(BaseTextBuffer* f) const;
+  const char* ToCString(Zone* zone) const;
   const char* ToCString() const;
 
   const NativeRegistersLocation& AsRegisters() const;
@@ -138,19 +139,21 @@
                           const NativeType& container_type,
                           ZoneGrowableArray<Register>* registers)
       : NativeLocation(payload_type, container_type), regs_(registers) {}
-  NativeRegistersLocation(const NativeType& payload_type,
+  NativeRegistersLocation(Zone* zone,
+                          const NativeType& payload_type,
                           const NativeType& container_type,
                           Register reg)
       : NativeLocation(payload_type, container_type) {
-    regs_ = new ZoneGrowableArray<Register>();
+    regs_ = new (zone) ZoneGrowableArray<Register>(zone, 1);
     regs_->Add(reg);
   }
-  NativeRegistersLocation(const NativeType& payload_type,
+  NativeRegistersLocation(Zone* zone,
+                          const NativeType& payload_type,
                           const NativeType& container_type,
                           Register register1,
                           Register register2)
       : NativeLocation(payload_type, container_type) {
-    regs_ = new ZoneGrowableArray<Register>();
+    regs_ = new (zone) ZoneGrowableArray<Register>(zone, 2);
     regs_->Add(register1);
     regs_->Add(register2);
   }
diff --git a/runtime/vm/compiler/ffi/native_type.cc b/runtime/vm/compiler/ffi/native_type.cc
index c5cd230..7c1faae 100644
--- a/runtime/vm/compiler/ffi/native_type.cc
+++ b/runtime/vm/compiler/ffi/native_type.cc
@@ -7,11 +7,8 @@
 #include "platform/assert.h"
 #include "platform/globals.h"
 #include "vm/class_id.h"
-#include "vm/compiler/runtime_api.h"
-#include "vm/growable_array.h"
-#include "vm/log.h"
-#include "vm/object.h"
-#include "vm/symbols.h"
+#include "vm/constants.h"
+#include "vm/zone_text_buffer.h"
 
 #if !defined(DART_PRECOMPILED_RUNTIME)
 #include "vm/compiler/backend/locations.h"
@@ -282,11 +279,14 @@
 }
 #endif  // !defined(DART_PRECOMPILED_RUNTIME)
 
+const char* NativeType::ToCString(Zone* zone) const {
+  ZoneTextBuffer textBuffer(zone);
+  PrintTo(&textBuffer);
+  return textBuffer.buffer();
+}
+
 const char* NativeType::ToCString() const {
-  char buffer[1024];
-  BufferFormatter bf(buffer, 1024);
-  PrintTo(&bf);
-  return Thread::Current()->zone()->MakeCopyOfString(buffer);
+  return ToCString(Thread::Current()->zone());
 }
 
 static const char* PrimitiveTypeToCString(PrimitiveType rep) {
@@ -328,11 +328,14 @@
   f->Printf("%s", PrimitiveTypeToCString(representation_));
 }
 
+const char* NativeFunctionType::ToCString(Zone* zone) const {
+  ZoneTextBuffer textBuffer(zone);
+  PrintTo(&textBuffer);
+  return textBuffer.buffer();
+}
+
 const char* NativeFunctionType::ToCString() const {
-  char buffer[1024];
-  BufferFormatter bf(buffer, 1024);
-  PrintTo(&bf);
-  return Thread::Current()->zone()->MakeCopyOfString(buffer);
+  return ToCString(Thread::Current()->zone());
 }
 
 void NativeFunctionType::PrintTo(BaseTextBuffer* f) const {
diff --git a/runtime/vm/compiler/ffi/native_type.h b/runtime/vm/compiler/ffi/native_type.h
index 693b7c0..d2ca9c1 100644
--- a/runtime/vm/compiler/ffi/native_type.h
+++ b/runtime/vm/compiler/ffi/native_type.h
@@ -5,12 +5,11 @@
 #ifndef RUNTIME_VM_COMPILER_FFI_NATIVE_TYPE_H_
 #define RUNTIME_VM_COMPILER_FFI_NATIVE_TYPE_H_
 
-#include <platform/globals.h>
-
 #include "platform/assert.h"
+#include "platform/globals.h"
 #include "vm/allocation.h"
-#include "vm/compiler/runtime_api.h"
 #include "vm/growable_array.h"
+#include "vm/object.h"
 
 #if !defined(DART_PRECOMPILED_RUNTIME)
 #include "vm/compiler/backend/locations.h"
@@ -58,7 +57,7 @@
 #if !defined(DART_PRECOMPILED_RUNTIME)
   static NativePrimitiveType& FromUnboxedRepresentation(Zone* zone,
                                                         Representation rep);
-#endif
+#endif  // !defined(DART_PRECOMPILED_RUNTIME)
 
   virtual bool IsPrimitive() const { return false; }
   const NativePrimitiveType& AsPrimitive() const;
@@ -104,6 +103,7 @@
   const NativeType& WidenTo4Bytes(Zone* zone) const;
 
   virtual void PrintTo(BaseTextBuffer* f) const;
+  const char* ToCString(Zone* zone) const;
   const char* ToCString() const;
 
   virtual ~NativeType() {}
@@ -180,6 +180,8 @@
   const NativeType& return_type() const { return return_type_; }
 
   void PrintTo(BaseTextBuffer* f) const;
+  const char* ToCString(Zone* zone) const;
+
   const char* ToCString() const;
 
  private: