blob: dbfc1e1e0ca112790d0b470b5cd7ec75449e5537 [file] [log] [blame]
Brian Gaeke03cac372004-04-25 07:04:49 +00001//===-- EmitAssembly.cpp - Emit SparcV9 Specific .s File -------------------==//
John Criswell482202a2003-10-20 19:43:21 +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//===----------------------------------------------------------------------===//
Chris Lattner036a3172001-09-19 13:47:27 +00009//
Misha Brukman7eb05a12003-08-18 14:43:39 +000010// This file implements all of the stuff necessary to output a .s file from
Chris Lattner036a3172001-09-19 13:47:27 +000011// LLVM. The code in this file assumes that the specified module has already
12// been compiled into the internal data structures of the Module.
13//
Chris Lattnerc8e66542002-04-27 06:56:12 +000014// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
15// The FunctionPass is pipelined together with all of the rest of the code
16// generation stages, and the Pass runs at the end to emit code for global
17// variables and such.
Chris Lattner036a3172001-09-19 13:47:27 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattnerca142372002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Vikram S. Adve71b265a2001-10-28 21:38:52 +000022#include "llvm/DerivedTypes.h"
Chris Lattner036a3172001-09-19 13:47:27 +000023#include "llvm/Module.h"
Chris Lattner63c52f32002-04-28 20:40:59 +000024#include "llvm/Pass.h"
Chris Lattnerc3728c22002-04-18 18:15:38 +000025#include "llvm/Assembly/Writer.h"
Chris Lattner12754fe2004-08-18 05:29:08 +000026#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman0f425852003-12-17 22:06:28 +000027#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFunction.h"
Misha Brukman0f425852003-12-17 22:06:28 +000029#include "llvm/CodeGen/MachineInstr.h"
Misha Brukman929d1d12004-01-15 22:44:19 +000030#include "llvm/Support/Mangler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/Statistic.h"
Brian Gaeke94e95d22004-02-25 18:44:15 +000033#include "SparcV9Internals.h"
Chris Lattner9fb30a42004-08-16 21:55:02 +000034#include "MachineFunctionInfo.h"
Misha Brukman1e88cfb2003-08-05 16:01:50 +000035#include <string>
Misha Brukman6675f982003-11-13 00:22:19 +000036using namespace llvm;
37
Chris Lattner036a3172001-09-19 13:47:27 +000038namespace {
Misha Brukman6675f982003-11-13 00:22:19 +000039 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Chris Lattner12754fe2004-08-18 05:29:08 +000040}
Vikram S. Adve270f56a2002-03-18 03:07:26 +000041
Misha Brukman6675f982003-11-13 00:22:19 +000042namespace {
Chris Lattner12754fe2004-08-18 05:29:08 +000043 struct SparcV9AsmPrinter : public AsmPrinter {
Misha Brukman6675f982003-11-13 00:22:19 +000044 public:
Misha Brukman6675f982003-11-13 00:22:19 +000045 enum Sections {
46 Unknown,
47 Text,
48 ReadOnlyData,
49 InitRWData,
50 ZeroInitRWData,
51 } CurSection;
52
Chris Lattner12754fe2004-08-18 05:29:08 +000053 SparcV9AsmPrinter(std::ostream &OS, TargetMachine &TM)
54 : AsmPrinter(OS, TM), CurSection(Unknown) {
55 ZeroDirective = 0; // No way to get zeros.
56 Data16bitsDirective = "\t.half\t";
57 Data32bitsDirective = "\t.word\t";
58 Data64bitsDirective = "\t.xword\t";
59 CommentString = "!";
Misha Brukman929d1d12004-01-15 22:44:19 +000060 }
61
Chris Lattner12754fe2004-08-18 05:29:08 +000062 const char *getPassName() const {
63 return "SparcV9 Assembly Printer";
Misha Brukman2133b052003-11-07 17:45:28 +000064 }
Misha Brukman2133b052003-11-07 17:45:28 +000065
Misha Brukman6675f982003-11-13 00:22:19 +000066 // Print a constant (which may be an aggregate) prefixed by all the
67 // appropriate directives. Uses printConstantValueOnly() to print the
68 // value or values.
Chris Lattner12754fe2004-08-18 05:29:08 +000069 void printConstant(const Constant* CV, const std::string &valID) {
70 emitAlignment(TM.getTargetData().getTypeAlignmentShift(CV->getType()));
71 O << "\t.type" << "\t" << valID << ",#object\n";
Misha Brukman6675f982003-11-13 00:22:19 +000072
Chris Lattner12754fe2004-08-18 05:29:08 +000073 unsigned constSize = TM.getTargetData().getTypeSize(CV->getType());
74 O << "\t.size" << "\t" << valID << "," << constSize << "\n";
Misha Brukman6675f982003-11-13 00:22:19 +000075
Chris Lattner12754fe2004-08-18 05:29:08 +000076 O << valID << ":\n";
Misha Brukman6675f982003-11-13 00:22:19 +000077
Chris Lattner12754fe2004-08-18 05:29:08 +000078 emitGlobalConstant(CV);
Misha Brukman6675f982003-11-13 00:22:19 +000079 }
80
Misha Brukman6675f982003-11-13 00:22:19 +000081 // enterSection - Use this method to enter a different section of the output
82 // executable. This is used to only output necessary section transitions.
83 //
84 void enterSection(enum Sections S) {
85 if (S == CurSection) return; // Only switch section if necessary
86 CurSection = S;
Misha Brukman2133b052003-11-07 17:45:28 +000087
Chris Lattner12754fe2004-08-18 05:29:08 +000088 O << "\n\t.section ";
Misha Brukman6675f982003-11-13 00:22:19 +000089 switch (S)
Vikram S. Adve96b801a2003-05-31 07:27:17 +000090 {
91 default: assert(0 && "Bad section name!");
Chris Lattner12754fe2004-08-18 05:29:08 +000092 case Text: O << "\".text\""; break;
93 case ReadOnlyData: O << "\".rodata\",#alloc"; break;
94 case InitRWData: O << "\".data\",#alloc,#write"; break;
95 case ZeroInitRWData: O << "\".bss\",#alloc,#write"; break;
Vikram S. Adve96b801a2003-05-31 07:27:17 +000096 }
Chris Lattner12754fe2004-08-18 05:29:08 +000097 O << "\n";
Misha Brukman6675f982003-11-13 00:22:19 +000098 }
Chris Lattner036a3172001-09-19 13:47:27 +000099
Misha Brukman929d1d12004-01-15 22:44:19 +0000100 // getID Wrappers - Ensure consistent usage
Brian Gaeke94e95d22004-02-25 18:44:15 +0000101 // Symbol names in SparcV9 assembly language have these rules:
Misha Brukman929d1d12004-01-15 22:44:19 +0000102 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
103 // (b) A name beginning in "." is treated as a local name.
Misha Brukman6675f982003-11-13 00:22:19 +0000104 std::string getID(const Function *F) {
Misha Brukman929d1d12004-01-15 22:44:19 +0000105 return Mang->getValueName(F);
Misha Brukman6675f982003-11-13 00:22:19 +0000106 }
107 std::string getID(const BasicBlock *BB) {
Misha Brukman929d1d12004-01-15 22:44:19 +0000108 return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB);
Misha Brukman6675f982003-11-13 00:22:19 +0000109 }
110 std::string getID(const GlobalVariable *GV) {
Misha Brukman929d1d12004-01-15 22:44:19 +0000111 return Mang->getValueName(GV);
Misha Brukman6675f982003-11-13 00:22:19 +0000112 }
113 std::string getID(const Constant *CV) {
Misha Brukman929d1d12004-01-15 22:44:19 +0000114 return ".C_" + Mang->getValueName(CV);
Misha Brukman6675f982003-11-13 00:22:19 +0000115 }
116 std::string getID(const GlobalValue *GV) {
117 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
118 return getID(V);
119 else if (const Function *F = dyn_cast<Function>(GV))
120 return getID(F);
121 assert(0 && "Unexpected type of GlobalValue!");
122 return "";
123 }
Vikram S. Advec3a5e4e2002-08-22 02:58:36 +0000124
Chris Lattner12754fe2004-08-18 05:29:08 +0000125 virtual bool runOnMachineFunction(MachineFunction &MF) {
126 setupMachineFunction(MF);
127 emitFunction(MF);
Misha Brukman6675f982003-11-13 00:22:19 +0000128 return false;
129 }
130
131 virtual bool doFinalization(Module &M) {
132 emitGlobals(M);
Chris Lattner12754fe2004-08-18 05:29:08 +0000133 AsmPrinter::doFinalization(M);
Misha Brukman6675f982003-11-13 00:22:19 +0000134 return false;
135 }
136
Chris Lattner12754fe2004-08-18 05:29:08 +0000137 void emitFunction(MachineFunction &F);
Misha Brukman6675f982003-11-13 00:22:19 +0000138 private :
139 void emitBasicBlock(const MachineBasicBlock &MBB);
140 void emitMachineInst(const MachineInstr *MI);
141
142 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
143 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
144
145 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
146 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
147
148 unsigned getOperandMask(unsigned Opcode) {
149 switch (Opcode) {
150 case V9::SUBccr:
151 case V9::SUBcci: return 1 << 3; // Remove CC argument
152 default: return 0; // By default, don't hack operands...
153 }
154 }
155
156 void emitGlobals(const Module &M);
157 void printGlobalVariable(const GlobalVariable *GV);
158 };
159
160} // End anonymous namespace
Chris Lattner036a3172001-09-19 13:47:27 +0000161
Chris Lattnerd6820452002-02-03 23:41:08 +0000162inline bool
Brian Gaeke94e95d22004-02-25 18:44:15 +0000163SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
Misha Brukman6675f982003-11-13 00:22:19 +0000164 unsigned int opNum) {
Brian Gaekeb22186a2004-02-11 20:47:34 +0000165 switch (MI->getOpcode()) {
Misha Brukman8bde6a62003-05-27 22:35:43 +0000166 case V9::JMPLCALLr:
167 case V9::JMPLCALLi:
168 case V9::JMPLRETr:
169 case V9::JMPLRETi:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000170 return (opNum == 0);
171 default:
172 return false;
Chris Lattnerd6820452002-02-03 23:41:08 +0000173 }
174}
175
Chris Lattnerd6820452002-02-03 23:41:08 +0000176inline bool
Brian Gaeke94e95d22004-02-25 18:44:15 +0000177SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
Misha Brukman6675f982003-11-13 00:22:19 +0000178 unsigned int opNum) {
Chris Lattner12754fe2004-08-18 05:29:08 +0000179 if (TM.getInstrInfo()->isLoad(MI->getOpcode()))
Chris Lattnerd6820452002-02-03 23:41:08 +0000180 return (opNum == 0);
Chris Lattner12754fe2004-08-18 05:29:08 +0000181 else if (TM.getInstrInfo()->isStore(MI->getOpcode()))
Chris Lattnerd6820452002-02-03 23:41:08 +0000182 return (opNum == 1);
183 else
184 return false;
185}
186
Chris Lattnerd6820452002-02-03 23:41:08 +0000187unsigned int
Chris Lattner12754fe2004-08-18 05:29:08 +0000188SparcV9AsmPrinter::printOperands(const MachineInstr *MI, unsigned opNum) {
Vikram S. Adve4e1ee142002-07-10 21:41:21 +0000189 const MachineOperand& mop = MI->getOperand(opNum);
Misha Brukman6675f982003-11-13 00:22:19 +0000190 if (OpIsBranchTargetLabel(MI, opNum)) {
Chris Lattner12754fe2004-08-18 05:29:08 +0000191 printOneOperand(mop, MI->getOpcode());
192 O << "+";
193 printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
Misha Brukman6675f982003-11-13 00:22:19 +0000194 return 2;
195 } else if (OpIsMemoryAddressBase(MI, opNum)) {
Chris Lattner12754fe2004-08-18 05:29:08 +0000196 O << "[";
197 printOneOperand(mop, MI->getOpcode());
198 O << "+";
199 printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
200 O << "]";
Misha Brukman6675f982003-11-13 00:22:19 +0000201 return 2;
202 } else {
Brian Gaekeb22186a2004-02-11 20:47:34 +0000203 printOneOperand(mop, MI->getOpcode());
Misha Brukman6675f982003-11-13 00:22:19 +0000204 return 1;
205 }
Chris Lattnerd6820452002-02-03 23:41:08 +0000206}
207
Chris Lattnerd6820452002-02-03 23:41:08 +0000208void
Brian Gaeke94e95d22004-02-25 18:44:15 +0000209SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop,
Chris Lattner12754fe2004-08-18 05:29:08 +0000210 MachineOpCode opCode)
Chris Lattnerd6820452002-02-03 23:41:08 +0000211{
Vikram S. Adve4e1ee142002-07-10 21:41:21 +0000212 bool needBitsFlag = true;
213
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000214 if (mop.isHiBits32())
Chris Lattner12754fe2004-08-18 05:29:08 +0000215 O << "%lm(";
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000216 else if (mop.isLoBits32())
Chris Lattner12754fe2004-08-18 05:29:08 +0000217 O << "%lo(";
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000218 else if (mop.isHiBits64())
Chris Lattner12754fe2004-08-18 05:29:08 +0000219 O << "%hh(";
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000220 else if (mop.isLoBits64())
Chris Lattner12754fe2004-08-18 05:29:08 +0000221 O << "%hm(";
Vikram S. Adve4e1ee142002-07-10 21:41:21 +0000222 else
223 needBitsFlag = false;
224
Chris Lattner6a30b022002-10-28 04:45:29 +0000225 switch (mop.getType())
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000226 {
Vikram S. Adved09c4c32003-07-06 20:13:59 +0000227 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve65280672003-07-10 19:42:11 +0000228 case MachineOperand::MO_CCRegister:
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000229 case MachineOperand::MO_MachineRegister:
230 {
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +0000231 int regNum = (int)mop.getReg();
Vikram S. Adve65280672003-07-10 19:42:11 +0000232
Chris Lattner12754fe2004-08-18 05:29:08 +0000233 if (regNum == TM.getRegInfo()->getInvalidRegNum()) {
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000234 // better to print code with NULL registers than to die
Chris Lattner12754fe2004-08-18 05:29:08 +0000235 O << "<NULL VALUE>";
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000236 } else {
Chris Lattner12754fe2004-08-18 05:29:08 +0000237 O << "%" << TM.getRegInfo()->getUnifiedRegName(regNum);
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000238 }
239 break;
240 }
Misha Brukman8d531672003-05-31 06:22:37 +0000241
Misha Brukman2133b052003-11-07 17:45:28 +0000242 case MachineOperand::MO_ConstantPoolIndex:
243 {
Chris Lattner12754fe2004-08-18 05:29:08 +0000244 O << ".CPI_" << CurrentFnName << "_" << mop.getConstantPoolIndex();
Misha Brukman2133b052003-11-07 17:45:28 +0000245 break;
246 }
247
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000248 case MachineOperand::MO_PCRelativeDisp:
249 {
250 const Value *Val = mop.getVRegValue();
Brian Gaeke94e95d22004-02-25 18:44:15 +0000251 assert(Val && "\tNULL Value in SparcV9AsmPrinter");
Misha Brukman8d531672003-05-31 06:22:37 +0000252
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000253 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Chris Lattner12754fe2004-08-18 05:29:08 +0000254 O << getID(BB);
Brian Gaekef2ff5dd2004-05-04 21:09:02 +0000255 else if (const Function *F = dyn_cast<Function>(Val))
Chris Lattner12754fe2004-08-18 05:29:08 +0000256 O << getID(F);
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000257 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattner12754fe2004-08-18 05:29:08 +0000258 O << getID(GV);
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000259 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattner12754fe2004-08-18 05:29:08 +0000260 O << getID(CV);
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000261 else
Brian Gaeke94e95d22004-02-25 18:44:15 +0000262 assert(0 && "Unrecognized value in SparcV9AsmPrinter");
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000263 break;
264 }
Misha Brukman8d531672003-05-31 06:22:37 +0000265
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000266 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner12754fe2004-08-18 05:29:08 +0000267 O << mop.getImmedValue();
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000268 break;
Misha Brukman8d531672003-05-31 06:22:37 +0000269
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000270 case MachineOperand::MO_UnextendedImmed:
Chris Lattner12754fe2004-08-18 05:29:08 +0000271 O << (uint64_t) mop.getImmedValue();
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000272 break;
Misha Brukman8d531672003-05-31 06:22:37 +0000273
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000274 default:
Chris Lattner12754fe2004-08-18 05:29:08 +0000275 O << mop; // use dump field
Vikram S. Adve96b801a2003-05-31 07:27:17 +0000276 break;
277 }
Vikram S. Adve4e1ee142002-07-10 21:41:21 +0000278
279 if (needBitsFlag)
Chris Lattner12754fe2004-08-18 05:29:08 +0000280 O << ")";
Chris Lattnerd6820452002-02-03 23:41:08 +0000281}
282
Brian Gaeke94e95d22004-02-25 18:44:15 +0000283void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) {
Brian Gaekeb22186a2004-02-11 20:47:34 +0000284 unsigned Opcode = MI->getOpcode();
Chris Lattnerd6820452002-02-03 23:41:08 +0000285
Brian Gaeke75935252004-08-18 20:04:24 +0000286 if (Opcode == V9::PHI)
287 return; // Ignore Machine-PHI nodes.
Chris Lattnerd6820452002-02-03 23:41:08 +0000288
Chris Lattner12754fe2004-08-18 05:29:08 +0000289 O << "\t" << TM.getInstrInfo()->getName(Opcode) << "\t";
Chris Lattnerd6820452002-02-03 23:41:08 +0000290
291 unsigned Mask = getOperandMask(Opcode);
292
293 bool NeedComma = false;
294 unsigned N = 1;
295 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
296 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Chris Lattner12754fe2004-08-18 05:29:08 +0000297 if (NeedComma) O << ", "; // Handle comma outputting
Chris Lattnerd6820452002-02-03 23:41:08 +0000298 NeedComma = true;
299 N = printOperands(MI, OpNum);
Chris Lattner59611022002-11-17 22:57:23 +0000300 } else
301 N = 1;
Chris Lattnerd6820452002-02-03 23:41:08 +0000302
Chris Lattner12754fe2004-08-18 05:29:08 +0000303 O << "\n";
Brian Gaeke4547ab12003-10-06 15:41:21 +0000304 ++EmittedInsts;
Chris Lattnerd6820452002-02-03 23:41:08 +0000305}
306
Brian Gaeke94e95d22004-02-25 18:44:15 +0000307void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
Chris Lattnerd6820452002-02-03 23:41:08 +0000308 // Emit a label for the basic block
Chris Lattner12754fe2004-08-18 05:29:08 +0000309 O << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerd6820452002-02-03 23:41:08 +0000310
311 // Loop over all of the instructions in the basic block...
Misha Brukman181ea4a2002-10-28 20:01:13 +0000312 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner8710aab2002-10-28 01:41:47 +0000313 MII != MIE; ++MII)
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000314 emitMachineInst(MII);
Chris Lattner12754fe2004-08-18 05:29:08 +0000315 O << "\n"; // Separate BB's with newlines
Chris Lattnerd6820452002-02-03 23:41:08 +0000316}
317
Chris Lattner12754fe2004-08-18 05:29:08 +0000318void SparcV9AsmPrinter::emitFunction(MachineFunction &MF) {
319 O << "!****** Outputing Function: " << CurrentFnName << " ******\n";
Misha Brukman2133b052003-11-07 17:45:28 +0000320
321 // Emit constant pool for this function
Chris Lattner12754fe2004-08-18 05:29:08 +0000322 const MachineConstantPool *MCP = MF.getConstantPool();
Misha Brukman2133b052003-11-07 17:45:28 +0000323 const std::vector<Constant*> &CP = MCP->getConstants();
324
Chris Lattner12754fe2004-08-18 05:29:08 +0000325 enterSection(ReadOnlyData);
Misha Brukman2133b052003-11-07 17:45:28 +0000326 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner12754fe2004-08-18 05:29:08 +0000327 std::string cpiName = ".CPI_" + CurrentFnName + "_" + utostr(i);
Misha Brukman2133b052003-11-07 17:45:28 +0000328 printConstant(CP[i], cpiName);
329 }
330
Chris Lattner12754fe2004-08-18 05:29:08 +0000331 enterSection(Text);
332 O << "\t.align\t4\n\t.global\t" << CurrentFnName << "\n";
333 //O << "\t.type\t" << CurrentFnName << ",#function\n";
334 O << "\t.type\t" << CurrentFnName << ", 2\n";
335 O << CurrentFnName << ":\n";
Chris Lattnerd6820452002-02-03 23:41:08 +0000336
Chris Lattner62b7fd12002-04-07 20:49:59 +0000337 // Output code for all of the basic blocks in the function...
Chris Lattner4d84d492002-12-28 20:15:01 +0000338 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukman181ea4a2002-10-28 20:01:13 +0000339 emitBasicBlock(*I);
Chris Lattnerd6820452002-02-03 23:41:08 +0000340
341 // Output a .size directive so the debugger knows the extents of the function
Chris Lattner12754fe2004-08-18 05:29:08 +0000342 O << ".EndOf_" << CurrentFnName << ":\n\t.size "
343 << CurrentFnName << ", .EndOf_"
344 << CurrentFnName << "-" << CurrentFnName << "\n";
Chris Lattnerd6820452002-02-03 23:41:08 +0000345
Chris Lattner62b7fd12002-04-07 20:49:59 +0000346 // Put some spaces between the functions
Chris Lattner12754fe2004-08-18 05:29:08 +0000347 O << "\n\n";
Chris Lattnerd6820452002-02-03 23:41:08 +0000348}
349
Brian Gaeke94e95d22004-02-25 18:44:15 +0000350void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
Vikram S. Adve8cbdbd82002-09-16 15:54:02 +0000351 if (GV->hasExternalLinkage())
Chris Lattner12754fe2004-08-18 05:29:08 +0000352 O << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve71b265a2001-10-28 21:38:52 +0000353
Misha Brukman6675f982003-11-13 00:22:19 +0000354 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue()) {
Vikram S. Adve71b265a2001-10-28 21:38:52 +0000355 printConstant(GV->getInitializer(), getID(GV));
Misha Brukman6675f982003-11-13 00:22:19 +0000356 } else {
Chris Lattner12754fe2004-08-18 05:29:08 +0000357 const Type *ValTy = GV->getType()->getElementType();
358 emitAlignment(TM.getTargetData().getTypeAlignmentShift(ValTy));
359 O << "\t.type\t" << getID(GV) << ",#object\n";
360 O << "\t.reserve\t" << getID(GV) << ","
361 << TM.getTargetData().getTypeSize(GV->getType()->getElementType())
362 << "\n";
Vikram S. Adve71b265a2001-10-28 21:38:52 +0000363 }
364}
365
Brian Gaeke94e95d22004-02-25 18:44:15 +0000366void SparcV9AsmPrinter::emitGlobals(const Module &M) {
Chris Lattnerc93df6b2002-08-07 21:39:48 +0000367 // Output global variables...
Vikram S. Adve10d81642002-10-13 00:32:18 +0000368 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
369 if (! GI->isExternal()) {
370 assert(GI->hasInitializer());
371 if (GI->isConstant())
Chris Lattner12754fe2004-08-18 05:29:08 +0000372 enterSection(ReadOnlyData); // read-only, initialized data
Vikram S. Adve10d81642002-10-13 00:32:18 +0000373 else if (GI->getInitializer()->isNullValue())
Chris Lattner12754fe2004-08-18 05:29:08 +0000374 enterSection(ZeroInitRWData); // read-write zero data
Vikram S. Adve10d81642002-10-13 00:32:18 +0000375 else
Chris Lattner12754fe2004-08-18 05:29:08 +0000376 enterSection(InitRWData); // read-write non-zero data
Vikram S. Adve10d81642002-10-13 00:32:18 +0000377
378 printGlobalVariable(GI);
Chris Lattnerc93df6b2002-08-07 21:39:48 +0000379 }
Chris Lattnerc93df6b2002-08-07 21:39:48 +0000380
Chris Lattner12754fe2004-08-18 05:29:08 +0000381 O << "\n";
Vikram S. Adve71b265a2001-10-28 21:38:52 +0000382}
383
Chris Lattner12754fe2004-08-18 05:29:08 +0000384FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out, TargetMachine &TM) {
Brian Gaeke94e95d22004-02-25 18:44:15 +0000385 return new SparcV9AsmPrinter(Out, TM);
Chris Lattnere772d282002-02-03 07:48:06 +0000386}