|
7 | 7 | #include "llvm/BinaryFormat/ELF.h"
|
8 | 8 | #include "llvm/IR/Constant.h"
|
9 | 9 | #include "llvm/IR/Constants.h"
|
| 10 | +#include "llvm/IR/GlobalValue.h" |
10 | 11 | #include "llvm/IR/InstrTypes.h"
|
11 | 12 | #include "llvm/IR/Instructions.h"
|
12 | 13 | #include "llvm/IR/Module.h"
|
@@ -56,6 +57,7 @@ enum OperandKind {
|
56 | 57 | Function,
|
57 | 58 | Block,
|
58 | 59 | Arg,
|
| 60 | + Global, |
59 | 61 | UnimplementedOperand = 255,
|
60 | 62 | };
|
61 | 63 |
|
@@ -128,6 +130,7 @@ class YkIRWriter {
|
128 | 130 |
|
129 | 131 | vector<llvm::Type *> Types;
|
130 | 132 | vector<llvm::Constant *> Constants;
|
| 133 | + vector<llvm::GlobalVariable *> Globals; |
131 | 134 |
|
132 | 135 | // Return the index of the LLVM type `Ty`, inserting a new entry if
|
133 | 136 | // necessary.
|
@@ -164,6 +167,19 @@ class YkIRWriter {
|
164 | 167 | return Idx;
|
165 | 168 | }
|
166 | 169 |
|
| 170 | + // Return the index of the LLVM global `G`, inserting a new entry if |
| 171 | + // necessary. |
| 172 | + size_t globalIndex(class GlobalVariable *G) { |
| 173 | + vector<class GlobalVariable *>::iterator Found = |
| 174 | + std::find(Globals.begin(), Globals.end(), G); |
| 175 | + if (Found != Globals.end()) { |
| 176 | + return std::distance(Globals.begin(), Found); |
| 177 | + } |
| 178 | + size_t Idx = Globals.size(); |
| 179 | + Globals.push_back(G); |
| 180 | + return Idx; |
| 181 | + } |
| 182 | + |
167 | 183 | size_t functionIndex(llvm::Function *F) {
|
168 | 184 | // FIXME: For now we assume that function indicies in LLVM IR and our IR
|
169 | 185 | // are the same.
|
@@ -221,9 +237,16 @@ class YkIRWriter {
|
221 | 237 | OutStreamer.emitSizeT(A->getArgNo());
|
222 | 238 | }
|
223 | 239 |
|
| 240 | + void serialiseGlobalOperand(GlobalVariable *G) { |
| 241 | + OutStreamer.emitInt8(OperandKind::Global); |
| 242 | + OutStreamer.emitSizeT(globalIndex(G)); |
| 243 | + } |
| 244 | + |
224 | 245 | void serialiseOperand(Instruction *Parent, ValueLoweringMap &VLMap,
|
225 | 246 | Value *V) {
|
226 |
| - if (llvm::Function *F = dyn_cast<llvm::Function>(V)) { |
| 247 | + if (llvm::GlobalVariable *G = dyn_cast<llvm::GlobalVariable>(V)) { |
| 248 | + serialiseGlobalOperand(G); |
| 249 | + } else if (llvm::Function *F = dyn_cast<llvm::Function>(V)) { |
227 | 250 | serialiseFunctionOperand(F);
|
228 | 251 | } else if (llvm::Constant *C = dyn_cast<llvm::Constant>(V)) {
|
229 | 252 | serialiseConstantOperand(Parent, C);
|
@@ -511,6 +534,11 @@ class YkIRWriter {
|
511 | 534 | }
|
512 | 535 | }
|
513 | 536 |
|
| 537 | + void serialiseGlobal(class GlobalVariable *G) { |
| 538 | + OutStreamer.emitInt8(G->isThreadLocal()); |
| 539 | + serialiseString(G->getName()); |
| 540 | + } |
| 541 | + |
514 | 542 | public:
|
515 | 543 | YkIRWriter(Module &M, MCStreamer &OutStreamer)
|
516 | 544 | : M(M), OutStreamer(OutStreamer), DL(&M) {}
|
@@ -542,6 +570,13 @@ class YkIRWriter {
|
542 | 570 | serialiseConstant(C);
|
543 | 571 | }
|
544 | 572 |
|
| 573 | + // num_globals: |
| 574 | + OutStreamer.emitSizeT(Globals.size()); |
| 575 | + // globals: |
| 576 | + for (class GlobalVariable *&G : Globals) { |
| 577 | + serialiseGlobal(G); |
| 578 | + } |
| 579 | + |
545 | 580 | // num_types:
|
546 | 581 | OutStreamer.emitSizeT(Types.size());
|
547 | 582 | // types:
|
|
0 commit comments