blob: 8baee4db772e8d6d1736f683c5185c8a9813efc2 [file] [log] [blame]
Gordon Henriksend930f912008-08-17 18:44:35 +00001//===-- OcamlGCPrinter.cpp - Ocaml frametable emitter ---------------------===//
Gordon Henriksenc7e991b2008-01-07 02:31:11 +00002//
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//
Gordon Henriksend930f912008-08-17 18:44:35 +000010// This file implements printing the assembly code for an Ocaml frametable.
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000011//
12//===----------------------------------------------------------------------===//
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000013
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000014#include "llvm/CodeGen/GCs.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000016#include "llvm/CodeGen/AsmPrinter.h"
Gordon Henriksend930f912008-08-17 18:44:35 +000017#include "llvm/CodeGen/GCMetadataPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000019#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattneref8240b2010-04-04 07:39:04 +000022#include "llvm/MC/MCContext.h"
Chris Lattner8e2b12b162010-03-14 07:36:49 +000023#include "llvm/MC/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000026#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
28#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000029#include "llvm/Target/TargetSubtargetInfo.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000030#include <cctype>
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000031using namespace llvm;
32
33namespace {
34
Philip Reames36319532015-01-16 23:16:12 +000035class OcamlGCMetadataPrinter : public GCMetadataPrinter {
36public:
37 void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
38 void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
39};
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000040}
41
Gordon Henriksendbe06d32008-08-17 12:08:44 +000042static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
Philip Reames36319532015-01-16 23:16:12 +000043 Y("ocaml", "ocaml 3.10-compatible collector");
Gordon Henriksendbe06d32008-08-17 12:08:44 +000044
Philip Reames36319532015-01-16 23:16:12 +000045void llvm::linkOcamlGCPrinter() {}
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000046
Chris Lattneref8240b2010-04-04 07:39:04 +000047static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000048 const std::string &MId = M.getModuleIdentifier();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000049
Chris Lattneref8240b2010-04-04 07:39:04 +000050 std::string SymName;
51 SymName += "caml";
52 size_t Letter = SymName.size();
David Majnemer0d955d02016-08-11 22:21:41 +000053 SymName.append(MId.begin(), find(MId, '.'));
Chris Lattneref8240b2010-04-04 07:39:04 +000054 SymName += "__";
55 SymName += Id;
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000056
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000057 // Capitalize the first letter of the module name.
Chris Lattneref8240b2010-04-04 07:39:04 +000058 SymName[Letter] = toupper(SymName[Letter]);
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000059
Chris Lattneref8240b2010-04-04 07:39:04 +000060 SmallString<128> TmpStr;
Rafael Espindolac233f742015-06-23 13:59:29 +000061 Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout());
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000062
Jim Grosbach6f482002015-05-18 18:43:14 +000063 MCSymbol *Sym = AP.OutContext.getOrCreateSymbol(TmpStr);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000064
Lang Hames9ff69c82015-04-24 19:11:51 +000065 AP.OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
66 AP.OutStreamer->EmitLabel(Sym);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000067}
68
Philip Reames1e308972014-12-11 01:47:23 +000069void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info,
70 AsmPrinter &AP) {
Lang Hames9ff69c82015-04-24 19:11:51 +000071 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
Philip Reamesde226052014-12-09 23:57:54 +000072 EmitCamlGlobal(M, AP, "code_begin");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000073
Lang Hames9ff69c82015-04-24 19:11:51 +000074 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
Philip Reamesde226052014-12-09 23:57:54 +000075 EmitCamlGlobal(M, AP, "data_begin");
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000076}
77
78/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000079///
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000080/// 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;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000089///
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000090/// Note that this precludes programs from stack frames larger than 64K
91/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
Gordon Henriksend930f912008-08-17 18:44:35 +000092/// either condition is detected in a function which uses the GC.
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000093///
Philip Reames1e308972014-12-11 01:47:23 +000094void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
95 AsmPrinter &AP) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +000096 unsigned IntPtrSize = M.getDataLayout().getPointerSize();
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000097
Lang Hames9ff69c82015-04-24 19:11:51 +000098 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
Philip Reamesde226052014-12-09 23:57:54 +000099 EmitCamlGlobal(M, AP, "code_end");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000100
Lang Hames9ff69c82015-04-24 19:11:51 +0000101 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
Philip Reamesde226052014-12-09 23:57:54 +0000102 EmitCamlGlobal(M, AP, "data_end");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000103
Chris Lattneref8240b2010-04-04 07:39:04 +0000104 // FIXME: Why does ocaml emit this??
Lang Hames9ff69c82015-04-24 19:11:51 +0000105 AP.OutStreamer->EmitIntValue(0, IntPtrSize);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000106
Lang Hames9ff69c82015-04-24 19:11:51 +0000107 AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
Philip Reamesde226052014-12-09 23:57:54 +0000108 EmitCamlGlobal(M, AP, "frametable");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000109
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000110 int NumDescriptors = 0;
Philip Reames1e308972014-12-11 01:47:23 +0000111 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000112 IE = Info.funcinfo_end();
113 I != IE; ++I) {
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000114 GCFunctionInfo &FI = **I;
Philip Reames1e308972014-12-11 01:47:23 +0000115 if (FI.getStrategy().getName() != getStrategy().getName())
116 // this function is managed by some other GC
117 continue;
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000118 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
119 NumDescriptors++;
120 }
121 }
122
Philip Reames36319532015-01-16 23:16:12 +0000123 if (NumDescriptors >= 1 << 16) {
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000124 // Very rude!
125 report_fatal_error(" Too much descriptor for ocaml GC");
126 }
127 AP.EmitInt16(NumDescriptors);
128 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
129
Philip Reames1e308972014-12-11 01:47:23 +0000130 for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
Philip Reames36319532015-01-16 23:16:12 +0000131 IE = Info.funcinfo_end();
132 I != IE; ++I) {
Gordon Henriksend930f912008-08-17 18:44:35 +0000133 GCFunctionInfo &FI = **I;
Philip Reames1e308972014-12-11 01:47:23 +0000134 if (FI.getStrategy().getName() != getStrategy().getName())
135 // this function is managed by some other GC
136 continue;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000137
Gordon Henriksend930f912008-08-17 18:44:35 +0000138 uint64_t FrameSize = FI.getFrameSize();
Philip Reames36319532015-01-16 23:16:12 +0000139 if (FrameSize >= 1 << 16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000140 // Very rude!
141 report_fatal_error("Function '" + FI.getFunction().getName() +
142 "' is too large for the ocaml GC! "
Philip Reames36319532015-01-16 23:16:12 +0000143 "Frame size " +
144 Twine(FrameSize) + ">= 65536.\n"
145 "(" +
146 Twine(uintptr_t(&FI)) + ")");
Gordon Henriksend930f912008-08-17 18:44:35 +0000147 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000148
Lang Hames9ff69c82015-04-24 19:11:51 +0000149 AP.OutStreamer->AddComment("live roots for " +
150 Twine(FI.getFunction().getName()));
151 AP.OutStreamer->AddBlankLine();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000152
Gordon Henriksend930f912008-08-17 18:44:35 +0000153 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
154 size_t LiveCount = FI.live_size(J);
Philip Reames36319532015-01-16 23:16:12 +0000155 if (LiveCount >= 1 << 16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000156 // Very rude!
157 report_fatal_error("Function '" + FI.getFunction().getName() +
158 "' is too large for the ocaml GC! "
Philip Reames36319532015-01-16 23:16:12 +0000159 "Live root count " +
160 Twine(LiveCount) + " >= 65536.");
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000161 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000162
Lang Hames9ff69c82015-04-24 19:11:51 +0000163 AP.OutStreamer->EmitSymbolValue(J->Label, IntPtrSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000164 AP.EmitInt16(FrameSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000165 AP.EmitInt16(LiveCount);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000166
Gordon Henriksend930f912008-08-17 18:44:35 +0000167 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
Philip Reames36319532015-01-16 23:16:12 +0000168 KE = FI.live_end(J);
169 K != KE; ++K) {
170 if (K->StackOffset >= 1 << 16) {
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000171 // Very rude!
172 report_fatal_error(
Philip Reames36319532015-01-16 23:16:12 +0000173 "GC root stack offset is outside of fixed stack frame and out "
174 "of range for ocaml GC!");
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000175 }
176 AP.EmitInt16(K->StackOffset);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000177 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000178
Chris Lattneref8240b2010-04-04 07:39:04 +0000179 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000180 }
181 }
182}