It's not legal to fold a load from a narrower stack slot into a wider instruction...
[llvm/avr.git] / lib / CodeGen / AsmPrinter / OcamlGCPrinter.cpp
blob06b92b7294b6273fb75bd98cab9481d911b3c589
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.
12 //===----------------------------------------------------------------------===//
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/MC/MCStreamer.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetLoweringObjectFile.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
28 namespace {
30 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
31 public:
32 void beginAssembly(raw_ostream &OS, AsmPrinter &AP,
33 const MCAsmInfo &MAI);
35 void finishAssembly(raw_ostream &OS, AsmPrinter &AP,
36 const MCAsmInfo &MAI);
41 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
42 Y("ocaml", "ocaml 3.10-compatible collector");
44 void llvm::linkOcamlGCPrinter() { }
46 static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP,
47 const MCAsmInfo &MAI, const char *Id) {
48 const std::string &MId = M.getModuleIdentifier();
50 std::string Mangled;
51 Mangled += MAI.getGlobalPrefix();
52 Mangled += "caml";
53 size_t Letter = Mangled.size();
54 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
55 Mangled += "__";
56 Mangled += Id;
58 // Capitalize the first letter of the module name.
59 Mangled[Letter] = toupper(Mangled[Letter]);
61 if (const char *GlobalDirective = MAI.getGlobalDirective())
62 OS << GlobalDirective << Mangled << "\n";
63 OS << Mangled << ":\n";
66 void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP,
67 const MCAsmInfo &MAI) {
68 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
69 EmitCamlGlobal(getModule(), OS, AP, MAI, "code_begin");
71 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
72 EmitCamlGlobal(getModule(), OS, AP, MAI, "data_begin");
75 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
76 ///
77 /// extern "C" struct align(sizeof(intptr_t)) {
78 /// uint16_t NumDescriptors;
79 /// struct align(sizeof(intptr_t)) {
80 /// void *ReturnAddress;
81 /// uint16_t FrameSize;
82 /// uint16_t NumLiveOffsets;
83 /// uint16_t LiveOffsets[NumLiveOffsets];
84 /// } Descriptors[NumDescriptors];
85 /// } caml${module}__frametable;
86 ///
87 /// Note that this precludes programs from stack frames larger than 64K
88 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
89 /// either condition is detected in a function which uses the GC.
90 ///
91 void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP,
92 const MCAsmInfo &MAI) {
93 const char *AddressDirective;
94 int AddressAlignLog;
95 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
96 AddressDirective = MAI.getData32bitsDirective();
97 AddressAlignLog = 2;
98 } else {
99 AddressDirective = MAI.getData64bitsDirective();
100 AddressAlignLog = 3;
103 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
104 EmitCamlGlobal(getModule(), OS, AP, MAI, "code_end");
106 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
107 EmitCamlGlobal(getModule(), OS, AP, MAI, "data_end");
109 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
110 AP.EOL();
112 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
113 EmitCamlGlobal(getModule(), OS, AP, MAI, "frametable");
115 for (iterator I = begin(), IE = end(); I != IE; ++I) {
116 GCFunctionInfo &FI = **I;
118 uint64_t FrameSize = FI.getFrameSize();
119 if (FrameSize >= 1<<16) {
120 std::string msg;
121 raw_string_ostream Msg(msg);
122 Msg << "Function '" << FI.getFunction().getName()
123 << "' is too large for the ocaml GC! "
124 << "Frame size " << FrameSize << " >= 65536.\n";
125 Msg << "(" << uintptr_t(&FI) << ")";
126 llvm_report_error(Msg.str()); // Very rude!
129 OS << "\t" << MAI.getCommentString() << " live roots for "
130 << FI.getFunction().getName() << "\n";
132 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
133 size_t LiveCount = FI.live_size(J);
134 if (LiveCount >= 1<<16) {
135 std::string msg;
136 raw_string_ostream Msg(msg);
137 Msg << "Function '" << FI.getFunction().getName()
138 << "' is too large for the ocaml GC! "
139 << "Live root count " << LiveCount << " >= 65536.";
140 llvm_report_error(Msg.str()); // Very rude!
143 OS << AddressDirective
144 << MAI.getPrivateGlobalPrefix() << "label" << J->Num;
145 AP.EOL("call return address");
147 AP.EmitInt16(FrameSize);
148 AP.EOL("stack frame size");
150 AP.EmitInt16(LiveCount);
151 AP.EOL("live root count");
153 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
154 KE = FI.live_end(J); K != KE; ++K) {
155 assert(K->StackOffset < 1<<16 &&
156 "GC root stack offset is outside of fixed stack frame and out "
157 "of range for ocaml GC!");
159 OS << "\t.word\t" << K->StackOffset;
160 AP.EOL("stack offset");
163 AP.EmitAlignment(AddressAlignLog);