blob: b30977b1a19d4196b1acbccfdc39717908becf4c [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/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 }
Nate Begeman07aada82004-08-30 02:28:06 +000095 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
96 MVT::ValueType VT) {
97 unsigned char value = MI->getOperand(OpNo).getImmedValue();
98 assert(value <= 63 && "Invalid u6imm argument!");
99 O << (unsigned int)value;
100 }
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000101 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
102 MVT::ValueType VT) {
103 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
104 }
105
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000106 void printConstantPool(MachineConstantPool *MCP);
107 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000108 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000109 };
110} // end of anonymous namespace
111
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000112/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000113/// assembly code for a MachineFunction to the given output stream,
114/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000115/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000116///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000117FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
118 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000119}
120
Nate Begemane59bf592004-08-14 22:09:10 +0000121// Include the auto-generated portion of the assembly writer
122#include "PowerPCGenAsmWriter.inc"
123
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000124/// printConstantPool - Print to the current output stream assembly
125/// representations of the constants in the constant pool MCP. This is
126/// used to print out constants which have been "spilled to memory" by
127/// the code generator.
128///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000129void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000130 const std::vector<Constant*> &CP = MCP->getConstants();
131 const TargetData &TD = TM.getTargetData();
132
133 if (CP.empty()) return;
134
135 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
136 O << "\t.const\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000137 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000138 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000139 << *CP[i] << "\n";
140 emitGlobalConstant(CP[i]);
141 }
142}
143
144/// runOnMachineFunction - This uses the printMachineInstruction()
145/// method to print assembly for each instruction.
146///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000147bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000148 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000149 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000150
151 // Print out constants referenced by the function
152 printConstantPool(MF.getConstantPool());
153
154 // Print out labels for the function.
Chris Lattner69d485e2004-08-17 19:26:03 +0000155 O << "\t.text\n";
156 emitAlignment(2);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000157 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000158 O << CurrentFnName << ":\n";
159
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000160 // Print out code for the function.
161 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
162 I != E; ++I) {
163 // Print a label for the basic block.
Chris Lattner69d485e2004-08-17 19:26:03 +0000164 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000165 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000166 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Chris Lattner69d485e2004-08-17 19:26:03 +0000167 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 // Print the assembly for the instruction.
169 O << "\t";
170 printMachineInstruction(II);
171 }
172 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000173 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000174
175 // We didn't modify anything.
176 return false;
177}
178
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000179void PPC32AsmPrinter::printOp(const MachineOperand &MO,
180 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000181 const MRegisterInfo &RI = *TM.getRegisterInfo();
182 int new_symbol;
183
184 switch (MO.getType()) {
185 case MachineOperand::MO_VirtualRegister:
186 if (Value *V = MO.getVRegValueOrNull()) {
187 O << "<" << V->getName() << ">";
188 return;
189 }
190 // FALLTHROUGH
191 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000192 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000193 O << LowercaseString(RI.get(MO.getReg()).Name);
194 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000195
196 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000197 case MachineOperand::MO_UnextendedImmed:
198 std::cerr << "printOp() does not handle immediate values\n";
199 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000200 return;
201
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000202 case MachineOperand::MO_PCRelativeDisp:
203 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
204 abort();
205 return;
206
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000207 case MachineOperand::MO_MachineBasicBlock: {
208 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
209 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000210 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000211 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000212 return;
213 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000214
215 case MachineOperand::MO_ConstantPoolIndex:
216 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000217 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000218
219 case MachineOperand::MO_ExternalSymbol:
220 O << MO.getSymbolName();
221 return;
222
Nate Begemanb73a7112004-08-13 09:32:01 +0000223 case MachineOperand::MO_GlobalAddress: {
224 GlobalValue *GV = MO.getGlobal();
225 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000226
Nate Begemanb73a7112004-08-13 09:32:01 +0000227 // Dynamically-resolved functions need a stub for the function. Be
228 // wary however not to output $stub for external functions whose addresses
229 // are taken. Those should be emitted as $non_lazy_ptr below.
230 Function *F = dyn_cast<Function>(GV);
231 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000232 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000233 FnStubs.insert(Name);
234 O << "L" << Name << "$stub";
235 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000236 }
Nate Begemane59bf592004-08-14 22:09:10 +0000237
Nate Begemanb73a7112004-08-13 09:32:01 +0000238 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000239 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000240 GVStubs.insert(Name);
241 O << "L" << Name << "$non_lazy_ptr";
242 return;
243 }
Nate Begemane59bf592004-08-14 22:09:10 +0000244
Chris Lattnera3840792004-08-16 23:25:21 +0000245 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000246 LinkOnceStubs.insert(Name);
247 O << "L" << Name << "$non_lazy_ptr";
248 return;
249 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000250
251 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000252 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000253 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000254
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000255 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000256 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000257 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000258 }
259}
260
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000261void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000262 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000263 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000264 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000265 } else {
266 O << Imm;
267 }
268}
269
Nate Begemane59bf592004-08-14 22:09:10 +0000270/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
271/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000272///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000273void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000274 ++EmittedInsts;
275 if (printInstruction(MI))
276 return; // Printer was automatically generated
277
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000278 unsigned Opcode = MI->getOpcode();
279 const TargetInstrInfo &TII = *TM.getInstrInfo();
280 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000281 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000282
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000283 unsigned ArgCount = MI->getNumOperands();
284 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000285 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
286 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
287 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
288 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
289 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000290 };
Misha Brukman5b570812004-08-10 22:47:03 +0000291 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000292 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000293 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000294 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000295
Misha Brukman61114612004-07-20 00:42:19 +0000296 // CALLpcrel and CALLindirect are handled specially here to print only the
297 // appropriate number of args that the assembler expects. This is because
298 // may have many arguments appended to record the uses of registers that are
299 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000300 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000301 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
302 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000303 } else if (Opcode == PPC::IMPLICIT_DEF) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000304 --EmittedInsts; // Not an actual machine instruction
Misha Brukman29188c62004-07-16 19:01:13 +0000305 O << "; IMPLICIT DEF ";
306 printOp(MI->getOperand(0));
307 O << "\n";
308 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000309 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000310 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000311 printOp(MI->getOperand(0));
312 O << "\n";
313 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000314 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000315 O << TII.getName(Opcode) << " ";
316 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000317 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000318 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000319 O << "\n";
320 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000321 } else if (Opcode == PPC::MovePCtoLR) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000322 ++EmittedInsts; // Actually two machine instructions
Misha Brukman61114612004-07-20 00:42:19 +0000323 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000324 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
325 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000326 O << "\tmflr ";
327 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000328 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000329 return;
330 }
331
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000332 O << TII.getName(Opcode) << " ";
Nate Begeman81d265d2004-08-19 05:20:54 +0000333 if (Opcode == PPC::LOADHiAddr) {
334 printOp(MI->getOperand(0));
335 O << ", ";
336 if (MI->getOperand(1).getReg() == PPC::R0)
337 O << "0";
338 else
339 printOp(MI->getOperand(1));
340 O << ", ha16(" ;
341 printOp(MI->getOperand(2), true /* LoadAddrOp */);
342 O << "-\"L0000" << LabelNumber << "$pb\")\n";
343 } else if (ArgCount == 3 && (MI->getOperand(2).isConstantPoolIndex()
344 || MI->getOperand(2).isGlobalAddress())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000345 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000346 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000347 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000348 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000349 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000350 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000351 O << "0";
352 else
353 printOp(MI->getOperand(1));
354 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000355 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000356 printOp(MI->getOperand(0));
357 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000358 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000359 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000360 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000361 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000362 O << "0";
363 else
364 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000365 O << ")\n";
366 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000367 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000368 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000369 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000370 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000371 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000372 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000373 // for long branch support, bc $+8
374 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
375 TII.isBranch(MI->getOpcode())) {
376 O << "$+8";
377 assert(8 == MI->getOperand(i).getImmedValue()
378 && "branch off PC not to pc+8?");
379 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000380 } else if (MI->getOperand(i).isImmediate()) {
381 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000382 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000383 printOp(MI->getOperand(i));
384 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000385 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000386 O << "\n";
387 else
388 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 }
390 }
Nate Begemane59bf592004-08-14 22:09:10 +0000391 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000392}
393
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000394// SwitchSection - Switch to the specified section of the executable if we are
395// not already in it!
396//
397static void SwitchSection(std::ostream &OS, std::string &CurSection,
398 const char *NewSection) {
399 if (CurSection != NewSection) {
400 CurSection = NewSection;
401 if (!CurSection.empty())
402 OS << "\t" << NewSection << "\n";
403 }
404}
405
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000406bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000407 const TargetData &TD = TM.getTargetData();
408 std::string CurSection;
409
410 // Print out module-level global variables here.
411 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
412 if (I->hasInitializer()) { // External global require no code
413 O << "\n\n";
414 std::string name = Mang->getValueName(I);
415 Constant *C = I->getInitializer();
416 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000417 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000418
Misha Brukman97a296f2004-07-21 20:11:11 +0000419 if (C->isNullValue() && /* FIXME: Verify correct */
420 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000421 SwitchSection(O, CurSection, ".data");
422 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000423 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000424 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000425 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000426 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000427 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000428 WriteAsOperand(O, I, true, true, &M);
429 O << "\n";
430 } else {
431 switch (I->getLinkage()) {
432 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000433 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
434 << ".weak_definition " << name << '\n'
435 << ".private_extern " << name << '\n'
436 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
437 LinkOnceStubs.insert(name);
438 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000439 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
440 // Nonnull linkonce -> weak
441 O << "\t.weak " << name << "\n";
442 SwitchSection(O, CurSection, "");
443 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
444 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000445 case GlobalValue::AppendingLinkage:
446 // FIXME: appending linkage variables should go into a section of
447 // their name or something. For now, just emit them as external.
448 case GlobalValue::ExternalLinkage:
449 // If external or appending, declare as a global symbol
450 O << "\t.globl " << name << "\n";
451 // FALL THROUGH
452 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000453 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000454 break;
455 }
456
Chris Lattner69d485e2004-08-17 19:26:03 +0000457 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000458 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000459 WriteAsOperand(O, I, true, true, &M);
460 O << " = ";
461 WriteAsOperand(O, C, false, false, &M);
462 O << "\n";
463 emitGlobalConstant(C);
464 }
465 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000466
467 // Output stubs for dynamically-linked functions
468 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
469 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000470 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000471 O << ".data\n";
472 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000473 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000474 O << "L" << *i << "$stub:\n";
475 O << "\t.indirect_symbol " << *i << "\n";
476 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000477 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000478 O << "L0$" << *i << ":\n";
479 O << "\tmflr r11\n";
480 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
481 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000482 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000483 O << "\tmtctr r12\n";
484 O << "\tbctr\n";
485 O << ".data\n";
486 O << ".lazy_symbol_pointer\n";
487 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000488 O << "\t.indirect_symbol " << *i << "\n";
489 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000490 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000491
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000492 O << "\n";
493
494 // Output stubs for external global variables
495 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000496 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000497 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
498 i != e; ++i) {
499 O << "L" << *i << "$non_lazy_ptr:\n";
500 O << "\t.indirect_symbol " << *i << "\n";
501 O << "\t.long\t0\n";
502 }
503
Nate Begemane59bf592004-08-14 22:09:10 +0000504 // Output stubs for link-once variables
505 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
506 O << ".data\n.align 2\n";
507 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
508 e = LinkOnceStubs.end(); i != e; ++i) {
509 O << "L" << *i << "$non_lazy_ptr:\n"
510 << "\t.long\t" << *i << '\n';
511 }
512
Chris Lattnera3840792004-08-16 23:25:21 +0000513 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000514 return false; // success
515}