[COMMITTED 088/145] gccrs: Parsing of options(...) done.

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Mon Mar 17 15:32:46 GMT 2025


From: jjasmine <tanghocle456@gmail.com>

This is without any mutually exclusive options checked, or
any relationship with reg_operands. Very primitive.

gcc/rust/ChangeLog:

	* ast/rust-expr.h: parsing of options(...)
	* expand/rust-macro-builtins-asm.cc (check_and_set):
	likewise.
	(parse_options): likewise.
	(parseAsmArg): likewise.
	* expand/rust-macro-builtins-asm.h (check_and_set):
	likewise.

gcc/testsuite/ChangeLog:

	* rust/compile/inline_asm_legal_options.rs: New test.
---
 gcc/rust/ast/rust-expr.h                      |  2 +-
 gcc/rust/expand/rust-macro-builtins-asm.cc    | 30 +++++++++----------
 gcc/rust/expand/rust-macro-builtins-asm.h     |  2 +-
 .../rust/compile/inline_asm_legal_options.rs  | 12 ++++++++
 4 files changed, 28 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/inline_asm_legal_options.rs

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 03336cdcc59..719a76cdbb3 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4843,7 +4843,7 @@ public:
   std::vector<InlineAsmOperand> operands;
   std::vector<TupleClobber> clobber_abi;
   // std::set<InlineAsmOptions> options;
-  std::set<std::string> options;
+  std::set<InlineAsmOptions> options;
 
   std::vector<location_t> line_spans;
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc b/gcc/rust/expand/rust-macro-builtins-asm.cc
index bcf8b6fb3f2..443c8a3bce3 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -116,7 +116,7 @@ parse_clobber_abi (Parser<MacroInvocLexer> &parser, TokenId last_token_id,
 
 void
 check_and_set (Parser<MacroInvocLexer> &p, AST::InlineAsm &inlineAsm,
-	       std::string option)
+	       AST::InlineAsmOptions option)
 {
   if (inlineAsm.options.count (option) == 1)
     {
@@ -148,43 +148,40 @@ parse_options (Parser<MacroInvocLexer> &parser, TokenId last_token_id,
     {
       if (!is_global_asm && check_identifier (parser, "pure"))
 	{
-	  check_and_set (parser, inlineAsm, "pure");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::PURE);
 	}
       else if (!is_global_asm && check_identifier (parser, "nomem"))
 	{
-	  check_and_set (parser, inlineAsm, "nomem");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NOMEM);
 	}
       else if (!is_global_asm && check_identifier (parser, "readonly"))
 	{
-	  check_and_set (parser, inlineAsm, "readonly");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::READONLY);
 	}
       else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
 	{
-	  check_and_set (parser, inlineAsm, "preserves_flags");
+	  check_and_set (parser, inlineAsm,
+			 AST::InlineAsmOptions::PRESERVES_FLAGS);
 	}
       else if (!is_global_asm && check_identifier (parser, "noreturn"))
 	{
-	  check_and_set (parser, inlineAsm, "noreturn");
-	}
-      else if (!is_global_asm && check_identifier (parser, "noreturn"))
-	{
-	  check_and_set (parser, inlineAsm, "noreturn");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NORETURN);
 	}
       else if (!is_global_asm && check_identifier (parser, "nostack"))
 	{
-	  check_and_set (parser, inlineAsm, "nostack");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::NOSTACK);
 	}
       else if (!is_global_asm && check_identifier (parser, "may_unwind"))
 	{
-	  check_and_set (parser, inlineAsm, "may_unwind");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::MAY_UNWIND);
 	}
       else if (check_identifier (parser, "att_syntax"))
 	{
-	  check_and_set (parser, inlineAsm, "att_syntax");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::ATT_SYNTAX);
 	}
       else if (check_identifier (parser, "raw"))
 	{
-	  check_and_set (parser, inlineAsm, "raw");
+	  check_and_set (parser, inlineAsm, AST::InlineAsmOptions::RAW);
 	}
       else
 	{
@@ -201,6 +198,7 @@ parse_options (Parser<MacroInvocLexer> &parser, TokenId last_token_id,
 	{
 	  // TODO: If the skip of comma is unsuccessful, which should be
 	  // illegal, pleaes emit the correct error.
+	  std::cout << "Illegal comma" << std::endl;
 	  return -1;
 	}
 
@@ -315,13 +313,13 @@ parseAsmArg (Parser<MacroInvocLexer> &parser, TokenId last_token_id,
       // TODO: Parse options
       if (check_identifier (parser, "options"))
 	{
-	  std::cout << "Parse optoins" << std::endl;
+	  parse_options (parser, last_token_id, inlineAsm);
 	  continue;
 	}
 
       // Ok after we have check that neither clobber_abi nor options works, the
       // only other logical choice is reg_operand
-      std::cout << "reg_operand" << std::endl;
+      // std::cout << "reg_operand" << std::endl;
       fm_string = parse_format_string (parser, last_token_id);
     }
   return 0;
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.h b/gcc/rust/expand/rust-macro-builtins-asm.h
index 1d7889f2698..163ad161b5a 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.h
+++ b/gcc/rust/expand/rust-macro-builtins-asm.h
@@ -24,7 +24,7 @@ check_identifier (Parser<MacroInvocLexer> &p, std::string ident);
 
 void
 check_and_set (Parser<MacroInvocLexer> &p, AST::InlineAsm &inlineAsm,
-	       std::string option);
+	       AST::InlineAsmOptions option);
 // From rustc
 int
 parse_operand (Parser<MacroInvocLexer> &parser, TokenId last_token_id,
diff --git a/gcc/testsuite/rust/compile/inline_asm_legal_options.rs b/gcc/testsuite/rust/compile/inline_asm_legal_options.rs
new file mode 100644
index 00000000000..a41dbd9f07a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/inline_asm_legal_options.rs
@@ -0,0 +1,12 @@
+#![feature(rustc_attrs)]
+
+#[rustc_builtin_macro]
+macro_rules! asm {
+    () => {}
+}
+
+fn main() {
+    unsafe {
+        asm!("nop", options(nomem, nostack, att_syntax, raw));
+    }
+}
\ No newline at end of file
-- 
2.45.2



More information about the Gcc-rust mailing list