Implement PIC for arm-linux.
[oota-llvm.git] / lib / Target / ARM / ARMConstantPoolValue.cpp
1 //===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARM specific constantpool value class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMConstantPoolValue.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Type.h"
18 using namespace llvm;
19
20 ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv, unsigned id,
21                                            ARMCP::ARMCPKind k,
22                                            unsigned char PCAdj,
23                                            const char *Modif)
24   : MachineConstantPoolValue((const Type*)gv->getType()),
25     GV(gv), S(NULL), LabelId(id), Kind(k), PCAdjust(PCAdj),
26     Modifier(Modif) {}
27
28 ARMConstantPoolValue::ARMConstantPoolValue(const char *s, unsigned id,
29                                            ARMCP::ARMCPKind k,
30                                            unsigned char PCAdj,
31                                            const char *Modif)
32   : MachineConstantPoolValue((const Type*)Type::Int32Ty),
33     GV(NULL), S(s), LabelId(id), Kind(k), PCAdjust(PCAdj),
34     Modifier(Modif) {}
35
36 ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv,
37                                            ARMCP::ARMCPKind k,
38                                            const char *Modif)
39   : MachineConstantPoolValue((const Type*)Type::Int32Ty),
40     GV(gv), S(NULL), LabelId(0), Kind(k), PCAdjust(0),
41     Modifier(Modif) {}
42
43 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
44                                                     unsigned Alignment) {
45   unsigned AlignMask = (1 << Alignment)-1;
46   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
47   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
48     if (Constants[i].isMachineConstantPoolEntry() &&
49         (Constants[i].Offset & AlignMask) == 0) {
50       ARMConstantPoolValue *CPV =
51         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
52       if (CPV->GV == GV &&
53           CPV->S == S &&
54           CPV->LabelId == LabelId &&
55           CPV->Kind == Kind &&
56           CPV->PCAdjust == PCAdjust)
57         return i;
58     }
59   }
60
61   return -1;
62 }
63
64 void
65 ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
66   ID.AddPointer(GV);
67   ID.AddPointer(S);
68   ID.AddInteger(LabelId);
69   ID.AddInteger((unsigned)Kind);
70   ID.AddInteger(PCAdjust);
71 }
72
73 void ARMConstantPoolValue::print(std::ostream &O) const {
74   if (GV)
75     O << GV->getName();
76   else
77     O << S;
78   if (isNonLazyPointer()) O << "$non_lazy_ptr";
79   else if (isStub()) O << "$stub";
80   if (Modifier) O << "(" << Modifier << ")";
81   if (PCAdjust != 0) O << "-(LPIC" << LabelId << "+"
82                        << (unsigned)PCAdjust << ")";
83 }