This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Disallowing wide string literals in asm


This patch makes the C front end reject the use of wide string
literals in 'asm'; they make no sense there.  (The C++ front end
already rejects them.)  I've also made a corresponding change to my
new parser.

Bootstrapped with no regressions on i686-pc-linux-gnu.  This patch was
prepared a month ago but stalled on bug 18164 (and then encountered
bug 18725 on testing after bug 18164 was fixed); presuming that in
these circumstances it isn't now appropriate for mainline, queued for
4.1.

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2004-10-26  Joseph S. Myers  <joseph@codesourcery.com>

	* c-parse.in (asm_string): New.  Don't allow wide strings in
	'asm'.
	(simple_asm_expr, asm_argument, asm_operand, asm_clobbers): Use
	asm_string instead of STRING.

testsuite:
2004-10-26  Joseph S. Myers  <joseph@codesourcery.com>

	* gcc.dg/asm-wide-1.c: New test.

diff -rupN GCC.orig/gcc/c-parse.in GCC/gcc/c-parse.in
--- GCC.orig/gcc/c-parse.in	2004-10-25 23:11:44.000000000 +0000
+++ GCC/gcc/c-parse.in	2004-10-26 15:20:25.000000000 +0000
@@ -209,7 +209,7 @@ do {									\
 %type <ttype> scspec SCSPEC STATIC TYPESPEC TYPE_QUAL maybe_volatile
 %type <ttype> initdecls notype_initdecls initdcl notype_initdcl
 %type <exprtype> init
-%type <ttype> simple_asm_expr maybeasm asm_stmt asm_argument
+%type <ttype> simple_asm_expr maybeasm asm_stmt asm_argument asm_string
 %type <ttype> asm_operands nonnull_asm_operands asm_operand asm_clobbers
 %type <ttype> maybe_attribute attributes attribute attribute_list attrib
 %type <ttype> any_word
@@ -2319,7 +2319,7 @@ label:	  CASE expr_no_commas ':'
    expression with inputs and outputs does not make sense.  */
 simple_asm_expr:
 	ASM_KEYWORD stop_string_translation
-	      '(' STRING ')' start_string_translation
+	      '(' asm_string ')' start_string_translation
 		{ $$ = $4; }
 	;
 
@@ -2348,16 +2348,16 @@ asm_stmt:
 
 asm_argument:
 	/* no operands */
-	STRING
+	asm_string
 		{ $$ = build_asm_expr ($1, 0, 0, 0, true); }
 	/* output operands */
-	| STRING ':' asm_operands
+	| asm_string ':' asm_operands
 		{ $$ = build_asm_expr ($1, $3, 0, 0, false); }
 	/* output and input operands */
-	| STRING ':' asm_operands ':' asm_operands
+	| asm_string ':' asm_operands ':' asm_operands
 		{ $$ = build_asm_expr ($1, $3, $5, 0, false); }
 	/* output and input operands and clobbers */
-	| STRING ':' asm_operands ':' asm_operands ':' asm_clobbers
+	| asm_string ':' asm_operands ':' asm_operands ':' asm_clobbers
 		{ $$ = build_asm_expr ($1, $3, $5, $7, false); }
 	;
 
@@ -2391,10 +2391,11 @@ nonnull_asm_operands:
 	;
 
 asm_operand:
-	  STRING start_string_translation '(' expr ')' stop_string_translation
+	  asm_string start_string_translation '(' expr ')'
+	    stop_string_translation
 		{ $$ = build_tree_list (build_tree_list (NULL_TREE, $1),
 					$4.value); }
-	| '[' identifier ']' STRING start_string_translation
+	| '[' identifier ']' asm_string start_string_translation
 	  '(' expr ')' stop_string_translation
 		{ $2 = build_string (IDENTIFIER_LENGTH ($2),
 				     IDENTIFIER_POINTER ($2));
@@ -2402,12 +2403,24 @@ asm_operand:
 	;
 
 asm_clobbers:
-	  STRING
+	  asm_string
 		{ $$ = tree_cons (NULL_TREE, $1, NULL_TREE); }
-	| asm_clobbers ',' STRING
+	| asm_clobbers ',' asm_string
 		{ $$ = tree_cons (NULL_TREE, $3, $1); }
 	;
 
+/* Strings in 'asm' must be narrow strings.  */
+asm_string:
+	  STRING
+		{ if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE ($1)))
+		      != char_type_node)
+		    {
+		      error ("wide string literal in %<asm%>");
+		      $$ = build_string (1, "");
+		    }
+		  else
+		    $$ = $1; }
+
 stop_string_translation:
         { c_lex_string_translate = 0; }
         ;
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/asm-wide-1.c GCC/gcc/testsuite/gcc.dg/asm-wide-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/asm-wide-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/asm-wide-1.c	2004-12-01 18:11:50.000000000 +0000
@@ -0,0 +1,33 @@
+/* Wide string literals should not be allowed in asm.  */
+/* Origin: Joseph Myers <joseph@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int foo asm (L"bar"); /* { dg-error "error: wide string literal in 'asm'" } */
+
+asm (L"foo"); /* { dg-error "error: wide string literal in 'asm'" } */
+
+void
+f (void)
+{
+  int x = 1;
+  asm (L"foo"); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" :
+       L"=g" (x)); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" : [x]
+       L"=g" (x)); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" : [x] "=g" (x),
+       L"=g" (x)); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" : :
+       L"g" (x)); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" : : :
+       L"memory"); /* { dg-error "error: wide string literal in 'asm'" } */
+  asm ("foo" : : : "memory",
+       L"memory"); /* { dg-error "error: wide string literal in 'asm'" } */
+}
+
+/* Extra errors from the substitution of "" for wide strings: */
+/* { dg-error "output" "output" { target *-*-* } 16 } */
+/* { dg-error "output" "output" { target *-*-* } 18 } */
+/* { dg-error "output" "output" { target *-*-* } 20 } */
+/* { dg-warning "match" "match" { target *-*-* } 21 } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]