]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Make inline mov compiles
authorbadumbatish <tanghocle456@gmail.com>
Wed, 28 Aug 2024 01:15:26 +0000 (18:15 -0700)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 19 Mar 2025 14:32:07 +0000 (15:32 +0100)
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.

gcc/rust/backend/rust-compile-asm.cc
gcc/rust/expand/rust-macro-builtins-asm.cc
gcc/rust/hir/tree/rust-hir-expr.h
gcc/testsuite/rust/compile/inline_asm_parse_operand.rs
gcc/testsuite/rust/compile/inline_asm_parse_output_operand.rs
gcc/testsuite/rust/execute/inline_asm_mov_x_5.rs [moved from gcc/testsuite/rust/execute/torture/inline_asm_mov_x_5.rs with 75% similarity]

index 92d60d756864adad375a9639a6d0f2f3f6312bf7..045cc2866fd79b154b4bec0a2aa7df972b5feac4 100644 (file)
@@ -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;
 }
 
index 379e5d8b967b3c76deef5722c3e5d2156ecb6b68..65bc6c581928097d8e838294e08f5f9a55e14214 100644 (file)
@@ -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 ();
index 8a42c8b3a6752d918e288908cfb6f6b84796b626..da5dd12cff59d39a0742b17346674d4c722c061f 100644 (file)
@@ -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 ()
index dfce295ca9908cbaddb481c8ff3fb106b569e0c3..e0efe1c420da63c77cc5c049e139ea0d954f46d5 100644 (file)
@@ -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
 }
index 3134c73b3c2924a22a9e2a5bb6f4b3e1cbb6b958..a67fff5f81b926faab6212c000251be38f5c2d4b 100644 (file)
@@ -10,7 +10,7 @@ fn main() {
     let mut _num2: i32 = 10;
     unsafe {
         asm!(
-            "mov {0}, 4",
+            "mov {}, 4",
             out(reg) _num1,
             out(reg) _num2,
         );
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 18bc87ab554c988128888007d2ecf9c08c531912..e09ea1a030ae2109c2e1dd443fa1b019533dd23a 100644 (file)
@@ -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);
     }
 }