blob: 863ffd7007a39d6f8d5a4fc93653c01d8789386e [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
John Criswellb576c942003-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 Lattnere88f78c2001-09-19 13:47:27 +00009//
Misha Brukman5560c9d2003-08-18 14:43:39 +000010// This file implements all of the stuff necessary to output a .s file from
Chris Lattnere88f78c2001-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 Lattnerf57b8452002-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 Lattnere88f78c2001-09-19 13:47:27 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattner31bcdb82002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000022#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000023#include "llvm/Module.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000024#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000025#include "llvm/Assembly/Writer.h"
Misha Brukman3d0ad412003-12-17 22:06:28 +000026#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFunctionInfo.h"
29#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmancbbbdf72004-01-15 22:44:19 +000030#include "llvm/Support/Mangler.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include "Support/StringExtras.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000032#include "Support/Statistic.h"
Misha Brukmanf4de7832003-08-05 16:01:50 +000033#include "SparcInternals.h"
34#include <string>
Misha Brukman6275a042003-11-13 00:22:19 +000035using namespace llvm;
36
Chris Lattnere88f78c2001-09-19 13:47:27 +000037namespace {
Misha Brukman6275a042003-11-13 00:22:19 +000038 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Brian Gaeke2c9b9132003-10-06 15:41:21 +000039
Misha Brukman6275a042003-11-13 00:22:19 +000040 //===--------------------------------------------------------------------===//
41 // Utility functions
Misha Brukmanf905ed52003-11-07 17:45:28 +000042
Misha Brukman6275a042003-11-13 00:22:19 +000043 /// getAsCString - Return the specified array as a C compatible string, only
Chris Lattner07ad6422004-01-14 17:15:17 +000044 /// if the predicate isString() is true.
Misha Brukman6275a042003-11-13 00:22:19 +000045 ///
46 std::string getAsCString(const ConstantArray *CVA) {
Chris Lattner07ad6422004-01-14 17:15:17 +000047 assert(CVA->isString() && "Array is not string compatible!");
Misha Brukmanf905ed52003-11-07 17:45:28 +000048
Chris Lattner07ad6422004-01-14 17:15:17 +000049 std::string Result = "\"";
50 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Misha Brukman6275a042003-11-13 00:22:19 +000051 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Misha Brukmanf905ed52003-11-07 17:45:28 +000052
Misha Brukman6275a042003-11-13 00:22:19 +000053 if (C == '"') {
54 Result += "\\\"";
55 } else if (C == '\\') {
56 Result += "\\\\";
57 } else if (isprint(C)) {
58 Result += C;
59 } else {
60 Result += '\\'; // print all other chars as octal value
61 // Convert C to octal representation
62 Result += ((C >> 6) & 7) + '0';
63 Result += ((C >> 3) & 7) + '0';
64 Result += ((C >> 0) & 7) + '0';
65 }
66 }
67 Result += "\"";
Misha Brukmanf905ed52003-11-07 17:45:28 +000068
Misha Brukman6275a042003-11-13 00:22:19 +000069 return Result;
70 }
71
72 inline bool ArrayTypeIsString(const ArrayType* arrayType) {
73 return (arrayType->getElementType() == Type::UByteTy ||
74 arrayType->getElementType() == Type::SByteTy);
75 }
76
77 inline const std::string
78 TypeToDataDirective(const Type* type) {
79 switch(type->getPrimitiveID())
Misha Brukmanf905ed52003-11-07 17:45:28 +000080 {
81 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
82 return ".byte";
83 case Type::UShortTyID: case Type::ShortTyID:
84 return ".half";
85 case Type::UIntTyID: case Type::IntTyID:
86 return ".word";
87 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
88 return ".xword";
89 case Type::FloatTyID:
90 return ".word";
91 case Type::DoubleTyID:
92 return ".xword";
93 case Type::ArrayTyID:
94 if (ArrayTypeIsString((ArrayType*) type))
95 return ".ascii";
96 else
97 return "<InvaliDataTypeForPrinting>";
98 default:
99 return "<InvaliDataTypeForPrinting>";
100 }
Misha Brukman6275a042003-11-13 00:22:19 +0000101 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000102
Misha Brukman6275a042003-11-13 00:22:19 +0000103 /// Get the size of the constant for the given target.
104 /// If this is an unsized array, return 0.
105 ///
106 inline unsigned int
107 ConstantToSize(const Constant* CV, const TargetMachine& target) {
108 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) {
Misha Brukmanf905ed52003-11-07 17:45:28 +0000109 const ArrayType *aty = cast<ArrayType>(CVA->getType());
110 if (ArrayTypeIsString(aty))
111 return 1 + CVA->getNumOperands();
112 }
113
Misha Brukman6275a042003-11-13 00:22:19 +0000114 return target.findOptimalStorageSize(CV->getType());
115 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000116
Misha Brukman6275a042003-11-13 00:22:19 +0000117 /// Align data larger than one L1 cache line on L1 cache line boundaries.
118 /// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
119 ///
120 inline unsigned int
121 SizeToAlignment(unsigned int size, const TargetMachine& target) {
122 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
123 if (size > (unsigned) cacheLineSize / 2)
124 return cacheLineSize;
125 else
126 for (unsigned sz=1; /*no condition*/; sz *= 2)
127 if (sz >= size)
128 return sz;
129 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000130
Misha Brukman6275a042003-11-13 00:22:19 +0000131 /// Get the size of the type and then use SizeToAlignment.
132 ///
133 inline unsigned int
134 TypeToAlignment(const Type* type, const TargetMachine& target) {
135 return SizeToAlignment(target.findOptimalStorageSize(type), target);
136 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000137
Misha Brukman6275a042003-11-13 00:22:19 +0000138 /// Get the size of the constant and then use SizeToAlignment.
139 /// Handles strings as a special case;
140 inline unsigned int
141 ConstantToAlignment(const Constant* CV, const TargetMachine& target) {
142 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
143 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
144 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Misha Brukmanf905ed52003-11-07 17:45:28 +0000145
Misha Brukman6275a042003-11-13 00:22:19 +0000146 return TypeToAlignment(CV->getType(), target);
147 }
148
149} // End anonymous namespace
150
Misha Brukman6275a042003-11-13 00:22:19 +0000151
152
Vikram S. Adved198c472002-03-18 03:07:26 +0000153//===---------------------------------------------------------------------===//
Misha Brukman6275a042003-11-13 00:22:19 +0000154// Code abstracted away from the AsmPrinter
Vikram S. Adved198c472002-03-18 03:07:26 +0000155//===---------------------------------------------------------------------===//
156
Misha Brukman6275a042003-11-13 00:22:19 +0000157namespace {
Misha Brukman6275a042003-11-13 00:22:19 +0000158 class AsmPrinter {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000159 // Mangle symbol names appropriately
160 Mangler *Mang;
161
Misha Brukman6275a042003-11-13 00:22:19 +0000162 public:
163 std::ostream &toAsm;
164 const TargetMachine &Target;
Misha Brukmanf905ed52003-11-07 17:45:28 +0000165
Misha Brukman6275a042003-11-13 00:22:19 +0000166 enum Sections {
167 Unknown,
168 Text,
169 ReadOnlyData,
170 InitRWData,
171 ZeroInitRWData,
172 } CurSection;
173
174 AsmPrinter(std::ostream &os, const TargetMachine &T)
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000175 : /* idTable(0), */ toAsm(os), Target(T), CurSection(Unknown) {}
Misha Brukmanf905ed52003-11-07 17:45:28 +0000176
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000177 ~AsmPrinter() {
178 delete Mang;
179 }
180
Misha Brukman6275a042003-11-13 00:22:19 +0000181 // (start|end)(Module|Function) - Callback methods invoked by subclasses
182 void startModule(Module &M) {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000183 Mang = new Mangler(M);
Misha Brukmanf905ed52003-11-07 17:45:28 +0000184 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000185
Misha Brukman6275a042003-11-13 00:22:19 +0000186 void PrintZeroBytesToPad(int numBytes) {
John Criswellccb2a672004-02-09 22:15:33 +0000187 //
188 // Always use single unsigned bytes for padding. We don't know upon
189 // what data size the beginning address is aligned, so using anything
190 // other than a byte may cause alignment errors in the assembler.
191 //
Misha Brukman6275a042003-11-13 00:22:19 +0000192 while (numBytes--)
193 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
Misha Brukmanf905ed52003-11-07 17:45:28 +0000194 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000195
Misha Brukman6275a042003-11-13 00:22:19 +0000196 /// Print a single constant value.
197 ///
198 void printSingleConstantValue(const Constant* CV);
Misha Brukmanf905ed52003-11-07 17:45:28 +0000199
Misha Brukman6275a042003-11-13 00:22:19 +0000200 /// Print a constant value or values (it may be an aggregate).
201 /// Uses printSingleConstantValue() to print each individual value.
202 ///
203 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
204
205 // Print a constant (which may be an aggregate) prefixed by all the
206 // appropriate directives. Uses printConstantValueOnly() to print the
207 // value or values.
208 void printConstant(const Constant* CV, std::string valID = "") {
209 if (valID.length() == 0)
210 valID = getID(CV);
Misha Brukmanf905ed52003-11-07 17:45:28 +0000211
Misha Brukman6275a042003-11-13 00:22:19 +0000212 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Misha Brukmanf905ed52003-11-07 17:45:28 +0000213
Misha Brukman6275a042003-11-13 00:22:19 +0000214 // Print .size and .type only if it is not a string.
Chris Lattner07ad6422004-01-14 17:15:17 +0000215 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
216 if (CVA->isString()) {
217 // print it as a string and return
218 toAsm << valID << ":\n";
219 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
220 return;
221 }
Misha Brukman6275a042003-11-13 00:22:19 +0000222
223 toAsm << "\t.type" << "\t" << valID << ",#object\n";
224
225 unsigned int constSize = ConstantToSize(CV, Target);
226 if (constSize)
227 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
228
Misha Brukmanf905ed52003-11-07 17:45:28 +0000229 toAsm << valID << ":\n";
Misha Brukman6275a042003-11-13 00:22:19 +0000230
231 printConstantValueOnly(CV);
232 }
233
Misha Brukman6275a042003-11-13 00:22:19 +0000234 // enterSection - Use this method to enter a different section of the output
235 // executable. This is used to only output necessary section transitions.
236 //
237 void enterSection(enum Sections S) {
238 if (S == CurSection) return; // Only switch section if necessary
239 CurSection = S;
Misha Brukmanf905ed52003-11-07 17:45:28 +0000240
Misha Brukman6275a042003-11-13 00:22:19 +0000241 toAsm << "\n\t.section ";
242 switch (S)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000243 {
244 default: assert(0 && "Bad section name!");
245 case Text: toAsm << "\".text\""; break;
246 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
247 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
248 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
249 }
Misha Brukman6275a042003-11-13 00:22:19 +0000250 toAsm << "\n";
251 }
Chris Lattnere88f78c2001-09-19 13:47:27 +0000252
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000253 // getID Wrappers - Ensure consistent usage
254 // Symbol names in Sparc assembly language have these rules:
255 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
256 // (b) A name beginning in "." is treated as a local name.
Misha Brukman6275a042003-11-13 00:22:19 +0000257 std::string getID(const Function *F) {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000258 return Mang->getValueName(F);
Misha Brukman6275a042003-11-13 00:22:19 +0000259 }
260 std::string getID(const BasicBlock *BB) {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000261 return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB);
Misha Brukman6275a042003-11-13 00:22:19 +0000262 }
263 std::string getID(const GlobalVariable *GV) {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000264 return Mang->getValueName(GV);
Misha Brukman6275a042003-11-13 00:22:19 +0000265 }
266 std::string getID(const Constant *CV) {
Misha Brukmancbbbdf72004-01-15 22:44:19 +0000267 return ".C_" + Mang->getValueName(CV);
Misha Brukman6275a042003-11-13 00:22:19 +0000268 }
269 std::string getID(const GlobalValue *GV) {
270 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
271 return getID(V);
272 else if (const Function *F = dyn_cast<Function>(GV))
273 return getID(F);
274 assert(0 && "Unexpected type of GlobalValue!");
275 return "";
276 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000277
Misha Brukman6275a042003-11-13 00:22:19 +0000278 // Combines expressions
279 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
280 const TargetMachine &TM,
281 const std::string &op) {
282 return "(" + valToExprString(CE->getOperand(0), TM) + op
283 + valToExprString(CE->getOperand(1), TM) + ")";
284 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000285
Misha Brukman6275a042003-11-13 00:22:19 +0000286 /// ConstantExprToString() - Convert a ConstantExpr to an asm expression
287 /// and return this as a string.
288 ///
289 std::string ConstantExprToString(const ConstantExpr* CE,
290 const TargetMachine& target);
291
292 /// valToExprString - Helper function for ConstantExprToString().
293 /// Appends result to argument string S.
294 ///
295 std::string valToExprString(const Value* V, const TargetMachine& target);
296 };
Misha Brukman6275a042003-11-13 00:22:19 +0000297} // End anonymous namespace
298
Misha Brukman6275a042003-11-13 00:22:19 +0000299
300/// Print a single constant value.
301///
302void AsmPrinter::printSingleConstantValue(const Constant* CV) {
303 assert(CV->getType() != Type::VoidTy &&
304 CV->getType() != Type::TypeTy &&
305 CV->getType() != Type::LabelTy &&
306 "Unexpected type for Constant");
307
308 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
309 && "Aggregate types should be handled outside this function");
310
311 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
312
313 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV)) {
314 // This is a constant address for a global variable or method.
315 // Use the name of the variable or method as the address value.
316 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
317 toAsm << getID(CPR->getValue()) << "\n";
318 } else if (isa<ConstantPointerNull>(CV)) {
319 // Null pointer value
320 toAsm << "0\n";
321 } else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
322 // Constant expression built from operators, constants, and symbolic addrs
323 toAsm << ConstantExprToString(CE, Target) << "\n";
324 } else if (CV->getType()->isPrimitiveType()) {
325 // Check primitive types last
326 if (CV->getType()->isFloatingPoint()) {
327 // FP Constants are printed as integer constants to avoid losing
328 // precision...
329 double Val = cast<ConstantFP>(CV)->getValue();
330 if (CV->getType() == Type::FloatTy) {
331 float FVal = (float)Val;
332 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
333 toAsm << *(unsigned int*)ProxyPtr;
334 } else if (CV->getType() == Type::DoubleTy) {
335 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
336 toAsm << *(uint64_t*)ProxyPtr;
337 } else {
338 assert(0 && "Unknown floating point type!");
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000339 }
Misha Brukman6275a042003-11-13 00:22:19 +0000340
341 toAsm << "\t! " << CV->getType()->getDescription()
342 << " value: " << Val << "\n";
343 } else {
344 WriteAsOperand(toAsm, CV, false, false) << "\n";
345 }
346 } else {
347 assert(0 && "Unknown elementary type for constant");
348 }
349}
Vikram S. Advee99941a2002-08-22 02:58:36 +0000350
Misha Brukman6275a042003-11-13 00:22:19 +0000351/// Print a constant value or values (it may be an aggregate).
352/// Uses printSingleConstantValue() to print each individual value.
353///
354void AsmPrinter::printConstantValueOnly(const Constant* CV,
Chris Lattner07ad6422004-01-14 17:15:17 +0000355 int numPadBytesAfter) {
356 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
357 if (CVA->isString()) {
358 // print the string alone and return
359 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
360 } else {
361 // Not a string. Print the values in successive locations
362 const std::vector<Use> &constValues = CVA->getValues();
363 for (unsigned i=0; i < constValues.size(); i++)
364 printConstantValueOnly(cast<Constant>(constValues[i].get()));
365 }
Misha Brukman6275a042003-11-13 00:22:19 +0000366 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
367 // Print the fields in successive locations. Pad to align if needed!
368 const StructLayout *cvsLayout =
369 Target.getTargetData().getStructLayout(CVS->getType());
370 const std::vector<Use>& constValues = CVS->getValues();
371 unsigned sizeSoFar = 0;
372 for (unsigned i=0, N = constValues.size(); i < N; i++) {
373 const Constant* field = cast<Constant>(constValues[i].get());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000374
Misha Brukman6275a042003-11-13 00:22:19 +0000375 // Check if padding is needed and insert one or more 0s.
376 unsigned fieldSize =
377 Target.getTargetData().getTypeSize(field->getType());
378 int padSize = ((i == N-1? cvsLayout->StructSize
379 : cvsLayout->MemberOffsets[i+1])
380 - cvsLayout->MemberOffsets[i]) - fieldSize;
381 sizeSoFar += (fieldSize + padSize);
Vikram S. Adve72666e62003-08-01 15:55:53 +0000382
Misha Brukman6275a042003-11-13 00:22:19 +0000383 // Now print the actual field value
384 printConstantValueOnly(field, padSize);
385 }
386 assert(sizeSoFar == cvsLayout->StructSize &&
387 "Layout of constant struct may be incorrect!");
388 }
389 else
390 printSingleConstantValue(CV);
Vikram S. Adve72666e62003-08-01 15:55:53 +0000391
Misha Brukman6275a042003-11-13 00:22:19 +0000392 if (numPadBytesAfter)
393 PrintZeroBytesToPad(numPadBytesAfter);
394}
Vikram S. Adve72666e62003-08-01 15:55:53 +0000395
Misha Brukman6275a042003-11-13 00:22:19 +0000396/// ConstantExprToString() - Convert a ConstantExpr to an asm expression
397/// and return this as a string.
398///
399std::string AsmPrinter::ConstantExprToString(const ConstantExpr* CE,
400 const TargetMachine& target) {
401 std::string S;
402 switch(CE->getOpcode()) {
403 case Instruction::GetElementPtr:
404 { // generate a symbolic expression for the byte address
405 const Value* ptrVal = CE->getOperand(0);
406 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
407 const TargetData &TD = target.getTargetData();
408 S += "(" + valToExprString(ptrVal, target) + ") + ("
409 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000410 break;
411 }
412
Misha Brukman6275a042003-11-13 00:22:19 +0000413 case Instruction::Cast:
414 // Support only non-converting casts for now, i.e., a no-op.
415 // This assertion is not a complete check.
416 assert(target.getTargetData().getTypeSize(CE->getType()) ==
417 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
418 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
419 break;
420
421 case Instruction::Add:
422 S += ConstantArithExprToString(CE, target, ") + (");
423 break;
424
425 case Instruction::Sub:
426 S += ConstantArithExprToString(CE, target, ") - (");
427 break;
428
429 case Instruction::Mul:
430 S += ConstantArithExprToString(CE, target, ") * (");
431 break;
432
433 case Instruction::Div:
434 S += ConstantArithExprToString(CE, target, ") / (");
435 break;
436
437 case Instruction::Rem:
438 S += ConstantArithExprToString(CE, target, ") % (");
439 break;
440
441 case Instruction::And:
442 // Logical && for booleans; bitwise & otherwise
443 S += ConstantArithExprToString(CE, target,
444 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
445 break;
446
447 case Instruction::Or:
448 // Logical || for booleans; bitwise | otherwise
449 S += ConstantArithExprToString(CE, target,
450 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
451 break;
452
453 case Instruction::Xor:
454 // Bitwise ^ for all types
455 S += ConstantArithExprToString(CE, target, ") ^ (");
456 break;
457
458 default:
459 assert(0 && "Unsupported operator in ConstantExprToString()");
460 break;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000461 }
462
Misha Brukman6275a042003-11-13 00:22:19 +0000463 return S;
464}
Vikram S. Advee99941a2002-08-22 02:58:36 +0000465
Misha Brukman6275a042003-11-13 00:22:19 +0000466/// valToExprString - Helper function for ConstantExprToString().
467/// Appends result to argument string S.
468///
469std::string AsmPrinter::valToExprString(const Value* V,
470 const TargetMachine& target) {
471 std::string S;
472 bool failed = false;
473 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
474 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
475 S += std::string(CB == ConstantBool::True ? "1" : "0");
476 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
477 S += itostr(CI->getValue());
478 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
479 S += utostr(CI->getValue());
480 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
481 S += ftostr(CFP->getValue());
482 else if (isa<ConstantPointerNull>(CV))
483 S += "0";
484 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
485 S += valToExprString(CPR->getValue(), target);
486 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
487 S += ConstantExprToString(CE, target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000488 else
489 failed = true;
Misha Brukman6275a042003-11-13 00:22:19 +0000490 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
491 S += getID(GV);
492 } else
493 failed = true;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000494
Misha Brukman6275a042003-11-13 00:22:19 +0000495 if (failed) {
496 assert(0 && "Cannot convert value to string");
497 S += "<illegal-value>";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000498 }
Misha Brukman6275a042003-11-13 00:22:19 +0000499 return S;
500}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000501
502
503//===----------------------------------------------------------------------===//
Misha Brukman6275a042003-11-13 00:22:19 +0000504// SparcAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000505//===----------------------------------------------------------------------===//
506
Misha Brukman6275a042003-11-13 00:22:19 +0000507namespace {
Misha Brukmanf905ed52003-11-07 17:45:28 +0000508
Misha Brukman6275a042003-11-13 00:22:19 +0000509 struct SparcAsmPrinter : public FunctionPass, public AsmPrinter {
510 inline SparcAsmPrinter(std::ostream &os, const TargetMachine &t)
511 : AsmPrinter(os, t) {}
Chris Lattner96c466b2002-04-29 14:57:45 +0000512
Misha Brukman6275a042003-11-13 00:22:19 +0000513 const Function *currFunction;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000514
Misha Brukman6275a042003-11-13 00:22:19 +0000515 const char *getPassName() const {
516 return "Output Sparc Assembly for Functions";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000517 }
Misha Brukman6275a042003-11-13 00:22:19 +0000518
519 virtual bool doInitialization(Module &M) {
520 startModule(M);
521 return false;
522 }
523
524 virtual bool runOnFunction(Function &F) {
525 currFunction = &F;
Misha Brukman6275a042003-11-13 00:22:19 +0000526 emitFunction(F);
Misha Brukman6275a042003-11-13 00:22:19 +0000527 return false;
528 }
529
530 virtual bool doFinalization(Module &M) {
531 emitGlobals(M);
532 return false;
533 }
534
535 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
536 AU.setPreservesAll();
537 }
538
539 void emitFunction(const Function &F);
540 private :
541 void emitBasicBlock(const MachineBasicBlock &MBB);
542 void emitMachineInst(const MachineInstr *MI);
543
544 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
545 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
546
547 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
548 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
549
550 unsigned getOperandMask(unsigned Opcode) {
551 switch (Opcode) {
552 case V9::SUBccr:
553 case V9::SUBcci: return 1 << 3; // Remove CC argument
554 default: return 0; // By default, don't hack operands...
555 }
556 }
557
558 void emitGlobals(const Module &M);
559 void printGlobalVariable(const GlobalVariable *GV);
560 };
561
562} // End anonymous namespace
Chris Lattnere88f78c2001-09-19 13:47:27 +0000563
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000564inline bool
Misha Brukman6275a042003-11-13 00:22:19 +0000565SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
566 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000567 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000568 case V9::JMPLCALLr:
569 case V9::JMPLCALLi:
570 case V9::JMPLRETr:
571 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000572 return (opNum == 0);
573 default:
574 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000575 }
576}
577
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000578inline bool
Misha Brukman6275a042003-11-13 00:22:19 +0000579SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
580 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000581 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
582 return (opNum == 0);
583 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
584 return (opNum == 1);
585 else
586 return false;
587}
588
589
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000590#define PrintOp1PlusOp2(mop1, mop2, opCode) \
591 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000592 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000593 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000594
595unsigned int
Misha Brukman6275a042003-11-13 00:22:19 +0000596SparcAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000597 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000598{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000599 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000600
Misha Brukman6275a042003-11-13 00:22:19 +0000601 if (OpIsBranchTargetLabel(MI, opNum)) {
602 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
603 return 2;
604 } else if (OpIsMemoryAddressBase(MI, opNum)) {
605 toAsm << "[";
606 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
607 toAsm << "]";
608 return 2;
609 } else {
610 printOneOperand(mop, MI->getOpCode());
611 return 1;
612 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000613}
614
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000615void
Misha Brukman6275a042003-11-13 00:22:19 +0000616SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
617 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000618{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000619 bool needBitsFlag = true;
620
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000621 if (mop.isHiBits32())
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000622 toAsm << "%lm(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000623 else if (mop.isLoBits32())
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000624 toAsm << "%lo(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000625 else if (mop.isHiBits64())
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000626 toAsm << "%hh(";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000627 else if (mop.isLoBits64())
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000628 toAsm << "%hm(";
629 else
630 needBitsFlag = false;
631
Chris Lattner133f0792002-10-28 04:45:29 +0000632 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000633 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000634 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000635 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000636 case MachineOperand::MO_MachineRegister:
637 {
638 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000639
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000640 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
641 // better to print code with NULL registers than to die
642 toAsm << "<NULL VALUE>";
643 } else {
644 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
645 }
646 break;
647 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000648
Misha Brukmanf905ed52003-11-07 17:45:28 +0000649 case MachineOperand::MO_ConstantPoolIndex:
650 {
651 toAsm << ".CPI_" << currFunction->getName()
652 << "_" << mop.getConstantPoolIndex();
653 break;
654 }
655
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000656 case MachineOperand::MO_PCRelativeDisp:
657 {
658 const Value *Val = mop.getVRegValue();
Misha Brukman6275a042003-11-13 00:22:19 +0000659 assert(Val && "\tNULL Value in SparcAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000660
Chris Lattner949a3622003-07-23 15:30:06 +0000661 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000662 toAsm << getID(BB);
663 else if (const Function *M = dyn_cast<Function>(Val))
664 toAsm << getID(M);
665 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
666 toAsm << getID(GV);
667 else if (const Constant *CV = dyn_cast<Constant>(Val))
668 toAsm << getID(CV);
669 else
Misha Brukman6275a042003-11-13 00:22:19 +0000670 assert(0 && "Unrecognized value in SparcAsmPrinter");
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000671 break;
672 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000673
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000674 case MachineOperand::MO_SignExtendedImmed:
675 toAsm << mop.getImmedValue();
676 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000677
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000678 case MachineOperand::MO_UnextendedImmed:
679 toAsm << (uint64_t) mop.getImmedValue();
680 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000681
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000682 default:
683 toAsm << mop; // use dump field
684 break;
685 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000686
687 if (needBitsFlag)
688 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000689}
690
Misha Brukman6275a042003-11-13 00:22:19 +0000691void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000692 unsigned Opcode = MI->getOpCode();
693
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000694 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000695 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000696
Chris Lattnerf44f9052002-10-29 17:35:41 +0000697 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000698
699 unsigned Mask = getOperandMask(Opcode);
700
701 bool NeedComma = false;
702 unsigned N = 1;
703 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
704 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Misha Brukman8b2fe192003-09-23 17:27:28 +0000705 if (NeedComma) toAsm << ", "; // Handle comma outputting
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000706 NeedComma = true;
707 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000708 } else
709 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000710
711 toAsm << "\n";
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000712 ++EmittedInsts;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000713}
714
Misha Brukman6275a042003-11-13 00:22:19 +0000715void SparcAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000716 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000717 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000718
719 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000720 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000721 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000722 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000723 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000724}
725
Misha Brukman6275a042003-11-13 00:22:19 +0000726void SparcAsmPrinter::emitFunction(const Function &F) {
Misha Brukmanf4de7832003-08-05 16:01:50 +0000727 std::string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000728 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Misha Brukmanf905ed52003-11-07 17:45:28 +0000729
730 // Emit constant pool for this function
731 const MachineConstantPool *MCP = MachineFunction::get(&F).getConstantPool();
732 const std::vector<Constant*> &CP = MCP->getConstants();
733
734 enterSection(AsmPrinter::ReadOnlyData);
735 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
736 std::string cpiName = ".CPI_" + F.getName() + "_" + utostr(i);
737 printConstant(CP[i], cpiName);
738 }
739
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000740 enterSection(AsmPrinter::Text);
741 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
742 //toAsm << "\t.type\t" << methName << ",#function\n";
743 toAsm << "\t.type\t" << methName << ", 2\n";
744 toAsm << methName << ":\n";
745
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000746 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000747 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000748 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000749 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000750
751 // Output a .size directive so the debugger knows the extents of the function
752 toAsm << ".EndOf_" << methName << ":\n\t.size "
753 << methName << ", .EndOf_"
754 << methName << "-" << methName << "\n";
755
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000756 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000757 toAsm << "\n\n";
758}
759
Misha Brukman6275a042003-11-13 00:22:19 +0000760void SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000761 if (GV->hasExternalLinkage())
762 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000763
Misha Brukman6275a042003-11-13 00:22:19 +0000764 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue()) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765 printConstant(GV->getInitializer(), getID(GV));
Misha Brukman6275a042003-11-13 00:22:19 +0000766 } else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000767 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
768 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000769 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000770 toAsm << "\t.reserve\t" << getID(GV) << ","
Misha Brukman6275a042003-11-13 00:22:19 +0000771 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000772 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000773 }
774}
775
Misha Brukman6275a042003-11-13 00:22:19 +0000776void SparcAsmPrinter::emitGlobals(const Module &M) {
Chris Lattner637ed862002-08-07 21:39:48 +0000777 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000778 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
779 if (! GI->isExternal()) {
780 assert(GI->hasInitializer());
781 if (GI->isConstant())
782 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
783 else if (GI->getInitializer()->isNullValue())
784 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
785 else
786 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
787
788 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000789 }
Chris Lattner637ed862002-08-07 21:39:48 +0000790
Chris Lattner697954c2002-01-20 22:54:45 +0000791 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000792}
793
Chris Lattner7446dc02004-01-13 21:27:59 +0000794FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out,
795 const TargetMachine &TM) {
Misha Brukman6275a042003-11-13 00:22:19 +0000796 return new SparcAsmPrinter(Out, TM);
Chris Lattnerc019a172002-02-03 07:48:06 +0000797}