OcamlGCPrinter.cpp revision 61fc6c80cf9d50c19fe46decedc592251918d78d
1//===-- OcamlGCPrinter.cpp - Ocaml frametable emitter ---------------------===//
2//
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//
10// This file implements printing the assembly code for an Ocaml frametable.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/GCs.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/GCMetadataPrinter.h"
17#include "llvm/Module.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/raw_ostream.h"
20#include "llvm/Target/TargetAsmInfo.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetMachine.h"
23
24using namespace llvm;
25
26namespace {
27
28  class VISIBILITY_HIDDEN OcamlGCMetadataPrinter : public GCMetadataPrinter {
29  public:
30    void beginAssembly(raw_ostream &OS, AsmPrinter &AP,
31                       const TargetAsmInfo &TAI);
32
33    void finishAssembly(raw_ostream &OS, AsmPrinter &AP,
34                        const TargetAsmInfo &TAI);
35  };
36
37}
38
39static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
40Y("ocaml", "ocaml 3.10-compatible collector");
41
42void llvm::linkOcamlGCPrinter() { }
43
44static void EmitCamlGlobal(const Module &M, raw_ostream &OS, AsmPrinter &AP,
45                           const TargetAsmInfo &TAI, const char *Id) {
46  const std::string &MId = M.getModuleIdentifier();
47
48  std::string Mangled;
49  Mangled += TAI.getGlobalPrefix();
50  Mangled += "caml";
51  size_t Letter = Mangled.size();
52  Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
53  Mangled += "__";
54  Mangled += Id;
55
56  // Capitalize the first letter of the module name.
57  Mangled[Letter] = toupper(Mangled[Letter]);
58
59  if (const char *GlobalDirective = TAI.getGlobalDirective())
60    OS << GlobalDirective << Mangled << "\n";
61  OS << Mangled << ":\n";
62}
63
64void OcamlGCMetadataPrinter::beginAssembly(raw_ostream &OS, AsmPrinter &AP,
65                                           const TargetAsmInfo &TAI) {
66  AP.SwitchToSection(TAI.getTextSection());
67  EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
68
69  AP.SwitchToSection(TAI.getDataSection());
70  EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
71}
72
73/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
74///
75///   extern "C" struct align(sizeof(intptr_t)) {
76///     uint16_t NumDescriptors;
77///     struct align(sizeof(intptr_t)) {
78///       void *ReturnAddress;
79///       uint16_t FrameSize;
80///       uint16_t NumLiveOffsets;
81///       uint16_t LiveOffsets[NumLiveOffsets];
82///     } Descriptors[NumDescriptors];
83///   } caml${module}__frametable;
84///
85/// Note that this precludes programs from stack frames larger than 64K
86/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
87/// either condition is detected in a function which uses the GC.
88///
89void OcamlGCMetadataPrinter::finishAssembly(raw_ostream &OS, AsmPrinter &AP,
90                                            const TargetAsmInfo &TAI) {
91  const char *AddressDirective;
92  int AddressAlignLog;
93  if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
94    AddressDirective = TAI.getData32bitsDirective();
95    AddressAlignLog = 2;
96  } else {
97    AddressDirective = TAI.getData64bitsDirective();
98    AddressAlignLog = 3;
99  }
100
101  AP.SwitchToSection(TAI.getTextSection());
102  EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
103
104  AP.SwitchToSection(TAI.getDataSection());
105  EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
106
107  OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
108  AP.EOL();
109
110  AP.SwitchToSection(TAI.getDataSection());
111  EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
112
113  for (iterator I = begin(), IE = end(); I != IE; ++I) {
114    GCFunctionInfo &FI = **I;
115
116    uint64_t FrameSize = FI.getFrameSize();
117    if (FrameSize >= 1<<16) {
118      cerr << "Function '" << FI.getFunction().getNameStart()
119           << "' is too large for the ocaml GC! "
120           << "Frame size " << FrameSize << " >= 65536.\n";
121      cerr << "(" << uintptr_t(&FI) << ")\n";
122      abort(); // Very rude!
123    }
124
125    OS << "\t" << TAI.getCommentString() << " live roots for "
126       << FI.getFunction().getNameStart() << "\n";
127
128    for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; ++J) {
129      size_t LiveCount = FI.live_size(J);
130      if (LiveCount >= 1<<16) {
131        cerr << "Function '" << FI.getFunction().getNameStart()
132             << "' is too large for the ocaml GC! "
133             << "Live root count " << LiveCount << " >= 65536.\n";
134        abort(); // Very rude!
135      }
136
137      OS << AddressDirective
138         << TAI.getPrivateGlobalPrefix() << "label" << J->Num;
139      AP.EOL("call return address");
140
141      AP.EmitInt16(FrameSize);
142      AP.EOL("stack frame size");
143
144      AP.EmitInt16(LiveCount);
145      AP.EOL("live root count");
146
147      for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
148                                         KE = FI.live_end(J); K != KE; ++K) {
149        assert(K->StackOffset < 1<<16 &&
150               "GC root stack offset is outside of fixed stack frame and out "
151               "of range for ocaml GC!");
152
153        OS << "\t.word\t" << K->StackOffset;
154        AP.EOL("stack offset");
155      }
156
157      AP.EmitAlignment(AddressAlignLog);
158    }
159  }
160}
161