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/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"
39 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
41 void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
42 void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
45 } // end anonymous namespace
47 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
48 Y("ocaml", "ocaml 3.10-compatible collector");
50 void llvm::linkOcamlGCPrinter() {}
52 static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
53 const std::string &MId = M.getModuleIdentifier();
57 size_t Letter = SymName.size();
58 SymName.append(MId.begin(), llvm::find(MId, '.'));
62 // Capitalize the first letter of the module name.
63 SymName[Letter] = toupper(SymName[Letter]);
65 SmallString<128> TmpStr;
66 Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout());
68 MCSymbol *Sym = AP.OutContext.getOrCreateSymbol(TmpStr);
70 AP.OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
71 AP.OutStreamer->EmitLabel(Sym);
74 void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info,
76 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
77 EmitCamlGlobal(M, AP, "code_begin");
79 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
80 EmitCamlGlobal(M, AP, "data_begin");
83 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
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;
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.
99 void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
101 unsigned IntPtrSize = M.getDataLayout().getPointerSize();
103 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
104 EmitCamlGlobal(M, AP, "code_end");
106 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
107 EmitCamlGlobal(M, AP, "data_end");
109 // FIXME: Why does ocaml emit this??
110 AP.OutStreamer->EmitIntValue(0, IntPtrSize);
112 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
113 EmitCamlGlobal(M, AP, "frametable");
115 int NumDescriptors = 0;
116 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
117 IE = Info.funcinfo_end();
119 GCFunctionInfo &FI = **I;
120 if (FI.getStrategy().getName() != getStrategy().getName())
121 // this function is managed by some other GC
123 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
128 if (NumDescriptors >= 1 << 16) {
130 report_fatal_error(" Too much descriptor for ocaml GC");
132 AP.EmitInt16(NumDescriptors);
133 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
135 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
136 IE = Info.funcinfo_end();
138 GCFunctionInfo &FI = **I;
139 if (FI.getStrategy().getName() != getStrategy().getName())
140 // this function is managed by some other GC
143 uint64_t FrameSize = FI.getFrameSize();
144 if (FrameSize >= 1 << 16) {
146 report_fatal_error("Function '" + FI.getFunction().getName() +
147 "' is too large for the ocaml GC! "
149 Twine(FrameSize) + ">= 65536.\n"
151 Twine(uintptr_t(&FI)) + ")");
154 AP.OutStreamer->AddComment("live roots for " +
155 Twine(FI.getFunction().getName()));
156 AP.OutStreamer->AddBlankLine();
158 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
159 size_t LiveCount = FI.live_size(J);
160 if (LiveCount >= 1 << 16) {
162 report_fatal_error("Function '" + FI.getFunction().getName() +
163 "' is too large for the ocaml GC! "
165 Twine(LiveCount) + " >= 65536.");
168 AP.OutStreamer->EmitSymbolValue(J->Label, IntPtrSize);
169 AP.EmitInt16(FrameSize);
170 AP.EmitInt16(LiveCount);
172 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
175 if (K->StackOffset >= 1 << 16) {
178 "GC root stack offset is outside of fixed stack frame and out "
179 "of range for ocaml GC!");
181 AP.EmitInt16(K->StackOffset);
184 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);