[gccrs COMMIT] gccrs: Improve AST::LlvmInlineAsm handling

[email protected]
Newsgroups gmane.comp.gcc.rust,gmane.comp.gcc.patches
Message-ID <[email protected]>
From: Owen Avery <[email protected]>

Adjusts some visitor functions and makes AST::LlvmInlineAsm store only a
single template string.

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc
	(TokenCollector::visit (LlvmInlineAsm)): Improve accuracy of
	token collection.
	* ast/rust-ast-pointer-visitor.cc
	(PointerVisitor::visit (LlvmInlineAsm)): Visit outer attributes.
	* ast/rust-ast-visitor.cc
	(DefaultASTVisitor::visit (LlvmInlineAsm)): Likewise.
	* ast/rust-expr.h (LlvmInlineAsm::templates): Remove member
	variable and replace with...
	(LlvmInlineAsm::template_str): ...new member variable.
	(LlvmInlineAsm::LlvmInlineAsm): Initialize template_str.
	(LlvmInlineAsm::get_templates): Remove member function.
	(LlvmInlineAsm::set_template): New member function.
	(LlvmInlineAsm::get_template): Likewise.
	* expand/rust-macro-builtins-asm.cc (parse_llvm_templates):
	Rename to...
	(parse_llvm_template): ...here and adjust to not expect multiple
	template strings.
	(parse_llvm_asm): Handle rename of parse_llvm_templates to
	parse_llvm_template.
	* hir/rust-ast-lower-expr.cc (check_llvm_asm_support): Handle
	changes to LlvmInlineAsm.
	(ASTLoweringExpr::visit (LlvmInlineAsm)): Likewise.

Signed-off-by: Owen Avery <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/d96a80d4fe72bd86121618937ea5bf319337d57e

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4778

 gcc/rust/ast/rust-ast-collector.cc         | 43 ++++++++++++++++++----
 gcc/rust/ast/rust-ast-pointer-visitor.cc   |  2 +
 gcc/rust/ast/rust-ast-visitor.cc           |  2 +
 gcc/rust/ast/rust-expr.h                   | 14 ++++---
 gcc/rust/expand/rust-macro-builtins-asm.cc | 21 ++++-------
 gcc/rust/hir/rust-ast-lower-expr.cc        |  5 +--
 6 files changed, 58 insertions(+), 29 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index b6dd650e6..b1238f3e5 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -1869,37 +1869,66 @@ TokenCollector::visit (LlvmInlineAsm &expr)
   push (Rust::Token::make_identifier (expr.get_locus (), "llvm_asm"));
   push (Rust::Token::make (EXCLAM, expr.get_locus ()));
   push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
-  for (auto &template_str : expr.get_templates ())
-    push (Rust::Token::make_string (template_str.get_locus (),
-				    std::move (template_str.symbol)));
+  push (Rust::Token::make_string (expr.get_template ().get_locus (),
+				  std::move (expr.get_template ().symbol)));
 
   push (Rust::Token::make (COLON, expr.get_locus ()));
+
+  bool needs_comma = false;
+
   for (auto output : expr.get_outputs ())
     {
+      if (needs_comma)
+	push (Rust::Token::make (COMMA, expr.get_locus ()));
+      needs_comma = true;
       push (Rust::Token::make_string (expr.get_locus (),
 				      std::move (output.constraint)));
+      push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
       visit (output.expr);
-      push (Rust::Token::make (COMMA, expr.get_locus ()));
+      push (Rust::Token::make (RIGHT_PAREN, expr.get_locus ()));
     }
 
   push (Rust::Token::make (COLON, expr.get_locus ()));
+  needs_comma = false;
   for (auto input : expr.get_inputs ())
     {
+      if (needs_comma)
+	push (Rust::Token::make (COMMA, expr.get_locus ()));
+      needs_comma = true;
       push (Rust::Token::make_string (expr.get_locus (),
 				      std::move (input.constraint)));
+      push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
       visit (input.expr);
-      push (Rust::Token::make (COMMA, expr.get_locus ()));
+      push (Rust::Token::make (RIGHT_PAREN, expr.get_locus ()));
     }
 
   push (Rust::Token::make (COLON, expr.get_locus ()));
+  needs_comma = false;
   for (auto &clobber : expr.get_clobbers ())
     {
+      if (needs_comma)
+	push (Rust::Token::make (COMMA, expr.get_locus ()));
+      needs_comma = true;
       push (Rust::Token::make_string (expr.get_locus (),
 				      std::move (clobber.symbol)));
-      push (Rust::Token::make (COMMA, expr.get_locus ()));
     }
   push (Rust::Token::make (COLON, expr.get_locus ()));
-  // Dump options
+
+#define X(code, s)                                                             \
+  if (expr.code)                                                               \
+    {                                                                          \
+      if (needs_comma)                                                         \
+	push (Rust::Token::make (COMMA, expr.get_locus ()));                   \
+      needs_comma = true;                                                      \
+      push (Rust::Token::make_string (expr.get_locus (), s));                  \
+    }
+
+  needs_comma = false;
+  X (is_volatile (), "volatile")
+  X (is_stack_aligned (), "alignstack")
+  X (get_dialect () == LlvmInlineAsm::Dialect::Intel, "intel")
+
+#undef X
 
   push (Rust::Token::make (RIGHT_PAREN, expr.get_locus ()));
 }
diff --git a/gcc/rust/ast/rust-ast-pointer-visitor.cc b/gcc/rust/ast/rust-ast-pointer-visitor.cc
index cea7ae647..48e4d2e7a 100644
--- a/gcc/rust/ast/rust-ast-pointer-visitor.cc
+++ b/gcc/rust/ast/rust-ast-pointer-visitor.cc
@@ -627,6 +627,8 @@ PointerVisitor::visit (AST::InlineAsm &expr)
 void
 PointerVisitor::visit (AST::LlvmInlineAsm &expr)
 {
+  visit_outer_attrs (expr);
+
   for (auto &output : expr.get_outputs ())
     reseat (output.expr);
 
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index c800f2c30..892a35648 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -759,6 +759,8 @@ DefaultASTVisitor::visit (AST::InlineAsm &expr)
 void
 DefaultASTVisitor::visit (AST::LlvmInlineAsm &expr)
 {
+  visit_outer_attrs (expr);
+
   for (auto &output : expr.get_outputs ())
     visit (output.expr);
 
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 921b79800..43f5802d3 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -5844,14 +5844,16 @@ private:
   std::vector<Attribute> outer_attrs;
   std::vector<LlvmOperand> inputs;
   std::vector<LlvmOperand> outputs;
-  std::vector<TupleTemplateStr> templates;
+  TupleTemplateStr template_str;
   std::vector<TupleClobber> clobbers;
   bool volatility;
   bool align_stack;
   Dialect dialect;
 
 public:
-  LlvmInlineAsm (location_t locus) : locus (locus) {}
+  LlvmInlineAsm (location_t locus)
+    : locus (locus), template_str (UNKNOWN_LOCATION, "")
+  {}
 
   Dialect get_dialect () { return dialect; }
 
@@ -5874,12 +5876,14 @@ public:
     return new LlvmInlineAsm (*this);
   }
 
-  std::vector<TupleTemplateStr> &get_templates () { return templates; }
-  const std::vector<TupleTemplateStr> &get_templates () const
+  void set_template (TupleTemplateStr template_str)
   {
-    return templates;
+    this->template_str = std::move (template_str);
   }
 
+  TupleTemplateStr &get_template () { return template_str; }
+  const TupleTemplateStr &get_template () const { return template_str; }
+
   Expr::Kind get_expr_kind () const override
   {
     return Expr::Kind::LlvmInlineAsm;
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 77c747bf5..a3683a0ca 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -990,7 +990,7 @@ validate (InlineAsmContext inline_asm_ctx)
 }
 
 tl::optional<LlvmAsmContext>
-parse_llvm_templates (LlvmAsmContext ctx)
+parse_llvm_template (LlvmAsmContext ctx)
 {
   auto &parser = ctx.parser;
 
@@ -1002,19 +1002,12 @@ parse_llvm_templates (LlvmAsmContext ctx)
       return tl::nullopt;
     }
 
-  ctx.llvm_asm.get_templates ().emplace_back (token->get_locus (),
-					      strip_double_quotes (
-						token->as_string ()));
-  ctx.parser.skip_token ();
+  // TODO: improve string handling?
+  ctx.llvm_asm.set_template (
+    AST::TupleTemplateStr (token->get_locus (),
+			   strip_double_quotes (token->as_string ())));
 
-  token = parser.peek_current_token ();
-  if (token->get_id () != ctx.last_token_id && token->get_id () != COLON
-      && token->get_id () != SCOPE_RESOLUTION)
-    {
-      // We do not handle multiple template string, we provide minimal support
-      // for the black_box intrinsics.
-      rust_unreachable ();
-    }
+  ctx.parser.skip_token ();
 
   return ctx;
 }
@@ -1184,7 +1177,7 @@ parse_llvm_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
   auto asm_ctx = LlvmAsmContext (llvm_asm, parser, last_token_id);
 
   tl::optional<LlvmAsmContext> resulting_context
-    = parse_llvm_templates (asm_ctx).and_then (parse_llvm_arguments);
+    = parse_llvm_template (asm_ctx).and_then (parse_llvm_arguments);
 
   if (resulting_context)
     {
diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc b/gcc/rust/hir/rust-ast-lower-expr.cc
index 0e9343077..8dc2ed04c 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.cc
+++ b/gcc/rust/hir/rust-ast-lower-expr.cc
@@ -1024,8 +1024,7 @@ check_llvm_asm_support (const std::vector<LlvmOperand> &inputs,
 {
   return outputs.size () == 0 && inputs.size () <= 1
 	 && expr.get_clobbers ().size () <= 1
-	 && expr.get_templates ().size () == 1
-	 && expr.get_templates ()[0].symbol == "";
+	 && expr.get_template ().symbol == "";
 }
 
 } // namespace
@@ -1074,7 +1073,7 @@ ASTLoweringExpr::visit (AST::LlvmInlineAsm &expr)
 
   translated
     = new HIR::LlvmInlineAsm (expr.get_locus (), inputs, outputs,
-			      expr.get_templates (), expr.get_clobbers (),
+			      {expr.get_template ()}, expr.get_clobbers (),
 			      options, expr.get_outer_attrs (), mapping);
 }
 

base-commit: 08ec968b3da40d830e5c4d9d13edd0e09bc46f42
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.