1 //===-- OcamlGCPrinter.cpp - Ocaml frametable emitter ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements printing the assembly code for an Ocaml frametable.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/GCs.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/GCMetadataPrinter.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetSubtargetInfo.h"
35 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
37 void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
38 void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
42 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
43 Y("ocaml", "ocaml 3.10-compatible collector");
45 void llvm::linkOcamlGCPrinter() {}
47 static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
48 const std::string &MId = M.getModuleIdentifier();
52 size_t Letter = SymName.size();
53 SymName.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
57 // Capitalize the first letter of the module name.
58 SymName[Letter] = toupper(SymName[Letter]);
60 SmallString<128> TmpStr;
61 AP.Mang->getNameWithPrefix(TmpStr, SymName);
63 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr);
65 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
66 AP.OutStreamer.EmitLabel(Sym);
69 void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info,
71 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
72 EmitCamlGlobal(M, AP, "code_begin");
74 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
75 EmitCamlGlobal(M, AP, "data_begin");
78 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
80 /// extern "C" struct align(sizeof(intptr_t)) {
81 /// uint16_t NumDescriptors;
82 /// struct align(sizeof(intptr_t)) {
83 /// void *ReturnAddress;
84 /// uint16_t FrameSize;
85 /// uint16_t NumLiveOffsets;
86 /// uint16_t LiveOffsets[NumLiveOffsets];
87 /// } Descriptors[NumDescriptors];
88 /// } caml${module}__frametable;
90 /// Note that this precludes programs from stack frames larger than 64K
91 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
92 /// either condition is detected in a function which uses the GC.
94 void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
97 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
99 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
100 EmitCamlGlobal(M, AP, "code_end");
102 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
103 EmitCamlGlobal(M, AP, "data_end");
105 // FIXME: Why does ocaml emit this??
106 AP.OutStreamer.EmitIntValue(0, IntPtrSize);
108 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
109 EmitCamlGlobal(M, AP, "frametable");
111 int NumDescriptors = 0;
112 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
113 IE = Info.funcinfo_end();
115 GCFunctionInfo &FI = **I;
116 if (FI.getStrategy().getName() != getStrategy().getName())
117 // this function is managed by some other GC
119 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
124 if (NumDescriptors >= 1 << 16) {
126 report_fatal_error(" Too much descriptor for ocaml GC");
128 AP.EmitInt16(NumDescriptors);
129 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
131 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
132 IE = Info.funcinfo_end();
134 GCFunctionInfo &FI = **I;
135 if (FI.getStrategy().getName() != getStrategy().getName())
136 // this function is managed by some other GC
139 uint64_t FrameSize = FI.getFrameSize();
140 if (FrameSize >= 1 << 16) {
142 report_fatal_error("Function '" + FI.getFunction().getName() +
143 "' is too large for the ocaml GC! "
145 Twine(FrameSize) + ">= 65536.\n"
147 Twine(uintptr_t(&FI)) + ")");
150 AP.OutStreamer.AddComment("live roots for " +
151 Twine(FI.getFunction().getName()));
152 AP.OutStreamer.AddBlankLine();
154 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
155 size_t LiveCount = FI.live_size(J);
156 if (LiveCount >= 1 << 16) {
158 report_fatal_error("Function '" + FI.getFunction().getName() +
159 "' is too large for the ocaml GC! "
161 Twine(LiveCount) + " >= 65536.");
164 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize);
165 AP.EmitInt16(FrameSize);
166 AP.EmitInt16(LiveCount);
168 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
171 if (K->StackOffset >= 1 << 16) {
174 "GC root stack offset is outside of fixed stack frame and out "
175 "of range for ocaml GC!");
177 AP.EmitInt16(K->StackOffset);
180 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);