1 //===-- OcamlCollector.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 lowering for the llvm.gc* intrinsics compatible with
11 // Objective Caml 3.10.0, which uses a liveness-accurate static stack map.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/Collectors.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/Collector.h"
18 #include "llvm/Module.h"
19 #include "llvm/Target/TargetAsmInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
27 class VISIBILITY_HIDDEN OcamlCollector : public Collector {
31 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
32 const TargetAsmInfo &TAI);
34 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
35 const TargetAsmInfo &TAI);
40 static CollectorRegistry::Add<OcamlCollector>
41 X("ocaml", "ocaml 3.10-compatible collector");
43 // -----------------------------------------------------------------------------
45 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
46 const TargetAsmInfo &TAI, const char *Id) {
47 const std::string &MId = M.getModuleIdentifier();
50 Mangled += TAI.getGlobalPrefix();
52 size_t Letter = Mangled.size();
53 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
57 // Capitalize the first letter of the module name.
58 Mangled[Letter] = toupper(Mangled[Letter]);
60 if (const char *GlobalDirective = TAI.getGlobalDirective())
61 OS << GlobalDirective << Mangled << "\n";
62 OS << Mangled << ":\n";
65 Collector *llvm::createOcamlCollector() {
66 return new OcamlCollector();
69 OcamlCollector::OcamlCollector() {
70 NeededSafePoints = 1 << GC::PostCall;
73 void OcamlCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
74 const TargetAsmInfo &TAI) {
75 AP.SwitchToTextSection(TAI.getTextSection());
76 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
78 AP.SwitchToDataSection(TAI.getDataSection());
79 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
82 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
84 /// extern "C" struct align(sizeof(intptr_t)) {
85 /// uint16_t NumDescriptors;
86 /// struct align(sizeof(intptr_t)) {
87 /// void *ReturnAddress;
88 /// uint16_t FrameSize;
89 /// uint16_t NumLiveOffsets;
90 /// uint16_t LiveOffsets[NumLiveOffsets];
91 /// } Descriptors[NumDescriptors];
92 /// } caml${module}__frametable;
94 /// Note that this precludes programs from stack frames larger than 64K
95 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
96 /// either condition is detected in a function which uses the collector.
98 void OcamlCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
99 const TargetAsmInfo &TAI) {
100 const char *AddressDirective;
102 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
103 AddressDirective = TAI.getData32bitsDirective();
106 AddressDirective = TAI.getData64bitsDirective();
110 AP.SwitchToTextSection(TAI.getTextSection());
111 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
113 AP.SwitchToDataSection(TAI.getDataSection());
114 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
116 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
119 AP.SwitchToDataSection(TAI.getDataSection());
120 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
122 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
123 CollectorMetadata &MD = **FI;
125 OS << "\t" << TAI.getCommentString() << " live roots for "
126 << MD.getFunction().getNameStart() << "\n";
128 for (CollectorMetadata::iterator PI = MD.begin(),
129 PE = MD.end(); PI != PE; ++PI) {
131 uint64_t FrameSize = MD.getFrameSize();
132 if (FrameSize >= 2<<16) {
133 cerr << "Function '" << MD.getFunction().getNameStart()
134 << "' is too large for the ocaml collector! "
135 << "Frame size " << FrameSize << " >= 65536.\n";
136 abort(); // Very rude!
139 size_t LiveCount = MD.live_size(PI);
140 if (LiveCount >= 2<<16) {
141 cerr << "Function '" << MD.getFunction().getNameStart()
142 << "' is too large for the ocaml collector! "
143 << "Live root count " << LiveCount << " >= 65536.\n";
144 abort(); // Very rude!
147 OS << AddressDirective
148 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
149 AP.EOL("call return address");
151 AP.EmitInt16(FrameSize);
152 AP.EOL("stack frame size");
154 AP.EmitInt16(LiveCount);
155 AP.EOL("live root count");
157 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
158 LE = MD.live_end(PI);
160 assert(LI->StackOffset < 2<<16 &&
161 "GC root stack offset is outside of fixed stack frame and out "
162 "of range for Ocaml collector!");
164 OS << "\t.word\t" << LI->StackOffset;
165 AP.EOL("stack offset");
168 AP.EmitAlignment(AddressAlignLog);