blob: 83e3b0519a93879341fffbfc6b2959a73ad27bd2 [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"
Misha Brukman05794492004-06-24 17:31:42 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000029#include "llvm/CodeGen/MachineInstr.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000030#include "llvm/Support/Mangler.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000031#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Nate Begeman17304c32004-10-26 06:02:38 +000034#include "llvm/Target/MRegisterInfo.h"
Nate Begemand4c8bea2004-11-25 07:09:01 +000035#include "llvm/Target/TargetInstrInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000038#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000039using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000040
41namespace {
42 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000044 class PPCAsmPrinter : public AsmPrinter {
Chris Lattnerd717b192005-12-11 07:45:47 +000045 public:
Chris Lattnera637c322005-12-16 00:22:14 +000046 std::set<std::string> FnStubs, GVStubs;
Chris Lattnerac7fd7f2005-11-14 18:52:46 +000047
Chris Lattner4c7b43b2005-10-14 23:37:35 +000048 PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner07455362005-11-21 08:14:07 +000049 : AsmPrinter(O, TM) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000050
Misha Brukman5dfe3a92004-06-21 16:55:25 +000051 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000052 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000053 }
54
Nate Begeman21e463b2005-10-16 05:39:50 +000055 PPCTargetMachine &getTM() {
56 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000057 }
58
Nate Begemanadeb43d2005-07-20 22:42:00 +000059 unsigned enumRegToMachineReg(unsigned enumReg) {
60 switch (enumReg) {
61 default: assert(0 && "Unhandled register!"); break;
62 case PPC::CR0: return 0;
63 case PPC::CR1: return 1;
64 case PPC::CR2: return 2;
65 case PPC::CR3: return 3;
66 case PPC::CR4: return 4;
67 case PPC::CR5: return 5;
68 case PPC::CR6: return 6;
69 case PPC::CR7: return 7;
70 }
71 abort();
72 }
73
Nate Begemane59bf592004-08-14 22:09:10 +000074 /// printInstruction - This method is automatically generated by tablegen
75 /// from the instruction set description. This method returns true if the
76 /// machine instruction was sufficiently described to print it, otherwise it
77 /// returns false.
78 bool printInstruction(const MachineInstr *MI);
79
Misha Brukman5dfe3a92004-06-21 16:55:25 +000080 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +000081 void printOp(const MachineOperand &MO);
Chris Lattner7bb424f2004-08-14 23:27:29 +000082
Nate Begeman391c5d22005-11-30 18:54:35 +000083 void printOperand(const MachineInstr *MI, unsigned OpNo){
Chris Lattner7bb424f2004-08-14 23:27:29 +000084 const MachineOperand &MO = MI->getOperand(OpNo);
85 if (MO.getType() == MachineOperand::MO_MachineRegister) {
86 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Chris Lattner9dc4d3c2005-08-22 22:00:02 +000087 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Chris Lattner0ea31712004-08-15 05:46:14 +000088 } else if (MO.isImmediate()) {
89 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000090 } else {
91 printOp(MO);
92 }
93 }
94
Nate Begeman391c5d22005-11-30 18:54:35 +000095 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanc3306122004-08-21 05:56:39 +000096 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000097 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000098 O << (unsigned int)value;
99 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000100 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman07aada82004-08-30 02:28:06 +0000101 unsigned char value = MI->getOperand(OpNo).getImmedValue();
102 assert(value <= 63 && "Invalid u6imm argument!");
103 O << (unsigned int)value;
104 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000105 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000106 O << (short)MI->getOperand(OpNo).getImmedValue();
107 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000108 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000109 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
110 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000111 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000112 O << (short)MI->getOperand(OpNo).getImmedValue()*4;
113 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000114 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000115 // Branches can take an immediate operand. This is used by the branch
116 // selection pass to print $+8, an eight byte displacement from the PC.
117 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman27499e32005-04-10 01:48:29 +0000118 O << "$+" << MI->getOperand(OpNo).getImmedValue();
Nate Begemaned428532004-09-04 05:00:00 +0000119 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000120 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000121 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000122 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000123 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000124 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner9542f9712005-11-17 19:40:30 +0000125 if (!PPCGenerateStaticCode) {
Chris Lattnera637c322005-12-16 00:22:14 +0000126 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
127 GlobalValue *GV = MO.getGlobal();
128 if (((GV->isExternal() || GV->hasWeakLinkage() ||
129 GV->hasLinkOnceLinkage()))) {
130 // Dynamically-resolved functions need a stub for the function.
131 std::string Name = Mang->getValueName(GV);
132 FnStubs.insert(Name);
133 O << "L" << Name << "$stub";
134 return;
135 }
136 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000137 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
138 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
139 FnStubs.insert(Name);
140 O << "L" << Name << "$stub";
141 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000142 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000143 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000144
145 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000146 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000147 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000148 O << (int)MI->getOperand(OpNo).getImmedValue()*4;
149 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000150 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera637c322005-12-16 00:22:14 +0000151 O << "\"L" << getFunctionNumber() << "$pb\"\n";
152 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000153 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000154 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman2497e632005-07-21 20:44:43 +0000155 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000156 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000157 } else {
158 O << "ha16(";
159 printOp(MI->getOperand(OpNo));
160 if (PICEnabled)
Chris Lattnera637c322005-12-16 00:22:14 +0000161 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000162 else
163 O << ')';
164 }
Nate Begemaned428532004-09-04 05:00:00 +0000165 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000166 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000167 if (MI->getOperand(OpNo).isImmediate()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000168 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000169 } else {
170 O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000171 printOp(MI->getOperand(OpNo));
Nate Begeman2497e632005-07-21 20:44:43 +0000172 if (PICEnabled)
Chris Lattnera637c322005-12-16 00:22:14 +0000173 O << "-\"L" << getFunctionNumber() << "$pb\")";
Nate Begeman2497e632005-07-21 20:44:43 +0000174 else
175 O << ')';
Nate Begemaned428532004-09-04 05:00:00 +0000176 }
177 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000178 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000179 unsigned CCReg = MI->getOperand(OpNo).getReg();
180 unsigned RegNo = enumRegToMachineReg(CCReg);
181 O << (0x80 >> RegNo);
182 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000183
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000184 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Nate Begemaned428532004-09-04 05:00:00 +0000185 virtual bool doFinalization(Module &M) = 0;
186 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000187
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000188 /// DarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac OS
189 /// X
190 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000191 struct DarwinAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000192
193 DarwinAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000194 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000195 CommentString = ";";
196 GlobalPrefix = "_";
Chris Lattnerf55366e2005-11-21 06:47:58 +0000197 PrivateGlobalPrefix = "L"; // Marker for constant pool idxs
Nate Begemaned428532004-09-04 05:00:00 +0000198 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
199 Data64bitsDirective = 0; // we can't emit a 64-bit unit
200 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000201 ConstantPoolSection = "\t.const\t";
Chris Lattnerd717b192005-12-11 07:45:47 +0000202 LCOMMDirective = "\t.lcomm\t";
Chris Lattnerd1239b72005-12-13 06:32:50 +0000203 StaticCtorsSection = ".mod_init_func";
204 StaticDtorsSection = ".mod_term_func";
Nate Begemaned428532004-09-04 05:00:00 +0000205 }
206
207 virtual const char *getPassName() const {
208 return "Darwin PPC Assembly Printer";
209 }
Chris Lattner646f7af2005-12-09 18:24:29 +0000210
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000211 bool runOnMachineFunction(MachineFunction &F);
Nate Begeman18ed0292005-07-21 01:25:49 +0000212 bool doInitialization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000213 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000214 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000215
Misha Brukman3e0b51a2004-09-05 02:42:44 +0000216 /// AIXAsmPrinter - PowerPC assembly printer, customized for AIX
217 ///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000218 struct AIXAsmPrinter : public PPCAsmPrinter {
Nate Begemaned428532004-09-04 05:00:00 +0000219 /// Map for labels corresponding to global variables
220 ///
221 std::map<const GlobalVariable*,std::string> GVToLabelMap;
222
223 AIXAsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000224 : PPCAsmPrinter(O, TM) {
Nate Begemaned428532004-09-04 05:00:00 +0000225 CommentString = "#";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000226 GlobalPrefix = ".";
Nate Begemaned428532004-09-04 05:00:00 +0000227 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
228 Data64bitsDirective = 0; // we can't emit a 64-bit unit
229 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattnerc569e612005-11-21 08:26:15 +0000230 ConstantPoolSection = "\t.const\t";
Nate Begemaned428532004-09-04 05:00:00 +0000231 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000232
Nate Begemaned428532004-09-04 05:00:00 +0000233 virtual const char *getPassName() const {
234 return "AIX PPC Assembly Printer";
235 }
236
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000237 bool runOnMachineFunction(MachineFunction &F);
Nate Begemaned428532004-09-04 05:00:00 +0000238 bool doInitialization(Module &M);
239 bool doFinalization(Module &M);
240 };
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000241} // end of anonymous namespace
242
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000243/// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
244/// code for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000245/// Darwin assembler can deal with.
246///
247FunctionPass *llvm::createDarwinAsmPrinter(std::ostream &o, TargetMachine &tm) {
248 return new DarwinAsmPrinter(o, tm);
249}
250
251/// createAIXAsmPrinterPass - Returns a pass that prints the PPC assembly code
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000252/// for a MachineFunction to the given output stream, in a format that the
Nate Begemaned428532004-09-04 05:00:00 +0000253/// AIX 5L assembler can deal with.
254///
255FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
256 return new AIXAsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000257}
258
Nate Begemane59bf592004-08-14 22:09:10 +0000259// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000260#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000261
Chris Lattner9ba13e42005-11-17 19:25:59 +0000262void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000263 const MRegisterInfo &RI = *TM.getRegisterInfo();
264 int new_symbol;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000265
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266 switch (MO.getType()) {
267 case MachineOperand::MO_VirtualRegister:
268 if (Value *V = MO.getVRegValueOrNull()) {
269 O << "<" << V->getName() << ">";
270 return;
271 }
272 // FALLTHROUGH
273 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000274 case MachineOperand::MO_CCRegister:
Chris Lattner9dc4d3c2005-08-22 22:00:02 +0000275 O << RI.get(MO.getReg()).Name;
Misha Brukman7f484a52004-06-24 23:51:00 +0000276 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000277
278 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000279 case MachineOperand::MO_UnextendedImmed:
280 std::cerr << "printOp() does not handle immediate values\n";
281 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000282 return;
283
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000284 case MachineOperand::MO_PCRelativeDisp:
285 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
286 abort();
287 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000288
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000289 case MachineOperand::MO_MachineBasicBlock: {
290 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner07455362005-11-21 08:14:07 +0000291 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << "_"
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000292 << MBBOp->getNumber() << "\t; " << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000293 return;
294 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000295
296 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner07455362005-11-21 08:14:07 +0000297 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
Chris Lattnerf55366e2005-11-21 06:47:58 +0000298 << '_' << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000299 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000300 case MachineOperand::MO_ExternalSymbol:
Chris Lattnera637c322005-12-16 00:22:14 +0000301 // Computing the address of an external symbol, not calling it.
302 if (!PPCGenerateStaticCode) {
303 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
304 GVStubs.insert(Name);
305 O << "L" << Name << "$non_lazy_ptr";
306 return;
307 }
Nate Begeman01d05262005-03-30 01:45:43 +0000308 O << GlobalPrefix << MO.getSymbolName();
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000309 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000310 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000311 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000312 GlobalValue *GV = MO.getGlobal();
313 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000314
Nate Begemanfcf4a422004-10-17 23:01:34 +0000315 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattnera637c322005-12-16 00:22:14 +0000316 if (!PPCGenerateStaticCode) {
317 if (((GV->isExternal() || GV->hasWeakLinkage() ||
318 GV->hasLinkOnceLinkage()))) {
Nate Begemand4c8bea2004-11-25 07:09:01 +0000319 GVStubs.insert(Name);
Chris Lattnera637c322005-12-16 00:22:14 +0000320 O << "L" << Name << "$non_lazy_ptr";
321 return;
322 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000323 }
Nate Begemand4c8bea2004-11-25 07:09:01 +0000324
Chris Lattner9542f9712005-11-17 19:40:30 +0000325 O << Name;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000326 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000327 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000328
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000329 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000330 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000331 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000332 }
333}
334
Nate Begemane59bf592004-08-14 22:09:10 +0000335/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
336/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000337///
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000338void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000339 ++EmittedInsts;
Chris Lattner617742b2005-10-14 22:44:13 +0000340
Nate Begemanad5f65c2005-04-05 18:19:50 +0000341 // Check for slwi/srwi mnemonics.
342 if (MI->getOpcode() == PPC::RLWINM) {
343 bool FoundMnemonic = false;
344 unsigned char SH = MI->getOperand(2).getImmedValue();
345 unsigned char MB = MI->getOperand(3).getImmedValue();
346 unsigned char ME = MI->getOperand(4).getImmedValue();
347 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
348 O << "slwi "; FoundMnemonic = true;
349 }
350 if (SH <= 31 && MB == (32-SH) && ME == 31) {
351 O << "srwi "; FoundMnemonic = true;
352 SH = 32-SH;
353 }
354 if (FoundMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000355 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000356 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000357 printOperand(MI, 1);
Nate Begemanad5f65c2005-04-05 18:19:50 +0000358 O << ", " << (unsigned int)SH << "\n";
359 return;
360 }
361 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000362
Nate Begemane59bf592004-08-14 22:09:10 +0000363 if (printInstruction(MI))
364 return; // Printer was automatically generated
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000365
Nate Begemaned428532004-09-04 05:00:00 +0000366 assert(0 && "Unhandled instruction in asm writer!");
367 abort();
Nate Begemane59bf592004-08-14 22:09:10 +0000368 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000369}
370
Chris Lattneref658742005-11-21 07:57:37 +0000371
Nate Begemaned428532004-09-04 05:00:00 +0000372/// runOnMachineFunction - This uses the printMachineInstruction()
373/// method to print assembly for each instruction.
374///
375bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000376 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000377 O << "\n\n";
378
379 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000380 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000381
382 // Print out labels for the function.
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000383 const Function *F = MF.getFunction();
Chris Lattnera637c322005-12-16 00:22:14 +0000384 switch (F->getLinkage()) {
385 default: assert(0 && "Unknown linkage type!");
386 case Function::InternalLinkage: // Symbols default to internal.
387 SwitchSection(".text", F);
388 EmitAlignment(4, F);
389 break;
390 case Function::ExternalLinkage:
391 SwitchSection(".text", F);
392 EmitAlignment(4, F);
Chris Lattnerf02a9162005-10-28 18:44:07 +0000393 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000394 break;
395 case Function::WeakLinkage:
396 case Function::LinkOnceLinkage:
397 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
398 F);
399 O << "\t.globl\t" << CurrentFnName << "\n";
400 O << "\t.weak_definition\t" << CurrentFnName << "\n";
401 break;
402 }
Nate Begemaned428532004-09-04 05:00:00 +0000403 O << CurrentFnName << ":\n";
404
405 // Print out code for the function.
406 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
407 I != E; ++I) {
408 // Print a label for the basic block.
Chris Lattner1db1adb2005-08-21 19:09:33 +0000409 if (I != MF.begin()) {
Chris Lattner07455362005-11-21 08:14:07 +0000410 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
Chris Lattner7f9ccde2005-11-21 07:41:05 +0000411 << I->getNumber() << ":\t";
Chris Lattner1db1adb2005-08-21 19:09:33 +0000412 if (!I->getBasicBlock()->getName().empty())
413 O << CommentString << " " << I->getBasicBlock()->getName();
414 O << "\n";
415 }
Nate Begemaned428532004-09-04 05:00:00 +0000416 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
417 II != E; ++II) {
418 // Print the assembly for the instruction.
419 O << "\t";
420 printMachineInstruction(II);
421 }
422 }
Nate Begemaned428532004-09-04 05:00:00 +0000423
424 // We didn't modify anything.
425 return false;
426}
427
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428
Nate Begeman18ed0292005-07-21 01:25:49 +0000429bool DarwinAsmPrinter::doInitialization(Module &M) {
Chris Lattner3c304a32005-08-05 22:05:03 +0000430 if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
431 O << "\t.machine ppc970\n";
Nate Begeman18ed0292005-07-21 01:25:49 +0000432 AsmPrinter::doInitialization(M);
Chris Lattner85eac0d2005-11-10 19:33:43 +0000433
434 // Darwin wants symbols to be quoted if they have complex names.
435 Mang->setUseQuotes(true);
Nate Begeman18ed0292005-07-21 01:25:49 +0000436 return false;
437}
438
Nate Begemaned428532004-09-04 05:00:00 +0000439bool DarwinAsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000440 const TargetData &TD = TM.getTargetData();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000441
442 // Print out module-level global variables here.
Chris Lattnerced704b2005-11-14 19:00:30 +0000443 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattnerdeea4162005-12-13 04:33:58 +0000444 I != E; ++I) {
445 if (!I->hasInitializer()) continue; // External global require no code
446
Chris Lattnerd1239b72005-12-13 06:32:50 +0000447 // Check to see if this is a special global used by LLVM, if so, emit it.
448 if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
449 continue;
Chris Lattnerdeea4162005-12-13 04:33:58 +0000450
451 O << '\n';
452 std::string name = Mang->getValueName(I);
453 Constant *C = I->getInitializer();
454 unsigned Size = TD.getTypeSize(C->getType());
455 unsigned Align = TD.getTypeAlignmentShift(C->getType());
456
457 if (C->isNullValue() && /* FIXME: Verify correct */
458 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
459 I->hasLinkOnceLinkage())) {
460 SwitchSection(".data", I);
461 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
462 if (I->hasInternalLinkage())
463 O << LCOMMDirective << name << "," << Size << "," << Align;
464 else
465 O << ".comm " << name << "," << Size;
466 O << "\t\t; '" << I->getName() << "'\n";
467 } else {
468 switch (I->getLinkage()) {
469 case GlobalValue::LinkOnceLinkage:
Chris Lattnerdeea4162005-12-13 04:33:58 +0000470 case GlobalValue::WeakLinkage:
Chris Lattnerd9e0ba42005-12-16 21:46:14 +0000471 O << ".globl " << name << '\n'
472 << ".weak_definition " << name << '\n'
Chris Lattnerdeea4162005-12-13 04:33:58 +0000473 << ".private_extern " << name << '\n';
Chris Lattnera637c322005-12-16 00:22:14 +0000474 SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000475 break;
476 case GlobalValue::AppendingLinkage:
477 // FIXME: appending linkage variables should go into a section of
478 // their name or something. For now, just emit them as external.
479 case GlobalValue::ExternalLinkage:
480 // If external or appending, declare as a global symbol
481 O << "\t.globl " << name << "\n";
482 // FALL THROUGH
483 case GlobalValue::InternalLinkage:
484 SwitchSection(".data", I);
485 break;
486 default:
487 std::cerr << "Unknown linkage type!";
488 abort();
489 }
490
491 EmitAlignment(Align, I);
492 O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
493 EmitGlobalConstant(C);
494 }
495 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000496
497 // Output stubs for dynamically-linked functions
Chris Lattnerdeea4162005-12-13 04:33:58 +0000498 if (PICEnabled) {
499 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
500 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000501 SwitchSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
502 "pure_instructions,32", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000503 EmitAlignment(2);
504 O << "L" << *i << "$stub:\n";
505 O << "\t.indirect_symbol " << *i << "\n";
506 O << "\tmflr r0\n";
507 O << "\tbcl 20,31,L0$" << *i << "\n";
508 O << "L0$" << *i << ":\n";
509 O << "\tmflr r11\n";
510 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
511 O << "\tmtlr r0\n";
512 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
513 O << "\tmtctr r12\n";
514 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000515 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000516 O << "L" << *i << "$lazy_ptr:\n";
517 O << "\t.indirect_symbol " << *i << "\n";
518 O << "\t.long dyld_stub_binding_helper\n";
519 }
520 } else {
521 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
522 i != e; ++i) {
Chris Lattnera637c322005-12-16 00:22:14 +0000523 SwitchSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
524 "pure_instructions,16", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000525 EmitAlignment(4);
526 O << "L" << *i << "$stub:\n";
527 O << "\t.indirect_symbol " << *i << "\n";
528 O << "\tlis r11,ha16(L" << *i << "$lazy_ptr)\n";
529 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr)(r11)\n";
530 O << "\tmtctr r12\n";
531 O << "\tbctr\n";
Chris Lattnera637c322005-12-16 00:22:14 +0000532 SwitchSection(".lazy_symbol_pointer", 0);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000533 O << "L" << *i << "$lazy_ptr:\n";
534 O << "\t.indirect_symbol " << *i << "\n";
535 O << "\t.long dyld_stub_binding_helper\n";
Nate Begeman2497e632005-07-21 20:44:43 +0000536 }
Misha Brukman46fd00a2004-06-24 23:04:11 +0000537 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000538
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000539 O << "\n";
540
Chris Lattnera637c322005-12-16 00:22:14 +0000541 // Output stubs for external and common global variables.
542 if (GVStubs.begin() != GVStubs.end()) {
543 SwitchSection(".non_lazy_symbol_pointer", 0);
544 for (std::set<std::string>::iterator I = GVStubs.begin(),
545 E = GVStubs.end(); I != E; ++I) {
546 O << "L" << *I << "$non_lazy_ptr:\n";
547 O << "\t.indirect_symbol " << *I << "\n";
548 O << "\t.long\t0\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000549 }
Nate Begemane59bf592004-08-14 22:09:10 +0000550 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000551
Chris Lattner5b0ac992005-11-01 00:12:36 +0000552 // Funny Darwin hack: This flag tells the linker that no global symbols
553 // contain code that falls through to other global symbols (e.g. the obvious
554 // implementation of multiple entry points). If this doesn't occur, the
555 // linker can safely perform dead code stripping. Since LLVM never generates
556 // code that does this, it is always safe to set.
557 O << "\t.subsections_via_symbols\n";
558
Chris Lattnera3840792004-08-16 23:25:21 +0000559 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000560 return false; // success
561}
Nate Begemaned428532004-09-04 05:00:00 +0000562
563/// runOnMachineFunction - This uses the printMachineInstruction()
564/// method to print assembly for each instruction.
565///
566bool AIXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000567 SetupMachineFunction(MF);
Nate Begemaned428532004-09-04 05:00:00 +0000568
569 // Print out constants referenced by the function
Chris Lattnerc569e612005-11-21 08:26:15 +0000570 EmitConstantPool(MF.getConstantPool());
Nate Begemaned428532004-09-04 05:00:00 +0000571
572 // Print out header for the function.
573 O << "\t.csect .text[PR]\n"
574 << "\t.align 2\n"
575 << "\t.globl " << CurrentFnName << '\n'
576 << "\t.globl ." << CurrentFnName << '\n'
577 << "\t.csect " << CurrentFnName << "[DS],3\n"
578 << CurrentFnName << ":\n"
579 << "\t.llong ." << CurrentFnName << ", TOC[tc0], 0\n"
580 << "\t.csect .text[PR]\n"
581 << '.' << CurrentFnName << ":\n";
582
583 // Print out code for the function.
584 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
585 I != E; ++I) {
586 // Print a label for the basic block.
Chris Lattner07455362005-11-21 08:14:07 +0000587 O << PrivateGlobalPrefix << "BB" << getFunctionNumber() << '_'
588 << I->getNumber()
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000589 << ":\t" << CommentString << I->getBasicBlock()->getName() << '\n';
Nate Begemaned428532004-09-04 05:00:00 +0000590 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
591 II != E; ++II) {
592 // Print the assembly for the instruction.
593 O << "\t";
594 printMachineInstruction(II);
595 }
596 }
Nate Begemaned428532004-09-04 05:00:00 +0000597
598 O << "LT.." << CurrentFnName << ":\n"
599 << "\t.long 0\n"
600 << "\t.byte 0,0,32,65,128,0,0,0\n"
601 << "\t.long LT.." << CurrentFnName << "-." << CurrentFnName << '\n'
602 << "\t.short 3\n"
603 << "\t.byte \"" << CurrentFnName << "\"\n"
604 << "\t.align 2\n";
605
606 // We didn't modify anything.
607 return false;
608}
609
Nate Begemaned428532004-09-04 05:00:00 +0000610bool AIXAsmPrinter::doInitialization(Module &M) {
Chris Lattnerac7fd7f2005-11-14 18:52:46 +0000611 SwitchSection("", 0);
Nate Begemaned428532004-09-04 05:00:00 +0000612 const TargetData &TD = TM.getTargetData();
Nate Begemaned428532004-09-04 05:00:00 +0000613
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000614 O << "\t.machine \"ppc64\"\n"
Nate Begemaned428532004-09-04 05:00:00 +0000615 << "\t.toc\n"
616 << "\t.csect .text[PR]\n";
617
618 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000619 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
620 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000621 if (!I->hasInitializer())
622 continue;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000623
Nate Begemaned428532004-09-04 05:00:00 +0000624 std::string Name = I->getName();
625 Constant *C = I->getInitializer();
626 // N.B.: We are defaulting to writable strings
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000627 if (I->hasExternalLinkage()) {
Nate Begemaned428532004-09-04 05:00:00 +0000628 O << "\t.globl " << Name << '\n'
629 << "\t.csect .data[RW],3\n";
630 } else {
631 O << "\t.csect _global.rw_c[RW],3\n";
632 }
633 O << Name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000634 EmitGlobalConstant(C);
Nate Begemaned428532004-09-04 05:00:00 +0000635 }
636
637 // Output labels for globals
Chris Lattnere4d5c442005-03-15 04:54:21 +0000638 if (M.global_begin() != M.global_end()) O << "\t.toc\n";
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000639 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
640 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000641 const GlobalVariable *GV = I;
642 // Do not output labels for unused variables
643 if (GV->isExternal() && GV->use_begin() == GV->use_end())
644 continue;
645
Chris Lattner07455362005-11-21 08:14:07 +0000646 IncrementFunctionNumber();
Nate Begemaned428532004-09-04 05:00:00 +0000647 std::string Name = GV->getName();
Chris Lattner07455362005-11-21 08:14:07 +0000648 std::string Label = "LC.." + utostr(getFunctionNumber());
Nate Begemaned428532004-09-04 05:00:00 +0000649 GVToLabelMap[GV] = Label;
650 O << Label << ":\n"
651 << "\t.tc " << Name << "[TC]," << Name;
652 if (GV->isExternal()) O << "[RW]";
653 O << '\n';
Chris Lattner07455362005-11-21 08:14:07 +0000654 }
Nate Begemaned428532004-09-04 05:00:00 +0000655
Chris Lattner3459bfb2005-11-10 18:20:29 +0000656 AsmPrinter::doInitialization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000657 return false; // success
658}
659
660bool AIXAsmPrinter::doFinalization(Module &M) {
661 const TargetData &TD = TM.getTargetData();
662 // Print out module-level global variables
Chris Lattner2e00d7d2005-07-26 19:03:27 +0000663 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
664 I != E; ++I) {
Nate Begemaned428532004-09-04 05:00:00 +0000665 if (I->hasInitializer() || I->hasExternalLinkage())
666 continue;
667
668 std::string Name = I->getName();
669 if (I->hasInternalLinkage()) {
670 O << "\t.lcomm " << Name << ",16,_global.bss_c";
671 } else {
672 O << "\t.comm " << Name << "," << TD.getTypeSize(I->getType())
Chris Lattner0561b3f2005-08-02 19:26:06 +0000673 << "," << Log2_32((unsigned)TD.getTypeAlignment(I->getType()));
Nate Begemaned428532004-09-04 05:00:00 +0000674 }
Chris Lattner6d5a4f62005-11-21 08:02:41 +0000675 O << "\t\t" << CommentString << " ";
Chris Lattner8ca02912005-10-03 07:08:36 +0000676 WriteAsOperand(O, I, false, true, &M);
Nate Begemaned428532004-09-04 05:00:00 +0000677 O << "\n";
678 }
679
680 O << "_section_.text:\n"
681 << "\t.csect .data[RW],3\n"
682 << "\t.llong _section_.text\n";
Chris Lattner3459bfb2005-11-10 18:20:29 +0000683 AsmPrinter::doFinalization(M);
Nate Begemaned428532004-09-04 05:00:00 +0000684 return false; // success
685}