blob: 9823d24986feb06a414e460ceca39229bdf23cf3 [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();
30
31 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
32 const TargetAsmInfo &TAI);
33
34 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
35 const TargetAsmInfo &TAI);
36 };
37
Gordon Henriksen572742e2008-01-07 02:31:11 +000038}
39
Dan Gohman844731a2008-05-13 00:00:25 +000040static CollectorRegistry::Add<OcamlCollector>
41X("ocaml", "ocaml 3.10-compatible collector");
42
Gordon Henriksen572742e2008-01-07 02:31:11 +000043// -----------------------------------------------------------------------------
44
45static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
46 const TargetAsmInfo &TAI, const char *Id) {
47 const std::string &MId = M.getModuleIdentifier();
48
49 std::string Mangled;
50 Mangled += TAI.getGlobalPrefix();
51 Mangled += "caml";
52 size_t Letter = Mangled.size();
53 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
54 Mangled += "__";
55 Mangled += Id;
56
57 // Capitalize the first letter of the module name.
58 Mangled[Letter] = toupper(Mangled[Letter]);
59
60 if (const char *GlobalDirective = TAI.getGlobalDirective())
61 OS << GlobalDirective << Mangled << "\n";
62 OS << Mangled << ":\n";
63}
64
65Collector *llvm::createOcamlCollector() {
66 return new OcamlCollector();
67}
68
69OcamlCollector::OcamlCollector() {
70 NeededSafePoints = 1 << GC::PostCall;
71}
72
73void OcamlCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
74 const TargetAsmInfo &TAI) {
75 AP.SwitchToTextSection(TAI.getTextSection());
76 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
77
78 AP.SwitchToDataSection(TAI.getDataSection());
79 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
80}
81
82/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
83///
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;
93///
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 collector.
97///
98void OcamlCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
99 const TargetAsmInfo &TAI) {
100 const char *AddressDirective;
101 int AddressAlignLog;
102 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
103 AddressDirective = TAI.getData32bitsDirective();
104 AddressAlignLog = 2;
105 } else {
106 AddressDirective = TAI.getData64bitsDirective();
107 AddressAlignLog = 3;
108 }
109
110 AP.SwitchToTextSection(TAI.getTextSection());
111 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
112
113 AP.SwitchToDataSection(TAI.getDataSection());
114 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
115
116 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
117 AP.EOL();
118
119 AP.SwitchToDataSection(TAI.getDataSection());
120 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
121
122 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
123 CollectorMetadata &MD = **FI;
124
125 OS << "\t" << TAI.getCommentString() << " live roots for "
126 << MD.getFunction().getNameStart() << "\n";
127
128 for (CollectorMetadata::iterator PI = MD.begin(),
129 PE = MD.end(); PI != PE; ++PI) {
130
131 uint64_t FrameSize = MD.getFrameSize();
Gordon Henriksen08db7362008-08-09 03:48:46 +0000132 if (FrameSize >= 1<<16) {
Gordon Henriksen572742e2008-01-07 02:31:11 +0000133 cerr << "Function '" << MD.getFunction().getNameStart()
134 << "' is too large for the ocaml collector! "
135 << "Frame size " << FrameSize << " >= 65536.\n";
136 abort(); // Very rude!
137 }
138
139 size_t LiveCount = MD.live_size(PI);
Gordon Henriksen08db7362008-08-09 03:48:46 +0000140 if (LiveCount >= 1<<16) {
Gordon Henriksen572742e2008-01-07 02:31:11 +0000141 cerr << "Function '" << MD.getFunction().getNameStart()
142 << "' is too large for the ocaml collector! "
143 << "Live root count " << LiveCount << " >= 65536.\n";
144 abort(); // Very rude!
145 }
146
147 OS << AddressDirective
148 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
149 AP.EOL("call return address");
150
151 AP.EmitInt16(FrameSize);
152 AP.EOL("stack frame size");
153
154 AP.EmitInt16(LiveCount);
155 AP.EOL("live root count");
156
157 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
158 LE = MD.live_end(PI);
159 LI != LE; ++LI) {
Gordon Henriksen08db7362008-08-09 03:48:46 +0000160 assert(LI->StackOffset < 1<<16 &&
Gordon Henriksen572742e2008-01-07 02:31:11 +0000161 "GC root stack offset is outside of fixed stack frame and out "
162 "of range for Ocaml collector!");
163
164 OS << "\t.word\t" << LI->StackOffset;
165 AP.EOL("stack offset");
166 }
167
168 AP.EmitAlignment(AddressAlignLog);
169 }
170 }
171}