From a90c1347e2de5faf441929834278851dd3e5a7ff Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 29 Nov 2024 10:21:31 -0500 Subject: [PATCH] Fix invalid codegen when returning a static array. Bug: issue #977 Test: new test case --- compiler/code-generator.cpp | 2 +- tests/regressions/return-static-array.out | 1 + tests/regressions/return-static-array.sp | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/regressions/return-static-array.out create mode 100644 tests/regressions/return-static-array.sp diff --git a/compiler/code-generator.cpp b/compiler/code-generator.cpp index cf2683b3..7fba2d3a 100644 --- a/compiler/code-generator.cpp +++ b/compiler/code-generator.cpp @@ -1501,7 +1501,7 @@ CodeGenerator::EmitRvalue(value* lval) auto var = lval->sym->as(); if (var->vclass() == sLOCAL || var->vclass() == sARGUMENT) __ emit(OP_LOAD_S_PRI, var->addr()); - else + else if (!(var->type()->isArray() || var->type()->isEnumStruct())) __ emit(OP_LOAD_PRI, var->addr()); break; } diff --git a/tests/regressions/return-static-array.out b/tests/regressions/return-static-array.out new file mode 100644 index 00000000..ac7ed041 --- /dev/null +++ b/tests/regressions/return-static-array.out @@ -0,0 +1 @@ +asdada \ No newline at end of file diff --git a/tests/regressions/return-static-array.sp b/tests/regressions/return-static-array.sp new file mode 100644 index 00000000..9caf3d0a --- /dev/null +++ b/tests/regressions/return-static-array.sp @@ -0,0 +1,13 @@ +native void print(const char[] x); +#define MAX_LEN 32 + +public void main() { + print(FooterGet()); +} + +char[] FooterGet() +{ + static char buffer[MAX_LEN] = "asdada"; + + return buffer; +}