xed-sys 0.6.0+xed-2024.05.20

Rust FFI bindings for Intel XED.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* BEGIN_LEGAL 

Copyright (c) 2023 Intel Corporation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  
END_LEGAL */
/// @file xed-encode.c

////////////////////////////////////////////////////////////////////////////
// This file contains the public interface to the encoder. 
////////////////////////////////////////////////////////////////////////////
#include "xed-internal-header.h"
#include "xed-encode-private.h"
#include "xed-operand-accessors.h"
#include "xed-reg-class.h"
#include "xed-ild-enum.h"

#include "xed-encoder.h" // a generated file of prototypes
#include <string.h>  // memset
//FIXME just need one proto from the above file: xed_bool_t
//xed_encode_nonterminal_ISA_ENCODE(xed_encoder_request_t& xes);



void xed_encoder_request_emit_bytes(xed_encoder_request_t* q,
                                   const xed_uint8_t bits,
                                   const xed_uint64_t value){
    
    xed_uint32_t byte_offset;
    xed_uint8_t* p;
    xed_uint32_t bit_offset = xed_encoder_request_bit_offset(q);
   
    //verify that we are aligned on byte
    xed_assert((bit_offset & 7) == 0);
    if ( bit_offset + bits > 8 * xed_encoder_request_ilen(q)) {
        xed3_operand_set_error(q,XED_ERROR_BUFFER_TOO_SHORT);
        return;
    }

    byte_offset = bit_offset >> 3;    

    xed_encoder_request_update_bit_offset(q, bits);
    p = q->_byte_array._enc+byte_offset;

    switch(bits){
    case 8:
        *p = XED_STATIC_CAST(xed_uint8_t,value);
        break;
    case 16:
        *XED_REINTERPRET_CAST(xed_uint16_t*,p) = 
                        XED_STATIC_CAST(xed_uint16_t,value);
        break;
    case 32:
        *XED_REINTERPRET_CAST(xed_uint32_t*,p)= 
                        XED_STATIC_CAST(xed_uint32_t,value);
        break;
    case 64:
        *XED_REINTERPRET_CAST(xed_uint64_t*,p)= value;
        break;
    default:
        xed_assert(0); 
    }
    return;
}


void xed_encoder_request_encode_emit(xed_encoder_request_t* q,
                                     const unsigned int bits,
                                     const xed_uint64_t value) {
    xed_uint32_t nbits;
    xed_uint32_t byte_offset;
    xed_uint32_t bit_in_byte;
    xed_uint32_t processed_bits;
    xed_uint32_t t_bit_offset = xed_encoder_request_bit_offset(q);
    if ( t_bit_offset + bits > 8 * xed_encoder_request_ilen(q)) {
        xed3_operand_set_error(q,XED_ERROR_BUFFER_TOO_SHORT);
        return;
    }

    nbits = bits;
    byte_offset = t_bit_offset >> 3;
    bit_in_byte = t_bit_offset & 7;
    processed_bits = 0;

    // looking for multiples of 8 on the input and when we are at natural
    // byte boundaries in the output.
    if ((bits&7) == 0 && bit_in_byte == 0)    {
        xed_uint8_t* p;
        //int shift;
        
        // the value to encode is a multiple of 8 bits and the current bit
        // pointer is aligned on an 8b boundary, then we can jam in the
        // bytes efficiently using 8/16/32/64b stores.
        xed_encoder_request_update_bit_offset(q, bits);
        p = q->_byte_array._enc+byte_offset;

        switch(bits)        {
          case 8:
            *p = XED_STATIC_CAST(xed_uint8_t,value);
            break;
          case 16:
            *XED_REINTERPRET_CAST(xed_uint16_t*,p) = XED_STATIC_CAST(xed_uint16_t,value);
            break;
          case 32:
            *XED_REINTERPRET_CAST(xed_uint32_t*,p)= XED_STATIC_CAST(xed_uint32_t,value);
            break;
          case 64:
            *XED_REINTERPRET_CAST(xed_uint64_t*,p)= value;
            break;
          default:
            xed_assert(0); 
        }
        return;
    }

    // for inputs that are not multiples of 8-bits:
    while (nbits > 0) {
        xed_uint64_t tvalue; // value we'll shift to get to the right bits
        xed_uint32_t tbits; // # of bits we are taking in this iteraation
        xed_uint32_t bits_remaining_in_byte = 8 - bit_in_byte; 


        if (bits_remaining_in_byte >= nbits) {
            // What's left fits in the current byte.
            tbits  = nbits;
            nbits  = 0;
            tvalue = value;
        }
        else {
            xed_uint32_t vshift;
            
            // we have more bits than fit in what remains of this
            // byte. split it up and take just what remains in this byte.
            tbits  = bits_remaining_in_byte;
            nbits  = nbits - tbits;
            vshift = bits - processed_bits - tbits;
            tvalue = value >> vshift;
            processed_bits += tbits;
        }
        
        if (tbits == 8)
            q->_byte_array._enc[byte_offset] = XED_STATIC_CAST(xed_uint8_t,tvalue);
        else {
            xed_uint64_t mask;
            xed_uint32_t shift;
 
            // we will be OR'ing bits in to this byte so it had better
            // start off as zero.
            if (bit_in_byte == 0)
                q->_byte_array._enc[byte_offset] = 0;

            mask = (1<<tbits)-1;
            shift = bits_remaining_in_byte - tbits;
            q->_byte_array._enc[byte_offset] |= XED_STATIC_CAST(xed_uint8_t,(tvalue & mask) << shift);
        }
        byte_offset++;
        bit_in_byte = 0;
    }
    xed_encoder_request_update_bit_offset(q,bits);
}



xed_bool_t
xed_encoder_request__memop_compatible(const xed_encoder_request_t* p,
                                      xed_operand_width_enum_t operand_width) {
    // return 1 if the memop specified in the operand storage is
    // compatible with the argument operand_width.
    xed_uint16_t operand_width_bytes;
    xed_uint16_t request_width_bytes = xed3_operand_get_mem_width(p);

    // figure out the width, in bytes, of the specified operand
    xed_uint_t eosz = xed3_operand_get_eosz(p);
    xed_assert(operand_width < XED_OPERAND_WIDTH_LAST);
    xed_assert(eosz < 4);
    operand_width_bytes = xed_width_bits[operand_width][eosz]>>3;
    //variable sized stuff we punt on the width
    if (operand_width_bytes == 0 || operand_width_bytes == request_width_bytes)
        return 1;
    return 0;
}

static XED_INLINE void zero_inst_enc(xed_encoder_request_t* p)
{
    memset(p,0,sizeof(xed_encoder_request_t));
}

void xed_encoder_request_zero_set_mode(xed_encoder_request_t* p,
                                        const xed_state_t* dstate) {
    zero_inst_enc(p);
    xed_operand_values_set_mode(p,dstate);
}

void xed_encoder_request_zero(xed_encoder_request_t* p)    {
    zero_inst_enc(p);
}

xed_iclass_enum_t
xed_encoder_request_get_iclass( const xed_encoder_request_t* p)
{
    return XED_STATIC_CAST(xed_iclass_enum_t,xed3_operand_get_iclass(p));
}
void   xed_encoder_request_set_iclass( xed_encoder_request_t* p, 
                                       xed_iclass_enum_t iclass) {
    xed3_operand_set_iclass(p,iclass);
}


void
xed_encoder_request_set_effective_operand_width( xed_encoder_request_t* p, 
                                                 xed_uint_t width_bits) {
    switch(width_bits) {
      // x87 memops use the width.
      case 8:      xed3_operand_set_eosz(p,0); break;
        
      case 16:     xed3_operand_set_eosz(p,1); break;
      case 32:     xed3_operand_set_eosz(p,2); break;
      case 64:     xed3_operand_set_eosz(p,3); break;
      default:
        xed_assert( width_bits == 8 ||
                    width_bits == 16 ||
                    width_bits == 32 ||
                    width_bits == 64 );
        break;
    }
}
void
xed_encoder_request_set_effective_address_size( xed_encoder_request_t* p, 
                                                xed_uint_t width_bits) {
    switch(width_bits) {
      case 16:     xed3_operand_set_easz(p,1); break;
      case 32:     xed3_operand_set_easz(p,2); break;
      case 64:     xed3_operand_set_easz(p,3); break;
      default:
        xed_assert( width_bits == 16 ||
                    width_bits == 32 ||
                    width_bits == 64 );
        break;
    }
}

void xed_encoder_request_set_branch_displacement(xed_encoder_request_t* p,
                                                 xed_int64_t brdisp,
                                                 xed_uint_t nbytes) {
    xed_operand_values_set_branch_displacement(p, brdisp, nbytes);
}

void xed_encoder_request_set_relbr(xed_encoder_request_t* p) {
    xed3_operand_set_relbr(p,1);
}
void xed_encoder_request_set_absbr(xed_encoder_request_t* p) {
    xed3_operand_set_absbr(p,1);
}
void xed_encoder_request_set_ptr(xed_encoder_request_t* p) {
    xed3_operand_set_ptr(p,1);
}

void xed_encoder_request_set_memory_displacement(xed_encoder_request_t* p,
                                                 xed_int64_t memdisp,
                                                 xed_uint_t nbytes) {
    xed_operand_values_set_memory_displacement(p, memdisp, nbytes);
}

void xed_encoder_request_set_uimm0(xed_encoder_request_t* p,
                                   xed_uint64_t uimm,
                                   xed_uint_t nbytes) {
    xed_operand_values_set_immediate_unsigned(p, uimm, nbytes);
    xed3_operand_set_imm0(p,1);
}

void xed_encoder_request_set_uimm0_bits(xed_encoder_request_t* p,
                                        xed_uint64_t uimm,
                                        xed_uint_t nbits) {
    xed_operand_values_set_immediate_unsigned_bits(p, uimm, nbits);
    xed3_operand_set_imm0(p,1);
}
void xed_encoder_request_set_uimm1(xed_encoder_request_t* p,
                                   xed_uint8_t uimm) {
    xed3_operand_set_imm1(p,1);
    xed3_operand_set_uimm1(p,uimm);
}

void xed_encoder_request_set_simm(xed_encoder_request_t* p,
                                  xed_int32_t simm,
                                  xed_uint_t nbytes) {

    xed_operand_values_set_immediate_signed(p, simm, nbytes);
    xed3_operand_set_imm0(p,1);
}

void xed_encoder_request_set_agen(xed_encoder_request_t* p) {
    xed3_operand_set_agen(p,1);
}
void xed_encoder_request_set_mem0(xed_encoder_request_t* p) {
    xed3_operand_set_mem0(p,1);
}
void xed_encoder_request_set_mem1(xed_encoder_request_t* p) {
    xed3_operand_set_mem1(p,1);
}
void xed_encoder_request_set_memory_operand_length(xed_encoder_request_t* p,
                                                   xed_uint_t nbytes) {
    xed3_operand_set_mem_width(p,XED_STATIC_CAST(xed_uint16_t,nbytes));
}
void xed_encoder_request_set_seg0(xed_encoder_request_t* p,
                                  xed_reg_enum_t seg_reg) {
    xed3_operand_set_seg0(p,seg_reg);
}
void xed_encoder_request_set_seg1(xed_encoder_request_t* p,
                                  xed_reg_enum_t seg_reg) {
    xed3_operand_set_seg1(p,seg_reg);
}
void xed_encoder_request_set_base0(xed_encoder_request_t* p,
                                  xed_reg_enum_t base_reg) {
    xed3_operand_set_base0(p,base_reg);
}
void xed_encoder_request_set_base1(xed_encoder_request_t* p,
                                  xed_reg_enum_t base_reg) {
    xed3_operand_set_base1(p,base_reg);
}
void xed_encoder_request_set_index(xed_encoder_request_t* p,
                                  xed_reg_enum_t index_reg) {
    xed3_operand_set_index(p,index_reg);
}
void xed_encoder_request_set_scale(xed_encoder_request_t* p,
                                   xed_uint_t scale) {
    xed3_operand_set_scale(p,scale);
}

void xed_encoder_request_set_operand_order(xed_encoder_request_t* p,
                                           xed_uint_t operand_index, 
                                           xed_operand_enum_t name) {
    xed_assert(operand_index < XED_ENCODE_ORDER_MAX_OPERANDS);
    p->_operand_order[operand_index] = name;

    /* track the maximum number of operands */
    if (operand_index+1 > p->_n_operand_order)
        p->_n_operand_order =
            XED_STATIC_CAST(xed_uint8_t, operand_index + 1);
}

xed_operand_enum_t
xed_encoder_request_get_operand_order(xed_encoder_request_t* p,
                                      xed_uint_t operand_index) {
    xed_assert(operand_index < XED_ENCODE_ORDER_MAX_OPERANDS &&
               operand_index < p->_n_operand_order);
    return XED_STATIC_CAST(xed_operand_enum_t,p->_operand_order[operand_index]);
}


void xed_encoder_request_zero_operand_order(xed_encoder_request_t* p) {
    p->_n_operand_order = 0;
}

void xed_encoder_request_set_reg(xed_encoder_request_t* p,
                                 xed_operand_enum_t operand,
                                 xed_reg_enum_t reg) {
    xed_assert(operand < XED_OPERAND_LAST);
    xed_operand_values_set_operand_reg(p,operand,reg);
}

void
xed_encode_request_print(const xed_encoder_request_t* p,
                         char* buf,
                         xed_uint_t buflen)  {
    char* t;
    xed_uint_t i;
    xed_int_t blen = XED_STATIC_CAST(xed_int_t,buflen);
    if (buflen < 1000) {
        (void)xed_strncpy(buf,
                          "Buffer passed to xed_encode_request_print is "
                          "too short. Try 1000 bytes",
                          blen);
        return;
    }
    blen = xed_strncpy(buf,
                      xed_iclass_enum_t2str(xed_encoder_request_get_iclass(p)),
                      blen);
    blen = xed_strncat(buf, " ",blen);
    t = buf + xed_strlen(buf);
    xed_operand_values_print_short( p, t, blen);
    blen = XED_STATIC_CAST(xed_int_t,buflen  - xed_strlen(buf));

    if (p->_n_operand_order) {
        blen = xed_strncat(buf,"\nOPERAND ORDER: ",blen);
        for(i=0;i<p->_n_operand_order;i++) { 
            const char*  s = xed_operand_enum_t2str(XED_STATIC_CAST(xed_operand_enum_t,p->_operand_order[i]));
            blen = xed_strncat(buf, s, blen);
            blen = xed_strncat(buf, " ", blen);
        }
    }
    (void) xed_strncat(buf, "\n", blen);
}


//////////////////////////////////////////////////////////////////////////////////////////////
//FIXME: I am not fond of this fixed table, since it is a testament to
//how difficult it is to encode instructions. One day, I'll make this
//use the XED encoding engine.
#define XED_MAX_FIXED_NOPS 9
static const xed_uint8_t xed_nop_array[XED_MAX_FIXED_NOPS][XED_MAX_FIXED_NOPS] = {
    /*1B*/  { 0x90 },
    /*2B*/  { 0x66, 0x90},
    /*3B*/  { 0x0F, 0x1F, 0x00},
    /*4B*/  { 0x0F, 0x1F, 0x40, 0x00},
    /*5B*/  { 0x0F, 0x1F, 0x44, 0x00, 0x00},
    /*6B*/  { 0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00},
    /*7B*/  { 0x0F, 0x1F, 0x80, 0x00, 0x00,0x00, 0x00},
    /*8B*/  { 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00,0x00, 0x00},
    /*9B*/  { 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00,0x00, 0x00},
};

XED_DLL_EXPORT xed_error_enum_t
xed_encode_nop(xed_uint8_t* array,
               const unsigned int ilen)
{
    if (ilen >= 1 && ilen <= XED_MAX_FIXED_NOPS)  {
        // subtract one from the requested length to get the array index.
        memcpy(array, xed_nop_array[ilen-1], ilen);
        return XED_ERROR_NONE;
    }
    return XED_ERROR_GENERAL_ERROR;
}


#if defined(XED_AVX) || defined(XED_SUPPORTS_AVX512)        
static void set_vl(xed_reg_enum_t reg, xed_uint_t* vl)
{
    xed_uint_t nvl = *vl;
    xed_reg_class_enum_t rc = xed_reg_class(reg);
    // ignore XMM class becaues *vl is either 0 or set by user.

    if (rc == XED_REG_CLASS_YMM && nvl < 1)
        nvl = 1;
#  if defined(XED_SUPPORTS_AVX512)
    else if (rc == XED_REG_CLASS_ZMM && nvl < 2)
        nvl = 2;
#  endif

    *vl = nvl;
}


// uvl= 0,    1,    2
// vl=  012   012   012
//      xgg   txt   ttx
//
// x=match
// t=trust
// g=grow (using observed guess)

static void xed_encode_precondition_vl(xed_encoder_request_t* req)
{
    xed_uint_t vl;
    vl = xed3_operand_get_vl(req);
    // If user set nonzero value, respect it.  If user set vl=0, we cannot
    // tell so we try to override.  Note: It would be very wrong to
    // override incoming nonzero VL values because the observed register
    // sizes represent a MINIMUM valid VL. The actual VL can be larger for
    // "shrinking" converts (PD2PS, PD2DQ, PD2UQQ, etc.). 
    if (vl == 0)  
    {
        xed_operand_enum_t i;
        xed_reg_enum_t r;

        r = xed3_operand_get_index(req);
        // set VL based on index reg if vector reg as it would be for VSIB.
        set_vl(r,&vl);  

        // set VL based on REG any operands
        for (i=XED_OPERAND_REG0;i<=XED_OPERAND_REG9;i++)
        {
            xed3_get_generic_operand(req, i, &r);
            if (r == XED_REG_INVALID)
                break;
            set_vl(r,&vl);
        }

        // FIXME: ONLY SET FOR THINGS WITH SIMD (OR K-MASK FOR FPCLASS, VCMP) REGISTERS
        if (xed3_operand_get_mem0(req)) {
            xed_uint_t bytes = xed3_operand_get_mem_width(req);
            if (bytes == 32) {
                if (vl < 1)
                    vl = 1;
            }
            else if (bytes == 64) {
                vl = 2;
            }
        }
        xed3_operand_set_vl(req,vl);
    }
}

#endif
#if defined(XED_APX)
// Return non-zero value if GPR index is ( 16 < GPR < 32 )
static xed_uint_t reg_is_egpr(xed_reg_enum_t reg)
{
    reg = xed_get_largest_enclosing_register(reg);
    if ((reg >= XED_REG_R16) && (reg <= XED_REG_R31)) {
        return 1;
    }
    return 0;
}

/* Some instructions (e.g. VEX, LEGACY_MAP2, LEGACY_MAP3) does not support EGPRs(GPR > 16).
 * Each encoder group can include both EGPR and non-EGPR variations of the same
 * instruction(iclass). For example, KMOV exists in both VEX (non-EGPR) and EVEX enc space.
 * XED adds HAS_EGPR input condition for non-egpr instructions to skip them, so we 
 * need to scan and detect EGPR as a preparation */
static void xed_encode_precondition_egpr(xed_encoder_request_t* req)
{
    xed_uint_t i;
    for (i=XED_OPERAND_REG0; i<=XED_OPERAND_REG9 ;i++)
    {
        xed_reg_enum_t r;
        xed3_get_generic_operand(req, i, &r);
        if (r == XED_REG_INVALID)
            break;
        if (reg_is_egpr(r)) {
            xed3_operand_set_has_egpr(req, 1);
            return;
        }
    }

    if (xed3_operand_get_mem0(req)) {
        // Detect EGPR within the memory operand structure
        xed_reg_enum_t gprs[3];
        gprs[0] = xed3_operand_get_base0(req);
        gprs[1] = xed3_operand_get_base1(req);
        gprs[2] = xed3_operand_get_index(req);
        for (i = 0; i < sizeof(gprs)/sizeof(xed_reg_enum_t) ; i++) {
            if (reg_is_egpr(gprs[i])) {
                xed3_operand_set_has_egpr(req, 1);
                return;
            }
        }
    }
}
#endif

static void xed_encode_precondition(xed_encoder_request_t* r) {
    /* If the base is RIP, then we help the encoder users by adjusting or
     * supplying a memory displacement. It must be 4B, even if it is zero.
     *
     * This ignores the case of a 2B displacement. No one should do that as
     * it is not legal in 64b mode.
     */
    xed_int64_t t;
    if (xed3_operand_get_base0(r)==XED_REG_RIP) {
        if (xed3_operand_get_disp_width(r) == 0) {
            xed3_operand_set_disp_width(r,32);
            xed3_operand_set_disp(r,0);
        }
        else if (xed3_operand_get_disp_width(r) == 8) {  
           xed3_operand_set_disp_width(r,32);
            /* sign extend the current value to 64b and then pick off the
             * correct part of it */
            t = xed3_operand_get_disp(r);
            xed_operand_values_set_memory_displacement_bits(r,
                                                            t, 32);
        }
    }
#if defined(XED_AVX) || defined(XED_SUPPORTS_AVX512)        
    xed_encode_precondition_vl(r);
#endif
#if defined(XED_APX)
    xed_encode_precondition_egpr(r);
#endif
}

XED_DLL_EXPORT xed_error_enum_t xed_encode(xed_encoder_request_t* r,
                                           xed_uint8_t* array, 
                                           const unsigned int ilen,
                                           unsigned int* olen) {
    xed_iclass_enum_t iclass = xed_encoder_request_get_iclass(r);
    if (iclass != XED_ICLASS_INVALID && 
        iclass < XED_ICLASS_LAST && 
        ilen > 0 &&
        array != 0)
    {
        xed_bool_t okay;
        xed_encoder_vars_t xev;
        r->_byte_array._enc = array;

        // FIRST THING: set up the ephemeral storage for the encoder
        xed_encoder_request_set_encoder_vars(r,&xev);

        xed_encoder_request_set_ilen(r,ilen);
        xed_encode_precondition(r);
        okay = xed_encode_nonterminal_ISA_ENCODE(r);

        if (okay) {
            xed_uint32_t t_bit_offset = xed_encoder_request_bit_offset(r);
            xed_assert((t_bit_offset & 7) == 0); // no partial bytes
            *olen = (t_bit_offset >> 3); // offset points to the 1st bit of next byte
            xed_assert(ilen >= *olen);
            xed_encoder_request_vars_remove(r);
            return XED_ERROR_NONE;
        }
        xed_encoder_request_vars_remove(r);
        if (xed3_operand_get_error(r) != XED_ERROR_NONE)
            return xed3_operand_get_error(r);
        return XED_ERROR_GENERAL_ERROR;
    }
    return XED_ERROR_GENERAL_ERROR; 
}