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/ADT/DenseMap.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/Collector.h"
19 #include "llvm/CodeGen/CollectorMetadata.h"
20 #include "llvm/Function.h"
21 #include "llvm/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
33 class VISIBILITY_HIDDEN OcamlCollector : public Collector {
37 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
38 const TargetAsmInfo &TAI);
40 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
41 const TargetAsmInfo &TAI);
44 CollectorRegistry::Add<OcamlCollector>
45 X("ocaml", "ocaml 3.10-compatible collector");
49 // -----------------------------------------------------------------------------
51 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
52 const TargetAsmInfo &TAI, const char *Id) {
53 const std::string &MId = M.getModuleIdentifier();
56 Mangled += TAI.getGlobalPrefix();
58 size_t Letter = Mangled.size();
59 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
63 // Capitalize the first letter of the module name.
64 Mangled[Letter] = toupper(Mangled[Letter]);
66 if (const char *GlobalDirective = TAI.getGlobalDirective())
67 OS << GlobalDirective << Mangled << "\n";
68 OS << Mangled << ":\n";
71 Collector *llvm::createOcamlCollector() {
72 return new OcamlCollector();
75 OcamlCollector::OcamlCollector() {
76 NeededSafePoints = 1 << GC::PostCall;
79 void OcamlCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
80 const TargetAsmInfo &TAI) {
81 AP.SwitchToTextSection(TAI.getTextSection());
82 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
84 AP.SwitchToDataSection(TAI.getDataSection());
85 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
88 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
90 /// extern "C" struct align(sizeof(intptr_t)) {
91 /// uint16_t NumDescriptors;
92 /// struct align(sizeof(intptr_t)) {
93 /// void *ReturnAddress;
94 /// uint16_t FrameSize;
95 /// uint16_t NumLiveOffsets;
96 /// uint16_t LiveOffsets[NumLiveOffsets];
97 /// } Descriptors[NumDescriptors];
98 /// } caml${module}__frametable;
100 /// Note that this precludes programs from stack frames larger than 64K
101 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
102 /// either condition is detected in a function which uses the collector.
104 void OcamlCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
105 const TargetAsmInfo &TAI) {
106 const char *AddressDirective;
108 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
109 AddressDirective = TAI.getData32bitsDirective();
112 AddressDirective = TAI.getData64bitsDirective();
116 AP.SwitchToTextSection(TAI.getTextSection());
117 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
119 AP.SwitchToDataSection(TAI.getDataSection());
120 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
122 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
125 AP.SwitchToDataSection(TAI.getDataSection());
126 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
128 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
129 CollectorMetadata &MD = **FI;
131 OS << "\t" << TAI.getCommentString() << " live roots for "
132 << MD.getFunction().getNameStart() << "\n";
134 for (CollectorMetadata::iterator PI = MD.begin(),
135 PE = MD.end(); PI != PE; ++PI) {
137 uint64_t FrameSize = MD.getFrameSize();
138 if (FrameSize >= 2<<16) {
139 cerr << "Function '" << MD.getFunction().getNameStart()
140 << "' is too large for the ocaml collector! "
141 << "Frame size " << FrameSize << " >= 65536.\n";
142 abort(); // Very rude!
145 size_t LiveCount = MD.live_size(PI);
146 if (LiveCount >= 2<<16) {
147 cerr << "Function '" << MD.getFunction().getNameStart()
148 << "' is too large for the ocaml collector! "
149 << "Live root count " << LiveCount << " >= 65536.\n";
150 abort(); // Very rude!
153 OS << AddressDirective
154 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
155 AP.EOL("call return address");
157 AP.EmitInt16(FrameSize);
158 AP.EOL("stack frame size");
160 AP.EmitInt16(LiveCount);
161 AP.EOL("live root count");
163 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
164 LE = MD.live_end(PI);
166 assert(LI->StackOffset < 2<<16 &&
167 "GC root stack offset is outside of fixed stack frame and out "
168 "of range for Ocaml collector!");
170 OS << "\t.word\t" << LI->StackOffset;
171 AP.EOL("stack offset");
174 AP.EmitAlignment(AddressAlignLog);