|
| 1 | +//===- OxCamlGCPrinter.cpp - OxCaml frametable emitter --------------------===// |
| 2 | +// |
| 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 |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements printing the assembly code for an OxCaml frametable. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/ADT/STLExtras.h" |
| 14 | +#include "llvm/ADT/SmallString.h" |
| 15 | +#include "llvm/ADT/Twine.h" |
| 16 | +#include "llvm/CodeGen/AsmPrinter.h" |
| 17 | +#include "llvm/CodeGen/GCMetadata.h" |
| 18 | +#include "llvm/CodeGen/GCMetadataPrinter.h" |
| 19 | +#include "llvm/CodeGen/StackMaps.h" |
| 20 | +#include "llvm/IR/BuiltinGCs.h" |
| 21 | +#include "llvm/IR/DataLayout.h" |
| 22 | +#include "llvm/IR/Function.h" |
| 23 | +#include "llvm/IR/Mangler.h" |
| 24 | +#include "llvm/IR/Module.h" |
| 25 | +#include "llvm/IR/Statepoint.h" |
| 26 | +#include "llvm/MC/MCContext.h" |
| 27 | +#include "llvm/MC/MCDirectives.h" |
| 28 | +#include "llvm/MC/MCStreamer.h" |
| 29 | +#include "llvm/Support/ErrorHandling.h" |
| 30 | +#include "llvm/Target/TargetLoweringObjectFile.h" |
| 31 | +#include <array> |
| 32 | +#include <cctype> |
| 33 | +#include <cstddef> |
| 34 | +#include <cstdint> |
| 35 | +#include <string> |
| 36 | + |
| 37 | +using namespace llvm; |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +class OxCamlGCMetadataPrinter : public GCMetadataPrinter { |
| 42 | +public: |
| 43 | + void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; |
| 44 | + void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; |
| 45 | + bool emitStackMaps(Module &M, StackMaps &SM, AsmPrinter &AP) override; |
| 46 | +}; |
| 47 | + |
| 48 | +} // end anonymous namespace |
| 49 | + |
| 50 | +static GCMetadataPrinterRegistry::Add<OxCamlGCMetadataPrinter> |
| 51 | + Y("oxcaml", "OxCaml frametable printer"); |
| 52 | + |
| 53 | +void llvm::linkOxCamlGCPrinter() {} |
| 54 | + |
| 55 | +static std::string camlGlobalSymName(const Module &M, const char *Id) { |
| 56 | + if (Metadata *ModuleMD = M.getModuleFlag(StringRef("oxcaml_module"))) { |
| 57 | + if (MDString *Str = dyn_cast<MDString>(ModuleMD)) { |
| 58 | + StringRef ModuleName = Str->getString(); |
| 59 | + |
| 60 | + std::string SymName; |
| 61 | + SymName += "caml"; |
| 62 | + SymName += ModuleName; |
| 63 | + SymName += "__"; |
| 64 | + SymName += Id; |
| 65 | + |
| 66 | + return SymName; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + report_fatal_error("Module name not provided for OxCaml GC!"); |
| 71 | +} |
| 72 | + |
| 73 | +static void emitCamlGlobal(const Module &M, MCStreamer &OS, const char *Id) { |
| 74 | + std::string SymName = camlGlobalSymName(M, Id); |
| 75 | + |
| 76 | + SmallString<128> TmpStr; |
| 77 | + Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout()); |
| 78 | + |
| 79 | + MCSymbol *Sym = OS.getContext().getOrCreateSymbol(TmpStr); |
| 80 | + |
| 81 | + OS.emitSymbolAttribute(Sym, MCSA_Global); |
| 82 | + OS.emitLabel(Sym); |
| 83 | +} |
| 84 | + |
| 85 | +void OxCamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info, |
| 86 | + AsmPrinter &AP) { |
| 87 | + AP.OutStreamer->switchSection(AP.getObjFileLowering().getTextSection()); |
| 88 | + emitCamlGlobal(M, *(AP.OutStreamer), "code_begin"); |
| 89 | + |
| 90 | + AP.OutStreamer->switchSection(AP.getObjFileLowering().getDataSection()); |
| 91 | + emitCamlGlobal(M, *(AP.OutStreamer), "data_begin"); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void OxCamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info, |
| 96 | + AsmPrinter &AP) { |
| 97 | + AP.OutStreamer->switchSection(AP.getObjFileLowering().getTextSection()); |
| 98 | + emitCamlGlobal(M, *(AP.OutStreamer), "code_end"); |
| 99 | + |
| 100 | + AP.OutStreamer->switchSection(AP.getObjFileLowering().getDataSection()); |
| 101 | + emitCamlGlobal(M, *(AP.OutStreamer), "data_end"); |
| 102 | +} |
| 103 | + |
| 104 | +/// Map LLVM DWARF register numbers to OxCaml register map. |
| 105 | +/// * See llvm/lib/Target/X86/X86RegisterInfo.td for DWARF register numbers. |
| 106 | +/// * See backend/amd64/proc.ml for the OxCaml register map. |
| 107 | + |
| 108 | +// TODO: This is target-specific and should probably live in a |
| 109 | +// target-specific location. |
| 110 | + |
| 111 | +// Directly taken from [Reg_class.gpr_dwarf_reg_numbers]: |
| 112 | +// https://github.com/oxcaml/oxcaml/blob/main/backend/amd64/reg_class.ml#L26 |
| 113 | +// Note that R14 and R15 are added for completeness |
| 114 | +static constexpr std::array<unsigned, 16> GPR_OxCamlToDwarf = |
| 115 | + { 0, 3, 5, 4, 1, 2, 8, 9, 12, 13, 10, 11, 6, 14, 15 }; |
| 116 | + |
| 117 | +static constexpr auto GPR_DwarfToOxCaml = []() { |
| 118 | + std::array<unsigned, 16> result{}; |
| 119 | + for (size_t ocaml_idx = 0; ocaml_idx < GPR_OxCamlToDwarf.size(); ++ocaml_idx) { |
| 120 | + unsigned dwarf_reg = GPR_OxCamlToDwarf[ocaml_idx]; |
| 121 | + if (dwarf_reg < result.size()) { |
| 122 | + result[dwarf_reg] = ocaml_idx; |
| 123 | + } |
| 124 | + } |
| 125 | + return result; |
| 126 | +}(); |
| 127 | + |
| 128 | +static const unsigned XMMBeginOxCaml = 100; |
| 129 | +static const unsigned XMMBeginDwarf = 17; |
| 130 | +static const unsigned XMMEndDwarf = 32; |
| 131 | + |
| 132 | +static unsigned mapLLVMDwarfRegToOxCamlIndex(unsigned DwarfRegNum) { |
| 133 | + if (DwarfRegNum < GPR_DwarfToOxCaml.size()) { |
| 134 | + return GPR_DwarfToOxCaml[DwarfRegNum]; |
| 135 | + } else if (XMMBeginDwarf <= DwarfRegNum && DwarfRegNum <= XMMEndDwarf) { |
| 136 | + return DwarfRegNum - XMMBeginDwarf + XMMBeginOxCaml; |
| 137 | + } else { |
| 138 | + report_fatal_error("Unrecognised DWARF register for use in OxCaml frametable: " |
| 139 | + + Twine(DwarfRegNum)); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +bool OxCamlGCMetadataPrinter::emitStackMaps(Module &M, StackMaps &SM, AsmPrinter &AP) { |
| 144 | + MCStreamer &OS = *AP.OutStreamer; |
| 145 | + unsigned PtrSize = M.getDataLayout().getPointerSize(); // Can only be 8 for now |
| 146 | + |
| 147 | + OS.switchSection(AP.getObjFileLowering().getDataSection()); |
| 148 | + |
| 149 | + emitCamlGlobal(M, OS, "frametable"); |
| 150 | + |
| 151 | + // Number of records |
| 152 | + OS.emitInt64(SM.getCSInfos().size()); |
| 153 | + |
| 154 | + for (const auto &CSI : SM.getCSInfos()) { |
| 155 | + // From runtime/frame_descriptors.h: |
| 156 | + // https://github.com/oxcaml/oxcaml/blob/main/runtime/caml/frame_descriptors.h#L63 |
| 157 | + // |
| 158 | + // typedef struct { |
| 159 | + // int32_t retaddr_rel; /* offset of return address from &retaddr_rel */ |
| 160 | + // uint16_t frame_data; /* frame size and various flags */ |
| 161 | + // uint16_t num_live; |
| 162 | + // uint16_t live_ofs[num_live]; |
| 163 | + // } frame_descr; |
| 164 | + |
| 165 | + // retaddr_rel |
| 166 | + MCSymbol *Here = OS.getContext().createTempSymbol(); |
| 167 | + OS.emitLabel(Here); |
| 168 | + const MCExpr *RelativeAddr = MCBinaryExpr::createSub( |
| 169 | + MCSymbolRefExpr::create(CSI.CSLabel, OS.getContext()), |
| 170 | + MCSymbolRefExpr::create(Here, OS.getContext()), |
| 171 | + OS.getContext()); |
| 172 | + OS.emitValue(RelativeAddr, 4); |
| 173 | + |
| 174 | + // frame_data |
| 175 | + uint64_t FrameSize = CSI.CSFunctionInfo.StaticStackSize; |
| 176 | + if (CSI.ID != StatepointDirectives::DefaultStatepointID) |
| 177 | + FrameSize += CSI.ID; // Stack offset from OxCaml |
| 178 | + FrameSize += PtrSize; // Return address |
| 179 | + |
| 180 | + if (FrameSize >= 1 << 16) |
| 181 | + report_fatal_error("Long frames not supported for OxCaml GC: FrameSize = " |
| 182 | + + Twine(FrameSize)); |
| 183 | + OS.emitInt16(FrameSize); |
| 184 | + |
| 185 | + // num_live |
| 186 | + uint64_t LiveCount = 0; |
| 187 | + for (const auto &Loc : CSI.Locations) { |
| 188 | + if (Loc.Type == StackMaps::Location::Register || |
| 189 | + Loc.Type == StackMaps::Location::Direct || |
| 190 | + Loc.Type == StackMaps::Location::Indirect) { |
| 191 | + LiveCount++; |
| 192 | + } |
| 193 | + } |
| 194 | + LiveCount += CSI.LiveOuts.size(); |
| 195 | + |
| 196 | + if (LiveCount >= 1 << 16) { |
| 197 | + // Very rude! |
| 198 | + report_fatal_error("Long frames not supported for OxCaml GC: LiveCount = " |
| 199 | + + Twine(LiveCount)); |
| 200 | + } |
| 201 | + OS.emitInt16(LiveCount); |
| 202 | + |
| 203 | + // live_ofs |
| 204 | + for (const auto &Loc : CSI.Locations) { |
| 205 | + if (Loc.Type == StackMaps::Location::Register) { |
| 206 | + // Register indices are tagged (2n+1) and follow the OxCaml register |
| 207 | + // map (see `mapLLVMDwarfRegToOxCamlIndex`) |
| 208 | + unsigned DwarfRegNum = Loc.Reg; |
| 209 | + unsigned OxCamlIndex = mapLLVMDwarfRegToOxCamlIndex(DwarfRegNum); |
| 210 | + uint16_t EncodedReg = (OxCamlIndex << 1) + 1; |
| 211 | + OS.emitInt16(EncodedReg); |
| 212 | + } else if (Loc.Type == StackMaps::Location::Direct || |
| 213 | + Loc.Type == StackMaps::Location::Indirect) { |
| 214 | + // For stack locations (Direct/Indirect): emit offset directly |
| 215 | + int64_t Offset = Loc.Offset; |
| 216 | + |
| 217 | + // BP-relative addressing -> SP |
| 218 | + if (Offset < 0) { |
| 219 | + int64_t TempFrameSize = |
| 220 | + FrameSize - PtrSize /* return address */ - PtrSize /* pushed BP */; |
| 221 | + Offset += TempFrameSize; |
| 222 | + } |
| 223 | + |
| 224 | + if (Offset < -(1 << 15) || Offset >= (1 << 15)) { |
| 225 | + // Very rude! |
| 226 | + report_fatal_error("Stack offset too large for OxCaml frametable: " |
| 227 | + + Twine(Offset)); |
| 228 | + } |
| 229 | + OS.emitInt16(static_cast<uint16_t>(Offset)); |
| 230 | + } else { |
| 231 | + // TODO: Do we need anything else here? |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + for (const auto &LO : CSI.LiveOuts) { |
| 236 | + unsigned OxCamlIndex = mapLLVMDwarfRegToOxCamlIndex(LO.DwarfRegNum); |
| 237 | + uint16_t EncodedReg = (OxCamlIndex << 1) + 1; |
| 238 | + OS.emitInt16(EncodedReg); |
| 239 | + } |
| 240 | + |
| 241 | + OS.emitValueToAlignment(Align(PtrSize)); |
| 242 | + } |
| 243 | + |
| 244 | + OS.addBlankLine(); |
| 245 | + return true; |
| 246 | +} |
0 commit comments