https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113889
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Gaius Mulley <ga...@gcc.gnu.org>: https://gcc.gnu.org/g:78b72ee5a80f45bd761a55006e2b3fc2cbe749bc commit r14-9063-g78b72ee5a80f45bd761a55006e2b3fc2cbe749bc Author: Gaius Mulley <gaiusm...@gmail.com> Date: Mon Feb 19 12:59:36 2024 +0000 PR modula2/113889 Incorrect constant string value if declared in a definition module This patch fixes a bug exposed when a constant string is declared in a definition module and imported by a program module. The bug fix was to defer the string assignment and concatenation until quadruples were generated. The conststring symbol has a known field which must be checked prior to retrieving the string contents. gcc/m2/ChangeLog: PR modula2/113889 * gm2-compiler/M2ALU.mod (StringFitsArray): Add tokeno parameter to GetStringLength. (InitialiseArrayOfCharWithString): Add tokeno parameter to GetStringLength. (CheckGetCharFromString): Add tokeno parameter to GetStringLength. * gm2-compiler/M2Const.mod (constResolveViaMeta): Replace PutConstString with PutConstStringKnown. * gm2-compiler/M2GCCDeclare.mod (DeclareCharConstant): Add tokenno parameter and add assert. Use tokenno to generate location. (DeclareStringConstant): Add tokenno and add asserts. Add tokenno parameter to calls to GetStringLength. (PromoteToString): Add assert and add tokenno parameter to GetStringLength. (PromoteToCString): Add assert and add tokenno parameter to GetStringLength. (DeclareConstString): New procedure function. (TryDeclareConst): Remove size local variable. Check IsConstStringKnown. Call DeclareConstString. (PrintString): New procedure. (PrintVerboseFromList): Call PrintString. (CheckResolveSubrange): Check IsConstStringKnown before creating subrange for char or issuing an error. * gm2-compiler/M2GenGCC.mod (ResolveConstantExpressions): Add StringLengthOp, StringConvertM2nulOp, StringConvertCnulOp case clauses. (FindSize): Add assert IsConstStringKnown. (StringToChar): New variable tokenno. Add tokenno parameter to GetStringLength. (FoldStringLength): New procedure. (FoldStringConvertM2nul): New procedure. (FoldStringConvertCnul): New procedure. (CodeAddr): Add tokenno parameter. Replace CurrentQuadToken with tokenno. Add tokenno parameter to GetStringLength. (PrepareCopyString): Rewrite. (IsConstStrKnown): New procedure function. (FoldAdd): Detect conststring op2 and op3 which are known and concat. Place result into op1. (FoldStandardFunction): Pass tokenno as a parameter to GetStringLength. (CodeXIndr): Rewrite comment. Rename op1 to left, op3 to right. Pass rightpos to GetStringLength. * gm2-compiler/M2Quads.def (QuadrupleOp): Add StringConvertCnulOp, StringConvertM2nulOp and StringLengthOp. * gm2-compiler/M2Quads.mod (import): Remove MakeConstLitString. Add CopyConstString and PutConstStringKnown. (IsInitialisingConst): Add StringConvertCnulOp, StringConvertM2nulOp and StringLengthOp. (callRequestDependant): Replace MakeConstLitString with MakeConstString. (DeferMakeConstStringCnul): New procedure function. (DeferMakeConstStringM2nul): New procedure function. (CheckParameter): Add early return if the string const is unknown. (DescribeType): Add token parameter to GetStringLength. Check for IsConstStringKnown. (ManipulateParameters): Use DeferMakeConstStringCnul and DeferMakeConstStringM2nul. (MakeLengthConst): Remove and replace with... (DeferMakeLengthConst): ... this. (doBuildBinaryOp): Create ConstString and set it to contents unknown. Check IsConstStringKnown before generating error message. (WriteQuad): Add StringConvertCnulOp, StringConvertM2nulOp and StringLengthOp. (WriteOperator): Add StringConvertCnulOp, StringConvertM2nulOp and StringLengthOp. * gm2-compiler/M2SymInit.mod (CheckReadBeforeInitQuad): Add StringConvertCnulOp, StringConvertM2nulOp and StringLengthOp. * gm2-compiler/NameKey.mod (LengthKey): Allow NulName to return 0. * gm2-compiler/P2SymBuild.mod (BuildString): Replace MakeConstLitString with MakeConstString. (DetermineType): Replace PutConstString with PutConstStringKnown. * gm2-compiler/SymbolTable.def (MakeConstVar): Tidy up comment. (MakeConstLitString): Remove. (MakeConstString): New procedure function. (MakeConstStringCnul): New procedure function. (MakeConstStringM2nul): New procedure function. (PutConstStringKnown): New procedure. (CopyConstString): New procedure. (IsConstStringKnown): New procedure function. (IsConstStringM2): New procedure function. (IsConstStringC): New procedure function. (IsConstStringM2nul): New procedure function. (IsConstStringCnul): New procedure function. (GetStringLength): Add token parameter. (PutConstString): Remove. (GetConstStringM2): Remove. (GetConstStringC): Remove. (GetConstStringM2nul): Remove. (GetConstStringCnul): Remove. (MakeConstStringC): Remove. * gm2-compiler/SymbolTable.mod (SymConstString): Remove M2Variant, NulM2Variant, CVariant, NulCVariant. Add Known. (CheckAnonymous): Replace $$ with __anon. (IsNameAnonymous): Replace $$ with __anon. (MakeConstVar): Detect whether the name is nul and treat as a temporary constant. (MakeConstLitString): Remove. (BackFillString): Remove. (InitConstString): Rewrite. (GetConstStringM2): Remove. (GetConstStringC): Remove. (GetConstStringContent): New procedure function. (GetConstStringM2nul): Remove. (GetConstStringCnul): Remove. (MakeConstStringCnul): Rewrite. (MakeConstStringM2nul): Rewrite. (MakeConstStringC): Remove. (MakeConstString): Rewrite. (PutConstStringKnown): New procedure. (CopyConstString): New procedure. (PutConstString): Remove. (IsConstStringKnown): New procedure function. (IsConstStringM2): New procedure function. (IsConstStringC): Rewrite. (IsConstStringM2nul): Rewrite. (IsConstStringCnul): Rewrite. (GetConstStringKind): New procedure function. (GetString): Check Known. (GetStringLength): Add token parameter and check Known. gcc/testsuite/ChangeLog: PR modula2/113889 * gm2/pim/run/pass/pim-run-pass.exp: Add filter for constdef.mod. * gm2/extensions/run/pass/callingc2.mod: New test. * gm2/extensions/run/pass/callingc3.mod: New test. * gm2/extensions/run/pass/callingc4.mod: New test. * gm2/extensions/run/pass/callingc5.mod: New test. * gm2/extensions/run/pass/callingc6.mod: New test. * gm2/extensions/run/pass/callingc7.mod: New test. * gm2/extensions/run/pass/callingc8.mod: New test. * gm2/extensions/run/pass/fixedarray.mod: New test. * gm2/extensions/run/pass/fixedarray2.mod: New test. * gm2/pim/run/pass/constdef.def: New test. * gm2/pim/run/pass/constdef.mod: New test. * gm2/pim/run/pass/testimportconst.mod: New test. Signed-off-by: Gaius Mulley <gaiusm...@gmail.com>