From 9dce2a35e32bd8eb784a61d310acc72c52a15129 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Tue, 16 Jul 2013 23:10:10 +0200
Subject: [PATCH] Compiler: Avoid folding constants at all in __INIT().

When constants are folded the semantics for __INIT()
changes, and can break code (like eg variables in Hilfe).

Fixes several testsuite failures for Hilfe.
---
 src/language.yacc | 40 ++++++++++++----------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)

diff --git a/src/language.yacc b/src/language.yacc
index 9c98b677f5..dfe67d4f83 100644
--- a/src/language.yacc
+++ b/src/language.yacc
@@ -1707,39 +1707,23 @@ new_name: optional_stars TOK_IDENTIFIER
   }
   expr0
   {
-    if (!TEST_COMPAT(7, 8) && ($5) && is_const($5) &&
-	!($5->tree_info & OPT_EXTERNAL_DEPEND) &&
+    if ((Pike_compiler->compiler_pass == 2) &&
+	!TEST_COMPAT(7, 8) && ($5) && ($5->token == F_CONSTANT) &&
 	!Pike_compiler->num_parse_error) {
-      /* Attempt to evaluate it to see if it is zero,
-       * in which case we can throw it away.
+      /* Check if it is zero, in which case we can throw it away.
        *
        * NB: The compat test is due to that this changes the semantics
        *     of calling __INIT() by hand.
        */
-      ptrdiff_t tmp = eval_low($5, 0);
-      if (tmp >= 1) {
-	if ((TYPEOF(Pike_sp[-tmp]) != PIKE_T_PROGRAM) ||
-	    ((Pike_sp[-tmp].u.program->flags &
-	      (PROGRAM_NEEDS_PARENT|PROGRAM_USES_PARENT|PROGRAM_FINISHED)) ==
-	     PROGRAM_FINISHED)) {
-	  /* NB: We cannot perform constant folding on programs that
-	   *     need their parent here, since the parent will be lost.
-	   *     Note also that the PROGRAM_{NEEDS,USES}_PARENT flags
-	   *     don't get set until pass 2.
-	   */
-	  free_node($5);
-	  $5 = NULL;
-	  if (!SAFE_IS_ZERO(Pike_sp - tmp) ||
-	      IDENTIFIER_IS_ALIAS(ID_FROM_INT(Pike_compiler->new_program,
-					      $<number>4)->identifier_flags)) {
-	    /* NB: Inherited variables get converted into aliases by
-	     *     define_variable, and we need to support clearing
-	     *     of inherited variables.
-	     */
-	    $5 = mkconstantsvaluenode(Pike_sp - tmp);
-	  }
-	}
-	pop_n_elems(tmp);
+      if (SAFE_IS_ZERO(&$5->u.sval) &&
+	  !IDENTIFIER_IS_ALIAS(ID_FROM_INT(Pike_compiler->new_program,
+					   $<number>4)->identifier_flags)) {
+	/* NB: Inherited variables get converted into aliases by
+	 *     define_variable, and we need to support clearing
+	 *     of inherited variables.
+	 */
+	free_node($5);
+	$5 = NULL;
       }
     }
     if ($5) {
-- 
GitLab