Skip to content

Commit b804706

Browse files
committed
Optimize C __builtin_strlen for literals
1 parent 474dbd4 commit b804706

File tree

8 files changed

+14
-7
lines changed

8 files changed

+14
-7
lines changed

Changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Version 6.1.6
33
- Implemented flush() for FATFS files (thanks to Ada)
44
- Improved some error messages
55
- In C++ output, treat unknown types in prints as generic
6-
- Optimized a few BASIC string expressions
6+
- Optimized a few BASIC string expressions
7+
- Optimized calculating length of string literals
78
- Revised BASIC use of + operator on strings
89

910
Version 6.1.5

ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ static void doASTDump(AST *ast, int indent)
10131013
case K_ASC:
10141014
opString = "asc";
10151015
break;
1016-
case K_LEN:
1016+
case K_STRLEN:
10171017
opString = "strlen";
10181018
break;
10191019
case K_POWER:

frontends/basic/basic.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ unary_op:
16421642
| BAS_ASC
16431643
{ $$ = AstOperator(K_ASC, NULL, NULL); }
16441644
| BAS_LEN
1645-
{ $$ = AstOperator(K_LEN, NULL, NULL); }
1645+
{ $$ = AstOperator(K_STRLEN, NULL, NULL); }
16461646
| BAS_SQRT
16471647
{ $$ = AstOperator(K_SQRT, NULL, NULL); }
16481648
;

frontends/c/cgram.y

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,8 @@ ConstructDefaultValue(AST *decl, AST *val)
10621062
%token C_BUILTIN_SETJMP "__builtin_setjmp"
10631063
%token C_BUILTIN_LONGJMP "__builtin_longjmp"
10641064

1065+
%token C_BUILTIN_STRLEN "__builtin_strlen"
1066+
10651067
%token C_EOF "end of file"
10661068

10671069
%start translation_unit
@@ -1254,6 +1256,10 @@ postfix_expression
12541256
{
12551257
$$ = NewAST(AST_THROW, $5, $3);
12561258
}
1259+
| C_BUILTIN_STRLEN '(' assignment_expression ')'
1260+
{
1261+
$$ = AstOperator(K_STRLEN, NULL, $3);
1262+
}
12571263
| postfix_expression '[' expression ']'
12581264
{ $$ = NewAST(AST_ARRAYREF, $1, $3); }
12591265
| postfix_expression '(' ')'

frontends/common.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ Aliases basicalias[] = {
253253
{ "getus", "_getus" },
254254
{ "getsec", "_getsec" },
255255
{ "kill", "_remove" },
256-
{ "len", "__builtin_strlen" },
257256
{ "lpeek", "__lpeek" },
258257
{ "lpoke", "__lpoke" },
259258
{ "mount", "_mount" },

frontends/lexer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,7 @@ struct reservedword c_keywords[] = {
24052405
{ "__builtin_propeller_rev", C_BUILTIN_REV },
24062406
{ "__builtin_setjmp", C_BUILTIN_SETJMP },
24072407
{ "__builtin_sqrt", C_BUILTIN_SQRT },
2408+
{ "__builtin_strlen", C_BUILTIN_STRLEN },
24082409
{ "__builtin_va_arg", C_BUILTIN_VA_ARG },
24092410
{ "__builtin_va_start", C_BUILTIN_VA_START },
24102411
{ "case", C_CASE },

frontends/types.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
11281128
}
11291129
}
11301130
return ast_type_long;
1131-
case K_LEN:
1131+
case K_STRLEN:
11321132
if (!CompatibleTypes(righttype, ast_type_string)) {
11331133
if (curfunc && IsBasicLang(curfunc->language)) {
11341134
ERROR(ast, "expected string argument to LEN");

optokens.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ enum OpToken {
4242
K_GTU,
4343
K_GEU,
4444

45-
K_ASC, /* BASIC ASC operator */
46-
K_LEN, /* BASIC LEN operator */
45+
K_ASC, /* BASIC ASC operator */
46+
K_STRLEN, /* BASIC LEN operator */
4747

4848
K_LIMITMIN_UNS,
4949
K_LIMITMAX_UNS,

0 commit comments

Comments
 (0)