|
@@ -53,92 +53,17 @@ struct CodeEmitContext{
|
|
|
bool is_compiling_class = false;
|
|
bool is_compiling_class = false;
|
|
|
int for_loop_depth = 0;
|
|
int for_loop_depth = 0;
|
|
|
|
|
|
|
|
- bool is_curr_block_loop() const {
|
|
|
|
|
- return co->blocks[curr_block_i].type == FOR_LOOP || co->blocks[curr_block_i].type == WHILE_LOOP;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void enter_block(CodeBlockType type){
|
|
|
|
|
- if(type == FOR_LOOP) for_loop_depth++;
|
|
|
|
|
- co->blocks.push_back(CodeBlock(
|
|
|
|
|
- type, curr_block_i, for_loop_depth, (int)co->codes.size()
|
|
|
|
|
- ));
|
|
|
|
|
- curr_block_i = co->blocks.size()-1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void exit_block(){
|
|
|
|
|
- auto curr_type = co->blocks[curr_block_i].type;
|
|
|
|
|
- if(curr_type == FOR_LOOP) for_loop_depth--;
|
|
|
|
|
- co->blocks[curr_block_i].end = co->codes.size();
|
|
|
|
|
- curr_block_i = co->blocks[curr_block_i].parent;
|
|
|
|
|
- if(curr_block_i < 0) FATAL_ERROR();
|
|
|
|
|
-
|
|
|
|
|
- if(curr_type == FOR_LOOP){
|
|
|
|
|
- // add a no op here to make block check work
|
|
|
|
|
- emit(OP_NO_OP, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // clear the expression stack and generate bytecode
|
|
|
|
|
- void emit_expr(){
|
|
|
|
|
- if(s_expr.size() != 1){
|
|
|
|
|
- throw std::runtime_error("s_expr.size() != 1\n" + _log_s_expr());
|
|
|
|
|
- }
|
|
|
|
|
- Expr_ expr = s_expr.popx();
|
|
|
|
|
- expr->emit(this);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- std::string _log_s_expr(){
|
|
|
|
|
- std::stringstream ss;
|
|
|
|
|
- for(auto& e: s_expr.data()) ss << e->str() << " ";
|
|
|
|
|
- return ss.str();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int emit(Opcode opcode, int arg, int line) {
|
|
|
|
|
- co->codes.push_back(
|
|
|
|
|
- Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg}
|
|
|
|
|
- );
|
|
|
|
|
- co->lines.push_back(line);
|
|
|
|
|
- int i = co->codes.size() - 1;
|
|
|
|
|
- if(line==BC_KEEPLINE){
|
|
|
|
|
- if(i>=1) co->lines[i] = co->lines[i-1];
|
|
|
|
|
- else co->lines[i] = 1;
|
|
|
|
|
- }
|
|
|
|
|
- return i;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void patch_jump(int index) {
|
|
|
|
|
- int target = co->codes.size();
|
|
|
|
|
- co->codes[index].arg = target;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool add_label(StrName name){
|
|
|
|
|
- if(co->labels.contains(name)) return false;
|
|
|
|
|
- co->labels.set(name, co->codes.size());
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int add_varname(StrName name){
|
|
|
|
|
- int index = co->varnames_inv.try_get(name);
|
|
|
|
|
- if(index >= 0) return index;
|
|
|
|
|
- co->varnames.push_back(name);
|
|
|
|
|
- index = co->varnames.size() - 1;
|
|
|
|
|
- co->varnames_inv.set(name, index);
|
|
|
|
|
- return index;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int add_const(PyObject* v){
|
|
|
|
|
- // simple deduplication, only works for int/float
|
|
|
|
|
- for(int i=0; i<co->consts.size(); i++){
|
|
|
|
|
- if(co->consts[i] == v) return i;
|
|
|
|
|
- }
|
|
|
|
|
- co->consts.push_back(v);
|
|
|
|
|
- return co->consts.size() - 1;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- int add_func_decl(FuncDecl_ decl){
|
|
|
|
|
- co->func_decls.push_back(decl);
|
|
|
|
|
- return co->func_decls.size() - 1;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ bool is_curr_block_loop() const;
|
|
|
|
|
+ void enter_block(CodeBlockType type);
|
|
|
|
|
+ void exit_block();
|
|
|
|
|
+ void emit_expr(); // clear the expression stack and generate bytecode
|
|
|
|
|
+ std::string _log_s_expr();
|
|
|
|
|
+ int emit(Opcode opcode, int arg, int line);
|
|
|
|
|
+ void patch_jump(int index);
|
|
|
|
|
+ bool add_label(StrName name);
|
|
|
|
|
+ int add_varname(StrName name);
|
|
|
|
|
+ int add_const(PyObject* v);
|
|
|
|
|
+ int add_func_decl(FuncDecl_ decl);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct NameExpr: Expr{
|
|
struct NameExpr: Expr{
|
|
@@ -148,54 +73,9 @@ struct NameExpr: Expr{
|
|
|
|
|
|
|
|
std::string str() const override { return fmt("Name(", name.escape(), ")"); }
|
|
std::string str() const override { return fmt("Name(", name.escape(), ")"); }
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- int index = ctx->co->varnames_inv.try_get(name);
|
|
|
|
|
- if(scope == NAME_LOCAL && index >= 0){
|
|
|
|
|
- ctx->emit(OP_LOAD_FAST, index, line);
|
|
|
|
|
- }else{
|
|
|
|
|
- Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
|
|
|
|
|
- // we cannot determine the scope when calling exec()/eval()
|
|
|
|
|
- if(scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
|
|
|
|
|
- ctx->emit(op, StrName(name).index, line);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_del(CodeEmitContext* ctx) override {
|
|
|
|
|
- switch(scope){
|
|
|
|
|
- case NAME_LOCAL:
|
|
|
|
|
- ctx->emit(OP_DELETE_FAST, ctx->add_varname(name), line);
|
|
|
|
|
- break;
|
|
|
|
|
- case NAME_GLOBAL:
|
|
|
|
|
- ctx->emit(OP_DELETE_GLOBAL, StrName(name).index, line);
|
|
|
|
|
- break;
|
|
|
|
|
- case NAME_GLOBAL_UNKNOWN:
|
|
|
|
|
- ctx->emit(OP_DELETE_NAME, StrName(name).index, line);
|
|
|
|
|
- break;
|
|
|
|
|
- default: FATAL_ERROR(); break;
|
|
|
|
|
- }
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_store(CodeEmitContext* ctx) override {
|
|
|
|
|
- if(ctx->is_compiling_class){
|
|
|
|
|
- int index = StrName(name).index;
|
|
|
|
|
- ctx->emit(OP_STORE_CLASS_ATTR, index, line);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
- switch(scope){
|
|
|
|
|
- case NAME_LOCAL:
|
|
|
|
|
- ctx->emit(OP_STORE_FAST, ctx->add_varname(name), line);
|
|
|
|
|
- break;
|
|
|
|
|
- case NAME_GLOBAL:
|
|
|
|
|
- ctx->emit(OP_STORE_GLOBAL, StrName(name).index, line);
|
|
|
|
|
- break;
|
|
|
|
|
- case NAME_GLOBAL_UNKNOWN:
|
|
|
|
|
- ctx->emit(OP_STORE_NAME, StrName(name).index, line);
|
|
|
|
|
- break;
|
|
|
|
|
- default: FATAL_ERROR(); break;
|
|
|
|
|
- }
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_del(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_store(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct StarredExpr: Expr{
|
|
struct StarredExpr: Expr{
|
|
@@ -203,19 +83,9 @@ struct StarredExpr: Expr{
|
|
|
Expr_ child;
|
|
Expr_ child;
|
|
|
StarredExpr(int level, Expr_&& child): level(level), child(std::move(child)) {}
|
|
StarredExpr(int level, Expr_&& child): level(level), child(std::move(child)) {}
|
|
|
std::string str() const override { return fmt("Starred(level=", level, ")"); }
|
|
std::string str() const override { return fmt("Starred(level=", level, ")"); }
|
|
|
-
|
|
|
|
|
int star_level() const override { return level; }
|
|
int star_level() const override { return level; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- child->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_UNARY_STAR, level, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_store(CodeEmitContext* ctx) override {
|
|
|
|
|
- if(level != 1) return false;
|
|
|
|
|
- // simply proxy to child
|
|
|
|
|
- return child->emit_store(ctx);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_store(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct NotExpr: Expr{
|
|
struct NotExpr: Expr{
|
|
@@ -223,36 +93,21 @@ struct NotExpr: Expr{
|
|
|
NotExpr(Expr_&& child): child(std::move(child)) {}
|
|
NotExpr(Expr_&& child): child(std::move(child)) {}
|
|
|
std::string str() const override { return "Not()"; }
|
|
std::string str() const override { return "Not()"; }
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- child->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_UNARY_NOT, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct AndExpr: Expr{
|
|
struct AndExpr: Expr{
|
|
|
Expr_ lhs;
|
|
Expr_ lhs;
|
|
|
Expr_ rhs;
|
|
Expr_ rhs;
|
|
|
std::string str() const override { return "And()"; }
|
|
std::string str() const override { return "And()"; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- lhs->emit(ctx);
|
|
|
|
|
- int patch = ctx->emit(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
|
|
|
|
|
- rhs->emit(ctx);
|
|
|
|
|
- ctx->patch_jump(patch);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct OrExpr: Expr{
|
|
struct OrExpr: Expr{
|
|
|
Expr_ lhs;
|
|
Expr_ lhs;
|
|
|
Expr_ rhs;
|
|
Expr_ rhs;
|
|
|
std::string str() const override { return "Or()"; }
|
|
std::string str() const override { return "Or()"; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- lhs->emit(ctx);
|
|
|
|
|
- int patch = ctx->emit(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
|
|
|
|
|
- rhs->emit(ctx);
|
|
|
|
|
- ctx->patch_jump(patch);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// [None, True, False, ...]
|
|
// [None, True, False, ...]
|
|
@@ -260,71 +115,24 @@ struct Literal0Expr: Expr{
|
|
|
TokenIndex token;
|
|
TokenIndex token;
|
|
|
Literal0Expr(TokenIndex token): token(token) {}
|
|
Literal0Expr(TokenIndex token): token(token) {}
|
|
|
std::string str() const override { return TK_STR(token); }
|
|
std::string str() const override { return TK_STR(token); }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- switch (token) {
|
|
|
|
|
- case TK("None"): ctx->emit(OP_LOAD_NONE, BC_NOARG, line); break;
|
|
|
|
|
- case TK("True"): ctx->emit(OP_LOAD_TRUE, BC_NOARG, line); break;
|
|
|
|
|
- case TK("False"): ctx->emit(OP_LOAD_FALSE, BC_NOARG, line); break;
|
|
|
|
|
- case TK("..."): ctx->emit(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
|
|
|
|
|
- default: FATAL_ERROR();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
bool is_json_object() const override { return true; }
|
|
bool is_json_object() const override { return true; }
|
|
|
|
|
+
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct LongExpr: Expr{
|
|
struct LongExpr: Expr{
|
|
|
Str s;
|
|
Str s;
|
|
|
LongExpr(const Str& s): s(s) {}
|
|
LongExpr(const Str& s): s(s) {}
|
|
|
std::string str() const override { return s.str(); }
|
|
std::string str() const override { return s.str(); }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- VM* vm = ctx->vm;
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(s)), line);
|
|
|
|
|
- ctx->emit(OP_BUILD_LONG, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// @num, @str which needs to invoke OP_LOAD_CONST
|
|
// @num, @str which needs to invoke OP_LOAD_CONST
|
|
|
struct LiteralExpr: Expr{
|
|
struct LiteralExpr: Expr{
|
|
|
TokenValue value;
|
|
TokenValue value;
|
|
|
LiteralExpr(TokenValue value): value(value) {}
|
|
LiteralExpr(TokenValue value): value(value) {}
|
|
|
- std::string str() const override {
|
|
|
|
|
- if(std::holds_alternative<i64>(value)){
|
|
|
|
|
- return std::to_string(std::get<i64>(value));
|
|
|
|
|
- }
|
|
|
|
|
- if(std::holds_alternative<f64>(value)){
|
|
|
|
|
- return std::to_string(std::get<f64>(value));
|
|
|
|
|
- }
|
|
|
|
|
- if(std::holds_alternative<Str>(value)){
|
|
|
|
|
- Str s = std::get<Str>(value).escape();
|
|
|
|
|
- return s.str();
|
|
|
|
|
- }
|
|
|
|
|
- FATAL_ERROR();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- VM* vm = ctx->vm;
|
|
|
|
|
- PyObject* obj = nullptr;
|
|
|
|
|
- if(std::holds_alternative<i64>(value)){
|
|
|
|
|
- i64 _val = std::get<i64>(value);
|
|
|
|
|
- if(_val >= INT16_MIN && _val <= INT16_MAX){
|
|
|
|
|
- ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- obj = VAR(_val);
|
|
|
|
|
- }
|
|
|
|
|
- if(std::holds_alternative<f64>(value)){
|
|
|
|
|
- obj = VAR(std::get<f64>(value));
|
|
|
|
|
- }
|
|
|
|
|
- if(std::holds_alternative<Str>(value)){
|
|
|
|
|
- obj = VAR(std::get<Str>(value));
|
|
|
|
|
- }
|
|
|
|
|
- if(obj == nullptr) FATAL_ERROR();
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ std::string str() const override;
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
bool is_literal() const override { return true; }
|
|
bool is_literal() const override { return true; }
|
|
|
bool is_json_object() const override { return true; }
|
|
bool is_json_object() const override { return true; }
|
|
|
};
|
|
};
|
|
@@ -334,33 +142,8 @@ struct NegatedExpr: Expr{
|
|
|
NegatedExpr(Expr_&& child): child(std::move(child)) {}
|
|
NegatedExpr(Expr_&& child): child(std::move(child)) {}
|
|
|
std::string str() const override { return "Negated()"; }
|
|
std::string str() const override { return "Negated()"; }
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- VM* vm = ctx->vm;
|
|
|
|
|
- // if child is a int of float, do constant folding
|
|
|
|
|
- if(child->is_literal()){
|
|
|
|
|
- LiteralExpr* lit = static_cast<LiteralExpr*>(child.get());
|
|
|
|
|
- if(std::holds_alternative<i64>(lit->value)){
|
|
|
|
|
- i64 _val = -std::get<i64>(lit->value);
|
|
|
|
|
- if(_val >= INT16_MIN && _val <= INT16_MAX){
|
|
|
|
|
- ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
|
|
|
|
|
- }
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- if(std::holds_alternative<f64>(lit->value)){
|
|
|
|
|
- PyObject* obj = VAR(-std::get<f64>(lit->value));
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- child->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool is_json_object() const override {
|
|
|
|
|
- return child->is_literal();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool is_json_object() const override { return child->is_literal(); }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct SliceExpr: Expr{
|
|
struct SliceExpr: Expr{
|
|
@@ -368,47 +151,15 @@ struct SliceExpr: Expr{
|
|
|
Expr_ stop;
|
|
Expr_ stop;
|
|
|
Expr_ step;
|
|
Expr_ step;
|
|
|
std::string str() const override { return "Slice()"; }
|
|
std::string str() const override { return "Slice()"; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- if(start){
|
|
|
|
|
- start->emit(ctx);
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(stop){
|
|
|
|
|
- stop->emit(ctx);
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(step){
|
|
|
|
|
- step->emit(ctx);
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct DictItemExpr: Expr{
|
|
struct DictItemExpr: Expr{
|
|
|
Expr_ key; // maybe nullptr if it is **kwargs
|
|
Expr_ key; // maybe nullptr if it is **kwargs
|
|
|
Expr_ value;
|
|
Expr_ value;
|
|
|
std::string str() const override { return "DictItem()"; }
|
|
std::string str() const override { return "DictItem()"; }
|
|
|
-
|
|
|
|
|
int star_level() const override { return value->star_level(); }
|
|
int star_level() const override { return value->star_level(); }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- if(is_starred()){
|
|
|
|
|
- PK_ASSERT(key == nullptr);
|
|
|
|
|
- value->emit(ctx);
|
|
|
|
|
- }else{
|
|
|
|
|
- value->emit(ctx);
|
|
|
|
|
- key->emit(ctx); // reverse order
|
|
|
|
|
- ctx->emit(OP_BUILD_TUPLE, 2, line);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct SequenceExpr: Expr{
|
|
struct SequenceExpr: Expr{
|
|
@@ -463,49 +214,8 @@ struct TupleExpr: SequenceExpr{
|
|
|
return OP_BUILD_TUPLE;
|
|
return OP_BUILD_TUPLE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- bool emit_store(CodeEmitContext* ctx) override {
|
|
|
|
|
- // TOS is an iterable
|
|
|
|
|
- // items may contain StarredExpr, we should check it
|
|
|
|
|
- int starred_i = -1;
|
|
|
|
|
- for(int i=0; i<items.size(); i++){
|
|
|
|
|
- if(!items[i]->is_starred()) continue;
|
|
|
|
|
- if(starred_i == -1) starred_i = i;
|
|
|
|
|
- else return false; // multiple StarredExpr not allowed
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(starred_i == -1){
|
|
|
|
|
- Bytecode& prev = ctx->co->codes.back();
|
|
|
|
|
- if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){
|
|
|
|
|
- // build tuple and unpack it is meaningless
|
|
|
|
|
- prev.op = OP_NO_OP;
|
|
|
|
|
- prev.arg = BC_NOARG;
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
|
|
|
|
|
- }
|
|
|
|
|
- }else{
|
|
|
|
|
- // starred assignment target must be in a tuple
|
|
|
|
|
- if(items.size() == 1) return false;
|
|
|
|
|
- // starred assignment target must be the last one (differ from cpython)
|
|
|
|
|
- if(starred_i != items.size()-1) return false;
|
|
|
|
|
- // a,*b = [1,2,3]
|
|
|
|
|
- // stack is [1,2,3] -> [1,[2,3]]
|
|
|
|
|
- ctx->emit(OP_UNPACK_EX, items.size()-1, line);
|
|
|
|
|
- }
|
|
|
|
|
- // do reverse emit
|
|
|
|
|
- for(int i=items.size()-1; i>=0; i--){
|
|
|
|
|
- bool ok = items[i]->emit_store(ctx);
|
|
|
|
|
- if(!ok) return false;
|
|
|
|
|
- }
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_del(CodeEmitContext* ctx) override{
|
|
|
|
|
- for(auto& e: items){
|
|
|
|
|
- bool ok = e->emit_del(ctx);
|
|
|
|
|
- if(!ok) return false;
|
|
|
|
|
- }
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ bool emit_store(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_del(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct CompExpr: Expr{
|
|
struct CompExpr: Expr{
|
|
@@ -517,28 +227,7 @@ struct CompExpr: Expr{
|
|
|
virtual Opcode op0() = 0;
|
|
virtual Opcode op0() = 0;
|
|
|
virtual Opcode op1() = 0;
|
|
virtual Opcode op1() = 0;
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx){
|
|
|
|
|
- ctx->emit(op0(), 0, line);
|
|
|
|
|
- iter->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- ctx->enter_block(FOR_LOOP);
|
|
|
|
|
- ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- bool ok = vars->emit_store(ctx);
|
|
|
|
|
- // this error occurs in `vars` instead of this line, but...nevermind
|
|
|
|
|
- PK_ASSERT(ok); // TODO: raise a SyntaxError instead
|
|
|
|
|
- if(cond){
|
|
|
|
|
- cond->emit(ctx);
|
|
|
|
|
- int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- expr->emit(ctx);
|
|
|
|
|
- ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- ctx->patch_jump(patch);
|
|
|
|
|
- }else{
|
|
|
|
|
- expr->emit(ctx);
|
|
|
|
|
- ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- }
|
|
|
|
|
- ctx->emit(OP_LOOP_CONTINUE, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- ctx->exit_block();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct ListCompExpr: CompExpr{
|
|
struct ListCompExpr: CompExpr{
|
|
@@ -578,52 +267,8 @@ struct FStringExpr: Expr{
|
|
|
return fmt("f", src.escape());
|
|
return fmt("f", src.escape());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void _load_simple_expr(CodeEmitContext* ctx, Str expr){
|
|
|
|
|
- // TODO: pre compile this into a function
|
|
|
|
|
- int dot = expr.index(".");
|
|
|
|
|
- if(dot < 0){
|
|
|
|
|
- ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
|
|
|
|
|
- }else{
|
|
|
|
|
- StrName name(expr.substr(0, dot).sv());
|
|
|
|
|
- StrName attr(expr.substr(dot+1).sv());
|
|
|
|
|
- ctx->emit(OP_LOAD_NAME, name.index, line);
|
|
|
|
|
- ctx->emit(OP_LOAD_ATTR, attr.index, line);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- VM* vm = ctx->vm;
|
|
|
|
|
- static const std::regex pattern(R"(\{(.*?)\})");
|
|
|
|
|
- std::cregex_iterator begin(src.begin(), src.end(), pattern);
|
|
|
|
|
- std::cregex_iterator end;
|
|
|
|
|
- int size = 0;
|
|
|
|
|
- int i = 0;
|
|
|
|
|
- for(auto it = begin; it != end; it++) {
|
|
|
|
|
- std::cmatch m = *it;
|
|
|
|
|
- if (i < m.position()) {
|
|
|
|
|
- Str literal = src.substr(i, m.position() - i);
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
|
|
|
|
|
- size++;
|
|
|
|
|
- }
|
|
|
|
|
- Str expr = m[1].str();
|
|
|
|
|
- int conon = expr.index(":");
|
|
|
|
|
- if(conon >= 0){
|
|
|
|
|
- _load_simple_expr(ctx, expr.substr(0, conon));
|
|
|
|
|
- Str spec = expr.substr(conon+1);
|
|
|
|
|
- ctx->emit(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
|
|
|
|
|
- }else{
|
|
|
|
|
- _load_simple_expr(ctx, expr);
|
|
|
|
|
- }
|
|
|
|
|
- size++;
|
|
|
|
|
- i = (int)(m.position() + m.length());
|
|
|
|
|
- }
|
|
|
|
|
- if (i < src.length()) {
|
|
|
|
|
- Str literal = src.substr(i, src.length() - i);
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
|
|
|
|
|
- size++;
|
|
|
|
|
- }
|
|
|
|
|
- ctx->emit(OP_BUILD_STRING, size, line);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void _load_simple_expr(CodeEmitContext* ctx, Str expr);
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct SubscrExpr: Expr{
|
|
struct SubscrExpr: Expr{
|
|
@@ -631,25 +276,9 @@ struct SubscrExpr: Expr{
|
|
|
Expr_ b;
|
|
Expr_ b;
|
|
|
std::string str() const override { return "Subscr()"; }
|
|
std::string str() const override { return "Subscr()"; }
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override{
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- b->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_del(CodeEmitContext* ctx) override {
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- b->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_store(CodeEmitContext* ctx) override {
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- b->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_del(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_store(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct AttribExpr: Expr{
|
|
struct AttribExpr: Expr{
|
|
@@ -659,32 +288,10 @@ struct AttribExpr: Expr{
|
|
|
AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
|
|
AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
|
|
|
std::string str() const override { return "Attrib()"; }
|
|
std::string str() const override { return "Attrib()"; }
|
|
|
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override{
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- int index = StrName(b).index;
|
|
|
|
|
- ctx->emit(OP_LOAD_ATTR, index, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_del(CodeEmitContext* ctx) override {
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- int index = StrName(b).index;
|
|
|
|
|
- ctx->emit(OP_DELETE_ATTR, index, line);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- bool emit_store(CodeEmitContext* ctx) override {
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- int index = StrName(b).index;
|
|
|
|
|
- ctx->emit(OP_STORE_ATTR, index, line);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void emit_method(CodeEmitContext* ctx) {
|
|
|
|
|
- a->emit(ctx);
|
|
|
|
|
- int index = StrName(b).index;
|
|
|
|
|
- ctx->emit(OP_LOAD_METHOD, index, line);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_del(CodeEmitContext* ctx) override;
|
|
|
|
|
+ bool emit_store(CodeEmitContext* ctx) override;
|
|
|
|
|
+ void emit_method(CodeEmitContext* ctx);
|
|
|
bool is_attrib() const override { return true; }
|
|
bool is_attrib() const override { return true; }
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -694,57 +301,7 @@ struct CallExpr: Expr{
|
|
|
// **a will be interpreted as a special keyword argument: {"**": a}
|
|
// **a will be interpreted as a special keyword argument: {"**": a}
|
|
|
std::vector<std::pair<Str, Expr_>> kwargs;
|
|
std::vector<std::pair<Str, Expr_>> kwargs;
|
|
|
std::string str() const override { return "Call()"; }
|
|
std::string str() const override { return "Call()"; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- bool vargs = false;
|
|
|
|
|
- bool vkwargs = false;
|
|
|
|
|
- for(auto& arg: args) if(arg->is_starred()) vargs = true;
|
|
|
|
|
- for(auto& item: kwargs) if(item.second->is_starred()) vkwargs = true;
|
|
|
|
|
-
|
|
|
|
|
- // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod` proxy
|
|
|
|
|
- if(callable->is_attrib()){
|
|
|
|
|
- auto p = static_cast<AttribExpr*>(callable.get());
|
|
|
|
|
- p->emit_method(ctx); // OP_LOAD_METHOD
|
|
|
|
|
- }else{
|
|
|
|
|
- callable->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(vargs || vkwargs){
|
|
|
|
|
- for(auto& item: args) item->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_BUILD_TUPLE_UNPACK, (int)args.size(), line);
|
|
|
|
|
-
|
|
|
|
|
- if(!kwargs.empty()){
|
|
|
|
|
- for(auto& item: kwargs){
|
|
|
|
|
- if(item.second->is_starred()){
|
|
|
|
|
- if(item.second->star_level() != 2) FATAL_ERROR();
|
|
|
|
|
- item.second->emit(ctx);
|
|
|
|
|
- }else{
|
|
|
|
|
- // k=v
|
|
|
|
|
- int index = ctx->add_const(py_var(ctx->vm, item.first));
|
|
|
|
|
- ctx->emit(OP_LOAD_CONST, index, line);
|
|
|
|
|
- item.second->emit(ctx);
|
|
|
|
|
- ctx->emit(OP_BUILD_TUPLE, 2, line);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- ctx->emit(OP_BUILD_DICT_UNPACK, (int)kwargs.size(), line);
|
|
|
|
|
- ctx->emit(OP_CALL_TP, 1, line);
|
|
|
|
|
- }else{
|
|
|
|
|
- ctx->emit(OP_CALL_TP, 0, line);
|
|
|
|
|
- }
|
|
|
|
|
- }else{
|
|
|
|
|
- // vectorcall protocal
|
|
|
|
|
- for(auto& item: args) item->emit(ctx);
|
|
|
|
|
- for(auto& item: kwargs){
|
|
|
|
|
- int index = StrName(item.first.sv()).index;
|
|
|
|
|
- ctx->emit(OP_LOAD_INTEGER, index, line);
|
|
|
|
|
- item.second->emit(ctx);
|
|
|
|
|
- }
|
|
|
|
|
- int KWARGC = (int)kwargs.size();
|
|
|
|
|
- int ARGC = (int)args.size();
|
|
|
|
|
- ctx->emit(OP_CALL, (KWARGC<<16)|ARGC, line);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
struct GroupedExpr: Expr{
|
|
struct GroupedExpr: Expr{
|
|
@@ -772,82 +329,9 @@ struct BinaryExpr: Expr{
|
|
|
Expr_ rhs;
|
|
Expr_ rhs;
|
|
|
std::string str() const override { return TK_STR(op); }
|
|
std::string str() const override { return TK_STR(op); }
|
|
|
|
|
|
|
|
- bool is_compare() const override {
|
|
|
|
|
- switch(op){
|
|
|
|
|
- case TK("<"): case TK("<="): case TK("=="):
|
|
|
|
|
- case TK("!="): case TK(">"): case TK(">="): return true;
|
|
|
|
|
- default: return false;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void _emit_compare(CodeEmitContext* ctx, std::vector<int>& jmps){
|
|
|
|
|
- if(lhs->is_compare()){
|
|
|
|
|
- static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
|
|
|
|
|
- }else{
|
|
|
|
|
- lhs->emit(ctx); // [a]
|
|
|
|
|
- }
|
|
|
|
|
- rhs->emit(ctx); // [a, b]
|
|
|
|
|
- ctx->emit(OP_DUP_TOP, BC_NOARG, line); // [a, b, b]
|
|
|
|
|
- ctx->emit(OP_ROT_THREE, BC_NOARG, line); // [b, a, b]
|
|
|
|
|
- switch(op){
|
|
|
|
|
- case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
|
|
|
|
|
- case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
|
|
|
|
|
- case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
|
|
|
|
|
- case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
|
|
|
|
|
- case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
|
|
|
|
|
- case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
|
|
|
|
|
- default: UNREACHABLE();
|
|
|
|
|
- }
|
|
|
|
|
- // [b, RES]
|
|
|
|
|
- int index = ctx->emit(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);
|
|
|
|
|
- jmps.push_back(index);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- std::vector<int> jmps;
|
|
|
|
|
- if(is_compare() && lhs->is_compare()){
|
|
|
|
|
- // (a < b) < c
|
|
|
|
|
- static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
|
|
|
|
|
- // [b, RES]
|
|
|
|
|
- }else{
|
|
|
|
|
- // (1 + 2) < c
|
|
|
|
|
- lhs->emit(ctx);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- rhs->emit(ctx);
|
|
|
|
|
- switch (op) {
|
|
|
|
|
- case TK("+"): ctx->emit(OP_BINARY_ADD, BC_NOARG, line); break;
|
|
|
|
|
- case TK("-"): ctx->emit(OP_BINARY_SUB, BC_NOARG, line); break;
|
|
|
|
|
- case TK("*"): ctx->emit(OP_BINARY_MUL, BC_NOARG, line); break;
|
|
|
|
|
- case TK("/"): ctx->emit(OP_BINARY_TRUEDIV, BC_NOARG, line); break;
|
|
|
|
|
- case TK("//"): ctx->emit(OP_BINARY_FLOORDIV, BC_NOARG, line); break;
|
|
|
|
|
- case TK("%"): ctx->emit(OP_BINARY_MOD, BC_NOARG, line); break;
|
|
|
|
|
- case TK("**"): ctx->emit(OP_BINARY_POW, BC_NOARG, line); break;
|
|
|
|
|
-
|
|
|
|
|
- case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
|
|
|
|
|
- case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
|
|
|
|
|
- case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
|
|
|
|
|
- case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
|
|
|
|
|
- case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
|
|
|
|
|
- case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
|
|
|
|
|
-
|
|
|
|
|
- case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
|
|
|
|
|
- case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
|
|
|
|
|
- case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
|
|
|
|
|
- case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
|
|
|
|
|
-
|
|
|
|
|
- case TK("<<"): ctx->emit(OP_BITWISE_LSHIFT, BC_NOARG, line); break;
|
|
|
|
|
- case TK(">>"): ctx->emit(OP_BITWISE_RSHIFT, BC_NOARG, line); break;
|
|
|
|
|
- case TK("&"): ctx->emit(OP_BITWISE_AND, BC_NOARG, line); break;
|
|
|
|
|
- case TK("|"): ctx->emit(OP_BITWISE_OR, BC_NOARG, line); break;
|
|
|
|
|
- case TK("^"): ctx->emit(OP_BITWISE_XOR, BC_NOARG, line); break;
|
|
|
|
|
-
|
|
|
|
|
- case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break;
|
|
|
|
|
- default: FATAL_ERROR();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for(int i: jmps) ctx->patch_jump(i);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ bool is_compare() const override;
|
|
|
|
|
+ void _emit_compare(CodeEmitContext* ctx, std::vector<int>& jmps);
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
@@ -856,16 +340,7 @@ struct TernaryExpr: Expr{
|
|
|
Expr_ true_expr;
|
|
Expr_ true_expr;
|
|
|
Expr_ false_expr;
|
|
Expr_ false_expr;
|
|
|
std::string str() const override { return "Ternary()"; }
|
|
std::string str() const override { return "Ternary()"; }
|
|
|
-
|
|
|
|
|
- void emit(CodeEmitContext* ctx) override {
|
|
|
|
|
- cond->emit(ctx);
|
|
|
|
|
- int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
|
|
|
|
|
- true_expr->emit(ctx);
|
|
|
|
|
- int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
|
|
|
|
|
- ctx->patch_jump(patch);
|
|
|
|
|
- false_expr->emit(ctx);
|
|
|
|
|
- ctx->patch_jump(patch_2);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ void emit(CodeEmitContext* ctx) override;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|