LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - OcamlGCPrinter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 64 79 81.0 %
Date: 2017-09-14 15:23:50 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- OcamlGCPrinter.cpp - Ocaml frametable emitter ----------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements printing the assembly code for an Ocaml frametable.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/ADT/SmallString.h"
      15             : #include "llvm/ADT/STLExtras.h"
      16             : #include "llvm/ADT/Twine.h"
      17             : #include "llvm/CodeGen/AsmPrinter.h"
      18             : #include "llvm/CodeGen/GCMetadata.h"
      19             : #include "llvm/CodeGen/GCMetadataPrinter.h"
      20             : #include "llvm/CodeGen/GCs.h"
      21             : #include "llvm/IR/DataLayout.h"
      22             : #include "llvm/IR/Function.h"
      23             : #include "llvm/IR/Mangler.h"
      24             : #include "llvm/IR/Module.h"
      25             : #include "llvm/MC/MCContext.h"
      26             : #include "llvm/MC/MCDirectives.h"
      27             : #include "llvm/MC/MCStreamer.h"
      28             : #include "llvm/Support/ErrorHandling.h"
      29             : #include "llvm/Target/TargetLoweringObjectFile.h"
      30             : #include <cctype>
      31             : #include <cstddef>
      32             : #include <cstdint>
      33             : #include <string>
      34             : 
      35             : using namespace llvm;
      36             : 
      37             : namespace {
      38             : 
      39           6 : class OcamlGCMetadataPrinter : public GCMetadataPrinter {
      40             : public:
      41             :   void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
      42             :   void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
      43             : };
      44             : 
      45             : } // end anonymous namespace
      46             : 
      47             : static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
      48      144612 :     Y("ocaml", "ocaml 3.10-compatible collector");
      49             : 
      50           0 : void llvm::linkOcamlGCPrinter() {}
      51             : 
      52          15 : static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
      53          15 :   const std::string &MId = M.getModuleIdentifier();
      54             : 
      55          30 :   std::string SymName;
      56          15 :   SymName += "caml";
      57          15 :   size_t Letter = SymName.size();
      58          60 :   SymName.append(MId.begin(), llvm::find(MId, '.'));
      59          15 :   SymName += "__";
      60          15 :   SymName += Id;
      61             : 
      62             :   // Capitalize the first letter of the module name.
      63          30 :   SymName[Letter] = toupper(SymName[Letter]);
      64             : 
      65          30 :   SmallString<128> TmpStr;
      66          30 :   Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout());
      67             : 
      68          30 :   MCSymbol *Sym = AP.OutContext.getOrCreateSymbol(TmpStr);
      69             : 
      70          30 :   AP.OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
      71          30 :   AP.OutStreamer->EmitLabel(Sym);
      72          15 : }
      73             : 
      74           3 : void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info,
      75             :                                            AsmPrinter &AP) {
      76           6 :   AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
      77           3 :   EmitCamlGlobal(M, AP, "code_begin");
      78             : 
      79           6 :   AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
      80           3 :   EmitCamlGlobal(M, AP, "data_begin");
      81           3 : }
      82             : 
      83             : /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
      84             : ///
      85             : ///   extern "C" struct align(sizeof(intptr_t)) {
      86             : ///     uint16_t NumDescriptors;
      87             : ///     struct align(sizeof(intptr_t)) {
      88             : ///       void *ReturnAddress;
      89             : ///       uint16_t FrameSize;
      90             : ///       uint16_t NumLiveOffsets;
      91             : ///       uint16_t LiveOffsets[NumLiveOffsets];
      92             : ///     } Descriptors[NumDescriptors];
      93             : ///   } caml${module}__frametable;
      94             : ///
      95             : /// Note that this precludes programs from stack frames larger than 64K
      96             : /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
      97             : /// either condition is detected in a function which uses the GC.
      98             : ///
      99           3 : void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
     100             :                                             AsmPrinter &AP) {
     101           3 :   unsigned IntPtrSize = M.getDataLayout().getPointerSize();
     102             : 
     103           6 :   AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
     104           3 :   EmitCamlGlobal(M, AP, "code_end");
     105             : 
     106           6 :   AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
     107           3 :   EmitCamlGlobal(M, AP, "data_end");
     108             : 
     109             :   // FIXME: Why does ocaml emit this??
     110           6 :   AP.OutStreamer->EmitIntValue(0, IntPtrSize);
     111             : 
     112           6 :   AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
     113           3 :   EmitCamlGlobal(M, AP, "frametable");
     114             : 
     115           3 :   int NumDescriptors = 0;
     116           3 :   for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
     117           3 :                                            IE = Info.funcinfo_end();
     118           6 :        I != IE; ++I) {
     119           6 :     GCFunctionInfo &FI = **I;
     120          12 :     if (FI.getStrategy().getName() != getStrategy().getName())
     121             :       // this function is managed by some other GC
     122           0 :       continue;
     123          10 :     for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
     124           1 :       NumDescriptors++;
     125             :     }
     126             :   }
     127             : 
     128           3 :   if (NumDescriptors >= 1 << 16) {
     129             :     // Very rude!
     130           0 :     report_fatal_error(" Too much descriptor for ocaml GC");
     131             :   }
     132           3 :   AP.EmitInt16(NumDescriptors);
     133           3 :   AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
     134             : 
     135           3 :   for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
     136           3 :                                            IE = Info.funcinfo_end();
     137           6 :        I != IE; ++I) {
     138           6 :     GCFunctionInfo &FI = **I;
     139          12 :     if (FI.getStrategy().getName() != getStrategy().getName())
     140             :       // this function is managed by some other GC
     141           0 :       continue;
     142             : 
     143           3 :     uint64_t FrameSize = FI.getFrameSize();
     144           3 :     if (FrameSize >= 1 << 16) {
     145             :       // Very rude!
     146           0 :       report_fatal_error("Function '" + FI.getFunction().getName() +
     147             :                          "' is too large for the ocaml GC! "
     148           0 :                          "Frame size " +
     149           0 :                          Twine(FrameSize) + ">= 65536.\n"
     150           0 :                                             "(" +
     151           0 :                          Twine(uintptr_t(&FI)) + ")");
     152             :     }
     153             : 
     154           9 :     AP.OutStreamer->AddComment("live roots for " +
     155          12 :                                Twine(FI.getFunction().getName()));
     156           6 :     AP.OutStreamer->AddBlankLine();
     157             : 
     158          10 :     for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
     159           2 :       size_t LiveCount = FI.live_size(J);
     160           1 :       if (LiveCount >= 1 << 16) {
     161             :         // Very rude!
     162           0 :         report_fatal_error("Function '" + FI.getFunction().getName() +
     163             :                            "' is too large for the ocaml GC! "
     164           0 :                            "Live root count " +
     165           0 :                            Twine(LiveCount) + " >= 65536.");
     166             :       }
     167             : 
     168           2 :       AP.OutStreamer->EmitSymbolValue(J->Label, IntPtrSize);
     169           1 :       AP.EmitInt16(FrameSize);
     170           1 :       AP.EmitInt16(LiveCount);
     171             : 
     172           2 :       for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
     173           2 :                                          KE = FI.live_end(J);
     174           1 :            K != KE; ++K) {
     175           0 :         if (K->StackOffset >= 1 << 16) {
     176             :           // Very rude!
     177           0 :           report_fatal_error(
     178             :               "GC root stack offset is outside of fixed stack frame and out "
     179             :               "of range for ocaml GC!");
     180             :         }
     181           0 :         AP.EmitInt16(K->StackOffset);
     182             :       }
     183             : 
     184           1 :       AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
     185             :     }
     186             :   }
     187      144615 : }

Generated by: LCOV version 1.13