Pruning includes.
[oota-llvm.git] / lib / CodeGen / OcamlCollector.cpp
1 //===-- 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"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/Collector.h"
18 #include "llvm/Module.h"
19 #include "llvm/Target/TargetAsmInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
22
23 using namespace llvm;
24
25 namespace {
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   
38   CollectorRegistry::Add<OcamlCollector>
39   X("ocaml", "ocaml 3.10-compatible collector");
40   
41 }
42
43 // -----------------------------------------------------------------------------
44
45 static 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
65 Collector *llvm::createOcamlCollector() {
66   return new OcamlCollector();
67 }
68
69 OcamlCollector::OcamlCollector() {
70   NeededSafePoints = 1 << GC::PostCall;
71 }
72
73 void 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 /// 
98 void 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();
132       if (FrameSize >= 2<<16) {
133         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);
140       if (LiveCount >= 2<<16) {
141         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) {
160         assert(LI->StackOffset < 2<<16 &&
161                "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 }