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