Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 1 | //===- OcamlGCPrinter.cpp - Ocaml frametable emitter ----------------------===// |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 9 | // This file implements printing the assembly code for an Ocaml frametable. |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 12 | |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/AsmPrinter.h" |
Philip Reames | 9b8c102 | 2018-11-10 16:08:10 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/BuiltinGCs.h" |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/GCMetadata.h" |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCDirectives.h" |
Chris Lattner | 8e2b12b16 | 2010-03-14 07:36:49 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 4b7dadb | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 29 | #include <cctype> |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <string> |
| 33 | |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
| 37 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 38 | class OcamlGCMetadataPrinter : public GCMetadataPrinter { |
| 39 | public: |
| 40 | void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; |
| 41 | void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; |
| 42 | }; |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 43 | |
| 44 | } // end anonymous namespace |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 45 | |
Gordon Henriksen | dbe06d3 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 46 | static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter> |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 47 | Y("ocaml", "ocaml 3.10-compatible collector"); |
Gordon Henriksen | dbe06d3 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 48 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 49 | void llvm::linkOcamlGCPrinter() {} |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 50 | |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 51 | static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) { |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 52 | const std::string &MId = M.getModuleIdentifier(); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 53 | |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 54 | std::string SymName; |
| 55 | SymName += "caml"; |
| 56 | size_t Letter = SymName.size(); |
Eugene Zelenko | 6ac7a34 | 2017-06-07 23:53:32 +0000 | [diff] [blame] | 57 | SymName.append(MId.begin(), llvm::find(MId, '.')); |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 58 | SymName += "__"; |
| 59 | SymName += Id; |
Mikhail Glushenkov | 4721ad8 | 2010-07-01 01:00:22 +0000 | [diff] [blame] | 60 | |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 61 | // Capitalize the first letter of the module name. |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 62 | SymName[Letter] = toupper(SymName[Letter]); |
Mikhail Glushenkov | 4721ad8 | 2010-07-01 01:00:22 +0000 | [diff] [blame] | 63 | |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 64 | SmallString<128> TmpStr; |
Rafael Espindola | c233f74 | 2015-06-23 13:59:29 +0000 | [diff] [blame] | 65 | Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout()); |
Mikhail Glushenkov | 4721ad8 | 2010-07-01 01:00:22 +0000 | [diff] [blame] | 66 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 67 | MCSymbol *Sym = AP.OutContext.getOrCreateSymbol(TmpStr); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 68 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 69 | AP.OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global); |
| 70 | AP.OutStreamer->EmitLabel(Sym); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 73 | void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info, |
| 74 | AsmPrinter &AP) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 75 | AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection()); |
Philip Reames | de22605 | 2014-12-09 23:57:54 +0000 | [diff] [blame] | 76 | EmitCamlGlobal(M, AP, "code_begin"); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 77 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 78 | AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection()); |
Philip Reames | de22605 | 2014-12-09 23:57:54 +0000 | [diff] [blame] | 79 | EmitCamlGlobal(M, AP, "data_begin"); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// emitAssembly - Print the frametable. The ocaml frametable format is thus: |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 83 | /// |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 84 | /// extern "C" struct align(sizeof(intptr_t)) { |
| 85 | /// uint16_t NumDescriptors; |
| 86 | /// struct align(sizeof(intptr_t)) { |
| 87 | /// void *ReturnAddress; |
| 88 | /// uint16_t FrameSize; |
| 89 | /// uint16_t NumLiveOffsets; |
| 90 | /// uint16_t LiveOffsets[NumLiveOffsets]; |
| 91 | /// } Descriptors[NumDescriptors]; |
| 92 | /// } caml${module}__frametable; |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 93 | /// |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 94 | /// Note that this precludes programs from stack frames larger than 64K |
| 95 | /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 96 | /// either condition is detected in a function which uses the GC. |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 97 | /// |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 98 | void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, |
| 99 | AsmPrinter &AP) { |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 100 | unsigned IntPtrSize = M.getDataLayout().getPointerSize(); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 101 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 102 | AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection()); |
Philip Reames | de22605 | 2014-12-09 23:57:54 +0000 | [diff] [blame] | 103 | EmitCamlGlobal(M, AP, "code_end"); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 104 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 105 | AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection()); |
Philip Reames | de22605 | 2014-12-09 23:57:54 +0000 | [diff] [blame] | 106 | EmitCamlGlobal(M, AP, "data_end"); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 107 | |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 108 | // FIXME: Why does ocaml emit this?? |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 109 | AP.OutStreamer->EmitIntValue(0, IntPtrSize); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 110 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 111 | AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection()); |
Philip Reames | de22605 | 2014-12-09 23:57:54 +0000 | [diff] [blame] | 112 | EmitCamlGlobal(M, AP, "frametable"); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 113 | |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 114 | int NumDescriptors = 0; |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 115 | for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 116 | IE = Info.funcinfo_end(); |
| 117 | I != IE; ++I) { |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 118 | GCFunctionInfo &FI = **I; |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 119 | if (FI.getStrategy().getName() != getStrategy().getName()) |
| 120 | // this function is managed by some other GC |
| 121 | continue; |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 122 | for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { |
| 123 | NumDescriptors++; |
| 124 | } |
| 125 | } |
| 126 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 127 | if (NumDescriptors >= 1 << 16) { |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 128 | // Very rude! |
| 129 | report_fatal_error(" Too much descriptor for ocaml GC"); |
| 130 | } |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 131 | AP.emitInt16(NumDescriptors); |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 132 | AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); |
| 133 | |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 134 | for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 135 | IE = Info.funcinfo_end(); |
| 136 | I != IE; ++I) { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 137 | GCFunctionInfo &FI = **I; |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 138 | if (FI.getStrategy().getName() != getStrategy().getName()) |
| 139 | // this function is managed by some other GC |
| 140 | continue; |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 141 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 142 | uint64_t FrameSize = FI.getFrameSize(); |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 143 | if (FrameSize >= 1 << 16) { |
Benjamin Kramer | a676926 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 144 | // Very rude! |
| 145 | report_fatal_error("Function '" + FI.getFunction().getName() + |
| 146 | "' is too large for the ocaml GC! " |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 147 | "Frame size " + |
| 148 | Twine(FrameSize) + ">= 65536.\n" |
| 149 | "(" + |
| 150 | Twine(uintptr_t(&FI)) + ")"); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 151 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 152 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 153 | AP.OutStreamer->AddComment("live roots for " + |
| 154 | Twine(FI.getFunction().getName())); |
| 155 | AP.OutStreamer->AddBlankLine(); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 156 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 157 | for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) { |
| 158 | size_t LiveCount = FI.live_size(J); |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 159 | if (LiveCount >= 1 << 16) { |
Benjamin Kramer | a676926 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 160 | // Very rude! |
| 161 | report_fatal_error("Function '" + FI.getFunction().getName() + |
| 162 | "' is too large for the ocaml GC! " |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 163 | "Live root count " + |
| 164 | Twine(LiveCount) + " >= 65536."); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 165 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 166 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 167 | AP.OutStreamer->EmitSymbolValue(J->Label, IntPtrSize); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 168 | AP.emitInt16(FrameSize); |
| 169 | AP.emitInt16(LiveCount); |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 170 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 171 | for (GCFunctionInfo::live_iterator K = FI.live_begin(J), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 172 | KE = FI.live_end(J); |
| 173 | K != KE; ++K) { |
| 174 | if (K->StackOffset >= 1 << 16) { |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 175 | // Very rude! |
| 176 | report_fatal_error( |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 177 | "GC root stack offset is outside of fixed stack frame and out " |
| 178 | "of range for ocaml GC!"); |
Nicolas Geoffray | c532722 | 2010-05-24 12:24:11 +0000 | [diff] [blame] | 179 | } |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 180 | AP.emitInt16(K->StackOffset); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 181 | } |
Mikhail Glushenkov | b2f9a73 | 2009-01-16 06:53:46 +0000 | [diff] [blame] | 182 | |
Chris Lattner | ef8240b | 2010-04-04 07:39:04 +0000 | [diff] [blame] | 183 | AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3); |
Gordon Henriksen | c7e991b | 2008-01-07 02:31:11 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |