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