blob: 115381767751db828a1e4bf8bf5fb945cb32d248 [file] [log] [blame]
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001//===-- OcamlGCPrinter.cpp - Ocaml frametable emitter ---------------------===//
Gordon Henriksen572742e2008-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 Henriksen5eca0752008-08-17 18:44:35 +000010// This file implements printing the assembly code for an Ocaml frametable.
Gordon Henriksen572742e2008-01-07 02:31:11 +000011//
12//===----------------------------------------------------------------------===//
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000013
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000014#include "llvm/CodeGen/GCs.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000015#include "llvm/CodeGen/AsmPrinter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000016#include "llvm/CodeGen/GCMetadataPrinter.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000017#include "llvm/Module.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattner7d73c7f2010-04-04 07:39:04 +000019#include "llvm/MC/MCContext.h"
Chris Lattner9a38e672010-03-14 07:36:49 +000020#include "llvm/MC/MCSymbol.h"
21#include "llvm/MC/MCStreamer.h"
Chris Lattner7d73c7f2010-04-04 07:39:04 +000022#include "llvm/Target/Mangler.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000023#include "llvm/Target/TargetData.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
Gordon Henriksen572742e2008-01-07 02:31:11 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner7d73c7f2010-04-04 07:39:04 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000028#include "llvm/Support/FormattedStream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000029#include <cctype>
Gordon Henriksen572742e2008-01-07 02:31:11 +000030using namespace llvm;
31
32namespace {
33
Nick Lewycky6726b6d2009-10-25 06:33:48 +000034 class OcamlGCMetadataPrinter : public GCMetadataPrinter {
Gordon Henriksenc317a602008-08-17 12:08:44 +000035 public:
Chris Lattner7d73c7f2010-04-04 07:39:04 +000036 void beginAssembly(AsmPrinter &AP);
37 void finishAssembly(AsmPrinter &AP);
Gordon Henriksen572742e2008-01-07 02:31:11 +000038 };
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000039
Gordon Henriksen572742e2008-01-07 02:31:11 +000040}
41
Gordon Henriksenc317a602008-08-17 12:08:44 +000042static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
43Y("ocaml", "ocaml 3.10-compatible collector");
44
Gordon Henriksen5eca0752008-08-17 18:44:35 +000045void llvm::linkOcamlGCPrinter() { }
Gordon Henriksen572742e2008-01-07 02:31:11 +000046
Chris Lattner7d73c7f2010-04-04 07:39:04 +000047static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
Gordon Henriksen572742e2008-01-07 02:31:11 +000048 const std::string &MId = M.getModuleIdentifier();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000049
Chris Lattner7d73c7f2010-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 Glushenkovdd2ad842010-07-01 01:00:22 +000056
Gordon Henriksen572742e2008-01-07 02:31:11 +000057 // Capitalize the first letter of the module name.
Chris Lattner7d73c7f2010-04-04 07:39:04 +000058 SymName[Letter] = toupper(SymName[Letter]);
Mikhail Glushenkovdd2ad842010-07-01 01:00:22 +000059
Chris Lattner7d73c7f2010-04-04 07:39:04 +000060 SmallString<128> TmpStr;
61 AP.Mang->getNameWithPrefix(TmpStr, SymName);
Mikhail Glushenkovdd2ad842010-07-01 01:00:22 +000062
Chris Lattner7d73c7f2010-04-04 07:39:04 +000063 MCSymbol *Sym = AP.OutContext.GetOrCreateSymbol(TmpStr);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000064
Chris Lattner7d73c7f2010-04-04 07:39:04 +000065 AP.OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
66 AP.OutStreamer.EmitLabel(Sym);
Gordon Henriksen572742e2008-01-07 02:31:11 +000067}
68
Chris Lattner7d73c7f2010-04-04 07:39:04 +000069void OcamlGCMetadataPrinter::beginAssembly(AsmPrinter &AP) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +000070 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
Chris Lattner7d73c7f2010-04-04 07:39:04 +000071 EmitCamlGlobal(getModule(), AP, "code_begin");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000072
Chris Lattner6c2f9e12009-08-19 05:49:37 +000073 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattner7d73c7f2010-04-04 07:39:04 +000074 EmitCamlGlobal(getModule(), AP, "data_begin");
Gordon Henriksen572742e2008-01-07 02:31:11 +000075}
76
77/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000078///
Gordon Henriksen572742e2008-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 Glushenkov5c1799b2009-01-16 06:53:46 +000088///
Gordon Henriksen572742e2008-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 Henriksen5eca0752008-08-17 18:44:35 +000091/// either condition is detected in a function which uses the GC.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000092///
Chris Lattner7d73c7f2010-04-04 07:39:04 +000093void OcamlGCMetadataPrinter::finishAssembly(AsmPrinter &AP) {
94 unsigned IntPtrSize = AP.TM.getTargetData()->getPointerSize();
Gordon Henriksen572742e2008-01-07 02:31:11 +000095
Chris Lattner6c2f9e12009-08-19 05:49:37 +000096 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getTextSection());
Chris Lattner7d73c7f2010-04-04 07:39:04 +000097 EmitCamlGlobal(getModule(), AP, "code_end");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000098
Chris Lattner6c2f9e12009-08-19 05:49:37 +000099 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000100 EmitCamlGlobal(getModule(), AP, "data_end");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000101
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000102 // FIXME: Why does ocaml emit this??
103 AP.OutStreamer.EmitIntValue(0, IntPtrSize, 0);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000104
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000105 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getDataSection());
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000106 EmitCamlGlobal(getModule(), AP, "frametable");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000107
Nicolas Geoffray3816c252010-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 Henriksen5eca0752008-08-17 18:44:35 +0000123 for (iterator I = begin(), IE = end(); I != IE; ++I) {
124 GCFunctionInfo &FI = **I;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000125
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000126 uint64_t FrameSize = FI.getFrameSize();
127 if (FrameSize >= 1<<16) {
Benjamin Kramer1bd73352010-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 Henriksen5eca0752008-08-17 18:44:35 +0000133 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000134
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000135 AP.OutStreamer.AddComment("live roots for " +
136 Twine(FI.getFunction().getName()));
137 AP.OutStreamer.AddBlankLine();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000138
Gordon Henriksen5eca0752008-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 Henriksen08db7362008-08-09 03:48:46 +0000141 if (LiveCount >= 1<<16) {
Benjamin Kramer1bd73352010-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 Henriksen572742e2008-01-07 02:31:11 +0000146 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000147
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000148 AP.OutStreamer.EmitSymbolValue(J->Label, IntPtrSize, 0);
Gordon Henriksen572742e2008-01-07 02:31:11 +0000149 AP.EmitInt16(FrameSize);
Gordon Henriksen572742e2008-01-07 02:31:11 +0000150 AP.EmitInt16(LiveCount);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000151
Gordon Henriksen5eca0752008-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 Geoffray3816c252010-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 Henriksen572742e2008-01-07 02:31:11 +0000161 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000162
Chris Lattner7d73c7f2010-04-04 07:39:04 +0000163 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
Gordon Henriksen572742e2008-01-07 02:31:11 +0000164 }
165 }
166}