blob: 850419c9ee44d46cc7b5342658af4af2fa7532ce [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
Nick Lewycky02d5f772009-10-25 06:33:48 +000035 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
Gordon Henriksendbe06d32008-08-17 12:08:44 +000036 public:
Philip Reamesde226052014-12-09 23:57:54 +000037 void beginAssembly(Module &M, AsmPrinter &AP) override;
38 void finishAssembly(Module &M, AsmPrinter &AP) override;
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000039 };
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000040
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000041}
42
Gordon Henriksendbe06d32008-08-17 12:08:44 +000043static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
44Y("ocaml", "ocaml 3.10-compatible collector");
45
Gordon Henriksend930f912008-08-17 18:44:35 +000046void llvm::linkOcamlGCPrinter() { }
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000047
Chris Lattneref8240b2010-04-04 07:39:04 +000048static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000049 const std::string &MId = M.getModuleIdentifier();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000050
Chris Lattneref8240b2010-04-04 07:39:04 +000051 std::string SymName;
52 SymName += "caml";
53 size_t Letter = SymName.size();
54 SymName.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
55 SymName += "__";
56 SymName += Id;
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000057
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000058 // Capitalize the first letter of the module name.
Chris Lattneref8240b2010-04-04 07:39:04 +000059 SymName[Letter] = toupper(SymName[Letter]);
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000060
Chris Lattneref8240b2010-04-04 07:39:04 +000061 SmallString<128> TmpStr;
62 AP.Mang->getNameWithPrefix(TmpStr, SymName);
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000063
Chris Lattneref8240b2010-04-04 07:39:04 +000064 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000065
Chris Lattneref8240b2010-04-04 07:39:04 +000066 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
67 AP.OutStreamer.EmitLabel(Sym);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000068}
69
Philip Reamesde226052014-12-09 23:57:54 +000070void OcamlGCMetadataPrinter::beginAssembly(Module &M, AsmPrinter &AP) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +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
Chris Lattner4b7dadb2009-08-19 05:49:37 +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 Reamesde226052014-12-09 23:57:54 +000094void OcamlGCMetadataPrinter::finishAssembly(Module &M, AsmPrinter &AP) {
Eric Christopherd9134482014-08-04 21:25:23 +000095 unsigned IntPtrSize =
96 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000097
Chris Lattner4b7dadb2009-08-19 05:49:37 +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
Chris Lattner4b7dadb2009-08-19 05:49:37 +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??
Eric Christophere3ab3d02013-01-09 01:57:54 +0000105 AP.OutStreamer.EmitIntValue(0, IntPtrSize);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000106
Chris Lattner4b7dadb2009-08-19 05:49:37 +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;
111 for (iterator I = begin(), IE = end(); I != IE; ++I) {
112 GCFunctionInfo &FI = **I;
113 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
114 NumDescriptors++;
115 }
116 }
117
118 if (NumDescriptors >= 1<<16) {
119 // Very rude!
120 report_fatal_error(" Too much descriptor for ocaml GC");
121 }
122 AP.EmitInt16(NumDescriptors);
123 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
124
Gordon Henriksend930f912008-08-17 18:44:35 +0000125 for (iterator I = begin(), IE = end(); I != IE; ++I) {
126 GCFunctionInfo &FI = **I;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000127
Gordon Henriksend930f912008-08-17 18:44:35 +0000128 uint64_t FrameSize = FI.getFrameSize();
129 if (FrameSize >= 1<<16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000130 // Very rude!
131 report_fatal_error("Function '" + FI.getFunction().getName() +
132 "' is too large for the ocaml GC! "
133 "Frame size " + Twine(FrameSize) + ">= 65536.\n"
134 "(" + Twine(uintptr_t(&FI)) + ")");
Gordon Henriksend930f912008-08-17 18:44:35 +0000135 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000136
Chris Lattneref8240b2010-04-04 07:39:04 +0000137 AP.OutStreamer.AddComment("live roots for " +
138 Twine(FI.getFunction().getName()));
139 AP.OutStreamer.AddBlankLine();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000140
Gordon Henriksend930f912008-08-17 18:44:35 +0000141 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
142 size_t LiveCount = FI.live_size(J);
Gordon Henriksenada201c2008-08-09 03:48:46 +0000143 if (LiveCount >= 1<<16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000144 // Very rude!
145 report_fatal_error("Function '" + FI.getFunction().getName() +
146 "' is too large for the ocaml GC! "
147 "Live root count "+Twine(LiveCount)+" >= 65536.");
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000148 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000149
Eric Christopherbf7bc492013-01-09 03:52:05 +0000150 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000151 AP.EmitInt16(FrameSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000152 AP.EmitInt16(LiveCount);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000153
Gordon Henriksend930f912008-08-17 18:44:35 +0000154 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
155 KE = FI.live_end(J); K != KE; ++K) {
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000156 if (K->StackOffset >= 1<<16) {
157 // Very rude!
158 report_fatal_error(
159 "GC root stack offset is outside of fixed stack frame and out "
160 "of range for ocaml GC!");
161 }
162 AP.EmitInt16(K->StackOffset);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000163 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000164
Chris Lattneref8240b2010-04-04 07:39:04 +0000165 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000166 }
167 }
168}