blob: 10dee202c49233682d0d1e6475b15e1ad367c532 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===- R600MCCodeEmitter.cpp - Code Emitter for R600->Cayman GPU families -===//
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/// \file
11///
Tom Stellardcfe2ef82013-05-06 17:50:44 +000012/// \brief The R600 code emitter produces machine code that can be executed
13/// directly on the GPU device.
Tom Stellard75aadc22012-12-11 21:25:42 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "R600Defines.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000018#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000019#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/Support/raw_ostream.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000027#include <stdio.h>
28
29#define SRC_BYTE_COUNT 11
30#define DST_BYTE_COUNT 5
31
32using namespace llvm;
33
34namespace {
35
36class R600MCCodeEmitter : public AMDGPUMCCodeEmitter {
David Blaikie772d4f72013-02-18 23:11:17 +000037 R600MCCodeEmitter(const R600MCCodeEmitter &) LLVM_DELETED_FUNCTION;
38 void operator=(const R600MCCodeEmitter &) LLVM_DELETED_FUNCTION;
Tom Stellard75aadc22012-12-11 21:25:42 +000039 const MCInstrInfo &MCII;
40 const MCRegisterInfo &MRI;
41 const MCSubtargetInfo &STI;
42 MCContext &Ctx;
43
44public:
45
46 R600MCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
47 const MCSubtargetInfo &sti, MCContext &ctx)
48 : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
49
50 /// \brief Encode the instruction and write it to the OS.
51 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
52 SmallVectorImpl<MCFixup> &Fixups) const;
53
54 /// \returns the encoding for an MCOperand.
55 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
56 SmallVectorImpl<MCFixup> &Fixups) const;
57private:
58
59 void EmitALUInstr(const MCInst &MI, SmallVectorImpl<MCFixup> &Fixups,
60 raw_ostream &OS) const;
61 void EmitSrc(const MCInst &MI, unsigned OpIdx, raw_ostream &OS) const;
Tom Stellard365366f2013-01-23 02:09:06 +000062 void EmitSrcISA(const MCInst &MI, unsigned RegOpIdx, unsigned SelOpIdx,
63 raw_ostream &OS) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000064 void EmitDst(const MCInst &MI, raw_ostream &OS) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000065 void EmitFCInstr(const MCInst &MI, raw_ostream &OS) const;
66
67 void EmitNullBytes(unsigned int byteCount, raw_ostream &OS) const;
68
69 void EmitByte(unsigned int byte, raw_ostream &OS) const;
70
71 void EmitTwoBytes(uint32_t bytes, raw_ostream &OS) const;
72
73 void Emit(uint32_t value, raw_ostream &OS) const;
74 void Emit(uint64_t value, raw_ostream &OS) const;
75
76 unsigned getHWRegChan(unsigned reg) const;
77 unsigned getHWReg(unsigned regNo) const;
78
79 bool isFCOp(unsigned opcode) const;
80 bool isTexOp(unsigned opcode) const;
81 bool isFlagSet(const MCInst &MI, unsigned Operand, unsigned Flag) const;
82
83};
84
85} // End anonymous namespace
86
87enum RegElement {
88 ELEMENT_X = 0,
89 ELEMENT_Y,
90 ELEMENT_Z,
91 ELEMENT_W
92};
93
Tom Stellard75aadc22012-12-11 21:25:42 +000094enum FCInstr {
95 FC_IF_PREDICATE = 0,
96 FC_ELSE,
97 FC_ENDIF,
98 FC_BGNLOOP,
99 FC_ENDLOOP,
100 FC_BREAK_PREDICATE,
101 FC_CONTINUE
102};
103
104enum TextureTypes {
105 TEXTURE_1D = 1,
106 TEXTURE_2D,
107 TEXTURE_3D,
108 TEXTURE_CUBE,
109 TEXTURE_RECT,
110 TEXTURE_SHADOW1D,
111 TEXTURE_SHADOW2D,
112 TEXTURE_SHADOWRECT,
113 TEXTURE_1D_ARRAY,
114 TEXTURE_2D_ARRAY,
115 TEXTURE_SHADOW1D_ARRAY,
116 TEXTURE_SHADOW2D_ARRAY
117};
118
119MCCodeEmitter *llvm::createR600MCCodeEmitter(const MCInstrInfo &MCII,
120 const MCRegisterInfo &MRI,
121 const MCSubtargetInfo &STI,
122 MCContext &Ctx) {
123 return new R600MCCodeEmitter(MCII, MRI, STI, Ctx);
124}
125
126void R600MCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
127 SmallVectorImpl<MCFixup> &Fixups) const {
Vincent Lejeune53f35252013-03-31 19:33:04 +0000128 if (isFCOp(MI.getOpcode())){
Tom Stellard75aadc22012-12-11 21:25:42 +0000129 EmitFCInstr(MI, OS);
130 } else if (MI.getOpcode() == AMDGPU::RETURN ||
Vincent Lejeune3f1d1362013-04-30 00:13:53 +0000131 MI.getOpcode() == AMDGPU::FETCH_CLAUSE ||
Vincent Lejeune3abdbf12013-04-30 00:14:38 +0000132 MI.getOpcode() == AMDGPU::ALU_CLAUSE ||
Tom Stellard75aadc22012-12-11 21:25:42 +0000133 MI.getOpcode() == AMDGPU::BUNDLE ||
134 MI.getOpcode() == AMDGPU::KILL) {
135 return;
136 } else {
137 switch(MI.getOpcode()) {
138 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
139 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
140 uint64_t inst = getBinaryCodeForInstr(MI, Fixups);
Tom Stellard75aadc22012-12-11 21:25:42 +0000141 Emit(inst, OS);
142 break;
143 }
144 case AMDGPU::CONSTANT_LOAD_eg:
145 case AMDGPU::VTX_READ_PARAM_8_eg:
146 case AMDGPU::VTX_READ_PARAM_16_eg:
147 case AMDGPU::VTX_READ_PARAM_32_eg:
Tom Stellard91da4e92013-02-13 22:05:20 +0000148 case AMDGPU::VTX_READ_PARAM_128_eg:
Tom Stellard75aadc22012-12-11 21:25:42 +0000149 case AMDGPU::VTX_READ_GLOBAL_8_eg:
150 case AMDGPU::VTX_READ_GLOBAL_32_eg:
Tom Stellard365366f2013-01-23 02:09:06 +0000151 case AMDGPU::VTX_READ_GLOBAL_128_eg:
Vincent Lejeune68501802013-02-18 14:11:19 +0000152 case AMDGPU::TEX_VTX_CONSTBUF:
153 case AMDGPU::TEX_VTX_TEXBUF : {
Tom Stellard75aadc22012-12-11 21:25:42 +0000154 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
155 uint32_t InstWord2 = MI.getOperand(2).getImm(); // Offset
Vincent Lejeune3f1d1362013-04-30 00:13:53 +0000156 InstWord2 |= 1 << 19;
Tom Stellard75aadc22012-12-11 21:25:42 +0000157
Tom Stellard75aadc22012-12-11 21:25:42 +0000158 Emit(InstWord01, OS);
159 Emit(InstWord2, OS);
Vincent Lejeune3f1d1362013-04-30 00:13:53 +0000160 Emit((u_int32_t) 0, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000161 break;
162 }
Vincent Lejeune53f35252013-03-31 19:33:04 +0000163 case AMDGPU::TEX_LD:
164 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
165 case AMDGPU::TEX_SAMPLE:
166 case AMDGPU::TEX_SAMPLE_C:
167 case AMDGPU::TEX_SAMPLE_L:
168 case AMDGPU::TEX_SAMPLE_C_L:
169 case AMDGPU::TEX_SAMPLE_LB:
170 case AMDGPU::TEX_SAMPLE_C_LB:
171 case AMDGPU::TEX_SAMPLE_G:
172 case AMDGPU::TEX_SAMPLE_C_G:
173 case AMDGPU::TEX_GET_GRADIENTS_H:
174 case AMDGPU::TEX_GET_GRADIENTS_V:
175 case AMDGPU::TEX_SET_GRADIENTS_H:
176 case AMDGPU::TEX_SET_GRADIENTS_V: {
177 unsigned Opcode = MI.getOpcode();
178 bool HasOffsets = (Opcode == AMDGPU::TEX_LD);
179 unsigned OpOffset = HasOffsets ? 3 : 0;
180 int64_t Sampler = MI.getOperand(OpOffset + 3).getImm();
181 int64_t TextureType = MI.getOperand(OpOffset + 4).getImm();
182
183 uint32_t SrcSelect[4] = {0, 1, 2, 3};
184 uint32_t Offsets[3] = {0, 0, 0};
185 uint64_t CoordType[4] = {1, 1, 1, 1};
186
187 if (HasOffsets)
Vincent Lejeunebcbb13d2013-04-04 14:00:09 +0000188 for (unsigned i = 0; i < 3; i++) {
189 int SignedOffset = MI.getOperand(i + 2).getImm();
190 Offsets[i] = (SignedOffset & 0x1F);
191 }
192
Vincent Lejeune53f35252013-03-31 19:33:04 +0000193
194 if (TextureType == TEXTURE_RECT ||
195 TextureType == TEXTURE_SHADOWRECT) {
196 CoordType[ELEMENT_X] = 0;
197 CoordType[ELEMENT_Y] = 0;
198 }
199
200 if (TextureType == TEXTURE_1D_ARRAY ||
201 TextureType == TEXTURE_SHADOW1D_ARRAY) {
202 if (Opcode == AMDGPU::TEX_SAMPLE_C_L ||
203 Opcode == AMDGPU::TEX_SAMPLE_C_LB) {
204 CoordType[ELEMENT_Y] = 0;
205 } else {
206 CoordType[ELEMENT_Z] = 0;
207 SrcSelect[ELEMENT_Z] = ELEMENT_Y;
208 }
209 } else if (TextureType == TEXTURE_2D_ARRAY ||
210 TextureType == TEXTURE_SHADOW2D_ARRAY) {
211 CoordType[ELEMENT_Z] = 0;
212 }
213
214
215 if ((TextureType == TEXTURE_SHADOW1D ||
216 TextureType == TEXTURE_SHADOW2D ||
217 TextureType == TEXTURE_SHADOWRECT ||
218 TextureType == TEXTURE_SHADOW1D_ARRAY) &&
219 Opcode != AMDGPU::TEX_SAMPLE_C_L &&
220 Opcode != AMDGPU::TEX_SAMPLE_C_LB) {
221 SrcSelect[ELEMENT_W] = ELEMENT_Z;
222 }
223
224 uint64_t Word01 = getBinaryCodeForInstr(MI, Fixups) |
225 CoordType[ELEMENT_X] << 60 | CoordType[ELEMENT_Y] << 61 |
226 CoordType[ELEMENT_Z] << 62 | CoordType[ELEMENT_W] << 63;
227 uint32_t Word2 = Sampler << 15 | SrcSelect[ELEMENT_X] << 20 |
228 SrcSelect[ELEMENT_Y] << 23 | SrcSelect[ELEMENT_Z] << 26 |
229 SrcSelect[ELEMENT_W] << 29 | Offsets[0] << 0 | Offsets[1] << 5 |
230 Offsets[2] << 10;
231
Vincent Lejeune53f35252013-03-31 19:33:04 +0000232 Emit(Word01, OS);
233 Emit(Word2, OS);
Vincent Lejeune3f1d1362013-04-30 00:13:53 +0000234 Emit((u_int32_t) 0, OS);
Vincent Lejeune53f35252013-03-31 19:33:04 +0000235 break;
236 }
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000237 case AMDGPU::CF_ALU:
238 case AMDGPU::CF_ALU_PUSH_BEFORE: {
239 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
Vincent Lejeunef43bc572013-04-01 21:47:42 +0000240 Emit(Inst, OS);
241 break;
242 }
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000243 case AMDGPU::CF_CALL_FS_EG:
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000244 case AMDGPU::CF_CALL_FS_R600:
Vincent Lejeune3f1d1362013-04-30 00:13:53 +0000245 case AMDGPU::CF_TC_EG:
246 case AMDGPU::CF_VC_EG:
247 case AMDGPU::CF_TC_R600:
248 case AMDGPU::CF_VC_R600:
Vincent Lejeune5f11dd32013-04-08 13:05:49 +0000249 case AMDGPU::WHILE_LOOP_EG:
250 case AMDGPU::END_LOOP_EG:
251 case AMDGPU::LOOP_BREAK_EG:
252 case AMDGPU::CF_CONTINUE_EG:
253 case AMDGPU::CF_JUMP_EG:
254 case AMDGPU::CF_ELSE_EG:
255 case AMDGPU::POP_EG:
256 case AMDGPU::WHILE_LOOP_R600:
257 case AMDGPU::END_LOOP_R600:
258 case AMDGPU::LOOP_BREAK_R600:
259 case AMDGPU::CF_CONTINUE_R600:
260 case AMDGPU::CF_JUMP_R600:
261 case AMDGPU::CF_ELSE_R600:
Vincent Lejeune218093e2013-04-17 15:17:32 +0000262 case AMDGPU::POP_R600:
263 case AMDGPU::EG_ExportSwz:
264 case AMDGPU::R600_ExportSwz:
265 case AMDGPU::EG_ExportBuf:
Vincent Lejeuneb6bfe852013-04-23 17:34:00 +0000266 case AMDGPU::R600_ExportBuf:
267 case AMDGPU::PAD:
268 case AMDGPU::CF_END_R600:
269 case AMDGPU::CF_END_EG:
270 case AMDGPU::CF_END_CM: {
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000271 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
Vincent Lejeunebfaa63a62013-04-01 21:48:05 +0000272 Emit(Inst, OS);
273 break;
274 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000275 default:
Vincent Lejeune3abdbf12013-04-30 00:14:38 +0000276 uint64_t Inst = getBinaryCodeForInstr(MI, Fixups);
Vincent Lejeune3abdbf12013-04-30 00:14:38 +0000277 Emit(Inst, OS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000278 break;
279 }
280 }
281}
282
283void R600MCCodeEmitter::EmitALUInstr(const MCInst &MI,
284 SmallVectorImpl<MCFixup> &Fixups,
285 raw_ostream &OS) const {
286 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
Tom Stellard75aadc22012-12-11 21:25:42 +0000287
Tom Stellard75aadc22012-12-11 21:25:42 +0000288 uint64_t InstWord01 = getBinaryCodeForInstr(MI, Fixups);
289
290 //older alu have different encoding for instructions with one or two src
291 //parameters.
292 if ((STI.getFeatureBits() & AMDGPU::FeatureR600ALUInst) &&
293 !(MCDesc.TSFlags & R600_InstFlag::OP3)) {
294 uint64_t ISAOpCode = InstWord01 & (0x3FFULL << 39);
295 InstWord01 &= ~(0x3FFULL << 39);
296 InstWord01 |= ISAOpCode << 1;
297 }
298
Tom Stellard365366f2013-01-23 02:09:06 +0000299 unsigned SrcNum = MCDesc.TSFlags & R600_InstFlag::OP3 ? 3 :
300 MCDesc.TSFlags & R600_InstFlag::OP2 ? 2 : 1;
Tom Stellard75aadc22012-12-11 21:25:42 +0000301
Tom Stellard365366f2013-01-23 02:09:06 +0000302 const unsigned SrcOps[3][2] = {
303 {R600Operands::SRC0, R600Operands::SRC0_SEL},
304 {R600Operands::SRC1, R600Operands::SRC1_SEL},
305 {R600Operands::SRC2, R600Operands::SRC2_SEL}
306 };
307
308 for (unsigned SrcIdx = 0; SrcIdx < SrcNum; ++SrcIdx) {
309 unsigned RegOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][0]];
310 unsigned SelOpIdx = R600Operands::ALUOpTable[SrcNum-1][SrcOps[SrcIdx][1]];
Tom Stellard75aadc22012-12-11 21:25:42 +0000311 }
312
313 Emit(InstWord01, OS);
314 return;
315}
316
317void R600MCCodeEmitter::EmitSrc(const MCInst &MI, unsigned OpIdx,
318 raw_ostream &OS) const {
319 const MCOperand &MO = MI.getOperand(OpIdx);
320 union {
321 float f;
322 uint32_t i;
323 } Value;
324 Value.i = 0;
325 // Emit the source select (2 bytes). For GPRs, this is the register index.
326 // For other potential instruction operands, (e.g. constant registers) the
327 // value of the source select is defined in the r600isa docs.
328 if (MO.isReg()) {
329 unsigned reg = MO.getReg();
330 EmitTwoBytes(getHWReg(reg), OS);
331 if (reg == AMDGPU::ALU_LITERAL_X) {
332 unsigned ImmOpIndex = MI.getNumOperands() - 1;
333 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
334 if (ImmOp.isFPImm()) {
335 Value.f = ImmOp.getFPImm();
336 } else {
337 assert(ImmOp.isImm());
338 Value.i = ImmOp.getImm();
339 }
340 }
341 } else {
342 // XXX: Handle other operand types.
343 EmitTwoBytes(0, OS);
344 }
345
346 // Emit the source channel (1 byte)
347 if (MO.isReg()) {
348 EmitByte(getHWRegChan(MO.getReg()), OS);
349 } else {
350 EmitByte(0, OS);
351 }
352
353 // XXX: Emit isNegated (1 byte)
354 if ((!(isFlagSet(MI, OpIdx, MO_FLAG_ABS)))
355 && (isFlagSet(MI, OpIdx, MO_FLAG_NEG) ||
356 (MO.isReg() &&
357 (MO.getReg() == AMDGPU::NEG_ONE || MO.getReg() == AMDGPU::NEG_HALF)))){
358 EmitByte(1, OS);
359 } else {
360 EmitByte(0, OS);
361 }
362
363 // Emit isAbsolute (1 byte)
364 if (isFlagSet(MI, OpIdx, MO_FLAG_ABS)) {
365 EmitByte(1, OS);
366 } else {
367 EmitByte(0, OS);
368 }
369
370 // XXX: Emit relative addressing mode (1 byte)
371 EmitByte(0, OS);
372
373 // Emit kc_bank, This will be adjusted later by r600_asm
374 EmitByte(0, OS);
375
376 // Emit the literal value, if applicable (4 bytes).
377 Emit(Value.i, OS);
378
379}
380
Tom Stellard365366f2013-01-23 02:09:06 +0000381void R600MCCodeEmitter::EmitSrcISA(const MCInst &MI, unsigned RegOpIdx,
382 unsigned SelOpIdx, raw_ostream &OS) const {
383 const MCOperand &RegMO = MI.getOperand(RegOpIdx);
384 const MCOperand &SelMO = MI.getOperand(SelOpIdx);
385
Tom Stellard75aadc22012-12-11 21:25:42 +0000386 union {
387 float f;
388 uint32_t i;
389 } InlineConstant;
390 InlineConstant.i = 0;
Tom Stellard365366f2013-01-23 02:09:06 +0000391 // Emit source type (1 byte) and source select (4 bytes). For GPRs type is 0
392 // and select is 0 (GPR index is encoded in the instr encoding. For constants
393 // type is 1 and select is the original const select passed from the driver.
394 unsigned Reg = RegMO.getReg();
395 if (Reg == AMDGPU::ALU_CONST) {
396 EmitByte(1, OS);
397 uint32_t Sel = SelMO.getImm();
398 Emit(Sel, OS);
399 } else {
400 EmitByte(0, OS);
401 Emit((uint32_t)0, OS);
402 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000403
Tom Stellard365366f2013-01-23 02:09:06 +0000404 if (Reg == AMDGPU::ALU_LITERAL_X) {
Vincent Lejeune22c42482013-04-30 00:14:08 +0000405 unsigned ImmOpIndex = MI.getNumOperands() - 2;
Tom Stellard365366f2013-01-23 02:09:06 +0000406 MCOperand ImmOp = MI.getOperand(ImmOpIndex);
407 if (ImmOp.isFPImm()) {
408 InlineConstant.f = ImmOp.getFPImm();
409 } else {
410 assert(ImmOp.isImm());
411 InlineConstant.i = ImmOp.getImm();
Tom Stellard75aadc22012-12-11 21:25:42 +0000412 }
413 }
414
415 // Emit the literal value, if applicable (4 bytes).
416 Emit(InlineConstant.i, OS);
417}
418
Tom Stellard75aadc22012-12-11 21:25:42 +0000419void R600MCCodeEmitter::EmitFCInstr(const MCInst &MI, raw_ostream &OS) const {
420
Tom Stellard75aadc22012-12-11 21:25:42 +0000421 // Emit SRC
422 unsigned NumOperands = MI.getNumOperands();
423 if (NumOperands > 0) {
424 assert(NumOperands == 1);
425 EmitSrc(MI, 0, OS);
426 } else {
427 EmitNullBytes(SRC_BYTE_COUNT, OS);
428 }
429
430 // Emit FC Instruction
431 enum FCInstr instr;
432 switch (MI.getOpcode()) {
433 case AMDGPU::PREDICATED_BREAK:
434 instr = FC_BREAK_PREDICATE;
435 break;
436 case AMDGPU::CONTINUE:
437 instr = FC_CONTINUE;
438 break;
439 case AMDGPU::IF_PREDICATE_SET:
440 instr = FC_IF_PREDICATE;
441 break;
442 case AMDGPU::ELSE:
443 instr = FC_ELSE;
444 break;
445 case AMDGPU::ENDIF:
446 instr = FC_ENDIF;
447 break;
448 case AMDGPU::ENDLOOP:
449 instr = FC_ENDLOOP;
450 break;
451 case AMDGPU::WHILELOOP:
452 instr = FC_BGNLOOP;
453 break;
454 default:
455 abort();
456 break;
457 }
458 EmitByte(instr, OS);
459}
460
461void R600MCCodeEmitter::EmitNullBytes(unsigned int ByteCount,
462 raw_ostream &OS) const {
463
464 for (unsigned int i = 0; i < ByteCount; i++) {
465 EmitByte(0, OS);
466 }
467}
468
469void R600MCCodeEmitter::EmitByte(unsigned int Byte, raw_ostream &OS) const {
470 OS.write((uint8_t) Byte & 0xff);
471}
472
473void R600MCCodeEmitter::EmitTwoBytes(unsigned int Bytes,
474 raw_ostream &OS) const {
475 OS.write((uint8_t) (Bytes & 0xff));
476 OS.write((uint8_t) ((Bytes >> 8) & 0xff));
477}
478
479void R600MCCodeEmitter::Emit(uint32_t Value, raw_ostream &OS) const {
480 for (unsigned i = 0; i < 4; i++) {
481 OS.write((uint8_t) ((Value >> (8 * i)) & 0xff));
482 }
483}
484
485void R600MCCodeEmitter::Emit(uint64_t Value, raw_ostream &OS) const {
486 for (unsigned i = 0; i < 8; i++) {
487 EmitByte((Value >> (8 * i)) & 0xff, OS);
488 }
489}
490
491unsigned R600MCCodeEmitter::getHWRegChan(unsigned reg) const {
492 return MRI.getEncodingValue(reg) >> HW_CHAN_SHIFT;
493}
494
495unsigned R600MCCodeEmitter::getHWReg(unsigned RegNo) const {
496 return MRI.getEncodingValue(RegNo) & HW_REG_MASK;
497}
498
499uint64_t R600MCCodeEmitter::getMachineOpValue(const MCInst &MI,
500 const MCOperand &MO,
501 SmallVectorImpl<MCFixup> &Fixup) const {
502 if (MO.isReg()) {
503 if (HAS_NATIVE_OPERANDS(MCII.get(MI.getOpcode()).TSFlags)) {
504 return MRI.getEncodingValue(MO.getReg());
505 } else {
506 return getHWReg(MO.getReg());
507 }
508 } else if (MO.isImm()) {
509 return MO.getImm();
510 } else {
511 assert(0);
512 return 0;
513 }
514}
515
516//===----------------------------------------------------------------------===//
517// Encoding helper functions
518//===----------------------------------------------------------------------===//
519
520bool R600MCCodeEmitter::isFCOp(unsigned opcode) const {
521 switch(opcode) {
522 default: return false;
523 case AMDGPU::PREDICATED_BREAK:
524 case AMDGPU::CONTINUE:
525 case AMDGPU::IF_PREDICATE_SET:
526 case AMDGPU::ELSE:
527 case AMDGPU::ENDIF:
528 case AMDGPU::ENDLOOP:
529 case AMDGPU::WHILELOOP:
530 return true;
531 }
532}
533
534bool R600MCCodeEmitter::isTexOp(unsigned opcode) const {
535 switch(opcode) {
536 default: return false;
537 case AMDGPU::TEX_LD:
538 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
539 case AMDGPU::TEX_SAMPLE:
540 case AMDGPU::TEX_SAMPLE_C:
541 case AMDGPU::TEX_SAMPLE_L:
542 case AMDGPU::TEX_SAMPLE_C_L:
543 case AMDGPU::TEX_SAMPLE_LB:
544 case AMDGPU::TEX_SAMPLE_C_LB:
545 case AMDGPU::TEX_SAMPLE_G:
546 case AMDGPU::TEX_SAMPLE_C_G:
547 case AMDGPU::TEX_GET_GRADIENTS_H:
548 case AMDGPU::TEX_GET_GRADIENTS_V:
549 case AMDGPU::TEX_SET_GRADIENTS_H:
550 case AMDGPU::TEX_SET_GRADIENTS_V:
551 return true;
552 }
553}
554
555bool R600MCCodeEmitter::isFlagSet(const MCInst &MI, unsigned Operand,
556 unsigned Flag) const {
557 const MCInstrDesc &MCDesc = MCII.get(MI.getOpcode());
558 unsigned FlagIndex = GET_FLAG_OPERAND_IDX(MCDesc.TSFlags);
559 if (FlagIndex == 0) {
560 return false;
561 }
562 assert(MI.getOperand(FlagIndex).isImm());
563 return !!((MI.getOperand(FlagIndex).getImm() >>
564 (NUM_MO_FLAGS * Operand)) & Flag);
565}
566
567#include "AMDGPUGenMCCodeEmitter.inc"