1 //===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
3 // This file implements all of the stuff neccesary to output a .s file from
4 // LLVM. The code in this file assumes that the specified module has already
5 // been compiled into the internal data structures of the Module.
7 // The entry point of this file is the UltraSparc::emitAssembly method.
9 //===----------------------------------------------------------------------===//
11 #include "SparcInternals.h"
12 #include "llvm/Analysis/SlotCalculator.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Method.h"
16 #include "llvm/Module.h"
17 #include "llvm/Support/StringExtras.h"
21 class SparcAsmPrinter {
24 const UltraSparc &Target;
33 inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
34 : Out(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
39 void emitModule(const Module *M);
41 void processSymbolTable(const SymbolTable &ST);
42 void processConstant(const ConstPoolVal *CPV);
43 void processGlobal(const GlobalVariable *GV);
45 void emitMethod(const Method *M);
46 //void processMethodArgument(const MethodArgument *MA);
47 void emitBasicBlock(const BasicBlock *BB);
48 void emitMachineInst(const MachineInstr *MI);
49 void printOperand(const MachineOperand &Op);
52 // enterSection - Use this method to enter a different section of the output
53 // executable. This is used to only output neccesary section transitions.
55 void enterSection(enum Sections S) {
56 if (S == CurSection) return; // Only switch section if neccesary
59 Out << ".section \".";
61 default: assert(0 && "Bad section name!");
62 case Text: Out << "text"; break;
63 case Data: Out << "data"; break;
64 case ReadOnly: Out << "rodata"; break;
69 string getEscapedString(const string &S) {
72 for (unsigned i = 0; i < S.size(); ++i) {
74 if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
75 (C >= '0' && C <= '9')) {
79 Result += char('0' + ((unsigned char)C >> 4));
80 Result += char('0' + (C & 0xF));
86 // getID - Return a valid identifier for the specified value. Base it on
87 // the name of the identifier if possible, use a numbered value based on
88 // prefix otherwise. FPrefix is always prepended to the output identifier.
90 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
91 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
93 return FP + getEscapedString(V->getName());
95 assert(Table.getValSlot(V) != -1 && "Value not in value table!");
96 return FP + string(Prefix) + itostr(Table.getValSlot(V));
100 // getID Wrappers - Ensure consistent usage...
101 string getID(const Method *M) { return getID(M, "anon_method$"); }
102 string getID(const BasicBlock *BB) {
103 return getID(BB, "LL", (".L$"+getID(BB->getParent())+"$").c_str());
106 unsigned getOperandMask(unsigned Opcode) {
108 case SUBcc: return 1 << 3; // Remove CC argument
109 case BA: case BRZ: // Remove Arg #0, which is always null or xcc
110 case BRLEZ: case BRLZ:
111 case BRNZ: case BRGZ:
112 case BRGEZ: return 1 << 0;
113 case RETURN: return 1 << 1; // Remove Arg #2 which is zero
115 default: return 0; // By default, don't hack operands...
121 void SparcAsmPrinter::printOperand(const MachineOperand &Op) {
122 switch (Op.getOperandType()) {
123 case MachineOperand::MO_VirtualRegister:
124 case MachineOperand::MO_CCRegister:
125 case MachineOperand::MO_MachineRegister: {
126 int RegNum = (int)Op.getAllocatedRegNum();
128 // ****this code is temporary till NULL Values are fixed
129 if (RegNum == 10000) {
130 Out << "<NULL VALUE>";
132 Out << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
137 case MachineOperand::MO_PCRelativeDisp: {
138 const Value *Val = Op.getVRegValue();
140 Out << "\t<*NULL Value*>";
141 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) {
144 Out << "<unknown value=" << Val << ">";
150 Out << Op; // use dump field
156 void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
157 unsigned Opcode = MI->getOpCode();
159 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
160 return; // IGNORE PHI NODES
162 Out << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
164 switch (Opcode) { // Some opcodes have special syntax...
166 assert(MI->getNumOperands() == 3 && "Unexpected JMPL instr!");
167 printOperand(MI->getOperand(0));
169 printOperand(MI->getOperand(0));
171 printOperand(MI->getOperand(0));
177 unsigned Mask = getOperandMask(Opcode);
179 bool NeedComma = false;
180 for(unsigned OpNum = 0; OpNum < MI->getNumOperands(); ++OpNum) {
181 if ((1 << OpNum) & Mask) continue; // Ignore this operand?
183 const MachineOperand &Op = MI->getOperand(OpNum);
184 if (NeedComma) Out << ", "; // Handle comma outputing
192 void SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) {
193 // Emit a label for the basic block
194 Out << getID(BB) << ":\n";
196 // Get the vector of machine instructions corresponding to this bb.
197 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
198 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
200 // Loop over all of the instructions in the basic block...
201 for (; MII != MIE; ++MII)
202 emitMachineInst(*MII);
203 Out << "\n"; // Seperate BB's with newlines
206 void SparcAsmPrinter::emitMethod(const Method *M) {
207 if (M->isExternal()) return;
209 // Make sure the slot table has information about this method...
210 Table.incorporateMethod(M);
212 string MethName = getID(M);
213 Out << "!****** Outputing Method: " << MethName << " ******\n";
215 Out << "\t.align 4\n\t.global\t" << MethName << "\n";
216 //Out << "\t.type\t" << MethName << ",#function\n";
217 Out << "\t.type\t" << MethName << ", 2\n";
218 Out << MethName << ":\n";
220 // Output code for all of the basic blocks in the method...
221 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
224 // Output a .size directive so the debugger knows the extents of the function
225 Out << ".EndOf$" << MethName << ":\n\t.size " << MethName << ", .EndOf$"
226 << MethName << "-" << MethName << endl;
228 // Put some spaces between the methods
231 // Forget all about M.
236 void SparcAsmPrinter::emitModule(const Module *M) {
237 // TODO: Look for a filename annotation on M to emit a .file directive
238 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
242 } // End anonymous namespace
245 // emitAssembly - Output assembly language code (a .s file) for the specified
246 // method. The specified method must have been compiled before this may be
249 void UltraSparc::emitAssembly(const Module *M, ostream &Out) const {
250 SparcAsmPrinter Print(Out, M, *this);