Setting GlobalDirective in TargetAsmInfo by default rather than
[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/ADT/DenseMap.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/Collector.h"
19 #include "llvm/CodeGen/CollectorMetadata.h"
20 #include "llvm/Function.h"
21 #include "llvm/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include <ctype.h>
28
29 using namespace llvm;
30
31 namespace {
32
33   class VISIBILITY_HIDDEN OcamlCollector : public Collector {
34   public:
35     OcamlCollector();
36     
37     void beginAssembly(std::ostream &OS, AsmPrinter &AP,
38                        const TargetAsmInfo &TAI);
39     
40     void finishAssembly(std::ostream &OS, AsmPrinter &AP,
41                         const TargetAsmInfo &TAI);
42   };
43   
44   CollectorRegistry::Add<OcamlCollector>
45   X("ocaml", "ocaml 3.10-compatible collector");
46   
47 }
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 }
78
79 void OcamlCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
80                                    const TargetAsmInfo &TAI) {
81   AP.SwitchToTextSection(TAI.getTextSection());
82   EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
83   
84   AP.SwitchToDataSection(TAI.getDataSection());
85   EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
86 }
87
88 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
89 /// 
90 ///   extern "C" struct align(sizeof(intptr_t)) {
91 ///     uint16_t NumDescriptors;
92 ///     struct align(sizeof(intptr_t)) {
93 ///       void *ReturnAddress;
94 ///       uint16_t FrameSize;
95 ///       uint16_t NumLiveOffsets;
96 ///       uint16_t LiveOffsets[NumLiveOffsets];
97 ///     } Descriptors[NumDescriptors];
98 ///   } caml${module}__frametable;
99 /// 
100 /// Note that this precludes programs from stack frames larger than 64K
101 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
102 /// either condition is detected in a function which uses the collector.
103 /// 
104 void OcamlCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
105                                     const TargetAsmInfo &TAI) {
106   const char *AddressDirective;
107   int AddressAlignLog;
108   if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
109     AddressDirective = TAI.getData32bitsDirective();
110     AddressAlignLog = 2;
111   } else {
112     AddressDirective = TAI.getData64bitsDirective();
113     AddressAlignLog = 3;
114   }
115
116   AP.SwitchToTextSection(TAI.getTextSection());
117   EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
118   
119   AP.SwitchToDataSection(TAI.getDataSection());
120   EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
121   
122   OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
123   AP.EOL();
124   
125   AP.SwitchToDataSection(TAI.getDataSection());
126   EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
127   
128   for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
129     CollectorMetadata &MD = **FI;
130     
131     OS << "\t" << TAI.getCommentString() << " live roots for "
132        << MD.getFunction().getNameStart() << "\n";
133     
134     for (CollectorMetadata::iterator PI = MD.begin(),
135                                      PE = MD.end(); PI != PE; ++PI) {
136       
137       uint64_t FrameSize = MD.getFrameSize();
138       if (FrameSize >= 2<<16) {
139         cerr << "Function '" << MD.getFunction().getNameStart()
140              << "' is too large for the ocaml collector! "
141              << "Frame size " << FrameSize << " >= 65536.\n";
142         abort(); // Very rude!
143       }
144       
145       size_t LiveCount = MD.live_size(PI);
146       if (LiveCount >= 2<<16) {
147         cerr << "Function '" << MD.getFunction().getNameStart()
148              << "' is too large for the ocaml collector! "
149              << "Live root count " << LiveCount << " >= 65536.\n";
150         abort(); // Very rude!
151       }
152       
153       OS << AddressDirective
154          << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
155       AP.EOL("call return address");
156       
157       AP.EmitInt16(FrameSize);
158       AP.EOL("stack frame size");
159       
160       AP.EmitInt16(LiveCount);
161       AP.EOL("live root count");
162       
163       for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
164                                             LE = MD.live_end(PI);
165                                             LI != LE; ++LI) {
166         assert(LI->StackOffset < 2<<16 &&
167                "GC root stack offset is outside of fixed stack frame and out "
168                "of range for Ocaml collector!");
169         
170         OS << "\t.word\t" << LI->StackOffset;
171         AP.EOL("stack offset");
172       }
173       
174       AP.EmitAlignment(AddressAlignLog);
175     }
176   }
177 }