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/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; 26 27 namespace { 28 29 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter { 30 public: 31 void beginAssembly(raw_ostream &OS, AsmPrinter &AP, 32 const TargetAsmInfo &TAI); 33 34 void finishAssembly(raw_ostream &OS, AsmPrinter &AP, 35 const TargetAsmInfo &TAI); 36 }; 37 38 } 39 40 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> 41 Y("ocaml", "ocaml 3.10-compatible collector"); 42 43 void llvm::linkOcamlGCPrinter() { } 44 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(); 48 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; 56 57 // Capitalize the first letter of the module name. 58 Mangled[Letter] = toupper(Mangled[Letter]); 59 60 if (const char *GlobalDirective = TAI.getGlobalDirective()) 61 OS << GlobalDirective << Mangled << "\n"; 62 OS << Mangled << ":\n"; 63 } 64 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"); 69 70 AP.SwitchToSection(AP.getObjFileLowering().getDataSection()); 71 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin"); 72 } 73 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; 100 } 101 102 AP.SwitchToSection(AP.getObjFileLowering().getTextSection()); 103 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end"); 104 105 AP.SwitchToSection(AP.getObjFileLowering().getDataSection()); 106 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end"); 107 108 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this?? 109 AP.EOL(); 110 111 AP.SwitchToSection(AP.getObjFileLowering().getDataSection()); 112 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable"); 113 114 for (iterator I = begin(), IE = end(); I != IE; ++I) { 115 GCFunctionInfo &FI = **I; 116 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! 126 } 127 128 OS << "\t" << TAI.getCommentString() << " live roots for " 129 << FI.getFunction().getName() << "\n"; 130 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! 140 } 141 142 OS << AddressDirective 143 << TAI.getPrivateGlobalPrefix() << "label" << J->Num; 144 AP.EOL("call return address"); 145 146 AP.EmitInt16(FrameSize); 147 AP.EOL("stack frame size"); 148 149 AP.EmitInt16(LiveCount); 150 AP.EOL("live root count"); 151 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!"); 157 158 OS << "\t.word\t" << K->StackOffset; 159 AP.EOL("stack offset"); 160 } 161 162 AP.EmitAlignment(AddressAlignLog); 163 } 164 } 165 } 166