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