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/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/GCMetadataPrinter.h"
17 #include "llvm/Module.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Target/Mangler.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetLoweringObjectFile.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FormattedStream.h"
33 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
35 void beginAssembly(AsmPrinter &AP);
36 void finishAssembly(AsmPrinter &AP);
41 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
42 Y("ocaml", "ocaml 3.10-compatible collector");
44 void llvm::linkOcamlGCPrinter() { }
46 static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
47 const std::string &MId = M.getModuleIdentifier();
51 size_t Letter = SymName.size();
52 SymName.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
56 // Capitalize the first letter of the module name.
57 SymName[Letter] = toupper(SymName[Letter]);
59 SmallString<128> TmpStr;
60 AP.Mang->getNameWithPrefix(TmpStr, SymName);
62 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr);
64 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
65 AP.OutStreamer.EmitLabel(Sym);
68 void OcamlGCMetadataPrinter::beginAssembly(AsmPrinter &AP) {
69 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
70 EmitCamlGlobal(getModule(), AP, "code_begin");
72 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
73 EmitCamlGlobal(getModule(), AP, "data_begin");
76 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
78 /// extern "C" struct align(sizeof(intptr_t)) {
79 /// uint16_t NumDescriptors;
80 /// struct align(sizeof(intptr_t)) {
81 /// void *ReturnAddress;
82 /// uint16_t FrameSize;
83 /// uint16_t NumLiveOffsets;
84 /// uint16_t LiveOffsets[NumLiveOffsets];
85 /// } Descriptors[NumDescriptors];
86 /// } caml${module}__frametable;
88 /// Note that this precludes programs from stack frames larger than 64K
89 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
90 /// either condition is detected in a function which uses the GC.
92 void OcamlGCMetadataPrinter::finishAssembly(AsmPrinter &AP) {
93 unsigned IntPtrSize = AP.TM.getTargetData()->getPointerSize();
95 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
96 EmitCamlGlobal(getModule(), AP, "code_end");
98 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
99 EmitCamlGlobal(getModule(), AP, "data_end");
101 // FIXME: Why does ocaml emit this??
102 AP.OutStreamer.EmitIntValue(0, IntPtrSize, 0);
104 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
105 EmitCamlGlobal(getModule(), AP, "frametable");
107 int NumDescriptors = 0;
108 for (iterator I = begin(), IE = end(); I != IE; ++I) {
109 GCFunctionInfo &FI = **I;
110 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
115 if (NumDescriptors >= 1<<16) {
117 report_fatal_error(" Too much descriptor for ocaml GC");
119 AP.EmitInt16(NumDescriptors);
120 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
122 for (iterator I = begin(), IE = end(); I != IE; ++I) {
123 GCFunctionInfo &FI = **I;
125 uint64_t FrameSize = FI.getFrameSize();
126 if (FrameSize >= 1<<16) {
128 report_fatal_error("Function '" + FI.getFunction().getName() +
129 "' is too large for the ocaml GC! "
130 "Frame size " + Twine(FrameSize) + ">= 65536.\n"
131 "(" + Twine(uintptr_t(&FI)) + ")");
134 AP.OutStreamer.AddComment("live roots for " +
135 Twine(FI.getFunction().getName()));
136 AP.OutStreamer.AddBlankLine();
138 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
139 size_t LiveCount = FI.live_size(J);
140 if (LiveCount >= 1<<16) {
142 report_fatal_error("Function '" + FI.getFunction().getName() +
143 "' is too large for the ocaml GC! "
144 "Live root count "+Twine(LiveCount)+" >= 65536.");
147 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize, 0);
148 AP.EmitInt16(FrameSize);
149 AP.EmitInt16(LiveCount);
151 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
152 KE = FI.live_end(J); K != KE; ++K) {
153 if (K->StackOffset >= 1<<16) {
156 "GC root stack offset is outside of fixed stack frame and out "
157 "of range for ocaml GC!");
159 AP.EmitInt16(K->StackOffset);
162 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);