2008-05-13 17:02:57 +08:00
|
|
|
//===- PIC16InstrInfo.cpp - PIC16 Instruction Information -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the PIC16 implementation of the TargetInstrInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PIC16.h"
|
|
|
|
#include "PIC16InstrInfo.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "PIC16GenInstrInfo.inc"
|
2008-10-08 15:23:46 +08:00
|
|
|
#include <cstdio>
|
2008-05-13 17:02:57 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2008-05-14 19:31:39 +08:00
|
|
|
// FIXME: Add the subtarget support on this constructor.
|
2008-05-13 17:02:57 +08:00
|
|
|
PIC16InstrInfo::PIC16InstrInfo(PIC16TargetMachine &tm)
|
|
|
|
: TargetInstrInfoImpl(PIC16Insts, array_lengthof(PIC16Insts)),
|
|
|
|
TM(tm), RI(*this) {}
|
|
|
|
|
|
|
|
static bool isZeroImm(const MachineOperand &op) {
|
2008-10-03 23:45:36 +08:00
|
|
|
return op.isImm() && op.getImm() == 0;
|
2008-05-13 17:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// isLoadFromStackSlot - If the specified machine instruction is a direct
|
|
|
|
/// load from a stack slot, return the virtual or physical register number of
|
|
|
|
/// the destination along with the FrameIndex of the loaded stack slot. If
|
|
|
|
/// not, return 0. This predicate must return 0 if the instruction has
|
|
|
|
/// any side effects other than loading from the stack slot.
|
|
|
|
unsigned PIC16InstrInfo::
|
2008-11-19 03:49:32 +08:00
|
|
|
isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
|
2008-05-13 17:02:57 +08:00
|
|
|
{
|
|
|
|
if (MI->getOpcode() == PIC16::MOVF) {
|
2008-10-03 23:45:36 +08:00
|
|
|
if ((MI->getOperand(2).isFI()) && // is a stack slot
|
|
|
|
(MI->getOperand(1).isImm()) && // the imm is zero
|
2008-05-13 17:02:57 +08:00
|
|
|
(isZeroImm(MI->getOperand(1)))) {
|
|
|
|
FrameIndex = MI->getOperand(2).getIndex();
|
|
|
|
return MI->getOperand(0).getReg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isStoreToStackSlot - If the specified machine instruction is a direct
|
|
|
|
/// store to a stack slot, return the virtual or physical register number of
|
|
|
|
/// the source reg along with the FrameIndex of the loaded stack slot. If
|
|
|
|
/// not, return 0. This predicate must return 0 if the instruction has
|
|
|
|
/// any side effects other than storing to the stack slot.
|
|
|
|
unsigned PIC16InstrInfo::
|
2008-11-19 03:49:32 +08:00
|
|
|
isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
|
2008-05-13 17:02:57 +08:00
|
|
|
{
|
|
|
|
if (MI->getOpcode() == PIC16::MOVWF) {
|
2008-10-03 23:45:36 +08:00
|
|
|
if ((MI->getOperand(0).isFI()) && // is a stack slot
|
|
|
|
(MI->getOperand(1).isImm()) && // the imm is zero
|
2008-05-13 17:02:57 +08:00
|
|
|
(isZeroImm(MI->getOperand(1)))) {
|
|
|
|
FrameIndex = MI->getOperand(0).getIndex();
|
|
|
|
return MI->getOperand(2).getReg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PIC16InstrInfo::
|
|
|
|
storeRegToStackSlot(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned SrcReg, bool isKill, int FI,
|
|
|
|
const TargetRegisterClass *RC) const {
|
|
|
|
const Function *Func = MBB.getParent()->getFunction();
|
|
|
|
const std::string FuncName = Func->getName();
|
|
|
|
|
|
|
|
char *tmpName = new char [strlen(FuncName.c_str()) + 6];
|
|
|
|
sprintf(tmpName, "%s_tmp_%d",FuncName.c_str(),FI);
|
|
|
|
|
|
|
|
if (RC == PIC16::CPURegsRegisterClass) {
|
|
|
|
//src is always WREG.
|
|
|
|
BuildMI(MBB, I, this->get(PIC16::MOVWF))
|
|
|
|
.addReg(SrcReg,false,false,true,true)
|
|
|
|
.addExternalSymbol(tmpName) // the current printer expects 3 operands,
|
|
|
|
.addExternalSymbol(tmpName); // all we need is actually one,
|
2008-05-14 19:31:39 +08:00
|
|
|
// so we repeat.
|
2008-05-13 17:02:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(0 && "Can't store this register to stack slot");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PIC16InstrInfo::
|
|
|
|
loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
|
|
|
unsigned DestReg, int FI,
|
|
|
|
const TargetRegisterClass *RC) const
|
|
|
|
{
|
|
|
|
const Function *Func = MBB.getParent()->getFunction();
|
|
|
|
const std::string FuncName = Func->getName();
|
|
|
|
|
|
|
|
char *tmpName = new char [strlen(FuncName.c_str()) + 6];
|
|
|
|
sprintf(tmpName, "%s_tmp_%d",FuncName.c_str(),FI);
|
|
|
|
|
|
|
|
if (RC == PIC16::CPURegsRegisterClass)
|
|
|
|
BuildMI(MBB, I, this->get(PIC16::MOVF), DestReg)
|
|
|
|
.addExternalSymbol(tmpName) // the current printer expects 3 operands,
|
|
|
|
.addExternalSymbol(tmpName); // all we need is actually one,so we repeat.
|
|
|
|
else
|
|
|
|
assert(0 && "Can't load this register from stack slot");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// InsertBranch - Insert a branch into the end of the specified
|
|
|
|
/// MachineBasicBlock. This operands to this method are the same as those
|
|
|
|
/// returned by AnalyzeBranch. This is invoked in cases where AnalyzeBranch
|
|
|
|
/// returns success and when an unconditional branch (TBB is non-null, FBB is
|
|
|
|
/// null, Cond is empty) needs to be inserted. It returns the number of
|
|
|
|
/// instructions inserted.
|
|
|
|
unsigned PIC16InstrInfo::
|
|
|
|
InsertBranch(MachineBasicBlock &MBB,
|
2008-05-14 19:31:39 +08:00
|
|
|
MachineBasicBlock *TBB, MachineBasicBlock *FBB,
|
2008-08-15 06:49:33 +08:00
|
|
|
const SmallVectorImpl<MachineOperand> &Cond) const
|
2008-05-13 17:02:57 +08:00
|
|
|
{
|
|
|
|
// Shouldn't be a fall through.
|
|
|
|
assert(TBB && "InsertBranch must not be told to insert a fallthrough");
|
|
|
|
|
|
|
|
if (FBB == 0) { // One way branch.
|
|
|
|
if (Cond.empty()) {
|
|
|
|
// Unconditional branch?
|
|
|
|
BuildMI(&MBB, get(PIC16::GOTO)).addMBB(TBB);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-05-14 19:31:39 +08:00
|
|
|
// FIXME: If the there are some conditions specified then conditional branch
|
2008-05-13 17:02:57 +08:00
|
|
|
// should be generated.
|
|
|
|
// For the time being no instruction is being generated therefore
|
|
|
|
// returning NULL.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|