Hi all,

This is the beginning of a patch to introduce the extended attribute
for asm declarations proposed in
https://gcc.gnu.org/pipermail/gcc-patches/2023-November/636563.html. I
will need some reviewer help in implementing this patch, as I am not
very familiar with gcc's internals.

The attribute in question looks as such (Example):

[[gnu::extended([output] "=r" (output) [otheroutput] "=r" (otheroutput),
                          [input] "r" (input) [otherinput] "r" (otherinput),
                          "%rcx" "%rdx", label, volatile stack)]]
asm ("");

I would really appreciate any reviews, as well as help in implementing
this patch

best regards,
Julian
From a189f2820025315b5574d0e9384b96301c6ba7e8 Mon Sep 17 00:00:00 2001
From: TheShermanTanker <tanksherman27@gmail.com>
Date: Fri, 17 Nov 2023 11:09:50 +0800
Subject: [PATCH] Introduce the extended attribute for asm declarations

---
 gcc/cp/parser.cc | 25 +++++++++++++++++++++++++
 gcc/cp/tree.cc   | 11 +++++++++++
 2 files changed, 36 insertions(+)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 5116bcb78f6..ecc5f2fabc1 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -15407,6 +15407,13 @@ cp_parser_block_declaration (cp_parser *parser,
 	cp_parser_commit_to_tentative_parse (parser);
       cp_parser_asm_definition (parser);
     }
+  else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
+       && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx, RID_ASM))
+    {
+      if (statement_p)
+    cp_parser_commit_to_tentative_parse (parser);
+      cp_parser_asm_definition (parser);
+    }
   /* If the next keyword is `namespace', we have a
      namespace-alias-definition.  */
   else if (token1->keyword == RID_NAMESPACE)
@@ -22397,6 +22404,23 @@ cp_parser_asm_definition (cp_parser* parser)
   bool invalid_inputs_p = false;
   bool invalid_outputs_p = false;
   required_token missing = RT_NONE;
+
+  tree attrs = cp_parser_std_attribute_spec_seq (parser);
+  tree extended = error_mark_node;
+
+  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
+    {
+      /* Error during attribute parsing that resulted in skipping
+         to next semicolon.  */
+      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
+      return;
+    }
+
+  if (attrs != error_mark_node)
+    {
+      extended = lookup_attribute ("gnu", "extended", attrs);
+    }
+
   location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
 
   /* Look for the `asm' keyword.  */
@@ -22511,6 +22535,7 @@ cp_parser_asm_definition (cp_parser* parser)
      two `:' tokens.  */
   if (cp_parser_allow_gnu_extensions_p (parser)
       && parser->in_function_body
+      && extended == error_mark_node
       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
     {
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 417c92ba76f..1f081b3dfd8 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -46,6 +46,7 @@ static tree verify_stmt_tree_r (tree *, int *, void *);
 
 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
+static tree handle_extended_attribute (tree *, tree, tree, int, bool *);
 static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
 
 /* If REF is an lvalue, returns the kind of lvalue that REF is.
@@ -5080,6 +5081,8 @@ const struct attribute_spec cxx_attribute_table[] =
     handle_init_priority_attribute, NULL },
   { "abi_tag", 1, -1, false, false, false, true,
     handle_abi_tag_attribute, NULL },
+  { "extended", 0, 5, true, false, false, false,
+    handle_extended_attribute, NULL },
   { NULL, 0, 0, false, false, false, false, NULL, NULL }
 };
 
@@ -5350,6 +5353,14 @@ handle_abi_tag_attribute (tree* node, tree name, tree args,
   return NULL_TREE;
 }
 
+static tree
+handle_extended_attribute (tree *node, tree name, tree args, int flags, bool *no_add_attrs)
+{
+  /* TODO What could be done here? */
+  *no_add_attrs = true;
+  return NULL_TREE;
+}
+
 /* Perform checking for contract attributes.  */
 
 tree
-- 
2.41.0

Reply via email to