text
stringlengths 30
1.67M
|
|---|
<s> package erjang . m . ets ; import com . trifork . clj_ds . IMapEntry ; import com . trifork . clj_ds . IPersistentCollection ; import com . trifork . clj_ds . IPersistentMap ; import com . trifork . clj_ds . ISeq ; import com . trifork . clj_ds . PersistentHashMap ; import com . trifork . clj_ds . PersistentList ; import com . trifork . clj_ds . PersistentTreeMap ; public class PersistentBag extends Object implements IPersistentBag { public static PersistentBag EMPTY = new PersistentBag ( PersistentHashMap . EMPTY , <NUM_LIT:0> ) ; IPersistentMap impl ; int count ; private PersistentBag ( IPersistentMap impl , int count ) { this . impl = impl ; this . count = count ; } @ Override public IPersistentBag disjoin ( Object element ) { Integer val = ( Integer ) impl . valAt ( element ) ; if ( val == null ) { return this ; } else if ( val == <NUM_LIT:1> ) { try { return new PersistentBag ( impl . without ( element ) , count - <NUM_LIT:1> ) ; } catch ( Exception e ) { throw new Error ( e ) ; } } else { return new PersistentBag ( impl . assoc ( element , val - <NUM_LIT:1> ) , count - <NUM_LIT:1> ) ; } } @ Override public IPersistentCollection cons ( Object element ) { Integer val = ( Integer ) impl . valAt ( element ) ; if ( val == null ) { return new PersistentBag ( impl . assoc ( element , <NUM_LIT:1> ) , count + <NUM_LIT:1> ) ; } else { return new PersistentBag ( impl . assoc ( element , val + <NUM_LIT:1> ) , count + <NUM_LIT:1> ) ; } } @ Override public int count ( ) { return count ; } @ Override public IPersistentCollection empty ( ) { return new PersistentBag ( PersistentTreeMap . EMPTY , <NUM_LIT:0> ) ; } @ Override public boolean equiv ( Object arg0 ) { throw new UnsupportedOperationException ( ) ; } class ElemSeq implements ISeq { private final int total ; private final int pos ; private final ISeq pairs ; @ Override public ISeq cons ( Object arg0 ) { return null ; } @ Override public Object first ( ) { IMapEntry elem_count_pair = ( IMapEntry ) pairs . first ( ) ; return elem_count_pair . getKey ( ) ; } @ Override public ISeq more ( ) { ISeq next = next ( ) ; if ( next == null ) return PersistentList . EMPTY ; return next ; } @ Override public ISeq next ( ) { IMapEntry elem_count_pair = ( IMapEntry ) pairs . first ( ) ; int count = ( Integer ) elem_count_pair . getValue ( ) ; if ( count == pos + <NUM_LIT:1> ) { ISeq succ = pairs . next ( ) ; if ( succ == null ) { return null ; } else { return new ElemSeq ( succ , <NUM_LIT:0> , total - <NUM_LIT:1> ) ; } } else { return new ElemSeq ( pairs , pos + <NUM_LIT:1> , total - <NUM_LIT:1> ) ; } } @ Override public int count ( ) { return total ; } @ Override public IPersistentCollection empty ( ) { return PersistentBag . EMPTY ; } @ Override public boolean equiv ( Object arg0 ) { throw new UnsupportedOperationException ( ) ; } @ Override public ISeq seq ( ) { return this ; } ElemSeq ( ISeq pairs , int count , int total ) { this . pairs = pairs ; this . pos = count ; this . total = total ; } } @ Override public ISeq seq ( ) { if ( count ( ) == <NUM_LIT:0> ) return null ; return new ElemSeq ( impl . seq ( ) , <NUM_LIT:0> , count ) ; } } </s>
|
<s> package erjang . m . file ; import erjang . BIF ; import erjang . EAtom ; import erjang . ENative ; import erjang . EObject ; public class Native extends ENative { public static final EAtom am_latin1 = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_utf8 = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_unicode = EAtom . intern ( "<STR_LIT>" ) ; @ BIF public static EObject native_name_encoding ( ) { return am_utf8 ; } } </s>
|
<s> package erjang . m . erlang ; import java . nio . ByteBuffer ; import java . util . ArrayList ; import java . util . List ; import java . util . logging . Level ; import java . util . logging . Logger ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAtom ; import erjang . EInternalPID ; import erjang . EInternalPort ; import erjang . EObject ; import erjang . EPort ; import erjang . EProc ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . EString ; import erjang . ETask ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . ETuple3 ; import erjang . driver . EDriver ; import erjang . driver . EDriverInstance ; import erjang . driver . EDriverTask ; import erjang . driver . EExecDriverTask ; import erjang . driver . EFDDriverTask ; import erjang . driver . ESpawnDriverTask ; import erjang . driver . tcp_inet . TCPINet ; public class ErlPort { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; public static final EAtom am_fd = EAtom . intern ( "<STR_LIT>" ) ; private static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer . allocate ( <NUM_LIT:0> ) ; public static EAtom am_spawn = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_spawn_driver = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_spawn_executable = EAtom . intern ( "<STR_LIT>" ) ; @ BIF static EObject port_connect ( EProc proc , EObject arg_port , EObject arg_pid ) throws Pausable { EInternalPort iport ; EInternalPID ipid ; if ( ( iport = arg_port . testInternalPort ( ) ) == null ) { EAtom port_name = arg_port . testAtom ( ) ; EObject resolved ; if ( port_name != null && ( resolved = ERT . whereis ( port_name ) ) != ERT . am_undefined && ( iport = resolved . testInternalPort ( ) ) != null ) { } else { throw ERT . badarg ( arg_port , arg_pid ) ; } } if ( ( ipid = arg_pid . testInternalPID ( ) ) == null ) { EAtom pid_name = arg_pid . testAtom ( ) ; EObject resolved ; if ( pid_name != null && ( resolved = ERT . whereis ( pid_name ) ) != ERT . am_undefined && ( ipid = resolved . testInternalPID ( ) ) != null ) { } else { throw ERT . badarg ( arg_port , arg_pid ) ; } } iport . set_owner ( ipid ) ; ipid . task ( ) . link_to ( iport ) ; return ERT . TRUE ; } @ BIF static EObject port_command ( EProc proc , EObject port , EObject data ) throws Pausable { EInternalPort p = port . testInternalPort ( ) ; if ( log . isLoggable ( Level . FINER ) ) log . finer ( "<STR_LIT>" + port + "<STR_LIT:U+002CU+0020>" + data ) ; if ( p == null ) { port = ERT . whereis ( port ) ; if ( port == ERT . am_undefined ) port = null ; else p = port . testInternalPort ( ) ; } List < ByteBuffer > ovec = new ArrayList < ByteBuffer > ( ) ; if ( p == null || ! data . collectIOList ( ovec ) ) { if ( log . isLoggable ( Level . WARNING ) ) { log . warning ( "<STR_LIT>" + p ) ; } throw ERT . badarg ( port , data ) ; } ByteBuffer [ ] out = new ByteBuffer [ ovec . size ( ) ] ; ovec . toArray ( out ) ; if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" ) ; TCPINet . dump_buffer ( out ) ; } p . command ( proc . self_handle ( ) , out ) ; return ERT . TRUE ; } @ BIF static EObject port_command ( EProc proc , EObject port , EObject data , EObject options ) throws Pausable { EInternalPort p = port . testInternalPort ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + port + "<STR_LIT:U+002CU+0020>" + data ) ; if ( p == null ) { port = ERT . whereis ( port ) ; if ( port == ERT . am_undefined ) port = null ; else p = port . testInternalPort ( ) ; } List < ByteBuffer > ovec = new ArrayList < ByteBuffer > ( ) ; if ( p == null || ! data . collectIOList ( ovec ) ) { if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + p ) ; } throw ERT . badarg ( port , data ) ; } ByteBuffer [ ] out = new ByteBuffer [ ovec . size ( ) ] ; ovec . toArray ( out ) ; EDriverInstance . dump_buffer ( log , "<STR_LIT>" , out ) ; p . command ( proc . self_handle ( ) , out ) ; return ERT . TRUE ; } @ BIF static EObject port_control ( EProc proc , EObject port , EObject operation , EObject data ) throws Pausable { try { return port_control0 ( proc , port , operation , data ) ; } catch ( RuntimeException e ) { e . printStackTrace ( ) ; throw e ; } catch ( Error e ) { e . printStackTrace ( ) ; throw e ; } } static EObject port_control0 ( EProc proc , EObject port , EObject operation , EObject data ) throws Pausable { EInternalPort p = port . testInternalPort ( ) ; if ( p == null ) { port = ERT . whereis ( port ) ; if ( port == ERT . am_undefined ) port = null ; p = port . testInternalPort ( ) ; } ESmall op = operation . testSmall ( ) ; List < ByteBuffer > ovec = new ArrayList < ByteBuffer > ( ) ; if ( p == null || op == null || ! data . collectIOList ( ovec ) ) { throw ERT . badarg ( port , operation , data ) ; } ByteBuffer cmd = flatten ( ovec ) ; return p . control ( proc , op . value , cmd ) ; } private static ByteBuffer flatten ( List < ByteBuffer > ovec ) { if ( ovec . size ( ) == <NUM_LIT:0> ) { return EMPTY_BYTEBUFFER ; } else if ( ovec . size ( ) == <NUM_LIT:1> ) { return ovec . get ( <NUM_LIT:0> ) ; } int len = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < ovec . size ( ) ; i ++ ) { len += ovec . get ( i ) . remaining ( ) ; } ByteBuffer res = ByteBuffer . allocate ( len ) ; for ( ByteBuffer bb : ovec ) { res . put ( bb ) ; } res . rewind ( ) ; return res ; } @ BIF static EObject port_call ( EProc proc , EObject port , EObject operation , EObject data ) throws Pausable { EInternalPort p = port . testInternalPort ( ) ; if ( p == null ) { port = ERT . whereis ( port ) ; if ( port == ERT . am_undefined ) port = null ; p = port . testInternalPort ( ) ; } ESmall op = operation . testSmall ( ) ; if ( p == null || op == null ) { throw ERT . badarg ( port , operation , data ) ; } return p . call ( proc , op . value , data ) ; } @ BIF static EPort open_port ( EProc proc , EObject portName , EObject portSetting ) throws Pausable { ETuple t ; if ( ( t = portName . testTuple ( ) ) == null ) throw ERT . badarg ( portName , portSetting ) ; ETask < ? extends EPort > task = null ; ETuple2 name ; ETuple3 name3 ; if ( ( name = ETuple2 . cast ( t ) ) != null ) { EAtom am = name . elem2 . testAtom ( ) ; EString command = ( am == null ) ? ( EString ) EString . make ( name . elem2 ) : EString . fromString ( am . getName ( ) ) ; if ( name . elem1 == am_spawn ) { EDriver drv = ERT . find_driver ( command ) ; if ( drv == null ) { task = new EExecDriverTask ( proc , name , command , portSetting ) ; } else { task = new ESpawnDriverTask ( proc , drv , command , portSetting ) ; } } else if ( name . elem1 == am_spawn_driver ) { EDriver drv = ERT . find_driver ( command ) ; if ( drv == null ) { throw ERT . badarg ( portName , portSetting ) ; } task = new ESpawnDriverTask ( proc , drv , command , portSetting ) ; } else if ( name . elem1 == am_spawn_executable ) { task = new EExecDriverTask ( proc , name , command , portSetting ) ; } } else if ( ( name3 = ETuple3 . cast ( portName ) ) != null && name3 . elem1 == am_fd ) { ESmall in = name3 . elem2 . testSmall ( ) ; ESmall out = name3 . elem3 . testSmall ( ) ; if ( in == null || out == null ) throw ERT . badarg ( portName , portSetting ) ; task = new EFDDriverTask ( proc , in . value , out . value , portSetting ) ; } if ( task != null ) { task . link_to ( proc ) ; ERT . run ( task ) ; return task . self_handle ( ) ; } throw ERT . badarg ( portName , portSetting ) ; } @ BIF static public EObject port_close ( EProc proc , EObject port ) throws Pausable { EPort p ; if ( ( p = port . testPort ( ) ) == null ) { EObject obj = ERT . whereis ( port ) ; if ( obj == ERT . am_undefined || ( ( p = obj . testPort ( ) ) == null ) ) { throw ERT . badarg ( port ) ; } } if ( ! p . isOpen ( ) ) { throw ERT . badarg ( port ) ; } proc . unlink ( p ) ; p . close ( ) ; return ERT . TRUE ; } @ BIF static public ESeq ports ( ) { return EDriverTask . all_ports ( ) ; } @ BIF static public EObject port_info ( EProc proc , EObject a1 , EObject a2 ) { EPort p = a1 . testPort ( ) ; EAtom spec = a2 . testAtom ( ) ; if ( p == null || spec == null ) throw ERT . badarg ( ) ; EObject info = p . port_info ( spec ) ; return info ; } @ BIF static public EObject port_set_data ( EObject port , EObject data ) { EPort p = id_or_name2port ( port ) ; if ( p == null ) { throw ERT . badarg ( port , data ) ; } p . set_data ( data ) ; return data ; } @ BIF static public EObject port_get_data ( EObject port ) { EPort p = id_or_name2port ( port ) ; if ( p == null ) { throw ERT . badarg ( port ) ; } return p . get_data ( ) ; } private static EPort id_or_name2port ( EObject port ) { EPort p = port . testPort ( ) ; if ( p != null ) return p ; EObject p2 = ERT . whereis ( port ) ; return p2 . testPort ( ) ; } } </s>
|
<s> package erjang . m . erlang ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAtom ; import erjang . EFun ; import erjang . EFunHandler ; import erjang . EModuleManager ; import erjang . EObject ; import erjang . EProc ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . FunID ; public class ErlFun { @ BIF public static EObject is_function ( EObject a1 , EObject a2 ) { return a1 . is_function ( a2 ) ; } @ BIF public static EObject function_exported ( EObject a1 , EObject a2 , EObject a3 ) { EAtom mod = a1 . testAtom ( ) ; EAtom fun = a2 . testAtom ( ) ; ESmall ary = a3 . testSmall ( ) ; return ERT . box ( EModuleManager . function_exported ( mod , fun , ary . value ) ) ; } @ BIF public static EObject fun_info ( EObject fun_arg , EObject spec_arg ) { EFun fun = fun_arg . testFunction ( ) ; EAtom spec = spec_arg . testAtom ( ) ; if ( fun == null || spec == null ) throw ERT . badarg ( fun_arg , spec_arg ) ; return fun . info ( spec ) ; } @ BIF public static ESeq fun_info ( EObject fun_arg ) { EFun fun = fun_arg . testFunction ( ) ; if ( fun == null ) throw ERT . badarg ( fun_arg ) ; ESeq res = ERT . NIL ; boolean is_local = fun . is_local ( ) ; res = res . cons ( fun . info ( ERT . am_type ) ) ; res = res . cons ( fun . info ( ERT . am_env ) ) ; res = res . cons ( fun . info ( ERT . am_arity ) ) ; res = res . cons ( fun . info ( ERT . am_name ) ) ; if ( is_local ) { res = res . cons ( fun . info ( ERT . am_uniq ) ) ; res = res . cons ( fun . info ( ERT . am_index ) ) ; res = res . cons ( fun . info ( ERT . am_new_uniq ) ) ; res = res . cons ( fun . info ( ERT . am_new_index ) ) ; } res = res . cons ( fun . info ( ERT . am_module ) ) ; if ( is_local ) { res = res . cons ( fun . info ( ERT . am_pid ) ) ; } return res ; } @ BIF public static EFun make_fun ( EObject m , EObject f , EObject a ) { EAtom mod = m . testAtom ( ) ; EAtom fun = f . testAtom ( ) ; ESmall arity = a . testSmall ( ) ; if ( mod == null || fun == null || arity == null ) { throw ERT . badarg ( m , f , a ) ; } return EModuleManager . resolve ( new FunID ( mod , fun , arity . value ) ) ; } } </s>
|
<s> package erjang . m . erlang ; import erjang . BIF ; import erjang . EObject ; import erjang . ECons ; import erjang . ESeq ; import erjang . ERT ; import java . util . LinkedList ; public class ErlList { @ BIF ( name = "<STR_LIT:-->" ) public static ECons listDiff ( EObject a1 , EObject a2 ) { ESeq l1 , l2 ; if ( ( l1 = a1 . testSeq ( ) ) == null || ( l2 = a2 . testSeq ( ) ) == null ) throw ERT . badarg ( a1 , a2 ) ; if ( l1 . isNil ( ) ) return l1 ; final EObject [ ] tmp = l1 . toArray ( ) ; int tmp_start = <NUM_LIT:0> ; for ( ESeq cur = l2 ; ! cur . isNil ( ) ; cur = cur . tail ( ) ) { EObject elm = cur . head ( ) ; for ( int i = tmp_start ; i < tmp . length ; i ++ ) { if ( tmp [ i ] != null && tmp [ i ] . equalsExactly ( elm ) ) { tmp [ i ] = null ; if ( i == tmp_start ) { do { tmp_start ++ ; } while ( tmp_start < tmp . length && tmp [ tmp_start ] == null ) ; if ( tmp_start == tmp . length ) return ERT . NIL ; } break ; } } } return ESeq . fromArraySkippingNulls ( tmp , tmp_start , tmp . length ) ; } } </s>
|
<s> package erjang . m . erlang ; import java . io . ByteArrayOutputStream ; import java . io . IOException ; import java . io . OutputStream ; import java . math . BigInteger ; import erjang . BIF ; import erjang . EAtom ; import erjang . EBinary ; import erjang . EBitString ; import erjang . ECons ; import erjang . EExternal ; import erjang . EInputStream ; import erjang . EInteger ; import erjang . EDouble ; import erjang . EObject ; import erjang . EOutputStream ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . EString ; import erjang . ETuple ; import erjang . ERef ; import erjang . ETuple2 ; import erjang . ErlangError ; import erjang . NotImplemented ; import erjang . driver . IO ; public class ErlConvert { private static final ESmall PLUS_SIGN = ERT . box ( ( int ) '<CHAR_LIT>' ) ; private static final EAtom am_compressed = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_minor_version = EAtom . intern ( "<STR_LIT>" ) ; @ BIF public static EObject binary_to_term ( EObject arg ) { EBinary bin ; if ( ( bin = arg . testBinary ( ) ) == null ) throw ERT . badarg ( arg ) ; EInputStream in = bin . getInputStream ( ) ; try { EObject val = in . read_any ( ) ; return val ; } catch ( IOException e ) { throw new ErlangError ( ERT . am_badarg , e , arg ) ; } } @ BIF public static EBinary term_to_binary ( EObject obj ) { EOutputStream eos = new EOutputStream ( ) ; eos . write ( EExternal . versionTag ) ; eos . write_any ( obj ) ; return eos . getBinaryContent ( ) ; } @ BIF public static ESmall external_size ( EObject obj ) { EBinary bin = term_to_binary ( obj ) ; return ERT . box ( bin . byteSize ( ) ) ; } @ BIF static ETuple2 split_binary ( EObject bin , EObject idx ) { EBitString b ; ESmall i ; if ( ( b = bin . testBitString ( ) ) == null || ( ( i = idx . testSmall ( ) ) == null ) || i . value < <NUM_LIT:0> || i . value > b . byteSize ( ) ) { throw ERT . badarg ( bin ) ; } long split = i . value * <NUM_LIT:8> ; return new ETuple2 ( b . substring ( <NUM_LIT:0> , split ) , b . substring ( split ) ) ; } @ BIF public static ESeq fun_to_list ( EObject fun ) { return EString . fromString ( fun . toString ( ) ) ; } @ BIF public static EBinary term_to_binary ( EObject obj , EObject spec ) { int compression = <NUM_LIT:0> ; int minor = <NUM_LIT:0> ; ESeq opts ; if ( ( opts = spec . testSeq ( ) ) == null ) { throw ERT . badarg ( obj , spec ) ; } while ( ! opts . isNil ( ) ) { EObject val = opts . head ( ) ; ETuple2 tup ; if ( val == am_compressed ) { compression = <NUM_LIT:6> ; } else if ( ( tup = ETuple2 . cast ( val ) ) != null ) { if ( tup . elem1 == am_compressed ) { ESmall sm ; if ( ( sm = tup . elem2 . testSmall ( ) ) != null ) { compression = sm . value ; } else { throw ERT . badarg ( obj , spec ) ; } } else if ( tup . elem1 == am_minor_version ) { ESmall sm ; if ( ( sm = tup . elem2 . testSmall ( ) ) != null ) { minor = sm . value ; } else { throw ERT . badarg ( obj , spec ) ; } } } else { throw ERT . badarg ( obj , spec ) ; } opts = opts . tail ( ) ; } if ( compression < <NUM_LIT:0> || compression > <NUM_LIT:9> || minor < <NUM_LIT:0> || minor > <NUM_LIT:1> ) { throw ERT . badarg ( obj , spec ) ; } if ( minor == <NUM_LIT:0> ) { throw new NotImplemented ( "<STR_LIT>" ) ; } EOutputStream eos = new EOutputStream ( ) ; eos . write ( EExternal . versionTag ) ; if ( compression != <NUM_LIT:0> ) { eos . write_compressed ( obj , compression ) ; } else { eos . write_any ( obj ) ; } return eos . getBinaryContent ( ) ; } @ BIF public static EAtom list_to_existing_atom ( EObject obj ) { EString seq ; if ( ( seq = obj . testString ( ) ) == null ) throw ERT . badarg ( obj ) ; return EAtom . existing_atom ( seq . stringValue ( ) ) ; } @ BIF public static EInteger list_to_integer ( EObject obj ) { EString seq ; if ( ( seq = obj . testString ( ) ) == null ) throw ERT . badarg ( obj ) ; if ( ! seq . isNil ( ) ) { if ( seq . head ( ) . equalsExactly ( PLUS_SIGN ) ) { seq = seq . tail ( ) . testString ( ) ; if ( seq == null ) { throw ERT . badarg ( obj ) ; } } } try { BigInteger val = new BigInteger ( seq . stringValue ( ) ) ; return ERT . box ( val ) ; } catch ( NumberFormatException e ) { throw ERT . badarg ( obj ) ; } } @ BIF public static EDouble list_to_float ( EObject obj ) { EString seq ; if ( ( seq = obj . testString ( ) ) == null ) throw ERT . badarg ( obj ) ; String string = seq . stringValue ( ) ; if ( string . length ( ) == <NUM_LIT:0> || string . charAt ( <NUM_LIT:0> ) == '<CHAR_LIT:.>' || string . charAt ( string . length ( ) - <NUM_LIT:1> ) == '<CHAR_LIT:.>' || string . indexOf ( '<CHAR_LIT:.>' ) == - <NUM_LIT:1> ) { throw ERT . badarg ( obj ) ; } try { double val = Double . parseDouble ( string ) ; return ERT . box ( val ) ; } catch ( NumberFormatException e ) { throw ERT . badarg ( obj ) ; } } @ BIF public static ETuple list_to_tuple ( EObject obj ) { ESeq seq ; if ( ( seq = obj . testSeq ( ) ) == null ) throw ERT . badarg ( obj ) ; return ETuple . make ( seq . toArray ( ) ) ; } @ BIF public static EString binary_to_list ( EObject obj ) { EBinary bin = obj . testBinary ( ) ; if ( bin == null ) throw ERT . badarg ( obj ) ; return EString . make ( bin ) ; } @ BIF public static EString binary_to_list ( EObject obj , EObject start , EObject stop ) { EBinary bin = obj . testBinary ( ) ; ESmall s = start . testSmall ( ) ; ESmall e = stop . testSmall ( ) ; if ( bin == null || s == null || e == null ) throw ERT . badarg ( obj , start , stop ) ; int idx0start = s . value - <NUM_LIT:1> ; int len = e . value - s . value + <NUM_LIT:1> ; if ( idx0start < <NUM_LIT:0> || len < <NUM_LIT:0> || ( idx0start + len ) > bin . byteSize ( ) ) throw ERT . badarg ( obj , start , stop ) ; return EString . make ( bin , idx0start , len ) ; } static private class BARR extends ByteArrayOutputStream { EBinary asBinary ( ) { return new EBinary ( super . buf , <NUM_LIT:0> , super . count ) ; } } @ BIF public static EBitString iolist_to_binary ( EObject list ) { EBitString bin = list . testBitString ( ) ; if ( bin != null ) return bin ; ECons iol = list . testCons ( ) ; if ( iol == null ) throw ERT . badarg ( list ) ; if ( iol . isNil ( ) ) { return EBinary . EMPTY ; } BARR barr = new BARR ( ) ; try { collectList ( list , iol , barr ) ; } catch ( IOException e ) { throw new InternalError ( ) ; } return barr . asBinary ( ) ; } private static void collectList ( EObject list , ECons iol , OutputStream barr ) throws IOException { EObject tail ; ECons cons ; for ( tail = iol ; ( cons = tail . testNonEmptyList ( ) ) != null ; tail = cons . tail ( ) ) { EObject hd = cons . head ( ) ; ESmall sm ; EBinary bi ; ECons co ; if ( ( sm = hd . testSmall ( ) ) != null ) { if ( sm . value < <NUM_LIT:0> || sm . value > <NUM_LIT:255> ) throw ERT . badarg ( list ) ; barr . write ( sm . value ) ; } else if ( ( bi = hd . testBinary ( ) ) != null ) { bi . writeTo ( barr ) ; } else if ( ( co = hd . testNonEmptyList ( ) ) != null ) { collectList ( list , co , barr ) ; } else if ( hd . isNil ( ) ) { } else { throw ERT . badarg ( list ) ; } } EBinary bi ; if ( ( bi = tail . testBinary ( ) ) != null ) { try { bi . writeTo ( barr ) ; } catch ( IOException e ) { throw new InternalError ( "<STR_LIT>" ) ; } } else if ( ! tail . isNil ( ) ) { throw ERT . badarg ( list ) ; } } public static void collectList ( EObject list , OutputStream out ) throws IOException { ECons iol = list . testCons ( ) ; if ( iol == null ) throw ERT . badarg ( list ) ; if ( iol . isNil ( ) ) { return ; } collectList ( list , iol , out ) ; } @ BIF public static EObject tuple_to_list ( EObject tup ) { ETuple t ; if ( ( t = tup . testTuple ( ) ) == null ) { throw ERT . badarg ( tup ) ; } ESeq res = ERT . NIL ; for ( int i = t . arity ( ) ; i > <NUM_LIT:0> ; i -- ) { EObject e = t . elm ( i ) ; res = res . cons ( e == null ? ERT . NIL : e ) ; } return res ; } @ BIF public static EString ref_to_list ( EObject obj ) { ERef ref = obj . testReference ( ) ; if ( ref == null ) throw ERT . badarg ( obj ) ; return new EString ( ref . toString ( ) ) ; } @ BIF public static EBinary atom_to_binary ( EObject obj , EObject enc ) { EAtom am = obj . testAtom ( ) ; EAtom en = enc . testAtom ( ) ; if ( am == null || en == null ) { throw ERT . badarg ( obj , enc ) ; } byte [ ] data ; String str = am . getName ( ) ; if ( en == ERT . am_latin1 ) { data = str . getBytes ( IO . ISO_LATIN_1 ) ; } else if ( en == ERT . am_utf8 || en == ERT . am_unicode ) { data = str . getBytes ( IO . UTF8 ) ; } else { throw ERT . badarg ( obj , enc ) ; } return new EBinary ( data ) ; } } </s>
|
<s> package erjang . m . erlang ; public interface EDist { public static final int DFLAG_PUBLISHED = <NUM_LIT> ; public static final int DFLAG_ATOM_CACHE = <NUM_LIT> ; public static final int DFLAG_EXTENDED_REFERENCES = <NUM_LIT> ; public static final int DFLAG_DIST_MONITOR = <NUM_LIT> ; public static final int DFLAG_FUN_TAGS = <NUM_LIT> ; public static final int DFLAG_DIST_MONITOR_NAME = <NUM_LIT> ; public static final int DFLAG_HIDDEN_ATOM_CACHE = <NUM_LIT> ; public static final int DFLAG_NEW_FUN_TAGS = <NUM_LIT> ; public static final int DFLAG_EXTENDED_PIDS_PORTS = <NUM_LIT> ; public static final int DFLAG_EXPORT_PTR_TAG = <NUM_LIT> ; public static final int DFLAG_BIT_BINARIES = <NUM_LIT> ; public static final int DFLAG_NEW_FLOATS = <NUM_LIT> ; public static final int DFLAG_UNICODE_IO = <NUM_LIT> ; public static final int DFLAG_DIST_HDR_ATOM_CACHE = <NUM_LIT> ; public static final int DFLAG_SMALL_ATOM_TAGS = <NUM_LIT> ; public static final int TERM_TO_BINARY_DFLAGS = ( DFLAG_EXTENDED_REFERENCES | DFLAG_NEW_FUN_TAGS | DFLAG_EXTENDED_PIDS_PORTS | DFLAG_EXPORT_PTR_TAG | DFLAG_BIT_BINARIES ) ; public static final int DOP_LINK = <NUM_LIT:1> ; public static final int DOP_SEND = <NUM_LIT:2> ; public static final int DOP_EXIT = <NUM_LIT:3> ; public static final int DOP_UNLINK = <NUM_LIT:4> ; public static final int DOP_NODE_LINK = <NUM_LIT:5> ; public static final int DOP_REG_SEND = <NUM_LIT:6> ; public static final int DOP_GROUP_LEADER = <NUM_LIT:7> ; public static final int DOP_EXIT2 = <NUM_LIT:8> ; public static final int DOP_SEND_TT = <NUM_LIT:12> ; public static final int DOP_EXIT_TT = <NUM_LIT> ; public static final int DOP_REG_SEND_TT = <NUM_LIT:16> ; public static final int DOP_EXIT2_TT = <NUM_LIT> ; public static final int DOP_MONITOR_P = <NUM_LIT> ; public static final int DOP_DEMONITOR_P = <NUM_LIT:20> ; public static final int DOP_MONITOR_P_EXIT = <NUM_LIT> ; } </s>
|
<s> package erjang . m . erlang ; import erjang . ENative ; import erjang . ERT ; public class Native extends ENative { @ Override public Class < ? > [ ] getNativeClasses ( ) { return new Class [ ] { BinOps . class , ErlBif . class , ErlProc . class , ErlList . class , ErlConvert . class , ErlPort . class , ErlHash . class , ErlDist . class , ErlFun . class , ERT . class } ; } } </s>
|
<s> package erjang . m . erlang ; import java . util . Map ; import java . util . concurrent . ConcurrentHashMap ; import erjang . EAtom ; import erjang . EInternalPort ; public class DistEntry { static Map < EAtom , DistEntry > table = new ConcurrentHashMap < EAtom , DistEntry > ( ) ; static { } private final EAtom sysname ; private final EInternalPort cid ; public int flags ; public DistEntry ( EAtom sysname , EInternalPort cid ) { this . sysname = sysname ; this . cid = cid ; table . put ( sysname , this ) ; } static DistEntry find ( EAtom sysname ) { return table . get ( sysname ) ; } static DistEntry find_or_insert ( EAtom sysname ) { DistEntry ent = table . get ( sysname ) ; if ( ent == null ) { return new DistEntry ( sysname , null ) ; } else { return ent ; } } static DistEntry sysname_to_connected_dist_entry ( EAtom sysname ) { DistEntry ent = table . get ( sysname ) ; if ( ent == null ) { return null ; } else { return ent . cid == null ? null : ent ; } } } </s>
|
<s> package erjang . m . erlang ; import java . lang . management . GarbageCollectorMXBean ; import java . lang . management . ManagementFactory ; import java . lang . management . RuntimeMXBean ; import java . math . BigDecimal ; import java . nio . ByteBuffer ; import java . security . MessageDigest ; import java . security . NoSuchAlgorithmException ; import java . util . ArrayList ; import java . util . Calendar ; import java . util . GregorianCalendar ; import java . util . List ; import java . util . TimeZone ; import java . util . logging . Level ; import java . util . logging . Logger ; import java . util . concurrent . atomic . AtomicLong ; import java . util . concurrent . atomic . AtomicStampedReference ; import kilim . Pausable ; import kilim . Task ; import erjang . BIF ; import erjang . EAtom ; import erjang . EBinary ; import erjang . EBitString ; import erjang . ECons ; import erjang . EDouble ; import erjang . EFun ; import erjang . EInteger ; import erjang . EModuleManager ; import erjang . ENode ; import erjang . ENumber ; import erjang . EObject ; import erjang . EPID ; import erjang . EPeer ; import erjang . EPort ; import erjang . EProc ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . EString ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . ETuple3 ; import erjang . ErlangError ; import erjang . ErlangException ; import erjang . ErlangRaise ; import erjang . ErlangThrow ; import erjang . FunID ; import erjang . Module ; import erjang . NotImplemented ; import erjang . BIF . Type ; import erjang . m . java . JavaObject ; @ Module ( "<STR_LIT>" ) public class ErlBif { private static final TimeZone UTC_TIME_ZONE = TimeZone . getTimeZone ( "<STR_LIT>" ) ; private static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private static EAtom am_wall_clock = EAtom . intern ( "<STR_LIT>" ) ; private static EAtom am_reductions = EAtom . intern ( "<STR_LIT>" ) ; private static EAtom am_garbage_collection = EAtom . intern ( "<STR_LIT>" ) ; private static EAtom am_runtime = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_run_queue = EAtom . intern ( "<STR_LIT>" ) ; @ BIF static EObject apply ( EProc proc , EObject fun , EObject args ) throws Pausable { ESeq a = args . testSeq ( ) ; if ( a == null ) throw ERT . badarg ( fun , args ) ; EObject res ; EFun f = fun . testFunction ( ) ; if ( f != null ) { res = apply_last ( proc , f , a ) ; } else { ETuple t = fun . testTuple ( ) ; if ( t == null ) { throw ERT . badarg ( fun , args ) ; } ETuple2 t2 = ETuple2 . cast ( t ) ; if ( t2 == null ) { throw ERT . badarg ( fun , args ) ; } EAtom mn = t2 . elem1 . testAtom ( ) ; EAtom fn = t2 . elem2 . testAtom ( ) ; FunID funspec ; f = EModuleManager . resolve ( funspec = new FunID ( mn , fn , a . length ( ) ) ) ; if ( f == null ) { throw ERT . undef ( funspec , a . toArray ( ) ) ; } res = apply_last ( proc , f , a ) ; } while ( res == EProc . TAIL_MARKER ) { res = proc . tail . go ( proc ) ; } return res ; } @ BIF public static EBinary list_to_binary ( EObject val ) { EString es ; if ( ( es = val . testString ( ) ) != null ) { return es . asBitString ( ) ; } ECons cons = val . testCons ( ) ; if ( cons == null ) throw ERT . badarg ( val ) ; List < ByteBuffer > out = new ArrayList < ByteBuffer > ( ) ; if ( ! cons . collectIOList ( out ) ) { throw ERT . badarg ( val ) ; } if ( out . size ( ) == <NUM_LIT:1> ) { ByteBuffer b = out . get ( <NUM_LIT:0> ) ; return EBinary . make ( b ) ; } int length = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < out . size ( ) ; i ++ ) { length += out . get ( i ) . remaining ( ) ; } byte [ ] all = new byte [ length ] ; int pos = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < out . size ( ) ; i ++ ) { ByteBuffer bb = out . get ( i ) ; int len = bb . remaining ( ) ; bb . get ( all , pos , len ) ; pos += len ; } assert ( pos == length ) ; return new EBinary ( all ) ; } @ BIF static ESmall iolist_size ( EObject val ) { EBinary bin ; if ( ( bin = val . testBinary ( ) ) != null ) { return new ESmall ( bin . byteSize ( ) ) ; } EString str ; if ( ( str = val . testString ( ) ) != null ) { return new ESmall ( str . length ( ) ) ; } ECons seq ; if ( ( seq = val . testCons ( ) ) == null ) { throw ERT . badarg ( val ) ; } ArrayList < ByteBuffer > al = new ArrayList < ByteBuffer > ( ) ; seq . collectIOList ( al ) ; int size = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < al . size ( ) ; i ++ ) { size += al . get ( i ) . remaining ( ) ; } return new ESmall ( size ) ; } @ BIF public static EObject apply ( EProc proc , EObject one , EObject two , EObject three ) throws Pausable { EAtom mod = one . testAtom ( ) ; ETuple t = one . testTuple ( ) ; JavaObject jo = one . testJavaObject ( ) ; EAtom fun = two . testAtom ( ) ; ESeq args = three . testSeq ( ) ; if ( ( mod == null && t == null && jo == null ) || fun == null || args == null ) throw ERT . badarg ( one , two , three ) ; EFun f = ERT . resolve_fun ( one , fun , args . length ( ) ) ; EObject res = apply_last ( proc , f , args ) ; while ( res == EProc . TAIL_MARKER ) { res = proc . tail . go ( proc ) ; } return res ; } private static EObject apply_last ( EProc proc , EFun fun , ESeq args ) throws Pausable { ESeq rargs = args . reverse ( ) ; int len = args . length ( ) ; switch ( len ) { case <NUM_LIT:11> : proc . arg10 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:10> : proc . arg9 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:9> : proc . arg8 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:8> : proc . arg7 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:7> : proc . arg6 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:6> : proc . arg5 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:5> : proc . arg4 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:4> : proc . arg3 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:3> : proc . arg2 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:2> : proc . arg1 = rargs . head ( ) ; rargs = rargs . tail ( ) ; case <NUM_LIT:1> : proc . arg0 = rargs . head ( ) ; case <NUM_LIT:0> : break ; default : return fun . apply ( proc , args ) ; } proc . tail = fun ; return EProc . TAIL_MARKER ; } @ BIF static public EPID self ( EProc proc ) { if ( proc == null ) { log . severe ( "<STR_LIT>" ) ; } return proc . self_handle ( ) ; } private static final AtomicStampedReference < ETuple > cachedDate = new AtomicStampedReference ( null , <NUM_LIT:0> ) ; static final int MILLIS_PER_MINUTE = <NUM_LIT> * <NUM_LIT:1000> ; @ BIF static public ETuple date ( ) { long millis = System . currentTimeMillis ( ) ; int curMinutes = ( int ) ( millis / MILLIS_PER_MINUTE ) ; int [ ] cacheValidUntilMinutes = new int [ <NUM_LIT:1> ] ; ETuple cachedResult = cachedDate . get ( cacheValidUntilMinutes ) ; if ( curMinutes < cacheValidUntilMinutes [ <NUM_LIT:0> ] && cachedResult != null ) { return cachedResult ; } GregorianCalendar cal = new GregorianCalendar ( ) ; int year = cal . get ( Calendar . YEAR ) ; int month = cal . get ( Calendar . MONTH ) + <NUM_LIT:1> ; int day = cal . get ( Calendar . DAY_OF_MONTH ) ; ETuple result = ETuple . make ( ERT . box ( year ) , ERT . box ( month ) , ERT . box ( day ) ) ; cal . add ( Calendar . DAY_OF_YEAR , <NUM_LIT:1> ) ; cal . set ( Calendar . HOUR , <NUM_LIT:0> ) ; cal . set ( Calendar . MINUTE , <NUM_LIT:0> ) ; cal . set ( Calendar . SECOND , <NUM_LIT:0> ) ; cal . set ( Calendar . MILLISECOND , <NUM_LIT:0> ) ; int newValidUntilMinutes = ( int ) ( cal . getTime ( ) . getTime ( ) / MILLIS_PER_MINUTE ) ; cachedDate . weakCompareAndSet ( cachedResult , result , cacheValidUntilMinutes [ <NUM_LIT:0> ] , newValidUntilMinutes ) ; return result ; } @ BIF static public ETuple time ( ) { GregorianCalendar cal = new GregorianCalendar ( ) ; int hour = cal . get ( Calendar . HOUR_OF_DAY ) ; int minute = cal . get ( Calendar . MINUTE ) ; int second = cal . get ( Calendar . SECOND ) ; return ETuple . make ( ERT . box ( hour ) , ERT . box ( minute ) , ERT . box ( second ) ) ; } @ BIF static public EString integer_to_list ( EObject obj ) { EInteger num ; if ( ( num = obj . testInteger ( ) ) != null ) { return new EString ( num . toString ( ) ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EAtom list_to_atom ( EObject obj ) { EString es ; if ( ( es = obj . testString ( ) ) != null ) { return EAtom . intern ( es . stringValue ( ) ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EPID list_to_pid ( EObject obj ) { ECons list ; if ( ( list = obj . testCons ( ) ) != null ) { ESeq s = EString . make ( list ) ; return ERT . loopkup_pid ( s ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EString pid_to_list ( EObject obj ) { EPID pid ; if ( ( pid = obj . testPID ( ) ) != null ) { return pid . getName ( ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EString port_to_list ( EObject obj ) { EPort port ; if ( ( port = obj . testPort ( ) ) != null ) { return port . getName ( ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EString float_to_list ( EObject obj ) { EDouble value ; if ( ( value = obj . testFloat ( ) ) != null ) { return value . to_list ( ) ; } throw ERT . badarg ( obj ) ; } @ BIF static public EObject error ( EObject reason ) { throw new ErlangError ( reason ) ; } @ BIF static public EObject error ( EObject reason , EObject args ) { ESeq aseq = args . testSeq ( ) ; if ( aseq == null ) throw ERT . badarg ( reason , args ) ; throw new ErlangError ( reason , aseq . toArray ( ) ) ; } @ BIF ( name = "<STR_LIT>" ) static public EObject throw_ex ( EObject reason ) { throw new ErlangThrow ( reason ) ; } @ BIF static public ESeq get_module_info ( EObject mod ) { ESeq res = ERT . NIL ; res = res . cons ( new ETuple2 ( ERT . am_attributes , get_module_info ( mod , ERT . am_attributes ) ) ) ; res = res . cons ( new ETuple2 ( ERT . am_exports , get_module_info ( mod , ERT . am_exports ) ) ) ; return res ; } @ BIF static public EObject get_module_info ( EObject mod , EObject key ) { EAtom m = mod . testAtom ( ) ; EAtom k = key . testAtom ( ) ; if ( m == null || k == null ) { throw ERT . badarg ( mod , key ) ; } if ( k == ERT . am_attributes ) { return EModuleManager . get_attributes ( m ) ; } if ( k == ERT . am_exports ) { return EModuleManager . get_exports ( m ) ; } if ( k == ERT . am_module ) { return mod ; } return ERT . am_undefined ; } public static final String [ ] PRE_LOADED_MODULES = new String [ ] { "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" } ; @ BIF public static ESeq pre_loaded ( ) { ESeq res = ERT . NIL ; for ( int i = <NUM_LIT:0> ; i < PRE_LOADED_MODULES . length ; i ++ ) { res = res . cons ( EAtom . intern ( PRE_LOADED_MODULES [ i ] ) ) ; } return res ; } @ BIF public static EObject garbage_collect ( ) { System . gc ( ) ; return ERT . TRUE ; } @ BIF public static EObject garbage_collect ( EObject pid ) { System . gc ( ) ; return ERT . TRUE ; } @ BIF static public ETuple setelement ( EObject a1 , EObject a2 , EObject term ) { ETuple t = a2 . testTuple ( ) ; ESmall i = a1 . testSmall ( ) ; if ( t == null || i == null ) throw ERT . badarg ( a1 , a2 , term ) ; return t . setelement ( i . value , term ) ; } @ BIF static public ETuple setelement ( int index , ETuple a2 , EObject term ) { return a2 . setelement ( index , term ) ; } @ BIF static public ETuple setelement ( ESmall index , ETuple a2 , EObject term ) { return a2 . setelement ( index . value , term ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) static public EObject element$g ( EObject idx , EObject tup ) { ESmall i = idx . testSmall ( ) ; ETuple t = tup . testTuple ( ) ; if ( i == null || t == null ) { return null ; } if ( i . value > t . arity ( ) ) return null ; return t . elm ( i . value ) ; } @ BIF static public EObject element ( EObject idx , EObject tup ) { ESmall i = idx . testSmall ( ) ; ETuple t = tup . testTuple ( ) ; if ( i == null || t == null || i . value < <NUM_LIT:1> || i . value > t . arity ( ) ) { throw ERT . badarg ( idx , tup ) ; } return t . elm ( i . asInt ( ) ) ; } @ BIF static public EObject element ( int idx , ETuple tup ) { if ( tup . arity ( ) >= idx ) { return tup . elm ( idx ) ; } throw ERT . badarg ( new ESmall ( idx ) , tup ) ; } @ BIF static public EObject element ( ESmall sidx , ETuple tup ) { int idx = sidx . value ; if ( tup . arity ( ) >= idx ) { return tup . elm ( idx ) ; } throw ERT . badarg ( sidx , tup ) ; } @ BIF static public EObject element ( int idx , EObject obj ) { ETuple tup ; if ( ( tup = obj . testTuple ( ) ) != null && tup . arity ( ) >= idx ) { return tup . elm ( idx ) ; } throw ERT . badarg ( new ESmall ( idx ) , obj ) ; } @ BIF static public EObject hd ( ECons cell ) { return cell . head ( ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) static public EObject hd$p ( ECons cell ) { return cell . head ( ) ; } @ BIF static public EObject hd ( EObject cell ) { ECons cons ; if ( ( cons = cell . testNonEmptyList ( ) ) != null ) { return cons . head ( ) ; } throw ERT . badarg ( cell ) ; } @ BIF static public EObject tl ( EObject cell ) { ECons cons ; if ( ( cons = cell . testNonEmptyList ( ) ) != null ) { return cons . tail ( ) ; } throw ERT . badarg ( cell ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) static public EObject hd$p ( EObject cell ) { ECons cons ; if ( ( cons = cell . testNonEmptyList ( ) ) != null ) { return cons . head ( ) ; } return null ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) static public EObject tl$p ( EObject cell ) { ECons cons ; if ( ( cons = cell . testNonEmptyList ( ) ) != null ) { return cons . tail ( ) ; } return null ; } @ BIF static public EInteger length ( EObject list ) { ESeq seq ; if ( ( seq = list . testSeq ( ) ) != null ) { return ERT . box ( seq . length ( ) ) ; } throw ERT . badarg ( list ) ; } @ BIF static public int length ( ESeq list ) { return list . length ( ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public ESmall length$p ( EObject list ) { ESeq seq ; if ( ( seq = list . testSeq ( ) ) != null ) { return ERT . box ( seq . length ( ) ) ; } return null ; } @ BIF static public EObject whereis ( EProc proc , EObject regname ) { return ERT . whereis ( regname ) ; } static final long wall_clock0 = System . currentTimeMillis ( ) ; private static final EAtom am_total = EAtom . intern ( "<STR_LIT>" ) ; static long last_wall_clock = wall_clock0 ; static long last_reductions = <NUM_LIT:0> ; static long last_runtime = <NUM_LIT:0> ; @ BIF static public EObject statistics ( EProc proc , EObject spec ) { if ( spec == am_wall_clock ) { long now = System . currentTimeMillis ( ) ; long since_last = now - last_wall_clock ; long since_epoch = now - wall_clock0 ; last_wall_clock = now ; return ETuple . make ( ERT . box ( since_epoch ) , ERT . box ( since_last ) ) ; } else if ( spec == am_reductions ) { long current_reds = proc . reds ; long since_last = current_reds - last_reductions ; last_reductions = current_reds ; return new ETuple2 ( ERT . box ( current_reds ) , ERT . box ( since_last ) ) ; } else if ( spec == am_runtime ) { RuntimeMXBean b = ManagementFactory . getRuntimeMXBean ( ) ; long current_runtime = b . getUptime ( ) ; long since_last = current_runtime - last_runtime ; last_runtime = current_runtime ; return new ETuple2 ( ERT . box ( current_runtime ) , ERT . box ( since_last ) ) ; } else if ( spec == am_garbage_collection ) { List < GarbageCollectorMXBean > b = ManagementFactory . getGarbageCollectorMXBeans ( ) ; long num_gcs = <NUM_LIT:0> ; long time_gcs = <NUM_LIT:0> ; for ( GarbageCollectorMXBean bb : b ) { num_gcs += bb . getCollectionCount ( ) ; time_gcs += bb . getCollectionTime ( ) ; } return ETuple . make ( ERT . box ( num_gcs ) , ERT . box ( time_gcs ) , ERT . box ( <NUM_LIT:0> ) ) ; } else if ( spec == am_run_queue ) { return ERT . box ( <NUM_LIT:0> ) ; } throw new NotImplemented ( "<STR_LIT>" + spec + "<STR_LIT:)>" ) ; } @ BIF static public EObject put ( EProc proc , EObject key , EObject value ) { return proc . put ( key , value ) ; } @ BIF static public EString name ( EObject a1 , EObject a2 ) { throw new NotImplemented ( ) ; } @ BIF static public ECons get ( EProc proc ) { return proc . get ( ) ; } @ BIF static public EObject get ( EProc proc , EObject key ) { return proc . get ( key ) ; } @ BIF ( type = Type . ARITHBIF ) static public double fdiv ( double v1 , double v2 ) { test_zero ( v1 , v2 ) ; return v1 / v2 ; } private static void test_zero ( double v1 , double v2 ) { if ( v2 == <NUM_LIT:0.0> ) throw new ErlangError ( ERT . AM_BADARITH , ERT . NIL . cons ( v2 ) . cons ( v1 ) ) ; } @ BIF ( type = Type . ARITHBIF ) static public double fsub ( double v1 , double v2 ) { return v1 - v2 ; } @ BIF ( type = Type . ARITHBIF ) static public double fadd ( double v1 , double v2 ) { return v1 + v2 ; } @ BIF ( type = Type . ARITHBIF ) static public double fmul ( double v1 , double v2 ) { return v1 * v2 ; } @ BIF ( type = Type . ARITHBIF ) public static double fnegate ( double val ) { return - val ; } @ BIF ( name = "<STR_LIT:->" ) static public ENumber neg ( EObject v1 ) { ENumber n1 ; if ( ( n1 = v1 . testNumber ( ) ) != null ) { return n1 . negate ( ) ; } throw ERT . badarg ( v1 ) ; } @ BIF ( name = "<STR_LIT:->" , type = Type . GUARD ) static public ENumber neg$g ( EObject v1 ) { ENumber n1 ; if ( ( n1 = v1 . testNumber ( ) ) != null ) { return n1 . negate ( ) ; } return null ; } @ BIF static public EInteger div ( EObject o1 , EObject o2 ) { return o1 . idiv ( o2 ) ; } @ BIF ( name = "<STR_LIT>" ) static public ENumber div ( EObject v1 , int v2 ) { return v1 . idiv ( v2 ) ; } @ BIF ( name = "<STR_LIT>" ) static public ENumber div ( ENumber n1 , int v2 ) { return n1 . idiv ( v2 ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public ENumber div$g ( EObject v1 , EObject v2 ) { ENumber n1 , n2 ; if ( ( n1 = v1 . testInteger ( ) ) != null && ( n2 = v2 . testInteger ( ) ) != null ) { if ( n2 . erlangEquals ( ESmall . ZERO ) ) return null ; return n1 . idiv ( n2 ) ; } return null ; } @ BIF ( name = "<STR_LIT:->" ) static public ENumber minus ( EObject v1 , int v2 ) { return v1 . subtract ( v2 ) ; } @ BIF ( name = "<STR_LIT:->" ) static public ENumber minus ( int v1 , int v2 ) { return ERT . box ( ( long ) v1 - ( long ) v2 ) ; } @ BIF ( name = "<STR_LIT:->" ) static public ENumber minus ( EObject v1 , EObject v2 ) { return v1 . subtract ( v2 , false ) ; } @ BIF ( name = "<STR_LIT:->" , type = Type . GUARD ) static public ENumber subtract$p ( EObject v1 , EObject v2 ) { return v1 . subtract ( v2 , true ) ; } @ BIF ( name = "<STR_LIT:/>" , type = Type . GUARD ) static public ENumber divide$p ( EObject v1 , EObject v2 ) { ENumber n1 ; if ( ( n1 = v1 . testNumber ( ) ) != null ) { ENumber n2 ; if ( ( n2 = v2 . testNumber ( ) ) != null ) { if ( n2 . doubleValue ( ) == <NUM_LIT:0.0> ) return null ; return n1 . divide ( v2 ) ; } } return null ; } @ BIF ( name = "<STR_LIT:/>" , type = Type . GUARD ) static public ENumber divide$p ( EObject v1 , double d2 ) { ENumber n1 ; if ( d2 != <NUM_LIT:0.0> && ( n1 = v1 . testNumber ( ) ) != null ) { return n1 . divide ( d2 ) ; } return null ; } @ BIF ( name = "<STR_LIT:/>" ) static public ENumber divide ( EObject v1 , EObject v2 ) { return v1 . divide ( v2 ) ; } @ BIF ( name = "<STR_LIT:+>" , type = Type . GUARD ) static public ENumber plus$p ( EObject v1 , EObject v2 ) { return v1 . add ( v2 , true ) ; } @ BIF ( name = "<STR_LIT:+>" , type = Type . GUARD ) static public ENumber plus$p ( EObject v1 , ESmall s2 ) { return v1 . add ( s2 . value , true ) ; } @ BIF ( name = "<STR_LIT:+>" ) static public ENumber plus ( int v1 , int v2 ) { return ERT . box ( ( long ) v1 + ( long ) v2 ) ; } @ BIF ( name = "<STR_LIT:+>" ) static public ENumber plus ( EObject v1 , EObject v2 ) { return v1 . add ( v2 , false ) ; } @ BIF ( name = "<STR_LIT:+>" ) static public ENumber plus ( EObject v1 , int i2 ) { return v1 . add ( i2 , false ) ; } @ BIF ( name = "<STR_LIT:+>" ) static public ENumber plus ( EObject v1 , ESmall i2 ) { return v1 . add ( i2 . value , false ) ; } @ BIF ( name = "<STR_LIT:->" ) static public ENumber minus ( EObject v1 , ESmall i2 ) { return v1 . subtract ( i2 . value ) ; } @ BIF ( name = "<STR_LIT:->" , type = Type . GUARD ) static public ENumber minus$g ( EObject v1 , ESmall i2 ) { ENumber n1 ; if ( ( n1 = v1 . testNumber ( ) ) != null ) { return n1 . subtract ( i2 . value ) ; } return null ; } @ BIF ( name = "<STR_LIT:*>" , type = Type . GUARD ) static public ENumber multiply$g ( EObject v1 , EObject v2 ) { ENumber n1 ; if ( ( n1 = v1 . testNumber ( ) ) != null ) { ENumber n2 ; if ( ( n2 = v2 . testNumber ( ) ) != null ) { return n1 . multiply ( n2 ) ; } } return null ; } @ BIF ( name = "<STR_LIT:*>" ) static public ENumber multiply ( int v1 , int v2 ) { return ERT . box ( ( long ) v1 * ( long ) v2 ) ; } @ BIF ( name = "<STR_LIT:*>" ) static public ENumber multiply ( EObject v1 , EObject v2 ) { return v1 . multiply ( v2 ) ; } @ BIF static public EInteger trunc ( EObject v1 ) { EInteger i1 ; if ( ( i1 = v1 . testInteger ( ) ) != null ) { return i1 ; } EDouble n1 ; if ( ( n1 = v1 . testFloat ( ) ) != null ) { return trunc ( n1 . value ) ; } throw ERT . badarg ( v1 ) ; } @ BIF static public EInteger trunc ( double d ) { return ERT . box ( new BigDecimal ( d ) . toBigInteger ( ) ) ; } @ BIF static public EInteger trunc ( EDouble d1 ) { return ERT . box ( new BigDecimal ( d1 . value ) . toBigInteger ( ) ) ; } @ BIF ( name = "<STR_LIT>" ) static public EInteger round ( double d ) { return ERT . box ( Math . round ( d ) ) ; } @ BIF ( name = "<STR_LIT>" ) static public EInteger round ( EDouble d ) { return ERT . box ( Math . round ( d . value ) ) ; } @ BIF ( name = "<STR_LIT>" ) static public EInteger round ( EObject o ) { EDouble d ; if ( ( d = o . testFloat ( ) ) != null ) return ERT . box ( Math . round ( d . value ) ) ; EInteger i ; if ( ( i = o . testInteger ( ) ) != null ) return i ; throw ERT . badarg ( o ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public EInteger round$g ( double d ) { return ERT . box ( Math . round ( d ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public EInteger round$g ( EDouble d ) { return ERT . box ( Math . round ( d . value ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public EInteger round$g ( EObject o ) { EDouble d = o . testFloat ( ) ; if ( d == null ) return null ; return ERT . box ( Math . round ( d . value ) ) ; } @ BIF ( name = "<STR_LIT:float>" ) static public double float$n ( int v ) { return ( double ) v ; } @ BIF ( name = "<STR_LIT:float>" ) static public double float$n ( ENumber v ) { return v . doubleValue ( ) ; } @ BIF ( name = "<STR_LIT:float>" ) static public EDouble float$n ( EObject v ) { ENumber n = v . testNumber ( ) ; if ( n == null ) throw ERT . badarg ( v ) ; return ERT . box ( n . doubleValue ( ) ) ; } @ BIF ( name = "<STR_LIT:float>" , type = Type . GUARD ) static public EDouble float$g ( EObject v ) { return v . testFloat ( ) ; } @ BIF static public ENumber rem ( EObject v1 , EObject v2 ) { return v1 . irem ( v2 ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public EInteger rem$p ( EObject v1 , EObject v2 ) { if ( v2 . erlangEquals ( ESmall . ZERO ) || v1 . testInteger ( ) == null || v2 . testInteger ( ) == null ) { return null ; } else { return v1 . irem ( v2 ) ; } } @ BIF ( name = "<STR_LIT>" ) static public EInteger rem ( EObject v1 , int v2 ) { return v1 . irem ( v2 ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public ENumber abs$p ( EObject v1 ) { ENumber num ; if ( ( num = v1 . testNumber ( ) ) != null ) { return abs ( num ) ; } return null ; } @ BIF ( name = "<STR_LIT>" ) static public ENumber abs ( EObject v1 ) { ENumber num ; if ( ( num = v1 . testNumber ( ) ) != null ) { return abs ( num ) ; } throw ERT . badarg ( v1 ) ; } @ BIF ( name = "<STR_LIT>" ) static public ENumber abs ( ENumber v1 ) { return v1 . abs ( ) ; } @ BIF ( name = "<STR_LIT>" ) static public ETuple3 now ( ) { long now = now_unique_micros ( ) ; int micros = ( int ) ( now % <NUM_LIT> ) ; now /= <NUM_LIT> ; int secs = ( int ) ( now % <NUM_LIT> ) ; now /= <NUM_LIT> ; int megas = ( int ) now ; ETuple3 res = new ETuple3 ( ) ; res . elem1 = ERT . box ( megas ) ; res . elem2 = ERT . box ( secs ) ; res . elem3 = ERT . box ( micros ) ; return res ; } final static AtomicLong latest_now = new AtomicLong ( ) ; final static long micros_from_epoch_to_nanotime = System . currentTimeMillis ( ) * <NUM_LIT:1000> - System . nanoTime ( ) / <NUM_LIT:1000> ; public static long now_raw_micros ( ) { return System . nanoTime ( ) / <NUM_LIT:1000> + micros_from_epoch_to_nanotime ; } static long now_unique_micros ( ) { long micros = now_raw_micros ( ) ; long prev ; while ( ( prev = latest_now . get ( ) ) < micros ) { if ( latest_now . compareAndSet ( prev , micros ) ) { return micros ; } } return latest_now . incrementAndGet ( ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_eq$p ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangEquals ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ne_exact$g2 ( EObject a1 , EObject a2 ) { return ERT . guard ( ! a1 . equalsExactly ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ne_exact$g ( EObject a1 , EAtom a2 ) { return ERT . guard ( a1 != a2 ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_ne_exact ( EObject a1 , EAtom a2 ) { return ERT . box ( a1 != a2 ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_ne_exact ( EObject a1 , EObject a2 ) { return ERT . box ( ! a1 . equalsExactly ( a2 ) ) ; } @ BIF public static final ESmall size ( EObject o ) { ETuple t ; if ( ( t = o . testTuple ( ) ) == null ) { EBinary b ; if ( ( b = o . testBinary ( ) ) == null ) throw ERT . badarg ( o ) ; return ERT . box ( b . byteSize ( ) ) ; } return ERT . box ( t . arity ( ) ) ; } @ BIF public static final ESmall size ( ETuple t ) { return ERT . box ( t . arity ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT:size>" ) public static final ESmall size$g ( EObject o ) { ETuple t ; if ( ( t = o . testTuple ( ) ) != null ) return ERT . box ( t . arity ( ) ) ; EBinary b ; if ( ( b = o . testBinary ( ) ) != null ) return ERT . box ( b . byteSize ( ) ) ; return null ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT:size>" ) public static final ESmall size$g ( ETuple t ) { return ERT . box ( t . arity ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT:size>" ) public static final ESmall size$g ( EBinary b ) { return ERT . box ( b . byteSize ( ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom eqxp ( EObject a1 , EAtom a2 ) { return ERT . guard ( a1 == a2 ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom eqxp ( EObject a1 , ESmall s2 ) { ESmall s1 ; if ( ( s1 = a1 . testSmall ( ) ) != null ) { return ERT . guard ( s1 . value == s2 . value ) ; } return ERT . guard ( s2 . equalsExactly ( a1 ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom eqxp ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . equalsExactly ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_eq_op$g ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangEquals ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_eq_op ( EObject a1 , EObject a2 ) { return a1 . erlangEquals ( a2 ) ? ERT . TRUE : ERT . FALSE ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ne_exact$g ( EObject a1 , EObject a2 ) { return ERT . guard ( ! a1 . equalsExactly ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ge$g2 ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangCompareTo ( a2 ) >= <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:>>" , type = Type . GUARD ) public static final EAtom is_gt$g ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangCompareTo ( a2 ) > <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ge$g ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangCompareTo ( a2 ) >= <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:>>" ) public static EAtom gt ( EObject v1 , EObject v2 ) { return ERT . box ( v1 . erlangCompareTo ( v2 ) > <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:>>" ) public static EAtom gt ( EObject v1 , ESmall v2 ) { return ERT . box ( v1 . erlangCompareTo ( v2 ) > <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:>>" ) public static EAtom gt ( ESmall v1 , EObject v2 ) { return ERT . box ( v1 . erlangCompareTo ( v2 ) > <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_ne ( EObject a1 , EObject a2 ) { boolean eq = a1 . erlangEquals ( a2 ) ; return ERT . box ( ! eq ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_ne$g ( EObject a1 , EObject a2 ) { boolean eq = a1 . erlangEquals ( a2 ) ; return ERT . guard ( ! eq ) ; } @ BIF ( name = "<STR_LIT:<>" , type = Type . GUARD ) public static final EAtom is_lt$g ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangCompareTo ( a2 ) < <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_le ( EObject a1 , EObject a2 ) { return ERT . box ( a1 . erlangCompareTo ( a2 ) <= <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:<>" ) public static final EAtom is_lt ( EObject a1 , ESmall a2 ) { return ERT . box ( a2 . erlangCompareTo ( a1 ) > <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:<>" ) public static final EAtom is_lt ( ESmall a1 , EObject a2 ) { return ERT . box ( a1 . erlangCompareTo ( a2 ) < <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static final EAtom is_le$g ( EObject a1 , EObject a2 ) { return ERT . guard ( a1 . erlangCompareTo ( a2 ) <= <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT:<>" ) public static final EAtom is_lt ( EObject a1 , EObject a2 ) { return ERT . box ( a1 . erlangCompareTo ( a2 ) < <NUM_LIT:0> ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_ge ( EObject a1 , EObject a2 ) { return ERT . box ( a1 . erlangCompareTo ( a2 ) >= <NUM_LIT:0> ) ; } @ BIF public static final EAtom is_eq ( EObject a1 , EObject a2 ) { return ERT . box ( a1 . erlangEquals ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" ) public static final EAtom is_eq_exact ( EObject a1 , EObject a2 ) { return ERT . box ( a1 . equalsExactly ( a2 ) ) ; } @ BIF ( name = "<STR_LIT>" ) public static EObject append ( EObject l1 , EObject l2 ) { ESeq ll1 = l1 . testSeq ( ) ; if ( ll1 == null ) throw ERT . badarg ( l1 , l2 ) ; return l2 . prepend ( ll1 ) ; } @ BIF public static EAtom is_list ( EObject o ) { return ERT . box ( o . testCons ( ) != null || o . testNil ( ) != null ) ; } @ BIF public static EAtom is_nil ( EObject o ) { return ERT . box ( o . testNil ( ) != null ) ; } @ BIF public static EString atom_to_list ( EObject atom ) { EAtom am = atom . testAtom ( ) ; if ( am == null ) throw ERT . badarg ( atom ) ; return new EString ( am . getName ( ) ) ; } @ BIF public static EObject process_flag ( EProc proc , EObject a1 , EObject a2 ) { return proc . process_flag ( a1 . testAtom ( ) , a2 ) ; } @ BIF public static ESeq nodes ( ) { return EPeer . getRemoteNodes ( ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_atom$p ( EObject obj ) { return ERT . guard ( obj . testAtom ( ) != null ) ; } @ BIF public static EAtom is_atom ( EObject obj ) { return ( obj . testAtom ( ) != null ) ? ERT . TRUE : ERT . FALSE ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_list$p ( EObject obj ) { return ERT . guard ( obj . testCons ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_tuple$p ( EObject obj ) { return ERT . guard ( obj . testTuple ( ) != null ) ; } @ BIF public static EAtom is_tuple ( EObject obj ) { return ERT . box ( obj . testTuple ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_binary$p ( EObject obj ) { return ERT . guard ( obj . testBinary ( ) != null ) ; } @ BIF public static EAtom is_binary ( EObject obj ) { return ERT . box ( obj . testBinary ( ) != null ) ; } @ BIF public static EAtom is_bitstring ( EObject obj ) { return ERT . box ( obj . testBitString ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_bitstring$g ( EObject obj ) { return ERT . guard ( obj . testBitString ( ) != null ) ; } @ BIF public static EAtom is_boolean ( EObject obj ) { return ERT . box ( obj == ERT . TRUE || obj == ERT . FALSE ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EAtom is_boolean$g ( EObject obj ) { return ERT . guard ( obj == ERT . TRUE || obj == ERT . FALSE ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_integer$p ( EObject obj ) { return ERT . guard ( obj . testInteger ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_float$g ( EObject obj ) { return ERT . guard ( obj . testFloat ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_number$g ( EObject obj ) { return ERT . guard ( obj . testNumber ( ) != null ) ; } @ BIF public static EAtom is_function ( EObject obj ) { return ERT . box ( obj . testFunction ( ) != null ) ; } @ BIF public static EAtom is_function ( EObject obj , ESmall num ) { return ERT . box ( obj . testFunction2 ( num . value ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_function_guard ( EObject obj ) { return ERT . guard ( obj . testFunction ( ) != null ) ; } @ BIF public static EAtom is_reference ( EObject obj ) { return ERT . box ( obj . testReference ( ) != null ) ; } @ BIF public static EAtom is_pid ( EObject obj ) { return ERT . box ( obj . testPID ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_pid_guard ( EObject obj ) { return ERT . guard ( obj . testPID ( ) != null ) ; } @ BIF public static EAtom is_port ( EObject obj ) { return ERT . box ( obj . testPort ( ) != null ) ; } @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) public static EAtom is_port$g ( EObject obj ) { return ERT . guard ( obj . testPort ( ) != null ) ; } @ BIF public static ESeq loaded ( ) { return EModuleManager . loaded_modules ( ) ; } @ BIF public static EAtom delete_module ( EObject m ) { EAtom mod = m . testAtom ( ) ; if ( mod == null ) throw ERT . badarg ( m ) ; return EModuleManager . delete_module ( mod ) ; } @ BIF public static ETuple2 load_module ( EProc proc , EObject mod , EObject bin ) throws Pausable { EAtom name = mod . testAtom ( ) ; EBinary binary = bin . testBinary ( ) ; return load_module ( proc , name , binary ) ; } @ BIF public static ETuple2 load_module ( EProc proc , EAtom mod , EBinary bin ) { if ( mod == null || bin == null ) throw ERT . badarg ( mod , bin ) ; try { ERT . load_module ( mod , bin ) ; } catch ( ErlangException e ) { log . log ( Level . FINE , "<STR_LIT>" , e ) ; return new ETuple2 ( ERT . am_error , e . reason ( ) ) ; } catch ( ThreadDeath e ) { throw e ; } catch ( Throwable e ) { ErlangError ee = new ErlangError ( ERT . am_badfile , e , mod , bin ) ; ETuple2 result = new ETuple2 ( ERT . am_error , ee . reason ( ) ) ; log . log ( Level . SEVERE , "<STR_LIT>" + mod , e ) ; return result ; } return new ETuple2 ( ERT . am_module , mod ) ; } @ BIF public static ETuple make_tuple ( EObject arity , EObject initial ) { ESmall sm = arity . testSmall ( ) ; if ( sm == null || sm . value < <NUM_LIT:0> ) throw ERT . badarg ( arity , initial ) ; ETuple et = ETuple . make ( sm . value ) ; for ( int i = <NUM_LIT:1> ; i <= sm . value ; i ++ ) { et . set ( i , initial ) ; } return et ; } @ BIF public static EAtom is_integer ( EObject o ) { return ERT . box ( o . testInteger ( ) != null ) ; } @ BIF public static EAtom is_float ( EObject o ) { return ERT . box ( o . testFloat ( ) != null ) ; } @ BIF public static EAtom is_number ( EObject o ) { return ERT . box ( o . testNumber ( ) != null ) ; } @ BIF public static ESmall tuple_size ( ETuple tup ) { return ERT . box ( tup . arity ( ) ) ; } @ BIF public static ESmall tuple_size ( EObject tup ) { ETuple t ; if ( ( t = tup . testTuple ( ) ) == null ) throw ERT . badarg ( tup ) ; return ERT . box ( t . arity ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static ESmall tuple_size_guard ( EObject tup ) { ETuple t ; if ( ( t = tup . testTuple ( ) ) == null ) return null ; return ERT . box ( t . arity ( ) ) ; } @ BIF public static ESmall byte_size ( EObject o ) { EBitString bin = o . testBitString ( ) ; if ( bin == null ) throw ERT . badarg ( o ) ; return ERT . box ( bin . totalByteSize ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static ESmall byte_size_guard ( EObject o ) { EBitString bin = o . testBitString ( ) ; if ( bin == null ) return null ; return ERT . box ( bin . totalByteSize ( ) ) ; } @ BIF public static EInteger bit_size ( EObject o ) { EBitString bin = o . testBitString ( ) ; if ( bin == null ) throw ERT . badarg ( o ) ; return ERT . box ( bin . bitSize ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bit_size_guard ( EObject o ) { EBitString bin = o . testBitString ( ) ; if ( bin == null ) return null ; return ERT . box ( bin . bitSize ( ) ) ; } @ BIF public static EAtom or ( EObject o1 , EObject o2 ) { Boolean b1 = ERT . asBoolean ( o1 ) ; Boolean b2 = ERT . asBoolean ( o2 ) ; if ( b1 == null || b2 == null ) throw ERT . badarg ( o1 , o2 ) ; return ERT . box ( b1 . booleanValue ( ) || b2 . booleanValue ( ) ) ; } @ BIF public static EAtom and ( EObject o1 , EObject o2 ) { Boolean b1 = ERT . asBoolean ( o1 ) ; Boolean b2 = ERT . asBoolean ( o2 ) ; if ( b1 == null || b2 == null ) throw ERT . badarg ( o1 , o2 ) ; return ERT . box ( b1 . booleanValue ( ) && b2 . booleanValue ( ) ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EAtom or$g ( EObject o1 , EObject o2 ) { if ( o1 == ERT . TRUE ) { if ( o2 == ERT . TRUE || o2 == ERT . FALSE ) return ERT . TRUE ; } else if ( o2 == ERT . TRUE ) { if ( o1 == ERT . FALSE ) return ERT . TRUE ; } else if ( o1 == ERT . FALSE && o2 == ERT . FALSE ) { return ERT . FALSE ; } return null ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EAtom and$g ( EObject o1 , EObject o2 ) { return ERT . guard ( o1 == ERT . TRUE && o2 == ERT . TRUE ) ; } @ BIF public static EAtom not ( EObject o1 ) { if ( o1 . testBoolean ( ) == null ) throw ERT . badarg ( o1 ) ; return ERT . box ( o1 == ERT . FALSE ) ; } @ BIF public static EInteger bnot ( EObject o ) { return o . bnot ( ) ; } @ BIF public static EInteger bor ( EObject o1 , EObject o2 ) { return o1 . bor ( o2 ) ; } @ BIF public static EInteger bxor ( EObject o1 , EObject o2 ) { return o1 . bxor ( o2 ) ; } @ BIF public static EInteger band ( EObject o1 , EObject o2 ) { return o1 . band ( o2 ) ; } @ BIF public static EInteger band ( EObject o1 , ESmall o2 ) { return o2 . band ( o1 ) ; } @ BIF public static EInteger bsl ( EObject o1 , EObject o2 ) { return o1 . bsl ( o2 ) ; } @ BIF public static EInteger bsr ( EObject o1 , EObject o2 ) { return o1 . bsr ( o2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EAtom not$g ( EObject o1 ) { if ( o1 . testBoolean ( ) == null ) return null ; return ERT . box ( o1 == ERT . FALSE ) ; } @ BIF public static EAtom xor ( EObject o1 , EObject o2 ) { EAtom a1 , a2 ; if ( ( a1 = o1 . testBoolean ( ) ) == null || ( a2 = o2 . testBoolean ( ) ) == null ) throw ERT . badarg ( o1 , o2 ) ; boolean b1 = a1 == ERT . TRUE , b2 = a2 == ERT . TRUE ; return ERT . box ( b1 ^ b2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bnot$g ( EObject o ) { EInteger i ; if ( ( i = o . testInteger ( ) ) == null ) return null ; return i . bnot ( ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bor$g ( EObject o1 , EObject o2 ) { EInteger i1 ; EInteger i2 ; if ( ( i1 = o1 . testInteger ( ) ) == null || ( i2 = o2 . testInteger ( ) ) == null ) return null ; return i1 . bor ( i2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bxor$g ( EObject o1 , EObject o2 ) { EInteger i1 ; EInteger i2 ; if ( ( i1 = o1 . testInteger ( ) ) == null || ( i2 = o2 . testInteger ( ) ) == null ) return null ; return i1 . bxor ( i2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger band$g ( EObject o1 , EObject o2 ) { EInteger i1 ; EInteger i2 ; if ( ( i1 = o1 . testInteger ( ) ) == null || ( i2 = o2 . testInteger ( ) ) == null ) return null ; return i1 . band ( i2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bsl$g ( EObject o1 , EObject o2 ) { EInteger i1 ; EInteger i2 ; if ( ( i1 = o1 . testInteger ( ) ) == null || ( i2 = o2 . testInteger ( ) ) == null ) return null ; return i1 . bsl ( i2 ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) public static EInteger bsr$g ( EObject o1 , EObject o2 ) { EInteger i1 ; EInteger i2 ; if ( ( i1 = o1 . testInteger ( ) ) == null || ( i2 = o2 . testInteger ( ) ) == null ) return null ; return i1 . bsr ( i2 ) ; } @ BIF public static EObject yield ( ) throws Pausable { Task . yield ( ) ; Task . getCurrentTask ( ) . checkKill ( ) ; return ERT . TRUE ; } @ BIF public static EObject bump_reductions ( EProc self , EObject howmuch ) throws Pausable { Task . yield ( ) ; Task . getCurrentTask ( ) . checkKill ( ) ; return ERT . box ( <NUM_LIT:1> ) ; } @ BIF public static ETuple2 localtime ( ) { Calendar c = GregorianCalendar . getInstance ( ) ; ETuple3 date = new ETuple3 ( ) ; date . set ( <NUM_LIT:1> , ERT . box ( c . get ( Calendar . YEAR ) ) ) ; date . set ( <NUM_LIT:2> , ERT . box ( c . get ( Calendar . MONTH ) - Calendar . JANUARY + <NUM_LIT:1> ) ) ; date . set ( <NUM_LIT:3> , ERT . box ( c . get ( Calendar . DAY_OF_MONTH ) ) ) ; ETuple3 time = new ETuple3 ( ) ; time . set ( <NUM_LIT:1> , ERT . box ( c . get ( Calendar . HOUR_OF_DAY ) ) ) ; time . set ( <NUM_LIT:2> , ERT . box ( c . get ( Calendar . MINUTE ) ) ) ; time . set ( <NUM_LIT:3> , ERT . box ( c . get ( Calendar . SECOND ) ) ) ; return new ETuple2 ( date , time ) ; } @ BIF public static ETuple2 universaltime ( ) { Calendar c = GregorianCalendar . getInstance ( UTC_TIME_ZONE ) ; ETuple3 date = new ETuple3 ( ) ; date . set ( <NUM_LIT:1> , ERT . box ( c . get ( Calendar . YEAR ) ) ) ; date . set ( <NUM_LIT:2> , ERT . box ( c . get ( Calendar . MONTH ) - Calendar . JANUARY + <NUM_LIT:1> ) ) ; date . set ( <NUM_LIT:3> , ERT . box ( c . get ( Calendar . DAY_OF_MONTH ) ) ) ; ETuple3 time = new ETuple3 ( ) ; time . set ( <NUM_LIT:1> , ERT . box ( c . get ( Calendar . HOUR_OF_DAY ) ) ) ; time . set ( <NUM_LIT:2> , ERT . box ( c . get ( Calendar . MINUTE ) ) ) ; time . set ( <NUM_LIT:3> , ERT . box ( c . get ( Calendar . SECOND ) ) ) ; return new ETuple2 ( date , time ) ; } @ BIF static public EObject localtime_to_universaltime ( EObject a1 ) { return localtime_to_universaltime ( a1 , ERT . am_undefined ) ; } @ BIF static public EObject localtime_to_universaltime ( EObject a1 , EObject a2 ) { ETuple2 dt ; if ( ( dt = ETuple2 . cast ( a1 ) ) != null ) { ETuple3 date ; ETuple3 time ; ESmall year ; ESmall month ; ESmall day ; ESmall hour ; ESmall minute ; ESmall sec ; if ( ( date = ETuple3 . cast ( dt . elem1 ) ) != null && ( year = date . elem1 . testSmall ( ) ) != null && ( month = date . elem2 . testSmall ( ) ) != null && ( day = date . elem3 . testSmall ( ) ) != null && ( time = ETuple3 . cast ( dt . elem2 ) ) != null && ( hour = time . elem1 . testSmall ( ) ) != null && ( minute = time . elem2 . testSmall ( ) ) != null && ( sec = time . elem3 . testSmall ( ) ) != null ) { Calendar in_date = GregorianCalendar . getInstance ( ) ; in_date . set ( Calendar . YEAR , year . value ) ; in_date . set ( Calendar . MONTH , month . value - <NUM_LIT:1> + Calendar . JANUARY ) ; in_date . set ( Calendar . DAY_OF_MONTH , day . value ) ; in_date . set ( Calendar . HOUR_OF_DAY , hour . value ) ; in_date . set ( Calendar . MINUTE , minute . value ) ; in_date . set ( Calendar . SECOND , sec . value ) ; Calendar out_date = GregorianCalendar . getInstance ( UTC_TIME_ZONE ) ; out_date . setTimeInMillis ( in_date . getTimeInMillis ( ) ) ; ETuple3 date2 = new ETuple3 ( ) ; date2 . set ( <NUM_LIT:1> , ERT . box ( out_date . get ( Calendar . YEAR ) ) ) ; date2 . set ( <NUM_LIT:2> , ERT . box ( out_date . get ( Calendar . MONTH ) - Calendar . JANUARY + <NUM_LIT:1> ) ) ; date2 . set ( <NUM_LIT:3> , ERT . box ( out_date . get ( Calendar . DAY_OF_MONTH ) ) ) ; ETuple3 time2 = new ETuple3 ( ) ; time2 . set ( <NUM_LIT:1> , ERT . box ( out_date . get ( Calendar . HOUR_OF_DAY ) ) ) ; time2 . set ( <NUM_LIT:2> , ERT . box ( out_date . get ( Calendar . MINUTE ) ) ) ; time2 . set ( <NUM_LIT:3> , ERT . box ( out_date . get ( Calendar . SECOND ) ) ) ; return new ETuple2 ( date2 , time2 ) ; } } throw ERT . badarg ( a1 , a2 ) ; } @ BIF static public EObject universaltime_to_localtime ( EObject a1 ) { ETuple2 dt ; if ( ( dt = ETuple2 . cast ( a1 ) ) != null ) { ETuple3 date ; ETuple3 time ; ESmall year ; ESmall month ; ESmall day ; ESmall hour ; ESmall minute ; ESmall sec ; if ( ( date = ETuple3 . cast ( dt . elem1 ) ) != null && ( year = date . elem1 . testSmall ( ) ) != null && ( month = date . elem2 . testSmall ( ) ) != null && ( day = date . elem3 . testSmall ( ) ) != null && ( time = ETuple3 . cast ( dt . elem2 ) ) != null && ( hour = time . elem1 . testSmall ( ) ) != null && ( minute = time . elem2 . testSmall ( ) ) != null && ( sec = time . elem3 . testSmall ( ) ) != null ) { Calendar in_date = GregorianCalendar . getInstance ( UTC_TIME_ZONE ) ; in_date . set ( Calendar . YEAR , year . value ) ; in_date . set ( Calendar . MONTH , month . value - <NUM_LIT:1> + Calendar . JANUARY ) ; in_date . set ( Calendar . DAY_OF_MONTH , day . value ) ; in_date . set ( Calendar . HOUR_OF_DAY , hour . value ) ; in_date . set ( Calendar . MINUTE , minute . value ) ; in_date . set ( Calendar . SECOND , sec . value ) ; Calendar out_date = GregorianCalendar . getInstance ( ) ; out_date . setTimeInMillis ( in_date . getTimeInMillis ( ) ) ; ETuple3 date2 = new ETuple3 ( ) ; date2 . set ( <NUM_LIT:1> , ERT . box ( out_date . get ( Calendar . YEAR ) ) ) ; date2 . set ( <NUM_LIT:2> , ERT . box ( out_date . get ( Calendar . MONTH ) - Calendar . JANUARY + <NUM_LIT:1> ) ) ; date2 . set ( <NUM_LIT:3> , ERT . box ( out_date . get ( Calendar . DAY_OF_MONTH ) ) ) ; ETuple3 time2 = new ETuple3 ( ) ; time2 . set ( <NUM_LIT:1> , ERT . box ( out_date . get ( Calendar . HOUR_OF_DAY ) ) ) ; time2 . set ( <NUM_LIT:2> , ERT . box ( out_date . get ( Calendar . MINUTE ) ) ) ; time2 . set ( <NUM_LIT:3> , ERT . box ( out_date . get ( Calendar . SECOND ) ) ) ; return new ETuple2 ( date2 , time2 ) ; } } throw ERT . badarg ( a1 ) ; } @ BIF static public EObject system_flag ( EObject flag_arg , EObject value ) { throw new NotImplemented ( ) ; } static EObject sysmon_pid = ERT . am_undefined ; @ BIF static public EObject system_monitor ( EObject pid , EObject opts ) { EPID spid = pid . testPID ( ) ; if ( spid == null ) throw ERT . badarg ( pid , opts ) ; sysmon_pid = pid ; return system_monitor ( ) ; } @ BIF static public EObject system_monitor ( ) { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; return new ETuple2 ( sysmon_pid , ERT . NIL ) ; } @ BIF static public EObject memory ( EObject type ) { Runtime runtime = Runtime . getRuntime ( ) ; if ( type == am_total ) { return ERT . box ( runtime . totalMemory ( ) - runtime . freeMemory ( ) ) ; } else { throw ERT . notsup ( ) ; } } @ BIF public static EObject md5_init ( EProc self ) { MessageDigest md ; try { md = MessageDigest . getInstance ( "<STR_LIT>" ) ; } catch ( NoSuchAlgorithmException e ) { throw new NotImplemented ( ) ; } return new JavaObject ( self , md ) ; } @ BIF public static EObject md5_update ( EProc self , EObject context , EObject iolist_arg ) { List < ByteBuffer > buf = new ArrayList < ByteBuffer > ( ) ; if ( ! iolist_arg . collectIOList ( buf ) ) { throw ERT . badarg ( context , iolist_arg ) ; } JavaObject jo ; if ( ( jo = context . testJavaObject ( ) ) != null && ( jo . realObject ( ) instanceof MessageDigest ) ) { MessageDigest md = ( MessageDigest ) jo . realObject ( ) ; for ( int i = <NUM_LIT:0> ; i < buf . size ( ) ; i ++ ) { md . update ( buf . get ( i ) ) ; } return new JavaObject ( self , md ) ; } throw ERT . badarg ( context , iolist_arg ) ; } @ BIF public static EObject md5_final ( EProc self , EObject context ) { JavaObject jo ; if ( ( jo = context . testJavaObject ( ) ) != null && ( jo . realObject ( ) instanceof MessageDigest ) ) { MessageDigest md = ( MessageDigest ) jo . realObject ( ) ; byte [ ] res = md . digest ( ) ; return EBinary . make ( res , <NUM_LIT:0> , res . length , <NUM_LIT:0> ) ; } throw ERT . badarg ( context ) ; } @ BIF public static EObject md5 ( EObject iolist_arg ) { List < ByteBuffer > buf = new ArrayList < ByteBuffer > ( ) ; if ( ! iolist_arg . collectIOList ( buf ) ) { throw ERT . badarg ( iolist_arg ) ; } MessageDigest md ; try { md = MessageDigest . getInstance ( "<STR_LIT>" ) ; } catch ( NoSuchAlgorithmException e ) { throw new NotImplemented ( ) ; } for ( int i = <NUM_LIT:0> ; i < buf . size ( ) ; i ++ ) { md . update ( buf . get ( i ) ) ; } byte [ ] res = md . digest ( ) ; return EBinary . make ( res , <NUM_LIT:0> , res . length , <NUM_LIT:0> ) ; } @ BIF public static EObject raise ( EObject kind , EObject value , EObject trace ) throws ErlangException { EAtom clazz = kind . testAtom ( ) ; ESeq traz = trace . testSeq ( ) ; if ( traz == null ) { return ERT . am_badarg ; } if ( clazz == ERT . am_exit || clazz == ERT . am_error || clazz == ERT . am_throw ) throw new ErlangRaise ( clazz , value , traz ) ; return ERT . am_badarg ; } @ BIF public static ESeq registered ( ) { return ERT . registered ( ) ; } @ BIF public static EAtom breakpoint ( ) { return ERT . am_ok ; } } </s>
|
<s> package erjang . m . erlang ; import java . lang . management . ManagementFactory ; import java . lang . management . MemoryMXBean ; import java . lang . management . MemoryPoolMXBean ; import java . lang . management . MemoryType ; import java . lang . management . MemoryUsage ; import java . util . List ; import java . util . logging . Level ; import java . util . logging . Logger ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAbstractNode ; import erjang . EAtom ; import erjang . ECons ; import erjang . EFun ; import erjang . EHandle ; import erjang . EModuleManager ; import erjang . EObject ; import erjang . EPID ; import erjang . EPeer ; import erjang . EProc ; import erjang . ERT ; import erjang . ERef ; import erjang . ESeq ; import erjang . ESmall ; import erjang . EString ; import erjang . ETask ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . ErjangHibernateException ; import erjang . ErlangException ; import erjang . ErlangExit ; import erjang . ErlangHalt ; import erjang . ErlangUndefined ; import erjang . FunID ; import erjang . Import ; import erjang . Main ; import erjang . NotImplemented ; public class ErlProc { private static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private static final EAtom am_smp_support = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_schedulers = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_break_ignored = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_threads = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_process = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_wordsize = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_thread_pool_size = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_os_type = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_win32 = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_unix = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_version = EAtom . intern ( "<STR_LIT:version>" ) ; private static final EAtom am_undefined = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_heap = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_non_heap = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_jvm = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_allocated_areas = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_otp_release = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_driver_version = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_global_heaps_size = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_process_count = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_system_architecture = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_logical_processors = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_hipe_architecture = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_machine = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_link = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_monitor = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_priority = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_system_version = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_flush = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_shutdown = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_ets_alloc = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_error_checker = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_debug_compiled = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_lock_checking = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_compat_rel = EAtom . intern ( "<STR_LIT>" ) ; @ BIF public static EObject process_info ( EObject pid , EObject what ) { EPID p = pid . testPID ( ) ; if ( p == null ) throw ERT . badarg ( pid , what ) ; return p . process_info ( what ) ; } @ BIF public static EObject process_info ( EObject pid ) { EPID p = pid . testPID ( ) ; if ( p == null ) throw ERT . badarg ( pid ) ; return p . process_info ( ) ; } @ BIF public static EObject display ( EProc proc , EObject obj ) { ERT . getOutputStream ( ) . println ( obj ) ; return ERT . TRUE ; } @ BIF public static ESeq get_stacktrace ( EProc proc ) { ErlangException ex = proc . getLastException ( ) ; return ex != null ? ex . getTrace ( ) : ERT . NIL ; } @ BIF public static EObject erase ( EProc proc , EObject key ) { return proc . erase ( key ) ; } @ BIF public static EObject erase ( EProc proc ) { return proc . erase ( ) ; } @ BIF public static EObject register ( EObject name , EObject pid ) { EAtom aname ; EHandle handle = pid . testHandle ( ) ; if ( ( aname = name . testAtom ( ) ) == null || handle == null ) throw ERT . badarg ( name , pid ) ; ERT . register ( aname , handle ) ; return ERT . TRUE ; } @ BIF public static EObject unregister ( EObject name ) { EAtom aname ; if ( ( aname = name . testAtom ( ) ) == null || ! ERT . unregister ( aname ) ) throw ERT . badarg ( name ) ; return ERT . TRUE ; } @ BIF public static EObject spawn_link ( EProc proc , EObject mod , EObject fun , EObject args ) throws Pausable { EAtom m = mod . testAtom ( ) ; EAtom f = fun . testAtom ( ) ; ESeq a = args . testSeq ( ) ; if ( m == null || f == null || a == null ) throw ERT . badarg ( mod , fun , args ) ; EProc p2 = new EProc ( proc . group_leader ( ) , m , f , a ) ; p2 . link_to ( proc ) ; ERT . run ( p2 ) ; return p2 . self_handle ( ) ; } @ BIF public static EObject spawn_opt ( EProc self , EObject tup ) throws Pausable { ETuple t ; EAtom m ; EAtom f ; ESeq a ; ESeq o ; if ( ( t = tup . testTuple ( ) ) == null || t . arity ( ) != <NUM_LIT:4> || ( m = t . elm ( <NUM_LIT:1> ) . testAtom ( ) ) == null || ( f = t . elm ( <NUM_LIT:2> ) . testAtom ( ) ) == null || ( a = t . elm ( <NUM_LIT:3> ) . testSeq ( ) ) == null || ( o = t . elm ( <NUM_LIT:4> ) . testSeq ( ) ) == null ) throw ERT . badarg ( tup ) ; boolean link = false ; boolean monitor = false ; EAtom priority = null ; for ( ; ! o . isNil ( ) ; o = o . tail ( ) ) { EObject val = o . head ( ) ; ETuple2 t2 ; if ( val == am_link ) { link = true ; } else if ( val == am_monitor ) { monitor = true ; } else if ( ( t2 = ETuple2 . cast ( val ) ) != null ) { if ( t2 . elm ( <NUM_LIT:1> ) == am_priority ) { EAtom am = t2 . elm ( <NUM_LIT:2> ) . testAtom ( ) ; if ( am != null ) priority = am ; } } } EProc p2 = new EProc ( self . group_leader ( ) , m , f , a ) ; if ( link ) { p2 . link_to ( self ) ; } if ( priority != null ) { p2 . process_flag ( am_priority , priority ) ; } ERef ref = null ; if ( monitor ) { ref = ERT . getLocalNode ( ) . createRef ( ) ; if ( ! self . monitor ( p2 . self_handle ( ) , p2 . self_handle ( ) , ref ) ) { throw new InternalError ( "<STR_LIT>" ) ; } } ERT . run ( p2 ) ; if ( monitor ) { return new ETuple2 ( p2 . self_handle ( ) , ref ) ; } else { return p2 . self_handle ( ) ; } } @ BIF public static EObject spawn ( EProc proc , EObject mod , EObject fun , EObject args ) { EAtom m = mod . testAtom ( ) ; EAtom f = fun . testAtom ( ) ; ESeq a = args . testSeq ( ) ; if ( m == null || f == null || a == null ) throw ERT . badarg ( mod , fun , args ) ; EProc p2 = new EProc ( proc . group_leader ( ) , m , f , a ) ; ERT . run ( p2 ) ; return p2 . self_handle ( ) ; } @ BIF public static EObject halt ( EProc proc ) { return halt ( proc , null ) ; } @ BIF public static EObject halt ( EProc proc , EObject value ) { int exitCode = <NUM_LIT:1> ; String message = null ; if ( value != null ) { ESmall val = value . testSmall ( ) ; if ( val != null ) { exitCode = val . value ; } EString str = value . testString ( ) ; if ( str != null ) { message = str . stringValue ( ) ; } } if ( message != null ) { log . severe ( "<STR_LIT>" + message ) ; } else { log . info ( "<STR_LIT>" ) ; } ERT . shutdown ( ) ; throw new ErlangHalt ( ) ; } @ BIF public static EObject unlink ( EProc self , EObject pid ) throws Pausable { EHandle h = EHandle . cast ( pid ) ; if ( h != null ) { self . unlink ( h ) ; } return pid ; } @ BIF static public EObject link ( EProc self , EObject pid ) throws Pausable { EHandle h = EHandle . cast ( pid ) ; if ( h == null ) throw ERT . badarg ( pid ) ; self . link_to ( h ) ; return ERT . TRUE ; } @ BIF static public EObject monitor ( EProc self , EObject how , EObject object ) throws Pausable { if ( how != am_process ) throw ERT . badarg ( how , object ) ; EHandle h = EHandle . cast ( object ) ; if ( h != null ) { ERef ref = ERT . getLocalNode ( ) . createRef ( ) ; if ( ! self . monitor ( h , h , ref ) ) { self . mbox_send ( ETuple . make ( ERT . am_DOWN , ref , am_process , object , ERT . am_noproc ) ) ; } return ref ; } EAtom name ; if ( h == null && ( name = object . testAtom ( ) ) != null ) { ERef ref = ERT . getLocalNode ( ) . createRef ( ) ; boolean success = false ; object = new ETuple2 ( name , ErlDist . node ( ) ) ; if ( ( h = ERT . whereis ( name ) . testHandle ( ) ) != null ) { success = self . monitor ( h , object , ref ) ; } if ( ! success ) { self . mbox_send ( ETuple . make ( ERT . am_DOWN , ref , am_process , object , ERT . am_noproc ) ) ; } return ref ; } ETuple tup ; EAtom node ; if ( ( tup = object . testTuple ( ) ) != null && tup . arity ( ) == <NUM_LIT:2> && ( name = tup . elm ( <NUM_LIT:1> ) . testAtom ( ) ) != null && ( node = tup . elm ( <NUM_LIT:2> ) . testAtom ( ) ) != null ) { if ( node == ErlDist . node ( ) ) { ERef ref = ERT . getLocalNode ( ) . createRef ( ) ; boolean success = false ; if ( ( h = ERT . whereis ( name ) . testHandle ( ) ) != null ) { success = self . monitor ( h , object , ref ) ; } if ( ! success ) { self . mbox_send ( ETuple . make ( ERT . am_DOWN , ref , am_process , object , ERT . am_noproc ) ) ; } return ref ; } else { EPeer peer = ( EPeer ) EPeer . get ( node ) ; if ( peer != null ) { ERef ref = ERT . getLocalNode ( ) . createRef ( ) ; self . monitor ( tup , ref ) ; peer . dsig_monitor ( self . self_handle ( ) , name , ref ) ; return ref ; } return ErlDist . dmonitor_p2_trap . invoke ( self , new EObject [ ] { how , object } ) ; } } throw ERT . badarg ( how , object ) ; } @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:2> ) static EFun flush_monitor_message = null ; @ BIF static public EObject demonitor ( EProc self , EObject ref ) throws Pausable { return demonitor ( self , ref , ERT . NIL ) ; } @ BIF static public EObject demonitor ( EProc self , EObject ref , EObject options ) throws Pausable { return demonitor ( ( ETask < ? > ) self , ref , options ) ; } static public EObject demonitor ( ETask < ? > self , EObject ref , EObject options ) throws Pausable { ERef r = ref . testReference ( ) ; ESeq o = options . testSeq ( ) ; if ( r == null || o == null ) throw ERT . badarg ( ref , options ) ; boolean flush = ( ! o . isNil ( ) && o . head ( ) == am_flush ) ; EObject found = self . demonitor ( r ) ; if ( found == null ) { return ERT . FALSE ; } EHandle h ; ETuple tup ; EAtom name ; EAtom node ; if ( ( h = found . testHandle ( ) ) != null ) { h . remove_monitor ( self . self_handle ( ) , r , flush ) ; } else if ( ( tup = found . testTuple ( ) ) != null && tup . arity ( ) == <NUM_LIT:2> && ( name = tup . elm ( <NUM_LIT:1> ) . testAtom ( ) ) != null && ( node = tup . elm ( <NUM_LIT:2> ) . testAtom ( ) ) != null ) { EAbstractNode n = EAbstractNode . get_or_connect ( self , node ) ; if ( n != null ) { n . dsig_demonitor ( self . self_handle ( ) , r , name ) ; } } if ( flush && ( self instanceof EProc ) ) { flush_monitor_message . invoke ( ( EProc ) self , new EObject [ ] { ref , ERT . am_ok } ) ; } return ERT . TRUE ; } @ BIF public static EAtom exit ( EProc proc , EObject p , EObject reason ) throws Pausable { EHandle pid = p . testHandle ( ) ; if ( pid == null ) throw ERT . badarg ( p , reason ) ; if ( pid == proc . self_handle ( ) ) { throw new ErlangExit ( reason ) ; } pid . exit_signal ( proc . self_handle ( ) , reason , true ) ; return ERT . TRUE ; } @ BIF public static EObject exit ( EObject a1 ) { throw new ErlangExit ( a1 ) ; } @ BIF public static ERef make_ref ( EProc proc ) { return ERT . getLocalNode ( ) . createRef ( ) ; } @ BIF static EObject group_leader ( EProc proc ) { return proc . group_leader ( ) ; } @ BIF static EObject group_leader ( EObject group_leader , EObject pid ) { EPID p = pid . testPID ( ) ; EPID gl = group_leader . testPID ( ) ; if ( p == null || gl == null ) throw ERT . badarg ( group_leader , pid ) ; p . set_group_leader ( gl ) ; return ERT . TRUE ; } static EAtom am_allocator = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_heap_type = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_shared = EAtom . intern ( "<STR_LIT>" ) ; @ BIF static EObject system_info ( EProc proc , EObject type ) { if ( type == am_machine ) { return EString . fromString ( "<STR_LIT>" ) ; } else if ( type == am_smp_support ) { return ERT . TRUE ; } else if ( type == am_schedulers ) { return ERT . box ( ERT . threadPoolSize ( ) ) ; } else if ( type == am_threads ) { return ERT . box ( true ) ; } else if ( type == am_thread_pool_size ) { return ERT . box ( ERT . asyncThreadPoolSize ( ) ) ; } else if ( type == am_break_ignored ) { return ERT . box ( false ) ; } else if ( type == am_compat_rel ) { return new ESmall ( <NUM_LIT> ) ; } ETuple2 tup ; if ( type == am_allocated_areas ) { ECons res = ERT . NIL ; List < MemoryPoolMXBean > bean2 = ManagementFactory . getMemoryPoolMXBeans ( ) ; if ( bean2 == null ) { MemoryMXBean bean = ManagementFactory . getMemoryMXBean ( ) ; if ( bean != null ) { MemoryUsage mu = bean . getHeapMemoryUsage ( ) ; res = res . cons ( ETuple . make ( am_heap , ERT . box ( mu . getCommitted ( ) ) , ERT . box ( mu . getUsed ( ) ) ) ) ; mu = bean . getNonHeapMemoryUsage ( ) ; res = res . cons ( ETuple . make ( am_non_heap , ERT . box ( mu . getCommitted ( ) ) , ERT . box ( mu . getUsed ( ) ) ) ) ; } return res ; } for ( MemoryPoolMXBean mb : bean2 ) { String name = mb . getName ( ) ; MemoryUsage mu = mb . getUsage ( ) ; if ( mu == null ) continue ; String name2 = ( mb . getType ( ) == MemoryType . HEAP ? "<STR_LIT>" : "<STR_LIT>" ) + name ; res = res . cons ( ETuple . make ( EAtom . intern ( name2 ) , ERT . box ( mu . getCommitted ( ) ) , ERT . box ( mu . getUsed ( ) ) ) ) ; } return res ; } else if ( type == am_allocator ) { return am_jvm ; } else if ( type == am_heap_type ) { return am_shared ; } else if ( type == am_smp_support ) { return ERT . TRUE ; } else if ( type == am_thread_pool_size ) { return new ESmall ( ERT . threadPoolSize ( ) ) ; } else if ( type == am_os_type ) { String os = System . getProperty ( "<STR_LIT>" ) ; if ( os . startsWith ( "<STR_LIT>" ) ) { return ETuple . make ( am_win32 , new EString ( os ) ) ; } else { return ETuple . make ( am_unix , new EString ( os ) ) ; } } else if ( type == am_threads ) { return ERT . TRUE ; } else if ( type == am_version ) { String erts_version = ERT . runtime_info . erts_version ; String prefix = "<STR_LIT>" ; if ( erts_version . startsWith ( prefix ) ) { erts_version = erts_version . substring ( prefix . length ( ) ) ; } return EString . fromString ( erts_version ) ; } else if ( type == am_otp_release ) { return new EString ( ERT . runtime_info . otp_version ) ; } else if ( type == am_logical_processors ) { return ERT . box ( Runtime . getRuntime ( ) . availableProcessors ( ) ) ; } else if ( type == am_global_heaps_size ) { return ERT . box ( Runtime . getRuntime ( ) . totalMemory ( ) ) ; } else if ( type == am_process_count ) { return ERT . box ( EProc . process_count ( ) ) ; } else if ( type == am_system_architecture ) { return new EString ( Main . SYSTEM_ARCHITECTURE ) ; } else if ( type == am_driver_version ) { return new EString ( Main . DRIVER_VERSION ) ; } else if ( type == am_wordsize ) { return new ESmall ( <NUM_LIT:4> ) ; } else if ( type == am_debug_compiled || type == am_lock_checking ) { throw ERT . badarg ( type ) ; } else if ( type == am_hipe_architecture ) { return am_undefined ; } else if ( type == am_system_version ) { return new EString ( "<STR_LIT>" + ERT . runtime_info . erts_version + "<STR_LIT:]>" ) ; } else if ( ( tup = ETuple2 . cast ( type ) ) != null ) { if ( tup . elem1 == am_allocator ) { if ( tup . elem2 == am_ets_alloc ) { return ERT . FALSE ; } } else if ( tup . elem1 == am_error_checker ) { throw ERT . badarg ( type ) ; } return am_undefined ; } else { log . info ( "<STR_LIT>" + type + "<STR_LIT>" ) ; throw ERT . badarg ( type ) ; } } @ BIF static EAtom module_loaded ( EObject mod ) { EAtom m ; if ( ( m = mod . testAtom ( ) ) == null ) throw ERT . badarg ( mod ) ; return EModuleManager . module_loaded ( m ) ? ERT . TRUE : ERT . FALSE ; } @ BIF static ESeq processes ( ) { return EProc . processes ( ) ; } @ BIF public static EAtom is_process_alive ( EObject p ) { EPID pid = p . testPID ( ) ; if ( pid == null ) throw ERT . badarg ( p ) ; return ERT . box ( pid . is_alive ( ) ) ; } @ BIF public static EObject suspend_process ( EObject a1 , EObject a2 ) { throw new NotImplemented ( ) ; } @ BIF public static EAtom check_process_code ( EObject pid_arg , EObject mod_arg ) { EPID pid = pid_arg . testPID ( ) ; EAtom mod = mod_arg . testAtom ( ) ; if ( pid == null || mod == null ) { throw ERT . badarg ( pid_arg , mod_arg ) ; } log . log ( Level . FINE , "<STR_LIT>" + pid + "<STR_LIT:U+002CU+0020>" + mod + "<STR_LIT:)>" ) ; return ERT . FALSE ; } @ BIF public static EAtom purge_module ( EObject mod_arg ) { log . log ( Level . FINE , "<STR_LIT>" + mod_arg + "<STR_LIT:)>" ) ; return ERT . TRUE ; } @ BIF public static EObject hibernate ( EProc self , EObject a1 , EObject a2 , EObject a3 ) { EAtom m = a1 . testAtom ( ) ; EAtom f = a2 . testAtom ( ) ; ESeq a = a3 . testSeq ( ) ; if ( m == null || f == null || a == null ) { throw ERT . badarg ( a1 , a2 , a3 ) ; } int arity = a . length ( ) ; EFun target = EModuleManager . resolve ( new FunID ( m , f , arity ) ) ; if ( target == null ) { throw new ErlangUndefined ( m , f , new ESmall ( arity ) ) ; } self . tail = target ; a = a . reverse ( ) ; switch ( arity ) { default : throw new NotImplemented ( "<STR_LIT>" + arity + "<STR_LIT>" ) ; case <NUM_LIT:7> : self . arg6 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:6> : self . arg5 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:5> : self . arg4 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:4> : self . arg3 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:3> : self . arg2 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:2> : self . arg1 = a . head ( ) ; a = a . tail ( ) ; case <NUM_LIT:1> : self . arg0 = a . head ( ) ; case <NUM_LIT:0> : } throw ErjangHibernateException . INSTANCE ; } } </s>
|
<s> package erjang . m . erlang ; import erjang . BIF ; import erjang . EInteger ; import erjang . EObject ; import erjang . ERT ; import erjang . ESmall ; public class ErlHash { @ BIF static public ESmall hash ( EObject a1 , EObject a2 ) { ESmall range ; if ( ( range = a2 . testSmall ( ) ) == null || range . value <= <NUM_LIT:0> ) throw ERT . badarg ( a1 , a2 ) ; int hash = make_broken_hash ( a1 ) ; return ERT . box ( <NUM_LIT:1> + ( hash % range . value ) ) ; } private static int make_broken_hash ( EObject value ) { return value . hashCode ( ) ; } static EInteger I2POW32 = ERT . box ( <NUM_LIT> ) ; @ BIF public static EObject phash ( EObject a1 , EObject a2 ) { long hash , final_hash , range ; if ( I2POW32 . equalsExactly ( a2 ) ) { range = <NUM_LIT:0> ; } else { EInteger ival ; if ( ( ival = a2 . testInteger ( ) ) == null || ( range = ival . intValue ( ) ) == <NUM_LIT:0> || ( ( range & ~ <NUM_LIT> ) != <NUM_LIT:0> ) ) throw ERT . badarg ( a1 , a2 ) ; range = Math . abs ( range ) & <NUM_LIT> ; } hash = make_hash ( a1 ) & <NUM_LIT> ; if ( range != <NUM_LIT:0> ) { final_hash = <NUM_LIT:1> + ( hash % range ) ; } else { final_hash = hash + <NUM_LIT:1> ; } return ERT . box ( final_hash ) ; } private static int make_hash ( EObject value ) { return value . hashCode ( ) ; } @ BIF public static ESmall phash2 ( EObject value ) { int hash = make_hash2 ( value ) & <NUM_LIT> ; return ERT . box ( hash & ( ( <NUM_LIT:1> << <NUM_LIT> ) - <NUM_LIT:1> ) ) ; } private static int make_hash2 ( EObject value ) { return value . hashCode ( ) ; } @ BIF public static EObject phash2 ( EObject a1 , EObject a2 ) { long hash , final_hash , range ; if ( I2POW32 . equals ( a2 ) ) { range = <NUM_LIT:0> ; } else { EInteger ival ; if ( ( ival = a2 . testInteger ( ) ) == null || ( range = ival . intValue ( ) ) == <NUM_LIT:0> || ( ( range & ~ <NUM_LIT> ) != <NUM_LIT:0> ) ) throw ERT . badarg ( a1 , a2 ) ; range = Math . abs ( range ) & <NUM_LIT> ; } hash = make_hash2 ( a1 ) & <NUM_LIT> ; if ( range != <NUM_LIT:0> ) { final_hash = <NUM_LIT:1> + ( hash % range ) ; } else { final_hash = hash + <NUM_LIT:1> ; } return ERT . box ( final_hash ) ; } } </s>
|
<s> package erjang . m . erlang ; import java . util . logging . Level ; import java . util . logging . Logger ; import java . util . regex . Pattern ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAbstractNode ; import erjang . EAtom ; import erjang . EFun ; import erjang . EHandle ; import erjang . EInternalPID ; import erjang . EInternalPort ; import erjang . ENode ; import erjang . EObject ; import erjang . EPeer ; import erjang . EProc ; import erjang . ERT ; import erjang . ERef ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . ErlangError ; import erjang . Import ; import erjang . NotImplemented ; import erjang . BIF . Type ; import erjang . driver . EDriverTask ; public class ErlDist { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private static DistEntry this_dist_entry = null ; private static final EAtom am_net_kernel = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom am_Noname = EAtom . intern ( "<STR_LIT>" ) ; static { this_dist_entry = new DistEntry ( am_Noname , ( EInternalPort ) null ) ; } @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:1> ) public static EFun net_kernel__connect__1 ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:2> ) static EFun dsend2_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:3> ) static EFun dsend3_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:1> ) static EFun dlink1_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:1> ) static EFun dunlink1_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:3> ) static EFun dmonitor_node3_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:2> ) static EFun dgroup_leader2_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:2> ) static EFun dexit2_trap ; @ Import ( module = "<STR_LIT>" , fun = "<STR_LIT>" , arity = <NUM_LIT:2> ) static EFun dmonitor_p2_trap ; static EAtom erts_is_alive = ERT . FALSE ; @ BIF public static final EObject is_alive ( ) { return erts_is_alive ; } @ BIF public static EObject nodes ( EObject node ) { throw new NotImplemented ( ) ; } @ BIF public static EObject monitor_node ( EProc proc , EObject node , EObject flag , EObject opts ) throws Pausable { EAtom aname = node . testAtom ( ) ; EAtom aflag = flag . testAtom ( ) ; ESeq sopts = opts . testSeq ( ) ; if ( aname == null || aflag == null || sopts == null ) { throw ERT . badarg ( node , flag ) ; } EAbstractNode n = EPeer . get ( aname ) ; if ( n == null ) { return dmonitor_node3_trap . invoke ( proc , new EObject [ ] { aname , aflag , opts } ) ; } else { n . monitor_node ( proc . self_handle ( ) , aflag == ERT . TRUE ) ; return ERT . TRUE ; } } @ BIF public static EObject monitor_node ( EProc proc , EObject node , EObject flag ) throws Pausable { return monitor_node ( proc , node , flag , ERT . NIL ) ; } @ BIF public static EObject dist_exit ( EObject a1 , EObject a2 , EObject a3 ) { throw new NotImplemented ( ) ; } @ BIF static public EAtom node ( ) { EAtom val = ERT . getLocalNode ( ) . node ( ) ; return val ; } @ BIF static public EAtom node ( EObject name ) { if ( ! ERT . getLocalNode ( ) . isALive ( ) ) { return ENode . am_nonode_at_nohost ; } ERef ref ; if ( ( ref = name . testReference ( ) ) != null ) return ref . node ( ) ; EHandle handle ; if ( ( handle = name . testHandle ( ) ) != null ) return handle . node ( ) ; throw ERT . badarg ( name ) ; } @ BIF ( type = Type . GUARD , name = "<STR_LIT>" ) static public EAtom node$p ( EObject name ) { if ( ! ERT . getLocalNode ( ) . isALive ( ) ) { return ENode . am_nonode_at_nohost ; } EHandle handle ; if ( ( handle = name . testHandle ( ) ) != null ) { return handle . node ( ) ; } ERef ref ; if ( ( ref = name . testReference ( ) ) != null ) { return ref . node ( ) ; } return null ; } @ BIF public static EObject setnode ( EObject arg_node , EObject arg_creation ) throws Pausable { int creation ; ESmall cr = arg_creation . testSmall ( ) ; EAtom node = arg_node . testAtom ( ) ; if ( cr == null || node == null || cr . value > <NUM_LIT:3> || ! is_node_name_atom ( node ) ) { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + cr + "<STR_LIT>" + node + "<STR_LIT>" + is_node_name_atom ( node ) ) ; throw ERT . badarg ( arg_node , arg_creation ) ; } EObject net_kernel = ERT . whereis ( am_net_kernel ) ; EInternalPID nk = net_kernel . testInternalPID ( ) ; if ( nk == null ) { throw new ErlangError ( EAtom . intern ( "<STR_LIT>" ) ) ; } nk . set_dist_entry ( this_dist_entry ) ; set_this_node ( node , cr . value ) ; erts_is_alive = ERT . TRUE ; EAbstractNode n = EPeer . get ( node ) ; n . node_up ( null , ERT . NIL ) ; return ERT . TRUE ; } private static void set_this_node ( EAtom node , int value ) { ERT . getLocalNode ( ) . set ( node , value ) ; } private static Pattern node_name_regex = Pattern . compile ( "<STR_LIT>" ) ; public static boolean is_node_name_atom ( EAtom node ) { if ( node_name_regex . matcher ( node . getName ( ) ) . matches ( ) ) { return true ; } else { return false ; } } @ BIF public static EObject setnode ( EObject node_arg , EObject cid_arg , EObject type_arg ) throws Pausable { EAtom node ; int creation = <NUM_LIT:0> ; ETuple2 tup ; if ( ( tup = ETuple2 . cast ( node_arg ) ) != null ) { node = tup . elem1 . testAtom ( ) ; ESmall sm = tup . elem2 . testSmall ( ) ; if ( node == null || sm == null || ! is_node_name_atom ( node ) ) throw ERT . badarg ( node_arg , cid_arg , type_arg ) ; creation = sm . value ; } else if ( ( node = node_arg . testAtom ( ) ) != null && is_node_name_atom ( node ) ) { } else { throw ERT . badarg ( node_arg , cid_arg , type_arg ) ; } EInternalPort port = cid_arg . testInternalPort ( ) ; if ( port == null ) { throw ERT . badarg ( node_arg , cid_arg , type_arg ) ; } ETuple t = type_arg . testTuple ( ) ; if ( t . arity ( ) != <NUM_LIT:4> ) { throw ERT . badarg ( node_arg , cid_arg , type_arg ) ; } ESmall flags = t . elm ( <NUM_LIT:1> ) . testSmall ( ) ; ESmall version = t . elm ( <NUM_LIT:2> ) . testSmall ( ) ; if ( flags == null || version == null ) { throw ERT . badarg ( node_arg , cid_arg , type_arg ) ; } EPeer n = EPeer . get_or_create ( node , creation , port , flags . value , version . value ) ; EDriverTask task = port . task ( ) ; if ( task != null ) { task . node ( n ) ; n . node_up ( null , ERT . NIL ) ; return ERT . TRUE ; } else { return ERT . FALSE ; } } } </s>
|
<s> package erjang . m . erlang ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAtom ; import erjang . EInteger ; import erjang . ENumber ; import erjang . EObject ; import erjang . EProc ; import erjang . ERT ; import erjang . ESmall ; import erjang . BIF . Type ; public class BinOps { @ BIF ( name = "<STR_LIT>" , type = Type . GUARD ) static public ENumber band_guard ( EObject o1 , EObject o2 ) { EInteger n1 ; EInteger n2 ; if ( ( n1 = o1 . testInteger ( ) ) != null ) { if ( ( n2 = o2 . testInteger ( ) ) != null ) { return n1 . band ( n2 ) ; } } return null ; } @ BIF static public EInteger band ( EObject o1 , EObject o2 ) { return o1 . band ( o2 ) ; } @ BIF static public EInteger bor ( EObject o1 , EObject o2 ) { return o1 . bor ( o2 ) ; } @ BIF static public EInteger bsr ( EObject o1 , EObject o2 ) { return o1 . bsr ( o2 ) ; } @ BIF static public EInteger bsl ( EObject o1 , EObject o2 ) { return o1 . bsl ( o2 ) ; } @ BIF static public EInteger bsl ( EInteger o1 , ESmall o2 ) { return o1 . bsl ( o2 ) ; } @ BIF ( name = "<STR_LIT>" ) static public EAtom ge ( EObject o1 , EObject o2 ) { return o1 . ge ( o2 ) ; } @ BIF static public EObject send ( EProc proc , EObject pid , EObject msg ) throws Pausable { return ERT . send ( proc , pid , msg ) ; } } </s>
|
<s> package erjang . m . crypto_server ; import erjang . BIF ; import erjang . ENative ; import erjang . EObject ; import erjang . ERT ; import erjang . ETuple2 ; public class Native extends ENative { @ BIF public static EObject init ( EObject arg ) { return new ETuple2 ( ERT . am_ok , ERT . NIL ) ; } } </s>
|
<s> package erjang . m . lists ; import kilim . Pausable ; import erjang . BIF ; import erjang . EAtom ; import erjang . EFun ; import erjang . EInteger ; import erjang . EList ; import erjang . ENative ; import erjang . EObject ; import erjang . EProc ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . NotImplemented ; public class Native extends ENative { @ BIF public static EObject keymember ( EObject key , EObject nth_arg , EObject list_arg ) { ESmall nth = nth_arg . testSmall ( ) ; ESeq list = list_arg . testSeq ( ) ; if ( key == null || nth == null | list == null ) throw ERT . badarg ( key , nth_arg , list_arg ) ; while ( ! list . isNil ( ) ) { EObject elm = list . head ( ) ; ETuple tup = elm . testTuple ( ) ; if ( tup != null && tup . arity ( ) >= nth . value ) { EObject val = tup . elm ( nth . value ) ; if ( val . equals ( key ) ) { return ERT . TRUE ; } } list = list . tail ( ) ; } return ERT . FALSE ; } @ BIF public static EObject keyfind ( EObject key , EObject nth_arg , EObject list_arg ) { ESmall nth = nth_arg . testSmall ( ) ; ESeq list = list_arg . testSeq ( ) ; if ( key == null || nth == null | list == null ) throw ERT . badarg ( key , nth_arg , list_arg ) ; while ( ! list . isNil ( ) ) { EObject elm = list . head ( ) ; ETuple tup = elm . testTuple ( ) ; if ( tup != null && tup . arity ( ) >= nth . value ) { EObject val = tup . elm ( nth . value ) ; if ( val . equals ( key ) ) { return tup ; } } list = list . tail ( ) ; } return ERT . FALSE ; } @ BIF public static EObject keysearch ( EObject k , EObject n , EObject list ) { ESmall idx = n . testSmall ( ) ; ESeq src = list . testSeq ( ) ; if ( k == null || idx == null || src == null || idx . value < <NUM_LIT:1> ) throw ERT . badarg ( k , n , list ) ; int index = idx . value ; while ( ! src . isNil ( ) ) { EObject elm = src . head ( ) ; ETuple tup ; if ( ( tup = elm . testTuple ( ) ) != null ) { if ( tup . arity ( ) >= index ) { if ( tup . elm ( index ) . equals ( k ) ) { return new ETuple2 ( ERT . am_value , tup ) ; } } } src = src . tail ( ) ; } return ERT . FALSE ; } @ BIF public static ESeq reverse ( EObject hd , EObject tl ) { ESeq res = tl . testSeq ( ) ; ESeq front = hd . testSeq ( ) ; if ( res == null ) throw ERT . badarg ( hd , tl ) ; return reverse ( front , res ) ; } @ BIF public static ESeq reverse ( ESeq front , ESeq res ) { while ( ! front . isNil ( ) ) { res = res . cons ( front . head ( ) ) ; front = front . tail ( ) ; } return res ; } @ BIF public static EAtom member ( EObject e , EObject l ) { ESeq list = l . testSeq ( ) ; if ( list == null ) throw ERT . badarg ( e , l ) ; while ( ! list . isNil ( ) ) { if ( e . equals ( list . head ( ) ) ) return ERT . TRUE ; list = list . tail ( ) ; } return ERT . FALSE ; } @ BIF public static ESeq map ( EProc proc , EObject f , EObject s ) throws Pausable { EFun fun = f . testFunction2 ( <NUM_LIT:1> ) ; ESeq seq = s . testSeq ( ) ; if ( fun == null || seq == null ) throw ERT . badarg ( f , s ) ; EObject [ ] arg = new EObject [ <NUM_LIT:1> ] ; ESeq rev = ERT . NIL ; for ( ; ! seq . isNil ( ) ; seq = seq . tail ( ) ) { arg [ <NUM_LIT:0> ] = seq . head ( ) ; EObject val = fun . invoke ( proc , arg ) ; rev = rev . cons ( val ) ; } return reverse ( rev , ERT . NIL ) ; } @ BIF public static ESeq seq ( EObject start , EObject end ) { ESmall sm_s = start . testSmall ( ) ; ESmall sm_e = end . testSmall ( ) ; if ( sm_s == null || sm_e == null ) { EInteger i_s ; EInteger i_e ; if ( ( i_s = start . testInteger ( ) ) == null || ( i_e = end . testInteger ( ) ) == null ) { throw ERT . badarg ( start , end ) ; } return seq_big ( i_s , i_e ) ; } if ( ( sm_e . value + <NUM_LIT:1> ) < sm_s . value ) throw ERT . badarg ( start , end ) ; ESeq l = ERT . NIL ; int val = sm_e . value ; int first = sm_s . value ; while ( val >= first ) { l = l . cons ( val ) ; val -= <NUM_LIT:1> ; } return l ; } static ESeq seq_big ( EInteger start , EInteger end ) { if ( ( end . inc ( ) ) . is_lt ( start ) ) throw ERT . badarg ( start , end ) ; ESeq l = ERT . NIL ; EInteger val = end ; EInteger first = start ; while ( val . is_ge ( first ) ) { l = l . cons ( val ) ; val = val . dec ( ) ; } return l ; } } </s>
|
<s> package erjang . m . code ; import erjang . BIF ; import erjang . ENative ; import erjang . EObject ; import erjang . ERT ; public class Native extends ENative { @ BIF public static EObject is_module_native ( EObject module ) { return ERT . FALSE ; } } </s>
|
<s> package erjang ; public abstract class EPseudoTerm extends EObject { @ Override int cmp_order ( ) { return CMP_ORDER_ERJANG_INTERNAL ; } @ Override public EBinary testBinary ( ) { return EBinary . fromString ( "<STR_LIT>" ) ; } @ Override public EBitString testBitString ( ) { return testBinary ( ) ; } } </s>
|
<s> package erjang ; import java . io . ByteArrayOutputStream ; import java . io . CharArrayWriter ; import java . io . IOException ; import java . nio . ByteBuffer ; import java . nio . charset . Charset ; import java . util . List ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; public class EBigString extends ESeq implements CharSequence { private static final Charset ISO_LATIN_1 = Charset . forName ( "<STR_LIT>" ) ; private static final EBigString EMPTY = new EBigString ( "<STR_LIT>" ) ; final char [ ] data ; final int off ; private int hash = - <NUM_LIT:1> ; public EBigString ( String value ) { this . hash = value . hashCode ( ) ; this . data = value . toCharArray ( ) ; this . off = <NUM_LIT:0> ; } public EBigString testBigString ( ) { return this ; } EBigString ( char [ ] data , int off ) { this . data = data ; this . off = off ; } EBigString ( char [ ] data , int off , int len ) { if ( data . length == off + len ) { this . data = data ; this . off = off ; } else { this . data = new char [ len ] ; this . off = <NUM_LIT:0> ; System . arraycopy ( data , off , this . data , <NUM_LIT:0> , len ) ; } } public static EBigString make ( char [ ] array , int arrayOffset , int len ) { if ( len == array . length - arrayOffset ) { return new EBigString ( array , arrayOffset ) ; } else { char [ ] copy = new char [ len ] ; System . arraycopy ( array , arrayOffset , copy , <NUM_LIT:0> , len ) ; return new EBigString ( copy , <NUM_LIT:0> ) ; } } public static EBigString make ( ECons list ) { EBigString s ; if ( ( s = list . testBigString ( ) ) != null ) { return s ; } else if ( list . isNil ( ) ) { return EBigString . EMPTY ; } else { CharArrayWriter barr = new CharArrayWriter ( ) ; EObject tail = list ; while ( ( list = tail . testNonEmptyList ( ) ) != null ) { EObject head = list . head ( ) ; ESmall intval ; if ( ( intval = head . testSmall ( ) ) == null ) { throw ERT . badarg ( ) ; } int byteValue = intval . value & <NUM_LIT> ; if ( intval . value != byteValue ) { throw ERT . badarg ( ) ; } barr . write ( byteValue ) ; tail = list . tail ( ) ; } return new EBigString ( barr . toCharArray ( ) , <NUM_LIT:0> ) ; } } @ Override public int hashCode ( ) { if ( hash == - <NUM_LIT:1> ) { hash = stringValue ( ) . hashCode ( ) ; } return hash ; } public String stringValue ( ) { return new String ( data , off , data . length - off ) ; } public boolean equalsExactly ( EObject rhs ) { ENil nil ; int length = length ( ) ; if ( ( nil = rhs . testNil ( ) ) != null ) { return length == <NUM_LIT:0> ; } EBigString str ; if ( ( str = rhs . testBigString ( ) ) != null ) { EBigString es = str ; if ( length != es . length ( ) ) return false ; for ( int i = <NUM_LIT:0> ; i < length ; i ++ ) { if ( charAt ( i ) != es . charAt ( i ) ) return false ; } return true ; } ESeq seq ; if ( ( seq = rhs . testSeq ( ) ) != null ) { for ( int i = <NUM_LIT:0> ; i < length ; i ++ ) { if ( seq . testNil ( ) != null ) return false ; if ( ! seq . head ( ) . equalsExactly ( new ESmall ( charAt ( i ) ) ) ) { return false ; } seq = seq . tail ( ) ; } return seq . isNil ( ) ; } return false ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof String ) { return ( ( String ) obj ) . equals ( stringValue ( ) ) ; } if ( ! ( obj instanceof EObject ) ) { return false ; } return equalsExactly ( ( EObject ) obj ) ; } @ Override public char charAt ( int index ) { return data [ off + index ] ; } @ Override public int length ( ) { return data . length - off ; } @ Override public CharSequence subSequence ( final int start , final int end ) { if ( end == length ( ) ) return new EBigString ( data , off + start ) ; return new SubSequence ( start , end - start ) ; } public class SubSequence implements CharSequence { private final int offset ; private final int length ; public SubSequence ( int start , int length ) { this . offset = start ; this . length = length ; EBigString . this . check_subseq ( offset , length ) ; } @ Override public char charAt ( int index ) { return EBigString . this . charAt ( offset + index ) ; } @ Override public int length ( ) { return length ; } @ Override public CharSequence subSequence ( int start , int end ) { return new SubSequence ( this . offset + start , end - start ) ; } } void check_subseq ( int offset , int length ) { if ( offset < <NUM_LIT:0> || length < <NUM_LIT:0> || ( offset + length ) > length ( ) ) throw new IllegalArgumentException ( ) ; } @ Override public String toString ( ) { return '<CHAR_LIT:">' + stringValue ( ) + '<CHAR_LIT:">' ; } public static EBigString fromString ( String s ) { return new EBigString ( s ) ; } private static final Type ESTRING_TYPE = Type . getType ( EBigString . class ) ; private static final Type STRING_TYPE = Type . getType ( String . class ) ; @ Override public Type emit_const ( MethodVisitor fa ) { Type type = ESTRING_TYPE ; fa . visitLdcInsn ( this . stringValue ( ) ) ; fa . visitMethodInsn ( Opcodes . INVOKESTATIC , type . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + STRING_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + type . getDescriptor ( ) ) ; return type ; } @ Override public EList cons ( EObject h ) { return new EList ( h , this ) ; } @ Override public ESeq tail ( ) { if ( off == data . length ) return ERT . NIL ; return new EBigString ( data , off + <NUM_LIT:1> ) ; } @ Override public ESmall head ( ) { return new ESmall ( data [ off ] & <NUM_LIT> ) ; } @ Override public ENil testNil ( ) { return length ( ) == <NUM_LIT:0> ? ERT . NIL : null ; } @ Override public ESeq testSeq ( ) { return this ; } public ECons testNonEmptyList ( ) { return length ( ) == <NUM_LIT:0> ? null : this ; } public ECons testCons ( ) { return this ; } @ Override int compare_same ( EObject rhs ) { if ( rhs . isNil ( ) ) return <NUM_LIT:1> ; int length = length ( ) ; EBigString str ; if ( ( str = rhs . testBigString ( ) ) != null ) { EBigString es = str ; int length2 = str . length ( ) ; int limit = Math . min ( length , length2 ) ; for ( int i = <NUM_LIT:0> ; i < limit ; i ++ ) { char ch1 = charAt ( i ) ; char ch2 = es . charAt ( i ) ; if ( ch1 < ch2 ) return - <NUM_LIT:1> ; if ( ch1 > ch2 ) return <NUM_LIT:1> ; } if ( length > length2 ) return <NUM_LIT:1> ; if ( length < length2 ) return - <NUM_LIT:1> ; return <NUM_LIT:0> ; } ECons seq ; if ( ( seq = rhs . testCons ( ) ) != null ) { int i = <NUM_LIT:0> ; while ( i < length ) { if ( ( seq . testNil ( ) ) != null ) { return - <NUM_LIT:1> ; } int cmp = ( new ESmall ( charAt ( i ++ ) ) ) . erlangCompareTo ( seq . head ( ) ) ; if ( cmp != <NUM_LIT:0> ) return cmp ; EObject res = seq . tail ( ) ; if ( ( seq = res . testCons ( ) ) != null ) { continue ; } return - res . erlangCompareTo ( new EBigString ( data , i ) ) ; } } return - rhs . erlangCompareTo ( this ) ; } @ Override public ESeq prepend ( ESeq list ) { EBigString other = list . testBigString ( ) ; if ( other != null ) { char [ ] out = new char [ length ( ) + other . length ( ) ] ; System . arraycopy ( other . data , other . off , out , <NUM_LIT:0> , other . length ( ) ) ; System . arraycopy ( this . data , this . off , out , other . length ( ) , this . length ( ) ) ; return EBigString . make ( out , <NUM_LIT:0> , out . length ) ; } else { return super . prepend ( list ) ; } } public EBinary asBitString ( Charset charset ) { byte [ ] data = stringValue ( ) . getBytes ( charset ) ; return new EBinary ( data , <NUM_LIT:0> , data . length ) ; } public static EBigString make ( EObject eObject ) { ESeq str ; if ( ( str = eObject . testSeq ( ) ) != null ) return make ( str ) ; EAtom am = eObject . testAtom ( ) ; if ( am != null ) { return fromString ( am . toString ( ) ) ; } throw ERT . badarg ( ) ; } public void collectCharList ( CharCollector out ) throws CharCollector . CollectingException , IOException { try { out . addIntegers ( data , off , data . length - off ) ; } catch ( CharCollector . PartialDecodingException e ) { int n = e . inputPos ; throw new CharCollector . CollectingException ( new EBigString ( data , off + n ) ) ; } } public static boolean isValidCodePoint ( int cp ) { return ( cp > > > <NUM_LIT:16> ) <= <NUM_LIT> && ( cp & ~ <NUM_LIT> ) != <NUM_LIT> && ( cp & ~ <NUM_LIT:1> ) != <NUM_LIT> ; } public static ESeq read ( EInputStream ei ) throws IOException { return ei . read_string ( ) ; } @ Override public void encode ( EOutputStream eos ) { eos . write_string ( stringValue ( ) ) ; } } </s>
|
<s> package erjang ; public class FunID implements Comparable < FunID > { public final EAtom module ; public final EAtom function ; public final int arity ; public FunID ( String module , String function , int arity ) { this ( EAtom . intern ( module ) , EAtom . intern ( function ) , arity ) ; } public FunID ( EAtom module , EAtom function , int arity ) { this . module = module ; this . function = function ; this . arity = arity ; } public FunID ( Import imp ) { this ( imp . module ( ) , imp . fun ( ) , imp . arity ( ) ) ; } public FunID ( Export exp ) { this ( exp . module ( ) , exp . fun ( ) , exp . arity ( ) ) ; } @ Override public int hashCode ( ) { return module . hashCode ( ) + function . hashCode ( ) + arity ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof FunID ) { FunID spec = ( FunID ) obj ; return module . equals ( spec . module ) && function . equals ( spec . function ) && arity == spec . arity ; } return false ; } @ Override public String toString ( ) { return module + "<STR_LIT::>" + function + "<STR_LIT:/>" + arity ; } @ Override public int compareTo ( FunID o ) { if ( module != o . module ) { int c1 = module . compareTo ( o . module ) ; if ( c1 != <NUM_LIT:0> ) return c1 ; } if ( function != o . function ) { int c2 = function . compareTo ( o . function ) ; if ( c2 != <NUM_LIT:0> ) return c2 ; } return arity - o . arity ; } } </s>
|
<s> package erjang ; public class ETuple0 extends ETuple { static public ETuple0 cast ( ETuple value ) { if ( value . arity ( ) == <NUM_LIT:0> ) return ( ETuple0 ) value ; return null ; } static public ETuple0 cast ( EObject value ) { if ( value instanceof ETuple0 ) return ( ETuple0 ) value ; return null ; } @ Override public ETuple0 blank ( ) { return new ETuple0 ( ) ; } @ Override public int arity ( ) { return <NUM_LIT:0> ; } @ Override public EObject elm ( int i ) { return bad_nth ( i ) ; } @ Override public void set ( int index , EObject term ) { bad_nth ( index ) ; } } </s>
|
<s> package erjang ; import java . math . BigInteger ; import java . nio . ByteOrder ; public class EBinMatchState extends EPseudoTerm { public static final BigInteger TWO_TO_64 = BigInteger . ONE . shiftLeft ( <NUM_LIT> ) ; public EBinMatchState testBinMatchState ( ) { return this ; } public static final int BSF_ALIGNED = <NUM_LIT:1> ; public static final int BSF_LITTLE = <NUM_LIT:2> ; public static final int BSF_SIGNED = <NUM_LIT:4> ; public static final int BSF_EXACT = <NUM_LIT:8> ; public static final int BSF_NATIVE = <NUM_LIT:16> ; public static final EAtom ATOM_ALL = EAtom . intern ( "<STR_LIT:all>" ) ; public final EBitString bin ; long offset ; long save_offset0 ; long [ ] save_offsetN ; private long start_offset ; private long save_offset ( int n ) { if ( n == <NUM_LIT:0> ) return save_offset0 ; return save_offsetN [ n - <NUM_LIT:1> ] ; } private void save_offset ( int n , long off ) { if ( n == <NUM_LIT:0> ) save_offset0 = off ; else save_offsetN [ n + <NUM_LIT:1> ] = off ; } private int slots ( ) { if ( save_offsetN == null ) return <NUM_LIT:1> ; else return save_offsetN . length + <NUM_LIT:1> ; } public long bitsLeft ( ) { return bin . bitSize ( ) - offset ; } public EBitString binary ( ) { return bin ; } public static EObject bs_context_to_binary ( EObject obj ) { EBinMatchState bms ; if ( ( bms = obj . testBinMatchState ( ) ) != null ) { long off = bms . start_offset ; if ( off % <NUM_LIT:8> == <NUM_LIT:0> ) { int start_byte = ( int ) ( off / <NUM_LIT:8> ) ; EBitString result = EBitString . makeByteOffsetTail ( bms . bin , start_byte ) ; return result ; } else { throw new NotImplemented ( ) ; } } else { return obj ; } } public static void bs_save2 ( EObject obj , int slot ) { EBinMatchState bms ; if ( ( bms = obj . testBinMatchState ( ) ) != null ) { bms . save_offset ( slot , bms . offset ) ; } else { throw new Error ( "<STR_LIT>" ) ; } } public static void bs_save2_start ( EObject obj ) { EBinMatchState bms ; if ( ( bms = obj . testBinMatchState ( ) ) != null ) { bms . start_offset = bms . offset ; } else { throw new Error ( "<STR_LIT>" ) ; } } public static void bs_restore2 ( EObject obj , int slot ) { EBinMatchState bms ; if ( ( bms = obj . testBinMatchState ( ) ) != null ) { bms . offset = bms . save_offset ( slot ) ; } else { throw new Error ( "<STR_LIT>" ) ; } } public static void bs_restore2_start ( EObject obj ) { EBinMatchState bms ; if ( ( bms = obj . testBinMatchState ( ) ) != null ) { bms . offset = bms . save_offset ( <NUM_LIT:0> ) ; } else { throw new Error ( "<STR_LIT>" ) ; } } public static EBinMatchState bs_start_match2 ( EObject obj , int slots ) { EBinMatchState ms ; if ( ( ms = obj . testBinMatchState ( ) ) != null ) { int actual_slots = ms . slots ( ) ; ms . save_offset ( <NUM_LIT:0> , ms . offset ) ; ms . start_offset = ms . offset ; if ( actual_slots < slots ) { EBinMatchState res = new EBinMatchState ( ms . bin , slots ) ; res . offset = ms . offset ; res . save_offset ( <NUM_LIT:0> , ms . save_offset ( <NUM_LIT:0> ) ) ; res . start_offset = ms . offset ; return res ; } return ms ; } EBitString bs ; if ( ( bs = obj . testBitString ( ) ) != null ) return new EBinMatchState ( bs , slots ) ; return null ; } public EBinMatchState ( EBitString binary , int slots ) { this . bin = binary ; this . offset = <NUM_LIT:0> ; if ( slots > <NUM_LIT:1> ) { save_offsetN = new long [ slots - <NUM_LIT:1> ] ; } } public EBitString bs_get_binary2 ( EObject spec , int flags ) { long bitLength ; if ( spec == ATOM_ALL ) { bitLength = bitsLeft ( ) ; } else { ESmall len = spec . testSmall ( ) ; if ( len != null ) { bitLength = len . intValue ( ) * <NUM_LIT> ; } else { throw new Error ( "<STR_LIT>" + spec ) ; } } if ( bitLength > bitsLeft ( ) ) { return null ; } else { EBitString result = bin . substring ( offset , bitLength ) ; offset += bitLength ; return result ; } } public EDouble bs_get_float2 ( int size , int unit , int flags ) { EInteger i = bs_get_integer2 ( size , unit , flags ) ; if ( ( size * unit ) == <NUM_LIT> ) { double d = Double . longBitsToDouble ( i . longValue ( ) ) ; return new EDouble ( d ) ; } if ( ( size * unit ) == <NUM_LIT:32> ) { double d = Float . intBitsToFloat ( i . intValue ( ) ) ; return new EDouble ( d ) ; } return null ; } public EInteger bs_get_integer2 ( int size , int unit , int flags ) { size *= unit ; boolean signed = ( ( flags & BSF_SIGNED ) == BSF_SIGNED ) ; boolean little_endian = ( ( flags & BSF_LITTLE ) == BSF_LITTLE ) ; boolean native_endian = ( ( flags & BSF_NATIVE ) == BSF_NATIVE ) ; if ( native_endian ) { little_endian = ByteOrder . nativeOrder ( ) == ByteOrder . LITTLE_ENDIAN ; } if ( size == <NUM_LIT:0> ) { return ESmall . ZERO ; } if ( size < <NUM_LIT:0> || bin . bitSize ( ) < ( offset + size ) ) { return null ; } if ( size <= <NUM_LIT:32> ) { int value = little_endian ? bin . intLittleEndianBitsAt ( offset , size ) : bin . intBitsAt ( offset , size ) ; offset += size ; if ( signed ) { value = EBitString . signExtend ( value , size ) ; } else if ( value < <NUM_LIT:0> ) { return new EBig ( value & <NUM_LIT> ) ; } ESmall res = new ESmall ( value ) ; return res ; } if ( size <= <NUM_LIT> ) { long value = little_endian ? bin . longLittleEndianBitsAt ( offset , size ) : bin . longBitsAt ( offset , size ) ; offset += size ; if ( signed ) { value = EBitString . signExtend ( value , size ) ; } else if ( value < <NUM_LIT:0> ) { BigInteger big = BigInteger . valueOf ( value ) . add ( TWO_TO_64 ) ; return new EBig ( big ) ; } EInteger res = ERT . box ( value ) ; return res ; } byte [ ] data ; int extra_in_front = ( size % <NUM_LIT:8> ) ; int bytes_needed , out_offset ; if ( extra_in_front != <NUM_LIT:0> ) { out_offset = <NUM_LIT:0> ; bytes_needed = size / <NUM_LIT:8> + <NUM_LIT:1> ; } else { out_offset = signed ? <NUM_LIT:0> : <NUM_LIT:1> ; bytes_needed = size / <NUM_LIT:8> + out_offset ; } data = new byte [ bytes_needed ] ; long read_offset ; if ( little_endian ) { read_offset = offset + size ; } else { read_offset = offset ; } if ( extra_in_front != <NUM_LIT:0> ) { if ( little_endian ) { read_offset -= extra_in_front ; data [ <NUM_LIT:0> ] = ( byte ) bin . intLittleEndianBitsAt ( read_offset , extra_in_front ) ; } else { data [ <NUM_LIT:0> ] = ( byte ) bin . intBitsAt ( read_offset , extra_in_front ) ; read_offset += extra_in_front ; } out_offset = <NUM_LIT:1> ; if ( signed ) { data [ <NUM_LIT:0> ] = ( byte ) ( <NUM_LIT> & EBitString . signExtend ( data [ <NUM_LIT:0> ] , extra_in_front ) ) ; } } if ( little_endian ) { for ( int i = <NUM_LIT:0> ; i < size / <NUM_LIT:8> ; i ++ ) { read_offset -= <NUM_LIT:8> ; data [ out_offset + i ] = ( byte ) bin . intBitsAt ( read_offset , <NUM_LIT:8> ) ; } } else { for ( int i = <NUM_LIT:0> ; i < size / <NUM_LIT:8> ; i ++ ) { data [ out_offset + i ] = ( byte ) bin . intBitsAt ( read_offset , <NUM_LIT:8> ) ; read_offset += <NUM_LIT:8> ; } } offset += size ; BigInteger bi = new BigInteger ( data ) ; return ERT . box ( bi ) ; } public EBitString bs_match_string ( EBitString ebs ) { long size = ebs . bitSize ( ) ; if ( size > bitsLeft ( ) ) { return null ; } long limit = size ; for ( int pos = <NUM_LIT:0> ; pos < limit ; pos += <NUM_LIT:8> ) { int rest = ( int ) Math . min ( <NUM_LIT:8> , limit - pos ) ; int oc1 = <NUM_LIT> & bin . intBitsAt ( offset + pos , rest ) ; int oc2 = <NUM_LIT> & ebs . intBitsAt ( pos , rest ) ; if ( oc1 != oc2 ) return null ; } offset += size ; return ebs ; } public EObject bs_skip_bits2 ( EObject count_o , int bits , int flags ) { if ( count_o == ATOM_ALL ) { long bitLength = bitsLeft ( ) ; offset += bitLength ; return ERT . TRUE ; } EInteger count ; if ( ( count = count_o . testInteger ( ) ) == null ) { return null ; } int bitsWanted = bits * count . intValue ( ) ; if ( bitsLeft ( ) < bitsWanted ) { return null ; } offset += bitsWanted ; return ERT . TRUE ; } public boolean bs_test_unit ( int unit ) { boolean res = ( bitsLeft ( ) % unit ) == <NUM_LIT:0> ; return res ; } public boolean bs_test_tail2 ( int expected_left ) { return ( offset + ( long ) expected_left == bin . bitSize ( ) ) ; } public boolean bs_skip_utf8 ( int flags ) { long save_offset = offset ; int character = decodeUTF8 ( ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return ( character >= <NUM_LIT:0> ) ; } public int bs_get_utf8 ( int flags ) { long save_offset = offset ; int character = decodeUTF8 ( ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return character ; } public boolean bs_skip_utf16 ( int flags ) { long save_offset = offset ; int character = decodeUTF16 ( flags ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return ( character >= <NUM_LIT:0> ) ; } public int bs_get_utf16 ( int flags ) { long save_offset = offset ; int character = decodeUTF16 ( flags ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return character ; } public boolean bs_skip_utf32 ( int flags ) { long save_offset = offset ; int character = decodeUTF32 ( flags ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return ( character >= <NUM_LIT:0> ) ; } public int bs_get_utf32 ( int flags ) { long save_offset = offset ; int character = decodeUTF32 ( flags ) ; if ( character < <NUM_LIT:0> ) assert ( offset == save_offset ) ; return character ; } static final int [ ] UTF8_MASK = { <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> } ; public int decodeUTF8 ( ) { if ( offset % <NUM_LIT:8> != <NUM_LIT:0> ) return - <NUM_LIT:1> ; int byte_pos = ( int ) ( offset / <NUM_LIT:8> ) ; if ( bitsLeft ( ) < <NUM_LIT:8> ) return - <NUM_LIT:1> ; int acc = bin . octetAt ( byte_pos ++ ) ; if ( acc < <NUM_LIT> ) { offset = <NUM_LIT> * byte_pos ; return acc ; } if ( ( acc & <NUM_LIT> ) == <NUM_LIT> ) return - <NUM_LIT:1> ; if ( acc > <NUM_LIT> ) return - <NUM_LIT:1> ; int len ; byte t = ( byte ) acc ; for ( len = <NUM_LIT:1> ; ( t <<= <NUM_LIT:1> ) < <NUM_LIT:0> ; len ++ ) { if ( bitsLeft ( ) - <NUM_LIT:8> * len < <NUM_LIT:8> ) return - <NUM_LIT:1> ; int b = bin . octetAt ( byte_pos ++ ) ; if ( ( b & <NUM_LIT> ) != <NUM_LIT> ) return - <NUM_LIT:1> ; acc = ( acc << <NUM_LIT:6> ) + ( b & <NUM_LIT> ) ; } acc &= UTF8_MASK [ len ] ; if ( acc <= UTF8_MASK [ len - <NUM_LIT:1> ] ) return - <NUM_LIT:1> ; if ( ! isValidCodePoint ( acc ) ) return - <NUM_LIT:1> ; offset = <NUM_LIT> * byte_pos ; return acc ; } public int decodeUTF16 ( int flags ) { if ( offset % <NUM_LIT:8> != <NUM_LIT:0> ) return - <NUM_LIT:1> ; int byte_pos = ( int ) ( offset / <NUM_LIT:8> ) ; if ( bitsLeft ( ) < <NUM_LIT:16> ) return - <NUM_LIT:1> ; int acc = bin . uint16_at ( byte_pos , flags ) ; byte_pos += <NUM_LIT:2> ; if ( ( acc & ~ <NUM_LIT> ) == <NUM_LIT> ) { if ( bitsLeft ( ) < <NUM_LIT:2> * <NUM_LIT:16> ) return - <NUM_LIT:1> ; int w2 = bin . uint16_at ( byte_pos , flags ) ; byte_pos += <NUM_LIT:2> ; if ( ( w2 & ~ <NUM_LIT> ) != <NUM_LIT> ) return - <NUM_LIT:1> ; acc = ( ( acc & <NUM_LIT> ) << <NUM_LIT:10> ) + ( w2 & <NUM_LIT> ) + <NUM_LIT> ; } else if ( ( acc & ~ <NUM_LIT> ) == <NUM_LIT> ) { return - <NUM_LIT:1> ; } if ( ! isValidCodePoint ( acc ) ) return - <NUM_LIT:1> ; offset = <NUM_LIT> * byte_pos ; return acc ; } public int decodeUTF32 ( int flags ) { if ( offset % <NUM_LIT:8> != <NUM_LIT:0> ) return - <NUM_LIT:1> ; int byte_pos = ( int ) ( offset / <NUM_LIT:8> ) ; if ( bitsLeft ( ) < <NUM_LIT:32> ) return - <NUM_LIT:1> ; int acc = bin . int32_at ( byte_pos , flags ) ; if ( acc < <NUM_LIT:0> || ! isValidCodePoint ( acc ) ) return - <NUM_LIT:1> ; offset += <NUM_LIT:32> ; return acc ; } public static boolean isValidCodePoint ( int c ) { return ( ( c & ~ <NUM_LIT> ) != <NUM_LIT> && ( c & ~ <NUM_LIT> ) != <NUM_LIT> && c < <NUM_LIT> ) ; } @ Override public int hashCode ( ) { return System . identityHashCode ( this ) ; } } </s>
|
<s> package erjang ; import java . math . BigInteger ; import java . nio . ByteBuffer ; import java . util . List ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; public final class ESmall extends EInteger { private static final Type ESMALL_TYPE = Type . getType ( ESmall . class ) ; public static final ESmall ZERO = new ESmall ( <NUM_LIT:0> ) ; public static final ESmall MINUS_ONE = new ESmall ( - <NUM_LIT:1> ) ; public static final ESmall ONE = new ESmall ( <NUM_LIT:1> ) ; public final int value ; final static int PREALLOC_COUNT = <NUM_LIT> ; static final ESmall [ ] little = new ESmall [ PREALLOC_COUNT ] ; static { for ( int i = <NUM_LIT:0> ; i < little . length ; i ++ ) { little [ i ] = new ESmall ( i ) ; } } @ Override public ESmall testSmall ( ) { return this ; } public ESmall ( int value ) { this . value = value ; } @ Override public int hashCode ( ) { return value ; } public long longValue ( ) { return value ; } public EInteger inc ( ) { if ( value == Integer . MAX_VALUE ) { return ERT . box ( ( long ) Integer . MAX_VALUE + <NUM_LIT:1L> ) ; } else { return new ESmall ( value + <NUM_LIT:1> ) ; } } public EInteger dec ( ) { if ( value == Integer . MIN_VALUE ) { return ERT . box ( ( long ) Integer . MIN_VALUE - <NUM_LIT:1L> ) ; } else { return new ESmall ( value - <NUM_LIT:1> ) ; } } public boolean is_zero ( ) { return value == <NUM_LIT:0> ; } @ Override public boolean collectIOList ( List < ByteBuffer > out ) { ByteBuffer b = ByteBuffer . allocate ( <NUM_LIT:1> ) ; b . put ( <NUM_LIT:0> , ( byte ) value ) ; out . add ( b ) ; return true ; } public boolean equals ( EObject other ) { if ( other == this ) return true ; if ( other instanceof ESmall ) return ( ( ESmall ) other ) . value == value ; if ( other instanceof EDouble ) return ( ( EDouble ) other ) . value == value ; return false ; } @ Override int compare_same ( EObject rhs ) { return rhs . r_compare_same ( this ) ; } int r_compare_same ( ESmall lhs ) { if ( lhs . value < value ) return - <NUM_LIT:1> ; if ( lhs . value == value ) return <NUM_LIT:0> ; return <NUM_LIT:1> ; } int r_compare_same ( EBig lhs ) { return lhs . value . compareTo ( BigInteger . valueOf ( value ) ) ; } int r_compare_same ( EDouble lhs ) { return lhs . value < value ? - <NUM_LIT:1> : lhs . value == value ? <NUM_LIT:0> : <NUM_LIT:1> ; } @ Override public boolean equalsExactly ( EObject rhs ) { return rhs . r_equals_exactly ( this ) ; } boolean r_equals_exactly ( ESmall lhs ) { return lhs . value == value ; } @ Override public int asInt ( ) { return value ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof ESmall ) { ESmall o = ( ESmall ) obj ; return o . value == value ; } return false ; } public int intValue ( ) { return value ; } @ Override public String toString ( ) { return String . valueOf ( value ) ; } @ Override public org . objectweb . asm . Type emit_const ( MethodVisitor fa ) { Type type = ESMALL_TYPE ; fa . visitTypeInsn ( Opcodes . NEW , type . getInternalName ( ) ) ; fa . visitInsn ( Opcodes . DUP ) ; fa . visitLdcInsn ( new Integer ( value ) ) ; fa . visitMethodInsn ( Opcodes . INVOKESPECIAL , type . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; return type ; } @ Override public EInteger abs ( ) { return ERT . box ( Math . abs ( ( long ) value ) ) ; } public static ESmall make ( int v ) { if ( v >= <NUM_LIT:0> && v < PREALLOC_COUNT ) return little [ v ] ; else return new ESmall ( v ) ; } public double doubleValue ( ) { return value ; } public BigInteger bigintValue ( ) { return BigInteger . valueOf ( value ) ; } public ENumber add ( EObject other , boolean guard ) { return other . add ( value , guard ) ; } @ BIF ( name = "<STR_LIT:+>" ) public ENumber add ( EObject rhs ) { return rhs . add ( value , false ) ; } public EInteger add ( int rhs , boolean guard ) { return ERT . box ( ( long ) value + ( long ) rhs ) ; } @ BIF ( name = "<STR_LIT:+>" ) public EInteger add ( ESmall rhs ) { return ERT . box ( ( long ) value + ( long ) rhs . value ) ; } public ENumber add ( double lhs , boolean guard ) { return ERT . box ( lhs + value ) ; } public ENumber add ( BigInteger lhs , boolean guard ) { return ERT . box ( lhs . add ( BigInteger . valueOf ( value ) ) ) ; } public ENumber subtract ( EObject other , boolean guard ) { return other . r_subtract ( value , guard ) ; } public ENumber subtract ( ESmall rhs ) { return ERT . box ( ( long ) value - ( long ) rhs . value ) ; } @ Deprecated public ENumber subtract ( int rhs ) { return ERT . box ( ( long ) value - rhs ) ; } public ENumber r_subtract ( int lhs , boolean guard ) { return ERT . box ( ( long ) lhs - ( long ) value ) ; } public ENumber r_subtract ( double lhs , boolean guard ) { return ERT . box ( lhs - value ) ; } public ENumber r_subtract ( BigInteger lhs , boolean guard ) { return ERT . box ( lhs . subtract ( BigInteger . valueOf ( value ) ) ) ; } @ BIF ( name = "<STR_LIT:*>" ) public ENumber multiply ( EObject other ) { return other . r_multiply ( value ) ; } public ENumber r_multiply ( int lhs ) { return ERT . box ( ( long ) lhs * ( long ) value ) ; } public EDouble r_multiply ( double lhs ) { return ERT . box ( lhs * value ) ; } public ENumber r_multiply ( BigInteger lhs ) { return ERT . box ( lhs . multiply ( BigInteger . valueOf ( value ) ) ) ; } @ Override public ENumber negate ( ) { return new ESmall ( - value ) ; } public EDouble divide ( EObject other ) { return other . r_divide ( value ) ; } public EDouble r_divide ( int lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( lhs , this ) ; return ERT . box ( ( double ) lhs / value ) ; } public EDouble r_divide ( double lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( ERT . box ( lhs ) , this ) ; return ERT . box ( lhs / value ) ; } public EDouble r_divide ( BigInteger lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( ERT . box ( lhs ) , this ) ; return ERT . box ( lhs . doubleValue ( ) / value ) ; } public EInteger idiv ( EObject other ) { return other . r_idiv ( value ) ; } public EInteger idiv ( int rhs ) { if ( rhs == <NUM_LIT:0> ) throw ERT . badarith ( this , ERT . box ( rhs ) ) ; return ERT . box ( value / rhs ) ; } public EInteger r_idiv ( int lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( ERT . box ( lhs ) , this ) ; return ERT . box ( ( long ) lhs / ( long ) value ) ; } public EInteger r_idiv ( BigInteger lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( lhs , this ) ; return ERT . box ( lhs . divide ( BigInteger . valueOf ( value ) ) ) ; } public ESmall irem ( int rhs ) { if ( rhs == <NUM_LIT:0> ) throw ERT . badarith ( this , ERT . box ( rhs ) ) ; return ERT . box ( value % rhs ) ; } public EInteger irem ( EObject other ) { return other . r_irem ( value ) ; } public EInteger r_irem ( int lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( ERT . box ( lhs ) , this ) ; return ERT . box ( lhs % value ) ; } public EInteger r_irem ( BigInteger lhs ) { if ( value == <NUM_LIT:0> ) throw ERT . badarith ( ERT . box ( lhs ) , this ) ; return ERT . box ( lhs . remainder ( BigInteger . valueOf ( value ) ) ) ; } public EInteger bsr ( EObject other ) { return other . r_bsr ( value ) ; } public EInteger r_bsr ( int lhs ) { if ( ( value & ~ <NUM_LIT:31> ) == <NUM_LIT:0> ) return ERT . box ( ( lhs > > value ) ) ; if ( value >= <NUM_LIT:32> ) { if ( lhs < <NUM_LIT:0> ) return ESmall . MINUS_ONE ; else return ZERO ; } else { return ERT . box ( BigInteger . valueOf ( lhs ) . shiftRight ( value ) ) ; } } public EInteger r_bsr ( BigInteger lhs ) { return ERT . box ( lhs . shiftRight ( value ) ) ; } public EInteger bsl ( EObject other ) { return other . r_bsl ( value ) ; } public EInteger r_bsl ( int lhs ) { if ( ( value & ~ <NUM_LIT:31> ) == <NUM_LIT:0> ) return ERT . box ( ( ( long ) lhs ) << value ) ; return ERT . box ( BigInteger . valueOf ( lhs ) . shiftLeft ( value ) ) ; } public EInteger r_bsl ( BigInteger lhs ) { return ERT . box ( lhs . shiftLeft ( value ) ) ; } public EInteger band ( EObject other ) { return other . band ( value ) ; } public EInteger band ( int lhs ) { return ERT . box ( ( lhs & value ) ) ; } public EInteger band ( BigInteger lhs ) { return ERT . box ( lhs . and ( BigInteger . valueOf ( value ) ) ) ; } public EInteger bor ( EObject other ) { return other . bor ( value ) ; } public EInteger bor ( int lhs ) { return ERT . box ( ( lhs | value ) ) ; } public EInteger bor ( BigInteger lhs ) { return ERT . box ( lhs . or ( BigInteger . valueOf ( value ) ) ) ; } public EInteger bxor ( EObject other ) { return other . bxor ( value ) ; } public EInteger bxor ( int lhs ) { return ERT . box ( ( lhs ^ value ) ) ; } public EInteger bxor ( BigInteger lhs ) { return ERT . box ( lhs . xor ( BigInteger . valueOf ( value ) ) ) ; } public EInteger bnot ( ) { return ERT . box ( ~ value ) ; } @ Override public void encode ( EOutputStream eos ) { eos . write_int ( value ) ; } } </s>
|
<s> package erjang ; import java . util . Iterator ; import java . util . NoSuchElementException ; public class EObjectIterator implements Iterator < EObject > { private EObject object = null ; public EObjectIterator ( EObject object ) { this . object = object ; } public boolean hasNext ( ) { if ( object == null ) { return false ; } return ! object . isNil ( ) ; } public EObject next ( ) { if ( ( object == null ) || object . isNil ( ) ) { throw new NoSuchElementException ( ) ; } EObject next ; ECons cons = object . testCons ( ) ; if ( ( cons != null ) && ! ( cons instanceof EString ) ) { next = cons . head ( ) ; object = cons . tail ( ) ; } else { next = object ; object = null ; } return next ; } public void remove ( ) { throw new UnsupportedOperationException ( ) ; } } </s>
|
<s> package erjang ; import java . io . BufferedReader ; import java . io . File ; import java . io . FileReader ; import java . io . IOException ; import java . net . InetAddress ; import java . net . UnknownHostException ; import java . util . Map ; import java . util . concurrent . ConcurrentHashMap ; import java . util . concurrent . atomic . AtomicInteger ; import erjang . m . erlang . ErlDist ; import kilim . Pausable ; public abstract class EAbstractNode { public static final EAtom am_nonode_at_nohost = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_nodedown = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_nodeup = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_nodedown_reason = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_node_type = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_visible = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_hidden = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom am_all = EAtom . intern ( "<STR_LIT:all>" ) ; static String localHost = null ; EAtom node = am_nonode_at_nohost ; String host ; String alive ; EAtom cookie ; static EAtom defaultCookie = null ; static final int NTYPE_R6 = <NUM_LIT> ; static final int NTYPE_R4_ERLANG = <NUM_LIT> ; static final int NTYPE_R4_HIDDEN = <NUM_LIT> ; public static final int ERTS_NODES_MON_OPT_TYPE_VISIBLE = <NUM_LIT:1> << <NUM_LIT:0> ; public static final int ERTS_NODES_MON_OPT_TYPE_HIDDEN = <NUM_LIT:1> << <NUM_LIT:1> ; public static final int ERTS_NODES_MON_OPT_DOWN_REASON = <NUM_LIT:1> << <NUM_LIT:2> ; public static final int ERTS_NODES_MON_OPT_TYPES = ERTS_NODES_MON_OPT_TYPE_VISIBLE | ERTS_NODES_MON_OPT_TYPE_HIDDEN ; static final int dFlagPublished = <NUM_LIT:1> ; static final int dFlagAtomCache = <NUM_LIT:2> ; static final int dFlagExtendedReferences = <NUM_LIT:4> ; static final int dFlagDistMonitor = <NUM_LIT:8> ; static final int dFlagFunTags = <NUM_LIT> ; static final int dFlagDistMonitorName = <NUM_LIT> ; static final int dFlagHiddenAtomCache = <NUM_LIT> ; static final int dflagNewFunTags = <NUM_LIT> ; static final int dFlagExtendedPidsPorts = <NUM_LIT> ; static final int dFlagExportPtrTag = <NUM_LIT> ; static final int dFlagBitBinaries = <NUM_LIT> ; static final int dFlagNewFloats = <NUM_LIT> ; static final int dFUnicodeIO = <NUM_LIT> ; static final int dFDistHdrAtomCache = <NUM_LIT> ; static final int dFlagSmallAtoms = <NUM_LIT> ; int ntype = NTYPE_R6 ; int proto = <NUM_LIT:0> ; int distHigh = <NUM_LIT:5> ; int distLow = <NUM_LIT:5> ; int creation = <NUM_LIT:0> ; int flags = dFlagExtendedReferences | dFlagExtendedPidsPorts | dFlagBitBinaries | dFlagNewFloats | dFlagFunTags | dflagNewFunTags ; static { try { localHost = InetAddress . getLocalHost ( ) . getHostName ( ) ; final int dot = localHost . indexOf ( "<STR_LIT:.>" ) ; if ( dot != - <NUM_LIT:1> ) { localHost = localHost . substring ( <NUM_LIT:0> , dot ) ; } } catch ( final UnknownHostException e ) { localHost = "<STR_LIT:localhost>" ; } final String dotCookieFilename = System . getProperty ( "<STR_LIT>" ) + File . separator + "<STR_LIT>" ; BufferedReader br = null ; try { final File dotCookieFile = ERT . newFile ( dotCookieFilename ) ; br = new BufferedReader ( new FileReader ( dotCookieFile ) ) ; defaultCookie = EAtom . intern ( br . readLine ( ) . trim ( ) ) ; } catch ( final IOException e ) { defaultCookie = EAtom . intern ( "<STR_LIT>" ) ; } finally { try { if ( br != null ) { br . close ( ) ; } } catch ( final IOException e ) { } } } public EAbstractNode ( EAtom node ) { this ( node , defaultCookie ) ; } public EAbstractNode ( ) { this ( am_nonode_at_nohost ) ; } public EAbstractNode ( EAtom node , EAtom cookie ) { this . cookie = cookie ; set ( node , <NUM_LIT:0> ) ; } public void set ( EAtom node , int cr ) { String name = node . getName ( ) ; final int i = name . indexOf ( '<CHAR_LIT>' , <NUM_LIT:0> ) ; if ( i < <NUM_LIT:0> ) { alive = name ; host = localHost ; } else { alive = name . substring ( <NUM_LIT:0> , i ) ; host = name . substring ( i + <NUM_LIT:1> , name . length ( ) ) ; } if ( alive . length ( ) > <NUM_LIT> ) { alive = alive . substring ( <NUM_LIT:0> , <NUM_LIT> ) ; } this . node = EAtom . intern ( alive + "<STR_LIT:@>" + host ) ; this . creation = cr ; } public EAtom node ( ) { return node ; } public String host ( ) { return host ; } public String alive ( ) { return alive ; } public EAtom cookie ( ) { return cookie ; } int type ( ) { return ntype ; } int distHigh ( ) { return distHigh ; } int distLow ( ) { return distLow ; } int proto ( ) { return proto ; } int creation ( ) { return creation ; } public EAtom setCookie ( final EAtom cookie ) { final EAtom prev = this . cookie ; this . cookie = cookie ; return prev ; } ConcurrentHashMap < EHandle , AtomicInteger > node_monitors = new ConcurrentHashMap < EHandle , AtomicInteger > ( ) ; public void monitor_node ( EHandle caller , boolean on ) { node_monitors . putIfAbsent ( caller , new AtomicInteger ( ) ) ; AtomicInteger ami = node_monitors . get ( caller ) ; if ( on ) { ami . incrementAndGet ( ) ; } else { ami . decrementAndGet ( ) ; } } static ConcurrentHashMap < EHandle , ConcurrentHashMap < Integer , AtomicInteger > > nodes_monitors = new ConcurrentHashMap ( ) ; public static Boolean monitor_nodes ( EHandle caller , boolean on , ESeq opts_list ) { boolean all = false , visible = false , hidden = false ; int opts = <NUM_LIT:0> ; for ( ; ! opts_list . isNil ( ) ; opts_list = opts_list . tail ( ) ) { EObject opt = opts_list . head ( ) ; ETuple2 tp ; if ( opt == am_nodedown_reason ) { opts |= ERTS_NODES_MON_OPT_DOWN_REASON ; } else if ( ( tp = ETuple2 . cast ( opt ) ) != null ) { if ( tp . elem1 == am_node_type ) { if ( tp . elem2 == am_visible ) { if ( hidden || all ) return null ; opts |= ERTS_NODES_MON_OPT_TYPE_VISIBLE ; visible = true ; } else if ( tp . elem2 == am_hidden ) { if ( visible || all ) return null ; opts |= ERTS_NODES_MON_OPT_TYPE_HIDDEN ; hidden = true ; } else if ( tp . elem2 == am_all ) { if ( visible || hidden ) return null ; opts |= ERTS_NODES_MON_OPT_TYPES ; } else { return null ; } } else { return null ; } } else { return null ; } } return Boolean . valueOf ( monitor_nodes ( caller , on , opts ) ) ; } public static boolean monitor_nodes ( EHandle caller , boolean on , int opts ) { if ( on ) nodes_monitors . putIfAbsent ( caller , new ConcurrentHashMap ( ) ) ; ConcurrentHashMap < Integer , AtomicInteger > forHandle = nodes_monitors . get ( caller ) ; if ( forHandle == null ) return false ; if ( on ) { forHandle . putIfAbsent ( opts , new AtomicInteger ( <NUM_LIT:0> ) ) ; AtomicInteger ami = forHandle . get ( opts ) ; if ( ami == null ) return false ; int old = ami . getAndIncrement ( ) ; return old > <NUM_LIT:0> ; } else { AtomicInteger old = forHandle . remove ( opts ) ; return old != null && old . get ( ) > <NUM_LIT:0> ; } } public void node_going_down ( EHandle sender , EObject reason ) throws Pausable { for ( Map . Entry < EHandle , AtomicInteger > ent : node_monitors . entrySet ( ) ) { EHandle handle = ent . getKey ( ) ; AtomicInteger howmany = ent . getValue ( ) ; ETuple nd = ETuple . make ( am_nodedown , this . node ( ) ) ; while ( howmany . decrementAndGet ( ) >= <NUM_LIT:0> ) { handle . send ( sender , nd ) ; } } node_monitors . clear ( ) ; } public void node_up ( EHandle sender , EObject reason ) throws Pausable { for ( Map . Entry < EHandle , ConcurrentHashMap < Integer , AtomicInteger > > ent : nodes_monitors . entrySet ( ) ) { EHandle handle = ent . getKey ( ) ; ConcurrentHashMap < Integer , AtomicInteger > submap = ent . getValue ( ) ; for ( Map . Entry < Integer , AtomicInteger > subent : submap . entrySet ( ) ) { int opts = subent . getKey ( ) . intValue ( ) ; int howmany = subent . getValue ( ) . get ( ) ; final ETuple msg ; if ( opts == <NUM_LIT:0> ) { msg = ETuple . make ( am_nodeup , this . node ( ) ) ; } else { ECons info = ERT . NIL ; msg = ETuple . make ( am_nodeup , this . node ( ) , info ) ; } for ( int i = <NUM_LIT:0> ; i < howmany ; i ++ ) { handle . send ( sender , msg ) ; } } } } public abstract EObject dsig_reg_send ( EInternalPID caller , EAtom name , EObject msg ) throws Pausable ; public abstract void dsig_demonitor ( EHandle sender , ERef ref , EObject to_pid_or_name ) throws Pausable ; public static EAbstractNode get_or_connect ( ETask proc , EAtom n ) throws Pausable { EAbstractNode res = EPeer . get ( n ) ; if ( res == null && ( proc instanceof EProc ) ) { if ( ErlDist . net_kernel__connect__1 . invoke ( ( EProc ) proc , new EObject [ ] { n } ) == ERT . TRUE ) { return EPeer . get ( n ) ; } } return res ; } } </s>
|
<s> package erjang ; import java . nio . ByteBuffer ; import java . util . List ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; public class ENil extends ESeq { private static final Type ENIL_TYPE = Type . getType ( ENil . class ) ; private static final Type ERT_TYPE = Type . getType ( ERT . class ) ; public ENil ( ) { super ( ) ; } public boolean isNil ( ) { return true ; } public String stringValue ( ) { return "<STR_LIT>" ; } @ Override public EObject head ( ) { throw new UnsupportedOperationException ( ) ; } @ Override public EList tail ( ) { throw new UnsupportedOperationException ( ) ; } public boolean collectIOList ( List < ByteBuffer > out ) { return true ; } @ Override public void collectCharList ( CharCollector out ) { } @ Override public String toString ( ) { return "<STR_LIT:[]>" ; } @ Override public ESeq cons ( EObject h ) { return new EList ( h , this ) ; } @ Override public Type emit_const ( MethodVisitor fa ) { fa . visitFieldInsn ( Opcodes . GETSTATIC , ERT_TYPE . getInternalName ( ) , "<STR_LIT>" , ENIL_TYPE . getDescriptor ( ) ) ; return ENIL_TYPE ; } @ Override public ECons testNonEmptyList ( ) { return null ; } @ Override public ECons testCons ( ) { return this ; } public ENil testNil ( ) { return this ; } @ Override int compare_same ( EObject rhs ) { if ( rhs . testNil ( ) != null ) return <NUM_LIT:0> ; return - <NUM_LIT:1> ; } @ Override public void encode ( EOutputStream eos ) { eos . write_nil ( ) ; } @ Override public ESeq prepend ( ESeq list ) { return list ; } @ Override public boolean equalsExactly ( EObject rhs ) { return rhs . isNil ( ) ; } @ Override public boolean equals ( EObject other ) { return other . isNil ( ) ; } @ Override public boolean equals ( Object other ) { if ( other instanceof EObject ) { return ( ( EObject ) other ) . isNil ( ) ; } else { return false ; } } @ Override public int hashCode ( ) { return <NUM_LIT:0> ; } } </s>
|
<s> package erjang ; import kilim . Pausable ; public interface EFunHandler { EObject invoke ( EProc proc , EObject [ ] args ) throws Pausable ; } </s>
|
<s> package erjang ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; public class EPair extends ECons { public final EObject head ; public final EObject tail ; public EPair ( EObject h , EObject t ) { assert ( h != null ) ; assert ( t != null ) ; this . head = h ; this . tail = t ; } public ECons testNonEmptyList ( ) { return this ; } public EObject head ( ) { return head ; } public EObject tail ( ) { return tail ; } @ Override public String toString ( ) { StringBuffer sb = new StringBuffer ( "<STR_LIT:[>" ) ; EObject val = this ; while ( val instanceof ECons && val != ERT . NIL ) { ECons ep = ( ECons ) val ; if ( val != this ) { sb . append ( "<STR_LIT:U+002C>" ) ; } sb . append ( ep . head ( ) ) ; val = ep . tail ( ) ; } if ( val != ERT . NIL ) { sb . append ( '<CHAR_LIT>' ) ; sb . append ( val ) ; } sb . append ( "<STR_LIT:]>" ) ; return sb . toString ( ) ; } static Type EPAIR_TYPE = Type . getType ( EPair . class ) ; static Type ETERM_TYPE = Type . getType ( EObject . class ) ; static String CONSTRUCTOR_DESC = "<STR_LIT:(>" + ETERM_TYPE . getDescriptor ( ) + ETERM_TYPE . getDescriptor ( ) + "<STR_LIT>" ; @ Override public Type emit_const ( MethodVisitor fa ) { Type type = EPAIR_TYPE ; fa . visitTypeInsn ( Opcodes . NEW , type . getInternalName ( ) ) ; fa . visitInsn ( Opcodes . DUP ) ; ( ( EObject ) head ) . emit_const ( fa ) ; ( ( EObject ) tail ) . emit_const ( fa ) ; fa . visitMethodInsn ( Opcodes . INVOKESPECIAL , type . getInternalName ( ) , "<STR_LIT>" , CONSTRUCTOR_DESC ) ; return type ; } @ Override public void encode ( EOutputStream eos ) { eos . write_list_head ( <NUM_LIT:1> ) ; eos . write_any ( head ) ; eos . write_any ( tail ) ; } @ Override public int hashCode ( ) { return <NUM_LIT:31> * head . hashCode ( ) + tail . hashCode ( ) ; } } </s>
|
<s> package erjang ; public class LocalFunID extends FunID { final int index ; final int new_index ; final int uniq ; final EBinary new_uniq ; public LocalFunID ( EAtom module , EAtom function , int arity , int index , int new_index , int uniq , EBinary new_uniq ) { super ( module , function , arity ) ; this . index = index ; this . new_index = new_index ; this . uniq = uniq ; this . new_uniq = new_uniq ; } } </s>
|
<s> package erjang ; import java . lang . annotation . ElementType ; import java . lang . annotation . Retention ; import java . lang . annotation . RetentionPolicy ; import java . lang . annotation . Target ; @ Retention ( RetentionPolicy . RUNTIME ) @ Target ( ElementType . FIELD ) public @ interface Import { String module ( ) ; String fun ( ) ; int arity ( ) ; } </s>
|
<s> package erjang ; import java . io . IOException ; import java . lang . reflect . Constructor ; import java . lang . reflect . Method ; import java . lang . reflect . Modifier ; import java . util . Collections ; import java . util . HashMap ; import java . util . Map ; import java . util . regex . Pattern ; import java . util . Set ; import kilim . Pausable ; import kilim . analysis . ClassInfo ; import kilim . analysis . ClassWeaver ; import org . objectweb . asm . ClassWriter ; import org . objectweb . asm . FieldVisitor ; import org . objectweb . asm . Label ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; import erjang . beam . Compiler ; import erjang . beam . CompilerVisitor ; import erjang . beam . EUtil ; import erjang . m . ets . EMatchContext ; import erjang . m . ets . ETermPattern ; public abstract class EFun extends EObject implements Opcodes { public abstract int arity ( ) ; @ BIF public EAtom is_function ( EObject arity ) { ESmall ary ; if ( ( ary = arity . testSmall ( ) ) != null ) { return ERT . box ( arity ( ) == ary . value ) ; } throw ERT . badarg ( this , arity ) ; } public EFun testFunction2 ( int nargs ) { if ( this . arity ( ) == nargs ) return this ; return null ; } public EFun testFunction ( ) { return this ; } @ Override int cmp_order ( ) { return CMP_ORDER_FUN ; } @ Override int compare_same ( EObject rhs ) { if ( rhs == this ) return <NUM_LIT:0> ; return ( System . identityHashCode ( this ) & <NUM_LIT> ) - ( System . identityHashCode ( rhs ) & <NUM_LIT> ) ; } public EObject go ( EProc eproc ) throws Pausable { return go2 ( eproc ) ; } public EObject go2 ( EProc eproc ) { throw new java . lang . AbstractMethodError ( this . getClass ( ) . getName ( ) + "<STR_LIT>" ) ; } public abstract EObject invoke ( EProc proc , EObject [ ] args ) throws Pausable ; public EObject invoke ( EProc proc , EObject [ ] args , int off , int len ) throws Pausable { EObject [ ] new_args = new EObject [ len ] ; System . arraycopy ( args , off , new_args , <NUM_LIT:0> , len ) ; return invoke ( proc , new_args ) ; } public boolean match ( ETermPattern matcher , EMatchContext r ) { return matcher . match ( this , r ) ; } private static final Type EFUN_TYPE = Type . getType ( EFun . class ) ; private static final String EFUN_NAME = EFUN_TYPE . getInternalName ( ) ; private static final Type EATOM_TYPE = Type . getType ( EAtom . class ) ; private static final Type EFUNHANDLER_TYPE = Type . getType ( EFunHandler . class ) ; private static final Type EOBJECT_TYPE = Type . getType ( EObject . class ) ; private static final Type STRING_TYPE = Type . getType ( String . class ) ; private static final Type EOBJECT_ARR_TYPE = Type . getType ( EObject [ ] . class ) ; private static final Type EPROC_TYPE = Type . getType ( EProc . class ) ; static final String GO_DESC = "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EOBJECT_TYPE . getDescriptor ( ) ; private static final String EPROC_NAME = EPROC_TYPE . getInternalName ( ) ; private static final String EOBJECT_DESC = EOBJECT_TYPE . getDescriptor ( ) ; private static final String EATOM_DESC = EATOM_TYPE . getDescriptor ( ) ; static final String [ ] PAUSABLE_EX = new String [ ] { Type . getType ( Pausable . class ) . getInternalName ( ) } ; private static final HashMap < Method , EFun > method_fun_map = new HashMap < Method , EFun > ( ) ; private static final String ERJANG_MODULES_DOT = "<STR_LIT>" ; public static synchronized EFun make ( Method method , String module ) { EFun fun = method_fun_map . get ( method ) ; if ( fun == null ) { method_fun_map . put ( method , fun = do_make ( method , module ) ) ; } return fun ; } private static EFun do_make ( Method method , String module ) { assert ( Modifier . isStatic ( method . getModifiers ( ) ) ) ; assert ( ! Modifier . isPrivate ( method . getModifiers ( ) ) ) ; Class < ? > [ ] parameterTypes = method . getParameterTypes ( ) ; int ary = parameterTypes . length ; boolean proc = ( ary > <NUM_LIT:0> && parameterTypes [ <NUM_LIT:0> ] . equals ( EProc . class ) ) ; if ( proc ) ary -= <NUM_LIT:1> ; String fname = erlangNameOfMethod ( method ) ; String mname = EUtil . getJavaName ( EAtom . intern ( fname ) , ary ) ; Class < ? > declaringClass = method . getDeclaringClass ( ) ; Type type = Type . getType ( declaringClass ) ; byte [ ] data = CompilerVisitor . make_invoker ( module , fname , type , mname , method . getName ( ) , ary , proc , true , null , Type . getType ( method . getReturnType ( ) ) , true , true ) ; ClassLoader cl = declaringClass . getClassLoader ( ) ; get_fun_class ( ary ) ; data = weave ( data ) ; String clname = type . getClassName ( ) + "<STR_LIT>" + mname ; clname = clname . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) ; Class < ? extends EFun > res_class = ERT . defineClass ( cl , clname , data ) ; try { return res_class . newInstance ( ) ; } catch ( Exception e ) { throw new Error ( e ) ; } } private static String erlangNameOfMethod ( Method method ) { BIF bif_ann = method . getAnnotation ( BIF . class ) ; if ( bif_ann != null ) { String bif_name = bif_ann . name ( ) . equals ( "<STR_LIT>" ) ? method . getName ( ) : bif_ann . name ( ) ; if ( bif_ann . type ( ) == BIF . Type . GUARD ) return bif_name + "<STR_LIT>" ; else return bif_name ; } else { return method . getName ( ) ; } } @ SuppressWarnings ( "<STR_LIT:unchecked>" ) static Class < ? extends EFun > get_fun_class ( int arity ) { try { String className = EFUN_TYPE . getClassName ( ) + arity ; return ( Class < ? extends EFun > ) Class . forName ( className , true , EFun . class . getClassLoader ( ) ) ; } catch ( ClassNotFoundException ex ) { } byte [ ] data = gen_fun_class_data ( arity ) ; data = weave ( data ) ; String self_type = EFUN_TYPE . getInternalName ( ) + arity ; self_type = self_type . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) ; return ERT . defineClass ( EFun . class . getClassLoader ( ) , self_type , data ) ; } static byte [ ] gen_fun_class_data ( int arity ) { String self_type = EFUN_TYPE . getInternalName ( ) + arity ; ClassWriter cw = new ClassWriter ( true ) ; cw . visit ( Opcodes . V1_5 , Opcodes . ACC_PUBLIC | Opcodes . ACC_ABSTRACT , self_type , null , EFUN_TYPE . getInternalName ( ) , null ) ; make_invoke_method ( cw , self_type , arity ) ; CompilerVisitor . make_invoketail_method ( cw , self_type , arity , <NUM_LIT:0> ) ; MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitLdcInsn ( new Integer ( arity ) ) ; mv . visitInsn ( Opcodes . IRETURN ) ; mv . visitMaxs ( <NUM_LIT:2> , <NUM_LIT:2> ) ; mv . visitEnd ( ) ; mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + EOBJECT_ARR_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EOBJECT_TYPE . getDescriptor ( ) , null , PAUSABLE_EX ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:1> ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:2> ) ; push_int ( mv , i ) ; mv . visitInsn ( Opcodes . AALOAD ) ; } mv . visitMethodInsn ( Opcodes . INVOKEVIRTUAL , self_type , "<STR_LIT>" , EUtil . getSignature ( arity , true ) ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; mv = cw . visitMethod ( Opcodes . ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( Opcodes . INVOKESPECIAL , EFUN_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( Opcodes . RETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; make_cast_method ( cw , arity ) ; cw . visitEnd ( ) ; byte [ ] data = cw . toByteArray ( ) ; return data ; } static Map < String , Constructor < ? extends EFun > > handlers = new HashMap < String , Constructor < ? extends EFun > > ( ) ; static final Pattern JAVA_ID = Pattern . compile ( "<STR_LIT>" ) ; public static EFun get_fun_with_handler ( String module , String function , int arity , EFunHandler handler , ClassLoader loader ) { String signature = module + function + arity ; Constructor < ? extends EFun > h = handlers . get ( signature ) ; if ( h == null ) { get_fun_class ( arity ) ; String safe_function = JAVA_ID . matcher ( function ) . matches ( ) ? function : make_valid_java_id ( function ) ; StringBuffer sb = new StringBuffer ( ) ; String self_type = sb . append ( EFUN_TYPE . getInternalName ( ) ) . append ( module ) . append ( safe_function ) . append ( "<STR_LIT>" ) . append ( arity ) . toString ( ) ; ClassWriter cw = new ClassWriter ( true ) ; String super_class_name = EFUN_TYPE . getInternalName ( ) + arity ; cw . visit ( Opcodes . V1_4 , ACC_PUBLIC , self_type , null , super_class_name , null ) ; cw . visitField ( ACC_PRIVATE , "<STR_LIT>" , EFUNHANDLER_TYPE . getDescriptor ( ) , null , null ) . visitEnd ( ) ; cw . visitField ( ACC_PRIVATE | ACC_FINAL , "<STR_LIT>" , EATOM_DESC , null , null ) . visitEnd ( ) ; cw . visitField ( ACC_PRIVATE | ACC_FINAL , "<STR_LIT>" , EATOM_DESC , null , null ) . visitEnd ( ) ; MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT:(>" + EFUNHANDLER_TYPE . getDescriptor ( ) + "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESPECIAL , super_class_name , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( PUTFIELD , self_type , "<STR_LIT>" , EFUNHANDLER_TYPE . getDescriptor ( ) ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitLdcInsn ( module ) ; mv . visitMethodInsn ( INVOKESTATIC , EATOM_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitFieldInsn ( PUTFIELD , self_type , "<STR_LIT>" , EATOM_DESC ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitLdcInsn ( function ) ; mv . visitMethodInsn ( INVOKESTATIC , EATOM_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitFieldInsn ( PUTFIELD , self_type , "<STR_LIT>" , EATOM_DESC ) ; mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , self_type , "<STR_LIT>" , EFUNHANDLER_TYPE . getDescriptor ( ) ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; make_invoke_method ( cw , self_type , arity ) ; make_go_method ( cw , self_type , arity ) ; make_encode_method ( cw , self_type , arity ) ; cw . visitEnd ( ) ; byte [ ] data = cw . toByteArray ( ) ; data = weave ( data ) ; Class < ? extends EFun > clazz = ERT . defineClass ( loader , self_type . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) , data ) ; try { h = clazz . getConstructor ( EFunHandler . class ) ; } catch ( Exception e ) { throw new Error ( e ) ; } handlers . put ( signature , h ) ; } try { return h . newInstance ( handler ) ; } catch ( Exception e ) { throw new Error ( e ) ; } } private static void make_cast_method ( ClassWriter cw , int n ) { MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC | Opcodes . ACC_STATIC , "<STR_LIT:cast>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + EFUN_NAME + n + "<STR_LIT:;>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitTypeInsn ( INSTANCEOF , EFUN_NAME + n ) ; Label fail = new Label ( ) ; mv . visitJumpInsn ( Opcodes . IFEQ , fail ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitTypeInsn ( Opcodes . CHECKCAST , EFUN_NAME + n ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitLabel ( fail ) ; mv . visitInsn ( Opcodes . ACONST_NULL ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:2> , <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private static void make_go_method ( ClassWriter cw , String self_type , int arity ) { MethodVisitor mv ; mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , GO_DESC , null , PAUSABLE_EX ) ; mv . visitCode ( ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitInsn ( ACONST_NULL ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , self_type , "<STR_LIT>" , EFUNHANDLER_TYPE . getDescriptor ( ) ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitLdcInsn ( new Integer ( arity ) ) ; mv . visitTypeInsn ( ANEWARRAY , EOBJECT_TYPE . getInternalName ( ) ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitInsn ( DUP ) ; mv . visitLdcInsn ( new Integer ( i ) ) ; mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; mv . visitInsn ( AASTORE ) ; } mv . visitMethodInsn ( INVOKEINTERFACE , EFUNHANDLER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT:[>" + EOBJECT_DESC + "<STR_LIT:)>" + EOBJECT_DESC ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; cw . visitEnd ( ) ; } private static void make_invoke_method ( ClassWriter cw , String self_type , int arity ) { MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , EUtil . getSignature ( arity , true ) , null , PAUSABLE_EX ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , self_type , "<STR_LIT>" , EUtil . getSignature ( arity , true ) ) ; mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:2> ) ; Label done = new Label ( ) ; Label loop = new Label ( ) ; mv . visitLabel ( loop ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:2> ) ; if ( EProc . TAIL_MARKER == null ) { mv . visitJumpInsn ( IFNONNULL , done ) ; } else { mv . visitFieldInsn ( GETSTATIC , EPROC_TYPE . getInternalName ( ) , "<STR_LIT>" , EOBJECT_TYPE . getDescriptor ( ) ) ; mv . visitJumpInsn ( IF_ACMPNE , done ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_TYPE . getInternalName ( ) , "<STR_LIT>" , EFUN_TYPE . getDescriptor ( ) ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EFUN_TYPE . getInternalName ( ) , "<STR_LIT>" , GO_DESC ) ; mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:2> ) ; mv . visitJumpInsn ( GOTO , loop ) ; mv . visitLabel ( done ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:2> ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } static void make_encode_method ( ClassWriter cw , String className , int arity ) { MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT:(>" + Type . getDescriptor ( EOutputStream . class ) + "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , className , "<STR_LIT>" , EATOM_DESC ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , className , "<STR_LIT>" , EATOM_DESC ) ; mv . visitLdcInsn ( new Integer ( arity ) ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , Type . getInternalName ( EOutputStream . class ) , "<STR_LIT>" , "<STR_LIT:(>" + EATOM_DESC + EATOM_DESC + "<STR_LIT>" ) ; mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:4> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } private static String make_valid_java_id ( CharSequence seq ) { StringBuffer sb = new StringBuffer ( ) ; for ( int i = <NUM_LIT:0> ; i < seq . length ( ) ; i ++ ) { char ch = seq . charAt ( i ) ; if ( ( ch >= '<CHAR_LIT:a>' && ch <= '<CHAR_LIT>' ) || ( ch >= '<CHAR_LIT:A>' && ch <= '<CHAR_LIT:Z>' ) || ( ch >= '<CHAR_LIT:0>' && ch <= '<CHAR_LIT:9>' ) || ch == '<CHAR_LIT:_>' || ch == '<CHAR_LIT>' ) { sb . append ( ch ) ; } else { sb . append ( '<CHAR_LIT:_>' ) . append ( '<CHAR_LIT>' ) ; if ( ch < <NUM_LIT> ) sb . append ( '<CHAR_LIT:0>' ) ; sb . append ( Integer . toHexString ( ch ) . toUpperCase ( ) ) ; } } return sb . toString ( ) ; } @ SuppressWarnings ( "<STR_LIT:unchecked>" ) static Class < ? extends EFun > get_exported_fun_class ( int arity ) { String className = EFUN_TYPE . getClassName ( ) + arity + "<STR_LIT>" ; try { return ( Class < ? extends EFun > ) Class . forName ( className , true , EFun . class . getClassLoader ( ) ) ; } catch ( ClassNotFoundException ex ) { } byte [ ] data = get_exported_fun_class_data ( arity ) ; data = weave ( data ) ; return ERT . defineClass ( EFun . class . getClassLoader ( ) , className , data ) ; } static byte [ ] get_exported_fun_class_data ( int arity ) { ensure ( arity ) ; String super_type = EFUN_TYPE . getInternalName ( ) + arity ; String self_type = super_type + "<STR_LIT>" ; ClassWriter cw = new ClassWriter ( true ) ; cw . visit ( Opcodes . V1_5 , Opcodes . ACC_PUBLIC | Opcodes . ACC_ABSTRACT , self_type , null , super_type , null ) ; cw . visitField ( ACC_PROTECTED | ACC_FINAL , "<STR_LIT>" , EATOM_TYPE . getDescriptor ( ) , null , null ) . visitEnd ( ) ; cw . visitField ( ACC_PROTECTED | ACC_FINAL , "<STR_LIT>" , EATOM_TYPE . getDescriptor ( ) , null , null ) . visitEnd ( ) ; MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( Opcodes . INVOKESPECIAL , super_type , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:1> ) ; mv . visitMethodInsn ( Opcodes . INVOKESTATIC , EATOM_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitFieldInsn ( Opcodes . PUTFIELD , self_type , "<STR_LIT>" , EATOM_DESC ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:2> ) ; mv . visitMethodInsn ( Opcodes . INVOKESTATIC , EATOM_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitFieldInsn ( Opcodes . PUTFIELD , self_type , "<STR_LIT>" , EATOM_DESC ) ; mv . visitInsn ( Opcodes . RETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; make_encode_method_for_exported ( cw , self_type , arity ) ; cw . visitEnd ( ) ; byte [ ] data = cw . toByteArray ( ) ; return data ; } static void make_encode_method_for_exported ( ClassWriter cw , String className , int arity ) { MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT:(>" + Type . getDescriptor ( EOutputStream . class ) + "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , className , "<STR_LIT>" , EATOM_DESC ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , className , "<STR_LIT>" , EATOM_DESC ) ; mv . visitLdcInsn ( new Integer ( arity ) ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , Type . getInternalName ( EOutputStream . class ) , "<STR_LIT>" , "<STR_LIT:(>" + EATOM_DESC + EATOM_DESC + "<STR_LIT>" ) ; mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:4> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } private static void push_int ( MethodVisitor mv , int i ) { if ( i >= - <NUM_LIT:1> && i <= <NUM_LIT:5> ) { mv . visitInsn ( Opcodes . ICONST_0 + i ) ; } else { mv . visitLdcInsn ( new Integer ( i ) ) ; } } public static byte [ ] weave ( byte [ ] data ) { ClassWeaver w = new ClassWeaver ( data , new Compiler . ErjangDetector ( "<STR_LIT>" , ( Set < String > ) Collections . EMPTY_SET ) ) ; for ( ClassInfo ci : w . getClassInfos ( ) ) { if ( ! ci . className . startsWith ( "<STR_LIT>" ) ) data = ci . bytes ; } return data ; } public EObject apply ( EProc proc , ESeq a ) throws Pausable { return invoke ( proc , a . toArray ( ) ) ; } public static void main ( String [ ] args ) { for ( int i = <NUM_LIT:0> ; i < <NUM_LIT:10> ; i ++ ) { get_fun_class ( i ) ; get_exported_fun_class ( i ) ; } } public static void ensure ( int arity ) { get_fun_class ( arity ) ; } public static void ensure_exported ( int arity ) { get_exported_fun_class ( arity ) ; } public static EFun read ( EInputStream eis ) throws IOException { return eis . read_fun ( ) ; } public EObject info ( EAtom spec ) { FunID id = get_id ( ) ; if ( spec == ERT . am_arity ) { return new ETuple2 ( spec , ERT . box ( id . arity - get_env ( ) . length ( ) ) ) ; } else if ( spec == ERT . am_module ) { return new ETuple2 ( spec , id . module ) ; } else if ( spec == ERT . am_name ) { return new ETuple2 ( spec , id . function ) ; } else if ( spec == ERT . am_env ) { return new ETuple2 ( spec , this . get_env ( ) ) ; } else if ( spec == ERT . am_type ) { return new ETuple2 ( ERT . am_type , ( id instanceof LocalFunID ) ? ERT . am_local : ERT . am_external ) ; } if ( id instanceof LocalFunID ) { LocalFunID lid = ( LocalFunID ) id ; if ( spec == ERT . am_index ) { return new ETuple2 ( spec , ERT . box ( lid . index ) ) ; } else if ( spec == ERT . am_new_index ) { return new ETuple2 ( spec , ERT . box ( lid . new_index ) ) ; } else if ( spec == ERT . am_uniq ) { return new ETuple2 ( spec , ERT . box ( lid . uniq ) ) ; } else if ( spec == ERT . am_new_uniq ) { return new ETuple2 ( spec , lid . new_uniq ) ; } else if ( spec == ERT . am_pid ) { return new ETuple2 ( spec , this . get_pid ( ) ) ; } } else { if ( spec == ERT . am_type ) { return new ETuple2 ( ERT . am_type , ERT . am_external ) ; } } return ERT . am_undefined ; } public boolean is_local ( ) { return this . get_id ( ) instanceof LocalFunID ; } protected EObject get_pid ( ) { return ERT . NIL ; } protected ESeq get_env ( ) { return ERT . NIL ; } @ Override public String toString ( ) { FunID id = get_id ( ) ; if ( id instanceof LocalFunID ) { LocalFunID lid = ( LocalFunID ) id ; return "<STR_LIT>" + id . module + "<STR_LIT:.>" + lid . index + "<STR_LIT:.>" + lid . uniq + "<STR_LIT:>>" ; } else { return "<STR_LIT>" + id . module + "<STR_LIT::>" + id . function + "<STR_LIT:/>" + id . arity + "<STR_LIT:>>" ; } } protected FunID get_id ( ) { String cname = getClass ( ) . getName ( ) ; EAtom module = null ; if ( cname . startsWith ( ERJANG_MODULES_DOT ) ) { int last = cname . lastIndexOf ( '<CHAR_LIT:.>' ) ; module = EAtom . intern ( cname . substring ( ERJANG_MODULES_DOT . length ( ) , last ) ) ; } EAtom fun = null ; int end = cname . lastIndexOf ( "<STR_LIT>" ) ; int start = cname . indexOf ( "<STR_LIT>" ) ; if ( start != <NUM_LIT:1> && end != - <NUM_LIT:1> ) { String method_name = cname . substring ( start + <NUM_LIT:4> , end ) ; fun = EAtom . intern ( EUtil . decodeJavaName ( method_name ) ) ; } return new FunID ( module , fun , arity ( ) ) ; } } </s>
|
<s> package erjang ; import java . io . File ; import java . io . FileNotFoundException ; import java . io . IOException ; import java . util . ArrayList ; import java . util . List ; import java . util . logging . Level ; import java . util . logging . Logger ; import erjang . beam . BeamFileData ; import erjang . beam . BeamLoader ; import erjang . beam . EUtil ; import erjang . beam . loader . ErjangBeamDisLoader ; import erjang . driver . efile . ClassPathResource ; import erjang . util . Progress ; class EModuleLoader { static final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; final static BeamLoader beamParser = new ErjangBeamDisLoader ( ) ; public static EModule find_and_load_module ( String moduleName ) throws IOException { File input = findBeamFile ( moduleName ) ; if ( input == null ) throw new FileNotFoundException ( moduleName ) ; return load_module ( moduleName , input ) ; } public static EModule load_module ( String moduleName , File beamFile ) throws IOException { return load_module ( moduleName , EUtil . readFile ( beamFile ) ) ; } static long acc_int_load = <NUM_LIT:0> ; static long acc_load = <NUM_LIT:0> ; public static EModule load_module ( String moduleName , EBinary beamBin ) throws IOException { boolean use_interpreter = ErjangConfig . getBoolean ( "<STR_LIT>" ) ; long before = System . currentTimeMillis ( ) ; long after ; EModule loaded_module ; Progress . activity ( "<STR_LIT>" + moduleName + "<STR_LIT:...>" ) ; if ( use_interpreter ) { BeamFileData bfd = beamParser . load ( beamBin . toByteArray ( ) ) ; loaded_module = erjang . beam . interpreter . Interpreter . beamFileToEModule ( bfd ) ; after = System . currentTimeMillis ( ) ; } else { EModuleClassLoader moduleClassLoader = ErjangCodeCache . getModuleClassLoader ( moduleName , beamBin , beamParser ) ; after = System . currentTimeMillis ( ) ; loaded_module = load_compiled_module ( moduleName , moduleClassLoader ) ; } if ( log . isLoggable ( Level . FINE ) ) { long after_load = System . currentTimeMillis ( ) ; if ( use_interpreter ) acc_int_load += ( after_load - after ) ; else acc_load += ( after_load - after ) ; log . fine ( "<STR_LIT:[>" + moduleName + "<STR_LIT::>" + "<STR_LIT>" + ( after - before ) + "<STR_LIT>" + "<STR_LIT:;>" + ( after_load - after ) + "<STR_LIT>" + "<STR_LIT:(>" + acc_load + "<STR_LIT:)>" ) ; } Progress . done ( ) ; return loaded_module ; } private static File findBeamFile ( String module ) { String n = module ; for ( File e : loadPath ) { File beam = new File ( e , n + "<STR_LIT>" ) ; if ( beam . exists ( ) ) return beam ; if ( ClassPathResource . read_file ( beam . getPath ( ) ) != null ) return beam ; } return null ; } final static List < File > loadPath = new ArrayList < File > ( ) ; static { String sys_path = System . getenv ( "<STR_LIT>" ) ; if ( sys_path != null ) addLoadPaths ( loadPath , sys_path ) ; String path = ErjangConfig . getString ( "<STR_LIT>" , "<STR_LIT:.>" ) ; addLoadPaths ( loadPath , path ) ; } private static void addLoadPaths ( List < File > out , String path ) { for ( String s : path . split ( File . pathSeparator ) ) { File elem = ERT . newFile ( s ) ; if ( elem . exists ( ) && elem . isDirectory ( ) ) { out . add ( elem ) ; } } } @ SuppressWarnings ( "<STR_LIT:unchecked>" ) public static EModule load_compiled_module ( String mod , EModuleClassLoader loader ) { if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + mod + "<STR_LIT>" + loader ) ; } String internalName = erjang . beam . Compiler . moduleClassName ( mod ) ; String java_name = internalName . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) ; Class < ? extends EModule > clazz ; try { clazz = ( Class < ? extends EModule > ) loader . loadClass ( java_name ) ; } catch ( ClassNotFoundException e1 ) { e1 . printStackTrace ( ) ; throw new ErlangError ( e1 ) ; } EModule mi ; try { mi = clazz . newInstance ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; throw new ErlangError ( e ) ; } return mi ; } } </s>
|
<s> package erjang ; public interface ELocalHandle { } </s>
|
<s> package erjang . beam ; import java . io . File ; import java . io . IOException ; public abstract class BeamLoader { public abstract BeamFileData load ( File file ) throws IOException ; public abstract BeamFileData load ( byte [ ] data ) throws IOException ; } </s>
|
<s> package erjang . beam ; import static erjang . beam . CodeAtoms . ERLANG_ATOM ; import static erjang . beam . CodeAtoms . FALSE_ATOM ; import static erjang . beam . CodeAtoms . TRUE_ATOM ; import java . io . IOException ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collection ; import java . util . HashMap ; import java . util . HashSet ; import java . util . List ; import java . util . Map ; import java . util . Set ; import java . util . TreeMap ; import java . util . TreeSet ; import java . util . logging . Level ; import java . util . logging . Logger ; import kilim . Pausable ; import kilim . analysis . ClassInfo ; import kilim . analysis . ClassWeaver ; import org . objectweb . asm . AnnotationVisitor ; import org . objectweb . asm . ClassVisitor ; import org . objectweb . asm . ClassWriter ; import org . objectweb . asm . FieldVisitor ; import org . objectweb . asm . Label ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; import org . objectweb . asm . commons . Method ; import com . trifork . clj_ds . IPersistentCollection ; import erjang . EAtom ; import erjang . EBinMatchState ; import erjang . EBinary ; import erjang . EBitString ; import erjang . EBitStringBuilder ; import erjang . ECons ; import erjang . EDouble ; import erjang . EFun ; import erjang . EInteger ; import erjang . EBig ; import erjang . EInternalPID ; import erjang . EList ; import erjang . EModuleManager ; import erjang . ENil ; import erjang . ENumber ; import erjang . EObject ; import erjang . EOutputStream ; import erjang . EPID ; import erjang . EPort ; import erjang . EProc ; import erjang . ERT ; import erjang . ERef ; import erjang . ESeq ; import erjang . ESmall ; import erjang . EString ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . ErlangException ; import erjang . Export ; import erjang . FunID ; import erjang . Import ; import erjang . LocalFunID ; import erjang . Module ; import erjang . beam . Arg . Kind ; import erjang . beam . ModuleAnalyzer . FunInfo ; import erjang . beam . repr . ExtFun ; import erjang . beam . repr . Insn ; import erjang . m . erlang . ErlBif ; public class CompilerVisitor implements ModuleVisitor , Opcodes { public static boolean PARANOIA_MODE = false ; public static final int ATOM_SELECT_IF_ELSE_LIMIT = <NUM_LIT:4> ; ECons atts = ERT . NIL ; private Set < String > exported = new HashSet < String > ( ) ; private final ClassVisitor cv ; private EAtom module_name ; private Type self_type ; private static final EObject ATOM_field_flags = EAtom . intern ( "<STR_LIT>" ) ; private static final EObject ATOM_start = EAtom . intern ( "<STR_LIT:start>" ) ; static final String [ ] PAUSABLE_EX = new String [ ] { Type . getType ( Pausable . class ) . getInternalName ( ) } ; static final Type EBINMATCHSTATE_TYPE = Type . getType ( EBinMatchState . class ) ; static final Type EBINSTRINGBUILDER_TYPE = Type . getType ( EBitStringBuilder . class ) ; static final Type ERLANG_EXCEPTION_TYPE = Type . getType ( ErlangException . class ) ; static final Type ERT_TYPE = Type . getType ( ERT . class ) ; static final Type EINTEGER_TYPE = Type . getType ( EInteger . class ) ; static final Type ESTRING_TYPE = Type . getType ( EString . class ) ; static final Type ECOMPILEDMODULE_TYPE = Type . getType ( ECompiledModule . class ) ; static final String ECOMPILEDMODULE_NAME = ECOMPILEDMODULE_TYPE . getInternalName ( ) ; static final Type ENUMBER_TYPE = Type . getType ( ENumber . class ) ; static final Type EOBJECT_TYPE = Type . getType ( EObject . class ) ; static final String EOBJECT_DESC = EOBJECT_TYPE . getDescriptor ( ) ; static final Type EPROC_TYPE = Type . getType ( EProc . class ) ; static final String EPROC_NAME = EPROC_TYPE . getInternalName ( ) ; static final String EPROC_DESC = EPROC_TYPE . getDescriptor ( ) ; static final Type ESMALL_TYPE = Type . getType ( ESmall . class ) ; static final String ESMALL_NAME = ESMALL_TYPE . getInternalName ( ) ; static final Type EEXCEPTION_TYPE = Type . getType ( ErlangException . class ) ; static final String EEXCEPTION_DESC = EEXCEPTION_TYPE . getDescriptor ( ) ; static final String GO_DESC = "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EOBJECT_DESC ; static final Type EDOUBLE_TYPE = Type . getType ( EDouble . class ) ; static final Type EBIG_TYPE = Type . getType ( EBig . class ) ; static final Type ENIL_TYPE = Type . getType ( ENil . class ) ; static final Type EATOM_TYPE = Type . getType ( EAtom . class ) ; static final Type ETUPLE_TYPE = Type . getType ( ETuple . class ) ; static final Type EBINARY_TYPE = Type . getType ( EBinary . class ) ; static final Type EBITSTRING_TYPE = Type . getType ( EBitString . class ) ; static final Type EBITSTRINGBUILDER_TYPE = Type . getType ( EBitStringBuilder . class ) ; static final Type ECONS_TYPE = Type . getType ( ECons . class ) ; static final Type ESEQ_TYPE = Type . getType ( ESeq . class ) ; static final Type ELIST_TYPE = Type . getType ( EList . class ) ; static final Type EFUN_TYPE = Type . getType ( EFun . class ) ; static final String EFUN_NAME = EFUN_TYPE . getInternalName ( ) ; static final String EOBJECT_NAME = EOBJECT_TYPE . getInternalName ( ) ; static final String ETUPLE_NAME = ETUPLE_TYPE . getInternalName ( ) ; static final String ERT_NAME = ERT_TYPE . getInternalName ( ) ; static final String EDOUBLE_NAME = EDOUBLE_TYPE . getInternalName ( ) ; static final String EBIG_NAME = EBIG_TYPE . getInternalName ( ) ; static final String EINTEGER_NAME = EINTEGER_TYPE . getInternalName ( ) ; static final String ENIL_NAME = ENIL_TYPE . getInternalName ( ) ; static final String ESEQ_NAME = ESEQ_TYPE . getInternalName ( ) ; static final String ETUPLE_DESC = ETUPLE_TYPE . getDescriptor ( ) ; static final String EATOM_DESC = EATOM_TYPE . getDescriptor ( ) ; static final String ECONS_DESC = ECONS_TYPE . getDescriptor ( ) ; static final String ESEQ_DESC = ESEQ_TYPE . getDescriptor ( ) ; static final String EFUN_DESCRIPTOR = EFUN_TYPE . getDescriptor ( ) ; static final Type EPID_TYPE = Type . getType ( EPID . class ) ; static final Type EPORT_TYPE = Type . getType ( EPort . class ) ; static final Type EMATCHSTATE_TYPE = Type . getType ( EBinMatchState . class ) ; static final Type MODULE_ANN_TYPE = Type . getType ( Module . class ) ; static final Type IMPORT_ANN_TYPE = Type . getType ( Import . class ) ; static final Type EXPORT_ANN_TYPE = Type . getType ( Export . class ) ; private final ClassRepo classRepo ; public CompilerVisitor ( ClassVisitor cv , ClassRepo classRepo ) { this . cv = cv ; this . classRepo = classRepo ; } @ Override public void visitModule ( EAtom name ) { this . module_name = name ; this . self_type = Type . getType ( "<STR_LIT>" + getInternalClassName ( ) + "<STR_LIT:;>" ) ; cv . visit ( V1_6 , ACC_PUBLIC , self_type . getInternalName ( ) , null , ECOMPILEDMODULE_NAME , null ) ; add_module_annotation ( cv ) ; cv . visitSource ( name . getName ( ) + "<STR_LIT>" , null ) ; } private void add_module_annotation ( ClassVisitor cv ) { AnnotationVisitor av = cv . visitAnnotation ( MODULE_ANN_TYPE . getDescriptor ( ) , true ) ; av . visit ( "<STR_LIT:value>" , getModuleName ( ) ) ; av . visitEnd ( ) ; } public String getInternalClassName ( ) { String moduleName = getModuleName ( ) ; return Compiler . moduleClassName ( moduleName ) ; } private String getModuleName ( ) { return module_name . getName ( ) ; } Map < EObject , String > constants = new HashMap < EObject , String > ( ) ; @ Override public void visitAttribute ( EAtom att , EObject value ) { atts = atts . cons ( ETuple2 . make ( att , value ) ) ; } @ Override public void visitEnd ( ) { for ( Map . Entry < String , ExtFun > ent : imported . entrySet ( ) ) { String field_name = ent . getKey ( ) ; ExtFun f = ent . getValue ( ) ; FieldVisitor fv = cv . visitField ( ACC_STATIC , ent . getKey ( ) , "<STR_LIT>" + EFUN_NAME + f . arity + "<STR_LIT:;>" , null , null ) ; EFun . ensure ( f . arity ) ; AnnotationVisitor av = fv . visitAnnotation ( IMPORT_ANN_TYPE . getDescriptor ( ) , true ) ; av . visit ( "<STR_LIT>" , f . mod . getName ( ) ) ; av . visit ( "<STR_LIT>" , f . fun . getName ( ) ) ; av . visit ( "<STR_LIT>" , f . arity ) ; av . visitEnd ( ) ; fv . visitEnd ( ) ; } generate_classinit ( ) ; cv . visitEnd ( ) ; } private void generate_classinit ( ) { MethodVisitor mv = cv . visitMethod ( ACC_STATIC | ACC_PRIVATE , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; for ( Map . Entry < String , String > ent : funs . entrySet ( ) ) { String field = ent . getKey ( ) ; String clazz = ent . getValue ( ) ; mv . visitTypeInsn ( NEW , clazz ) ; mv . visitInsn ( DUP ) ; mv . visitMethodInsn ( INVOKESPECIAL , clazz , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitFieldInsn ( PUTSTATIC , self_type . getInternalName ( ) , field , "<STR_LIT>" + funt . get ( field ) + "<STR_LIT:;>" ) ; } for ( Map . Entry < EObject , String > ent : constants . entrySet ( ) ) { EObject term = ent . getKey ( ) ; term . emit_const ( mv ) ; mv . visitFieldInsn ( Opcodes . PUTSTATIC , self_type . getInternalName ( ) , ent . getValue ( ) , Type . getType ( term . getClass ( ) ) . getDescriptor ( ) ) ; } cv . visitField ( ACC_STATIC | ACC_PRIVATE , "<STR_LIT>" , ESEQ_TYPE . getDescriptor ( ) , null , null ) ; atts . emit_const ( mv ) ; mv . visitFieldInsn ( Opcodes . PUTSTATIC , self_type . getInternalName ( ) , "<STR_LIT>" , ESEQ_TYPE . getDescriptor ( ) ) ; if ( this . module_md5 != null ) { cv . visitField ( ACC_STATIC , "<STR_LIT>" , EBINARY_TYPE . getDescriptor ( ) , null , null ) ; module_md5 . emit_const ( mv ) ; mv . visitFieldInsn ( PUTSTATIC , self_type . getInternalName ( ) , "<STR_LIT>" , EBINARY_TYPE . getDescriptor ( ) ) ; } mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT> , <NUM_LIT:10> ) ; mv . visitEnd ( ) ; mv = cv . visitMethod ( ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitLdcInsn ( this . module_name . getName ( ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; mv = cv . visitMethod ( ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" + ESEQ_TYPE . getDescriptor ( ) , null , null ) ; mv . visitCode ( ) ; mv . visitFieldInsn ( Opcodes . GETSTATIC , self_type . getInternalName ( ) , "<STR_LIT>" , ESEQ_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; mv = cv . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESPECIAL , ECOMPILEDMODULE_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; mv = cv . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESPECIAL , ECOMPILEDMODULE_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; for ( Lambda l : lambdas_xx . values ( ) ) { mv . visitTypeInsn ( NEW , Type . getInternalName ( LocalFunID . class ) ) ; mv . visitInsn ( DUP ) ; module_name . emit_const ( mv ) ; l . fun . emit_const ( mv ) ; push_int ( mv , l . arity ) ; push_int ( mv , l . old_index ) ; push_int ( mv , l . index ) ; push_int ( mv , l . old_uniq ) ; mv . visitFieldInsn ( GETSTATIC , self_type . getInternalName ( ) , "<STR_LIT>" , EBINARY_TYPE . getDescriptor ( ) ) ; mv . visitMethodInsn ( INVOKESPECIAL , Type . getInternalName ( LocalFunID . class ) , "<STR_LIT>" , "<STR_LIT:(>" + EATOM_DESC + EATOM_DESC + "<STR_LIT>" + EBINARY_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; mv . visitInsn ( DUP ) ; cv . visitField ( ACC_STATIC , anon_fun_name ( l ) , Type . getDescriptor ( LocalFunID . class ) , null , null ) . visitEnd ( ) ; mv . visitFieldInsn ( PUTSTATIC , self_type . getInternalName ( ) , anon_fun_name ( l ) , Type . getDescriptor ( LocalFunID . class ) ) ; String mname = EUtil . getJavaName ( l . fun , l . arity - l . freevars ) ; String outer_name = self_type . getInternalName ( ) ; String inner_name = "<STR_LIT>" + mname ; String full_inner_name = outer_name + "<STR_LIT:$>" + inner_name ; mv . visitLdcInsn ( full_inner_name . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) ) ; mv . visitMethodInsn ( INVOKESTATIC , Type . getInternalName ( Class . class ) , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitMethodInsn ( INVOKESTATIC , Type . getInternalName ( EModuleManager . class ) , "<STR_LIT>" , "<STR_LIT:(>" + Type . getDescriptor ( LocalFunID . class ) + Type . getDescriptor ( Class . class ) + "<STR_LIT>" ) ; } mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } public static String anon_fun_name ( Lambda l ) { return "<STR_LIT>" + l . index + "<STR_LIT:_>" + l . old_index + "<STR_LIT:_>" + l . old_uniq ; } private void push_int ( MethodVisitor mv , int val ) { if ( val == - <NUM_LIT:1> ) { mv . visitInsn ( ICONST_M1 ) ; } else if ( val >= <NUM_LIT:0> && val <= <NUM_LIT:5> ) { mv . visitInsn ( ICONST_0 + val ) ; } else { mv . visitLdcInsn ( new Integer ( val ) ) ; } } @ Override public void visitExport ( EAtom name , int arity , int entry ) { exported . add ( EUtil . getJavaName ( name , arity ) ) ; } boolean isExported ( EAtom name , int arity ) { return exported . contains ( EUtil . getJavaName ( name , arity ) ) ; } @ Override public void declareFunction ( EAtom fun , int arity , int label ) { } @ Override public FunctionVisitor visitFunction ( EAtom name , int arity , int startLabel ) { return new ASMFunctionAdapter ( name , arity , startLabel ) ; } Map < FunID , Lambda > lambdas_xx = new TreeMap < FunID , Lambda > ( ) ; Map < String , String > funs = new HashMap < String , String > ( ) ; Map < String , String > funt = new HashMap < String , String > ( ) ; Set < String > non_pausable_methods = new HashSet < String > ( ) ; public EBinary module_md5 ; private static final String SEQ_CONS_SIG = "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + ESEQ_DESC ; private static final String FUNC_INFO_SIG = "<STR_LIT:(>" + EATOM_DESC + EATOM_DESC + ESEQ_DESC + "<STR_LIT:)>" + EOBJECT_DESC ; private static final String ERT_CONS_SIG = "<STR_LIT:(>" + EOBJECT_DESC + EOBJECT_DESC + "<STR_LIT:)>" + ECONS_TYPE . getDescriptor ( ) ; private static final String TEST_FUN_SIG = "<STR_LIT:(>" + EOBJECT_DESC + EFUN_DESCRIPTOR + "<STR_LIT>" ; class ASMFunctionAdapter implements FunctionVisitor2 { private final EAtom fun_name ; private final int arity ; private final int startLabel ; Map < Integer , Label > labels = new TreeMap < Integer , Label > ( ) ; Map < Integer , Label > ex_handler_labels = new TreeMap < Integer , Label > ( ) ; Set < Integer > label_inserted = new TreeSet < Integer > ( ) ; List < EXHandler > ex_handlers = new ArrayList < EXHandler > ( ) ; EXHandler activeExceptionHandler ; BeamExceptionHandler active_beam_exh ; private boolean isTailRecursive ; private MethodVisitor mv ; private int [ ] xregs ; private int [ ] yregs ; private int [ ] fpregs ; private Label start ; private Label end ; private int scratch_reg ; private int bit_string_builder ; private int bit_string_matcher ; private int bit_string_save ; private FunInfo funInfo ; Label getLabel ( int i ) { if ( i <= <NUM_LIT:0> ) throw new Error ( ) ; Label l = labels . get ( i ) ; if ( l == null ) { labels . put ( i , l = new Label ( ) ) ; } return l ; } Label getExceptionHandlerLabel ( BeamExceptionHandler exh ) { int i = exh . getHandlerLabel ( ) ; Label l = ex_handler_labels . get ( i ) ; if ( l == null ) { ex_handler_labels . put ( i , l = new Label ( ) ) ; } return l ; } public ASMFunctionAdapter ( EAtom name , int arity , int startLabel ) { this . fun_name = name ; this . arity = arity ; this . startLabel = startLabel ; } @ Override public void visitMaxs ( Collection < Integer > x_regs , int y_count , int fp_count , boolean isTailRecursive ) { FunID me = new FunID ( module_name , fun_name , arity ) ; this . funInfo = funInfos . get ( me ) ; this . isTailRecursive = isTailRecursive ; Lambda lambda = get_lambda_freevars ( fun_name , arity ) ; final int freevars = lambda == null ? <NUM_LIT:0> : lambda . freevars ; int real_arity = arity - freevars ; String javaName = EUtil . getJavaName ( fun_name , real_arity ) ; String signature = EUtil . getSignature ( arity , true ) ; mv = cv . visitMethod ( ACC_STATIC | ACC_PUBLIC , javaName , signature , null , funInfo . is_pausable ? PAUSABLE_EX : null ) ; if ( ! funInfo . is_pausable ) { non_pausable_methods . add ( javaName ) ; } this . start = new Label ( ) ; this . end = new Label ( ) ; mv . visitCode ( ) ; allocate_regs_to_locals ( x_regs , y_count , fp_count ) ; mv . visitLabel ( start ) ; mv . visitJumpInsn ( GOTO , getLabel ( startLabel ) ) ; } @ Override public void visitEnd ( ) { adjust_exception_handlers ( null , false ) ; mv . visitLabel ( end ) ; for ( EXHandler h : ex_handlers ) { if ( ! label_inserted . contains ( h . handler_beam_label ) ) throw new InternalError ( "<STR_LIT>" + h . handler_beam_label ) ; mv . visitTryCatchBlock ( h . begin , h . end , h . target , Type . getType ( ErlangException . class ) . getInternalName ( ) ) ; } mv . visitMaxs ( <NUM_LIT:20> , scratch_reg + <NUM_LIT:3> ) ; mv . visitEnd ( ) ; int arity_plus = arity ; Lambda lambda = get_lambda_freevars ( fun_name , arity_plus ) ; final int freevars = lambda == null ? <NUM_LIT:0> : lambda . freevars ; int real_arity = arity_plus - freevars ; String mname = EUtil . getJavaName ( fun_name , real_arity ) ; String outer_name = self_type . getInternalName ( ) ; String inner_name = "<STR_LIT>" + mname ; String full_inner_name = outer_name + "<STR_LIT:$>" + inner_name ; boolean make_fun = false ; boolean is_exported = isExported ( fun_name , arity ) ; if ( lambda != null ) { CompilerVisitor . this . module_md5 = lambda . uniq ; make_fun = true ; } else { if ( funInfo . is_called_locally_in_nontail_position ) generate_invoke_call_self ( ) ; if ( funInfo . is_called_locally_in_tail_position ) generate_tail_call_self ( full_inner_name ) ; if ( funInfo . mustHaveFun ( ) ) { FieldVisitor fv = cv . visitField ( ACC_STATIC | ACC_FINAL , mname , "<STR_LIT>" + full_inner_name + "<STR_LIT:;>" , null , null ) ; EFun . ensure ( arity ) ; if ( is_exported ) { if ( ModuleAnalyzer . log . isLoggable ( Level . FINE ) ) ModuleAnalyzer . log . fine ( "<STR_LIT>" + module_name + "<STR_LIT::>" + fun_name + "<STR_LIT:/>" + arity ) ; AnnotationVisitor an = fv . visitAnnotation ( EXPORT_ANN_TYPE . getDescriptor ( ) , true ) ; an . visit ( "<STR_LIT>" , module_name . getName ( ) ) ; an . visit ( "<STR_LIT>" , fun_name . getName ( ) ) ; an . visit ( "<STR_LIT>" , new Integer ( arity ) ) ; an . visitEnd ( ) ; } fv . visitEnd ( ) ; funs . put ( mname , full_inner_name ) ; funt . put ( mname , full_inner_name ) ; EFun . ensure ( arity ) ; make_fun = true ; } } if ( make_fun ) { cv . visitInnerClass ( full_inner_name , outer_name , inner_name , ACC_STATIC ) ; byte [ ] data = CompilerVisitor . make_invoker ( module_name . getName ( ) , fun_name . getName ( ) , self_type , mname , mname , arity , true , is_exported , lambda , EOBJECT_TYPE , funInfo . may_return_tail_marker , funInfo . is_pausable | funInfo . call_is_pausable ) ; ClassWeaver w = new ClassWeaver ( data , new Compiler . ErjangDetector ( self_type . getInternalName ( ) , non_pausable_methods ) ) ; if ( w . getClassInfos ( ) . size ( ) == <NUM_LIT:0> ) { try { classRepo . store ( full_inner_name , data ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } else { for ( ClassInfo ci : w . getClassInfos ( ) ) { try { classRepo . store ( ci . className , ci . bytes ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } } } } private void ensure_exception_handler_in_place ( ) { adjust_exception_handlers ( active_beam_exh , false ) ; } private void adjust_exception_handlers ( BeamExceptionHandler exh , boolean expectChange ) { int desiredLabel = exh == null ? - <NUM_LIT:1> : exh . getHandlerLabel ( ) ; int actualLabel = activeExceptionHandler == null ? - <NUM_LIT:1> : activeExceptionHandler . handler_beam_label ; if ( expectChange ) assert ( actualLabel != desiredLabel ) ; if ( actualLabel != desiredLabel ) { if ( activeExceptionHandler != null ) { mv . visitLabel ( activeExceptionHandler . end ) ; activeExceptionHandler = null ; } if ( exh != null ) { EXHandler h = new EXHandler ( ) ; h . begin = new Label ( ) ; h . end = new Label ( ) ; h . target = getExceptionHandlerLabel ( exh ) ; h . handler_beam_label = exh . getHandlerLabel ( ) ; h . beam_exh = exh ; ex_handlers . add ( h ) ; mv . visitLabel ( h . begin ) ; mv . visitInsn ( NOP ) ; activeExceptionHandler = h ; } } } private void generate_invoke_call_self ( ) { boolean pausable = funInfo . is_pausable || funInfo . call_is_pausable ; String javaName = EUtil . getJavaName ( fun_name , arity ) ; String signature = EUtil . getSignature ( arity , true ) ; mv = cv . visitMethod ( ACC_STATIC , javaName + "<STR_LIT>" , signature , null , pausable ? PAUSABLE_EX : null ) ; mv . visitCode ( ) ; if ( ! pausable ) { non_pausable_methods . add ( javaName + "<STR_LIT>" ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( ALOAD , i + <NUM_LIT:1> ) ; } mv . visitMethodInsn ( INVOKESTATIC , self_type . getInternalName ( ) , javaName , EUtil . getSignature ( arity , true ) ) ; if ( funInfo . may_return_tail_marker ) { mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:1> ) ; Label done = new Label ( ) ; Label loop = new Label ( ) ; mv . visitLabel ( loop ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:1> ) ; if ( EProc . TAIL_MARKER == null ) { mv . visitJumpInsn ( IFNONNULL , done ) ; } else { mv . visitFieldInsn ( GETSTATIC , EPROC_NAME , "<STR_LIT>" , EOBJECT_DESC ) ; mv . visitJumpInsn ( IF_ACMPNE , done ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_NAME , "<STR_LIT>" , EFUN_DESCRIPTOR ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EFUN_NAME , ( pausable ? "<STR_LIT>" : "<STR_LIT>" ) , GO_DESC ) ; mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:1> ) ; mv . visitJumpInsn ( GOTO , loop ) ; mv . visitLabel ( done ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:1> ) ; } mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private void generate_tail_call_self ( String full_inner_name ) { String javaName = EUtil . getJavaName ( fun_name , arity ) ; String signature = EUtil . getSignature ( arity , true ) ; mv = cv . visitMethod ( ACC_STATIC , javaName + "<STR_LIT>" , signature , null , null ) ; mv . visitCode ( ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( ALOAD , i + <NUM_LIT:1> ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETSTATIC , self_type . getInternalName ( ) , javaName , "<STR_LIT>" + full_inner_name + "<STR_LIT:;>" ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" , EFUN_DESCRIPTOR ) ; if ( EProc . TAIL_MARKER == null ) { mv . visitInsn ( ACONST_NULL ) ; } else { mv . visitFieldInsn ( GETSTATIC , EPROC_NAME , "<STR_LIT>" , EOBJECT_DESC ) ; } mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private void allocate_regs_to_locals ( Collection < Integer > xRegs , int yCount , int fpCount ) { int max_y = yCount ; int max_f = fpCount ; int local = <NUM_LIT:1> ; Integer [ ] xxregs = xRegs . toArray ( new Integer [ xRegs . size ( ) ] ) ; if ( xxregs . length > <NUM_LIT:0> ) { Arrays . sort ( xxregs ) ; Integer biggest_used = xxregs [ xxregs . length - <NUM_LIT:1> ] ; xregs = new int [ biggest_used + <NUM_LIT:1> ] ; for ( int i = <NUM_LIT:0> ; i < xxregs . length ; i ++ ) { xregs [ xxregs [ i ] ] = i + local ; } local += xxregs . length ; } yregs = new int [ max_y ] ; for ( int i = <NUM_LIT:0> ; i < max_y ; i ++ ) { yregs [ i ] = local ; local += <NUM_LIT:1> ; } fpregs = new int [ max_f ] ; for ( int i = <NUM_LIT:0> ; i < max_f ; i ++ ) { fpregs [ i ] = local ; local += <NUM_LIT:2> ; } this . bit_string_builder = local ++ ; this . bit_string_matcher = local ++ ; this . bit_string_save = local ++ ; this . scratch_reg = local ; } @ Override public BlockVisitor visitLabeledBlock ( int label ) { Label blockLabel = getLabel ( label ) ; mv . visitLabel ( blockLabel ) ; label_inserted . add ( label ) ; mv . visitLineNumber ( label & <NUM_LIT> , blockLabel ) ; return new ASMBlockVisitor ( label ) ; } class ASMBlockVisitor implements BlockVisitor2 { final int beam_label ; public ASMBlockVisitor ( int beam_label ) { this . beam_label = beam_label ; } @ Override public void visitBegin ( BeamExceptionHandler exh ) { active_beam_exh = exh ; if ( exh == null || ( exh . getHandlerLabel ( ) != beam_label ) ) adjust_exception_handlers ( exh , false ) ; } @ Override public void visitBSAdd ( Arg in1 , Arg in2 , int scale , Arg out ) { push ( in1 , Type . INT_TYPE ) ; push_scaled ( in2 , scale ) ; mv . visitInsn ( IADD ) ; pop ( out , Type . INT_TYPE ) ; } @ Override public void visitBS ( BeamOpcode opcode , Arg arg , Arg imm ) { switch ( opcode ) { case bs_save2 : case bs_restore2 : push ( arg , EOBJECT_TYPE ) ; if ( imm . value == ATOM_start ) { String methName = ( opcode == BeamOpcode . bs_restore2 ) ? "<STR_LIT>" : "<STR_LIT>" ; mv . visitMethodInsn ( INVOKESTATIC , EBINMATCHSTATE_TYPE . getInternalName ( ) , methName , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; } else { push ( imm , Type . INT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , EBINMATCHSTATE_TYPE . getInternalName ( ) , opcode . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; } return ; case bs_context_to_binary : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , EBINMATCHSTATE_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + EOBJECT_DESC ) ; pop ( arg , EOBJECT_TYPE ) ; return ; case bs_utf8_size : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , EBINSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + ESMALL_TYPE . getDescriptor ( ) ) ; pop ( imm , ESMALL_TYPE ) ; return ; case bs_utf16_size : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , EBINSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + ESMALL_TYPE . getDescriptor ( ) ) ; pop ( imm , ESMALL_TYPE ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } @ Override public void visitInitWritable ( Arg size , Arg out ) { push ( size , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + EBITSTRINGBUILDER_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , bit_string_builder ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" + EBITSTRING_TYPE . getDescriptor ( ) ) ; pop ( out , EBITSTRING_TYPE ) ; } @ Override public void visitInitBitString ( Arg size , int flags , Arg out , boolean unit_is_bits ) { push ( size , Type . INT_TYPE ) ; mv . visitLdcInsn ( new Integer ( flags ) ) ; String methodName = unit_is_bits ? "<STR_LIT>" : "<STR_LIT>" ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , methodName , "<STR_LIT>" + EBITSTRINGBUILDER_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , bit_string_builder ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" + EBITSTRING_TYPE . getDescriptor ( ) ) ; pop ( out , EBITSTRING_TYPE ) ; return ; } @ Override public void visitBitStringAppend ( BeamOpcode opcode , int label , Arg extra_size , Arg src , int unit , int flags , Arg dst ) { push ( src , EOBJECT_TYPE ) ; push ( extra_size , Type . INT_TYPE ) ; push_int ( unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKESTATIC , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , opcode . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + EBITSTRINGBUILDER_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , bit_string_builder ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" + EBITSTRING_TYPE . getDescriptor ( ) ) ; pop ( dst , EBITSTRING_TYPE ) ; return ; } @ Override public void visitBitStringPut ( BeamOpcode opcode , Arg arg , Arg size , int unit , int flags ) { switch ( opcode ) { case bs_put_string : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , ESTRING_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + ESTRING_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; return ; case bs_put_integer : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EINTEGER_TYPE ) ; push_scaled ( size , unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; case bs_put_float : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EDOUBLE_TYPE ) ; push_scaled ( size , unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; case bs_put_utf8 : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EINTEGER_TYPE ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; case bs_put_utf16 : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EINTEGER_TYPE ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; case bs_put_utf32 : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EINTEGER_TYPE ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; case bs_put_binary : mv . visitVarInsn ( ALOAD , bit_string_builder ) ; push ( arg , EBITSTRING_TYPE ) ; if ( size . kind == Kind . IMMEDIATE && size . value . equals ( EAtom . intern ( "<STR_LIT:all>" ) ) ) push_int ( - <NUM_LIT:1> ) ; else push_scaled ( size , unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBITSTRINGBUILDER_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } @ Override public void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , int intg , Arg dst ) { switch ( test ) { case bs_start_match2 : { push ( in , EOBJECT_TYPE ) ; push_int ( intg ) ; mv . visitMethodInsn ( INVOKESTATIC , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + EMATCHSTATE_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , bit_string_matcher ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , bit_string_matcher ) ; pop ( dst , EBINMATCHSTATE_TYPE ) ; return ; } case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : { push ( in , EBINMATCHSTATE_TYPE ) ; push_int ( intg ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT>" ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ISTORE , scratch_reg ) ; mv . visitJumpInsn ( IFLT , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ILOAD , scratch_reg ) ; emit_box ( Type . INT_TYPE , ESMALL_TYPE ) ; pop ( dst , ESMALL_TYPE ) ; return ; } default : throw new Error ( "<STR_LIT>" + test ) ; } } @ Override public void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , EBitString bin ) { switch ( test ) { case bs_match_string : { push ( in , EBINMATCHSTATE_TYPE ) ; push_immediate ( bin , EBITSTRING_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT:(>" + EBITSTRING_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EBITSTRING_TYPE ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; return ; } default : throw new Error ( "<STR_LIT>" + test ) ; } } @ Override public void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , Arg bits , int unit , int flags ) { switch ( test ) { case bs_skip_bits2 : { push ( in , EBINMATCHSTATE_TYPE ) ; push ( bits , EINTEGER_TYPE ) ; push_int ( unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + EOBJECT_DESC ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; return ; } default : throw new Error ( "<STR_LIT>" + test ) ; } } @ Override public void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , Arg bits , int unit , int flags , Arg dst ) { switch ( test ) { case bs_get_binary2 : { push ( in , EBINMATCHSTATE_TYPE ) ; push ( bits , EOBJECT_TYPE ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + EBITSTRING_TYPE ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( dst , EBITSTRING_TYPE ) ; return ; } case bs_get_integer2 : { push ( in , EBINMATCHSTATE_TYPE ) ; push ( bits , Type . INT_TYPE ) ; push_int ( unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT>" + EINTEGER_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( dst , EOBJECT_TYPE ) ; return ; } case bs_get_float2 : { push ( in , EBINMATCHSTATE_TYPE ) ; push ( bits , Type . INT_TYPE ) ; push_int ( unit ) ; push_int ( flags ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT>" + EDOUBLE_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( dst , EOBJECT_TYPE ) ; return ; } default : throw new Error ( "<STR_LIT>" + test ) ; } } @ Override public void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , int intg ) { push ( in , EBINMATCHSTATE_TYPE ) ; push_int ( intg ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBINMATCHSTATE_TYPE . getInternalName ( ) , test . name ( ) , "<STR_LIT>" ) ; mv . visitJumpInsn ( IFEQ , getLabel ( failLabel ) ) ; return ; } @ Override public void visitInsn ( BeamOpcode opcode , int failLabel , Arg [ ] in , Arg ex ) { if ( opcode == BeamOpcode . raise ) { push ( in [ <NUM_LIT:0> ] , EOBJECT_TYPE ) ; push ( in [ <NUM_LIT:1> ] , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + EOBJECT_DESC + "<STR_LIT:)>" + EOBJECT_DESC ) ; mv . visitInsn ( ARETURN ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } public void visitDecrement ( Arg src , Arg out ) { push ( src , src . type ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , "<STR_LIT>" , "<STR_LIT>" + ENUMBER_TYPE . getDescriptor ( ) ) ; pop ( out , EOBJECT_TYPE ) ; } ; @ Override public void visitIncrement ( Arg src , Arg out ) { push ( src , src . type ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , "<STR_LIT>" , "<STR_LIT>" + ENUMBER_TYPE . getDescriptor ( ) ) ; pop ( out , EOBJECT_TYPE ) ; return ; } @ Override public void visitInsn ( BeamOpcode opcode , int failLabel , Arg [ ] in , Arg out , BuiltInFunction bif ) { ensure_exception_handler_in_place ( ) ; switch ( opcode ) { case bif0 : case bif1 : case bif2 : case gc_bif1 : case gc_bif2 : case fadd : case fsub : case fmul : case fdiv : Type [ ] parameterTypes = bif . getArgumentTypes ( ) ; push ( in , parameterTypes , bif . isVirtual ( ) ) ; mv . visitMethodInsn ( bif . isVirtual ( ) ? INVOKEVIRTUAL : INVOKESTATIC , bif . owner . getInternalName ( ) , bif . getName ( ) , bif . getDescriptor ( ) ) ; if ( failLabel != <NUM_LIT:0> ) { if ( out != null ) { pop ( out , bif . getReturnType ( ) ) ; push ( out , bif . getReturnType ( ) ) ; } if ( bif . getReturnType ( ) . getSort ( ) == Type . BOOLEAN ) { mv . visitJumpInsn ( IFEQ , getLabel ( failLabel ) ) ; } else { if ( bif . getReturnType ( ) . getSort ( ) != Type . OBJECT ) throw new Error ( "<STR_LIT>" + bif ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; } } else { if ( PARANOIA_MODE && bif . getReturnType ( ) . getSort ( ) == Type . OBJECT ) { mv . visitInsn ( DUP ) ; mv . visitLdcInsn ( bif . toString ( ) ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; } pop ( out , bif . getReturnType ( ) ) ; } return ; } throw new Error ( ) ; } public void visitUnreachablePoint ( ) { mv . visitLdcInsn ( "<STR_LIT>" ) ; mv . visitInsn ( DUP ) ; mv . visitMethodInsn ( INVOKESPECIAL , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( ATHROW ) ; } public void visitCatchBlockStart ( BeamOpcode opcode , int label , Arg out , BeamExceptionHandler exh ) { switch ( opcode ) { case K_try : case K_catch : { active_beam_exh = exh ; return ; } } } public void visitCatchBlockEnd ( BeamOpcode opcode , Arg out , BeamExceptionHandler exh ) { active_beam_exh = exh . getParent ( ) ; adjust_exception_handlers ( active_beam_exh , false ) ; switch ( opcode ) { case try_end : { } break ; case catch_end : { Label after = new Label ( ) ; mv . visitJumpInsn ( GOTO , after ) ; mv . visitLabel ( getExceptionHandlerLabel ( exh ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitInsn ( SWAP ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" , EEXCEPTION_DESC ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + ERLANG_EXCEPTION_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; mv . visitLabel ( after ) ; } break ; case try_case : { mv . visitLabel ( getExceptionHandlerLabel ( exh ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitInsn ( SWAP ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" , EEXCEPTION_DESC ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + ERLANG_EXCEPTION_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + getTubleType ( <NUM_LIT:3> ) . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitFieldInsn ( GETFIELD , ETUPLE_NAME + <NUM_LIT:3> , "<STR_LIT>" , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; mv . visitInsn ( DUP ) ; mv . visitFieldInsn ( GETFIELD , ETUPLE_NAME + <NUM_LIT:3> , "<STR_LIT>" , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:1> ] ) ; mv . visitFieldInsn ( GETFIELD , ETUPLE_NAME + <NUM_LIT:3> , "<STR_LIT>" , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:2> ] ) ; } break ; } } private int var_index ( Arg out ) { switch ( out . kind ) { case X : return xregs [ out . no ] ; case Y : return yregs [ out . no ] ; case F : return fpregs [ out . no ] ; } throw new Error ( ) ; } private void pop ( Arg out , Type stack_type ) { if ( out == null ) { if ( stack_type != null && Type . DOUBLE_TYPE . equals ( stack_type ) ) { mv . visitInsn ( POP2 ) ; } else { mv . visitInsn ( POP ) ; } return ; } if ( stack_type == Type . DOUBLE_TYPE && ( out . kind == Kind . X || out . kind == Kind . Y ) ) { emit_convert ( stack_type , stack_type == Type . DOUBLE_TYPE ? EDOUBLE_TYPE : ( stack_type == Type . INT_TYPE ? EINTEGER_TYPE : EOBJECT_TYPE ) ) ; } if ( out . kind == Kind . X || out . kind == Kind . Y ) { if ( out . type == Type . INT_TYPE || out . type == Type . BOOLEAN_TYPE || ( out . type == null && stack_type == Type . INT_TYPE ) || ( out . type == null && stack_type == Type . BOOLEAN_TYPE ) ) { mv . visitVarInsn ( ISTORE , var_index ( out ) ) ; } else { mv . visitVarInsn ( ASTORE , var_index ( out ) ) ; } } else if ( out . kind == Kind . F ) { if ( ! stack_type . equals ( Type . DOUBLE_TYPE ) ) { emit_convert ( stack_type , Type . DOUBLE_TYPE ) ; } mv . visitVarInsn ( DSTORE , var_index ( out ) ) ; } else { throw new Error ( ) ; } } private void emit_convert ( Type from_type , Type to_type ) { if ( from_type . equals ( to_type ) ) return ; if ( from_type . getSort ( ) == Type . OBJECT && to_type . getSort ( ) == Type . OBJECT ) { return ; } if ( to_type . getSort ( ) == Type . OBJECT ) { emit_box ( from_type , to_type ) ; } else { emit_unbox ( from_type , to_type ) ; } } private void emit_unbox ( Type fromType , Type toType ) { if ( toType . equals ( Type . INT_TYPE ) ) { if ( fromType . equals ( ESMALL_TYPE ) ) { mv . visitFieldInsn ( GETFIELD , ESMALL_NAME , "<STR_LIT:value>" , "<STR_LIT:I>" ) ; return ; } } if ( toType . equals ( Type . DOUBLE_TYPE ) ) { if ( fromType . equals ( EDOUBLE_TYPE ) ) { mv . visitFieldInsn ( GETFIELD , EDOUBLE_NAME , "<STR_LIT:value>" , "<STR_LIT:D>" ) ; return ; } } mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" + primTypeName ( toType ) , "<STR_LIT:(>" + fromType . getDescriptor ( ) + "<STR_LIT:)>" + toType . getDescriptor ( ) ) ; } private String primTypeName ( Type typ ) { switch ( typ . getSort ( ) ) { case Type . DOUBLE : return "<STR_LIT>" ; case Type . INT : return "<STR_LIT>" ; case Type . BOOLEAN : return "<STR_LIT>" ; default : throw new Error ( ) ; } } private void emit_box ( Type fromType , Type toType ) { if ( fromType . equals ( Type . INT_TYPE ) && toType . getSort ( ) == Type . OBJECT ) { mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT>" + ESMALL_TYPE . getDescriptor ( ) ) ; } else if ( fromType . equals ( Type . DOUBLE_TYPE ) && toType . getSort ( ) == Type . OBJECT ) { mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT>" + EDOUBLE_TYPE . getDescriptor ( ) ) ; } else if ( fromType . equals ( Type . BOOLEAN_TYPE ) && toType . getSort ( ) == Type . OBJECT ) { mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT>" + EATOM_TYPE . getDescriptor ( ) ) ; } else { throw new Error ( "<STR_LIT>" + fromType + "<STR_LIT>" + toType ) ; } } private void push ( Arg [ ] in , Type [ ] parameterTypes , boolean isVirtual ) { int off = <NUM_LIT:0> ; if ( isVirtual ) { push ( in [ <NUM_LIT:0> ] , EOBJECT_TYPE ) ; off = <NUM_LIT:1> ; } if ( in . length == parameterTypes . length - <NUM_LIT:1> && EPROC_TYPE . equals ( parameterTypes [ <NUM_LIT:0> ] ) ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; } for ( int i = <NUM_LIT:0> ; i < in . length - off ; i ++ ) { Arg arg = in [ i + off ] ; Type pt = parameterTypes [ i ] ; push ( arg , pt ) ; } } private void push_scaled ( Arg value , int factor ) { if ( value . kind == Kind . IMMEDIATE && value . value instanceof ESmall ) { ESmall sm = ( ESmall ) value . value ; mv . visitLdcInsn ( new Integer ( factor * sm . intValue ( ) ) ) ; } else { push ( value , Type . INT_TYPE ) ; push_int ( factor ) ; mv . visitInsn ( IMUL ) ; } } private void push ( Arg value , Type stack_type ) { Type t = value . type ; if ( value . kind == Kind . X || value . kind == Kind . Y ) { if ( value . type == Type . INT_TYPE || value . type == Type . BOOLEAN_TYPE ) { mv . visitVarInsn ( ILOAD , var_index ( value ) ) ; } else { mv . visitVarInsn ( ALOAD , var_index ( value ) ) ; } } else if ( value . kind == Kind . F ) { mv . visitVarInsn ( DLOAD , var_index ( value ) ) ; } else if ( value . kind == Kind . IMMEDIATE ) { t = push_immediate ( value . value , stack_type ) ; } else { throw new Error ( ) ; } if ( t != null && ! t . equals ( stack_type ) ) { emit_convert ( t , stack_type ) ; } } private Type push_immediate ( EObject value , Type stack_type ) { if ( value == ERT . NIL ) { mv . visitFieldInsn ( GETSTATIC , ERT_NAME , "<STR_LIT>" , ENIL_TYPE . getDescriptor ( ) ) ; return ENIL_TYPE ; } if ( value == ERT . TRUE ) { mv . visitFieldInsn ( GETSTATIC , ERT_NAME , "<STR_LIT>" , EATOM_DESC ) ; return EATOM_TYPE ; } if ( value == ERT . FALSE ) { mv . visitFieldInsn ( GETSTATIC , ERT_NAME , "<STR_LIT>" , EATOM_DESC ) ; return EATOM_TYPE ; } if ( stack_type . getSort ( ) != Type . OBJECT ) { if ( value instanceof ESmall ) { mv . visitLdcInsn ( new Integer ( value . asInt ( ) ) ) ; return Type . INT_TYPE ; } else if ( value instanceof EBig && stack_type . getSort ( ) == Type . DOUBLE ) { push_immediate ( value , EBIG_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EBIG_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; return Type . DOUBLE_TYPE ; } else if ( value instanceof EDouble ) { mv . visitLdcInsn ( new Double ( ( ( EDouble ) value ) . value ) ) ; return Type . DOUBLE_TYPE ; } else if ( value == TRUE_ATOM ) { mv . visitInsn ( ICONST_1 ) ; return Type . BOOLEAN_TYPE ; } else if ( value == FALSE_ATOM ) { mv . visitInsn ( ICONST_0 ) ; return Type . BOOLEAN_TYPE ; } if ( value instanceof ETuple2 ) { ETuple2 t2 = ( ETuple2 ) value ; if ( t2 . elm ( <NUM_LIT:1> ) == ATOM_field_flags ) { push_int ( t2 . elem2 . asInt ( ) ) ; return Type . INT_TYPE ; } } throw new Error ( "<STR_LIT>" + value + "<STR_LIT>" + stack_type ) ; } String known = constants . get ( value ) ; if ( known == null ) { Type type = Type . getType ( value . getClass ( ) ) ; String cn = getConstantName ( value , constants . size ( ) ) ; constants . put ( value , known = cn ) ; cv . visitField ( ACC_STATIC , known , type . getDescriptor ( ) , null , null ) ; } if ( known != null ) { Type type = Type . getType ( value . getClass ( ) ) ; mv . visitFieldInsn ( GETSTATIC , self_type . getInternalName ( ) , known , type . getDescriptor ( ) ) ; return type ; } throw new Error ( "<STR_LIT>" + value + "<STR_LIT>" + stack_type ) ; } private String getConstantName ( EObject value , int size ) { if ( value instanceof EAtom ) return "<STR_LIT>" + EUtil . toJavaIdentifier ( ( EAtom ) value ) ; if ( value instanceof ENumber ) return EUtil . toJavaIdentifier ( "<STR_LIT>" + value . toString ( ) . replace ( '<CHAR_LIT:.>' , '<CHAR_LIT:_>' ) . replace ( '<CHAR_LIT:->' , '<CHAR_LIT:_>' ) ) ; if ( value instanceof EString ) return "<STR_LIT>" + size ; else return "<STR_LIT>" + size ; } @ Override public void visitReceive ( BeamOpcode opcode , int blockLabel , Arg out ) { switch ( opcode ) { case loop_rec : ensure_exception_handler_in_place ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT:)>" + EOBJECT_DESC ) ; mv . visitInsn ( DUP ) ; pop ( out , EOBJECT_TYPE ) ; mv . visitJumpInsn ( IFNULL , getLabel ( blockLabel ) ) ; return ; } throw new Error ( ) ; } @ Override public void visitInsn ( BeamOpcode opcode , Arg [ ] in , Arg out ) { switch ( opcode ) { case put_list : push ( in [ <NUM_LIT:0> ] , EOBJECT_TYPE ) ; push ( in [ <NUM_LIT:1> ] , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , ERT_CONS_SIG ) ; pop ( out , ECONS_TYPE ) ; return ; case call_fun : case i_call_fun_last : { ensure_exception_handler_in_place ( ) ; boolean is_tail = opcode == BeamOpcode . i_call_fun_last ; int nargs = in . length - <NUM_LIT:1> ; push ( in [ nargs ] , EOBJECT_TYPE ) ; mv . visitInsn ( DUP ) ; String funtype = EFUN_NAME + nargs ; mv . visitMethodInsn ( INVOKESTATIC , funtype , "<STR_LIT:cast>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + funtype + "<STR_LIT:;>" ) ; mv . visitInsn ( DUP_X1 ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , TEST_FUN_SIG ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; for ( int i = <NUM_LIT:0> ; i < nargs ; i ++ ) { push ( in [ i ] , EOBJECT_TYPE ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , funtype , is_tail ? "<STR_LIT>" : "<STR_LIT>" , EUtil . getSignature ( nargs , true ) ) ; if ( is_tail ) { mv . visitInsn ( ARETURN ) ; } else { pop ( out , EOBJECT_TYPE ) ; } return ; } } throw new Error ( ) ; } @ Override public void visitInsn ( BeamOpcode insn ) { switch ( insn ) { case if_end : mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , EUtil . getSignature ( <NUM_LIT:0> , false ) ) ; mv . visitInsn ( ARETURN ) ; return ; case timeout : mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_DESC + "<STR_LIT>" ) ; return ; case remove_message : mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; return ; } throw new Error ( ) ; } @ Override public void visitInsn ( BeamOpcode opcode , int val , Arg out ) { switch ( opcode ) { case put_tuple : mv . visitTypeInsn ( NEW , out . type . getInternalName ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitMethodInsn ( INVOKESPECIAL , out . type . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; pop ( out , out . type ) ; return ; case wait_timeout : { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; push ( out , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + EOBJECT_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; mv . visitJumpInsn ( IFNE , getLabel ( val ) ) ; return ; } case loop_rec_end : mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; mv . visitJumpInsn ( GOTO , getLabel ( val ) ) ; return ; case wait : { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EPROC_TYPE . getDescriptor ( ) + "<STR_LIT>" ) ; mv . visitJumpInsn ( GOTO , getLabel ( val ) ) ; return ; } } throw new Error ( "<STR_LIT>" + opcode ) ; } @ Override public void visitInsn ( BeamOpcode opcode , Arg val , Arg out , int pos ) { if ( opcode == BeamOpcode . put ) { push ( out , out . type ) ; push ( val , EOBJECT_TYPE ) ; mv . visitFieldInsn ( PUTFIELD , out . type . getInternalName ( ) , "<STR_LIT>" + pos , EOBJECT_DESC ) ; return ; } else if ( opcode == BeamOpcode . get_tuple_element ) { int known_arity = get_known_arity ( val . type ) ; if ( known_arity >= pos + <NUM_LIT:1> ) { push ( val , val . type ) ; mv . visitFieldInsn ( GETFIELD , val . type . getInternalName ( ) , "<STR_LIT>" + ( pos + <NUM_LIT:1> ) , EOBJECT_DESC ) ; } else { push ( val , val . type ) ; mv . visitTypeInsn ( CHECKCAST , ETUPLE_NAME ) ; push_int ( pos + <NUM_LIT:1> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" + EOBJECT_DESC ) ; } pop ( out , EOBJECT_TYPE ) ; return ; } else if ( opcode == BeamOpcode . set_tuple_element ) { push ( out , out . type ) ; if ( get_known_arity ( out . type ) < <NUM_LIT:0> ) mv . visitTypeInsn ( CHECKCAST , ETUPLE_NAME ) ; push_int ( pos + <NUM_LIT:1> ) ; push ( val , val . type ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" + EOBJECT_DESC + "<STR_LIT>" ) ; return ; } throw new Error ( ) ; } private int get_known_arity ( Type type ) { String in = type . getInternalName ( ) ; if ( in . startsWith ( ETUPLE_NAME ) ) { int pfx_len = ETUPLE_NAME . length ( ) ; if ( in . length ( ) == pfx_len ) { return <NUM_LIT:0> ; } try { String arity = in . substring ( pfx_len ) ; return Integer . parseInt ( arity ) ; } catch ( NumberFormatException e ) { return <NUM_LIT:0> ; } } return - <NUM_LIT:1> ; } private void push_int ( int pos ) { if ( pos >= - <NUM_LIT:1> && pos <= <NUM_LIT:5> ) { mv . visitInsn ( ICONST_0 + pos ) ; } else { mv . visitLdcInsn ( new Integer ( pos ) ) ; } } @ Override public void visitInsn ( BeamOpcode test , int failLabel , Arg arg1 , org . objectweb . asm . commons . Method bif ) { throw new Error ( ) ; } @ Override public void visitEnd ( ) { } @ Override public void visitInsn ( Insn insn ) { throw new Error ( ) ; } @ Override public void visitInsn ( BeamOpcode opcode , Arg arg ) { switch ( opcode ) { case K_return : push ( arg , EOBJECT_TYPE ) ; mv . visitInsn ( ARETURN ) ; return ; case init : push_immediate ( ERT . NIL , ENIL_TYPE ) ; pop ( arg , ENIL_TYPE ) ; return ; case try_case_end : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , EUtil . getSignature ( <NUM_LIT:1> , false ) ) ; mv . visitInsn ( ARETURN ) ; return ; case case_end : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , EUtil . getSignature ( <NUM_LIT:1> , false ) ) ; mv . visitInsn ( ARETURN ) ; return ; case badmatch : push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , EUtil . getSignature ( <NUM_LIT:1> , false ) ) ; mv . visitInsn ( ARETURN ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } public void visitInsn ( BeamOpcode opcode , ExtFun f ) { switch ( opcode ) { case func_info : push_immediate ( f . mod , EATOM_TYPE ) ; push_immediate ( f . fun , EATOM_TYPE ) ; push_immediate ( ERT . NIL , ENIL_TYPE ) ; for ( int i = f . arity - <NUM_LIT:1> ; i >= <NUM_LIT:0> ; i -- ) { push ( new Arg ( Kind . X , i ) , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ESEQ_NAME , "<STR_LIT>" , SEQ_CONS_SIG ) ; } mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , FUNC_INFO_SIG ) ; mv . visitInsn ( ARETURN ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } @ Override public void visitTest ( BeamOpcode test , int failLabel , Arg arg1 , Type out ) { Method test_bif = get_test_bif ( test , arg1 . type ) ; push ( arg1 , arg1 . type ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , test_bif . getName ( ) , test_bif . getDescriptor ( ) ) ; Type returnType = test_bif . getReturnType ( ) ; if ( failLabel != <NUM_LIT:0> ) { if ( returnType . getSort ( ) != Type . OBJECT ) throw new Error ( "<STR_LIT>" + test_bif ) ; if ( arg1 != null ) { mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; } mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; if ( arg1 != null ) { mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( arg1 , returnType ) ; } } else { pop ( arg1 , returnType ) ; } } @ Override public void visitTest ( BeamOpcode test , int failLabel , Arg arg , int arity , Type tupleType ) { switch ( test ) { case test_arity : { Type tt = getTubleType ( arity ) ; if ( tt . equals ( arg . type ) ) { } else { push ( arg , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKESTATIC , tt . getInternalName ( ) , "<STR_LIT:cast>" , "<STR_LIT:(>" + arg . type . getDescriptor ( ) + "<STR_LIT:)>" + tt . getDescriptor ( ) ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( arg , getTubleType ( arity ) ) ; } return ; } } throw new Error ( "<STR_LIT>" + test ) ; } @ Override public void visitTest ( BeamOpcode test , int failLabel , Arg arg , Arg arity , Type funType ) { switch ( test ) { case is_function2 : push ( arg , EOBJECT_TYPE ) ; push ( arity , Type . INT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , "<STR_LIT>" , "<STR_LIT>" + EFUN_DESCRIPTOR ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ASTORE , scratch_reg ) ; mv . visitJumpInsn ( IFNULL , getLabel ( failLabel ) ) ; mv . visitVarInsn ( ALOAD , scratch_reg ) ; pop ( arg , funType ) ; return ; } throw new Error ( "<STR_LIT>" + test ) ; } @ Override public void visitTest ( BeamOpcode test , int failLabel , Arg [ ] args , Type outType ) { switch ( test ) { case is_eq_exact : { if ( args [ <NUM_LIT:0> ] . kind == Kind . IMMEDIATE && args [ <NUM_LIT:0> ] . value . equalsExactly ( ESmall . ZERO ) ) { push ( args [ <NUM_LIT:1> ] , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitJumpInsn ( IFEQ , getLabel ( failLabel ) ) ; return ; } if ( args [ <NUM_LIT:1> ] . kind == Kind . IMMEDIATE && args [ <NUM_LIT:1> ] . value . equalsExactly ( ESmall . ZERO ) ) { push ( args [ <NUM_LIT:0> ] , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitJumpInsn ( IFEQ , getLabel ( failLabel ) ) ; return ; } } case is_ne_exact : case is_ne : case is_eq : { if ( args [ <NUM_LIT:0> ] . type . equals ( EOBJECT_TYPE ) && ! args [ <NUM_LIT:1> ] . type . equals ( EOBJECT_TYPE ) ) { Arg t = args [ <NUM_LIT:0> ] ; args [ <NUM_LIT:0> ] = args [ <NUM_LIT:1> ] ; args [ <NUM_LIT:1> ] = t ; } } case is_lt : case is_ge : { if ( ( test == BeamOpcode . is_eq_exact || test == BeamOpcode . is_eq ) && ( args [ <NUM_LIT:0> ] . type . equals ( EATOM_TYPE ) || args [ <NUM_LIT:1> ] . type . equals ( EATOM_TYPE ) ) ) { push ( args [ <NUM_LIT:0> ] , EOBJECT_TYPE ) ; push ( args [ <NUM_LIT:1> ] , EOBJECT_TYPE ) ; mv . visitJumpInsn ( IF_ACMPNE , getLabel ( failLabel ) ) ; return ; } for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { push ( args [ i ] , EOBJECT_TYPE ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , EOBJECT_NAME , test . name ( ) , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" ) ; if ( failLabel != <NUM_LIT:0> ) { mv . visitJumpInsn ( IFEQ , getLabel ( failLabel ) ) ; } else { throw new Error ( "<STR_LIT>" ) ; } if ( test == BeamOpcode . is_eq_exact && ! Type . VOID_TYPE . equals ( outType ) ) { if ( args [ <NUM_LIT:0> ] . type . equals ( EOBJECT_TYPE ) && args [ <NUM_LIT:0> ] . kind . isReg ( ) ) { push ( args [ <NUM_LIT:1> ] , outType ) ; args [ <NUM_LIT:0> ] . type = outType ; pop ( args [ <NUM_LIT:0> ] , outType ) ; } else if ( args [ <NUM_LIT:1> ] . type . equals ( EOBJECT_TYPE ) && args [ <NUM_LIT:1> ] . kind . isReg ( ) ) { push ( args [ <NUM_LIT:0> ] , outType ) ; args [ <NUM_LIT:1> ] . type = outType ; pop ( args [ <NUM_LIT:1> ] , outType ) ; } } return ; } } throw new Error ( "<STR_LIT>" + test ) ; } private String test2name ( BeamOpcode test ) { return test . name ( ) ; } @ Override public void visitInsn ( BeamOpcode opcode , Arg arg1 , Arg arg2 ) { switch ( opcode ) { case fconv : case move : case fmove : if ( arg1 . kind == Kind . F ) { push ( arg1 , Type . DOUBLE_TYPE ) ; if ( arg2 . kind == Kind . F ) { pop ( arg2 , Type . DOUBLE_TYPE ) ; } else { emit_convert ( Type . DOUBLE_TYPE , EDOUBLE_TYPE ) ; pop ( arg2 , EDOUBLE_TYPE ) ; } } else { if ( arg2 . kind == Kind . F ) { push ( arg1 , Type . DOUBLE_TYPE ) ; pop ( arg2 , Type . DOUBLE_TYPE ) ; } else { push ( arg1 , arg1 . type ) ; pop ( arg2 , arg1 . type ) ; } } break ; default : throw new Error ( "<STR_LIT>" + opcode ) ; } } private Method get_test_bif ( BeamOpcode test , Type type ) { if ( ! type . getInternalName ( ) . startsWith ( "<STR_LIT>" ) ) { throw new Error ( "<STR_LIT>" ) ; } switch ( test ) { case is_nonempty_list : return IS_NONEMPTY_LIST_TEST ; case is_nil : return IS_NIL_TEST ; case is_boolean : return IS_BOOLEAN_TEST ; case is_number : return IS_NUMBER_TEST ; case is_float : return IS_FLOAT_TEST ; case is_atom : return IS_ATOM_TEST ; case is_list : return IS_LIST_TEST ; case is_tuple : return IS_TUPLE_TEST ; case is_integer : return IS_INTEGER_TEST ; case is_binary : return IS_BINARY_TEST ; case is_bitstr : return IS_BITSTRING_TEST ; case is_pid : return IS_PID_TEST ; case is_port : return IS_PORT_TEST ; case is_reference : return IS_REFERENCE_TEST ; case is_function : return IS_FUNCTION_TEST ; case is_function2 : return IS_FUNCTION2_TEST ; } throw new Error ( "<STR_LIT>" + test ) ; } @ Override public void visitInsn ( BeamOpcode opcode , Arg [ ] ys ) { if ( opcode == BeamOpcode . allocate_zero || opcode == BeamOpcode . allocate_heap_zero ) { mv . visitFieldInsn ( GETSTATIC , ERT_NAME , "<STR_LIT>" , ENIL_TYPE . getDescriptor ( ) ) ; for ( int i = <NUM_LIT:0> ; i < ys . length ; i ++ ) { if ( i != ( ys . length - <NUM_LIT:1> ) ) mv . visitInsn ( DUP ) ; mv . visitInsn ( NOP ) ; pop ( ys [ i ] , ENIL_TYPE ) ; } return ; } else if ( opcode == BeamOpcode . get_list ) { push ( ys [ <NUM_LIT:0> ] , ECONS_TYPE ) ; mv . visitInsn ( DUP ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ECONS_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" + EOBJECT_DESC ) ; pop ( ys [ <NUM_LIT:1> ] , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ECONS_TYPE . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" + EOBJECT_DESC ) ; pop ( ys [ <NUM_LIT:2> ] , EOBJECT_TYPE ) ; return ; } ensure_exception_handler_in_place ( ) ; if ( opcode == BeamOpcode . apply || opcode == BeamOpcode . apply_last ) { int arity = ys . length - <NUM_LIT:2> ; push ( ys [ ys . length - <NUM_LIT:2> ] , EOBJECT_TYPE ) ; push ( ys [ ys . length - <NUM_LIT:1> ] , EOBJECT_TYPE ) ; push_int ( arity ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + EOBJECT_DESC + "<STR_LIT>" + EFUN_DESCRIPTOR ) ; String funtype = EFUN_NAME + arity ; mv . visitMethodInsn ( INVOKESTATIC , funtype , "<STR_LIT:cast>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT>" + funtype + "<STR_LIT:;>" ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; int loops = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i <= ys . length - <NUM_LIT:3> ; i ++ ) { push ( ys [ i ] , EOBJECT_TYPE ) ; loops += <NUM_LIT:1> ; } if ( loops != arity ) { throw new InternalError ( "<STR_LIT>" ) ; } boolean is_tail = opcode == BeamOpcode . apply_last ; if ( is_tail ) { mv . visitMethodInsn ( INVOKEVIRTUAL , funtype , "<STR_LIT>" , EUtil . getSignature ( arity , true ) ) ; mv . visitInsn ( ARETURN ) ; } else { mv . visitMethodInsn ( INVOKEVIRTUAL , funtype , "<STR_LIT>" , EUtil . getSignature ( arity , true ) ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; } return ; } else if ( opcode == BeamOpcode . send ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; for ( int i = <NUM_LIT:0> ; i < ys . length ; i ++ ) { push ( ys [ i ] , EOBJECT_TYPE ) ; } mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , EUtil . getSignature ( ys . length , true ) ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; return ; } throw new Error ( "<STR_LIT>" + opcode ) ; } @ Override public void visitInsn ( BeamOpcode opcode , ExtFun efun , Arg [ ] freevars , int index , int old_index , EBinary uniq , int old_uniq ) { ensure_exception_handler_in_place ( ) ; if ( opcode == BeamOpcode . make_fun2 ) { CompilerVisitor . this . register_lambda ( efun . fun , efun . arity , freevars . length , index , old_index , uniq , old_uniq ) ; String inner = EUtil . getFunClassName ( self_type , efun , freevars . length ) ; mv . visitTypeInsn ( NEW , inner ) ; mv . visitInsn ( DUP ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EPROC_NAME , "<STR_LIT>" , "<STR_LIT>" + Type . getDescriptor ( EInternalPID . class ) ) ; for ( int i = <NUM_LIT:0> ; i < freevars . length ; i ++ ) { push ( freevars [ i ] , EOBJECT_TYPE ) ; } StringBuilder sb = new StringBuilder ( "<STR_LIT:(>" ) ; sb . append ( EPID_TYPE . getDescriptor ( ) ) ; for ( int i = <NUM_LIT:0> ; i < freevars . length ; i ++ ) { sb . append ( EOBJECT_DESC ) ; } sb . append ( "<STR_LIT>" ) ; String gen_fun_desc = sb . toString ( ) ; mv . visitMethodInsn ( INVOKESPECIAL , inner , "<STR_LIT>" , gen_fun_desc ) ; mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; return ; } throw new Error ( ) ; } @ Override public void visitSelectValue ( Arg in , int failLabel , Arg [ ] values , int [ ] targets ) { boolean all_small_ints = true ; for ( int i = <NUM_LIT:0> ; i < values . length ; i ++ ) { if ( ! ( values [ i ] . value instanceof ESmall ) ) { all_small_ints = false ; break ; } } if ( all_small_ints ) { int [ ] ivals = new int [ values . length ] ; Label [ ] label = new Label [ values . length ] ; for ( int i = <NUM_LIT:0> ; i < values . length ; i ++ ) { ivals [ i ] = values [ i ] . value . asInt ( ) ; label [ i ] = getLabel ( targets [ i ] ) ; } push ( in , Type . INT_TYPE ) ; sort ( ivals , label ) ; mv . visitLookupSwitchInsn ( getLabel ( failLabel ) , ivals , label ) ; return ; } if ( values . length < ATOM_SELECT_IF_ELSE_LIMIT ) { boolean all_atoms = true ; for ( int i = <NUM_LIT:0> ; i < values . length ; i ++ ) { if ( ! ( values [ i ] . value instanceof EAtom ) ) { all_atoms = false ; break ; } } if ( all_atoms ) { for ( int i = <NUM_LIT:0> ; i < values . length ; i ++ ) { push ( in , in . type ) ; push ( values [ i ] , values [ i ] . type ) ; mv . visitJumpInsn ( IF_ACMPEQ , getLabel ( targets [ i ] ) ) ; } mv . visitJumpInsn ( GOTO , getLabel ( failLabel ) ) ; return ; } } class Case implements Comparable < Case > { final Arg arg ; final Label label ; public Case ( Arg arg , Label label ) { this . arg = arg ; this . label = label ; } EObject value ( ) { return arg . value ; } @ Override public int compareTo ( Case o ) { int h = hashCode ( ) ; int ho = o . hashCode ( ) ; if ( h < ho ) return - <NUM_LIT:1> ; if ( h > ho ) return <NUM_LIT:1> ; return value ( ) . erlangCompareTo ( o . value ( ) ) ; } } Map < Integer , List < Case > > cases = new TreeMap < Integer , List < Case > > ( ) ; for ( int i = <NUM_LIT:0> ; i < values . length ; i ++ ) { int hash = values [ i ] . value . hashCode ( ) ; List < Case > c = cases . get ( hash ) ; if ( c == null ) { cases . put ( hash , c = new ArrayList < Case > ( ) ) ; } c . add ( new Case ( values [ i ] , getLabel ( targets [ i ] ) ) ) ; } int [ ] hashes = new int [ cases . size ( ) ] ; Label [ ] tests = new Label [ cases . size ( ) ] ; List < Case > [ ] idx_cases = new List [ cases . size ( ) ] ; int idx = <NUM_LIT:0> ; for ( Map . Entry < Integer , List < Case > > c : cases . entrySet ( ) ) { hashes [ idx ] = c . getKey ( ) ; idx_cases [ idx ] = c . getValue ( ) ; tests [ idx ++ ] = new Label ( ) ; } push ( in , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitLookupSwitchInsn ( getLabel ( failLabel ) , hashes , tests ) ; for ( int i = <NUM_LIT:0> ; i < idx_cases . length ; i ++ ) { mv . visitLabel ( tests [ i ] ) ; for ( Case c : idx_cases [ i ] ) { Arg val_j = c . arg ; Label target_j = c . label ; if ( val_j . type . equals ( EATOM_TYPE ) ) { push ( in , in . type ) ; push ( val_j , val_j . type ) ; mv . visitJumpInsn ( IF_ACMPEQ , target_j ) ; } else { if ( in . type == val_j . type ) { push ( in , in . type ) ; push ( val_j , val_j . type ) ; mv . visitMethodInsn ( INVOKESTATIC , ERT_NAME , "<STR_LIT>" , "<STR_LIT:(>" + in . type . getDescriptor ( ) + in . type . getDescriptor ( ) + "<STR_LIT>" ) ; } else { push ( in , EOBJECT_TYPE ) ; push ( val_j , EOBJECT_TYPE ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ; } mv . visitJumpInsn ( IFNE , target_j ) ; } } mv . visitJumpInsn ( GOTO , getLabel ( failLabel ) ) ; } } private int [ ] sort ( int [ ] ivals , Label [ ] label ) { Label [ ] orig_labels = label . clone ( ) ; int [ ] orig_ivals = ivals . clone ( ) ; int [ ] res = new int [ ivals . length ] ; Arrays . sort ( ivals ) ; next_val : for ( int i = <NUM_LIT:0> ; i < ivals . length ; i ++ ) { int find = ivals [ i ] ; for ( int p = <NUM_LIT:0> ; p < orig_ivals . length ; p ++ ) { int was = orig_ivals [ p ] ; if ( find == was ) { res [ p ] = i ; label [ i ] = orig_labels [ p ] ; continue next_val ; } } } return res ; } class TupleArityLabel implements Comparable < TupleArityLabel > { Label cast_label = new Label ( ) ; Label target ; int arity ; public TupleArityLabel ( int arity , Label target ) { this . arity = arity ; this . target = target ; } @ Override public int compareTo ( TupleArityLabel o ) { if ( this . arity < o . arity ) return - <NUM_LIT:1> ; if ( this . arity == o . arity ) return <NUM_LIT:0> ; return <NUM_LIT:1> ; } } @ Override public void visitSelectTuple ( Arg in , int failLabel , int [ ] arities , int [ ] targets ) { push ( in , ETUPLE_TYPE ) ; mv . visitTypeInsn ( CHECKCAST , ETUPLE_NAME ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; TupleArityLabel [ ] cases = new TupleArityLabel [ targets . length ] ; for ( int i = <NUM_LIT:0> ; i < targets . length ; i ++ ) { cases [ i ] = new TupleArityLabel ( arities [ i ] , getLabel ( targets [ i ] ) ) ; } Arrays . sort ( cases ) ; Label [ ] casts = new Label [ cases . length ] ; int [ ] values = new int [ cases . length ] ; for ( int i = <NUM_LIT:0> ; i < cases . length ; i ++ ) { values [ i ] = cases [ i ] . arity ; casts [ i ] = cases [ i ] . cast_label ; } mv . visitLookupSwitchInsn ( getLabel ( failLabel ) , values , casts ) ; for ( int i = <NUM_LIT:0> ; i < cases . length ; i ++ ) { mv . visitLabel ( cases [ i ] . cast_label ) ; push ( in , ETUPLE_TYPE ) ; mv . visitTypeInsn ( CHECKCAST , getTubleType ( cases [ i ] . arity ) . getInternalName ( ) ) ; pop ( in , getTubleType ( cases [ i ] . arity ) ) ; mv . visitJumpInsn ( GOTO , cases [ i ] . target ) ; } } @ Override public void visitJump ( int label ) { mv . visitJumpInsn ( GOTO , getLabel ( label ) ) ; } private Type getTubleType ( int i ) { return Type . getType ( "<STR_LIT>" + ETUPLE_NAME + i + "<STR_LIT:;>" ) ; } @ Override public void visitCall ( ExtFun fun , Arg [ ] args , boolean is_tail , boolean isExternal ) { ensure_exception_handler_in_place ( ) ; BuiltInFunction bif = null ; bif = BIFUtil . getMethod ( fun . mod . getName ( ) , fun . fun . getName ( ) , args , false , false ) ; if ( bif != null || isExternal ) { if ( bif == null ) { String field = CompilerVisitor . this . getExternalFunction ( fun ) ; String funTypeName = EFUN_NAME + args . length ; EFun . ensure ( args . length ) ; mv . visitFieldInsn ( GETSTATIC , self_type . getInternalName ( ) , field , "<STR_LIT>" + funTypeName + "<STR_LIT:;>" ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { push ( args [ i ] , EOBJECT_TYPE ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , funTypeName , ( is_tail && ! isExitFunc ( fun ) ) ? "<STR_LIT>" : "<STR_LIT>" , EUtil . getSignature ( args . length , true ) ) ; } else if ( bif . isVirtual ( ) ) { push ( args [ <NUM_LIT:0> ] , bif . owner ) ; int off = <NUM_LIT:0> ; if ( bif . getArgumentTypes ( ) . length > <NUM_LIT:0> && bif . getArgumentTypes ( ) [ <NUM_LIT:0> ] . equals ( EPROC_TYPE ) ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; off = <NUM_LIT:1> ; } for ( int i = <NUM_LIT:1> ; i < args . length ; i ++ ) { push ( args [ i ] , bif . getArgumentTypes ( ) [ off - <NUM_LIT:1> ] ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , bif . owner . getInternalName ( ) , bif . getName ( ) , bif . getDescriptor ( ) ) ; } else { int off = <NUM_LIT:0> ; if ( bif . getArgumentTypes ( ) . length > <NUM_LIT:0> && bif . getArgumentTypes ( ) [ <NUM_LIT:0> ] . equals ( EPROC_TYPE ) ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; off = <NUM_LIT:1> ; } for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { push ( args [ i ] , bif . getArgumentTypes ( ) [ off + i ] ) ; } mv . visitMethodInsn ( INVOKESTATIC , bif . owner . getInternalName ( ) , bif . getName ( ) , bif . getDescriptor ( ) ) ; } if ( is_tail || isExitFunc ( fun ) ) { mv . visitInsn ( ARETURN ) ; } else { mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; } } else { if ( is_tail && fun . arity == ASMFunctionAdapter . this . arity && fun . fun == ASMFunctionAdapter . this . fun_name ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EPROC_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitJumpInsn ( GOTO , getLabel ( ASMFunctionAdapter . this . startLabel ) ) ; return ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { push ( args [ i ] , EOBJECT_TYPE ) ; } FunInfo target = funInfos . get ( new FunID ( fun . mod , fun . name ( ) , fun . arity ) ) ; mv . visitMethodInsn ( INVOKESTATIC , self_type . getInternalName ( ) , EUtil . getJavaName ( fun . fun , fun . arity ) + ( is_tail ? "<STR_LIT>" : ( target . may_return_tail_marker ? "<STR_LIT>" : "<STR_LIT>" ) ) , EUtil . getSignature ( args . length , true ) ) ; if ( is_tail ) { mv . visitInsn ( ARETURN ) ; } else { mv . visitVarInsn ( ASTORE , xregs [ <NUM_LIT:0> ] ) ; } } } private boolean isExitFunc ( ExtFun fun ) { if ( fun . mod == ERLANG_ATOM ) { if ( fun . fun == CodeAtoms . EXIT_ATOM && fun . arity == <NUM_LIT:1> ) return true ; if ( fun . fun == CodeAtoms . ERROR_ATOM && ( fun . arity == <NUM_LIT:1> || fun . arity == <NUM_LIT:2> ) ) return true ; if ( fun . fun == CodeAtoms . THROW_ATOM && fun . arity == <NUM_LIT:1> ) return true ; } return false ; } } } final static Method IS_NONEMPTY_LIST_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_LIST_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_TUPLE_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_INTEGER_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_ATOM_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_FLOAT_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_NIL_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_BOOLEAN_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_NUMBER_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_BINARY_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_BITSTRING_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_PID_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_PORT_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_REFERENCE_TEST = Method . getMethod ( ERef . class . getName ( ) + "<STR_LIT>" ) ; final static Method IS_FUNCTION_TEST = Method . getMethod ( "<STR_LIT>" ) ; final static Method IS_FUNCTION2_TEST = Method . getMethod ( "<STR_LIT>" ) ; Map < String , ExtFun > imported = new HashMap < String , ExtFun > ( ) ; private Map < FunID , FunInfo > funInfos ; public String getExternalFunction ( ExtFun fun ) { String name = EUtil . getJavaName ( fun ) ; if ( ! imported . containsKey ( name ) ) { imported . put ( name , fun ) ; } return name ; } static class Lambda { private final EAtom fun ; private final int arity ; private final int freevars ; private final int index ; private final int old_index ; private final int old_uniq ; private final EBinary uniq ; public Lambda ( EAtom fun , int arity , int freevars , int index , int old_index , EBinary uniq , int old_uniq ) { this . fun = fun ; this . arity = arity ; this . freevars = freevars ; this . index = index ; this . old_index = old_index ; this . uniq = uniq ; this . old_uniq = old_uniq ; } } public void register_lambda ( EAtom fun , int arity_plus , int freevars , int index , int old_index , EBinary uniq , int old_uniq ) { lambdas_xx . put ( new FunID ( module_name , fun , arity_plus ) , new Lambda ( fun , arity_plus , freevars , index , old_index , uniq , old_uniq ) ) ; } public Lambda get_lambda_freevars ( EAtom fun , int arity_plus ) { return lambdas_xx . get ( new FunID ( module_name , fun , arity_plus ) ) ; } static public byte [ ] make_invoker ( String module , String function , Type self_type , String mname , String fname , int arity , boolean proc , boolean exported , Lambda lambda , Type return_type , boolean is_tail_call , final boolean is_pausable ) { int freevars = lambda == null ? <NUM_LIT:0> : lambda . freevars ; String outer_name = self_type . getInternalName ( ) ; String inner_name = "<STR_LIT>" + mname ; String full_inner_name = outer_name + "<STR_LIT:$>" + inner_name ; ClassWriter cw = new ClassWriter ( true ) ; int residual_arity = arity - freevars ; String super_class_name = EFUN_NAME + residual_arity + ( exported ? "<STR_LIT>" : "<STR_LIT>" ) ; if ( exported ) EFun . ensure_exported ( residual_arity ) ; else EFun . ensure ( residual_arity ) ; cw . visit ( V1_6 , ACC_FINAL | ACC_PUBLIC , full_inner_name , null , super_class_name , null ) ; if ( lambda != null ) { cw . visitField ( ACC_STATIC | ACC_PUBLIC | ACC_FINAL , "<STR_LIT:index>" , "<STR_LIT:I>" , null , new Integer ( lambda . index ) ) ; cw . visitField ( ACC_STATIC | ACC_PUBLIC | ACC_FINAL , "<STR_LIT>" , "<STR_LIT:I>" , null , new Integer ( lambda . old_index ) ) ; cw . visitField ( ACC_STATIC | ACC_PUBLIC | ACC_FINAL , "<STR_LIT>" , "<STR_LIT:I>" , null , new Integer ( lambda . old_uniq ) ) ; MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT:(>" + Type . getDescriptor ( EOutputStream . class ) + "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner_name , "<STR_LIT>" , EPID_TYPE . getDescriptor ( ) ) ; mv . visitLdcInsn ( module ) ; mv . visitFieldInsn ( GETSTATIC , full_inner_name , "<STR_LIT>" , "<STR_LIT:I>" ) ; mv . visitInsn ( I2L ) ; mv . visitLdcInsn ( new Integer ( arity ) ) ; mv . visitFieldInsn ( GETSTATIC , outer_name , "<STR_LIT>" , EBINARY_TYPE . getDescriptor ( ) ) ; mv . visitFieldInsn ( GETSTATIC , full_inner_name , "<STR_LIT:index>" , "<STR_LIT:I>" ) ; mv . visitInsn ( I2L ) ; mv . visitFieldInsn ( GETSTATIC , full_inner_name , "<STR_LIT>" , "<STR_LIT:I>" ) ; mv . visitInsn ( I2L ) ; mv . visitLdcInsn ( new Integer ( freevars ) ) ; mv . visitTypeInsn ( ANEWARRAY , EOBJECT_NAME ) ; for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { mv . visitInsn ( DUP ) ; if ( i <= <NUM_LIT:5> ) { mv . visitInsn ( ICONST_0 + i ) ; } else { mv . visitLdcInsn ( new Integer ( i ) ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner_name , "<STR_LIT>" + i , EOBJECT_DESC ) ; mv . visitInsn ( AASTORE ) ; } mv . visitMethodInsn ( INVOKEVIRTUAL , Type . getInternalName ( EOutputStream . class ) , "<STR_LIT>" , "<STR_LIT:(>" + EPID_TYPE . getDescriptor ( ) + "<STR_LIT>" + "<STR_LIT>" + EBINARY_TYPE . getDescriptor ( ) + "<STR_LIT>" + Type . getDescriptor ( EObject [ ] . class ) + "<STR_LIT>" ) ; mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:10> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; } make_constructor ( cw , module , function , full_inner_name , super_class_name , lambda , exported ) ; make_go_method ( cw , outer_name , fname , full_inner_name , arity , proc , freevars , return_type , is_tail_call , is_pausable ) ; make_go2_method ( cw , outer_name , fname , full_inner_name , arity , proc , freevars , return_type , is_tail_call , is_pausable ) ; return cw . toByteArray ( ) ; } private static void make_constructor ( ClassWriter cw , String module_name , String function_name , String full_inner_name , String super_class_name , Lambda lambda , boolean exported ) { StringBuilder sb = new StringBuilder ( "<STR_LIT:(>" ) ; int freevars = lambda == null ? <NUM_LIT:0> : lambda . freevars ; if ( lambda != null ) { sb . append ( EPID_TYPE . getDescriptor ( ) ) ; cw . visitField ( ACC_PUBLIC | ACC_FINAL , "<STR_LIT>" , EPID_TYPE . getDescriptor ( ) , null , null ) ; } for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { cw . visitField ( ACC_PUBLIC | ACC_FINAL , "<STR_LIT>" + i , EOBJECT_DESC , null , null ) ; sb . append ( EOBJECT_DESC ) ; } sb . append ( "<STR_LIT>" ) ; MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , sb . toString ( ) , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; if ( exported ) { mv . visitLdcInsn ( module_name ) ; mv . visitLdcInsn ( function_name ) ; mv . visitMethodInsn ( INVOKESPECIAL , super_class_name , "<STR_LIT>" , "<STR_LIT>" ) ; } else { mv . visitMethodInsn ( INVOKESPECIAL , super_class_name , "<STR_LIT>" , "<STR_LIT>" ) ; } if ( lambda != null ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( PUTFIELD , full_inner_name , "<STR_LIT>" , EPID_TYPE . getDescriptor ( ) ) ; } for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; mv . visitFieldInsn ( PUTFIELD , full_inner_name , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitInsn ( RETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; if ( lambda != null ) { mv = cw . visitMethod ( ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" + ESEQ_DESC , null , null ) ; mv . visitCode ( ) ; mv . visitFieldInsn ( GETSTATIC , ERT_NAME , "<STR_LIT>" , ENIL_TYPE . getDescriptor ( ) ) ; for ( int i = freevars - <NUM_LIT:1> ; i >= <NUM_LIT:0> ; i -- ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner_name , "<STR_LIT>" + i , EOBJECT_DESC ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , ESEQ_NAME , "<STR_LIT>" , "<STR_LIT:(>" + EOBJECT_DESC + "<STR_LIT:)>" + ESEQ_DESC ) ; } mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; mv = cw . visitMethod ( ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" + EOBJECT_DESC , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner_name , "<STR_LIT>" , EPID_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; mv = cw . visitMethod ( ACC_PROTECTED , "<STR_LIT>" , "<STR_LIT>" + Type . getDescriptor ( FunID . class ) , null , null ) ; mv . visitCode ( ) ; mv . visitFieldInsn ( GETSTATIC , full_inner_name . substring ( <NUM_LIT:0> , full_inner_name . indexOf ( '<CHAR_LIT>' ) ) , anon_fun_name ( lambda ) , Type . getDescriptor ( LocalFunID . class ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; } } private static void make_invoke_method ( ClassWriter cw , String outer_name , String mname , int arity , boolean proc , int freevars , Type returnType , boolean isTailCall ) { MethodVisitor mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , EUtil . getSignature ( arity - freevars , true ) , null , PAUSABLE_EX ) ; mv . visitCode ( ) ; if ( proc ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; } for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , outer_name + "<STR_LIT>" + mname , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitMethodInsn ( INVOKESTATIC , outer_name , mname , EUtil . getSignature ( arity , proc , returnType ) ) ; if ( isTailCall ) { mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:2> ) ; Label done = new Label ( ) ; Label loop = new Label ( ) ; mv . visitLabel ( loop ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:2> ) ; if ( EProc . TAIL_MARKER == null ) { mv . visitJumpInsn ( IFNONNULL , done ) ; } else { mv . visitFieldInsn ( GETSTATIC , EPROC_NAME , "<STR_LIT>" , EOBJECT_DESC ) ; mv . visitJumpInsn ( IF_ACMPNE , done ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_NAME , "<STR_LIT>" , EFUN_DESCRIPTOR ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitMethodInsn ( INVOKEVIRTUAL , EFUN_NAME , "<STR_LIT>" , GO_DESC ) ; mv . visitVarInsn ( ASTORE , arity + <NUM_LIT:2> ) ; mv . visitJumpInsn ( GOTO , loop ) ; mv . visitLabel ( done ) ; mv . visitVarInsn ( ALOAD , arity + <NUM_LIT:2> ) ; } mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } public static void make_invoketail_method ( ClassWriter cw , String full_inner , int arity , int freevars ) { MethodVisitor mv ; mv = cw . visitMethod ( ACC_PUBLIC | ACC_FINAL , "<STR_LIT>" , EUtil . getSignature ( arity - freevars , true ) , null , null ) ; mv . visitCode ( ) ; for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" , EFUN_DESCRIPTOR ) ; if ( EProc . TAIL_MARKER == null ) { mv . visitInsn ( ACONST_NULL ) ; } else { mv . visitFieldInsn ( GETSTATIC , EPROC_NAME , "<STR_LIT>" , EOBJECT_DESC ) ; } mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private static void make_go_method ( ClassWriter cw , String outer_name , String mname , String full_inner , int arity , boolean proc , int freevars , Type returnType , boolean isTailCall , boolean isPausable ) { if ( ! isPausable ) return ; MethodVisitor mv ; mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , GO_DESC , null , PAUSABLE_EX ) ; mv . visitCode ( ) ; for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitInsn ( ACONST_NULL ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; } if ( proc ) mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitMethodInsn ( INVOKESTATIC , outer_name , mname , EUtil . getSignature ( arity , proc , returnType ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; cw . visitEnd ( ) ; } private static void make_go2_method ( ClassWriter cw , String outer_name , String mname , String full_inner , int arity , boolean proc , int freevars , Type returnType , boolean isTailCall , boolean isPausable ) { if ( isPausable ) { if ( ModuleAnalyzer . log . isLoggable ( Level . FINE ) ) { ModuleAnalyzer . log . fine ( "<STR_LIT>" + full_inner ) ; } return ; } MethodVisitor mv ; mv = cw . visitMethod ( ACC_PUBLIC , "<STR_LIT>" , GO_DESC , null , null ) ; mv . visitCode ( ) ; for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitFieldInsn ( GETFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; mv . visitVarInsn ( ASTORE , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; mv . visitInsn ( ACONST_NULL ) ; mv . visitFieldInsn ( PUTFIELD , EPROC_NAME , "<STR_LIT>" + i , EOBJECT_DESC ) ; } if ( proc ) mv . visitVarInsn ( ALOAD , <NUM_LIT:1> ) ; for ( int i = <NUM_LIT:0> ; i < arity - freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , i + <NUM_LIT:2> ) ; } for ( int i = <NUM_LIT:0> ; i < freevars ; i ++ ) { mv . visitVarInsn ( ALOAD , <NUM_LIT:0> ) ; mv . visitFieldInsn ( GETFIELD , full_inner , "<STR_LIT>" + i , EOBJECT_DESC ) ; } mv . visitMethodInsn ( INVOKESTATIC , outer_name , mname , EUtil . getSignature ( arity , proc , returnType ) ) ; mv . visitInsn ( ARETURN ) ; mv . visitMaxs ( arity + <NUM_LIT:2> , arity + <NUM_LIT:2> ) ; mv . visitEnd ( ) ; cw . visitEnd ( ) ; } public void setFunInfos ( Map < FunID , FunInfo > funInfos ) { this . funInfos = funInfos ; } } class EXHandler { int handler_beam_label ; Label begin , end , target ; BeamExceptionHandler beam_exh ; } </s>
|
<s> package erjang . beam ; import java . lang . reflect . Method ; import java . lang . reflect . Modifier ; import java . util . ArrayList ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; import java . util . logging . Level ; import org . objectweb . asm . Type ; import erjang . EAtom ; import erjang . EBig ; import erjang . EDouble ; import erjang . ENative ; import erjang . EObject ; import erjang . EProc ; import erjang . ERT ; import erjang . ESmall ; import erjang . m . erlang . BinOps ; import erjang . m . erlang . ErlBif ; public class BIFUtil { public static final Type EOBJECT_TYPE = Type . getType ( EObject . class ) ; public static final Type EPROC_TYPE = Type . getType ( EProc . class ) ; static Map < String , BIFHandler > bifs = new HashMap < String , BIFHandler > ( ) ; static Map < String , BIFHandler > guard_bifs = new HashMap < String , BIFHandler > ( ) ; static { try { loadBIFs ( new String [ ] { "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" } ) ; } catch ( Exception e ) { throw new Error ( "<STR_LIT>" , e ) ; } } static class Args { Class [ ] args ; private Args generic ; private int hashCode ; private int code ; Args ( Type [ ] types ) { args = new Class [ types . length ] ; for ( int i = <NUM_LIT:0> ; i < types . length ; i ++ ) { if ( types [ i ] == Type . DOUBLE_TYPE ) { args [ i ] = double . class ; continue ; } if ( types [ i ] == Type . INT_TYPE ) { args [ i ] = int . class ; continue ; } try { args [ i ] = Class . forName ( types [ i ] . getClassName ( ) ) ; } catch ( ClassNotFoundException e ) { if ( types [ i ] == Type . BOOLEAN_TYPE ) { args [ i ] = boolean . class ; } else if ( types [ i ] == Type . INT_TYPE ) { args [ i ] = int . class ; } else if ( types [ i ] == Type . DOUBLE_TYPE ) { args [ i ] = double . class ; } else { throw new Error ( e ) ; } } } } public Args ( Class [ ] a ) { this . args = a ; } @ Override public int hashCode ( ) { if ( code != <NUM_LIT:0> ) return code ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { code += args [ i ] . getName ( ) . hashCode ( ) ; } for ( Class c : args ) { code += c . hashCode ( ) ; } return code ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof Args ) { Args other = ( Args ) obj ; if ( other . args . length == args . length ) { for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { if ( ! args [ i ] . equals ( other . args [ i ] ) ) { return false ; } } return true ; } } return false ; } @ Override public String toString ( ) { StringBuilder sb = new StringBuilder ( "<STR_LIT:(>" ) ; boolean first = true ; for ( Class c : args ) { if ( ! first ) sb . append ( "<STR_LIT:U+002C>" ) ; else first = false ; sb . append ( c . getName ( ) ) ; } sb . append ( "<STR_LIT:)>" ) ; return sb . toString ( ) ; } public Args generic ( ) { if ( generic == null ) { Class [ ] a = new Class [ args . length ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { a [ i ] = EObject . class ; } generic = new Args ( a ) ; } return generic ; } public List < Args > generalize ( ) { ArrayList < Args > res = new ArrayList < Args > ( ) ; Class [ ] aa = this . args . clone ( ) ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { for ( Class c = args [ i ] ; ! c . equals ( Object . class ) ; c = super_class ( c ) ) { aa [ i ] = c ; res . add ( new Args ( aa . clone ( ) ) ) ; for ( int j = i + <NUM_LIT:1> ; j < args . length ; j ++ ) { for ( Class cc = args [ j ] ; ! cc . equals ( Object . class ) ; cc = super_class ( cc ) ) { aa [ j ] = cc ; res . add ( new Args ( aa . clone ( ) ) ) ; } } } } return res ; } private Class super_class ( Class c ) { if ( c . isPrimitive ( ) ) { if ( c == double . class ) return EDouble . class ; if ( c == int . class ) return ESmall . class ; if ( c == long . class ) return EBig . class ; if ( c == boolean . class ) return EAtom . class ; return EObject . class ; } else { return c . getSuperclass ( ) ; } } } static class BIFHandler { Map < Args , BuiltInFunction > found = new HashMap < Args , BuiltInFunction > ( ) ; private final String name ; private final String javaName ; @ Override public int hashCode ( ) { return name . hashCode ( ) + javaName . hashCode ( ) + found . values ( ) . hashCode ( ) ; } public BIFHandler ( String name ) { this . name = name ; this . javaName = name ; } public BIFHandler ( String name , String javaName ) { this . name = name ; this . javaName = javaName ; } public Type getResult ( Type [ ] parmTypes ) { BuiltInFunction method = getMethod ( parmTypes ) ; if ( method == null ) { throw new Error ( "<STR_LIT>" + this . name + "<STR_LIT>" + new Args ( parmTypes ) ) ; } return method . getReturnType ( ) ; } public void registerMethod ( Method method ) { Args a ; Class < ? > [ ] pt = method . getParameterTypes ( ) ; if ( ! Modifier . isStatic ( method . getModifiers ( ) ) ) { Class [ ] all = new Class [ pt . length + <NUM_LIT:1> ] ; all [ <NUM_LIT:0> ] = method . getDeclaringClass ( ) ; System . arraycopy ( pt , <NUM_LIT:0> , all , <NUM_LIT:1> , pt . length ) ; a = new Args ( all ) ; } else { a = new Args ( pt ) ; } found . put ( a , new BuiltInFunction ( method ) ) ; } public BuiltInFunction getMethod ( Type [ ] parmTypes ) { BuiltInFunction m = find_bif ( parmTypes ) ; if ( m != null ) return m ; if ( parmTypes . length == <NUM_LIT:0> || ! EPROC_TYPE . equals ( parmTypes [ <NUM_LIT:0> ] ) ) { Type [ ] extra = new Type [ parmTypes . length + <NUM_LIT:1> ] ; extra [ <NUM_LIT:0> ] = EPROC_TYPE ; for ( int i = <NUM_LIT:0> ; i < parmTypes . length ; i ++ ) { extra [ i + <NUM_LIT:1> ] = parmTypes [ i ] ; } m = find_bif ( extra ) ; if ( m != null ) return m ; } return m ; } private BuiltInFunction find_bif ( Type [ ] parmTypes ) { Args args = new Args ( parmTypes ) ; BuiltInFunction m = found . get ( args ) ; if ( m != null ) { return m ; } for ( Args a : args . generalize ( ) ) { m = found . get ( a ) ; if ( m != null ) { if ( ERT . log . isLoggable ( Level . FINE ) ) ERT . log . fine ( "<STR_LIT>" + EAtom . intern ( name ) + "<STR_LIT:/>" + parmTypes . length + "<STR_LIT:U+0020>" + args + "<STR_LIT>" + m ) ; return m ; } } return null ; } } public static Type getBifResult ( String module , String name , Type [ ] parmTypes , boolean isGuard ) { Map < String , BIFHandler > tab = isGuard ? guard_bifs : bifs ; BIFHandler bif = null ; String key = module + "<STR_LIT::>" + name ; if ( tab . containsKey ( key ) ) { bif = tab . get ( key ) ; } else { throw new Error ( "<STR_LIT>" + ( isGuard ? "<STR_LIT>" : "<STR_LIT>" ) + "<STR_LIT>" + module + "<STR_LIT>" + name + "<STR_LIT>" + parmTypes . length ) ; } return bif . getResult ( parmTypes ) ; } @ SuppressWarnings ( "<STR_LIT:unchecked>" ) private static void loadBIFs ( String [ ] mods ) throws Exception { for ( String mod : mods ) { Class < ENative > en = ( Class < ENative > ) Class . forName ( "<STR_LIT>" + mod + "<STR_LIT>" ) ; registerBifs ( mod , en ) ; ENative enative = en . newInstance ( ) ; for ( Class c : enative . getNativeClasses ( ) ) { if ( c != en ) { registerBifs ( mod , c ) ; } } } registerBifs ( "<STR_LIT>" , ERT . class ) ; registerBifs ( "<STR_LIT>" , EObject . class ) ; registerBifs ( "<STR_LIT>" , ESmall . class ) ; registerBifs ( "<STR_LIT>" , EBig . class ) ; } public static void registerBifs ( String module , Class < ? > clazz ) { Method [ ] m = clazz . getMethods ( ) ; for ( int i = <NUM_LIT:0> ; i < m . length ; i ++ ) { Method method = m [ i ] ; erjang . BIF ann = method . getAnnotation ( erjang . BIF . class ) ; if ( ann != null ) { if ( ( method . getModifiers ( ) & Modifier . STATIC ) != Modifier . STATIC ) { } Map < String , BIFHandler > tab = ann . type ( ) == erjang . BIF . Type . GUARD ? guard_bifs : bifs ; String bifName = ann . name ( ) ; if ( bifName . equals ( "<STR_LIT>" ) ) { bifName = method . getName ( ) ; } String key = module + "<STR_LIT::>" + bifName ; BIFHandler h = tab . get ( key ) ; if ( h == null ) { tab . put ( key , h = new BIFHandler ( bifName ) ) ; } h . registerMethod ( method ) ; } } } public static BuiltInFunction getMethod ( String module , String name , int arity , boolean isGuard , boolean fail_when_missing ) { return getMethod ( module , name , eobjectParmTypes ( arity ) , isGuard , fail_when_missing ) ; } private static Type [ ] eobjectParmTypes ( int length ) { Type [ ] res = new Type [ length ] ; for ( int i = <NUM_LIT:0> ; i < length ; i ++ ) { res [ i ] = EOBJECT_TYPE ; } return res ; } public static BuiltInFunction getMethod ( String module , String name , Type [ ] parmTypes , boolean isGuard , boolean fail_when_missing ) { Map < String , BIFHandler > tab = isGuard ? guard_bifs : bifs ; BIFHandler bif = null ; String key = module + "<STR_LIT::>" + name ; if ( tab . containsKey ( key ) ) { bif = tab . get ( key ) ; } else if ( fail_when_missing ) { throw new Error ( "<STR_LIT>" + ( isGuard ? "<STR_LIT>" : "<STR_LIT>" ) + "<STR_LIT>" + module + "<STR_LIT>" + name + "<STR_LIT>" + parmTypes . length ) ; } else { return null ; } return bif . getMethod ( parmTypes ) ; } public static BuiltInFunction getMethod ( String module , String name , Arg [ ] args , boolean isGuard , boolean fail_when_missing ) { Type [ ] parms = new Type [ args . length ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { parms [ i ] = args [ i ] . type ; } return getMethod ( module , name , parms , isGuard , fail_when_missing ) ; } public static BuiltInFunction getMethod ( EAtom module , EAtom function , int arity , boolean isGuard , boolean failWhenMissing ) { return getMethod ( module . getName ( ) , function . getName ( ) , arity , isGuard , failWhenMissing ) ; } static long all_bif_hash = <NUM_LIT:0> ; public static long all_bif_hash ( ) { if ( all_bif_hash == <NUM_LIT:0> ) { for ( BIFHandler b : bifs . values ( ) ) { all_bif_hash += b . hashCode ( ) ; } for ( BIFHandler b : guard_bifs . values ( ) ) { all_bif_hash += b . hashCode ( ) ; } } return all_bif_hash ; } } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; import erjang . EObject ; public class ModuleAdapter implements ModuleVisitor { private final ModuleVisitor mv ; public ModuleAdapter ( ModuleVisitor mv ) { this . mv = mv ; } public void visitModule ( EAtom name ) { mv . visitModule ( name ) ; } public void visitExport ( EAtom fun , int arity , int entry ) { mv . visitExport ( fun , arity , entry ) ; } public void visitAttribute ( EAtom att , EObject value ) { mv . visitAttribute ( att , value ) ; } @ Override public void declareFunction ( EAtom fun , int arity , int label ) { mv . declareFunction ( fun , arity , label ) ; } public FunctionVisitor visitFunction ( EAtom name , int arity , int startLabel ) { return mv . visitFunction ( name , arity , startLabel ) ; } public void visitEnd ( ) { mv . visitEnd ( ) ; } } </s>
|
<s> package erjang . beam ; import java . io . ByteArrayOutputStream ; import java . io . EOFException ; import java . io . File ; import java . io . FileInputStream ; import java . io . IOException ; import java . util . ArrayList ; import java . util . Set ; import java . util . regex . Matcher ; import java . util . regex . Pattern ; import java . util . zip . CRC32 ; import java . util . zip . CheckedInputStream ; import kilim . analysis . ClassInfo ; import kilim . analysis . ClassWeaver ; import kilim . analysis . Detector ; import kilim . mirrors . ClassMirrorNotFoundException ; import kilim . mirrors . Mirrors ; import org . objectweb . asm . ClassWriter ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . util . CheckClassAdapter ; import com . ericsson . otp . erlang . OtpAuthException ; import erjang . EBinary ; import erjang . EFun ; import erjang . EObject ; import erjang . ERT ; import erjang . ETuple ; import erjang . beam . analysis . BeamTypeAnalysis ; import erjang . ErjangCodeCache ; import erjang . beam . loader . ErjangBeamDisLoader ; public class Compiler implements Opcodes { private ClassRepo classRepo ; public Compiler ( ClassRepo repo ) throws OtpAuthException , IOException { this . classRepo = repo ; } public static void compile ( BeamFileData data , ClassRepo repo ) throws IOException { ClassWeaver . reset ( ) ; ClassWriter cw = new ClassWriter ( true ) ; CompilerVisitor cv = new CompilerVisitor ( cw , repo ) ; BeamTypeAnalysis analysis = new BeamTypeAnalysis ( cv ) ; ModuleAnalyzer ma = new ModuleAnalyzer ( ) ; data . accept ( ma ) ; cv . setFunInfos ( ma . getFunInfos ( ) ) ; try { data . accept ( analysis ) ; } catch ( Error e ) { e . printStackTrace ( ) ; } byte [ ] byteArray = cw . toByteArray ( ) ; repo . store ( "<STR_LIT>" + cv . getInternalClassName ( ) , byteArray ) ; boolean written = false ; ClassWeaver cwe = new ClassWeaver ( byteArray , new ErjangDetector ( cv . getInternalClassName ( ) , cv . non_pausable_methods ) ) ; for ( ClassInfo ci : cwe . getClassInfos ( ) ) { String name = ci . className ; byte [ ] bytes = ci . bytes ; String iname = name . replace ( '<CHAR_LIT:.>' , '<CHAR_LIT:/>' ) ; if ( iname . equals ( cv . getInternalClassName ( ) ) ) { written = true ; } repo . store ( iname , bytes ) ; } if ( ! written ) { repo . store ( cv . getInternalClassName ( ) , byteArray ) ; } } public void compile ( File file , BeamLoader beam_parser ) throws IOException { EBinary eb = EUtil . readFile ( file ) ; BeamFileData bfd = beam_parser . load ( eb . getByteArray ( ) ) ; compile ( bfd , this . classRepo ) ; } static public class ErjangDetector extends Detector { private final String className ; private final Set < String > nonPausableMethods ; public ErjangDetector ( String className , Set < String > nonPausableMethods ) { super ( Mirrors . getRuntimeMirrors ( ) ) ; this . className = className ; this . nonPausableMethods = nonPausableMethods ; } static Pattern FUN = Pattern . compile ( "<STR_LIT>" ) ; @ Override public ArrayList < String > getSuperClasses ( String cc ) throws ClassMirrorNotFoundException { Matcher m = FUN . matcher ( cc ) ; if ( m . matches ( ) ) { int arity = Integer . parseInt ( m . group ( <NUM_LIT:1> ) ) ; ArrayList < String > result = new ArrayList < String > ( ) ; result . add ( EFun . class . getName ( ) + arity ) ; result . add ( EFun . class . getName ( ) ) ; result . add ( EObject . class . getName ( ) ) ; result . add ( Object . class . getName ( ) ) ; return result ; } return super . getSuperClasses ( cc ) ; } @ Override public int getPausableStatus ( String className , String methodName , String desc ) { if ( className . startsWith ( CompilerVisitor . ETUPLE_NAME ) ) { return Detector . METHOD_NOT_PAUSABLE ; } if ( className . startsWith ( CompilerVisitor . EFUN_NAME ) ) { if ( methodName . equals ( "<STR_LIT>" ) ) return Detector . PAUSABLE_METHOD_FOUND ; if ( methodName . equals ( "<STR_LIT>" ) ) return Detector . PAUSABLE_METHOD_FOUND ; return Detector . METHOD_NOT_PAUSABLE ; } if ( className . equals ( this . className ) ) { if ( methodName . endsWith ( "<STR_LIT>" ) ) return Detector . METHOD_NOT_PAUSABLE ; if ( methodName . endsWith ( "<STR_LIT>" ) ) return Detector . METHOD_NOT_PAUSABLE ; if ( methodName . equals ( "<STR_LIT>" ) ) return Detector . METHOD_NOT_PAUSABLE ; if ( nonPausableMethods . contains ( methodName ) ) return Detector . METHOD_NOT_PAUSABLE ; return Detector . PAUSABLE_METHOD_FOUND ; } return super . getPausableStatus ( className , methodName , desc ) ; } } public static String moduleClassName ( String moduleName ) { String cn = EUtil . toJavaIdentifier ( moduleName ) ; String base = "<STR_LIT>" + cn + "<STR_LIT:/>" + cn ; return base ; } public static void main ( String [ ] args ) throws Exception { File out_dir = new File ( "<STR_LIT>" ) ; out_dir . mkdirs ( ) ; BeamLoader beamParser = new ErjangBeamDisLoader ( ) ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { if ( args [ i ] . endsWith ( "<STR_LIT>" ) ) { File in = new File ( args [ i ] ) ; if ( ! in . exists ( ) || ! in . isFile ( ) || ! in . canRead ( ) ) throw new IOException ( "<STR_LIT>" + in ) ; int idx = args [ i ] . lastIndexOf ( '<CHAR_LIT:.>' ) ; int idx0 = args [ i ] . lastIndexOf ( File . separator ) ; String shortName = args [ i ] . substring ( idx0 + <NUM_LIT:1> , idx ) ; File out = new File ( out_dir , ErjangCodeCache . moduleJarFileName ( shortName , crcFile ( in ) ) ) ; JarClassRepo jcp = new JarClassRepo ( out ) ; System . out . println ( "<STR_LIT>" + in + "<STR_LIT>" + out + "<STR_LIT>" ) ; new Compiler ( jcp ) . compile ( in , beamParser ) ; jcp . close ( ) ; } } } private static long crcFile ( File file ) throws IOException { CheckedInputStream cis = null ; long fileSize = <NUM_LIT:0> ; cis = new CheckedInputStream ( new FileInputStream ( file ) , new CRC32 ( ) ) ; try { byte [ ] buf = new byte [ <NUM_LIT:4> * <NUM_LIT> ] ; while ( cis . read ( buf ) >= <NUM_LIT:0> ) ; return cis . getChecksum ( ) . getValue ( ) ; } finally { cis . close ( ) ; } } } </s>
|
<s> package erjang . beam ; import java . io . IOException ; public interface ClassRepo { void store ( String internal_name , byte [ ] data ) throws IOException ; void close ( ) throws IOException ; } </s>
|
<s> package erjang . beam . repr ; import java . util . HashMap ; import java . util . Map ; import erjang . EAtom ; import erjang . EBitString ; import erjang . EObject ; import erjang . EString ; public class CodeTables { protected EAtom [ ] atoms ; protected EObject [ ] literals ; protected ExtFun [ ] externalFuns ; protected AnonFun [ ] anonymousFuns ; protected byte [ ] stringpool ; protected Map < Integer , FunctionInfo > functionMap = new HashMap < Integer , FunctionInfo > ( ) ; public EAtom atom ( int nr ) { return atoms [ nr - <NUM_LIT:1> ] ; } public EObject literal ( int nr ) { return literals [ nr ] ; } public ExtFun extFun ( int nr ) { return externalFuns [ nr ] ; } public AnonFun anonFun ( int nr ) { return anonymousFuns [ nr ] ; } public EBitString bitstring ( int start , int bits ) { return EBitString . make ( stringpool , start , bits / <NUM_LIT:8> , bits % <NUM_LIT:8> ) ; } public EString string ( int start , int bytes ) { return EString . make ( stringpool , start , bytes ) ; } public EAtom moduleName ( ) { return atom ( <NUM_LIT:1> ) ; } protected void addFunctionAtLabel ( FunctionInfo fi ) { assert ( ! functionMap . containsKey ( fi . label ) ) ; functionMap . put ( fi . label , fi ) ; } public FunctionInfo functionAtLabel ( int label ) { return functionMap . get ( label ) ; } } </s>
|
<s> package erjang . beam . repr ; import java . util . List ; import erjang . beam . BeamOpcode ; import erjang . beam . ModuleVisitor ; import erjang . beam . FunctionVisitor ; import erjang . beam . BlockVisitor ; import erjang . beam . CodeAtoms ; import erjang . EObject ; import erjang . ETuple ; import erjang . ESmall ; import erjang . ESeq ; import erjang . beam . loader . Rewriter ; public class FunctionRepr { protected FunctionInfo sig ; protected List < Insn > body ; public FunctionRepr ( FunctionInfo sig , List < Insn > body ) { this . sig = sig ; this . body = body ; } public void declare ( ModuleVisitor mv ) { mv . declareFunction ( sig . fun , sig . arity , sig . label ) ; } public void accept ( ModuleVisitor mv ) { FunctionVisitor fv = mv . visitFunction ( sig . fun , sig . arity , sig . label ) ; accept ( fv ) ; } public void accept ( FunctionVisitor fv ) { BlockVisitor bv = null ; for ( Insn insn : body ) { if ( insn . opcode == BeamOpcode . label ) { if ( bv != null ) bv . visitEnd ( ) ; bv = fv . visitLabeledBlock ( ( ( Insn . I ) insn ) . i1 ) ; } else { bv . visitInsn ( insn ) ; } } if ( bv != null ) bv . visitEnd ( ) ; fv . visitEnd ( ) ; } public ETuple toSymbolic ( ) { EObject [ ] symBody = new EObject [ body . size ( ) ] ; int i = <NUM_LIT:0> ; for ( Insn insn : body ) { symBody [ i ++ ] = insn . toSymbolic ( ) ; } ETuple fun = ETuple . make ( CodeAtoms . FUNCTION_ATOM , sig . fun , new ESmall ( sig . arity ) , new ESmall ( sig . label ) , ESeq . fromArray ( symBody ) ) ; return fun ; } public void rewrite ( Rewriter rw ) { rw . rewriteFunctionBody ( body , sig ) ; } } </s>
|
<s> package erjang . beam . repr ; import static erjang . beam . CodeAtoms . ARITHFBIF_ATOM ; import static erjang . beam . CodeAtoms . ATOM_ATOM ; import static erjang . beam . CodeAtoms . BIF_ATOM ; import static erjang . beam . CodeAtoms . FIELD_FLAGS_ATOM ; import static erjang . beam . CodeAtoms . F_ATOM ; import static erjang . beam . CodeAtoms . GCBIF_ATOM ; import static erjang . beam . CodeAtoms . NOFAIL_ATOM ; import static erjang . beam . CodeAtoms . START_ATOM ; import static erjang . beam . CodeAtoms . TEST_ATOM ; import erjang . EList ; import erjang . EObject ; import erjang . ERT ; import erjang . ESmall ; import erjang . ETuple ; import erjang . beam . BeamInstruction ; import erjang . beam . BeamOpcode ; import erjang . beam . repr . Operands . AllocList ; import erjang . beam . repr . Operands . Atom ; import erjang . beam . repr . Operands . BitString ; import erjang . beam . repr . Operands . ByteString ; import erjang . beam . repr . Operands . DestinationOperand ; import erjang . beam . repr . Operands . Label ; import erjang . beam . repr . Operands . SelectList ; import erjang . beam . repr . Operands . SourceOperand ; import erjang . beam . repr . Operands . XReg ; import erjang . beam . repr . Operands . YReg ; public class Insn implements BeamInstruction { protected final BeamOpcode opcode ; public Insn ( BeamOpcode opcode ) { this . opcode = opcode ; } public BeamOpcode opcode ( ) { return opcode ; } public String toString ( ) { return toSymbolic ( ) . toString ( ) ; } public EObject toSymbolic ( ) { return opcode . symbol ; } public final ETuple toSymbolicTuple ( ) { EObject symInsn0 = toSymbolic ( ) ; ETuple symInsn = ( symInsn0 instanceof ETuple ) ? ( ( ETuple ) symInsn0 ) : ETuple . make ( symInsn0 ) ; return symInsn ; } private static EObject NOFAIL_REPR = ETuple . make ( F_ATOM , new ESmall ( <NUM_LIT:0> ) ) ; private static EObject START_REPR = ETuple . make ( ATOM_ATOM , START_ATOM ) ; static EObject labelToSymbolic ( Label label ) { return label == null ? NOFAIL_REPR : label . toSymbolic ( ) ; } static EObject labelToSymbolic_nf ( Label label ) { return label == null ? NOFAIL_ATOM : label . toSymbolic ( ) ; } static EObject bsFieldFlagsToSymbolic ( int flags ) { return ETuple . make ( FIELD_FLAGS_ATOM , new ESmall ( flags ) ) ; } public static class I extends Insn { public final int i1 ; public I ( BeamOpcode opcode , int i1 ) { super ( opcode ) ; this . i1 = i1 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) ) ; } } public static class S extends Insn { public final SourceOperand src ; public S ( BeamOpcode opcode , SourceOperand src ) { super ( opcode ) ; this . src = src ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) ) ; } } public static class D extends Insn { public final DestinationOperand dest ; public D ( BeamOpcode opcode , DestinationOperand dest ) { super ( opcode ) ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , dest . toSymbolic ( ) ) ; } } public static class L extends Insn { public final Label label ; public L ( BeamOpcode opcode , Label label ) { super ( opcode ) ; this . label = label ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , label . toSymbolic ( ) ) ; } } public static class F extends Insn { public final int anon_fun_no ; public final AnonFun anon_fun ; public F ( BeamOpcode opcode , int anon_fun_no , AnonFun anon_fun ) { super ( opcode ) ; this . anon_fun_no = anon_fun_no ; this . anon_fun = anon_fun ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , anon_fun . toSymbolic ( ) , new ESmall ( anon_fun_no ) , new ESmall ( anon_fun . old_uniq ) , new ESmall ( anon_fun . free_vars ) ) ; } } public static class Y extends Insn { public final YReg y ; public Y ( BeamOpcode opcode , YReg y ) { super ( opcode ) ; this . y = y ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , y . toSymbolic ( ) ) ; } } public static class By extends Insn { public final ByteString bin ; public By ( BeamOpcode opcode , ByteString bin ) { super ( opcode ) ; this . bin = bin ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( bin . byteLength ( ) ) , bin . toSymbolic ( ) ) ; } } public static class DI extends Insn { public final DestinationOperand dest ; public final int i2 ; public final boolean is_saverestore ; public DI ( BeamOpcode opcode , DestinationOperand dest , int i2 , boolean is_saverestore ) { super ( opcode ) ; this . dest = dest ; this . i2 = i2 ; this . is_saverestore = is_saverestore ; } public ETuple toSymbolic ( ) { if ( is_saverestore ) return ETuple . make ( opcode . symbol , dest . toSymbolic ( ) , ( i2 == - <NUM_LIT:1> ? START_REPR : new ESmall ( i2 ) ) ) ; else return ETuple . make ( opcode . symbol , dest . toSymbolic ( ) , new ESmall ( i2 ) ) ; } } public static class SD extends Insn { public final SourceOperand src ; public final DestinationOperand dest ; public SD ( BeamOpcode opcode , SourceOperand src , DestinationOperand dest ) { super ( opcode ) ; this . src = src ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class LD extends L { public final DestinationOperand dest ; public final boolean is_test ; public LD ( BeamOpcode opcode , Label label , DestinationOperand dest ) { super ( opcode , label ) ; this . dest = dest ; this . is_test = false ; } public LD ( BeamOpcode opcode , Label label , DestinationOperand dest , boolean is_test ) { super ( opcode , label ) ; this . dest = dest ; this . is_test = is_test ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class YL extends Insn { public final YReg y ; public final Label label ; public YL ( BeamOpcode opcode , YReg y , Label label ) { super ( opcode ) ; this . y = y ; this . label = label ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , y . toSymbolic ( ) , label . toSymbolic ( ) ) ; } } public static class LS extends L { public final SourceOperand src ; public final boolean is_test ; public LS ( BeamOpcode opcode , Label label , SourceOperand src , boolean is_test ) { super ( opcode , label ) ; this . src = src ; this . is_test = is_test ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( src . toSymbolic ( ) ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src . toSymbolic ( ) ) ; } } public static class II extends I { public final int i2 ; public II ( BeamOpcode opcode , int i1 , int i2 ) { super ( opcode , i1 ) ; this . i2 = i2 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , new ESmall ( i2 ) ) ; } } public static class IL extends I { public final Label label ; public final boolean is_call ; public final FunctionInfo functionAtLabel ; public IL ( BeamOpcode opcode , int i1 , Label label , FunctionInfo fal ) { super ( opcode , i1 ) ; this . label = label ; this . is_call = true ; this . functionAtLabel = fal ; } public ETuple toSymbolic ( ) { if ( is_call ) return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , functionAtLabel . toSymbolic ( ) ) ; else return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , label . toSymbolic ( ) ) ; } } public static class SS extends Insn { public final SourceOperand src1 , src2 ; public SS ( BeamOpcode opcode , SourceOperand src1 , SourceOperand src2 ) { super ( opcode ) ; this . src1 = src1 ; this . src2 = src2 ; } public ETuple toSymbolic ( ) { if ( opcode == BeamOpcode . raise ) return ETuple . make ( opcode . symbol , new Label ( <NUM_LIT:0> ) . toSymbolic ( ) , EList . make ( src1 . toSymbolic ( ) , src2 . toSymbolic ( ) ) , XReg . get ( <NUM_LIT:0> ) . toSymbolic ( ) ) ; else return ETuple . make ( opcode . symbol , src1 . toSymbolic ( ) , src2 . toSymbolic ( ) ) ; } } public static class AAI extends Insn { public final Atom a1 , a2 ; public final int i3 ; public AAI ( BeamOpcode opcode , Atom a1 , Atom a2 , int i3 ) { super ( opcode ) ; this . a1 = a1 ; this . a2 = a2 ; this . i3 = i3 ; } public ExtFun getExtFun ( ) { return new ExtFun ( a1 . getEAtom ( ) , a2 . getEAtom ( ) , i3 ) ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , a1 . toSymbolic ( ) , a2 . toSymbolic ( ) , new ESmall ( i3 ) ) ; } } public static class IE extends I { public final ExtFun ext_fun ; public IE ( BeamOpcode opcode , int i1 , ExtFun ext_fun ) { super ( opcode , i1 ) ; this . ext_fun = ext_fun ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , ext_fun . toSymbolic ( ) ) ; } } public static class ID extends Insn { public final int i1 ; public final DestinationOperand dest ; public ID ( BeamOpcode opcode , int i1 , DestinationOperand dest ) { super ( opcode ) ; this . i1 = i1 ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , dest . toSymbolic ( ) ) ; } } public static class WI extends Insn { public final AllocList alist ; public final int i2 ; public WI ( BeamOpcode opcode , AllocList alist , int i2 ) { super ( opcode ) ; this . alist = alist ; this . i2 = i2 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , alist . toSymbolic ( ) , new ESmall ( i2 ) ) ; } } public static class IWI extends I { public final int i3 ; public final AllocList al ; public IWI ( BeamOpcode opcode , int i1 , AllocList al , int i3 ) { super ( opcode , i1 ) ; this . al = al ; this . i3 = i3 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , al . toSymbolic ( ) , new ESmall ( i3 ) ) ; } } public static class LDI extends LD { public final int i ; public LDI ( BeamOpcode opcode , Label label , DestinationOperand dest , int i , boolean is_test ) { super ( opcode , label , dest , is_test ) ; this . i = i ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , new ESmall ( i ) ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , dest . toSymbolic ( ) , new ESmall ( i ) ) ; } } public static class SID extends Insn { public final SourceOperand src ; public final int i ; public final DestinationOperand dest ; public SID ( BeamOpcode opcode , SourceOperand src , int i , DestinationOperand dest ) { super ( opcode ) ; this . src = src ; this . i = i ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) , new ESmall ( i ) , dest . toSymbolic ( ) ) ; } } public static class LSS extends L { public final SourceOperand src1 , src2 ; public final boolean is_test ; public LSS ( BeamOpcode opcode , Label label , SourceOperand src1 , SourceOperand src2 , boolean is_test ) { super ( opcode , label ) ; this . src1 = src1 ; this . src2 = src2 ; this . is_test = is_test ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( src1 . toSymbolic ( ) , src2 . toSymbolic ( ) ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src1 . toSymbolic ( ) , src2 . toSymbolic ( ) ) ; } } public static class LDS extends LD { public final SourceOperand src ; public LDS ( BeamOpcode opcode , Label label , DestinationOperand dest , SourceOperand src , boolean is_test ) { super ( opcode , label , dest , is_test ) ; this . src = src ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , src . toSymbolic ( ) ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , dest . toSymbolic ( ) , src . toSymbolic ( ) ) ; } } public static class LSD extends Insn { public final Label label ; public final SourceOperand src ; public final DestinationOperand dest ; public LSD ( BeamOpcode opcode , Label label , SourceOperand src , DestinationOperand dest ) { super ( opcode ) ; this . label = label ; this . src = src ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class SDD extends Insn { public final SourceOperand src ; public final DestinationOperand dest1 ; public final DestinationOperand dest2 ; public SDD ( BeamOpcode opcode , SourceOperand src , DestinationOperand dest1 , DestinationOperand dest2 ) { super ( opcode ) ; this . src = src ; this . dest1 = dest1 ; this . dest2 = dest2 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) , dest1 . toSymbolic ( ) , dest2 . toSymbolic ( ) ) ; } } public static class SSD extends Insn { public final SourceOperand src1 ; public final SourceOperand src2 ; public final DestinationOperand dest ; public SSD ( BeamOpcode opcode , SourceOperand src1 , SourceOperand src2 , DestinationOperand dest ) { super ( opcode ) ; this . src1 = src1 ; this . src2 = src2 ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src1 . toSymbolic ( ) , src2 . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class SDI extends Insn { public final SourceOperand src ; public final DestinationOperand dest ; public final int i ; public SDI ( BeamOpcode opcode , SourceOperand src , DestinationOperand dest , int i ) { super ( opcode ) ; this . src = src ; this . dest = dest ; this . i = i ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) , dest . toSymbolic ( ) , new ESmall ( i ) ) ; } } public static class ILI extends IL { public final int i3 ; public final boolean is_call ; public ILI ( BeamOpcode opcode , int i1 , Label label , int i3 , FunctionInfo fal ) { super ( opcode , i1 , label , fal ) ; this . i3 = i3 ; this . is_call = true ; } public ETuple toSymbolic ( ) { if ( is_call ) return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , functionAtLabel . toSymbolic ( ) , new ESmall ( i3 ) ) ; else return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , label . toSymbolic ( ) , new ESmall ( i3 ) ) ; } } public static class IEI extends IE { public final int i3 ; public IEI ( BeamOpcode opcode , int i1 , ExtFun ext_fun , int i3 ) { super ( opcode , i1 , ext_fun ) ; this . i3 = i3 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( i1 ) , ext_fun . toSymbolic ( ) , new ESmall ( i3 ) ) ; } } public static class ByD extends Insn { public final ByteString bin ; public final DestinationOperand dest ; public ByD ( BeamOpcode opcode , ByteString bin , DestinationOperand dest ) { super ( opcode ) ; this . bin = bin ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , new ESmall ( bin . byteLength ( ) ) , bin . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class LSSD extends Insn { public final Label label ; public final SourceOperand src1 ; public final SourceOperand src2 ; public final DestinationOperand dest ; public final boolean is_arithfbif ; public LSSD ( BeamOpcode opcode , Label label , SourceOperand src1 , SourceOperand src2 , DestinationOperand dest , boolean is_arithfbif ) { super ( opcode ) ; this . label = label ; this . src1 = src1 ; this . src2 = src2 ; this . dest = dest ; this . is_arithfbif = is_arithfbif ; } public ETuple toSymbolic ( ) { if ( is_arithfbif ) return ETuple . make ( ARITHFBIF_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( src1 . toSymbolic ( ) , src2 . toSymbolic ( ) ) , dest . toSymbolic ( ) ) ; else return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src1 . toSymbolic ( ) , src2 . toSymbolic ( ) , dest . toSymbolic ( ) ) ; } } public static class LSSID extends Insn { public final Label label ; public final SourceOperand src1 ; public final SourceOperand src2 ; public final int i3 ; public final DestinationOperand dest ; public LSSID ( BeamOpcode opcode , Label label , SourceOperand src1 , SourceOperand src2 , int i3 , DestinationOperand dest ) { super ( opcode ) ; this . label = label ; this . src1 = src1 ; this . src2 = src2 ; this . i3 = i3 ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , EList . make ( src1 . toSymbolic ( ) , src2 . toSymbolic ( ) , new ESmall ( i3 ) ) , dest . toSymbolic ( ) ) ; } } public static class LDBi extends LD { public final BitString bin ; public LDBi ( BeamOpcode opcode , Label label , DestinationOperand dest , BitString bin ) { super ( opcode , label , dest , true ) ; this . bin = bin ; } public ETuple toSymbolic ( ) { return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , ERT . box ( bin . bitLength ( ) ) , bin . toSymbolic ( ) ) ) ; } } public static class LDII extends LD { public final int i3 , i4 ; public LDII ( BeamOpcode opcode , Label label , DestinationOperand dest , int i3 , int i4 ) { super ( opcode , label , dest , true ) ; this . i3 = i3 ; this . i4 = i4 ; } public ETuple toSymbolic ( ) { return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , new ESmall ( i3 ) , bsFieldFlagsToSymbolic ( i4 ) ) ) ; } } public static class LSIIS extends Insn { public final Label label ; public final SourceOperand src2 , src5 ; public final int i3 , i4 ; public final boolean is_bs ; public LSIIS ( BeamOpcode opcode , Label label , SourceOperand src2 , int i3 , int i4 , SourceOperand src5 , boolean is_bs ) { super ( opcode ) ; this . label = label ; this . src2 = src2 ; this . i3 = i3 ; this . i4 = i4 ; this . src5 = src5 ; this . is_bs = is_bs ; } public ETuple toSymbolic ( ) { if ( is_bs ) return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src2 . toSymbolic ( ) , new ESmall ( i3 ) , bsFieldFlagsToSymbolic ( i4 ) , src5 . toSymbolic ( ) ) ; else throw new erjang . NotImplemented ( ) ; } } public static class LIS extends Insn { public final Label label ; public final int i2 ; public final SourceOperand src ; public final boolean is_bsput ; public LIS ( BeamOpcode opcode , Label label , int i2 , SourceOperand src , boolean is_bsput ) { super ( opcode ) ; this . label = label ; this . i2 = i2 ; this . src = src ; this . is_bsput = is_bsput ; } public ETuple toSymbolic ( ) { if ( is_bsput ) return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , bsFieldFlagsToSymbolic ( i2 ) , src . toSymbolic ( ) ) ; else throw new erjang . NotImplemented ( ) ; } } public static class LDIID extends LD { public final int i3 , i4 ; public final DestinationOperand dest5 ; public final boolean i4_is_bsflags ; public LDIID ( BeamOpcode opcode , Label label , DestinationOperand dest2 , int i3 , int i4 , DestinationOperand dest5 , boolean i4_is_bsflags ) { super ( opcode , label , dest2 , true ) ; this . i3 = i3 ; this . i4 = i4 ; this . dest5 = dest5 ; this . i4_is_bsflags = i4_is_bsflags ; } public ETuple toSymbolic ( ) { return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , new ESmall ( i3 ) , i4_is_bsflags ? bsFieldFlagsToSymbolic ( i4 ) : new ESmall ( i4 ) , dest5 . toSymbolic ( ) ) ) ; } } public static class LSIIID extends Insn { public final Label label ; public final SourceOperand src2 ; public final int i3 , i4 , i5 ; public final DestinationOperand dest ; public final boolean is_bs ; public LSIIID ( BeamOpcode opcode , Label label , SourceOperand src2 , int i3 , int i4 , int i5 , DestinationOperand dest , boolean is_bs ) { super ( opcode ) ; this . label = label ; this . src2 = src2 ; this . i3 = i3 ; this . i4 = i4 ; this . i5 = i5 ; this . dest = dest ; this . is_bs = is_bs ; } public ETuple toSymbolic ( ) { if ( is_bs ) return ETuple . make ( opcode . symbol , labelToSymbolic ( label ) , src2 . toSymbolic ( ) , new ESmall ( i3 ) , new ESmall ( i4 ) , bsFieldFlagsToSymbolic ( i5 ) , dest . toSymbolic ( ) ) ; else throw new erjang . NotImplemented ( ) ; } } public static class LDSII extends LD { public final SourceOperand src3 ; public final int i4 , i5 ; public LDSII ( BeamOpcode opcode , Label label , DestinationOperand dest , SourceOperand src3 , int i4 , int i5 ) { super ( opcode , label , dest , true ) ; this . src3 = src3 ; this . i4 = i4 ; this . i5 = i5 ; } public ETuple toSymbolic ( ) { return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , src3 . toSymbolic ( ) , new ESmall ( i4 ) , bsFieldFlagsToSymbolic ( i5 ) ) ) ; } } public static class LDISIID extends LD { public final int i3 ; public final SourceOperand src4 ; public final int i5 , i6 ; public final DestinationOperand dest7 ; public LDISIID ( BeamOpcode opcode , Label label , DestinationOperand dest2 , int i3 , SourceOperand src4 , int i5 , int i6 , DestinationOperand dest7 ) { super ( opcode , label , dest2 , true ) ; this . i3 = i3 ; this . src4 = src4 ; this . i5 = i5 ; this . i6 = i6 ; this . dest7 = dest7 ; } public ETuple toSymbolic ( ) { if ( is_test ) return ETuple . make ( TEST_ATOM , opcode . symbol , label . toSymbolic ( ) , EList . make ( dest . toSymbolic ( ) , new ESmall ( i3 ) , src4 . toSymbolic ( ) , new ESmall ( i5 ) , bsFieldFlagsToSymbolic ( i6 ) , dest7 . toSymbolic ( ) ) ) ; else throw new erjang . NotImplemented ( ) ; } } public static class Bif extends Insn { public final Label label ; public final ExtFun ext_fun ; public final SourceOperand [ ] args ; public final DestinationOperand dest ; protected Bif ( BeamOpcode opcode , Label label , ExtFun ext_fun , SourceOperand [ ] args , DestinationOperand dest ) { super ( opcode ) ; this . label = label ; this . ext_fun = ext_fun ; this . dest = dest ; this . args = args ; } private static SourceOperand [ ] NO_ARGS = new SourceOperand [ ] { } ; public Bif ( BeamOpcode opcode , Label label , ExtFun ext_fun , DestinationOperand dest ) { this ( opcode , label , ext_fun , NO_ARGS , dest ) ; } public Bif ( BeamOpcode opcode , Label label , ExtFun ext_fun , SourceOperand src , DestinationOperand dest ) { this ( opcode , label , ext_fun , new SourceOperand [ ] { src } , dest ) ; } public Bif ( BeamOpcode opcode , Label label , ExtFun ext_fun , SourceOperand src1 , SourceOperand src2 , DestinationOperand dest ) { this ( opcode , label , ext_fun , new SourceOperand [ ] { src1 , src2 } , dest ) ; } public SourceOperand [ ] argList ( ) { return args ; } public ETuple toSymbolic ( ) { return ETuple . make ( BIF_ATOM , ext_fun . fun , labelToSymbolic_nf ( label ) , Operands . toSymbolicList ( args ) , dest . toSymbolic ( ) ) ; } } public static class GcBif extends Insn { public final Label label ; public final ExtFun ext_fun ; public final int i ; public final SourceOperand [ ] args ; public final DestinationOperand dest ; protected GcBif ( BeamOpcode opcode , Label label , ExtFun ext_fun , int i , SourceOperand [ ] args , DestinationOperand dest ) { super ( opcode ) ; this . label = label ; this . ext_fun = ext_fun ; this . i = i ; this . args = args ; this . dest = dest ; } public GcBif ( BeamOpcode opcode , Label label , ExtFun ext_fun , int i , SourceOperand src , DestinationOperand dest ) { this ( opcode , label , ext_fun , i , new SourceOperand [ ] { src } , dest ) ; } public GcBif ( BeamOpcode opcode , Label label , ExtFun ext_fun , int i , SourceOperand src1 , SourceOperand src2 , DestinationOperand dest ) { this ( opcode , label , ext_fun , i , new SourceOperand [ ] { src1 , src2 } , dest ) ; } public SourceOperand [ ] argList ( ) { return args ; } public ETuple toSymbolic ( ) { return ETuple . make ( GCBIF_ATOM , ext_fun . fun , label . toSymbolic ( ) , new ESmall ( i ) , Operands . toSymbolicList ( args ) , dest . toSymbolic ( ) ) ; } } public static class Select extends Insn { public final SourceOperand src ; public final Label defaultLabel ; public final SelectList jumpTable ; public Select ( BeamOpcode opcode , SourceOperand src , Label defaultLabel , SelectList jumpTable ) { super ( opcode ) ; this . src = src ; this . defaultLabel = defaultLabel ; this . jumpTable = jumpTable ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , src . toSymbolic ( ) , defaultLabel . toSymbolic ( ) , jumpTable . toSymbolic ( ) ) ; } } public static class BSAppend extends Insn { public final Label label ; public final SourceOperand src2 ; public final int i3 , i4 , i5 , i7 ; public final SourceOperand src6 ; public final DestinationOperand dest8 ; public BSAppend ( BeamOpcode opcode , Label label , SourceOperand src2 , int i3 , int i4 , int i5 , SourceOperand src6 , int i7 , DestinationOperand dest8 ) { super ( opcode ) ; this . label = label ; this . src2 = src2 ; this . i3 = i3 ; this . i4 = i4 ; this . i5 = i5 ; this . src6 = src6 ; this . i7 = i7 ; this . dest8 = dest8 ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src2 . toSymbolic ( ) , new ESmall ( i3 ) , new ESmall ( i4 ) , new ESmall ( i5 ) , src6 . toSymbolic ( ) , bsFieldFlagsToSymbolic ( i7 ) , dest8 . toSymbolic ( ) ) ; } } public static class BSPrivateAppend extends Insn { public final Label label ; public final SourceOperand src2 , src4 ; public final int i3 , i5 ; public final DestinationOperand dest ; public BSPrivateAppend ( BeamOpcode opcode , Label label , SourceOperand src2 , int i3 , SourceOperand src4 , int i5 , DestinationOperand dest ) { super ( opcode ) ; this . label = label ; this . src2 = src2 ; this . i3 = i3 ; this . src4 = src4 ; this . i5 = i5 ; this . dest = dest ; } public ETuple toSymbolic ( ) { return ETuple . make ( opcode . symbol , label . toSymbolic ( ) , src2 . toSymbolic ( ) , new ESmall ( i3 ) , src4 . toSymbolic ( ) , bsFieldFlagsToSymbolic ( i5 ) , dest . toSymbolic ( ) ) ; } } } </s>
|
<s> package erjang . beam . repr ; import erjang . EAtom ; import erjang . EBinary ; import erjang . ETuple ; import erjang . ESmall ; public class AnonFun { public final EAtom mod , fun ; public final int total_arity , free_vars ; public final int label ; public final int index ; public final int old_uniq ; public final int old_index ; public final EBinary mod_md5 ; public AnonFun ( EAtom mod , EAtom fun , int arity , int label , int old_uniq , int old_index , EBinary mod_md5 , int index , int free_vars ) { this . mod = mod ; this . fun = fun ; this . total_arity = arity ; this . free_vars = free_vars ; this . label = label ; this . index = index ; this . old_index = old_index ; this . old_uniq = old_uniq ; this . mod_md5 = mod_md5 ; } public ExtFun asExtFun ( ) { return new ExtFun ( mod , fun , total_arity ) ; } public String toString ( ) { return fun + "<STR_LIT:/>" + total_arity ; } public ETuple toSymbolic ( ) { return ETuple . make ( mod , fun , new ESmall ( total_arity ) ) ; } } </s>
|
<s> package erjang . beam . repr ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . logging . Level ; import java . util . logging . Logger ; import erjang . EAtom ; import erjang . EObject ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . beam . BeamFileData ; import erjang . beam . CodeAtoms ; import erjang . beam . ModuleVisitor ; import erjang . beam . loader . Rewriter ; public class ModuleRepr implements BeamFileData { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private EAtom moduleName ; private final CodeTables ct ; private final FunctionInfo [ ] exports ; private final FunctionRepr [ ] functions ; private ESeq attributes ; private ESeq compilation_info ; public ModuleRepr ( CodeTables ct , EAtom moduleName , FunctionInfo [ ] exports , ESeq attributes , ESeq comp_info , FunctionRepr [ ] functions ) { this . moduleName = moduleName ; this . ct = ct ; this . exports = exports ; this . attributes = attributes ; this . compilation_info = comp_info ; this . functions = functions ; } public void accept ( ModuleVisitor v ) { v . visitModule ( moduleName ) ; visit_exports ( v ) ; visit_attributes ( v ) ; try { for ( FunctionRepr fun : functions ) fun . declare ( v ) ; for ( FunctionRepr fun : functions ) fun . accept ( v ) ; } catch ( RuntimeException e ) { log . severe ( "<STR_LIT>" + e . getMessage ( ) ) ; log . log ( Level . FINE , "<STR_LIT>" , e ) ; throw ( e ) ; } finally { v . visitEnd ( ) ; } } private void visit_attributes ( ModuleVisitor v ) { for ( ESeq exp = ( ESeq ) attributes ; exp != ERT . NIL ; exp = exp . tail ( ) ) { ETuple attr = ( ETuple ) exp . head ( ) ; v . visitAttribute ( ( EAtom ) attr . elm ( <NUM_LIT:1> ) , attr . elm ( <NUM_LIT:2> ) ) ; } } private void visit_exports ( ModuleVisitor v ) { for ( FunctionInfo exp : exports ) { v . visitExport ( exp . fun , exp . arity , exp . label ) ; } } public ETuple toSymbolic ( ) { return ETuple . make ( CodeAtoms . BEAM_FILE_ATOM , moduleName , symbolicExportList ( ) , symbolicAttributes ( ) , compilation_info , symbolicCode ( ) ) ; } public EObject symbolicAttributes ( ) { EObject [ ] attrs = attributes . toArray ( ) ; Arrays . sort ( attrs , EObject . ERLANG_ORDERING ) ; return ESeq . fromArray ( attrs ) ; } public ESeq symbolicExportList ( ) { ArrayList < EObject > symExports = new ArrayList < EObject > ( exports . length ) ; int i = <NUM_LIT:0> ; for ( FunctionInfo f : exports ) { symExports . add ( ETuple . make ( f . fun , new ESmall ( f . arity ) , new ESmall ( f . label ) ) ) ; } Collections . sort ( symExports , EObject . ERLANG_ORDERING ) ; return ESeq . fromList ( symExports ) ; } public ESeq symbolicCode ( ) { ArrayList < ETuple > symFunctions = new ArrayList < ETuple > ( functions . length ) ; for ( FunctionRepr fun : functions ) { symFunctions . add ( fun . toSymbolic ( ) ) ; } return ESeq . fromList ( symFunctions ) ; } public void rewrite ( Rewriter rw ) { for ( FunctionRepr f : functions ) { f . rewrite ( rw ) ; } } } </s>
|
<s> package erjang . beam . repr ; import erjang . EObject ; import erjang . EAtom ; import erjang . ESmall ; import erjang . EBig ; import erjang . EDouble ; import erjang . ETuple ; import erjang . ESeq ; import erjang . EString ; import erjang . EBitString ; import erjang . ERT ; import java . util . ArrayList ; import java . math . BigInteger ; import static erjang . beam . CodeAtoms . * ; public class Operands { public static abstract class Operand { public SourceOperand asSource ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public DestinationOperand asDestination ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public Label asLabel ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public Literal asLiteral ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public Atom asAtom ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public CodeInt asCodeInt ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public SelectList asSelectList ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public AllocList asAllocList ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public YReg asYReg ( ) { throw new IllegalArgumentException ( "<STR_LIT>" + this ) ; } public TableLiteral testTableLiteral ( ) { return null ; } public Atom testAtom ( ) { return null ; } public Int testInt ( ) { return null ; } public BigInt testBigInt ( ) { return null ; } public Float testFloat ( ) { return null ; } public SelectList testSelectList ( ) { return null ; } public AllocList testAllocList ( ) { return null ; } public DestinationOperand testDestination ( ) { return null ; } public XReg testXReg ( ) { return null ; } public YReg testYReg ( ) { return null ; } public FReg testFReg ( ) { return null ; } public abstract EObject toSymbolic ( ) ; } public static ESeq toSymbolicList ( Operand [ ] args ) { ESeq res = ERT . NIL ; for ( int i = args . length - <NUM_LIT:1> ; i >= <NUM_LIT:0> ; i -- ) { res = res . cons ( args [ i ] . toSymbolic ( ) ) ; } return res ; } public static abstract class SourceOperand extends Operand { @ Override public SourceOperand asSource ( ) { return this ; } } public static abstract class DestinationOperand extends SourceOperand { @ Override public DestinationOperand asDestination ( ) { return this ; } @ Override public DestinationOperand testDestination ( ) { return this ; } } public static abstract class Literal extends SourceOperand { @ Override public Literal asLiteral ( ) { return this ; } public abstract EObject literalValue ( ) ; } public static Literal makeInt ( byte [ ] d ) { BigInteger tmp = new BigInteger ( d ) ; return ( d . length <= <NUM_LIT:4> ) ? new Int ( tmp . intValue ( ) ) : new BigInt ( tmp ) ; } public static class CodeInt extends Operand { public final int value ; public CodeInt ( int value ) { this . value = value ; } @ Override public CodeInt asCodeInt ( ) { return this ; } public EObject toSymbolic ( ) { return new ESmall ( value ) ; } } public static class Int extends Literal { public final int value ; public Int ( int value ) { this . value = value ; } public boolean equals ( int val ) { return value == val ; } @ Override public Int testInt ( ) { return this ; } @ Override public EObject literalValue ( ) { return new ESmall ( value ) ; } public EObject toSymbolic ( ) { return ETuple . make ( INTEGER_ATOM , new ESmall ( value ) ) ; } } public static class BigInt extends Literal { public final BigInteger value ; public BigInt ( BigInteger value ) { this . value = value ; } @ Override public BigInt testBigInt ( ) { return this ; } @ Override public EObject literalValue ( ) { return new EBig ( value ) ; } public EObject toSymbolic ( ) { return ETuple . make ( INTEGER_ATOM , new EBig ( value ) ) ; } } public static class Float extends Literal { public final double value ; public Float ( double value ) { this . value = value ; } @ Override public Float testFloat ( ) { return this ; } @ Override public EObject literalValue ( ) { return new EDouble ( value ) ; } public EObject toSymbolic ( ) { return ETuple . make ( FLOAT_ATOM , new EDouble ( value ) ) ; } } public static final Nil Nil = new Nil ( ) ; public static class Nil extends Literal { private Nil ( ) { } @ Override public EObject literalValue ( ) { return ERT . NIL ; } public EObject toSymbolic ( ) { return NIL_ATOM ; } } public static class SelectList extends Operand { Operand [ ] list ; public SelectList ( Operand [ ] list ) { this . list = list ; } @ Override public SelectList asSelectList ( ) { return this ; } public int size ( ) { return list . length / <NUM_LIT:2> ; } public Operand getValue ( int i ) { return list [ <NUM_LIT:2> * i ] ; } public Label getLabel ( int i ) { return ( Label ) list [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ; } public EObject toSymbolic ( ) { EObject [ ] elems = new EObject [ list . length ] ; for ( int i = <NUM_LIT:0> ; i < list . length ; i ++ ) { elems [ i ] = list [ i ] . toSymbolic ( ) ; } return ETuple . make ( LIST_ATOM , ESeq . fromArray ( elems ) ) ; } } public static class AllocList extends Operand { static final int WORDS = <NUM_LIT:0> ; static final int FLOATS = <NUM_LIT:1> ; int [ ] list ; public AllocList ( int [ ] list ) { this . list = list ; } public AllocList ( int words ) { list = new int [ <NUM_LIT:2> ] ; list [ <NUM_LIT:0> ] = WORDS ; list [ <NUM_LIT:1> ] = words ; } @ Override public AllocList asAllocList ( ) { return this ; } public EObject toSymbolic ( ) { int len = list . length / <NUM_LIT:2> ; if ( len == <NUM_LIT:1> && list [ <NUM_LIT:0> ] == WORDS ) return new ESmall ( list [ <NUM_LIT:1> ] ) ; EObject [ ] elems = new EObject [ len ] ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { elems [ i ] = ETuple . make ( kindToSymbolic ( list [ <NUM_LIT:2> * i ] ) , new ESmall ( list [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) ; } return ETuple . make ( ALLOC_ATOM , ESeq . fromArray ( elems ) ) ; } protected EAtom kindToSymbolic ( int kind ) { switch ( kind ) { case WORDS : return WORDS_ATOM ; case FLOATS : return FLOATS_ATOM ; default : throw new IllegalArgumentException ( "<STR_LIT>" + kind ) ; } } } public static class Atom extends Literal { private EAtom value ; public Atom ( EAtom value ) { this . value = value ; } @ Override public Atom asAtom ( ) { return this ; } @ Override public Atom testAtom ( ) { return this ; } @ Override public EObject literalValue ( ) { return value ; } public EAtom getEAtom ( ) { return value ; } public EObject toSymbolic ( ) { return ETuple . make ( ATOM_ATOM , value ) ; } @ Override public String toString ( ) { return "<STR_LIT>" + value . toString ( ) ; } } public static class BitString extends Literal { public final EBitString value ; public BitString ( EBitString value ) { this . value = value ; } @ Override public EObject literalValue ( ) { return value ; } public long bitLength ( ) { return value . bitSize ( ) ; } @ Override public EObject toSymbolic ( ) { return value ; } } public static class ByteString extends Literal { public final EString value ; public ByteString ( EString value ) { this . value = value ; } @ Override public EObject literalValue ( ) { return value ; } public int byteLength ( ) { return value . length ( ) ; } @ Override public EObject toSymbolic ( ) { return ETuple . make ( STRING_ATOM , value ) ; } } public static class TableLiteral extends Literal { public final EObject value ; public TableLiteral ( EObject value ) { this . value = value ; } @ Override public EObject literalValue ( ) { return value ; } @ Override public TableLiteral testTableLiteral ( ) { return this ; } public EObject toSymbolic ( ) { return ETuple . make ( LITERAL_ATOM , value ) ; } } public static class Label extends Operand { public final int nr ; public Label ( int nr ) { this . nr = nr ; } @ Override public Label asLabel ( ) { return this ; } public EObject toSymbolic ( ) { return ETuple . make ( F_ATOM , new ESmall ( nr ) ) ; } @ Override public int hashCode ( ) { return nr ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof Label ) { Label lab = ( Label ) obj ; return lab . nr == nr ; } else { return false ; } } } public static class XReg extends DestinationOperand { public final int nr ; public XReg ( int nr ) { this . nr = nr ; } @ Override public XReg testXReg ( ) { return this ; } private static ArrayList < XReg > cache = new ArrayList < XReg > ( ) ; public static XReg get ( int nr ) { while ( cache . size ( ) <= nr ) { cache . add ( new XReg ( cache . size ( ) ) ) ; } return cache . get ( nr ) ; } public EObject toSymbolic ( ) { return ETuple . make ( X_ATOM , new ESmall ( nr ) ) ; } } public static class YReg extends DestinationOperand { public final int nr ; public YReg ( int nr ) { this . nr = nr ; } @ Override public YReg asYReg ( ) { return this ; } @ Override public YReg testYReg ( ) { return this ; } private static ArrayList < YReg > cache = new ArrayList < YReg > ( ) ; public static YReg get ( int nr ) { while ( cache . size ( ) <= nr ) { cache . add ( new YReg ( cache . size ( ) ) ) ; } return cache . get ( nr ) ; } public EObject toSymbolic ( ) { return ETuple . make ( Y_ATOM , new ESmall ( nr ) ) ; } } public static class FReg extends DestinationOperand { public final int nr ; public FReg ( int nr ) { this . nr = nr ; } @ Override public FReg testFReg ( ) { return this ; } private static ArrayList < FReg > cache = new ArrayList < FReg > ( ) ; public static FReg get ( int nr ) { while ( cache . size ( ) <= nr ) { cache . add ( new FReg ( cache . size ( ) ) ) ; } return cache . get ( nr ) ; } public EObject toSymbolic ( ) { return ETuple . make ( FR_ATOM , new ESmall ( nr ) ) ; } } } </s>
|
<s> package erjang . beam . repr ; import erjang . EAtom ; import erjang . ETuple ; import erjang . ESmall ; import static erjang . beam . CodeAtoms . EXTFUNC_ATOM ; public class ExtFun { public final EAtom mod , fun ; public final int arity ; public ExtFun ( EAtom mod , EAtom fun , int arity ) { this . mod = mod ; this . fun = fun ; this . arity = arity ; } public String toString ( ) { return mod + "<STR_LIT::>" + fun + "<STR_LIT:/>" + arity ; } public EAtom module ( ) { return mod ; } public EAtom name ( ) { return fun ; } public ETuple toSymbolic ( ) { return ETuple . make ( EXTFUNC_ATOM , mod , fun , new ESmall ( arity ) ) ; } } </s>
|
<s> package erjang . beam . repr ; import erjang . EAtom ; import erjang . ETuple ; import erjang . ESmall ; public class FunctionInfo { public final EAtom mod , fun ; public final int arity , label ; public FunctionInfo ( EAtom mod , EAtom fun , int arity , int label ) { this . mod = mod ; this . fun = fun ; this . arity = arity ; this . label = label ; } public String toString ( ) { return mod + "<STR_LIT::>" + fun + "<STR_LIT:/>" + arity ; } public ExtFun asExtFun ( ) { return new ExtFun ( mod , fun , arity ) ; } public ETuple toSymbolic ( ) { return ETuple . make ( mod , fun , new ESmall ( arity ) ) ; } } </s>
|
<s> package erjang . beam ; public interface FunctionVisitor { void visitEnd ( ) ; BlockVisitor visitLabeledBlock ( int label ) ; } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; import java . util . EnumSet ; public enum BeamOpcode { label ( <NUM_LIT> ) , func_info ( <NUM_LIT> ) , int_code_end ( <NUM_LIT> ) , call ( <NUM_LIT> ) , call_last ( <NUM_LIT> ) , call_only ( <NUM_LIT> ) , call_ext ( <NUM_LIT> ) , call_ext_last ( <NUM_LIT> ) , bif0 ( <NUM_LIT> ) , bif1 ( <NUM_LIT> ) , bif2 ( <NUM_LIT> ) , allocate ( <NUM_LIT> ) , allocate_heap ( <NUM_LIT> ) , allocate_zero ( <NUM_LIT> ) , allocate_heap_zero ( <NUM_LIT> ) , test_heap ( <NUM_LIT> ) , init ( <NUM_LIT> ) , deallocate ( <NUM_LIT> ) , K_return ( <NUM_LIT> ) , send ( <NUM_LIT> ) , remove_message ( <NUM_LIT> ) , timeout ( <NUM_LIT> ) , loop_rec ( <NUM_LIT> ) , loop_rec_end ( <NUM_LIT> ) , wait ( <NUM_LIT> ) , wait_timeout ( <NUM_LIT> ) , is_lt ( <NUM_LIT> ) , is_ge ( <NUM_LIT> ) , is_eq ( <NUM_LIT> ) , is_ne ( <NUM_LIT> ) , is_eq_exact ( <NUM_LIT> ) , is_ne_exact ( <NUM_LIT> ) , is_integer ( <NUM_LIT> ) , is_float ( <NUM_LIT> ) , is_number ( <NUM_LIT> ) , is_atom ( <NUM_LIT> ) , is_pid ( <NUM_LIT> ) , is_reference ( <NUM_LIT> ) , is_port ( <NUM_LIT> ) , is_nil ( <NUM_LIT> ) , is_binary ( <NUM_LIT> ) , is_list ( <NUM_LIT> ) , is_nonempty_list ( <NUM_LIT> ) , is_tuple ( <NUM_LIT> ) , test_arity ( <NUM_LIT> ) , select_val ( <NUM_LIT> ) , select_tuple_arity ( <NUM_LIT> ) , jump ( <NUM_LIT> ) , K_catch ( <NUM_LIT> ) , catch_end ( <NUM_LIT> ) , move ( <NUM_LIT> ) , get_list ( <NUM_LIT> ) , get_tuple_element ( <NUM_LIT> ) , set_tuple_element ( <NUM_LIT> ) , put_string ( <NUM_LIT> ) , put_list ( <NUM_LIT> ) , put_tuple ( <NUM_LIT> ) , put ( <NUM_LIT> ) , badmatch ( <NUM_LIT> ) , if_end ( <NUM_LIT> ) , case_end ( <NUM_LIT> ) , call_fun ( <NUM_LIT> ) , is_function ( <NUM_LIT> ) , call_ext_only ( <NUM_LIT> ) , bs_put_integer ( <NUM_LIT> ) , bs_put_binary ( <NUM_LIT> ) , bs_put_float ( <NUM_LIT> ) , bs_put_string ( <NUM_LIT> ) , fclearerror ( <NUM_LIT> ) , fcheckerror ( <NUM_LIT> ) , fmove ( <NUM_LIT> ) , fconv ( <NUM_LIT> ) , fadd ( <NUM_LIT> ) , fsub ( <NUM_LIT> ) , fmul ( <NUM_LIT> ) , fdiv ( <NUM_LIT> ) , fnegate ( <NUM_LIT> ) , make_fun2 ( <NUM_LIT> ) , K_try ( <NUM_LIT> ) , try_end ( <NUM_LIT> ) , try_case ( <NUM_LIT> ) , try_case_end ( <NUM_LIT> ) , raise ( <NUM_LIT> ) , bs_init2 ( <NUM_LIT> ) , bs_bits_to_bytes ( <NUM_LIT> ) , bs_add ( <NUM_LIT> ) , apply ( <NUM_LIT> ) , apply_last ( <NUM_LIT> ) , is_boolean ( <NUM_LIT> ) , is_function2 ( <NUM_LIT> ) , bs_start_match2 ( <NUM_LIT> ) , bs_get_integer2 ( <NUM_LIT> ) , bs_get_float2 ( <NUM_LIT> ) , bs_get_binary2 ( <NUM_LIT> ) , bs_skip_bits2 ( <NUM_LIT> ) , bs_test_tail2 ( <NUM_LIT> ) , bs_save2 ( <NUM_LIT> ) , bs_restore2 ( <NUM_LIT> ) , gc_bif1 ( <NUM_LIT> ) , gc_bif2 ( <NUM_LIT> ) , is_bitstr ( <NUM_LIT> ) , bs_context_to_binary ( <NUM_LIT> ) , bs_test_unit ( <NUM_LIT> ) , bs_match_string ( <NUM_LIT> ) , bs_init_writable ( <NUM_LIT> ) , bs_append ( <NUM_LIT> ) , bs_private_append ( <NUM_LIT> ) , trim ( <NUM_LIT> ) , bs_init_bits ( <NUM_LIT> ) , bs_get_utf8 ( <NUM_LIT> ) , bs_skip_utf8 ( <NUM_LIT> ) , bs_get_utf16 ( <NUM_LIT> ) , bs_skip_utf16 ( <NUM_LIT> ) , bs_get_utf32 ( <NUM_LIT> ) , bs_skip_utf32 ( <NUM_LIT> ) , bs_utf8_size ( <NUM_LIT> ) , bs_put_utf8 ( <NUM_LIT> ) , bs_utf16_size ( <NUM_LIT> ) , bs_put_utf16 ( <NUM_LIT> ) , bs_put_utf32 ( <NUM_LIT> ) , on_load ( <NUM_LIT> ) , recv_mark ( <NUM_LIT> ) , recv_set ( <NUM_LIT> ) , test , bif , gc_bif , arithfbif , i_call_fun_last ( <NUM_LIT> ) , NONE ; public final EAtom symbol = EAtom . intern ( name ( ) . startsWith ( "<STR_LIT>" ) ? name ( ) . substring ( <NUM_LIT:2> ) : name ( ) ) ; public final int encoding ; BeamOpcode ( ) { this . encoding = - <NUM_LIT:1> ; } BeamOpcode ( int encoding ) { this . encoding = encoding ; } static BeamOpcode [ ] decodeMap ; static { decodeMap = new BeamOpcode [ <NUM_LIT> ] ; for ( BeamOpcode x : EnumSet . allOf ( BeamOpcode . class ) ) if ( x . encoding >= <NUM_LIT:0> ) decodeMap [ x . encoding ] = x ; } public static BeamOpcode decode ( int opcode ) { return decodeMap [ opcode ] ; } static EAtom TRY = EAtom . intern ( "<STR_LIT>" ) ; static EAtom CATCH = EAtom . intern ( "<STR_LIT>" ) ; static EAtom RETURN = EAtom . intern ( "<STR_LIT>" ) ; static public BeamOpcode get ( EAtom sym ) { String name = sym . getName ( ) ; if ( sym == TRY ) return K_try ; if ( sym == CATCH ) return K_catch ; if ( sym == RETURN ) return K_return ; return valueOf ( name ) ; } } </s>
|
<s> package erjang . beam ; import java . util . Map ; import java . util . HashMap ; import java . util . Collection ; public class RamClassRepo implements ClassRepo { private final HashMap < String , byte [ ] > map = new HashMap ( ) ; public RamClassRepo ( ) { } @ Override public void store ( String internalName , byte [ ] data ) { map . put ( internalName , data ) ; } @ Override public void close ( ) { } public byte [ ] get ( String className ) { String pathName = className . replace ( '<CHAR_LIT:.>' , '<CHAR_LIT:/>' ) ; return map . get ( pathName ) ; } public Collection < Map . Entry < String , byte [ ] > > entrySet ( ) { return map . entrySet ( ) ; } } </s>
|
<s> package erjang . beam ; import java . io . File ; import java . io . ByteArrayOutputStream ; import java . io . DataOutputStream ; import java . io . FileInputStream ; import java . io . IOException ; import java . util . HashMap ; import java . util . Map ; import java . util . regex . Pattern ; import org . objectweb . asm . Type ; import erjang . EAtom ; import erjang . EBinary ; import erjang . beam . repr . ExtFun ; public class EUtil { static final Pattern SIMPLE_ID = Pattern . compile ( "<STR_LIT>" ) ; private static final String EOBJECT_DESC = CompilerVisitor . EOBJECT_TYPE . getDescriptor ( ) ; private static final String EPROC_DESC = CompilerVisitor . EPROC_TYPE . getDescriptor ( ) ; static Map < Integer , String > signatures = new HashMap < Integer , String > ( ) ; static Map < Integer , String > noproc_signatures = new HashMap < Integer , String > ( ) ; public static String getSignature ( int arity , boolean withProc ) { Map < Integer , String > signatures = withProc ? noproc_signatures : EUtil . signatures ; String res = signatures . get ( arity ) ; if ( res == null ) { StringBuffer sb = new StringBuffer ( "<STR_LIT:(>" ) ; if ( withProc ) { sb . append ( EPROC_DESC ) ; } for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { sb . append ( EOBJECT_DESC ) ; } sb . append ( "<STR_LIT:)>" ) ; sb . append ( EOBJECT_DESC ) ; signatures . put ( arity , res = sb . toString ( ) ) ; } return res ; } static String toJavaIdentifier ( EAtom name ) { return toJavaIdentifier ( name . getName ( ) ) ; } static String toJavaIdentifier ( String name ) { if ( SIMPLE_ID . matcher ( name ) . matches ( ) ) return name ; StringBuilder sb = new StringBuilder ( ) ; for ( char c : name . toCharArray ( ) ) { if ( c == '<CHAR_LIT>' ) { sb . append ( "<STR_LIT>" ) ; continue ; } else if ( sb . length ( ) == <NUM_LIT:0> ) { if ( Character . isJavaIdentifierStart ( c ) ) { sb . append ( c ) ; continue ; } } else if ( Character . isJavaIdentifierPart ( c ) ) { sb . append ( c ) ; continue ; } try { sb . append ( '<CHAR_LIT>' ) ; ByteArrayOutputStream baro = new ByteArrayOutputStream ( <NUM_LIT:5> ) ; DataOutputStream dao = new DataOutputStream ( baro ) ; dao . writeUTF ( new String ( new char [ ] { c } ) ) ; dao . close ( ) ; byte [ ] data = baro . toByteArray ( ) ; writeHexByte ( sb , <NUM_LIT> & data [ <NUM_LIT:2> ] ) ; if ( data . length > <NUM_LIT:3> ) writeHexByte ( sb , <NUM_LIT> & data [ <NUM_LIT:3> ] ) ; if ( data . length > <NUM_LIT:4> ) writeHexByte ( sb , <NUM_LIT> & data [ <NUM_LIT:4> ] ) ; } catch ( IOException ex ) { throw new Error ( ) ; } } return sb . toString ( ) ; } private static void writeHexByte ( StringBuilder sb , int b ) { if ( b < <NUM_LIT> ) { sb . append ( '<CHAR_LIT:0>' ) ; } sb . append ( Integer . toHexString ( b ) . toUpperCase ( ) ) ; } public static String plen ( Object o ) { String s = String . valueOf ( o ) ; StringBuilder sb = new StringBuilder ( "<STR_LIT:_>" ) ; writeHexByte ( sb , s . length ( ) ) ; sb . append ( s ) ; return s . toString ( ) ; } public static String getJavaName ( EAtom fun , int arity ) { String fname = fun . getName ( ) ; if ( fname . indexOf ( "<STR_LIT>" ) == - <NUM_LIT:1> ) { return toJavaIdentifier ( fun . getName ( ) + "<STR_LIT>" + arity ) ; } else { return toJavaIdentifier ( plen ( fun . getName ( ) ) + "<STR_LIT>" + arity ) ; } } public static String getJavaName ( ExtFun fun ) { return toJavaIdentifier ( fun . mod ) + "<STR_LIT>" + getJavaName ( fun . fun , fun . arity ) ; } public static String getFunClassName ( Type self_type , ExtFun efun ) { return self_type . getInternalName ( ) + "<STR_LIT>" + getJavaName ( efun . fun , efun . arity ) ; } public static String getFunClassName ( Type self_type , ExtFun efun , int freevars ) { return self_type . getInternalName ( ) + "<STR_LIT>" + getJavaName ( efun . fun , efun . arity - freevars ) ; } public static String getSignature ( int arity , boolean withProc , Type returnType ) { StringBuffer sb = new StringBuffer ( "<STR_LIT:(>" ) ; if ( withProc ) { sb . append ( EPROC_DESC ) ; } for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { sb . append ( EOBJECT_DESC ) ; } sb . append ( "<STR_LIT:)>" ) ; sb . append ( returnType . getDescriptor ( ) ) ; return sb . toString ( ) ; } public static String decodeJavaName ( String methodName ) { int idx ; if ( ( idx = methodName . indexOf ( '<CHAR_LIT>' ) ) == - <NUM_LIT:1> ) return methodName ; StringBuilder sb = new StringBuilder ( ) ; int start = <NUM_LIT:0> ; while ( idx != - <NUM_LIT:1> ) { sb . append ( methodName . substring ( start , idx ) ) ; if ( methodName . charAt ( idx + <NUM_LIT:1> ) == '<CHAR_LIT>' ) { sb . append ( '<CHAR_LIT>' ) ; start = idx + <NUM_LIT:2> ; } else { String hex = methodName . substring ( idx + <NUM_LIT:1> , idx + <NUM_LIT:3> ) ; char chval ; try { chval = ( char ) Integer . parseInt ( hex , <NUM_LIT:16> ) ; } catch ( NumberFormatException e ) { chval = '<CHAR_LIT>' ; } sb . append ( chval ) ; start = idx + <NUM_LIT:3> ; } idx = methodName . indexOf ( '<CHAR_LIT>' , start ) ; } sb . append ( methodName . substring ( start ) ) ; return sb . toString ( ) ; } public static EBinary readFile ( File file ) throws IOException { int length = ( int ) file . length ( ) ; byte [ ] data = new byte [ length ] ; FileInputStream fi = new FileInputStream ( file ) ; try { ByteArrayOutputStream bo = new ByteArrayOutputStream ( ) ; int read = <NUM_LIT:0> ; while ( read < length ) { read += fi . read ( data , read , length - read ) ; } } finally { fi . close ( ) ; } EBinary bin = new EBinary ( data ) ; return bin ; } } </s>
|
<s> package erjang . beam ; import erjang . EModule ; import erjang . EModuleManager ; import erjang . ENative ; import erjang . EFun ; import erjang . FunID ; import erjang . EObject ; import erjang . EProc ; import erjang . Export ; import erjang . Import ; import erjang . BIF ; import java . lang . reflect . Field ; import java . lang . reflect . Method ; import java . lang . reflect . Modifier ; public abstract class ECompiledModule extends EModule { public ClassLoader getModuleClassLoader ( ) { return this . getClass ( ) . getClassLoader ( ) ; } public void registerImportsAndExports ( ) throws Exception { Class < ? extends EModule > module_class = this . getClass ( ) ; Field [ ] fields = module_class . getDeclaredFields ( ) ; for ( Field field : fields ) { if ( ! Modifier . isStatic ( field . getModifiers ( ) ) ) continue ; Import imp = field . getAnnotation ( Import . class ) ; if ( imp != null ) { FunID f = new FunID ( imp ) ; EModuleManager . add_import ( f , new FieldBinder ( field , f , module_name ( ) ) ) ; continue ; } Export exp = field . getAnnotation ( Export . class ) ; if ( exp != null ) { field . setAccessible ( true ) ; EFun value ; try { value = ( EFun ) field . get ( null ) ; } catch ( Exception e ) { throw new Error ( e ) ; } if ( value == null ) throw new Error ( "<STR_LIT>" + field + "<STR_LIT>" ) ; FunID f ; EModuleManager . add_export ( this , f = new FunID ( exp ) , value ) ; continue ; } } process_native_annotations ( module_class ) ; String cname = module_class . getName ( ) ; String nname = cname . substring ( <NUM_LIT:0> , cname . lastIndexOf ( '<CHAR_LIT:.>' ) ) + "<STR_LIT>" ; load_native_bifs ( ) ; } } </s>
|
<s> package erjang . beam ; import java . util . HashMap ; import java . util . HashSet ; import java . util . Map ; import java . util . Set ; import java . util . logging . Level ; import java . util . logging . Logger ; import erjang . EAtom ; import erjang . EObject ; import erjang . ERT ; import erjang . FunID ; import erjang . beam . repr . Insn ; import erjang . beam . repr . Insn . IE ; import erjang . beam . repr . Insn . IL ; import erjang . beam . repr . Insn . ILI ; import erjang . beam . repr . Operands . Label ; import erjang . beam . repr . Operands . SourceOperand ; public class ModuleAnalyzer implements ModuleVisitor { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; Map < Label , FunInfo > info = new HashMap < Label , FunInfo > ( ) ; static class FunInfo { Set < FunInfo > callers = new HashSet < FunInfo > ( ) ; Set < FunInfo > tail_callers = new HashSet < FunInfo > ( ) ; FunID name ; boolean may_return_tail_marker , is_pausable , call_is_pausable ; public boolean exported ; protected boolean is_called_locally_in_tail_position ; protected boolean is_anon_fun ; protected boolean is_called_locally_in_nontail_position ; boolean mustHaveFun ( ) { return exported || is_called_locally_in_tail_position || is_anon_fun ; } @ Override public String toString ( ) { return ( may_return_tail_marker ? "<STR_LIT:T>" : "<STR_LIT:->" ) + ( is_pausable ? "<STR_LIT>" : "<STR_LIT:->" ) + "<STR_LIT:U+0020>" + name ; } void addCaller ( FunInfo caller ) { callers . add ( caller ) ; } void addTailCaller ( FunInfo caller ) { tail_callers . add ( caller ) ; } @ Override public int hashCode ( ) { return name . hashCode ( ) ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof FunInfo ) { FunInfo fi = ( FunInfo ) obj ; return name . equals ( fi . name ) ; } return false ; } } void propagate ( ) { while ( propagate_one ( ) ) ; } boolean propagate_one ( ) { boolean effect = false ; for ( FunInfo fun : info . values ( ) ) { if ( fun . is_pausable || fun . call_is_pausable ) { for ( FunInfo caller : fun . callers ) { if ( ! caller . is_pausable ) { effect = caller . is_pausable = true ; if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + fun + "<STR_LIT>" + caller ) ; } } } for ( FunInfo caller : fun . tail_callers ) { if ( ! caller . call_is_pausable ) { effect = caller . call_is_pausable = true ; if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + fun + "<STR_LIT>" + caller ) ; } } } } } return effect ; } FunInfo get ( Label label ) { FunInfo fi = info . get ( label ) ; if ( fi == null ) { fi = new FunInfo ( ) ; info . put ( label , fi ) ; } return fi ; } FunInfo get ( EAtom fun , int arity , Label label ) { FunInfo fi = get ( label ) ; fi . name = new FunID ( name , fun , arity ) ; return fi ; } private EAtom name ; @ Override public void visitAttribute ( EAtom att , EObject value ) { } @ Override public void visitExport ( EAtom fun , int arity , int entry ) { Label label = new Label ( entry ) ; FunInfo fi = get ( label ) ; fi . exported = true ; } @ Override public void visitEnd ( ) { propagate ( ) ; if ( log . isLoggable ( Level . FINE ) ) { for ( Map . Entry < Label , FunInfo > e : info . entrySet ( ) ) { log . fine ( e . getValue ( ) . toString ( ) ) ; } } } @ Override public void declareFunction ( EAtom fun , int arity , int startLabel ) { Label label = new Label ( startLabel ) ; get ( fun , arity , label ) ; } @ Override public FunctionVisitor visitFunction ( EAtom name , int arity , final int startLabel ) { if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + ModuleAnalyzer . this . name + "<STR_LIT::>" + name + "<STR_LIT:/>" + arity ) ; } Label start = new Label ( startLabel ) ; final FunInfo self = get ( name , arity , start ) ; return new FunctionVisitor ( ) { @ Override public BlockVisitor visitLabeledBlock ( int label ) { return new BlockVisitor ( ) { @ Override public void visitInsn ( Insn insn ) { BeamOpcode op ; switch ( op = insn . opcode ( ) ) { case send : { if ( log . isLoggable ( Level . FINE ) && ! self . is_pausable ) { log . fine ( "<STR_LIT>" ) ; } self . is_pausable = true ; break ; } case call : { Insn . IL cl = ( Insn . IL ) insn ; FunInfo target = get ( cl . label ) ; BuiltInFunction bif = BIFUtil . getMethod ( target . name . module , target . name . function , target . name . arity , false , false ) ; if ( bif == null ) { target . addCaller ( self ) ; target . is_called_locally_in_nontail_position = true ; } break ; } case call_last : { ILI cl = ( Insn . ILI ) insn ; boolean is_self_call = cl . label . nr == startLabel ; self . may_return_tail_marker |= ! is_self_call ; FunInfo target = get ( cl . label ) ; BuiltInFunction bif = BIFUtil . getMethod ( target . name . module , target . name . function , target . name . arity , false , false ) ; if ( bif == null ) { target . addTailCaller ( self ) ; target . is_called_locally_in_tail_position |= ! is_self_call ; } break ; } case call_only : { IL cl = ( Insn . IL ) insn ; boolean is_self_call = cl . label . nr == startLabel ; self . may_return_tail_marker |= ! is_self_call ; FunInfo target = get ( cl . label ) ; BuiltInFunction bif = BIFUtil . getMethod ( target . name . module , target . name . function , target . name . arity , false , false ) ; if ( bif == null ) { target . addTailCaller ( self ) ; target . is_called_locally_in_tail_position |= ! is_self_call ; } break ; } case make_fun2 : { Insn . F fi = ( Insn . F ) insn ; Label anon = new Label ( fi . anon_fun . label ) ; get ( anon ) . is_anon_fun = true ; break ; } case apply_last : case i_call_fun_last : { if ( log . isLoggable ( Level . FINE ) && ! self . call_is_pausable ) { log . fine ( "<STR_LIT>" + op ) ; } self . may_return_tail_marker = true ; self . call_is_pausable = true ; break ; } case call_ext_last : case call_ext_only : { Insn . IE e = ( IE ) insn ; String mod = e . ext_fun . mod . getName ( ) ; String fun = e . ext_fun . fun . getName ( ) ; int arity = e . ext_fun . arity ; if ( mod . equals ( "<STR_LIT>" ) && arity == <NUM_LIT:1> && ( fun . equals ( "<STR_LIT>" ) || fun . equals ( "<STR_LIT:error>" ) || fun . equals ( "<STR_LIT>" ) ) ) { break ; } } if ( log . isLoggable ( Level . FINE ) && ! self . call_is_pausable ) { log . fine ( "<STR_LIT>" + op ) ; } self . may_return_tail_marker = true ; self . call_is_pausable = true ; case call_ext : if ( self . is_pausable ) { break ; } Insn . IE e = ( IE ) insn ; String mod = e . ext_fun . mod . getName ( ) ; String fun = e . ext_fun . fun . getName ( ) ; int arity = e . ext_fun . arity ; BuiltInFunction bif = BIFUtil . getMethod ( mod , fun , arity , false , false ) ; if ( bif != null ) { if ( log . isLoggable ( Level . FINE ) && ! self . is_pausable && bif . isPausable ( ) ) { log . fine ( "<STR_LIT>" + bif . javaMethod ) ; } self . is_pausable |= bif . isPausable ( ) ; } else if ( op == BeamOpcode . call_ext ) { if ( log . isLoggable ( Level . FINE ) && ! self . is_pausable ) { log . fine ( "<STR_LIT>" + mod + "<STR_LIT::>" + fun + "<STR_LIT:/>" + arity ) ; } self . is_pausable = true ; } break ; case apply : case call_fun : case wait : case wait_timeout : if ( log . isLoggable ( Level . FINE ) && ! self . is_pausable ) { log . fine ( "<STR_LIT>" + op ) ; } self . is_pausable = true ; break ; case bif : case bif0 : case bif1 : case bif2 : { if ( self . is_pausable ) break ; Insn . Bif bi = ( Insn . Bif ) insn ; EAtom name = bi . ext_fun . fun ; SourceOperand [ ] srcs = bi . args ; bif = BIFUtil . getMethod ( "<STR_LIT>" , name . getName ( ) , srcs . length , false , true ) ; if ( bif == null ) { throw new Error ( "<STR_LIT>" + bi . ext_fun ) ; } self . is_pausable |= bif . isPausable ( ) ; if ( log . isLoggable ( Level . FINE ) && self . is_pausable ) { log . fine ( "<STR_LIT>" + bif . javaMethod ) ; } } default : break ; } } @ Override public void visitEnd ( ) { } } ; } @ Override public void visitEnd ( ) { } } ; } @ Override public void visitModule ( EAtom name ) { this . name = name ; } public Map < FunID , FunInfo > getFunInfos ( ) { Map < FunID , FunInfo > res = new HashMap < FunID , FunInfo > ( ) ; for ( FunInfo fi : info . values ( ) ) { res . put ( fi . name , fi ) ; } return res ; } } </s>
|
<s> package erjang . beam ; import org . objectweb . asm . Type ; import erjang . EObject ; public class Arg { public enum Kind { X , Y , F , IMMEDIATE , EXT_FUNC ; public boolean isReg ( ) { return this == X || this == Y || this == F ; } } public static final Arg [ ] NO_ARGS = new Arg [ <NUM_LIT:0> ] ; public final Kind kind ; int no ; public Type type ; EObject value ; @ Override public String toString ( ) { return kind . name ( ) + "<STR_LIT>" + no + "<STR_LIT>" + String . valueOf ( value ) + "<STR_LIT>" + String . valueOf ( type ) + "<STR_LIT:}>" ; } public Arg ( Kind kind , int reg ) { this ( kind , reg , kind == Kind . F ? Type . DOUBLE_TYPE : null ) ; } public Arg ( Kind kind , int reg , Type type ) { if ( kind == Kind . IMMEDIATE ) { throw new IllegalArgumentException ( ) ; } this . kind = kind ; this . no = reg ; this . type = type ; } public Arg ( EObject value ) { this . kind = Kind . IMMEDIATE ; this . value = value ; this . type = Type . getType ( value . getClass ( ) ) ; } public Arg ( EObject value , Type type ) { this . kind = Kind . IMMEDIATE ; this . value = value ; this . type = type ; } public Arg ( Arg arg1 , Type type ) { this . kind = arg1 . kind ; this . no = arg1 . no ; this . value = arg1 . value ; this . type = type ; } @ Override public boolean equals ( Object obj ) { if ( obj instanceof Arg ) { Arg arg = ( Arg ) obj ; return kind == arg . kind && no == arg . no && value == arg . value ; } return false ; } } </s>
|
<s> package erjang . beam ; public class FunctionAdapter implements FunctionVisitor { protected final FunctionVisitor fv ; public FunctionAdapter ( FunctionVisitor fv ) { this . fv = fv ; } public void visitEnd ( ) { fv . visitEnd ( ) ; } public BlockVisitor visitLabeledBlock ( int label ) { return fv . visitLabeledBlock ( label ) ; } } </s>
|
<s> package erjang . beam ; import java . io . File ; import java . io . FileOutputStream ; import java . io . IOException ; public class DirClassRepo implements ClassRepo { private File dir ; public DirClassRepo ( File dir ) { this . dir = dir ; dir . mkdirs ( ) ; } @ Override public void store ( String internalName , byte [ ] data ) throws IOException { File out = new File ( dir , internalName + "<STR_LIT:.class>" ) ; File out_dir = out . getParentFile ( ) ; out_dir . mkdirs ( ) ; FileOutputStream fo = new FileOutputStream ( out ) ; fo . write ( data ) ; fo . close ( ) ; } @ Override public void close ( ) throws IOException { } } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; import erjang . EList ; import erjang . EObject ; import erjang . ERT ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . NotImplemented ; import erjang . beam . repr . CodeTables ; import erjang . beam . repr . Insn ; public class SymbolicBeamFileData implements BeamFileData { private static final EAtom BEAM_FILE = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom MODULE = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom EXPORTS = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom ATTRIBUTES = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom COMP_INFO = EAtom . intern ( "<STR_LIT>" ) ; private static final EAtom CODE = EAtom . intern ( "<STR_LIT:code>" ) ; private EAtom module ; private ESeq exports ; private ESeq attributes ; private ESeq comp_info ; private ESeq code ; public SymbolicBeamFileData ( ETuple data ) { assert ( data . elm ( <NUM_LIT:1> ) == BEAM_FILE ) ; module = data . elm ( <NUM_LIT:2> ) . testAtom ( ) ; exports = data . elm ( <NUM_LIT:3> ) . testSeq ( ) ; attributes = data . elm ( <NUM_LIT:4> ) . testSeq ( ) ; comp_info = data . elm ( <NUM_LIT:5> ) . testSeq ( ) ; code = data . elm ( <NUM_LIT:6> ) . testSeq ( ) ; } public void accept ( ModuleVisitor v ) { v . visitModule ( module ) ; visit_exports ( v ) ; visit_attributes ( v ) ; try { for ( ESeq exp = ( ESeq ) code ; exp != ERT . NIL ; exp = exp . tail ( ) ) { ETuple fun = ( ETuple ) exp . head ( ) ; visit_function ( v , fun ) ; } } finally { v . visitEnd ( ) ; } } private void visit_function ( ModuleVisitor v , ETuple fun ) { EAtom name = ( EAtom ) fun . elm ( <NUM_LIT:2> ) ; int ary = fun . elm ( <NUM_LIT:3> ) . asInt ( ) ; int entry = fun . elm ( <NUM_LIT:4> ) . asInt ( ) ; EList insns = ( EList ) fun . elm ( <NUM_LIT:5> ) ; FunctionVisitor fv = v . visitFunction ( name , ary , entry ) ; visit_insns ( insns , fv ) ; fv . visitEnd ( ) ; } private void visit_insns ( EList insns , FunctionVisitor fv ) { throw new NotImplemented ( ) ; } private void visit_attributes ( ModuleVisitor v ) { for ( ESeq exp = ( ESeq ) attributes ; exp != ERT . NIL ; exp = exp . tail ( ) ) { ETuple one = ( ETuple ) exp . head ( ) ; v . visitAttribute ( ( EAtom ) one . elm ( <NUM_LIT:1> ) , one . elm ( <NUM_LIT:2> ) ) ; } } private void visit_exports ( ModuleVisitor v ) { for ( ESeq exp = ( ESeq ) exports ; exp != ERT . NIL ; exp = exp . tail ( ) ) { ETuple one = ( ETuple ) exp . head ( ) ; v . visitExport ( ( EAtom ) one . elm ( <NUM_LIT:1> ) , one . elm ( <NUM_LIT:2> ) . asInt ( ) , one . elm ( <NUM_LIT:3> ) . asInt ( ) ) ; } } } </s>
|
<s> package erjang . beam . analysis ; import java . util . HashMap ; import java . util . Map ; import java . util . Set ; import java . util . logging . Logger ; import org . objectweb . asm . Type ; import com . trifork . clj_ds . IPersistentMap ; import com . trifork . clj_ds . PersistentHashMap ; import erjang . EObject ; import erjang . ETuple ; class TypeMap { private static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private static Type TYPE_EOBJECT = Type . getType ( EObject . class ) ; private static Type TYPE_ETUPLE = Type . getType ( ETuple . class ) ; private static String ETUPLE_NAME = TYPE_ETUPLE . getInternalName ( ) ; private static Type [ ] NO_TYPES = new Type [ <NUM_LIT:0> ] ; private static IPersistentMap NO_XTYPES = PersistentHashMap . EMPTY ; private final IPersistentMap xregs ; private final Type [ ] yregs , fregs ; final int stacksize ; final BasicBlock bb ; final ExceptionHandler exh ; public TypeMap ( BasicBlock bb ) { xregs = NO_XTYPES ; yregs = NO_TYPES ; fregs = NO_TYPES ; stacksize = <NUM_LIT:0> ; this . bb = bb ; exh = null ; } public String toString ( ) { StringBuilder sb = new StringBuilder ( "<STR_LIT:[>" ) ; boolean first = true ; IPersistentMap xregs = this . xregs ; first = printRegs ( sb , first , ( Map < Integer , Type > ) xregs , '<CHAR_LIT>' ) ; first = printRegs ( sb , first , fregs , '<CHAR_LIT>' ) ; first = printYRegs ( sb , first ) ; sb . append ( "<STR_LIT>" + stacksize ) ; sb . append ( '<CHAR_LIT:]>' ) ; return sb . toString ( ) ; } private boolean printRegs ( StringBuilder sb , boolean first , Type [ ] regs , char reg ) { for ( int i = <NUM_LIT:0> ; i < regs . length ; i ++ ) { if ( regs [ i ] != null ) { if ( first == false ) { sb . append ( "<STR_LIT:U+002CU+0020>" ) ; } else { first = false ; } sb . append ( reg ) . append ( i ) . append ( '<CHAR_LIT::>' ) ; sb . append ( shortName ( regs [ i ] ) ) ; } } return first ; } private boolean printRegs ( StringBuilder sb , boolean first , Map < Integer , Type > regs , char reg ) { for ( int i : regs . keySet ( ) ) { if ( regs . get ( i ) != null ) { if ( first == false ) { sb . append ( "<STR_LIT:U+002CU+0020>" ) ; } else { first = false ; } sb . append ( reg ) . append ( i ) . append ( '<CHAR_LIT::>' ) ; sb . append ( shortName ( regs . get ( i ) ) ) ; } } return first ; } private boolean printYRegs ( StringBuilder sb , boolean first ) { for ( int i = <NUM_LIT:0> ; i < stacksize ; i ++ ) { if ( i >= yregs . length ) continue ; if ( yregs [ i ] != null ) { if ( first == false ) { sb . append ( "<STR_LIT:U+002CU+0020>" ) ; } else { first = false ; } sb . append ( '<CHAR_LIT>' ) . append ( get_ypos ( i ) ) . append ( '<CHAR_LIT::>' ) ; sb . append ( shortName ( yregs [ i ] ) ) ; } } return first ; } private String shortName ( Type type ) { if ( type . getSort ( ) != Type . OBJECT ) { return type . getDescriptor ( ) ; } String in = type . getInternalName ( ) ; if ( type . getSort ( ) == Type . OBJECT ) { int idx = in . lastIndexOf ( '<CHAR_LIT:/>' ) ; String sh = in . substring ( idx + <NUM_LIT:1> ) ; if ( sh . length ( ) > <NUM_LIT:6> && sh . startsWith ( "<STR_LIT>" ) ) { return "<STR_LIT:T>" + sh . substring ( <NUM_LIT:6> ) ; } else { return sh . substring ( <NUM_LIT:1> , <NUM_LIT:3> ) ; } } else { return in ; } } private TypeMap ( IPersistentMap xregs , Type [ ] yregs , Type [ ] fregs , int stacksize , BasicBlock bb , ExceptionHandler exh ) { super ( ) ; this . xregs = xregs ; this . yregs = yregs ; this . fregs = fregs ; this . stacksize = stacksize ; this . bb = bb ; this . exh = exh ; if ( stacksize > <NUM_LIT> ) { log . warning ( "<STR_LIT>" ) ; } } public boolean equals ( Object obj ) { if ( obj instanceof TypeMap ) { TypeMap other = ( TypeMap ) obj ; return eq ( ( Map ) xregs , ( Map ) other . xregs ) && eqy ( this , other ) && eq ( fregs , other . fregs ) && eq ( exh , other . exh ) ; } return false ; } private static boolean eqy ( TypeMap me , TypeMap other ) { if ( me . stacksize != other . stacksize ) return false ; return eqy_prefix ( me , other , Math . min ( me . stacksize , other . stacksize ) ) ; } private static boolean eqy_prefix ( TypeMap me , TypeMap other , int count ) { for ( int i = <NUM_LIT:0> ; i < count ; i ++ ) { if ( ! eq ( me . gety ( i ) , other . gety ( i ) ) ) return false ; } return true ; } private boolean eq ( Map m1 , Map m2 ) { return m1 . equals ( m2 ) ; } private boolean eq ( Type [ ] r1 , Type [ ] r2 ) { int max = Math . max ( r1 . length , r2 . length ) ; for ( int i = <NUM_LIT:0> ; i < max ; i ++ ) { if ( ! eq ( get ( r1 , i ) , get ( r2 , i ) ) ) return false ; } return true ; } private static boolean eq ( Type t1 , Type t2 ) { if ( t1 == t2 ) return true ; if ( t1 == null || t2 == null ) return false ; return t1 . equals ( t2 ) ; } private static boolean eq ( ExceptionHandler e1 , ExceptionHandler e2 ) { return ( e1 == e2 ) || ( e1 != null && e1 . equals ( e2 ) ) ; } public TypeMap mergeFrom ( TypeMap other ) { IPersistentMap new_x = eq ( ( Map ) xregs , ( Map ) other . xregs ) ? xregs : merge_regs ( xregs , other . xregs ) ; Type [ ] new_f = eq ( fregs , other . fregs ) ? fregs : merge_regs ( fregs , other . fregs ) ; Type [ ] new_y = yregs ; int new_stacksize = Math . min ( stacksize , other . stacksize ) ; if ( ! eqy_prefix ( this , other , new_stacksize ) ) { new_y = new Type [ new_stacksize ] ; boolean differs_from_my_y = false ; for ( int i = <NUM_LIT:0> ; i < new_stacksize ; i ++ ) { Type t1 ; Type res = merge ( t1 = this . gety ( i ) , other . gety ( i ) ) ; new_y [ new_stacksize - i - <NUM_LIT:1> ] = res ; differs_from_my_y = differs_from_my_y || ( res != t1 && ( res == null || ! res . equals ( t1 ) ) ) ; } if ( ! differs_from_my_y ) new_y = yregs ; } ExceptionHandler new_exh ; try { new_exh = ExceptionHandler . merge ( exh , other . exh ) ; } catch ( IllegalArgumentException iae ) { new_exh = ExceptionHandler . Ambiguous . make ( exh , other . exh ) ; } if ( new_x == xregs && new_y == yregs && new_f == fregs && new_stacksize == stacksize && new_exh == exh ) { return this ; } else { return new TypeMap ( new_x , new_y , new_f , new_stacksize , bb , new_exh ) ; } } private Type [ ] merge_regs ( Type [ ] r1 , Type [ ] r2 ) { Type [ ] res = new Type [ Math . max ( r1 . length , r2 . length ) ] ; boolean differs_from_r1 = false ; for ( int i = <NUM_LIT:0> ; i < res . length ; i ++ ) { Type t1 = get ( r1 , i ) ; Type t2 = get ( r2 , i ) ; res [ i ] = merge ( t1 , t2 ) ; differs_from_r1 = differs_from_r1 || ( res [ i ] != t1 && ( res [ i ] == null || ! res [ i ] . equals ( t1 ) ) ) ; } assert ( eq ( r1 , res ) || differs_from_r1 ) ; return differs_from_r1 ? res : r1 ; } private IPersistentMap merge_regs ( IPersistentMap r1 , IPersistentMap r2 ) { IPersistentMap out = PersistentHashMap . EMPTY ; for ( Integer r : ( ( Map < Integer , Type > ) r1 ) . keySet ( ) ) { Type t1 = ( Type ) r1 . valAt ( r ) ; Type t2 = ( Type ) r2 . valAt ( r ) ; Type m = merge ( t1 , t2 ) ; if ( m != null ) { out = out . assoc ( r , m ) ; } } return out ; } private Type merge ( Type t1 , Type t2 ) { if ( t1 == null || t2 == null ) return null ; if ( t1 . equals ( t2 ) ) return t1 ; if ( t1 . getSort ( ) == Type . OBJECT && t2 . getSort ( ) == Type . OBJECT ) { if ( t1 . getInternalName ( ) . startsWith ( ETUPLE_NAME ) && t2 . getInternalName ( ) . startsWith ( ETUPLE_NAME ) ) { return TYPE_ETUPLE ; } } return TYPE_EOBJECT ; } private Type get ( Type [ ] regs , int i ) { if ( i < regs . length ) return regs [ i ] ; else return null ; } public TypeMap setx ( int reg , Type t , XRegMarker marker ) { marker . mark_xreg_as_used ( reg ) ; bb . kill_x ( reg ) ; if ( eq ( getx ( reg ) , t ) ) return this ; IPersistentMap new_xregs = xregs . assoc ( reg , t ) ; return new TypeMap ( new_xregs , yregs , fregs , stacksize , bb , exh ) ; } public TypeMap setf ( int reg , Type t ) { bb . kill_fr ( reg ) ; if ( eq ( getf ( reg ) , t ) ) return this ; Type [ ] new_fregs ; if ( fregs . length <= reg ) { new_fregs = grow ( fregs , reg ) ; } else { new_fregs = copy ( fregs ) ; } new_fregs [ reg ] = t ; return new TypeMap ( xregs , yregs , new_fregs , stacksize , bb , exh ) ; } private Type [ ] copy ( Type [ ] regs ) { Type [ ] res = new Type [ regs . length ] ; for ( int i = <NUM_LIT:0> ; i < regs . length ; i ++ ) { res [ i ] = regs [ i ] ; } return res ; } private static Type [ ] grow ( Type [ ] regs , int reg ) { Type [ ] res = new Type [ reg + <NUM_LIT:6> ] ; for ( int i = <NUM_LIT:0> ; i < regs . length ; i ++ ) { res [ i ] = regs [ i ] ; } return res ; } public Type getx ( int reg ) { bb . use_x ( reg ) ; return ( Type ) xregs . valAt ( reg , null ) ; } public Type getf ( int reg ) { bb . use_fr ( reg ) ; if ( reg >= fregs . length ) { return null ; } else { return fregs [ reg ] ; } } public TypeMap sety ( int reg , Type t ) { bb . kill_y ( this , reg ) ; if ( eq ( gety ( reg ) , t ) ) return this ; int pos = get_ypos ( reg ) ; if ( pos < <NUM_LIT:0> || pos >= stacksize ) throw new IllegalArgumentException ( "<STR_LIT>" + reg + "<STR_LIT>" ) ; Type [ ] new_yregs ; if ( yregs . length <= pos ) { new_yregs = grow ( yregs , pos ) ; } else { new_yregs = copy ( yregs ) ; } new_yregs [ pos ] = t ; return new TypeMap ( xregs , new_yregs , fregs , stacksize , bb , exh ) ; } public Type gety ( int reg ) { bb . use_y ( this , reg ) ; int pos = get_ypos ( reg ) ; if ( pos < <NUM_LIT:0> || pos >= stacksize ) throw new IllegalArgumentException ( "<STR_LIT>" + reg + "<STR_LIT>" ) ; if ( pos >= yregs . length ) return null ; return yregs [ pos ] ; } public int get_ypos ( int reg ) { if ( reg < <NUM_LIT:0> || reg >= stacksize ) throw new IllegalArgumentException ( "<STR_LIT>" + reg + "<STR_LIT>" ) ; return stacksize - reg - <NUM_LIT:1> ; } public TypeMap trim_y ( int howmuch ) { return new TypeMap ( xregs , yregs , fregs , stacksize - howmuch , bb , exh ) ; } public TypeMap alloc_y ( int howmuch ) { return new TypeMap ( xregs , yregs , fregs , stacksize + howmuch , bb , exh ) ; } public TypeMap clearLive ( BasicBlock bb ) { return new TypeMap ( xregs , yregs , fregs , stacksize , bb , exh ) ; } public TypeMap pushExceptionHandler ( int handlerLabel ) { ExceptionHandler new_exh = ExceptionHandler . push ( exh , handlerLabel ) ; return new TypeMap ( xregs , yregs , fregs , stacksize , bb , new_exh ) ; } public TypeMap popExceptionHandler ( ) { ExceptionHandler new_exh = ( exh instanceof ExceptionHandler . Ambiguous ) ? null : ExceptionHandler . pop ( exh ) ; return new TypeMap ( xregs , yregs , fregs , stacksize , bb , new_exh ) ; } public void add_succ ( BasicBlock succ ) { bb . succ ( succ ) ; } public void touchx ( int from , int to ) { for ( int i = from ; i < to ; i ++ ) { bb . use_x ( i ) ; } } public Set < Integer > allXregs ( ) { return ( ( Map ) xregs ) . keySet ( ) ; } public int max_freg ( ) { int max = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < fregs . length ; i ++ ) { if ( fregs [ i ] != null ) max = i ; } return max + <NUM_LIT:1> ; } interface XRegMarker { void mark_xreg_as_used ( int reg ) ; } } </s>
|
<s> package erjang . beam . analysis ; import erjang . beam . BeamExceptionHandler ; import erjang . ErlangException ; import java . util . Set ; import java . util . HashSet ; import java . util . List ; import java . util . ArrayList ; public class ExceptionHandler implements BeamExceptionHandler { public static final BeamExceptionHandler NULL = new BeamExceptionHandler ( ) { public int getHandlerLabel ( ) { throw new InternalError ( ) ; } public BeamExceptionHandler getParent ( ) { throw new InternalError ( ) ; } public String toString ( ) { return "<STR_LIT>" ; } } ; private final int handlerLabel ; private final ExceptionHandler parent ; public ExceptionHandler ( int handlerLabel , ExceptionHandler parent ) { this . handlerLabel = handlerLabel ; this . parent = parent ; } public int getHandlerLabel ( ) { return handlerLabel ; } public ExceptionHandler getParent ( ) { return parent ; } public List < BeamExceptionHandler > ambiguousities ( ) { return null ; } public static ExceptionHandler push ( ExceptionHandler org , int newHandlerLabel ) { assert ( ! ( org instanceof Ambiguous ) ) ; return new ExceptionHandler ( newHandlerLabel , org ) ; } public static ExceptionHandler pop ( ExceptionHandler org ) { if ( org == null ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; assert ( ! ( org instanceof Ambiguous ) ) ; return org . parent ; } public static ExceptionHandler merge ( ExceptionHandler e1 , ExceptionHandler e2 ) throws ErlangException { if ( e1 == e2 ) return e1 ; if ( e1 == null || e2 == null || e1 . handlerLabel != e2 . handlerLabel ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; ExceptionHandler mergedParent = merge ( e1 . parent , e2 . parent ) ; if ( mergedParent == e1 . parent ) return e1 ; if ( mergedParent == e2 . parent ) return e2 ; return new ExceptionHandler ( e1 . handlerLabel , mergedParent ) ; } public boolean equals ( Object other ) { return other instanceof ExceptionHandler && equals ( ( ExceptionHandler ) other ) ; } public boolean equals ( ExceptionHandler other ) { return equals ( this , other ) ; } public static boolean equals ( ExceptionHandler e1 , ExceptionHandler e2 ) { if ( e1 == e2 ) return true ; if ( e1 == null || e2 == null ) return false ; if ( e1 . handlerLabel != e2 . handlerLabel ) return false ; if ( e1 instanceof Ambiguous && e2 instanceof Ambiguous ) { Ambiguous a1 = ( Ambiguous ) e1 ; Ambiguous a2 = ( Ambiguous ) e2 ; return a1 . exhs . equals ( a2 . exhs ) ; } if ( e1 instanceof Ambiguous || e2 instanceof Ambiguous ) return false ; return equals ( e1 . parent , e2 . parent ) ; } public int hashCode ( ) { return handlerLabel ; } public int compareTo ( Object other ) { if ( ! ( other instanceof ExceptionHandler ) ) return <NUM_LIT:1> ; return compareTo ( ( ExceptionHandler ) other ) ; } public int compareTo ( ExceptionHandler other ) { return this . handlerLabel - other . handlerLabel ; } public String toString ( ) { return "<STR_LIT>" + handlerLabel + "<STR_LIT:+>" + parent ; } public static class Ambiguous extends ExceptionHandler { final Set < BeamExceptionHandler > exhs ; public Ambiguous ( ExceptionHandler src1 , ExceptionHandler src2 ) { super ( - <NUM_LIT:1> , null ) ; exhs = new HashSet ( ) ; add_to_set ( exhs , src1 ) ; add_to_set ( exhs , src2 ) ; } public Ambiguous ( Set < BeamExceptionHandler > exhs ) { super ( - <NUM_LIT:1> , null ) ; this . exhs = exhs ; } public static Ambiguous make ( ExceptionHandler src1 , ExceptionHandler src2 ) { HashSet < BeamExceptionHandler > exhs = new HashSet ( ) ; add_to_set ( exhs , src1 ) ; add_to_set ( exhs , src2 ) ; Ambiguous a ; if ( src1 instanceof Ambiguous && exhs . equals ( ( a = ( Ambiguous ) src1 ) . exhs ) ) return a ; if ( src2 instanceof Ambiguous && exhs . equals ( ( a = ( Ambiguous ) src2 ) . exhs ) ) return a ; return new Ambiguous ( exhs ) ; } private static void add_to_set ( Set < BeamExceptionHandler > exhs , ExceptionHandler src ) { if ( src instanceof Ambiguous ) exhs . addAll ( ( ( Ambiguous ) src ) . exhs ) ; else if ( src == null ) exhs . add ( NULL ) ; else exhs . add ( src ) ; } public int hashCode ( ) { return exhs . hashCode ( ) ; } public List < BeamExceptionHandler > ambiguousities ( ) { List < BeamExceptionHandler > result = new ArrayList < BeamExceptionHandler > ( exhs . size ( ) ) ; for ( BeamExceptionHandler x : exhs ) result . add ( x == NULL ? null : x ) ; return result ; } public String toString ( ) { StringBuffer res = new StringBuffer ( "<STR_LIT>" ) ; for ( BeamExceptionHandler e : exhs ) { res . append ( e ) . append ( "<STR_LIT:U+0020>" ) ; } res . append ( "<STR_LIT:)>" ) ; return res . toString ( ) ; } } } </s>
|
<s> package erjang . beam . analysis ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collection ; import java . util . Comparator ; import java . util . HashSet ; import java . util . List ; import java . util . Map ; import java . util . Set ; import java . util . SortedSet ; import java . util . TreeMap ; import java . util . TreeSet ; import java . util . logging . Level ; import java . util . logging . Logger ; import org . objectweb . asm . Type ; import erjang . EAtom ; import erjang . EBig ; import erjang . EBinMatchState ; import erjang . EBinary ; import erjang . EBitString ; import erjang . ECons ; import erjang . EDouble ; import erjang . EFun ; import erjang . EInteger ; import erjang . EList ; import erjang . EString ; import erjang . ENil ; import erjang . ENumber ; import erjang . EObject ; import erjang . EPID ; import erjang . EPort ; import erjang . ERT ; import erjang . ERef ; import erjang . ESeq ; import erjang . ESmall ; import erjang . ETuple ; import erjang . ETuple2 ; import erjang . beam . Arg ; import erjang . beam . BIFUtil ; import erjang . beam . BeamCodeBlock ; import erjang . beam . BeamFunction ; import erjang . beam . BeamInstruction ; import erjang . beam . BeamOpcode ; import erjang . beam . BeamExceptionHandler ; import erjang . beam . BlockVisitor ; import erjang . beam . BlockVisitor2 ; import erjang . beam . BuiltInFunction ; import erjang . beam . ExtFunc ; import erjang . beam . FunctionAdapter ; import erjang . beam . FunctionVisitor ; import erjang . beam . FunctionVisitor2 ; import erjang . beam . ModuleAdapter ; import erjang . beam . ModuleVisitor ; import erjang . beam . Arg . Kind ; import erjang . beam . repr . Insn ; import erjang . beam . repr . ExtFun ; import erjang . beam . repr . Operands ; import erjang . beam . repr . Operands . Int ; import erjang . beam . repr . Operands . XReg ; import static erjang . beam . repr . Operands . SourceOperand ; import static erjang . beam . repr . Operands . DestinationOperand ; import static erjang . beam . CodeAtoms . * ; public class BeamTypeAnalysis extends ModuleAdapter { static final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; public BeamTypeAnalysis ( ModuleVisitor mv ) { super ( mv ) ; } static final Type ESMALL_TYPE = Type . getType ( ESmall . class ) ; static final Type EBIG_TYPE = Type . getType ( EBig . class ) ; static final Type EINTEGER_TYPE = Type . getType ( EInteger . class ) ; static final Type ENUMBER_TYPE = Type . getType ( ENumber . class ) ; static final Type EOBJECT_TYPE = Type . getType ( EObject . class ) ; static final Type EDOUBLE_TYPE = Type . getType ( EDouble . class ) ; static final Type ENIL_TYPE = Type . getType ( ENil . class ) ; static final Type EATOM_TYPE = Type . getType ( EAtom . class ) ; static final Type ETUPLE_TYPE = Type . getType ( ETuple . class ) ; static final Type EBINARY_TYPE = Type . getType ( EBinary . class ) ; static final Type EBITSTRING_TYPE = Type . getType ( EBitString . class ) ; static final Type ECONS_TYPE = Type . getType ( ECons . class ) ; static final Type ESEQ_TYPE = Type . getType ( ESeq . class ) ; static final Type ELIST_TYPE = Type . getType ( EList . class ) ; static final Type EFUN_TYPE = Type . getType ( EFun . class ) ; static final Type EPID_TYPE = Type . getType ( EPID . class ) ; static final Type EPORT_TYPE = Type . getType ( EPort . class ) ; static final Type EREFERENCE_TYPE = Type . getType ( ERef . class ) ; static final Type EMATCHSTATE_TYPE = Type . getType ( EBinMatchState . class ) ; static final EAtom am_plus = EAtom . intern ( "<STR_LIT:+>" ) ; static final EAtom am_minus = EAtom . intern ( "<STR_LIT:->" ) ; private static final ETuple X0_REG = ETuple . make ( new EObject [ ] { X_ATOM , new ESmall ( <NUM_LIT:0> ) } ) ; private EAtom moduleName ; private List < FV > functions = new ArrayList < FV > ( ) ; @ Override public void declareFunction ( EAtom fun , int arity , int label ) { } @ Override public FunctionVisitor visitFunction ( EAtom name , int arity , int startLabel ) { FV f = new FV ( super . visitFunction ( name , arity , startLabel ) , name , arity , startLabel ) ; functions . add ( f ) ; return f ; } class FV extends FunctionAdapter implements BeamFunction , TypeMap . XRegMarker { BasicBlock makeBasicBlock ( int label , int index ) { assert ( ( label & <NUM_LIT> ) == label ) ; assert ( ( index & <NUM_LIT> ) == index ) ; int bbk = ( label << <NUM_LIT:16> ) | index ; BasicBlock bb = bbs . get ( bbk ) ; if ( bb == null ) { bbs . put ( bbk , bb = new BasicBlock ( label , index ) ) ; } return bb ; } TreeMap < Integer , BasicBlock > bbs = new TreeMap < Integer , BasicBlock > ( ) ; TreeMap < Integer , List < BeamExceptionHandler > > blocks_with_ambiguous_exh = new TreeMap < Integer , List < BeamExceptionHandler > > ( ) ; void live_analysis ( ) { Integer [ ] all = bbs . keySet ( ) . toArray ( new Integer [ bbs . size ( ) ] ) ; Arrays . sort ( all ) ; boolean change = false ; int iter = <NUM_LIT:0> ; do { iter += <NUM_LIT:1> ; for ( int n = all . length - <NUM_LIT:1> ; n >= <NUM_LIT:0> ; n -- ) { BasicBlock bb = bbs . get ( all [ n ] ) ; TreeSet < Integer > inq = bb . in ; TreeSet < Integer > outq = bb . out ; TreeSet < Integer > in_n = new TreeSet < Integer > ( ) ; in_n . addAll ( outq ) ; in_n . removeAll ( bb . kill ) ; in_n . addAll ( bb . use ) ; bb . in = in_n ; TreeSet < Integer > out_n = new TreeSet < Integer > ( ) ; for ( BasicBlock s : bb . succ ) { TreeSet < Integer > in_s = s . in ; if ( in_s != null ) { out_n . addAll ( in_s ) ; } } bb . out = out_n ; change = ( ! inq . equals ( in_n ) ) || ( ! outq . equals ( out_n ) ) ; } } while ( change ) ; } Map < Integer , LabeledBlock > lbs = new TreeMap < Integer , LabeledBlock > ( ) ; private final EAtom name ; private final int arity ; private final int startLabel ; private SortedSet < LabeledBlock > needs_analyze = new TreeSet < LabeledBlock > ( new Comparator < LabeledBlock > ( ) { @ Override public int compare ( LabeledBlock o1 , LabeledBlock o2 ) { return o2 . block_label - o1 . block_label ; } } ) ; public int max_stack ; public HashSet < Integer > all_xregs = new HashSet ( ) ; public int max_freg ; private boolean is_tail_recursive ; public FV ( FunctionVisitor fv , EAtom name , int arity , int startLabel ) { super ( fv ) ; this . name = name ; this . arity = arity ; this . startLabel = startLabel ; } @ Override public void visitEnd ( ) { LabeledBlock lb = lbs . get ( startLabel ) ; lb . merge_from ( this . make_initial ( ) ) ; try { while ( ! needs_analyze . isEmpty ( ) ) { lb = needs_analyze . first ( ) ; needs_analyze . remove ( lb ) ; lb . analyze ( ) ; } } catch ( RuntimeException t ) { dump ( ) ; throw t ; } catch ( Error t ) { dump ( ) ; throw t ; } SortedSet < Integer > labels = new TreeSet < Integer > ( ) ; labels . addAll ( lbs . keySet ( ) ) ; for ( int i : labels ) { lb = lbs . get ( i ) ; if ( lb . initial == null ) continue ; ExceptionHandler e = lb . initial . exh ; if ( e == null ) continue ; List < BeamExceptionHandler > ambi = e . ambiguousities ( ) ; if ( ambi != null ) { blocks_with_ambiguous_exh . put ( i , ambi ) ; } } boolean has_unreachable_code = false ; for ( int i : labels ) { lb = lbs . get ( i ) ; if ( lb . isDeadCode ( ) ) { if ( lb . insns . get ( <NUM_LIT:0> ) . opcode ( ) == BeamOpcode . func_info ) { } else { log . fine ( "<STR_LIT>" + lb . block_label ) ; has_unreachable_code = true ; } } } if ( has_unreachable_code ) { this . dump ( ) ; } function_visit_end ( ) ; } private void function_visit_end ( ) { if ( fv instanceof FunctionVisitor2 ) { ( ( FunctionVisitor2 ) fv ) . visitMaxs ( ( Collection < Integer > ) this . all_xregs , this . max_stack , this . max_freg , this . is_tail_recursive ) ; } for ( LabeledBlock block : this . lbs . values ( ) ) { ExceptionHandler block_exh = block . initial == null ? null : block . initial . exh ; assert ( blocks_with_ambiguous_exh . containsKey ( block . block_label ) == ( block_exh != null && block_exh . ambiguousities ( ) != null ) ) ; if ( block_exh != null && block_exh . ambiguousities ( ) != null ) { List < BeamExceptionHandler > ambi = blocks_with_ambiguous_exh . get ( block . block_label ) ; for ( BeamExceptionHandler e : ambi ) { int ext_label = extendedLabel ( block . block_label , e ) ; function_visit_end_aux ( block , ext_label , e ) ; } } else { int ext_label = extendedLabel ( block . block_label , block_exh ) ; function_visit_end_aux ( block , ext_label , block_exh ) ; } } super . fv . visitEnd ( ) ; } private void function_visit_end_aux ( LabeledBlock block , int ext_label , BeamExceptionHandler exh ) { BlockVisitor vis = super . fv . visitLabeledBlock ( ext_label ) ; try { block . accept ( vis , exh ) ; } catch ( Error e ) { dump ( ) ; throw e ; } } private void dump ( ) { if ( log . isLoggable ( Level . FINER ) ) { log . finer ( "<STR_LIT>" + name + "<STR_LIT:/>" + arity ) ; for ( Map . Entry < Integer , LabeledBlock > ent : lbs . entrySet ( ) ) { ent . getValue ( ) . dump ( ) ; } } } @ Override public BlockVisitor visitLabeledBlock ( int label ) { return get_lb ( label , true ) ; } private TypeMap make_initial ( ) { TypeMap res = new TypeMap ( makeBasicBlock ( startLabel , <NUM_LIT:0> ) ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { res = res . setx ( i , EOBJECT_TYPE , this ) ; } return res ; } private LabeledBlock get_lb ( int label , boolean create ) { if ( lbs . containsKey ( label ) ) { LabeledBlock res = lbs . get ( label ) ; return res ; } else if ( create ) { LabeledBlock res = new LabeledBlock ( label ) ; lbs . put ( label , res ) ; return res ; } else { return null ; } } int extendedLabel ( int label , BeamExceptionHandler exh ) { assert ( ( label & ~ <NUM_LIT> ) == <NUM_LIT:0> ) ; int extLabel = label ; if ( blocks_with_ambiguous_exh . containsKey ( label ) ) { if ( exh != null ) { assert ( ! blocks_with_ambiguous_exh . containsKey ( exh . getHandlerLabel ( ) ) ) ; int handlerLabel = exh . getHandlerLabel ( ) ; assert ( ( handlerLabel & ~ <NUM_LIT> ) == <NUM_LIT:0> ) ; extLabel |= ( handlerLabel << <NUM_LIT:16> ) ; } } return extLabel ; } public void mark_xreg_as_used ( int reg ) { all_xregs . add ( reg ) ; } private void update_max_regs ( TypeMap current ) { max_stack = Math . max ( max_stack , current . stacksize ) ; max_freg = Math . max ( max_freg , current . max_freg ( ) ) ; } class LabeledBlock implements BlockVisitor , BeamCodeBlock { private final int block_label ; TypeMap initial ; boolean last = false ; TypeMap [ ] map ; public LabeledBlock ( int label ) { this . block_label = label ; initial = null ; } public void accept ( BlockVisitor vis , BeamExceptionHandler exh ) { try { if ( ! isDeadCode ( ) ) { if ( vis instanceof BlockVisitor2 ) { accept_2 ( ( BlockVisitor2 ) vis , exh ) ; } else { accept_1 ( vis ) ; } } } finally { vis . visitEnd ( ) ; } } private void accept_1 ( BlockVisitor vis ) { throw new erjang . NotImplemented ( ) ; } private void accept_2 ( BlockVisitor2 vis , BeamExceptionHandler exh ) { int tuple_pos = <NUM_LIT:0> ; Arg tuple_reg = null ; vis . visitBegin ( exh ) ; for ( int insn_idx = <NUM_LIT:0> ; insn_idx < insns . size ( ) ; insn_idx ++ ) { Insn insn_ = insns . get ( insn_idx ) ; BeamOpcode opcode = insn_ . opcode ( ) ; TypeMap type_map = this . map [ insn_idx ] ; switch ( opcode ) { case func_info : { Insn . AAI insn = ( Insn . AAI ) insn_ ; vis . visitInsn ( opcode , insn . getExtFun ( ) ) ; break ; } case fconv : case fmove : case move : { Insn . SD insn = ( Insn . SD ) insn_ ; Arg src = src_arg ( insn_idx , insn . src ) ; Arg dest = dest_arg ( insn_idx , insn . dest ) ; if ( dest . kind != Kind . F ) { if ( src . kind == Kind . F ) { dest = new Arg ( dest , EDOUBLE_TYPE ) ; } else { dest = new Arg ( dest , src . type ) ; } } else { } vis . visitInsn ( opcode , src , dest ) ; break ; } case put_string : { Insn . ByD insn = ( Insn . ByD ) insn_ ; Arg src = src_arg ( insn_idx , insn . bin ) ; Arg dest = dest_arg ( insn_idx , insn . dest ) ; dest = new Arg ( dest , ESEQ_TYPE ) ; vis . visitInsn ( BeamOpcode . move , src , dest ) ; break ; } case fadd : case fsub : case fmul : case fdiv : { Insn . LSSD insn = ( Insn . LSSD ) insn_ ; EAtom name = opcode . symbol ; int failLabel = decode_labelref ( insn . label , type_map . exh ) ; Arg [ ] in = new Arg [ ] { src_arg ( insn_idx , insn . src1 ) , src_arg ( insn_idx , insn . src2 ) } ; Type [ ] inTypes = new Type [ ] { in [ <NUM_LIT:0> ] . type , in [ <NUM_LIT:1> ] . type } ; Arg out = dest_arg ( insn_idx , insn . dest ) ; BuiltInFunction bif = BIFUtil . getMethod ( "<STR_LIT>" , name . getName ( ) , inTypes , failLabel != <NUM_LIT:0> , true ) ; vis . visitInsn ( opcode , failLabel , in , out , bif ) ; break ; } case bif0 : case bif1 : case bif2 : { Insn . Bif insn = ( Insn . Bif ) insn_ ; EAtom name = insn . ext_fun . fun ; int failLabel = decode_labelref ( insn . label , type_map . exh ) ; SourceOperand [ ] srcs = insn . args ; Arg [ ] in = src_args ( insn_idx , srcs ) ; Arg out = dest_arg ( insn_idx , insn . dest ) ; BuiltInFunction bif = BIFUtil . getMethod ( "<STR_LIT>" , name . getName ( ) , parmTypes ( type_map , srcs ) , failLabel != <NUM_LIT:0> , true ) ; vis . visitInsn ( opcode , failLabel , in , out , bif ) ; break ; } case gc_bif1 : case gc_bif2 : { Insn . GcBif insn = ( Insn . GcBif ) insn_ ; EAtom name = insn . ext_fun . fun ; int failLabel = decode_labelref ( insn . label , type_map . exh ) ; SourceOperand [ ] srcs = insn . args ; Arg [ ] in = src_args ( insn_idx , srcs ) ; Arg out = dest_arg ( insn_idx , insn . dest ) ; Int lop = null , rop = null ; if ( srcs . length == <NUM_LIT:2> && ( ( ( name == am_plus || name == am_minus ) && ( rop = srcs [ <NUM_LIT:1> ] . testInt ( ) ) != null && rop . equals ( <NUM_LIT:1> ) ) || ( name == am_plus && ( lop = srcs [ <NUM_LIT:0> ] . testInt ( ) ) != null && lop . equals ( <NUM_LIT:1> ) ) ) ) { if ( name == am_plus ) { Arg src = ( lop == null ) ? in [ <NUM_LIT:0> ] : in [ <NUM_LIT:1> ] ; vis . visitIncrement ( src , out ) ; break ; } else if ( name == am_minus ) { Arg src = in [ <NUM_LIT:0> ] ; vis . visitDecrement ( src , out ) ; break ; } } BuiltInFunction bif = BIFUtil . getMethod ( "<STR_LIT>" , name . getName ( ) , parmTypes ( type_map , srcs ) , failLabel != <NUM_LIT:0> , true ) ; vis . visitInsn ( opcode , failLabel , in , out , bif ) ; break ; } case is_tuple : { if ( insn_idx + <NUM_LIT:1> < insns . size ( ) ) { Insn next_insn = insns . get ( insn_idx + <NUM_LIT:1> ) ; if ( next_insn . opcode ( ) == BeamOpcode . test_arity ) { int this_fail = decode_labelref ( ( ( Insn . L ) insn_ ) . label , this . map [ insn_idx ] . exh ) ; int next_fail = decode_labelref ( ( ( Insn . L ) next_insn ) . label , this . map [ insn_idx + <NUM_LIT:1> ] . exh ) ; if ( this_fail == next_fail ) { Arg this_arg = src_arg ( insn_idx , ( ( Insn . LD ) insn_ ) . dest ) ; Arg next_arg = src_arg ( insn_idx + <NUM_LIT:1> , ( ( Insn . LD ) next_insn ) . dest ) ; if ( this_arg . equals ( next_arg ) ) { break ; } } } } } case is_integer : case is_float : case is_number : case is_atom : case is_pid : case is_reference : case is_port : case is_nil : case is_binary : case is_list : case is_nonempty_list : case is_function : case is_boolean : case is_bitstr : case test_arity : case bs_test_tail2 : case bs_test_unit : case is_lt : case is_ge : case is_eq : case is_ne : case is_eq_exact : case is_ne_exact : case is_function2 : case bs_match_string : case bs_skip_utf8 : case bs_skip_utf16 : case bs_skip_utf32 : case bs_start_match2 : case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : case bs_skip_bits2 : case bs_get_integer2 : case bs_get_float2 : case bs_get_binary2 : accept_2_test ( vis , ( Insn . L ) insn_ , insn_idx ) ; break ; case K_return : vis . visitInsn ( opcode , new Arg ( Arg . Kind . X , <NUM_LIT:0> , type_map . getx ( <NUM_LIT:0> ) ) ) ; break ; case allocate_heap_zero : case allocate_zero : { Insn . I insn = ( Insn . I ) insn_ ; int depth = type_map . stacksize ; int count = insn . i1 ; Arg [ ] ys = new Arg [ count ] ; for ( int i = <NUM_LIT:0> ; i < count ; i ++ ) { ys [ i ] = new Arg ( Arg . Kind . Y , depth + i , null ) ; } vis . visitInsn ( opcode , ( Arg [ ] ) ys ) ; break ; } case test_heap : break ; case fclearerror : case fcheckerror : break ; case recv_mark : case recv_set : break ; case call_ext_last : case call_ext_only : do_call ( vis , insn_idx , ( Insn . I ) insn_ , true , true ) ; break ; case call_ext : do_call ( vis , insn_idx , ( Insn . I ) insn_ , false , true ) ; if ( is_exceptional_call ( insn_ ) ) { vis . visitUnreachablePoint ( ) ; } break ; case call : do_call ( vis , insn_idx , ( Insn . I ) insn_ , false , false ) ; break ; case call_last : case call_only : do_call ( vis , insn_idx , ( Insn . I ) insn_ , true , false ) ; break ; case apply_last : case apply : { Insn . I insn = ( Insn . I ) insn_ ; Arg [ ] args = new Arg [ <NUM_LIT:2> + insn . i1 ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { args [ i ] = new Arg ( Arg . Kind . X , i , map [ insn_idx ] . getx ( i ) ) ; } vis . visitInsn ( opcode , args ) ; break ; } case make_fun2 : { Insn . F insn = ( Insn . F ) insn_ ; ExtFun efun = insn . anon_fun . asExtFun ( ) ; int numfree = insn . anon_fun . free_vars ; int index = insn . anon_fun . index ; int old_index = insn . anon_fun . old_index ; EBinary uniq = insn . anon_fun . mod_md5 ; int old_uniq = insn . anon_fun . old_uniq ; Arg [ ] free = new Arg [ numfree ] ; for ( int i = <NUM_LIT:0> ; i < numfree ; i ++ ) { free [ i ] = new Arg ( Arg . Kind . X , i , map [ insn_idx ] . getx ( i ) ) ; } vis . visitInsn ( opcode , efun , free , index , old_index , uniq , old_uniq ) ; break ; } case init : { Insn . D insn = ( Insn . D ) insn_ ; vis . visitInsn ( opcode , dest_arg ( insn_idx , insn . dest ) ) ; break ; } case put_list : { Insn . SSD insn = ( Insn . SSD ) insn_ ; Arg [ ] in = new Arg [ ] { src_arg ( insn_idx , insn . src1 ) , src_arg ( insn_idx , insn . src2 ) } ; Arg out = dest_arg ( insn_idx , insn . dest ) ; vis . visitInsn ( opcode , in , out ) ; break ; } case put_tuple : { Insn . ID insn = ( Insn . ID ) insn_ ; int arity = insn . i1 ; tuple_reg = new Arg ( dest_arg ( insn_idx , insn . dest ) , getTupleType ( arity ) ) ; vis . visitInsn ( opcode , arity , tuple_reg ) ; tuple_pos = <NUM_LIT:1> ; break ; } case put : { Insn . S insn = ( Insn . S ) insn_ ; Arg val = src_arg ( insn_idx , insn . src ) ; vis . visitInsn ( opcode , val , tuple_reg , tuple_pos ++ ) ; break ; } case set_tuple_element : { Insn . SDI insn = ( Insn . SDI ) insn_ ; Arg in = src_arg ( insn_idx , insn . src ) ; Arg out = src_arg ( insn_idx , insn . dest ) ; int idx = insn . i ; vis . visitInsn ( opcode , in , out , idx ) ; break ; } case allocate_heap : case allocate : case deallocate : { break ; } case select_tuple_arity : { Insn . Select insn = ( Insn . Select ) insn_ ; int failLabel = decode_labelref ( insn . defaultLabel , type_map . exh ) ; Arg in = src_arg ( insn_idx , insn . src ) ; Operands . SelectList jumpTable = insn . jumpTable ; int len = jumpTable . size ( ) ; int [ ] arities = new int [ len ] ; int [ ] targets = new int [ len ] ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { Operands . Operand value = jumpTable . getValue ( i ) ; Operands . Label target = jumpTable . getLabel ( i ) ; arities [ i ] = value . asCodeInt ( ) . value ; targets [ i ] = decode_labelref ( target , type_map . exh ) ; } vis . visitSelectTuple ( in , failLabel , arities , targets ) ; break ; } case select_val : { Insn . Select insn = ( Insn . Select ) insn_ ; int failLabel = decode_labelref ( insn . defaultLabel , type_map . exh ) ; Arg in = src_arg ( insn_idx , insn . src ) ; Operands . SelectList jumpTable = insn . jumpTable ; int len = jumpTable . size ( ) ; Arg [ ] values = new Arg [ len ] ; int [ ] targets = new int [ len ] ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { Operands . Operand value = jumpTable . getValue ( i ) ; Operands . Label target = jumpTable . getLabel ( i ) ; values [ i ] = arg ( value . asLiteral ( ) ) ; targets [ i ] = decode_labelref ( target , type_map . exh ) ; } vis . visitSelectValue ( in , failLabel , values , targets ) ; break ; } case get_tuple_element : { Insn . SID insn = ( Insn . SID ) insn_ ; Arg in = src_arg ( insn_idx , insn . src ) ; int idx = insn . i ; Arg out = dest_arg ( insn_idx , insn . dest ) ; vis . visitInsn ( opcode , in , out , idx ) ; break ; } case jump : { Insn . L insn = ( Insn . L ) insn_ ; vis . visitJump ( decode_labelref ( insn . label , type_map . exh ) ) ; break ; } case on_load : case trim : break ; case get_list : { Insn . SDD insn = ( Insn . SDD ) insn_ ; vis . visitInsn ( opcode , new Arg [ ] { src_arg ( insn_idx , insn . src ) , dest_arg ( insn_idx , insn . dest1 ) , dest_arg ( insn_idx , insn . dest2 ) } ) ; break ; } case try_case_end : case badmatch : case case_end : { Insn . S insn = ( Insn . S ) insn_ ; vis . visitInsn ( opcode , src_arg ( insn_idx , insn . src ) ) ; break ; } case if_end : vis . visitInsn ( opcode ) ; break ; case send : { vis . visitInsn ( opcode , new Arg [ ] { new Arg ( Arg . Kind . X , <NUM_LIT:0> ) , new Arg ( Arg . Kind . X , <NUM_LIT:1> ) } ) ; break ; } case K_try : case K_catch : { Insn . YL insn = ( Insn . YL ) insn_ ; TypeMap type_map_after = this . map [ insn_idx + <NUM_LIT:1> ] ; vis . visitCatchBlockStart ( opcode , decode_labelref ( insn . label , type_map . exh ) , src_arg ( insn_idx , insn . y ) , type_map_after . exh ) ; break ; } case raise : { Insn . SS insn = ( Insn . SS ) insn_ ; Arg [ ] in = { src_arg ( insn_idx , insn . src1 ) , src_arg ( insn_idx , insn . src2 ) } ; Arg ex = new Arg ( Arg . Kind . X , <NUM_LIT:0> ) ; int failLabel = <NUM_LIT:0> ; vis . visitInsn ( opcode , failLabel , in , ex ) ; break ; } case try_end : case try_case : case catch_end : { Insn . Y insn = ( Insn . Y ) insn_ ; vis . visitCatchBlockEnd ( opcode , src_arg ( insn_idx , insn . y ) , type_map . exh ) ; break ; } case loop_rec : { Insn . LD insn = ( Insn . LD ) insn_ ; vis . visitReceive ( opcode , decode_labelref ( insn . label , type_map . exh ) , dest_arg ( insn_idx , insn . dest ) ) ; break ; } case remove_message : case timeout : vis . visitInsn ( opcode ) ; break ; case loop_rec_end : case wait : { Insn . L insn = ( Insn . L ) insn_ ; vis . visitInsn ( opcode , decode_labelref ( insn . label , type_map . exh ) , null ) ; break ; } case wait_timeout : { Insn . LS insn = ( Insn . LS ) insn_ ; vis . visitInsn ( opcode , decode_labelref ( insn . label , type_map . exh ) , src_arg ( insn_idx , insn . src ) ) ; break ; } case call_fun : case i_call_fun_last : { Insn . I insn = ( Insn . I ) insn_ ; int nargs = insn . i1 ; Arg [ ] args = new Arg [ nargs + <NUM_LIT:1> ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { args [ i ] = new Arg ( Arg . Kind . X , i , map [ insn_idx ] . getx ( i ) ) ; } vis . visitInsn ( opcode , args , new Arg ( Arg . Kind . X , <NUM_LIT:0> , null ) ) ; break ; } case bs_add : { Insn . LSSID insn = ( Insn . LSSID ) insn_ ; vis . visitBSAdd ( src_arg ( insn_idx , insn . src1 ) , src_arg ( insn_idx , insn . src2 ) , insn . i3 , dest_arg ( insn_idx , insn . dest ) ) ; break ; } case bs_context_to_binary : { Insn . D insn = ( Insn . D ) insn_ ; vis . visitBS ( opcode , dest_arg ( insn_idx , insn . dest ) , null ) ; break ; } case bs_restore2 : case bs_save2 : { Insn . DI insn = ( Insn . DI ) insn_ ; vis . visitBS ( opcode , src_arg ( insn_idx , insn . dest ) , insn . i2 == - <NUM_LIT:1> ? new Arg ( EAtom . intern ( "<STR_LIT:start>" ) ) : new Arg ( new ESmall ( insn . i2 ) ) ) ; break ; } case bs_init_writable : { Arg size = src_arg ( insn_idx , new Operands . XReg ( <NUM_LIT:0> ) ) ; Arg dest = dest_arg ( new Operands . XReg ( <NUM_LIT:0> ) ) ; vis . visitInitWritable ( size , dest ) ; break ; } case bs_init2 : case bs_init_bits : { Insn . LSIIID insn = ( Insn . LSIIID ) insn_ ; Arg size = src_arg ( insn_idx , insn . src2 ) ; int flags = insn . i5 ; Arg out = dest_arg ( insn_idx , insn . dest ) ; boolean unit_is_bits = ( opcode == BeamOpcode . bs_init_bits ) ; vis . visitInitBitString ( size , flags , out , unit_is_bits ) ; break ; } case bs_put_string : { Insn . By insn = ( Insn . By ) insn_ ; Arg str = arg ( insn . bin ) ; vis . visitBitStringPut ( opcode , str , null , - <NUM_LIT:1> , - <NUM_LIT:1> ) ; break ; } case bs_put_binary : case bs_put_integer : case bs_put_float : { Insn . LSIIS insn = ( Insn . LSIIS ) insn_ ; Arg size = src_arg ( insn_idx , insn . src2 ) ; int unit = insn . i3 ; int flags = insn . i4 ; Arg value = src_arg ( insn_idx , insn . src5 ) ; vis . visitBitStringPut ( opcode , value , size , unit , flags ) ; break ; } case bs_put_utf8 : case bs_put_utf16 : case bs_put_utf32 : { Insn . LIS insn = ( Insn . LIS ) insn_ ; int flags = insn . i2 ; Arg value = src_arg ( insn_idx , insn . src ) ; vis . visitBitStringPut ( opcode , value , null , - <NUM_LIT:1> , flags ) ; break ; } case bs_utf8_size : case bs_utf16_size : { Insn . LSD insn = ( Insn . LSD ) insn_ ; Arg value = src_arg ( insn_idx , insn . src ) ; Arg out = dest_arg ( insn_idx , insn . dest ) ; vis . visitBS ( opcode , value , out ) ; break ; } case bs_private_append : { Insn . BSPrivateAppend insn = ( Insn . BSPrivateAppend ) insn_ ; Arg extra_size = src_arg ( insn_idx , insn . src2 ) ; Arg src = src_arg ( insn_idx , insn . src4 ) ; int unit = insn . i3 ; int flags = insn . i5 ; Arg dst = dest_arg ( insn_idx , insn . dest ) ; vis . visitBitStringAppend ( opcode , decode_labelref ( insn . label , type_map . exh ) , extra_size , src , unit , flags , dst ) ; break ; } case bs_append : { Insn . BSAppend insn = ( Insn . BSAppend ) insn_ ; Arg extra_size = src_arg ( insn_idx , insn . src2 ) ; Arg src = src_arg ( insn_idx , insn . src6 ) ; int unit = insn . i5 ; int flags = insn . i7 ; Arg dst = dest_arg ( insn_idx , insn . dest8 ) ; vis . visitBitStringAppend ( opcode , decode_labelref ( insn . label , type_map . exh ) , extra_size , src , unit , flags , dst ) ; break ; } default : throw new Error ( "<STR_LIT>" + insn_ . toSymbolicTuple ( ) ) ; } } } private void do_call ( BlockVisitor2 vis , int insn_idx , Insn . I insn , boolean is_tail , boolean is_external ) throws Error { int arg_count = insn . i1 ; Arg [ ] args = new Arg [ arg_count ] ; for ( int i = <NUM_LIT:0> ; i < arg_count ; i ++ ) { args [ i ] = new Arg ( Kind . X , i , this . map [ insn_idx ] . getx ( i ) ) ; } ExtFun fun ; if ( insn instanceof Insn . IE ) { fun = ( ( Insn . IE ) insn ) . ext_fun ; } else if ( insn instanceof Insn . IL ) { fun = ( ( Insn . IL ) insn ) . functionAtLabel . asExtFun ( ) ; } else throw new Error ( "<STR_LIT>" + insn ) ; vis . visitCall ( fun , args , is_tail , is_external ) ; } private void accept_2_test ( BlockVisitor2 vis , Insn . L insn_ , int insn_idx ) { TypeMap typeMap = this . map [ insn_idx ] ; int failLabel = decode_labelref ( insn_ . label , typeMap . exh ) ; BeamOpcode test = insn_ . opcode ( ) ; if ( insn_ instanceof Insn . LD ) { Insn . LD insn = ( Insn . LD ) insn_ ; Arg arg = src_arg ( insn_idx , insn . dest ) ; Type test_type = type_tested_for ( insn ) ; if ( test_type != null ) { if ( insn_ . opcode ( ) == BeamOpcode . is_nonempty_list || ! test_type . equals ( arg . type ) ) vis . visitTest ( test , failLabel , arg , test_type ) ; return ; } } switch ( test ) { case is_function2 : { Insn . LDS insn = ( Insn . LDS ) insn_ ; vis . visitTest ( test , failLabel , dest_arg ( insn_idx , insn . dest ) , src_arg ( insn_idx , insn . src ) , EFUN_TYPE ) ; break ; } case is_eq_exact : case is_lt : case is_ge : case is_ne_exact : case is_ne : case is_eq : { Insn . LSS insn = ( Insn . LSS ) insn_ ; Arg [ ] args = new Arg [ ] { src_arg ( insn_idx , insn . src1 ) , src_arg ( insn_idx , insn . src2 ) } ; Type outType = Type . VOID_TYPE ; Type t1 = getType ( typeMap , insn . src1 ) ; Type t2 = getType ( typeMap , insn . src2 ) ; if ( t1 . equals ( EOBJECT_TYPE ) && ! t2 . equals ( EOBJECT_TYPE ) ) { outType = t2 ; } if ( t2 . equals ( EOBJECT_TYPE ) && ! t1 . equals ( EOBJECT_TYPE ) ) { outType = t1 ; } vis . visitTest ( test , failLabel , args , outType ) ; break ; } case test_arity : { Insn . LDI insn = ( Insn . LDI ) insn_ ; int arity = insn . i ; Arg reg = src_arg ( insn_idx , insn . dest ) ; vis . visitTest ( test , failLabel , reg , arity , getTupleType ( arity ) ) ; break ; } case bs_start_match2 : case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : { Insn . LDIID insn = ( Insn . LDIID ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , insn . i4 , dest_arg ( insn_idx , insn . dest5 ) ) ; break ; } case bs_match_string : { Insn . LDBi insn = ( Insn . LDBi ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , insn . bin . value ) ; break ; } case bs_get_integer2 : case bs_get_float2 : case bs_get_binary2 : { Insn . LDISIID insn = ( Insn . LDISIID ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , src_arg ( insn_idx , insn . src4 ) , insn . i5 , insn . i6 , dest_arg ( insn_idx , insn . dest7 ) ) ; break ; } case bs_skip_bits2 : { Insn . LDSII insn = ( Insn . LDSII ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , src_arg ( insn_idx , insn . src3 ) , insn . i4 , insn . i5 ) ; break ; } case bs_test_unit : case bs_test_tail2 : { Insn . LDI insn = ( Insn . LDI ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , insn . i ) ; break ; } case bs_skip_utf8 : case bs_skip_utf16 : case bs_skip_utf32 : { Insn . LDII insn = ( Insn . LDII ) insn_ ; vis . visitBitStringTest ( test , failLabel , src_arg ( insn_idx , insn . dest ) , insn . i4 ) ; break ; } default : throw new Error ( "<STR_LIT>" + insn_ . toSymbolic ( ) + "<STR_LIT>" + insn_idx + "<STR_LIT>" + test + "<STR_LIT::>" + test . ordinal ( ) ) ; } } private Arg [ ] decode_args ( int insn_idx , EObject [ ] input ) { Arg [ ] output = new Arg [ input . length ] ; for ( int i = <NUM_LIT:0> ; i < input . length ; i ++ ) { output [ i ] = decode_arg ( insn_idx , input [ i ] ) ; } return output ; } private Arg decode_arg ( int insn_idx , EObject src ) { TypeMap current = this . map [ insn_idx ] ; if ( src instanceof ETuple2 ) { ETuple2 tup = ( ETuple2 ) src ; if ( tup . elem1 == X_ATOM ) { int xreg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . X , xreg , current . getx ( xreg ) ) ; } else if ( tup . elem1 == Y_ATOM ) { int yreg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . Y , current . get_ypos ( yreg ) , current . gety ( yreg ) ) ; } else if ( tup . elem1 == FR_ATOM ) { int freg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . F , freg , Type . DOUBLE_TYPE ) ; } else if ( tup . elem1 == ATOM_ATOM ) { return new Arg ( tup . elem2 , EATOM_TYPE ) ; } else if ( tup . elem1 == LITERAL_ATOM ) { return new Arg ( tup . elem2 ) ; } else if ( tup . elem1 == STRING_ATOM ) { return new Arg ( tup . elem2 ) ; } else if ( tup . elem1 == INTEGER_ATOM ) { return new Arg ( tup . elem2 ) ; } else if ( tup . elem1 == FLOAT_ATOM ) { return new Arg ( tup . elem2 , Type . DOUBLE_TYPE ) ; } } else if ( src == NIL_ATOM ) { return new Arg ( ERT . NIL , ENIL_TYPE ) ; } return new Arg ( src ) ; } private Arg [ ] src_args ( int insn_idx , SourceOperand [ ] args ) { Arg [ ] res = new Arg [ args . length ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { res [ i ] = src_arg ( insn_idx , args [ i ] ) ; } return res ; } private Arg src_arg ( int insn_idx , SourceOperand src ) { TypeMap current = this . map [ insn_idx ] ; if ( src instanceof Operands . XReg ) return src_arg ( ( Operands . XReg ) src , current ) ; if ( src instanceof Operands . YReg ) return src_arg ( ( Operands . YReg ) src , current ) ; if ( src instanceof Operands . FReg ) return arg ( ( Operands . FReg ) src ) ; if ( src instanceof Operands . Float ) return arg ( ( Operands . Float ) src ) ; if ( src instanceof Operands . Literal ) return arg ( ( Operands . Literal ) src ) ; throw new Error ( "<STR_LIT>" + src . toSymbolic ( ) ) ; } private Arg dest_arg ( int insn_idx , DestinationOperand dest ) { TypeMap current = this . map [ insn_idx ] ; if ( dest instanceof Operands . XReg ) return dest_arg ( ( Operands . XReg ) dest ) ; if ( dest instanceof Operands . YReg ) return dest_arg ( ( Operands . YReg ) dest , current ) ; if ( dest instanceof Operands . FReg ) return arg ( ( Operands . FReg ) dest ) ; throw new Error ( "<STR_LIT>" + dest . toSymbolic ( ) ) ; } private Arg src_arg ( Operands . XReg xreg , TypeMap tm ) { return new Arg ( Kind . X , xreg . nr , tm . getx ( xreg . nr ) ) ; } private Arg src_arg ( Operands . YReg yreg , TypeMap tm ) { return new Arg ( Kind . Y , tm . get_ypos ( yreg . nr ) , tm . gety ( yreg . nr ) ) ; } private Arg dest_arg ( Operands . XReg xreg ) { return new Arg ( Kind . X , xreg . nr ) ; } private Arg dest_arg ( Operands . YReg yreg , TypeMap tm ) { return new Arg ( Kind . Y , tm . get_ypos ( yreg . nr ) ) ; } private Arg arg ( Operands . FReg freg ) { return new Arg ( Kind . F , freg . nr ) ; } private Arg arg ( Operands . Float flt ) { return new Arg ( flt . literalValue ( ) , Type . DOUBLE_TYPE ) ; } private Arg arg ( Operands . Literal lit ) { return new Arg ( lit . literalValue ( ) ) ; } private Arg decode_value ( EObject src ) { if ( src instanceof ETuple2 ) { ETuple2 tup = ( ETuple2 ) src ; if ( tup . elem1 == ATOM_ATOM ) { return new Arg ( tup . elem2 , EATOM_TYPE ) ; } else if ( tup . elem1 == LITERAL_ATOM ) { return new Arg ( tup . elem2 ) ; } else if ( tup . elem1 == INTEGER_ATOM ) { return new Arg ( tup . elem2 ) ; } else if ( tup . elem1 == FLOAT_ATOM ) { return new Arg ( tup . elem2 ) ; } } else if ( src == NIL_ATOM ) { return new Arg ( ERT . NIL , ENIL_TYPE ) ; } throw new Error ( "<STR_LIT>" + src ) ; } private Arg decode_out_arg ( int insn_idx , EObject src ) { TypeMap current = this . map [ insn_idx ] ; if ( src instanceof ETuple2 ) { ETuple2 tup = ( ETuple2 ) src ; if ( tup . elem1 == X_ATOM ) { int xreg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . X , xreg ) ; } else if ( tup . elem1 == Y_ATOM ) { int yreg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . Y , current . get_ypos ( yreg ) ) ; } else if ( tup . elem1 == FR_ATOM ) { int freg = tup . elem2 . asInt ( ) ; return new Arg ( Arg . Kind . F , freg ) ; } } throw new Error ( ) ; } private int decode_labelref ( EObject f_tup , ExceptionHandler exh ) { if ( f_tup == NOFAIL_ATOM ) return <NUM_LIT:0> ; assert ( f_tup . testTuple ( ) . elm ( <NUM_LIT:1> ) == F_ATOM ) ; return extendedLabel ( f_tup . testTuple ( ) . elm ( <NUM_LIT:2> ) . asInt ( ) , exh ) ; } private int decode_labelref ( Operands . Label label , ExceptionHandler exh ) { if ( label == null ) return <NUM_LIT:0> ; return extendedLabel ( label . nr , exh ) ; } public boolean isDeadCode ( ) { return initial == null ; } public void analyze ( ) { try { analyze0 ( ) ; } catch ( RuntimeException x ) { throw x ; } } private void dump ( ) { if ( ! log . isLoggable ( Level . FINER ) ) return ; next_insn : for ( int i = <NUM_LIT:0> ; i < insns . size ( ) ; i ++ ) { log . finer ( name + "<STR_LIT:(>" + block_label + "<STR_LIT>" + i + "<STR_LIT>" + ( map == null ? "<STR_LIT:?>" : map [ i ] ) ) ; log . finer ( "<STR_LIT>" + insns . get ( i ) ) ; } } public void analyze0 ( ) { TypeMap current = initial ; BeamOpcode last_opcode = BeamOpcode . NONE ; Insn last_insn = null ; map = new TypeMap [ insns . size ( ) ] ; next_insn : for ( int insn_idx = <NUM_LIT:0> ; insn_idx < insns . size ( ) ; insn_idx ++ ) { update_max_regs ( current ) ; if ( is_term ( last_opcode ) ) { throw new Error ( "<STR_LIT>" + this . block_label + "<STR_LIT::>" + insn_idx ) ; } map [ insn_idx ] = current ; Insn insn_ = insns . get ( insn_idx ) ; BeamOpcode code = insn_ . opcode ( ) ; last_opcode = code ; last_insn = insn_ ; if ( current . exh != null && may_terminate_exceptionally ( code ) ) addExceptionEdge ( current ) ; switch ( code ) { case fmove : case move : { Insn . SD insn = ( Insn . SD ) insn_ ; SourceOperand src = insn . src ; DestinationOperand dst = insn . dest ; Type srcType = getType ( current , src ) ; Type dstType = srcType ; if ( dst . testFReg ( ) != null ) { dstType = Type . DOUBLE_TYPE ; } else if ( sizeof ( current , src ) > sizeof ( current , dst ) ) { if ( srcType . equals ( Type . DOUBLE_TYPE ) ) { dstType = EDOUBLE_TYPE ; } else { throw new Error ( "<STR_LIT>" + insn . toSymbolic ( ) + "<STR_LIT>" + getType ( current , src ) ) ; } } current = setType ( current , dst , dstType ) ; continue next_insn ; } case put_string : { Insn . ByD insn = ( Insn . ByD ) insn_ ; DestinationOperand dst = insn . dest ; current = setType ( current , dst , ESEQ_TYPE ) ; continue next_insn ; } case jump : { Insn . L insn = ( Insn . L ) insn_ ; current = branch ( current , insn . label , insn_idx ) ; continue next_insn ; } case send : { current . touchx ( <NUM_LIT:0> , <NUM_LIT:2> ) ; current = current . setx ( <NUM_LIT:0> , current . getx ( <NUM_LIT:1> ) , FV . this ) ; continue next_insn ; } case fadd : case fsub : case fmul : case fdiv : { Insn . LSSD insn = ( Insn . LSSD ) insn_ ; EAtom name = insn . opcode ( ) . symbol ; SourceOperand [ ] parms = new SourceOperand [ ] { insn . src1 , insn . src2 } ; Type type = getBifResult ( "<STR_LIT>" , name . getName ( ) , parmTypes ( current , parms ) , false ) ; current = setType ( current , insn . dest , type ) ; continue next_insn ; } case gc_bif1 : case gc_bif2 : { Insn . GcBif insn = ( Insn . GcBif ) insn_ ; boolean is_guard = ( insn . label . nr != <NUM_LIT:0> ) ; current = branch ( current , insn . label , insn_idx ) ; EAtom name = insn . ext_fun . fun ; SourceOperand [ ] parms = insn . argList ( ) ; Type type = getBifResult ( "<STR_LIT>" , name . getName ( ) , parmTypes ( current , parms ) , is_guard ) ; current = setType ( current , insn . dest , type ) ; continue next_insn ; } case bif0 : case bif1 : case bif2 : { Insn . Bif insn = ( Insn . Bif ) insn_ ; current = branch ( current , insn . label , insn_idx ) ; EAtom name = insn . ext_fun . fun ; SourceOperand [ ] parms = insn . argList ( ) ; Type type = getBifResult ( "<STR_LIT>" , name . getName ( ) , parmTypes ( current , parms ) , false ) ; current = setType ( current , insn . dest , type ) ; continue next_insn ; } case is_tuple : { if ( insn_idx + <NUM_LIT:1> < insns . size ( ) ) { Insn next_insn = insns . get ( insn_idx + <NUM_LIT:1> ) ; if ( next_insn . opcode ( ) == BeamOpcode . test_arity ) { if ( this . map [ insn_idx + <NUM_LIT:1> ] == null ) { this . map [ insn_idx + <NUM_LIT:1> ] = this . map [ insn_idx ] ; } int this_fail = decode_labelref ( ( ( Insn . L ) insn_ ) . label , this . map [ insn_idx ] . exh ) ; int next_fail = decode_labelref ( ( ( Insn . L ) next_insn ) . label , this . map [ insn_idx + <NUM_LIT:1> ] . exh ) ; if ( this_fail == next_fail ) { Arg this_arg = src_arg ( insn_idx , ( ( Insn . LD ) insn_ ) . dest ) ; Arg next_arg = src_arg ( insn_idx + <NUM_LIT:1> , ( ( Insn . LD ) next_insn ) . dest ) ; if ( this_arg . equals ( next_arg ) ) { continue next_insn ; } } } } } case is_integer : case is_float : case is_number : case is_atom : case is_pid : case is_reference : case is_port : case is_nil : case is_binary : case is_list : case is_nonempty_list : case is_function : case is_boolean : case is_bitstr : case test_arity : case bs_test_tail2 : case bs_test_unit : case is_lt : case is_ge : case is_eq : case is_ne : case is_eq_exact : case is_ne_exact : case is_function2 : case bs_match_string : case bs_skip_utf8 : case bs_skip_utf16 : case bs_skip_utf32 : case bs_start_match2 : case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : case bs_skip_bits2 : case bs_get_integer2 : case bs_get_float2 : case bs_get_binary2 : { try { current = analyze_test ( current , ( Insn . L ) insn_ , insn_idx ) ; } catch ( Error e ) { throw new Error ( "<STR_LIT>" + LabeledBlock . this . block_label + "<STR_LIT::>" + insn_idx , e ) ; } assert ( current != null ) ; continue next_insn ; } case fconv : { Insn . SD insn = ( Insn . SD ) insn_ ; getType ( current , insn . src ) ; current = setType ( current , insn . dest , Type . DOUBLE_TYPE ) ; continue next_insn ; } case init : { Insn . D insn = ( Insn . D ) insn_ ; current = setType ( current , insn . dest , ENIL_TYPE ) ; continue next_insn ; } case set_tuple_element : { Insn . SDI insn = ( Insn . SDI ) insn_ ; getType ( current , insn . src ) ; getType ( current , insn . dest ) ; continue next_insn ; } case get_tuple_element : { Insn . SID insn = ( Insn . SID ) insn_ ; getType ( current , insn . src ) ; current = setType ( current , insn . dest , EOBJECT_TYPE ) ; continue next_insn ; } case get_list : { Insn . SDD insn = ( Insn . SDD ) insn_ ; current = setType ( current , insn . dest1 , EOBJECT_TYPE ) ; Type list_type = getType ( current , insn . src ) ; Type tail_type = ( list_type == ELIST_TYPE || list_type == ESEQ_TYPE ) ? ESEQ_TYPE : EOBJECT_TYPE ; current = setType ( current , insn . dest2 , tail_type ) ; continue next_insn ; } case put_list : { Insn . SSD insn = ( Insn . SSD ) insn_ ; Type head_type = getType ( current , insn . src1 ) ; Type tail_type = getType ( current , insn . src2 ) ; if ( tail_type == null ) { throw new Error ( "<STR_LIT>" + insn . src2 . toSymbolic ( ) + "<STR_LIT>" ) ; } Type list_type = ( tail_type . equals ( ENIL_TYPE ) || tail_type . equals ( ESEQ_TYPE ) || tail_type . equals ( ELIST_TYPE ) ) ? ELIST_TYPE : ECONS_TYPE ; current = setType ( current , insn . dest , list_type ) ; continue next_insn ; } case put_tuple : { Insn . ID insn = ( Insn . ID ) insn_ ; int arity = insn . i1 ; current = setType ( current , insn . dest , getTupleType ( arity ) ) ; continue next_insn ; } case K_try : { Insn . YL insn = ( Insn . YL ) insn_ ; current = setType ( current , insn . y , EOBJECT_TYPE ) ; current = installExceptionHandler ( current , insn . label , insn_idx ) ; continue next_insn ; } case try_end : { current = current . popExceptionHandler ( ) ; continue next_insn ; } case try_case : { Insn . Y insn = ( Insn . Y ) insn_ ; getType ( current , insn . y ) ; current = current . popExceptionHandler ( ) ; current = current . setx ( <NUM_LIT:0> , EATOM_TYPE , FV . this ) ; current = current . setx ( <NUM_LIT:1> , EOBJECT_TYPE , FV . this ) ; current = current . setx ( <NUM_LIT:2> , EOBJECT_TYPE , FV . this ) ; continue next_insn ; } case try_case_end : continue next_insn ; case raise : { Insn . SS insn = ( Insn . SS ) insn_ ; checkArg ( current , insn . src1 ) ; checkArg ( current , insn . src2 ) ; current = setType ( current , Operands . XReg . get ( <NUM_LIT:0> ) , EOBJECT_TYPE ) ; continue next_insn ; } case K_catch : { Insn . YL insn = ( Insn . YL ) insn_ ; current = installExceptionHandler ( current , insn . label , insn_idx ) ; continue next_insn ; } case catch_end : { current = current . popExceptionHandler ( ) ; current = current . setx ( <NUM_LIT:0> , EOBJECT_TYPE , FV . this ) ; continue next_insn ; } case make_fun2 : { Insn . F insn = ( Insn . F ) insn_ ; current . touchx ( <NUM_LIT:0> , insn . anon_fun . free_vars ) ; current = current . setx ( <NUM_LIT:0> , EFUN_TYPE , FV . this ) ; continue next_insn ; } case loop_rec : { Insn . LD insn = ( Insn . LD ) insn_ ; current = branch ( current , insn . label , insn_idx ) ; current = setType ( current , insn . dest , EOBJECT_TYPE ) ; continue next_insn ; } case remove_message : continue next_insn ; case loop_rec_end : case timeout : { continue next_insn ; } case wait_timeout : { checkArg ( current , ( ( Insn . LS ) insn_ ) . src ) ; } case wait : { Insn . L insn = ( Insn . L ) insn_ ; current = branch ( current , insn . label , insn_idx ) ; continue next_insn ; } case deallocate : case trim : { Insn . I insn = ( Insn . I ) insn_ ; int howmuch = insn . i1 ; current = current . trim_y ( howmuch ) ; continue next_insn ; } case on_load : case test_heap : { continue next_insn ; } case allocate_zero : case allocate_heap_zero : { Insn . I insn = ( Insn . I ) insn_ ; int slots = insn . i1 ; current = current . alloc_y ( slots ) ; for ( int slot = <NUM_LIT:0> ; slot < slots ; slot ++ ) { current = current . sety ( slot , ENIL_TYPE ) ; } continue next_insn ; } case allocate : case allocate_heap : { Insn . I insn = ( Insn . I ) insn_ ; current = current . alloc_y ( insn . i1 ) ; continue next_insn ; } case fcheckerror : case fclearerror : continue next_insn ; case recv_mark : case recv_set : continue next_insn ; case put : { Insn . S insn = ( Insn . S ) insn_ ; checkArg ( current , insn . src ) ; continue next_insn ; } case select_tuple_arity : { Insn . Select insn = ( Insn . Select ) insn_ ; current = branch ( current , insn . defaultLabel , insn_idx ) ; checkArg ( current , insn . src ) ; DestinationOperand dest = insn . src . testDestination ( ) ; Operands . SelectList jumpTable = insn . jumpTable ; int len = jumpTable . size ( ) ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { Operands . Operand value = jumpTable . getValue ( i ) ; Operands . Label target = jumpTable . getLabel ( i ) ; if ( dest != null ) { int arity = value . asCodeInt ( ) . value ; current = setType ( current , dest , getTupleType ( arity ) ) ; } current = branch ( current , target , insn_idx ) ; } continue next_insn ; } case select_val : { Insn . Select insn = ( Insn . Select ) insn_ ; current = branch ( current , insn . defaultLabel , insn_idx ) ; checkArg ( current , insn . src ) ; Operands . SelectList jumpTable = insn . jumpTable ; int len = jumpTable . size ( ) ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { Operands . Label target = jumpTable . getLabel ( i ) ; current = branch ( current , target , insn_idx ) ; } continue next_insn ; } case apply : case call : case call_ext : { Insn . I insn = ( Insn . I ) insn_ ; int argCount = insn . i1 ; current . touchx ( <NUM_LIT:0> , argCount ) ; current = current . setx ( <NUM_LIT:0> , EOBJECT_TYPE , FV . this ) ; continue next_insn ; } case K_return : getType ( current , X0_REG ) ; continue next_insn ; case apply_last : case call_last : case call_only : case call_ext_last : case call_ext_only : { Insn . I insn = ( Insn . I ) insn_ ; int argCount = insn . i1 ; current . touchx ( <NUM_LIT:0> , argCount ) ; is_tail_recursive = true ; continue next_insn ; } case func_info : continue next_insn ; case if_end : case badmatch : case case_end : continue next_insn ; case bs_add : { Insn . LSSID insn = ( Insn . LSSID ) insn_ ; checkArg ( current , insn . src1 ) ; checkArg ( current , insn . src2 ) ; current = setType ( current , insn . dest , Type . INT_TYPE ) ; continue next_insn ; } case bs_context_to_binary : { Insn . D insn = ( Insn . D ) insn_ ; checkArg ( current , insn . dest ) ; current = current . setx ( <NUM_LIT:0> , EBINARY_TYPE , FV . this ) ; continue next_insn ; } case bs_save2 : { continue next_insn ; } case bs_restore2 : { Insn . DI insn = ( Insn . DI ) insn_ ; current = setType ( current , insn . dest , EMATCHSTATE_TYPE ) ; continue next_insn ; } case bs_init2 : { Insn . LSIIID insn = ( Insn . LSIIID ) insn_ ; current = setType ( current , insn . dest , EBINARY_TYPE ) ; continue next_insn ; } case bs_init_bits : { Insn . LSIIID insn = ( Insn . LSIIID ) insn_ ; current = setType ( current , insn . dest , EBITSTRING_TYPE ) ; continue next_insn ; } case bs_init_writable : { XReg x0 = new Operands . XReg ( <NUM_LIT:0> ) ; checkArg ( current , x0 ) ; current = setType ( current , x0 , EBITSTRING_TYPE ) ; continue next_insn ; } case bs_private_append : { Insn . BSPrivateAppend insn = ( Insn . BSPrivateAppend ) insn_ ; checkArg ( current , insn . src2 ) ; checkArg ( current , insn . src4 ) ; current = setType ( current , insn . dest , EBITSTRING_TYPE ) ; continue next_insn ; } case bs_append : { Insn . BSAppend insn = ( Insn . BSAppend ) insn_ ; checkArg ( current , insn . src6 ) ; current = setType ( current , insn . dest8 , EBITSTRING_TYPE ) ; continue next_insn ; } case bs_put_string : case bs_put_binary : case bs_put_float : case bs_put_integer : { continue next_insn ; } case bs_put_utf8 : case bs_put_utf16 : case bs_put_utf32 : { Insn . LIS insn = ( Insn . LIS ) insn_ ; checkArg ( current , insn . src ) ; continue next_insn ; } case bs_utf8_size : case bs_utf16_size : { Insn . LSD insn = ( Insn . LSD ) insn_ ; checkArg ( current , insn . src ) ; current = setType ( current , insn . dest , ESMALL_TYPE ) ; continue next_insn ; } case call_fun : case i_call_fun_last : { Insn . I insn = ( Insn . I ) insn_ ; int nargs = insn . i1 ; for ( int i = <NUM_LIT:0> ; i < nargs ; i ++ ) { if ( current . getx ( i ) == null ) throw new Error ( "<STR_LIT>" + i ) ; } if ( code == BeamOpcode . i_call_fun_last ) { is_tail_recursive = true ; } else { current = current . setx ( <NUM_LIT:0> , EOBJECT_TYPE , FV . this ) ; } continue next_insn ; } default : { ETuple insn = insn_ . toSymbolicTuple ( ) ; throw new Error ( "<STR_LIT>" + insn + "<STR_LIT>" + current ) ; } } } update_max_regs ( current ) ; if ( is_term ( last_opcode ) == false && ! is_exceptional_call ( last_insn ) ) { LabeledBlock lbv ; lbv = get_lb ( this . block_label + <NUM_LIT:1> , false ) ; try { if ( lbv != null ) lbv . merge_from ( current ) ; } catch ( Error e ) { log . severe ( "<STR_LIT>" + current + "<STR_LIT>" + lbv . initial + "<STR_LIT>" ) ; throw e ; } } } boolean is_term ( BeamOpcode code ) { switch ( code ) { case K_return : case if_end : case badmatch : case case_end : case try_case_end : case call_last : case call_only : case call_ext_last : case call_ext_only : case func_info : case apply_last : case i_call_fun_last : case wait : case select_tuple_arity : case select_val : case jump : return true ; default : return false ; } } boolean may_terminate_exceptionally ( BeamOpcode code ) { switch ( code ) { case label : case jump : case K_return : case move : case K_try : case K_catch : case try_end : case catch_end : return false ; default : return true ; } } boolean is_exceptional_call ( Insn insn ) { BeamOpcode opcode = insn . opcode ( ) ; if ( opcode == BeamOpcode . call_ext ) { Insn . IE spec_insn = ( Insn . IE ) insn ; ExtFun ext_fun = spec_insn . ext_fun ; if ( ext_fun . mod == ERLANG_ATOM && ( ext_fun . fun == ERROR_ATOM || ext_fun . fun == THROW_ATOM || ext_fun . fun == EXIT_ATOM ) && ext_fun . arity == <NUM_LIT:1> ) return true ; } return false ; } private int sizeof ( TypeMap current , Operands . SourceOperand cell ) { if ( cell instanceof Operands . XReg || cell instanceof Operands . YReg ) return <NUM_LIT:32> ; if ( cell instanceof Operands . FReg ) return <NUM_LIT> ; Type t = getType ( current , cell ) ; if ( t == Type . DOUBLE_TYPE ) { return <NUM_LIT> ; } else { return <NUM_LIT:32> ; } } private Type getBifResult ( String module , String name , Type [ ] parmTypes , boolean is_guard ) { return BIFUtil . getBifResult ( module , name , parmTypes , is_guard ) ; } @ Deprecated private Type [ ] parmTypes ( TypeMap current , ESeq args ) { ArrayList < Type > res = new ArrayList < Type > ( ) ; while ( args != ERT . NIL ) { EObject arg = args . head ( ) ; res . add ( getType ( current , arg ) ) ; args = args . tail ( ) ; } return res . toArray ( new Type [ res . size ( ) ] ) ; } private Type [ ] parmTypes ( TypeMap current , SourceOperand [ ] args ) { Type [ ] res = new Type [ args . length ] ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { SourceOperand arg = args [ i ] ; Type argType = getType ( current , arg ) ; if ( argType == null ) { throw new Error ( "<STR_LIT>" + arg ) ; } res [ i ] = argType ; } return res ; } private void checkArg ( TypeMap current , SourceOperand arg ) { Type argType = getType ( current , arg ) ; if ( argType == null ) { throw new Error ( "<STR_LIT>" + arg ) ; } } private TypeMap analyze_test ( TypeMap current , Insn . L insn_ , int insn_idx ) { current = branch ( current , insn_ . label , insn_idx ) ; BeamOpcode opcode = insn_ . opcode ( ) ; switch ( opcode ) { case is_lt : case is_ge : case is_ne : case is_eq : case is_ne_exact : { Insn . LSS insn = ( Insn . LSS ) insn_ ; checkArg ( current , insn . src1 ) ; checkArg ( current , insn . src2 ) ; return current ; } case is_eq_exact : { Insn . LSS insn = ( Insn . LSS ) insn_ ; checkArg ( current , insn . src1 ) ; checkArg ( current , insn . src2 ) ; Type t1 = getType ( current , insn . src1 ) ; Type t2 = getType ( current , insn . src2 ) ; if ( ! t1 . equals ( t2 ) ) { DestinationOperand reg ; if ( ( reg = insn . src1 . testDestination ( ) ) != null ) { current = setType ( current , reg , t2 ) ; } if ( ( reg = insn . src2 . testDestination ( ) ) != null ) { current = setType ( current , reg , t1 ) ; } } return current ; } case bs_start_match2 : { Insn . LDIID insn = ( Insn . LDIID ) insn_ ; checkArg ( current , insn . dest ) ; return setType ( current , insn . dest5 , EMATCHSTATE_TYPE ) ; } case bs_get_integer2 : { Insn . LDISIID insn = ( Insn . LDISIID ) insn_ ; if ( ! EMATCHSTATE_TYPE . equals ( getType ( current , insn . dest ) ) ) { throw new Error ( "<STR_LIT>" ) ; } return setType ( current , insn . dest7 , EINTEGER_TYPE ) ; } case bs_get_binary2 : { Insn . LDISIID insn = ( Insn . LDISIID ) insn_ ; if ( ! EMATCHSTATE_TYPE . equals ( getType ( current , insn . dest ) ) ) { throw new Error ( "<STR_LIT>" ) ; } return setType ( current , insn . dest7 , EBINARY_TYPE ) ; } case bs_get_float2 : { Insn . LDISIID insn = ( Insn . LDISIID ) insn_ ; if ( ! EMATCHSTATE_TYPE . equals ( getType ( current , insn . dest ) ) ) { throw new Error ( "<STR_LIT>" ) ; } return setType ( current , insn . dest7 , Type . DOUBLE_TYPE ) ; } case bs_test_tail2 : case bs_test_unit : case bs_skip_bits2 : case bs_match_string : case bs_skip_utf8 : case bs_skip_utf16 : case bs_skip_utf32 : { Insn . LD insn = ( Insn . LD ) insn_ ; if ( ! EMATCHSTATE_TYPE . equals ( getType ( current , insn . dest ) ) ) { throw new Error ( "<STR_LIT>" ) ; } return current ; } case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : { Insn . LDIID insn = ( Insn . LDIID ) insn_ ; if ( ! EMATCHSTATE_TYPE . equals ( getType ( current , insn . dest ) ) ) { throw new Error ( "<STR_LIT>" ) ; } return setType ( current , insn . dest5 , ESMALL_TYPE ) ; } default : { Insn . LD insn = ( Insn . LD ) insn_ ; checkArg ( current , insn . dest ) ; switch ( opcode ) { case test_arity : { int arity = ( ( Insn . LDI ) insn ) . i ; return setType ( current , insn . dest , getTupleType ( arity ) ) ; } case is_function2 : { Insn . LDS insn2 = ( Insn . LDS ) insn ; checkArg ( current , insn2 . src ) ; return setType ( current , insn . dest , EFUN_TYPE ) ; } default : { if ( insn instanceof Insn . LD ) { Type test_type = type_tested_for ( ( Insn . LD ) insn ) ; if ( test_type != null ) return setType ( current , insn . dest , test_type ) ; } throw new Error ( "<STR_LIT>" + insn_ . toSymbolic ( ) ) ; } } } } } private Type type_tested_for ( Insn . LD insn ) { switch ( insn . opcode ( ) ) { case is_nil : return ENIL_TYPE ; case is_binary : return EBINARY_TYPE ; case is_tuple : return ETUPLE_TYPE ; case is_integer : return EINTEGER_TYPE ; case is_bitstr : return EBITSTRING_TYPE ; case is_number : return ENUMBER_TYPE ; case is_pid : return EPID_TYPE ; case is_port : return EPORT_TYPE ; case is_reference : return EREFERENCE_TYPE ; case is_float : return EDOUBLE_TYPE ; case is_function : return EFUN_TYPE ; case is_list : case is_nonempty_list : return ECONS_TYPE ; case is_boolean : case is_atom : return EATOM_TYPE ; default : return null ; } } private TypeMap branch ( TypeMap current , Operands . Label target , int idx ) { return branch ( current , target == null ? - <NUM_LIT:1> : target . nr , idx ) ; } private TypeMap branch ( TypeMap current , int target , int idx ) { if ( target > <NUM_LIT:0> ) { get_lb ( target , false ) . merge_from ( current ) ; } return current . clearLive ( makeBasicBlock ( block_label , idx + <NUM_LIT:1> ) ) ; } private TypeMap installExceptionHandler ( TypeMap current , Operands . Label target , int idx ) { TypeMap afterPush = current . pushExceptionHandler ( target . nr ) ; return afterPush . clearLive ( makeBasicBlock ( block_label , idx + <NUM_LIT:1> ) ) ; } private void addExceptionEdge ( TypeMap current ) { int handler_lbl = current . exh . getHandlerLabel ( ) ; if ( handler_lbl >= <NUM_LIT:0> ) get_lb ( handler_lbl , false ) . merge_from ( current ) ; } private Type getTupleType ( int arity ) { ETuple . get_tuple_class ( arity ) ; String tt = "<STR_LIT>" + ETUPLE_TYPE . getInternalName ( ) + arity + "<STR_LIT:;>" ; return Type . getType ( tt ) ; } public void merge_from ( TypeMap typeMap ) { if ( initial == null ) { initial = typeMap . clearLive ( makeBasicBlock ( this . block_label , <NUM_LIT:0> ) ) ; needs_analyze . add ( this ) ; } else { TypeMap new_types = initial . mergeFrom ( typeMap ) ; if ( new_types == initial ) { } else if ( new_types . equals ( initial ) ) { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; } else { initial = new_types ; needs_analyze . add ( this ) ; } } typeMap . add_succ ( initial . bb ) ; } List < Insn > insns = new ArrayList < Insn > ( ) ; @ Override public void visitEnd ( ) { } @ Override public void visitInsn ( Insn insn ) { insns . add ( insn ) ; } private TypeMap setType ( TypeMap current , EObject dd , Type type ) { ETuple dst = dd . testTuple ( ) ; EObject key = dst . elm ( <NUM_LIT:1> ) ; EObject value = dst . elm ( <NUM_LIT:2> ) ; if ( key == X_ATOM ) { current = current . setx ( value . asInt ( ) , type == Type . DOUBLE_TYPE ? EDOUBLE_TYPE : type , FV . this ) ; } else if ( key == Y_ATOM ) { current = current . sety ( value . asInt ( ) , type == Type . DOUBLE_TYPE ? EDOUBLE_TYPE : type ) ; } else if ( key == FR_ATOM ) { current = current . setf ( value . asInt ( ) , type ) ; } else { throw new Error ( "<STR_LIT>" + dst ) ; } return current ; } private TypeMap setType ( TypeMap current , DestinationOperand dst , Type type ) { { Operands . FReg freg ; if ( ( freg = dst . testFReg ( ) ) != null ) { return current . setf ( freg . nr , type ) ; } } type = type == Type . DOUBLE_TYPE ? EDOUBLE_TYPE : type ; { Operands . XReg xreg ; if ( ( xreg = dst . testXReg ( ) ) != null ) { return current . setx ( xreg . nr , type , FV . this ) ; } } { Operands . YReg yreg ; if ( ( yreg = dst . testYReg ( ) ) != null ) { return current . sety ( yreg . nr , type ) ; } } throw new Error ( "<STR_LIT>" + dst ) ; } @ Deprecated private Type getType ( TypeMap current , EObject src ) { if ( src instanceof ETuple2 ) { ETuple2 tup = ( ETuple2 ) src ; if ( tup . elem1 == X_ATOM ) { return current . getx ( tup . elem2 . asInt ( ) ) ; } else if ( tup . elem1 == Y_ATOM ) { return current . gety ( tup . elem2 . asInt ( ) ) ; } else if ( tup . elem1 == FR_ATOM ) { return current . getf ( tup . elem2 . asInt ( ) ) ; } else if ( tup . elem1 == ATOM_ATOM ) { return EATOM_TYPE ; } else if ( tup . elem1 == LITERAL_ATOM ) { return Type . getType ( tup . elem2 . getClass ( ) ) ; } else if ( tup . elem1 == INTEGER_ATOM ) { if ( tup . elem2 . getClass ( ) == ESmall . class ) { return ESMALL_TYPE ; } else if ( tup . elem2 . getClass ( ) == EBig . class ) { return EBIG_TYPE ; } else { return Type . getType ( tup . elem2 . getClass ( ) ) ; } } else if ( tup . elem1 == FLOAT_ATOM ) { return Type . DOUBLE_TYPE ; } else if ( tup . elem1 == FIELD_FLAGS_ATOM ) { return Type . INT_TYPE ; } } else if ( src == NIL_ATOM ) { return ENIL_TYPE ; } else if ( src instanceof ESmall ) { return EINTEGER_TYPE ; } throw new Error ( "<STR_LIT>" + src ) ; } private Type getType ( TypeMap current , SourceOperand src ) { { Operands . XReg xreg ; if ( ( xreg = src . testXReg ( ) ) != null ) return current . getx ( xreg . nr ) ; } { Operands . YReg yreg ; if ( ( yreg = src . testYReg ( ) ) != null ) return current . gety ( yreg . nr ) ; } { Operands . FReg freg ; if ( ( freg = src . testFReg ( ) ) != null ) return current . getf ( freg . nr ) ; } if ( ( src . testAtom ( ) ) != null ) return EATOM_TYPE ; else if ( ( src . testInt ( ) ) != null ) return ESMALL_TYPE ; else if ( ( src . testBigInt ( ) ) != null ) return EBIG_TYPE ; else if ( ( src . testFloat ( ) ) != null ) return Type . DOUBLE_TYPE ; else if ( src instanceof Operands . Nil ) return ENIL_TYPE ; { Operands . TableLiteral lit ; if ( ( lit = src . testTableLiteral ( ) ) != null ) return Type . getType ( lit . value . getClass ( ) ) ; } throw new Error ( "<STR_LIT>" + src ) ; } @ Override public BeamInstruction [ ] getInstructions ( ) { BeamInstruction [ ] res = new BeamInstruction [ insns . size ( ) ] ; for ( int i = <NUM_LIT:0> ; i < insns . size ( ) ; i ++ ) { res [ i ] = new BInsn ( insns . get ( i ) , this . map [ i ] ) ; } return res ; } @ Override public int getLabel ( ) { return this . block_label ; } class BInsn implements BeamInstruction { private final Insn insn ; private final TypeMap current ; public BInsn ( Insn insn , TypeMap current ) { this . insn = insn ; this . current = current ; } @ Override public BeamOpcode opcode ( ) { return insn . opcode ( ) ; } @ Override public String toString ( ) { return insn . toString ( ) ; } } } @ Override public int getArity ( ) { return arity ; } @ Override public String getName ( ) { return name . getName ( ) ; } @ Override public String getModuleName ( ) { return moduleName . getName ( ) ; } @ Override public boolean isExported ( ) { String externalName = getName ( ) + "<STR_LIT:/>" + getArity ( ) ; return exports . contains ( externalName ) ; } @ Override public int getFregCount ( ) { return max_freg ; } @ Override public Set < Integer > getXRegisters ( ) { return all_xregs ; } @ Override public int getYregCount ( ) { return max_stack ; } @ Override public BeamCodeBlock [ ] getCodeBlocks ( ) { BeamCodeBlock [ ] blocks = this . lbs . values ( ) . toArray ( new BeamCodeBlock [ <NUM_LIT:0> ] ) ; return blocks ; } @ Override public int getEntryLabel ( ) { return startLabel ; } } public void visitModule ( EAtom name ) { this . moduleName = name ; super . visitModule ( name ) ; } Set < String > exports = new HashSet < String > ( ) ; public void visitExport ( EAtom fun , int arity , int entry ) { exports . add ( fun . getName ( ) + "<STR_LIT:/>" + arity ) ; super . visitExport ( fun , arity , entry ) ; } public void visitAttribute ( EAtom att , EObject value ) { super . visitAttribute ( att , value ) ; } public String getModuleName ( ) { return this . moduleName . getName ( ) ; } public BeamFunction [ ] functions ( ) { return functions . toArray ( new BeamFunction [ functions . size ( ) ] ) ; } } </s>
|
<s> package erjang . beam . analysis ; import java . util . Comparator ; import java . util . Set ; import java . util . TreeSet ; class BasicBlock { final int label ; final int index ; TreeSet < Integer > use = new TreeSet < Integer > ( ) ; TreeSet < Integer > kill = new TreeSet < Integer > ( ) ; TreeSet < Integer > in = new TreeSet < Integer > ( ) ; TreeSet < Integer > out = new TreeSet < Integer > ( ) ; Set < BasicBlock > succ = new TreeSet < BasicBlock > ( new Comparator < BasicBlock > ( ) { @ Override public int compare ( BasicBlock o1 , BasicBlock o2 ) { if ( o1 == o2 ) return <NUM_LIT:0> ; int loff = o1 . label - o2 . label ; if ( loff != <NUM_LIT:0> ) { return loff ; } return o1 . index - o2 . index ; } } ) ; ; public BasicBlock ( int label , int index ) { this . label = label ; this . index = index ; } public void succ ( BasicBlock bb ) { succ . add ( bb ) ; } public void use_x ( int reg ) { use . add ( KEY_X | reg ) ; } public void use_y ( TypeMap map , int reg ) { use . add ( KEY_X | map . get_ypos ( reg ) ) ; } public void use_fr ( int reg ) { use . add ( KEY_X | reg ) ; } public void kill_x ( int reg ) { kill . add ( KEY_X | reg ) ; } public void kill_y ( TypeMap map , int reg ) { kill . add ( KEY_X | map . get_ypos ( reg ) ) ; } public void kill_fr ( int reg ) { kill . add ( KEY_X | reg ) ; } static final int KEY_X = <NUM_LIT:0> << <NUM_LIT:16> ; static final int KEY_Y = <NUM_LIT:1> << <NUM_LIT:16> ; static final int KEY_FR = <NUM_LIT:2> << <NUM_LIT:16> ; } </s>
|
<s> package erjang . beam ; public interface BeamExceptionHandler { public int getHandlerLabel ( ) ; public BeamExceptionHandler getParent ( ) ; } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; import erjang . ETuple ; public class ExtFunc extends erjang . beam . Arg { public final EAtom mod ; public final EAtom fun ; @ Override public String toString ( ) { return mod . toString ( ) + '<CHAR_LIT::>' + fun . toString ( ) + "<STR_LIT:/>" + no ; } public ExtFunc ( EAtom mod , EAtom fun , int arity ) { super ( Kind . EXT_FUNC , arity ) ; this . mod = mod ; this . fun = fun ; } public ExtFunc ( ETuple ext ) { super ( Kind . EXT_FUNC , ext . elm ( <NUM_LIT:3> ) . asInt ( ) ) ; this . mod = ext . elm ( <NUM_LIT:1> ) . testAtom ( ) ; this . fun = ext . elm ( <NUM_LIT:2> ) . testAtom ( ) ; } } </s>
|
<s> package erjang . beam ; import java . util . HashMap ; import java . util . Map ; import com . ericsson . otp . erlang . OtpErlangAtom ; import com . ericsson . otp . erlang . OtpErlangBinary ; import com . ericsson . otp . erlang . OtpErlangBitstr ; import com . ericsson . otp . erlang . OtpErlangDouble ; import com . ericsson . otp . erlang . OtpErlangList ; import com . ericsson . otp . erlang . OtpErlangLong ; import com . ericsson . otp . erlang . OtpErlangObject ; import com . ericsson . otp . erlang . OtpErlangString ; import com . ericsson . otp . erlang . OtpErlangTuple ; import erjang . EAtom ; import erjang . EBinary ; import erjang . EBitString ; import erjang . EObject ; import erjang . ERT ; import erjang . EString ; import erjang . ETuple ; abstract class Converter < T > { abstract EObject conv ( T obj ) ; } public class OtpConverter { static Map < Class , Converter > conv = new HashMap < Class , Converter > ( ) ; static < T > void add ( Class < T > c , Converter < T > co ) { conv . put ( c , co ) ; } static { add ( OtpErlangTuple . class , new Converter < OtpErlangTuple > ( ) { EObject conv ( OtpErlangTuple obj ) { EObject [ ] vals = new EObject [ obj . arity ( ) ] ; for ( int i = <NUM_LIT:0> ; i < obj . arity ( ) ; i ++ ) { vals [ i ] = convert ( obj . elementAt ( i ) ) ; } return ETuple . make ( vals ) ; } } ) ; add ( OtpErlangList . class , new Converter < OtpErlangList > ( ) { @ Override EObject conv ( OtpErlangList obj ) { EObject tail = obj . getLastTail ( ) == null ? ERT . NIL : convert ( obj . getLastTail ( ) ) ; for ( int i = obj . arity ( ) - <NUM_LIT:1> ; i >= <NUM_LIT:0> ; i -- ) { tail = ERT . cons ( convert ( obj . elementAt ( i ) ) , tail ) ; } return tail ; } } ) ; add ( OtpErlangAtom . class , new Converter < OtpErlangAtom > ( ) { EObject conv ( OtpErlangAtom obj ) { return EAtom . intern ( obj . atomValue ( ) ) ; } } ) ; add ( OtpErlangLong . class , new Converter < OtpErlangLong > ( ) { EObject conv ( OtpErlangLong obj ) { return ( obj . isLong ( ) ) ? ERT . box ( obj . longValue ( ) ) : ERT . box ( obj . bigIntegerValue ( ) ) ; } } ) ; add ( OtpErlangString . class , new Converter < OtpErlangString > ( ) { EObject conv ( OtpErlangString obj ) { return new EString ( obj . stringValue ( ) ) ; } } ) ; add ( OtpErlangDouble . class , new Converter < OtpErlangDouble > ( ) { EObject conv ( OtpErlangDouble obj ) { return ERT . box ( obj . doubleValue ( ) ) ; } } ) ; add ( OtpErlangBinary . class , new Converter < OtpErlangBinary > ( ) { EObject conv ( OtpErlangBinary obj ) { return new EBinary ( obj . binaryValue ( ) ) ; } } ) ; add ( OtpErlangBitstr . class , new Converter < OtpErlangBitstr > ( ) { EObject conv ( OtpErlangBitstr obj ) { return EBitString . make ( obj . binaryValue ( ) , <NUM_LIT:0> , obj . size ( ) , obj . pad_bits ( ) ) ; } } ) ; } public static EObject convert ( OtpErlangObject value ) { Class < ? extends OtpErlangObject > c = value . getClass ( ) ; Converter cc = conv . get ( c ) ; if ( cc == null ) { throw new Error ( "<STR_LIT>" + c ) ; } else { return cc . conv ( value ) ; } } } </s>
|
<s> package erjang . beam ; import java . util . Collection ; public interface FunctionVisitor2 extends FunctionVisitor { public void visitMaxs ( Collection < Integer > maxXreg , int y_count , int fp_count , boolean isTailRecursive ) ; } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; import erjang . EObject ; public interface ModuleVisitor { void visitModule ( EAtom name ) ; void visitExport ( EAtom fun , int arity , int entry ) ; void visitAttribute ( EAtom att , EObject value ) ; void declareFunction ( EAtom fun , int arity , int label ) ; FunctionVisitor visitFunction ( EAtom name , int arity , int startLabel ) ; void visitEnd ( ) ; } </s>
|
<s> package erjang . beam ; public interface BeamInstruction { public BeamOpcode opcode ( ) ; } </s>
|
<s> package erjang . beam ; import java . io . File ; import java . io . IOException ; import java . util . logging . Level ; import java . util . logging . Logger ; import com . ericsson . otp . erlang . OtpAuthException ; import com . ericsson . otp . erlang . OtpConnection ; import com . ericsson . otp . erlang . OtpErlangAtom ; import com . ericsson . otp . erlang . OtpErlangBinary ; import com . ericsson . otp . erlang . OtpErlangExit ; import com . ericsson . otp . erlang . OtpErlangObject ; import com . ericsson . otp . erlang . OtpErlangString ; import com . ericsson . otp . erlang . OtpErlangTuple ; import com . ericsson . otp . erlang . OtpPeer ; import com . ericsson . otp . erlang . OtpSelf ; import erjang . ETuple ; public class ErlangBeamDisLoader extends BeamLoader { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; final boolean TRY_NATIVE_LOADER = true ; String myid = "<STR_LIT>" ; OtpConnection conn ; private OtpSelf self ; private OtpPeer peer ; public ErlangBeamDisLoader ( ) throws OtpAuthException , IOException { self = new OtpSelf ( myid ) ; peer = new OtpPeer ( "<STR_LIT>" ) ; conn = self . connect ( peer ) ; } @ Override public BeamFileData load ( File file ) throws IOException { sendGEN ( conn , "<STR_LIT>" , new OtpErlangTuple ( new OtpErlangObject [ ] { new OtpErlangAtom ( "<STR_LIT>" ) , new OtpErlangString ( file . getAbsolutePath ( ) ) } ) ) ; try { OtpErlangObject reply = conn . receiveRPC ( ) ; return new SymbolicBeamFileData ( check ( ( ETuple ) OtpConverter . convert ( reply ) , file ) ) ; } catch ( OtpErlangExit e ) { throw new RuntimeException ( "<STR_LIT>" , e ) ; } catch ( OtpAuthException e ) { throw new RuntimeException ( "<STR_LIT>" , e ) ; } } @ Override public BeamFileData load ( byte [ ] data ) throws IOException { sendGEN ( conn , "<STR_LIT>" , new OtpErlangTuple ( new OtpErlangObject [ ] { new OtpErlangAtom ( "<STR_LIT>" ) , new OtpErlangBinary ( data ) } ) ) ; try { OtpErlangObject reply = conn . receiveRPC ( ) ; return new SymbolicBeamFileData ( check ( ( ETuple ) OtpConverter . convert ( reply ) , data ) ) ; } catch ( OtpErlangExit e ) { throw new RuntimeException ( "<STR_LIT>" , e ) ; } catch ( OtpAuthException e ) { throw new RuntimeException ( "<STR_LIT>" , e ) ; } } public void sendGEN ( final OtpConnection conn , String server , final OtpErlangObject request ) throws IOException { final OtpErlangObject [ ] gen = new OtpErlangObject [ <NUM_LIT:3> ] ; final OtpErlangObject [ ] reply = new OtpErlangObject [ <NUM_LIT:2> ] ; reply [ <NUM_LIT:0> ] = self . pid ( ) ; reply [ <NUM_LIT:1> ] = self . createRef ( ) ; gen [ <NUM_LIT:0> ] = new OtpErlangAtom ( "<STR_LIT>" ) ; gen [ <NUM_LIT:1> ] = new OtpErlangTuple ( reply ) ; gen [ <NUM_LIT:2> ] = request ; conn . send ( server , new OtpErlangTuple ( gen ) ) ; } protected ETuple check ( ETuple dis1 , File file ) throws IOException { if ( ! TRY_NATIVE_LOADER ) return dis1 ; ETuple dis2 = erjang . beam . loader . BeamLoader . read ( file . getAbsolutePath ( ) ) . toSymbolic ( ) ; return check ( dis1 , dis2 ) ; } protected ETuple check ( ETuple dis1 , byte [ ] data ) throws IOException { if ( ! TRY_NATIVE_LOADER ) return dis1 ; ETuple dis2 = erjang . beam . loader . BeamLoader . parse ( data ) . toSymbolic ( ) ; return check ( dis1 , dis2 ) ; } protected ETuple check ( ETuple dis1 , ETuple dis2 ) { log . fine ( "<STR_LIT>" ) ; boolean eq = false ; try { eq = dis1 . equals ( dis2 ) ; } catch ( RuntimeException re ) { log . log ( Level . WARNING , "<STR_LIT>" , re ) ; } if ( eq ) log . fine ( "<STR_LIT:OK>" ) ; else { if ( log . isLoggable ( Level . FINE ) ) { log . fine ( "<STR_LIT>" + dis1 + "<STR_LIT>" + dis2 ) ; } } return dis1 ; } } </s>
|
<s> package erjang . beam . loader ; import java . io . File ; import java . io . IOException ; import erjang . ETuple ; import erjang . beam . BeamFileData ; import erjang . beam . loader . BeamLoader ; import erjang . beam . repr . ModuleRepr ; public class ErjangBeamDisLoader extends erjang . beam . BeamLoader { public ErjangBeamDisLoader ( ) { } @ Override public BeamFileData load ( File file ) throws IOException { ModuleRepr mod = BeamLoader . read ( file . getAbsolutePath ( ) ) ; mod . rewrite ( new Rewriter ( ) ) ; return mod ; } @ Override public BeamFileData load ( byte [ ] data ) throws IOException { ModuleRepr mod = BeamLoader . parse ( data ) ; mod . rewrite ( new Rewriter ( ) ) ; return mod ; } } </s>
|
<s> package erjang . beam . loader ; import static erjang . beam . CodeAtoms . START_ATOM ; import java . io . ByteArrayInputStream ; import java . io . DataInputStream ; import java . io . EOFException ; import java . io . File ; import java . io . FileInputStream ; import java . io . IOException ; import java . math . BigInteger ; import java . security . MessageDigest ; import java . security . NoSuchAlgorithmException ; import java . util . ArrayList ; import java . util . HashMap ; import java . util . logging . Level ; import java . util . logging . Logger ; import erjang . EAtom ; import erjang . EBinary ; import erjang . EInputStream ; import erjang . EObject ; import erjang . ESeq ; import erjang . beam . BeamOpcode ; import erjang . beam . repr . AnonFun ; import erjang . beam . repr . CodeTables ; import erjang . beam . repr . ExtFun ; import erjang . beam . repr . FunctionInfo ; import erjang . beam . repr . FunctionRepr ; import erjang . beam . repr . Insn ; import erjang . beam . repr . ModuleRepr ; import erjang . beam . repr . Operands ; import erjang . beam . repr . Operands . AllocList ; import erjang . beam . repr . Operands . Atom ; import erjang . beam . repr . Operands . BitString ; import erjang . beam . repr . Operands . ByteString ; import erjang . beam . repr . Operands . DestinationOperand ; import erjang . beam . repr . Operands . FReg ; import erjang . beam . repr . Operands . Label ; import erjang . beam . repr . Operands . Literal ; import erjang . beam . repr . Operands . Operand ; import erjang . beam . repr . Operands . SelectList ; import erjang . beam . repr . Operands . SourceOperand ; import erjang . beam . repr . Operands . TableLiteral ; import erjang . beam . repr . Operands . XReg ; import erjang . beam . repr . Operands . YReg ; public class BeamLoader extends CodeTables { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; public static void main ( String [ ] args ) throws IOException { for ( String filename : args ) read ( filename ) ; } public static ModuleRepr read ( String filename ) throws IOException { long file_size = new File ( filename ) . length ( ) ; DataInputStream in = null ; try { in = new DataInputStream ( new FileInputStream ( filename ) ) ; BeamLoader bl = new BeamLoader ( in , file_size , false ) ; bl . read ( ) ; return bl . toModuleRepr ( ) ; } finally { if ( in != null ) in . close ( ) ; } } public static ModuleRepr parse ( byte [ ] data ) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream ( data ) ; BeamLoader bl = new BeamLoader ( new DataInputStream ( in ) , data . length , false ) ; bl . read ( ) ; return bl . toModuleRepr ( ) ; } private EInputStream in ; private boolean include_debug_info ; private EObject attributes , compilation_info , abstract_tree ; private FunctionInfo [ ] exports = new FunctionInfo [ <NUM_LIT:0> ] , localFunctions = new FunctionInfo [ <NUM_LIT:0> ] ; private ArrayList < Insn > code ; private ArrayList < FunctionRepr > functionReprs ; private EBinary module_md5 ; public ModuleRepr toModuleRepr ( ) { FunctionRepr [ ] functions = new FunctionRepr [ functionReprs . size ( ) ] ; functions = functionReprs . toArray ( functions ) ; return new ModuleRepr ( this , atom ( <NUM_LIT:1> ) , exports , ( ESeq ) attributes , ( ESeq ) compilation_info , functions ) ; } static final int FOR1 = <NUM_LIT> ; static final int BEAM = <NUM_LIT> ; static final int GZIP = <NUM_LIT> ; static final int ATOM = <NUM_LIT> ; static final int CODE = <NUM_LIT> ; static final int STR_T = <NUM_LIT> ; static final int IMP_T = <NUM_LIT> ; static final int EXP_T = <NUM_LIT> ; static final int LIT_T = <NUM_LIT> ; static final int FUN_T = <NUM_LIT> ; static final int LOC_T = <NUM_LIT> ; static final int ATTR = <NUM_LIT> ; static final int C_INF = <NUM_LIT> ; static final int ABST = <NUM_LIT> ; static final int [ ] SECTION_READING_ORDER = { ATOM , STR_T , LIT_T , IMP_T , EXP_T , FUN_T , LOC_T , CODE , C_INF , ATTR , ABST } ; public BeamLoader ( DataInputStream in , long actual_file_size , boolean include_debug_info ) throws IOException { this . include_debug_info = include_debug_info ; int header ; in = new DataInputStream ( new java . io . BufferedInputStream ( in ) ) ; boolean zipped = false ; in . mark ( <NUM_LIT:8> ) ; if ( ( header = in . readInt ( ) ) != FOR1 ) { if ( ( header & <NUM_LIT> ) == GZIP ) { in . reset ( ) ; in = new DataInputStream ( new java . util . zip . GZIPInputStream ( in ) ) ; zipped = true ; if ( in . readInt ( ) != FOR1 ) { throw new IOException ( "<STR_LIT>" ) ; } } else { throw new IOException ( "<STR_LIT>" + Integer . toHexString ( header ) ) ; } } int stated_length = in . readInt ( ) ; if ( in . readInt ( ) != BEAM ) throw new IOException ( "<STR_LIT>" ) ; if ( ! zipped && ( stated_length + <NUM_LIT:8> != actual_file_size ) ) throw new IOException ( "<STR_LIT>" + ( stated_length + <NUM_LIT:8> ) + "<STR_LIT>" + actual_file_size ) ; byte [ ] data = new byte [ stated_length - <NUM_LIT:4> ] ; in . readFully ( data ) ; this . in = new EInputStream ( data ) ; } public void read ( ) throws IOException { final HashMap < Integer , SectionMetadata > section_map = new HashMap < Integer , SectionMetadata > ( ) ; for ( SectionMetadata smd ; ( smd = readSectionHeader ( ) ) != null ; ) { section_map . put ( smd . tag , smd ) ; in . setPos ( smd . offset + ( ( smd . length + <NUM_LIT:3> ) & ~ <NUM_LIT:3> ) ) ; } compute_module_md5 ( section_map ) ; for ( int tag : SECTION_READING_ORDER ) { SectionMetadata smd = section_map . get ( tag ) ; if ( smd != null ) readSection ( smd ) ; } functionReprs = partitionCodeByFunction ( ) ; } static final int [ ] SECTION_MD5_ORDER = { ATOM , CODE , STR_T , IMP_T , EXP_T , } ; private void compute_module_md5 ( final HashMap < Integer , SectionMetadata > section_map ) { SectionMetadata smd ; MessageDigest context ; try { context = MessageDigest . getInstance ( "<STR_LIT>" ) ; } catch ( NoSuchAlgorithmException e ) { return ; } for ( int tag : SECTION_MD5_ORDER ) { smd = section_map . get ( tag ) ; in . updateMessageDigest ( context , smd . offset , smd . length ) ; } if ( ( smd = section_map . get ( FUN_T ) ) != null ) { int start = smd . offset ; int left = smd . length ; if ( left >= <NUM_LIT:4> ) { byte [ ] zero = new byte [ <NUM_LIT:4> ] ; in . updateMessageDigest ( context , start , <NUM_LIT:4> ) ; start += <NUM_LIT:4> ; left -= <NUM_LIT:4> ; while ( left >= <NUM_LIT:24> ) { in . updateMessageDigest ( context , start , <NUM_LIT:20> ) ; context . update ( zero , <NUM_LIT:0> , <NUM_LIT:4> ) ; start += <NUM_LIT:24> ; left -= <NUM_LIT:24> ; } } if ( left > <NUM_LIT:0> ) { in . updateMessageDigest ( context , start , left ) ; } } if ( ( smd = section_map . get ( LIT_T ) ) != null ) { in . updateMessageDigest ( context , smd . offset , smd . length ) ; } byte [ ] digest = context . digest ( ) ; this . module_md5 = new EBinary ( digest ) ; } public ArrayList < FunctionRepr > partitionCodeByFunction ( ) { int funCount = ( exports == null ? <NUM_LIT:0> : exports . length ) + ( localFunctions == null ? <NUM_LIT:0> : localFunctions . length ) ; ArrayList < FunctionRepr > functions = new ArrayList < FunctionRepr > ( funCount ) ; FunctionInfo fi = null ; ArrayList < Insn > currentFunctionBody = null ; for ( Insn insn : code ) { FunctionInfo newFI = null ; if ( insn . opcode ( ) == BeamOpcode . label ) { int labelNr = ( ( Insn . I ) insn ) . i1 ; newFI = functionAtLabel ( labelNr + <NUM_LIT:1> ) ; if ( newFI == null ) newFI = functionAtLabel ( labelNr ) ; } else if ( insn . opcode ( ) == BeamOpcode . int_code_end ) { newFI = new FunctionInfo ( null , null , - <NUM_LIT:1> , - <NUM_LIT:1> ) ; } if ( newFI != null && newFI != fi ) { if ( fi != null ) { FunctionRepr fun = new FunctionRepr ( fi , currentFunctionBody ) ; functions . add ( fun ) ; } fi = newFI ; currentFunctionBody = new ArrayList < Insn > ( ) ; } currentFunctionBody . add ( insn ) ; } return functions ; } public SectionMetadata readSectionHeader ( ) throws IOException { int tag ; try { tag = in . read4BE ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + toSymbolicTag ( tag ) + "<STR_LIT>" + in . getPos ( ) ) ; } catch ( EOFException eof ) { return null ; } int sectionLength = in . read4BE ( ) ; int startPos = in . getPos ( ) ; return new SectionMetadata ( tag , startPos , sectionLength ) ; } public void readSection ( SectionMetadata section ) throws IOException { in . setPos ( section . offset ) ; try { if ( section . length > <NUM_LIT:0> ) switch ( section . tag ) { case ATOM : readAtomSection ( ) ; break ; case CODE : readCodeSection ( ) ; break ; case STR_T : readStringSection ( section . length ) ; break ; case IMP_T : readImportSection ( ) ; break ; case EXP_T : readExportSection ( ) ; break ; case FUN_T : readFunctionSection ( ) ; break ; case LIT_T : readLiteralSection ( ) ; break ; case LOC_T : readLocalFunctionSection ( ) ; break ; case ATTR : readAttributeSection ( ) ; break ; case C_INF : readCompilationInfoSection ( ) ; break ; case ABST : readASTSection ( ) ; break ; default : if ( log . isLoggable ( Level . WARNING ) ) log . warning ( "<STR_LIT>" + Integer . toHexString ( section . tag ) ) ; } } catch ( Exception e ) { int relPos = in . getPos ( ) - section . offset ; try { int curPos = in . getPos ( ) ; in . setPos ( curPos - <NUM_LIT:16> ) ; byte [ ] d = new byte [ <NUM_LIT> ] ; int ctxlen = in . read ( d ) ; if ( log . isLoggable ( Level . SEVERE ) ) { StringBuilder sb = new StringBuilder ( ) ; sb . append ( "<STR_LIT>" ) ; for ( int i = <NUM_LIT:0> ; i < ctxlen ; i ++ ) { int byt = d [ i ] & <NUM_LIT> ; if ( byt < <NUM_LIT:16> ) sb . append ( "<STR_LIT:0>" ) ; sb . append ( Integer . toHexString ( byt & <NUM_LIT> ) ) ; sb . append ( "<STR_LIT:U+0020>" ) ; if ( ( i + <NUM_LIT:1> ) % <NUM_LIT:16> == <NUM_LIT:0> || ( i + <NUM_LIT:1> ) == ctxlen ) sb . append ( "<STR_LIT:n>" ) ; } log . severe ( sb . toString ( ) ) ; } } catch ( Exception e2 ) { } throw new IOException ( "<STR_LIT>" + relPos + "<STR_LIT>" + Integer . toHexString ( relPos ) + "<STR_LIT>" + Integer . toHexString ( section . tag ) , e ) ; } int readLength = in . getPos ( ) - section . offset ; if ( readLength > section . length ) throw new IOException ( "<STR_LIT>" + Integer . toHexString ( section . tag ) + "<STR_LIT>" + readLength + "<STR_LIT>" + section . length + "<STR_LIT>" + in . getPos ( ) ) ; } private String toSymbolicTag ( int tag ) { char [ ] sym = new char [ <NUM_LIT:4> ] ; for ( int i = <NUM_LIT:3> , d = tag ; i >= <NUM_LIT:0> ; i -- , d >>>= <NUM_LIT:8> ) { sym [ i ] = ( char ) ( d & <NUM_LIT> ) ; if ( ! Character . isJavaIdentifierPart ( sym [ i ] ) ) { return "<STR_LIT>" + Integer . toHexString ( tag ) ; } } return new String ( sym ) ; } public void readAtomSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int nAtoms = in . read4BE ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nAtoms ) ; atoms = new EAtom [ nAtoms ] ; for ( int i = <NUM_LIT:0> ; i < nAtoms ; i ++ ) { String atom = readString ( in . read1 ( ) ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + ( i + <NUM_LIT:1> ) + "<STR_LIT>" + atom + "<STR_LIT:'>" ) ; atoms [ i ] = EAtom . intern ( atom ) ; } } public void readStringSection ( int sectionLength ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; stringpool = readBinary ( sectionLength ) ; } public void readExportSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int nExports = in . read4BE ( ) ; exports = new FunctionInfo [ nExports ] ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nExports ) ; EAtom mod = moduleName ( ) ; for ( int i = <NUM_LIT:0> ; i < nExports ; i ++ ) { int fun_atom_nr = in . read4BE ( ) ; int arity = in . read4BE ( ) ; int label = in . read4BE ( ) ; EAtom fun = atom ( fun_atom_nr ) ; exports [ i ] = new FunctionInfo ( mod , fun , arity , label ) ; addFunctionAtLabel ( exports [ i ] ) ; if ( log . isLoggable ( Level . FINE ) && atoms != null ) { log . fine ( "<STR_LIT>" + ( i + <NUM_LIT:1> ) + "<STR_LIT::U+0020>" + atom ( fun_atom_nr ) + "<STR_LIT:/>" + arity + "<STR_LIT>" + label ) ; } } } public void readLocalFunctionSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int nLocals = in . read4BE ( ) ; localFunctions = new FunctionInfo [ nLocals ] ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nLocals ) ; EAtom mod = moduleName ( ) ; for ( int i = <NUM_LIT:0> ; i < nLocals ; i ++ ) { int fun_atom_nr = in . read4BE ( ) ; int arity = in . read4BE ( ) ; int label = in . read4BE ( ) ; EAtom fun = atom ( fun_atom_nr ) ; localFunctions [ i ] = new FunctionInfo ( mod , fun , arity , label ) ; addFunctionAtLabel ( localFunctions [ i ] ) ; if ( log . isLoggable ( Level . FINE ) && atoms != null ) { log . fine ( "<STR_LIT>" + ( i + <NUM_LIT:1> ) + "<STR_LIT::U+0020>" + atom ( fun_atom_nr ) + "<STR_LIT:/>" + arity + "<STR_LIT>" + label ) ; } } } public void readFunctionSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int nFunctions = in . read4BE ( ) ; anonymousFuns = new AnonFun [ nFunctions ] ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nFunctions ) ; EAtom mod = moduleName ( ) ; for ( int i = <NUM_LIT:0> ; i < nFunctions ; i ++ ) { int fun_atom_nr = in . read4BE ( ) ; int arity = in . read4BE ( ) ; int label = in . read4BE ( ) ; int index = in . read4BE ( ) ; int free_vars = in . read4BE ( ) ; int old_uniq = in . read4BE ( ) ; EAtom fun = atom ( fun_atom_nr ) ; anonymousFuns [ i ] = new AnonFun ( mod , fun , arity , label , old_uniq , i , module_md5 , index , free_vars ) ; if ( log . isLoggable ( Level . FINE ) && atoms != null ) { log . fine ( "<STR_LIT>" + ( i + <NUM_LIT:1> ) + "<STR_LIT::U+0020>" + fun + "<STR_LIT:/>" + arity + "<STR_LIT>" + label ) ; log . fine ( "<STR_LIT>" + index + "<STR_LIT>" + free_vars + "<STR_LIT>" + old_uniq ) ; } } } public void readImportSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int nImports = in . read4BE ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nImports ) ; externalFuns = new ExtFun [ nImports ] ; for ( int i = <NUM_LIT:0> ; i < nImports ; i ++ ) { int m_atm_no = in . read4BE ( ) ; int f_atm_no = in . read4BE ( ) ; int arity = in . read4BE ( ) ; EAtom mod = atom ( m_atm_no ) , fun = atom ( f_atm_no ) ; externalFuns [ i ] = new ExtFun ( mod , fun , arity ) ; if ( log . isLoggable ( Level . FINE ) && atoms != null ) { log . fine ( "<STR_LIT>" + ( i + <NUM_LIT:1> ) + "<STR_LIT::U+0020>" + mod + "<STR_LIT::>" + fun + "<STR_LIT:/>" + arity ) ; } } } public void readAttributeSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; attributes = in . read_any ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + attributes ) ; } public void readCompilationInfoSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; compilation_info = in . read_any ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + compilation_info ) ; } public void readASTSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; if ( ! include_debug_info ) return ; abstract_tree = in . read_any ( ) ; } public void readLiteralSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; final byte [ ] buf = in . read_size_and_inflate ( ) ; final EInputStream is = new EInputStream ( buf ) ; int nLiterals = is . read4BE ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + nLiterals ) ; literals = new EObject [ nLiterals ] ; for ( int i = <NUM_LIT:0> ; i < nLiterals ; i ++ ) { int lit_length = is . read4BE ( ) ; int pos_before_lit = is . getPos ( ) ; literals [ i ] = is . read_any ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + i + "<STR_LIT::U+0020>" + literals [ i ] ) ; int pos_after_lit = is . getPos ( ) ; assert ( pos_after_lit == pos_before_lit + lit_length ) ; } } public void readCodeSection ( ) throws IOException { if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" ) ; int flags = in . read4BE ( ) ; int zero = in . read4BE ( ) ; int highestOpcode = in . read4BE ( ) ; int labelCnt = in . read4BE ( ) ; int funCnt = in . read4BE ( ) ; if ( log . isLoggable ( Level . FINE ) ) log . fine ( "<STR_LIT>" + flags + "<STR_LIT>" + zero + "<STR_LIT>" + highestOpcode + "<STR_LIT>" + labelCnt + "<STR_LIT>" + funCnt ) ; code = new ArrayList < Insn > ( ) ; Insn insn ; do { insn = readInstruction ( ) ; code . add ( insn ) ; } while ( insn . opcode ( ) != BeamOpcode . int_code_end ) ; } public Insn readInstruction ( ) throws IOException { int opcode_no = in . read1 ( ) ; BeamOpcode opcode = BeamOpcode . decode ( opcode_no ) ; if ( opcode != null ) { switch ( opcode ) { case K_return : case send : case remove_message : case timeout : case if_end : case int_code_end : case fclearerror : case bs_init_writable : case on_load : return new Insn ( opcode ) ; case label : case deallocate : case call_fun : case apply : { int i1 = readCodeInteger ( ) ; if ( log . isLoggable ( Level . FINE ) && opcode == BeamOpcode . label ) log . fine ( "<STR_LIT>" + i1 + "<STR_LIT>" ) ; return new Insn . I ( opcode , i1 ) ; } case loop_rec_end : case wait : case jump : case fcheckerror : case recv_mark : case recv_set : { Label lbl = readLabel ( ) ; return new Insn . L ( opcode , lbl ) ; } case put : case badmatch : case case_end : case try_case_end : { SourceOperand src = readSource ( ) ; return new Insn . S ( opcode , src ) ; } case init : case bs_context_to_binary : { DestinationOperand dest = readDestination ( ) ; return new Insn . D ( opcode , dest ) ; } case make_fun2 : { int fun_ref = readCodeInteger ( ) ; return new Insn . F ( opcode , fun_ref , anonFun ( fun_ref ) ) ; } case try_end : case catch_end : case try_case : { YReg y = readYReg ( ) ; return new Insn . Y ( opcode , y ) ; } case bs_put_string : { ByteString bin = readBytestringRef ( ) ; return new Insn . By ( opcode , bin ) ; } case allocate : case allocate_zero : case trim : case apply_last : { int i1 = readCodeInteger ( ) ; int i2 = readCodeInteger ( ) ; return new Insn . II ( opcode , i1 , i2 ) ; } case test_heap : { AllocList al = readAllocList ( ) ; int i2 = readCodeInteger ( ) ; return new Insn . WI ( opcode , al , i2 ) ; } case call : case call_only : { int i1 = readCodeInteger ( ) ; Label label = readLabel ( ) ; return new Insn . IL ( opcode , i1 , label , functionAtLabel ( label . nr ) ) ; } case call_ext : case call_ext_only : { int i1 = readCodeInteger ( ) ; int ext_fun_ref = readCodeInteger ( ) ; return new Insn . IE ( opcode , i1 , extFun ( ext_fun_ref ) ) ; } case bs_save2 : case bs_restore2 : { DestinationOperand dest = readDestination ( ) ; int i2 ; if ( ( peekTag ( ) & <NUM_LIT> ) == ATOM4_TAG ) { if ( readAtom ( ) . getEAtom ( ) != START_ATOM ) throw new IOException ( "<STR_LIT>" ) ; i2 = - <NUM_LIT:1> ; } else i2 = readCodeInteger ( ) ; return new Insn . DI ( opcode , dest , i2 , true ) ; } case move : case fmove : case fconv : { SourceOperand src = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . SD ( opcode , src , dest ) ; } case put_tuple : { int i1 = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . ID ( opcode , i1 , dest ) ; } case loop_rec : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LD ( opcode , label , dest ) ; } case K_try : case K_catch : { YReg y = readYReg ( ) ; Label label = readLabel ( ) ; return new Insn . YL ( opcode , y , label ) ; } case is_integer : case is_float : case is_number : case is_atom : case is_pid : case is_reference : case is_port : case is_nil : case is_binary : case is_list : case is_nonempty_list : case is_tuple : case is_function : case is_boolean : case is_bitstr : { Label label = readLabel ( ) ; DestinationOperand src = readDestination ( ) ; return new Insn . LD ( opcode , label , src , true ) ; } case wait_timeout : { Label label = readLabel ( ) ; SourceOperand src = readSource ( ) ; return new Insn . LS ( opcode , label , src , false ) ; } case raise : { SourceOperand src1 = readSource ( ) ; SourceOperand src2 = readSource ( ) ; return new Insn . SS ( opcode , src1 , src2 ) ; } case put_string : { ByteString bin = readBytestringRef ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . ByD ( opcode , bin , dest ) ; } case allocate_heap : case allocate_heap_zero : { int i1 = readCodeInteger ( ) ; AllocList al = readAllocList ( ) ; int i3 = readCodeInteger ( ) ; return new Insn . IWI ( opcode , i1 , al , i3 ) ; } case func_info : { Atom mod = readAtom ( ) ; Atom fun = readAtom ( ) ; int arity = readCodeInteger ( ) ; return new Insn . AAI ( opcode , mod , fun , arity ) ; } case call_ext_last : { int arity = readCodeInteger ( ) ; int ext_fun_ref = readCodeInteger ( ) ; int dealloc = readCodeInteger ( ) ; return new Insn . IEI ( opcode , arity , extFun ( ext_fun_ref ) , dealloc ) ; } case put_list : { SourceOperand src1 = readSource ( ) ; SourceOperand src2 = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . SSD ( opcode , src1 , src2 , dest ) ; } case get_tuple_element : { SourceOperand src = readSource ( ) ; int i = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . SID ( opcode , src , i , dest ) ; } case set_tuple_element : { SourceOperand src = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; int i = readCodeInteger ( ) ; return new Insn . SDI ( opcode , src , dest , i ) ; } case get_list : { SourceOperand src = readSource ( ) ; DestinationOperand dest1 = readDestination ( ) ; DestinationOperand dest2 = readDestination ( ) ; return new Insn . SDD ( opcode , src , dest1 , dest2 ) ; } case test_arity : case bs_test_tail2 : case bs_test_unit : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; int i1 = readCodeInteger ( ) ; return new Insn . LDI ( opcode , label , dest , i1 , true ) ; } case is_lt : case is_ge : case is_eq : case is_ne : case is_eq_exact : case is_ne_exact : { Label label = readLabel ( ) ; SourceOperand src1 = readSource ( ) ; SourceOperand src2 = readSource ( ) ; return new Insn . LSS ( opcode , label , src1 , src2 , true ) ; } case is_function2 : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; SourceOperand src = readSource ( ) ; return new Insn . LDS ( opcode , label , dest , src , true ) ; } case fnegate : case bs_utf8_size : case bs_utf16_size : { Label label = readLabel ( ) ; SourceOperand src = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LSD ( opcode , label , src , dest ) ; } case call_last : { int i1 = readCodeInteger ( ) ; Label label = readLabel ( ) ; int i3 = readCodeInteger ( ) ; return new Insn . ILI ( opcode , i1 , label , i3 , functionAtLabel ( label . nr ) ) ; } case fadd : case fsub : case fmul : case fdiv : { Label label = readLabel ( ) ; SourceOperand src1 = readSource ( ) ; SourceOperand src2 = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LSSD ( opcode , label , src1 , src2 , dest , true ) ; } case bs_add : { Label label = readLabel ( ) ; SourceOperand src1 = readSource ( ) ; SourceOperand src2 = readSource ( ) ; int i3 = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LSSID ( opcode , label , src1 , src2 , i3 , dest ) ; } case bs_skip_utf8 : case bs_skip_utf16 : case bs_skip_utf32 : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; return new Insn . LDII ( opcode , label , dest , i3 , i4 ) ; } case bs_match_string : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; BitString bin = readBitstringRef ( ) ; return new Insn . LDBi ( opcode , label , dest , bin ) ; } case bs_put_utf8 : case bs_put_utf16 : case bs_put_utf32 : { Label label = readLabel ( ) ; int i2 = readCodeInteger ( ) ; SourceOperand src = readSource ( ) ; return new Insn . LIS ( opcode , label , i2 , src , true ) ; } case bs_start_match2 : case bs_get_utf8 : case bs_get_utf16 : case bs_get_utf32 : { Label label = readLabel ( ) ; DestinationOperand dest1 = readDestination ( ) ; int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; DestinationOperand dest2 = readDestination ( ) ; return new Insn . LDIID ( opcode , label , dest1 , i3 , i4 , dest2 , opcode != BeamOpcode . bs_start_match2 ) ; } case bs_put_integer : case bs_put_float : case bs_put_binary : { Label label = readLabel ( ) ; SourceOperand src2 = readSource ( ) ; int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; SourceOperand src5 = readSource ( ) ; return new Insn . LSIIS ( opcode , label , src2 , i3 , i4 , src5 , true ) ; } case bs_init2 : case bs_init_bits : { Label label = readOptionalLabel ( ) ; SourceOperand src2 ; if ( ( peekTag ( ) & <NUM_LIT> ) == CODEINT4_TAG ) { int i2 = readCodeInteger ( ) ; src2 = new Operands . Int ( i2 ) ; } else { src2 = readSource ( ) ; } int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; int i5 = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LSIIID ( opcode , label , src2 , i3 , i4 , i5 , dest , true ) ; } case bs_skip_bits2 : { Label label = readLabel ( ) ; DestinationOperand dest = readDestination ( ) ; SourceOperand src = readSource ( ) ; int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; return new Insn . LDSII ( opcode , label , dest , src , i3 , i4 ) ; } case bs_get_integer2 : case bs_get_float2 : case bs_get_binary2 : { Label label = readLabel ( ) ; DestinationOperand dest2 = readDestination ( ) ; int i3 = readCodeInteger ( ) ; SourceOperand src4 = readSource ( ) ; int i5 = readCodeInteger ( ) ; int i6 = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . LDISIID ( opcode , label , dest2 , i3 , src4 , i5 , i6 , dest ) ; } case bs_append : { Label label = readLabel ( ) ; SourceOperand src2 = readSource ( ) ; int i3 = readCodeInteger ( ) ; int i4 = readCodeInteger ( ) ; int i5 = readCodeInteger ( ) ; SourceOperand src6 = readSource ( ) ; int i7 = readCodeInteger ( ) ; DestinationOperand dest8 = readDestination ( ) ; return new Insn . BSAppend ( opcode , label , src2 , i3 , i4 , i5 , src6 , i7 , dest8 ) ; } case bs_private_append : { Label label = readLabel ( ) ; SourceOperand src2 = readSource ( ) ; int i3 = readCodeInteger ( ) ; SourceOperand src4 = readSource ( ) ; int i5 = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . BSPrivateAppend ( opcode , label , src2 , i3 , src4 , i5 , dest ) ; } case select_val : case select_tuple_arity : { SourceOperand src = readSource ( ) ; Label defaultLbl = readLabel ( ) ; SelectList jumpTable = readSelectList ( ) ; return new Insn . Select ( opcode , src , defaultLbl , jumpTable ) ; } case bif0 : { Label optLabel = readOptionalLabel ( ) ; int ext_fun_ref = readCodeInteger ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . Bif ( opcode , optLabel , extFun ( ext_fun_ref ) , dest ) ; } case bif1 : { Label optLabel = readOptionalLabel ( ) ; int ext_fun_ref = readCodeInteger ( ) ; SourceOperand arg = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . Bif ( opcode , optLabel , extFun ( ext_fun_ref ) , arg , dest ) ; } case bif2 : { Label optLabel = readOptionalLabel ( ) ; int ext_fun_ref = readCodeInteger ( ) ; SourceOperand arg1 = readSource ( ) ; SourceOperand arg2 = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . Bif ( opcode , optLabel , extFun ( ext_fun_ref ) , arg1 , arg2 , dest ) ; } case gc_bif1 : { Label optLabel = readOptionalLabel ( ) ; int save = readCodeInteger ( ) ; int ext_fun_ref = readCodeInteger ( ) ; SourceOperand arg = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . GcBif ( opcode , optLabel , extFun ( ext_fun_ref ) , save , arg , dest ) ; } case gc_bif2 : { Label optLabel = readOptionalLabel ( ) ; int save = readCodeInteger ( ) ; int ext_fun_ref = readCodeInteger ( ) ; SourceOperand arg1 = readSource ( ) ; SourceOperand arg2 = readSource ( ) ; DestinationOperand dest = readDestination ( ) ; return new Insn . GcBif ( opcode , optLabel , extFun ( ext_fun_ref ) , save , arg1 , arg2 , dest ) ; } default : throw new IOException ( "<STR_LIT>" + opcode ) ; } } else throw new IOException ( "<STR_LIT>" + Integer . toHexString ( opcode_no ) ) ; } public String readString ( int len ) throws IOException { return new String ( readBinary ( len ) ) ; } public byte [ ] readBinary ( int len ) throws IOException { byte [ ] data = new byte [ len ] ; in . readFully ( data ) ; return data ; } static final int CODEINT4_TAG = <NUM_LIT:0> ; static final int INTLIT4_TAG = <NUM_LIT:1> ; static final int ATOM4_TAG = <NUM_LIT:2> ; static final int XREG4_TAG = <NUM_LIT:3> ; static final int YREG4_TAG = <NUM_LIT:4> ; static final int LABEL4_TAG = <NUM_LIT:5> ; static final int EXTENDED_TAG = <NUM_LIT:7> ; static final int CODEINT12_TAG = <NUM_LIT:8> ; static final int BIGINT_TAG = <NUM_LIT:9> ; static final int ATOM12_TAG = <NUM_LIT:10> ; static final int XREG12_TAG = <NUM_LIT:11> ; static final int YREG12_TAG = <NUM_LIT:12> ; static final int LABEL12_TAG = <NUM_LIT> ; static final int EXTENDED2_TAG = <NUM_LIT:7> ; static final int FLOATLIT_TAG2 = <NUM_LIT:0> ; static final int SELECTLIST_TAG2 = <NUM_LIT:1> ; static final int FLOATREG_TAG2 = <NUM_LIT:2> ; static final int ALLOCLIST_TAG2 = <NUM_LIT:3> ; static final int LITERAL_TAG2 = <NUM_LIT:4> ; int peekTag ( ) throws IOException { return in . peek ( ) & <NUM_LIT> ; } public SourceOperand readSource ( ) throws IOException { return readOperand ( ) . asSource ( ) ; } public DestinationOperand readDestination ( ) throws IOException { return readOperand ( ) . asDestination ( ) ; } public Label readOptionalLabel ( ) throws IOException { return ( peekTag ( ) == LABEL4_TAG || peekTag ( ) == LABEL12_TAG ) ? readLabel ( ) : null ; } public Label readLabel ( ) throws IOException { return readOperand ( ) . asLabel ( ) ; } public Literal readLiteral ( ) throws IOException { return readOperand ( ) . asLiteral ( ) ; } public Atom readAtom ( ) throws IOException { return readOperand ( ) . asAtom ( ) ; } public BitString readBitstringRef ( ) throws IOException { int bits = readCodeInteger ( ) ; int start = readCodeInteger ( ) ; return new BitString ( bitstring ( start , bits ) ) ; } public ByteString readBytestringRef ( ) throws IOException { int bytes = readCodeInteger ( ) ; int start = readCodeInteger ( ) ; return new ByteString ( string ( start , bytes ) ) ; } public SelectList readSelectList ( ) throws IOException { return readOperand ( ) . asSelectList ( ) ; } public AllocList readAllocList ( ) throws IOException { switch ( peekTag ( ) ) { case CODEINT4_TAG : case CODEINT12_TAG : { int words = readCodeInteger ( ) ; return new AllocList ( words ) ; } case EXTENDED_TAG : { return readOperand ( ) . asAllocList ( ) ; } default : throw new IOException ( "<STR_LIT>" + readOperand ( ) . toSymbolic ( ) ) ; } } public YReg readYReg ( ) throws IOException { return readOperand ( ) . asYReg ( ) ; } public int readCodeInteger ( ) throws IOException { int d1 = in . read1 ( ) ; int tag = d1 & <NUM_LIT> ; if ( tag == CODEINT4_TAG ) return readSmallIntValue ( d1 ) ; else throw new IOException ( "<STR_LIT>" + readOperand ( d1 ) . toSymbolic ( ) ) ; } public Operand readOperand ( ) throws IOException { int d1 = in . read1 ( ) ; return readOperand ( d1 ) ; } public Operand readOperand ( int d1 ) throws IOException { int tag = d1 & <NUM_LIT> ; switch ( tag ) { case CODEINT4_TAG : return new Operands . CodeInt ( readSmallIntValue ( d1 ) ) ; case INTLIT4_TAG : { if ( ( d1 & <NUM_LIT> ) == <NUM_LIT:0> ) return new Operands . Int ( readSmallIntValue ( d1 ) ) ; else { int hdata = d1 > > <NUM_LIT:4> ; if ( ( hdata & <NUM_LIT:1> ) == <NUM_LIT:0> ) { return new Operands . Int ( ( hdata << <NUM_LIT:7> ) + in . read1 ( ) ) ; } else { int len ; if ( hdata < <NUM_LIT:15> ) { len = <NUM_LIT:2> + ( hdata > > <NUM_LIT:1> ) ; } else { len = <NUM_LIT:2> + ( hdata > > <NUM_LIT:1> ) + readCodeInteger ( ) ; } byte d [ ] = new byte [ len ] ; in . readFully ( d ) ; return Operands . makeInt ( d ) ; } } } case ATOM4_TAG : case ATOM12_TAG : { int nr = readSmallIntValue ( d1 ) ; return ( nr == <NUM_LIT:0> ) ? Operands . Nil : new Operands . Atom ( atom ( nr ) ) ; } case XREG4_TAG : case XREG12_TAG : { int nr = readSmallIntValue ( d1 ) ; return XReg . get ( nr ) ; } case YREG4_TAG : case YREG12_TAG : { int nr = readSmallIntValue ( d1 ) ; return YReg . get ( nr ) ; } case LABEL4_TAG : case LABEL12_TAG : { int nr = readSmallIntValue ( d1 ) ; return new Label ( nr ) ; } case EXTENDED_TAG : { int moretag = d1 > > <NUM_LIT:4> ; switch ( moretag ) { case FLOATLIT_TAG2 : { double value = Double . longBitsToDouble ( in . readBE ( <NUM_LIT:8> ) ) ; return new Operands . Float ( value ) ; } case SELECTLIST_TAG2 : { int length = readCodeInteger ( ) ; assert ( length % <NUM_LIT:2> == <NUM_LIT:0> ) ; Operand [ ] list = new Operand [ length ] ; for ( int i = <NUM_LIT:0> ; i < length ; ) { list [ i ++ ] = readOperand ( ) ; list [ i ++ ] = readLabel ( ) ; } return new SelectList ( list ) ; } case ALLOCLIST_TAG2 : { int length = readCodeInteger ( ) ; int [ ] list = new int [ <NUM_LIT:2> * length ] ; for ( int i = <NUM_LIT:0> ; i < length ; i ++ ) { list [ <NUM_LIT:2> * i ] = readCodeInteger ( ) ; list [ <NUM_LIT:2> * i + <NUM_LIT:1> ] = readCodeInteger ( ) ; } return new AllocList ( list ) ; } case FLOATREG_TAG2 : { int nr = readSmallIntValue ( in . read1 ( ) ) ; return new FReg ( nr ) ; } case LITERAL_TAG2 : { int nr = readSmallIntValue ( in . read1 ( ) ) ; return new TableLiteral ( literal ( nr ) ) ; } default : log . warning ( "<STR_LIT>" + moretag ) ; } break ; } default : log . warning ( "<STR_LIT>" + tag ) ; } return null ; } public int readSmallIntValue ( int head ) throws IOException { int tag = head & <NUM_LIT> ; int hdata = head > > <NUM_LIT:4> ; if ( ( tag & <NUM_LIT> ) == <NUM_LIT:0> ) { return hdata ; } else if ( ( hdata & <NUM_LIT:1> ) == <NUM_LIT:0> ) { return ( hdata << <NUM_LIT:7> ) + in . read1 ( ) ; } else { int len = <NUM_LIT:2> + ( hdata > > <NUM_LIT:1> ) ; byte d [ ] = new byte [ len ] ; in . readFully ( d ) ; BigInteger value = new BigInteger ( d ) ; if ( len > <NUM_LIT:4> || value . compareTo ( BigInteger . ZERO ) < <NUM_LIT:0> ) throw new IOException ( "<STR_LIT>" + value ) ; else return value . intValue ( ) ; } } static class SectionMetadata { final int tag , offset , length ; public SectionMetadata ( int tag , int offset , int length ) { this . tag = tag ; this . offset = offset ; this . length = length ; } } } </s>
|
<s> package erjang . beam . loader ; import erjang . beam . BeamOpcode ; import erjang . beam . repr . Insn ; import erjang . beam . repr . Operands . Label ; import erjang . beam . repr . FunctionInfo ; import erjang . EAtom ; import java . util . List ; import java . util . ListIterator ; public class Rewriter { public void rewriteFunctionBody ( List < Insn > body , FunctionInfo sig ) { ListIterator < Insn > it = body . listIterator ( ) ; while ( it . hasNext ( ) ) { final Insn insn = it . next ( ) ; switch ( insn . opcode ( ) ) { case call_fun : { int save_pos = savePos ( it ) ; boolean trigger = ( it . hasNext ( ) && it . next ( ) . opcode ( ) == BeamOpcode . deallocate && it . hasNext ( ) && it . next ( ) . opcode ( ) == BeamOpcode . K_return ) ; restorePos ( it , save_pos ) ; if ( trigger ) { Insn . I old = ( Insn . I ) insn ; it . set ( new Insn . I ( BeamOpcode . i_call_fun_last , old . i1 ) ) ; it . next ( ) ; it . remove ( ) ; it . next ( ) ; it . remove ( ) ; } } } } } private int savePos ( ListIterator < Insn > it ) { return it . nextIndex ( ) - <NUM_LIT:1> ; } private void restorePos ( ListIterator < Insn > it , int save_pos ) { int i = <NUM_LIT:0> ; while ( it . previousIndex ( ) >= save_pos ) { int pi = it . previousIndex ( ) ; Insn insn = it . previous ( ) ; i ++ ; } if ( it . nextIndex ( ) <= save_pos ) it . next ( ) ; } private Insn nop ( ) { return new Insn . I ( BeamOpcode . label , - <NUM_LIT:1> ) ; } } </s>
|
<s> package erjang . beam ; import erjang . beam . repr . Insn ; public interface BlockVisitor { public void visitInsn ( Insn insn ) ; public void visitEnd ( ) ; } </s>
|
<s> package erjang . beam ; import java . io . File ; import java . io . FileOutputStream ; import java . io . IOException ; import java . util . jar . JarOutputStream ; import java . util . zip . ZipEntry ; public class JarClassRepo implements ClassRepo { private JarOutputStream jo ; public JarClassRepo ( File jarFile ) throws IOException { FileOutputStream fo = new FileOutputStream ( jarFile ) ; jo = new JarOutputStream ( fo ) ; } @ Override public void close ( ) throws IOException { jo . close ( ) ; } @ Override public void store ( String internalName , byte [ ] data ) throws IOException { String out = internalName + "<STR_LIT:.class>" ; jo . putNextEntry ( new ZipEntry ( out ) ) ; jo . write ( data ) ; jo . closeEntry ( ) ; } } </s>
|
<s> package erjang . beam ; import java . util . Set ; import com . trifork . clj_ds . IPersistentCollection ; public interface BeamFunction { boolean isExported ( ) ; String getModuleName ( ) ; String getName ( ) ; int getArity ( ) ; Set < Integer > getXRegisters ( ) ; int getYregCount ( ) ; int getFregCount ( ) ; int getEntryLabel ( ) ; BeamCodeBlock [ ] getCodeBlocks ( ) ; } </s>
|
<s> package erjang . beam ; import erjang . EAtom ; public class CodeAtoms { public static final EAtom TRUE_ATOM = EAtom . intern ( "<STR_LIT:true>" ) ; public static final EAtom FALSE_ATOM = EAtom . intern ( "<STR_LIT:false>" ) ; public static final EAtom X_ATOM = EAtom . intern ( "<STR_LIT:x>" ) ; public static final EAtom Y_ATOM = EAtom . intern ( "<STR_LIT:y>" ) ; public static final EAtom FR_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom NIL_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom INTEGER_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom STRING_ATOM = EAtom . intern ( "<STR_LIT:string>" ) ; public static final EAtom FLOAT_ATOM = EAtom . intern ( "<STR_LIT:float>" ) ; public static final EAtom ATOM_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom LIST_ATOM = EAtom . intern ( "<STR_LIT:list>" ) ; public static final EAtom LITERAL_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom TEST_ATOM = EAtom . intern ( "<STR_LIT:test>" ) ; public static final EAtom ALLOC_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom WORDS_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom FLOATS_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom NOFAIL_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom START_ATOM = EAtom . intern ( "<STR_LIT:start>" ) ; public static final EAtom F_ATOM = EAtom . intern ( "<STR_LIT:f>" ) ; public static final EAtom FIELD_FLAGS_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom EXTFUNC_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom APPLY_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom ERLANG_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom ERROR_ATOM = EAtom . intern ( "<STR_LIT:error>" ) ; public static final EAtom EXIT_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom THROW_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom BEAM_FILE_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom FUNCTION_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom BIF_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom GCBIF_ATOM = EAtom . intern ( "<STR_LIT>" ) ; public static final EAtom ARITHFBIF_ATOM = EAtom . intern ( "<STR_LIT>" ) ; } </s>
|
<s> package erjang . beam ; import org . objectweb . asm . Type ; import org . objectweb . asm . commons . Method ; import erjang . beam . repr . ExtFun ; import erjang . EBinary ; import erjang . EBitString ; public interface BlockVisitor2 extends BlockVisitor { void visitBegin ( BeamExceptionHandler exh ) ; void visitInsn ( BeamOpcode insn ) ; void visitInsn ( BeamOpcode opcode , int failLabel , Arg arg1 , Method bif ) ; void visitInsn ( BeamOpcode opcode , int failLabel , Arg [ ] in , Arg out , BuiltInFunction bif ) ; void visitTest ( BeamOpcode test , int failLabel , Arg arg1 , Type out ) ; void visitInsn ( BeamOpcode opcode , Arg arg ) ; void visitInsn ( BeamOpcode opcode , ExtFun ext_fun ) ; void visitTest ( BeamOpcode test , int failLabel , Arg [ ] args , Type voidType ) ; void visitInsn ( BeamOpcode opcode , Arg arg1 , Arg arg2 ) ; void visitCall ( ExtFun fun , Arg [ ] args , boolean isTail , boolean isExternal ) ; void visitInsn ( BeamOpcode opcode , Arg [ ] ys ) ; void visitInsn ( BeamOpcode opcode , ExtFun efun , Arg [ ] freevars , int index , int old_index , EBinary uniq , int old_uniq ) ; void visitInsn ( BeamOpcode opcode , Arg [ ] in , Arg out ) ; void visitInsn ( BeamOpcode opcode , int val , Arg out ) ; void visitInsn ( BeamOpcode opcode , Arg val , Arg out , int pos ) ; void visitSelectTuple ( Arg in , int failLabel , int [ ] arities , int [ ] targets ) ; void visitSelectValue ( Arg in , int failLabel , Arg [ ] values , int [ ] targets ) ; void visitTest ( BeamOpcode test , int failLabel , Arg arg , int arity , Type tupleType ) ; void visitTest ( BeamOpcode test , int failLabel , Arg arg , Arg arity , Type funType ) ; void visitJump ( int decodeLabelref ) ; void visitReceive ( BeamOpcode opcode , int block_label , Arg out ) ; void visitInsn ( BeamOpcode opcode , int failLabel , Arg [ ] in , Arg ex ) ; void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , int intg , Arg dst ) ; void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , EBitString bin ) ; void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , Arg bits , int unit , int flags ) ; void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , Arg bits , int unit , int flags , Arg dst ) ; void visitBitStringTest ( BeamOpcode test , int failLabel , Arg in , int intg ) ; void visitInitBitString ( Arg size , int flags , Arg out , boolean unit_is_bits ) ; void visitBitStringPut ( BeamOpcode opcode , Arg value , Arg size , int unit , int flags ) ; void visitBitStringAppend ( BeamOpcode opcode , int label , Arg extra_size , Arg src , int unit , int flags , Arg dst ) ; void visitBS ( BeamOpcode opcode , Arg arg1 , Arg arg2 ) ; void visitBSAdd ( Arg in1 , Arg in2 , int scale , Arg out ) ; void visitUnreachablePoint ( ) ; void visitCatchBlockStart ( BeamOpcode opcode , int label , Arg out , BeamExceptionHandler exh ) ; void visitCatchBlockEnd ( BeamOpcode opcode , Arg out , BeamExceptionHandler exh ) ; void visitIncrement ( Arg src , Arg out ) ; void visitDecrement ( Arg src , Arg out ) ; void visitInitWritable ( Arg size , Arg dest ) ; } </s>
|
<s> package erjang . beam . interpreter ; import java . util . List ; import java . util . ArrayList ; import java . util . HashMap ; import java . util . logging . Level ; import java . util . logging . Logger ; import erjang . EModule ; import erjang . EModuleManager ; import erjang . EFun ; import erjang . FunID ; import erjang . EModuleClassLoader ; import erjang . ERT ; import erjang . ErlangError ; import erjang . ErlangException ; import erjang . EObject ; import erjang . EAtom ; import erjang . ESeq ; import erjang . ETuple3 ; import erjang . EPseudoTerm ; import erjang . beam . ModuleVisitor ; import erjang . beam . FunctionVisitor ; import erjang . beam . repr . Insn ; import erjang . beam . repr . Operands ; import erjang . beam . repr . ExtFun ; import erjang . beam . repr . FunctionInfo ; import kilim . Pausable ; public class AbstractInterpreter { static Logger log = Logger . getLogger ( "<STR_LIT>" ) ; public static abstract class Encoder implements ModuleVisitor { private EAtom moduleName ; final HashMap < Integer , Insn > insn_start = new HashMap < Integer , Insn > ( ) ; final ArrayList < Character > code = new ArrayList < Character > ( ) ; final ArrayList < EObject > consts = new ArrayList < EObject > ( ) ; final ArrayList < ValueJumpTable > value_jump_tables = new ArrayList < ValueJumpTable > ( ) ; final ArrayList < ArityJumpTable > arity_jump_tables = new ArrayList < ArityJumpTable > ( ) ; final HashMap < EObject , Integer > const_map = new HashMap < EObject , Integer > ( ) ; final HashMap < Integer , Integer > label_map = new HashMap < Integer , Integer > ( ) ; final ArrayList < Backpatch > backpatches = new ArrayList < Backpatch > ( ) ; final HashMap < FunIDWithGuardedness , Integer > ext_fun_map = new HashMap < FunIDWithGuardedness , Integer > ( ) ; final ArrayList < FunIDWithGuardedness > imports = new ArrayList < FunIDWithGuardedness > ( ) ; final ArrayList < FunctionInfo > raw_exports = new ArrayList < FunctionInfo > ( ) ; public void visitModule ( EAtom name ) { this . moduleName = name ; } public void visitExport ( EAtom fun , int arity , int entryLabel ) { raw_exports . add ( new FunctionInfo ( moduleName , fun , arity , entryLabel ) ) ; } public void visitAttribute ( EAtom att , EObject value ) { } public void visitEnd ( ) { log . fine ( "<STR_LIT>" + moduleName + "<STR_LIT>" ) ; for ( Backpatch bp : backpatches ) { bp . patch ( label_map . get ( bp . label ) ) ; } if ( log . isLoggable ( Level . FINE ) ) { for ( int i = <NUM_LIT:0> ; i < code . size ( ) ; i ++ ) { Insn insn = insn_start . get ( i ) ; log . fine ( ( insn != null ? "<STR_LIT:*>" : "<STR_LIT:U+0020>" ) + i + "<STR_LIT::U+0020>" + ( int ) code . get ( i ) + ( insn != null ? ( "<STR_LIT:t>" + insn . toSymbolic ( ) . toString ( ) ) : "<STR_LIT>" ) ) ; } } } public EModule toEModule ( ) { char [ ] codeArray = toArray ( code ) ; EObject [ ] constArray = consts . toArray ( new EObject [ consts . size ( ) ] ) ; ValueJumpTable [ ] valueJumpTableArray = value_jump_tables . toArray ( new ValueJumpTable [ value_jump_tables . size ( ) ] ) ; ArityJumpTable [ ] arityJumpTableArray = arity_jump_tables . toArray ( new ArityJumpTable [ arity_jump_tables . size ( ) ] ) ; List < FunIDWithEntry > exports = convertExports ( raw_exports ) ; log . fine ( "<STR_LIT>" + moduleName . getName ( ) ) ; return makeModule ( moduleName . getName ( ) , codeArray , constArray , valueJumpTableArray , arityJumpTableArray , exports , imports ) ; } protected abstract EModule makeModule ( String name , char [ ] code , EObject [ ] consts , ValueJumpTable [ ] value_jump_tables , ArityJumpTable [ ] arity_jump_tables , List < FunIDWithEntry > exports , List < FunIDWithGuardedness > imports ) ; static char [ ] toArray ( List < Character > org ) { int len = org . size ( ) ; char [ ] res = new char [ len ] ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) res [ i ] = org . get ( i ) ; return res ; } List < FunIDWithEntry > convertExports ( List < FunctionInfo > org ) { List < FunIDWithEntry > res = new ArrayList ( org . size ( ) ) ; for ( FunctionInfo fi : org ) { res . add ( new FunIDWithEntry ( fi . mod , fi . fun , fi . arity , label_map . get ( fi . label ) ) ) ; } return res ; } public void declareFunction ( EAtom name , int arity , int startLabel ) { } public abstract FunctionVisitor visitFunction ( EAtom name , int arity , int startLabel ) ; protected int codePos ( ) { return code . size ( ) ; } protected void emit ( char val ) { code . add ( val ) ; } protected void emitAt ( int pos , char val ) { code . set ( pos , val ) ; } protected void nop ( int code_pos ) { code . remove ( code_pos ) ; code . subList ( code_pos , code . size ( ) ) . clear ( ) ; } protected void emit ( int intval ) { char val = ( char ) intval ; if ( val != intval ) throw new Error ( "<STR_LIT>" + intval ) ; code . add ( val ) ; } protected void emitAt ( int pos , int intval ) { char val = ( char ) intval ; if ( val != intval ) throw new Error ( "<STR_LIT>" + intval ) ; code . set ( pos , val ) ; } protected int emitPlaceholder ( ) { int pos = codePos ( ) ; code . add ( ( char ) <NUM_LIT> ) ; return pos ; } protected int encodeLiteral ( Operands . Literal lit ) { EObject value = lit . literalValue ( ) ; Integer index = const_map . get ( value ) ; if ( index == null ) { index = const_map . size ( ) ; consts . add ( value ) ; const_map . put ( value , index ) ; } return index ; } protected int encodeExtFun ( ExtFun extfun ) { FunIDWithGuardedness id = new FunIDWithGuardedness ( extfun . mod , extfun . fun , extfun . arity , false ) ; return encodeExtFun_common ( id ) ; } protected int encodeGuardExtFun ( ExtFun extfun ) { FunIDWithGuardedness id = new FunIDWithGuardedness ( extfun . mod , extfun . fun , extfun . arity , true ) ; return encodeExtFun_common ( id ) ; } protected int encodeExtFun_common ( FunIDWithGuardedness id ) { Integer index = ext_fun_map . get ( id ) ; if ( index == null ) { index = imports . size ( ) ; imports . add ( id ) ; ext_fun_map . put ( id , index ) ; } return index ; } protected int encodeValueJumpTable ( Operands . SelectList jumpTable ) { final ValueJumpTable table = new ValueJumpTable ( ) ; for ( int i = <NUM_LIT:0> ; i < jumpTable . size ( ) ; i ++ ) { final EObject value = ( ( Operands . Literal ) jumpTable . getValue ( i ) ) . literalValue ( ) ; int label = jumpTable . getLabel ( i ) . nr ; table . put ( value , - <NUM_LIT> ) ; backpatches . add ( new Backpatch ( label ) { public String toString ( ) { return "<STR_LIT>" + label + "<STR_LIT:>>" ; } public void patch ( int labelOffset ) { table . put ( value , labelOffset ) ; } } ) ; } int index = value_jump_tables . size ( ) ; value_jump_tables . add ( table ) ; return index ; } protected int encodeArityJumpTable ( Operands . SelectList jumpTable ) { final ArityJumpTable table = new ArityJumpTable ( ) ; for ( int i = <NUM_LIT:0> ; i < jumpTable . size ( ) ; i ++ ) { final int arity = ( ( Operands . CodeInt ) jumpTable . getValue ( i ) ) . value ; int label = jumpTable . getLabel ( i ) . nr ; table . put ( arity , - <NUM_LIT> ) ; backpatches . add ( new Backpatch ( label ) { public String toString ( ) { return "<STR_LIT>" + label + "<STR_LIT:>>" ; } public void patch ( int labelOffset ) { table . put ( arity , labelOffset ) ; } } ) ; } int index = arity_jump_tables . size ( ) ; arity_jump_tables . add ( table ) ; return index ; } protected int encodeLabel ( int label ) { if ( label_map . containsKey ( label ) ) { return ( char ) label_map . get ( label ) . intValue ( ) ; } else { if ( label != <NUM_LIT:0> ) { final int codePos = codePos ( ) ; backpatches . add ( new Backpatch ( label ) { public String toString ( ) { return "<STR_LIT>" + label + "<STR_LIT>" + codePos + "<STR_LIT:>>" ; } public void patch ( int labelOffset ) { emitAt ( codePos , ( char ) labelOffset ) ; } } ) ; } return ( char ) - <NUM_LIT:1> ; } } protected void registerLabel ( int beamLabel ) { assert ( ! label_map . containsKey ( beamLabel ) ) ; label_map . put ( beamLabel , codePos ( ) ) ; } } public static abstract class Module extends EModule { final protected String name ; final protected ClassLoader module_class_loader ; protected Module ( String name , boolean delay_setup ) { super ( delay_setup ) ; this . name = name ; this . module_class_loader = new EModuleClassLoader ( null ) ; } public ClassLoader getModuleClassLoader ( ) { return module_class_loader ; } public String module_name ( ) { return name ; } } protected static EObject [ ] getRegs ( erjang . EProc proc ) { if ( proc . regs == null ) { proc . regs = new EObject [ <NUM_LIT> ] ; } return proc . regs ; } protected static EObject [ ] ensureCapacity ( EObject [ ] array , int atLeast ) { if ( atLeast >= array . length ) { EObject [ ] tmp = new EObject [ ( atLeast ) * <NUM_LIT:3> / <NUM_LIT:2> ] ; System . arraycopy ( array , <NUM_LIT:0> , tmp , <NUM_LIT:0> , array . length ) ; array = tmp ; } return array ; } protected static EObject [ ] ensureCapacity ( EObject [ ] array , int atLeast , int keep ) { if ( atLeast >= array . length ) { EObject [ ] tmp = new EObject [ ( atLeast ) * <NUM_LIT:3> / <NUM_LIT:2> ] ; System . arraycopy ( array , <NUM_LIT:0> , tmp , <NUM_LIT:0> , keep ) ; array = tmp ; } return array ; } public static ESeq xregsSeq ( EObject [ ] reg , int arity ) { ESeq res = ERT . NIL ; for ( int i = arity - <NUM_LIT:1> ; i >= <NUM_LIT:0> ; i -- ) { res = res . cons ( reg [ i ] ) ; } return res ; } public static EObject [ ] xregsArray ( EObject [ ] reg , int arity ) { EObject [ ] res = new EObject [ arity ] ; System . arraycopy ( reg , <NUM_LIT:0> , res , <NUM_LIT:0> , arity ) ; return res ; } public static int nofailLabel ( ) { throw new ErlangError ( EAtom . intern ( "<STR_LIT>" ) ) ; } static class FunIDWithEntry extends FunID { final int start_pc ; public FunIDWithEntry ( EAtom mod , EAtom fun , int arity , int start_pc ) { super ( mod , fun , arity ) ; this . start_pc = start_pc ; } } static class FunIDWithGuardedness { final FunID fun ; final boolean is_guard ; public FunIDWithGuardedness ( EAtom mod , EAtom fun , int arity , boolean is_guard ) { this . fun = new FunID ( mod , fun , arity ) ; this . is_guard = is_guard ; } @ Override public int hashCode ( ) { return fun . hashCode ( ) + ( is_guard ? <NUM_LIT> : <NUM_LIT:0> ) ; } @ Override public boolean equals ( Object other ) { return ( other instanceof FunIDWithGuardedness ) && ( ( FunIDWithGuardedness ) other ) . fun . equals ( this . fun ) && ( ( FunIDWithGuardedness ) other ) . is_guard == this . is_guard ; } } static class ValueJumpTable extends HashMap < EObject , Integer > { public int lookup ( EObject key , int defaultLabel ) { Integer lbl = get ( key ) ; return ( lbl != null ) ? lbl . intValue ( ) : defaultLabel ; } } static class ArityJumpTable extends HashMap < Integer , Integer > { public int lookup ( int key , int defaultLabel ) { Integer lbl = get ( key ) ; return ( lbl != null ) ? lbl . intValue ( ) : defaultLabel ; } } static abstract class Backpatch { final int label ; public Backpatch ( int label ) { this . label = label ; } public abstract void patch ( int labelOffset ) ; } protected static class VectorFunBinder extends EModuleManager . FunctionBinder { final EFun [ ] vector ; final FunID funID ; final int index ; public VectorFunBinder ( EFun [ ] vector , FunID funID , int index ) { this . vector = vector ; this . funID = funID ; this . index = index ; } public FunID getFunID ( ) { return funID ; } public void bind ( EFun value ) { vector [ index ] = value ; } } static abstract class ExceptionHandlerStackElement extends EPseudoTerm { final int pc ; final ExceptionHandlerStackElement next ; public ExceptionHandlerStackElement ( int pc , ExceptionHandlerStackElement next ) { this . pc = pc ; this . next = next ; } public String toString ( ) { return "<STR_LIT>" + pc + "<STR_LIT:U+002C>" + next + "<STR_LIT:)>" ; } public abstract void catchAction ( ErlangException e , EObject [ ] reg ) ; @ Override public int hashCode ( ) { return <NUM_LIT:2> ; } } static class CatchExceptionHandler extends ExceptionHandlerStackElement { public CatchExceptionHandler ( int pc , ExceptionHandlerStackElement next ) { super ( pc , next ) ; } public void catchAction ( ErlangException e , EObject [ ] reg ) { reg [ <NUM_LIT:0> ] = e . getCatchValue ( ) ; } } static class TryExceptionHandler extends ExceptionHandlerStackElement { public TryExceptionHandler ( int pc , ExceptionHandlerStackElement next ) { super ( pc , next ) ; } public void catchAction ( ErlangException e , EObject [ ] reg ) { ETuple3 tmp = e . getTryValue ( ) ; reg [ <NUM_LIT:0> ] = tmp . elem1 ; reg [ <NUM_LIT:1> ] = tmp . elem2 ; reg [ <NUM_LIT:2> ] = tmp . elem3 ; } } } </s>
|
<s> package erjang . beam ; import java . lang . reflect . Modifier ; import kilim . Pausable ; import org . objectweb . asm . Type ; import org . objectweb . asm . commons . Method ; public class BuiltInFunction { public final Type owner ; public final java . lang . reflect . Method javaMethod ; public final Method method ; public final boolean isVirtual ; public final boolean isPausable ; @ Override public String toString ( ) { return method . toString ( ) ; } @ Override public int hashCode ( ) { return toString ( ) . hashCode ( ) ; } public BuiltInFunction ( java . lang . reflect . Method m ) { this . javaMethod = m ; this . owner = Type . getType ( m . getDeclaringClass ( ) ) ; this . method = new Method ( m . getName ( ) , Type . getType ( m . getReturnType ( ) ) , Type . getArgumentTypes ( m ) ) ; isVirtual = ! Modifier . isStatic ( m . getModifiers ( ) ) ; boolean p = false ; for ( Class c : m . getExceptionTypes ( ) ) { if ( Pausable . class . equals ( c ) ) { p = true ; break ; } } isPausable = p ; } public Type [ ] getArgumentTypes ( ) { return method . getArgumentTypes ( ) ; } public String getName ( ) { return method . getName ( ) ; } public String getDescriptor ( ) { return method . getDescriptor ( ) ; } public Type getReturnType ( ) { return method . getReturnType ( ) ; } public Type getOwner ( ) { return owner ; } public boolean isVirtual ( ) { return isVirtual ; } public boolean isPausable ( ) { return isPausable ; } } </s>
|
<s> package erjang . beam ; public interface BeamCodeBlock { boolean isDeadCode ( ) ; int getLabel ( ) ; BeamInstruction [ ] getInstructions ( ) ; } </s>
|
<s> package erjang . beam ; public interface BeamFileData { public void accept ( ModuleVisitor v ) ; } </s>
|
<s> package erjang . console ; import java . awt . Color ; import java . awt . Toolkit ; import java . awt . datatransfer . Clipboard ; import java . awt . datatransfer . DataFlavor ; import java . awt . datatransfer . Transferable ; import java . awt . event . KeyEvent ; import java . awt . event . KeyListener ; import java . io . IOException ; import java . io . InputStream ; import java . io . OutputStream ; import java . nio . ByteBuffer ; import java . nio . ByteOrder ; import javax . swing . JTextPane ; import javax . swing . SwingUtilities ; import javax . swing . text . AbstractDocument ; import javax . swing . text . AttributeSet ; import javax . swing . text . BadLocationException ; import javax . swing . text . Document ; import javax . swing . text . DocumentFilter ; import javax . swing . text . MutableAttributeSet ; import javax . swing . text . SimpleAttributeSet ; import javax . swing . text . StyleConstants ; import kilim . Pausable ; import erjang . EBinary ; import erjang . EHandle ; import erjang . EObject ; import erjang . EPID ; import erjang . ERT ; import erjang . EString ; import erjang . driver . EDriverInstance ; import erjang . driver . IO ; public class TTYTextAreaDriverControl extends EDriverInstance implements KeyListener { static final int OP_PUTC = <NUM_LIT:0> ; static final int OP_MOVE = <NUM_LIT:1> ; static final int OP_INSC = <NUM_LIT:2> ; static final int OP_DELC = <NUM_LIT:3> ; static final int OP_BEEP = <NUM_LIT:4> ; static final int CTRL_OP_GET_WINSIZE = <NUM_LIT:100> ; static final int CTRL_OP_GET_UNICODE_STATE = <NUM_LIT> ; static final int CTRL_OP_SET_UNICODE_STATE = <NUM_LIT> ; static final int CONTROL_TAG = <NUM_LIT> ; static final int ESCAPED_TAG = <NUM_LIT> ; static final int TAG_MASK = <NUM_LIT> ; static final int MAXSIZE = <NUM_LIT:1> << <NUM_LIT:16> ; private JTextPane area ; private SimpleAttributeSet promptStyle ; private SimpleAttributeSet inputStyle ; private SimpleAttributeSet outputStyle ; private SimpleAttributeSet resultStyle ; private int startPos ; private boolean utf8_mode = true ; private Clipboard clipboard ; private SimpleAttributeSet errorStyle ; private static final int MAX_DOC_SIZE = <NUM_LIT> ; public TTYTextAreaDriverControl ( TTYTextAreaDriver driver , JTextPane text , String message ) { super ( driver ) ; this . clipboard = text . getToolkit ( ) . getSystemClipboard ( ) ; this . area = text ; text . addKeyListener ( this ) ; text . setAutoscrolls ( true ) ; if ( text . getDocument ( ) instanceof AbstractDocument ) ( ( AbstractDocument ) text . getDocument ( ) ) . setDocumentFilter ( new DocumentFilter ( ) { public void insertString ( DocumentFilter . FilterBypass fb , int offset , String string , AttributeSet attr ) throws BadLocationException { if ( offset >= startPos ) super . insertString ( fb , offset , string , attr ) ; } public void remove ( DocumentFilter . FilterBypass fb , int offset , int length ) throws BadLocationException { if ( offset >= startPos || offset == <NUM_LIT:0> ) super . remove ( fb , offset , length ) ; } public void replace ( DocumentFilter . FilterBypass fb , int offset , int length , String text , AttributeSet attrs ) throws BadLocationException { if ( offset >= startPos ) super . replace ( fb , offset , length , text , attrs ) ; } } ) ; promptStyle = new SimpleAttributeSet ( ) ; StyleConstants . setForeground ( promptStyle , new Color ( <NUM_LIT> , <NUM_LIT:0x00> , <NUM_LIT:0x00> ) ) ; inputStyle = new SimpleAttributeSet ( ) ; StyleConstants . setForeground ( inputStyle , new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; outputStyle = new SimpleAttributeSet ( ) ; StyleConstants . setForeground ( outputStyle , Color . darkGray ) ; errorStyle = new SimpleAttributeSet ( ) ; StyleConstants . setForeground ( errorStyle , Color . red ) ; resultStyle = new SimpleAttributeSet ( ) ; StyleConstants . setItalic ( resultStyle , true ) ; StyleConstants . setForeground ( resultStyle , new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; if ( message != null ) { final MutableAttributeSet messageStyle = new SimpleAttributeSet ( ) ; StyleConstants . setBackground ( messageStyle , text . getForeground ( ) ) ; StyleConstants . setForeground ( messageStyle , text . getBackground ( ) ) ; append ( message , messageStyle ) ; } startPos = text . getDocument ( ) . getLength ( ) ; } public InputStream getInputStream ( ) { return ERT . getInputStream ( ) ; } public OutputStream getOutputStream ( ) { return new Output ( outputStyle ) ; } public OutputStream getErrorStream ( ) { return new Output ( errorStyle ) ; } class Output extends OutputStream { private AttributeSet style ; public Output ( AttributeSet style ) { this . style = style ; } @ Override public void write ( int b ) throws IOException { String data = new String ( new byte [ ] { ( byte ) b } , IO . UTF8 ) ; append ( data , style ) ; } @ Override public void write ( byte [ ] b , int off , int len ) throws IOException { String data = new String ( b , off , len , IO . UTF8 ) ; append ( data , style ) ; } } protected void append ( final String toAppend , final AttributeSet style ) { SwingUtilities . invokeLater ( new Runnable ( ) { public void run ( ) { try { Document doc = area . getDocument ( ) ; doc . insertString ( doc . getLength ( ) , toAppend , style ) ; area . setCaretPosition ( doc . getLength ( ) ) ; int extra = doc . getLength ( ) - MAX_DOC_SIZE ; if ( extra > <NUM_LIT:0> ) { int removeBytes = extra + MAX_DOC_SIZE / <NUM_LIT:10> ; doc . remove ( <NUM_LIT:0> , removeBytes ) ; startPos -= removeBytes ; } } catch ( BadLocationException e ) { } } } ) ; } @ Override protected void output ( EHandle caller , ByteBuffer buf ) throws IOException , Pausable { switch ( buf . get ( ) ) { case OP_PUTC : put_chars ( buf ) ; break ; case OP_MOVE : move_rel ( buf . getShort ( ) ) ; break ; case OP_INSC : ins_chars ( buf ) ; break ; case OP_DELC : del_chars ( buf . getShort ( ) ) ; break ; case OP_BEEP : visible_beep ( ) ; break ; default : break ; } return ; } private void visible_beep ( ) { { Color fg = area . getForeground ( ) ; Color bg = area . getBackground ( ) ; area . setForeground ( bg ) ; area . setBackground ( fg ) ; area . repaint ( ) ; Toolkit . getDefaultToolkit ( ) . beep ( ) ; try { Thread . sleep ( <NUM_LIT:100> ) ; } catch ( InterruptedException e ) { } area . setForeground ( fg ) ; area . setBackground ( bg ) ; area . repaint ( ) ; } } private void del_chars ( int i ) { try { Document doc = area . getDocument ( ) ; int offs = area . getCaretPosition ( ) + i ; doc . remove ( offs , - i ) ; } catch ( BadLocationException e ) { } } private void ins_chars ( ByteBuffer buf ) { final String string = new String ( buf . array ( ) , buf . arrayOffset ( ) + buf . position ( ) , buf . remaining ( ) , IO . UTF8 ) ; SwingUtilities . invokeLater ( new Runnable ( ) { public void run ( ) { try { Document doc = area . getDocument ( ) ; int pos = area . getCaretPosition ( ) ; doc . insertString ( pos , string , inputStyle ) ; area . setCaretPosition ( pos + string . length ( ) ) ; area . select ( area . getCaretPosition ( ) , area . getCaretPosition ( ) ) ; int extra = doc . getLength ( ) - MAX_DOC_SIZE ; if ( extra > <NUM_LIT:0> ) { int removeBytes = extra + MAX_DOC_SIZE / <NUM_LIT:10> ; doc . remove ( <NUM_LIT:0> , removeBytes ) ; startPos -= removeBytes ; } } catch ( BadLocationException e ) { } } } ) ; } private void move_rel ( final int delta_pos ) { if ( delta_pos != <NUM_LIT:0> ) { SwingUtilities . invokeLater ( new Runnable ( ) { public void run ( ) { int curr = area . getCaretPosition ( ) ; area . setCaretPosition ( curr + delta_pos ) ; area . select ( area . getCaretPosition ( ) , area . getCaretPosition ( ) ) ; } } ) ; } } private void put_chars ( ByteBuffer buf ) { String string = new String ( buf . array ( ) , buf . arrayOffset ( ) + buf . position ( ) , buf . remaining ( ) , IO . UTF8 ) ; append ( string , outputStyle ) ; } private void put_chars ( String string ) { append ( string , outputStyle ) ; } @ Override public void keyTyped ( KeyEvent e ) { e . consume ( ) ; } @ Override public void keyReleased ( KeyEvent e ) { } @ Override public void keyPressed ( KeyEvent event ) { int code = event . getKeyCode ( ) ; if ( code == KeyEvent . VK_COPY || ( code == KeyEvent . VK_C && event . isMetaDown ( ) ) ) { return ; } if ( code == KeyEvent . VK_PASTE || ( code == KeyEvent . VK_V && event . isMetaDown ( ) ) ) { Transferable clipData = clipboard . getContents ( clipboard ) ; try { if ( clipData . isDataFlavorSupported ( DataFlavor . stringFlavor ) ) { String s = ( String ) ( clipData . getTransferData ( DataFlavor . stringFlavor ) ) ; out ( s ) ; } } catch ( Exception ufe ) { } event . consume ( ) ; return ; } if ( code == KeyEvent . VK_CUT || ( code == KeyEvent . VK_X && event . isMetaDown ( ) ) ) { event . consume ( ) ; visible_beep ( ) ; return ; } event . consume ( ) ; switch ( code ) { case KeyEvent . VK_TAB : out ( '<STR_LIT:\t>' ) ; return ; case KeyEvent . VK_ESCAPE : out_byte ( ( byte ) <NUM_LIT> ) ; return ; case KeyEvent . VK_LEFT : out_ctrl ( '<CHAR_LIT:b>' ) ; return ; case KeyEvent . VK_DELETE : out_ctrl ( '<CHAR_LIT>' ) ; return ; case KeyEvent . VK_RIGHT : out_ctrl ( '<CHAR_LIT>' ) ; return ; case KeyEvent . VK_BACK_SPACE : out_ctrl ( '<CHAR_LIT>' ) ; return ; case KeyEvent . VK_ENTER : out_ctrl ( '<CHAR_LIT>' ) ; return ; case KeyEvent . VK_DOWN : out_ctrl ( '<CHAR_LIT>' ) ; return ; case KeyEvent . VK_UP : out_ctrl ( '<CHAR_LIT>' ) ; return ; } if ( event . getKeyChar ( ) == KeyEvent . CHAR_UNDEFINED ) { return ; } if ( ( event . getModifiersEx ( ) & KeyEvent . CTRL_DOWN_MASK ) != <NUM_LIT:0> ) { if ( code >= KeyEvent . VK_A && code <= KeyEvent . VK_Z ) { out_ctrl ( '<CHAR_LIT:a>' + code - KeyEvent . VK_A ) ; } } else { out ( event . getKeyChar ( ) ) ; } } private void out_ctrl ( int i ) { if ( i >= '<CHAR_LIT:a>' && i <= '<CHAR_LIT>' ) { out_byte ( ( byte ) ( ( i - '<CHAR_LIT:a>' ) + <NUM_LIT:1> ) ) ; } } private void out_byte ( int b ) { ByteBuffer buf = ByteBuffer . wrap ( new byte [ ] { ( byte ) b } ) ; EObject out ; if ( task . send_binary_data ( ) ) { out = EBinary . make ( buf ) ; } else { out = EString . make ( buf ) ; } task . output_from_driver_b ( out ) ; } private void out ( char ch ) { String string = new String ( new char [ ] { ch } ) ; out ( string ) ; } private void out ( String string ) { byte [ ] bytes = string . getBytes ( IO . UTF8 ) ; ByteBuffer buf = ByteBuffer . wrap ( bytes ) ; EObject out ; if ( task . send_binary_data ( ) ) { out = EBinary . make ( buf ) ; } else { out = EString . make ( buf ) ; } task . output_from_driver_b ( out ) ; } @ Override protected ByteBuffer control ( EPID pid , int command , ByteBuffer cmd ) throws Pausable { if ( command == CTRL_OP_GET_WINSIZE ) { ByteBuffer rep = ByteBuffer . allocate ( <NUM_LIT:8> ) ; rep . order ( ByteOrder . nativeOrder ( ) ) ; rep . putInt ( <NUM_LIT> ) ; rep . putInt ( <NUM_LIT> ) ; return rep ; } else if ( command == CTRL_OP_GET_UNICODE_STATE ) { ByteBuffer rep = ByteBuffer . allocate ( <NUM_LIT:1> ) ; rep . put ( ( byte ) ( utf8_mode ? <NUM_LIT:1> : <NUM_LIT:0> ) ) ; return rep ; } else if ( command == CTRL_OP_SET_UNICODE_STATE && cmd . remaining ( ) == <NUM_LIT:1> ) { ByteBuffer rep = ByteBuffer . allocate ( <NUM_LIT:1> ) ; rep . put ( ( byte ) ( utf8_mode ? <NUM_LIT:1> : <NUM_LIT:0> ) ) ; utf8_mode = cmd . get ( ) == <NUM_LIT:0> ? false : true ; return rep ; } else { return null ; } } } </s>
|
<s> package erjang . console ; import java . awt . BorderLayout ; import java . awt . Color ; import java . awt . Component ; import java . awt . Dimension ; import java . awt . Font ; import java . awt . Graphics ; import java . awt . GraphicsEnvironment ; import java . awt . Insets ; import java . awt . SystemColor ; import java . awt . event . WindowAdapter ; import java . awt . event . WindowEvent ; import java . util . Arrays ; import java . util . Timer ; import java . util . TimerTask ; import javax . swing . BorderFactory ; import javax . swing . Icon ; import javax . swing . JFrame ; import javax . swing . JLabel ; import javax . swing . JPanel ; import javax . swing . JScrollPane ; import javax . swing . JTextPane ; import javax . swing . ScrollPaneConstants ; import javax . swing . SwingUtilities ; import erjang . ERT ; import erjang . util . Progress ; import erjang . util . ProgressListener ; public class ERLConsole extends JFrame { public ERLConsole ( String title ) { super ( title ) ; } static final int HINSET = <NUM_LIT:8> ; public static void main ( final String [ ] args ) { ERLConsole console = new ERLConsole ( "<STR_LIT>" ) ; console . getContentPane ( ) . setLayout ( new BorderLayout ( ) ) ; console . setSize ( <NUM_LIT> , <NUM_LIT> ) ; final JTextPane text = new JTextPane ( ) ; text . setMargin ( new Insets ( <NUM_LIT:8> , HINSET , <NUM_LIT:8> , HINSET ) ) ; text . setCaretColor ( new Color ( <NUM_LIT> , <NUM_LIT:0x00> , <NUM_LIT:0x00> ) ) ; text . setBackground ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; text . setForeground ( new Color ( <NUM_LIT> , <NUM_LIT:0x00> , <NUM_LIT:0x00> ) ) ; Font font = console . findFont ( "<STR_LIT>" , Font . PLAIN , <NUM_LIT> , new String [ ] { "<STR_LIT>" , "<STR_LIT>" } ) ; text . setFont ( font ) ; JScrollPane pane = new JScrollPane ( ) ; pane . setHorizontalScrollBarPolicy ( ScrollPaneConstants . HORIZONTAL_SCROLLBAR_NEVER ) ; pane . setViewportView ( text ) ; pane . setBorder ( BorderFactory . createLineBorder ( Color . darkGray ) ) ; console . getContentPane ( ) . add ( pane , BorderLayout . CENTER ) ; final StatusBar status = new StatusBar ( ) ; console . getContentPane ( ) . add ( status , BorderLayout . SOUTH ) ; console . validate ( ) ; final TTYTextAreaDriver tty = new TTYTextAreaDriver ( text , "<STR_LIT>" ) ; console . addWindowListener ( new WindowAdapter ( ) { public void windowClosing ( WindowEvent e ) { ERT . shutdown ( ) ; } } ) ; console . setVisible ( true ) ; Timer timer = new Timer ( true ) ; timer . schedule ( new TimerTask ( ) { @ Override public void run ( ) { status . updateMemory ( ) ; } } , <NUM_LIT:1000> , <NUM_LIT:1000> ) ; erjang . OTPMain . add_driver ( tty ) ; Thread t2 = new Thread ( ) { public void run ( ) { try { erjang . Main . main ( args ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } } ; t2 . start ( ) ; try { t2 . join ( ) ; } catch ( InterruptedException ie ) { } timer . cancel ( ) ; timer . purge ( ) ; timer = null ; console . setVisible ( false ) ; console . dispose ( ) ; console = null ; } private Font findFont ( String otherwise , int style , int size , String [ ] families ) { String [ ] fonts = GraphicsEnvironment . getLocalGraphicsEnvironment ( ) . getAvailableFontFamilyNames ( ) ; Arrays . sort ( fonts ) ; Font font = null ; for ( int i = <NUM_LIT:0> ; i < families . length ; i ++ ) { if ( Arrays . binarySearch ( fonts , families [ i ] ) >= <NUM_LIT:0> ) { font = new Font ( families [ i ] , style , size ) ; break ; } } if ( font == null ) font = new Font ( otherwise , style , size ) ; return font ; } private static final long serialVersionUID = <NUM_LIT> ; } @ SuppressWarnings ( "<STR_LIT:serial>" ) class StatusBar extends JPanel { private JLabel mem_label ; private JLabel progress ; public StatusBar ( ) { setLayout ( new BorderLayout ( ) ) ; setPreferredSize ( new Dimension ( <NUM_LIT:10> , <NUM_LIT> ) ) ; JPanel rightPanel = new JPanel ( new BorderLayout ( ) ) ; rightPanel . add ( new JLabel ( new AngledLinesWindowsCornerIcon ( ) ) , BorderLayout . SOUTH ) ; rightPanel . setOpaque ( false ) ; add ( rightPanel , BorderLayout . EAST ) ; setBackground ( SystemColor . control ) ; progress = new JLabel ( "<STR_LIT>" ) ; progress . setPreferredSize ( new Dimension ( <NUM_LIT> , <NUM_LIT> ) ) ; Progress . setListener ( new ProgressListener ( ) { @ Override public void progress ( final String msg ) { SwingUtilities . invokeLater ( new Runnable ( ) { public void run ( ) { progress . setText ( msg ) ; progress . repaint ( ) ; } } ) ; } } ) ; add ( progress , BorderLayout . WEST ) ; mem_label = new JLabel ( ) ; add ( mem_label , BorderLayout . CENTER ) ; } void updateMemory ( ) { double total = Runtime . getRuntime ( ) . totalMemory ( ) / ( <NUM_LIT> * <NUM_LIT> ) ; double free = Runtime . getRuntime ( ) . freeMemory ( ) / ( <NUM_LIT> * <NUM_LIT> ) ; String msg = "<STR_LIT>" + ( ( int ) ( total - free ) ) + "<STR_LIT:/>" + ( ( int ) total ) + "<STR_LIT>" ; mem_label . setText ( msg ) ; } protected void paintComponent ( Graphics g ) { super . paintComponent ( g ) ; int y = <NUM_LIT:0> ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y ++ ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y ++ ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y ++ ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y = getHeight ( ) - <NUM_LIT:3> ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y ++ ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; y = getHeight ( ) - <NUM_LIT:1> ; g . setColor ( new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ; g . drawLine ( <NUM_LIT:0> , y , getWidth ( ) , y ) ; } } class AngledLinesWindowsCornerIcon implements Icon { private static final Color WHITE_LINE_COLOR = new Color ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ; private static final Color GRAY_LINE_COLOR = new Color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ; private static final int WIDTH = <NUM_LIT> ; private static final int HEIGHT = <NUM_LIT> ; public int getIconHeight ( ) { return WIDTH ; } public int getIconWidth ( ) { return HEIGHT ; } public void paintIcon ( Component c , Graphics g , int x , int y ) { g . setColor ( WHITE_LINE_COLOR ) ; g . drawLine ( <NUM_LIT:0> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:0> ) ; g . drawLine ( <NUM_LIT:5> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:5> ) ; g . drawLine ( <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:10> ) ; g . setColor ( GRAY_LINE_COLOR ) ; g . drawLine ( <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:1> ) ; g . drawLine ( <NUM_LIT:2> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:2> ) ; g . drawLine ( <NUM_LIT:3> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:3> ) ; g . drawLine ( <NUM_LIT:6> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:6> ) ; g . drawLine ( <NUM_LIT:7> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:7> ) ; g . drawLine ( <NUM_LIT:8> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:8> ) ; g . drawLine ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:11> ) ; g . drawLine ( <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:12> ) ; } } </s>
|
<s> package erjang . console ; import java . io . PrintStream ; import java . util . concurrent . locks . ReentrantLock ; import javax . swing . JTextPane ; import erjang . ERT ; import erjang . EString ; import erjang . driver . EDriver ; import erjang . driver . EDriverControl ; public class TTYTextAreaDriver implements EDriver { private kilim . ReentrantLock lock ; final JTextPane text ; final String message ; private TTYTextAreaDriverControl control ; public TTYTextAreaDriver ( JTextPane text2 , String message ) { this . text = text2 ; this . message = message ; this . control = new TTYTextAreaDriverControl ( this , text , message ) ; ERT . set_stdio ( control . getInputStream ( ) , new PrintStream ( control . getOutputStream ( ) ) , new PrintStream ( control . getErrorStream ( ) ) ) ; } @ Override public String driverName ( ) { return "<STR_LIT>" ; } @ Override public void finish ( ) { } @ Override public ReentrantLock getLock ( ) { if ( lock == null ) { lock = new kilim . ReentrantLock ( ) ; } return lock ; } @ Override public EDriverControl start ( EString command ) { return control ; } @ Override public boolean useDriverLevelLocking ( ) { return false ; } } </s>
|
<s> package erjang . epmd ; public interface Connection { int CLOSED = <NUM_LIT:0> ; int OPEN = <NUM_LIT:1> ; int OPENING = <NUM_LIT:2> ; int CLOSING = <NUM_LIT:4> ; void setName ( String nm ) ; } </s>
|
<s> package erjang . epmd ; import java . io . * ; import java . net . * ; import java . util . * ; import java . util . logging . Level ; import java . util . logging . Logger ; import java . nio . * ; import java . nio . channels . * ; public abstract class PacketServer { static final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private ServerSocket ss ; private ServerSocketChannel sschan ; private Selector selector ; private int bufsz = <NUM_LIT> ; protected void listen ( int port ) { int n = <NUM_LIT:0> ; Iterator < SelectionKey > it ; SelectionKey key ; log . info ( "<STR_LIT>" + port ) ; try { sschan = ServerSocketChannel . open ( ) ; sschan . configureBlocking ( false ) ; ss = sschan . socket ( ) ; ss . bind ( new InetSocketAddress ( InetAddress . getByAddress ( new byte [ ] { <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> } ) , port ) ) ; selector = Selector . open ( ) ; sschan . register ( selector , SelectionKey . OP_ACCEPT ) ; } catch ( IOException ie ) { ie . printStackTrace ( ) ; System . exit ( <NUM_LIT:0> ) ; } while ( true ) { try { n = selector . select ( ) ; } catch ( Exception e ) { log . severe ( "<STR_LIT>" + e . getMessage ( ) ) ; log . log ( Level . FINE , "<STR_LIT>" , e ) ; } log . fine ( "<STR_LIT>" + n ) ; Set < SelectionKey > selectedKeys = selector . selectedKeys ( ) ; it = selectedKeys . iterator ( ) ; while ( it . hasNext ( ) ) { key = ( SelectionKey ) it . next ( ) ; int kro = key . readyOps ( ) ; log . fine ( "<STR_LIT>" + kro ) ; if ( ( kro & SelectionKey . OP_READ ) == SelectionKey . OP_READ ) doRead ( key ) ; if ( ( kro & SelectionKey . OP_WRITE ) == SelectionKey . OP_WRITE ) doWrite ( key ) ; if ( ( kro & SelectionKey . OP_ACCEPT ) == SelectionKey . OP_ACCEPT ) doAccept ( key ) ; if ( ( kro & SelectionKey . OP_CONNECT ) == SelectionKey . OP_CONNECT ) doConnect ( key ) ; if ( key . isValid ( ) && key . interestOps ( ) == <NUM_LIT:0> ) { it . remove ( ) ; } } } } private void doAccept ( SelectionKey sk ) { ServerSocketChannel sc = ( ServerSocketChannel ) sk . channel ( ) ; log . fine ( "<STR_LIT>" ) ; SocketChannel usc = null ; ByteBuffer data ; try { usc = sc . accept ( ) ; if ( usc == null ) return ; usc . configureBlocking ( false ) ; Socket sock = usc . socket ( ) ; String nm = sock . getInetAddress ( ) + "<STR_LIT::>" + sock . getPort ( ) ; log . info ( "<STR_LIT>" + nm ) ; sock . setKeepAlive ( true ) ; data = ByteBuffer . allocate ( bufsz ) ; data . position ( data . limit ( ) ) ; SelectionKey dsk = usc . register ( selector , SelectionKey . OP_READ , null ) ; Connection conn = newConnection ( dsk ) ; conn . setName ( nm ) ; dsk . attach ( conn ) ; } catch ( IOException re ) { log . severe ( "<STR_LIT>" + re . getMessage ( ) ) ; log . log ( Level . FINE , "<STR_LIT>" , re ) ; } } protected abstract PacketConnection newConnection ( SelectionKey dsk ) ; private void doRead ( SelectionKey sk ) { PacketConnection conn = ( PacketConnection ) sk . attachment ( ) ; conn . doRead ( ) ; } private void doWrite ( SelectionKey sk ) { PacketConnection conn = ( PacketConnection ) sk . attachment ( ) ; conn . doWrite ( ) ; } private void doConnect ( SelectionKey sk ) { PacketConnection conn = ( PacketConnection ) sk . attachment ( ) ; conn . doConnect ( ) ; } } </s>
|
<s> package erjang . epmd ; import java . nio . channels . SelectionKey ; import java . util . logging . Level ; import java . util . logging . Logger ; public class EPMDServer extends PacketServer { private static int epmd_port ; @ Override protected PacketConnection newConnection ( SelectionKey dsk ) { return new EPMDConnection ( epmd_port , dsk ) ; } static public void main ( String [ ] args ) { epmd_port = <NUM_LIT> ; Level level = Level . WARNING ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { if ( args [ i ] . startsWith ( "<STR_LIT>" ) ) epmd_port = toInt ( args [ i ] . substring ( <NUM_LIT:2> ) , epmd_port ) ; if ( args [ i ] . startsWith ( "<STR_LIT>" ) ) level = toLevel ( toInt ( args [ i ] . substring ( <NUM_LIT:2> ) , <NUM_LIT:2> ) ) ; } final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; log . setLevel ( level ) ; new EPMDServer ( ) . listen ( epmd_port ) ; } public static int toInt ( String intString , int defaultValue ) { try { return Integer . parseInt ( intString ) ; } catch ( NumberFormatException e ) { return defaultValue ; } } public static Level toLevel ( int level ) { if ( level <= <NUM_LIT:0> ) { return Level . OFF ; } switch ( level ) { case <NUM_LIT:1> : return Level . SEVERE ; case <NUM_LIT:2> : return Level . WARNING ; case <NUM_LIT:3> : return Level . INFO ; case <NUM_LIT:4> : return Level . FINE ; case <NUM_LIT:5> : return Level . FINER ; case <NUM_LIT:6> : return Level . FINEST ; default : return Level . FINEST ; } } } </s>
|
<s> package erjang . epmd ; import java . nio . ByteBuffer ; import java . nio . channels . SelectionKey ; import java . util . HashMap ; import java . util . Map ; import java . util . logging . Logger ; import erjang . driver . IO ; public class EPMDConnection extends PacketConnection { static final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; private NodeInfo connectedNode ; private int epmdPort ; static final byte EPMD_ALIVE_REQ = '<CHAR_LIT:a>' ; static final byte EPMD_ALIVE_OK_RESP = '<CHAR_LIT>' ; static final byte EPMD_PORT_REQ = '<CHAR_LIT>' ; static final byte EPMD_NAMES_REQ = '<CHAR_LIT>' ; static final byte EPMD_DUMP_REQ = '<CHAR_LIT>' ; static final byte EPMD_KILL_REQ = '<CHAR_LIT>' ; static final byte EPMD_STOP_REQ = '<CHAR_LIT>' ; static final int ALIVE2_REQ = '<CHAR_LIT>' ; static final int ALIVE2_RESP = '<CHAR_LIT>' ; static final int PORT2_REQ = '<CHAR_LIT>' ; static final byte PORT2_RESP = '<CHAR_LIT>' ; public EPMDConnection ( int epmdPort , SelectionKey dsk ) { super ( dsk ) ; this . epmdPort = epmdPort ; } @ Override protected void receivePacket ( ByteBuffer buf ) { int op = buf . get ( ) ; log . fine ( "<STR_LIT>" + op + "<STR_LIT>" + getName ( ) ) ; switch ( op ) { case ALIVE2_REQ : { int eport = buf . getShort ( ) & <NUM_LIT> ; int nodetype = buf . get ( ) ; int proto = buf . get ( ) ; int highvsn = buf . getShort ( ) & <NUM_LIT> ; int lowvsn = buf . getShort ( ) & <NUM_LIT> ; int nlen = buf . getShort ( ) & <NUM_LIT> ; byte [ ] name = new byte [ nlen ] ; buf . get ( name ) ; int elen = buf . getShort ( ) & <NUM_LIT> ; byte [ ] extra = new byte [ elen ] ; buf . get ( extra ) ; NodeInfo node ; if ( ( node = node_reg2 ( name , eport , nodetype , proto , highvsn , lowvsn , extra ) ) == null ) { ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:4> ) ; resp . put ( ( byte ) ALIVE2_RESP ) ; resp . put ( ( byte ) <NUM_LIT:1> ) ; resp . putShort ( ( short ) <NUM_LIT> ) ; this . sendPacket ( resp , false ) ; } else { ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:4> ) ; resp . put ( ( byte ) ALIVE2_RESP ) ; resp . put ( ( byte ) <NUM_LIT:0> ) ; resp . putShort ( node . creation ) ; this . keep = true ; this . sendPacket ( resp , false ) ; } return ; } case PORT2_REQ : { int len = buf . remaining ( ) ; byte [ ] data = new byte [ len ] ; buf . get ( data ) ; String n = new String ( data , IO . ISO_LATIN_1 ) ; NodeInfo node = nodes . get ( n ) ; if ( n == null ) { ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:2> ) ; resp . put ( ( byte ) PORT2_RESP ) ; resp . put ( ( byte ) <NUM_LIT:1> ) ; sendPacket ( resp , false ) ; return ; } else { ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:20> + node . extra . length + node . nodeName . length ) ; resp . put ( PORT2_RESP ) ; resp . put ( ( byte ) <NUM_LIT:0> ) ; resp . putShort ( ( short ) node . portNo ) ; resp . put ( ( byte ) node . nodeType ) ; resp . put ( ( byte ) node . protocol ) ; resp . putShort ( ( short ) node . highVersion ) ; resp . putShort ( ( short ) node . lowVersion ) ; resp . putShort ( ( short ) node . nodeName . length ) ; resp . put ( node . nodeName ) ; resp . putShort ( ( short ) node . extra . length ) ; resp . put ( node . extra ) ; sendPacket ( resp , false ) ; } return ; } case EPMD_NAMES_REQ : { StringBuffer sb = new StringBuffer ( ) ; for ( NodeInfo n : nodes . values ( ) ) { sb . append ( "<STR_LIT>" ) . append ( n . name ) . append ( "<STR_LIT>" ) . append ( n . portNo ) . append ( '<STR_LIT:\n>' ) ; } ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:4> + sb . length ( ) ) ; resp . putInt ( epmdPort ) ; resp . put ( sb . toString ( ) . getBytes ( IO . ISO_LATIN_1 ) ) ; sendPacket ( resp , false ) ; return ; } case EPMD_DUMP_REQ : { StringBuffer sb = new StringBuffer ( ) ; for ( NodeInfo n : nodes . values ( ) ) { sb . append ( "<STR_LIT>" ) . append ( n . name ) . append ( "<STR_LIT>" ) . append ( n . portNo ) . append ( "<STR_LIT>" ) . append ( n . fd ) . append ( '<STR_LIT:\n>' ) ; } for ( NodeInfo n : nodes . values ( ) ) { sb . append ( "<STR_LIT>" ) . append ( n . name ) . append ( "<STR_LIT>" ) . append ( n . portNo ) . append ( "<STR_LIT>" ) . append ( n . fd ) . append ( '<STR_LIT:\n>' ) ; } ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:4> + sb . length ( ) ) ; resp . putInt ( epmdPort ) ; resp . put ( sb . toString ( ) . getBytes ( IO . ISO_LATIN_1 ) ) ; sendPacket ( resp , false ) ; return ; } case EPMD_KILL_REQ : { ByteBuffer resp = ByteBuffer . allocate ( <NUM_LIT:7> ) ; resp . put ( "<STR_LIT>" . getBytes ( IO . ISO_LATIN_1 ) ) ; sendPacket ( resp , false ) ; return ; } default : log . warning ( "<STR_LIT>" + op ) ; System . exit ( <NUM_LIT:1> ) ; } } static Map < String , NodeInfo > unreg = new HashMap < String , NodeInfo > ( ) ; static Map < String , NodeInfo > nodes = new HashMap < String , NodeInfo > ( ) ; private NodeInfo node_reg2 ( byte [ ] name , int eport , int nodetype , int proto , int highvsn , int lowvsn , byte [ ] extra ) { String n = new String ( name , IO . ISO_LATIN_1 ) ; if ( nodes . containsKey ( n ) ) { return null ; } NodeInfo node = unreg . remove ( n ) ; if ( node == null ) { node = new NodeInfo ( ) ; node . creation = ( short ) ( ( System . currentTimeMillis ( ) % <NUM_LIT:3> ) + <NUM_LIT:1> ) ; } else { node . creation = ( short ) ( ( node . creation % <NUM_LIT:3> ) + <NUM_LIT:1> ) ; } node . name = n ; node . portNo = eport ; node . nodeType = nodetype ; node . protocol = proto ; node . highVersion = highvsn ; node . lowVersion = lowvsn ; node . extra = extra ; node . fd = <NUM_LIT:0> ; nodes . put ( n , node ) ; this . connectedNode = node ; return node ; } @ Override protected void connectionClosed ( ) { NodeInfo node = this . connectedNode ; this . connectedNode = null ; if ( node != null ) { nodes . remove ( node . name ) ; unreg . put ( node . name , node ) ; } } } </s>
|
<s> package erjang . epmd ; class NodeInfo { public int portNo ; public int nodeType ; public int protocol ; public int highVersion ; public int lowVersion ; public byte [ ] nodeName ; public byte [ ] extra ; public short creation ; public String name ; public int fd ; } </s>
|
<s> package erjang . epmd ; import java . io . IOException ; import java . net . Socket ; import java . nio . ByteBuffer ; import java . nio . channels . GatheringByteChannel ; import java . nio . channels . SelectionKey ; import java . nio . channels . SocketChannel ; import java . util . ArrayList ; import java . util . logging . Level ; import java . util . logging . Logger ; public abstract class PacketConnection implements Connection { static final Logger log = Logger . getLogger ( "<STR_LIT>" ) ; protected SelectionKey sk ; private int state ; private ByteBuffer recvBuffer = null ; private String name = "<STR_LIT>" ; ArrayList < ByteBuffer > outbuf = new ArrayList < ByteBuffer > ( ) ; protected void receivePacket ( ByteBuffer buf ) { } protected void connectionClosed ( ) { } void sendPacket ( ByteBuffer buf , boolean includeSize ) { if ( includeSize ) { ByteBuffer size = ByteBuffer . allocate ( <NUM_LIT:2> ) ; size . putShort ( <NUM_LIT:0> , ( short ) buf . position ( ) ) ; outbuf . add ( size ) ; } buf . flip ( ) ; outbuf . add ( buf ) ; sk . interestOps ( SelectionKey . OP_WRITE | sk . interestOps ( ) ) ; } PacketConnection ( SelectionKey sk ) { SocketChannel sch = ( SocketChannel ) sk . channel ( ) ; if ( sch . isConnected ( ) ) { sk . interestOps ( SelectionKey . OP_READ ) ; state = Connection . OPEN ; } else if ( sch . isConnectionPending ( ) ) { sk . interestOps ( SelectionKey . OP_CONNECT ) ; state = Connection . OPENING ; } this . sk = sk ; sk . attach ( this ) ; recvBuffer = ByteBuffer . allocate ( <NUM_LIT> ) ; } public void doConnect ( ) { SocketChannel sc = ( SocketChannel ) sk . channel ( ) ; try { sc . finishConnect ( ) ; sk . interestOps ( sk . interestOps ( ) & ~ SelectionKey . OP_CONNECT ) ; log . fine ( "<STR_LIT>" ) ; state = Connection . OPEN ; } catch ( IOException e ) { e . printStackTrace ( ) ; closeComplete ( ) ; } } ByteBuffer rcvBuf ; protected boolean keep ; public void doRead ( ) { SocketChannel sc = ( SocketChannel ) sk . channel ( ) ; if ( sc . isOpen ( ) ) { int len ; recvBuffer . clear ( ) ; try { len = sc . read ( recvBuffer ) ; } catch ( IOException e ) { len = - <NUM_LIT:1> ; } log . finer ( "<STR_LIT>" + len ) ; if ( len == - <NUM_LIT:1> ) { close ( ) ; return ; } int start = <NUM_LIT:0> ; while ( recvBuffer . position ( ) > start + <NUM_LIT:2> ) { int pklen = recvBuffer . getShort ( start ) & <NUM_LIT> ; if ( recvBuffer . position ( ) >= start + <NUM_LIT:2> + pklen ) { ByteBuffer buf = slice ( recvBuffer , start + <NUM_LIT:2> , pklen ) ; receivePacket ( buf ) ; if ( ! this . keep && outbuf . isEmpty ( ) ) { close ( ) ; return ; } start += <NUM_LIT:2> + pklen ; } else if ( recvBuffer . position ( ) == recvBuffer . limit ( ) ) { int available = recvBuffer . position ( ) - start ; byte [ ] data = new byte [ pklen + <NUM_LIT:2> ] ; recvBuffer . position ( start ) ; recvBuffer . get ( data , <NUM_LIT:0> , available ) ; recvBuffer = ByteBuffer . wrap ( data ) ; return ; } } if ( start > <NUM_LIT:0> && recvBuffer . position ( ) > <NUM_LIT:0> ) { if ( recvBuffer . position ( ) == start ) { recvBuffer . clear ( ) ; } else { int left = recvBuffer . position ( ) - start ; byte [ ] data = new byte [ left ] ; recvBuffer . position ( start ) ; recvBuffer . get ( data ) ; recvBuffer . clear ( ) ; recvBuffer . put ( data ) ; } } } else { close ( ) ; } } private ByteBuffer slice ( ByteBuffer buf , int start , int len ) { int savePos = buf . position ( ) ; int saveLim = buf . limit ( ) ; buf . position ( start ) ; buf . limit ( start + len ) ; ByteBuffer res = buf . slice ( ) ; buf . position ( savePos ) ; buf . limit ( saveLim ) ; return res ; } public void doWrite ( ) { log . finer ( "<STR_LIT>" ) ; if ( ! outbuf . isEmpty ( ) ) { GatheringByteChannel bc = ( GatheringByteChannel ) sk . channel ( ) ; ByteBuffer [ ] out = outbuf . toArray ( new ByteBuffer [ outbuf . size ( ) ] ) ; try { bc . write ( out ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } for ( int i = <NUM_LIT:0> ; i < out . length && ! out [ i ] . hasRemaining ( ) ; i ++ ) { outbuf . remove ( <NUM_LIT:0> ) ; } } if ( outbuf . isEmpty ( ) ) { sk . interestOps ( sk . interestOps ( ) & ~ SelectionKey . OP_WRITE ) ; } if ( ! this . keep && outbuf . isEmpty ( ) ) { close ( ) ; } } public void close ( ) { if ( state != Connection . CLOSED ) { SocketChannel sc = ( SocketChannel ) sk . channel ( ) ; if ( sc . isOpen ( ) ) { if ( state == Connection . OPEN ) { log . fine ( "<STR_LIT>" ) ; state = Connection . CLOSING ; Socket sock = sc . socket ( ) ; try { sock . shutdownOutput ( ) ; } catch ( IOException se ) { log . severe ( "<STR_LIT>" + se . getMessage ( ) ) ; log . log ( Level . FINE , "<STR_LIT>" , se ) ; } } else closeComplete ( ) ; } else log . warning ( "<STR_LIT>" ) ; } } private void closeComplete ( ) { log . fine ( "<STR_LIT>" ) ; try { sk . interestOps ( <NUM_LIT:0> ) ; SocketChannel sc = ( SocketChannel ) sk . channel ( ) ; if ( sc != null && sc . isOpen ( ) ) sc . close ( ) ; sk . selector ( ) . wakeup ( ) ; sk . attach ( null ) ; } catch ( IOException ce ) { log . severe ( "<STR_LIT>" + ce . getMessage ( ) ) ; log . log ( Level . FINE , "<STR_LIT>" , ce ) ; } state = Connection . CLOSED ; connectionClosed ( ) ; } public String getName ( ) { return name ; } public void setName ( String nm ) { name = nm ; } public int getState ( ) { return state ; } } </s>
|
<s> package erjang ; import java . io . ByteArrayInputStream ; import java . io . IOException ; import java . io . EOFException ; import java . math . BigDecimal ; import java . math . BigInteger ; import java . security . MessageDigest ; import erjang . driver . IO ; public class EInputStream extends ByteArrayInputStream { public static int DECODE_INT_LISTS_AS_STRINGS = <NUM_LIT:1> ; private final int flags ; private EAtom [ ] atom_cache_refs ; public EInputStream ( final byte [ ] buf ) { this ( buf , <NUM_LIT:0> ) ; } public EInputStream ( final byte [ ] buf , final int flags ) { super ( buf ) ; this . flags = flags ; } public EInputStream ( final byte [ ] buf , final int offset , final int length , final int flags ) { super ( buf , offset , length ) ; this . flags = flags ; } public void updateMessageDigest ( MessageDigest digest , int offset , int len ) { digest . update ( this . buf , offset , len ) ; } public int getPos ( ) { return super . pos ; } public int setPos ( int pos ) { final int oldpos = super . pos ; if ( pos > super . count ) { pos = super . count ; } else if ( pos < <NUM_LIT:0> ) { pos = <NUM_LIT:0> ; } super . pos = pos ; return oldpos ; } public int readN ( final byte [ ] buf ) throws IOException { return this . readN ( buf , <NUM_LIT:0> , buf . length ) ; } public int readN ( final byte [ ] buf , final int off , final int len ) throws IOException { if ( len == <NUM_LIT:0> && available ( ) == <NUM_LIT:0> ) { return <NUM_LIT:0> ; } final int i = super . read ( buf , off , len ) ; if ( i < <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" ) ; } return i ; } public int peek ( ) throws IOException { return peek1 ( ) ; } public int peek1 ( ) throws IOException { int i ; try { i = super . buf [ super . pos ] ; if ( i < <NUM_LIT:0> ) { i += <NUM_LIT> ; } return i ; } catch ( final Exception e ) { throw new IOException ( "<STR_LIT>" ) ; } } public int peek1skip_version ( ) throws IOException { int i = peek1 ( ) ; if ( i == EExternal . versionTag ) { read1 ( ) ; i = peek1 ( ) ; } return i ; } public int read1 ( ) throws IOException { int i ; i = super . read ( ) ; if ( i < <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" ) ; } return i ; } public int read1skip_version ( ) throws IOException { int tag = read1 ( ) ; if ( tag == EExternal . versionTag ) { tag = read1 ( ) ; } return tag ; } public void readFully ( byte [ ] b ) throws IOException { int pos = <NUM_LIT:0> ; while ( pos < b . length ) { int read ; try { read = super . read ( b , pos , b . length - pos ) ; } catch ( IndexOutOfBoundsException ioobe ) { read = <NUM_LIT:0> ; } if ( read == <NUM_LIT:0> ) throw new EOFException ( "<STR_LIT>" ) ; pos += read ; } } public int read2BE ( ) throws IOException { final byte [ ] b = new byte [ <NUM_LIT:2> ] ; readFully ( b ) ; return ( b [ <NUM_LIT:0> ] << <NUM_LIT:8> & <NUM_LIT> ) + ( b [ <NUM_LIT:1> ] & <NUM_LIT> ) ; } public int read4BE ( ) throws IOException { final byte [ ] b = new byte [ <NUM_LIT:4> ] ; readFully ( b ) ; return ( b [ <NUM_LIT:0> ] << <NUM_LIT:24> & <NUM_LIT> ) + ( b [ <NUM_LIT:1> ] << <NUM_LIT:16> & <NUM_LIT> ) + ( b [ <NUM_LIT:2> ] << <NUM_LIT:8> & <NUM_LIT> ) + ( b [ <NUM_LIT:3> ] & <NUM_LIT> ) ; } public int read2LE ( ) throws IOException { final byte [ ] b = new byte [ <NUM_LIT:2> ] ; readFully ( b ) ; return ( b [ <NUM_LIT:1> ] << <NUM_LIT:8> & <NUM_LIT> ) + ( b [ <NUM_LIT:0> ] & <NUM_LIT> ) ; } public int read4LE ( ) throws IOException { final byte [ ] b = new byte [ <NUM_LIT:4> ] ; readFully ( b ) ; return ( b [ <NUM_LIT:3> ] << <NUM_LIT:24> & <NUM_LIT> ) + ( b [ <NUM_LIT:2> ] << <NUM_LIT:16> & <NUM_LIT> ) + ( b [ <NUM_LIT:1> ] << <NUM_LIT:8> & <NUM_LIT> ) + ( b [ <NUM_LIT:0> ] & <NUM_LIT> ) ; } public long readLE ( int n ) throws IOException { final byte [ ] b = new byte [ n ] ; try { super . read ( b ) ; } catch ( final IOException e ) { throw new IOException ( "<STR_LIT>" ) ; } ; long v = <NUM_LIT:0> ; while ( n -- > <NUM_LIT:0> ) { v = v << <NUM_LIT:8> | ( long ) b [ n ] & <NUM_LIT> ; } return v ; } public long readBE ( final int n ) throws IOException { final byte [ ] b = new byte [ n ] ; try { super . read ( b ) ; } catch ( final IOException e ) { throw new IOException ( "<STR_LIT>" ) ; } ; long v = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < n ; i ++ ) { v = v << <NUM_LIT:8> | ( long ) b [ i ] & <NUM_LIT> ; } return v ; } public boolean read_boolean ( ) throws IOException { return read_atom ( ) == ERT . TRUE ; } public EAtom read_atom ( ) throws IOException { int tag ; int len ; byte [ ] strbuf ; String atom ; tag = read1skip_version ( ) ; if ( tag == EExternal . atomCacheRef ) { int index = read1 ( ) & <NUM_LIT> ; EAtom res = atom_cache_refs [ index ] ; if ( res == null ) { throw new IOException ( "<STR_LIT>" + index ) ; } return res ; } if ( tag == EExternal . smallAtomTag ) { len = read1 ( ) ; } else { if ( tag != EExternal . atomTag ) { throw new IOException ( "<STR_LIT>" + EExternal . atomTag + "<STR_LIT>" + tag ) ; } len = read2BE ( ) ; } strbuf = new byte [ len ] ; this . readN ( strbuf ) ; char [ ] in = new char [ len ] ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { in [ i ] = ( char ) ( strbuf [ i ] & <NUM_LIT> ) ; } atom = new String ( in ) ; if ( atom . length ( ) > EExternal . maxAtomLength ) { atom = atom . substring ( <NUM_LIT:0> , EExternal . maxAtomLength ) ; } return EAtom . intern ( atom ) ; } public byte [ ] read_binary ( ) throws IOException { int tag ; int len ; byte [ ] bin ; tag = read1skip_version ( ) ; if ( tag != EExternal . binTag ) { throw new IOException ( "<STR_LIT>" + EExternal . binTag + "<STR_LIT>" + tag ) ; } len = read4BE ( ) ; bin = new byte [ len ] ; this . readN ( bin ) ; return bin ; } public byte [ ] read_bitstr ( final int pad_bits [ ] ) throws IOException { int tag ; int len ; byte [ ] bin ; tag = read1skip_version ( ) ; if ( tag != EExternal . bitBinTag ) { throw new IOException ( "<STR_LIT>" + EExternal . bitBinTag + "<STR_LIT>" + tag ) ; } len = read4BE ( ) ; bin = new byte [ len ] ; final int tail_bits = read1 ( ) ; if ( tail_bits < <NUM_LIT:0> || <NUM_LIT:7> < tail_bits ) { throw new IOException ( "<STR_LIT>" + tail_bits ) ; } if ( len == <NUM_LIT:0> && tail_bits != <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" + tail_bits ) ; } this . readN ( bin ) ; pad_bits [ <NUM_LIT:0> ] = <NUM_LIT:8> - tail_bits ; return bin ; } public float read_float ( ) throws IOException { final double d = read_double ( ) ; return ( float ) d ; } public double read_double ( ) throws IOException { int tag ; tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . newFloatTag : { return Double . longBitsToDouble ( readBE ( <NUM_LIT:8> ) ) ; } case EExternal . floatTag : { BigDecimal val ; int epos ; int exp ; final byte [ ] strbuf = new byte [ <NUM_LIT:31> ] ; String str ; this . readN ( strbuf ) ; str = new String ( strbuf , IO . ISO_LATIN_1 ) ; epos = str . indexOf ( '<CHAR_LIT:e>' , <NUM_LIT:0> ) ; if ( epos < <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" + str + "<STR_LIT:'>" ) ; } String estr = str . substring ( epos + <NUM_LIT:1> ) . trim ( ) ; if ( estr . substring ( <NUM_LIT:0> , <NUM_LIT:1> ) . equals ( "<STR_LIT:+>" ) ) { estr = estr . substring ( <NUM_LIT:1> ) ; } exp = Integer . valueOf ( estr ) . intValue ( ) ; val = new BigDecimal ( str . substring ( <NUM_LIT:0> , epos ) ) . movePointRight ( exp ) ; return val . doubleValue ( ) ; } default : throw new IOException ( "<STR_LIT>" + EExternal . newFloatTag + "<STR_LIT>" + tag ) ; } } public byte read_byte ( ) throws IOException { final long l = this . read_long ( false ) ; final byte i = ( byte ) l ; if ( l != i ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public char read_char ( ) throws IOException { final long l = this . read_long ( true ) ; final char i = ( char ) l ; if ( l != ( i & <NUM_LIT> ) ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public int read_uint ( ) throws IOException { final long l = this . read_long ( true ) ; final int i = ( int ) l ; if ( l != ( i & <NUM_LIT> ) ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public int read_int ( ) throws IOException { final long l = this . read_long ( false ) ; final int i = ( int ) l ; if ( l != i ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public short read_ushort ( ) throws IOException { final long l = this . read_long ( true ) ; final short i = ( short ) l ; if ( l != ( i & <NUM_LIT> ) ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public short read_short ( ) throws IOException { final long l = this . read_long ( false ) ; final short i = ( short ) l ; if ( l != i ) { throw new IOException ( "<STR_LIT>" + l ) ; } return i ; } public long read_ulong ( ) throws IOException { return this . read_long ( true ) ; } public long read_long ( ) throws IOException { return this . read_long ( false ) ; } public long read_long ( final boolean unsigned ) throws IOException { final byte [ ] b = read_integer_byte_array ( ) ; return EInputStream . byte_array_to_long ( b , unsigned ) ; } public EInteger read_tagged_integer ( ) throws IOException { int tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . smallIntTag : return new ESmall ( read1 ( ) ) ; case EExternal . intTag : return new ESmall ( read4BE ( ) ) ; default : setPos ( getPos ( ) - <NUM_LIT:1> ) ; return ERT . box ( new BigInteger ( read_integer_byte_array ( ) ) ) ; } } public byte [ ] read_integer_byte_array ( ) throws IOException { int tag ; byte [ ] nb ; tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . smallIntTag : nb = new byte [ <NUM_LIT:2> ] ; nb [ <NUM_LIT:0> ] = <NUM_LIT:0> ; nb [ <NUM_LIT:1> ] = ( byte ) read1 ( ) ; break ; case EExternal . intTag : nb = new byte [ <NUM_LIT:4> ] ; if ( this . readN ( nb ) != <NUM_LIT:4> ) { throw new IOException ( "<STR_LIT>" ) ; } break ; case EExternal . smallBigTag : case EExternal . largeBigTag : int arity ; int sign ; if ( tag == EExternal . smallBigTag ) { arity = read1 ( ) ; sign = read1 ( ) ; } else { arity = read4BE ( ) ; sign = read1 ( ) ; if ( arity + <NUM_LIT:1> < <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" + arity + "<STR_LIT>" + sign ) ; } } nb = new byte [ arity + <NUM_LIT:1> ] ; if ( this . readN ( nb , <NUM_LIT:0> , arity ) != arity ) { throw new IOException ( "<STR_LIT>" ) ; } for ( int i = <NUM_LIT:0> , j = nb . length ; i < j -- ; i ++ ) { final byte b = nb [ i ] ; nb [ i ] = nb [ j ] ; nb [ j ] = b ; } if ( sign != <NUM_LIT:0> ) { int c = <NUM_LIT:1> ; for ( int j = nb . length ; j -- > <NUM_LIT:0> ; ) { c = ( ~ nb [ j ] & <NUM_LIT> ) + c ; nb [ j ] = ( byte ) c ; c >>= <NUM_LIT:8> ; } } break ; default : throw new IOException ( "<STR_LIT>" + tag ) ; } return nb ; } public static long byte_array_to_long ( final byte [ ] b , final boolean unsigned ) throws IOException { long v ; switch ( b . length ) { case <NUM_LIT:0> : v = <NUM_LIT:0> ; break ; case <NUM_LIT:2> : v = ( ( b [ <NUM_LIT:0> ] & <NUM_LIT> ) << <NUM_LIT:8> ) + ( b [ <NUM_LIT:1> ] & <NUM_LIT> ) ; v = ( short ) v ; if ( v < <NUM_LIT:0> && unsigned ) { throw new IOException ( "<STR_LIT>" + v ) ; } break ; case <NUM_LIT:4> : v = ( ( b [ <NUM_LIT:0> ] & <NUM_LIT> ) << <NUM_LIT:24> ) + ( ( b [ <NUM_LIT:1> ] & <NUM_LIT> ) << <NUM_LIT:16> ) + ( ( b [ <NUM_LIT:2> ] & <NUM_LIT> ) << <NUM_LIT:8> ) + ( b [ <NUM_LIT:3> ] & <NUM_LIT> ) ; v = ( int ) v ; if ( v < <NUM_LIT:0> && unsigned ) { throw new IOException ( "<STR_LIT>" + v ) ; } break ; default : int i = <NUM_LIT:0> ; final byte c = b [ i ] ; if ( unsigned ) { if ( c < <NUM_LIT:0> ) { throw new IOException ( "<STR_LIT>" + b ) ; } while ( b [ i ] == <NUM_LIT:0> ) { i ++ ; } } else { if ( c == <NUM_LIT:0> || c == - <NUM_LIT:1> ) { i = <NUM_LIT:1> ; while ( i < b . length && b [ i ] == c ) { i ++ ; } if ( i < b . length ) { if ( ( ( c ^ b [ i ] ) & <NUM_LIT> ) != <NUM_LIT:0> ) { i -- ; } } } } if ( b . length - i > <NUM_LIT:8> ) { throw new IOException ( "<STR_LIT>" + b ) ; } for ( v = c < <NUM_LIT:0> ? - <NUM_LIT:1> : <NUM_LIT:0> ; i < b . length ; i ++ ) { v = v << <NUM_LIT:8> | b [ i ] & <NUM_LIT> ; } } return v ; } public int read_list_head ( ) throws IOException { int arity = <NUM_LIT:0> ; final int tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . nilTag : arity = <NUM_LIT:0> ; break ; case EExternal . stringTag : arity = read2BE ( ) ; break ; case EExternal . listTag : arity = read4BE ( ) ; break ; default : throw new IOException ( "<STR_LIT>" + tag ) ; } return arity ; } public int read_tuple_head ( ) throws IOException { int arity = <NUM_LIT:0> ; final int tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . smallTupleTag : arity = read1 ( ) ; break ; case EExternal . largeTupleTag : arity = read4BE ( ) ; break ; default : throw new IOException ( "<STR_LIT>" + tag ) ; } return arity ; } public int read_nil ( ) throws IOException { int arity = <NUM_LIT:0> ; final int tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . nilTag : arity = <NUM_LIT:0> ; break ; default : throw new IOException ( "<STR_LIT>" + tag ) ; } return arity ; } public EPID read_pid ( ) throws IOException { EAtom node ; int id ; int serial ; int creation ; int tag ; tag = read1skip_version ( ) ; if ( tag != EExternal . pidTag ) { throw new IOException ( "<STR_LIT>" + EExternal . pidTag + "<STR_LIT>" + tag ) ; } node = read_atom ( ) ; id = read4BE ( ) & <NUM_LIT> ; serial = read4BE ( ) & <NUM_LIT> ; creation = read1 ( ) & <NUM_LIT> ; return EPID . make ( node , id , serial , creation ) ; } public EPort read_port ( ) throws IOException { EAtom node ; int id ; int creation ; int tag ; tag = read1skip_version ( ) ; if ( tag != EExternal . portTag ) { throw new IOException ( "<STR_LIT>" + EExternal . portTag + "<STR_LIT>" + tag ) ; } node = read_atom ( ) ; id = read4BE ( ) & <NUM_LIT> ; creation = read1 ( ) & <NUM_LIT> ; return EPort . make ( node , id , creation ) ; } public ERef read_ref ( ) throws IOException { EAtom node ; int id ; int creation ; int tag ; tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . refTag : node = read_atom ( ) ; id = read4BE ( ) & <NUM_LIT> ; creation = read1 ( ) & <NUM_LIT> ; return new ERef ( node , id , creation ) ; case EExternal . newRefTag : final int arity = read2BE ( ) ; node = read_atom ( ) ; creation = read1 ( ) & <NUM_LIT> ; final int [ ] ids = new int [ arity ] ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { ids [ i ] = read4BE ( ) ; } ids [ <NUM_LIT:0> ] &= <NUM_LIT> ; return new ERef ( node , ids , creation ) ; default : throw new IOException ( "<STR_LIT>" + tag ) ; } } public EFun read_fun ( ) throws IOException { final int tag = read1skip_version ( ) ; if ( tag == EExternal . funTag ) { final int nFreeVars = read4BE ( ) ; final EPID pid = read_pid ( ) ; final EAtom module = read_atom ( ) ; final long index = read_long ( ) ; final long uniq = read_long ( ) ; final EObject [ ] freeVars = new EObject [ nFreeVars ] ; for ( int i = <NUM_LIT:0> ; i < nFreeVars ; ++ i ) { freeVars [ i ] = read_any ( ) ; } return EModuleManager . resolve ( pid , module , ( int ) uniq , ( int ) index , freeVars ) ; } else if ( tag == EExternal . newFunTag ) { @ SuppressWarnings ( "<STR_LIT:unused>" ) final int n = read4BE ( ) ; final int arity = read1 ( ) ; final byte [ ] md5 = new byte [ <NUM_LIT:16> ] ; readN ( md5 ) ; final int index = read4BE ( ) ; final int nFreeVars = read4BE ( ) ; final EAtom module = read_atom ( ) ; final long oldIndex = read_long ( ) ; final long uniq = read_long ( ) ; final EPID pid = read_pid ( ) ; final EObject [ ] freeVars = new EObject [ nFreeVars ] ; for ( int i = <NUM_LIT:0> ; i < nFreeVars ; ++ i ) { freeVars [ i ] = read_any ( ) ; } return EModuleManager . resolve ( pid , module , new EBinary ( md5 ) , index , ( int ) uniq , ( int ) oldIndex , arity , freeVars ) ; } else if ( tag == EExternal . externalFunTag ) { final EAtom module = read_atom ( ) ; final EAtom function = read_atom ( ) ; final int arity = ( int ) read_long ( ) ; return EModuleManager . resolve ( new FunID ( module , function , arity ) ) ; } else { throw new IOException ( "<STR_LIT>" + tag ) ; } } public EFun read_external_fun ( ) throws IOException { final int tag = read1skip_version ( ) ; if ( tag != EExternal . externalFunTag ) { throw new IOException ( "<STR_LIT>" + tag ) ; } final EAtom module = read_atom ( ) ; final EAtom function = read_atom ( ) ; final int arity = ( int ) read_long ( ) ; return EModuleManager . resolve ( new FunID ( module , function , arity ) ) ; } public ESeq read_string ( ) throws IOException { int tag ; int len ; byte [ ] strbuf ; int [ ] intbuf ; tag = read1skip_version ( ) ; switch ( tag ) { case EExternal . stringTag : len = read2BE ( ) ; strbuf = new byte [ len ] ; this . readN ( strbuf ) ; return EString . make ( strbuf ) ; case EExternal . nilTag : return ERT . NIL ; case EExternal . listTag : len = read4BE ( ) ; intbuf = new int [ len ] ; boolean unicode = false ; for ( int i = <NUM_LIT:0> ; i < len ; i ++ ) { intbuf [ i ] = read_int ( ) ; unicode |= intbuf [ i ] > <NUM_LIT> ; if ( ! EString . isValidCodePoint ( intbuf [ i ] ) ) { throw new IOException ( "<STR_LIT>" + intbuf [ i ] ) ; } } read_nil ( ) ; return EList . make ( intbuf ) ; default : throw new IOException ( "<STR_LIT>" + EExternal . stringTag + "<STR_LIT>" + EExternal . listTag + "<STR_LIT>" + tag ) ; } } public EObject read_compressed ( ) throws IOException { final int tag = read1skip_version ( ) ; if ( tag != EExternal . compressedTag ) { throw new IOException ( "<STR_LIT>" + EExternal . compressedTag + "<STR_LIT>" + tag ) ; } final byte [ ] buf = read_size_and_inflate ( ) ; final EInputStream ois = new EInputStream ( buf , flags ) ; return ois . read_any ( ) ; } public byte [ ] read_size_and_inflate ( ) throws IOException { final int size = read4BE ( ) ; final byte [ ] buf = new byte [ size ] ; final java . util . zip . Inflater inflater = new java . util . zip . Inflater ( ) ; final java . util . zip . InflaterInputStream is = new java . util . zip . InflaterInputStream ( this , inflater ) ; try { int pos = <NUM_LIT:0> ; while ( pos < size ) { final int dsize = is . read ( buf , pos , size - pos ) ; if ( dsize == <NUM_LIT:0> ) break ; pos += dsize ; } if ( pos < size ) { throw new IOException ( "<STR_LIT>" + pos + "<STR_LIT>" + size ) ; } int read_ahead = inflater . getRemaining ( ) ; setPos ( getPos ( ) - read_ahead ) ; } finally { is . close ( ) ; } return buf ; } public EObject read_any ( ) throws IOException { final int tag = peek1skip_version ( ) ; switch ( tag ) { case EExternal . smallIntTag : case EExternal . intTag : case EExternal . smallBigTag : case EExternal . largeBigTag : return EInteger . read ( this ) ; case EExternal . atomCacheRef : case EExternal . atomTag : case EExternal . smallAtomTag : return EAtom . read ( this ) ; case EExternal . floatTag : case EExternal . newFloatTag : return EDouble . read ( this ) ; case EExternal . refTag : case EExternal . newRefTag : return ERef . read ( this ) ; case EExternal . portTag : return EPort . read ( this ) ; case EExternal . pidTag : return EPID . read ( this ) ; case EExternal . stringTag : return EString . read ( this ) ; case EExternal . listTag : case EExternal . nilTag : if ( ( flags & DECODE_INT_LISTS_AS_STRINGS ) != <NUM_LIT:0> ) { final int savePos = getPos ( ) ; try { return EString . read ( this ) ; } catch ( final IOException e ) { } setPos ( savePos ) ; } return EList . read ( this ) ; case EExternal . smallTupleTag : case EExternal . largeTupleTag : return ETuple . read ( this ) ; case EExternal . binTag : return EBinary . read ( this ) ; case EExternal . bitBinTag : return EBitString . read ( this ) ; case EExternal . compressedTag : return read_compressed ( ) ; case EExternal . newFunTag : case EExternal . funTag : return EFun . read ( this ) ; case EExternal . externalFunTag : return read_external_fun ( ) ; default : throw new IOException ( "<STR_LIT>" + tag + "<STR_LIT>" + getPos ( ) ) ; } } public void setAtomCacheRefs ( EAtom [ ] atomCacheRefs ) { this . atom_cache_refs = atomCacheRefs ; } } </s>
|
<s> package erjang ; public class EHash { static final int BINARY_DEF = <NUM_LIT> ; static final int LIST_DEF = <NUM_LIT> ; static final int NIL_DEF = <NUM_LIT> ; static final int TUPLE_DEF = <NUM_LIT> ; static final int PID_DEF = <NUM_LIT> ; static final int EXTERNAL_PID_DEF = <NUM_LIT> ; static final int PORT_DEF = <NUM_LIT> ; static final int EXTERNAL_PORT_DEF = <NUM_LIT> ; static final int EXPORT_DEF = <NUM_LIT> ; static final int FUN_DEF = <NUM_LIT> ; static final int REF_DEF = <NUM_LIT> ; static final int EXTERNAL_REF_DEF = <NUM_LIT> ; static final int ATOM_DEF = <NUM_LIT> ; static final int FLOAT_DEF = <NUM_LIT> ; static final int BIG_DEF = <NUM_LIT> ; static final int SMALL_DEF = <NUM_LIT> ; static final int FUNNY_NUMBER1 = <NUM_LIT> ; static final int FUNNY_NUMBER2 = <NUM_LIT> ; static final int FUNNY_NUMBER3 = <NUM_LIT> ; static final int FUNNY_NUMBER4 = <NUM_LIT> ; static final int FUNNY_NUMBER5 = <NUM_LIT> ; static final int FUNNY_NUMBER6 = <NUM_LIT> ; static final int FUNNY_NUMBER7 = <NUM_LIT> ; static final int FUNNY_NUMBER8 = <NUM_LIT> ; static final int FUNNY_NUMBER9 = <NUM_LIT> ; static final int FUNNY_NUMBER10 = <NUM_LIT> ; static final int FUNNY_NUMBER11 = <NUM_LIT> ; static final int FUNNY_NUMBER12 = <NUM_LIT> ; static final int HCONST = <NUM_LIT> ; int block_hash ( byte [ ] k , int length , int initval ) { int a , b , c ; int len ; len = length ; a = b = HCONST ; c = initval ; int p = <NUM_LIT:0> ; while ( len >= <NUM_LIT:12> ) { a += ( uint ( k [ p + <NUM_LIT:0> ] ) + uint ( k [ p + <NUM_LIT:1> ] ) << <NUM_LIT:8> + uint ( k [ p + <NUM_LIT:2> ] ) << <NUM_LIT:16> + uint ( k [ p + <NUM_LIT:3> ] ) << <NUM_LIT:24> ) ; b += ( uint ( k [ p + <NUM_LIT:4> ] ) + uint ( k [ p + <NUM_LIT:5> ] ) << <NUM_LIT:8> + uint ( k [ p + <NUM_LIT:6> ] ) << <NUM_LIT:16> + uint ( k [ p + <NUM_LIT:7> ] ) << <NUM_LIT:24> ) ; c += ( uint ( k [ p + <NUM_LIT:8> ] ) + uint ( k [ p + <NUM_LIT:9> ] ) << <NUM_LIT:8> + uint ( k [ p + <NUM_LIT:10> ] ) << <NUM_LIT:16> + uint ( k [ p + <NUM_LIT:11> ] ) << <NUM_LIT:24> ) ; a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:8> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT> ) ; a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT:12> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:16> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT:5> ) ; a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT:3> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:10> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT:15> ) ; p += <NUM_LIT:12> ; len -= <NUM_LIT:12> ; } c += length ; switch ( len ) { case <NUM_LIT:11> : c += uint ( k [ p + <NUM_LIT:10> ] ) << <NUM_LIT:24> ; case <NUM_LIT:10> : c += uint ( k [ p + <NUM_LIT:9> ] ) << <NUM_LIT:16> ; case <NUM_LIT:9> : c += uint ( k [ p + <NUM_LIT:8> ] ) << <NUM_LIT:8> ; case <NUM_LIT:8> : b += uint ( k [ p + <NUM_LIT:7> ] ) << <NUM_LIT:24> ; case <NUM_LIT:7> : b += uint ( k [ p + <NUM_LIT:6> ] ) << <NUM_LIT:16> ; case <NUM_LIT:6> : b += uint ( k [ p + <NUM_LIT:5> ] ) << <NUM_LIT:8> ; case <NUM_LIT:5> : b += uint ( k [ <NUM_LIT:4> ] ) ; case <NUM_LIT:4> : a += uint ( k [ p + <NUM_LIT:3> ] ) << <NUM_LIT:24> ; case <NUM_LIT:3> : a += uint ( k [ p + <NUM_LIT:2> ] ) << <NUM_LIT:16> ; case <NUM_LIT:2> : a += uint ( k [ p + <NUM_LIT:1> ] ) << <NUM_LIT:8> ; case <NUM_LIT:1> : a += uint ( k [ p + <NUM_LIT:0> ] ) ; } c = mix ( a , b , c ) ; return c ; } static final int uint ( byte b ) { return ( ( int ) b ) & <NUM_LIT> ; } static final int mix ( int a , int b , int c ) { a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:8> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT> ) ; a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT:12> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:16> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT:5> ) ; a -= b ; a -= c ; a ^= ( c > > > <NUM_LIT:3> ) ; b -= c ; b -= a ; b ^= ( a << <NUM_LIT:10> ) ; c -= a ; c -= b ; c ^= ( b > > > <NUM_LIT:15> ) ; return c ; } static final int HCONST_2 = <NUM_LIT> ; static final int HCONST_3 = <NUM_LIT> ; static final int HCONST_4 = <NUM_LIT> ; static final int HCONST_5 = <NUM_LIT> ; static final int HCONST_6 = <NUM_LIT> ; static final int HCONST_7 = <NUM_LIT> ; static final int HCONST_8 = <NUM_LIT> ; static final int HCONST_9 = <NUM_LIT> ; static final int HCONST_10 = <NUM_LIT> ; static final int HCONST_11 = <NUM_LIT> ; static final int HCONST_12 = <NUM_LIT> ; static final int HCONST_13 = <NUM_LIT> ; static final int HCONST_14 = <NUM_LIT> ; static final int HCONST_15 = <NUM_LIT> ; static final int HCONST_NIL = ( int ) <NUM_LIT> ; static final int UINT32_HASH_2 ( int expr1 , int expr2 , int aconst , int hash ) { int a , b ; a = aconst + expr1 ; b = aconst + expr2 ; return mix ( a , b , hash ) ; } static final int UINT32_HASH ( int expr , int aconst , int hash ) { return UINT32_HASH_2 ( expr , <NUM_LIT:0> , aconst , hash ) ; } static final int SINT32_HASH ( int expr , int aconst , int hash ) { if ( expr < <NUM_LIT:0> ) { return UINT32_HASH ( - expr , aconst , hash ) ; } else { return UINT32_HASH ( expr , aconst , hash ) ; } } public static int hash2 ( EAtom val , int hash ) { if ( hash == <NUM_LIT:0> ) { return val . hash ; } else { return UINT32_HASH ( val . hash , HCONST_3 , hash ) ; } } public static int hash2 ( ESmall val , int hash ) { return SINT32_HASH ( val . value , HCONST , hash ) ; } public static int hash2 ( EString val , int hash ) { int c = <NUM_LIT:0> ; int sh = <NUM_LIT:0> ; for ( int p = <NUM_LIT:0> ; p < val . length ( ) ; p ++ ) { sh = ( sh << <NUM_LIT:8> ) + val . charAt ( p ) ; if ( c == <NUM_LIT:3> ) { hash = UINT32_HASH ( sh , HCONST_4 , hash ) ; c = sh = <NUM_LIT:0> ; } else { c ++ ; } } if ( c > <NUM_LIT:0> ) { hash = UINT32_HASH ( sh , HCONST_4 , hash ) ; } return hash ; } public static int hash2 ( ERef ref , int hash ) { return UINT32_HASH ( ref . internal_ref_numbers ( ) [ <NUM_LIT:0> ] , HCONST_7 , hash ) ; } public static int hash2 ( EInternalPort port , int hash ) { return UINT32_HASH ( port . internal_port_number ( ) , HCONST_6 , hash ) ; } public static int hash2 ( EInternalPID pid , int hash ) { return UINT32_HASH ( pid . internal_pid_number ( ) , HCONST_5 , hash ) ; } public static int hash2 ( ENil nil , int hash ) { if ( hash == <NUM_LIT:0> ) { return HCONST_NIL ; } else { return UINT32_HASH ( NIL_DEF , HCONST_2 , hash ) ; } } } </s>
|
<s> package erjang ; import java . io . File ; import java . io . IOException ; import java . io . InputStream ; import java . net . URL ; import java . net . URLClassLoader ; import erjang . beam . RamClassRepo ; public class EModuleClassLoader extends URLClassLoader { private final RamClassRepo ramClassRepo ; public EModuleClassLoader ( URL loadFrom , RamClassRepo repo ) { super ( loadFrom == null ? new URL [ <NUM_LIT:0> ] : new URL [ ] { loadFrom } , EObject . class . getClassLoader ( ) ) ; this . ramClassRepo = repo ; } public EModuleClassLoader ( URL loadFrom ) { this ( loadFrom , null ) ; } String ETUPLE_NAME = ETuple . class . getName ( ) ; String EFUN_NAME = EFun . class . getName ( ) ; @ Override protected Class < ? > findClass ( String name ) throws ClassNotFoundException { if ( name . startsWith ( ETUPLE_NAME ) ) { int arity = Integer . parseInt ( name . substring ( ETUPLE_NAME . length ( ) ) ) ; return ETuple . get_tuple_class ( arity ) ; } if ( name . startsWith ( EFUN_NAME ) ) { if ( name . endsWith ( "<STR_LIT>" ) ) { int num_start = EFUN_NAME . length ( ) ; int num_end = name . length ( ) - "<STR_LIT>" . length ( ) ; int arity = Integer . parseInt ( name . substring ( num_start , num_end ) ) ; return EFun . get_exported_fun_class ( arity ) ; } else { int arity = Integer . parseInt ( name . substring ( EFUN_NAME . length ( ) ) ) ; return EFun . get_fun_class ( arity ) ; } } if ( name . startsWith ( "<STR_LIT>" ) ) { String classResourceName = name . replace ( '<CHAR_LIT:.>' , '<CHAR_LIT:/>' ) + "<STR_LIT:.class>" ; InputStream resource = super . getResourceAsStream ( classResourceName ) ; byte [ ] bb ; if ( resource != null ) { try { bb = new byte [ resource . available ( ) ] ; resource . read ( bb ) ; } catch ( IOException ex ) { throw new Error ( ex ) ; } } else if ( ramClassRepo != null ) { bb = ramClassRepo . get ( name ) ; } else bb = null ; if ( bb == null ) { throw new ClassNotFoundException ( name , new Error ( "<STR_LIT>" + this . getURLs ( ) [ <NUM_LIT:0> ] ) ) ; } return ERT . defineClass ( EModuleClassLoader . class . getClassLoader ( ) , name , bb ) ; } if ( ramClassRepo != null ) { byte [ ] data = ramClassRepo . get ( name ) ; if ( data != null ) { return super . defineClass ( name , data , <NUM_LIT:0> , data . length ) ; } } return super . findClass ( name ) ; } } </s>
|
<s> package erjang ; public abstract class ELazySeq extends ESeq { ESeq value = null ; @ Override public ESeq cons ( EObject h ) { return value ( ) . cons ( h ) ; } private ESeq value ( ) { if ( value == null ) { value = initialValue ( ) ; assert value . testNil ( ) == null ; } return value ; } protected abstract ESeq initialValue ( ) ; @ Override public ESeq tail ( ) { return value ( ) . tail ( ) ; } @ Override public EObject head ( ) { return value ( ) . head ( ) ; } @ Override public boolean isNil ( ) { return value ( ) . isNil ( ) ; } @ Override public ESeq testSeq ( ) { return value ( ) . testSeq ( ) ; } } </s>
|
<s> package erjang ; import java . util . Timer ; import java . util . TimerTask ; import java . util . concurrent . ConcurrentHashMap ; import java . util . Collections ; import java . util . Map ; import kilim . Pausable ; import kilim . Task ; public abstract class ETimerTask extends TimerTask implements ExitHook { static ConcurrentHashMap < ERef , ETimerTask > timer_refs = new ConcurrentHashMap ( ) ; static Timer send_timer = new Timer ( ) ; final ERef ref ; private final EInternalPID pid ; long when = - <NUM_LIT:1> ; public ETimerTask ( ) { ref = ERT . getLocalNode ( ) . createRef ( ) ; pid = null ; timer_refs . put ( ref , this ) ; } public ETimerTask ( EInternalPID pid ) { ref = ERT . getLocalNode ( ) . createRef ( ) ; this . pid = pid ; if ( pid != null ) { pid . add_exit_hook ( this ) ; } timer_refs . put ( ref , this ) ; } @ Override public synchronized final void run ( ) { if ( timer_refs . remove ( ref ) == null ) { return ; } if ( pid != null ) { pid . remove_exit_hook ( this ) ; } ERT . scheduler . schedule ( new Task ( ) { @ Override public void execute ( ) throws Pausable , Exception { on_timeout ( ) ; } } ) ; } @ Override public boolean cancel ( ) { return cancel_timer ( ) >= <NUM_LIT:0> ; } public synchronized long cancel_timer ( ) { if ( super . cancel ( ) ) { timer_refs . remove ( ref ) ; if ( pid != null ) { pid . remove_exit_hook ( this ) ; } return when - System . currentTimeMillis ( ) ; } else { return - <NUM_LIT:1> ; } } public static long cancel ( ERef ref ) { ETimerTask timer_task = timer_refs . get ( ref ) ; if ( timer_task != null ) { return timer_task . cancel_timer ( ) ; } return - <NUM_LIT:1> ; } public synchronized final void on_exit ( EInternalPID pid ) throws Pausable { assert ( pid == this . pid ) : "<STR_LIT>" ; timer_refs . remove ( ref ) ; this . cancel ( ) ; } ; protected abstract void on_timeout ( ) throws Pausable ; public void schedule ( long ms_delay ) { when = System . currentTimeMillis ( ) + ms_delay ; send_timer . schedule ( this , ms_delay ) ; } public static long read_timer ( ERef ref ) { ETimerTask timer_task = timer_refs . get ( ref ) ; if ( timer_task != null ) { return timer_task . when - System . currentTimeMillis ( ) ; } return - <NUM_LIT:1> ; } public static void shutdown ( ) { send_timer . cancel ( ) ; send_timer . purge ( ) ; } } </s>
|
<s> package erjang ; import erjang . m . rpc . MBox ; public class RPC { static EAtom am_$gen_call = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_rex = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_undefined = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_user = EAtom . intern ( "<STR_LIT:user>" ) ; static EAtom am_call = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_rpc = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_call_from_java = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_init = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_get_status = EAtom . intern ( "<STR_LIT>" ) ; static EAtom am_started = EAtom . intern ( "<STR_LIT>" ) ; public static EObject call ( EAtom mod , EAtom fun , EObject ... args ) { return call ( mod , fun , EList . fromArray ( args ) ) ; } public static EObject call ( EAtom mod , EAtom fun , ESeq args ) { MBox mbox = new MBox ( ) ; ESeq callargs = EList . make ( mod , fun , args , mbox ) ; EProc proc = new EProc ( default_groupleader ( ) , am_rpc , am_call_from_java , callargs ) ; ERT . run ( proc ) ; return mbox . get_b ( ) ; } private static EPID default_groupleader ( ) { return erjang . m . rpc . Native . get_local_group_leader ( ) ; } public static EObject call ( EAtom mod , EAtom fun , ESeq args , long timeout ) { MBox mbox = new MBox ( ) ; EProc proc = new EProc ( default_groupleader ( ) , am_rpc , am_call_from_java , EList . make ( mod , fun , args , mbox ) ) ; ERT . run ( proc ) ; return mbox . get_b ( timeout ) ; } public static void wait_for_erjang_started ( long timeout ) { erjang . m . rpc . Native . wait_for_started ( timeout ) ; } } </s>
|
<s> package erjang ; import java . lang . annotation . ElementType ; import java . lang . annotation . Retention ; import java . lang . annotation . RetentionPolicy ; import java . lang . annotation . Target ; @ Retention ( RetentionPolicy . RUNTIME ) @ Target ( ElementType . TYPE ) public @ interface Module { String value ( ) ; } </s>
|
<s> package erjang ; import java . math . BigInteger ; import java . io . IOException ; public abstract class EInteger extends ENumber { abstract BigInteger bigintValue ( ) ; public EInteger testInteger ( ) { return this ; } public EInteger asInteger ( ) { return this ; } public abstract EInteger dec ( ) ; public long longValue ( ) { return bigintValue ( ) . longValue ( ) ; } public static EInteger read ( EInputStream ei ) throws IOException { return ei . read_tagged_integer ( ) ; } } </s>
|
<s> package erjang ; import java . io . File ; import java . io . IOException ; import java . util . ArrayList ; import erjang . driver . efile . EFile ; public class Main { public static final String SYSTEM_ARCHITECTURE = "<STR_LIT>" ; public static final String DRIVER_VERSION = "<STR_LIT>" ; public static void main ( String [ ] args ) throws Exception { ErjangConfig . init ( ) ; ArrayList < String > ra = new ArrayList < String > ( ) ; String cmd_line_root = null ; String otp_version = ( ErjangConfig . hasString ( "<STR_LIT>" ) ? ErjangConfig . getString ( "<STR_LIT>" , null ) : null ) ; String erts_version = ( ErjangConfig . hasString ( "<STR_LIT>" ) ? "<STR_LIT>" + ErjangConfig . getString ( "<STR_LIT>" , null ) : null ) ; for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { if ( "<STR_LIT>" . equals ( args [ i ] ) && i < args . length ) { cmd_line_root = args [ i + <NUM_LIT:1> ] ; i += <NUM_LIT:1> ; } else if ( "<STR_LIT>" . equals ( args [ i ] ) && i < args . length ) { erts_version = "<STR_LIT>" + args [ i + <NUM_LIT:1> ] ; i += <NUM_LIT:1> ; } } RuntimeInfo runtimeInfo = RuntimeInfo . setup ( erts_version , otp_version , cmd_line_root ) ; try { runtimeInfo . verify ( ) ; } catch ( RuntimeException e ) { String reason = e . getMessage ( ) ; ERT . log . severe ( reason ) ; System . err . println ( reason ) ; return ; } String root = runtimeInfo . erl_rootdir ; ra . add ( "<STR_LIT>" ) ; ra . add ( root ) ; arg_loop : for ( int i = <NUM_LIT:0> ; i < args . length ; i ++ ) { String arg = args [ i ] ; if ( "<STR_LIT>" . equals ( arg ) ) { for ( int ii = i ; ii < args . length ; ii ++ ) { ra . add ( args [ ii ] ) ; } break ; } if ( "<STR_LIT>" . equals ( args [ i ] ) && i < args . length ) { i ++ ; continue ; } if ( arg . startsWith ( "<STR_LIT:+>" ) ) { switch ( arg . charAt ( <NUM_LIT:1> ) ) { case '<CHAR_LIT:a>' : case '<CHAR_LIT:e>' : case '<CHAR_LIT:A>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : case '<CHAR_LIT>' : System . setProperty ( "<STR_LIT>" + arg . substring ( <NUM_LIT:1> ) , args [ i + <NUM_LIT:1> ] ) ; i += <NUM_LIT:1> ; continue arg_loop ; default : System . setProperty ( "<STR_LIT>" + arg . substring ( <NUM_LIT:1> ) , "<STR_LIT:true>" ) ; continue arg_loop ; } } ra . add ( arg ) ; } if ( ! ra . contains ( "<STR_LIT>" ) ) { ra . add ( "<STR_LIT>" ) ; ra . add ( System . getProperty ( "<STR_LIT>" ) ) ; } ERT . setRuntimeInfo ( runtimeInfo ) ; if ( ! ( new File ( runtimeInfo . erl_bootstrap_ebindir ) ) . exists ( ) && ! runtimeInfo . erl_bootstrap_ebindir . startsWith ( EFile . RESOURCE_PREFIX ) ) { ERT . log . severe ( "<STR_LIT>" + runtimeInfo . erl_bootstrap_ebindir ) ; throw new IllegalArgumentException ( "<STR_LIT>" + runtimeInfo . erl_bootstrap_ebindir ) ; } OTPMain . main ( ra . toArray ( new String [ ra . size ( ) ] ) ) ; } } </s>
|
<s> package erjang ; import java . io . IOException ; import java . nio . ByteBuffer ; import java . util . List ; import java . util . Set ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; import erjang . m . ets . EMatchContext ; import erjang . m . ets . EPattern ; import erjang . m . ets . ETermPattern ; public class EBitString extends EObject { public static final Type EBITSTRING_TYPE = Type . getType ( EBitString . class ) ; public static final String EBITSTRING_NAME = EBITSTRING_TYPE . getInternalName ( ) ; protected final byte [ ] data ; private final int data_offset ; private final int byte_size ; protected final int extra_bits ; protected EBitString ( byte [ ] data ) { this ( data . clone ( ) , <NUM_LIT:0> , data . length , <NUM_LIT:0> ) ; } @ Override public int hashCode ( ) { int h = <NUM_LIT:0> ; for ( int i = <NUM_LIT:0> ; i < byte_size ; i ++ ) { h = h * <NUM_LIT:31> + data [ data_offset + i ] ; } if ( extra_bits != <NUM_LIT:0> ) { int val = data [ data_offset + byte_size ] > > > ( <NUM_LIT:8> - extra_bits ) ; h = h * <NUM_LIT:31> + val ; } return h ; } public long bitSize ( ) { return byteSize ( ) * <NUM_LIT> + extra_bits ; } protected int dataByteSize ( ) { return byteSize ( ) + ( extra_bits > <NUM_LIT:0> ? <NUM_LIT:1> : <NUM_LIT:0> ) ; } public EBitString testBitString ( ) { return this ; } public boolean match ( ETermPattern matcher , EMatchContext r ) { return matcher . match ( this , r ) ; } public EBinary testBinary ( ) { if ( isBinary ( ) ) { assert false : "<STR_LIT>" ; return new EBinary ( toByteArray ( ) ) ; } return null ; } @ Override int cmp_order ( ) { return CMP_ORDER_BITSTRING ; } @ Override public Type emit_const ( MethodVisitor fa ) { char [ ] chs = new char [ dataByteSize ( ) ] ; for ( int byteIdx = <NUM_LIT:0> ; byteIdx < byteSize ( ) ; byteIdx += <NUM_LIT:1> ) { chs [ byteIdx ] = ( char ) ( data [ byteIdx ] & <NUM_LIT> ) ; } if ( extra_bits != <NUM_LIT:0> ) { int rest = intBitsAt ( byteSize ( ) * <NUM_LIT:8> , extra_bits ) ; rest <<= ( <NUM_LIT:8> - extra_bits ) ; chs [ byteSize ( ) ] = ( char ) rest ; } String str = new String ( chs ) ; fa . visitLdcInsn ( str ) ; fa . visitLdcInsn ( new Integer ( extra_bits ) ) ; fa . visitMethodInsn ( Opcodes . INVOKESTATIC , EBITSTRING_NAME , "<STR_LIT>" , "<STR_LIT>" + EBITSTRING_NAME + "<STR_LIT:;>" ) ; return EBITSTRING_TYPE ; } public static EBitString make ( String str , int extra ) { int in_len = str . length ( ) ; byte [ ] data = new byte [ in_len ] ; for ( int i = <NUM_LIT:0> ; i < in_len ; i ++ ) { data [ i ] = ( byte ) str . charAt ( i ) ; } int data_len = in_len - ( extra > <NUM_LIT:0> ? <NUM_LIT:1> : <NUM_LIT:0> ) ; return EBitString . make ( data , <NUM_LIT:0> , data_len , extra ) ; } @ Override public boolean equalsExactly ( EObject rhs ) { if ( rhs . cmp_order ( ) != CMP_ORDER_BITSTRING ) return false ; EBitString ebs = ( EBitString ) rhs ; if ( byteSize ( ) != ebs . byteSize ( ) ) return false ; if ( extra_bits != ebs . extra_bits ) return false ; int byteOffset = byteOffset ( ) ; int ebsByteOffset = ebs . byteOffset ( ) ; for ( int i = <NUM_LIT:0> ; i < byte_size ; i ++ ) { if ( data [ byteOffset + i ] != ebs . data [ ebsByteOffset + i ] ) return false ; } if ( extra_bits != <NUM_LIT:0> ) { int myExtraBits = intBitsAt ( <NUM_LIT> * byte_size , extra_bits ) ; int hisExtraBits = ebs . intBitsAt ( <NUM_LIT> * byte_size , extra_bits ) ; return myExtraBits == hisExtraBits ; } else { return true ; } } @ Override int compare_same ( EObject rhs ) { EBitString ebs = ( EBitString ) rhs ; long bc1 = bitSize ( ) ; long bc2 = ebs . bitSize ( ) ; long limit = Math . min ( bc1 , bc2 ) ; for ( int pos = <NUM_LIT:0> ; pos < limit ; pos += <NUM_LIT:8> ) { int rest = limit - pos > <NUM_LIT:8> ? <NUM_LIT:8> : ( int ) ( limit - pos ) ; int oc1 = <NUM_LIT> & intBitsAt ( pos , rest ) ; int oc2 = <NUM_LIT> & ebs . intBitsAt ( pos , rest ) ; if ( oc1 == oc2 ) continue ; if ( oc1 < oc2 ) return - <NUM_LIT:1> ; if ( oc1 > oc2 ) return <NUM_LIT:1> ; } if ( bc1 == bc2 ) return <NUM_LIT:0> ; if ( bc1 < bc2 ) return - <NUM_LIT:1> ; else return <NUM_LIT:1> ; } public static EBitString make ( byte [ ] data , int byteOff , int byteLength , int extra_bits ) { if ( extra_bits == <NUM_LIT:0> ) { return new EBinary ( data , byteOff , byteLength ) ; } else { return new EBitString ( data , byteOff , byteLength , extra_bits ) ; } } public static EBitString makeByteOffsetTail ( EBitString org , int byteOff ) { return EBitString . make ( org . data , org . data_offset + byteOff , org . byte_size - byteOff , org . extra_bits ) ; } protected EBitString ( byte [ ] data , int byte_off , int byte_len , int extra_bits ) { this . data = data ; this . data_offset = byte_off ; this . byte_size = byte_len ; this . extra_bits = extra_bits ; if ( data . length < byte_off + byte_len + ( extra_bits > <NUM_LIT:0> ? <NUM_LIT:1> : <NUM_LIT:0> ) ) { throw new IllegalArgumentException ( ) ; } if ( extra_bits < <NUM_LIT:0> || extra_bits >= <NUM_LIT:8> ) { throw new IllegalArgumentException ( ) ; } } public boolean isBinary ( ) { return extra_bits == <NUM_LIT:0> ; } public int octetAt ( int byteIndex ) { return data [ byteOffset ( ) + byteIndex ] & <NUM_LIT> ; } public int uint16_at ( int byteIndex , int flags ) { int b1 = data [ byteOffset ( ) + byteIndex ] & <NUM_LIT> ; int b2 = data [ byteOffset ( ) + byteIndex + <NUM_LIT:1> ] & <NUM_LIT> ; if ( ( flags & EBinMatchState . BSF_LITTLE ) > <NUM_LIT:0> ) return b1 + ( b2 << <NUM_LIT:8> ) ; else return ( b1 << <NUM_LIT:8> ) + b2 ; } public int int32_at ( int byteIndex , int flags ) { int b1 = data [ byteOffset ( ) + byteIndex ] & <NUM_LIT> ; int b2 = data [ byteOffset ( ) + byteIndex + <NUM_LIT:1> ] & <NUM_LIT> ; int b3 = data [ byteOffset ( ) + byteIndex + <NUM_LIT:2> ] & <NUM_LIT> ; int b4 = data [ byteOffset ( ) + byteIndex + <NUM_LIT:3> ] & <NUM_LIT> ; if ( ( flags & EBinMatchState . BSF_LITTLE ) > <NUM_LIT:0> ) return b1 + ( b2 << <NUM_LIT:8> ) + ( b3 << <NUM_LIT:16> ) + ( b4 << <NUM_LIT:24> ) ; else return ( b1 << <NUM_LIT:24> ) + ( b2 << <NUM_LIT:16> ) + ( b3 << <NUM_LIT:8> ) + b4 ; } public EBitString substring ( long bitOff ) { return substring ( bitOff , bitSize ( ) - bitOff ) ; } public EBitString substring ( long bitOff , long bit_len ) { if ( bitOff < <NUM_LIT:0> || bitOff + bit_len > bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } bitOff += byteOffset ( ) * <NUM_LIT:8> ; int out_full_bytes = ( int ) ( bit_len / <NUM_LIT:8> ) ; int extra = ( int ) ( bit_len % <NUM_LIT:8> ) ; if ( <NUM_LIT:0> == ( bitOff % <NUM_LIT:8> ) ) { return EBitString . make ( data , ( int ) ( bitOff / <NUM_LIT:8> ) , out_full_bytes , extra ) ; } int out_bytes = ( int ) ( out_full_bytes + ( extra == <NUM_LIT:0> ? <NUM_LIT:0> : <NUM_LIT:1> ) ) ; byte [ ] res = new byte [ out_bytes ] ; for ( int i = <NUM_LIT:0> ; i < out_full_bytes ; i ++ ) { res [ i ] = ( byte ) intBitsAt ( bitOff + i * <NUM_LIT:8> , <NUM_LIT:8> ) ; } if ( extra != <NUM_LIT:0> ) { res [ ( int ) out_full_bytes ] = ( byte ) ( intBitsAt ( bitOff + bit_len - extra , extra ) << ( <NUM_LIT:8> - extra ) ) ; } return EBitString . make ( res , <NUM_LIT:0> , ( int ) out_full_bytes , extra ) ; } public int bitAt ( long bitPos ) { if ( bitPos < <NUM_LIT:0> || bitPos >= bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } bitPos += byteOffset ( ) * <NUM_LIT:8> ; int data_byte = ( int ) data [ ( int ) ( bitPos > > > <NUM_LIT:3> ) ] ; int shift = <NUM_LIT:7> - ( int ) ( bitPos & <NUM_LIT> ) ; int bit = <NUM_LIT> & ( data_byte > > shift ) ; return bit ; } public int intBitsAt ( long bitPos , int bitLength ) { if ( bitPos + bitLength > this . bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } if ( bitLength < <NUM_LIT:0> || bitLength > <NUM_LIT:32> ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; bitPos += byteOffset ( ) * <NUM_LIT:8> ; int res = <NUM_LIT:0> ; if ( ( bitPos & <NUM_LIT> ) != <NUM_LIT:0> ) { int len = <NUM_LIT:8> - ( int ) ( bitPos & <NUM_LIT> ) ; int val = <NUM_LIT> & ( int ) data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] ; res = val & ( ( <NUM_LIT:1> << len ) - <NUM_LIT:1> ) ; if ( bitLength < len ) { res >>>= ( len - bitLength ) ; return res ; } bitLength -= len ; bitPos += len ; } assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; int pos = ( int ) ( bitPos > > <NUM_LIT:3> ) ; while ( bitLength > <NUM_LIT:7> ) { res <<= <NUM_LIT:8> ; res |= <NUM_LIT> & ( int ) data [ pos ++ ] ; bitPos += <NUM_LIT:8> ; bitLength -= <NUM_LIT:8> ; } assert ( bitLength < <NUM_LIT:8> ) ; if ( bitLength != <NUM_LIT:0> ) { int len = bitLength ; int val = <NUM_LIT> & ( int ) data [ pos ] ; res <<= bitLength ; res |= val > > ( <NUM_LIT:8> - len ) ; bitLength -= len ; bitPos += len ; } assert ( bitLength == <NUM_LIT:0> ) ; return res ; } public int intLittleEndianBitsAt ( long bitPos , int bitLength ) { if ( bitPos + bitLength > this . bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } if ( bitLength < <NUM_LIT:0> || bitLength > <NUM_LIT:32> ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; bitPos += byteOffset ( ) * <NUM_LIT:8> ; int res = <NUM_LIT:0> ; int res_offset = <NUM_LIT:0> ; if ( ( bitPos & <NUM_LIT> ) != <NUM_LIT:0> ) { int len = <NUM_LIT:8> - ( int ) ( bitPos & <NUM_LIT> ) ; int val = data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] & <NUM_LIT> ; res = val & ( ( <NUM_LIT:1> << len ) - <NUM_LIT:1> ) ; if ( bitLength < len ) { res >>>= ( len - bitLength ) ; return res ; } res_offset += len ; bitLength -= len ; bitPos += len ; } assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; int pos = ( int ) ( bitPos > > <NUM_LIT:3> ) ; while ( bitLength > <NUM_LIT:7> ) { res |= ( data [ pos ++ ] & <NUM_LIT> ) << res_offset ; res_offset += <NUM_LIT:8> ; bitPos += <NUM_LIT:8> ; bitLength -= <NUM_LIT:8> ; } assert ( bitLength < <NUM_LIT:8> ) ; if ( bitLength != <NUM_LIT:0> ) { int len = bitLength ; int val = <NUM_LIT> & ( int ) data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] ; res |= ( val > > ( <NUM_LIT:8> - len ) ) << res_offset ; res_offset += len ; bitLength -= len ; bitPos += len ; } assert ( bitLength == <NUM_LIT:0> ) ; return res ; } static int signExtend ( int val , int bits ) { if ( bits == <NUM_LIT:32> ) return val ; int r ; int m = <NUM_LIT:1> << ( bits - <NUM_LIT:1> ) ; r = ( val ^ m ) - m ; return r ; } static long signExtend ( long val , int bits ) { if ( bits == <NUM_LIT> ) return val ; long r ; long m = <NUM_LIT:1L> << ( bits - <NUM_LIT:1> ) ; r = ( val ^ m ) - m ; return r ; } public byte byteAt ( int bitPos ) { return ( byte ) intBitsAt ( bitPos , <NUM_LIT:8> ) ; } public double doubleAt ( int bitPos ) { return Double . longBitsToDouble ( longBitsAt ( bitPos , <NUM_LIT> ) ) ; } public double floatAt ( int bitPos ) { return Float . intBitsToFloat ( intBitsAt ( bitPos , <NUM_LIT:32> ) ) ; } public long longBitsAt ( long bitPos , int bitLength ) { if ( bitPos + bitLength > this . bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } if ( bitLength < <NUM_LIT:0> || bitLength > <NUM_LIT> ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; long res = <NUM_LIT:0> ; bitPos += byteOffset ( ) * <NUM_LIT:8> ; if ( ( bitPos & <NUM_LIT> ) != <NUM_LIT:0> ) { int len = <NUM_LIT:8> - ( int ) ( bitPos & <NUM_LIT> ) ; int val = <NUM_LIT> & ( int ) data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] ; res = val & ( ( <NUM_LIT:1> << len ) - <NUM_LIT:1> ) ; if ( bitLength < len ) { res >>>= ( len - bitLength ) ; return res ; } bitLength -= len ; bitPos += len ; } assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; int pos = ( int ) ( bitPos > > <NUM_LIT:3> ) ; while ( bitLength > <NUM_LIT:7> ) { res <<= <NUM_LIT:8> ; res |= <NUM_LIT> & ( int ) data [ pos ++ ] ; bitPos += <NUM_LIT:8> ; bitLength -= <NUM_LIT:8> ; } assert ( bitLength < <NUM_LIT:8> ) ; assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; if ( bitLength != <NUM_LIT:0> ) { int len = bitLength ; int val = <NUM_LIT> & ( int ) data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] ; res <<= bitLength ; res |= val > > ( <NUM_LIT:8> - len ) ; bitLength -= len ; bitPos += len ; } assert ( bitLength == <NUM_LIT:0> ) ; return res ; } public long longLittleEndianBitsAt ( long bitPos , int bitLength ) { if ( bitPos + bitLength > this . bitSize ( ) ) { throw new IllegalArgumentException ( "<STR_LIT>" ) ; } if ( bitLength < <NUM_LIT:0> || bitLength > <NUM_LIT> ) throw new IllegalArgumentException ( "<STR_LIT>" ) ; long res = <NUM_LIT:0> ; int res_offset = <NUM_LIT:0> ; bitPos += byteOffset ( ) * <NUM_LIT:8> ; if ( ( bitPos & <NUM_LIT> ) != <NUM_LIT:0> ) { int len = <NUM_LIT:8> - ( int ) ( bitPos & <NUM_LIT> ) ; int val = data [ ( int ) ( bitPos > > <NUM_LIT:3> ) ] & <NUM_LIT> ; res = val & ( ( <NUM_LIT:1> << len ) - <NUM_LIT:1> ) ; if ( bitLength < len ) { res >>>= ( len - bitLength ) ; return res ; } res_offset += len ; bitLength -= len ; bitPos += len ; } assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; int pos = ( int ) ( bitPos > > <NUM_LIT:3> ) ; while ( bitLength >= <NUM_LIT:8> ) { res |= ( ( long ) ( data [ pos ++ ] & <NUM_LIT> ) ) << res_offset ; res_offset += <NUM_LIT:8> ; bitPos += <NUM_LIT:8> ; bitLength -= <NUM_LIT:8> ; } assert ( bitLength < <NUM_LIT:8> ) ; assert ( ( bitPos & <NUM_LIT> ) == <NUM_LIT:0> ) ; if ( bitLength != <NUM_LIT:0> ) { int len = bitLength ; int val = data [ pos ] & <NUM_LIT> ; res |= ( long ) ( val > > ( <NUM_LIT:8> - len ) ) << res_offset ; res_offset += len ; bitLength -= len ; bitPos += len ; } assert ( bitLength == <NUM_LIT:0> ) ; return res ; } @ Override public String toString ( ) { foo : if ( extra_bits == <NUM_LIT:0> ) { StringBuilder sb = new StringBuilder ( "<STR_LIT>" ) ; for ( int i = <NUM_LIT:0> ; i < bitSize ( ) ; i += <NUM_LIT:8> ) { char ch = ( char ) ( <NUM_LIT> & intBitsAt ( i , <NUM_LIT:8> ) ) ; if ( ch < '<CHAR_LIT:U+0020>' || ch > '<CHAR_LIT>' ) break foo ; sb . append ( ( char ) ch ) ; } sb . append ( "<STR_LIT>" ) ; return sb . toString ( ) ; } StringBuilder sb = new StringBuilder ( "<STR_LIT>" ) ; int i = <NUM_LIT:0> ; long max = Math . min ( bitSize ( ) - <NUM_LIT:8> , <NUM_LIT:20> * <NUM_LIT:8> ) ; for ( ; i < max ; i += <NUM_LIT:8> ) { sb . append ( <NUM_LIT> & intBitsAt ( i , <NUM_LIT:8> ) ) ; sb . append ( '<CHAR_LIT:U+002C>' ) ; } if ( max != bitSize ( ) - <NUM_LIT:8> ) { sb . append ( "<STR_LIT>" ) ; i = ( int ) ( bitSize ( ) - <NUM_LIT:8> ) ; } int lastBitLength = ( int ) ( bitSize ( ) - i ) ; sb . append ( <NUM_LIT> & intBitsAt ( i , lastBitLength ) ) ; if ( lastBitLength != <NUM_LIT:8> ) { sb . append ( '<CHAR_LIT::>' ) . append ( lastBitLength ) ; } sb . append ( "<STR_LIT>" ) ; return sb . toString ( ) ; } public byte [ ] toByteArray ( ) { byte [ ] result = new byte [ dataByteSize ( ) ] ; System . arraycopy ( data , byteOffset ( ) , result , <NUM_LIT:0> , dataByteSize ( ) ) ; return result ; } @ Override public boolean collectIOList ( List < ByteBuffer > out ) { if ( extra_bits != <NUM_LIT:0> ) return false ; if ( byteSize ( ) > <NUM_LIT:0> ) { out . add ( ByteBuffer . wrap ( data , byteOffset ( ) , byteSize ( ) ) ) ; } return true ; } public static EBitString read ( EInputStream eInputStream ) throws IOException { int [ ] pad_bits = new int [ <NUM_LIT:1> ] ; byte [ ] data = eInputStream . read_bitstr ( pad_bits ) ; int extra_bits = <NUM_LIT:8> - pad_bits [ <NUM_LIT:0> ] ; if ( extra_bits == <NUM_LIT:8> ) { return new EBinary ( data ) ; } else { int len = data . length - <NUM_LIT:1> ; return make ( data , <NUM_LIT:0> , data . length - <NUM_LIT:1> , extra_bits ) ; } } protected int byteOffset ( ) { return data_offset ; } public int byteSize ( ) { return byte_size ; } public int totalByteSize ( ) { return byte_size + ( extra_bits > <NUM_LIT:0> ? <NUM_LIT:1> : <NUM_LIT:0> ) ; } @ Override public void collectCharList ( CharCollector out ) throws CharCollector . CollectingException , CharCollector . InvalidElementException , IOException { if ( extra_bits != <NUM_LIT:0> ) throw new CharCollector . InvalidElementException ( ) ; try { out . addBinary ( data , data_offset , byte_size ) ; } catch ( CharCollector . PartialDecodingException e ) { int n = e . inputPos ; throw new CharCollector . CollectingException ( EBitString . make ( data , data_offset + n , byte_size - n , extra_bits ) ) ; } } @ Override public void encode ( EOutputStream eos ) { eos . write_bitstr ( toByteArray ( ) , extra_bits == <NUM_LIT:0> ? <NUM_LIT:0> : <NUM_LIT:8> - extra_bits ) ; } @ Override public ETermPattern compileMatch ( Set < Integer > out ) { return EPattern . compilePattern ( this , out ) ; } } </s>
|
<s> package erjang ; import java . io . File ; import java . io . FileOutputStream ; import java . io . IOException ; import java . util . HashMap ; import java . util . Map ; import java . util . Set ; import org . objectweb . asm . ClassAdapter ; import org . objectweb . asm . ClassWriter ; import org . objectweb . asm . Label ; import org . objectweb . asm . MethodVisitor ; import org . objectweb . asm . Opcodes ; import org . objectweb . asm . Type ; import org . objectweb . asm . util . CheckClassAdapter ; import erjang . m . ets . EMatchContext ; import erjang . m . ets . EPattern ; import erjang . m . ets . ETermPattern ; public abstract class ETuple extends EObject implements Cloneable { @ Override int cmp_order ( ) { return CMP_ORDER_TUPLE ; } @ Override int compare_same ( EObject rhs ) { ETuple other = ( ETuple ) rhs ; if ( arity ( ) < other . arity ( ) ) return - <NUM_LIT:1> ; if ( arity ( ) > other . arity ( ) ) return <NUM_LIT:1> ; for ( int i = <NUM_LIT:1> ; i <= arity ( ) ; i ++ ) { int cmp = elm ( i ) . erlangCompareTo ( other . elm ( i ) ) ; if ( cmp != <NUM_LIT:0> ) return cmp ; } return <NUM_LIT:0> ; } public ETuple testTuple ( ) { return this ; } public boolean match ( ETermPattern matcher , EMatchContext r ) { return matcher . match ( this , r ) ; } @ Override public ETermPattern compileMatch ( Set < Integer > out ) { return EPattern . compilePattern ( this , out ) ; } public abstract int arity ( ) ; public abstract EObject elm ( int i ) ; public static ETuple make ( int len ) { switch ( len ) { case <NUM_LIT:0> : return new ETuple0 ( ) ; case <NUM_LIT:1> : return new ETuple1 ( ) ; case <NUM_LIT:2> : return new ETuple2 ( ) ; case <NUM_LIT:3> : return new ETuple3 ( ) ; case <NUM_LIT:4> : return new ETuple4 ( ) ; default : return make_big ( len ) ; } } public static ETuple make ( EObject ... array ) { ETuple res = make ( array . length ) ; for ( int i = <NUM_LIT:0> ; i < array . length ; i ++ ) { res . set ( i + <NUM_LIT:1> , array [ i ] ) ; } return res ; } @ Override public ETuple clone ( ) { try { return ( ETuple ) super . clone ( ) ; } catch ( CloneNotSupportedException e ) { throw new Error ( ) ; } } public ETuple setelement ( int index , EObject term ) { ETuple t = clone ( ) ; t . set ( index , term ) ; return t ; } public abstract void set ( int index , EObject term ) ; public abstract ETuple blank ( ) ; private static final Type ETUPLE_TYPE = Type . getType ( ETuple . class ) ; private static final String ETUPLE_NAME = ETUPLE_TYPE . getInternalName ( ) ; private static final Type ETERM_TYPE = Type . getType ( EObject . class ) ; private static Map < Integer , ETuple > protos = new HashMap < Integer , ETuple > ( ) ; @ SuppressWarnings ( "<STR_LIT:unchecked>" ) private static ETuple make_big ( int size ) { ETuple proto = protos . get ( size ) ; if ( proto == null ) { try { Class < ? extends ETuple > c = get_tuple_class ( size ) ; protos . put ( size , proto = c . newInstance ( ) ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } } return proto . blank ( ) ; } @ SuppressWarnings ( "<STR_LIT:unchecked>" ) static public Class get_tuple_class ( int num_cells ) { try { return Class . forName ( ETuple . class . getName ( ) + num_cells ) ; } catch ( ClassNotFoundException e ) { } byte [ ] data = make_tuple_class_data ( num_cells ) ; String name = ( ETUPLE_NAME + num_cells ) . replace ( '<CHAR_LIT:/>' , '<CHAR_LIT:.>' ) ; return ERT . defineClass ( ETuple . class . getClassLoader ( ) , name , data ) ; } static byte [ ] make_tuple_class_data ( int num_cells ) { ClassWriter cww = new ClassWriter ( true ) ; CheckClassAdapter cw = new CheckClassAdapter ( cww ) ; String this_class_name = ETUPLE_NAME + num_cells ; String super_class_name = ETUPLE_NAME ; cw . visit ( Opcodes . V1_4 , Opcodes . ACC_PUBLIC | Opcodes . ACC_SUPER , this_class_name , null , super_class_name , null ) ; for ( int i = <NUM_LIT:1> ; i <= num_cells ; i ++ ) { cw . visitField ( Opcodes . ACC_PUBLIC , "<STR_LIT>" + i , ETERM_TYPE . getDescriptor ( ) , null , null ) ; } create_count ( cw , num_cells ) ; create_cast ( cw , num_cells ) ; create_cast2 ( cw , num_cells ) ; create_constructor ( cw , super_class_name ) ; create_tuple_copy ( num_cells , cw , this_class_name , super_class_name ) ; create_tuple_nth ( num_cells , cw , this_class_name ) ; create_tuple_set ( num_cells , cw , this_class_name ) ; cw . visitEnd ( ) ; byte [ ] data = cww . toByteArray ( ) ; return data ; } private static void create_tuple_copy ( int i , ClassAdapter cw , String this_class_name , String super_class_name ) { MethodVisitor mv ; make_blank_bridge ( cw , this_class_name , super_class_name ) ; mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" + this_class_name + "<STR_LIT:;>" , null , null ) ; mv . visitCode ( ) ; mv . visitTypeInsn ( Opcodes . NEW , this_class_name ) ; mv . visitInsn ( Opcodes . DUP ) ; mv . visitMethodInsn ( Opcodes . INVOKESPECIAL , this_class_name , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; } private static void make_blank_bridge ( ClassAdapter cw , String this_class_name , String super_class_name ) { MethodVisitor mv ; mv = cw . visitMethod ( Opcodes . ACC_PUBLIC | Opcodes . ACC_SYNTHETIC | Opcodes . ACC_BRIDGE , "<STR_LIT>" , "<STR_LIT>" + super_class_name + "<STR_LIT:;>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( Opcodes . INVOKEVIRTUAL , this_class_name , "<STR_LIT>" , "<STR_LIT>" + this_class_name + "<STR_LIT:;>" ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } private static void create_tuple_nth ( int n_cells , ClassAdapter cw , String this_class_name ) { MethodVisitor mv ; mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" + ETERM_TYPE . getDescriptor ( ) , null , null ) ; mv . visitCode ( ) ; Label dflt = new Label ( ) ; Label [ ] labels = new Label [ n_cells ] ; for ( int i = <NUM_LIT:0> ; i < n_cells ; i ++ ) { labels [ i ] = new Label ( ) ; } mv . visitVarInsn ( Opcodes . ILOAD , <NUM_LIT:1> ) ; mv . visitTableSwitchInsn ( <NUM_LIT:1> , n_cells , dflt , labels ) ; for ( int zbase = <NUM_LIT:0> ; zbase < n_cells ; zbase ++ ) { mv . visitLabel ( labels [ zbase ] ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; String field = "<STR_LIT>" + ( zbase + <NUM_LIT:1> ) ; mv . visitFieldInsn ( Opcodes . GETFIELD , this_class_name , field , ETERM_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( Opcodes . ARETURN ) ; } mv . visitLabel ( dflt ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ILOAD , <NUM_LIT:1> ) ; mv . visitMethodInsn ( Opcodes . INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" + ETERM_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private static void create_tuple_set ( int n_cells , ClassAdapter cw , String this_class_name ) { MethodVisitor mv ; mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" + ETERM_TYPE . getDescriptor ( ) + "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; Label dflt = new Label ( ) ; Label [ ] labels = new Label [ n_cells ] ; for ( int i = <NUM_LIT:0> ; i < n_cells ; i ++ ) { labels [ i ] = new Label ( ) ; } mv . visitVarInsn ( Opcodes . ILOAD , <NUM_LIT:1> ) ; mv . visitTableSwitchInsn ( <NUM_LIT:1> , n_cells , dflt , labels ) ; for ( int zbase = <NUM_LIT:0> ; zbase < n_cells ; zbase ++ ) { mv . visitLabel ( labels [ zbase ] ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:2> ) ; String field = "<STR_LIT>" + ( zbase + <NUM_LIT:1> ) ; mv . visitFieldInsn ( Opcodes . PUTFIELD , this_class_name , field , ETERM_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( Opcodes . RETURN ) ; } mv . visitLabel ( dflt ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitVarInsn ( Opcodes . ILOAD , <NUM_LIT:1> ) ; mv . visitMethodInsn ( Opcodes . INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" + ETERM_TYPE . getDescriptor ( ) ) ; mv . visitInsn ( Opcodes . POP ) ; mv . visitInsn ( Opcodes . RETURN ) ; mv . visitMaxs ( <NUM_LIT:3> , <NUM_LIT:3> ) ; mv . visitEnd ( ) ; } protected final EObject bad_nth ( int i ) { throw ERT . badarg ( this ) ; } private static void create_count ( ClassAdapter cw , int n ) { MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; if ( n <= <NUM_LIT:5> ) { mv . visitInsn ( Opcodes . ICONST_0 + n ) ; } else { mv . visitLdcInsn ( new Integer ( n ) ) ; } mv . visitInsn ( Opcodes . IRETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } private static void create_cast ( ClassAdapter cw , int n ) { MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC | Opcodes . ACC_STATIC , "<STR_LIT:cast>" , "<STR_LIT>" + ETUPLE_NAME + "<STR_LIT>" + ETUPLE_NAME + n + "<STR_LIT:;>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( Opcodes . INVOKEVIRTUAL , ETUPLE_NAME , "<STR_LIT>" , "<STR_LIT>" ) ; if ( n <= <NUM_LIT:5> ) { mv . visitInsn ( Opcodes . ICONST_0 + n ) ; } else { mv . visitLdcInsn ( new Integer ( n ) ) ; } Label fail = new Label ( ) ; mv . visitJumpInsn ( Opcodes . IF_ICMPNE , fail ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitTypeInsn ( Opcodes . CHECKCAST , ETUPLE_NAME + n ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitLabel ( fail ) ; mv . visitInsn ( Opcodes . ACONST_NULL ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:2> , <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private static void create_cast2 ( ClassAdapter cw , int n ) { MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC | Opcodes . ACC_STATIC , "<STR_LIT:cast>" , "<STR_LIT>" + Type . getInternalName ( EObject . class ) + "<STR_LIT>" + ETUPLE_NAME + n + "<STR_LIT:;>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitInsn ( Opcodes . DUP ) ; mv . visitTypeInsn ( Opcodes . INSTANCEOF , ETUPLE_NAME + n ) ; Label fail = new Label ( ) ; mv . visitJumpInsn ( Opcodes . IFEQ , fail ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitTypeInsn ( Opcodes . CHECKCAST , ETUPLE_NAME + n ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitLabel ( fail ) ; mv . visitInsn ( Opcodes . ACONST_NULL ) ; mv . visitInsn ( Opcodes . ARETURN ) ; mv . visitMaxs ( <NUM_LIT:2> , <NUM_LIT:2> ) ; mv . visitEnd ( ) ; } private static void create_constructor ( ClassAdapter cw , String super_class_name ) { MethodVisitor mv = cw . visitMethod ( Opcodes . ACC_PUBLIC , "<STR_LIT>" , "<STR_LIT>" , null , null ) ; mv . visitCode ( ) ; mv . visitVarInsn ( Opcodes . ALOAD , <NUM_LIT:0> ) ; mv . visitMethodInsn ( Opcodes . INVOKESPECIAL , super_class_name , "<STR_LIT>" , "<STR_LIT>" ) ; mv . visitInsn ( Opcodes . RETURN ) ; mv . visitMaxs ( <NUM_LIT:1> , <NUM_LIT:1> ) ; mv . visitEnd ( ) ; } static void dump ( String name , byte [ ] data ) { String pkg = name . substring ( <NUM_LIT:0> , name . lastIndexOf ( '<CHAR_LIT:/>' ) ) ; File out_dir = new File ( new File ( "<STR_LIT>" ) , pkg ) ; out_dir . mkdirs ( ) ; FileOutputStream fo ; try { String fname = "<STR_LIT>" + name + "<STR_LIT:.class>" ; fo = new FileOutputStream ( fname ) ; fo . write ( data ) ; fo . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } @ Override public String toString ( ) { StringBuilder sb = new StringBuilder ( "<STR_LIT:{>" ) ; for ( int i = <NUM_LIT:1> ; i <= this . arity ( ) ; i ++ ) { if ( i != <NUM_LIT:1> ) sb . append ( '<CHAR_LIT:U+002C>' ) ; sb . append ( elm ( i ) ) ; } sb . append ( "<STR_LIT:}>" ) ; return sb . toString ( ) ; } @ Override public int hashCode ( ) { int arity = arity ( ) ; int result = arity ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { result *= <NUM_LIT:3> ; result += elm ( i + <NUM_LIT:1> ) . hashCode ( ) ; } return result ; } @ Override public boolean equalsExactly ( EObject obj ) { if ( obj instanceof ETuple ) { ETuple ot = ( ETuple ) obj ; if ( arity ( ) != ot . arity ( ) ) return false ; for ( int i = <NUM_LIT:0> ; i < arity ( ) ; i ++ ) { if ( ! elm ( i + <NUM_LIT:1> ) . equalsExactly ( ot . elm ( i + <NUM_LIT:1> ) ) ) return false ; } return true ; } return false ; } @ Override public Type emit_const ( MethodVisitor fa ) { Type type = Type . getType ( this . getClass ( ) ) ; fa . visitTypeInsn ( Opcodes . NEW , type . getInternalName ( ) ) ; fa . visitInsn ( Opcodes . DUP ) ; fa . visitMethodInsn ( Opcodes . INVOKESPECIAL , type . getInternalName ( ) , "<STR_LIT>" , "<STR_LIT>" ) ; for ( int i = <NUM_LIT:0> ; i < arity ( ) ; i ++ ) { fa . visitInsn ( Opcodes . DUP ) ; ( ( EObject ) elm ( i + <NUM_LIT:1> ) ) . emit_const ( fa ) ; fa . visitFieldInsn ( Opcodes . PUTFIELD , type . getInternalName ( ) , "<STR_LIT>" + ( i + <NUM_LIT:1> ) , ETERM_TYPE . getDescriptor ( ) ) ; } return type ; } public static ETuple read ( EInputStream buf ) throws IOException { final int arity = buf . read_tuple_head ( ) ; ETuple res = ETuple . make ( arity ) ; for ( int i = <NUM_LIT:0> ; i < arity ; i ++ ) { res . set ( i + <NUM_LIT:1> , buf . read_any ( ) ) ; } return res ; } @ Override public void encode ( EOutputStream eos ) { int arity = arity ( ) ; eos . write_tuple_head ( arity ) ; for ( int i = <NUM_LIT:1> ; i <= arity ; i ++ ) { eos . write_any ( elm ( i ) ) ; } } } </s>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.