blob: 98177c0ba1cff3e508a71514627e55f245755665 [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"
19#include "llvm/IR/Module.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattneref8240b2010-04-04 07:39:04 +000021#include "llvm/MC/MCContext.h"
Chris Lattner8e2b12b162010-03-14 07:36:49 +000022#include "llvm/MC/MCStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000025#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/Mangler.h"
27#include "llvm/Target/TargetLoweringObjectFile.h"
28#include "llvm/Target/TargetMachine.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000029#include <cctype>
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000030using namespace llvm;
31
32namespace {
33
Nick Lewycky02d5f772009-10-25 06:33:48 +000034 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
Gordon Henriksendbe06d32008-08-17 12:08:44 +000035 public:
Chris Lattneref8240b2010-04-04 07:39:04 +000036 void beginAssembly(AsmPrinter &AP);
37 void finishAssembly(AsmPrinter &AP);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000038 };
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000039
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000040}
41
Gordon Henriksendbe06d32008-08-17 12:08:44 +000042static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
43Y("ocaml", "ocaml 3.10-compatible collector");
44
Gordon Henriksend930f912008-08-17 18:44:35 +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();
53 SymName.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
54 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;
61 AP.Mang->getNameWithPrefix(TmpStr, SymName);
Mikhail Glushenkov4721ad82010-07-01 01:00:22 +000062
Chris Lattneref8240b2010-04-04 07:39:04 +000063 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000064
Chris Lattneref8240b2010-04-04 07:39:04 +000065 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
66 AP.OutStreamer.EmitLabel(Sym);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000067}
68
Chris Lattneref8240b2010-04-04 07:39:04 +000069void OcamlGCMetadataPrinter::beginAssembly(AsmPrinter &AP) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +000070 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
Chris Lattneref8240b2010-04-04 07:39:04 +000071 EmitCamlGlobal(getModule(), AP, "code_begin");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000072
Chris Lattner4b7dadb2009-08-19 05:49:37 +000073 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattneref8240b2010-04-04 07:39:04 +000074 EmitCamlGlobal(getModule(), AP, "data_begin");
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000075}
76
77/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000078///
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000079/// extern "C" struct align(sizeof(intptr_t)) {
80/// uint16_t NumDescriptors;
81/// struct align(sizeof(intptr_t)) {
82/// void *ReturnAddress;
83/// uint16_t FrameSize;
84/// uint16_t NumLiveOffsets;
85/// uint16_t LiveOffsets[NumLiveOffsets];
86/// } Descriptors[NumDescriptors];
87/// } caml${module}__frametable;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000088///
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000089/// Note that this precludes programs from stack frames larger than 64K
90/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
Gordon Henriksend930f912008-08-17 18:44:35 +000091/// either condition is detected in a function which uses the GC.
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000092///
Chris Lattneref8240b2010-04-04 07:39:04 +000093void OcamlGCMetadataPrinter::finishAssembly(AsmPrinter &AP) {
Chandler Carruth5da3f052012-11-01 09:14:31 +000094 unsigned IntPtrSize = AP.TM.getDataLayout()->getPointerSize();
Gordon Henriksenc7e991b2008-01-07 02:31:11 +000095
Chris Lattner4b7dadb2009-08-19 05:49:37 +000096 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
Chris Lattneref8240b2010-04-04 07:39:04 +000097 EmitCamlGlobal(getModule(), AP, "code_end");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +000098
Chris Lattner4b7dadb2009-08-19 05:49:37 +000099 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattneref8240b2010-04-04 07:39:04 +0000100 EmitCamlGlobal(getModule(), AP, "data_end");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000101
Chris Lattneref8240b2010-04-04 07:39:04 +0000102 // FIXME: Why does ocaml emit this??
Eric Christophere3ab3d02013-01-09 01:57:54 +0000103 AP.OutStreamer.EmitIntValue(0, IntPtrSize);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000104
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000105 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattneref8240b2010-04-04 07:39:04 +0000106 EmitCamlGlobal(getModule(), AP, "frametable");
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000107
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000108 int NumDescriptors = 0;
109 for (iterator I = begin(), IE = end(); I != IE; ++I) {
110 GCFunctionInfo &FI = **I;
111 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
112 NumDescriptors++;
113 }
114 }
115
116 if (NumDescriptors >= 1<<16) {
117 // Very rude!
118 report_fatal_error(" Too much descriptor for ocaml GC");
119 }
120 AP.EmitInt16(NumDescriptors);
121 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
122
Gordon Henriksend930f912008-08-17 18:44:35 +0000123 for (iterator I = begin(), IE = end(); I != IE; ++I) {
124 GCFunctionInfo &FI = **I;
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000125
Gordon Henriksend930f912008-08-17 18:44:35 +0000126 uint64_t FrameSize = FI.getFrameSize();
127 if (FrameSize >= 1<<16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000128 // Very rude!
129 report_fatal_error("Function '" + FI.getFunction().getName() +
130 "' is too large for the ocaml GC! "
131 "Frame size " + Twine(FrameSize) + ">= 65536.\n"
132 "(" + Twine(uintptr_t(&FI)) + ")");
Gordon Henriksend930f912008-08-17 18:44:35 +0000133 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000134
Chris Lattneref8240b2010-04-04 07:39:04 +0000135 AP.OutStreamer.AddComment("live roots for " +
136 Twine(FI.getFunction().getName()));
137 AP.OutStreamer.AddBlankLine();
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000138
Gordon Henriksend930f912008-08-17 18:44:35 +0000139 for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
140 size_t LiveCount = FI.live_size(J);
Gordon Henriksenada201c2008-08-09 03:48:46 +0000141 if (LiveCount >= 1<<16) {
Benjamin Kramera6769262010-04-08 10:44:28 +0000142 // Very rude!
143 report_fatal_error("Function '" + FI.getFunction().getName() +
144 "' is too large for the ocaml GC! "
145 "Live root count "+Twine(LiveCount)+" >= 65536.");
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000146 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000147
Eric Christopherbf7bc492013-01-09 03:52:05 +0000148 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000149 AP.EmitInt16(FrameSize);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000150 AP.EmitInt16(LiveCount);
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000151
Gordon Henriksend930f912008-08-17 18:44:35 +0000152 for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
153 KE = FI.live_end(J); K != KE; ++K) {
Nicolas Geoffrayc5327222010-05-24 12:24:11 +0000154 if (K->StackOffset >= 1<<16) {
155 // Very rude!
156 report_fatal_error(
157 "GC root stack offset is outside of fixed stack frame and out "
158 "of range for ocaml GC!");
159 }
160 AP.EmitInt16(K->StackOffset);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000161 }
Mikhail Glushenkovb2f9a732009-01-16 06:53:46 +0000162
Chris Lattneref8240b2010-04-04 07:39:04 +0000163 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
Gordon Henriksenc7e991b2008-01-07 02:31:11 +0000164 }
165 }
166}