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 {
32 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
34 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
35 const TargetAsmInfo &TAI);
37 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
38 const TargetAsmInfo &TAI);
43 static CollectorRegistry::Add<OcamlCollector>
44 X("ocaml", "ocaml 3.10-compatible collector");
46 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
47 Y("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;
80 void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
81 const TargetAsmInfo &TAI) {
82 AP.SwitchToTextSection(TAI.getTextSection());
83 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
85 AP.SwitchToDataSection(TAI.getDataSection());
86 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
89 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
91 /// extern "C" struct align(sizeof(intptr_t)) {
92 /// uint16_t NumDescriptors;
93 /// struct align(sizeof(intptr_t)) {
94 /// void *ReturnAddress;
95 /// uint16_t FrameSize;
96 /// uint16_t NumLiveOffsets;
97 /// uint16_t LiveOffsets[NumLiveOffsets];
98 /// } Descriptors[NumDescriptors];
99 /// } caml${module}__frametable;
101 /// Note that this precludes programs from stack frames larger than 64K
102 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
103 /// either condition is detected in a function which uses the collector.
105 void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
106 const TargetAsmInfo &TAI) {
107 const char *AddressDirective;
109 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
110 AddressDirective = TAI.getData32bitsDirective();
113 AddressDirective = TAI.getData64bitsDirective();
117 AP.SwitchToTextSection(TAI.getTextSection());
118 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
120 AP.SwitchToDataSection(TAI.getDataSection());
121 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
123 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
126 AP.SwitchToDataSection(TAI.getDataSection());
127 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
129 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
130 CollectorMetadata &MD = **FI;
132 OS << "\t" << TAI.getCommentString() << " live roots for "
133 << MD.getFunction().getNameStart() << "\n";
135 for (CollectorMetadata::iterator PI = MD.begin(),
136 PE = MD.end(); PI != PE; ++PI) {
138 uint64_t FrameSize = MD.getFrameSize();
139 if (FrameSize >= 1<<16) {
140 cerr << "Function '" << MD.getFunction().getNameStart()
141 << "' is too large for the ocaml collector! "
142 << "Frame size " << FrameSize << " >= 65536.\n";
143 abort(); // Very rude!
146 size_t LiveCount = MD.live_size(PI);
147 if (LiveCount >= 1<<16) {
148 cerr << "Function '" << MD.getFunction().getNameStart()
149 << "' is too large for the ocaml collector! "
150 << "Live root count " << LiveCount << " >= 65536.\n";
151 abort(); // Very rude!
154 OS << AddressDirective
155 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
156 AP.EOL("call return address");
158 AP.EmitInt16(FrameSize);
159 AP.EOL("stack frame size");
161 AP.EmitInt16(LiveCount);
162 AP.EOL("live root count");
164 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
165 LE = MD.live_end(PI);
167 assert(LI->StackOffset < 1<<16 &&
168 "GC root stack offset is outside of fixed stack frame and out "
169 "of range for Ocaml collector!");
171 OS << "\t.word\t" << LI->StackOffset;
172 AP.EOL("stack offset");
175 AP.EmitAlignment(AddressAlignLog);