1 2#include <stdio.h> 3#include <stdlib.h> 4#include <assert.h> 5#include "tests/malloc.h" 6 7typedef unsigned char UChar; 8typedef unsigned int UInt; 9typedef unsigned long int UWord; 10typedef unsigned long long int ULong; 11 12 13typedef struct { UChar cs[40]; } Block; 14 15void showBlock ( char* msg, Block* b ) 16{ 17 int i; 18 printf(" %s ", msg); 19 for (i = 0; i < 40; i++) 20 printf("%02x", (UInt)b->cs[i]); 21 printf("\n"); 22} 23 24UChar randUChar ( void ) 25{ 26 static UInt seed = 80021; 27 seed = 1103515245 * seed + 12345; 28 return (seed >> 17) & 0xFF; 29} 30 31void randBlock ( Block* b ) 32{ 33 int i; 34 UChar* p = (UChar*)b; 35 for (i = 0; i < sizeof(Block); i++) 36 p[i] = randUChar(); 37} 38 39/* Generate a function test_NAME, that tests the given insn. 40 The insn may only mention (%rax) and r9. */ 41 42#define GEN_test_Monly(_name, _mem_form) \ 43 \ 44 __attribute__ ((noinline)) static void test_##_name ( void ) \ 45 { \ 46 Block* b = memalign32(sizeof(Block)); \ 47 randBlock(b); \ 48 printf("%s\n", #_name); \ 49 showBlock("before", b); \ 50 __asm__ __volatile__( \ 51 "leaq 16(%0),%%rax" "\n\t" \ 52 "movq 24(%0),%%r9" "\n\t" \ 53 _mem_form "\n\t" \ 54 "movq %%r9, 32(%0)" "\n\t" \ 55 : /*OUT*/ \ 56 : /*IN*/"r"(b) \ 57 : /*TRASH*/"r9","rax","memory","cc" \ 58 ); \ 59 showBlock("after ", b); \ 60 printf("\n"); \ 61 free(b); \ 62 } 63 64GEN_test_Monly( MOVBE_RtoM_64, "movbe %%r9, 1(%%rax)") 65GEN_test_Monly( MOVBE_RtoM_32, "movbe %%r9d,1(%%rax)") 66GEN_test_Monly( MOVBE_RtoM_16, "movbe %%r9w,1(%%rax)") 67 68GEN_test_Monly( MOVBE_MtoR_64, "movbe 1(%%rax), %%r9") 69GEN_test_Monly( MOVBE_MtoR_32, "movbe 1(%%rax), %%r9d") 70GEN_test_Monly( MOVBE_MtoR_16, "movbe 1(%%rax), %%r9w") 71 72int main ( void ) 73{ 74 test_MOVBE_RtoM_64(); 75 test_MOVBE_RtoM_32(); 76 test_MOVBE_RtoM_16(); 77 test_MOVBE_MtoR_64(); 78 test_MOVBE_MtoR_32(); 79 test_MOVBE_MtoR_16(); 80 return 0; 81} 82