blob: 9014d216aa563cf27cd22ecbc4c71883c91af791 [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2//
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.
6//
7// The entry point of this file is the UltraSparc::emitAssembly method.
8//
9//===----------------------------------------------------------------------===//
10
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"
18
19namespace {
20
21class SparcAsmPrinter {
22 ostream &Out;
23 SlotCalculator Table;
24 UltraSparc &Target;
25
26 enum Sections {
27 Unknown,
28 Text,
29 Data,
30 ReadOnly,
31 } CurSection;
32public:
33 inline SparcAsmPrinter(ostream &o, const Module *M, UltraSparc &t)
34 : Out(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
35 emitModule(M);
36 }
37
38private :
39 void emitModule(const Module *M);
40 /*
41 void processSymbolTable(const SymbolTable &ST);
42 void processConstant(const ConstPoolVal *CPV);
43 void processGlobal(const GlobalVariable *GV);
44 */
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
50
51 //void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
52
53
54 // enterSection - Use this method to enter a different section of the output
55 // executable. This is used to only output neccesary section transitions.
56 //
57 void enterSection(enum Sections S) {
58 if (S == CurSection) return; // Only switch section if neccesary
59 CurSection = S;
60
61 Out << ".section \".";
62 switch (S) {
63 default: assert(0 && "Bad section name!");
64 case Text: Out << "text"; break;
65 case Data: Out << "data"; break;
66 case ReadOnly: Out << "rodata"; break;
67 }
68 Out << "\"\n";
69 }
70
71 // getID - Return a valid identifier for the specified value. Base it on
72 // the name of the identifier if possible, use a numbered value based on
73 // prefix otherwise. FPrefix is always prepended to the output identifier.
74 //
75 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
76 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
77 if (V->hasName()) {
78 return FP + V->getName(); // TODO: Escape name if needed
79 } else {
80 assert(Table.getValSlot(V) != -1 && "Value not in value table!");
81 return FP + string(Prefix) + itostr(Table.getValSlot(V));
82 }
83 }
84
85 // getID Wrappers - Ensure consistent usage...
86 string getID(const Method *M) { return getID(M, "anon_method$"); }
87 string getID(const BasicBlock *BB) {
88 return getID(BB, "LL", (".L$"+getID(BB->getParent())).c_str());
89 }
90
91 unsigned getOperandMask(unsigned Opcode) {
92 switch (Opcode) {
93 case SUBcc: return 1 << 3; // Remove CC argument
94 case BA: return 1 << 0; // Remove Arg #0, which is always null
95 default: return 0; // By default, don't hack operands...
96 }
97 }
98};
99
100
101void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
102 unsigned Opcode = MI->getOpCode();
103
104 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
105 return; // IGNORE PHI NODES
106
107 Out << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
108
109 unsigned Mask = getOperandMask(Opcode);
110
111 bool NeedComma = false;
112 for(unsigned OpNum = 0; OpNum < MI->getNumOperands(); ++OpNum) {
113 if ((1 << OpNum) & Mask) continue; // Ignore this operand?
114
115 const MachineOperand &Op = MI->getOperand(OpNum);
116 if (NeedComma) Out << ", "; // Handle comma outputing
117 NeedComma = true;
118
119 switch (Op.getOperandType()) {
120 case MachineOperand::MO_VirtualRegister:
121 case MachineOperand::MO_CCRegister:
122 case MachineOperand::MO_MachineRegister: {
123 int RegNum = (int)Op.getAllocatedRegNum();
124
125 // ****this code is temporary till NULL Values are fixed
126 if (RegNum == 10000) {
127 Out << "<NULL VALUE>";
128 continue;
129 }
130
131 Out << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
132 break;
133 }
134
135 case MachineOperand::MO_PCRelativeDisp: {
136 const Value *Val = Op.getVRegValue();
137 if (!Val) {
138 Out << "\t<*NULL Value*>";
139 } else if (Val->isBasicBlock()) {
140 Out << getID(Val->castBasicBlockAsserting());
141 } else {
142 Out << "<unknown value=" << Val << ">";
143 }
144 break;
145 }
146
147 default:
148 Out << Op; // use dump field
149 break;
150 }
151 }
152 Out << endl;
153}
154
155void SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) {
156 // Emit a label for the basic block
157 Out << getID(BB) << ":\n";
158
159 // Get the vector of machine instructions corresponding to this bb.
160 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
161 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
162
163 // Loop over all of the instructions in the basic block...
164 for (; MII != MIE; ++MII)
165 emitMachineInst(*MII);
166 Out << "\n"; // Seperate BB's with newlines
167}
168
169void SparcAsmPrinter::emitMethod(const Method *M) {
170 if (M->isExternal()) return;
171
172 // Make sure the slot table has information about this method...
173 Table.incorporateMethod(M);
174
175 string MethName = getID(M);
176 Out << "!****** Outputing Method: " << MethName << " ******\n";
177 enterSection(Text);
178 Out << "\t.align 4\n\t.global\t" << MethName << "\n";
179 Out << "\t.type\t" << MethName << ",#function\n";
180 Out << MethName << ":\n";
181
182 // Output code for all of the basic blocks in the method...
183 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
184 emitBasicBlock(*I);
185
186 // Output a .size directive so the debugger knows the extents of the function
187 Out << ".EndOf$" << MethName << ":\n\t.size " << MethName << ", .EndOf$"
188 << MethName << "-" << MethName << endl;
189
190 // Put some spaces between the methods
191 Out << "\n\n";
192
193 // Forget all about M.
194 Table.purgeMethod();
195}
196
197
198void SparcAsmPrinter::emitModule(const Module *M) {
199 // TODO: Look for a filename annotation on M to emit a .file directive
200 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
201 emitMethod(*I);
202}
203
204} // End anonymous namespace
205
206//
207// emitAssembly - Output assembly language code (a .s file) for the specified
208// method. The specified method must have been compiled before this may be
209// used.
210//
211void UltraSparc::emitAssembly(const Module *M, ostream &Out) {
212 SparcAsmPrinter Print(Out, M, *this);
213}