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/ADT/SmallString.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/GCMetadataPrinter.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/Mangler.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/MC/MCAsmInfo.h" 22 #include "llvm/MC/MCContext.h" 23 #include "llvm/MC/MCStreamer.h" 24 #include "llvm/MC/MCSymbol.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/FormattedStream.h" 27 #include "llvm/Target/TargetLoweringObjectFile.h" 28 #include "llvm/Target/TargetMachine.h" 29 #include "llvm/Target/TargetSubtargetInfo.h" 30 #include <cctype> 31 using namespace llvm; 32 33 namespace { 34 35 class OcamlGCMetadataPrinter : public GCMetadataPrinter { 36 public: 37 void beginAssembly(Module &M, GCModuleInfo &Info, 38 AsmPrinter &AP) override; 39 void finishAssembly(Module &M, GCModuleInfo &Info, 40 AsmPrinter &AP) override; 41 }; 42 43 } 44 45 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> 46 Y("ocaml", "ocaml 3.10-compatible collector"); 47 48 void llvm::linkOcamlGCPrinter() { } 49 50 static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) { 51 const std::string &MId = M.getModuleIdentifier(); 52 53 std::string SymName; 54 SymName += "caml"; 55 size_t Letter = SymName.size(); 56 SymName.append(MId.begin(), std::find(MId.begin(), MId.end(), '.')); 57 SymName += "__"; 58 SymName += Id; 59 60 // Capitalize the first letter of the module name. 61 SymName[Letter] = toupper(SymName[Letter]); 62 63 SmallString<128> TmpStr; 64 AP.Mang->getNameWithPrefix(TmpStr, SymName); 65 66 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr); 67 68 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global); 69 AP.OutStreamer.EmitLabel(Sym); 70 } 71 72 void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info, 73 AsmPrinter &AP) { 74 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 75 EmitCamlGlobal(M, AP, "code_begin"); 76 77 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 78 EmitCamlGlobal(M, AP, "data_begin"); 79 } 80 81 /// emitAssembly - Print the frametable. The ocaml frametable format is thus: 82 /// 83 /// extern "C" struct align(sizeof(intptr_t)) { 84 /// uint16_t NumDescriptors; 85 /// struct align(sizeof(intptr_t)) { 86 /// void *ReturnAddress; 87 /// uint16_t FrameSize; 88 /// uint16_t NumLiveOffsets; 89 /// uint16_t LiveOffsets[NumLiveOffsets]; 90 /// } Descriptors[NumDescriptors]; 91 /// } caml${module}__frametable; 92 /// 93 /// Note that this precludes programs from stack frames larger than 64K 94 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if 95 /// either condition is detected in a function which uses the GC. 96 /// 97 void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, 98 AsmPrinter &AP) { 99 unsigned IntPtrSize = 100 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize(); 101 102 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 103 EmitCamlGlobal(M, AP, "code_end"); 104 105 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 106 EmitCamlGlobal(M, AP, "data_end"); 107 108 // FIXME: Why does ocaml emit this?? 109 AP.OutStreamer.EmitIntValue(0, IntPtrSize); 110 111 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 112 EmitCamlGlobal(M, AP, "frametable"); 113 114 int NumDescriptors = 0; 115 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), 116 IE = Info.funcinfo_end(); I != IE; ++I) { 117 GCFunctionInfo &FI = **I; 118 if (FI.getStrategy().getName() != getStrategy().getName()) 119 // this function is managed by some other GC 120 continue; 121 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { 122 NumDescriptors++; 123 } 124 } 125 126 if (NumDescriptors >= 1<<16) { 127 // Very rude! 128 report_fatal_error(" Too much descriptor for ocaml GC"); 129 } 130 AP.EmitInt16(NumDescriptors); 131 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); 132 133 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), 134 IE = Info.funcinfo_end(); I != IE; ++I) { 135 GCFunctionInfo &FI = **I; 136 if (FI.getStrategy().getName() != getStrategy().getName()) 137 // this function is managed by some other GC 138 continue; 139 140 uint64_t FrameSize = FI.getFrameSize(); 141 if (FrameSize >= 1<<16) { 142 // Very rude! 143 report_fatal_error("Function '" + FI.getFunction().getName() + 144 "' is too large for the ocaml GC! " 145 "Frame size " + Twine(FrameSize) + ">= 65536.\n" 146 "(" + Twine(uintptr_t(&FI)) + ")"); 147 } 148 149 AP.OutStreamer.AddComment("live roots for " + 150 Twine(FI.getFunction().getName())); 151 AP.OutStreamer.AddBlankLine(); 152 153 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { 154 size_t LiveCount = FI.live_size(J); 155 if (LiveCount >= 1<<16) { 156 // Very rude! 157 report_fatal_error("Function '" + FI.getFunction().getName() + 158 "' is too large for the ocaml GC! " 159 "Live root count "+Twine(LiveCount)+" >= 65536."); 160 } 161 162 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize); 163 AP.EmitInt16(FrameSize); 164 AP.EmitInt16(LiveCount); 165 166 for (GCFunctionInfo::live_iterator K = FI.live_begin(J), 167 KE = FI.live_end(J); K != KE; ++K) { 168 if (K->StackOffset >= 1<<16) { 169 // Very rude! 170 report_fatal_error( 171 "GC root stack offset is outside of fixed stack frame and out " 172 "of range for ocaml GC!"); 173 } 174 AP.EmitInt16(K->StackOffset); 175 } 176 177 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); 178 } 179 } 180 } 181