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