This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcodes.py
131 lines (98 loc) · 2.46 KB
/
opcodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2016, 2017 University of Vienna
# All rights reserved.
# BSD license.
# Author: Ali Baharev <ali.baharev@gmail.com>
from __future__ import print_function
from itertools import repeat
from six import itervalues
from py3compat import izip
from utils import as_pairs, duplicates
# ARITY: {opcode: arity}
# NAME: {opcode: name}
# OPERATORS: all the possible operators in human readable format
__all__ = ('ARITY', 'NAME', 'OPERATORS')
#-------------------------------------------------------------------------------
__unary = (
'o13', 'floor',
'o14', 'ceil',
'o15', 'abs',
'o16', 'neg',
'o34', 'not',
'o37', 'tanh',
'o38', 'tan',
'o39', 'sqrt',
'o40', 'sinh',
'o41', 'sin',
'o42', 'log10',
'o43', 'log',
'o44', 'exp',
'o45', 'cosh',
'o46', 'cos',
'o47', 'atanh',
'o49', 'atan',
'o50', 'asinh',
'o51', 'asin',
'o52', 'acosh',
'o53', 'acos',
)
__binary = (
'o0', 'plus',
'o1', 'minus',
'o2', 'mult',
'o3', 'div',
'o4', 'rem',
'o5', 'pow',
'o6', 'less',
'o20', 'or',
'o21', 'and',
'o22', 'lt',
'o23', 'le',
'o24', 'eq',
'o28', 'ge',
'o29', 'gt',
'o30', 'ne',
'o48', 'atan2',
'o55', 'intdiv',
'o56', 'precision',
'o57', 'round',
'o58', 'trunc',
'o73', 'iff',
)
__n_ary = (
'o11', 'min',
'o12', 'max',
'o54', 'sum',
'o59', 'count',
'o60', 'numberof',
'o61', 'numberofs',
'o70', 'n_ary_and',
'o71', 'n_ary_or',
'o74', 'alldiff',
'o35', 'if',
'o65', 'ifs',
'o72', 'implies',
)
ARITY = {}
def __add_arities(operators, arity):
opcodes = operators[::2]
dups = duplicates(opcodes)
assert not dups, dups
already_added = set(opcodes) & set(ARITY)
assert not already_added, already_added
ARITY.update(izip(opcodes, repeat(arity)))
__add_arities(__unary, 1)
__add_arities(__binary, 2)
__add_arities(__n_ary, None)
NAME = {}
def __add_names(operators):
seen_names = set(itervalues(NAME))
for opcode, pretty_name in as_pairs(operators):
assert opcode not in NAME, (opcode, pretty_name)
assert pretty_name not in seen_names, (opcode, pretty_name)
seen_names.add(pretty_name)
NAME[opcode] = pretty_name
__add_names(__unary)
__add_names(__binary)
__add_names(__n_ary)
assert set(NAME) == set(ARITY)
OPERATORS = set(itervalues(NAME))