From 1a4e2edede25957e373fc2b15d0f9229e2fd0aa0 Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 20 Mar 2010 18:51:59 +0000 Subject: [PATCH] Reenable compile time evaluation of strlen for string literals. git-svn-id: svn://svn.cc65.org/cc65/trunk@4631 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/expr.c | 5 ++--- src/cc65/exprdesc.c | 3 ++- src/cc65/exprdesc.h | 8 ++++++-- src/cc65/stdfunc.c | 12 ++++++------ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index ff5cde4f0..343ecdfed 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -578,7 +578,6 @@ static void Primary (ExprDesc* E) /* This is the lowest level of the expression parser. */ { SymEntry* Sym; - Literal* L; /* Initialize fields in the expression stucture */ ED_Init (E); @@ -749,11 +748,11 @@ static void Primary (ExprDesc* E) case TOK_SCONST: case TOK_WCSCONST: /* String literal */ - L = UseLiteral (CurTok.SVal); + E->LVal = UseLiteral (CurTok.SVal); E->Type = GetCharArrayType (GetLiteralSize (CurTok.SVal)); E->Flags = E_LOC_LITERAL | E_RTYPE_RVAL; E->IVal = 0; - E->Name = GetLiteralLabel (CurTok.SVal); + E->Name = GetLiteralLabel (CurTok.SVal); NextToken (); break; diff --git a/src/cc65/exprdesc.c b/src/cc65/exprdesc.c index 1bde63abf..2615e9dbd 100644 --- a/src/cc65/exprdesc.c +++ b/src/cc65/exprdesc.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2002-2009, Ullrich von Bassewitz */ +/* (C) 2002-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -63,6 +63,7 @@ ExprDesc* ED_Init (ExprDesc* Expr) Expr->Name = 0; Expr->IVal = 0; Expr->FVal = FP_D_Make (0.0); + Expr->LVal = 0; Expr->BitOffs = 0; Expr->BitWidth = 0; return Expr; diff --git a/src/cc65/exprdesc.h b/src/cc65/exprdesc.h index 9de064952..16322fb09 100644 --- a/src/cc65/exprdesc.h +++ b/src/cc65/exprdesc.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2002-2009, Ullrich von Bassewitz */ +/* (C) 2002-2010, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -89,6 +89,9 @@ enum { }; +/* Forward */ +struct Literal; + /* Describe the result of an expression */ typedef struct ExprDesc ExprDesc; struct ExprDesc { @@ -98,6 +101,7 @@ struct ExprDesc { unsigned long Name; /* Name or label number */ long IVal; /* Integer value if expression constant */ Double FVal; /* Floating point value */ + struct Literal* LVal; /* Literal value */ /* Bit field stuff */ unsigned BitOffs; /* Bit offset for bit fields */ @@ -182,7 +186,7 @@ INLINE int ED_IsLocExpr (const ExprDesc* Expr) #if defined(HAVE_INLINE) INLINE int ED_IsLocLiteral (const ExprDesc* Expr) /* Return true if the expression is a string from the literal pool */ -{ +{ return (Expr->Flags & E_MASK_LOC) == E_LOC_LITERAL; } #else diff --git a/src/cc65/stdfunc.c b/src/cc65/stdfunc.c index 0c3e2779d..122434c4c 100644 --- a/src/cc65/stdfunc.c +++ b/src/cc65/stdfunc.c @@ -933,8 +933,7 @@ static void StdFunc_strlen (FuncDesc* F attribute ((unused)), ExprDesc* Expr) /* Do type conversion */ TypeConversion (&Arg, ArgType); - -#if 0 + /* If the expression is a literal, and if string literals are read * only, we can calculate the length of the string and remove it * from the literal pool. Otherwise we have to calculate the length @@ -943,15 +942,16 @@ static void StdFunc_strlen (FuncDesc* F attribute ((unused)), ExprDesc* Expr) if (ED_IsLocLiteral (&Arg) && IS_Get (&WritableStrings) == 0) { /* Constant string literal */ - ED_MakeConstAbs (Expr, GetLiteralSize (GetLiteral (Arg.IVal)), type_size_t); + ED_MakeConstAbs (Expr, GetLiteralSize (Arg.LVal) - 1, type_size_t); + + /* We don't need the literal any longer */ + ReleaseLiteral (Arg.LVal); /* We will inline strlen for arrays with constant addresses, if either the * inlining was forced on the command line, or the array is smaller than * 256, so the inlining is considered safe. */ - } else -#endif - if (ED_IsLocConst (&Arg) && IsArray && + } else if (ED_IsLocConst (&Arg) && IsArray && (IS_Get (&InlineStdFuncs) || IsByteIndex)) { /* Generate the strlen code */