<s> import os , random , string , struct <EOL> import py <EOL> from pypy . jit . backend . x86 import ri386 as i386 <EOL> from pypy . jit . backend . x86 . ri386setup import all_instructions <EOL> from pypy . tool . udir import udir <EOL> INPUTNAME = str ( udir . join ( '<STR_LIT>' ) ) <EOL> FILENAME = str ( udir . join ( '<STR_LIT>' ) ) <EOL> BEGIN_TAG = '<STR_LIT>' <EOL> END_TAG = '<STR_LIT>' <EOL> COUNT1 = <NUM_LIT:15> <EOL> COUNT2 = <NUM_LIT> <EOL> sizes = { <EOL> i386 . EAX : <NUM_LIT:4> , <EOL> i386 . ECX : <NUM_LIT:4> , <EOL> i386 . EDX : <NUM_LIT:4> , <EOL> i386 . EBX : <NUM_LIT:4> , <EOL> i386 . ESP : <NUM_LIT:4> , <EOL> i386 . EBP : <NUM_LIT:4> , <EOL> i386 . ESI : <NUM_LIT:4> , <EOL> i386 . EDI : <NUM_LIT:4> , <EOL> i386 . AL : <NUM_LIT:1> , <EOL> i386 . CL : <NUM_LIT:1> , <EOL> i386 . DL : <NUM_LIT:1> , <EOL> i386 . BL : <NUM_LIT:1> , <EOL> i386 . AH : <NUM_LIT:1> , <EOL> i386 . CH : <NUM_LIT:1> , <EOL> i386 . DH : <NUM_LIT:1> , <EOL> i386 . BH : <NUM_LIT:1> , <EOL> i386 . REG : <NUM_LIT:4> , <EOL> i386 . MODRM : <NUM_LIT:4> , <EOL> i386 . MODRM64 : <NUM_LIT:8> , <EOL> i386 . IMM32 : <NUM_LIT:4> , <EOL> i386 . REG8 : <NUM_LIT:1> , <EOL> i386 . MODRM8 : <NUM_LIT:1> , <EOL> i386 . IMM8 : <NUM_LIT:1> , <EOL> i386 . IMM16 : <NUM_LIT:2> , <EOL> i386 . REL32 : <NUM_LIT:4> , <EOL> } <EOL> suffixes = { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT:b>' , <NUM_LIT:2> : '<STR_LIT:w>' , <NUM_LIT:4> : '<STR_LIT:l>' } <EOL> def reg_tests ( ) : <EOL> return i386 . registers [ : ] <EOL> def reg8_tests ( ) : <EOL> return i386 . registers8 [ : ] <EOL> def imm8_tests ( ) : <EOL> v = [ - <NUM_LIT> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> ] + [ random . randrange ( - <NUM_LIT> , <NUM_LIT> ) for i in range ( COUNT1 ) ] <EOL> return map ( i386 . imm8 , v ) <EOL> def imm16_tests ( ) : <EOL> v = [ - <NUM_LIT> , <NUM_LIT> ] + [ random . randrange ( - <NUM_LIT> , - <NUM_LIT> ) for i in range ( COUNT1 ) ] + [ random . randrange ( <NUM_LIT> , <NUM_LIT> ) for i in range ( COUNT1 ) ] <EOL> return map ( i386 . imm16 , v ) <EOL> def imm32_tests ( ) : <EOL> v = ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT:255> ] + <EOL> [ random . randrange ( <NUM_LIT:0> , <NUM_LIT> ) << <NUM_LIT:16> | <EOL> random . randrange ( <NUM_LIT:0> , <NUM_LIT> ) for i in range ( COUNT1 ) ] + <EOL> [ random . randrange ( <NUM_LIT> , <NUM_LIT> ) for i in range ( COUNT1 ) ] ) <EOL> return map ( i386 . imm32 , filter ( lambda x : x < - <NUM_LIT> or x >= <NUM_LIT> , v ) ) <EOL> def pick1 ( memSIB , cache = [ ] ) : <EOL> base = random . choice ( [ None , None , None ] + i386 . registers ) <EOL> index = random . choice ( [ None , None , None ] + i386 . registers ) <EOL> if index is i386 . esp : index = None <EOL> scale = random . randrange ( <NUM_LIT:0> , <NUM_LIT:4> ) <EOL> if not cache : <EOL> cache [ : ] = [ x . value for x in imm8_tests ( ) + imm32_tests ( ) ] + [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> random . shuffle ( cache ) <EOL> offset = cache . pop ( ) <EOL> if base is None and scale == <NUM_LIT:0> : <EOL> base = index <EOL> index = None <EOL> return memSIB ( base , index , scale , offset ) <EOL> def modrm_tests ( ) : <EOL> return i386 . registers + [ pick1 ( i386 . memSIB ) for i in range ( COUNT2 ) ] <EOL> def modrm_noreg_tests ( ) : <EOL> return [ pick1 ( i386 . memSIB ) for i in range ( COUNT2 ) ] <EOL> def modrm64_tests ( ) : <EOL> return [ pick1 ( i386 . memSIB64 ) for i in range ( COUNT2 ) ] <EOL> def xmm_tests ( ) : <EOL> return i386 . xmm_registers <EOL> def modrm8_tests ( ) : <EOL> return i386 . registers8 + [ pick1 ( i386 . memSIB8 ) for i in range ( COUNT2 ) ] <EOL> tests = { <EOL> i386 . EAX : lambda : [ i386 . eax ] , <EOL> i386 . ECX : lambda : [ i386 . ecx ] , <EOL> i386 . EDX : lambda : [ i386 . edx ] , <EOL> i386 . EBX : lambda : [ i386 . ebx ] , <EOL> i386 . ESP : lambda : [ i386 . esp ] , <EOL> i386 . EBP : lambda : [ i386 . ebp ] , <EOL> i386 . ESI : lambda : [ i386 . esi ] , <EOL> i386 . EDI : lambda : [ i386 . edi ] , <EOL> i386 . AL : lambda : [ i386 . al ] , <EOL> i386 . CL : lambda : [ i386 . cl ] , <EOL> i386 . DL : lambda : [ i386 . dl ] , <EOL> i386 . BL : lambda : [ i386 . bl ] , <EOL> i386 . AH : lambda : [ i386 . ah ] , <EOL> i386 . CH : lambda : [ i386 . ch ] , <EOL> i386 . DH : lambda : [ i386 . dh ] , <EOL> i386 . BH : lambda : [ i386 . bh ] , <EOL> i386 . REG : reg_tests , <EOL> i386 . MODRM : modrm_tests , <EOL> i386 . MODRM64 : modrm64_tests , <EOL> i386 . XMMREG : xmm_tests , <EOL> i386 . IMM32 : imm32_tests , <EOL> i386 . REG8 : reg8_tests , <EOL> i386 . MODRM8 : modrm8_tests , <EOL> i386 . IMM8 : imm8_tests , <EOL> i386 . IMM16 : imm16_tests , <EOL> i386 . REL32 : lambda : [ ] , <EOL> } <EOL> def run_test ( instrname , instr , args_lists ) : <EOL> global labelcount <EOL> instrname = instr . as_alias or instrname <EOL> labelcount = <NUM_LIT:0> <EOL> oplist = [ ] <EOL> g = open ( INPUTNAME , '<STR_LIT:w>' ) <EOL> g . write ( '<STR_LIT>' % BEGIN_TAG ) <EOL> for args in args_lists : <EOL> suffix = "<STR_LIT>" <EOL> all = instr . as_all_suffixes <EOL> for m , extra in args : <EOL> if m in ( i386 . MODRM , i386 . MODRM8 ) or all : <EOL> if instrname != '<STR_LIT>' : <EOL> suffix = suffixes [ sizes [ m ] ] + suffix <EOL> if m is i386 . MODRM64 and instrname in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> suffix = '<STR_LIT:l>' <EOL> following = "<STR_LIT>" <EOL> if instr . indirect : <EOL> suffix = "<STR_LIT>" <EOL> if args [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == i386 . REL32 : <EOL> labelcount += <NUM_LIT:1> <EOL> following = "<STR_LIT>" % labelcount <EOL> elif args [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] in ( i386 . IMM8 , i386 . IMM32 ) : <EOL> args = list ( args ) <EOL> args [ - <NUM_LIT:1> ] = ( "<STR_LIT>" , args [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> else : <EOL> suffix += "<STR_LIT>" <EOL> k = - <NUM_LIT:1> <EOL> else : <EOL> k = len ( args ) <EOL> for m , extra in args [ : k ] : <EOL> assert m != i386 . REL32 <EOL> ops = [ extra . assembler ( ) for m , extra in args ] <EOL> ops . reverse ( ) <EOL> op = '<STR_LIT>' % ( instrname , suffix , string . join ( ops , "<STR_LIT:U+002CU+0020>" ) , <EOL> following ) <EOL> g . write ( '<STR_LIT>' % op ) <EOL> oplist . append ( op ) <EOL> g . write ( '<STR_LIT>' % END_TAG ) <EOL> g . close ( ) <EOL> os . system ( '<STR_LIT>' % ( INPUTNAME , FILENAME ) ) <EOL> try : <EOL> f = open ( FILENAME , '<STR_LIT:rb>' ) <EOL> except IOError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> i = string . find ( data , BEGIN_TAG ) <EOL> assert i >= <NUM_LIT:0> <EOL> j = string . find ( data , END_TAG , i ) <EOL> assert j >= <NUM_LIT:0> <EOL> as_code = data [ i + len ( BEGIN_TAG ) + <NUM_LIT:1> : j ] <EOL> return oplist , as_code <EOL> def rec_test_all ( instrname , modes , args = [ ] ) : <EOL> if modes : <EOL> m = modes [ <NUM_LIT:0> ] <EOL> if instrname . startswith ( '<STR_LIT:F>' ) and m is i386 . MODRM : <EOL> lst = modrm_noreg_tests ( ) <EOL> else : <EOL> lst = tests [ m ] ( ) <EOL> random . shuffle ( lst ) <EOL> result = [ ] <EOL> for extra in lst : <EOL> result += rec_test_all ( instrname , modes [ <NUM_LIT:1> : ] , args + [ ( m , extra ) ] ) <EOL> return result <EOL> else : <EOL> if instrname == "<STR_LIT>" : <EOL> if ( ( args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] in ( i386 . eax , i386 . al ) ) <EOL> and args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . assembler ( ) . lstrip ( '<STR_LIT:->' ) . isdigit ( ) ) : <EOL> return [ ] <EOL> if ( ( args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] in ( i386 . eax , i386 . al ) ) <EOL> and args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . assembler ( ) . lstrip ( '<STR_LIT:->' ) . isdigit ( ) ) : <EOL> return [ ] <EOL> if instrname == "<STR_LIT>" : <EOL> return [ ] <EOL> if instrname == "<STR_LIT>" : <EOL> if ( args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . __class__ != i386 . MODRM or <EOL> args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . is_register ( ) ) : <EOL> return [ ] <EOL> if instrname == "<STR_LIT>" : <EOL> if args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . value == <NUM_LIT:3> : <EOL> return [ ] <EOL> if instrname in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . assembler ( ) == '<STR_LIT>' : <EOL> return [ ] <EOL> if instrname in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . width == <NUM_LIT:4> : <EOL> return [ ] <EOL> if instrname == "<STR_LIT>" : <EOL> if ( args [ <NUM_LIT:0> ] != args [ <NUM_LIT:1> ] and <EOL> isinstance ( args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , i386 . REG ) and <EOL> isinstance ( args [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , i386 . REG ) ) : <EOL> return [ ] <EOL> if instrname . endswith ( '<STR_LIT>' ) : <EOL> return [ ] <EOL> return [ args ] <EOL> def hexdump ( s ) : <EOL> return string . join ( [ "<STR_LIT>" % ord ( c ) for c in s ] , "<STR_LIT:U+0020>" ) <EOL> class CodeChecker ( i386 . I386CodeBuilder ) : <EOL> def __init__ ( self , expected ) : <EOL> self . expected = expected <EOL> self . index = <NUM_LIT:0> <EOL> def begin ( self , op ) : <EOL> self . op = op <EOL> self . instrindex = self . index <EOL> def write ( self , data ) : <EOL> end = self . index + len ( data ) <EOL> if data != self . expected [ self . index : end ] : <EOL> print self . op <EOL> print "<STR_LIT>" , hexdump ( self . expected [ self . instrindex : self . index ] + data ) + "<STR_LIT>" <EOL> print "<STR_LIT>" , hexdump ( self . expected [ self . instrindex : end ] ) + "<STR_LIT>" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . index += len ( data ) <EOL> def complete_test ( instrname , instr ) : <EOL> print "<STR_LIT>" % ( instrname , len ( instr . modes ) ) <EOL> ilist = [ ] <EOL> for modes in instr . modes : <EOL> ilist += rec_test_all ( instrname , modes ) <EOL> oplist , as_code = run_test ( instrname , instr , ilist ) <EOL> cc = CodeChecker ( as_code ) <EOL> for op , args in zip ( oplist , ilist ) : <EOL> if op : <EOL> cc . begin ( op ) <EOL> getattr ( cc , instrname ) ( * [ extra for m , extra in args ] ) <EOL> def complete_tests ( ) : <EOL> FORBIDDEN = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> items = i386 . __dict__ . items ( ) <EOL> items . sort ( ) <EOL> for key , value in items : <EOL> if isinstance ( value , i386 . Instruction ) : <EOL> if key in FORBIDDEN : <EOL> print "<STR_LIT>" , key <EOL> else : <EOL> complete_test ( key , value ) <EOL> print "<STR_LIT>" <EOL> def test_auto ( ) : <EOL> import os <EOL> g = os . popen ( '<STR_LIT>' ) <EOL> data = g . read ( ) <EOL> g . close ( ) <EOL> if not data . startswith ( '<STR_LIT>' ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> def do_test ( name , insn ) : <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> complete_test ( name , insn ) <EOL> items = all_instructions . items ( ) <EOL> items . sort ( ) <EOL> for key , value in items : <EOL> yield do_test , key , value <EOL> del key , value <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> complete_tests ( ) </s>
