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/Support/Compiler.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetMachine.h"
28 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter
: public GCMetadataPrinter
{
30 void beginAssembly(raw_ostream
&OS
, AsmPrinter
&AP
,
31 const TargetAsmInfo
&TAI
);
33 void finishAssembly(raw_ostream
&OS
, AsmPrinter
&AP
,
34 const TargetAsmInfo
&TAI
);
39 static GCMetadataPrinterRegistry::Add
<OcamlGCMetadataPrinter
>
40 Y("ocaml", "ocaml 3.10-compatible collector");
42 void llvm::linkOcamlGCPrinter() { }
44 static void EmitCamlGlobal(const Module
&M
, raw_ostream
&OS
, AsmPrinter
&AP
,
45 const TargetAsmInfo
&TAI
, const char *Id
) {
46 const std::string
&MId
= M
.getModuleIdentifier();
49 Mangled
+= TAI
.getGlobalPrefix();
51 size_t Letter
= Mangled
.size();
52 Mangled
.append(MId
.begin(), std::find(MId
.begin(), MId
.end(), '.'));
56 // Capitalize the first letter of the module name.
57 Mangled
[Letter
] = toupper(Mangled
[Letter
]);
59 if (const char *GlobalDirective
= TAI
.getGlobalDirective())
60 OS
<< GlobalDirective
<< Mangled
<< "\n";
61 OS
<< Mangled
<< ":\n";
64 void OcamlGCMetadataPrinter::beginAssembly(raw_ostream
&OS
, AsmPrinter
&AP
,
65 const TargetAsmInfo
&TAI
) {
66 AP
.SwitchToSection(TAI
.getTextSection());
67 EmitCamlGlobal(getModule(), OS
, AP
, TAI
, "code_begin");
69 AP
.SwitchToSection(TAI
.getDataSection());
70 EmitCamlGlobal(getModule(), OS
, AP
, TAI
, "data_begin");
73 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
75 /// extern "C" struct align(sizeof(intptr_t)) {
76 /// uint16_t NumDescriptors;
77 /// struct align(sizeof(intptr_t)) {
78 /// void *ReturnAddress;
79 /// uint16_t FrameSize;
80 /// uint16_t NumLiveOffsets;
81 /// uint16_t LiveOffsets[NumLiveOffsets];
82 /// } Descriptors[NumDescriptors];
83 /// } caml${module}__frametable;
85 /// Note that this precludes programs from stack frames larger than 64K
86 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
87 /// either condition is detected in a function which uses the GC.
89 void OcamlGCMetadataPrinter::finishAssembly(raw_ostream
&OS
, AsmPrinter
&AP
,
90 const TargetAsmInfo
&TAI
) {
91 const char *AddressDirective
;
93 if (AP
.TM
.getTargetData()->getPointerSize() == sizeof(int32_t)) {
94 AddressDirective
= TAI
.getData32bitsDirective();
97 AddressDirective
= TAI
.getData64bitsDirective();
101 AP
.SwitchToSection(TAI
.getTextSection());
102 EmitCamlGlobal(getModule(), OS
, AP
, TAI
, "code_end");
104 AP
.SwitchToSection(TAI
.getDataSection());
105 EmitCamlGlobal(getModule(), OS
, AP
, TAI
, "data_end");
107 OS
<< AddressDirective
<< 0; // FIXME: Why does ocaml emit this??
110 AP
.SwitchToSection(TAI
.getDataSection());
111 EmitCamlGlobal(getModule(), OS
, AP
, TAI
, "frametable");
113 for (iterator I
= begin(), IE
= end(); I
!= IE
; ++I
) {
114 GCFunctionInfo
&FI
= **I
;
116 uint64_t FrameSize
= FI
.getFrameSize();
117 if (FrameSize
>= 1<<16) {
118 cerr
<< "Function '" << FI
.getFunction().getNameStart()
119 << "' is too large for the ocaml GC! "
120 << "Frame size " << FrameSize
<< " >= 65536.\n";
121 cerr
<< "(" << uintptr_t(&FI
) << ")\n";
122 abort(); // Very rude!
125 OS
<< "\t" << TAI
.getCommentString() << " live roots for "
126 << FI
.getFunction().getNameStart() << "\n";
128 for (GCFunctionInfo::iterator J
= FI
.begin(), JE
= FI
.end(); J
!= JE
; ++J
) {
129 size_t LiveCount
= FI
.live_size(J
);
130 if (LiveCount
>= 1<<16) {
131 cerr
<< "Function '" << FI
.getFunction().getNameStart()
132 << "' is too large for the ocaml GC! "
133 << "Live root count " << LiveCount
<< " >= 65536.\n";
134 abort(); // Very rude!
137 OS
<< AddressDirective
138 << TAI
.getPrivateGlobalPrefix() << "label" << J
->Num
;
139 AP
.EOL("call return address");
141 AP
.EmitInt16(FrameSize
);
142 AP
.EOL("stack frame size");
144 AP
.EmitInt16(LiveCount
);
145 AP
.EOL("live root count");
147 for (GCFunctionInfo::live_iterator K
= FI
.live_begin(J
),
148 KE
= FI
.live_end(J
); K
!= KE
; ++K
) {
149 assert(K
->StackOffset
< 1<<16 &&
150 "GC root stack offset is outside of fixed stack frame and out "
151 "of range for ocaml GC!");
153 OS
<< "\t.word\t" << K
->StackOffset
;
154 AP
.EOL("stack offset");
157 AP
.EmitAlignment(AddressAlignLog
);