blob: 7f58f58c29bfc8a6e7c87046c3cc2aa8593e3cfb [file] [log] [blame]
Gordon Henriksen572742e2008-01-07 02:31:11 +00001//===-- OcamlCollector.cpp - Ocaml frametable emitter ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements lowering for the llvm.gc* intrinsics compatible with
11// Objective Caml 3.10.0, which uses a liveness-accurate static stack map.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/Collectors.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000016#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/Collector.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000018#include "llvm/Module.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000019#include "llvm/Target/TargetAsmInfo.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Target/TargetMachine.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000022
23using namespace llvm;
24
25namespace {
26
27 class VISIBILITY_HIDDEN OcamlCollector : public Collector {
28 public:
29 OcamlCollector();
Gordon Henriksenc317a602008-08-17 12:08:44 +000030 };
31
32 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
33 public:
Gordon Henriksen572742e2008-01-07 02:31:11 +000034 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
35 const TargetAsmInfo &TAI);
36
37 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
38 const TargetAsmInfo &TAI);
39 };
40
Gordon Henriksen572742e2008-01-07 02:31:11 +000041}
42
Dan Gohman844731a2008-05-13 00:00:25 +000043static CollectorRegistry::Add<OcamlCollector>
44X("ocaml", "ocaml 3.10-compatible collector");
45
Gordon Henriksenc317a602008-08-17 12:08:44 +000046static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
47Y("ocaml", "ocaml 3.10-compatible collector");
48
Gordon Henriksen572742e2008-01-07 02:31:11 +000049// -----------------------------------------------------------------------------
50
51static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
52 const TargetAsmInfo &TAI, const char *Id) {
53 const std::string &MId = M.getModuleIdentifier();
54
55 std::string Mangled;
56 Mangled += TAI.getGlobalPrefix();
57 Mangled += "caml";
58 size_t Letter = Mangled.size();
59 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
60 Mangled += "__";
61 Mangled += Id;
62
63 // Capitalize the first letter of the module name.
64 Mangled[Letter] = toupper(Mangled[Letter]);
65
66 if (const char *GlobalDirective = TAI.getGlobalDirective())
67 OS << GlobalDirective << Mangled << "\n";
68 OS << Mangled << ":\n";
69}
70
71Collector *llvm::createOcamlCollector() {
72 return new OcamlCollector();
73}
74
75OcamlCollector::OcamlCollector() {
76 NeededSafePoints = 1 << GC::PostCall;
Gordon Henriksenc317a602008-08-17 12:08:44 +000077 UsesMetadata = true;
Gordon Henriksen572742e2008-01-07 02:31:11 +000078}
79
Gordon Henriksenc317a602008-08-17 12:08:44 +000080void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
81 const TargetAsmInfo &TAI) {
Gordon Henriksen572742e2008-01-07 02:31:11 +000082 AP.SwitchToTextSection(TAI.getTextSection());
83 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
84
85 AP.SwitchToDataSection(TAI.getDataSection());
86 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
87}
88
89/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
90///
91/// extern "C" struct align(sizeof(intptr_t)) {
92/// uint16_t NumDescriptors;
93/// struct align(sizeof(intptr_t)) {
94/// void *ReturnAddress;
95/// uint16_t FrameSize;
96/// uint16_t NumLiveOffsets;
97/// uint16_t LiveOffsets[NumLiveOffsets];
98/// } Descriptors[NumDescriptors];
99/// } caml${module}__frametable;
100///
101/// Note that this precludes programs from stack frames larger than 64K
102/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
103/// either condition is detected in a function which uses the collector.
104///
Gordon Henriksenc317a602008-08-17 12:08:44 +0000105void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
106 const TargetAsmInfo &TAI) {
Gordon Henriksen572742e2008-01-07 02:31:11 +0000107 const char *AddressDirective;
108 int AddressAlignLog;
109 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
110 AddressDirective = TAI.getData32bitsDirective();
111 AddressAlignLog = 2;
112 } else {
113 AddressDirective = TAI.getData64bitsDirective();
114 AddressAlignLog = 3;
115 }
116
117 AP.SwitchToTextSection(TAI.getTextSection());
118 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
119
120 AP.SwitchToDataSection(TAI.getDataSection());
121 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
122
123 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
124 AP.EOL();
125
126 AP.SwitchToDataSection(TAI.getDataSection());
127 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
128
129 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
130 CollectorMetadata &MD = **FI;
131
132 OS << "\t" << TAI.getCommentString() << " live roots for "
133 << MD.getFunction().getNameStart() << "\n";
134
135 for (CollectorMetadata::iterator PI = MD.begin(),
136 PE = MD.end(); PI != PE; ++PI) {
137
138 uint64_t FrameSize = MD.getFrameSize();
Gordon Henriksen08db7362008-08-09 03:48:46 +0000139 if (FrameSize >= 1<<16) {
Gordon Henriksen572742e2008-01-07 02:31:11 +0000140 cerr << "Function '" << MD.getFunction().getNameStart()
141 << "' is too large for the ocaml collector! "
142 << "Frame size " << FrameSize << " >= 65536.\n";
143 abort(); // Very rude!
144 }
145
146 size_t LiveCount = MD.live_size(PI);
Gordon Henriksen08db7362008-08-09 03:48:46 +0000147 if (LiveCount >= 1<<16) {
Gordon Henriksen572742e2008-01-07 02:31:11 +0000148 cerr << "Function '" << MD.getFunction().getNameStart()
149 << "' is too large for the ocaml collector! "
150 << "Live root count " << LiveCount << " >= 65536.\n";
151 abort(); // Very rude!
152 }
153
154 OS << AddressDirective
155 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
156 AP.EOL("call return address");
157
158 AP.EmitInt16(FrameSize);
159 AP.EOL("stack frame size");
160
161 AP.EmitInt16(LiveCount);
162 AP.EOL("live root count");
163
164 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
165 LE = MD.live_end(PI);
166 LI != LE; ++LI) {
Gordon Henriksen08db7362008-08-09 03:48:46 +0000167 assert(LI->StackOffset < 1<<16 &&
Gordon Henriksen572742e2008-01-07 02:31:11 +0000168 "GC root stack offset is outside of fixed stack frame and out "
169 "of range for Ocaml collector!");
170
171 OS << "\t.word\t" << LI->StackOffset;
172 AP.EOL("stack offset");
173 }
174
175 AP.EmitAlignment(AddressAlignLog);
176 }
177 }
178}