From ce0ba313d1064132c9169d1c50f69a4d0fface84 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 13 Feb 2023 18:06:52 +0800 Subject: [PATCH] fix neg constant --- src/codeobject.h | 4 ++-- src/vm.h | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/codeobject.h b/src/codeobject.h index 2592105b..293a4861 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -65,6 +65,8 @@ struct CodeObject { std::vector blocks = { CodeBlock{NO_BLOCK, -1} }; emhash8::HashMap labels; + void optimize(VM* vm); + bool add_label(const Str& label){ if(labels.contains(label)) return false; labels[label] = codes.size(); @@ -86,8 +88,6 @@ struct CodeObject { return consts.size() - 1; } - void optimize(VM* vm); - /************************************************/ int _curr_block_i = 0; bool _rvalue = false; diff --git a/src/vm.h b/src/vm.h index 1e4db040..84effb4a 100644 --- a/src/vm.h +++ b/src/vm.h @@ -1129,9 +1129,10 @@ PyVar pkpy::NativeFunc::operator()(VM* vm, const pkpy::Args& args) const{ void CodeObject::optimize(VM* vm){ for(int i=1; inum_negated(consts[pos]); + if(codes[i].op == OP_UNARY_NEGATIVE && codes[i-1].op == OP_LOAD_CONST){ + codes[i].op = OP_NO_OP; + int pos = codes[i-1].arg; + consts[pos] = vm->num_negated(consts[pos]); + } } } \ No newline at end of file