OcamlGCPrinter.cpp revision 9a38e67590fa1431bd7e950f4ce2684d9f572176
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/CodeGen/GCs.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/GCMetadataPrinter.h"
17#include "llvm/Module.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetLoweringObjectFile.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/FormattedStream.h"
26using namespace llvm;
27
28namespace {
29
30  class OcamlGCMetadataPrinter : public GCMetadataPrinter {
31  public:
32    void beginAssembly(raw_ostream &OS, AsmPrinter &AP,
33                       const MCAsmInfo &MAI);
34
35    void finishAssembly(raw_ostream &OS, AsmPrinter &AP,
36                        const MCAsmInfo &MAI);
37  };
38
39}
40
41static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
42Y("ocaml", "ocaml 3.10-compatible collector");
43
44void llvm::linkOcamlGCPrinter() { }
45
46static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP,
47                           const MCAsmInfo &MAI, const char *Id) {
48  const std::string &MId = M.getModuleIdentifier();
49
50  std::string Mangled;
51  Mangled += MAI.getGlobalPrefix();
52  Mangled += "caml";
53  size_t Letter = Mangled.size();
54  Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
55  Mangled += "__";
56  Mangled += Id;
57
58  // Capitalize the first letter of the module name.
59  Mangled[Letter] = toupper(Mangled[Letter]);
60
61  if (const char *GlobalDirective = MAI.getGlobalDirective())
62    OS << GlobalDirective << Mangled << "\n";
63  OS << Mangled << ":\n";
64}
65
66void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP,
67                                           const MCAsmInfo &MAI) {
68  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
69  EmitCamlGlobal(getModule(), OS, AP, MAI, "code_begin");
70
71  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
72  EmitCamlGlobal(getModule(), OS, AP, MAI, "data_begin");
73}
74
75/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
76///
77///   extern "C" struct align(sizeof(intptr_t)) {
78///     uint16_t NumDescriptors;
79///     struct align(sizeof(intptr_t)) {
80///       void *ReturnAddress;
81///       uint16_t FrameSize;
82///       uint16_t NumLiveOffsets;
83///       uint16_t LiveOffsets[NumLiveOffsets];
84///     } Descriptors[NumDescriptors];
85///   } caml${module}__frametable;
86///
87/// Note that this precludes programs from stack frames larger than 64K
88/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
89/// either condition is detected in a function which uses the GC.
90///
91void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP,
92                                            const MCAsmInfo &MAI) {
93  const char *AddressDirective;
94  int AddressAlignLog;
95  if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
96    AddressDirective = MAI.getData32bitsDirective();
97    AddressAlignLog = 2;
98  } else {
99    AddressDirective = MAI.getData64bitsDirective();
100    AddressAlignLog = 3;
101  }
102
103  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
104  EmitCamlGlobal(getModule(), OS, AP, MAI, "code_end");
105
106  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
107  EmitCamlGlobal(getModule(), OS, AP, MAI, "data_end");
108
109  OS << AddressDirective << 0 << '\n'; // FIXME: Why does ocaml emit this??
110
111  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
112  EmitCamlGlobal(getModule(), OS, AP, MAI, "frametable");
113
114  for (iterator I = begin(), IE = end(); I != IE; ++I) {
115    GCFunctionInfo &FI = **I;
116
117    uint64_t FrameSize = FI.getFrameSize();
118    if (FrameSize >= 1<<16) {
119      std::string msg;
120      raw_string_ostream Msg(msg);
121      Msg << "Function '" << FI.getFunction().getName()
122           << "' is too large for the ocaml GC! "
123           << "Frame size " << FrameSize << " >= 65536.\n";
124      Msg << "(" << uintptr_t(&FI) << ")";
125      llvm_report_error(Msg.str()); // Very rude!
126    }
127
128    OS << "\t" << MAI.getCommentString() << " live roots for "
129       << FI.getFunction().getName() << "\n";
130
131    for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
132      size_t LiveCount = FI.live_size(J);
133      if (LiveCount >= 1<<16) {
134        std::string msg;
135        raw_string_ostream Msg(msg);
136        Msg << "Function '" << FI.getFunction().getName()
137             << "' is too large for the ocaml GC! "
138             << "Live root count " << LiveCount << " >= 65536.";
139        llvm_report_error(Msg.str()); // Very rude!
140      }
141
142      OS << AddressDirective << J->Label->getName() << '\n';
143
144      AP.EmitInt16(FrameSize);
145
146      AP.EmitInt16(LiveCount);
147
148      for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
149                                         KE = FI.live_end(J); K != KE; ++K) {
150        assert(K->StackOffset < 1<<16 &&
151               "GC root stack offset is outside of fixed stack frame and out "
152               "of range for ocaml GC!");
153
154        AP.EmitInt32(K->StackOffset);
155      }
156
157      AP.EmitAlignment(AddressAlignLog);
158    }
159  }
160}
161