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/Support/raw_ostream.h"
19 #include "llvm/Target/TargetAsmInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
27 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
29 void beginAssembly(raw_ostream &OS, AsmPrinter &AP,
30 const TargetAsmInfo &TAI);
32 void finishAssembly(raw_ostream &OS, AsmPrinter &AP,
33 const TargetAsmInfo &TAI);
38 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
39 Y("ocaml", "ocaml 3.10-compatible collector");
41 void llvm::linkOcamlGCPrinter() { }
43 static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP,
44 const TargetAsmInfo &TAI, const char *Id) {
45 const std::string &MId = M.getModuleIdentifier();
48 Mangled += TAI.getGlobalPrefix();
50 size_t Letter = Mangled.size();
51 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
55 // Capitalize the first letter of the module name.
56 Mangled[Letter] = toupper(Mangled[Letter]);
58 if (const char *GlobalDirective = TAI.getGlobalDirective())
59 OS << GlobalDirective << Mangled << "\n";
60 OS << Mangled << ":\n";
63 void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP,
64 const TargetAsmInfo &TAI) {
65 AP.SwitchToSection(TAI.getTextSection());
66 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
68 AP.SwitchToSection(TAI.getDataSection());
69 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
72 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
74 /// extern "C" struct align(sizeof(intptr_t)) {
75 /// uint16_t NumDescriptors;
76 /// struct align(sizeof(intptr_t)) {
77 /// void *ReturnAddress;
78 /// uint16_t FrameSize;
79 /// uint16_t NumLiveOffsets;
80 /// uint16_t LiveOffsets[NumLiveOffsets];
81 /// } Descriptors[NumDescriptors];
82 /// } caml${module}__frametable;
84 /// Note that this precludes programs from stack frames larger than 64K
85 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
86 /// either condition is detected in a function which uses the GC.
88 void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP,
89 const TargetAsmInfo &TAI) {
90 const char *AddressDirective;
92 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
93 AddressDirective = TAI.getData32bitsDirective();
96 AddressDirective = TAI.getData64bitsDirective();
100 AP.SwitchToSection(TAI.getTextSection());
101 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
103 AP.SwitchToSection(TAI.getDataSection());
104 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
106 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
109 AP.SwitchToSection(TAI.getDataSection());
110 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
112 for (iterator I = begin(), IE = end(); I != IE; ++I) {
113 GCFunctionInfo &FI = **I;
115 uint64_t FrameSize = FI.getFrameSize();
116 if (FrameSize >= 1<<16) {
117 cerr << "Function '" << FI.getFunction().getNameStart()
118 << "' is too large for the ocaml GC! "
119 << "Frame size " << FrameSize << " >= 65536.\n";
120 cerr << "(" << uintptr_t(&FI) << ")\n";
121 abort(); // Very rude!
124 OS << "\t" << TAI.getCommentString() << " live roots for "
125 << FI.getFunction().getNameStart() << "\n";
127 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
128 size_t LiveCount = FI.live_size(J);
129 if (LiveCount >= 1<<16) {
130 cerr << "Function '" << FI.getFunction().getNameStart()
131 << "' is too large for the ocaml GC! "
132 << "Live root count " << LiveCount << " >= 65536.\n";
133 abort(); // Very rude!
136 OS << AddressDirective
137 << TAI.getPrivateGlobalPrefix() << "label" << J->Num;
138 AP.EOL("call return address");
140 AP.EmitInt16(FrameSize);
141 AP.EOL("stack frame size");
143 AP.EmitInt16(LiveCount);
144 AP.EOL("live root count");
146 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
147 KE = FI.live_end(J); K != KE; ++K) {
148 assert(K->StackOffset < 1<<16 &&
149 "GC root stack offset is outside of fixed stack frame and out "
150 "of range for ocaml GC!");
152 OS << "\t.word\t" << K->StackOffset;
153 AP.EOL("stack offset");
156 AP.EmitAlignment(AddressAlignLog);