about summary refs log tree commit diff
path: root/src/rustllvm/RustGCMetadataPrinter.cpp
blob: f002b67224f55770f2080f94468ceecd7e620055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===-- RustGCPrinter.cpp - Rust garbage collection map printer -----------===
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===
//
// This file defines the emitter for the Rust garbage collection stack maps.
//
//===----------------------------------------------------------------------===

#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/CodeGen/GCs.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/GCMetadataPrinter.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
#include <cctype>
#include <map>

using namespace llvm;

namespace {
  enum RustGCMetaType {
    RGCMT_DestIndex,  // Type descriptor index -> type descriptor.
    RGCMT_SrcIndex,   // Value -> type descriptor index.
    RGCMT_Static      // Value with static type descriptor.
  };

  class RustGCMetadataPrinter : public GCMetadataPrinter {
  private:
    std::pair<RustGCMetaType,const Constant *>
      GetGCMetadataForRoot(const GCRoot &Root);
    void EmitGCMetadata(AsmPrinter &AP, MCStreamer &Out, GCRoot &Root);
    bool HandleDestIndex(const GCRoot &Root);
  public:
    void beginAssembly(AsmPrinter &AP) {};
    void finishAssembly(AsmPrinter &AP);
  };

  struct OrderedSymbol {
    unsigned Index;
    MCSymbol *Sym;

    OrderedSymbol(unsigned I, MCSymbol *S) : Index(I), Sym(S) {}

    static OrderedSymbol make(unsigned I, MCSymbol *S) {
      OrderedSymbol OS(I, S);
      return OS;
    }
  };
}

static GCMetadataPrinterRegistry::Add<RustGCMetadataPrinter>
X("rust", "Rust GC metadata printer");


typedef std::vector< std::pair< MCSymbol *,std::vector<GCRoot> > > RootMap;

std::pair<RustGCMetaType,const Constant *>
RustGCMetadataPrinter::GetGCMetadataForRoot(const GCRoot &Root) {
  const GlobalVariable *GCMetaVar =
    cast<const GlobalVariable>(Root.Metadata->stripPointerCasts());

  const Constant *GCMetaInit = GCMetaVar->getInitializer();
  if (isa<ConstantAggregateZero>(GCMetaInit)) {
    // "zeroinitializer": expand to (0, 0).
    IntegerType *I32 = IntegerType::get(GCMetaInit->getContext(), 32);
    ConstantInt *Zero = ConstantInt::get(I32, 0);
    return std::make_pair(RGCMT_DestIndex, Zero);
  }

  const ConstantStruct *GCMeta =
    cast<const ConstantStruct>(GCMetaVar->getInitializer());

  RustGCMetaType GCMetaType = (RustGCMetaType)
    (cast<const ConstantInt>(GCMeta->getOperand(0))->getZExtValue());
  const Constant *Payload = cast<const Constant>(GCMeta->getOperand(1));
  return std::make_pair(GCMetaType, Payload);
}

void RustGCMetadataPrinter::EmitGCMetadata(AsmPrinter &AP, MCStreamer &Out,
                                           GCRoot &Root) {
  int WordSize = AP.TM.getTargetData()->getPointerSize();

  std::pair<RustGCMetaType,const Constant *> Pair =
    GetGCMetadataForRoot(Root);
  const GlobalValue *Tydesc = 0;

  switch (Pair.first) {
  case RGCMT_DestIndex: // Dest index.
    assert(0 && "Dest index should not be here!");
  case RGCMT_SrcIndex:
    // TODO: Use the mapping to find the tydesc frame offset.
    Out.EmitIntValue(1, WordSize, 0);
    Out.EmitIntValue(0, WordSize, 0);
    return;
  case 2: // Static type descriptor.
    Out.EmitIntValue(0, WordSize, 0);
    Tydesc = cast<const GlobalValue>(Pair.second);
    break;
  }

  MCSymbol *TydescSym = AP.Mang->getSymbol(Tydesc);
  Out.EmitSymbolValue(TydescSym, WordSize, 0);
}

// Records the destination index of a type descriptor in the type descriptor
// map, if this GC root is a destination index. Returns true if the GC root is
// a destination index and false otherwise.
bool RustGCMetadataPrinter::HandleDestIndex(const GCRoot &Root) {
  std::pair<RustGCMetaType,const Constant *> Pair =
    GetGCMetadataForRoot(Root);
  return Pair.first == RGCMT_DestIndex; // TODO
}

void RustGCMetadataPrinter::finishAssembly(AsmPrinter &AP) {
  MCStreamer &Out = AP.OutStreamer;

  // Use the data section.
  Out.SwitchSection(AP.getObjFileLowering().getDataSection());

  // Iterate over each function.
  RootMap Map;

  iterator FI = begin(), FE = end();
  while (FI != FE) {
    GCFunctionInfo &GCFI = **FI;

    // Iterate over each safe point.
    GCFunctionInfo::iterator SPI = GCFI.begin(), SPE = GCFI.end();
    while (SPI != SPE) {
      std::vector<GCRoot> Roots;

      // Iterate over each live root.
      GCFunctionInfo::live_iterator LI = GCFI.live_begin(SPI);
      GCFunctionInfo::live_iterator LE = GCFI.live_end(SPI);
      while (LI != LE) {
        if (!HandleDestIndex(*LI))
          Roots.push_back(*LI);
        ++LI;
      }

      Map.push_back(std::make_pair(SPI->Label, Roots));
      ++SPI;
    }
    ++FI;
  }

  // Write out the map.
  Out.AddBlankLine();

  int WordSize = AP.TM.getTargetData()->getPointerSize();

  MCSymbol *SafePointSym = AP.GetExternalSymbolSymbol("rust_gc_safe_points");
  Out.EmitSymbolAttribute(SafePointSym, MCSA_Global);
  Out.EmitLabel(SafePointSym);
  Out.EmitIntValue(Map.size(), WordSize, 0);

  std::vector<MCSymbol *> FrameMapLabels;

  RootMap::iterator MI = Map.begin(), ME = Map.end();
  unsigned i = 0;
  while (MI != ME) {
    Out.EmitSymbolValue(MI->first, WordSize, 0);
    MCSymbol *FrameMapLabel = AP.GetTempSymbol("rust_frame_map_label", i);
    Out.EmitSymbolValue(FrameMapLabel, WordSize, 0);
    FrameMapLabels.push_back(FrameMapLabel);
    ++MI, ++i;
  }

  MI = Map.begin(), i = 0;
  while (MI != ME) {
    Out.EmitLabel(FrameMapLabels[i]);

    std::vector<GCRoot> &Roots = MI->second;
    Out.EmitIntValue(Roots.size(), WordSize, 0);

    std::vector<GCRoot>::iterator RI = Roots.begin(), RE = Roots.end();
    while (RI != RE) {
      Out.EmitIntValue(RI->StackOffset, WordSize, 0);
      EmitGCMetadata(AP, Out, *RI);
      ++RI;
    }

    ++MI, ++i;
  }
}