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/Target/TargetAsmInfo.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
26 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
28 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
29 const TargetAsmInfo &TAI);
31 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
32 const TargetAsmInfo &TAI);
37 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
38 Y("ocaml", "ocaml 3.10-compatible collector");
40 void llvm::linkOcamlGCPrinter() { }
42 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
43 const TargetAsmInfo &TAI, const char *Id) {
44 const std::string &MId = M.getModuleIdentifier();
47 Mangled += TAI.getGlobalPrefix();
49 size_t Letter = Mangled.size();
50 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
54 // Capitalize the first letter of the module name.
55 Mangled[Letter] = toupper(Mangled[Letter]);
57 if (const char *GlobalDirective = TAI.getGlobalDirective())
58 OS << GlobalDirective << Mangled << "\n";
59 OS << Mangled << ":\n";
62 void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
63 const TargetAsmInfo &TAI) {
64 AP.SwitchToTextSection(TAI.getTextSection());
65 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
67 AP.SwitchToDataSection(TAI.getDataSection());
68 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
71 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
73 /// extern "C" struct align(sizeof(intptr_t)) {
74 /// uint16_t NumDescriptors;
75 /// struct align(sizeof(intptr_t)) {
76 /// void *ReturnAddress;
77 /// uint16_t FrameSize;
78 /// uint16_t NumLiveOffsets;
79 /// uint16_t LiveOffsets[NumLiveOffsets];
80 /// } Descriptors[NumDescriptors];
81 /// } caml${module}__frametable;
83 /// Note that this precludes programs from stack frames larger than 64K
84 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
85 /// either condition is detected in a function which uses the GC.
87 void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
88 const TargetAsmInfo &TAI) {
89 const char *AddressDirective;
91 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
92 AddressDirective = TAI.getData32bitsDirective();
95 AddressDirective = TAI.getData64bitsDirective();
99 AP.SwitchToTextSection(TAI.getTextSection());
100 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
102 AP.SwitchToDataSection(TAI.getDataSection());
103 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
105 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
108 AP.SwitchToDataSection(TAI.getDataSection());
109 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
111 for (iterator I = begin(), IE = end(); I != IE; ++I) {
112 GCFunctionInfo &FI = **I;
114 uint64_t FrameSize = FI.getFrameSize();
115 if (FrameSize >= 1<<16) {
116 cerr << "Function '" << FI.getFunction().getNameStart()
117 << "' is too large for the ocaml GC! "
118 << "Frame size " << FrameSize << " >= 65536.\n";
119 cerr << "(" << uintptr_t(&FI) << ")\n";
120 abort(); // Very rude!
123 OS << "\t" << TAI.getCommentString() << " live roots for "
124 << FI.getFunction().getNameStart() << "\n";
126 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
127 size_t LiveCount = FI.live_size(J);
128 if (LiveCount >= 1<<16) {
129 cerr << "Function '" << FI.getFunction().getNameStart()
130 << "' is too large for the ocaml GC! "
131 << "Live root count " << LiveCount << " >= 65536.\n";
132 abort(); // Very rude!
135 OS << AddressDirective
136 << TAI.getPrivateGlobalPrefix() << "label" << J->Num;
137 AP.EOL("call return address");
139 AP.EmitInt16(FrameSize);
140 AP.EOL("stack frame size");
142 AP.EmitInt16(LiveCount);
143 AP.EOL("live root count");
145 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
146 KE = FI.live_end(J); K != KE; ++K) {
147 assert(K->StackOffset < 1<<16 &&
148 "GC root stack offset is outside of fixed stack frame and out "
149 "of range for ocaml GC!");
151 OS << "\t.word\t" << K->StackOffset;
152 AP.EOL("stack offset");
155 AP.EmitAlignment(AddressAlignLog);