OcamlGCPrinter.cpp revision af76e592c7f9deff0e55c13dbb4a34f07f1c7f64
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/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" 26using namespace llvm; 27 28namespace { 29 30 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter { 31 public: 32 void beginAssembly(raw_ostream &OS, AsmPrinter &AP, 33 const MCAsmInfo &TAI); 34 35 void finishAssembly(raw_ostream &OS, AsmPrinter &AP, 36 const MCAsmInfo &TAI); 37 }; 38 39} 40 41static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> 42Y("ocaml", "ocaml 3.10-compatible collector"); 43 44void llvm::linkOcamlGCPrinter() { } 45 46static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP, 47 const MCAsmInfo &TAI, const char *Id) { 48 const std::string &MId = M.getModuleIdentifier(); 49 50 std::string Mangled; 51 Mangled += TAI.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; 57 58 // Capitalize the first letter of the module name. 59 Mangled[Letter] = toupper(Mangled[Letter]); 60 61 if (const char *GlobalDirective = TAI.getGlobalDirective()) 62 OS << GlobalDirective << Mangled << "\n"; 63 OS << Mangled << ":\n"; 64} 65 66void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP, 67 const MCAsmInfo &TAI) { 68 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 69 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin"); 70 71 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 72 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin"); 73} 74 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/// 91void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP, 92 const MCAsmInfo &TAI) { 93 const char *AddressDirective; 94 int AddressAlignLog; 95 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) { 96 AddressDirective = TAI.getData32bitsDirective(); 97 AddressAlignLog = 2; 98 } else { 99 AddressDirective = TAI.getData64bitsDirective(); 100 AddressAlignLog = 3; 101 } 102 103 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 104 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end"); 105 106 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 107 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end"); 108 109 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this?? 110 AP.EOL(); 111 112 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 113 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable"); 114 115 for (iterator I = begin(), IE = end(); I != IE; ++I) { 116 GCFunctionInfo &FI = **I; 117 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! 127 } 128 129 OS << "\t" << TAI.getCommentString() << " live roots for " 130 << FI.getFunction().getName() << "\n"; 131 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! 141 } 142 143 OS << AddressDirective 144 << TAI.getPrivateGlobalPrefix() << "label" << J->Num; 145 AP.EOL("call return address"); 146 147 AP.EmitInt16(FrameSize); 148 AP.EOL("stack frame size"); 149 150 AP.EmitInt16(LiveCount); 151 AP.EOL("live root count"); 152 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!"); 158 159 OS << "\t.word\t" << K->StackOffset; 160 AP.EOL("stack offset"); 161 } 162 163 AP.EmitAlignment(AddressAlignLog); 164 } 165 } 166} 167