Drop an unnecessary include.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / OcamlGCPrinter.cpp
1 //===-- OcamlGCPrinter.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 printing the assembly code for an Ocaml frametable.
11 //
12 //===----------------------------------------------------------------------===//
13                         
14 #include "llvm/CodeGen/GCs.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/GCMetadataPrinter.h"
17 #include "llvm/Module.h"
18 #include "llvm/Target/TargetAsmInfo.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
21
22 using namespace llvm;
23
24 namespace {
25
26   class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
27   public:
28     void beginAssembly(std::ostream &OS, AsmPrinter &AP,
29                        const TargetAsmInfo &TAI);
30     
31     void finishAssembly(std::ostream &OS, AsmPrinter &AP,
32                         const TargetAsmInfo &TAI);
33   };
34   
35 }
36
37 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
38 Y("ocaml", "ocaml 3.10-compatible collector");
39
40 void llvm::linkOcamlGCPrinter() { }
41
42 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
43                            const TargetAsmInfo &TAI, const char *Id) {
44   const std::string &MId = M.getModuleIdentifier();
45   
46   std::string Mangled;
47   Mangled += TAI.getGlobalPrefix();
48   Mangled += "caml";
49   size_t Letter = Mangled.size();
50   Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
51   Mangled += "__";
52   Mangled += Id;
53   
54   // Capitalize the first letter of the module name.
55   Mangled[Letter] = toupper(Mangled[Letter]);
56   
57   if (const char *GlobalDirective = TAI.getGlobalDirective())
58     OS << GlobalDirective << Mangled << "\n";
59   OS << Mangled << ":\n";
60 }
61
62 void OcamlGCMetadataPrinter::beginAssembly(std::ostream &OS, AsmPrinter &AP,
63                                            const TargetAsmInfo &TAI) {
64   AP.SwitchToTextSection(TAI.getTextSection());
65   EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
66   
67   AP.SwitchToDataSection(TAI.getDataSection());
68   EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
69 }
70
71 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
72 /// 
73 ///   extern "C" struct align(sizeof(intptr_t)) {
74 ///     uint16_t NumDescriptors;
75 ///     struct align(sizeof(intptr_t)) {
76 ///       void *ReturnAddress;
77 ///       uint16_t FrameSize;
78 ///       uint16_t NumLiveOffsets;
79 ///       uint16_t LiveOffsets[NumLiveOffsets];
80 ///     } Descriptors[NumDescriptors];
81 ///   } caml${module}__frametable;
82 /// 
83 /// Note that this precludes programs from stack frames larger than 64K
84 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
85 /// either condition is detected in a function which uses the GC.
86 /// 
87 void OcamlGCMetadataPrinter::finishAssembly(std::ostream &OS, AsmPrinter &AP,
88                                             const TargetAsmInfo &TAI) {
89   const char *AddressDirective;
90   int AddressAlignLog;
91   if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
92     AddressDirective = TAI.getData32bitsDirective();
93     AddressAlignLog = 2;
94   } else {
95     AddressDirective = TAI.getData64bitsDirective();
96     AddressAlignLog = 3;
97   }
98
99   AP.SwitchToTextSection(TAI.getTextSection());
100   EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
101   
102   AP.SwitchToDataSection(TAI.getDataSection());
103   EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
104   
105   OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
106   AP.EOL();
107   
108   AP.SwitchToDataSection(TAI.getDataSection());
109   EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
110   
111   for (iterator I = begin(), IE = end(); I != IE; ++I) {
112     GCFunctionInfo &FI = **I;
113     
114     uint64_t FrameSize = FI.getFrameSize();
115     if (FrameSize >= 1<<16) {
116       cerr << "Function '" << FI.getFunction().getNameStart()
117            << "' is too large for the ocaml GC! "
118            << "Frame size " << FrameSize << " >= 65536.\n";
119       cerr << "(" << uintptr_t(&FI) << ")\n";
120       abort(); // Very rude!
121     }
122     
123     OS << "\t" << TAI.getCommentString() << " live roots for "
124        << FI.getFunction().getNameStart() << "\n";
125     
126     for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
127       size_t LiveCount = FI.live_size(J);
128       if (LiveCount >= 1<<16) {
129         cerr << "Function '" << FI.getFunction().getNameStart()
130              << "' is too large for the ocaml GC! "
131              << "Live root count " << LiveCount << " >= 65536.\n";
132         abort(); // Very rude!
133       }
134       
135       OS << AddressDirective
136          << TAI.getPrivateGlobalPrefix() << "label" << J->Num;
137       AP.EOL("call return address");
138       
139       AP.EmitInt16(FrameSize);
140       AP.EOL("stack frame size");
141       
142       AP.EmitInt16(LiveCount);
143       AP.EOL("live root count");
144       
145       for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
146                                          KE = FI.live_end(J); K != KE; ++K) {
147         assert(K->StackOffset < 1<<16 &&
148                "GC root stack offset is outside of fixed stack frame and out "
149                "of range for ocaml GC!");
150         
151         OS << "\t.word\t" << K->StackOffset;
152         AP.EOL("stack offset");
153       }
154       
155       AP.EmitAlignment(AddressAlignLog);
156     }
157   }
158 }