To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

bytecodes.py 3.89 KB
Newer Older
1
2
from rpython.rlib.unroll import unrolling_iterable

Stefan Marr's avatar
Stefan Marr committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Bytecodes(object):
    
    # Bytecodes used by the Simple Object Machine (SOM)
    halt             =  0
    dup              =  1
    push_local       =  2
    push_argument    =  3
    push_field       =  4
    push_block       =  5
    push_constant    =  6
    push_global      =  7
    pop              =  8
    pop_local        =  9
    pop_argument     = 10
    pop_field        = 11
    send             = 12
    super_send       = 13
    return_local     = 14
    return_non_local = 15
    
    _num_bytecodes   = 16
    
25
    _bytecode_length = [ 1, # halt
26
27
28
29
30
31
32
33
34
35
36
37
38
39
                         1,  # dup             
                         3,  # push_local      
                         3,  # push_argument   
                         2,  # push_field      
                         2,  # push_block      
                         2,  # push_constant   
                         2,  # push_global     
                         1,  # pop             
                         3,  # pop_local       
                         3,  # pop_argument    
                         2,  # pop_field       
                         2,  # send            
                         2,  # super_send      
                         1,  # return_local    
40
                         1 ] # return_non_local
Stefan Marr's avatar
Stefan Marr committed
41
    
42
    _stack_effect_depends_on_message = -1000 # chose a unresonable number to be recognizable
Stefan Marr's avatar
Stefan Marr committed
43
    
44
    _bytecode_stack_effect = [ 0,                               # halt            
45
46
47
48
49
50
51
52
53
54
55
56
57
58
                               1,                               # dup             
                               1,                               # push_local      
                               1,                               # push_argument   
                               1,                               # push_field      
                               1,                               # push_block      
                               1,                               # push_constant   
                               1,                               # push_global     
                              -1,                               # pop             
                              -1,                               # pop_local       
                              -1,                               # pop_argument    
                              -1,                               # pop_field       
                              _stack_effect_depends_on_message, # send            
                              _stack_effect_depends_on_message, # super_send      
                              0,                                # return_local    
59
                              0 ]                               # return_non_local
60

61
    
62
63
64
65
66
67
68
69
70
71
def bytecode_length(bytecode):
    return Bytecodes._bytecode_length[bytecode]


def bytecode_stack_effect(bytecode, number_of_arguments_of_message_send = 0):
    if bytecode_stack_effect_depends_on_send(bytecode):
        return -number_of_arguments_of_message_send + 1 # +1 in order to account for the return value
    else:
        return Bytecodes._bytecode_stack_effect[bytecode]

Stefan Marr's avatar
Stefan Marr committed
72

73
def bytecode_stack_effect_depends_on_send(bytecode):
74
75
    assert bytecode >= 0 and bytecode <= Bytecodes._num_bytecodes
    return Bytecodes._bytecode_stack_effect[bytecode] == Bytecodes._stack_effect_depends_on_message
76
77
78
79
80


def bytecode_as_str(bytecode):
    if not isinstance(bytecode, int):
        raise ValueError('bytecode is expected to be an integer.')
81
82
83
84
85
86
87
88
89
90
91
92
93

    if bytecode > len(_bytecode_names):
        raise ValueError('No Bytecode defined for the value %d.' % bytecode)
    else:
        return _bytecode_names[bytecode]

def _sorted_bytecode_names(cls):
    "NOT_RPYTHON"
    return [key.upper() for value, key in \
            sorted([(value, key) for key, value in cls.__dict__.items()]) \
            if isinstance(value, int) and key[0] != "_"
    ]
_bytecode_names = _sorted_bytecode_names(Bytecodes)