blob: 833ed28f085c4d48dd5d01ef5dd5564f4fd9f239 [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"
Chris Lattneraa23fa92006-02-01 22:41:11 +000028#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000029using namespace llvm;
30
Jim Laskey261779b2006-09-07 22:06:40 +000031AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
32 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000033: FunctionNumber(0), O(o), TM(tm), TAI(T)
34{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000035
Chris Lattnerdc822412006-10-05 02:42:47 +000036std::string AsmPrinter::getSectionForFunction(const Function &F) const {
37 return TAI->getTextSection();
38}
39
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000040
Chris Lattner8488ba22006-05-09 04:59:56 +000041/// SwitchToTextSection - Switch to the specified text section of the executable
42/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000043///
Chris Lattner8488ba22006-05-09 04:59:56 +000044void AsmPrinter::SwitchToTextSection(const char *NewSection,
45 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000046 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000047 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000048 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000049 else
50 NS = NewSection;
51
52 // If we're already in this section, we're done.
53 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000054
Chris Lattner4ebc6a22006-05-09 05:33:48 +000055 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000056 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
57 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000058
Chris Lattner4ebc6a22006-05-09 05:33:48 +000059 CurrentSection = NS;
60
61 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000062 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000063}
64
Nate Begeman78756502006-07-27 01:13:04 +000065/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000066/// if we are not already in it!
67///
68void AsmPrinter::SwitchToDataSection(const char *NewSection,
69 const GlobalValue *GV) {
70 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000071 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000072 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000073 else
74 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000075
Chris Lattner6341df82006-05-09 05:23:12 +000076 // If we're already in this section, we're done.
77 if (CurrentSection == NS) return;
78
Chris Lattner4ebc6a22006-05-09 05:33:48 +000079 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000080 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
81 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000082
83 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000084
Chris Lattner4ebc6a22006-05-09 05:33:48 +000085 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000086 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000087}
88
89
Chris Lattner6a8e0f52004-08-16 23:15:22 +000090bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000091 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000092
Chris Lattner00fcdfe2006-01-24 04:16:34 +000093 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000094 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000095 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000096 << "\n" << TAI->getCommentString()
97 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000098
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +000099 SwitchToDataSection(""); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000100
101 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
102 DebugInfo->AnalyzeModule(M);
103 }
104
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000105 return false;
106}
107
108bool AsmPrinter::doFinalization(Module &M) {
Rafael Espindolad7998d02006-12-18 03:37:18 +0000109 if (TAI->getWeakRefDirective()) {
110 if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
111 SwitchToDataSection("");
112
113 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
114 e = ExtWeakSymbols.end(); i != e; ++i) {
115 const GlobalValue *GV = *i;
116 std::string Name = Mang->getValueName(GV);
117 O << TAI->getWeakRefDirective() << Name << "\n";
118 }
119 }
120
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000121 delete Mang; Mang = 0;
122 return false;
123}
124
Chris Lattnerbb644e32005-11-21 07:51:36 +0000125void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000126 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000127 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000128 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000129}
130
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000131/// EmitConstantPool - Print to the current output stream assembly
132/// representations of the constants in the constant pool MCP. This is
133/// used to print out constants which have been "spilled to memory" by
134/// the code generator.
135///
136void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000137 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000138 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000139
140 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
141 // in special sections.
142 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
143 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
144 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
145 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000146 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000147 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000148 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000149 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000150 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000151 TM.getTargetData()->getTypeSize(Ty) == 4)
152 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000153 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000154 TM.getTargetData()->getTypeSize(Ty) == 8)
155 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000156 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000157 TM.getTargetData()->getTypeSize(Ty) == 16)
158 SixteenByteCPs.push_back(std::make_pair(CPE, i));
159 else
160 OtherCPs.push_back(std::make_pair(CPE, i));
161 }
162
163 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000164 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
165 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
166 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
167 SixteenByteCPs);
168 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000169}
170
171void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
172 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
173 if (CP.empty()) return;
174
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000175 SwitchToDataSection(Section);
Evan Chengec1d60b2006-06-29 00:26:09 +0000176 EmitAlignment(Alignment);
177 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
179 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000180 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
181 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000182 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000183 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000184 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000185 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000186 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000187 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000188 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000189 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000190 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000191 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000192 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000193 }
194}
195
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000196/// EmitJumpTableInfo - Print assembly representations of the jump tables used
197/// by the current function to the current output stream.
198///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000199void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
200 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000201 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
202 if (JT.empty()) return;
Jim Laskey70323a82006-12-14 19:17:33 +0000203 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begemanefc312a2006-07-27 16:46:58 +0000204
Jim Laskey70323a82006-12-14 19:17:33 +0000205 // Use JumpTableDirective otherwise honor the entry size from the jump table
206 // info.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000207 const char *JTEntryDirective = TAI->getJumpTableDirective();
Jim Laskey70323a82006-12-14 19:17:33 +0000208 bool HadJTEntryDirective = JTEntryDirective != NULL;
209 if (!HadJTEntryDirective) {
210 JTEntryDirective = MJTI->getEntrySize() == 4 ?
211 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
212 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000213
Nate Begeman78756502006-07-27 01:13:04 +0000214 // Pick the directive to use to print the jump table entries, and switch to
215 // the appropriate section.
Jim Laskey70323a82006-12-14 19:17:33 +0000216 TargetLowering *LoweringInfo = TM.getTargetLowering();
217
218 if (IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) {
219 // In PIC mode, we need to emit the jump table to the same section as the
220 // function body itself, otherwise the label differences won't make sense.
221 const Function *F = MF.getFunction();
222 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000223 } else {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000224 SwitchToDataSection(TAI->getJumpTableDataSection());
Nate Begeman78756502006-07-27 01:13:04 +0000225 }
Jim Laskey70323a82006-12-14 19:17:33 +0000226
227 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000228
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000229 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000230 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner28bfe382006-10-28 18:10:06 +0000231
232 // If this jump table was deleted, ignore it.
233 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +0000234
235 // For PIC codegen, if possible we want to use the SetDirective to reduce
236 // the number of relocations the assembler will generate for the jump table.
237 // Set directives are all printed before the jump table itself.
238 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskey70323a82006-12-14 19:17:33 +0000239 if (TAI->getSetDirective() && IsPic)
Nate Begeman984c1a42006-08-12 21:29:52 +0000240 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
241 if (EmittedSets.insert(JTBBs[ii]).second)
242 printSetLabel(i, JTBBs[ii]);
243
Jim Laskeya6211dc2006-09-06 18:34:40 +0000244 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
245 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000246
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000247 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000248 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000249 // If we have emitted set directives for the jump table entries, print
250 // them rather than the entries themselves. If we're emitting PIC, then
251 // emit the table entries as differences between two text section labels.
252 // If we're emitting non-PIC code, then emit the entries as direct
253 // references to the target basic blocks.
254 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000255 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
256 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Jim Laskey70323a82006-12-14 19:17:33 +0000257 } else if (IsPic) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000258 printBasicBlockLabel(JTBBs[ii], false, false);
Jim Laskey70323a82006-12-14 19:17:33 +0000259 //If the arch uses custom Jump Table directives, don't calc relative to JT
260 if (!HadJTEntryDirective)
261 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
262 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000263 } else {
264 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000265 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000266 O << '\n';
267 }
268 }
269}
270
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000271/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
272/// special global used by LLVM. If so, emit it and return true, otherwise
273/// do nothing and return false.
274bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000275 // Ignore debug and non-emitted data.
276 if (GV->getSection() == "llvm.metadata") return true;
277
278 if (!GV->hasAppendingLinkage()) return false;
279
280 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000281
Chris Lattner66af3902006-09-26 03:38:18 +0000282 if (GV->getName() == "llvm.used") {
283 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
284 EmitLLVMUsedList(GV->getInitializer());
285 return true;
286 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000287
Chris Lattner3760e902006-01-12 19:17:23 +0000288 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000289 SwitchToDataSection(TAI->getStaticCtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000290 EmitAlignment(2, 0);
291 EmitXXStructorList(GV->getInitializer());
292 return true;
293 }
294
Chris Lattner3760e902006-01-12 19:17:23 +0000295 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000296 SwitchToDataSection(TAI->getStaticDtorsSection());
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000297 EmitAlignment(2, 0);
298 EmitXXStructorList(GV->getInitializer());
299 return true;
300 }
301
302 return false;
303}
304
Chris Lattner66af3902006-09-26 03:38:18 +0000305/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
306/// global in the specified llvm.used list as being used with this directive.
307void AsmPrinter::EmitLLVMUsedList(Constant *List) {
308 const char *Directive = TAI->getUsedDirective();
309
310 // Should be an array of 'sbyte*'.
311 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
312 if (InitList == 0) return;
313
314 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
315 O << Directive;
316 EmitConstantValueOnly(InitList->getOperand(i));
317 O << "\n";
318 }
319}
320
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000321/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
322/// function pointers, ignoring the init priority.
323void AsmPrinter::EmitXXStructorList(Constant *List) {
324 // Should be an array of '{ int, void ()* }' structs. The first value is the
325 // init priority, which we ignore.
326 if (!isa<ConstantArray>(List)) return;
327 ConstantArray *InitList = cast<ConstantArray>(List);
328 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
329 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
330 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000331
332 if (CS->getOperand(1)->isNullValue())
333 return; // Found a null terminator, exit printing.
334 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000335 EmitGlobalConstant(CS->getOperand(1));
336 }
337}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000338
Jim Laskey71262542006-10-17 13:41:07 +0000339/// getGlobalLinkName - Returns the asm/link name of of the specified
340/// global variable. Should be overridden by each target asm printer to
341/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000342const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
343 std::string LinkName;
Jim Laskeyd7f53cd2006-11-20 20:29:06 +0000344
345 if (isa<Function>(GV)) {
346 LinkName += TAI->getFunctionAddrPrefix();
347 LinkName += Mang->getValueName(GV);
348 LinkName += TAI->getFunctionAddrSuffix();
349 } else {
350 LinkName += TAI->getGlobalVarAddrPrefix();
351 LinkName += Mang->getValueName(GV);
352 LinkName += TAI->getGlobalVarAddrSuffix();
353 }
354
Jim Laskeyd24b9132006-10-17 17:17:24 +0000355 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000356}
357
Chris Lattnerbb644e32005-11-21 07:51:36 +0000358// EmitAlignment - Emit an alignment directive to the specified power of two.
359void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000360 if (GV && GV->getAlignment())
361 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000362 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000363 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
364 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000365}
366
Chris Lattnerbb644e32005-11-21 07:51:36 +0000367/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000368///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000369void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000370 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000371 if (TAI->getZeroDirective()) {
372 O << TAI->getZeroDirective() << NumZeros;
373 if (TAI->getZeroDirectiveSuffix())
374 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000375 O << "\n";
376 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000377 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000378 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000379 }
380 }
381}
382
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000383// Print out the specified constant, without a storage class. Only the
384// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000385void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000386 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000387 O << "0";
388 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnereea983e2006-09-28 23:17:41 +0000389 assert(CB->getValue());
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000390 O << "1";
Reid Spencere0fc4df2006-10-20 07:07:24 +0000391 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
392 if (CI->getType()->isSigned()) {
393 if (((CI->getSExtValue() << 32) >> 32) == CI->getSExtValue())
394 O << CI->getSExtValue();
395 else
396 O << (uint64_t)CI->getSExtValue();
397 } else
398 O << CI->getZExtValue();
399 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000400 // This is a constant address for a global variable or function. Use the
401 // name of the variable or function as the address value, possibly
402 // decorating it with GlobalVarAddrPrefix/Suffix or
403 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000404 if (isa<Function>(GV)) {
405 O << TAI->getFunctionAddrPrefix()
406 << Mang->getValueName(GV)
407 << TAI->getFunctionAddrSuffix();
408 } else {
409 O << TAI->getGlobalVarAddrPrefix()
410 << Mang->getValueName(GV)
411 << TAI->getGlobalVarAddrSuffix();
412 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000413 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000414 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000415 switch(CE->getOpcode()) {
416 case Instruction::GetElementPtr: {
417 // generate a symbolic expression for the byte address
418 const Constant *ptrVal = CE->getOperand(0);
419 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000420 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000421 if (Offset)
422 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000423 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000424 if (Offset > 0)
425 O << ") + " << Offset;
426 else if (Offset < 0)
427 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000428 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000429 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000430 }
431 break;
432 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000433 case Instruction::Trunc:
434 case Instruction::ZExt:
435 case Instruction::SExt:
436 case Instruction::FPTrunc:
437 case Instruction::FPExt:
438 case Instruction::UIToFP:
439 case Instruction::SIToFP:
440 case Instruction::FPToUI:
441 case Instruction::FPToSI:
442 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
443 break;
Chris Lattnerb9e41f52006-12-12 05:14:13 +0000444 case Instruction::BitCast:
445 return EmitConstantValueOnly(CE->getOperand(0));
446
Chris Lattner0c537da2006-12-12 05:18:19 +0000447 case Instruction::IntToPtr: {
448 // Handle casts to pointers by changing them into casts to the appropriate
449 // integer type. This promotes constant folding and simplifies this code.
450 Constant *Op = CE->getOperand(0);
451 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
452 return EmitConstantValueOnly(Op);
453 }
454
455
456 case Instruction::PtrToInt: {
Chris Lattner85492422006-07-29 01:57:19 +0000457 // Support only foldable casts to/from pointers that can be eliminated by
458 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000459 Constant *Op = CE->getOperand(0);
Chris Lattner0c537da2006-12-12 05:18:19 +0000460 const Type *Ty = CE->getType();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000461
Chris Lattner0c537da2006-12-12 05:18:19 +0000462 // We can emit the pointer value into this slot if the slot is an
463 // integer slot greater or equal to the size of the pointer.
464 if (Ty->isIntegral() &&
465 Ty->getPrimitiveSize() >= TD->getTypeSize(Op->getType()))
Chris Lattner85492422006-07-29 01:57:19 +0000466 return EmitConstantValueOnly(Op);
Chris Lattner85492422006-07-29 01:57:19 +0000467
468 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000469 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000470 break;
471 }
472 case Instruction::Add:
473 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000474 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000475 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000476 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000477 O << ")";
478 break;
479 default:
480 assert(0 && "Unsupported operator!");
481 }
482 } else {
483 assert(0 && "Unknown constant value!");
484 }
485}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000486
487/// toOctal - Convert the low order bits of X into an octal digit.
488///
489static inline char toOctal(int X) {
490 return (X&7)+'0';
491}
492
Chris Lattner55a6d902005-11-10 18:06:33 +0000493/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000494/// the predicate isString is true.
495///
Chris Lattner55a6d902005-11-10 18:06:33 +0000496static void printAsCString(std::ostream &O, const ConstantArray *CVA,
497 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000498 assert(CVA->isString() && "Array is not string compatible!");
499
500 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000501 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000502 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000503 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000504
505 if (C == '"') {
506 O << "\\\"";
507 } else if (C == '\\') {
508 O << "\\\\";
509 } else if (isprint(C)) {
510 O << C;
511 } else {
512 switch(C) {
513 case '\b': O << "\\b"; break;
514 case '\f': O << "\\f"; break;
515 case '\n': O << "\\n"; break;
516 case '\r': O << "\\r"; break;
517 case '\t': O << "\\t"; break;
518 default:
519 O << '\\';
520 O << toOctal(C >> 6);
521 O << toOctal(C >> 3);
522 O << toOctal(C >> 0);
523 break;
524 }
525 }
526 }
527 O << "\"";
528}
529
Jeff Cohen24a62a92006-05-02 01:16:28 +0000530/// EmitString - Emit a zero-byte-terminated string constant.
531///
532void AsmPrinter::EmitString(const ConstantArray *CVA) const {
533 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000534 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000535 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000536 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000537 printAsCString(O, CVA, NumElts-1);
538 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000539 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000540 printAsCString(O, CVA, NumElts);
541 }
542 O << "\n";
543}
544
Chris Lattnerbb644e32005-11-21 07:51:36 +0000545/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000546///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000547void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000548 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000549
Chris Lattner61753bf2004-10-16 18:19:26 +0000550 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000551 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000552 return;
553 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
554 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000555 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000556 } else { // Not a string. Print the values in successive locations
557 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000558 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000559 }
560 return;
561 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
562 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000563 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000564 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000565 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
566 const Constant* field = CVS->getOperand(i);
567
568 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000569 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000570 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000571 : cvsLayout->MemberOffsets[i+1])
572 - cvsLayout->MemberOffsets[i]) - fieldSize;
573 sizeSoFar += fieldSize + padSize;
574
575 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000576 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000577
578 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000579 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000580 }
581 assert(sizeSoFar == cvsLayout->StructSize &&
582 "Layout of constant struct may be incorrect!");
583 return;
584 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
585 // FP Constants are printed as integer constants to avoid losing
586 // precision...
587 double Val = CFP->getValue();
588 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000589 if (TAI->getData64bitsDirective())
590 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
591 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000592 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000593 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
594 << "\t" << TAI->getCommentString()
595 << " double most significant word " << Val << "\n";
596 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
597 << "\t" << TAI->getCommentString()
598 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000599 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000600 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
601 << "\t" << TAI->getCommentString()
602 << " double least significant word " << Val << "\n";
603 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
604 << "\t" << TAI->getCommentString()
605 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000606 }
607 return;
608 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000609 O << TAI->getData32bitsDirective() << FloatToBits(Val)
610 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000611 return;
612 }
613 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
614 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000615 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000616
Jim Laskeya6211dc2006-09-06 18:34:40 +0000617 if (TAI->getData64bitsDirective())
618 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000619 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000620 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
621 << "\t" << TAI->getCommentString()
622 << " Double-word most significant word " << Val << "\n";
623 O << TAI->getData32bitsDirective() << unsigned(Val)
624 << "\t" << TAI->getCommentString()
625 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000626 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000627 O << TAI->getData32bitsDirective() << unsigned(Val)
628 << "\t" << TAI->getCommentString()
629 << " Double-word least significant word " << Val << "\n";
630 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
631 << "\t" << TAI->getCommentString()
632 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000633 }
634 return;
635 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000636 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
637 const PackedType *PTy = CP->getType();
638
639 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
640 EmitGlobalConstant(CP->getOperand(I));
641
642 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000643 }
644
645 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000646 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000647 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000648 O << "\n";
649}
Chris Lattner061d9e22006-01-27 02:10:10 +0000650
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000651void
652AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
653 // Target doesn't support this yet!
654 abort();
655}
656
Chris Lattnera32814b2006-09-26 23:59:50 +0000657/// PrintSpecial - Print information related to the specified machine instr
658/// that is independent of the operand, and may be independent of the instr
659/// itself. This can be useful for portably encoding the comment character
660/// or other bits of target-specific knowledge into the asmstrings. The
661/// syntax used is ${:comment}. Targets can override this to add support
662/// for their own strange codes.
663void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000664 if (!strcmp(Code, "private")) {
665 O << TAI->getPrivateGlobalPrefix();
666 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000667 O << TAI->getCommentString();
668 } else if (!strcmp(Code, "uid")) {
669 // Assign a unique ID to this machine instruction.
670 static const MachineInstr *LastMI = 0;
671 static unsigned Counter = 0U-1;
672 // If this is a new machine instruction, bump the counter.
673 if (LastMI != MI) { ++Counter; LastMI = MI; }
674 O << Counter;
675 } else {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000676 cerr << "Unknown special formatter '" << Code
677 << "' for machine instr: " << *MI;
Chris Lattnera32814b2006-09-26 23:59:50 +0000678 exit(1);
679 }
680}
681
682
Chris Lattner061d9e22006-01-27 02:10:10 +0000683/// printInlineAsm - This method formats and prints the specified machine
684/// instruction that is an inline asm.
685void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000686 unsigned NumOperands = MI->getNumOperands();
687
688 // Count the number of register definitions.
689 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000690 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
691 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000692 assert(NumDefs != NumOperands-1 && "No asm string?");
693
694 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000695
696 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000697 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000698
Chris Lattner3390cbe2006-07-28 00:17:20 +0000699 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
700 if (AsmStr[0] == 0) {
701 O << "\n"; // Tab already printed, avoid double indenting next instr.
702 return;
703 }
704
Jim Laskeya6211dc2006-09-06 18:34:40 +0000705 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000706
Chris Lattneraa23fa92006-02-01 22:41:11 +0000707 // The variant of the current asmprinter: FIXME: change.
708 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000709
Chris Lattneraa23fa92006-02-01 22:41:11 +0000710 int CurVariant = -1; // The number of the {.|.|.} region we are in.
711 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000712
Chris Lattneraa23fa92006-02-01 22:41:11 +0000713 while (*LastEmitted) {
714 switch (*LastEmitted) {
715 default: {
716 // Not a special case, emit the string section literally.
717 const char *LiteralEnd = LastEmitted+1;
718 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000719 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000720 ++LiteralEnd;
721 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
722 O.write(LastEmitted, LiteralEnd-LastEmitted);
723 LastEmitted = LiteralEnd;
724 break;
725 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000726 case '\n':
727 ++LastEmitted; // Consume newline character.
728 O << "\n\t"; // Indent code with newline.
729 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000730 case '$': {
731 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000732 bool Done = true;
733
734 // Handle escapes.
735 switch (*LastEmitted) {
736 default: Done = false; break;
737 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000738 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
739 O << '$';
740 ++LastEmitted; // Consume second '$' character.
741 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000742 case '(': // $( -> same as GCC's { character.
743 ++LastEmitted; // Consume '(' character.
744 if (CurVariant != -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000745 cerr << "Nested variants found in inline asm string: '"
746 << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000747 exit(1);
748 }
749 CurVariant = 0; // We're in the first variant now.
750 break;
751 case '|':
752 ++LastEmitted; // consume '|' character.
753 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000754 cerr << "Found '|' character outside of variant in inline asm "
755 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000756 exit(1);
757 }
758 ++CurVariant; // We're in the next variant.
759 break;
760 case ')': // $) -> same as GCC's } char.
761 ++LastEmitted; // consume ')' character.
762 if (CurVariant == -1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000763 cerr << "Found '}' character outside of variant in inline asm "
764 << "string: '" << AsmStr << "'\n";
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000765 exit(1);
766 }
767 CurVariant = -1;
768 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000769 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000770 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000771
772 bool HasCurlyBraces = false;
773 if (*LastEmitted == '{') { // ${variable}
774 ++LastEmitted; // Consume '{' character.
775 HasCurlyBraces = true;
776 }
777
778 const char *IDStart = LastEmitted;
779 char *IDEnd;
780 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
781 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000782 cerr << "Bad $ operand number in inline asm string: '"
783 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000784 exit(1);
785 }
786 LastEmitted = IDEnd;
787
Chris Lattner34f74c12006-02-06 22:17:23 +0000788 char Modifier[2] = { 0, 0 };
789
Chris Lattneraa23fa92006-02-01 22:41:11 +0000790 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000791 // If we have curly braces, check for a modifier character. This
792 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
793 if (*LastEmitted == ':') {
794 ++LastEmitted; // Consume ':' character.
795 if (*LastEmitted == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000796 cerr << "Bad ${:} expression in inline asm string: '"
797 << AsmStr << "'\n";
Chris Lattner34f74c12006-02-06 22:17:23 +0000798 exit(1);
799 }
800
801 Modifier[0] = *LastEmitted;
802 ++LastEmitted; // Consume modifier character.
803 }
804
Chris Lattneraa23fa92006-02-01 22:41:11 +0000805 if (*LastEmitted != '}') {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000806 cerr << "Bad ${} expression in inline asm string: '"
807 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000808 exit(1);
809 }
810 ++LastEmitted; // Consume '}' character.
811 }
812
813 if ((unsigned)Val >= NumOperands-1) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000814 cerr << "Invalid $ operand number in inline asm string: '"
815 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000816 exit(1);
817 }
818
Chris Lattner571d9642006-02-23 19:21:04 +0000819 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000820 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000821 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
822 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000823
824 bool Error = false;
825
Chris Lattner571d9642006-02-23 19:21:04 +0000826 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000827 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000828 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000829 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
830 OpNo += (OpFlags >> 3) + 1;
831 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000832
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000833 if (OpNo >= MI->getNumOperands()) {
834 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000835 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000836 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
837 ++OpNo; // Skip over the ID number.
838
839 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
840 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
841 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
842 Modifier[0] ? Modifier : 0);
843 } else {
844 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
845 Modifier[0] ? Modifier : 0);
846 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000847 }
848 if (Error) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000849 cerr << "Invalid operand found in inline asm: '"
850 << AsmStr << "'\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000851 MI->dump();
852 exit(1);
853 }
Chris Lattner571d9642006-02-23 19:21:04 +0000854 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000855 break;
856 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000857 }
858 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000859 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000860}
861
862/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
863/// instruction, using the specified assembler variant. Targets should
864/// overried this to format as appropriate.
865bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000866 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000867 // Target doesn't support this yet!
868 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000869}
Chris Lattner1d08c652006-02-24 20:21:58 +0000870
871bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
872 unsigned AsmVariant,
873 const char *ExtraCode) {
874 // Target doesn't support this yet!
875 return true;
876}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000877
878/// printBasicBlockLabel - This method prints the label for the specified
879/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000880void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
881 bool printColon,
882 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000883 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000884 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000885 if (printColon)
886 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +0000887 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000888 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000889}
Nate Begeman984c1a42006-08-12 21:29:52 +0000890
891/// printSetLabel - This method prints a set label for the specified
892/// MachineBasicBlock
893void AsmPrinter::printSetLabel(unsigned uid,
894 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000895 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000896 return;
897
Jim Laskeya6211dc2006-09-06 18:34:40 +0000898 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
899 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000900 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000901 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000902 << '_' << uid << '\n';
903}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000904
Evan Cheng91f120f2006-11-01 09:23:08 +0000905void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2,
906 const MachineBasicBlock *MBB) const {
907 if (!TAI->getSetDirective())
908 return;
909
910 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
911 << getFunctionNumber() << '_' << uid << '_' << uid2
912 << "_set_" << MBB->getNumber() << ',';
913 printBasicBlockLabel(MBB, false, false);
914 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
915 << '_' << uid << '_' << uid2 << '\n';
916}
917
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000918/// printDataDirective - This method prints the asm directive for the
919/// specified type.
920void AsmPrinter::printDataDirective(const Type *type) {
921 const TargetData *TD = TM.getTargetData();
922 switch (type->getTypeID()) {
923 case Type::BoolTyID:
924 case Type::UByteTyID: case Type::SByteTyID:
925 O << TAI->getData8bitsDirective();
926 break;
927 case Type::UShortTyID: case Type::ShortTyID:
928 O << TAI->getData16bitsDirective();
929 break;
930 case Type::PointerTyID:
931 if (TD->getPointerSize() == 8) {
932 assert(TAI->getData64bitsDirective() &&
933 "Target cannot handle 64-bit pointer exprs!");
934 O << TAI->getData64bitsDirective();
935 break;
936 }
937 //Fall through for pointer size == int size
938 case Type::UIntTyID: case Type::IntTyID:
939 O << TAI->getData32bitsDirective();
940 break;
941 case Type::ULongTyID: case Type::LongTyID:
942 assert(TAI->getData64bitsDirective() &&
943 "Target cannot handle 64-bit constant exprs!");
944 O << TAI->getData64bitsDirective();
945 break;
946 case Type::FloatTyID: case Type::DoubleTyID:
947 assert (0 && "Should have already output floating point constant.");
948 default:
949 assert (0 && "Can't handle printing this type of thing");
950 break;
951 }
952}