https://gcc.gnu.org/g:23eb5d2c3a03e262e164342c18dfb8f61017f01f

commit r15-8354-g23eb5d2c3a03e262e164342c18dfb8f61017f01f
Author: badumbatish <tanghocle...@gmail.com>
Date:   Tue Aug 27 18:15:26 2024 -0700

    gccrs: Make inline mov compiles
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-asm.cc (CompileAsm::asm_build_expr):
            Remove debug
            * expand/rust-macro-builtins-asm.cc (expand_inline_asm_strings):
            properly formatted via rust instead of c
            (parse_asm): formatted comment
            (parse_format_strings): formatted comment
            * hir/tree/rust-hir-expr.h: fix is_simple_asm()
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/inline_asm_parse_operand.rs: Fix format asm
            * rust/compile/inline_asm_parse_output_operand.rs:
            Fix format asm
            * rust/execute/torture/inline_asm_mov_x_5.rs: Move to...
            * rust/execute/inline_asm_mov_x_5.rs: ...here.

Diff:
---
 gcc/rust/backend/rust-compile-asm.cc               |  2 +-
 gcc/rust/expand/rust-macro-builtins-asm.cc         | 67 ++++++++++++++++++----
 gcc/rust/hir/tree/rust-hir-expr.h                  |  4 +-
 .../rust/compile/inline_asm_parse_operand.rs       | 14 +++--
 .../compile/inline_asm_parse_output_operand.rs     |  2 +-
 .../execute/{torture => }/inline_asm_mov_x_5.rs    |  5 +-
 6 files changed, 71 insertions(+), 23 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-asm.cc 
b/gcc/rust/backend/rust-compile-asm.cc
index 92d60d756864..045cc2866fd7 100644
--- a/gcc/rust/backend/rust-compile-asm.cc
+++ b/gcc/rust/backend/rust-compile-asm.cc
@@ -26,7 +26,7 @@ CompileAsm::asm_build_expr (HIR::InlineAsm &expr)
   ASM_BASIC_P (asm_expr) = expr.is_simple_asm ();
   ASM_VOLATILE_P (asm_expr) = false;
   ASM_INLINE_P (asm_expr) = expr.is_inline_asm ();
-  Backend::debug (asm_expr);
+  /*Backend::debug (asm_expr);*/
   return asm_expr;
 }
 
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index 379e5d8b967b..65bc6c581928 100644
--- a/gcc/rust/expand/rust-macro-builtins-asm.cc
+++ b/gcc/rust/expand/rust-macro-builtins-asm.cc
@@ -761,6 +761,8 @@ expand_inline_asm_strings (InlineAsmContext &inline_asm_ctx)
   auto &inline_asm = inline_asm_ctx.inline_asm;
 
   auto str_vec = inline_asm.get_template_strs ();
+
+  decltype (str_vec) resulting_template_vec;
   for (auto &template_str : str_vec)
     {
       /*std::cout << template_str.symbol << std::endl;*/
@@ -769,20 +771,57 @@ expand_inline_asm_strings (InlineAsmContext 
&inline_asm_ctx)
                                          Fmt::ffi::ParseMode::InlineAsm);
       auto pieces_vec = pieces.get_pieces ();
 
+      std::string transformed_template_str = "";
       for (size_t i = 0; i < pieces_vec.size (); i++)
        {
          auto piece = pieces_vec[i];
          if (piece.tag == Fmt::ffi::Piece::Tag::String)
            {
+             transformed_template_str += piece.string._0.to_string ();
+           }
+         else if (piece.tag == Fmt::ffi::Piece::Tag::NextArgument)
+           {
+             /*    std::cout << "       " << i << ": "*/
+             /*<< piece.next_argument._0.to_string () << std::endl;*/
+
+             auto next_argument = piece.next_argument._0;
+             switch (piece.next_argument._0.position.tag)
+               {
+                 case Fmt::ffi::Position::Tag::ArgumentImplicitlyIs: {
+                   auto idx = next_argument.position.argument_implicitly_is._0;
+                   /*auto trait = next_argument.format;*/
+                   /*auto arg = arguments.at (idx);*/
+
+                   /* // FIXME(Arthur): This API sucks*/
+                   /* rust_assert (arg.get_kind ().kind*/
+                   /*== AST::FormatArgumentKind::Kind::Normal);*/
+                   /**/
+                   /* args.push_back ({arg.get_expr ().clone_expr (),
+                    * trait});*/
+
+                   transformed_template_str += "%" + std::to_string (idx);
+                   /*std::cout << "argument implicitly is: " << idx <<
+                    * std::endl;*/
+                   /*std::cout << "trait: " << trait.to_string () <<
+                    * std::endl;*/
+                   /*std::cout << "arg: " << arg.to_string () << std::endl;*/
+                 }
+                 break;
+               case Fmt::ffi::Position::Tag::ArgumentIs:
+               case Fmt::ffi::Position::Tag::ArgumentNamed:
+                 rust_sorry_at (inline_asm.get_locus (),
+                                "unhandled argument position specifier");
+                 break;
+               }
            }
-         /*std::cout << "       " << i << ": " << piece.string._0.to_string
-          * ()*/
-         /*   << std::endl;*/
        }
+      template_str.symbol = transformed_template_str;
     }
 
+  inline_asm.template_strs = str_vec;
   return inline_asm_ctx;
 }
+
 tl::optional<AST::Fragment>
 parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
           AST::InvocKind semicolon, AST::AsmKind is_global_asm)
@@ -791,7 +830,8 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
   // We first parse all formatted strings. If we fail, then we return
   // tl::nullopt
 
-  // We then parse the asm arguments. If we fail, then we return tl::nullopt
+  // We then parse the asm arguments. If we fail, then we return
+  // tl::nullopt
 
   // We then validate. If we fail, then we return tl::nullopt
 
@@ -808,9 +848,10 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
                             .and_then (parse_asm_arg)
                             .and_then (validate);
 
-  // TODO: I'm putting the validation here because the rust reference put it
-  // here Per Arthur's advice we would actually do the validation in a 
different
-  // stage. and visit on the InlineAsm AST instead of it's context.
+  // TODO: I'm putting the validation here because the rust reference put
+  // it here Per Arthur's advice we would actually do the validation in a
+  // different stage. and visit on the InlineAsm AST instead of it's
+  // context.
   auto is_valid = (bool) resulting_context;
   if (is_valid)
     {
@@ -822,8 +863,9 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
 
       std::vector<AST::SingleASTNode> single_vec = {};
 
-      // If the macro invocation has a semicolon (`asm!("...");`), then we need
-      // to make it a statement. This way, it will be expanded properly.
+      // If the macro invocation has a semicolon (`asm!("...");`), then we
+      // need to make it a statement. This way, it will be expanded
+      // properly.
       if (semicolon == AST::InvocKind::Semicoloned)
        single_vec.emplace_back (AST::SingleASTNode (
          Rust::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
@@ -846,7 +888,8 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData 
&invoc,
 tl::expected<InlineAsmContext, InlineAsmParseError>
 parse_format_strings (InlineAsmContext inline_asm_ctx)
 {
-  // Parse the first ever formatted string, success or not, will skip 1 token
+  // Parse the first ever formatted string, success or not, will skip 1
+  // token
   auto &parser = inline_asm_ctx.parser;
   auto last_token_id = inline_asm_ctx.last_token_id;
   auto fm_string = parse_format_string (inline_asm_ctx);
@@ -875,8 +918,8 @@ parse_format_strings (InlineAsmContext inline_asm_ctx)
        {
          break;
        }
-      // Ok after the comma is good, we better be parsing correctly everything
-      // in here, which is formatted string in ABNF
+      // Ok after the comma is good, we better be parsing correctly
+      // everything in here, which is formatted string in ABNF
       inline_asm_ctx.consumed_comma_without_formatted_string = false;
 
       token = parser.peek_current_token ();
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h 
b/gcc/rust/hir/tree/rust-hir-expr.h
index 8a42c8b3a675..da5dd12cff59 100644
--- a/gcc/rust/hir/tree/rust-hir-expr.h
+++ b/gcc/rust/hir/tree/rust-hir-expr.h
@@ -4148,8 +4148,8 @@ public:
 
   bool is_simple_asm ()
   {
-    // TODO: Check back later to determine how an InlineAsm is simple.
-    return true;
+    // INFO: A simple asm is an asm that does not have any operands
+    return this->operands.size () == 0;
   }
 
   bool is_inline_asm ()
diff --git a/gcc/testsuite/rust/compile/inline_asm_parse_operand.rs 
b/gcc/testsuite/rust/compile/inline_asm_parse_operand.rs
index dfce295ca990..e0efe1c420da 100644
--- a/gcc/testsuite/rust/compile/inline_asm_parse_operand.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_parse_operand.rs
@@ -2,13 +2,13 @@
 
 #[rustc_builtin_macro]
 macro_rules! asm {
-    () => {}
+    () => {};
 }
 
-fn main() {
+fn main() -> i32 {
     unsafe {
         asm!(
-            "add {0:e}, {0:e}",
+            "add {}, {}",
             in(reg) 0
         );
     }
@@ -20,18 +20,20 @@ fn main() {
     let _num2: i32 = 20;
     unsafe {
         asm!(
-            "add {0}, {0}",
+            "add {}, {}",
             inout(reg) num1 =>_num1,
             in(reg) _num2,
         );
     }
 
-    let mut _output_testing : u32 = 0;
+    let mut _output_testing: u32 = 0;
     unsafe {
         asm!(
-            "add {0}, {0}",
+            "add {}, {}",
             in(reg) _num1,
             //out(reg) _,
         );
     }
+
+    0
 }
diff --git a/gcc/testsuite/rust/compile/inline_asm_parse_output_operand.rs 
b/gcc/testsuite/rust/compile/inline_asm_parse_output_operand.rs
index 3134c73b3c29..a67fff5f81b9 100644
--- a/gcc/testsuite/rust/compile/inline_asm_parse_output_operand.rs
+++ b/gcc/testsuite/rust/compile/inline_asm_parse_output_operand.rs
@@ -10,7 +10,7 @@ fn main() {
     let mut _num2: i32 = 10;
     unsafe {
         asm!(
-            "mov {0}, 4",
+            "mov {}, 4",
             out(reg) _num1,
             out(reg) _num2,
         );
diff --git a/gcc/testsuite/rust/execute/torture/inline_asm_mov_x_5.rs 
b/gcc/testsuite/rust/execute/inline_asm_mov_x_5.rs
similarity index 75%
rename from gcc/testsuite/rust/execute/torture/inline_asm_mov_x_5.rs
rename to gcc/testsuite/rust/execute/inline_asm_mov_x_5.rs
index 18bc87ab554c..e09ea1a030ae 100644
--- a/gcc/testsuite/rust/execute/torture/inline_asm_mov_x_5.rs
+++ b/gcc/testsuite/rust/execute/inline_asm_mov_x_5.rs
@@ -1,9 +1,11 @@
-#![feature(rustc_attrs)]
+/* { dg-output "5\r*\n" }*/
 
+#![feature(rustc_attrs)]
 #[rustc_builtin_macro]
 macro_rules! asm {
     () => {};
 }
+
 extern "C" {
     fn printf(s: *const i8, ...);
 }
@@ -15,5 +17,6 @@ fn main() {
             "mov {}, 5",
             out(reg) _x
         );
+        printf("%d\n\0" as *const str as *const i8, _x);
     }
 }

Reply via email to