1 //===- OcamlGCPrinter.cpp - Ocaml frametable emitter ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements printing the assembly code for an Ocaml frametable.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/BuiltinGCs.h"
18 #include "llvm/CodeGen/GCMetadata.h"
19 #include "llvm/CodeGen/GCMetadataPrinter.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDirectives.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
38 class OcamlGCMetadataPrinter
: public GCMetadataPrinter
{
40 void beginAssembly(Module
&M
, GCModuleInfo
&Info
, AsmPrinter
&AP
) override
;
41 void finishAssembly(Module
&M
, GCModuleInfo
&Info
, AsmPrinter
&AP
) override
;
44 } // end anonymous namespace
46 static GCMetadataPrinterRegistry::Add
<OcamlGCMetadataPrinter
>
47 Y("ocaml", "ocaml 3.10-compatible collector");
49 void llvm::linkOcamlGCPrinter() {}
51 static void EmitCamlGlobal(const Module
&M
, AsmPrinter
&AP
, const char *Id
) {
52 const std::string
&MId
= M
.getModuleIdentifier();
56 size_t Letter
= SymName
.size();
57 SymName
.append(MId
.begin(), llvm::find(MId
, '.'));
61 // Capitalize the first letter of the module name.
62 SymName
[Letter
] = toupper(SymName
[Letter
]);
64 SmallString
<128> TmpStr
;
65 Mangler::getNameWithPrefix(TmpStr
, SymName
, M
.getDataLayout());
67 MCSymbol
*Sym
= AP
.OutContext
.getOrCreateSymbol(TmpStr
);
69 AP
.OutStreamer
->EmitSymbolAttribute(Sym
, MCSA_Global
);
70 AP
.OutStreamer
->EmitLabel(Sym
);
73 void OcamlGCMetadataPrinter::beginAssembly(Module
&M
, GCModuleInfo
&Info
,
75 AP
.OutStreamer
->SwitchSection(AP
.getObjFileLowering().getTextSection());
76 EmitCamlGlobal(M
, AP
, "code_begin");
78 AP
.OutStreamer
->SwitchSection(AP
.getObjFileLowering().getDataSection());
79 EmitCamlGlobal(M
, AP
, "data_begin");
82 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
84 /// extern "C" struct align(sizeof(intptr_t)) {
85 /// uint16_t NumDescriptors;
86 /// struct align(sizeof(intptr_t)) {
87 /// void *ReturnAddress;
88 /// uint16_t FrameSize;
89 /// uint16_t NumLiveOffsets;
90 /// uint16_t LiveOffsets[NumLiveOffsets];
91 /// } Descriptors[NumDescriptors];
92 /// } caml${module}__frametable;
94 /// Note that this precludes programs from stack frames larger than 64K
95 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
96 /// either condition is detected in a function which uses the GC.
98 void OcamlGCMetadataPrinter::finishAssembly(Module
&M
, GCModuleInfo
&Info
,
100 unsigned IntPtrSize
= M
.getDataLayout().getPointerSize();
102 AP
.OutStreamer
->SwitchSection(AP
.getObjFileLowering().getTextSection());
103 EmitCamlGlobal(M
, AP
, "code_end");
105 AP
.OutStreamer
->SwitchSection(AP
.getObjFileLowering().getDataSection());
106 EmitCamlGlobal(M
, AP
, "data_end");
108 // FIXME: Why does ocaml emit this??
109 AP
.OutStreamer
->EmitIntValue(0, IntPtrSize
);
111 AP
.OutStreamer
->SwitchSection(AP
.getObjFileLowering().getDataSection());
112 EmitCamlGlobal(M
, AP
, "frametable");
114 int NumDescriptors
= 0;
115 for (GCModuleInfo::FuncInfoVec::iterator I
= Info
.funcinfo_begin(),
116 IE
= Info
.funcinfo_end();
118 GCFunctionInfo
&FI
= **I
;
119 if (FI
.getStrategy().getName() != getStrategy().getName())
120 // this function is managed by some other GC
122 for (GCFunctionInfo::iterator J
= FI
.begin(), JE
= FI
.end(); J
!= JE
; ++J
) {
127 if (NumDescriptors
>= 1 << 16) {
129 report_fatal_error(" Too much descriptor for ocaml GC");
131 AP
.emitInt16(NumDescriptors
);
132 AP
.EmitAlignment(IntPtrSize
== 4 ? llvm::Align(4) : llvm::Align(8));
134 for (GCModuleInfo::FuncInfoVec::iterator I
= Info
.funcinfo_begin(),
135 IE
= Info
.funcinfo_end();
137 GCFunctionInfo
&FI
= **I
;
138 if (FI
.getStrategy().getName() != getStrategy().getName())
139 // this function is managed by some other GC
142 uint64_t FrameSize
= FI
.getFrameSize();
143 if (FrameSize
>= 1 << 16) {
145 report_fatal_error("Function '" + FI
.getFunction().getName() +
146 "' is too large for the ocaml GC! "
148 Twine(FrameSize
) + ">= 65536.\n"
150 Twine(uintptr_t(&FI
)) + ")");
153 AP
.OutStreamer
->AddComment("live roots for " +
154 Twine(FI
.getFunction().getName()));
155 AP
.OutStreamer
->AddBlankLine();
157 for (GCFunctionInfo::iterator J
= FI
.begin(), JE
= FI
.end(); J
!= JE
; ++J
) {
158 size_t LiveCount
= FI
.live_size(J
);
159 if (LiveCount
>= 1 << 16) {
161 report_fatal_error("Function '" + FI
.getFunction().getName() +
162 "' is too large for the ocaml GC! "
164 Twine(LiveCount
) + " >= 65536.");
167 AP
.OutStreamer
->EmitSymbolValue(J
->Label
, IntPtrSize
);
168 AP
.emitInt16(FrameSize
);
169 AP
.emitInt16(LiveCount
);
171 for (GCFunctionInfo::live_iterator K
= FI
.live_begin(J
),
174 if (K
->StackOffset
>= 1 << 16) {
177 "GC root stack offset is outside of fixed stack frame and out "
178 "of range for ocaml GC!");
180 AP
.emitInt16(K
->StackOffset
);
183 AP
.EmitAlignment(IntPtrSize
== 4 ? llvm::Align(4) : llvm::Align(8));