blob: 1d31e3b13028f31b46865d18a3efe141fee4a7af [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
Chris Lattner97b2a2e2004-08-15 05:20:16 +000089 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
90 MVT::ValueType VT) {
91 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
92 }
93
Misha Brukman5dfe3a92004-06-21 16:55:25 +000094 void printConstantPool(MachineConstantPool *MCP);
95 bool runOnMachineFunction(MachineFunction &F);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000096 bool doFinalization(Module &M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +000097 };
98} // end of anonymous namespace
99
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000100/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000101/// assembly code for a MachineFunction to the given output stream,
102/// using the given target machine description. This should work
Misha Brukman7103fba2004-08-09 22:27:45 +0000103/// regardless of whether the function is in SSA form or not.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000104///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000105FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
106 return new PPC32AsmPrinter(o, tm);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000107}
108
Nate Begemane59bf592004-08-14 22:09:10 +0000109// Include the auto-generated portion of the assembly writer
110#include "PowerPCGenAsmWriter.inc"
111
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000112/// printConstantPool - Print to the current output stream assembly
113/// representations of the constants in the constant pool MCP. This is
114/// used to print out constants which have been "spilled to memory" by
115/// the code generator.
116///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000117void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000118 const std::vector<Constant*> &CP = MCP->getConstants();
119 const TargetData &TD = TM.getTargetData();
120
121 if (CP.empty()) return;
122
123 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
124 O << "\t.const\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000125 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000126 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000127 << *CP[i] << "\n";
128 emitGlobalConstant(CP[i]);
129 }
130}
131
132/// runOnMachineFunction - This uses the printMachineInstruction()
133/// method to print assembly for each instruction.
134///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000135bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnera3840792004-08-16 23:25:21 +0000136 setupMachineFunction(MF);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000137 O << "\n\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000138
139 // Print out constants referenced by the function
140 printConstantPool(MF.getConstantPool());
141
142 // Print out labels for the function.
Chris Lattner69d485e2004-08-17 19:26:03 +0000143 O << "\t.text\n";
144 emitAlignment(2);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000145 O << "\t.globl\t" << CurrentFnName << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000146 O << CurrentFnName << ":\n";
147
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000148 // Print out code for the function.
149 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
150 I != E; ++I) {
151 // Print a label for the basic block.
Chris Lattner69d485e2004-08-17 19:26:03 +0000152 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000153 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000154 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Chris Lattner69d485e2004-08-17 19:26:03 +0000155 II != E; ++II) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000156 // Print the assembly for the instruction.
157 O << "\t";
158 printMachineInstruction(II);
159 }
160 }
Misha Brukmancf8d2442004-07-26 16:28:33 +0000161 ++LabelNumber;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000162
163 // We didn't modify anything.
164 return false;
165}
166
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000167void PPC32AsmPrinter::printOp(const MachineOperand &MO,
168 bool LoadAddrOp /* = false */) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000169 const MRegisterInfo &RI = *TM.getRegisterInfo();
170 int new_symbol;
171
172 switch (MO.getType()) {
173 case MachineOperand::MO_VirtualRegister:
174 if (Value *V = MO.getVRegValueOrNull()) {
175 O << "<" << V->getName() << ">";
176 return;
177 }
178 // FALLTHROUGH
179 case MachineOperand::MO_MachineRegister:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000180 case MachineOperand::MO_CCRegister:
Misha Brukman7f484a52004-06-24 23:51:00 +0000181 O << LowercaseString(RI.get(MO.getReg()).Name);
182 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000183
184 case MachineOperand::MO_SignExtendedImmed:
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000185 case MachineOperand::MO_UnextendedImmed:
186 std::cerr << "printOp() does not handle immediate values\n";
187 abort();
Misha Brukman97a296f2004-07-21 20:11:11 +0000188 return;
189
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000190 case MachineOperand::MO_PCRelativeDisp:
191 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
192 abort();
193 return;
194
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000195 case MachineOperand::MO_MachineBasicBlock: {
196 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
197 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
Misha Brukman218bec72004-06-29 17:13:26 +0000198 << "_" << MBBOp->getNumber() << "\t; "
Misha Brukman2bf183c2004-06-25 15:42:10 +0000199 << MBBOp->getBasicBlock()->getName();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000200 return;
201 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000202
203 case MachineOperand::MO_ConstantPoolIndex:
204 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000205 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000206
207 case MachineOperand::MO_ExternalSymbol:
208 O << MO.getSymbolName();
209 return;
210
Nate Begemanb73a7112004-08-13 09:32:01 +0000211 case MachineOperand::MO_GlobalAddress: {
212 GlobalValue *GV = MO.getGlobal();
213 std::string Name = Mang->getValueName(GV);
Misha Brukmane2eceb52004-07-23 16:08:20 +0000214
Nate Begemanb73a7112004-08-13 09:32:01 +0000215 // Dynamically-resolved functions need a stub for the function. Be
216 // wary however not to output $stub for external functions whose addresses
217 // are taken. Those should be emitted as $non_lazy_ptr below.
218 Function *F = dyn_cast<Function>(GV);
219 if (F && F->isExternal() && !LoadAddrOp &&
Chris Lattnera3840792004-08-16 23:25:21 +0000220 getTM().CalledFunctions.count(F)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000221 FnStubs.insert(Name);
222 O << "L" << Name << "$stub";
223 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000224 }
Nate Begemane59bf592004-08-14 22:09:10 +0000225
Nate Begemanb73a7112004-08-13 09:32:01 +0000226 // External global variables need a non-lazily-resolved stub
Chris Lattnera3840792004-08-16 23:25:21 +0000227 if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
Nate Begemanb73a7112004-08-13 09:32:01 +0000228 GVStubs.insert(Name);
229 O << "L" << Name << "$non_lazy_ptr";
230 return;
231 }
Nate Begemane59bf592004-08-14 22:09:10 +0000232
Chris Lattnera3840792004-08-16 23:25:21 +0000233 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
Nate Begemane59bf592004-08-14 22:09:10 +0000234 LinkOnceStubs.insert(Name);
235 O << "L" << Name << "$non_lazy_ptr";
236 return;
237 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000238
239 O << Mang->getValueName(GV);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000240 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000241 }
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000242
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000243 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000244 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000245 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000246 }
247}
248
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000249void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000250 int Imm = MO.getImmedValue();
Misha Brukman5b570812004-08-10 22:47:03 +0000251 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000252 O << (short)Imm;
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000253 } else {
254 O << Imm;
255 }
256}
257
Nate Begemane59bf592004-08-14 22:09:10 +0000258/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
259/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000260///
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000261void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Nate Begemane59bf592004-08-14 22:09:10 +0000262 ++EmittedInsts;
263 if (printInstruction(MI))
264 return; // Printer was automatically generated
265
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000266 unsigned Opcode = MI->getOpcode();
267 const TargetInstrInfo &TII = *TM.getInstrInfo();
268 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000269 unsigned i;
Misha Brukmanc6cc10f2004-06-25 19:24:52 +0000270
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000271 unsigned ArgCount = MI->getNumOperands();
272 unsigned ArgType[] = {
Misha Brukman5b570812004-08-10 22:47:03 +0000273 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
274 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
275 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
276 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
277 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
Misha Brukman22e12072004-06-25 15:11:34 +0000278 };
Misha Brukman5b570812004-08-10 22:47:03 +0000279 assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000280 "Instruction requires VMX support");
Misha Brukman5b570812004-08-10 22:47:03 +0000281 assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
Misha Brukman46fd00a2004-06-24 23:04:11 +0000282 "Instruction requires 64 bit support");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000283
Misha Brukman61114612004-07-20 00:42:19 +0000284 // CALLpcrel and CALLindirect are handled specially here to print only the
285 // appropriate number of args that the assembler expects. This is because
286 // may have many arguments appended to record the uses of registers that are
287 // holding arguments to the called function.
Misha Brukman5b570812004-08-10 22:47:03 +0000288 if (Opcode == PPC::COND_BRANCH) {
Misha Brukmanab967902004-07-27 18:40:39 +0000289 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
290 abort();
Misha Brukman5b570812004-08-10 22:47:03 +0000291 } else if (Opcode == PPC::IMPLICIT_DEF) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000292 --EmittedInsts; // Not an actual machine instruction
Misha Brukman29188c62004-07-16 19:01:13 +0000293 O << "; IMPLICIT DEF ";
294 printOp(MI->getOperand(0));
295 O << "\n";
296 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000297 } else if (Opcode == PPC::CALLpcrel) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000298 O << TII.getName(Opcode) << " ";
Misha Brukman61114612004-07-20 00:42:19 +0000299 printOp(MI->getOperand(0));
300 O << "\n";
301 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000302 } else if (Opcode == PPC::CALLindirect) {
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000303 O << TII.getName(Opcode) << " ";
304 printImmOp(MI->getOperand(0), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000305 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000306 printImmOp(MI->getOperand(1), ArgType[0]);
Misha Brukman61114612004-07-20 00:42:19 +0000307 O << "\n";
308 return;
Misha Brukman5b570812004-08-10 22:47:03 +0000309 } else if (Opcode == PPC::MovePCtoLR) {
Nate Begeman81d265d2004-08-19 05:20:54 +0000310 ++EmittedInsts; // Actually two machine instructions
Misha Brukman61114612004-07-20 00:42:19 +0000311 // FIXME: should probably be converted to cout.width and cout.fill
Misha Brukmancf8d2442004-07-26 16:28:33 +0000312 O << "bl \"L0000" << LabelNumber << "$pb\"\n";
313 O << "\"L0000" << LabelNumber << "$pb\":\n";
Misha Brukman218bec72004-06-29 17:13:26 +0000314 O << "\tmflr ";
315 printOp(MI->getOperand(0));
Misha Brukman218bec72004-06-29 17:13:26 +0000316 O << "\n";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000317 return;
318 }
319
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000320 O << TII.getName(Opcode) << " ";
Nate Begeman81d265d2004-08-19 05:20:54 +0000321 if (Opcode == PPC::LOADHiAddr) {
322 printOp(MI->getOperand(0));
323 O << ", ";
324 if (MI->getOperand(1).getReg() == PPC::R0)
325 O << "0";
326 else
327 printOp(MI->getOperand(1));
328 O << ", ha16(" ;
329 printOp(MI->getOperand(2), true /* LoadAddrOp */);
330 O << "-\"L0000" << LabelNumber << "$pb\")\n";
331 } else if (ArgCount == 3 && (MI->getOperand(2).isConstantPoolIndex()
332 || MI->getOperand(2).isGlobalAddress())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000333 printOp(MI->getOperand(0));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000334 O << ", lo16(";
Nate Begemanb73a7112004-08-13 09:32:01 +0000335 printOp(MI->getOperand(2), true /* LoadAddrOp */);
Misha Brukmancf8d2442004-07-26 16:28:33 +0000336 O << "-\"L0000" << LabelNumber << "$pb\")";
Misha Brukman218bec72004-06-29 17:13:26 +0000337 O << "(";
Misha Brukman5b570812004-08-10 22:47:03 +0000338 if (MI->getOperand(1).getReg() == PPC::R0)
Misha Brukman218bec72004-06-29 17:13:26 +0000339 O << "0";
340 else
341 printOp(MI->getOperand(1));
342 O << ")\n";
Misha Brukman5b570812004-08-10 22:47:03 +0000343 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000344 printOp(MI->getOperand(0));
345 O << ", ";
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000346 printImmOp(MI->getOperand(1), ArgType[1]);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000347 O << "(";
Misha Brukmanb9e8f972004-06-30 21:54:12 +0000348 if (MI->getOperand(2).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000349 MI->getOperand(2).getReg() == PPC::R0)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000350 O << "0";
351 else
352 printOp(MI->getOperand(2));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000353 O << ")\n";
354 } else {
Misha Brukman7f484a52004-06-24 23:51:00 +0000355 for (i = 0; i < ArgCount; ++i) {
Misha Brukmanab967902004-07-27 18:40:39 +0000356 // addi and friends
Misha Brukman5b570812004-08-10 22:47:03 +0000357 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
Misha Brukman4363bdb2004-07-01 21:09:12 +0000358 MI->getOperand(1).hasAllocatedReg() &&
Misha Brukman5b570812004-08-10 22:47:03 +0000359 MI->getOperand(1).getReg() == PPC::R0) {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000360 O << "0";
Misha Brukmanab967902004-07-27 18:40:39 +0000361 // for long branch support, bc $+8
362 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
363 TII.isBranch(MI->getOpcode())) {
364 O << "$+8";
365 assert(8 == MI->getOperand(i).getImmedValue()
366 && "branch off PC not to pc+8?");
367 //printOp(MI->getOperand(i));
Misha Brukmanaf313fb2004-07-28 00:00:48 +0000368 } else if (MI->getOperand(i).isImmediate()) {
369 printImmOp(MI->getOperand(i), ArgType[i]);
Misha Brukman218bec72004-06-29 17:13:26 +0000370 } else {
Misha Brukman46fd00a2004-06-24 23:04:11 +0000371 printOp(MI->getOperand(i));
372 }
Misha Brukman7f484a52004-06-24 23:51:00 +0000373 if (ArgCount - 1 == i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000374 O << "\n";
375 else
376 O << ", ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000377 }
378 }
Nate Begemane59bf592004-08-14 22:09:10 +0000379 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000380}
381
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382// SwitchSection - Switch to the specified section of the executable if we are
383// not already in it!
384//
385static void SwitchSection(std::ostream &OS, std::string &CurSection,
386 const char *NewSection) {
387 if (CurSection != NewSection) {
388 CurSection = NewSection;
389 if (!CurSection.empty())
390 OS << "\t" << NewSection << "\n";
391 }
392}
393
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000394bool PPC32AsmPrinter::doFinalization(Module &M) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000395 const TargetData &TD = TM.getTargetData();
396 std::string CurSection;
397
398 // Print out module-level global variables here.
399 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
400 if (I->hasInitializer()) { // External global require no code
401 O << "\n\n";
402 std::string name = Mang->getValueName(I);
403 Constant *C = I->getInitializer();
404 unsigned Size = TD.getTypeSize(C->getType());
Chris Lattner69d485e2004-08-17 19:26:03 +0000405 unsigned Align = TD.getTypeAlignmentShift(C->getType());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000406
Misha Brukman97a296f2004-07-21 20:11:11 +0000407 if (C->isNullValue() && /* FIXME: Verify correct */
408 (I->hasInternalLinkage() || I->hasWeakLinkage())) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000409 SwitchSection(O, CurSection, ".data");
410 if (I->hasInternalLinkage())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000411 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner69d485e2004-08-17 19:26:03 +0000412 << "," << Align;
Misha Brukman218bec72004-06-29 17:13:26 +0000413 else
Misha Brukmane2eceb52004-07-23 16:08:20 +0000414 O << ".comm " << name << "," << TD.getTypeSize(C->getType());
Misha Brukman218bec72004-06-29 17:13:26 +0000415 O << "\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000416 WriteAsOperand(O, I, true, true, &M);
417 O << "\n";
418 } else {
419 switch (I->getLinkage()) {
420 case GlobalValue::LinkOnceLinkage:
Misha Brukman97a296f2004-07-21 20:11:11 +0000421 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
422 << ".weak_definition " << name << '\n'
423 << ".private_extern " << name << '\n'
424 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
425 LinkOnceStubs.insert(name);
426 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000427 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
428 // Nonnull linkonce -> weak
429 O << "\t.weak " << name << "\n";
430 SwitchSection(O, CurSection, "");
431 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
432 break;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000433 case GlobalValue::AppendingLinkage:
434 // FIXME: appending linkage variables should go into a section of
435 // their name or something. For now, just emit them as external.
436 case GlobalValue::ExternalLinkage:
437 // If external or appending, declare as a global symbol
438 O << "\t.globl " << name << "\n";
439 // FALL THROUGH
440 case GlobalValue::InternalLinkage:
Misha Brukman61297ee2004-06-29 23:40:57 +0000441 SwitchSection(O, CurSection, ".data");
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000442 break;
443 }
444
Chris Lattner69d485e2004-08-17 19:26:03 +0000445 emitAlignment(Align);
Misha Brukman218bec72004-06-29 17:13:26 +0000446 O << name << ":\t\t\t\t; ";
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000447 WriteAsOperand(O, I, true, true, &M);
448 O << " = ";
449 WriteAsOperand(O, C, false, false, &M);
450 O << "\n";
451 emitGlobalConstant(C);
452 }
453 }
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000454
455 // Output stubs for dynamically-linked functions
456 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
457 i != e; ++i)
Misha Brukman46fd00a2004-06-24 23:04:11 +0000458 {
Misha Brukmane2eceb52004-07-23 16:08:20 +0000459 O << ".data\n";
460 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
Chris Lattner69d485e2004-08-17 19:26:03 +0000461 emitAlignment(2);
Misha Brukman46fd00a2004-06-24 23:04:11 +0000462 O << "L" << *i << "$stub:\n";
463 O << "\t.indirect_symbol " << *i << "\n";
464 O << "\tmflr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000465 O << "\tbcl 20,31,L0$" << *i << "\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000466 O << "L0$" << *i << ":\n";
467 O << "\tmflr r11\n";
468 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
469 O << "\tmtlr r0\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000470 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000471 O << "\tmtctr r12\n";
472 O << "\tbctr\n";
473 O << ".data\n";
474 O << ".lazy_symbol_pointer\n";
475 O << "L" << *i << "$lazy_ptr:\n";
Misha Brukmane2eceb52004-07-23 16:08:20 +0000476 O << "\t.indirect_symbol " << *i << "\n";
477 O << "\t.long dyld_stub_binding_helper\n";
Misha Brukman46fd00a2004-06-24 23:04:11 +0000478 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000479
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000480 O << "\n";
481
482 // Output stubs for external global variables
483 if (GVStubs.begin() != GVStubs.end())
Misha Brukmane2eceb52004-07-23 16:08:20 +0000484 O << ".data\n.non_lazy_symbol_pointer\n";
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000485 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
486 i != e; ++i) {
487 O << "L" << *i << "$non_lazy_ptr:\n";
488 O << "\t.indirect_symbol " << *i << "\n";
489 O << "\t.long\t0\n";
490 }
491
Nate Begemane59bf592004-08-14 22:09:10 +0000492 // Output stubs for link-once variables
493 if (LinkOnceStubs.begin() != LinkOnceStubs.end())
494 O << ".data\n.align 2\n";
495 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
496 e = LinkOnceStubs.end(); i != e; ++i) {
497 O << "L" << *i << "$non_lazy_ptr:\n"
498 << "\t.long\t" << *i << '\n';
499 }
500
Chris Lattnera3840792004-08-16 23:25:21 +0000501 AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000502 return false; // success
503}