-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathpython_evaluator.h
More file actions
145 lines (119 loc) · 4.18 KB
/
python_evaluator.h
File metadata and controls
145 lines (119 loc) · 4.18 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
#ifndef LFORTRAN_PYTHON_EVALUATOR_H
#define LFORTRAN_PYTHON_EVALUATOR_H
#include <iostream>
#include <memory>
#include <libasr/alloc.h>
#include <libasr/asr_scopes.h>
#include <libasr/asr.h>
#include <lpython/python_ast.h>
#include <lpython/utils.h>
#include <libasr/config.h>
#include <libasr/diagnostics.h>
#include <libasr/pass/pass_manager.h>
namespace llvm {
class Type;
}
namespace LCompilers {
class LLVMModule;
class LLVMEvaluator;
/*
PythonCompiler is the main class to access the Python compiler.
This class is used for both interactive (.evaluate()) and non-interactive
(.get_llvm2()) compilation. The methods return diagnostic messages (errors,
warnings, style suggestions, ...) as an argument. One can use
Diagnostic::render to render them.
One can use get_asr2() to obtain the ASR and then hand it over to other
backends by hand.
*/
class PythonCompiler
{
public:
CompilerOptions compiler_options;
PythonCompiler(CompilerOptions compiler_options);
~PythonCompiler();
struct EvalResult {
enum {
integer1,
integer2,
unsignedInteger1,
unsignedInteger2,
integer4,
integer8,
unsignedInteger4,
unsignedInteger8,
real4,
real8,
complex4,
complex8,
boolean,
string,
struct_type,
statement,
none
} type;
union {
int32_t i32;
int64_t i64;
uint32_t u32;
uint64_t u64;
bool b;
float f32;
double f64;
char *str;
struct {float re, im;} c32;
struct {double re, im;} c64;
struct {
void *structure;
ASR::ttype_t *ttype;
size_t *offsets;
size_t element_size;
} structure;
};
std::string ast;
std::string asr;
std::string llvm_ir;
};
Result<PythonCompiler::EvalResult> evaluate(
const std::string &code_orig, bool verbose, LocationManager &lm,
LCompilers::PassManager& pass_manager, diag::Diagnostics &diagnostics);
Result<PythonCompiler::EvalResult> evaluate2(const std::string &code);
Result<std::string> get_ast(const std::string &code,
LocationManager &lm, diag::Diagnostics &diagnostics);
Result<LCompilers::LPython::AST::ast_t*> get_ast2(
const std::string &code_orig, diag::Diagnostics &diagnostics);
Result<std::string> get_asr(const std::string &code,
LocationManager &lm, diag::Diagnostics &diagnostics);
Result<ASR::TranslationUnit_t*> get_asr2(
const std::string &code_orig, LocationManager &lm,
diag::Diagnostics &diagnostics);
Result<ASR::TranslationUnit_t*> get_asr3(
LCompilers::LPython::AST::ast_t &ast, diag::Diagnostics &diagnostics,
LocationManager &lm, bool is_interactive=false);
Result<std::string> get_llvm(
const std::string &code, LocationManager &lm, LCompilers::PassManager& pass_manager,
diag::Diagnostics &diagnostics);
Result<std::unique_ptr<LLVMModule>> get_llvm2(
const std::string &code, LocationManager &lm, LCompilers::PassManager& pass_manager,
diag::Diagnostics &diagnostics);
Result<std::unique_ptr<LLVMModule>> get_llvm3(ASR::TranslationUnit_t &asr,
LCompilers::PassManager& lpm, diag::Diagnostics &diagnostics, LCompilers::LocationManager& lm,
const std::string &infile);
Result<std::string> get_asm(const std::string &code,
LocationManager &lm,
LCompilers::PassManager& pass_manager,
diag::Diagnostics &diagnostics);
std::string aggregate_type_to_string(const struct EvalResult &r);
private:
void compute_offsets(llvm::Type *type, ASR::symbol_t *asr_type, EvalResult &result);
private:
Allocator al;
#ifdef HAVE_LFORTRAN_LLVM
std::unique_ptr<LLVMEvaluator> e;
#endif
int eval_count;
SymbolTable *symbol_table;
std::string run_fn;
std::string global_underscore_name;
};
} // namespace LCompilers
#endif // LFORTRAN_PYTHON_EVALUATOR_H