2006-05-14 22:18:28 +00:00
|
|
|
//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the "Instituto Nokia de Tecnologia" and
|
|
|
|
// is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to GAS-format ARM assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMInstrInfo.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-09-06 18:34:40 +00:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2006-07-27 11:38:51 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2006-05-14 22:18:28 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Mangler.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <cctype>
|
|
|
|
#include <iostream>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
|
|
|
|
2006-11-02 15:00:02 +00:00
|
|
|
static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
|
|
|
|
switch (CC) {
|
|
|
|
default: assert(0 && "Unknown condition code");
|
|
|
|
case ARMCC::EQ: return "eq";
|
|
|
|
case ARMCC::NE: return "ne";
|
|
|
|
case ARMCC::CS: return "cs";
|
|
|
|
case ARMCC::CC: return "cc";
|
|
|
|
case ARMCC::MI: return "mi";
|
|
|
|
case ARMCC::PL: return "pl";
|
|
|
|
case ARMCC::VS: return "vs";
|
|
|
|
case ARMCC::VC: return "vc";
|
|
|
|
case ARMCC::HI: return "hi";
|
|
|
|
case ARMCC::LS: return "ls";
|
|
|
|
case ARMCC::GE: return "ge";
|
|
|
|
case ARMCC::LT: return "lt";
|
|
|
|
case ARMCC::GT: return "gt";
|
|
|
|
case ARMCC::LE: return "le";
|
|
|
|
case ARMCC::AL: return "al";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-06 18:34:40 +00:00
|
|
|
struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
|
2006-09-07 22:06:40 +00:00
|
|
|
ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
|
2006-09-06 18:34:40 +00:00
|
|
|
: AsmPrinter(O, TM, T) {
|
|
|
|
}
|
2006-05-14 22:18:28 +00:00
|
|
|
|
|
|
|
/// We name each basic block in a Function with a unique number, so
|
|
|
|
/// that we can consistently refer to them later. This is cleared
|
|
|
|
/// at the beginning of each call to runOnMachineFunction().
|
|
|
|
///
|
|
|
|
typedef std::map<const Value *, unsigned> ValueMapTy;
|
|
|
|
ValueMapTy NumberForBB;
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "ARM Assembly Printer";
|
|
|
|
}
|
|
|
|
|
2006-09-11 17:25:40 +00:00
|
|
|
void printAddrMode1(const MachineInstr *MI, int opNum);
|
2006-10-17 18:04:53 +00:00
|
|
|
void printAddrMode5(const MachineInstr *MI, int opNum);
|
2006-09-11 17:25:40 +00:00
|
|
|
|
2006-08-17 17:09:40 +00:00
|
|
|
void printMemRegImm(const MachineInstr *MI, int opNum,
|
|
|
|
const char *Modifier = NULL) {
|
|
|
|
const MachineOperand &MO1 = MI->getOperand(opNum);
|
|
|
|
const MachineOperand &MO2 = MI->getOperand(opNum + 1);
|
2006-08-01 18:53:10 +00:00
|
|
|
assert(MO1.isImmediate());
|
2006-08-17 17:09:40 +00:00
|
|
|
bool arith = false;
|
|
|
|
if (Modifier != NULL) {
|
|
|
|
assert(strcmp(Modifier, "arith") == 0);
|
|
|
|
arith = true;
|
|
|
|
}
|
2006-08-01 18:53:10 +00:00
|
|
|
|
|
|
|
if (MO2.isConstantPoolIndex()) {
|
2006-08-17 17:09:40 +00:00
|
|
|
printOperand(MI, opNum + 1);
|
2006-08-01 18:53:10 +00:00
|
|
|
} else if (MO2.isRegister()) {
|
2006-08-17 17:09:40 +00:00
|
|
|
if(!arith)
|
|
|
|
O << '[';
|
|
|
|
printOperand(MI, opNum + 1);
|
2006-08-01 18:53:10 +00:00
|
|
|
O << ", ";
|
2006-08-17 17:09:40 +00:00
|
|
|
printOperand(MI, opNum);
|
|
|
|
if(!arith)
|
|
|
|
O << ']';
|
2006-08-01 18:53:10 +00:00
|
|
|
} else {
|
|
|
|
assert(0 && "Invalid Operand Type");
|
|
|
|
}
|
2006-07-11 11:36:48 +00:00
|
|
|
}
|
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum);
|
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
const char *Modifier = 0);
|
|
|
|
void printCCOperand(const MachineInstr *MI, int opNum);
|
|
|
|
|
|
|
|
bool printInstruction(const MachineInstr *MI); // autogenerated.
|
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
#include "ARMGenAsmWriter.inc"
|
|
|
|
|
|
|
|
/// createARMCodePrinterPass - Returns a pass that prints the ARM
|
|
|
|
/// assembly code for a MachineFunction to the given output stream,
|
|
|
|
/// using the given target machine description. This should work
|
|
|
|
/// regardless of whether the function is in SSA form.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
|
|
|
|
TargetMachine &tm) {
|
2006-09-07 22:06:40 +00:00
|
|
|
return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
|
|
|
bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2006-05-23 02:48:20 +00:00
|
|
|
SetupMachineFunction(MF);
|
|
|
|
O << "\n\n";
|
|
|
|
|
|
|
|
// Print out constants referenced by the function
|
|
|
|
EmitConstantPool(MF.getConstantPool());
|
|
|
|
|
|
|
|
// Print out jump tables referenced by the function
|
2006-10-05 03:01:21 +00:00
|
|
|
EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
|
2006-05-23 02:48:20 +00:00
|
|
|
|
|
|
|
// Print out labels for the function.
|
|
|
|
const Function *F = MF.getFunction();
|
2006-10-05 02:49:23 +00:00
|
|
|
SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
|
|
|
|
|
2006-05-23 02:48:20 +00:00
|
|
|
switch (F->getLinkage()) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
|
|
|
case Function::InternalLinkage:
|
|
|
|
break;
|
|
|
|
case Function::ExternalLinkage:
|
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
|
|
|
break;
|
|
|
|
case Function::WeakLinkage:
|
|
|
|
case Function::LinkOnceLinkage:
|
|
|
|
assert(0 && "Not implemented");
|
|
|
|
break;
|
|
|
|
}
|
2006-05-26 10:56:17 +00:00
|
|
|
EmitAlignment(2, F);
|
2006-05-23 02:48:20 +00:00
|
|
|
O << CurrentFnName << ":\n";
|
|
|
|
|
|
|
|
// Print out code for the function.
|
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Print a label for the basic block.
|
|
|
|
if (I != MF.begin()) {
|
|
|
|
printBasicBlockLabel(I, true);
|
|
|
|
O << '\n';
|
|
|
|
}
|
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
|
|
|
II != E; ++II) {
|
|
|
|
// Print the assembly for the instruction.
|
|
|
|
O << "\t";
|
|
|
|
printInstruction(II);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-09-11 17:25:40 +00:00
|
|
|
void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
|
2006-09-13 12:09:43 +00:00
|
|
|
const MachineOperand &Arg = MI->getOperand(opNum);
|
|
|
|
const MachineOperand &Shift = MI->getOperand(opNum + 1);
|
|
|
|
const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
|
2006-09-11 17:25:40 +00:00
|
|
|
|
2006-09-13 12:09:43 +00:00
|
|
|
if(Arg.isImmediate()) {
|
|
|
|
assert(Shift.getImmedValue() == 0);
|
2006-09-11 17:25:40 +00:00
|
|
|
printOperand(MI, opNum);
|
|
|
|
} else {
|
2006-09-13 12:09:43 +00:00
|
|
|
assert(Arg.isRegister());
|
2006-09-11 17:25:40 +00:00
|
|
|
printOperand(MI, opNum);
|
2006-09-13 12:09:43 +00:00
|
|
|
if(Shift.isRegister() || Shift.getImmedValue() != 0) {
|
|
|
|
const char *s = NULL;
|
|
|
|
switch(ShiftType.getImmedValue()) {
|
|
|
|
case ARMShift::LSL:
|
|
|
|
s = ", lsl ";
|
|
|
|
break;
|
|
|
|
case ARMShift::LSR:
|
|
|
|
s = ", lsr ";
|
|
|
|
break;
|
|
|
|
case ARMShift::ASR:
|
|
|
|
s = ", asr ";
|
|
|
|
break;
|
|
|
|
case ARMShift::ROR:
|
|
|
|
s = ", ror ";
|
|
|
|
break;
|
|
|
|
case ARMShift::RRX:
|
|
|
|
s = ", rrx ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
O << s;
|
|
|
|
printOperand(MI, opNum + 1);
|
|
|
|
}
|
2006-09-11 17:25:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-17 18:04:53 +00:00
|
|
|
void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
|
|
|
|
const MachineOperand &Arg = MI->getOperand(opNum);
|
|
|
|
const MachineOperand &Offset = MI->getOperand(opNum + 1);
|
|
|
|
assert(Offset.isImmediate());
|
|
|
|
|
|
|
|
if (Arg.isConstantPoolIndex()) {
|
|
|
|
assert(Offset.getImmedValue() == 0);
|
|
|
|
printOperand(MI, opNum);
|
|
|
|
} else {
|
|
|
|
assert(Arg.isRegister());
|
|
|
|
O << '[';
|
|
|
|
printOperand(MI, opNum);
|
|
|
|
O << ", ";
|
|
|
|
printOperand(MI, opNum + 1);
|
|
|
|
O << ']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
|
2006-05-25 12:57:06 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand (opNum);
|
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
|
|
|
|
O << LowercaseString (RI.get(MO.getReg()).Name);
|
|
|
|
else
|
|
|
|
assert(0 && "not implemented");
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
O << "#" << (int)MO.getImmedValue();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2006-08-24 13:45:55 +00:00
|
|
|
printBasicBlockLabel(MO.getMachineBasicBlock());
|
2006-05-25 12:57:06 +00:00
|
|
|
return;
|
2006-07-16 01:02:57 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
GlobalValue *GV = MO.getGlobal();
|
|
|
|
std::string Name = Mang->getValueName(GV);
|
|
|
|
O << Name;
|
|
|
|
}
|
2006-05-25 12:57:06 +00:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2006-10-16 21:10:32 +00:00
|
|
|
O << TAI->getGlobalPrefix() << MO.getSymbolName();
|
2006-05-25 12:57:06 +00:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2006-09-06 18:34:40 +00:00
|
|
|
O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
|
2006-08-01 12:58:43 +00:00
|
|
|
<< '_' << MO.getConstantPoolIndex();
|
2006-05-25 12:57:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
O << "<unknown operand type>"; abort (); break;
|
|
|
|
}
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
const char *Modifier) {
|
|
|
|
assert(0 && "not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
|
2006-08-24 16:13:15 +00:00
|
|
|
int CC = (int)MI->getOperand(opNum).getImmedValue();
|
|
|
|
O << ARMCondCodeToString((ARMCC::CondCodes)CC);
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMAsmPrinter::doInitialization(Module &M) {
|
2006-09-11 12:49:38 +00:00
|
|
|
AsmPrinter::doInitialization(M);
|
2006-05-14 22:18:28 +00:00
|
|
|
return false; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMAsmPrinter::doFinalization(Module &M) {
|
2006-07-27 11:38:51 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
|
|
|
|
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!I->hasInitializer()) // External global require no code
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
|
|
|
unsigned Align = TD->getTypeAlignment(C->getType());
|
|
|
|
|
2006-10-19 13:30:40 +00:00
|
|
|
if (C->isNullValue() &&
|
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage())) {
|
|
|
|
SwitchToDataSection(".data", I);
|
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
|
|
|
|
|
|
|
O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
|
|
|
|
<< "," << (unsigned)TD->getTypeAlignment(C->getType());
|
|
|
|
O << "\t\t";
|
|
|
|
O << TAI->getCommentString() << " ";
|
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << "\n";
|
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
|
|
|
break;
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
break;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
break;
|
|
|
|
}
|
2006-07-27 11:38:51 +00:00
|
|
|
|
2006-11-01 14:26:44 +00:00
|
|
|
if (C->isNullValue())
|
|
|
|
SwitchToDataSection(".bss", I);
|
|
|
|
else
|
|
|
|
SwitchToDataSection(".data", I);
|
2006-07-27 11:38:51 +00:00
|
|
|
|
2006-10-19 13:30:40 +00:00
|
|
|
EmitAlignment(Align, I);
|
|
|
|
O << "\t.type " << name << ", %object\n";
|
|
|
|
O << "\t.size " << name << ", " << Size << "\n";
|
|
|
|
O << name << ":\n";
|
|
|
|
EmitGlobalConstant(C);
|
|
|
|
}
|
2006-07-27 11:38:51 +00:00
|
|
|
}
|
2006-10-19 13:30:40 +00:00
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false; // success
|
|
|
|
}
|