From: Peter Johnson Date: Thu, 1 Jun 2006 06:40:12 +0000 (-0000) Subject: Fix use of arithmetic with single-character constants in data declarations. X-Git-Tag: v0.5.0~6^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2fd5d202856be289b126ae525fa5659fd1a37fa3;p=yasm Fix use of arithmetic with single-character constants in data declarations. Multi-character constants are still not valid, as they are somewhat difficult to handle without adding some sort of string handling into yasm_expr. This fix may break big-endian targets (we don't have any yet). * nasm-bison.y: Add new token ONECHARSTR. Add string rule that takes STRING and ONECHARSTR; use this instead of STRING everywhere but dataval. In dvexpr, take ONECHARSTR as character constant. * nasm-token.re: Generate ONECHARSTR for one-character string contants. Reported by: Robert Riebisch svn path=/trunk/yasm/; revision=1548 --- diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index ab5c0b67..d874f2ed 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -91,7 +91,7 @@ static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name, %token INTNUM %token FLTNUM %token DIRECTIVE_NAME FILENAME -%token STRING +%token STRING ONECHARSTR %token SIZE_OVERRIDE %token DECLARE_DATA %token RESERVE_SPACE @@ -114,6 +114,7 @@ static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name, %type directive_valparam %type operands %type operand +%type string %left ':' %left WRT @@ -202,19 +203,19 @@ exp: instr | RESERVE_SPACE expr { $$ = yasm_bc_create_reserve($2, $1/8, cur_line); } - | INCBIN STRING { + | INCBIN string { $$ = yasm_bc_create_incbin($2.contents, NULL, NULL, cur_line); } - | INCBIN STRING ',' { + | INCBIN string ',' { $$ = yasm_bc_create_incbin($2.contents, NULL, NULL, cur_line); } - | INCBIN STRING ',' expr { + | INCBIN string ',' expr { $$ = yasm_bc_create_incbin($2.contents, $4, NULL, cur_line); } - | INCBIN STRING ',' expr ',' { + | INCBIN string ',' expr ',' { $$ = yasm_bc_create_incbin($2.contents, $4, NULL, cur_line); } - | INCBIN STRING ',' expr ',' expr { + | INCBIN string ',' expr ',' expr { $$ = yasm_bc_create_incbin($2.contents, $4, $6, cur_line); } ; @@ -317,7 +318,7 @@ directive_valparam: direxpr { $$ = yasm_vp_create(NULL, $1); } } - | STRING { $$ = yasm_vp_create($1.contents, NULL); } + | string { $$ = yasm_vp_create($1.contents, NULL); } | ID '=' direxpr { yasm_expr__traverse_leaves_in($3, parser_nasm, fix_directive_symrec); $$ = yasm_vp_create($1, $3); @@ -394,6 +395,11 @@ direxpr: INTNUM { $$ = p_expr_new_ident(yasm_expr_int($1)); } dvexpr: INTNUM { $$ = p_expr_new_ident(yasm_expr_int($1)); } | FLTNUM { $$ = p_expr_new_ident(yasm_expr_float($1)); } + | ONECHARSTR { + $$ = p_expr_new_ident(yasm_expr_int( + yasm_intnum_create_charconst_nasm($1.contents))); + yasm_xfree($1.contents); + } | explabel { $$ = p_expr_new_ident(yasm_expr_sym($1)); } /*| dvexpr '||' dvexpr { $$ = p_expr_new_tree($1, YASM_EXPR_LOR, $3); }*/ | dvexpr '|' dvexpr { $$ = p_expr_new_tree($1, YASM_EXPR_OR, $3); } @@ -426,12 +432,12 @@ dvexpr: INTNUM { $$ = p_expr_new_ident(yasm_expr_int($1)); } /* Expressions for operands and memory expressions. * We don't attempt to check memory expressions for validity here. - * Essentially the same as expr_no_string above but adds REG and STRING. + * Essentially the same as dvexpr above but adds REG and string. */ expr: INTNUM { $$ = p_expr_new_ident(yasm_expr_int($1)); } | FLTNUM { $$ = p_expr_new_ident(yasm_expr_float($1)); } | REG { $$ = p_expr_new_ident(yasm_expr_reg($1[0])); } - | STRING { + | string { $$ = p_expr_new_ident(yasm_expr_int( yasm_intnum_create_charconst_nasm($1.contents))); yasm_xfree($1.contents); @@ -491,6 +497,8 @@ explabel: ID { } ; +string: STRING | ONECHARSTR; + %% /*@=usedef =nullassign =memtrans =usereleased =compdef =mustfree@*/ diff --git a/modules/parsers/nasm/nasm-token.re b/modules/parsers/nasm/nasm-token.re index b529e350..4030e0d9 100644 --- a/modules/parsers/nasm/nasm-token.re +++ b/modules/parsers/nasm/nasm-token.re @@ -542,6 +542,8 @@ stringconst_scan: lvalp->str.len = count; if (parser_nasm->save_input && cursor != s->eof) cursor = save_line(parser_nasm, cursor); + if (count == 1) + RETURN(ONECHARSTR); RETURN(STRING); } @@ -550,6 +552,8 @@ stringconst_scan: strbuf[count] = '\0'; lvalp->str.contents = (char *)strbuf; lvalp->str.len = count; + if (count == 1) + RETURN(ONECHARSTR); RETURN(STRING); } diff --git a/modules/parsers/nasm/tests/Makefile.inc b/modules/parsers/nasm/tests/Makefile.inc index a4351798..4c9fdf8f 100644 --- a/modules/parsers/nasm/tests/Makefile.inc +++ b/modules/parsers/nasm/tests/Makefile.inc @@ -9,6 +9,9 @@ EXTRA_DIST += modules/parsers/nasm/tests/alignnop16.hex EXTRA_DIST += modules/parsers/nasm/tests/alignnop32.asm EXTRA_DIST += modules/parsers/nasm/tests/alignnop32.errwarn EXTRA_DIST += modules/parsers/nasm/tests/alignnop32.hex +EXTRA_DIST += modules/parsers/nasm/tests/charconstmath.asm +EXTRA_DIST += modules/parsers/nasm/tests/charconstmath.errwarn +EXTRA_DIST += modules/parsers/nasm/tests/charconstmath.hex EXTRA_DIST += modules/parsers/nasm/tests/endcomma.asm EXTRA_DIST += modules/parsers/nasm/tests/endcomma.errwarn EXTRA_DIST += modules/parsers/nasm/tests/endcomma.hex diff --git a/modules/parsers/nasm/tests/charconstmath.asm b/modules/parsers/nasm/tests/charconstmath.asm new file mode 100644 index 00000000..b673bab6 --- /dev/null +++ b/modules/parsers/nasm/tests/charconstmath.asm @@ -0,0 +1 @@ +db "string", " "+80h diff --git a/modules/parsers/nasm/tests/charconstmath.errwarn b/modules/parsers/nasm/tests/charconstmath.errwarn new file mode 100644 index 00000000..e69de29b diff --git a/modules/parsers/nasm/tests/charconstmath.hex b/modules/parsers/nasm/tests/charconstmath.hex new file mode 100644 index 00000000..199e34ff --- /dev/null +++ b/modules/parsers/nasm/tests/charconstmath.hex @@ -0,0 +1,7 @@ +73 +74 +72 +69 +6e +67 +a0