Add support for targets without a .zero directive
[oota-llvm.git] / lib / CodeGen / AsmPrinter.cpp
1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AsmPrinter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Instruction.h"
17 #include "llvm/Support/Mangler.h"
18 #include "llvm/Target/TargetMachine.h"
19 using namespace llvm;
20
21 bool AsmPrinter::doInitialization(Module &M) {
22   Mang = new Mangler(M, GlobalPrefix);
23   return false;
24 }
25
26 bool AsmPrinter::doFinalization(Module &M) {
27   delete Mang; Mang = 0;
28   return false;
29 }
30
31 void AsmPrinter::setupMachineFunction(MachineFunction &MF) {
32   // What's my mangled name?
33   CurrentFnName = Mang->getValueName((Value*)MF.getFunction());
34 }
35
36 // emitAlignment - Emit an alignment directive to the specified power of two.
37 void AsmPrinter::emitAlignment(unsigned NumBits) const {
38   if (AlignmentIsInBytes) NumBits = 1 << NumBits;
39   O << AlignDirective << NumBits << "\n";
40 }
41
42 /// emitZeros - Emit a block of zeros.
43 ///
44 void AsmPrinter::emitZeros(unsigned NumZeros) const {
45   if (NumZeros) {
46     if (ZeroDirective)
47       O << ZeroDirective << NumZeros << "\n";
48     else {
49       if (NumZeros >= 8 && Data64bitsDirective) {
50         for (; NumZeros >= 8; NumZeros -= 8)
51           O << Data64bitsDirective << "0\n";
52       }
53       for (; NumZeros >= 4; NumZeros -= 4)
54         O << Data32bitsDirective << "0\n";
55       for (; NumZeros; --NumZeros)
56         O << Data8bitsDirective << "0\n";
57     }
58   }
59 }
60
61 // Print out the specified constant, without a storage class.  Only the
62 // constants valid in constant expressions can occur here.
63 void AsmPrinter::emitConstantValueOnly(const Constant *CV) {
64   if (CV->isNullValue())
65     O << "0";
66   else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
67     assert(CB == ConstantBool::True);
68     O << "1";
69   } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
70     if (((CI->getValue() << 32) >> 32) == CI->getValue())
71       O << CI->getValue();
72     else
73       O << (unsigned long long)CI->getValue();
74   else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
75     O << CI->getValue();
76   else if (isa<GlobalValue>((Value*)CV))
77     // This is a constant address for a global variable or function.  Use the
78     // name of the variable or function as the address value.
79     O << Mang->getValueName(CV);
80   else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
81     const TargetData &TD = TM.getTargetData();
82     switch(CE->getOpcode()) {
83     case Instruction::GetElementPtr: {
84       // generate a symbolic expression for the byte address
85       const Constant *ptrVal = CE->getOperand(0);
86       std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
87       if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
88         O << "(";
89         emitConstantValueOnly(ptrVal);
90         O << ") + " << Offset;
91       } else {
92         emitConstantValueOnly(ptrVal);
93       }
94       break;
95     }
96     case Instruction::Cast: {
97       // Support only non-converting or widening casts for now, that is, ones
98       // that do not involve a change in value.  This assertion is really gross,
99       // and may not even be a complete check.
100       Constant *Op = CE->getOperand(0);
101       const Type *OpTy = Op->getType(), *Ty = CE->getType();
102
103       // Remember, kids, pointers can be losslessly converted back and forth
104       // into 32-bit or wider integers, regardless of signedness. :-P
105       assert(((isa<PointerType>(OpTy)
106                && (Ty == Type::LongTy || Ty == Type::ULongTy
107                    || Ty == Type::IntTy || Ty == Type::UIntTy))
108               || (isa<PointerType>(Ty)
109                   && (OpTy == Type::LongTy || OpTy == Type::ULongTy
110                       || OpTy == Type::IntTy || OpTy == Type::UIntTy))
111               || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
112                    && OpTy->isLosslesslyConvertibleTo(Ty))))
113              && "FIXME: Don't yet support this kind of constant cast expr");
114       O << "(";
115       emitConstantValueOnly(Op);
116       O << ")";
117       break;
118     }
119     case Instruction::Add:
120       O << "(";
121       emitConstantValueOnly(CE->getOperand(0));
122       O << ") + (";
123       emitConstantValueOnly(CE->getOperand(1));
124       O << ")";
125       break;
126     default:
127       assert(0 && "Unsupported operator!");
128     }
129   } else {
130     assert(0 && "Unknown constant value!");
131   }
132 }
133
134 /// toOctal - Convert the low order bits of X into an octal digit.
135 ///
136 static inline char toOctal(int X) {
137   return (X&7)+'0';
138 }
139
140 /// getAsCString - Return the specified array as a C compatible string, only if
141 /// the predicate isString is true.
142 ///
143 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
144   assert(CVA->isString() && "Array is not string compatible!");
145
146   O << "\"";
147   for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
148     unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
149
150     if (C == '"') {
151       O << "\\\"";
152     } else if (C == '\\') {
153       O << "\\\\";
154     } else if (isprint(C)) {
155       O << C;
156     } else {
157       switch(C) {
158       case '\b': O << "\\b"; break;
159       case '\f': O << "\\f"; break;
160       case '\n': O << "\\n"; break;
161       case '\r': O << "\\r"; break;
162       case '\t': O << "\\t"; break;
163       default:
164         O << '\\';
165         O << toOctal(C >> 6);
166         O << toOctal(C >> 3);
167         O << toOctal(C >> 0);
168         break;
169       }
170     }
171   }
172   O << "\"";
173 }
174
175 /// emitGlobalConstant - Print a general LLVM constant to the .s file.
176 ///
177 void AsmPrinter::emitGlobalConstant(const Constant *CV) {  
178   const TargetData &TD = TM.getTargetData();
179
180   if (CV->isNullValue()) {
181     emitZeros(TD.getTypeSize(CV->getType()));
182     return;
183   } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
184     if (CVA->isString()) {
185       O << AsciiDirective;
186       printAsCString(O, CVA);
187       O << "\n";
188     } else { // Not a string.  Print the values in successive locations
189       for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
190         emitGlobalConstant(CVA->getOperand(i));
191     }
192     return;
193   } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
194     // Print the fields in successive locations. Pad to align if needed!
195     const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
196     unsigned sizeSoFar = 0;
197     for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
198       const Constant* field = CVS->getOperand(i);
199
200       // Check if padding is needed and insert one or more 0s.
201       unsigned fieldSize = TD.getTypeSize(field->getType());
202       unsigned padSize = ((i == e-1? cvsLayout->StructSize
203                            : cvsLayout->MemberOffsets[i+1])
204                           - cvsLayout->MemberOffsets[i]) - fieldSize;
205       sizeSoFar += fieldSize + padSize;
206
207       // Now print the actual field value
208       emitGlobalConstant(field);
209
210       // Insert the field padding unless it's zero bytes...
211       emitZeros(padSize);
212     }
213     assert(sizeSoFar == cvsLayout->StructSize &&
214            "Layout of constant struct may be incorrect!");
215     return;
216   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
217     // FP Constants are printed as integer constants to avoid losing
218     // precision...
219     double Val = CFP->getValue();
220     if (CFP->getType() == Type::DoubleTy) {
221       union DU {                            // Abide by C TBAA rules
222         double FVal;
223         uint64_t UVal;
224       } U;
225       U.FVal = Val;
226
227       if (Data64bitsDirective)
228         O << Data64bitsDirective << U.UVal << "\t" << CommentChar
229           << " double value: " << Val << "\n";
230       else if (TD.isBigEndian()) {
231         O << Data32bitsDirective << unsigned(U.UVal >> 32)
232           << "\t" << CommentChar << " double most significant word "
233           << Val << "\n";
234         O << Data32bitsDirective << unsigned(U.UVal)
235           << "\t" << CommentChar << " double least significant word "
236           << Val << "\n";
237       } else {
238         O << Data32bitsDirective << unsigned(U.UVal)
239           << "\t" << CommentChar << " double least significant word " << Val
240           << "\n";
241         O << Data32bitsDirective << unsigned(U.UVal >> 32)
242           << "\t" << CommentChar << " double most significant word " << Val
243           << "\n";
244       }
245       return;
246     } else {
247       union FU {                            // Abide by C TBAA rules
248         float FVal;
249         int32_t UVal;
250       } U;
251       U.FVal = Val;
252       
253       O << Data32bitsDirective << U.UVal << "\t" << CommentChar
254         << " float " << Val << "\n";
255       return;
256     }
257   } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
258     if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
259       uint64_t Val = CI->getRawValue();
260         
261       if (Data64bitsDirective)
262         O << Data64bitsDirective << Val << "\n";
263       else if (TD.isBigEndian()) {
264         O << Data32bitsDirective << unsigned(Val >> 32)
265           << "\t" << CommentChar << " Double-word most significant word "
266           << Val << "\n";
267         O << Data32bitsDirective << unsigned(Val)
268           << "\t" << CommentChar << " Double-word least significant word "
269           << Val << "\n";
270       } else {
271         O << Data32bitsDirective << unsigned(Val)
272           << "\t" << CommentChar << " Double-word least significant word "
273           << Val << "\n";
274         O << Data32bitsDirective << unsigned(Val >> 32)
275           << "\t" << CommentChar << " Double-word most significant word "
276           << Val << "\n";
277       }
278       return;
279     }
280   }
281
282   const Type *type = CV->getType();
283   switch (type->getTypeID()) {
284   case Type::UByteTyID: case Type::SByteTyID:
285     O << Data8bitsDirective;
286     break;
287   case Type::UShortTyID: case Type::ShortTyID:
288     O << Data16bitsDirective;
289     break;
290   case Type::BoolTyID: 
291   case Type::PointerTyID:
292   case Type::UIntTyID: case Type::IntTyID:
293     O << Data32bitsDirective;
294     break;
295   case Type::ULongTyID: case Type::LongTyID:    
296     assert (0 && "Should have already output double-word constant.");
297   case Type::FloatTyID: case Type::DoubleTyID:
298     assert (0 && "Should have already output floating point constant.");
299   default:
300     assert (0 && "Can't handle printing this type of thing");
301     break;
302   }
303   emitConstantValueOnly(CV);
304   O << "\n";
305 }