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/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"
30 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
32 void beginAssembly(raw_ostream &OS, AsmPrinter &AP,
33 const MCAsmInfo &MAI);
35 void finishAssembly(raw_ostream &OS, AsmPrinter &AP,
36 const MCAsmInfo &MAI);
41 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
42 Y("ocaml", "ocaml 3.10-compatible collector");
44 void llvm::linkOcamlGCPrinter() { }
46 static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP,
47 const MCAsmInfo &MAI, const char *Id) {
48 const std::string &MId = M.getModuleIdentifier();
51 Mangled += MAI.getGlobalPrefix();
53 size_t Letter = Mangled.size();
54 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
58 // Capitalize the first letter of the module name.
59 Mangled[Letter] = toupper(Mangled[Letter]);
61 if (const char *GlobalDirective = MAI.getGlobalDirective())
62 OS << GlobalDirective << Mangled << "\n";
63 OS << Mangled << ":\n";
66 void 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");
71 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
72 EmitCamlGlobal(getModule(), OS, AP, MAI, "data_begin");
75 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
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;
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.
91 void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP,
92 const MCAsmInfo &MAI) {
93 const char *AddressDirective;
95 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
96 AddressDirective = MAI.getData32bitsDirective();
99 AddressDirective = MAI.getData64bitsDirective();
103 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
104 EmitCamlGlobal(getModule(), OS, AP, MAI, "code_end");
106 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
107 EmitCamlGlobal(getModule(), OS, AP, MAI, "data_end");
109 OS << AddressDirective << 0 << '\n'; // FIXME: Why does ocaml emit this??
111 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
112 EmitCamlGlobal(getModule(), OS, AP, MAI, "frametable");
114 for (iterator I = begin(), IE = end(); I != IE; ++I) {
115 GCFunctionInfo &FI = **I;
117 uint64_t FrameSize = FI.getFrameSize();
118 if (FrameSize >= 1<<16) {
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!
128 OS << "\t" << MAI.getCommentString() << " live roots for "
129 << FI.getFunction().getName() << "\n";
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) {
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!
142 OS << AddressDirective << J->Label->getName() << '\n';
144 AP.EmitInt16(FrameSize);
146 AP.EmitInt16(LiveCount);
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!");
154 AP.EmitInt32(K->StackOffset);
157 AP.EmitAlignment(AddressAlignLog);