1 //===-- OcamlGCMetadataPrinter.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 gc metadata printing for the llvm.gc* intrinsics
11 // compatible with Objective Caml 3.10.0, which uses a liveness-accurate static
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/Collectors.h"
18 #include "llvm/CodeGen/Collector.h"
19 #include "llvm/Module.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
27 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
29 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
30 const TargetAsmInfo &TAI);
31 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
32 const TargetAsmInfo &TAI);
36 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
37 X("ocaml", "ocaml 3.10-compatible collector");
39 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
40 const TargetAsmInfo &TAI, const char *Id) {
41 const std::string &MId = M.getModuleIdentifier();
44 Mangled += TAI.getGlobalPrefix();
46 size_t Letter = Mangled.size();
47 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
51 // Capitalize the first letter of the module name.
52 Mangled[Letter] = toupper(Mangled[Letter]);
54 if (const char *GlobalDirective = TAI.getGlobalDirective())
55 OS << GlobalDirective << Mangled << "\n";
56 OS << Mangled << ":\n";
59 void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
60 const TargetAsmInfo &TAI) {
61 AP.SwitchToTextSection(TAI.getTextSection());
62 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
64 AP.SwitchToDataSection(TAI.getDataSection());
65 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
68 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
70 /// extern "C" struct align(sizeof(intptr_t)) {
71 /// uint16_t NumDescriptors;
72 /// struct align(sizeof(intptr_t)) {
73 /// void *ReturnAddress;
74 /// uint16_t FrameSize;
75 /// uint16_t NumLiveOffsets;
76 /// uint16_t LiveOffsets[NumLiveOffsets];
77 /// } Descriptors[NumDescriptors];
78 /// } caml${module}__frametable;
80 /// Note that this precludes programs from stack frames larger than 64K
81 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
82 /// either condition is detected in a function which uses the collector.
84 void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
85 const TargetAsmInfo &TAI) {
86 const char *AddressDirective;
88 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
89 AddressDirective = TAI.getData32bitsDirective();
92 AddressDirective = TAI.getData64bitsDirective();
96 AP.SwitchToTextSection(TAI.getTextSection());
97 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
99 AP.SwitchToDataSection(TAI.getDataSection());
100 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
102 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
105 AP.SwitchToDataSection(TAI.getDataSection());
106 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
108 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
109 CollectorMetadata &MD = **FI;
111 OS << "\t" << TAI.getCommentString() << " live roots for "
112 << MD.getFunction().getNameStart() << "\n";
114 for (CollectorMetadata::iterator PI = MD.begin(),
115 PE = MD.end(); PI != PE; ++PI) {
117 uint64_t FrameSize = MD.getFrameSize();
118 if (FrameSize >= 1<<16) {
119 cerr << "Function '" << MD.getFunction().getNameStart()
120 << "' is too large for the ocaml collector! "
121 << "Frame size " << FrameSize << " >= 65536.\n";
122 abort(); // Very rude!
125 size_t LiveCount = MD.live_size(PI);
126 if (LiveCount >= 1<<16) {
127 cerr << "Function '" << MD.getFunction().getNameStart()
128 << "' is too large for the ocaml collector! "
129 << "Live root count " << LiveCount << " >= 65536.\n";
130 abort(); // Very rude!
133 OS << AddressDirective
134 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
135 AP.EOL("call return address");
137 AP.EmitInt16(FrameSize);
138 AP.EOL("stack frame size");
140 AP.EmitInt16(LiveCount);
141 AP.EOL("live root count");
143 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
144 LE = MD.live_end(PI);
146 assert(LI->StackOffset < 1<<16 &&
147 "GC root stack offset is outside of fixed stack frame and out "
148 "of range for Ocaml collector!");
150 OS << "\t.word\t" << LI->StackOffset;
151 AP.EOL("stack offset");
154 AP.EmitAlignment(AddressAlignLog);