blob: 8bb8b2616c8f3775a8cc4c29bf2118d2f351ab98 [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 Grosbach08bd5492010-10-12 23:00:24 +000052 /// getCCOutOpValue - Return encoding of the 's' bit.
53 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
54 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
55 // '1' respectively.
56 return MI.getOperand(Op).getReg() == ARM::CPSR;
57 }
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000058 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
59 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
60 unsigned SoImm = MI.getOperand(Op).getImm();
61 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
62 assert(SoImmVal != -1 && "Not a valid so_imm value!");
63
64 // Encode rotate_imm.
65 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
66 << ARMII::SoRotImmShift;
67
68 // Encode immed_8.
69 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
70 return Binary;
71 }
Jim Grosbach08bd5492010-10-12 23:00:24 +000072
Jim Grosbach568eeed2010-09-17 18:46:17 +000073 unsigned getNumFixupKinds() const {
74 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +000075 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +000076 }
77
78 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
79 static MCFixupKindInfo rtn;
80 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
81 return rtn;
82 }
83
Jim Grosbach568eeed2010-09-17 18:46:17 +000084 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
85 OS << (char)C;
86 ++CurByte;
87 }
88
89 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
90 raw_ostream &OS) const {
91 // Output the constant in little endian byte order.
92 for (unsigned i = 0; i != Size; ++i) {
93 EmitByte(Val & 255, CurByte, OS);
94 Val >>= 8;
95 }
96 }
97
98 void EmitImmediate(const MCOperand &Disp,
99 unsigned ImmSize, MCFixupKind FixupKind,
100 unsigned &CurByte, raw_ostream &OS,
101 SmallVectorImpl<MCFixup> &Fixups,
102 int ImmOffset = 0) const;
103
104 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
105 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000106};
107
108} // end anonymous namespace
109
Jim Grosbach568eeed2010-09-17 18:46:17 +0000110MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
111 TargetMachine &TM,
112 MCContext &Ctx) {
113 return new ARMMCCodeEmitter(TM, Ctx);
114}
115
116void ARMMCCodeEmitter::
117EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
118 unsigned &CurByte, raw_ostream &OS,
119 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
120 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
121}
122
Jim Grosbach56ac9072010-10-08 21:45:55 +0000123/// getMachineOpValue - Return binary encoding of operand. If the machine
124/// operand requires relocation, record the relocation and return zero.
125unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
126 const MCOperand &MO) const {
127 if (MO.isReg())
Jim Grosbach56ac9072010-10-08 21:45:55 +0000128 return getARMRegisterNumbering(MO.getReg());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000129 else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000130 return static_cast<unsigned>(MO.getImm());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000131 } else {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000132#ifndef NDEBUG
133 errs() << MO;
134#endif
135 llvm_unreachable(0);
136 }
137 return 0;
138}
139
Jim Grosbach568eeed2010-09-17 18:46:17 +0000140void ARMMCCodeEmitter::
141EncodeInstruction(const MCInst &MI, raw_ostream &OS,
142 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000143 unsigned Opcode = MI.getOpcode();
144 const TargetInstrDesc &Desc = TII.get(Opcode);
145 uint64_t TSFlags = Desc.TSFlags;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000146 // Keep track of the current byte being emitted.
147 unsigned CurByte = 0;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000148
149 // Pseudo instructions don't get encoded.
150 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
151 return;
152
153 ++MCNumEmitted; // Keep track of the # of mi's emitted
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000154 // FIXME: TableGen doesn't deal well with operands that expand to multiple
155 // machine instruction operands, so for now we'll fix those up here.
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000156 // Similarly, operands that are encoded as other than their literal
157 // values in the MI.
158 unsigned Value = getBinaryCodeForInstr(MI);
Jim Grosbach3e094132010-10-08 17:45:54 +0000159 switch (Opcode) {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000160 default: break;
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000161 case ARM::ADDrs:
162 case ARM::ANDrs:
163 case ARM::BICrs:
164 case ARM::EORrs:
165 case ARM::ORRrs:
166 case ARM::SUBrs: {
167 // The so_reg operand needs the shift ammount encoded.
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000168 unsigned ShVal = MI.getOperand(4).getImm();
169 unsigned ShType = ARM_AM::getShiftOpcEncoding(ARM_AM::getSORegShOp(ShVal));
170 unsigned ShAmt = ARM_AM::getSORegOffset(ShVal);
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000171 Value |= ShType << ARMII::ShiftTypeShift;
172 Value |= ShAmt << ARMII::ShiftShift;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000173 break;
174 }
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000175 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000176 EmitConstant(Value, 4, CurByte, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000177}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000178
179// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
180// be able to generate code emitter helpers for either variant, like it
181// does for the AsmWriter.
182#define ARMCodeEmitter ARMMCCodeEmitter
183#define MachineInstr MCInst
184#include "ARMGenCodeEmitter.inc"
185#undef ARMCodeEmitter
186#undef MachineInstr