# HG changeset patch # User Jonas Hultén # Date 1634793096 -7200 # Thu Oct 21 07:11:36 2021 +0200 # Branch develop # Node ID 2ce5e872d50b8845961962ad2001c1910edb038d # Parent 22b66d5eecb99df6c2eeca5e2bfe57201f8a062c Fixed a crash when a symbol definition referred to itself. diff --git a/jasm/assemble/assembler_impl/symbols_impl.cpp b/jasm/assemble/assembler_impl/symbols_impl.cpp --- a/jasm/assemble/assembler_impl/symbols_impl.cpp +++ b/jasm/assemble/assembler_impl/symbols_impl.cpp @@ -663,11 +663,25 @@ // and previous pass references must be allowed for constants. if (variable_reference != nullptr) { - value.type = ValueType::ValueReference; - value.set_found_in_current_pass(found_in_current_pass); - value.variable_reference_hash = combined_hash; - value.type_hash = _static_value_reference_type; + if (variable_reference->type_hash == 0) { + // This can happen if the symbol is created and used in the same expression. + // So the variable hasn't been properly initialized yet. + set_unknown(value); + value.global = true; + value.storage_type = StorageType::Constant; // Not entirely sure what to set this to. Type must be checked before this. + + if (generate) { + std::stringstream ss; + ss << "A symbol's definition can't refer to itself."; + report_error(component.source_location, AssemblyErrorCodes::SymbolRecursion, ss.str()); + } + } else { + value.type = ValueType::ValueReference; + value.set_found_in_current_pass(found_in_current_pass); + value.variable_reference_hash = combined_hash; + value.type_hash = _static_value_reference_type; + } } else { // symbol was not found or is ambiguous diff --git a/jasm/exceptions/error_codes.h b/jasm/exceptions/error_codes.h --- a/jasm/exceptions/error_codes.h +++ b/jasm/exceptions/error_codes.h @@ -207,6 +207,7 @@ AddressingModeRequiresBitSizeArgument, MacroValueExpected, ExpectedBooleanResult, + SymbolRecursion, // assembler warnings SubroutineFallthrough = 3500, diff --git a/jasm/unit_tests/results/test_self_assignment_constant.stdout b/jasm/unit_tests/results/test_self_assignment_constant.stdout new file mode 100644 --- /dev/null +++ b/jasm/unit_tests/results/test_self_assignment_constant.stdout @@ -0,0 +1,2 @@ +unit_tests/test_self_assignment_constant.asm(3,11) : Error 3116 : A symbol's definition can't refer to itself. +Assembly ended with errors. diff --git a/jasm/unit_tests/results/test_self_assignment_variable.stdout b/jasm/unit_tests/results/test_self_assignment_variable.stdout new file mode 100644 --- /dev/null +++ b/jasm/unit_tests/results/test_self_assignment_variable.stdout @@ -0,0 +1,2 @@ +unit_tests/test_self_assignment_variable.asm(3,9) : Error 3116 : A symbol's definition can't refer to itself. +Assembly ended with errors. diff --git a/jasm/unit_tests/test_self_assignment_constant.asm b/jasm/unit_tests/test_self_assignment_constant.asm new file mode 100644 --- /dev/null +++ b/jasm/unit_tests/test_self_assignment_constant.asm @@ -0,0 +1,7 @@ +// assembler command line arguments: 65c02 [-v0] + +const q = q + +section code, "main", 0, 100 +{ +} diff --git a/jasm/unit_tests/test_self_assignment_variable.asm b/jasm/unit_tests/test_self_assignment_variable.asm new file mode 100644 --- /dev/null +++ b/jasm/unit_tests/test_self_assignment_variable.asm @@ -0,0 +1,7 @@ +// assembler command line arguments: 65c02 [-v0] + +var q = q + +section code, "main", 0, 100 +{ +}