[llvm] r248394 - [WebAssembly] Fix hasAddr64 being used before being initializer.

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 09:59:10 PDT 2015


Author: djg
Date: Wed Sep 23 11:59:10 2015
New Revision: 248394

URL: http://llvm.org/viewvc/llvm-project?rev=248394&view=rev
Log:
[WebAssembly] Fix hasAddr64 being used before being initializer.

This reverts r248388 and fixes the underlying bug: hasAddr64 was initialized
in runOnMachineFunction, but runOnMachineFunction isn't ever called in
CodeGen/WebAssembly/global.ll since that testcase has no functions. The fix
here is to use AsmPrinter's getPointerSize() as needed to determine the
pointer size instead.

Modified:
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp?rev=248394&r1=248393&r2=248394&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp Wed Sep 23 11:59:10 2015
@@ -40,12 +40,11 @@ using namespace llvm;
 namespace {
 
 class WebAssemblyAsmPrinter final : public AsmPrinter {
-  bool hasAddr64;
   const WebAssemblyInstrInfo *TII;
 
 public:
   WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
-      : AsmPrinter(TM, std::move(Streamer)), hasAddr64(false), TII(nullptr) {}
+      : AsmPrinter(TM, std::move(Streamer)), TII(nullptr) {}
 
 private:
   const char *getPassName() const override {
@@ -62,7 +61,6 @@ private:
 
   bool runOnMachineFunction(MachineFunction &MF) override {
     const auto &Subtarget = MF.getSubtarget<WebAssemblySubtarget>();
-    hasAddr64 = Subtarget.hasAddr64();
     TII = Subtarget.getInstrInfo();
     return AsmPrinter::runOnMachineFunction(MF);
   }
@@ -80,6 +78,9 @@ private:
   void EmitFunctionBodyEnd() override;
 
   void EmitInstruction(const MachineInstr *MI) override;
+
+  static std::string toString(const APFloat &APF);
+  const char *toString(Type *Ty) const;
 };
 
 } // end anonymous namespace
@@ -101,7 +102,7 @@ static SmallString<32> OpcodeName(const
 
 static std::string toSymbol(StringRef S) { return ("$" + S).str(); }
 
-static std::string toString(const APFloat &FP) {
+std::string WebAssemblyAsmPrinter::toString(const APFloat &FP) {
   static const size_t BufBytes = 128;
   char buf[BufBytes];
   if (FP.isNaN())
@@ -118,20 +119,37 @@ static std::string toString(const APFloa
   return buf;
 }
 
-static const char *toString(const Type *Ty, bool hasAddr64) {
+const char *WebAssemblyAsmPrinter::toString(Type *Ty) const {
   switch (Ty->getTypeID()) {
-  default: break;
+  default:
+    break;
   // Treat all pointers as the underlying integer into linear memory.
-  case Type::PointerTyID: return hasAddr64 ? "i64" : "i32";
-  case Type::FloatTyID:  return "f32";
-  case Type::DoubleTyID: return "f64";
+  case Type::PointerTyID:
+    switch (getPointerSize()) {
+    case 4:
+      return "i32";
+    case 8:
+      return "i64";
+    default:
+      llvm_unreachable("unsupported pointer size");
+    }
+    break;
+  case Type::FloatTyID:
+    return "f32";
+  case Type::DoubleTyID:
+    return "f64";
   case Type::IntegerTyID:
     switch (Ty->getIntegerBitWidth()) {
-    case 8: return "i8";
-    case 16: return "i16";
-    case 32: return "i32";
-    case 64: return "i64";
-    default: break;
+    case 8:
+      return "i8";
+    case 16:
+      return "i16";
+    case 32:
+      return "i32";
+    case 64:
+      return "i64";
+    default:
+      break;
     }
   }
   DEBUG(dbgs() << "Invalid type "; Ty->print(dbgs()); dbgs() << '\n');
@@ -139,7 +157,6 @@ static const char *toString(const Type *
   return "<invalid>";
 }
 
-
 //===----------------------------------------------------------------------===//
 // WebAssemblyAsmPrinter Implementation.
 //===----------------------------------------------------------------------===//
@@ -195,8 +212,7 @@ void WebAssemblyAsmPrinter::EmitGlobalVa
     return;
   }
 
-  OS << "(global " << toSymbol(Name) << ' '
-     << toString(Init->getType(), hasAddr64) << ' ';
+  OS << "(global " << toSymbol(Name) << ' ' << toString(Init->getType()) << ' ';
   if (const auto *C = dyn_cast<ConstantInt>(Init)) {
     assert(C->getBitWidth() <= 64 && "Printing wider types unimplemented");
     OS << C->getZExtValue();
@@ -241,12 +257,12 @@ void WebAssemblyAsmPrinter::EmitFunction
   SmallString<128> Str;
   raw_svector_ostream OS(Str);
   const Function *F = MF->getFunction();
-  const Type *Rt = F->getReturnType();
+  Type *Rt = F->getReturnType();
   if (!Rt->isVoidTy() || !F->arg_empty()) {
     for (const Argument &A : F->args())
-      OS << " (param " << toString(A.getType(), hasAddr64) << ')';
+      OS << " (param " << toString(A.getType()) << ')';
     if (!Rt->isVoidTy())
-      OS << " (result " << toString(Rt, hasAddr64) << ')';
+      OS << " (result " << toString(Rt) << ')';
     OutStreamer->EmitRawText(OS.str());
   }
 }




More information about the llvm-commits mailing list