<commit_before>! Copyright (C) 2008, 2010 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: tools.disassembler namespaces combinators alien
alien.syntax alien.c-types lexer parser kernel sequences layouts
math math.order alien.libraries math.parser system make fry
arrays libc destructors tools.memory tools.disassembler.utils
tools.disassembler.private splitting alien.data classes.struct ;
IN: tools.disassembler.udis

<<
"libudis86" {
    { [ os macosx? ] [ "libudis86.0.dylib" ] }
    { [ os unix? ] [ "libudis86.so.0" ] }
    { [ os windows? ] [ "libudis86.dll" ] }
} cond cdecl add-library
>>

LIBRARY: libudis86

STRUCT: ud_operand
    { type int }
    { size uchar }
    { lval ulonglong }
    { base int }
    { index int }
    { offset uchar }
    { scale uchar } ;

STRUCT: ud
    { inp_hook void* }
    { inp_curr uchar }
    { inp_fill uchar }
    { inp_file void* }
    { inp_ctr uchar }
    { inp_buff c-string }
    { inp_buff_end c-string }
    { inp_end uchar }
    { translator void* }
    { insn_offset ulonglong }
    { insn_hexcode char[32] }
    { insn_buffer char[64] }
    { insn_fill uint }
    { dis_mode uchar }
    { pc ulonglong }
    { vendor uchar }
    { mapen void* }
    { mnemonic int }
    { operand ud_operand[3] }
    { error uchar }
    { pfx_rex uchar }
    { pfx_seg uchar }
    { pfx_opr uchar }
    { pfx_adr uchar }
    { pfx_lock uchar }
    { pfx_rep uchar }
    { pfx_repe uchar }
    { pfx_repne uchar }
    { pfx_insn uchar }
    { default64 uchar }
    { opr_mode uchar }
    { adr_mode uchar }
    { br_far uchar }
    { br_near uchar }
    { implicit_addr uchar }
    { c1 uchar }
    { c2 uchar }
    { c3 uchar }
    { inp_cache uchar[256] }
    { inp_sess uchar[64] }
    { have_modrm uchar }
    { modrm uchar }
    { user_opaque_data void* }
    { itab_entry void* }
    { le void* } ;

FUNCTION: void ud_translate_intel ( ud* u ) ;
FUNCTION: void ud_translate_att ( ud* u ) ;

: UD_SYN_INTEL ( -- addr ) &: ud_translate_intel ; inline
: UD_SYN_ATT ( -- addr ) &: ud_translate_att ; inline

CONSTANT: UD_EOI          -1
CONSTANT: UD_INP_CACHE_SZ 32
CONSTANT: UD_VENDOR_AMD   0
CONSTANT: UD_VENDOR_INTEL 1

FUNCTION: void ud_init ( ud* u ) ;
FUNCTION: void ud_set_mode ( ud* u, uchar mode ) ;
FUNCTION: void ud_set_pc ( ud* u, ulonglong pc ) ;
FUNCTION: void ud_set_input_buffer ( ud* u, c-string offset, size_t size ) ;
FUNCTION: void ud_set_vendor ( ud* u, uint vendor ) ;
FUNCTION: void ud_set_syntax ( ud* u, void* syntax ) ;
FUNCTION: void ud_input_skip ( ud* u, size_t size ) ;
FUNCTION: int ud_input_end ( ud* u ) ;
FUNCTION: uint ud_decode ( ud* u ) ;
FUNCTION: uint ud_disassemble ( ud* u ) ;
FUNCTION: c-string ud_insn_asm ( ud* u ) ;
FUNCTION: void* ud_insn_ptr ( ud* u ) ;
FUNCTION: ulonglong ud_insn_off ( ud* u ) ;
FUNCTION: c-string ud_insn_hex ( ud* u ) ;
FUNCTION: uint ud_insn_len ( ud* u ) ;
FUNCTION: c-string ud_lookup_mnemonic ( int c ) ;

: <ud> ( -- ud )
    ud malloc-struct &free
    dup ud_init
    dup cell-bits ud_set_mode
    dup UD_SYN_INTEL ud_set_syntax ;

: with-ud ( ..a quot: ( ..a ud -- ..b ) -- ..b )
    [ [ [ <ud> ] dip call ] with-destructors ] with-code-blocks ; inline

SINGLETON: udis-disassembler

: buf/len ( from to -- buf len ) [ drop <alien> ] [ swap - ] 2bi ;

: resolve-call ( str -- str' ) "0x" split1-last [ resolve-xt append ] when* ;

: format-disassembly ( lines -- lines' )
    dup [ second length ] [ max ] map-reduce
    '[
        [
            [ first >hex cell 2 * CHAR: 0 pad-head % ": " % ]
            [ second _ CHAR: \s pad-tail % "  " % ]
            [ third resolve-call % ]
            tri
        ] "" make
    ] map ;<commit_msg>tools.disassembler.udis: 1k ought to be enough for anybody.<commit_after>! Copyright (C) 2008, 2010 <NAME>, <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: tools.disassembler namespaces combinators alien
alien.syntax alien.c-types lexer parser kernel sequences layouts
math math.order alien.libraries math.parser system make fry
arrays libc destructors tools.memory tools.disassembler.utils
tools.disassembler.private splitting alien.data classes.struct ;
IN: tools.disassembler.udis

<<
"libudis86" {
    { [ os macosx? ] [ "libudis86.0.dylib" ] }
    { [ os unix? ] [ "libudis86.so.0" ] }
    { [ os windows? ] [ "libudis86.dll" ] }
} cond cdecl add-library
>>

LIBRARY: libudis86

TYPEDEF: void ud

FUNCTION: void ud_translate_intel ( ud* u ) ;
FUNCTION: void ud_translate_att ( ud* u ) ;

: UD_SYN_INTEL ( -- addr ) &: ud_translate_intel ; inline
: UD_SYN_ATT ( -- addr ) &: ud_translate_att ; inline

CONSTANT: UD_EOI          -1
CONSTANT: UD_INP_CACHE_SZ 32
CONSTANT: UD_VENDOR_AMD   0
CONSTANT: UD_VENDOR_INTEL 1

FUNCTION: void ud_init ( ud* u ) ;
FUNCTION: void ud_set_mode ( ud* u, uchar mode ) ;
FUNCTION: void ud_set_pc ( ud* u, ulonglong pc ) ;
FUNCTION: void ud_set_input_buffer ( ud* u, c-string offset, size_t size ) ;
FUNCTION: void ud_set_vendor ( ud* u, uint vendor ) ;
FUNCTION: void ud_set_syntax ( ud* u, void* syntax ) ;
FUNCTION: void ud_input_skip ( ud* u, size_t size ) ;
FUNCTION: int ud_input_end ( ud* u ) ;
FUNCTION: uint ud_decode ( ud* u ) ;
FUNCTION: uint ud_disassemble ( ud* u ) ;
FUNCTION: c-string ud_insn_asm ( ud* u ) ;
FUNCTION: void* ud_insn_ptr ( ud* u ) ;
FUNCTION: ulonglong ud_insn_off ( ud* u ) ;
FUNCTION: c-string ud_insn_hex ( ud* u ) ;
FUNCTION: uint ud_insn_len ( ud* u ) ;
FUNCTION: c-string ud_lookup_mnemonic ( int c ) ;

: <ud> ( -- ud )
    1,000 malloc &free
    dup ud_init
    dup cell-bits ud_set_mode
    dup UD_SYN_INTEL ud_set_syntax ;

: with-ud ( ..a quot: ( ..a ud -- ..b ) -- ..b )
    [ [ [ <ud> ] dip call ] with-destructors ] with-code-blocks ; inline

SINGLETON: udis-disassembler

: buf/len ( from to -- buf len ) [ drop <alien> ] [ swap - ] 2bi ;

: resolve-call ( str -- str' ) "0x" split1-last [ resolve-xt append ] when* ;

: format-disassembly ( lines -- lines' )
    dup [ second length ] [ max ] map-reduce
    '[
        [
            [ first >hex cell 2 * CHAR: 0 pad-head % ": " % ]
            [ second _ CHAR: \s pad-tail % "  " % ]
            [ third resolve-call % ]
            tri
        ] "" make
    ] map ;
{'fraction_non_alphanumeric': 0.12493962324907422, 'fraction_numerical': 0.012880373530832394, 'mean_word_length': 2.724220623501199, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1071152', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2347, 'n_words': 618}