<s> """<STR_LIT>""" <EOL> import copy <EOL> import logging <EOL> from pyparsing import alphanums <EOL> from pyparsing import alphas <EOL> from pyparsing import Combine <EOL> from pyparsing import Literal <EOL> from pyparsing import nums <EOL> from pyparsing import Optional <EOL> from pyparsing import Or <EOL> from pyparsing import Suppress <EOL> from pyparsing import Word <EOL> from barf . core . reil . reil import ReilEmptyOperand <EOL> from barf . core . reil . reil import ReilImmediateOperand <EOL> from barf . core . reil . reil import ReilInstructionBuilder <EOL> from barf . core . reil . reil import ReilMnemonic <EOL> from barf . core . reil . reil import ReilRegisterOperand <EOL> logger = logging . getLogger ( __name__ ) <EOL> def parse_operand ( string , location , tokens ) : <EOL> """<STR_LIT>""" <EOL> sizes = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:64> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:32> , <EOL> "<STR_LIT>" : <NUM_LIT:16> , <EOL> "<STR_LIT>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> } <EOL> if "<STR_LIT>" in tokens : <EOL> imm_str = "<STR_LIT>" . join ( tokens [ "<STR_LIT>" ] ) <EOL> base = <NUM_LIT:16> if imm_str . startswith ( "<STR_LIT>" ) or imm_str . startswith ( "<STR_LIT>" ) else <NUM_LIT:10> <EOL> immediate = int ( imm_str , base ) <EOL> oprnd = ReilImmediateOperand ( immediate ) <EOL> if "<STR_LIT>" in tokens : <EOL> if tokens [ "<STR_LIT>" ] in [ "<STR_LIT:e>" , "<STR_LIT>" ] : <EOL> oprnd = ReilEmptyOperand ( ) <EOL> oprnd . size = <NUM_LIT:0> <EOL> else : <EOL> name = tokens [ "<STR_LIT>" ] <EOL> oprnd = ReilRegisterOperand ( name ) <EOL> if "<STR_LIT:size>" in tokens : <EOL> size = int ( sizes [ tokens [ "<STR_LIT:size>" ] ] ) <EOL> oprnd . size = size <EOL> return [ oprnd ] <EOL> def parse_instruction ( string , location , tokens ) : <EOL> """<STR_LIT>""" <EOL> mnemonic = ReilMnemonic . from_string ( tokens [ "<STR_LIT>" ] ) <EOL> oprnd1 = tokens [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> oprnd2 = tokens [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> oprnd3 = tokens [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> ins_builder = ReilInstructionBuilder ( ) <EOL> return ins_builder . build ( mnemonic , oprnd1 , oprnd2 , oprnd3 ) <EOL> comma = Literal ( "<STR_LIT:U+002C>" ) <EOL> hex_num = Combine ( "<STR_LIT>" + Word ( "<STR_LIT>" ) ) <EOL> dec_num = Word ( "<STR_LIT>" ) <EOL> immediate = Optional ( "<STR_LIT:->" ) + Or ( [ hex_num , dec_num ] ) <EOL> register = Word ( alphanums ) <EOL> mnemonic = Or ( [ <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT:str>" ) , <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> ] ) <EOL> size = Or ( [ <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> Literal ( "<STR_LIT>" ) , <EOL> ] ) ( "<STR_LIT:size>" ) <EOL> operand = ( Optional ( size ) + Or ( [ <EOL> immediate ( "<STR_LIT>" ) , <EOL> register ( "<STR_LIT>" ) <EOL> ] ) ) . setParseAction ( parse_operand ) <EOL> instruction = ( <EOL> mnemonic ( "<STR_LIT>" ) + <EOL> Suppress ( "<STR_LIT:[>" ) + <EOL> operand ( "<STR_LIT>" ) + Suppress ( comma ) + <EOL> operand ( "<STR_LIT>" ) + Suppress ( comma ) + <EOL> operand ( "<STR_LIT>" ) + <EOL> Suppress ( "<STR_LIT:]>" ) <EOL> ) . setParseAction ( parse_instruction ) <EOL> class ReilParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _cache = { } <EOL> def parse ( self , instrs ) : <EOL> """<STR_LIT>""" <EOL> instrs_reil = [ ] <EOL> try : <EOL> for instr in instrs : <EOL> instr_lower = instr . lower ( ) <EOL> if not instr_lower in self . _cache : <EOL> self . _cache [ instr_lower ] = instruction . parseString ( <EOL> instr_lower ) [ <NUM_LIT:0> ] <EOL> instrs_reil += [ copy . deepcopy ( self . _cache [ instr_lower ] ) ] <EOL> except : <EOL> instr_reil = None <EOL> error_msg = "<STR_LIT>" <EOL> logger . error ( error_msg , instr , exc_info = True ) <EOL> return instrs_reil </s>
