1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
|
//===-- EmitAssembly.cpp - Emit SparcV9 Specific .s File -------------------==//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements all of the stuff necessary to output a .s file from
// LLVM. The code in this file assumes that the specified module has already
// been compiled into the internal data structures of the Module.
//
// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
// The FunctionPass is pipelined together with all of the rest of the code
// generation stages, and the Pass runs at the end to emit code for global
// variables and such.
//
//===----------------------------------------------------------------------===//
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/Support/Mangler.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Statistic.h"
#include "SparcV9Internals.h"
#include "MachineFunctionInfo.h"
#include <string>
using namespace llvm;
namespace {
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
//===--------------------------------------------------------------------===//
// Utility functions
/// getAsCString - Return the specified array as a C compatible string, only
/// if the predicate isString() is true.
///
std::string getAsCString(const ConstantArray *CVA) {
assert(CVA->isString() && "Array is not string compatible!");
std::string Result = "\"";
for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
if (C == '"') {
Result += "\\\"";
} else if (C == '\\') {
Result += "\\\\";
} else if (isprint(C)) {
Result += C;
} else {
Result += '\\'; // print all other chars as octal value
// Convert C to octal representation
Result += ((C >> 6) & 7) + '0';
Result += ((C >> 3) & 7) + '0';
Result += ((C >> 0) & 7) + '0';
}
}
Result += "\"";
return Result;
}
inline bool ArrayTypeIsString(const ArrayType* arrayType) {
return (arrayType->getElementType() == Type::UByteTy ||
arrayType->getElementType() == Type::SByteTy);
}
unsigned findOptimalStorageSize(const TargetMachine &TM, const Type *Ty) {
// All integer types smaller than ints promote to 4 byte integers.
if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4)
return 4;
return TM.getTargetData().getTypeSize(Ty);
}
inline const std::string
TypeToDataDirective(const Type* type) {
switch(type->getTypeID()) {
case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
return ".byte";
case Type::UShortTyID: case Type::ShortTyID:
return ".half";
case Type::UIntTyID: case Type::IntTyID:
return ".word";
case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
return ".xword";
case Type::FloatTyID:
return ".word";
case Type::DoubleTyID:
return ".xword";
case Type::ArrayTyID:
if (ArrayTypeIsString((ArrayType*) type))
return ".ascii";
else
return "<InvaliDataTypeForPrinting>";
default:
return "<InvaliDataTypeForPrinting>";
}
}
/// Get the size of the constant for the given target.
/// If this is an unsized array, return 0.
///
inline unsigned int
ConstantToSize(const Constant* CV, const TargetMachine& target) {
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) {
const ArrayType *aty = cast<ArrayType>(CVA->getType());
if (ArrayTypeIsString(aty))
return 1 + CVA->getNumOperands();
}
return findOptimalStorageSize(target, CV->getType());
}
/// Align data larger than one L1 cache line on L1 cache line boundaries.
/// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
///
inline unsigned int
SizeToAlignment(unsigned int size, const TargetMachine& target) {
const unsigned short cacheLineSize = 16;
if (size > (unsigned) cacheLineSize / 2)
return cacheLineSize;
else
for (unsigned sz=1; /*no condition*/; sz *= 2)
if (sz >= size)
return sz;
}
/// Get the size of the type and then use SizeToAlignment.
///
inline unsigned int
TypeToAlignment(const Type* type, const TargetMachine& target) {
return SizeToAlignment(findOptimalStorageSize(target, type), target);
}
/// Get the size of the constant and then use SizeToAlignment.
/// Handles strings as a special case;
inline unsigned int
ConstantToAlignment(const Constant* CV, const TargetMachine& target) {
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
return SizeToAlignment(1 + CVA->getNumOperands(), target);
return TypeToAlignment(CV->getType(), target);
}
} // End anonymous namespace
namespace {
enum Sections {
Unknown,
Text,
ReadOnlyData,
InitRWData,
ZeroInitRWData,
};
class AsmPrinter {
// Mangle symbol names appropriately
Mangler *Mang;
public:
std::ostream &O;
const TargetMachine &TM;
enum Sections CurSection;
AsmPrinter(std::ostream &os, const TargetMachine &T)
: /* idTable(0), */ O(os), TM(T), CurSection(Unknown) {}
~AsmPrinter() {
delete Mang;
}
// (start|end)(Module|Function) - Callback methods invoked by subclasses
void startModule(Module &M) {
Mang = new Mangler(M);
}
void PrintZeroBytesToPad(int numBytes) {
//
// Always use single unsigned bytes for padding. We don't know upon
// what data size the beginning address is aligned, so using anything
// other than a byte may cause alignment errors in the assembler.
//
while (numBytes--)
printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
}
/// Print a single constant value.
///
void printSingleConstantValue(const Constant* CV);
/// Print a constant value or values (it may be an aggregate).
/// Uses printSingleConstantValue() to print each individual value.
///
void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
// Print a constant (which may be an aggregate) prefixed by all the
// appropriate directives. Uses printConstantValueOnly() to print the
// value or values.
void printConstant(const Constant* CV, std::string valID = "") {
if (valID.length() == 0)
valID = getID(CV);
O << "\t.align\t" << ConstantToAlignment(CV, TM) << "\n";
// Print .size and .type only if it is not a string.
if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
if (CVA->isString()) {
// print it as a string and return
O << valID << ":\n";
O << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
return;
}
O << "\t.type" << "\t" << valID << ",#object\n";
unsigned int constSize = ConstantToSize(CV, TM);
if (constSize)
O << "\t.size" << "\t" << valID << "," << constSize << "\n";
O << valID << ":\n";
printConstantValueOnly(CV);
}
// enterSection - Use this method to enter a different section of the output
// executable. This is used to only output necessary section transitions.
//
void enterSection(enum Sections S) {
if (S == CurSection) return; // Only switch section if necessary
CurSection = S;
O << "\n\t.section ";
switch (S)
{
default: assert(0 && "Bad section name!");
case Text: O << "\".text\""; break;
case ReadOnlyData: O << "\".rodata\",#alloc"; break;
case InitRWData: O << "\".data\",#alloc,#write"; break;
case ZeroInitRWData: O << "\".bss\",#alloc,#write"; break;
}
O << "\n";
}
// getID Wrappers - Ensure consistent usage
// Symbol names in SparcV9 assembly language have these rules:
// (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
// (b) A name beginning in "." is treated as a local name.
std::string getID(const Function *F) {
return Mang->getValueName(F);
}
std::string getID(const BasicBlock *BB) {
return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB);
}
std::string getID(const GlobalVariable *GV) {
return Mang->getValueName(GV);
}
std::string getID(const Constant *CV) {
return ".C_" + Mang->getValueName(CV);
}
std::string getID(const GlobalValue *GV) {
if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
return getID(V);
else if (const Function *F = dyn_cast<Function>(GV))
return getID(F);
assert(0 && "Unexpected type of GlobalValue!");
return "";
}
// Combines expressions
inline std::string ConstantArithExprToString(const ConstantExpr* CE,
const TargetMachine &TM,
const std::string &op) {
return "(" + valToExprString(CE->getOperand(0), TM) + op
+ valToExprString(CE->getOperand(1), TM) + ")";
}
/// ConstantExprToString() - Convert a ConstantExpr to an asm expression
/// and return this as a string.
///
std::string ConstantExprToString(const ConstantExpr* CE,
const TargetMachine& target);
/// valToExprString - Helper function for ConstantExprToString().
/// Appends result to argument string S.
///
std::string valToExprString(const Value* V, const TargetMachine& target);
};
} // End anonymous namespace
/// Print a single constant value.
///
void AsmPrinter::printSingleConstantValue(const Constant* CV) {
assert(CV->getType() != Type::VoidTy &&
CV->getType() != Type::LabelTy &&
"Unexpected type for Constant");
assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
&& "Aggregate types should be handled outside this function");
O << "\t" << TypeToDataDirective(CV->getType()) << "\t";
if (const GlobalValue* GV = dyn_cast<GlobalValue>(CV)) {
O << getID(GV) << "\n";
} else if (isa<ConstantPointerNull>(CV) || isa<UndefValue>(CV)) {
// Null pointer value
O << "0\n";
} else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
// Constant expression built from operators, constants, and symbolic addrs
O << ConstantExprToString(CE, TM) << "\n";
} else if (CV->getType()->isPrimitiveType()) {
// Check primitive types last
if (isa<UndefValue>(CV)) {
O << "0\n";
} else if (CV->getType()->isFloatingPoint()) {
// FP Constants are printed as integer constants to avoid losing
// precision...
double Val = cast<ConstantFP>(CV)->getValue();
if (CV->getType() == Type::FloatTy) {
float FVal = (float)Val;
char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
O << *(unsigned int*)ProxyPtr;
} else if (CV->getType() == Type::DoubleTy) {
char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
O << *(uint64_t*)ProxyPtr;
} else {
assert(0 && "Unknown floating point type!");
}
O << "\t! " << CV->getType()->getDescription()
<< " value: " << Val << "\n";
} else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
O << (int)CB->getValue() << "\n";
} else {
WriteAsOperand(O, CV, false, false) << "\n";
}
} else {
assert(0 && "Unknown elementary type for constant");
}
}
/// Print a constant value or values (it may be an aggregate).
/// Uses printSingleConstantValue() to print each individual value.
///
void AsmPrinter::printConstantValueOnly(const Constant* CV,
int numPadBytesAfter) {
if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
if (CVA->isString()) {
// print the string alone and return
O << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
} else {
// Not a string. Print the values in successive locations
for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
printConstantValueOnly(CVA->getOperand(i));
}
} else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
// Print the fields in successive locations. Pad to align if needed!
const StructLayout *cvsLayout =
TM.getTargetData().getStructLayout(CVS->getType());
unsigned sizeSoFar = 0;
for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
const Constant* field = CVS->getOperand(i);
// Check if padding is needed and insert one or more 0s.
unsigned fieldSize =
TM.getTargetData().getTypeSize(field->getType());
int padSize = ((i == e-1? cvsLayout->StructSize
: cvsLayout->MemberOffsets[i+1])
- cvsLayout->MemberOffsets[i]) - fieldSize;
sizeSoFar += (fieldSize + padSize);
// Now print the actual field value
printConstantValueOnly(field, padSize);
}
assert(sizeSoFar == cvsLayout->StructSize &&
"Layout of constant struct may be incorrect!");
} else if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) {
PrintZeroBytesToPad(TM.getTargetData().getTypeSize(CV->getType()));
} else
printSingleConstantValue(CV);
if (numPadBytesAfter)
PrintZeroBytesToPad(numPadBytesAfter);
}
/// ConstantExprToString() - Convert a ConstantExpr to an asm expression
/// and return this as a string.
///
std::string AsmPrinter::ConstantExprToString(const ConstantExpr* CE,
const TargetMachine& target) {
std::string S;
switch(CE->getOpcode()) {
case Instruction::GetElementPtr:
{ // generate a symbolic expression for the byte address
const Value* ptrVal = CE->getOperand(0);
std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
const TargetData &TD = target.getTargetData();
S += "(" + valToExprString(ptrVal, target) + ") + ("
+ utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
break;
}
case Instruction::Cast:
// Support only non-converting casts for now, i.e., a no-op.
// This assertion is not a complete check.
assert(target.getTargetData().getTypeSize(CE->getType()) ==
target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
S += "(" + valToExprString(CE->getOperand(0), target) + ")";
break;
case Instruction::Add:
S += ConstantArithExprToString(CE, target, ") + (");
break;
case Instruction::Sub:
S += ConstantArithExprToString(CE, target, ") - (");
break;
case Instruction::Mul:
S += ConstantArithExprToString(CE, target, ") * (");
break;
case Instruction::Div:
S += ConstantArithExprToString(CE, target, ") / (");
break;
case Instruction::Rem:
S += ConstantArithExprToString(CE, target, ") % (");
break;
case Instruction::And:
// Logical && for booleans; bitwise & otherwise
S += ConstantArithExprToString(CE, target,
((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
break;
case Instruction::Or:
// Logical || for booleans; bitwise | otherwise
S += ConstantArithExprToString(CE, target,
((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
break;
case Instruction::Xor:
// Bitwise ^ for all types
S += ConstantArithExprToString(CE, target, ") ^ (");
break;
default:
assert(0 && "Unsupported operator in ConstantExprToString()");
break;
}
return S;
}
/// valToExprString - Helper function for ConstantExprToString().
/// Appends result to argument string S.
///
std::string AsmPrinter::valToExprString(const Value* V,
const TargetMachine& target) {
std::string S;
bool failed = false;
if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
S += getID(GV);
} else if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
S += std::string(CB == ConstantBool::True ? "1" : "0");
else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
S += itostr(CI->getValue());
else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
S += utostr(CI->getValue());
else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
S += ftostr(CFP->getValue());
else if (isa<ConstantPointerNull>(CV) || isa<UndefValue>(CV))
S += "0";
else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
S += ConstantExprToString(CE, target);
else
failed = true;
} else
failed = true;
if (failed) {
assert(0 && "Cannot convert value to string");
S += "<illegal-value>";
}
return S;
}
namespace {
struct SparcV9AsmPrinter : public FunctionPass, public AsmPrinter {
inline SparcV9AsmPrinter(std::ostream &os, const TargetMachine &t)
: AsmPrinter(os, t) {}
const Function *currFunction;
const char *getPassName() const {
return "Output SparcV9 Assembly for Functions";
}
virtual bool doInitialization(Module &M) {
startModule(M);
return false;
}
virtual bool runOnFunction(Function &F) {
currFunction = &F;
emitFunction(F);
return false;
}
virtual bool doFinalization(Module &M) {
emitGlobals(M);
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}
void emitFunction(const Function &F);
private :
void emitBasicBlock(const MachineBasicBlock &MBB);
void emitMachineInst(const MachineInstr *MI);
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
unsigned getOperandMask(unsigned Opcode) {
switch (Opcode) {
case V9::SUBccr:
case V9::SUBcci: return 1 << 3; // Remove CC argument
default: return 0; // By default, don't hack operands...
}
}
void emitGlobals(const Module &M);
void printGlobalVariable(const GlobalVariable *GV);
};
} // End anonymous namespace
inline bool
SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
unsigned int opNum) {
switch (MI->getOpcode()) {
case V9::JMPLCALLr:
case V9::JMPLCALLi:
case V9::JMPLRETr:
case V9::JMPLRETi:
return (opNum == 0);
default:
return false;
}
}
inline bool
SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
unsigned int opNum) {
if (TM.getInstrInfo()->isLoad(MI->getOpcode()))
return (opNum == 0);
else if (TM.getInstrInfo()->isStore(MI->getOpcode()))
return (opNum == 1);
else
return false;
}
unsigned int
SparcV9AsmPrinter::printOperands(const MachineInstr *MI, unsigned opNum) {
const MachineOperand& mop = MI->getOperand(opNum);
if (OpIsBranchTargetLabel(MI, opNum)) {
printOneOperand(mop, MI->getOpcode());
O << "+";
printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
return 2;
} else if (OpIsMemoryAddressBase(MI, opNum)) {
O << "[";
printOneOperand(mop, MI->getOpcode());
O << "+";
printOneOperand(MI->getOperand(opNum+1), MI->getOpcode());
O << "]";
return 2;
} else {
printOneOperand(mop, MI->getOpcode());
return 1;
}
}
void
SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop,
MachineOpCode opCode)
{
bool needBitsFlag = true;
if (mop.isHiBits32())
O << "%lm(";
else if (mop.isLoBits32())
O << "%lo(";
else if (mop.isHiBits64())
O << "%hh(";
else if (mop.isLoBits64())
O << "%hm(";
else
needBitsFlag = false;
switch (mop.getType())
{
case MachineOperand::MO_VirtualRegister:
case MachineOperand::MO_CCRegister:
case MachineOperand::MO_MachineRegister:
{
int regNum = (int)mop.getReg();
if (regNum == TM.getRegInfo()->getInvalidRegNum()) {
// better to print code with NULL registers than to die
O << "<NULL VALUE>";
} else {
O << "%" << TM.getRegInfo()->getUnifiedRegName(regNum);
}
break;
}
case MachineOperand::MO_ConstantPoolIndex:
{
O << ".CPI_" << getID(currFunction)
<< "_" << mop.getConstantPoolIndex();
break;
}
case MachineOperand::MO_PCRelativeDisp:
{
const Value *Val = mop.getVRegValue();
assert(Val && "\tNULL Value in SparcV9AsmPrinter");
if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
O << getID(BB);
else if (const Function *F = dyn_cast<Function>(Val))
O << getID(F);
else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
O << getID(GV);
else if (const Constant *CV = dyn_cast<Constant>(Val))
O << getID(CV);
else
assert(0 && "Unrecognized value in SparcV9AsmPrinter");
break;
}
case MachineOperand::MO_SignExtendedImmed:
O << mop.getImmedValue();
break;
case MachineOperand::MO_UnextendedImmed:
O << (uint64_t) mop.getImmedValue();
break;
default:
O << mop; // use dump field
break;
}
if (needBitsFlag)
O << ")";
}
void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) {
unsigned Opcode = MI->getOpcode();
if (Opcode == V9::PHI)
return; // Ignore Machine-PHI nodes.
O << "\t" << TM.getInstrInfo()->getName(Opcode) << "\t";
unsigned Mask = getOperandMask(Opcode);
bool NeedComma = false;
unsigned N = 1;
for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
if (NeedComma) O << ", "; // Handle comma outputting
NeedComma = true;
N = printOperands(MI, OpNum);
} else
N = 1;
O << "\n";
++EmittedInsts;
}
void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
// Emit a label for the basic block
O << getID(MBB.getBasicBlock()) << ":\n";
// Loop over all of the instructions in the basic block...
for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
MII != MIE; ++MII)
emitMachineInst(MII);
O << "\n"; // Separate BB's with newlines
}
void SparcV9AsmPrinter::emitFunction(const Function &F) {
std::string CurrentFnName = getID(&F);
MachineFunction &MF = MachineFunction::get(&F);
O << "!****** Outputing Function: " << CurrentFnName << " ******\n";
// Emit constant pool for this function
const MachineConstantPool *MCP = MF.getConstantPool();
const std::vector<Constant*> &CP = MCP->getConstants();
enterSection(ReadOnlyData);
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
std::string cpiName = ".CPI_" + CurrentFnName + "_" + utostr(i);
printConstant(CP[i], cpiName);
}
enterSection(Text);
O << "\t.align\t4\n\t.global\t" << CurrentFnName << "\n";
//O << "\t.type\t" << CurrentFnName << ",#function\n";
O << "\t.type\t" << CurrentFnName << ", 2\n";
O << CurrentFnName << ":\n";
// Output code for all of the basic blocks in the function...
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
emitBasicBlock(*I);
// Output a .size directive so the debugger knows the extents of the function
O << ".EndOf_" << CurrentFnName << ":\n\t.size "
<< CurrentFnName << ", .EndOf_"
<< CurrentFnName << "-" << CurrentFnName << "\n";
// Put some spaces between the functions
O << "\n\n";
}
void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
if (GV->hasExternalLinkage())
O << "\t.global\t" << getID(GV) << "\n";
if (GV->hasInitializer() &&
!(GV->getInitializer()->isNullValue() ||
isa<UndefValue>(GV->getInitializer()))) {
printConstant(GV->getInitializer(), getID(GV));
} else {
O << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
TM) << "\n";
O << "\t.type\t" << getID(GV) << ",#object\n";
O << "\t.reserve\t" << getID(GV) << ","
<< findOptimalStorageSize(TM, GV->getType()->getElementType())
<< "\n";
}
}
void SparcV9AsmPrinter::emitGlobals(const Module &M) {
// Output global variables...
for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
if (! GI->isExternal()) {
assert(GI->hasInitializer());
if (GI->isConstant())
enterSection(ReadOnlyData); // read-only, initialized data
else if (GI->getInitializer()->isNullValue() ||
isa<UndefValue>(GI->getInitializer()))
enterSection(ZeroInitRWData); // read-write zero data
else
enterSection(InitRWData); // read-write non-zero data
printGlobalVariable(GI);
}
O << "\n";
}
FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out, TargetMachine &TM) {
return new SparcV9AsmPrinter(Out, TM);
}
|