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/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Target/Mangler.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetLoweringObjectFile.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/FormattedStream.h"
34 class OcamlGCMetadataPrinter
: public GCMetadataPrinter
{
36 void beginAssembly(AsmPrinter
&AP
);
37 void finishAssembly(AsmPrinter
&AP
);
42 static GCMetadataPrinterRegistry::Add
<OcamlGCMetadataPrinter
>
43 Y("ocaml", "ocaml 3.10-compatible collector");
45 void llvm::linkOcamlGCPrinter() { }
47 static void EmitCamlGlobal(const Module
&M
, AsmPrinter
&AP
, const char *Id
) {
48 const std::string
&MId
= M
.getModuleIdentifier();
52 size_t Letter
= SymName
.size();
53 SymName
.append(MId
.begin(), std::find(MId
.begin(), MId
.end(), '.'));
57 // Capitalize the first letter of the module name.
58 SymName
[Letter
] = toupper(SymName
[Letter
]);
60 SmallString
<128> TmpStr
;
61 AP
.Mang
->getNameWithPrefix(TmpStr
, SymName
);
63 MCSymbol
*Sym
= AP
.OutContext
.GetOrCreateSymbol(TmpStr
);
65 AP
.OutStreamer
.EmitSymbolAttribute(Sym
, MCSA_Global
);
66 AP
.OutStreamer
.EmitLabel(Sym
);
69 void OcamlGCMetadataPrinter::beginAssembly(AsmPrinter
&AP
) {
70 AP
.OutStreamer
.SwitchSection(AP
.getObjFileLowering().getTextSection());
71 EmitCamlGlobal(getModule(), AP
, "code_begin");
73 AP
.OutStreamer
.SwitchSection(AP
.getObjFileLowering().getDataSection());
74 EmitCamlGlobal(getModule(), AP
, "data_begin");
77 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
79 /// extern "C" struct align(sizeof(intptr_t)) {
80 /// uint16_t NumDescriptors;
81 /// struct align(sizeof(intptr_t)) {
82 /// void *ReturnAddress;
83 /// uint16_t FrameSize;
84 /// uint16_t NumLiveOffsets;
85 /// uint16_t LiveOffsets[NumLiveOffsets];
86 /// } Descriptors[NumDescriptors];
87 /// } caml${module}__frametable;
89 /// Note that this precludes programs from stack frames larger than 64K
90 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
91 /// either condition is detected in a function which uses the GC.
93 void OcamlGCMetadataPrinter::finishAssembly(AsmPrinter
&AP
) {
94 unsigned IntPtrSize
= AP
.TM
.getTargetData()->getPointerSize();
96 AP
.OutStreamer
.SwitchSection(AP
.getObjFileLowering().getTextSection());
97 EmitCamlGlobal(getModule(), AP
, "code_end");
99 AP
.OutStreamer
.SwitchSection(AP
.getObjFileLowering().getDataSection());
100 EmitCamlGlobal(getModule(), AP
, "data_end");
102 // FIXME: Why does ocaml emit this??
103 AP
.OutStreamer
.EmitIntValue(0, IntPtrSize
, 0);
105 AP
.OutStreamer
.SwitchSection(AP
.getObjFileLowering().getDataSection());
106 EmitCamlGlobal(getModule(), AP
, "frametable");
108 int NumDescriptors
= 0;
109 for (iterator I
= begin(), IE
= end(); I
!= IE
; ++I
) {
110 GCFunctionInfo
&FI
= **I
;
111 for (GCFunctionInfo::iterator J
= FI
.begin(), JE
= FI
.end(); J
!= JE
; ++J
) {
116 if (NumDescriptors
>= 1<<16) {
118 report_fatal_error(" Too much descriptor for ocaml GC");
120 AP
.EmitInt16(NumDescriptors
);
121 AP
.EmitAlignment(IntPtrSize
== 4 ? 2 : 3);
123 for (iterator I
= begin(), IE
= end(); I
!= IE
; ++I
) {
124 GCFunctionInfo
&FI
= **I
;
126 uint64_t FrameSize
= FI
.getFrameSize();
127 if (FrameSize
>= 1<<16) {
129 report_fatal_error("Function '" + FI
.getFunction().getName() +
130 "' is too large for the ocaml GC! "
131 "Frame size " + Twine(FrameSize
) + ">= 65536.\n"
132 "(" + Twine(uintptr_t(&FI
)) + ")");
135 AP
.OutStreamer
.AddComment("live roots for " +
136 Twine(FI
.getFunction().getName()));
137 AP
.OutStreamer
.AddBlankLine();
139 for (GCFunctionInfo::iterator J
= FI
.begin(), JE
= FI
.end(); J
!= JE
; ++J
) {
140 size_t LiveCount
= FI
.live_size(J
);
141 if (LiveCount
>= 1<<16) {
143 report_fatal_error("Function '" + FI
.getFunction().getName() +
144 "' is too large for the ocaml GC! "
145 "Live root count "+Twine(LiveCount
)+" >= 65536.");
148 AP
.OutStreamer
.EmitSymbolValue(J
->Label
, IntPtrSize
, 0);
149 AP
.EmitInt16(FrameSize
);
150 AP
.EmitInt16(LiveCount
);
152 for (GCFunctionInfo::live_iterator K
= FI
.live_begin(J
),
153 KE
= FI
.live_end(J
); K
!= KE
; ++K
) {
154 if (K
->StackOffset
>= 1<<16) {
157 "GC root stack offset is outside of fixed stack frame and out "
158 "of range for ocaml GC!");
160 AP
.EmitInt16(K
->StackOffset
);
163 AP
.EmitAlignment(IntPtrSize
== 4 ? 2 : 3);