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

bytecodes.py 3.77 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    _bytecode_length = ( 1, # halt
                         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    
                         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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    _bytecode_stack_effect = ( 0,                               # halt            
                               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    
                              0 )                               # return_non_local

Stefan Marr's avatar
Stefan Marr committed
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
74
75
76
77
78
79
80
def bytecode_stack_effect_depends_on_send(bytecode):
    return Bytecodes._bytecode_stack_effect[bytecode] is Bytecodes._stack_effect_depends_on_message


def bytecode_as_str(bytecode):
    if not isinstance(bytecode, int):
        raise ValueError('bytecode is expected to be an integer.')
    
81
82
    bytecodes = unrolling_iterable(int_constants_of(Bytecodes))
    for key, val in bytecodes:
83
84
        if val == bytecode:
            return key.upper()
Stefan Marr's avatar
Stefan Marr committed
85
        
86
    raise ValueError('No defined defined for the value %d.' % bytecode)
87
88
89
90
91
92
93

def int_constants_of(cls):
    out = {}
    for key, value in cls.__dict__.items():
        if isinstance(value, int):
            out[key] = value
    return out