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