From f5e68b07d932c270b4fc22ed84c61c2a28a466a2 Mon Sep 17 00:00:00 2001 From: gf712 Date: Wed, 26 Nov 2025 21:27:11 +0000 Subject: [PATCH 1/8] add CI --- .github/workflows/premerge.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/premerge.yml diff --git a/.github/workflows/premerge.yml b/.github/workflows/premerge.yml new file mode 100644 index 0000000..70b1409 --- /dev/null +++ b/.github/workflows/premerge.yml @@ -0,0 +1,33 @@ +name: Premerge + +on: + pull_request: + branches: [ "main" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install LLVM + run: | + wget https://apt.llvm.org/llvm.sh + chmod +x llvm.sh + sudo ./llvm.sh 20 all + + - name: Configure CMake + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --target test + From d50259ce5fbd0007fafcb1e73f47771d71a02cf6 Mon Sep 17 00:00:00 2001 From: gf712 Date: Wed, 26 Nov 2025 21:38:03 +0000 Subject: [PATCH 2/8] Bump LLVM version requirement to 20.1 --- .github/workflows/premerge.yml | 1 - src/CMakeLists.txt | 2 +- src/executable/mlir/CMakeLists.txt | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/premerge.yml b/.github/workflows/premerge.yml index 70b1409..2b91a7d 100644 --- a/.github/workflows/premerge.yml +++ b/.github/workflows/premerge.yml @@ -5,7 +5,6 @@ on: branches: [ "main" ] env: - # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: Release jobs: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0855266..a2e5e76 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -298,7 +298,7 @@ target_link_libraries(python-cpp ) # LLVM backend -find_package(LLVM CONFIG 20.0) +find_package(LLVM CONFIG 20.1) if(ENABLE_LLVM_BACKEND AND NOT LLVM_FOUND) message(FATAL_ERROR "Could not find LLVM in the local environment") elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND) diff --git a/src/executable/mlir/CMakeLists.txt b/src/executable/mlir/CMakeLists.txt index 060a16c..dee5c9f 100644 --- a/src/executable/mlir/CMakeLists.txt +++ b/src/executable/mlir/CMakeLists.txt @@ -1,4 +1,4 @@ -find_package(LLVM 20.0 REQUIRED CONFIG) +find_package(LLVM 20.1 REQUIRED CONFIG) message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") find_package(MLIR CONFIG REQUIRED From dfd04981d712480e8d7d9b459da1c900a9f54a9f Mon Sep 17 00:00:00 2001 From: gf712 Date: Thu, 27 Nov 2025 09:00:18 +0000 Subject: [PATCH 3/8] Install MLIR in CI step --- .github/workflows/premerge.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/premerge.yml b/.github/workflows/premerge.yml index 2b91a7d..59e052e 100644 --- a/.github/workflows/premerge.yml +++ b/.github/workflows/premerge.yml @@ -19,6 +19,7 @@ jobs: wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh sudo ./llvm.sh 20 all + sudo apt install libmlir-20-dev mlir-20-tools - name: Configure CMake run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} From 1662dd3289e97537eac5a69f2fefe0a82bd62d28 Mon Sep 17 00:00:00 2001 From: gf712 Date: Mon, 15 Dec 2025 16:35:06 +0000 Subject: [PATCH 4/8] MLIR: fix and clean up register allocation --- integration/program.cpp | 2 +- src/ast/AST.hpp | 4 +- src/ast/optimizers/ConstantFolding.cpp | 4 +- src/executable/Label.hpp | 2 +- src/executable/Mangler.cpp | 4 +- src/executable/bytecode/Bytecode.cpp | 8 +- src/executable/bytecode/BytecodeProgram.cpp | 12 +- .../bytecode/BytecodeProgram_tests.cpp | 2 +- src/executable/bytecode/Bytecode_tests.cpp | 2 +- .../bytecode/codegen/BytecodeGenerator.cpp | 94 +-- .../bytecode/codegen/BytecodeGenerator.hpp | 12 +- .../codegen/BytecodeGenerator_tests.cpp | 2 +- .../bytecode/codegen/VariablesResolver.cpp | 20 +- .../codegen/VariablesResolver_tests.cpp | 2 +- .../bytecode/instructions/BinaryOperation.cpp | 4 +- .../bytecode/instructions/BuildList.cpp | 2 +- .../instructions/CompareOperation.cpp | 4 +- .../bytecode/instructions/DeleteAttr.cpp | 2 +- .../bytecode/instructions/DeleteDeref.cpp | 2 +- .../bytecode/instructions/DictAdd.cpp | 6 +- .../bytecode/instructions/DictMerge.cpp | 12 +- .../bytecode/instructions/ForIter.cpp | 4 +- .../bytecode/instructions/FunctionCall.cpp | 4 +- .../bytecode/instructions/FunctionCallEx.cpp | 8 +- .../instructions/FunctionCallWithKeywords.cpp | 2 +- .../bytecode/instructions/Instructions.cpp | 2 +- src/executable/bytecode/instructions/Jump.cpp | 4 +- .../bytecode/instructions/JumpForward.cpp | 4 +- .../instructions/JumpIfExceptionMatch.cpp | 6 +- .../bytecode/instructions/JumpIfFalse.cpp | 4 +- .../instructions/JumpIfFalseOrPop.cpp | 4 +- .../instructions/JumpIfNotExceptionMatch.cpp | 6 +- .../bytecode/instructions/JumpIfTrue.cpp | 4 +- .../bytecode/instructions/JumpIfTrueOrPop.cpp | 4 +- .../bytecode/instructions/ListAppend.cpp | 6 +- .../bytecode/instructions/ListExtend.cpp | 6 +- .../bytecode/instructions/ListToTuple.cpp | 4 +- .../bytecode/instructions/LoadAttr.cpp | 2 +- .../bytecode/instructions/LoadConst.cpp | 6 +- .../bytecode/instructions/MakeFunction.cpp | 2 +- .../bytecode/instructions/MethodCall.cpp | 2 +- .../bytecode/instructions/RaiseVarargs.cpp | 2 +- .../bytecode/instructions/RaiseVarargs.hpp | 2 +- .../bytecode/instructions/ReRaise.cpp | 2 +- .../bytecode/instructions/SetAdd.cpp | 6 +- .../bytecode/instructions/SetUpdate.cpp | 6 +- .../instructions/SetupExceptionHandling.cpp | 4 +- .../bytecode/instructions/SetupWith.cpp | 4 +- .../bytecode/instructions/StoreAttr.cpp | 2 +- .../bytecode/instructions/StoreDeref.cpp | 2 +- .../bytecode/instructions/WithExceptStart.cpp | 2 +- src/executable/llvm/LLVMGenerator.cpp | 18 +- src/executable/llvm/LLVMGenerator_tests.cpp | 2 +- src/executable/llvm/LLVMPyUtils.cpp | 8 +- src/executable/mlir/CMakeLists.txt | 2 +- .../PythonToPythonBytecode.cpp | 4 +- .../mlir/Dialect/Python/MLIRGenerator.cpp | 12 +- .../mlir/Dialect/Python/MLIRGenerator.hpp | 2 +- src/executable/mlir/Target/CMakeLists.txt | 3 +- .../mlir/Target/PythonBytecode/CMakeLists.txt | 2 +- .../LinearScanRegisterAllocation.hpp | 699 ++++++++++++++++++ .../Target/PythonBytecode/LiveAnalysis.hpp | 477 ++++++++++++ .../PythonBytecode/LiveIntervalAnalysis.hpp | 196 +++++ .../RegisterAllocationLogger.hpp | 63 ++ .../RegisterAllocationTypes.hpp | 80 ++ .../TranslateToPythonBytecode.cpp | 3 + src/interpreter/Interpreter.cpp | 8 +- src/interpreter/InterpreterSession.hpp | 6 +- src/lexer/Lexer.cpp | 8 +- src/lexer/Lexer.hpp | 2 +- src/memory/GarbageCollector.cpp | 4 +- src/memory/Heap.cpp | 2 +- src/memory/Heap.hpp | 10 +- src/memory/Heap_tests.cpp | 2 +- src/parser/Parser.cpp | 2 +- src/runtime/AssertionError.cpp | 2 +- src/runtime/AttributeError.cpp | 4 +- src/runtime/AttributeError.hpp | 4 +- src/runtime/BaseException.cpp | 13 +- src/runtime/Exception.cpp | 2 +- src/runtime/Import.cpp | 8 +- src/runtime/ImportError.hpp | 4 +- src/runtime/IndexError.cpp | 2 +- src/runtime/IndexError.hpp | 4 +- src/runtime/KeyError.cpp | 12 +- src/runtime/KeyError.hpp | 4 +- src/runtime/LookupError.cpp | 2 +- src/runtime/LookupError.hpp | 4 +- src/runtime/MemoryError.cpp | 2 +- src/runtime/ModuleNotFoundError.hpp | 4 +- src/runtime/NameError.hpp | 4 +- src/runtime/NotImplementedError.cpp | 2 +- src/runtime/NotImplementedError.hpp | 4 +- src/runtime/OSError.cpp | 2 +- src/runtime/OSError.hpp | 4 +- src/runtime/PyBool.cpp | 6 +- src/runtime/PyByteArray.cpp | 8 +- src/runtime/PyCell.cpp | 5 +- src/runtime/PyClassMethod.cpp | 6 +- src/runtime/PyClassMethodDescriptor.cpp | 2 +- src/runtime/PyCode.cpp | 6 +- src/runtime/PyDict.cpp | 20 +- src/runtime/PyEllipsis.cpp | 2 +- src/runtime/PyFloat.cpp | 2 +- src/runtime/PyFrame.cpp | 8 +- src/runtime/PyInteger.cpp | 2 +- src/runtime/PyList.cpp | 4 +- src/runtime/PyModule.cpp | 12 +- src/runtime/PyObject.cpp | 30 +- src/runtime/PyProperty.cpp | 18 +- src/runtime/PyRange.cpp | 6 +- src/runtime/PySlotWrapper.cpp | 4 +- src/runtime/PyStaticMethod.cpp | 4 +- src/runtime/PyString.cpp | 134 ++-- src/runtime/PyString.hpp | 6 +- src/runtime/PySuper.cpp | 2 +- src/runtime/PyTuple.cpp | 8 +- src/runtime/PyType.cpp | 10 +- src/runtime/RuntimeError.cpp | 2 +- src/runtime/RuntimeError.hpp | 6 +- src/runtime/StopIteration.cpp | 4 +- src/runtime/SyntaxError.cpp | 2 +- src/runtime/SyntaxError.hpp | 4 +- src/runtime/TypeError.cpp | 2 +- src/runtime/TypeError.hpp | 4 +- src/runtime/UnboundLocalError.cpp | 2 +- src/runtime/UnboundLocalError.hpp | 4 +- src/runtime/Value.cpp | 2 +- src/runtime/ValueError.cpp | 6 +- src/runtime/ValueError.hpp | 4 +- src/runtime/modules/BuiltinsModule.cpp | 24 +- src/runtime/modules/ImpModule.cpp | 42 +- src/runtime/modules/PosixModule.cpp | 2 +- src/runtime/modules/SysModule.cpp | 6 +- src/runtime/modules/thread/Lock.hpp | 2 +- src/runtime/modules/thread/RLock.hpp | 2 +- .../modules/weakref/PyCallableProxyType.cpp | 6 +- src/runtime/modules/weakref/PyWeakProxy.cpp | 6 +- src/runtime/modules/weakref/PyWeakRef.cpp | 6 +- src/runtime/warnings/DeprecationWarning.cpp | 2 +- src/runtime/warnings/DeprecationWarning.hpp | 4 +- src/runtime/warnings/ImportWarning.cpp | 4 +- src/runtime/warnings/ImportWarning.hpp | 4 +- .../warnings/PendingDeprecationWarning.cpp | 2 +- .../warnings/PendingDeprecationWarning.hpp | 4 +- src/runtime/warnings/ResourceWarning.cpp | 2 +- src/runtime/warnings/ResourceWarning.hpp | 4 +- src/runtime/warnings/Warning.cpp | 2 +- src/runtime/warnings/Warning.hpp | 4 +- src/utilities.hpp | 21 +- src/vm/VM.cpp | 4 +- src/vm/VM.hpp | 8 +- 152 files changed, 2018 insertions(+), 510 deletions(-) create mode 100644 src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp create mode 100644 src/executable/mlir/Target/PythonBytecode/LiveAnalysis.hpp create mode 100644 src/executable/mlir/Target/PythonBytecode/LiveIntervalAnalysis.hpp create mode 100644 src/executable/mlir/Target/PythonBytecode/RegisterAllocationLogger.hpp create mode 100644 src/executable/mlir/Target/PythonBytecode/RegisterAllocationTypes.hpp diff --git a/integration/program.cpp b/integration/program.cpp index 7a4a574..36517a0 100644 --- a/integration/program.cpp +++ b/integration/program.cpp @@ -88,7 +88,7 @@ template void assert_interpreter_object_value(std::string name, T ex auto key_ = p->operator[](0); auto value_ = p->operator[](1); // only support string keys for now - ASSERT(key_.unwrap()) + ASSERT(key_.unwrap()); auto key_string = as(key_.unwrap())->value(); check_value(value_.unwrap(), expected_value[key_string]); } diff --git a/src/ast/AST.hpp b/src/ast/AST.hpp index b222a78..1a210ad 100644 --- a/src/ast/AST.hpp +++ b/src/ast/AST.hpp @@ -1187,7 +1187,7 @@ class Subscript : public ASTNode const std::shared_ptr &value() const { return m_value; } const SliceType &slice() const { - ASSERT(m_slice) + ASSERT(m_slice); return *m_slice; } ContextType context() const { return m_ctx; } @@ -1304,7 +1304,7 @@ class Assert : public ASTNode : ASTNode(ASTNodeType::Assert, source_location), m_test(std::move(test)), m_msg(std::move(msg)) { - ASSERT(m_test) + ASSERT(m_test); } const std::shared_ptr &test() const { return m_test; } diff --git a/src/ast/optimizers/ConstantFolding.cpp b/src/ast/optimizers/ConstantFolding.cpp index db62ac1..f9a531b 100644 --- a/src/ast/optimizers/ConstantFolding.cpp +++ b/src/ast/optimizers/ConstantFolding.cpp @@ -11,8 +11,8 @@ namespace optimizer { && node->rhs()->node_type() == ASTNodeType::Constant) { const auto &lhs = as(node->lhs())->value(); const auto &rhs = as(node->rhs())->value(); - ASSERT(lhs) - ASSERT(rhs) + ASSERT(lhs); + ASSERT(rhs); switch (node->op_type()) { case BinaryOpType::PLUS: { auto result = std::visit( diff --git a/src/executable/Label.hpp b/src/executable/Label.hpp index 90bf85d..ed1c675 100644 --- a/src/executable/Label.hpp +++ b/src/executable/Label.hpp @@ -24,7 +24,7 @@ class Label protected: void set_position(int64_t position) const { - ASSERT(!m_position.has_value()) + ASSERT(!m_position.has_value()); m_position = position; } diff --git a/src/executable/Mangler.cpp b/src/executable/Mangler.cpp index 77cef7f..b46b50b 100644 --- a/src/executable/Mangler.cpp +++ b/src/executable/Mangler.cpp @@ -62,8 +62,8 @@ class DefaultMangler : public Mangler std::string_view mangled_class{ mangled_name.c_str() + start, end - start }; - ASSERT(mangled_class.starts_with("__class__")) - ASSERT(mangled_class.ends_with("__")) + ASSERT(mangled_class.starts_with("__class__")); + ASSERT(mangled_class.ends_with("__")); // extract name from __class__{}__ return std::string{ mangled_class.begin() + 9, mangled_class.end() - 2 }; diff --git a/src/executable/bytecode/Bytecode.cpp b/src/executable/bytecode/Bytecode.cpp index 77ff8ab..350e6c7 100644 --- a/src/executable/bytecode/Bytecode.cpp +++ b/src/executable/bytecode/Bytecode.cpp @@ -125,14 +125,14 @@ py::PyResult Bytecode::eval_loop(VirtualMachine &vm, Interpreter &int const auto end_instruction_it = end(); for (; vm.instruction_pointer() != end_instruction_it; vm.set_instruction_pointer(std::next(vm.instruction_pointer()))) { - ASSERT((*vm.instruction_pointer()).get()) + ASSERT((*vm.instruction_pointer()).get()); const auto ¤t_ip = vm.instruction_pointer(); const auto &instruction = *current_ip; spdlog::debug("{} {}", (void *)instruction.get(), instruction->to_string()); auto result = instruction->execute(vm, vm.interpreter()); // we left the current stack frame in the previous instruction if (vm.stack().size() != stack_depth) { - ASSERT(result.is_ok()) + ASSERT(result.is_ok()); return result; } // vm.dump(); @@ -143,7 +143,7 @@ py::PyResult Bytecode::eval_loop(VirtualMachine &vm, Interpreter &int PyTraceback *tb_next = exception->traceback(); auto traceback = PyTraceback::create(interpreter.execution_frame(), tb_lasti, tb_lineno, tb_next); - ASSERT(traceback.is_ok()) + ASSERT(traceback.is_ok()); exception->set_traceback(traceback.unwrap()); interpreter.raise_exception(exception); @@ -165,6 +165,6 @@ py::PyResult Bytecode::eval_loop(VirtualMachine &vm, Interpreter &int } } - ASSERT(value.has_value()) + ASSERT(value.has_value()); return Ok(*value); } diff --git a/src/executable/bytecode/BytecodeProgram.cpp b/src/executable/bytecode/BytecodeProgram.cpp index 0fcd935..8850caf 100644 --- a/src/executable/bytecode/BytecodeProgram.cpp +++ b/src/executable/bytecode/BytecodeProgram.cpp @@ -104,14 +104,14 @@ size_t BytecodeProgram::main_stack_size() const { return m_main_function->regist InstructionVector::const_iterator BytecodeProgram::begin() const { // FIXME: assumes all functions are bytecode - ASSERT(m_main_function->function()->backend() == FunctionExecutionBackend::BYTECODE) + ASSERT(m_main_function->function()->backend() == FunctionExecutionBackend::BYTECODE); return static_cast(m_main_function->function().get())->begin(); } InstructionVector::const_iterator BytecodeProgram::end() const { // FIXME: assumes all functions are bytecode - ASSERT(m_main_function->function()->backend() == FunctionExecutionBackend::BYTECODE) + ASSERT(m_main_function->function()->backend() == FunctionExecutionBackend::BYTECODE); return static_cast(m_main_function->function().get())->end(); } @@ -136,7 +136,7 @@ int BytecodeProgram::execute(VirtualMachine *vm) if (result.is_err()) { auto *exception = interpreter.execution_frame()->pop_exception(); - ASSERT(exception == result.unwrap_err()) + ASSERT(exception == result.unwrap_err()); std::cout << exception->format_traceback() << std::endl; // if (interpreter.execution_frame()->exception_info().has_value()) { @@ -214,7 +214,7 @@ std::vector BytecodeProgram::serialize() const } // TODO: Add support to serialize functions from different backends - ASSERT(m_backends.empty()) + ASSERT(m_backends.empty()); return result; } @@ -226,14 +226,14 @@ std::shared_ptr BytecodeProgram::deserialize(const std::vector< auto span = std::span{ buffer }; auto deserialized_result = PyCode::deserialize(span, program); - ASSERT(deserialized_result.first.is_ok()) + ASSERT(deserialized_result.first.is_ok()); program->m_main_function = deserialized_result.first.unwrap(); spdlog::debug( "Deserialized main function:\n{}\n\n", program->m_main_function->function()->to_string()); while (!span.empty()) { deserialized_result = PyCode::deserialize(span, program); - ASSERT(deserialized_result.first.is_ok()) + ASSERT(deserialized_result.first.is_ok()); program->m_functions.push_back(deserialized_result.first.unwrap()); spdlog::debug("Deserialized function {}:\n{}\n\n", program->m_functions.back()->function()->function_name(), diff --git a/src/executable/bytecode/BytecodeProgram_tests.cpp b/src/executable/bytecode/BytecodeProgram_tests.cpp index e0f1542..93fecbc 100644 --- a/src/executable/bytecode/BytecodeProgram_tests.cpp +++ b/src/executable/bytecode/BytecodeProgram_tests.cpp @@ -15,7 +15,7 @@ std::shared_ptr generate_bytecode(std::string_view program) p.parse(); auto module = as(p.module()); - ASSERT(module) + ASSERT(module); return std::static_pointer_cast(compiler::compile( module, {}, compiler::Backend::BYTECODE_GENERATOR, compiler::OptimizationLevel::None)); diff --git a/src/executable/bytecode/Bytecode_tests.cpp b/src/executable/bytecode/Bytecode_tests.cpp index 41a8c24..ee0b05c 100644 --- a/src/executable/bytecode/Bytecode_tests.cpp +++ b/src/executable/bytecode/Bytecode_tests.cpp @@ -17,7 +17,7 @@ // p.parse(); // auto module = as(p.module()); -// ASSERT(module) +// ASSERT(module); // auto bytecode = // codegen::BytecodeGenerator::compile(module, {}, compiler::OptimizationLevel::None); diff --git a/src/executable/bytecode/codegen/BytecodeGenerator.cpp b/src/executable/bytecode/codegen/BytecodeGenerator.cpp index c1a9cc1..1a2c3b5 100644 --- a/src/executable/bytecode/codegen/BytecodeGenerator.cpp +++ b/src/executable/bytecode/codegen/BytecodeGenerator.cpp @@ -295,7 +295,7 @@ void BytecodeGenerator::store_name(const std::string &name, BytecodeValue *src) case VariablesResolver::Visibility::LOCAL: { auto *value = [&] { if (auto it = m_stack.top().locals.find(name); it != m_stack.top().locals.end()) { - ASSERT(std::holds_alternative(it->second)) + ASSERT(std::holds_alternative(it->second)); return std::get(it->second); } else { auto *value = create_stack_value(); @@ -309,7 +309,7 @@ void BytecodeGenerator::store_name(const std::string &name, BytecodeValue *src) case VariablesResolver::Visibility::FREE: { auto *value = [&]() -> BytecodeFreeValue * { if (auto it = m_stack.top().locals.find(name); it != m_stack.top().locals.end()) { - ASSERT(std::holds_alternative(it->second)) + ASSERT(std::holds_alternative(it->second)); return std::get(it->second); } else { auto *value = create_free_value(name); @@ -354,7 +354,7 @@ BytecodeValue *BytecodeGenerator::load_var(const std::string &name) case VariablesResolver::Visibility::LOCAL: { auto *value = [&]() -> BytecodeStackValue * { if (auto it = m_stack.top().locals.find(name); it != m_stack.top().locals.end()) { - ASSERT(std::holds_alternative(it->second)) + ASSERT(std::holds_alternative(it->second)); return std::get(it->second); } else { auto *value = create_stack_value(); @@ -368,7 +368,7 @@ BytecodeValue *BytecodeGenerator::load_var(const std::string &name) case VariablesResolver::Visibility::FREE: { ASSERT(m_stack.top().locals.contains(name)); const auto &l = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(l)) + ASSERT(std::holds_alternative(l)); ASSERT(std::get(l)->get_name() == name); emit( dst->get_register(), std::get(l)->get_free_var_index()); @@ -414,7 +414,7 @@ void BytecodeGenerator::delete_var(const std::string &name) case VariablesResolver::Visibility::LOCAL: { auto *value = [&]() -> BytecodeStackValue * { if (auto it = m_stack.top().locals.find(name); it != m_stack.top().locals.end()) { - ASSERT(std::holds_alternative(it->second)) + ASSERT(std::holds_alternative(it->second)); return std::get(it->second); } else { auto *value = create_stack_value(); @@ -466,7 +466,7 @@ BytecodeNameValue *BytecodeGenerator::load_name(const std::string &name, size_t Value *BytecodeGenerator::visit(const Name *node) { - ASSERT(node->ids().size() == 1) + ASSERT(node->ids().size() == 1); if (node->context_type() == ContextType::LOAD) { return load_var(node->ids()[0]); } else if (node->context_type() == ContextType::DELETE) { @@ -583,7 +583,7 @@ Value *BytecodeGenerator::generate_function(const FunctionType *node) decorator_functions.reserve(node->decorator_list().size()); for (const auto &decorator_function : node->decorator_list()) { auto *f = generate(decorator_function.get(), m_function_id); - ASSERT(f) + ASSERT(f); decorator_functions.push_back(f); } @@ -595,7 +595,7 @@ Value *BytecodeGenerator::generate_function(const FunctionType *node) auto *f = create_function(function_name); const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; const bool is_generator = name_visibility_it->second->is_generator; @@ -772,7 +772,7 @@ Value *BytecodeGenerator::generate_function(const FunctionType *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -844,7 +844,7 @@ Value *BytecodeGenerator::visit(const Lambda *node) auto *f = create_function(function_name); const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; const bool is_generator = name_visibility_it->second->is_generator; @@ -1023,7 +1023,7 @@ Value *BytecodeGenerator::visit(const Lambda *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -1168,7 +1168,7 @@ Value *BytecodeGenerator::visit(const ast::YieldFrom *node) Value *BytecodeGenerator::visit(const Assign *node) { auto *src = generate(node->value().get(), m_function_id); - ASSERT(node->targets().size() > 0) + ASSERT(node->targets().size() > 0); for (const auto &target : node->targets()) { if (auto ast_name = as(target)) { @@ -1278,7 +1278,7 @@ Value *BytecodeGenerator::visit(const Call *node) for (const auto &el : node->keywords()) { if (is_kwargs_expansion(el)) { if (first_kwargs_expansion) { - ASSERT(key_registers.size() == value_registers.size()) + ASSERT(key_registers.size() == value_registers.size()); dict_value = build_dict(key_registers, value_registers); key_registers.clear(); value_registers.clear(); @@ -1301,8 +1301,8 @@ Value *BytecodeGenerator::visit(const Call *node) } } } - ASSERT(first_kwargs_expansion == false) - ASSERT(dict_value) + ASSERT(first_kwargs_expansion == false); + ASSERT(dict_value); keyword_values.push_back(dict_value); } else { // dummy value that will be ignore at runtime, since requires_kwargs_expansion is false @@ -1326,8 +1326,8 @@ Value *BytecodeGenerator::visit(const Call *node) } if (requires_args_expansion || requires_kwargs_expansion) { - ASSERT(arg_values.size() == 1) - ASSERT(keyword_values.size() == 1) + ASSERT(arg_values.size() == 1); + ASSERT(keyword_values.size() == 1); emit(func->get_register(), arg_values[0]->get_register(), @@ -1384,7 +1384,7 @@ Value *BytecodeGenerator::visit(const If *node) { static size_t if_count = 0; - ASSERT(!node->body().empty()) + ASSERT(!node->body().empty()); auto orelse_start_label = make_label(fmt::format("ORELSE_{}", if_count), m_function_id); auto end_label = make_label(fmt::format("IF_END_{}", if_count++), m_function_id); @@ -1411,7 +1411,7 @@ Value *BytecodeGenerator::visit(const For *node) { static size_t for_loop_count = 0; - ASSERT(!node->body().empty()) + ASSERT(!node->body().empty()); auto forloop_start_label = make_label(fmt::format("FOR_START_{}", for_loop_count), m_function_id); @@ -1498,7 +1498,7 @@ Value *BytecodeGenerator::visit(const While *node) { static size_t while_loop_count = 0; - ASSERT(!node->body().empty()) + ASSERT(!node->body().empty()); auto while_loop_start_label = make_label(fmt::format("WHILE_START_{}", while_loop_count), m_function_id); @@ -1653,7 +1653,7 @@ Value *BytecodeGenerator::visit(const ClassDefinition *node) mangle_namespace(m_stack), node->name(), node->source_location()); const auto &name_visibility_it = m_variable_visibility.find(class_mangled_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &class_scope = name_visibility_it->second; auto *class_builder_func = create_function(class_mangled_name); @@ -1750,7 +1750,7 @@ Value *BytecodeGenerator::visit(const ClassDefinition *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -1816,7 +1816,7 @@ Value *BytecodeGenerator::visit(const ClassDefinition *node) Value *BytecodeGenerator::visit(const Dict *node) { - ASSERT(node->keys().size() == node->values().size()) + ASSERT(node->keys().size() == node->values().size()); std::vector> key_registers; std::vector value_registers; @@ -1880,7 +1880,7 @@ Value *BytecodeGenerator::visit(const AugAssign *node) { auto *lhs = [this, node]() { if (auto named_target = as(node->target())) { - ASSERT(named_target->context_type() == ContextType::STORE) + ASSERT(named_target->context_type() == ContextType::STORE); if (named_target->ids().size() != 1) { TODO(); } return load_var(named_target->ids()[0]); } else if (auto attr = as(node->target())) { @@ -2132,7 +2132,7 @@ Value *BytecodeGenerator::visit(const Subscript *node) Value *BytecodeGenerator::visit(const Raise *node) { if (node->cause()) { - ASSERT(node->exception()) + ASSERT(node->exception()); const auto *exception = generate(node->exception().get(), m_function_id); const auto *cause = generate(node->cause().get(), m_function_id); emit(exception->get_register(), cause->get_register()); @@ -2221,7 +2221,7 @@ Value *BytecodeGenerator::visit(const WithItem *node) if (auto optional_vars = node->optional_vars()) { if (auto name = as(optional_vars)) { - ASSERT(as(optional_vars)->ids().size() == 1) + ASSERT(as(optional_vars)->ids().size() == 1); store_name(as(optional_vars)->ids()[0], enter_result); } else if (auto tuple = as(optional_vars)) { (void)tuple; @@ -2250,7 +2250,7 @@ Value *BytecodeGenerator::visit(const IfExpr *node) auto *test_result = generate(node->test().get(), m_function_id); emit(test_result->get_register(), orelse_start_label); auto *if_result = generate(node->body().get(), m_function_id); - ASSERT(if_result) + ASSERT(if_result); emit(return_value->get_register(), if_result->get_register()); emit(end_label); @@ -2308,7 +2308,7 @@ Value *BytecodeGenerator::visit(const Try *node) emit(); if (!node->orelse().empty()) { - ASSERT(orelse_label) + ASSERT(orelse_label); emit(orelse_label); } else { emit(finally_label); @@ -2490,9 +2490,9 @@ Value *BytecodeGenerator::visit(const Pass *) { return nullptr; } Value *BytecodeGenerator::visit(const NamedExpr *node) { - ASSERT(as(node->target())) - ASSERT(as(node->target())->context_type() == ContextType::STORE) - ASSERT(as(node->target())->ids().size() == 1) + ASSERT(as(node->target())); + ASSERT(as(node->target())->context_type() == ContextType::STORE); + ASSERT(as(node->target())->ids().size() == 1); auto *dst = create_value(); auto *src = generate(node->value().get(), m_function_id); @@ -2594,7 +2594,7 @@ std::tuple>, std::vector(dst->get_register(), it->get_register(), end_label); if (node->target()->node_type() == ASTNodeType::Name) { const auto name = std::static_pointer_cast(node->target()); - ASSERT(name->ids().size() == 1) + ASSERT(name->ids().size() == 1); store_name(name->ids()[0], dst); } else if (auto target = as(node->target())) { std::vector unpack_dst; @@ -2660,7 +2660,7 @@ Value *BytecodeGenerator::visit(const ListComp *node) auto *list = build_list({}); auto [start_labels, end_labels] = visit_comprehension(node->generators()); auto *element = generate(node->elt().get(), m_function_id); - ASSERT(element) + ASSERT(element); emit(list->get_register(), element->get_register()); ASSERT(start_labels.size() == end_labels.size()); while (!start_labels.empty()) { @@ -2685,7 +2685,7 @@ Value *BytecodeGenerator::visit(const ListComp *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -2701,7 +2701,7 @@ Value *BytecodeGenerator::visit(const ListComp *node) "comprehension_iterator", }; const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; for (const auto &symbol : symbol_map.symbols) { const auto &varname = symbol.name; @@ -2788,7 +2788,7 @@ Value *BytecodeGenerator::visit(const DictComp *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -2804,7 +2804,7 @@ Value *BytecodeGenerator::visit(const DictComp *node) "comprehension_iterator", }; const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; for (const auto &symbol : symbol_map.symbols) { const auto &varname = symbol.name; @@ -2893,7 +2893,7 @@ Value *BytecodeGenerator::visit(const GeneratorExp *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -2909,7 +2909,7 @@ Value *BytecodeGenerator::visit(const GeneratorExp *node) "comprehension_iterator", }; const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; for (const auto &symbol : symbol_map.symbols) { const auto &varname = symbol.name; @@ -2969,7 +2969,7 @@ Value *BytecodeGenerator::visit(const SetComp *node) auto *set = build_set({}); auto [start_labels, end_labels] = visit_comprehension(node->generators()); auto *element = generate(node->elt().get(), m_function_id); - ASSERT(element) + ASSERT(element); emit(set->get_register(), element->get_register()); ASSERT(start_labels.size() == end_labels.size()); while (!start_labels.empty()) { @@ -2994,7 +2994,7 @@ Value *BytecodeGenerator::visit(const SetComp *node) for (const auto &[name, el] : captures) { ASSERT(m_stack.top().locals.contains(name)); const auto &value = m_stack.top().locals.at(name); - ASSERT(std::holds_alternative(value)) + ASSERT(std::holds_alternative(value)); emit(el->get_free_var_index(), std::get(value)->get_free_var_index()); capture_regs.push_back(el->get_free_var_index()); @@ -3010,7 +3010,7 @@ Value *BytecodeGenerator::visit(const SetComp *node) "comprehension_iterator", }; const auto &name_visibility_it = m_variable_visibility.find(function_name); - ASSERT(name_visibility_it != m_variable_visibility.end()) + ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; for (const auto &symbol : symbol_map.symbols) { const auto &varname = symbol.name; @@ -3080,7 +3080,7 @@ BytecodeGenerator::~BytecodeGenerator() {} void BytecodeGenerator::exit_function(size_t function_id) { - ASSERT(function_id < m_functions.functions.size()) + ASSERT(function_id < m_functions.functions.size()); auto function = std::next(m_functions.functions.begin(), function_id); function->metadata.register_count = register_count(); function->metadata.stack_size = stack_variable_count() + free_variable_count(); @@ -3116,15 +3116,15 @@ void BytecodeGenerator::relocate_labels(const FunctionBlocks &functions) std::shared_ptr BytecodeGenerator::generate_executable(std::string filename, std::vector argv) { - ASSERT(m_frame_stack_value_count.size() == 2) - ASSERT(m_frame_free_var_count.size() == 2) + ASSERT(m_frame_stack_value_count.size() == 2); + ASSERT(m_frame_free_var_count.size() == 2); relocate_labels(m_functions); return BytecodeProgram::create(std::move(m_functions), filename, argv); } InstructionVector *BytecodeGenerator::allocate_block(size_t function_id) { - ASSERT(function_id < m_functions.functions.size()) + ASSERT(function_id < m_functions.functions.size()); auto function = std::next(m_functions.functions.begin(), function_id); return &function->blocks; @@ -3135,7 +3135,7 @@ std::shared_ptr BytecodeGenerator::compile(std::shared_ptr compiler::OptimizationLevel lvl) { auto module = as(node); - ASSERT(module) + ASSERT(module); if (lvl > compiler::OptimizationLevel::None) { ast::optimizer::constant_folding(node); } diff --git a/src/executable/bytecode/codegen/BytecodeGenerator.hpp b/src/executable/bytecode/codegen/BytecodeGenerator.hpp index 0cb3806..4bd2caa 100644 --- a/src/executable/bytecode/codegen/BytecodeGenerator.hpp +++ b/src/executable/bytecode/codegen/BytecodeGenerator.hpp @@ -141,14 +141,14 @@ class BytecodeGenerator : public ast::CodeGenerator const std::shared_ptr