XED
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xed-encoder-hl.h
Go to the documentation of this file.
1 /*BEGIN_LEGAL
2 Intel Open Source License
3 
4 Copyright (c) 2002-2014 Intel Corporation. All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9 
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer. Redistributions
12 in binary form must reproduce the above copyright notice, this list of
13 conditions and the following disclaimer in the documentation and/or
14 other materials provided with the distribution. Neither the name of
15 the Intel Corporation nor the names of its contributors may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
23 ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 END_LEGAL */
31 
32 #ifndef _XED_ENCODER_HL_H_
33 # define _XED_ENCODER_HL_H_
34 #include "xed-types.h"
35 #include "xed-reg-enum.h"
36 #include "xed-state.h"
37 #include "xed-iclass-enum.h"
38 #include "xed-portability.h"
39 #include "xed-encode.h"
40 
41 
42 typedef struct {
43  xed_uint64_t displacement;
44  xed_uint32_t displacement_width;
45 } xed_enc_displacement_t; /* fixme bad name */
46 
48 
49 static XED_INLINE
52 xed_enc_displacement_t xed_disp(xed_uint64_t displacement,
53  xed_uint32_t displacement_width ) {
55  x.displacement = displacement;
56  x.displacement_width = displacement_width;
57  return x;
58 }
60 
61 typedef struct {
65  xed_uint32_t scale;
67 } xed_memop_t;
68 
69 
70 typedef enum {
79 
80  /* special for things with suppressed implicit memops */
82 
83  /* special for things with suppressed implicit memops */
85 
86  /* specific operand storage fields -- must supply a name */
89 
90 typedef struct {
92  union {
94  xed_int32_t brdisp;
95  xed_uint64_t imm0;
96  xed_uint8_t imm1;
97  struct {
99  xed_uint32_t value;
100  } s;
102  } u;
103  xed_uint32_t width;
105 
107 
108 static XED_INLINE xed_encoder_operand_t xed_relbr(xed_int32_t brdisp,
111  xed_uint_t width) {
114  o.u.brdisp = brdisp;
115  o.width = width;
116  return o;
117 }
119 
121 
122 static XED_INLINE xed_encoder_operand_t xed_ptr(xed_int32_t brdisp,
126  xed_uint_t width) {
129  o.u.brdisp = brdisp;
130  o.width = width;
131  return o;
132 }
134 
136 
142  o.u.reg = reg;
143  o.width = 0;
144  return o;
145 }
146 
150  xed_uint_t width) {
153  o.u.imm0 = v;
154  o.width = width;
155  return o;
156 }
160  xed_uint_t width) {
163  /* sign conversion: we store the int32 in an uint64. It gets sign
164  extended. Later we convert it to the right width for the
165  instruction. The maximum width of a signed immediate is currently
166  32b. */
167  o.u.imm0 = v;
168  o.width = width;
169  return o;
170 }
171 
177  o.u.imm1 = v;
178  o.width = 8;
179  return o;
180 }
181 
182 
186  xed_operand_enum_t operand_name,
187  xed_int32_t value) {
190  o.u.s.operand_name = operand_name;
191  o.u.s.value = value;
192  o.width = 0;
193  return o;
194 }
196 
197 
199 
201 
202 
208  o.u.reg = seg0;
209  return o;
210 }
211 
217  o.u.reg = seg1;
218  return o;
219 }
220 
224  xed_uint_t width) {
227  o.u.mem.base = base;
228  o.u.mem.seg = XED_REG_INVALID;
230  o.u.mem.scale = 0;
231  o.u.mem.disp.displacement = 0;
233  o.width = width;
234  return o;
235 }
236 
241  xed_uint_t width) {
244  o.u.mem.base = base;
245  o.u.mem.seg = XED_REG_INVALID;
247  o.u.mem.scale = 0;
248  o.u.mem.disp =disp;
249  o.width = width;
250  return o;
251 }
252 
256  xed_reg_enum_t index,
257  xed_uint_t scale,
259  xed_uint_t width) {
262  o.u.mem.base = base;
263  o.u.mem.seg = XED_REG_INVALID;
264  o.u.mem.index= index;
265  o.u.mem.scale = scale;
266  o.u.mem.disp = disp;
267  o.width = width;
268  return o;
269 }
270 
271 
275  xed_reg_enum_t base,
276  xed_uint_t width) {
279  o.u.mem.base = base;
280  o.u.mem.seg = seg;
282  o.u.mem.scale = 0;
283  o.u.mem.disp.displacement = 0;
285  o.width = width;
286  return o;
287 }
288 
292  xed_reg_enum_t base,
294  xed_uint_t width) {
297  o.u.mem.base = base;
298  o.u.mem.seg = seg;
300  o.u.mem.scale = 0;
301  o.u.mem.disp = disp;
302  o.width = width;
303  return o;
304 }
305 
310  xed_uint_t width) {
314  o.u.mem.seg = seg;
316  o.u.mem.scale = 0;
317  o.u.mem.disp = disp;
318  o.width = width;
319  return o;
320 }
321 
325  xed_reg_enum_t base,
326  xed_reg_enum_t index,
327  xed_uint_t scale,
329  xed_uint_t width) {
332  o.u.mem.base = base;
333  o.u.mem.seg = seg;
334  o.u.mem.index= index;
335  o.u.mem.scale = scale;
336  o.u.mem.disp = disp;
337  o.width = width;
338  return o;
339 }
341 
342 typedef union {
343  struct {
344  xed_uint32_t rep :1;
345  xed_uint32_t repne :1;
346  xed_uint32_t lock :1;
347  xed_uint32_t br_hint_taken :1;
348  xed_uint32_t br_hint_not_taken :1;
349  } s;
350  xed_uint32_t i;
352 
353 #define XED_ENCODER_OPERANDS_MAX 5 /* FIXME */
354 typedef struct {
356  xed_iclass_enum_t iclass; /*FIXME: use iform instead? or allow either */
358 
359  /* the effective_address_width is only requires to be set for
360  * instructions * with implicit suppressed memops or memops with no
361  * base or index regs. When base or index regs are present, XED pick
362  * this up automatically from the register names.
363 
364  * FIXME: make effective_address_width required by all encodes for
365  * unifority. Add to xed_inst[0123]() APIs??? */
366  xed_uint32_t effective_address_width;
367 
369  xed_uint32_t noperands;
372 
374 
381  xed_uint_t width) {
382  x->effective_address_width = width;
383 }
384 
385 
388  x->prefixes.s.rep=1;
389 }
390 
393  x->prefixes.s.repne=1;
394 }
395 
398  x->prefixes.s.lock=1;
399 }
400 
401 
402 
403 
410 
412 
414 /* FIXME: rather than return the xed_encoder_instruction_t I can make
415  * another version that returns a xed_encoder_request_t. Saves silly
416  * copying. Although the xed_encoder_instruction_t might be handy for
417  * having code templates that get customized & passed to encoder later. */
420 
421 
424 static XED_INLINE void xed_inst0(
426  xed_state_t mode,
427  xed_iclass_enum_t iclass,
428  xed_uint_t effective_operand_width) {
429 
430  inst->mode=mode;
431  inst->iclass = iclass;
432  inst->effective_operand_width = effective_operand_width;
433  inst->effective_address_width = 0;
434  inst->prefixes.i = 0;
435  inst->noperands = 0;
436 }
437 
440 static XED_INLINE void xed_inst1(
442  xed_state_t mode,
443  xed_iclass_enum_t iclass,
444  xed_uint_t effective_operand_width,
445  xed_encoder_operand_t op0) {
446 
447  inst->mode=mode;
448  inst->iclass = iclass;
449  inst->effective_operand_width = effective_operand_width;
450  inst->effective_address_width = 0;
451  inst->prefixes.i = 0;
452  inst->operands[0] = op0;
453  inst->noperands = 1;
454 }
455 
458 static XED_INLINE void xed_inst2(
460  xed_state_t mode,
461  xed_iclass_enum_t iclass,
462  xed_uint_t effective_operand_width,
464  xed_encoder_operand_t op1) {
465 
466  inst->mode=mode;
467  inst->iclass = iclass;
468  inst->effective_operand_width = effective_operand_width;
469  inst->effective_address_width = 0;
470  inst->prefixes.i = 0;
471  inst->operands[0] = op0;
472  inst->operands[1] = op1;
473  inst->noperands = 2;
474 }
475 
478 static XED_INLINE void xed_inst3(
480  xed_state_t mode,
481  xed_iclass_enum_t iclass,
482  xed_uint_t effective_operand_width,
485  xed_encoder_operand_t op2) {
486 
487  inst->mode=mode;
488  inst->iclass = iclass;
489  inst->effective_operand_width = effective_operand_width;
490  inst->effective_address_width = 0;
491  inst->prefixes.i = 0;
492  inst->operands[0] = op0;
493  inst->operands[1] = op1;
494  inst->operands[2] = op2;
495  inst->noperands = 3;
496 }
497 
498 
501 static XED_INLINE void xed_inst4(
503  xed_state_t mode,
504  xed_iclass_enum_t iclass,
505  xed_uint_t effective_operand_width,
509  xed_encoder_operand_t op3) {
510 
511  inst->mode=mode;
512  inst->iclass = iclass;
513  inst->effective_operand_width = effective_operand_width;
514  inst->effective_address_width = 0;
515  inst->prefixes.i = 0;
516  inst->operands[0] = op0;
517  inst->operands[1] = op1;
518  inst->operands[2] = op2;
519  inst->operands[3] = op3;
520  inst->noperands = 4;
521 }
522 
525 static XED_INLINE void xed_inst5(
527  xed_state_t mode,
528  xed_iclass_enum_t iclass,
529  xed_uint_t effective_operand_width,
534  xed_encoder_operand_t op4) {
535 
536  inst->mode=mode;
537  inst->iclass = iclass;
538  inst->effective_operand_width = effective_operand_width;
539  inst->effective_address_width = 0;
540  inst->prefixes.i = 0;
541  inst->operands[0] = op0;
542  inst->operands[1] = op1;
543  inst->operands[2] = op2;
544  inst->operands[3] = op3;
545  inst->operands[4] = op4;
546  inst->noperands = 5;
547 }
548 
549 
553 static XED_INLINE void xed_inst(
555  xed_state_t mode,
556  xed_iclass_enum_t iclass,
557  xed_uint_t effective_operand_width,
558  xed_uint_t number_of_operands,
559  const xed_encoder_operand_t* operand_array) {
560 
561  xed_uint_t i;
562  inst->mode=mode;
563  inst->iclass = iclass;
564  inst->effective_operand_width = effective_operand_width;
565  inst->effective_address_width = 0;
566  inst->prefixes.i = 0;
567  xed_assert(number_of_operands < XED_ENCODER_OPERANDS_MAX);
568  for(i=0;i<number_of_operands;i++) {
569  inst->operands[i] = operand_array[i];
570  }
571  inst->noperands = number_of_operands;
572 }
573 
575 
576 /*
577  xed_encoder_instruction_t x,y;
578 
579  xed_inst2(&x, state, XED_ICLASS_ADD, 32,
580  xed_reg(XED_REG_EAX),
581  xed_mem_bd(XED_REG_EDX, xed_disp(0x11223344, 32), 32));
582 
583  xed_inst2(&y, state, XED_ICLASS_ADD, 32,
584  xed_reg(XED_REG_EAX),
585  xed_mem_gbisd(XED_REG_FS, XED_REG_EAX, XED_REG_ESI,4,
586  xed_disp(0x11223344, 32), 32));
587 
588  */
589 
590 #endif
xed_encoder_operand_t operands[XED_ENCODER_OPERANDS_MAX]
struct xed_encoder_prefixes_t::@4 s
The main container for instructions.
static XED_INLINE void xed_lock(xed_encoder_instruction_t *x)
static XED_INLINE xed_enc_displacement_t xed_disp(xed_uint64_t displacement, xed_uint32_t displacement_width)
a memory displacement (not for branches)
union xed_encoder_operand_t::@2 u
xed_uint64_t displacement
static XED_INLINE xed_encoder_operand_t xed_mem_b(xed_reg_enum_t base, xed_uint_t width)
memory operand - base only
xed_iclass_enum_t
xed_uint32_t effective_operand_width
static XED_INLINE xed_encoder_operand_t xed_mem_bisd(xed_reg_enum_t base, xed_reg_enum_t index, xed_uint_t scale, xed_enc_displacement_t disp, xed_uint_t width)
memory operand - base, index, scale, displacement
xed_uint32_t scale
xed_encoder_prefixes_t prefixes
static XED_INLINE xed_encoder_operand_t xed_relbr(xed_int32_t brdisp, xed_uint_t width)
a relative branch displacement operand
static XED_INLINE void xed_inst5(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_encoder_operand_t op0, xed_encoder_operand_t op1, xed_encoder_operand_t op2, xed_encoder_operand_t op3, xed_encoder_operand_t op4)
instruction with five operands
static XED_INLINE xed_encoder_operand_t xed_mem_gd(xed_reg_enum_t seg, xed_enc_displacement_t disp, xed_uint_t width)
memory operand - segment and displacement only
xed_operand_enum_t operand_name
static XED_INLINE xed_encoder_operand_t xed_imm0(xed_uint64_t v, xed_uint_t width)
a first immediate operand (known as IMM0)
xed_reg_enum_t index
xed_uint32_t effective_address_width
unsigned int xed_bool_t
Definition: xed-types.h:68
static XED_INLINE xed_encoder_operand_t xed_reg(xed_reg_enum_t reg)
a register operand
static XED_INLINE xed_encoder_operand_t xed_seg1(xed_reg_enum_t seg1)
seg reg override for implicit suppressed memory ops
xed_reg_enum_t base
static XED_INLINE void xed_inst3(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_encoder_operand_t op0, xed_encoder_operand_t op1, xed_encoder_operand_t op2)
instruction with three operands
static XED_INLINE void xed_inst(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_uint_t number_of_operands, const xed_encoder_operand_t *operand_array)
instruction with an array of operands.
xed_operand_enum_t
#define XED_INLINE
#define xed_assert(x)
Definition: xed-util.h:123
xed_reg_enum_t
Definition: xed-reg-enum.h:39
static XED_INLINE void xed_inst2(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_encoder_operand_t op0, xed_encoder_operand_t op1)
instruction with two operands
static XED_INLINE void xed_inst4(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_encoder_operand_t op0, xed_encoder_operand_t op1, xed_encoder_operand_t op2, xed_encoder_operand_t op3)
instruction with four operands
static XED_INLINE void xed_rep(xed_encoder_instruction_t *x)
static XED_INLINE xed_encoder_operand_t xed_imm1(xed_uint8_t v)
an second immediate operand (known as IMM1)
Encapsulates machine modes for decoder/encoder requests.
Definition: xed-state.h:52
xed_encoder_operand_type_t
static XED_INLINE xed_encoder_operand_t xed_mem_bd(xed_reg_enum_t base, xed_enc_displacement_t disp, xed_uint_t width)
memory operand - base and displacement only
static XED_INLINE xed_encoder_operand_t xed_seg0(xed_reg_enum_t seg0)
seg reg override for implicit suppressed memory ops
static XED_INLINE xed_encoder_operand_t xed_mem_gbd(xed_reg_enum_t seg, xed_reg_enum_t base, xed_enc_displacement_t disp, xed_uint_t width)
memory operand - segment, base and displacement only
xed_reg_enum_t seg
unsigned int xed_uint_t
Definition: xed-types.h:65
struct xed_encoder_operand_t::@2::@3 s
static XED_INLINE void xed_addr(xed_encoder_instruction_t *x, xed_uint_t width)
This is to specify effective address size different than the default.
static XED_INLINE xed_encoder_operand_t xed_ptr(xed_int32_t brdisp, xed_uint_t width)
a relative displacement for a PTR operand – the subsequent imm0 holds the 16b selector ...
static XED_INLINE void xed_repne(xed_encoder_instruction_t *x)
xed_uint32_t displacement_width
static XED_INLINE void xed_inst1(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width, xed_encoder_operand_t op0)
instruction with one operand
xed_reg_enum_t reg
static XED_INLINE xed_encoder_operand_t xed_mem_gbisd(xed_reg_enum_t seg, xed_reg_enum_t base, xed_reg_enum_t index, xed_uint_t scale, xed_enc_displacement_t disp, xed_uint_t width)
memory operand - segment, base, index, scale, and displacement
XED_DLL_EXPORT xed_bool_t xed_convert_to_encoder_request(xed_encoder_request_t *out, xed_encoder_instruction_t *in)
convert a xed_encoder_instruction_t to a xed_encoder_request_t for encoding
xed_encoder_operand_type_t type
static XED_INLINE xed_encoder_operand_t xed_other(xed_operand_enum_t operand_name, xed_int32_t value)
an operand storage field name and value
#define XED_ENCODER_OPERANDS_MAX
xed_iclass_enum_t iclass
static XED_INLINE xed_encoder_operand_t xed_simm0(xed_int32_t v, xed_uint_t width)
an 32b signed immediate operand
xed_enc_displacement_t disp
#define XED_DLL_EXPORT
static XED_INLINE xed_encoder_operand_t xed_mem_gb(xed_reg_enum_t seg, xed_reg_enum_t base, xed_uint_t width)
memory operand - segment and base only
static XED_INLINE void xed_inst0(xed_encoder_instruction_t *inst, xed_state_t mode, xed_iclass_enum_t iclass, xed_uint_t effective_operand_width)
instruction with no operands