xed-encoder-hl.h

Go to the documentation of this file.
00001 /*BEGIN_LEGAL 
00002 Copyright (c) 2004-2008, Intel Corp.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are
00007 met:
00008 
00009     * Redistributions of source code must retain the above copyright
00010       notice, this list of conditions and the following disclaimer.
00011 
00012     * Redistributions in binary form must reproduce the above
00013       copyright notice, this list of conditions and the following
00014       disclaimer in the documentation and/or other materials provided
00015       with the distribution.
00016 
00017     * Neither the name of Intel Corporation nor the names of its
00018       contributors may be used to endorse or promote products derived
00019       from this software without specific prior written permission.
00020 
00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 END_LEGAL */
00033 
00034 #ifndef _XED_ENCODER_HL_H_
00035 # define _XED_ENCODER_HL_H_
00036 #include "xed-types.h"
00037 #include "xed-reg-enum.h"
00038 #include "xed-state.h"
00039 #include "xed-iclass-enum.h"
00040 #include "xed-portability.h"
00041 #include "xed-encode.h"
00042 
00043 
00044 typedef struct {
00045     xed_uint64_t   displacement; 
00046     xed_uint32_t   displacement_width;
00047 } xed_enc_displacement_t; /* fixme bad name */
00048 
00050 
00051 
00052 
00053 XED_INLINE static  xed_enc_displacement_t xdisp(xed_uint64_t   displacement,
00054                                                 xed_uint32_t   displacement_width   ) {
00055     xed_enc_displacement_t x;
00056     x.displacement = displacement;
00057     x.displacement_width = displacement_width;
00058     return x;
00059 }
00061 
00062 typedef struct {
00063     xed_reg_enum_t seg;
00064     xed_reg_enum_t base;
00065     xed_reg_enum_t index;
00066     xed_uint32_t   scale;
00067     xed_enc_displacement_t disp;
00068 } xed_memop_t;
00069 
00070 
00071 typedef enum {
00072     XED_ENCODER_OPERAND_TYPE_INVALID,
00073     XED_ENCODER_OPERAND_TYPE_BRDISP,
00074     XED_ENCODER_OPERAND_TYPE_REG,
00075     XED_ENCODER_OPERAND_TYPE_IMM0,
00076     XED_ENCODER_OPERAND_TYPE_SIMM0,
00077     XED_ENCODER_OPERAND_TYPE_IMM1,
00078     XED_ENCODER_OPERAND_TYPE_MEM,
00079     XED_ENCODER_OPERAND_TYPE_PTR,
00080     XED_ENCODER_OPERAND_TYPE_SEG0, /* special for things with suppressed implicit memops */
00081     XED_ENCODER_OPERAND_TYPE_SEG1  /* special for things with suppressed implicit memops */
00082 } xed_encoder_operand_type_t;
00083 
00084 typedef struct {
00085     xed_encoder_operand_type_t  type;
00086     union {
00087         xed_reg_enum_t reg;
00088         xed_int32_t brdisp;
00089         xed_uint64_t imm0;
00090         xed_uint8_t imm1;
00091         xed_memop_t mem;
00092     } u;
00093     xed_uint32_t width;
00094 } xed_encoder_operand_t;
00095 
00097 
00098 
00099 
00100 XED_INLINE static  xed_encoder_operand_t xrelbr(xed_int32_t brdisp, xed_uint_t width) {
00101     xed_encoder_operand_t o;
00102     o.type = XED_ENCODER_OPERAND_TYPE_BRDISP;
00103     o.u.brdisp = brdisp;
00104     o.width = width;
00105     return o;
00106 }
00108 
00110 
00111 
00112 
00113 XED_INLINE static  xed_encoder_operand_t xptr(xed_int32_t brdisp, xed_uint_t width) {
00114     xed_encoder_operand_t o;
00115     o.type = XED_ENCODER_OPERAND_TYPE_PTR;
00116     o.u.brdisp = brdisp;
00117     o.width = width;
00118     return o;
00119 }
00121 
00123 
00124 
00125 
00126 XED_INLINE static  xed_encoder_operand_t xreg(xed_reg_enum_t reg) {
00127     xed_encoder_operand_t o;
00128     o.type = XED_ENCODER_OPERAND_TYPE_REG;
00129     o.u.reg = reg;
00130     o.width = 0;
00131     return o;
00132 }
00133 
00136 XED_INLINE static  xed_encoder_operand_t imm0(xed_uint64_t v, xed_uint_t width) {
00137     xed_encoder_operand_t o;
00138     o.type = XED_ENCODER_OPERAND_TYPE_IMM0;
00139     o.u.imm0 = v;
00140     o.width = width;
00141     return o;
00142 }
00145 XED_INLINE static  xed_encoder_operand_t simm0(xed_int64_t v, xed_uint_t width) {
00146     xed_encoder_operand_t o;
00147     o.type = XED_ENCODER_OPERAND_TYPE_SIMM0;
00148     o.u.imm0 = v; /* fixme sign conversion */
00149     o.width = width;
00150     return o;
00151 }
00152 
00155 XED_INLINE static  xed_encoder_operand_t imm1(xed_uint8_t v) {
00156     xed_encoder_operand_t o;
00157     o.type = XED_ENCODER_OPERAND_TYPE_IMM1;
00158     o.u.imm1 = v; 
00159     o.width = 8;
00160     return o;
00161 }
00162 
00164 
00166 
00167 
00170 XED_INLINE static  xed_encoder_operand_t xseg0(xed_reg_enum_t seg0) {
00171     xed_encoder_operand_t o;
00172     o.type = XED_ENCODER_OPERAND_TYPE_SEG0;
00173     o.u.reg = seg0;
00174     return o;
00175 }
00176 
00179 XED_INLINE static  xed_encoder_operand_t xseg1(xed_reg_enum_t seg1) {
00180     xed_encoder_operand_t o;
00181     o.type = XED_ENCODER_OPERAND_TYPE_SEG1;
00182     o.u.reg = seg1;
00183     return o;
00184 }
00185 
00188 XED_INLINE static  xed_encoder_operand_t xmem_b(xed_reg_enum_t base, xed_uint_t width) {
00189     xed_encoder_operand_t o;
00190     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00191     o.u.mem.base = base;
00192     o.u.mem.seg = XED_REG_INVALID;
00193     o.u.mem.index= XED_REG_INVALID;
00194     o.u.mem.scale = 0;
00195     o.u.mem.disp.displacement = 0;
00196     o.u.mem.disp.displacement_width = 0;
00197     o.width = width;
00198     return o;
00199 }
00200 
00203 XED_INLINE static  xed_encoder_operand_t xmem_bd(xed_reg_enum_t base, 
00204                               xed_enc_displacement_t disp,
00205                               xed_uint_t width) {
00206     xed_encoder_operand_t o;
00207     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00208     o.u.mem.base = base;
00209     o.u.mem.seg = XED_REG_INVALID;
00210     o.u.mem.index= XED_REG_INVALID;
00211     o.u.mem.scale = 0;
00212     o.u.mem.disp =disp;
00213     o.width = width;
00214     return o;
00215 }
00216 
00219 XED_INLINE static  xed_encoder_operand_t xmem_bisd(xed_reg_enum_t base, 
00220                                 xed_reg_enum_t index, 
00221                                 xed_uint_t scale,
00222                                 xed_enc_displacement_t disp,
00223                                 xed_uint_t width) {
00224     xed_encoder_operand_t o;
00225     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00226     o.u.mem.base = base;
00227     o.u.mem.seg = XED_REG_INVALID;
00228     o.u.mem.index= index;
00229     o.u.mem.scale = scale;
00230     o.u.mem.disp = disp;
00231     o.width = width;
00232     return o;
00233 }
00234 
00235 
00238 XED_INLINE static  xed_encoder_operand_t xmem_gb(xed_reg_enum_t seg, xed_reg_enum_t base, xed_uint_t width) {
00239     xed_encoder_operand_t o;
00240     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00241     o.u.mem.base = base;
00242     o.u.mem.seg = seg;
00243     o.u.mem.index= XED_REG_INVALID;
00244     o.u.mem.scale = 0;
00245     o.u.mem.disp.displacement = 0;
00246     o.u.mem.disp.displacement_width = 0;
00247     o.width = width;
00248     return o;
00249 }
00250 
00253 XED_INLINE static  xed_encoder_operand_t xmem_gbd(xed_reg_enum_t seg, xed_reg_enum_t base, 
00254                                xed_enc_displacement_t disp,
00255                                xed_uint_t width) {
00256     xed_encoder_operand_t o;
00257     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00258     o.u.mem.base = base;
00259     o.u.mem.seg = seg;
00260     o.u.mem.index= XED_REG_INVALID;
00261     o.u.mem.scale = 0;
00262     o.u.mem.disp = disp;
00263     o.width = width;
00264     return o;
00265 }
00266 
00269 XED_INLINE static  xed_encoder_operand_t xmem_gbisd(xed_reg_enum_t seg, 
00270                                  xed_reg_enum_t base, 
00271                                  xed_reg_enum_t index, 
00272                                  xed_uint_t scale,
00273                                  xed_enc_displacement_t disp, 
00274                                  xed_uint_t width) {
00275     xed_encoder_operand_t o;
00276     o.type = XED_ENCODER_OPERAND_TYPE_MEM;
00277     o.u.mem.base = base;
00278     o.u.mem.seg = seg;
00279     o.u.mem.index= index;
00280     o.u.mem.scale = scale;
00281     o.u.mem.disp = disp;
00282     o.width = width;
00283     return o;
00284 }
00286 
00287 typedef union {
00288     struct {
00289         xed_uint32_t rep               :1;
00290         xed_uint32_t repne             :1;
00291         xed_uint32_t lock              :1;
00292         xed_uint32_t br_hint_taken     :1;
00293         xed_uint32_t br_hint_not_taken :1;
00294     } s;
00295     xed_uint32_t i;
00296 }  xed_encoder_prefixes_t;
00297 
00298 #define XED_ENCODER_OPERANDS_MAX 3 /* FIXME */
00299 typedef struct {
00300     xed_state_t mode;
00301     xed_iclass_enum_t iclass; /*FIXME: use iform instead? or allow either */
00302     xed_uint32_t effective_operand_width;
00303 
00304     /* the effective_address_width is only requires to be set for
00305      *  instructions * with implicit suppressed memops or memops with no
00306      *  base or index regs.  When base or index regs are present, XED pick
00307      *  this up automatically from the register names.
00308 
00309      * FIXME: make effective_address_width required by all encodes for
00310      * unifority. Add to xed_inst[0123]() APIs??? */
00311     xed_uint32_t effective_address_width; 
00312 
00313     xed_encoder_prefixes_t prefixes;
00314     xed_uint32_t noperands;
00315     xed_encoder_operand_t operands[XED_ENCODER_OPERANDS_MAX];
00316 } xed_encoder_instruction_t;
00317 
00319 
00320 
00321 
00322 
00323 
00324 
00325 XED_INLINE static void xaddr(xed_encoder_instruction_t* x, 
00326                              xed_uint_t width) {
00327     x->effective_address_width = width;
00328 }
00329 
00330 
00332 XED_INLINE static  void xrep(xed_encoder_instruction_t* x) { 
00333     x->prefixes.s.rep=1;
00334 }
00335 
00337 XED_INLINE static  void xrepne(xed_encoder_instruction_t* x) { 
00338     x->prefixes.s.repne=1;
00339 }
00340 
00342 XED_INLINE static  void xlock(xed_encoder_instruction_t* x) { 
00343     x->prefixes.s.lock=1;
00344 }
00345 
00346 
00347 
00348 
00351 xed_bool_t xed_convert_to_encoder_request(xed_encoder_request_t* out,
00352                                           xed_encoder_instruction_t* in);
00353 
00355 
00357 /* FIXME: rather than return the xed_encoder_instruction_t I can make
00358  * another version that returns a xed_encoder_request_t. Saves silly
00359  * copying. Although the xed_encoder_instruction_t might be handy for
00360  * having code templates that get customized & passed to encoder later. */
00363 
00364 
00367 XED_INLINE static  void xed_inst0(
00368     xed_encoder_instruction_t* inst,
00369     xed_state_t mode,
00370     xed_iclass_enum_t iclass,
00371     xed_uint_t effective_operand_width) {
00372 
00373     inst->mode=mode;
00374     inst->iclass = iclass;
00375     inst->effective_operand_width = effective_operand_width;
00376     inst->effective_address_width = 0;
00377     inst->prefixes.i = 0;
00378     inst->noperands = 0;
00379 }
00380 
00383 XED_INLINE static  void xed_inst1(
00384     xed_encoder_instruction_t* inst,
00385     xed_state_t mode,
00386     xed_iclass_enum_t iclass,
00387     xed_uint_t effective_operand_width,
00388     xed_encoder_operand_t op0) {
00389     
00390     inst->mode=mode;
00391     inst->iclass = iclass;
00392     inst->effective_operand_width = effective_operand_width;
00393     inst->effective_address_width = 0;
00394     inst->prefixes.i = 0;
00395     inst->operands[0] = op0;
00396     inst->noperands = 1;
00397 }
00398 
00401 XED_INLINE static  void xed_inst2(
00402     xed_encoder_instruction_t* inst,
00403     xed_state_t mode,
00404     xed_iclass_enum_t iclass,
00405     xed_uint_t effective_operand_width,
00406     xed_encoder_operand_t op0,
00407     xed_encoder_operand_t op1) {
00408 
00409     inst->mode=mode;
00410     inst->iclass = iclass;
00411     inst->effective_operand_width = effective_operand_width;
00412     inst->effective_address_width = 0;
00413     inst->prefixes.i = 0;
00414     inst->operands[0] = op0;
00415     inst->operands[1] = op1;
00416     inst->noperands = 2;
00417 }
00418 
00421 XED_INLINE static  void xed_inst3(
00422     xed_encoder_instruction_t* inst,
00423     xed_state_t mode,
00424     xed_iclass_enum_t iclass,
00425     xed_uint_t effective_operand_width,
00426     xed_encoder_operand_t op0,
00427     xed_encoder_operand_t op1,
00428     xed_encoder_operand_t op2) {
00429 
00430     inst->mode=mode;
00431     inst->iclass = iclass;
00432     inst->effective_operand_width = effective_operand_width;
00433     inst->effective_address_width = 0;
00434     inst->prefixes.i = 0;
00435     inst->operands[0] = op0;
00436     inst->operands[1] = op1;
00437     inst->operands[2] = op2;
00438     inst->noperands = 3;
00439 }
00440 
00442 
00443 /*
00444  xed_encoder_instruction_t x,y;
00445 
00446  xed_inst2(&x, state, XED_ICLASS_ADD, 32, 
00447            xreg(XED_REG_EAX), 
00448            xmem_bd(XED_REG_EDX, xdisp(0x11223344, 32), 32));
00449   
00450  xed_inst2(&y, state, XED_ICLASS_ADD, 32, 
00451            xreg(XED_REG_EAX), 
00452            xmem_gbisd(XED_REG_FS, XED_REG_EAX, XED_REG_ESI,4, xdisp(0x11223344, 32), 32));
00453 
00454  */
00455 
00456 #endif

Generated on Wed Dec 3 03:10:46 2008 for XED2 by  doxygen 1.4.6