blob: 1b25b015fa5253ba35f46841bc0009142b9b8e55 [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"
Bill Wendling5c3966a2006-11-29 00:39:47 +000023#include "llvm/Support/Streams.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000026#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000027#include "llvm/Target/TargetMachine.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000028#include <ostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000029#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000030using namespace llvm;
31
Jim Laskey261779b2006-09-07 22:06:40 +000032AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
33 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000034: FunctionNumber(0), O(o), TM(tm), TAI(T)
35{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000036
Chris Lattnerdc822412006-10-05 02:42:47 +000037std::string AsmPrinter::getSectionForFunction(const Function &F) const {
38 return TAI->getTextSection();
39}
40
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000041
Chris Lattner8488ba22006-05-09 04:59:56 +000042/// SwitchToTextSection - Switch to the specified text section of the executable
43/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000044///
Chris Lattner8488ba22006-05-09 04:59:56 +000045void AsmPrinter::SwitchToTextSection(const char *NewSection,
46 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000047 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000048 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000049 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000050 else
51 NS = NewSection;
52
53 // If we're already in this section, we're done.
54 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000055
Chris Lattner4ebc6a22006-05-09 05:33:48 +000056 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000057 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
58 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000059
Chris Lattner4ebc6a22006-05-09 05:33:48 +000060 CurrentSection = NS;
61
62 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000063 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000064}
65
Nate Begeman78756502006-07-27 01:13:04 +000066/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000067/// if we are not already in it!
68///
69void AsmPrinter::SwitchToDataSection(const char *NewSection,
70 const GlobalValue *GV) {
71 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000072 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000073 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000074 else
75 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000076
Chris Lattner6341df82006-05-09 05:23:12 +000077 // If we're already in this section, we're done.
78 if (CurrentSection == NS) return;
79
Chris Lattner4ebc6a22006-05-09 05:33:48 +000080 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000081 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
82 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000083
84 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000085
Chris Lattner4ebc6a22006-05-09 05:33:48 +000086 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000087 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000088}
89
90
Chris Lattner6a8e0f52004-08-16 23:15:22 +000091bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000092 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000093
Chris Lattner00fcdfe2006-01-24 04:16:34 +000094 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000095 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000096 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000097 << "\n" << TAI->getCommentString()
98 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000099
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000100 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000101
102 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
103 DebugInfo->AnalyzeModule(M);
104 }
105
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000106 return false;
107}
108
109bool AsmPrinter::doFinalization(Module &M) {
110 delete Mang; Mang = 0;
111 return false;
112}
113
Chris Lattnerbb644e32005-11-21 07:51:36 +0000114void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000115 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000116 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000117 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000118}
119
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000120/// EmitConstantPool - Print to the current output stream assembly
121/// representations of the constants in the constant pool MCP. This is
122/// used to print out constants which have been "spilled to memory" by
123/// the code generator.
124///
125void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000126 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000127 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000128
129 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
130 // in special sections.
131 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
132 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
133 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
134 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000135 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000136 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000137 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000138 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000139 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000140 TM.getTargetData()->getTypeSize(Ty) == 4)
141 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000142 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000143 TM.getTargetData()->getTypeSize(Ty) == 8)
144 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000145 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000146 TM.getTargetData()->getTypeSize(Ty) == 16)
147 SixteenByteCPs.push_back(std::make_pair(CPE, i));
148 else
149 OtherCPs.push_back(std::make_pair(CPE, i));
150 }
151
152 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000153 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
154 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
155 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
156 SixteenByteCPs);
157 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000158}
159
160void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
161 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
162 if (CP.empty()) return;
163
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000164 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000165 EmitAlignment(Alignment);
166 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000167 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
168 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000169 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
170 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000171 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000172 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000173 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000174 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000175 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000176 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000177 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000178 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000179 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000180 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000181 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000182 }
183}
184
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000185/// EmitJumpTableInfo - Print assembly representations of the jump tables used
186/// by the current function to the current output stream.
187///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000188void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
189 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000190 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
191 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000192 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000193
194 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
195 // and 32 bits for PIC since PIC jump table entries are differences, not
196 // pointers to blocks.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000197 // Use the architecture specific relocation directive, if it is set
198 const char *JTEntryDirective = TAI->getJumpTableDirective();
199 if (!JTEntryDirective)
200 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000201
Nate Begeman78756502006-07-27 01:13:04 +0000202 // Pick the directive to use to print the jump table entries, and switch to
203 // the appropriate section.
204 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner90438232006-10-06 22:50:56 +0000205 TargetLowering *LoweringInfo = TM.getTargetLowering();
206 if (LoweringInfo && LoweringInfo->usesGlobalOffsetTable()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000207 SwitchToDataSection(TAI->getJumpTableDataSection());
Andrew Lenhartha6bbf332006-10-11 04:29:42 +0000208 if (TD->getPointerSize() == 8 && !JTEntryDirective)
Chris Lattner90438232006-10-06 22:50:56 +0000209 JTEntryDirective = TAI->getData64bitsDirective();
210 } else {
211 // In PIC mode, we need to emit the jump table to the same section as the
212 // function body itself, otherwise the label differences won't make sense.
213 const Function *F = MF.getFunction();
214 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
215 }
Nate Begeman78756502006-07-27 01:13:04 +0000216 } else {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000217 SwitchToDataSection(TAI->getJumpTableDataSection());
Nate Begeman78756502006-07-27 01:13:04 +0000218 if (TD->getPointerSize() == 8)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000219 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman78756502006-07-27 01:13:04 +0000220 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000221 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000222
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000223 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000224 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000225
226 // If this jump table was deleted, ignore it.
227 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000228
229 // For PIC codegen, if possible we want to use the SetDirective to reduce
230 // the number of relocations the assembler will generate for the jump table.
231 // Set directives are all printed before the jump table itself.
232 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeya6211dc2006-09-06 18:34:40 +0000233 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman984c1a42006-08-12 21:29:52 +0000234 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
235 if (EmittedSets.insert(JTBBs[ii]).second)
236 printSetLabel(i, JTBBs[ii]);
237
Jim Laskeya6211dc2006-09-06 18:34:40 +0000238 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
239 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000240
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000241 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000242 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000243 // If we have emitted set directives for the jump table entries, print
244 // them rather than the entries themselves. If we're emitting PIC, then
245 // emit the table entries as differences between two text section labels.
246 // If we're emitting non-PIC code, then emit the entries as direct
247 // references to the target basic blocks.
248 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000249 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
250 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman984c1a42006-08-12 21:29:52 +0000251 } else if (TM.getRelocationModel() == Reloc::PIC_) {
252 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000253 //If the arch uses custom Jump Table directives, don't calc relative to JT
254 if (!TAI->getJumpTableDirective())
255 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
256 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000257 } else {
258 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000259 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000260 O << '\n';
261 }
262 }
263}
264
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000265/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
266/// special global used by LLVM. If so, emit it and return true, otherwise
267/// do nothing and return false.
268bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000269 // Ignore debug and non-emitted data.
270 if (GV->getSection() == "llvm.metadata") return true;
271
272 if (!GV->hasAppendingLinkage()) return false;
273
274 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000275
Chris Lattner66af3902006-09-26 03:38:18 +0000276 if (GV->getName() == "llvm.used") {
277 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
278 EmitLLVMUsedList(GV->getInitializer());
279 return true;
280 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000281
Chris Lattner3760e902006-01-12 19:17:23 +0000282 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000283 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000284 EmitAlignment(2, 0);
285 EmitXXStructorList(GV->getInitializer());
286 return true;
287 }
288
Chris Lattner3760e902006-01-12 19:17:23 +0000289 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000290 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000291 EmitAlignment(2, 0);
292 EmitXXStructorList(GV->getInitializer());
293 return true;
294 }
295
296 return false;
297}
298
Chris Lattner66af3902006-09-26 03:38:18 +0000299/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
300/// global in the specified llvm.used list as being used with this directive.
301void AsmPrinter::EmitLLVMUsedList(Constant *List) {
302 const char *Directive = TAI->getUsedDirective();
303
304 // Should be an array of 'sbyte*'.
305 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
306 if (InitList == 0) return;
307
308 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
309 O << Directive;
310 EmitConstantValueOnly(InitList->getOperand(i));
311 O << "\n";
312 }
313}
314
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000315/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
316/// function pointers, ignoring the init priority.
317void AsmPrinter::EmitXXStructorList(Constant *List) {
318 // Should be an array of '{ int, void ()* }' structs. The first value is the
319 // init priority, which we ignore.
320 if (!isa<ConstantArray>(List)) return;
321 ConstantArray *InitList = cast<ConstantArray>(List);
322 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
323 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
324 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000325
326 if (CS->getOperand(1)->isNullValue())
327 return; // Found a null terminator, exit printing.
328 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000329 EmitGlobalConstant(CS->getOperand(1));
330 }
331}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000332
Jim Laskey71262542006-10-17 13:41:07 +0000333/// getGlobalLinkName - Returns the asm/link name of of the specified
334/// global variable. Should be overridden by each target asm printer to
335/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000336const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
337 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000338
339 if (isa<Function>(GV)) {
340 LinkName += TAI->getFunctionAddrPrefix();
341 LinkName += Mang->getValueName(GV);
342 LinkName += TAI->getFunctionAddrSuffix();
343 } else {
344 LinkName += TAI->getGlobalVarAddrPrefix();
345 LinkName += Mang->getValueName(GV);
346 LinkName += TAI->getGlobalVarAddrSuffix();
347 }
348
Jim Laskeyd24b9132006-10-17 17:17:24 +0000349 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000350}
351
Chris Lattnerbb644e32005-11-21 07:51:36 +0000352// EmitAlignment - Emit an alignment directive to the specified power of two.
353void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000354 if (GV && GV->getAlignment())
355 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000356 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000357 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
358 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000359}
360
Chris Lattnerbb644e32005-11-21 07:51:36 +0000361/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000362///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000363void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000364 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000365 if (TAI->getZeroDirective()) {
366 O << TAI->getZeroDirective() << NumZeros;
367 if (TAI->getZeroDirectiveSuffix())
368 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000369 O << "\n";
370 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000371 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000372 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000373 }
374 }
375}
376
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000377// Print out the specified constant, without a storage class. Only the
378// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000379void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000380 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000381 O << "0";
382 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnereea983e2006-09-28 23:17:41 +0000383 assert(CB->getValue());
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000384 O << "1";
Reid Spencere0fc4df2006-10-20 07:07:24 +0000385 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
386 if (CI->getType()->isSigned()) {
387 if (((CI->getSExtValue() << 32) >> 32) == CI->getSExtValue())
388 O << CI->getSExtValue();
389 else
390 O << (uint64_t)CI->getSExtValue();
391 } else
392 O << CI->getZExtValue();
393 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000394 // This is a constant address for a global variable or function. Use the
395 // name of the variable or function as the address value, possibly
396 // decorating it with GlobalVarAddrPrefix/Suffix or
397 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000398 if (isa<Function>(GV)) {
399 O << TAI->getFunctionAddrPrefix()
400 << Mang->getValueName(GV)
401 << TAI->getFunctionAddrSuffix();
402 } else {
403 O << TAI->getGlobalVarAddrPrefix()
404 << Mang->getValueName(GV)
405 << TAI->getGlobalVarAddrSuffix();
406 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000407 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000408 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000409 switch(CE->getOpcode()) {
410 case Instruction::GetElementPtr: {
411 // generate a symbolic expression for the byte address
412 const Constant *ptrVal = CE->getOperand(0);
413 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000414 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000415 if (Offset)
416 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000417 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000418 if (Offset > 0)
419 O << ") + " << Offset;
420 else if (Offset < 0)
421 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000422 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000423 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000424 }
425 break;
426 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000427 case Instruction::Trunc:
428 case Instruction::ZExt:
429 case Instruction::SExt:
430 case Instruction::FPTrunc:
431 case Instruction::FPExt:
432 case Instruction::UIToFP:
433 case Instruction::SIToFP:
434 case Instruction::FPToUI:
435 case Instruction::FPToSI:
436 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
437 break;
438 case Instruction::IntToPtr:
439 case Instruction::PtrToInt:
440 case Instruction::BitCast: {
Chris Lattner85492422006-07-29 01:57:19 +0000441 // Support only foldable casts to/from pointers that can be eliminated by
442 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000443 Constant *Op = CE->getOperand(0);
444 const Type *OpTy = Op->getType(), *Ty = CE->getType();
445
Chris Lattner85492422006-07-29 01:57:19 +0000446 // Handle casts to pointers by changing them into casts to the appropriate
447 // integer type. This promotes constant folding and simplifies this code.
448 if (isa<PointerType>(Ty)) {
449 const Type *IntPtrTy = TD->getIntPtrType();
450 Op = ConstantExpr::getCast(Op, IntPtrTy);
451 return EmitConstantValueOnly(Op);
452 }
453
454 // We know the dest type is not a pointer. Is the src value a pointer or
455 // integral?
456 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
457 // We can emit the pointer value into this slot if the slot is an
458 // integer slot greater or equal to the size of the pointer.
459 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
460 return EmitConstantValueOnly(Op);
461 }
462
463 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000464 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000465 break;
466 }
467 case Instruction::Add:
468 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000469 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000470 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000471 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000472 O << ")";
473 break;
474 default:
475 assert(0 && "Unsupported operator!");
476 }
477 } else {
478 assert(0 && "Unknown constant value!");
479 }
480}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000481
482/// toOctal - Convert the low order bits of X into an octal digit.
483///
484static inline char toOctal(int X) {
485 return (X&7)+'0';
486}
487
Chris Lattner55a6d902005-11-10 18:06:33 +0000488/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000489/// the predicate isString is true.
490///
Chris Lattner55a6d902005-11-10 18:06:33 +0000491static void printAsCString(std::ostream &O, const ConstantArray *CVA,
492 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000493 assert(CVA->isString() && "Array is not string compatible!");
494
495 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000496 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000497 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000498 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000499
500 if (C == '"') {
501 O << "\\\"";
502 } else if (C == '\\') {
503 O << "\\\\";
504 } else if (isprint(C)) {
505 O << C;
506 } else {
507 switch(C) {
508 case '\b': O << "\\b"; break;
509 case '\f': O << "\\f"; break;
510 case '\n': O << "\\n"; break;
511 case '\r': O << "\\r"; break;
512 case '\t': O << "\\t"; break;
513 default:
514 O << '\\';
515 O << toOctal(C >> 6);
516 O << toOctal(C >> 3);
517 O << toOctal(C >> 0);
518 break;
519 }
520 }
521 }
522 O << "\"";
523}
524
Jeff Cohen24a62a92006-05-02 01:16:28 +0000525/// EmitString - Emit a zero-byte-terminated string constant.
526///
527void AsmPrinter::EmitString(const ConstantArray *CVA) const {
528 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000529 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000530 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000531 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000532 printAsCString(O, CVA, NumElts-1);
533 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000534 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000535 printAsCString(O, CVA, NumElts);
536 }
537 O << "\n";
538}
539
Chris Lattnerbb644e32005-11-21 07:51:36 +0000540/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000542void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000543 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000544
Chris Lattner61753bf2004-10-16 18:19:26 +0000545 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000546 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000547 return;
548 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
549 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000550 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000551 } else { // Not a string. Print the values in successive locations
552 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000553 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000554 }
555 return;
556 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
557 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000558 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000559 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000560 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
561 const Constant* field = CVS->getOperand(i);
562
563 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000564 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000565 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000566 : cvsLayout->MemberOffsets[i+1])
567 - cvsLayout->MemberOffsets[i]) - fieldSize;
568 sizeSoFar += fieldSize + padSize;
569
570 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000571 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000572
573 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000574 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000575 }
576 assert(sizeSoFar == cvsLayout->StructSize &&
577 "Layout of constant struct may be incorrect!");
578 return;
579 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
580 // FP Constants are printed as integer constants to avoid losing
581 // precision...
582 double Val = CFP->getValue();
583 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000584 if (TAI->getData64bitsDirective())
585 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
586 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000587 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000588 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
589 << "\t" << TAI->getCommentString()
590 << " double most significant word " << Val << "\n";
591 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
592 << "\t" << TAI->getCommentString()
593 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000594 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000595 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
596 << "\t" << TAI->getCommentString()
597 << " double least significant word " << Val << "\n";
598 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
599 << "\t" << TAI->getCommentString()
600 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000601 }
602 return;
603 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000604 O << TAI->getData32bitsDirective() << FloatToBits(Val)
605 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000606 return;
607 }
608 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
609 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000610 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000611
Jim Laskeya6211dc2006-09-06 18:34:40 +0000612 if (TAI->getData64bitsDirective())
613 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000614 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000615 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
616 << "\t" << TAI->getCommentString()
617 << " Double-word most significant word " << Val << "\n";
618 O << TAI->getData32bitsDirective() << unsigned(Val)
619 << "\t" << TAI->getCommentString()
620 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000621 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000622 O << TAI->getData32bitsDirective() << unsigned(Val)
623 << "\t" << TAI->getCommentString()
624 << " Double-word least significant word " << Val << "\n";
625 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
626 << "\t" << TAI->getCommentString()
627 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000628 }
629 return;
630 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000631 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
632 const PackedType *PTy = CP->getType();
633
634 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
635 EmitGlobalConstant(CP->getOperand(I));
636
637 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000638 }
639
640 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000641 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000642 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000643 O << "\n";
644}
Chris Lattner061d9e22006-01-27 02:10:10 +0000645
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000646void
647AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
648 // Target doesn't support this yet!
649 abort();
650}
651
Chris Lattnera32814b2006-09-26 23:59:50 +0000652/// PrintSpecial - Print information related to the specified machine instr
653/// that is independent of the operand, and may be independent of the instr
654/// itself. This can be useful for portably encoding the comment character
655/// or other bits of target-specific knowledge into the asmstrings. The
656/// syntax used is ${:comment}. Targets can override this to add support
657/// for their own strange codes.
658void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000659 if (!strcmp(Code, "private")) {
660 O << TAI->getPrivateGlobalPrefix();
661 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000662 O << TAI->getCommentString();
663 } else if (!strcmp(Code, "uid")) {
664 // Assign a unique ID to this machine instruction.
665 static const MachineInstr *LastMI = 0;
666 static unsigned Counter = 0U-1;
667 // If this is a new machine instruction, bump the counter.
668 if (LastMI != MI) { ++Counter; LastMI = MI; }
669 O << Counter;
670 } else {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000671 llvm_cerr << "Unknown special formatter '" << Code
Chris Lattnera32814b2006-09-26 23:59:50 +0000672 << "' for machine instr: " << *MI;
673 exit(1);
674 }
675}
676
677
Chris Lattner061d9e22006-01-27 02:10:10 +0000678/// printInlineAsm - This method formats and prints the specified machine
679/// instruction that is an inline asm.
680void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000681 unsigned NumOperands = MI->getNumOperands();
682
683 // Count the number of register definitions.
684 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000685 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
686 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000687 assert(NumDefs != NumOperands-1 && "No asm string?");
688
689 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000690
691 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000692 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000693
Chris Lattner3390cbe2006-07-28 00:17:20 +0000694 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
695 if (AsmStr[0] == 0) {
696 O << "\n"; // Tab already printed, avoid double indenting next instr.
697 return;
698 }
699
Jim Laskeya6211dc2006-09-06 18:34:40 +0000700 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000701
Chris Lattneraa23fa92006-02-01 22:41:11 +0000702 // The variant of the current asmprinter: FIXME: change.
703 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000704
Chris Lattneraa23fa92006-02-01 22:41:11 +0000705 int CurVariant = -1; // The number of the {.|.|.} region we are in.
706 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000707
Chris Lattneraa23fa92006-02-01 22:41:11 +0000708 while (*LastEmitted) {
709 switch (*LastEmitted) {
710 default: {
711 // Not a special case, emit the string section literally.
712 const char *LiteralEnd = LastEmitted+1;
713 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000714 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000715 ++LiteralEnd;
716 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
717 O.write(LastEmitted, LiteralEnd-LastEmitted);
718 LastEmitted = LiteralEnd;
719 break;
720 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000721 case '\n':
722 ++LastEmitted; // Consume newline character.
723 O << "\n\t"; // Indent code with newline.
724 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000725 case '$': {
726 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000727 bool Done = true;
728
729 // Handle escapes.
730 switch (*LastEmitted) {
731 default: Done = false; break;
732 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000733 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
734 O << '$';
735 ++LastEmitted; // Consume second '$' character.
736 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000737 case '(': // $( -> same as GCC's { character.
738 ++LastEmitted; // Consume '(' character.
739 if (CurVariant != -1) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000740 llvm_cerr << "Nested variants found in inline asm string: '"
741 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000742 exit(1);
743 }
744 CurVariant = 0; // We're in the first variant now.
745 break;
746 case '|':
747 ++LastEmitted; // consume '|' character.
748 if (CurVariant == -1) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000749 llvm_cerr << "Found '|' character outside of variant in inline asm "
750 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000751 exit(1);
752 }
753 ++CurVariant; // We're in the next variant.
754 break;
755 case ')': // $) -> same as GCC's } char.
756 ++LastEmitted; // consume ')' character.
757 if (CurVariant == -1) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000758 llvm_cerr << "Found '}' character outside of variant in inline asm "
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000759 << "string: '" << AsmStr << "'\n";
760 exit(1);
761 }
762 CurVariant = -1;
763 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000764 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000765 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000766
767 bool HasCurlyBraces = false;
768 if (*LastEmitted == '{') { // ${variable}
769 ++LastEmitted; // Consume '{' character.
770 HasCurlyBraces = true;
771 }
772
773 const char *IDStart = LastEmitted;
774 char *IDEnd;
775 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
776 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000777 llvm_cerr << "Bad $ operand number in inline asm string: '"
Chris Lattneraa23fa92006-02-01 22:41:11 +0000778 << AsmStr << "'\n";
779 exit(1);
780 }
781 LastEmitted = IDEnd;
782
Chris Lattner34f74c12006-02-06 22:17:23 +0000783 char Modifier[2] = { 0, 0 };
784
Chris Lattneraa23fa92006-02-01 22:41:11 +0000785 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000786 // If we have curly braces, check for a modifier character. This
787 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
788 if (*LastEmitted == ':') {
789 ++LastEmitted; // Consume ':' character.
790 if (*LastEmitted == 0) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000791 llvm_cerr << "Bad ${:} expression in inline asm string: '"
Chris Lattner34f74c12006-02-06 22:17:23 +0000792 << AsmStr << "'\n";
793 exit(1);
794 }
795
796 Modifier[0] = *LastEmitted;
797 ++LastEmitted; // Consume modifier character.
798 }
799
Chris Lattneraa23fa92006-02-01 22:41:11 +0000800 if (*LastEmitted != '}') {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000801 llvm_cerr << "Bad ${} expression in inline asm string: '"
Chris Lattneraa23fa92006-02-01 22:41:11 +0000802 << AsmStr << "'\n";
803 exit(1);
804 }
805 ++LastEmitted; // Consume '}' character.
806 }
807
808 if ((unsigned)Val >= NumOperands-1) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000809 llvm_cerr << "Invalid $ operand number in inline asm string: '"
Chris Lattneraa23fa92006-02-01 22:41:11 +0000810 << AsmStr << "'\n";
811 exit(1);
812 }
813
Chris Lattner571d9642006-02-23 19:21:04 +0000814 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000815 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000816 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
817 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000818
819 bool Error = false;
820
Chris Lattner571d9642006-02-23 19:21:04 +0000821 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000822 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000823 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000824 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
825 OpNo += (OpFlags >> 3) + 1;
826 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000827
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000828 if (OpNo >= MI->getNumOperands()) {
829 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000830 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000831 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
832 ++OpNo; // Skip over the ID number.
833
834 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
835 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
836 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
837 Modifier[0] ? Modifier : 0);
838 } else {
839 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
840 Modifier[0] ? Modifier : 0);
841 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000842 }
843 if (Error) {
Bill Wendling5c3966a2006-11-29 00:39:47 +0000844 llvm_cerr << "Invalid operand found in inline asm: '"
Chris Lattneraa23fa92006-02-01 22:41:11 +0000845 << AsmStr << "'\n";
846 MI->dump();
847 exit(1);
848 }
Chris Lattner571d9642006-02-23 19:21:04 +0000849 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000850 break;
851 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000852 }
853 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000854 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000855}
856
857/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
858/// instruction, using the specified assembler variant. Targets should
859/// overried this to format as appropriate.
860bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000861 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000862 // Target doesn't support this yet!
863 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000864}
Chris Lattner1d08c652006-02-24 20:21:58 +0000865
866bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
867 unsigned AsmVariant,
868 const char *ExtraCode) {
869 // Target doesn't support this yet!
870 return true;
871}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000872
873/// printBasicBlockLabel - This method prints the label for the specified
874/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000875void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
876 bool printColon,
877 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000878 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000879 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000880 if (printColon)
881 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +0000882 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000883 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000884}
Nate Begeman984c1a42006-08-12 21:29:52 +0000885
886/// printSetLabel - This method prints a set label for the specified
887/// MachineBasicBlock
888void AsmPrinter::printSetLabel(unsigned uid,
889 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000890 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000891 return;
892
Jim Laskeya6211dc2006-09-06 18:34:40 +0000893 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
894 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000895 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000896 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000897 << '_' << uid << '\n';
898}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000899
Evan Cheng91f120f2006-11-01 09:23:08 +0000900void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
901 const MachineBasicBlock *MBB) const {
902 if (!TAI->getSetDirective())
903 return;
904
905 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
906 << getFunctionNumber() << '_' << uid << '_' << uid2
907 << "_set_" << MBB->getNumber() << ',';
908 printBasicBlockLabel(MBB, false, false);
909 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
910 << '_' << uid << '_' << uid2 << '\n';
911}
912
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000913/// printDataDirective - This method prints the asm directive for the
914/// specified type.
915void AsmPrinter::printDataDirective(const Type *type) {
916 const TargetData *TD = TM.getTargetData();
917 switch (type->getTypeID()) {
918 case Type::BoolTyID:
919 case Type::UByteTyID: case Type::SByteTyID:
920 O << TAI->getData8bitsDirective();
921 break;
922 case Type::UShortTyID: case Type::ShortTyID:
923 O << TAI->getData16bitsDirective();
924 break;
925 case Type::PointerTyID:
926 if (TD->getPointerSize() == 8) {
927 assert(TAI->getData64bitsDirective() &&
928 "Target cannot handle 64-bit pointer exprs!");
929 O << TAI->getData64bitsDirective();
930 break;
931 }
932 //Fall through for pointer size == int size
933 case Type::UIntTyID: case Type::IntTyID:
934 O << TAI->getData32bitsDirective();
935 break;
936 case Type::ULongTyID: case Type::LongTyID:
937 assert(TAI->getData64bitsDirective() &&
938 "Target cannot handle 64-bit constant exprs!");
939 O << TAI->getData64bitsDirective();
940 break;
941 case Type::FloatTyID: case Type::DoubleTyID:
942 assert (0 && "Should have already output floating point constant.");
943 default:
944 assert (0 && "Can't handle printing this type of thing");
945 break;
946 }
947}