blob: cb27e1f3888672e340b1b5bedd60e7ffde2d8f71 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// 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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
Chris Lattner83660c52004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000013//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman218bec72004-06-29 17:13:26 +000016//
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukman05794492004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattner26689592005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000021#include "PPCTargetMachine.h"
Chris Lattner26689592005-10-14 23:51:18 +000022#include "PPCSubtarget.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000027#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeyf5395ce2005-12-16 22:45:29 +000028#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukman05794492004-06-24 17:31:42 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000030#include "llvm/CodeGen/MachineInstr.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000032#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Nate Begeman17304c32004-10-26 06:02:38 +000035#include "llvm/Target/MRegisterInfo.h"
Nate Begemand4c8bea2004-11-25 07:09:01 +000036#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000039#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000040using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000041
42namespace {
43 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
44
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000045 class PPCAsmPrinter : public AsmPrinter {
Chris Lattnerd717b192005-12-11 07:45:47 +000046 public:
Chris Lattnera637c322005-12-16 00:22:14 +000047 std::set<std::string> FnStubs, GVStubs;
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000048
Chris Lattner4c7b43b2005-10-14 23:37:35 +000049 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner07455362005-11-21 08:14:07 +000050 : AsmPrinter(O, TM) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000051
Misha Brukman5dfe3a92004-06-21 16:55:25 +000052 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000053 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000054 }
55
Nate Begeman21e463b2005-10-16 05:39:50 +000056 PPCTargetMachine &getTM() {
57 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000058 }
59
Nate Begemanadeb43d2005-07-20 22:42:00 +000060 unsigned enumRegToMachineReg(unsigned enumReg) {
61 switch (enumReg) {
62 default: assert(0 && "Unhandled register!"); break;
63 case PPC::CR0: return 0;
64 case PPC::CR1: return 1;
65 case PPC::CR2: return 2;
66 case PPC::CR3: return 3;
67 case PPC::CR4: return 4;
68 case PPC::CR5: return 5;
69 case PPC::CR6: return 6;
70 case PPC::CR7: return 7;
71 }
72 abort();
73 }
74
Nate Begemane59bf592004-08-14 22:09:10 +000075 /// printInstruction - This method is automatically generated by tablegen
76 /// from the instruction set description. This method returns true if the
77 /// machine instruction was sufficiently described to print it, otherwise it
78 /// returns false.
79 bool printInstruction(const MachineInstr *MI);
80
Misha Brukman5dfe3a92004-06-21 16:55:25 +000081 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +000082 void printOp(const MachineOperand &MO);
Chris Lattner7bb424f2004-08-14 23:27:29 +000083
Nate Begeman391c5d22005-11-30 18:54:35 +000084 void printOperand(const MachineInstr *MI, unsigned OpNo){
Chris Lattner7bb424f2004-08-14 23:27:29 +000085 const MachineOperand &MO = MI->getOperand(OpNo);
86 if (MO.getType() == MachineOperand::MO_MachineRegister) {
87 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Chris Lattner9dc4d3c2005-08-22 22:00:02 +000088 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Chris Lattner0ea31712004-08-15 05:46:14 +000089 } else if (MO.isImmediate()) {
90 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000091 } else {
92 printOp(MO);
93 }
94 }
95
Nate Begeman391c5d22005-11-30 18:54:35 +000096 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +000097 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000098 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000099 O << (unsigned int)value;
100 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000101 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-08-30 02:28:06 +0000102 unsigned char value = MI->getOperand(OpNo).getImmedValue();
103 assert(value <= 63 && "Invalid u6imm argument!");
104 O << (unsigned int)value;
105 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000106 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000107 O << (short)MI->getOperand(OpNo).getImmedValue();
108 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000109 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000110 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
111 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000112 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000113 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
114 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000115 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000116 // Branches can take an immediate operand. This is used by the branch
117 // selection pass to print $+8, an eight byte displacement from the PC.
118 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman27499e32005-04-10 01:48:29 +0000119 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begemaned428532004-09-04 05:00:00 +0000120 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000121 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000122 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000123 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000124 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000125 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner9542f9712005-11-17 19:40:30 +0000126 if (!PPCGenerateStaticCode) {
Chris Lattnera637c322005-12-16 00:22:14 +0000127 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
128 GlobalValue *GV = MO.getGlobal();
129 if (((GV->isExternal() || GV->hasWeakLinkage() ||
130 GV->hasLinkOnceLinkage()))) {
131 // Dynamically-resolved functions need a stub for the function.
132 std::string Name = Mang->getValueName(GV);
133 FnStubs.insert(Name);
134 O << "L" << Name << "$stub";
135 return;
136 }
137 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000138 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
139 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
140 FnStubs.insert(Name);
141 O << "L" << Name << "$stub";
142 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000143 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000144 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000145
146 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000147 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000148 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000149 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
150 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000151 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera637c322005-12-16 00:22:14 +0000152 O << "\"L" << getFunctionNumber() << "$pb\"\n";
153 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000154 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000155 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000156 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000157 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000158 } else {
159 O << "ha16(";
160 printOp(MI->getOperand(OpNo));
161 if (PICEnabled)
Chris Lattnera637c322005-12-16 00:22:14 +0000162 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000163 else
164 O << ')';
165 }
Nate Begemaned428532004-09-04 05:00:00 +0000166 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000167 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000168 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000169 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000170 } else {
171 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000172 printOp(MI->getOperand(OpNo));
Nate Begeman2497e632005-07-21 20:44:43 +0000173 if (PICEnabled)
Chris Lattnera637c322005-12-16 00:22:14 +0000174 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000175 else
176 O << ')';
Nate Begemaned428532004-09-04 05:00:00 +0000177 }
178 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000179 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000180 unsigned CCReg = MI->getOperand(OpNo).getReg();
181 unsigned RegNo = enumRegToMachineReg(CCReg);
182 O << (0x80 >> RegNo);
183 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000184
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000185 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000186 virtual bool doFinalization(Module &M) = 0;
187 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000188
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000189 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
190 /// X
191 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000192 struct DarwinAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000193
194 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000195 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000196 CommentString = ";";
197 GlobalPrefix = "_";
Chris Lattnerf55366e2005-11-21 06:47:58 +0000198 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begemaned428532004-09-04 05:00:00 +0000199 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
200 Data64bitsDirective = 0; // we can't emit a 64-bit unit
201 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000202 ConstantPoolSection = "\t.const\t";
Chris Lattnerd717b192005-12-11 07:45:47 +0000203 LCOMMDirective = "\t.lcomm\t";
Chris Lattnerd1239b72005-12-13 06:32:50 +0000204 StaticCtorsSection = ".mod_init_func";
205 StaticDtorsSection = ".mod_term_func";
Nate Begemaned428532004-09-04 05:00:00 +0000206 }
207
208 virtual const char *getPassName() const {
209 return "Darwin PPC Assembly Printer";
210 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000211
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000213 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000214 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000215 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000216
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000217 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
218 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000219 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000220 /// Map for labels corresponding to global variables
221 ///
222 std::map<const GlobalVariable*,std::string> GVToLabelMap;
223
224 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000225 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000226 CommentString = "#";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000227 GlobalPrefix = ".";
Nate Begemaned428532004-09-04 05:00:00 +0000228 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
229 Data64bitsDirective = 0; // we can't emit a 64-bit unit
230 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000231 ConstantPoolSection = "\t.const\t";
Nate Begemaned428532004-09-04 05:00:00 +0000232 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000233
Nate Begemaned428532004-09-04 05:00:00 +0000234 virtual const char *getPassName() const {
235 return "AIX PPC Assembly Printer";
236 }
237
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000238 bool runOnMachineFunction(MachineFunction &F);
Nate Begemaned428532004-09-04 05:00:00 +0000239 bool doInitialization(Module &M);
240 bool doFinalization(Module &M);
241 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000242} // end of anonymous namespace
243
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000244/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
245/// code for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000246/// Darwin assembler can deal with.
247///
248FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
249 return new DarwinAsmPrinter(o, tm);
250}
251
252/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000253/// for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000254/// AIX 5L assembler can deal with.
255///
256FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
257 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000258}
259
Nate Begemane59bf592004-08-14 22:09:10 +0000260// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000261#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000262
Chris Lattner9ba13e42005-11-17 19:25:59 +0000263void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000264 const MRegisterInfo &RI = *TM.getRegisterInfo();
265 int new_symbol;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000266
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000267 switch (MO.getType()) {
268 case MachineOperand::MO_VirtualRegister:
269 if (Value *V = MO.getVRegValueOrNull()) {
270 O << "<" << V->getName() << ">";
271 return;
272 }
273 // FALLTHROUGH
274 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000275 case MachineOperand::MO_CCRegister:
Chris Lattner9dc4d3c2005-08-22 22:00:02 +0000276 O << RI.get(MO.getReg()).Name;
Misha Brukman7f484a52004-06-24 23:51:00 +0000277 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000278
279 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000280 case MachineOperand::MO_UnextendedImmed:
281 std::cerr << "printOp() does not handle immediate values\n";
282 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000283 return;
284
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000285 case MachineOperand::MO_PCRelativeDisp:
286 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
287 abort();
288 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000289
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000290 case MachineOperand::MO_MachineBasicBlock: {
291 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner07455362005-11-21 08:14:07 +0000292 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000293 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000294 return;
295 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000296
297 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner07455362005-11-21 08:14:07 +0000298 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattnerf55366e2005-11-21 06:47:58 +0000299 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000300 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000301 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000302 // Computing the address of an external symbol, not calling it.
303 if (!PPCGenerateStaticCode) {
304 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
305 GVStubs.insert(Name);
306 O << "L" << Name << "$non_lazy_ptr";
307 return;
308 }
Nate Begeman01d05262005-03-30 01:45:43 +0000309 O << GlobalPrefix << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000310 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000311 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000312 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000313 GlobalValue *GV = MO.getGlobal();
314 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000315
Nate Begemanfcf4a422004-10-17 23:01:34 +0000316 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattnera637c322005-12-16 00:22:14 +0000317 if (!PPCGenerateStaticCode) {
318 if (((GV->isExternal() || GV->hasWeakLinkage() ||
319 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000320 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000321 O << "L" << Name << "$non_lazy_ptr";
322 return;
323 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000324 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000325
Chris Lattner9542f9712005-11-17 19:40:30 +0000326 O << Name;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000327 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000328 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000329
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000330 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000331 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000332 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000333 }
334}
335
Nate Begemane59bf592004-08-14 22:09:10 +0000336/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
337/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000338///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000339void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000340 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000341
Nate Begemanad5f65c2005-04-05 18:19:50 +0000342 // Check for slwi/srwi mnemonics.
343 if (MI->getOpcode() == PPC::RLWINM) {
344 bool FoundMnemonic = false;
345 unsigned char SH = MI->getOperand(2).getImmedValue();
346 unsigned char MB = MI->getOperand(3).getImmedValue();
347 unsigned char ME = MI->getOperand(4).getImmedValue();
348 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
349 O << "slwi "; FoundMnemonic = true;
350 }
351 if (SH <= 31 && MB == (32-SH) && ME == 31) {
352 O << "srwi "; FoundMnemonic = true;
353 SH = 32-SH;
354 }
355 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000356 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000357 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000358 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000359 O << ", " << (unsigned int)SH << "\n";
360 return;
361 }
362 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000363
Nate Begemane59bf592004-08-14 22:09:10 +0000364 if (printInstruction(MI))
365 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000366
Nate Begemaned428532004-09-04 05:00:00 +0000367 assert(0 && "Unhandled instruction in asm writer!");
368 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000369 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000370}
371
Chris Lattneref658742005-11-21 07:57:37 +0000372
Nate Begemaned428532004-09-04 05:00:00 +0000373/// runOnMachineFunction - This uses the printMachineInstruction()
374/// method to print assembly for each instruction.
375///
376bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000377 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000378 O << "\n\n";
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000379
380 // Print out dwarf file info
381 MachineDebugInfo &DebugInfo = MF.getDebugInfo();
382 std::vector<std::string> Sources = DebugInfo.getSourceFiles();
383 for (unsigned i = 0, N = Sources.size(); i < N; i++) {
384 O << "\t; .file\t" << (i + 1) << "," << "\"" << Sources[i] << "\"" << "\n";
385 }
Nate Begemaned428532004-09-04 05:00:00 +0000386
387 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000388 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000389
390 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000391 const Function *F = MF.getFunction();
Chris Lattnera637c322005-12-16 00:22:14 +0000392 switch (F->getLinkage()) {
393 default: assert(0 && "Unknown linkage type!");
394 case Function::InternalLinkage: // Symbols default to internal.
395 SwitchSection(".text", F);
396 EmitAlignment(4, F);
397 break;
398 case Function::ExternalLinkage:
399 SwitchSection(".text", F);
400 EmitAlignment(4, F);
Chris Lattnerf02a9162005-10-28 18:44:07 +0000401 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000402 break;
403 case Function::WeakLinkage:
404 case Function::LinkOnceLinkage:
405 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
406 F);
407 O << "\t.globl\t" << CurrentFnName << "\n";
408 O << "\t.weak_definition\t" << CurrentFnName << "\n";
409 break;
410 }
Nate Begemaned428532004-09-04 05:00:00 +0000411 O << CurrentFnName << ":\n";
412
413 // Print out code for the function.
414 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
415 I != E; ++I) {
416 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000417 if (I != MF.begin()) {
Chris Lattner07455362005-11-21 08:14:07 +0000418 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000419 << I->getNumber() << ":\t";
Chris Lattner1db1adb2005-08-21 19:09:33 +0000420 if (!I->getBasicBlock()->getName().empty())
421 O << CommentString << " " << I->getBasicBlock()->getName();
422 O << "\n";
423 }
Nate Begemaned428532004-09-04 05:00:00 +0000424 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
425 II != E; ++II) {
426 // Print the assembly for the instruction.
427 O << "\t";
428 printMachineInstruction(II);
429 }
430 }
Nate Begemaned428532004-09-04 05:00:00 +0000431
432 // We didn't modify anything.
433 return false;
434}
435
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000436
Nate Begeman18ed0292005-07-21 01:25:49 +0000437bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000438 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
439 O << "\t.machine ppc970\n";
Nate Begeman18ed0292005-07-21 01:25:49 +0000440 AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000441
442 // Darwin wants symbols to be quoted if they have complex names.
443 Mang->setUseQuotes(true);
Nate Begeman18ed0292005-07-21 01:25:49 +0000444 return false;
445}
446
Nate Begemaned428532004-09-04 05:00:00 +0000447bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000448 const TargetData &TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000449
450 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000451 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000452 I != E; ++I) {
453 if (!I->hasInitializer()) continue; // External global require no code
454
Chris Lattnerd1239b72005-12-13 06:32:50 +0000455 // Check to see if this is a special global used by LLVM, if so, emit it.
456 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
457 continue;
Chris Lattnerdeea4162005-12-13 04:33:58 +0000458
459 O << '\n';
460 std::string name = Mang->getValueName(I);
461 Constant *C = I->getInitializer();
462 unsigned Size = TD.getTypeSize(C->getType());
463 unsigned Align = TD.getTypeAlignmentShift(C->getType());
464
465 if (C->isNullValue() && /* FIXME: Verify correct */
466 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
467 I->hasLinkOnceLinkage())) {
468 SwitchSection(".data", I);
469 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
470 if (I->hasInternalLinkage())
471 O << LCOMMDirective << name << "," << Size << "," << Align;
472 else
473 O << ".comm " << name << "," << Size;
474 O << "\t\t; '" << I->getName() << "'\n";
475 } else {
476 switch (I->getLinkage()) {
477 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000478 case GlobalValue::WeakLinkage:
Chris Lattnerd9e0ba42005-12-16 21:46:14 +0000479 O << ".globl " << name << '\n'
480 << ".weak_definition " << name << '\n'
Chris Lattnerdeea4162005-12-13 04:33:58 +0000481 << ".private_extern " << name << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000482 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000483 break;
484 case GlobalValue::AppendingLinkage:
485 // FIXME: appending linkage variables should go into a section of
486 // their name or something. For now, just emit them as external.
487 case GlobalValue::ExternalLinkage:
488 // If external or appending, declare as a global symbol
489 O << "\t.globl " << name << "\n";
490 // FALL THROUGH
491 case GlobalValue::InternalLinkage:
492 SwitchSection(".data", I);
493 break;
494 default:
495 std::cerr << "Unknown linkage type!";
496 abort();
497 }
498
499 EmitAlignment(Align, I);
500 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
501 EmitGlobalConstant(C);
502 }
503 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000504
505 // Output stubs for dynamically-linked functions
Chris Lattnerdeea4162005-12-13 04:33:58 +0000506 if (PICEnabled) {
507 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
508 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000509 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
510 "pure_instructions,32", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000511 EmitAlignment(2);
512 O << "L" << *i << "$stub:\n";
513 O << "\t.indirect_symbol " << *i << "\n";
514 O << "\tmflr r0\n";
515 O << "\tbcl 20,31,L0$" << *i << "\n";
516 O << "L0$" << *i << ":\n";
517 O << "\tmflr r11\n";
518 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
519 O << "\tmtlr r0\n";
520 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
521 O << "\tmtctr r12\n";
522 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000523 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000524 O << "L" << *i << "$lazy_ptr:\n";
525 O << "\t.indirect_symbol " << *i << "\n";
526 O << "\t.long dyld_stub_binding_helper\n";
527 }
528 } else {
529 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
530 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000531 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
532 "pure_instructions,16", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000533 EmitAlignment(4);
534 O << "L" << *i << "$stub:\n";
535 O << "\t.indirect_symbol " << *i << "\n";
536 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
537 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
538 O << "\tmtctr r12\n";
539 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000540 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000541 O << "L" << *i << "$lazy_ptr:\n";
542 O << "\t.indirect_symbol " << *i << "\n";
543 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +0000544 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000545 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000546
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000547 O << "\n";
548
Chris Lattnera637c322005-12-16 00:22:14 +0000549 // Output stubs for external and common global variables.
550 if (GVStubs.begin() != GVStubs.end()) {
551 SwitchSection(".non_lazy_symbol_pointer", 0);
552 for (std::set<std::string>::iterator I = GVStubs.begin(),
553 E = GVStubs.end(); I != E; ++I) {
554 O << "L" << *I << "$non_lazy_ptr:\n";
555 O << "\t.indirect_symbol " << *I << "\n";
556 O << "\t.long\t0\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000557 }
Nate Begemane59bf592004-08-14 22:09:10 +0000558 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000559
Chris Lattner5b0ac992005-11-01 00:12:36 +0000560 // Funny Darwin hack: This flag tells the linker that no global symbols
561 // contain code that falls through to other global symbols (e.g. the obvious
562 // implementation of multiple entry points). If this doesn't occur, the
563 // linker can safely perform dead code stripping. Since LLVM never generates
564 // code that does this, it is always safe to set.
565 O << "\t.subsections_via_symbols\n";
566
Chris Lattnera3840792004-08-16 23:25:21 +0000567 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000568 return false; // success
569}
Nate Begemaned428532004-09-04 05:00:00 +0000570
571/// runOnMachineFunction - This uses the printMachineInstruction()
572/// method to print assembly for each instruction.
573///
574bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000575 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000576
577 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000578 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000579
580 // Print out header for the function.
581 O << "\t.csect .text[PR]\n"
582 << "\t.align 2\n"
583 << "\t.globl " << CurrentFnName << '\n'
584 << "\t.globl ." << CurrentFnName << '\n'
585 << "\t.csect " << CurrentFnName << "[DS],3\n"
586 << CurrentFnName << ":\n"
587 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
588 << "\t.csect .text[PR]\n"
589 << '.' << CurrentFnName << ":\n";
590
591 // Print out code for the function.
592 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
593 I != E; ++I) {
594 // Print a label for the basic block.
Chris Lattner07455362005-11-21 08:14:07 +0000595 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
596 << I->getNumber()
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000597 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begemaned428532004-09-04 05:00:00 +0000598 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
599 II != E; ++II) {
600 // Print the assembly for the instruction.
601 O << "\t";
602 printMachineInstruction(II);
603 }
604 }
Nate Begemaned428532004-09-04 05:00:00 +0000605
606 O << "LT.." << CurrentFnName << ":\n"
607 << "\t.long 0\n"
608 << "\t.byte 0,0,32,65,128,0,0,0\n"
609 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
610 << "\t.short 3\n"
611 << "\t.byte \"" << CurrentFnName << "\"\n"
612 << "\t.align 2\n";
613
614 // We didn't modify anything.
615 return false;
616}
617
Nate Begemaned428532004-09-04 05:00:00 +0000618bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000619 SwitchSection("", 0);
Nate Begemaned428532004-09-04 05:00:00 +0000620 const TargetData &TD = TM.getTargetData();
Nate Begemaned428532004-09-04 05:00:00 +0000621
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000622 O << "\t.machine \"ppc64\"\n"
Nate Begemaned428532004-09-04 05:00:00 +0000623 << "\t.toc\n"
624 << "\t.csect .text[PR]\n";
625
626 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000627 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
628 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000629 if (!I->hasInitializer())
630 continue;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000631
Nate Begemaned428532004-09-04 05:00:00 +0000632 std::string Name = I->getName();
633 Constant *C = I->getInitializer();
634 // N.B.: We are defaulting to writable strings
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000635 if (I->hasExternalLinkage()) {
Nate Begemaned428532004-09-04 05:00:00 +0000636 O << "\t.globl " << Name << '\n'
637 << "\t.csect .data[RW],3\n";
638 } else {
639 O << "\t.csect _global.rw_c[RW],3\n";
640 }
641 O << Name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000642 EmitGlobalConstant(C);
Nate Begemaned428532004-09-04 05:00:00 +0000643 }
644
645 // Output labels for globals
Chris Lattnere4d5c442005-03-15 04:54:21 +0000646 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000647 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
648 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000649 const GlobalVariable *GV = I;
650 // Do not output labels for unused variables
651 if (GV->isExternal() && GV->use_begin() == GV->use_end())
652 continue;
653
Chris Lattner07455362005-11-21 08:14:07 +0000654 IncrementFunctionNumber();
Nate Begemaned428532004-09-04 05:00:00 +0000655 std::string Name = GV->getName();
Chris Lattner07455362005-11-21 08:14:07 +0000656 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begemaned428532004-09-04 05:00:00 +0000657 GVToLabelMap[GV] = Label;
658 O << Label << ":\n"
659 << "\t.tc " << Name << "[TC]," << Name;
660 if (GV->isExternal()) O << "[RW]";
661 O << '\n';
Chris Lattner07455362005-11-21 08:14:07 +0000662 }
Nate Begemaned428532004-09-04 05:00:00 +0000663
Chris Lattner3459bfb2005-11-10 18:20:29 +0000664 AsmPrinter::doInitialization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000665 return false; // success
666}
667
668bool AIXAsmPrinter::doFinalization(Module &M) {
669 const TargetData &TD = TM.getTargetData();
670 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000671 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
672 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000673 if (I->hasInitializer() || I->hasExternalLinkage())
674 continue;
675
676 std::string Name = I->getName();
677 if (I->hasInternalLinkage()) {
678 O << "\t.lcomm " << Name << ",16,_global.bss_c";
679 } else {
680 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner0561b3f2005-08-02 19:26:06 +0000681 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begemaned428532004-09-04 05:00:00 +0000682 }
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000683 O << "\t\t" << CommentString << " ";
Chris Lattner8ca02912005-10-03 07:08:36 +0000684 WriteAsOperand(O, I, false, true, &M);
Nate Begemaned428532004-09-04 05:00:00 +0000685 O << "\n";
686 }
687
688 O << "_section_.text:\n"
689 << "\t.csect .data[RW],3\n"
690 << "\t.llong _section_.text\n";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000691 AsmPrinter::doFinalization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000692 return false; // success
693}