forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic_wrapper.py
More file actions
310 lines (268 loc) · 11 KB
/
Copy patharithmetic_wrapper.py
File metadata and controls
310 lines (268 loc) · 11 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright 2018 Braxton Mckee
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typed_python.python_ast as python_ast
import nativepython.type_wrappers.runtime_functions as runtime_functions
from nativepython.type_wrappers.wrapper import Wrapper
from nativepython.type_wrappers.exceptions import generateThrowException
import nativepython.native_ast as native_ast
import nativepython
from typed_python import *
pyOpToNative = {
python_ast.BinaryOp.Add(): native_ast.BinaryOp.Add(),
python_ast.BinaryOp.Sub(): native_ast.BinaryOp.Sub(),
python_ast.BinaryOp.Mult(): native_ast.BinaryOp.Mul(),
python_ast.BinaryOp.Div(): native_ast.BinaryOp.Div(),
python_ast.BinaryOp.Mod(): native_ast.BinaryOp.Mod(),
python_ast.BinaryOp.LShift(): native_ast.BinaryOp.LShift(),
python_ast.BinaryOp.RShift(): native_ast.BinaryOp.RShift(),
python_ast.BinaryOp.BitOr(): native_ast.BinaryOp.BitOr(),
python_ast.BinaryOp.BitXor(): native_ast.BinaryOp.BitXor(),
python_ast.BinaryOp.BitAnd(): native_ast.BinaryOp.BitAnd()
}
pyOpNotForFloat = {
python_ast.BinaryOp.LShift(),
python_ast.BinaryOp.RShift(),
python_ast.BinaryOp.BitOr(),
python_ast.BinaryOp.BitXor(),
python_ast.BinaryOp.BitAnd()
}
pyCompOp = {
python_ast.ComparisonOp.Eq(): native_ast.BinaryOp.Eq(),
python_ast.ComparisonOp.NotEq(): native_ast.BinaryOp.NotEq(),
python_ast.ComparisonOp.Lt(): native_ast.BinaryOp.Lt(),
python_ast.ComparisonOp.LtE(): native_ast.BinaryOp.LtE(),
python_ast.ComparisonOp.Gt(): native_ast.BinaryOp.Gt(),
python_ast.ComparisonOp.GtE(): native_ast.BinaryOp.GtE()
}
class ArithmeticTypeWrapper(Wrapper):
is_pod = True
is_pass_by_ref = False
def convert_default_initialize(self, context, target):
self.convert_copy_initialize(
context,
target,
nativepython.python_object_representation.pythonObjectRepresentation(context, self.typeRepresentation())
)
def convert_assign(self, context, target, toStore):
assert target.isReference
context.pushEffect(
target.expr.store(toStore.nonref_expr)
)
def convert_copy_initialize(self, context, target, toStore):
assert target.isReference
context.pushEffect(
target.expr.store(toStore.nonref_expr)
)
def convert_destroy(self, context, instance):
pass
def convert_unary_op(self, context, instance, op):
if op.matches.USub:
return context.pushPod(self, instance.nonref_expr.negate())
return super().convert_unary_op(context, instance, op)
class Int64Wrapper(ArithmeticTypeWrapper):
def __init__(self):
super().__init__(Int64)
def getNativeLayoutType(self):
return native_ast.Type.Int(bits=64, signed=True)
def toFloat64(self, context, e):
return context.pushPod(
float,
native_ast.Expression.Cast(
left=e.nonref_expr,
to_type=native_ast.Type.Float(bits=64)
)
)
def convert_to_type(self, context, e, target_type):
if target_type.typeRepresentation == Float64:
return context.pushPod(
float,
native_ast.Expression.Cast(
left=e.nonref_expr,
to_type=native_ast.Type.Float(bits=64)
)
)
elif target_type.typeRepresentation == Int64:
return e
elif target_type.typeRepresentation == Bool:
return e != 0
return super().convert_to_type(context, e, target_type)
def convert_bin_op(self, context, left, op, right):
if op.matches.Div:
if right.expr_type == self:
return left.toFloat64().convert_bin_op(op, right)
if right.expr_type == left.expr_type:
if op.matches.Mod:
return context.pushPod(
self,
native_ast.Expression.Branch(
cond=right.nonref_expr,
true=runtime_functions.mod_int64_int64.call(left.nonref_expr, right.nonref_expr),
false=generateThrowException(context, ZeroDivisionError())
)
)
if op.matches.Pow:
return context.pushPod(
self,
runtime_functions.pow_int64_int64.call(left.nonref_expr, right.nonref_expr)
)
if op.matches.LShift or op.matches.RShift:
return context.pushPod(
self,
native_ast.Expression.Branch(
cond=(right >= 0).nonref_expr,
true=native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyOpToNative[op]
),
false=generateThrowException(context, ValueError("negative shift count"))
)
)
if op.matches.FloorDiv:
res = left.toFloat64()
if res is None:
return None
res = res.convert_bin_op(python_ast.BinaryOp.Div(), right)
if res is None:
return None
return res.toInt64()
if op in pyOpToNative:
return context.pushPod(
self,
native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyOpToNative[op]
)
)
if op in pyCompOp:
return context.pushPod(
bool,
native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyCompOp[op]
)
)
if isinstance(right.expr_type, Float64Wrapper):
return left.toFloat64().convert_bin_op(op, right)
return super().convert_bin_op(context, left, op, right)
class BoolWrapper(ArithmeticTypeWrapper):
def __init__(self):
super().__init__(Bool)
def getNativeLayoutType(self):
return native_ast.Type.Int(bits=1, signed=False)
def convert_to_type(self, context, e, target_type):
if target_type.typeRepresentation == Float64:
return context.pushPod(
float,
native_ast.Expression.Cast(
left=e.nonref_expr,
to_type=native_ast.Type.Float(bits=64)
)
)
elif target_type.typeRepresentation == Int64:
return context.pushPod(
int,
native_ast.Expression.Cast(
left=e.nonref_expr,
to_type=native_ast.Type.Int(bits=64, signed=True)
)
)
elif target_type.typeRepresentation == Bool:
return e
return super().convert_to_type(context, e, target_type)
def convert_bin_op(self, context, left, op, right):
if right.expr_type == left.expr_type:
if op.matches.BitOr or op.matches.BitAnd or op.matches.BitXor:
return context.pushPod(
self,
native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyOpToNative[op]
)
)
return super().convert_bin_op(context, left, op, right)
class Float64Wrapper(ArithmeticTypeWrapper):
def __init__(self):
super().__init__(Float64)
def getNativeLayoutType(self):
return native_ast.Type.Float(bits=64)
def convert_to_type(self, context, e, target_type):
if target_type.typeRepresentation == Float64:
return e
elif target_type.typeRepresentation == Int64:
return context.pushPod(
int,
native_ast.Expression.Cast(
left=e.nonref_expr,
to_type=native_ast.Type.Int(bits=64, signed=True)
)
)
elif target_type.typeRepresentation == Bool:
return e != 0.0
return super().convert_to_type(context, e, target_type)
def convert_bin_op(self, context, left, op, right):
if isinstance(right.expr_type, Int64Wrapper):
right = right.toFloat64()
if right.expr_type == left.expr_type:
if op.matches.Mod:
return context.pushPod(
self,
native_ast.Expression.Branch(
cond=right.nonref_expr,
true=runtime_functions.mod_float64_float64.call(left.nonref_expr, right.nonref_expr),
false=generateThrowException(context, ZeroDivisionError())
)
)
if op.matches.Div:
return context.pushPod(
self,
native_ast.Expression.Branch(
cond=right.nonref_expr,
true=native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyOpToNative[op]
),
false=generateThrowException(context, ZeroDivisionError())
)
)
if op.matches.Pow:
return context.pushPod(
self,
runtime_functions.pow_float64_float64.call(
left.nonref_expr, right.nonref_expr
)
)
if op in pyOpToNative and op not in pyOpNotForFloat:
return context.pushPod(
self,
native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyOpToNative[op]
)
)
if op in pyCompOp:
return context.pushPod(
bool,
native_ast.Expression.Binop(
left=left.nonref_expr,
right=right.nonref_expr,
op=pyCompOp[op]
)
)
return super().convert_bin_op(context, left, op, right)