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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* BEGIN_LEGAL 

Copyright (c) 2024 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-util.c
/// 


////////////////////////////////////////////////////////////////////////////

#include "xed-types.h"
#include "xed-util.h"
#include "xed-util-private.h"
#include "xed-portability.h"
#include "xed-portability-private.h"
#include "xed-common-defs.h"
#include "xed-common-hdrs.h"

#if defined(__linux__) && defined(__STDC_HOSTED__) && __STDC_HOSTED__ == 0
   extern void abort (void)  __attribute__ ((__noreturn__));
#else
#  include <stdlib.h>
#endif

#include <ctype.h>


////////////////////////////////////////////////////////////////////////////
int xed_verbose   = 2;

#include <stdio.h> //required for fprintf,stderr in xed_abort()
#if defined(XED_MESSAGES)
FILE* xed_log_file;
#endif

////////////////////////////////////////////////////////////////////////////
void xed_set_verbosity(int v) {
    xed_verbose = v;
}

void  xed_set_log_file(void* o) {
#if defined(XED_MESSAGES)
    xed_log_file = (FILE*)o;
#else
    (void)o;
#endif

}





#if !defined(XED_64B)
static XED_INLINE xed_uint64_t get_bit63(xed_uint64_t x) {
    const xed_uint64_t s = 63;
    return (x >> s) & 1;
}

static XED_INLINE  xed_uint64_t xed_divide_by_10_64by32(xed_uint64_t numerator) {
    /* ONLY WORKS FOR DIVIDE BY 10 because 2*9+1=19 and that is < 20, so I can use subtract */
    int i=0;
    const xed_uint32_t denominator = 10;
    xed_uint64_t tn=numerator;
    xed_uint64_t tqlo=0;
    xed_uint32_t ir=0,b=0;
    xed_uint32_t num=0;
    xed_uint32_t qbit=0;

    /* binary long division */
    for(i=0;i<64;i++) {
        b = get_bit63(tn);          // next bit of the numerator
        num = (ir << 1) | b;        // intermediate remainder from last step + new numerator bit 
        if (num >= denominator) { 
            ir  = num - denominator;         
            qbit = 1;
        }
        else {
            ir = num; 
            qbit = 0;
        }
        tqlo = (tqlo <<1) | qbit;                 
        tn   = tn << 1;
    }

    // ignore quotient overflow;
    return tqlo;
}
#endif

int xed_itoa(char* buf, xed_uint64_t f, int buflen) {
    char tbuf[100];
    char* p = tbuf;
    char* fp;
    xed_uint64_t t = f;
    xed_uint64_t x,v;

    if (f == 0) {
        *p++ = '0';
        *p  = 0;
        return xed_strncpy(buf,tbuf,buflen);
    }

    while(t)    {
#if defined(XED_64B)
        x = t / 10;
#else
        x = xed_divide_by_10_64by32(t);
#endif
        v = t - (x*10);
        *p++ = XED_STATIC_CAST(char, '0' + v);
        t = x;
    }
    /* reverse string */
    *p=0;
    p--;
    fp = tbuf;
    while(fp < p) {
        char ec = *p;
        char fc = *fp;
        *fp = ec;
        *p  = fc;
        fp++;
        p--;
    }

    return xed_strncpy(buf,tbuf,buflen);
}

static int add_leading_zeros(char* buf,
                             char* tbuf,
                             int buflen,
                             xed_uint_t bits_to_print)
{
    char* p = buf;
    xed_uint_t ilen = xed_strlen(tbuf);
    if (ilen < bits_to_print) {
        xed_uint_t i;
        xed_uint_t zeros = bits_to_print - ilen;
        for(i=0 ; i < zeros && buflen>0 ; i++) {
            buflen--;
            *p++ = '0';
        }
    }
    return xed_strncpy(p,tbuf,buflen);
}

int xed_itoa_bin(char *buf,
                 xed_uint64_t f,
                 xed_uint_t bits_to_print,
                 xed_uint_t buflen)
{
    char tbuf[65];
    xed_int_t i;
    char *p = tbuf;
    xed_uint64_t base_letter = XED_STATIC_CAST(xed_uint64_t, '0');
    xed_uint_t num_bits = (bits_to_print > 64) ? 64 : bits_to_print;
    if (num_bits >= buflen) {
        // Adjust `num_bits` to fit within the available buffer size
        num_bits = buflen - 1;
    }

    for (i = num_bits - 1; i >= 0; i--) {
            *p++ = XED_STATIC_CAST(char, base_letter + ((f >> i) & 1));
    }

    // tack on a null
    *p = 0;
    return xed_strncpy(buf, tbuf, buflen);
}

int xed_itoa_hex_ul(char* buf, 
                    xed_uint64_t f, 
                    xed_uint_t bits_to_print,
                    xed_bool_t leading_zeros,                            
                    int buflen,
                    xed_bool_t lowercase)
{
    const xed_uint64_t one = 1;
    xed_uint_t nibbles_to_print = (bits_to_print+3)/4;
    xed_uint64_t mul,rdiv;
    xed_uint64_t n;
    char tbuf[100];
    char* p = tbuf;
    //  mask the value to the bits we care about. makes everything else easier.
    xed_uint64_t ff,t; 
    xed_uint_t div = 0;
    xed_uint64_t base_letter;

    if (bits_to_print == 64) // no masking required
        ff = f;
    else
        ff = f & ((one<<bits_to_print)-1);

    if (ff == 0) {
        *p++ = '0';
        *p  = 0;
        if (leading_zeros)
            return add_leading_zeros(buf,tbuf,buflen,bits_to_print);
        else 
            return xed_strncpy(buf,tbuf,buflen);
    }

    t = ff;
    while(t)    {
        t = t >> 4;
        div++;
    }

    n = ff;
    
    if (lowercase)
        base_letter = XED_STATIC_CAST(xed_uint64_t,'a');
    else
        base_letter = XED_STATIC_CAST(xed_uint64_t,'A');

    while(div > 0) {

        div--;
        rdiv = one<<(4*div);
        //mul =  xed_divide(n,rdiv);
        mul =  (n >> (4*div)) & 0xF;
        if (div <= nibbles_to_print) {
            if (mul<10)
                *p++ = XED_STATIC_CAST(char,mul + '0');
            else
                *p++ = XED_STATIC_CAST(char,mul - 10  + base_letter);
        }
        n = n - (mul*rdiv);
    }
        
    // tack on a null
    *p = 0;
    if (leading_zeros)
        return add_leading_zeros(buf,tbuf,buflen,bits_to_print);
    return xed_strncpy(buf,tbuf,buflen);
}

int xed_itoa_hex_zeros(char* buf, 
                       xed_uint64_t f, 
                       xed_uint_t bits_to_print, 
                       xed_bool_t leading_zeros, 
                       int buflen) {
    const xed_bool_t lowercase=1;
    return xed_itoa_hex_ul(buf,f,bits_to_print, leading_zeros, buflen, lowercase);
}

int xed_itoa_hex(char* buf, 
                 xed_uint64_t f, 
                 xed_uint_t bits_to_print, 
                 int buflen)
{
    const xed_bool_t lowercase = 1;
    const xed_bool_t leading_zeros = 0;
    return xed_itoa_hex_ul(buf, f, bits_to_print, leading_zeros, buflen, lowercase);
}

int xed_itoa_signed(char* buf, xed_int64_t f, int buflen) {
    xed_uint64_t x;
    int blen = buflen;
    if (f<0) {
        blen = xed_strncpy(buf,"-",blen);
        x = XED_STATIC_CAST(xed_uint64_t,-f);
    }
    else
        x = XED_STATIC_CAST(xed_uint64_t,f);
    return xed_itoa(buf+xed_strlen(buf), x, blen);
}

int xed_sprintf_uint8_hex(char* buf, xed_uint8_t x, int buflen) {
    return xed_itoa_hex(buf,x,8,buflen);
}
int xed_sprintf_uint16_hex(char* buf, xed_uint16_t x, int buflen) {
    return xed_itoa_hex(buf,x,16,buflen);
}
int xed_sprintf_uint32_hex(char* buf, xed_uint32_t x, int buflen) {
    return xed_itoa_hex(buf,x,32,buflen);
}
int xed_sprintf_uint64_hex(char* buf, xed_uint64_t x, int buflen) {
    return xed_itoa_hex(buf,x,64,buflen);
}
int xed_sprintf_uint8(char* buf, xed_uint8_t x, int buflen) {
    return xed_itoa(buf, x, buflen);
}
int xed_sprintf_uint16(char* buf, xed_uint16_t x, int buflen) {
    return xed_itoa(buf, x, buflen);
}
int xed_sprintf_uint32(char* buf, xed_uint32_t x, int buflen) {
    return xed_itoa(buf, x, buflen);
}
int xed_sprintf_uint64(char* buf, xed_uint64_t x, int buflen) {
    return xed_itoa(buf, x, buflen);
}
int xed_sprintf_int8(char* buf, xed_int8_t x, int buflen) {
    return xed_itoa_signed(buf,x,buflen);
}
int xed_sprintf_int16(char* buf, xed_int16_t x, int buflen) {
    return xed_itoa_signed(buf,x,buflen);
}
int xed_sprintf_int32(char* buf, xed_int32_t x, int buflen) {
    return xed_itoa_signed(buf,x,buflen);
}
int xed_sprintf_int64(char* buf, xed_int64_t x, int buflen) {
    return xed_itoa_signed(buf,x,buflen);
}

char xed_to_ascii_hex_nibble(xed_uint_t x, xed_bool_t lowercase) {
    if (x<=9)
        return XED_STATIC_CAST(char,x+'0');
    if (x<=15)  {
        if (lowercase)
            return XED_STATIC_CAST(char,x-10+'a');
        else
            return XED_STATIC_CAST(char,x-10+'A');
    }
    return '?';
}
static char xed_tolower(char c) {
    if (c >= 'A' && c <= 'Z')
        return c-'A'+'a';
    return c;
}


int xed_strncat_lower(char* dst, const char* src, int len) {
    xed_uint_t dst_len = xed_strlen(dst) ;
    xed_uint_t orig_max = dst_len + XED_STATIC_CAST(xed_uint_t,len);
    xed_uint_t i;
    xed_uint_t src_len = xed_strlen(src);
    xed_uint_t copy_max = src_len;
    xed_uint_t ulen = (xed_uint_t)len-1;
    if (len <= 0) 
        return 0;

    /* do not copy more bytes than fit in the buffer including the null */

    if (src_len > ulen)
        copy_max = ulen;
        
    for(i=0;i<copy_max;i++) 
        dst[dst_len+i]=xed_tolower(src[i]);

    dst[dst_len+copy_max]=0;
    // should never go negative
    return XED_STATIC_CAST(int,orig_max - xed_strlen(dst));
}



////////////////////////////////////////////////////////////////////////////

/* arbitrary sign extension from a qty of "bits" length to 64b */
xed_int64_t xed_sign_extend_arbitrary_to_64(xed_uint64_t x, unsigned int bits) {
    xed_uint64_t one = 1;
    xed_int64_t mask = one<<(bits-1);
    xed_int64_t vmask, o=0;
    if (bits < 64) {
        vmask = (one<<bits)-1;
        o = ((x&vmask) ^ mask)- mask;
    }
    else if (bits == 64) 
        o=x;
    else 
        xed_assert(0);
    return o;
}

/* arbitrary sign extension from a qty of "bits" length to 32b */
xed_int32_t xed_sign_extend_arbitrary_to_32(xed_uint32_t x, unsigned int bits) {
    xed_int32_t mask = 1<<(bits-1);
    xed_int32_t vmask, o=0;
    if (bits < 32) {
        vmask = (1<<bits)-1;
        o = ((x&vmask) ^ mask)- mask;
    }
    else if (bits == 32) 
        o=x;
    else 
        xed_assert(0);
    return o;
}


xed_int64_t xed_sign_extend32_64(xed_int32_t x) {
    return x;
}
xed_int64_t xed_sign_extend16_64(xed_int16_t x) {
    return x;
}
xed_int64_t xed_sign_extend8_64(xed_int8_t x) {
    return x;
}
xed_int32_t xed_sign_extend16_32(xed_int16_t x) {
    return x;
}
xed_int32_t xed_sign_extend8_32(xed_int8_t x) {
    return x;
}
xed_int16_t xed_sign_extend8_16(xed_int8_t x) {
    return x;
}


xed_uint64_t xed_zero_extend32_64(xed_uint32_t x) {
    return x;
}
xed_uint64_t xed_zero_extend16_64(xed_uint16_t x) {
    return x;
}
xed_uint64_t xed_zero_extend8_64(xed_uint8_t x) {
    return x;
}
xed_uint32_t xed_zero_extend16_32(xed_uint16_t x) {
    return x;
}
xed_uint32_t xed_zero_extend8_32(xed_uint8_t x) {
    return x;
}
xed_uint16_t xed_zero_extend8_16(xed_uint8_t x) {
    return x;
}

#if defined(XED_LITTLE_ENDIAN_SWAPPING)
static XED_INLINE xed_int64_t xed_sign_extend4_64(xed_int8_t x) {
    const xed_int64_t eight = 8;
    xed_int64_t o = (x ^ eight)- eight;
    return o;
}
static XED_INLINE xed_int32_t xed_sign_extend4_32(xed_int8_t x) {
    xed_int32_t o = (x ^ 0x00000008)- 0x00000008;
    return o;
}

xed_int64_t xed_little_endian_hilo_to_int64(xed_uint32_t hi_le, xed_uint32_t lo_le, unsigned int len) {
    switch(len)    {
      case 4:
        return xed_sign_extend4_64(XED_STATIC_CAST(xed_int8_t,lo_le&0xF));
      case 8:
        return xed_sign_extend8_64(XED_STATIC_CAST(xed_int8_t,lo_le));
      case 16:
        return xed_sign_extend16_64(xed_bswap16(XED_STATIC_CAST(xed_uint16_t,lo_le)));
      case 32: {
          xed_int32_t y = xed_bswap32(XED_STATIC_CAST(xed_uint32_t,lo_le));
          xed_int64_t z;
          //printf("BSWAPING %lx -> %x\n", x,y);
          z= xed_sign_extend32_64(y);
          //printf("SEXT -> %lx\n", z);
          return z;
      }
      case 64:   {
        xed_uint64_t z = xed_make_uint64(xed_bswap32(hi_le),xed_bswap32(lo_le));
        return XED_STATIC_CAST(xed_int64_t,z);
      }
      default:
        xed_assert(0);
        return 0;
    }
}
xed_uint64_t xed_little_endian_hilo_to_uint64(xed_uint32_t hi_le, xed_uint32_t lo_le, unsigned int len) {
    switch(len)     {
      case 4:
        return XED_STATIC_CAST(xed_uint8_t,lo_le&0xF);
      case 8:
        return XED_STATIC_CAST(xed_uint8_t,lo_le);
      case 16:
        return xed_bswap16(XED_STATIC_CAST(xed_uint16_t,lo_le));
      case 32:
        return xed_bswap32(XED_STATIC_CAST(xed_uint32_t,lo_le));
      case 64:
        return xed_make_uint64(xed_bswap32(hi_le),xed_bswap32(lo_le));
      default:
        xed_assert(0);
        return 0;
    }
}

xed_uint64_t  xed_little_endian_to_uint64(xed_uint64_t x, unsigned int len) {
    switch(len)     {
      case 4:
        return XED_STATIC_CAST(xed_uint8_t,x&0xF);
      case 8:
        return XED_STATIC_CAST(xed_uint8_t,x);
      case 16:
        return xed_bswap16(XED_STATIC_CAST(xed_uint16_t,x));
      case 32:
        return xed_bswap32(XED_STATIC_CAST(xed_uint32_t,x));
      case 64:
        return xed_bswap64(x);
      default:
        xed_assert(0);
        return 0;
    }
}

xed_int64_t xed_little_endian_to_int64(xed_uint64_t x, unsigned int len){
    switch(len)    {
      case 4:
        return xed_sign_extend4_64(XED_STATIC_CAST(xed_int8_t,x&0xF));
      case 8:
        return xed_sign_extend8_64(XED_STATIC_CAST(xed_int8_t,x));
      case 16:
        return xed_sign_extend16_64(xed_bswap16(XED_STATIC_CAST(xed_uint16_t,x)));
      case 32: {
          xed_int32_t y = xed_bswap32(XED_STATIC_CAST(xed_uint32_t,x));
          xed_int64_t z;
          //printf("BSWAPING %lx -> %x\n", x,y);
          z= xed_sign_extend32_64(y);
          //printf("SEXT -> %lx\n", z);
          return z;
      }
      case 64:
        return XED_STATIC_CAST(xed_int64_t,xed_bswap64(x));
      default:
        xed_assert(0);
        return 0;
    }
}

xed_int32_t xed_little_endian_to_int32(xed_uint64_t x, unsigned int len){
    // heavily reliant on the type system
    switch(len)    {
      case 4:
        return xed_sign_extend4_32(XED_STATIC_CAST(xed_int8_t,x&0xF));
      case 8:
        return xed_sign_extend8_32(XED_STATIC_CAST(xed_int8_t,x));
      case 16:
        return xed_sign_extend16_32(xed_bswap16(XED_STATIC_CAST(xed_uint16_t,x)));
      case 32: {
          xed_int32_t y = xed_bswap32(XED_STATIC_CAST(xed_uint32_t,x));
          return y;
      }
      default:
        xed_assert(0);
        return 0;
    }
}
#endif    
    
xed_uint8_t xed_get_byte(xed_uint64_t x, unsigned int i, unsigned int len) {
    // THIS IS THE "IN REGISTER" VIEW!
    // 1B      .. .. .. .. .. .. .. 00
    // 2B      .. .. .. .. .. .. 11 00
    // 4B      .. .. .. .. 33 22 11 00
    // 8B      77 66 55 44 33 22 11 00
    // (The least significant byte is  00)

    xed_assert (i < len);
    (void)len; //pacify compiler
    return XED_BYTE_CAST(x >> (i*8));
}



xed_uint_t
xed_shortest_width_signed(xed_int64_t x, xed_uint8_t legal_widths) {
    static const  xed_int64_t max1[] = { 0x7f, 0x7fff, 0x7fffffff };
    static const  xed_int64_t min1[] = {
        -128,
        // supposedly the negative 64b values w/leading F digit is machine
        // dependent. So we use a leading minus sign to cause the compiler
        // to do 2s complement and install the desired values.
        -0x8000LL,       // == 0xffffffffffff8000LL
        -0x80000000LL    // == 0xffffffff80000000LL
    };
    /*historical note: I experimented with different ways of computing the
     * constants without making memory references, by shifting, etc. The
     * thing is that any way I coded it, Gcc's optimizer unrolled the loop,
     * removed the shifts or memops and did the right thing!  I was
     * astounded.  This version of the code was the simplest to maintain
     * and understand so I'm sticking with it.
     */
    unsigned int i,j;
    for(i=0;i<3;i++)  
    {
        j = 1 << i;
        if ((j & legal_widths)==j) 
            if (x <= max1[i] && x >= min1[i])
                break;
    }
    /* returns 1,2,4 or 8 */
    return 1<<i;
}

xed_uint_t
xed_shortest_width_unsigned(xed_uint64_t x, xed_uint8_t legal_widths) {

    unsigned int i,j;
    const xed_uint64_t one = 1;
    for(i=0;i<3; i++)
    {
        j = 1 << i;
        if ((j & legal_widths)==j)
            if (x < (one<<(j*8)))
                break;
    }
    /* returns 1,2,4 or 8 */
    return 1<<i;
}



void xed_derror(const char* s) {
    XED2DIE((xed_log_file,"%s\n", s));
    (void)s; // needed for when msgs are disabled and compiler warnings  are errors
}

//////////////////////////////////////////

static xed_user_abort_function_t xed_user_abort_function = 0;


static void* xed_user_abort_other = 0;

void xed_register_abort_function(xed_user_abort_function_t fn,
                                 void* other) {
    xed_user_abort_function = fn;
    xed_user_abort_other = other;
}

void xed_internal_assert( const char* msg, const char* file, int line) {
    if (xed_user_abort_function) {
      (*xed_user_abort_function)(msg, file, line, xed_user_abort_other);
    }
    else {
      fprintf(stderr,"ASSERTION FAILURE %s at %s:%d\n", msg, file, line);
    }
    abort();
}