LLVM API Documentation
00001 //===-- OcamlGCPrinter.cpp - Ocaml frametable emitter ---------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements printing the assembly code for an Ocaml frametable. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/GCs.h" 00015 #include "llvm/CodeGen/AsmPrinter.h" 00016 #include "llvm/CodeGen/GCMetadataPrinter.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/MC/MCStreamer.h" 00019 #include "llvm/Target/TargetAsmInfo.h" 00020 #include "llvm/Target/TargetData.h" 00021 #include "llvm/Target/TargetLoweringObjectFile.h" 00022 #include "llvm/Target/TargetMachine.h" 00023 #include "llvm/Support/Compiler.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 using namespace llvm; 00027 00028 namespace { 00029 00030 class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter { 00031 public: 00032 void beginAssembly(raw_ostream &OS, AsmPrinter &AP, 00033 const TargetAsmInfo &TAI); 00034 00035 void finishAssembly(raw_ostream &OS, AsmPrinter &AP, 00036 const TargetAsmInfo &TAI); 00037 }; 00038 00039 } 00040 00041 static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> 00042 Y("ocaml", "ocaml 3.10-compatible collector"); 00043 00044 void llvm::linkOcamlGCPrinter() { } 00045 00046 static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP, 00047 const TargetAsmInfo &TAI, const char *Id) { 00048 const std::string &MId = M.getModuleIdentifier(); 00049 00050 std::string Mangled; 00051 Mangled += TAI.getGlobalPrefix(); 00052 Mangled += "caml"; 00053 size_t Letter = Mangled.size(); 00054 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.')); 00055 Mangled += "__"; 00056 Mangled += Id; 00057 00058 // Capitalize the first letter of the module name. 00059 Mangled[Letter] = toupper(Mangled[Letter]); 00060 00061 if (const char *GlobalDirective = TAI.getGlobalDirective()) 00062 OS << GlobalDirective << Mangled << "\n"; 00063 OS << Mangled << ":\n"; 00064 } 00065 00066 void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP, 00067 const TargetAsmInfo &TAI) { 00068 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 00069 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin"); 00070 00071 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 00072 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin"); 00073 } 00074 00075 /// emitAssembly - Print the frametable. The ocaml frametable format is thus: 00076 /// 00077 /// extern "C" struct align(sizeof(intptr_t)) { 00078 /// uint16_t NumDescriptors; 00079 /// struct align(sizeof(intptr_t)) { 00080 /// void *ReturnAddress; 00081 /// uint16_t FrameSize; 00082 /// uint16_t NumLiveOffsets; 00083 /// uint16_t LiveOffsets[NumLiveOffsets]; 00084 /// } Descriptors[NumDescriptors]; 00085 /// } caml${module}__frametable; 00086 /// 00087 /// Note that this precludes programs from stack frames larger than 64K 00088 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if 00089 /// either condition is detected in a function which uses the GC. 00090 /// 00091 void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP, 00092 const TargetAsmInfo &TAI) { 00093 const char *AddressDirective; 00094 int AddressAlignLog; 00095 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) { 00096 AddressDirective = TAI.getData32bitsDirective(); 00097 AddressAlignLog = 2; 00098 } else { 00099 AddressDirective = TAI.getData64bitsDirective(); 00100 AddressAlignLog = 3; 00101 } 00102 00103 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection()); 00104 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end"); 00105 00106 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 00107 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end"); 00108 00109 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this?? 00110 AP.EOL(); 00111 00112 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection()); 00113 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable"); 00114 00115 for (iterator I = begin(), IE = end(); I != IE; ++I) { 00116 GCFunctionInfo &FI = **I; 00117 00118 uint64_t FrameSize = FI.getFrameSize(); 00119 if (FrameSize >= 1<<16) { 00120 std::string msg; 00121 raw_string_ostream Msg(msg); 00122 Msg << "Function '" << FI.getFunction().getName() 00123 << "' is too large for the ocaml GC! " 00124 << "Frame size " << FrameSize << " >= 65536.\n"; 00125 Msg << "(" << uintptr_t(&FI) << ")"; 00126 llvm_report_error(Msg.str()); // Very rude! 00127 } 00128 00129 OS << "\t" << TAI.getCommentString() << " live roots for " 00130 << FI.getFunction().getName() << "\n"; 00131 00132 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { 00133 size_t LiveCount = FI.live_size(J); 00134 if (LiveCount >= 1<<16) { 00135 std::string msg; 00136 raw_string_ostream Msg(msg); 00137 Msg << "Function '" << FI.getFunction().getName() 00138 << "' is too large for the ocaml GC! " 00139 << "Live root count " << LiveCount << " >= 65536."; 00140 llvm_report_error(Msg.str()); // Very rude! 00141 } 00142 00143 OS << AddressDirective 00144 << TAI.getPrivateGlobalPrefix() << "label" << J->Num; 00145 AP.EOL("call return address"); 00146 00147 AP.EmitInt16(FrameSize); 00148 AP.EOL("stack frame size"); 00149 00150 AP.EmitInt16(LiveCount); 00151 AP.EOL("live root count"); 00152 00153 for (GCFunctionInfo::live_iterator K = FI.live_begin(J), 00154 KE = FI.live_end(J); K != KE; ++K) { 00155 assert(K->StackOffset < 1<<16 && 00156 "GC root stack offset is outside of fixed stack frame and out " 00157 "of range for ocaml GC!"); 00158 00159 OS << "\t.word\t" << K->StackOffset; 00160 AP.EOL("stack offset"); 00161 } 00162 00163 AP.EmitAlignment(AddressAlignLog); 00164 } 00165 } 00166 }