blob: dafad915bb65afd0985a10ca73c0635ae4a359ba [file] [log] [blame]
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001//===-- 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
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000024#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000025#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000026using namespace llvm;
27
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000028AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
29: FunctionNumber(0), O(o), TM(tm),
30 CommentString("#"),
31 GlobalPrefix(""),
32 PrivateGlobalPrefix("."),
33 GlobalVarAddrPrefix(""),
34 GlobalVarAddrSuffix(""),
35 FunctionAddrPrefix(""),
36 FunctionAddrSuffix(""),
Chris Lattnera633c3132006-05-05 21:47:05 +000037 InlineAsmStart("#APP"),
38 InlineAsmEnd("#NO_APP"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000040 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000041 AsciiDirective("\t.ascii\t"),
42 AscizDirective("\t.asciz\t"),
43 Data8bitsDirective("\t.byte\t"),
44 Data16bitsDirective("\t.short\t"),
45 Data32bitsDirective("\t.long\t"),
46 Data64bitsDirective("\t.quad\t"),
47 AlignDirective("\t.align\t"),
48 AlignmentIsInBytes(true),
49 SwitchToSectionDirective("\t.section\t"),
Jeff Cohen470f4312006-05-02 03:58:45 +000050 MLSections(false),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000051 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman4ca2ea52006-04-22 18:53:45 +000052 JumpTableSection("\t.section .rodata\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000053 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
54 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
55 LCOMMDirective(0),
56 COMMDirective("\t.comm\t"),
57 COMMDirectiveTakesAlignment(true),
58 HasDotTypeDotSizeDirective(true) {
59}
60
61
Chris Lattner8488ba22006-05-09 04:59:56 +000062/// SwitchToTextSection - Switch to the specified text section of the executable
63/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000064///
Chris Lattner8488ba22006-05-09 04:59:56 +000065void AsmPrinter::SwitchToTextSection(const char *NewSection,
66 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000067 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000068 if (GV && GV->hasSection())
69 NS = GV->getSection();
70 else
71 NS = NewSection;
72
73 // If we're already in this section, we're done.
74 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000075
76 // Microsoft ML/MASM has a fundamentally different approach to handling
77 // sections.
78
79 if (MLSections) {
Chris Lattner8c2bfc02006-05-09 05:24:50 +000080 if (!CurrentSection.empty())
81 O << CurrentSection << "\tends\n\n";
82 CurrentSection = NS;
83 if (!CurrentSection.empty())
84 O << CurrentSection << "\tsegment 'CODE'\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000085 } else {
Chris Lattner8c2bfc02006-05-09 05:24:50 +000086 CurrentSection = NS;
87 if (!CurrentSection.empty())
88 O << CurrentSection << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000089 }
90}
91
Chris Lattner8488ba22006-05-09 04:59:56 +000092/// SwitchToTextSection - Switch to the specified text section of the executable
93/// if we are not already in it!
94///
95void AsmPrinter::SwitchToDataSection(const char *NewSection,
96 const GlobalValue *GV) {
97 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000098 if (GV && GV->hasSection())
99 NS = SwitchToSectionDirective + GV->getSection();
100 else
101 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000102
Chris Lattner6341df82006-05-09 05:23:12 +0000103 // If we're already in this section, we're done.
104 if (CurrentSection == NS) return;
105
Chris Lattner8488ba22006-05-09 04:59:56 +0000106 // Microsoft ML/MASM has a fundamentally different approach to handling
107 // sections.
108
109 if (MLSections) {
Chris Lattner6341df82006-05-09 05:23:12 +0000110 if (!CurrentSection.empty())
111 O << CurrentSection << "\tends\n\n";
112 CurrentSection = NS;
113 if (!CurrentSection.empty())
114 O << CurrentSection << "\tsegment 'DATA'\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000115 } else {
Chris Lattner6341df82006-05-09 05:23:12 +0000116 CurrentSection = NS;
117 if (!CurrentSection.empty())
118 O << CurrentSection << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000119 }
120}
121
122
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000123bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000124 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000125
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000126 if (!M.getModuleInlineAsm().empty())
127 O << CommentString << " Start of file scope inline assembly\n"
128 << M.getModuleInlineAsm()
129 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000130
Chris Lattner8488ba22006-05-09 04:59:56 +0000131 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000132
133 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
134 DebugInfo->AnalyzeModule(M);
135 }
136
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000137 return false;
138}
139
140bool AsmPrinter::doFinalization(Module &M) {
141 delete Mang; Mang = 0;
142 return false;
143}
144
Chris Lattnerbb644e32005-11-21 07:51:36 +0000145void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000146 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000147 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000148 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000149}
150
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000151/// EmitConstantPool - Print to the current output stream assembly
152/// representations of the constants in the constant pool MCP. This is
153/// used to print out constants which have been "spilled to memory" by
154/// the code generator.
155///
156void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000157 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000158 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000159 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000160
Chris Lattner8488ba22006-05-09 04:59:56 +0000161 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000162 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000163 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000164 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000165 << ":\t\t\t\t\t" << CommentString << " ";
166 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000167 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000168 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000169 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000170 unsigned ValEnd = CP[i].Offset + EntSize;
171 // Emit inter-object padding for alignment.
172 EmitZeros(CP[i+1].Offset-ValEnd);
173 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000174 }
175}
176
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000177/// EmitJumpTableInfo - Print assembly representations of the jump tables used
178/// by the current function to the current output stream.
179///
180void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
181 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
182 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000183 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000184
185 // FIXME: someday we need to handle PIC jump tables
186 assert((TM.getRelocationModel() == Reloc::Static ||
187 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
188 "Unhandled relocation model emitting jump table information!");
189
Chris Lattner8488ba22006-05-09 04:59:56 +0000190 SwitchToDataSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000191 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000192 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
193 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
194 << ":\n";
195 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
196 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
197 O << Data32bitsDirective << ' ';
198 printBasicBlockLabel(JTBBs[ii]);
199 O << '\n';
200 }
201 }
202}
203
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000204/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
205/// special global used by LLVM. If so, emit it and return true, otherwise
206/// do nothing and return false.
207bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000208 // Ignore debug and non-emitted data.
209 if (GV->getSection() == "llvm.metadata") return true;
210
211 if (!GV->hasAppendingLinkage()) return false;
212
213 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000214
215 if (GV->getName() == "llvm.used")
216 return true; // No need to emit this at all.
217
Chris Lattner3760e902006-01-12 19:17:23 +0000218 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000219 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000220 EmitAlignment(2, 0);
221 EmitXXStructorList(GV->getInitializer());
222 return true;
223 }
224
Chris Lattner3760e902006-01-12 19:17:23 +0000225 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000226 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000227 EmitAlignment(2, 0);
228 EmitXXStructorList(GV->getInitializer());
229 return true;
230 }
231
232 return false;
233}
234
235/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
236/// function pointers, ignoring the init priority.
237void AsmPrinter::EmitXXStructorList(Constant *List) {
238 // Should be an array of '{ int, void ()* }' structs. The first value is the
239 // init priority, which we ignore.
240 if (!isa<ConstantArray>(List)) return;
241 ConstantArray *InitList = cast<ConstantArray>(List);
242 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
243 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
244 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000245
246 if (CS->getOperand(1)->isNullValue())
247 return; // Found a null terminator, exit printing.
248 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000249 EmitGlobalConstant(CS->getOperand(1));
250 }
251}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000252
Chris Lattnera9b25252006-02-05 01:29:18 +0000253/// getPreferredAlignmentLog - Return the preferred alignment of the
254/// specified global, returned in log form. This includes an explicitly
255/// requested alignment (if the global has one).
256unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000257 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000258 if (GV->getAlignment() > (1U << Alignment))
259 Alignment = Log2_32(GV->getAlignment());
260
Chris Lattnercbab2842006-02-05 01:46:49 +0000261 if (GV->hasInitializer()) {
262 // Always round up alignment of global doubles to 8 bytes.
263 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
264 Alignment = 3;
265 if (Alignment < 4) {
266 // If the global is not external, see if it is large. If so, give it a
267 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000268 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000269 Alignment = 4; // 16-byte alignment.
270 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000271 }
272 return Alignment;
273}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000274
Chris Lattnerbb644e32005-11-21 07:51:36 +0000275// EmitAlignment - Emit an alignment directive to the specified power of two.
276void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000277 if (GV && GV->getAlignment())
278 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000279 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000280 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
281 O << AlignDirective << NumBits << "\n";
282}
283
Chris Lattnerbb644e32005-11-21 07:51:36 +0000284/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000285///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000286void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000287 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000288 if (ZeroDirective) {
289 O << ZeroDirective << NumZeros;
290 if (ZeroDirectiveSuffix)
291 O << ZeroDirectiveSuffix;
292 O << "\n";
293 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000294 for (; NumZeros; --NumZeros)
295 O << Data8bitsDirective << "0\n";
296 }
297 }
298}
299
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000300// Print out the specified constant, without a storage class. Only the
301// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000302void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000303 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000304 O << "0";
305 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
306 assert(CB == ConstantBool::True);
307 O << "1";
308 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
309 if (((CI->getValue() << 32) >> 32) == CI->getValue())
310 O << CI->getValue();
311 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000312 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000313 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
314 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000315 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000316 // This is a constant address for a global variable or function. Use the
317 // name of the variable or function as the address value, possibly
318 // decorating it with GlobalVarAddrPrefix/Suffix or
319 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000320 if (isa<Function>(GV))
321 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000322 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000323 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000324 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000325 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000326 switch(CE->getOpcode()) {
327 case Instruction::GetElementPtr: {
328 // generate a symbolic expression for the byte address
329 const Constant *ptrVal = CE->getOperand(0);
330 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000331 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000332 if (Offset)
333 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000334 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000335 if (Offset > 0)
336 O << ") + " << Offset;
337 else if (Offset < 0)
338 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000339 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000340 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000341 }
342 break;
343 }
344 case Instruction::Cast: {
345 // Support only non-converting or widening casts for now, that is, ones
346 // that do not involve a change in value. This assertion is really gross,
347 // and may not even be a complete check.
348 Constant *Op = CE->getOperand(0);
349 const Type *OpTy = Op->getType(), *Ty = CE->getType();
350
351 // Remember, kids, pointers can be losslessly converted back and forth
352 // into 32-bit or wider integers, regardless of signedness. :-P
353 assert(((isa<PointerType>(OpTy)
354 && (Ty == Type::LongTy || Ty == Type::ULongTy
355 || Ty == Type::IntTy || Ty == Type::UIntTy))
356 || (isa<PointerType>(Ty)
357 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
358 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000359 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000360 && OpTy->isLosslesslyConvertibleTo(Ty))))
361 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000362 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000363 break;
364 }
365 case Instruction::Add:
366 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000367 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000368 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000369 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000370 O << ")";
371 break;
372 default:
373 assert(0 && "Unsupported operator!");
374 }
375 } else {
376 assert(0 && "Unknown constant value!");
377 }
378}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000379
380/// toOctal - Convert the low order bits of X into an octal digit.
381///
382static inline char toOctal(int X) {
383 return (X&7)+'0';
384}
385
Chris Lattner55a6d902005-11-10 18:06:33 +0000386/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000387/// the predicate isString is true.
388///
Chris Lattner55a6d902005-11-10 18:06:33 +0000389static void printAsCString(std::ostream &O, const ConstantArray *CVA,
390 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000391 assert(CVA->isString() && "Array is not string compatible!");
392
393 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000394 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000395 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000396 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000397
398 if (C == '"') {
399 O << "\\\"";
400 } else if (C == '\\') {
401 O << "\\\\";
402 } else if (isprint(C)) {
403 O << C;
404 } else {
405 switch(C) {
406 case '\b': O << "\\b"; break;
407 case '\f': O << "\\f"; break;
408 case '\n': O << "\\n"; break;
409 case '\r': O << "\\r"; break;
410 case '\t': O << "\\t"; break;
411 default:
412 O << '\\';
413 O << toOctal(C >> 6);
414 O << toOctal(C >> 3);
415 O << toOctal(C >> 0);
416 break;
417 }
418 }
419 }
420 O << "\"";
421}
422
Jeff Cohen24a62a92006-05-02 01:16:28 +0000423/// EmitString - Emit a zero-byte-terminated string constant.
424///
425void AsmPrinter::EmitString(const ConstantArray *CVA) const {
426 unsigned NumElts = CVA->getNumOperands();
427 if (AscizDirective && NumElts &&
428 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
429 O << AscizDirective;
430 printAsCString(O, CVA, NumElts-1);
431 } else {
432 O << AsciiDirective;
433 printAsCString(O, CVA, NumElts);
434 }
435 O << "\n";
436}
437
Chris Lattnerbb644e32005-11-21 07:51:36 +0000438/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000439///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000440void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000441 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000442
Chris Lattner61753bf2004-10-16 18:19:26 +0000443 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000444 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000445 return;
446 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
447 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000448 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000449 } else { // Not a string. Print the values in successive locations
450 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000451 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000452 }
453 return;
454 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
455 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000456 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000457 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000458 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
459 const Constant* field = CVS->getOperand(i);
460
461 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000462 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000463 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000464 : cvsLayout->MemberOffsets[i+1])
465 - cvsLayout->MemberOffsets[i]) - fieldSize;
466 sizeSoFar += fieldSize + padSize;
467
468 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000469 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000470
471 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000472 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000473 }
474 assert(sizeSoFar == cvsLayout->StructSize &&
475 "Layout of constant struct may be incorrect!");
476 return;
477 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
478 // FP Constants are printed as integer constants to avoid losing
479 // precision...
480 double Val = CFP->getValue();
481 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000482 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000483 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000484 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000485 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000486 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000487 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000488 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000489 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000490 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000491 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000492 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000493 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000494 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000495 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000496 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000497 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000498 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000499 }
500 return;
501 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000502 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000503 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000504 return;
505 }
506 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
507 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
508 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000509
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000510 if (Data64bitsDirective)
511 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000512 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000513 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000514 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000515 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000516 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000517 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000518 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000519 } else {
520 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000521 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000522 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000523 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000524 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000525 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000526 }
527 return;
528 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000529 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
530 const PackedType *PTy = CP->getType();
531
532 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
533 EmitGlobalConstant(CP->getOperand(I));
534
535 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000536 }
537
538 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000539 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000540 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541 case Type::UByteTyID: case Type::SByteTyID:
542 O << Data8bitsDirective;
543 break;
544 case Type::UShortTyID: case Type::ShortTyID:
545 O << Data16bitsDirective;
546 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000547 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000548 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000549 O << Data64bitsDirective;
550 break;
551 }
552 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000553 case Type::UIntTyID: case Type::IntTyID:
554 O << Data32bitsDirective;
555 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000556 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000557 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
558 O << Data64bitsDirective;
559 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000560 case Type::FloatTyID: case Type::DoubleTyID:
561 assert (0 && "Should have already output floating point constant.");
562 default:
563 assert (0 && "Can't handle printing this type of thing");
564 break;
565 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000566 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000567 O << "\n";
568}
Chris Lattner061d9e22006-01-27 02:10:10 +0000569
570/// printInlineAsm - This method formats and prints the specified machine
571/// instruction that is an inline asm.
572void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000573 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000574 unsigned NumOperands = MI->getNumOperands();
575
576 // Count the number of register definitions.
577 unsigned NumDefs = 0;
578 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
579 assert(NumDefs != NumOperands-1 && "No asm string?");
580
581 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000582
583 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000584 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000585
586 // The variant of the current asmprinter: FIXME: change.
587 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000588
Chris Lattneraa23fa92006-02-01 22:41:11 +0000589 int CurVariant = -1; // The number of the {.|.|.} region we are in.
590 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000591
Chris Lattneraa23fa92006-02-01 22:41:11 +0000592 while (*LastEmitted) {
593 switch (*LastEmitted) {
594 default: {
595 // Not a special case, emit the string section literally.
596 const char *LiteralEnd = LastEmitted+1;
597 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000598 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000599 ++LiteralEnd;
600 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
601 O.write(LastEmitted, LiteralEnd-LastEmitted);
602 LastEmitted = LiteralEnd;
603 break;
604 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000605 case '\n':
606 ++LastEmitted; // Consume newline character.
607 O << "\n\t"; // Indent code with newline.
608 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000609 case '$': {
610 ++LastEmitted; // Consume '$' character.
611 if (*LastEmitted == '$') { // $$ -> $
612 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
613 O << '$';
614 ++LastEmitted; // Consume second '$' character.
615 break;
616 }
617
618 bool HasCurlyBraces = false;
619 if (*LastEmitted == '{') { // ${variable}
620 ++LastEmitted; // Consume '{' character.
621 HasCurlyBraces = true;
622 }
623
624 const char *IDStart = LastEmitted;
625 char *IDEnd;
626 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
627 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
628 std::cerr << "Bad $ operand number in inline asm string: '"
629 << AsmStr << "'\n";
630 exit(1);
631 }
632 LastEmitted = IDEnd;
633
Chris Lattner34f74c12006-02-06 22:17:23 +0000634 char Modifier[2] = { 0, 0 };
635
Chris Lattneraa23fa92006-02-01 22:41:11 +0000636 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000637 // If we have curly braces, check for a modifier character. This
638 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
639 if (*LastEmitted == ':') {
640 ++LastEmitted; // Consume ':' character.
641 if (*LastEmitted == 0) {
642 std::cerr << "Bad ${:} expression in inline asm string: '"
643 << AsmStr << "'\n";
644 exit(1);
645 }
646
647 Modifier[0] = *LastEmitted;
648 ++LastEmitted; // Consume modifier character.
649 }
650
Chris Lattneraa23fa92006-02-01 22:41:11 +0000651 if (*LastEmitted != '}') {
652 std::cerr << "Bad ${} expression in inline asm string: '"
653 << AsmStr << "'\n";
654 exit(1);
655 }
656 ++LastEmitted; // Consume '}' character.
657 }
658
659 if ((unsigned)Val >= NumOperands-1) {
660 std::cerr << "Invalid $ operand number in inline asm string: '"
661 << AsmStr << "'\n";
662 exit(1);
663 }
664
Chris Lattner571d9642006-02-23 19:21:04 +0000665 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000666 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000667 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
668 unsigned OpNo = 1;
669
670 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000671 for (; Val; --Val) {
672 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
673 OpNo += (OpFlags >> 3) + 1;
674 }
Chris Lattner571d9642006-02-23 19:21:04 +0000675
Chris Lattner1d08c652006-02-24 20:21:58 +0000676 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000677 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000678
679 bool Error;
680 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
681 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
682 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
683 Modifier[0] ? Modifier : 0);
684 } else {
685 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
686 Modifier[0] ? Modifier : 0);
687 }
688 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000689 std::cerr << "Invalid operand found in inline asm: '"
690 << AsmStr << "'\n";
691 MI->dump();
692 exit(1);
693 }
Chris Lattner571d9642006-02-23 19:21:04 +0000694 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000695 break;
696 }
697 case '{':
698 ++LastEmitted; // Consume '{' character.
699 if (CurVariant != -1) {
700 std::cerr << "Nested variants found in inline asm string: '"
701 << AsmStr << "'\n";
702 exit(1);
703 }
704 CurVariant = 0; // We're in the first variant now.
705 break;
706 case '|':
707 ++LastEmitted; // consume '|' character.
708 if (CurVariant == -1) {
709 std::cerr << "Found '|' character outside of variant in inline asm "
710 << "string: '" << AsmStr << "'\n";
711 exit(1);
712 }
713 ++CurVariant; // We're in the next variant.
714 break;
715 case '}':
716 ++LastEmitted; // consume '}' character.
717 if (CurVariant == -1) {
718 std::cerr << "Found '}' character outside of variant in inline asm "
719 << "string: '" << AsmStr << "'\n";
720 exit(1);
721 }
722 CurVariant = -1;
723 break;
724 }
725 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000726 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000727}
728
729/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
730/// instruction, using the specified assembler variant. Targets should
731/// overried this to format as appropriate.
732bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000733 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000734 // Target doesn't support this yet!
735 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000736}
Chris Lattner1d08c652006-02-24 20:21:58 +0000737
738bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
739 unsigned AsmVariant,
740 const char *ExtraCode) {
741 // Target doesn't support this yet!
742 return true;
743}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000744
745/// printBasicBlockLabel - This method prints the label for the specified
746/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000747void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
748 bool printColon,
749 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000750 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
751 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000752 if (printColon)
753 O << ':';
754 if (printComment)
755 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000756}