blob: b2584f9ad85960e1f7bb8593f17ba3e72b1567bb [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "arm-emitter"
15#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000017#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000018#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000021#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000022#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
Jim Grosbachd6d4b422010-10-07 22:12:50 +000025STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
Jim Grosbach568eeed2010-09-17 18:46:17 +000027namespace {
28class ARMMCCodeEmitter : public MCCodeEmitter {
29 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
30 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
31 const TargetMachine &TM;
32 const TargetInstrInfo &TII;
33 MCContext &Ctx;
34
35public:
36 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000038 }
39
40 ~ARMMCCodeEmitter() {}
41
Jim Grosbach0de6ab32010-10-12 17:11:26 +000042 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
43
Jim Grosbach9af82ba2010-10-07 21:57:55 +000044 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
Jim Grosbachbade37b2010-10-08 00:21:28 +000046 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000047
48 /// getMachineOpValue - Return binary encoding of operand. If the machine
49 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach3e094132010-10-08 17:45:54 +000050 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000051
Jim Grosbach568eeed2010-09-17 18:46:17 +000052 unsigned getNumFixupKinds() const {
53 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +000054 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +000055 }
56
57 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
58 static MCFixupKindInfo rtn;
59 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
60 return rtn;
61 }
62
Jim Grosbach568eeed2010-09-17 18:46:17 +000063 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
64 OS << (char)C;
65 ++CurByte;
66 }
67
68 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
69 raw_ostream &OS) const {
70 // Output the constant in little endian byte order.
71 for (unsigned i = 0; i != Size; ++i) {
72 EmitByte(Val & 255, CurByte, OS);
73 Val >>= 8;
74 }
75 }
76
77 void EmitImmediate(const MCOperand &Disp,
78 unsigned ImmSize, MCFixupKind FixupKind,
79 unsigned &CurByte, raw_ostream &OS,
80 SmallVectorImpl<MCFixup> &Fixups,
81 int ImmOffset = 0) const;
82
83 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
84 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +000085};
86
87} // end anonymous namespace
88
Jim Grosbach0de6ab32010-10-12 17:11:26 +000089unsigned ARMMCCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) const {
90 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
91 assert(SoImmVal != -1 && "Not a valid so_imm value!");
92
93 // Encode rotate_imm.
94 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
95 << ARMII::SoRotImmShift;
96
97 // Encode immed_8.
98 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
99 return Binary;
100}
Jim Grosbach568eeed2010-09-17 18:46:17 +0000101
102MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
103 TargetMachine &TM,
104 MCContext &Ctx) {
105 return new ARMMCCodeEmitter(TM, Ctx);
106}
107
108void ARMMCCodeEmitter::
109EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
110 unsigned &CurByte, raw_ostream &OS,
111 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
112 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
113}
114
Jim Grosbach56ac9072010-10-08 21:45:55 +0000115/// getMachineOpValue - Return binary encoding of operand. If the machine
116/// operand requires relocation, record the relocation and return zero.
117unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
118 const MCOperand &MO) const {
119 if (MO.isReg())
Jim Grosbach56ac9072010-10-08 21:45:55 +0000120 return getARMRegisterNumbering(MO.getReg());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000121 else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000122 return static_cast<unsigned>(MO.getImm());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000123 } else {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000124#ifndef NDEBUG
125 errs() << MO;
126#endif
127 llvm_unreachable(0);
128 }
129 return 0;
130}
131
Jim Grosbach568eeed2010-09-17 18:46:17 +0000132void ARMMCCodeEmitter::
133EncodeInstruction(const MCInst &MI, raw_ostream &OS,
134 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000135 unsigned Opcode = MI.getOpcode();
136 const TargetInstrDesc &Desc = TII.get(Opcode);
137 uint64_t TSFlags = Desc.TSFlags;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000138 // Keep track of the current byte being emitted.
139 unsigned CurByte = 0;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000140
141 // Pseudo instructions don't get encoded.
142 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
143 return;
144
145 ++MCNumEmitted; // Keep track of the # of mi's emitted
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000146 // FIXME: TableGen doesn't deal well with operands that expand to multiple
147 // machine instruction operands, so for now we'll fix those up here.
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000148 // Similarly, operands that are encoded as other than their literal
149 // values in the MI.
150 unsigned Value = getBinaryCodeForInstr(MI);
Jim Grosbach3e094132010-10-08 17:45:54 +0000151 switch (Opcode) {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000152 default: break;
Jim Grosbachf59818b2010-10-12 18:09:12 +0000153 case ARM::MOVi:
154 // The 's' bit.
155 if (MI.getOperand(4).getReg() == ARM::CPSR)
156 Value |= 1 << ARMII::S_BitShift;
157 // The shifted immediate value.
158 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(1).getImm());
159 break;
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000160 case ARM::ADDri:
161 case ARM::ANDri:
162 case ARM::BICri:
163 case ARM::EORri:
164 case ARM::ORRri:
165 case ARM::SUBri:
166 // The 's' bit.
167 if (MI.getOperand(5).getReg() == ARM::CPSR)
168 Value |= 1 << ARMII::S_BitShift;
169 // The shifted immediate value.
170 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(2).getImm());
171 break;
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000172 case ARM::ADDrs:
173 case ARM::ANDrs:
174 case ARM::BICrs:
175 case ARM::EORrs:
176 case ARM::ORRrs:
177 case ARM::SUBrs: {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000178 // The 's' bit.
179 if (MI.getOperand(7).getReg() == ARM::CPSR)
180 Value |= 1 << ARMII::S_BitShift;
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000181 // The so_reg operand needs the shift ammount encoded.
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000182 unsigned ShVal = MI.getOperand(4).getImm();
183 unsigned ShType = ARM_AM::getShiftOpcEncoding(ARM_AM::getSORegShOp(ShVal));
184 unsigned ShAmt = ARM_AM::getSORegOffset(ShVal);
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000185 Value |= ShType << ARMII::ShiftTypeShift;
186 Value |= ShAmt << ARMII::ShiftShift;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000187 break;
188 }
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000189 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000190 EmitConstant(Value, 4, CurByte, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000191}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000192
193// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
194// be able to generate code emitter helpers for either variant, like it
195// does for the AsmWriter.
196#define ARMCodeEmitter ARMMCCodeEmitter
197#define MachineInstr MCInst
198#include "ARMGenCodeEmitter.inc"
199#undef ARMCodeEmitter
200#undef MachineInstr