1 //===-- EmitAssembly.cpp - Emit SparcV9 Specific .s File -------------------==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the stuff necessary to output a .s file from
11 // LLVM. The code in this file assumes that the specified module has already
12 // been compiled into the internal data structures of the Module.
14 // 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.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/Support/Mangler.h"
31 #include "Support/StringExtras.h"
32 #include "Support/Statistic.h"
33 #include "SparcV9Internals.h"
34 #include "MachineFunctionInfo.h"
39 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
43 struct SparcV9AsmPrinter : public AsmPrinter {
53 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";
62 const char *getPassName() const {
63 return "SparcV9 Assembly Printer";
66 // Print a constant (which may be an aggregate) prefixed by all the
67 // appropriate directives. Uses printConstantValueOnly() to print the
69 void printConstant(const Constant* CV, const std::string &valID) {
70 emitAlignment(TM.getTargetData().getTypeAlignmentShift(CV->getType()));
71 O << "\t.type" << "\t" << valID << ",#object\n";
73 unsigned constSize = TM.getTargetData().getTypeSize(CV->getType());
74 O << "\t.size" << "\t" << valID << "," << constSize << "\n";
78 emitGlobalConstant(CV);
81 // enterSection - Use this method to enter a different section of the output
82 // executable. This is used to only output necessary section transitions.
84 void enterSection(enum Sections S) {
85 if (S == CurSection) return; // Only switch section if necessary
91 default: assert(0 && "Bad section name!");
92 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;
100 // getID Wrappers - Ensure consistent usage
101 // Symbol names in SparcV9 assembly language have these rules:
102 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
103 // (b) A name beginning in "." is treated as a local name.
104 std::string getID(const Function *F) {
105 return Mang->getValueName(F);
107 std::string getID(const BasicBlock *BB) {
108 return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB);
110 std::string getID(const GlobalVariable *GV) {
111 return Mang->getValueName(GV);
113 std::string getID(const Constant *CV) {
114 return ".C_" + Mang->getValueName(CV);
116 std::string getID(const GlobalValue *GV) {
117 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
119 else if (const Function *F = dyn_cast<Function>(GV))
121 assert(0 && "Unexpected type of GlobalValue!");
125 virtual bool runOnMachineFunction(MachineFunction &MF) {
126 setupMachineFunction(MF);
131 virtual bool doFinalization(Module &M) {
133 AsmPrinter::doFinalization(M);
137 void emitFunction(MachineFunction &F);
139 void emitBasicBlock(const MachineBasicBlock &MBB);
140 void emitMachineInst(const MachineInstr *MI);
142 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
143 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
145 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
146 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
148 unsigned getOperandMask(unsigned Opcode) {
151 case V9::SUBcci: return 1 << 3; // Remove CC argument
152 default: return 0; // By default, don't hack operands...
156 void emitGlobals(const Module &M);
157 void printGlobalVariable(const GlobalVariable *GV);
160 } // End anonymous namespace
163 SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
164 unsigned int opNum) {
165 switch (MI->getOpcode()) {
177 SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
178 unsigned int opNum) {
179 if (TM.getInstrInfo()->isLoad(MI->getOpcode()))
181 else if (TM.getInstrInfo()->isStore(MI->getOpcode()))
188 SparcV9AsmPrinter::printOperands(const MachineInstr *MI, unsigned opNum) {
189 const MachineOperand& mop = MI->getOperand(opNum);
190 if (OpIsBranchTargetLabel(MI, opNum)) {
191 printOneOperand(mop, MI->getOpcode());
193 printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
195 } else if (OpIsMemoryAddressBase(MI, opNum)) {
197 printOneOperand(mop, MI->getOpcode());
199 printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
203 printOneOperand(mop, MI->getOpcode());
209 SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop,
210 MachineOpCode opCode)
212 bool needBitsFlag = true;
214 if (mop.isHiBits32())
216 else if (mop.isLoBits32())
218 else if (mop.isHiBits64())
220 else if (mop.isLoBits64())
223 needBitsFlag = false;
225 switch (mop.getType())
227 case MachineOperand::MO_VirtualRegister:
228 case MachineOperand::MO_CCRegister:
229 case MachineOperand::MO_MachineRegister:
231 int regNum = (int)mop.getReg();
233 if (regNum == TM.getRegInfo()->getInvalidRegNum()) {
234 // better to print code with NULL registers than to die
237 O << "%" << TM.getRegInfo()->getUnifiedRegName(regNum);
242 case MachineOperand::MO_ConstantPoolIndex:
244 O << ".CPI_" << CurrentFnName << "_" << mop.getConstantPoolIndex();
248 case MachineOperand::MO_PCRelativeDisp:
250 const Value *Val = mop.getVRegValue();
251 assert(Val && "\tNULL Value in SparcV9AsmPrinter");
253 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
255 else if (const Function *F = dyn_cast<Function>(Val))
257 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
259 else if (const Constant *CV = dyn_cast<Constant>(Val))
262 assert(0 && "Unrecognized value in SparcV9AsmPrinter");
266 case MachineOperand::MO_SignExtendedImmed:
267 O << mop.getImmedValue();
270 case MachineOperand::MO_UnextendedImmed:
271 O << (uint64_t) mop.getImmedValue();
275 O << mop; // use dump field
283 void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) {
284 unsigned Opcode = MI->getOpcode();
286 if (Opcode == V9::PHI)
287 return; // Ignore Machine-PHI nodes.
289 O << "\t" << TM.getInstrInfo()->getName(Opcode) << "\t";
291 unsigned Mask = getOperandMask(Opcode);
293 bool NeedComma = false;
295 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
296 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
297 if (NeedComma) O << ", "; // Handle comma outputting
299 N = printOperands(MI, OpNum);
307 void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
308 // Emit a label for the basic block
309 O << getID(MBB.getBasicBlock()) << ":\n";
311 // Loop over all of the instructions in the basic block...
312 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
314 emitMachineInst(MII);
315 O << "\n"; // Separate BB's with newlines
318 void SparcV9AsmPrinter::emitFunction(MachineFunction &MF) {
319 O << "!****** Outputing Function: " << CurrentFnName << " ******\n";
321 // Emit constant pool for this function
322 const MachineConstantPool *MCP = MF.getConstantPool();
323 const std::vector<Constant*> &CP = MCP->getConstants();
325 enterSection(ReadOnlyData);
326 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
327 std::string cpiName = ".CPI_" + CurrentFnName + "_" + utostr(i);
328 printConstant(CP[i], cpiName);
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";
337 // Output code for all of the basic blocks in the function...
338 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
341 // Output a .size directive so the debugger knows the extents of the function
342 O << ".EndOf_" << CurrentFnName << ":\n\t.size "
343 << CurrentFnName << ", .EndOf_"
344 << CurrentFnName << "-" << CurrentFnName << "\n";
346 // Put some spaces between the functions
350 void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
351 if (GV->hasExternalLinkage())
352 O << "\t.global\t" << getID(GV) << "\n";
354 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue()) {
355 printConstant(GV->getInitializer(), getID(GV));
357 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())
366 void SparcV9AsmPrinter::emitGlobals(const Module &M) {
367 // Output global variables...
368 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())
372 enterSection(ReadOnlyData); // read-only, initialized data
373 else if (GI->getInitializer()->isNullValue())
374 enterSection(ZeroInitRWData); // read-write zero data
376 enterSection(InitRWData); // read-write non-zero data
378 printGlobalVariable(GI);
384 FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out, TargetMachine &TM) {
385 return new SparcV9AsmPrinter(Out, TM);