2004-08-11 00:09:42 +00:00
|
|
|
//===-- PPC32AsmPrinter.cpp - Print machine instrs to PowerPC assembly ----===//
|
2004-06-21 16:55:25 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-07-08 17:58:04 +00:00
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to PowerPC assembly language. This printer is
|
2004-07-28 20:18:53 +00:00
|
|
|
// the output mechanism used by `llc'.
|
2004-06-21 16:55:25 +00:00
|
|
|
//
|
2004-07-08 17:58:04 +00:00
|
|
|
// Documentation at http://developer.apple.com/documentation/DeveloperTools/
|
|
|
|
// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
|
2004-06-29 17:13:26 +00:00
|
|
|
//
|
2004-06-21 16:55:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-06-24 17:31:42 +00:00
|
|
|
#define DEBUG_TYPE "asmprinter"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "PowerPC.h"
|
2004-08-17 04:55:41 +00:00
|
|
|
#include "PPC32TargetMachine.h"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
2004-08-16 23:25:21 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2004-06-24 17:31:42 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2004-08-14 23:27:29 +00:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2004-06-24 17:31:42 +00:00
|
|
|
#include "Support/CommandLine.h"
|
|
|
|
#include "Support/Debug.h"
|
2004-06-21 16:55:25 +00:00
|
|
|
#include "Support/Statistic.h"
|
|
|
|
#include "Support/StringExtras.h"
|
2004-06-24 17:31:42 +00:00
|
|
|
#include <set>
|
2004-08-16 23:25:21 +00:00
|
|
|
using namespace llvm;
|
2004-06-21 16:55:25 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
struct PPC32AsmPrinter : public AsmPrinter {
|
2004-07-21 20:11:11 +00:00
|
|
|
std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
|
2004-06-21 16:55:25 +00:00
|
|
|
std::set<std::string> Strings;
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
PPC32AsmPrinter(std::ostream &O, TargetMachine &TM)
|
2004-08-17 02:29:00 +00:00
|
|
|
: AsmPrinter(O, TM), LabelNumber(0) {
|
2004-08-17 16:27:26 +00:00
|
|
|
CommentChar = ";";
|
2004-08-17 06:07:43 +00:00
|
|
|
GlobalPrefix = "_";
|
2004-08-17 06:37:12 +00:00
|
|
|
ZeroDirective = "\t.space\t"; // ".space N" emits N zeros.
|
|
|
|
Data64bitsDirective = 0; // we can't emit a 64-bit unit
|
2004-08-17 02:29:00 +00:00
|
|
|
}
|
2004-06-21 16:55:25 +00:00
|
|
|
|
2004-07-26 16:28:33 +00:00
|
|
|
/// Unique incrementer for label values for referencing Global values.
|
2004-06-29 17:13:26 +00:00
|
|
|
///
|
2004-07-26 16:28:33 +00:00
|
|
|
unsigned LabelNumber;
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
virtual const char *getPassName() const {
|
2004-08-17 04:55:41 +00:00
|
|
|
return "PPC32 Assembly Printer";
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
PPC32TargetMachine &getTM() {
|
|
|
|
return static_cast<PPC32TargetMachine&>(TM);
|
2004-08-16 23:25:21 +00:00
|
|
|
}
|
|
|
|
|
2004-08-14 22:09:10 +00:00
|
|
|
/// printInstruction - This method is automatically generated by tablegen
|
|
|
|
/// from the instruction set description. This method returns true if the
|
|
|
|
/// machine instruction was sufficiently described to print it, otherwise it
|
|
|
|
/// returns false.
|
|
|
|
bool printInstruction(const MachineInstr *MI);
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
2004-08-13 09:32:01 +00:00
|
|
|
void printOp(const MachineOperand &MO, bool LoadAddrOp = false);
|
2004-07-28 00:00:48 +00:00
|
|
|
void printImmOp(const MachineOperand &MO, unsigned ArgType);
|
2004-08-14 23:27:29 +00:00
|
|
|
|
|
|
|
void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
if (MO.getType() == MachineOperand::MO_MachineRegister) {
|
|
|
|
assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
|
|
|
|
O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name);
|
2004-08-15 05:46:14 +00:00
|
|
|
} else if (MO.isImmediate()) {
|
|
|
|
O << MO.getImmedValue();
|
2004-08-14 23:27:29 +00:00
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-15 05:20:16 +00:00
|
|
|
void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MVT::ValueType VT) {
|
|
|
|
O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
|
|
|
|
}
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
void printConstantPool(MachineConstantPool *MCP);
|
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2004-08-11 00:09:42 +00:00
|
|
|
/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC
|
2004-06-21 16:55:25 +00:00
|
|
|
/// assembly code for a MachineFunction to the given output stream,
|
|
|
|
/// using the given target machine description. This should work
|
2004-08-09 22:27:45 +00:00
|
|
|
/// regardless of whether the function is in SSA form or not.
|
2004-06-21 16:55:25 +00:00
|
|
|
///
|
2004-08-17 04:55:41 +00:00
|
|
|
FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) {
|
|
|
|
return new PPC32AsmPrinter(o, tm);
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
2004-08-14 22:09:10 +00:00
|
|
|
// Include the auto-generated portion of the assembly writer
|
|
|
|
#include "PowerPCGenAsmWriter.inc"
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
/// printConstantPool - Print to the current output stream assembly
|
|
|
|
/// representations of the constants in the constant pool MCP. This is
|
|
|
|
/// used to print out constants which have been "spilled to memory" by
|
|
|
|
/// the code generator.
|
|
|
|
///
|
2004-08-17 04:55:41 +00:00
|
|
|
void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
|
2004-06-21 16:55:25 +00:00
|
|
|
const std::vector<Constant*> &CP = MCP->getConstants();
|
|
|
|
const TargetData &TD = TM.getTargetData();
|
|
|
|
|
|
|
|
if (CP.empty()) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
|
|
|
|
O << "\t.const\n";
|
|
|
|
O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
|
|
|
|
<< "\n";
|
2004-06-29 17:13:26 +00:00
|
|
|
O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t;"
|
2004-06-21 16:55:25 +00:00
|
|
|
<< *CP[i] << "\n";
|
|
|
|
emitGlobalConstant(CP[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
2004-08-17 04:55:41 +00:00
|
|
|
bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2004-08-16 23:25:21 +00:00
|
|
|
setupMachineFunction(MF);
|
2004-06-21 16:55:25 +00:00
|
|
|
O << "\n\n";
|
|
|
|
|
|
|
|
// Print out constants referenced by the function
|
|
|
|
printConstantPool(MF.getConstantPool());
|
|
|
|
|
|
|
|
// Print out labels for the function.
|
|
|
|
O << "\t.text\n";
|
|
|
|
O << "\t.globl\t" << CurrentFnName << "\n";
|
2004-06-29 23:40:57 +00:00
|
|
|
O << "\t.align 2\n";
|
2004-06-21 16:55:25 +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.
|
2004-06-29 17:13:26 +00:00
|
|
|
O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t; "
|
2004-06-21 16:55:25 +00:00
|
|
|
<< I->getBasicBlock()->getName() << "\n";
|
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
2004-06-24 23:04:11 +00:00
|
|
|
II != E; ++II) {
|
2004-06-21 16:55:25 +00:00
|
|
|
// Print the assembly for the instruction.
|
|
|
|
O << "\t";
|
|
|
|
printMachineInstruction(II);
|
|
|
|
}
|
|
|
|
}
|
2004-07-26 16:28:33 +00:00
|
|
|
++LabelNumber;
|
2004-06-21 16:55:25 +00:00
|
|
|
|
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
void PPC32AsmPrinter::printOp(const MachineOperand &MO,
|
|
|
|
bool LoadAddrOp /* = false */) {
|
2004-06-21 16:55:25 +00:00
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
|
|
|
int new_symbol;
|
|
|
|
|
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
if (Value *V = MO.getVRegValueOrNull()) {
|
|
|
|
O << "<" << V->getName() << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
2004-07-08 17:58:04 +00:00
|
|
|
case MachineOperand::MO_CCRegister:
|
2004-06-24 23:51:00 +00:00
|
|
|
O << LowercaseString(RI.get(MO.getReg()).Name);
|
|
|
|
return;
|
2004-06-21 16:55:25 +00:00
|
|
|
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
2004-07-28 00:00:48 +00:00
|
|
|
std::cerr << "printOp() does not handle immediate values\n";
|
|
|
|
abort();
|
2004-06-21 16:55:25 +00:00
|
|
|
return;
|
2004-07-28 00:00:48 +00:00
|
|
|
|
2004-07-08 17:58:04 +00:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs";
|
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
|
|
|
|
O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
|
2004-06-29 17:13:26 +00:00
|
|
|
<< "_" << MBBOp->getNumber() << "\t; "
|
2004-06-25 15:42:10 +00:00
|
|
|
<< MBBOp->getBasicBlock()->getName();
|
2004-06-21 16:55:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-07-08 17:58:04 +00:00
|
|
|
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
|
2004-06-21 16:55:25 +00:00
|
|
|
return;
|
2004-07-08 17:58:04 +00:00
|
|
|
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
return;
|
|
|
|
|
2004-08-13 09:32:01 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
GlobalValue *GV = MO.getGlobal();
|
|
|
|
std::string Name = Mang->getValueName(GV);
|
|
|
|
|
|
|
|
// Dynamically-resolved functions need a stub for the function. Be
|
|
|
|
// wary however not to output $stub for external functions whose addresses
|
|
|
|
// are taken. Those should be emitted as $non_lazy_ptr below.
|
|
|
|
Function *F = dyn_cast<Function>(GV);
|
|
|
|
if (F && F->isExternal() && !LoadAddrOp &&
|
2004-08-16 23:25:21 +00:00
|
|
|
getTM().CalledFunctions.count(F)) {
|
2004-08-13 09:32:01 +00:00
|
|
|
FnStubs.insert(Name);
|
|
|
|
O << "L" << Name << "$stub";
|
|
|
|
return;
|
|
|
|
}
|
2004-08-14 22:09:10 +00:00
|
|
|
|
2004-08-13 09:32:01 +00:00
|
|
|
// External global variables need a non-lazily-resolved stub
|
2004-08-16 23:25:21 +00:00
|
|
|
if (GV->isExternal() && getTM().AddressTaken.count(GV)) {
|
2004-08-13 09:32:01 +00:00
|
|
|
GVStubs.insert(Name);
|
|
|
|
O << "L" << Name << "$non_lazy_ptr";
|
|
|
|
return;
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
2004-08-14 22:09:10 +00:00
|
|
|
|
2004-08-16 23:25:21 +00:00
|
|
|
if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) {
|
2004-08-14 22:09:10 +00:00
|
|
|
LinkOnceStubs.insert(Name);
|
|
|
|
O << "L" << Name << "$non_lazy_ptr";
|
|
|
|
return;
|
|
|
|
}
|
2004-08-13 09:32:01 +00:00
|
|
|
|
|
|
|
O << Mang->getValueName(GV);
|
2004-06-21 16:55:25 +00:00
|
|
|
return;
|
2004-08-13 09:32:01 +00:00
|
|
|
}
|
2004-07-08 17:58:04 +00:00
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
default:
|
2004-07-08 17:58:04 +00:00
|
|
|
O << "<unknown operand type: " << MO.getType() << ">";
|
2004-06-25 15:11:34 +00:00
|
|
|
return;
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) {
|
2004-07-28 00:00:48 +00:00
|
|
|
int Imm = MO.getImmedValue();
|
2004-08-10 22:47:03 +00:00
|
|
|
if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) {
|
2004-07-28 00:00:48 +00:00
|
|
|
O << (short)Imm;
|
|
|
|
} else {
|
|
|
|
O << Imm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-14 22:09:10 +00:00
|
|
|
/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
|
|
|
|
/// the current output stream.
|
2004-06-21 16:55:25 +00:00
|
|
|
///
|
2004-08-17 04:55:41 +00:00
|
|
|
void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
2004-08-14 22:09:10 +00:00
|
|
|
++EmittedInsts;
|
|
|
|
if (printInstruction(MI))
|
|
|
|
return; // Printer was automatically generated
|
|
|
|
|
2004-06-21 16:55:25 +00:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
const TargetInstrInfo &TII = *TM.getInstrInfo();
|
|
|
|
const TargetInstrDescriptor &Desc = TII.get(Opcode);
|
2004-07-28 00:00:48 +00:00
|
|
|
unsigned i;
|
2004-06-25 19:24:52 +00:00
|
|
|
|
2004-07-28 00:00:48 +00:00
|
|
|
unsigned ArgCount = MI->getNumOperands();
|
|
|
|
unsigned ArgType[] = {
|
2004-08-10 22:47:03 +00:00
|
|
|
(Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask,
|
|
|
|
(Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask,
|
|
|
|
(Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask,
|
|
|
|
(Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask,
|
|
|
|
(Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask
|
2004-06-25 15:11:34 +00:00
|
|
|
};
|
2004-08-10 22:47:03 +00:00
|
|
|
assert(((Desc.TSFlags & PPCII::VMX) == 0) &&
|
2004-06-24 23:04:11 +00:00
|
|
|
"Instruction requires VMX support");
|
2004-08-10 22:47:03 +00:00
|
|
|
assert(((Desc.TSFlags & PPCII::PPC64) == 0) &&
|
2004-06-24 23:04:11 +00:00
|
|
|
"Instruction requires 64 bit support");
|
2004-06-21 16:55:25 +00:00
|
|
|
|
2004-07-20 00:42:19 +00:00
|
|
|
// CALLpcrel and CALLindirect are handled specially here to print only the
|
|
|
|
// appropriate number of args that the assembler expects. This is because
|
|
|
|
// may have many arguments appended to record the uses of registers that are
|
|
|
|
// holding arguments to the called function.
|
2004-08-10 22:47:03 +00:00
|
|
|
if (Opcode == PPC::COND_BRANCH) {
|
2004-07-27 18:40:39 +00:00
|
|
|
std::cerr << "Error: untranslated conditional branch psuedo instruction!\n";
|
|
|
|
abort();
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (Opcode == PPC::IMPLICIT_DEF) {
|
2004-07-16 19:01:13 +00:00
|
|
|
O << "; IMPLICIT DEF ";
|
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << "\n";
|
|
|
|
return;
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (Opcode == PPC::CALLpcrel) {
|
2004-07-28 00:00:48 +00:00
|
|
|
O << TII.getName(Opcode) << " ";
|
2004-07-20 00:42:19 +00:00
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << "\n";
|
|
|
|
return;
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (Opcode == PPC::CALLindirect) {
|
2004-07-28 00:00:48 +00:00
|
|
|
O << TII.getName(Opcode) << " ";
|
|
|
|
printImmOp(MI->getOperand(0), ArgType[0]);
|
2004-07-20 00:42:19 +00:00
|
|
|
O << ", ";
|
2004-07-28 00:00:48 +00:00
|
|
|
printImmOp(MI->getOperand(1), ArgType[0]);
|
2004-07-20 00:42:19 +00:00
|
|
|
O << "\n";
|
|
|
|
return;
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (Opcode == PPC::MovePCtoLR) {
|
2004-07-20 00:42:19 +00:00
|
|
|
// FIXME: should probably be converted to cout.width and cout.fill
|
2004-07-26 16:28:33 +00:00
|
|
|
O << "bl \"L0000" << LabelNumber << "$pb\"\n";
|
|
|
|
O << "\"L0000" << LabelNumber << "$pb\":\n";
|
2004-06-29 17:13:26 +00:00
|
|
|
O << "\tmflr ";
|
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << "\n";
|
2004-06-21 16:55:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-07-28 00:00:48 +00:00
|
|
|
O << TII.getName(Opcode) << " ";
|
2004-08-10 22:47:03 +00:00
|
|
|
if (Opcode == PPC::LOADLoDirect || Opcode == PPC::LOADLoIndirect) {
|
2004-06-21 16:55:25 +00:00
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << ", lo16(";
|
2004-08-13 09:32:01 +00:00
|
|
|
printOp(MI->getOperand(2), true /* LoadAddrOp */);
|
2004-07-26 16:28:33 +00:00
|
|
|
O << "-\"L0000" << LabelNumber << "$pb\")";
|
2004-06-29 17:13:26 +00:00
|
|
|
O << "(";
|
2004-08-10 22:47:03 +00:00
|
|
|
if (MI->getOperand(1).getReg() == PPC::R0)
|
2004-06-29 17:13:26 +00:00
|
|
|
O << "0";
|
|
|
|
else
|
|
|
|
printOp(MI->getOperand(1));
|
|
|
|
O << ")\n";
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (Opcode == PPC::LOADHiAddr) {
|
2004-06-21 16:55:25 +00:00
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << ", ";
|
2004-08-10 22:47:03 +00:00
|
|
|
if (MI->getOperand(1).getReg() == PPC::R0)
|
2004-06-29 17:13:26 +00:00
|
|
|
O << "0";
|
|
|
|
else
|
|
|
|
printOp(MI->getOperand(1));
|
2004-06-21 16:55:25 +00:00
|
|
|
O << ", ha16(" ;
|
2004-08-13 09:32:01 +00:00
|
|
|
printOp(MI->getOperand(2), true /* LoadAddrOp */);
|
2004-07-26 16:28:33 +00:00
|
|
|
O << "-\"L0000" << LabelNumber << "$pb\")\n";
|
2004-08-10 22:47:03 +00:00
|
|
|
} else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) {
|
2004-06-21 16:55:25 +00:00
|
|
|
printOp(MI->getOperand(0));
|
|
|
|
O << ", ";
|
2004-07-28 00:00:48 +00:00
|
|
|
printImmOp(MI->getOperand(1), ArgType[1]);
|
2004-06-21 16:55:25 +00:00
|
|
|
O << "(";
|
2004-06-30 21:54:12 +00:00
|
|
|
if (MI->getOperand(2).hasAllocatedReg() &&
|
2004-08-10 22:47:03 +00:00
|
|
|
MI->getOperand(2).getReg() == PPC::R0)
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "0";
|
|
|
|
else
|
|
|
|
printOp(MI->getOperand(2));
|
2004-06-21 16:55:25 +00:00
|
|
|
O << ")\n";
|
|
|
|
} else {
|
2004-06-24 23:51:00 +00:00
|
|
|
for (i = 0; i < ArgCount; ++i) {
|
2004-07-27 18:40:39 +00:00
|
|
|
// addi and friends
|
2004-08-10 22:47:03 +00:00
|
|
|
if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 &&
|
2004-07-01 21:09:12 +00:00
|
|
|
MI->getOperand(1).hasAllocatedReg() &&
|
2004-08-10 22:47:03 +00:00
|
|
|
MI->getOperand(1).getReg() == PPC::R0) {
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "0";
|
2004-07-27 18:40:39 +00:00
|
|
|
// for long branch support, bc $+8
|
|
|
|
} else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() &&
|
|
|
|
TII.isBranch(MI->getOpcode())) {
|
|
|
|
O << "$+8";
|
|
|
|
assert(8 == MI->getOperand(i).getImmedValue()
|
|
|
|
&& "branch off PC not to pc+8?");
|
|
|
|
//printOp(MI->getOperand(i));
|
2004-07-28 00:00:48 +00:00
|
|
|
} else if (MI->getOperand(i).isImmediate()) {
|
|
|
|
printImmOp(MI->getOperand(i), ArgType[i]);
|
2004-06-29 17:13:26 +00:00
|
|
|
} else {
|
2004-06-24 23:04:11 +00:00
|
|
|
printOp(MI->getOperand(i));
|
|
|
|
}
|
2004-06-24 23:51:00 +00:00
|
|
|
if (ArgCount - 1 == i)
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "\n";
|
|
|
|
else
|
|
|
|
O << ", ";
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-14 22:09:10 +00:00
|
|
|
return;
|
2004-06-21 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwitchSection - Switch to the specified section of the executable if we are
|
|
|
|
// not already in it!
|
|
|
|
//
|
|
|
|
static void SwitchSection(std::ostream &OS, std::string &CurSection,
|
|
|
|
const char *NewSection) {
|
|
|
|
if (CurSection != NewSection) {
|
|
|
|
CurSection = NewSection;
|
|
|
|
if (!CurSection.empty())
|
|
|
|
OS << "\t" << NewSection << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-17 04:55:41 +00:00
|
|
|
bool PPC32AsmPrinter::doFinalization(Module &M) {
|
2004-06-21 16:55:25 +00:00
|
|
|
const TargetData &TD = TM.getTargetData();
|
|
|
|
std::string CurSection;
|
|
|
|
|
|
|
|
// Print out module-level global variables here.
|
|
|
|
for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
|
|
|
|
if (I->hasInitializer()) { // External global require no code
|
|
|
|
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());
|
|
|
|
|
2004-07-21 20:11:11 +00:00
|
|
|
if (C->isNullValue() && /* FIXME: Verify correct */
|
|
|
|
(I->hasInternalLinkage() || I->hasWeakLinkage())) {
|
2004-06-21 16:55:25 +00:00
|
|
|
SwitchSection(O, CurSection, ".data");
|
|
|
|
if (I->hasInternalLinkage())
|
2004-07-23 16:08:20 +00:00
|
|
|
O << ".lcomm " << name << "," << TD.getTypeSize(C->getType())
|
2004-06-29 17:13:26 +00:00
|
|
|
<< "," << (unsigned)TD.getTypeAlignment(C->getType());
|
|
|
|
else
|
2004-07-23 16:08:20 +00:00
|
|
|
O << ".comm " << name << "," << TD.getTypeSize(C->getType());
|
2004-06-29 17:13:26 +00:00
|
|
|
O << "\t\t; ";
|
2004-06-21 16:55:25 +00:00
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << "\n";
|
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
2004-07-21 20:11:11 +00:00
|
|
|
O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
|
|
|
|
<< ".weak_definition " << name << '\n'
|
|
|
|
<< ".private_extern " << name << '\n'
|
|
|
|
<< ".section __DATA,__datacoal_nt,coalesced,no_toc\n";
|
|
|
|
LinkOnceStubs.insert(name);
|
|
|
|
break;
|
2004-06-21 16:55:25 +00:00
|
|
|
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
|
|
|
|
// Nonnull linkonce -> weak
|
|
|
|
O << "\t.weak " << name << "\n";
|
|
|
|
SwitchSection(O, CurSection, "");
|
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
|
|
|
break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// FIXME: appending linkage variables should go into a section of
|
|
|
|
// their name or something. For now, just emit them as external.
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// If external or appending, declare as a global symbol
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
// FALL THROUGH
|
|
|
|
case GlobalValue::InternalLinkage:
|
2004-06-29 23:40:57 +00:00
|
|
|
SwitchSection(O, CurSection, ".data");
|
2004-06-21 16:55:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "\t.align " << Align << "\n";
|
2004-06-29 17:13:26 +00:00
|
|
|
O << name << ":\t\t\t\t; ";
|
2004-06-21 16:55:25 +00:00
|
|
|
WriteAsOperand(O, I, true, true, &M);
|
|
|
|
O << " = ";
|
|
|
|
WriteAsOperand(O, C, false, false, &M);
|
|
|
|
O << "\n";
|
|
|
|
emitGlobalConstant(C);
|
|
|
|
}
|
|
|
|
}
|
2004-07-16 20:29:04 +00:00
|
|
|
|
|
|
|
// Output stubs for dynamically-linked functions
|
|
|
|
for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
|
|
|
|
i != e; ++i)
|
2004-06-24 23:04:11 +00:00
|
|
|
{
|
2004-07-23 16:08:20 +00:00
|
|
|
O << ".data\n";
|
|
|
|
O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n";
|
|
|
|
O << "\t.align 2\n";
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "L" << *i << "$stub:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\tmflr r0\n";
|
2004-07-23 16:08:20 +00:00
|
|
|
O << "\tbcl 20,31,L0$" << *i << "\n";
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "L0$" << *i << ":\n";
|
|
|
|
O << "\tmflr r11\n";
|
|
|
|
O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n";
|
|
|
|
O << "\tmtlr r0\n";
|
2004-07-23 16:08:20 +00:00
|
|
|
O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n";
|
2004-06-24 23:04:11 +00:00
|
|
|
O << "\tmtctr r12\n";
|
|
|
|
O << "\tbctr\n";
|
|
|
|
O << ".data\n";
|
|
|
|
O << ".lazy_symbol_pointer\n";
|
|
|
|
O << "L" << *i << "$lazy_ptr:\n";
|
2004-07-23 16:08:20 +00:00
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\t.long dyld_stub_binding_helper\n";
|
2004-06-24 23:04:11 +00:00
|
|
|
}
|
2004-06-21 16:55:25 +00:00
|
|
|
|
2004-07-16 20:29:04 +00:00
|
|
|
O << "\n";
|
|
|
|
|
|
|
|
// Output stubs for external global variables
|
|
|
|
if (GVStubs.begin() != GVStubs.end())
|
2004-07-23 16:08:20 +00:00
|
|
|
O << ".data\n.non_lazy_symbol_pointer\n";
|
2004-07-16 20:29:04 +00:00
|
|
|
for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
O << "L" << *i << "$non_lazy_ptr:\n";
|
|
|
|
O << "\t.indirect_symbol " << *i << "\n";
|
|
|
|
O << "\t.long\t0\n";
|
|
|
|
}
|
|
|
|
|
2004-08-14 22:09:10 +00:00
|
|
|
// Output stubs for link-once variables
|
|
|
|
if (LinkOnceStubs.begin() != LinkOnceStubs.end())
|
|
|
|
O << ".data\n.align 2\n";
|
|
|
|
for (std::set<std::string>::iterator i = LinkOnceStubs.begin(),
|
|
|
|
e = LinkOnceStubs.end(); i != e; ++i) {
|
|
|
|
O << "L" << *i << "$non_lazy_ptr:\n"
|
|
|
|
<< "\t.long\t" << *i << '\n';
|
|
|
|
}
|
|
|
|
|
2004-08-16 23:25:21 +00:00
|
|
|
AsmPrinter::doFinalization(M);
|
2004-06-21 16:55:25 +00:00
|
|
|
return false; // success
|
|
|
|
}
|