forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_comptime_eval.h
More file actions
416 lines (385 loc) · 19.9 KB
/
Copy pathpython_comptime_eval.h
File metadata and controls
416 lines (385 loc) · 19.9 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#ifndef LPYTHON_SEMANTICS_COMPTIME_EVAL_H
#define LPYTHON_SEMANTICS_COMPTIME_EVAL_H
#include <complex>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <libasr/asr.h>
#include <lpython/ast.h>
#include <lpython/bigint.h>
#include <libasr/string_utils.h>
#include <lpython/utils.h>
#include <lpython/semantics/semantic_exception.h>
namespace LFortran {
struct PythonIntrinsicProcedures {
const std::string m_builtin = "lpython_builtin";
typedef ASR::expr_t* (*comptime_eval_callback)(Allocator &, const Location &, Vec<ASR::expr_t*> &);
// Table of intrinsics
// The callback is only called if all arguments have compile time `value`
// which is always one of the `Constant*` expression ASR nodes, so inside
// the callback one can assume that.
std::map<std::string, std::tuple<std::string, comptime_eval_callback>> comptime_eval_map;
PythonIntrinsicProcedures() {
comptime_eval_map = {
{"abs", {m_builtin, &eval_abs}},
{"str", {m_builtin, &eval_str}},
{"bool", {m_builtin, &eval_bool}},
{"chr", {m_builtin, &eval_chr}},
{"ord", {m_builtin, &eval_ord}},
{"len", {m_builtin, &eval_len}},
{"pow", {m_builtin, &eval_pow}},
{"int", {m_builtin, &eval_int}},
{"float", {m_builtin, &eval_float}},
{"bin", {m_builtin, &eval_bin}},
{"hex", {m_builtin, &eval_hex}},
{"oct", {m_builtin, &eval_oct}},
};
}
// Return `true` if `name` is in the table of intrinsics
bool is_intrinsic(std::string name) const {
auto search = comptime_eval_map.find(name);
if (search != comptime_eval_map.end()) {
return true;
} else {
return false;
}
}
// Looks up `name` in the table of intrinsics and returns the corresponding
// module name; Otherwise rises an exception
std::string get_module(std::string name, const Location &loc) const {
auto search = comptime_eval_map.find(name);
if (search != comptime_eval_map.end()) {
std::string module_name = std::get<0>(search->second);
return module_name;
} else {
throw SemanticError("Function '" + name
+ "' not found among intrinsic procedures",
loc);
}
}
// Evaluates the intrinsic function `name` at compile time
ASR::expr_t *comptime_eval(std::string name, Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) const {
auto search = comptime_eval_map.find(name);
if (search != comptime_eval_map.end()) {
comptime_eval_callback cb = std::get<1>(search->second);
Vec<ASR::expr_t*> arg_values = ASRUtils::get_arg_values(al, args);
if (arg_values.size() != args.size()) {
// Not all arguments have compile time values; we do not call the callback
return nullptr;
}
return cb(al, loc, arg_values);
} else {
throw SemanticError("Intrinsic function '" + name
+ "' compile time evaluation is not implemented yet",
loc);
}
}
static ASR::expr_t *eval_abs(Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args
) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
if (args.size() != 1) {
throw SemanticError("Intrinsic abs function accepts exactly 1 argument", loc);
}
ASR::expr_t* trig_arg = args[0];
ASR::ttype_t* t = ASRUtils::expr_type(args[0]);
if (ASR::is_a<ASR::Real_t>(*t)) {
double rv = ASR::down_cast<ASR::ConstantReal_t>(trig_arg)->m_r;
double val = std::abs(rv);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantReal_t(al, loc, val, t));
} else if (ASR::is_a<ASR::Integer_t>(*t)) {
int64_t rv = ASR::down_cast<ASR::ConstantInteger_t>(trig_arg)->m_n;
int64_t val = std::abs(rv);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(al, loc, val, t));
} else if (ASR::is_a<ASR::Complex_t>(*t)) {
double re = ASR::down_cast<ASR::ConstantComplex_t>(trig_arg)->m_re;
double im = ASR::down_cast<ASR::ConstantComplex_t>(trig_arg)->m_im;
std::complex<double> x(re, im);
double result = std::abs(x);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantReal_t(al, loc, result, t));
} else {
throw SemanticError("Argument of the abs function must be Integer, Real or Complex", loc);
}
}
static ASR::expr_t *eval_str(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
ASR::ttype_t* str_type = ASRUtils::TYPE(ASR::make_Character_t(al,
loc, 1, 1, nullptr, nullptr, 0));
if (args.size() == 0) { // create an empty string
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(al, loc, s2c(al, ""), str_type));
}
ASR::expr_t* arg = ASRUtils::expr_value(args[0]);
ASR::ttype_t* arg_type = ASRUtils::expr_type(arg);
if (ASRUtils::is_integer(*arg_type)) {
int64_t ival = ASR::down_cast<ASR::ConstantInteger_t>(arg)->m_n;
std::string s = std::to_string(ival);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(al, loc, s2c(al, s), str_type));
} else if (ASRUtils::is_real(*arg_type)) {
double rval = ASR::down_cast<ASR::ConstantReal_t>(arg)->m_r;
std::string s = std::to_string(rval);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(al, loc, s2c(al, s), str_type));
} else if (ASRUtils::is_logical(*arg_type)) {
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(arg)->m_value;
std::string s = rv ? "True" : "False";
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(al, loc, s2c(al, s), str_type));
} else if (ASRUtils::is_character(*arg_type)) {
char* c = ASR::down_cast<ASR::ConstantString_t>(arg)->m_s;
std::string s = std::string(c);
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(al, loc, s2c(al, s), str_type));
} else {
throw SemanticError("str() argument must be real, integer, logical, or a string, not '" +
ASRUtils::type_to_str(arg_type) + "'", loc);
}
}
static ASR::expr_t *eval_bool(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
if (args.size() != 1) {
throw SemanticError("bool() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Logical_t(al, loc,
1, nullptr, 0));
ASR::expr_t* arg = ASRUtils::expr_value(args[0]);
ASR::ttype_t* t = ASRUtils::expr_type(arg);
bool result;
if (ASRUtils::is_real(*t)) {
double rv = ASR::down_cast<ASR::ConstantReal_t>(arg)->m_r;
result = rv ? true : false;
} else if (ASRUtils::is_integer(*t)) {
int64_t rv = ASR::down_cast<ASR::ConstantInteger_t>(arg)->m_n;
result = rv ? true : false;
} else if (ASRUtils::is_complex(*t)) {
double re = ASR::down_cast<ASR::ConstantComplex_t>(arg)->m_re;
double im = ASR::down_cast<ASR::ConstantComplex_t>(arg)->m_im;
std::complex<double> c(re, im);
result = (re || im) ? true : false;
} else if (ASRUtils::is_logical(*t)) {
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(arg)->m_value;
result = rv;
} else if (ASRUtils::is_character(*t)) {
char* c = ASR::down_cast<ASR::ConstantString_t>(ASRUtils::expr_value(arg))->m_s;
result = strlen(s2c(al, std::string(c))) ? true : false;
} else {
throw SemanticError("bool() must have one real, integer, character,"
" complex, or logical argument, not '" + ASRUtils::type_to_str(t) + "'", loc);
}
return ASR::down_cast<ASR::expr_t>(make_ConstantLogical_t(al, loc, result, type));
}
static ASR::expr_t *eval_chr(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
ASR::expr_t* arg = args[0];
ASR::ttype_t* type = ASRUtils::expr_type(arg);
if (ASR::is_a<ASR::Integer_t>(*type)) {
int64_t c = ASR::down_cast<ASR::ConstantInteger_t>(arg)->m_n;
ASR::ttype_t* str_type =
ASRUtils::TYPE(ASR::make_Character_t(al,
loc, 1, 1, nullptr, nullptr, 0));
if (! (c >= 0 && c <= 127) ) {
throw SemanticError("The argument 'x' in chr(x) must be in the range 0 <= x <= 127.", loc);
}
char cc = c;
std::string svalue;
svalue += cc;
return ASR::down_cast<ASR::expr_t>(
ASR::make_ConstantString_t(al, loc, s2c(al, svalue), str_type));
} else {
throw SemanticError("chr() must have one integer argument.", loc);
}
}
static ASR::expr_t *eval_ord(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
ASR::expr_t* char_expr = args[0];
ASR::ttype_t* char_type = ASRUtils::expr_type(char_expr);
if (ASRUtils::is_character(*char_type)) {
char* c = ASR::down_cast<ASR::ConstantString_t>(ASRUtils::expr_value(char_expr))->m_s;
ASR::ttype_t* int_type =
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
return ASR::down_cast<ASR::expr_t>(
ASR::make_ConstantInteger_t(al, loc, c[0], int_type));
} else {
throw SemanticError("ord() must have one character argument", loc);
}
}
static ASR::expr_t *eval_len(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
if (args.size() != 1) {
throw SemanticError("len() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::expr_t *arg = ASRUtils::expr_value(args[0]);
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
if (arg->type == ASR::exprType::ConstantString) {
char* str_value = ASR::down_cast<ASR::ConstantString_t>(arg)->m_s;
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc,
(int64_t)strlen(s2c(al, std::string(str_value))), type));
} else if (arg->type == ASR::exprType::ConstantArray) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(al, loc,
(int64_t)ASR::down_cast<ASR::ConstantArray_t>(arg)->n_args, type));
} else if (arg->type == ASR::exprType::ConstantTuple) {
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc,
(int64_t)ASR::down_cast<ASR::ConstantTuple_t>(arg)->n_elements, type));
} else if (arg->type == ASR::exprType::ConstantDictionary) {
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc,
(int64_t)ASR::down_cast<ASR::ConstantDictionary_t>(arg)->n_keys, type));
} else if (arg->type == ASR::exprType::ConstantSet) {
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc,
(int64_t)ASR::down_cast<ASR::ConstantSet_t>(arg)->n_elements, type));
} else {
throw SemanticError("len() only works on strings, lists, tuples, dictionaries and sets",
loc);
}
}
static ASR::expr_t *eval_pow(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args));
ASR::expr_t* arg1 = ASRUtils::expr_value(args[0]);
ASR::expr_t* arg2 = ASRUtils::expr_value(args[1]);
ASR::ttype_t* arg1_type = ASRUtils::expr_type(arg1);
ASR::ttype_t* arg2_type = ASRUtils::expr_type(arg2);
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
ASR::ttype_t *real_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc, 8, nullptr, 0));
if (!ASRUtils::check_equal_type(arg1_type, arg2_type)) {
throw SemanticError("The arguments to pow() must have the same type.", loc);
}
if (ASRUtils::is_integer(*arg1_type) && ASRUtils::is_integer(*arg2_type)) {
int64_t a = ASR::down_cast<ASR::ConstantInteger_t>(arg1)->m_n;
int64_t b = ASR::down_cast<ASR::ConstantInteger_t>(arg2)->m_n;
if (a == 0 && b < 0) { // Zero Division
throw SemanticError("0.0 cannot be raised to a negative power.", loc);
}
if (b < 0) // Negative power
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc,
pow(a, b), real_type));
else // Positive power
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc,
(int64_t)pow(a, b), int_type));
} else {
throw SemanticError("The arguments to pow() must be of type integers for now.", loc);
}
}
static ASR::expr_t *eval_int(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
if (args.size() == 0) {
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, 0, type));
}
ASR::expr_t* int_expr = ASRUtils::expr_value(args[0]);
ASR::ttype_t* int_type = ASRUtils::expr_type(int_expr);
if (ASRUtils::is_integer(*int_type)) {
int64_t ival = ASR::down_cast<ASR::ConstantInteger_t>(int_expr)->m_n;
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, ival, type));
} else if (ASRUtils::is_character(*int_type)) {
// convert a string to an int
char* c = ASR::down_cast<ASR::ConstantString_t>(int_expr)->m_s;
std::string str = std::string(c);
int64_t ival = std::stoll(str);
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, ival, type));
} else if (ASRUtils::is_real(*int_type)) {
int64_t ival = ASR::down_cast<ASR::ConstantReal_t>(int_expr)->m_r;
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, ival, type));
} else if (ASRUtils::is_logical(*int_type)) {
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(int_expr)->m_value;
int8_t val = rv ? 1 : 0;
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, val, type));
} else {
throw SemanticError("int() argument must be real, integer, logical, or a string, not '" +
ASRUtils::type_to_str(int_type) + "'", loc);
}
}
static ASR::expr_t *eval_float(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Real_t(al, loc, 8, nullptr, 0));
if (args.size() == 0) {
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc, 0.0, type));
}
ASR::expr_t* expr = ASRUtils::expr_value(args[0]);
ASR::ttype_t* float_type = ASRUtils::expr_type(expr);
if (ASRUtils::is_real(*float_type)) {
float rv = ASR::down_cast<ASR::ConstantReal_t>(expr)->m_r;
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc, rv, type));
} else if (ASRUtils::is_integer(*float_type)) {
double rv = ASR::down_cast<ASR::ConstantInteger_t>(expr)->m_n;
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc, rv, type));
} else if (ASRUtils::is_logical(*float_type)) {
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(expr)->m_value;
float val = rv ? 1.0 : 0.0;
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc, val, type));
} else if (ASRUtils::is_character(*float_type)) {
// convert a string to a float
char* c = ASR::down_cast<ASR::ConstantString_t>(expr)->m_s;
std::string str = std::string(c);
float rv = std::stof(str);
return ASR::down_cast<ASR::expr_t>(make_ConstantReal_t(al, loc, rv, type));
} else {
throw SemanticError("float() argument must be real, integer, logical, or a string, not '" +
ASRUtils::type_to_str(float_type) + "'", loc);
}
}
static ASR::expr_t *eval_bin(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
if (args.size() != 1) {
throw SemanticError("bin() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::expr_t* expr = ASRUtils::expr_value(args[0]);
ASR::ttype_t* type = ASRUtils::expr_type(expr);
if (ASRUtils::is_integer(*type)) {
int64_t n = ASR::down_cast<ASR::ConstantInteger_t>(expr)->m_n;
ASR::ttype_t* str_type = ASRUtils::TYPE(ASR::make_Character_t(al,
loc, 1, 1, nullptr, nullptr, 0));
std::string str, prefix;
prefix = n > 0 ? "0b" : "-0b";
str += std::bitset<64>(std::abs(n)).to_string();
str.erase(0, str.find_first_not_of('0'));
str.insert(0, prefix);
return ASR::down_cast<ASR::expr_t>(make_ConstantString_t(al, loc, s2c(al, str), str_type));
} else {
throw SemanticError("bin() argument must be an integer, not '" +
ASRUtils::type_to_str(type) + "'", loc);
}
}
static ASR::expr_t *eval_hex(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
if (args.size() != 1) {
throw SemanticError("hex() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::expr_t* expr = ASRUtils::expr_value(args[0]);
ASR::ttype_t* type = ASRUtils::expr_type(expr);
if (ASRUtils::is_integer(*type)) {
int64_t n = ASR::down_cast<ASR::ConstantInteger_t>(expr)->m_n;
ASR::ttype_t* str_type = ASRUtils::TYPE(ASR::make_Character_t(al,
loc, 1, 1, nullptr, nullptr, 0));
std::string str, prefix;
std::stringstream ss;
prefix = n > 0 ? "0x" : "-0x";
ss << std::hex << std::abs(n);
str += ss.str();
str.insert(0, prefix);
return ASR::down_cast<ASR::expr_t>(make_ConstantString_t(al, loc, s2c(al, str), str_type));
} else {
throw SemanticError("hex() argument must be an integer, not '" +
ASRUtils::type_to_str(type) + "'", loc);
}
}
static ASR::expr_t *eval_oct(Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args) {
if (args.size() != 1) {
throw SemanticError("oct() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::expr_t* expr = ASRUtils::expr_value(args[0]);
ASR::ttype_t* type = ASRUtils::expr_type(expr);
if (ASRUtils::is_integer(*type)) {
int64_t n = ASR::down_cast<ASR::ConstantInteger_t>(expr)->m_n;
ASR::ttype_t* str_type = ASRUtils::TYPE(ASR::make_Character_t(al,
loc, 1, 1, nullptr, nullptr, 0));
std::string str, prefix;
std::stringstream ss;
prefix = n > 0 ? "0o" : "-0o";
ss << std::oct << std::abs(n);
str += ss.str();
str.insert(0, prefix);
return ASR::down_cast<ASR::expr_t>(make_ConstantString_t(al, loc, s2c(al, str), str_type));
} else {
throw SemanticError("oct() argument must be an integer, not '" +
ASRUtils::type_to_str(type) + "'", loc);
}
}
}; // ComptimeEval
} // namespace LFortran
#endif /* LPYTHON_SEMANTICS_COMPTIME_EVAL_H */