blob: 122193170918e00594342d425d26dda0c4adda0e [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 }
58
Jim Grosbach568eeed2010-09-17 18:46:17 +000059 unsigned getNumFixupKinds() const {
60 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +000061 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +000062 }
63
64 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
65 static MCFixupKindInfo rtn;
66 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
67 return rtn;
68 }
69
Jim Grosbach568eeed2010-09-17 18:46:17 +000070 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
71 OS << (char)C;
72 ++CurByte;
73 }
74
75 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
76 raw_ostream &OS) const {
77 // Output the constant in little endian byte order.
78 for (unsigned i = 0; i != Size; ++i) {
79 EmitByte(Val & 255, CurByte, OS);
80 Val >>= 8;
81 }
82 }
83
84 void EmitImmediate(const MCOperand &Disp,
85 unsigned ImmSize, MCFixupKind FixupKind,
86 unsigned &CurByte, raw_ostream &OS,
87 SmallVectorImpl<MCFixup> &Fixups,
88 int ImmOffset = 0) const;
89
90 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
91 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +000092};
93
94} // end anonymous namespace
95
Jim Grosbach0de6ab32010-10-12 17:11:26 +000096unsigned ARMMCCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) const {
97 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
98 assert(SoImmVal != -1 && "Not a valid so_imm value!");
99
100 // Encode rotate_imm.
101 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
102 << ARMII::SoRotImmShift;
103
104 // Encode immed_8.
105 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
106 return Binary;
107}
Jim Grosbach568eeed2010-09-17 18:46:17 +0000108
109MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
110 TargetMachine &TM,
111 MCContext &Ctx) {
112 return new ARMMCCodeEmitter(TM, Ctx);
113}
114
115void ARMMCCodeEmitter::
116EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
117 unsigned &CurByte, raw_ostream &OS,
118 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
119 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
120}
121
Jim Grosbach56ac9072010-10-08 21:45:55 +0000122/// getMachineOpValue - Return binary encoding of operand. If the machine
123/// operand requires relocation, record the relocation and return zero.
124unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
125 const MCOperand &MO) const {
126 if (MO.isReg())
Jim Grosbach56ac9072010-10-08 21:45:55 +0000127 return getARMRegisterNumbering(MO.getReg());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000128 else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000129 return static_cast<unsigned>(MO.getImm());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000130 } else {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000131#ifndef NDEBUG
132 errs() << MO;
133#endif
134 llvm_unreachable(0);
135 }
136 return 0;
137}
138
Jim Grosbach568eeed2010-09-17 18:46:17 +0000139void ARMMCCodeEmitter::
140EncodeInstruction(const MCInst &MI, raw_ostream &OS,
141 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000142 unsigned Opcode = MI.getOpcode();
143 const TargetInstrDesc &Desc = TII.get(Opcode);
144 uint64_t TSFlags = Desc.TSFlags;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000145 // Keep track of the current byte being emitted.
146 unsigned CurByte = 0;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000147
148 // Pseudo instructions don't get encoded.
149 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
150 return;
151
152 ++MCNumEmitted; // Keep track of the # of mi's emitted
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000153 // FIXME: TableGen doesn't deal well with operands that expand to multiple
154 // machine instruction operands, so for now we'll fix those up here.
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000155 // Similarly, operands that are encoded as other than their literal
156 // values in the MI.
157 unsigned Value = getBinaryCodeForInstr(MI);
Jim Grosbach3e094132010-10-08 17:45:54 +0000158 switch (Opcode) {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000159 default: break;
Jim Grosbachf59818b2010-10-12 18:09:12 +0000160 case ARM::MOVi:
Jim Grosbachf59818b2010-10-12 18:09:12 +0000161 // The shifted immediate value.
162 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(1).getImm());
163 break;
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000164 case ARM::ADDri:
165 case ARM::ANDri:
166 case ARM::BICri:
167 case ARM::EORri:
168 case ARM::ORRri:
169 case ARM::SUBri:
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000170 // The shifted immediate value.
171 Value |= getMachineSoImmOpValue((unsigned)MI.getOperand(2).getImm());
172 break;
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000173 case ARM::ADDrs:
174 case ARM::ANDrs:
175 case ARM::BICrs:
176 case ARM::EORrs:
177 case ARM::ORRrs:
178 case ARM::SUBrs: {
179 // The so_reg operand needs the shift ammount encoded.
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000180 unsigned ShVal = MI.getOperand(4).getImm();
181 unsigned ShType = ARM_AM::getShiftOpcEncoding(ARM_AM::getSORegShOp(ShVal));
182 unsigned ShAmt = ARM_AM::getSORegOffset(ShVal);
Jim Grosbach42fac8e2010-10-11 23:16:21 +0000183 Value |= ShType << ARMII::ShiftTypeShift;
184 Value |= ShAmt << ARMII::ShiftShift;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000185 break;
186 }
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000187 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000188 EmitConstant(Value, 4, CurByte, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000189}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000190
191// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
192// be able to generate code emitter helpers for either variant, like it
193// does for the AsmWriter.
194#define ARMCodeEmitter ARMMCCodeEmitter
195#define MachineInstr MCInst
196#include "ARMGenCodeEmitter.inc"
197#undef ARMCodeEmitter
198#undef MachineInstr