7f58f58c29bfc8a6e7c87046c3cc2aa8593e3cfb
[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
32   class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
33   public:
34     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   
41 }
42
43 static CollectorRegistry::Add<OcamlCollector>
44 X("ocaml", "ocaml 3.10-compatible collector");
45
46 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
47 Y("ocaml", "ocaml 3.10-compatible collector");
48
49 // -----------------------------------------------------------------------------
50
51 static 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
71 Collector *llvm::createOcamlCollector() {
72   return new OcamlCollector();
73 }
74
75 OcamlCollector::OcamlCollector() {
76   NeededSafePoints = 1 << GC::PostCall;
77   UsesMetadata = true;
78 }
79
80 void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
81                                            const TargetAsmInfo &TAI) {
82   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 /// 
105 void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
106                                             const TargetAsmInfo &TAI) {
107   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();
139       if (FrameSize >= 1<<16) {
140         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);
147       if (LiveCount >= 1<<16) {
148         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) {
167         assert(LI->StackOffset < 1<<16 &&
168                "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 }