blob: 8119fd30940f9f2a3badf8734f4bf60a4667811b [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32AsmPrinter.cpp - Print machine instrs to PowerPC assembly ----===//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00002//
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//
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"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000020#include "PowerPC.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000021#include "PPC32TargetMachine.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000026#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000027#include "llvm/CodeGen/MachineConstantPool.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"
Chris Lattner7bb424f2004-08-14 23:27:29 +000030#include "llvm/CodeGen/ValueTypes.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/Support/Mangler.h"
Misha Brukman05794492004-06-24 17:31:42 +000032#include "Support/CommandLine.h"
33#include "Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000034#include "Support/Statistic.h"
35#include "Support/StringExtras.h"
Misha Brukman05794492004-06-24 17:31:42 +000036#include <set>
Chris Lattnera3840792004-08-16 23:25:21 +000037using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000038
39namespace {
40 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
41
Misha Brukmanf2ccb772004-08-17 04:55:41 +000042 struct PPC32AsmPrinter : public AsmPrinter {
Misha Brukman97a296f2004-07-21 20:11:11 +000043 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000044 std::set<std::string> Strings;
45
Misha Brukmanf2ccb772004-08-17 04:55:41 +000046 PPC32AsmPrinter(std::ostream &O, TargetMachine &TM)
Chris Lattner505e7832004-08-17 02:29:00 +000047 : AsmPrinter(O, TM), LabelNumber(0) {
Chris Lattnerf746a7d2004-08-18 02:22:55 +000048 CommentString = ";";
Chris Lattnerb462e472004-08-17 06:07:43 +000049 GlobalPrefix = "_";
Chris Lattner79835d92004-08-17 06:37:12 +000050 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
51 Data64bitsDirective = 0; // we can't emit a 64-bit unit
Chris Lattner69d485e2004-08-17 19:26:03 +000052 AlignmentIsInBytes = false; // Alignment is by power of 2.
Chris Lattner505e7832004-08-17 02:29:00 +000053 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +000054
Misha Brukmancf8d2442004-07-26 16:28:33 +000055 /// Unique incrementer for label values for referencing Global values.
Misha Brukman218bec72004-06-29 17:13:26 +000056 ///
Misha Brukmancf8d2442004-07-26 16:28:33 +000057 unsigned LabelNumber;
58
Misha Brukman5dfe3a92004-06-21 16:55:25 +000059 virtual const char *getPassName() const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000060 return "PPC32 Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000061 }
62
Misha Brukmanf2ccb772004-08-17 04:55:41 +000063 PPC32TargetMachine &getTM() {
64 return static_cast<PPC32TargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000065 }
66
Nate Begemane59bf592004-08-14 22:09:10 +000067 /// printInstruction - This method is automatically generated by tablegen
68 /// from the instruction set description. This method returns true if the
69 /// machine instruction was sufficiently described to print it, otherwise it
70 /// returns false.
71 bool printInstruction(const MachineInstr *MI);
72
Misha Brukman5dfe3a92004-06-21 16:55:25 +000073 void printMachineInstruction(const MachineInstr *MI);
Nate Begemanb73a7112004-08-13 09:32:01 +000074 void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
Misha Brukmanaf313fb2004-07-28 00:00:48 +000075 void printImmOp(const MachineOperand &MO, unsigned ArgType);
Chris Lattner7bb424f2004-08-14 23:27:29 +000076
77 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
78 const MachineOperand &MO = MI->getOperand(OpNo);
79 if (MO.getType() == MachineOperand::MO_MachineRegister) {
80 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
81 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
Chris Lattner0ea31712004-08-15 05:46:14 +000082 } else if (MO.isImmediate()) {
83 O << MO.getImmedValue();
Chris Lattner7bb424f2004-08-14 23:27:29 +000084 } else {
85 printOp(MO);
86 }
87 }
88
Nate Begemanc3306122004-08-21 05:56:39 +000089 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
90 MVT::ValueType VT) {
91 unsigned char value = MI->getOperand(OpNo).getImmedValue();
Chris Lattner12585ba2004-08-21 19:11:03 +000092 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +000093 O << (unsigned int)value;
94 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +000095 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
96 MVT::ValueType VT) {
97 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
98 }
99
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000100 void printConstantPool(MachineConstantPool *MCP);
101 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000102 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000103 };
104} // end of anonymous namespace
105
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000106/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000107/// assembly code for a MachineFunction to the given output stream,
108/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000109/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000110///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000111FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
112 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000113}
114
Nate Begemane59bf592004-08-14 22:09:10 +0000115// Include the auto-generated portion of the assembly writer
116#include "PowerPCGenAsmWriter.inc"
117
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000118/// printConstantPool - Print to the current output stream assembly
119/// representations of the constants in the constant pool MCP. This is
120/// used to print out constants which have been "spilled to memory" by
121/// the code generator.
122///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000123void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000124 const std::vector<Constant*> &CP = MCP->getConstants();
125 const TargetData &TD = TM.getTargetData();
126
127 if (CP.empty()) return;
128
129 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
130 O << "\t.const\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000131 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000132 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000133 << *CP[i] << "\n";
134 emitGlobalConstant(CP[i]);
135 }
136}
137
138/// runOnMachineFunction - This uses the printMachineInstruction()
139/// method to print assembly for each instruction.
140///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000141bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000142 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000143 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000144
145 // Print out constants referenced by the function
146 printConstantPool(MF.getConstantPool());
147
148 // Print out labels for the function.
Chris Lattner69d485e2004-08-17 19:26:03 +0000149 O << "\t.text\n";
150 emitAlignment(2);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000151 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000152 O << CurrentFnName << ":\n";
153
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000154 // Print out code for the function.
155 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
156 I != E; ++I) {
157 // Print a label for the basic block.
Chris Lattner69d485e2004-08-17 19:26:03 +0000158 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000159 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Chris Lattner69d485e2004-08-17 19:26:03 +0000161 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000162 // Print the assembly for the instruction.
163 O << "\t";
164 printMachineInstruction(II);
165 }
166 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000167 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168
169 // We didn't modify anything.
170 return false;
171}
172
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000173void PPC32AsmPrinter::printOp(const MachineOperand &MO,
174 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000175 const MRegisterInfo &RI = *TM.getRegisterInfo();
176 int new_symbol;
177
178 switch (MO.getType()) {
179 case MachineOperand::MO_VirtualRegister:
180 if (Value *V = MO.getVRegValueOrNull()) {
181 O << "<" << V->getName() << ">";
182 return;
183 }
184 // FALLTHROUGH
185 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000186 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000187 O << LowercaseString(RI.get(MO.getReg()).Name);
188 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000189
190 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000191 case MachineOperand::MO_UnextendedImmed:
192 std::cerr << "printOp() does not handle immediate values\n";
193 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000194 return;
195
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000196 case MachineOperand::MO_PCRelativeDisp:
197 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
198 abort();
199 return;
200
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000201 case MachineOperand::MO_MachineBasicBlock: {
202 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
203 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000204 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000205 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000206 return;
207 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000208
209 case MachineOperand::MO_ConstantPoolIndex:
210 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000211 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000212
213 case MachineOperand::MO_ExternalSymbol:
214 O << MO.getSymbolName();
215 return;
216
Nate Begemanb73a7112004-08-13 09:32:01 +0000217 case MachineOperand::MO_GlobalAddress: {
218 GlobalValue *GV = MO.getGlobal();
219 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000220
Nate Begemanb73a7112004-08-13 09:32:01 +0000221 // Dynamically-resolved functions need a stub for the function. Be
222 // wary however not to output $stub for external functions whose addresses
223 // are taken. Those should be emitted as $non_lazy_ptr below.
224 Function *F = dyn_cast<Function>(GV);
225 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000226 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000227 FnStubs.insert(Name);
228 O << "L" << Name << "$stub";
229 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000230 }
Nate Begemane59bf592004-08-14 22:09:10 +0000231
Nate Begemanb73a7112004-08-13 09:32:01 +0000232 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000233 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000234 GVStubs.insert(Name);
235 O << "L" << Name << "$non_lazy_ptr";
236 return;
237 }
Nate Begemane59bf592004-08-14 22:09:10 +0000238
Chris Lattnera3840792004-08-16 23:25:21 +0000239 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000240 LinkOnceStubs.insert(Name);
241 O << "L" << Name << "$non_lazy_ptr";
242 return;
243 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000244
245 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000246 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000247 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000248
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000249 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000250 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000251 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000252 }
253}
254
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000255void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000256 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000257 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000258 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000259 } else {
260 O << Imm;
261 }
262}
263
Nate Begemane59bf592004-08-14 22:09:10 +0000264/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
265/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000267void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000268 ++EmittedInsts;
269 if (printInstruction(MI))
270 return; // Printer was automatically generated
271
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000272 unsigned Opcode = MI->getOpcode();
273 const TargetInstrInfo &TII = *TM.getInstrInfo();
274 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000275 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000276
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000277 unsigned ArgCount = MI->getNumOperands();
278 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000279 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
280 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
281 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
282 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
283 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000284 };
Misha Brukman5b570812004-08-10 22:47:03 +0000285 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000286 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000287 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000288 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000289
Misha Brukman61114612004-07-20 00:42:19 +0000290 // CALLpcrel and CALLindirect are handled specially here to print only the
291 // appropriate number of args that the assembler expects. This is because
292 // may have many arguments appended to record the uses of registers that are
293 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000294 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000295 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
296 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000297 } else if (Opcode == PPC::IMPLICIT_DEF) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000298 --EmittedInsts; // Not an actual machine instruction
Misha Brukman29188c62004-07-16 19:01:13 +0000299 O << "; IMPLICIT DEF ";
300 printOp(MI->getOperand(0));
301 O << "\n";
302 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000303 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000304 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000305 printOp(MI->getOperand(0));
306 O << "\n";
307 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000308 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000309 O << TII.getName(Opcode) << " ";
310 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000311 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000312 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000313 O << "\n";
314 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000315 } else if (Opcode == PPC::MovePCtoLR) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000316 ++EmittedInsts; // Actually two machine instructions
Misha Brukman61114612004-07-20 00:42:19 +0000317 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000318 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
319 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000320 O << "\tmflr ";
321 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000322 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000323 return;
324 }
325
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000326 O << TII.getName(Opcode) << " ";
Nate Begeman81d265d2004-08-19 05:20:54 +0000327 if (Opcode == PPC::LOADHiAddr) {
328 printOp(MI->getOperand(0));
329 O << ", ";
330 if (MI->getOperand(1).getReg() == PPC::R0)
331 O << "0";
332 else
333 printOp(MI->getOperand(1));
334 O << ", ha16(" ;
335 printOp(MI->getOperand(2), true /* LoadAddrOp */);
336 O << "-\"L0000" << LabelNumber << "$pb\")\n";
337 } else if (ArgCount == 3 && (MI->getOperand(2).isConstantPoolIndex()
338 || MI->getOperand(2).isGlobalAddress())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000339 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000340 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000341 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000342 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000343 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000344 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000345 O << "0";
346 else
347 printOp(MI->getOperand(1));
348 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000349 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000350 printOp(MI->getOperand(0));
351 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000352 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000353 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000354 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000355 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000356 O << "0";
357 else
358 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000359 O << ")\n";
360 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000361 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000362 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000363 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000364 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000365 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000366 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000367 // for long branch support, bc $+8
368 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
369 TII.isBranch(MI->getOpcode())) {
370 O << "$+8";
371 assert(8 == MI->getOperand(i).getImmedValue()
372 && "branch off PC not to pc+8?");
373 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000374 } else if (MI->getOperand(i).isImmediate()) {
375 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000376 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000377 printOp(MI->getOperand(i));
378 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000379 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000380 O << "\n";
381 else
382 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000383 }
384 }
Nate Begemane59bf592004-08-14 22:09:10 +0000385 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000386}
387
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000388// SwitchSection - Switch to the specified section of the executable if we are
389// not already in it!
390//
391static void SwitchSection(std::ostream &OS, std::string &CurSection,
392 const char *NewSection) {
393 if (CurSection != NewSection) {
394 CurSection = NewSection;
395 if (!CurSection.empty())
396 OS << "\t" << NewSection << "\n";
397 }
398}
399
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000400bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000401 const TargetData &TD = TM.getTargetData();
402 std::string CurSection;
403
404 // Print out module-level global variables here.
405 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
406 if (I->hasInitializer()) { // External global require no code
407 O << "\n\n";
408 std::string name = Mang->getValueName(I);
409 Constant *C = I->getInitializer();
410 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000411 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000412
Misha Brukman97a296f2004-07-21 20:11:11 +0000413 if (C->isNullValue() && /* FIXME: Verify correct */
414 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000415 SwitchSection(O, CurSection, ".data");
416 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000417 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000418 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000419 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000420 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000421 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000422 WriteAsOperand(O, I, true, true, &M);
423 O << "\n";
424 } else {
425 switch (I->getLinkage()) {
426 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000427 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
428 << ".weak_definition " << name << '\n'
429 << ".private_extern " << name << '\n'
430 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
431 LinkOnceStubs.insert(name);
432 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000433 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
434 // Nonnull linkonce -> weak
435 O << "\t.weak " << name << "\n";
436 SwitchSection(O, CurSection, "");
437 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
438 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000439 case GlobalValue::AppendingLinkage:
440 // FIXME: appending linkage variables should go into a section of
441 // their name or something. For now, just emit them as external.
442 case GlobalValue::ExternalLinkage:
443 // If external or appending, declare as a global symbol
444 O << "\t.globl " << name << "\n";
445 // FALL THROUGH
446 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000447 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000448 break;
449 }
450
Chris Lattner69d485e2004-08-17 19:26:03 +0000451 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000452 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000453 WriteAsOperand(O, I, true, true, &M);
454 O << " = ";
455 WriteAsOperand(O, C, false, false, &M);
456 O << "\n";
457 emitGlobalConstant(C);
458 }
459 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000460
461 // Output stubs for dynamically-linked functions
462 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
463 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000464 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000465 O << ".data\n";
466 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000467 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000468 O << "L" << *i << "$stub:\n";
469 O << "\t.indirect_symbol " << *i << "\n";
470 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000471 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000472 O << "L0$" << *i << ":\n";
473 O << "\tmflr r11\n";
474 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
475 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000476 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000477 O << "\tmtctr r12\n";
478 O << "\tbctr\n";
479 O << ".data\n";
480 O << ".lazy_symbol_pointer\n";
481 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000482 O << "\t.indirect_symbol " << *i << "\n";
483 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000484 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000485
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000486 O << "\n";
487
488 // Output stubs for external global variables
489 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000490 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000491 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
492 i != e; ++i) {
493 O << "L" << *i << "$non_lazy_ptr:\n";
494 O << "\t.indirect_symbol " << *i << "\n";
495 O << "\t.long\t0\n";
496 }
497
Nate Begemane59bf592004-08-14 22:09:10 +0000498 // Output stubs for link-once variables
499 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
500 O << ".data\n.align 2\n";
501 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
502 e = LinkOnceStubs.end(); i != e; ++i) {
503 O << "L" << *i << "$non_lazy_ptr:\n"
504 << "\t.long\t" << *i << '\n';
505 }
506
Chris Lattnera3840792004-08-16 23:25:21 +0000507 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000508 return false; // success
509}