LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-macro-builtins-asm.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 85.1 % 503 428
Test Date: 2025-06-21 16:26:05 Functions: 100.0 % 30 30
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #include "expected.h"
      20                 :             : #include "rust-macro-builtins-asm.h"
      21                 :             : #include "rust-ast-fragment.h"
      22                 :             : #include "rust-ast.h"
      23                 :             : #include "rust-fmt.h"
      24                 :             : #include "rust-stmt.h"
      25                 :             : #include "rust-parse.h"
      26                 :             : 
      27                 :             : namespace Rust {
      28                 :             : std::map<AST::InlineAsmOption, std::string> InlineAsmOptionMap{
      29                 :             :   {AST::InlineAsmOption::PURE, "pure"},
      30                 :             :   {AST::InlineAsmOption::NOMEM, "nomem"},
      31                 :             :   {AST::InlineAsmOption::READONLY, "readonly"},
      32                 :             :   {AST::InlineAsmOption::PRESERVES_FLAGS, "preserves_flags"},
      33                 :             :   {AST::InlineAsmOption::NORETURN, "noreturn"},
      34                 :             :   {AST::InlineAsmOption::NOSTACK, "nostack"},
      35                 :             :   {AST::InlineAsmOption::MAY_UNWIND, "may_unwind"},
      36                 :             :   {AST::InlineAsmOption::ATT_SYNTAX, "att_syntax"},
      37                 :             :   {AST::InlineAsmOption::RAW, "raw"},
      38                 :             : };
      39                 :             : 
      40                 :             : std::set<std::string> potentially_nonpromoted_keywords
      41                 :             :   = {"in", "out", "lateout", "inout", "inlateout", "const", "sym", "label"};
      42                 :             : 
      43                 :             : // Helper function strips the beginning and ending double quotes from a
      44                 :             : // string.
      45                 :             : std::string
      46                 :          64 : strip_double_quotes (const std::string &str)
      47                 :             : {
      48                 :          64 :   std::string result = str;
      49                 :             : 
      50                 :          64 :   rust_assert (!str.empty ());
      51                 :             : 
      52                 :          64 :   rust_assert (str.front () == '\"');
      53                 :          64 :   rust_assert (str.back () == '\"');
      54                 :             : 
      55                 :             :   // we have to special case empty strings which just contain a set of quotes
      56                 :             :   // so, if the string is "\"\"", just return ""
      57                 :          64 :   if (result.size () == 2)
      58                 :           2 :     return "";
      59                 :             : 
      60                 :          62 :   rust_assert (result.size () >= 3);
      61                 :             : 
      62                 :          62 :   result.erase (0, 1);
      63                 :          62 :   result.erase (result.size () - 1, 1);
      64                 :             : 
      65                 :          62 :   return result;
      66                 :          64 : }
      67                 :             : 
      68                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
      69                 :           6 : parse_clobber_abi (InlineAsmContext inline_asm_ctx)
      70                 :             : {
      71                 :             :   // clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
      72                 :             :   // PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA clobber_abi
      73                 :             :   // identifier keyword
      74                 :           6 :   auto &parser = inline_asm_ctx.parser;
      75                 :           6 :   auto last_token_id = inline_asm_ctx.last_token_id;
      76                 :           6 :   auto &inline_asm = inline_asm_ctx.inline_asm;
      77                 :           6 :   auto token = parser.peek_current_token ();
      78                 :           6 :   if (!parser.skip_token (LEFT_PAREN))
      79                 :             :     {
      80                 :           4 :       token = parser.peek_current_token ();
      81                 :             : 
      82                 :             :       // TODO: Error reporting shifted to the left 1 character, I'm not sure
      83                 :             :       // why.
      84                 :           4 :       if (token->get_id () == last_token_id)
      85                 :             :         {
      86                 :           2 :           rust_error_at (token->get_locus (),
      87                 :             :                          "expected %<(%>, found end of macro arguments");
      88                 :           2 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
      89                 :             :         }
      90                 :             :       else
      91                 :             :         {
      92                 :           2 :           rust_error_at (token->get_locus (), "expected %<(%>, found %qs",
      93                 :             :                          token->get_token_description ());
      94                 :             :         }
      95                 :           2 :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
      96                 :             :     }
      97                 :             : 
      98                 :           2 :   if (parser.skip_token (RIGHT_PAREN))
      99                 :             :     {
     100                 :           2 :       rust_error_at (
     101                 :           2 :         parser.peek_current_token ()->get_locus (),
     102                 :             :         "at least one abi must be provided as an argument to %<clobber_abi%>");
     103                 :           2 :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     104                 :             :     }
     105                 :             : 
     106                 :           0 :   std::vector<AST::TupleClobber> new_abis;
     107                 :             : 
     108                 :           0 :   token = parser.peek_current_token ();
     109                 :             : 
     110                 :           0 :   while (token->get_id () != last_token_id && token->get_id () != RIGHT_PAREN)
     111                 :             :     {
     112                 :             :       // Check if it is a string literal or not, codename: <ABI> in ABNF
     113                 :           0 :       if (token->get_id () == STRING_LITERAL)
     114                 :             :         {
     115                 :             :           // TODO: Caring for span in here.
     116                 :           0 :           new_abis.push_back ({token->as_string (), token->get_locus ()});
     117                 :             :         }
     118                 :             :       else
     119                 :             :         {
     120                 :             :           // TODO: We encountered something that is not string literal, which
     121                 :             :           // should be illegal, please emit the correct error
     122                 :             :           // https://github.com/rust-lang/rust/blob/b92758a9aef1cef7b79e2b72c3d8ba113e547f89/compiler/rustc_builtin_macros/src/asm.rs#L387
     123                 :           0 :           rust_unreachable ();
     124                 :             :         }
     125                 :             : 
     126                 :           0 :       if (parser.skip_token (RIGHT_PAREN))
     127                 :             :         {
     128                 :             :           break;
     129                 :             :         }
     130                 :             : 
     131                 :           0 :       if (!parser.skip_token (COMMA))
     132                 :             :         {
     133                 :             :           // TODO: If the skip of comma is unsuccessful, which should be
     134                 :             :           // illegal, pleaes emit the correct error.
     135                 :           0 :           rust_unreachable ();
     136                 :             :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     137                 :             :         }
     138                 :             : 
     139                 :           0 :       token = parser.peek_current_token ();
     140                 :             :     }
     141                 :             : 
     142                 :             :   // Done processing the local clobber abis, push that to the main Args in
     143                 :             :   // argument
     144                 :             : 
     145                 :           0 :   for (auto abi : new_abis)
     146                 :             :     {
     147                 :           0 :       inline_asm.clobber_abi.push_back (abi);
     148                 :           0 :     }
     149                 :             : 
     150                 :           0 :   return inline_asm_ctx;
     151                 :           0 : }
     152                 :             : 
     153                 :             : tl::optional<AST::InlineAsmRegOrRegClass>
     154                 :          39 : parse_reg (InlineAsmContext &inline_asm_ctx)
     155                 :             : {
     156                 :          39 :   using RegType = AST::InlineAsmRegOrRegClass::Type;
     157                 :          39 :   auto &parser = inline_asm_ctx.parser;
     158                 :             : 
     159                 :          39 :   if (!parser.skip_token (LEFT_PAREN))
     160                 :             :     {
     161                 :             :       // TODO: we expect a left parenthesis here, please return the correct
     162                 :             :       // error.
     163                 :           0 :       rust_unreachable ();
     164                 :             :       return tl::nullopt;
     165                 :             :     }
     166                 :             : 
     167                 :             :   // after successful left parenthesis parsing, we should return ast of
     168                 :             :   // InlineAsmRegOrRegClass of reg or reg class
     169                 :          39 :   auto token = parser.peek_current_token ();
     170                 :          39 :   AST::InlineAsmRegOrRegClass reg_class;
     171                 :          39 :   if (parser.skip_token (IDENTIFIER))
     172                 :             :     {
     173                 :             :       // construct a InlineAsmRegOrRegClass
     174                 :          37 :       reg_class.type = RegType::RegClass;
     175                 :          37 :       reg_class.reg_class.Symbol = token->as_string ();
     176                 :             :     }
     177                 :           2 :   else if (parser.skip_token (STRING_LITERAL))
     178                 :             :     {
     179                 :             :       // TODO: there is STRING_LITERAL, and BYTE_STRING_LITERAL, should we check
     180                 :             :       // for both?
     181                 :             : 
     182                 :             :       // construct a InlineAsmRegOrRegClass
     183                 :             :       // parse_format_string
     184                 :           2 :       reg_class.type = RegType::Reg;
     185                 :           2 :       inline_asm_ctx.is_explicit = true;
     186                 :           2 :       reg_class.reg_class.Symbol = token->as_string ();
     187                 :             :     }
     188                 :             :   else
     189                 :             :     {
     190                 :             :       // TODO: This should emit error
     191                 :             :       //  return
     192                 :             :       //  Err(p.dcx().create_err(errors::ExpectedRegisterClassOrExplicitRegister
     193                 :             :       //  {
     194                 :             :       //           span: p.token.span,
     195                 :             :       //       }));
     196                 :           0 :       rust_unreachable ();
     197                 :             :     }
     198                 :          39 :   if (!parser.skip_token (RIGHT_PAREN))
     199                 :             :     {
     200                 :             :       // TODO: we expect a left parenthesis here, please return the correct
     201                 :             :       // error.
     202                 :           0 :       rust_unreachable ();
     203                 :             :       return tl::nullopt;
     204                 :             :     }
     205                 :             : 
     206                 :          39 :   return reg_class;
     207                 :          39 : }
     208                 :             : 
     209                 :             : // From rustc
     210                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     211                 :          69 : parse_reg_operand (InlineAsmContext inline_asm_ctx)
     212                 :             : {
     213                 :             :   // let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
     214                 :             :   //           let (ident, _) = p.token.ident().unwrap();
     215                 :             :   //           p.bump();
     216                 :             :   //           p.expect(&token::Eq)?;
     217                 :             :   //           allow_templates = false;
     218                 :             :   //           Some(ident.name)
     219                 :             :   //       } else {
     220                 :             :   //           None
     221                 :             :   //       };
     222                 :          69 :   auto &parser = inline_asm_ctx.parser;
     223                 :          69 :   auto token = parser.peek_current_token ();
     224                 :          69 :   auto iden_token = parser.peek_current_token ();
     225                 :             : 
     226                 :          69 :   tl::optional<std::string> name = tl::nullopt;
     227                 :          69 :   if (check_identifier (parser, ""))
     228                 :             :     {
     229                 :           6 :       auto equal_token = parser.peek_current_token ();
     230                 :             : 
     231                 :           6 :       if (parser.skip_token (EQUAL))
     232                 :             :         {
     233                 :           6 :           name = token->as_string ();
     234                 :             :         }
     235                 :             :       else
     236                 :             :         {
     237                 :           0 :           rust_error_at (token->get_locus (),
     238                 :             :                          "expected operand, %s, options, or "
     239                 :             :                          "additional template string",
     240                 :             :                          "clobber_abi");
     241                 :           0 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     242                 :             :         }
     243                 :           6 :     }
     244                 :             : 
     245                 :          69 :   int slot = inline_asm_ctx.inline_asm.operands.size ();
     246                 :             : 
     247                 :             :   // Here is all parse_reg_operand functions we're using in a for loop
     248                 :          69 :   auto parse_funcs = {parse_reg_operand_in,        parse_reg_operand_out,
     249                 :             :                       parse_reg_operand_lateout,   parse_reg_operand_inout,
     250                 :             :                       parse_reg_operand_const,     parse_reg_operand_sym,
     251                 :          69 :                       parse_reg_operand_unexpected};
     252                 :             : 
     253                 :             :   // Loop over and execute the parsing functions, if the parser successfullly
     254                 :             :   // parses or if the parser fails to parse while it has committed to a token,
     255                 :             :   // we propogate the result.
     256                 :          69 :   tl::expected<InlineAsmContext, InlineAsmParseError> parsing_operand (
     257                 :          69 :     inline_asm_ctx);
     258                 :         275 :   for (auto &parse_func : parse_funcs)
     259                 :             :     {
     260                 :         275 :       auto result = parsing_operand.and_then (parse_func);
     261                 :             : 
     262                 :             :       // Per rust's asm.rs's structure
     263                 :             :       // After we've parse successfully, we break out and do a local validation
     264                 :             :       // of named, positional & explicit register operands
     265                 :             : 
     266                 :         275 :       if (result.has_value ())
     267                 :             :         {
     268                 :          39 :           inline_asm_ctx = *result;
     269                 :          39 :           break;
     270                 :             :         }
     271                 :         236 :       else if (result.error () == COMMITTED)
     272                 :             :         {
     273                 :          30 :           if (parse_func == parse_reg_operand_unexpected)
     274                 :          30 :             return inline_asm_ctx;
     275                 :             :           else
     276                 :           0 :             return result;
     277                 :             :         }
     278                 :             :     }
     279                 :             : 
     280                 :          39 :   auto &inline_asm = inline_asm_ctx.inline_asm;
     281                 :             : 
     282                 :          39 :   token = inline_asm_ctx.parser.peek_current_token ();
     283                 :          39 :   rust_debug_loc (token->get_locus (), "Here\n");
     284                 :          39 :   if (inline_asm_ctx.is_explicit)
     285                 :             :     {
     286                 :           2 :       if (name != tl::nullopt)
     287                 :             :         {
     288                 :           2 :           rust_error_at (token->get_locus (),
     289                 :             :                          "explicit register arguments cannot have names");
     290                 :           2 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     291                 :             :         }
     292                 :           0 :       inline_asm.reg_args.insert (slot);
     293                 :             :     }
     294                 :          37 :   else if (name != tl::nullopt)
     295                 :             :     {
     296                 :           4 :       if (inline_asm.named_args.find (name.value ())
     297                 :           4 :           != inline_asm.named_args.end ())
     298                 :             :         {
     299                 :           2 :           rust_error_at (token->get_locus (), "duplicate argument named %qs",
     300                 :           2 :                          name.value ().c_str ());
     301                 :           2 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     302                 :             :         }
     303                 :           2 :       inline_asm.named_args[name.value ()] = slot;
     304                 :             :     }
     305                 :             :   else
     306                 :             :     {
     307                 :          33 :       if (!inline_asm.named_args.empty () || !inline_asm.reg_args.empty ())
     308                 :             :         {
     309                 :             :           // positional arguments cannot follow named arguments or explicit
     310                 :             :           // register arguments
     311                 :           0 :           rust_error_at (token->get_locus (),
     312                 :             :                          "positional arguments cannot follow named arguments "
     313                 :             :                          "or explicit register arguments");
     314                 :           0 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     315                 :             :         }
     316                 :             :     }
     317                 :             : 
     318                 :          35 :   return inline_asm_ctx;
     319                 :         138 : }
     320                 :             : 
     321                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     322                 :          69 : parse_reg_operand_in (InlineAsmContext inline_asm_ctx)
     323                 :             : {
     324                 :             :   // For the keyword IN, currently we count it as a seperate keyword called
     325                 :             :   // Rust::IN search for #define RS_TOKEN_LIST in code base.
     326                 :          69 :   auto &parser = inline_asm_ctx.parser;
     327                 :          69 :   location_t locus = parser.peek_current_token ()->get_locus ();
     328                 :          69 :   if (!inline_asm_ctx.is_global_asm () && parser.skip_token (IN))
     329                 :             :     {
     330                 :          17 :       auto reg = parse_reg (inline_asm_ctx);
     331                 :             : 
     332                 :          17 :       if (parser.skip_token (UNDERSCORE))
     333                 :             :         {
     334                 :             :           // We are sure to be failing a test here, based on asm.rs
     335                 :             :           // https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
     336                 :           0 :           rust_unreachable ();
     337                 :             :           // return tl::unexpected<InlineAsmParseError> (COMMITTED);
     338                 :             :         }
     339                 :             : 
     340                 :          17 :       auto expr = parser.parse_expr ();
     341                 :             : 
     342                 :             :       // TODO: When we've succesfully parse an expr, remember to clone_expr()
     343                 :             :       // instead of nullptr
     344                 :          17 :       AST::InlineAsmOperand::In in (reg, std::move (expr));
     345                 :          17 :       inline_asm_ctx.inline_asm.operands.emplace_back (in, locus);
     346                 :          17 :       return inline_asm_ctx;
     347                 :          34 :     }
     348                 :          52 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     349                 :             : }
     350                 :             : 
     351                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     352                 :          52 : parse_reg_operand_out (InlineAsmContext inline_asm_ctx)
     353                 :             : {
     354                 :          52 :   auto &parser = inline_asm_ctx.parser;
     355                 :          52 :   location_t locus = parser.peek_current_token ()->get_locus ();
     356                 :         104 :   if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "out"))
     357                 :             :     {
     358                 :          20 :       auto reg = parse_reg (inline_asm_ctx);
     359                 :          20 :       std::unique_ptr<AST::Expr> expr = parser.parse_expr ();
     360                 :             : 
     361                 :          20 :       rust_assert (expr != nullptr);
     362                 :             : 
     363                 :             :       /*auto expr_ptr =
     364                 :             :          std::make_unique<AST::Expr>(AST::LiteralExpr(Literal))*/
     365                 :             : 
     366                 :             :       // TODO: When we've succesfully parse an expr, remember to clone_expr()
     367                 :             :       // instead of nullptr
     368                 :          20 :       AST::InlineAsmOperand::Out out (reg, false, std::move (expr));
     369                 :             : 
     370                 :          20 :       inline_asm_ctx.inline_asm.operands.emplace_back (out, locus);
     371                 :             : 
     372                 :          20 :       return inline_asm_ctx;
     373                 :          40 :     }
     374                 :             : 
     375                 :          32 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     376                 :             : }
     377                 :             : 
     378                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     379                 :          32 : parse_reg_operand_lateout (InlineAsmContext inline_asm_ctx)
     380                 :             : {
     381                 :          32 :   auto &parser = inline_asm_ctx.parser;
     382                 :          32 :   auto token = parser.peek_current_token ();
     383                 :          64 :   if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "lateout"))
     384                 :             :     {
     385                 :           0 :       rust_error_at (token->get_locus (),
     386                 :             :                      "The lateout feature is not implemented");
     387                 :           0 :       rust_unreachable ();
     388                 :             :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     389                 :             :     }
     390                 :             : 
     391                 :          32 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     392                 :          32 : }
     393                 :             : 
     394                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     395                 :          32 : parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
     396                 :             : {
     397                 :          32 :   auto &parser = inline_asm_ctx.parser;
     398                 :          32 :   auto token = parser.peek_current_token ();
     399                 :             : 
     400                 :          64 :   if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "inout"))
     401                 :             :     {
     402                 :           2 :       auto reg = parse_reg (inline_asm_ctx);
     403                 :             : 
     404                 :           2 :       if (parser.skip_token (UNDERSCORE))
     405                 :             :         {
     406                 :             :           // We are sure to be failing a test here, based on asm.rs
     407                 :             :           // https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
     408                 :           0 :           rust_error_at (token->get_locus (),
     409                 :             :                          "The lateout feature is not implemented");
     410                 :           0 :           rust_unreachable ();
     411                 :             :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     412                 :             :         }
     413                 :             : 
     414                 :             :       // TODO: Is error propogation our top priority, the ? in rust's asm.rs is
     415                 :             :       // doing a lot of work.
     416                 :             :       // TODO: Not sure how to use parse_expr
     417                 :           2 :       if (!check_identifier (parser, ""))
     418                 :           0 :         rust_unreachable ();
     419                 :             :       // auto expr = parse_format_string (inline_asm_ctx);
     420                 :             : 
     421                 :           2 :       std::unique_ptr<AST::Expr> out_expr;
     422                 :             : 
     423                 :           2 :       if (parser.skip_token (MATCH_ARROW))
     424                 :             :         {
     425                 :           0 :           if (!parser.skip_token (UNDERSCORE))
     426                 :             :             {
     427                 :             :               // auto result = parse_format_string (inline_asm_ctx);
     428                 :             : 
     429                 :           0 :               if (!check_identifier (parser, ""))
     430                 :           0 :                 rust_unreachable ();
     431                 :             :               // out_expr = parser.parse_expr();
     432                 :             :             }
     433                 :             : 
     434                 :             :           // TODO: Rembmer to pass in clone_expr() instead of nullptr
     435                 :             :           // https://github.com/rust-lang/rust/blob/a3167859f2fd8ff2241295469876a2b687280bdc/compiler/rustc_builtin_macros/src/asm.rs#L135
     436                 :             :           // RUST VERSION: ast::InlineAsmOperand::SplitInOut { reg, in_expr:
     437                 :             :           // expr, out_expr, late: false }
     438                 :             :           //      struct AST::InlineAsmOperand::SplitInOut split_in_out (reg,
     439                 :             :           // false, nullptr,
     440                 :             :           // nullptr);
     441                 :             :           // inline_asm_ctx.inline_asm.operands.push_back (split_in_out);
     442                 :             : 
     443                 :           0 :           return inline_asm_ctx;
     444                 :             :         }
     445                 :             :       else
     446                 :             :         {
     447                 :             :           // https://github.com/rust-lang/rust/blob/a3167859f2fd8ff2241295469876a2b687280bdc/compiler/rustc_builtin_macros/src/asm.rs#L137
     448                 :             :           // RUST VERSION: ast::InlineAsmOperand::InOut { reg, expr, late: false
     449                 :             :           // }
     450                 :             :           //      struct AST::InlineAsmOperand::InOut inout (reg, false,
     451                 :             :           // nullptr);
     452                 :             :           //      inline_asm_ctx.inline_asm.operands.push_back (inout);
     453                 :           2 :           return inline_asm_ctx;
     454                 :             :         }
     455                 :           4 :     }
     456                 :             : 
     457                 :          30 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     458                 :          32 : }
     459                 :             : 
     460                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     461                 :          30 : parse_reg_operand_const (InlineAsmContext inline_asm_ctx)
     462                 :             : {
     463                 :          30 :   auto &parser = inline_asm_ctx.parser;
     464                 :          60 :   if (parser.peek_current_token ()->get_id () == CONST)
     465                 :             :     {
     466                 :             :       // TODO: Please handle const with parse_expr instead.
     467                 :           0 :       auto anon_const = parse_format_string (inline_asm_ctx);
     468                 :           0 :       rust_unreachable ();
     469                 :             :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     470                 :             :     }
     471                 :             : 
     472                 :          30 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     473                 :             : }
     474                 :             : 
     475                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     476                 :          30 : parse_reg_operand_sym (InlineAsmContext inline_asm_ctx)
     477                 :             : {
     478                 :          30 :   auto &parser = inline_asm_ctx.parser;
     479                 :             : 
     480                 :          30 :   if (check_identifier (parser, "sym"))
     481                 :             :     {
     482                 :             :       // TODO: Please handle sym, which needs ExprKind::Path in Rust's asm.rs
     483                 :           0 :       rust_unreachable ();
     484                 :             :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     485                 :             :     }
     486                 :          30 :   return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
     487                 :             : }
     488                 :             : 
     489                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     490                 :          30 : parse_reg_operand_unexpected (InlineAsmContext inline_asm_ctx)
     491                 :             : {
     492                 :          30 :   auto token = inline_asm_ctx.parser.peek_current_token ();
     493                 :             :   // TODO: It is  weird that we can't seem to match any identifier,
     494                 :             :   // something must be wrong. consult compiler code in asm.rs or rust online
     495                 :             :   // compiler.
     496                 :             :   // rust_unreachable ();
     497                 :             : 
     498                 :             :   // rust_error_at (token->get_locus (), "ERROR RIGHT HERE");
     499                 :          30 :   return tl::unexpected<InlineAsmParseError> (COMMITTED);
     500                 :          30 : }
     501                 :             : 
     502                 :             : void
     503                 :          18 : check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option)
     504                 :             : {
     505                 :          18 :   auto &parser = inline_asm_ctx.parser;
     506                 :          18 :   auto &inline_asm = inline_asm_ctx.inline_asm;
     507                 :          18 :   if (inline_asm.options.count (option) != 0)
     508                 :             :     {
     509                 :             :       // TODO: report an error of duplication
     510                 :           4 :       rust_error_at (parser.peek_current_token ()->get_locus (),
     511                 :             :                      "the %qs option was already provided",
     512                 :           4 :                      InlineAsmOptionMap[option].c_str ());
     513                 :           4 :       return;
     514                 :             :     }
     515                 :             :   else
     516                 :             :     {
     517                 :          14 :       inline_asm.options.insert (option);
     518                 :             :     }
     519                 :             : }
     520                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     521                 :          14 : parse_options (InlineAsmContext &inline_asm_ctx)
     522                 :             : {
     523                 :          14 :   auto &parser = inline_asm_ctx.parser;
     524                 :          14 :   bool is_global_asm = inline_asm_ctx.inline_asm.is_global_asm;
     525                 :             :   // Parse everything commitedly
     526                 :          14 :   if (!parser.skip_token (LEFT_PAREN))
     527                 :             :     {
     528                 :           2 :       auto local_token = parser.peek_current_token ();
     529                 :           2 :       rust_error_at (local_token->get_locus (), "expected %qs, found %qs", "(",
     530                 :           2 :                      local_token->as_string ().c_str ());
     531                 :           2 :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     532                 :           2 :     }
     533                 :             : 
     534                 :          12 :   auto token = parser.peek_current_token ();
     535                 :          22 :   while (!parser.skip_token (RIGHT_PAREN))
     536                 :             :     {
     537                 :          44 :       if (!is_global_asm && check_identifier (parser, "pure"))
     538                 :             :         {
     539                 :           0 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::PURE);
     540                 :             :         }
     541                 :          44 :       else if (!is_global_asm && check_identifier (parser, "nomem"))
     542                 :             :         {
     543                 :           6 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::NOMEM);
     544                 :             :         }
     545                 :          32 :       else if (!is_global_asm && check_identifier (parser, "readonly"))
     546                 :             :         {
     547                 :           0 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::READONLY);
     548                 :             :         }
     549                 :          32 :       else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
     550                 :             :         {
     551                 :           0 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::PRESERVES_FLAGS);
     552                 :             :         }
     553                 :          32 :       else if (!is_global_asm && check_identifier (parser, "noreturn"))
     554                 :             :         {
     555                 :           6 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::NORETURN);
     556                 :             :         }
     557                 :          20 :       else if (!is_global_asm && check_identifier (parser, "nostack"))
     558                 :             :         {
     559                 :           2 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::NOSTACK);
     560                 :             :         }
     561                 :          16 :       else if (!is_global_asm && check_identifier (parser, "may_unwind"))
     562                 :             :         {
     563                 :           0 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::MAY_UNWIND);
     564                 :             :         }
     565                 :           8 :       else if (check_identifier (parser, "att_syntax"))
     566                 :             :         {
     567                 :           2 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::ATT_SYNTAX);
     568                 :             :         }
     569                 :           6 :       else if (check_identifier (parser, "raw"))
     570                 :             :         {
     571                 :           2 :           check_and_set (inline_asm_ctx, AST::InlineAsmOption::RAW);
     572                 :             :         }
     573                 :             :       else
     574                 :             :         {
     575                 :           4 :           rust_error_at (token->get_locus (),
     576                 :             :                          "expected one of %qs, %qs, %qs, %qs, %qs, %qs, %qs, "
     577                 :             :                          "%qs, %qs, or %qs, found %qs",
     578                 :             :                          ")", "att_syntax", "may_unwind", "nomem", "noreturn",
     579                 :             :                          "nostack", "preserves_flags", "pure", "raw",
     580                 :           4 :                          "readonly", token->as_string ().c_str ());
     581                 :           4 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     582                 :             :         }
     583                 :          18 :       if (parser.skip_token (RIGHT_PAREN))
     584                 :             :         {
     585                 :             :           break;
     586                 :             :         }
     587                 :             : 
     588                 :             :       // Parse comma as optional
     589                 :          10 :       if (parser.skip_token (COMMA))
     590                 :          10 :         continue;
     591                 :             :       else
     592                 :             :         {
     593                 :           0 :           rust_unreachable ();
     594                 :             :           token = parser.peek_current_token ();
     595                 :             :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     596                 :             :         }
     597                 :             :     }
     598                 :             : 
     599                 :             :   // TODO: Per rust asm.rs regarding options_spans
     600                 :             :   // I'm guessing this has to do with some error reporting.
     601                 :             :   // let new_span = span_start.to(p.prev_token.span);
     602                 :             :   // args.options_spans.push(new_span);
     603                 :             : 
     604                 :           8 :   return inline_asm_ctx;
     605                 :          12 : }
     606                 :             : 
     607                 :             : bool
     608                 :         513 : check_identifier (Parser<MacroInvocLexer> &p, std::string ident)
     609                 :             : {
     610                 :         513 :   auto token = p.peek_current_token ();
     611                 :             : 
     612                 :         513 :   if (token->get_id () == IDENTIFIER)
     613                 :             :     {
     614                 :         246 :       auto str = token->as_string ();
     615                 :             : 
     616                 :             :       // For non-promoted keywords, we need to also check for them.
     617                 :             : 
     618                 :         246 :       if (str == ident)
     619                 :             :         {
     620                 :          60 :           p.skip_token ();
     621                 :          60 :           return true;
     622                 :             :         }
     623                 :         186 :       if (ident == "")
     624                 :             :         {
     625                 :          28 :           if (potentially_nonpromoted_keywords.find (str)
     626                 :          28 :               == potentially_nonpromoted_keywords.end ())
     627                 :             :             {
     628                 :           8 :               p.skip_token ();
     629                 :           8 :               return true;
     630                 :             :             }
     631                 :             :           return false;
     632                 :             :         }
     633                 :         246 :     }
     634                 :             : 
     635                 :             :   return false;
     636                 :         513 : }
     637                 :             : 
     638                 :             : tl::optional<std::string>
     639                 :         106 : parse_format_string (InlineAsmContext &inline_asm_ctx)
     640                 :             : {
     641                 :         106 :   auto &parser = inline_asm_ctx.parser;
     642                 :         106 :   auto last_token_id = inline_asm_ctx.last_token_id;
     643                 :         106 :   auto token = parser.peek_current_token ();
     644                 :             : 
     645                 :         106 :   if (token->get_id () != last_token_id && token->get_id () == STRING_LITERAL)
     646                 :             :     {
     647                 :             :       // very nice, we got a supposedly formatted string.
     648                 :          56 :       parser.skip_token ();
     649                 :          56 :       return token->as_string ();
     650                 :             :     }
     651                 :             :   else
     652                 :             :     {
     653                 :          50 :       return tl::nullopt;
     654                 :             :     }
     655                 :         106 : }
     656                 :             : 
     657                 :             : tl::optional<AST::Fragment>
     658                 :          58 : MacroBuiltin::asm_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
     659                 :             :                            AST::InvocKind semicolon, AST::AsmKind is_global_asm)
     660                 :             : {
     661                 :          58 :   return parse_asm (invoc_locus, invoc, semicolon, is_global_asm);
     662                 :             : }
     663                 :             : 
     664                 :             : tl::optional<AST::Fragment>
     665                 :           2 : MacroBuiltin::llvm_asm_handler (location_t invoc_locus,
     666                 :             :                                 AST::MacroInvocData &invoc,
     667                 :             :                                 AST::InvocKind semicolon,
     668                 :             :                                 AST::AsmKind is_global_asm)
     669                 :             : {
     670                 :           2 :   return parse_llvm_asm (invoc_locus, invoc, semicolon, is_global_asm);
     671                 :             : }
     672                 :             : 
     673                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     674                 :          56 : parse_asm_arg (InlineAsmContext inline_asm_ctx)
     675                 :             : {
     676                 :          56 :   auto &parser = inline_asm_ctx.parser;
     677                 :          56 :   auto last_token_id = inline_asm_ctx.last_token_id;
     678                 :          56 :   auto token = parser.peek_current_token ();
     679                 :          56 :   tl::optional<std::string> fm_string;
     680                 :         129 :   while (token->get_id () != last_token_id)
     681                 :             :     {
     682                 :          89 :       token = parser.peek_current_token ();
     683                 :             : 
     684                 :          89 :       if (token->get_id () == COLON || token->get_id () == SCOPE_RESOLUTION)
     685                 :             :         {
     686                 :           0 :           rust_error_at (
     687                 :             :             token->get_locus (),
     688                 :             :             "the legacy LLVM-style %<asm!%> syntax is no longer supported");
     689                 :          16 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     690                 :             :         }
     691                 :             : 
     692                 :             :       // We accept a comma token here.
     693                 :          89 :       if (token->get_id () != COMMA
     694                 :          89 :           && inline_asm_ctx.consumed_comma_without_formatted_string)
     695                 :             :         {
     696                 :             :           // if it is not a comma, but we consumed it previously, this is fine
     697                 :             :           // but we have to set it to false tho.
     698                 :           0 :           inline_asm_ctx.consumed_comma_without_formatted_string = false;
     699                 :             :         }
     700                 :          89 :       else if (token->get_id () == COMMA
     701                 :          89 :                && !inline_asm_ctx.consumed_comma_without_formatted_string)
     702                 :             :         {
     703                 :          26 :           inline_asm_ctx.consumed_comma_without_formatted_string = false;
     704                 :          26 :           parser.skip_token ();
     705                 :             :         }
     706                 :          63 :       else if (token->get_id () == COMMA
     707                 :          63 :                && inline_asm_ctx.consumed_comma_without_formatted_string)
     708                 :             :         {
     709                 :             :           // We consumed comma, and there happens to also be a comma
     710                 :             :           // error should be: expected expression, found `,`
     711                 :           0 :           rust_error_at (token->get_locus (), "expected expression, found %qs",
     712                 :             :                          ",");
     713                 :           0 :           return tl::unexpected<InlineAsmParseError> (COMMITTED);
     714                 :          89 :           break;
     715                 :             :         }
     716                 :             : 
     717                 :             :       // And if that token comma is also the trailing comma, we break
     718                 :          89 :       token = parser.peek_current_token ();
     719                 :          89 :       if (token->get_id () == COMMA && token->get_id () == last_token_id)
     720                 :             :         {
     721                 :           0 :           parser.skip_token ();
     722                 :           0 :           break;
     723                 :             :         }
     724                 :             : 
     725                 :             :       // Ok after the left paren is good, we better be parsing correctly
     726                 :             :       // everything in here, which is operand in ABNF
     727                 :             : 
     728                 :             :       // Parse clobber abi, eat the identifier named "clobber_abi" if true
     729                 :          89 :       if (check_identifier (parser, "clobber_abi"))
     730                 :             :         {
     731                 :           6 :           auto expected = parse_clobber_abi (inline_asm_ctx);
     732                 :           6 :           if (expected.has_value ())
     733                 :           0 :             continue;
     734                 :           6 :           else if (expected.error () == COMMITTED)
     735                 :           6 :             return expected;
     736                 :             : 
     737                 :             :           // The error type is definitely non-committed (we have checked above),
     738                 :             :           // we are allowed to keep on parsing
     739                 :             :         }
     740                 :             : 
     741                 :          83 :       if (check_identifier (parser, "options"))
     742                 :             :         {
     743                 :          14 :           auto expected = parse_options (inline_asm_ctx);
     744                 :          14 :           if (expected.has_value ())
     745                 :           8 :             continue;
     746                 :           6 :           else if (expected.error () == COMMITTED)
     747                 :           6 :             return expected;
     748                 :             : 
     749                 :             :           // The error type is definitely non-committed (we have checked above),
     750                 :             :           // we are allowed to keep on parsing
     751                 :             :         }
     752                 :             : 
     753                 :             :       // Ok after we have check that neither clobber_abi nor options works, the
     754                 :             :       // only other logical choice is reg_operand
     755                 :             : 
     756                 :          69 :       auto expected = parse_reg_operand (inline_asm_ctx);
     757                 :          69 :       if (expected.has_value ())
     758                 :          65 :         continue;
     759                 :           4 :       else if (expected.error () == COMMITTED)
     760                 :           4 :         return expected;
     761                 :             : 
     762                 :             :       // Since parse_reg_operand is the last thing we've considered,
     763                 :             :       // The non-committed parse error type means that we have exhausted our
     764                 :             :       // search path
     765                 :             : 
     766                 :             :       // We then should return the error of COMMITTED, even though we have not
     767                 :             :       // committed to anything So that the error bubbles up and we recover from
     768                 :             :       // this error gracefully
     769                 :           0 :       rust_error_at (token->get_locus (),
     770                 :             :                      "expected operand, %s, options, or additional "
     771                 :             :                      "template string",
     772                 :             :                      "clobber_abi");
     773                 :           0 :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     774                 :             :     }
     775                 :          40 :   return tl::expected<InlineAsmContext, InlineAsmParseError> (inline_asm_ctx);
     776                 :          56 : }
     777                 :             : 
     778                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     779                 :          40 : expand_inline_asm_strings (InlineAsmContext inline_asm_ctx)
     780                 :             : {
     781                 :          40 :   auto &inline_asm = inline_asm_ctx.inline_asm;
     782                 :             : 
     783                 :          40 :   auto str_vec = inline_asm.get_template_strs ();
     784                 :             : 
     785                 :          40 :   decltype (str_vec) resulting_template_vec;
     786                 :          80 :   for (auto &template_str : str_vec)
     787                 :             :     {
     788                 :             :       /*std::cout << template_str.symbol << std::endl;*/
     789                 :             : 
     790                 :          40 :       auto pieces = Fmt::Pieces::collect (template_str.symbol, false,
     791                 :          40 :                                           Fmt::ffi::ParseMode::InlineAsm);
     792                 :          40 :       auto pieces_vec = pieces.get_pieces ();
     793                 :             : 
     794                 :          40 :       std::string transformed_template_str = "";
     795                 :         126 :       for (size_t i = 0; i < pieces_vec.size (); i++)
     796                 :             :         {
     797                 :          86 :           auto piece = pieces_vec[i];
     798                 :          86 :           if (piece.tag == Fmt::ffi::Piece::Tag::String)
     799                 :             :             {
     800                 :         110 :               transformed_template_str += piece.string._0.to_string ();
     801                 :             :             }
     802                 :          31 :           else if (piece.tag == Fmt::ffi::Piece::Tag::NextArgument)
     803                 :             :             {
     804                 :             :               /*    std::cout << "       " << i << ": "*/
     805                 :             :               /*<< piece.next_argument._0.to_string () << std::endl;*/
     806                 :             : 
     807                 :          31 :               auto next_argument = piece.next_argument._0;
     808                 :          31 :               switch (piece.next_argument._0.position.tag)
     809                 :             :                 {
     810                 :          31 :                   case Fmt::ffi::Position::Tag::ArgumentImplicitlyIs: {
     811                 :          31 :                     auto idx = next_argument.position.argument_implicitly_is._0;
     812                 :             :                     /*auto trait = next_argument.format;*/
     813                 :             :                     /*auto arg = arguments.at (idx);*/
     814                 :             : 
     815                 :             :                     /* // FIXME(Arthur): This API sucks*/
     816                 :             :                     /* rust_assert (arg.get_kind ().kind*/
     817                 :             :                     /*== AST::FormatArgumentKind::Kind::Normal);*/
     818                 :             :                     /**/
     819                 :             :                     /* args.push_back ({arg.get_expr ().clone_expr (),
     820                 :             :                      * trait});*/
     821                 :             : 
     822                 :          62 :                     transformed_template_str += "%" + std::to_string (idx);
     823                 :             :                     // std::cout << "argument implicitly is: " << idx <<
     824                 :             :                     // std::endl; std::cout << "transformed template str is:"
     825                 :             :                     // << transformed_template_str << std::endl;
     826                 :             :                     /*std::cout << "trait: " << trait.to_string () <<
     827                 :             :                      * std::endl;*/
     828                 :             :                     /*std::cout << "arg: " << arg.to_string () << std::endl;*/
     829                 :             :                   }
     830                 :          31 :                   break;
     831                 :           0 :                 case Fmt::ffi::Position::Tag::ArgumentIs:
     832                 :           0 :                 case Fmt::ffi::Position::Tag::ArgumentNamed:
     833                 :           0 :                   rust_sorry_at (inline_asm.get_locus (),
     834                 :             :                                  "unhandled argument position specifier");
     835                 :           0 :                   break;
     836                 :             :                 }
     837                 :             :             }
     838                 :             :         }
     839                 :          40 :       template_str.symbol = transformed_template_str;
     840                 :          40 :     }
     841                 :             : 
     842                 :          40 :   inline_asm.template_strs = str_vec;
     843                 :          40 :   return inline_asm_ctx;
     844                 :          40 : }
     845                 :             : 
     846                 :             : tl::optional<AST::Fragment>
     847                 :          58 : parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
     848                 :             :            AST::InvocKind semicolon, AST::AsmKind is_global_asm)
     849                 :             : {
     850                 :             :   // From the rule of asm.
     851                 :             :   // We first parse all formatted strings. If we fail, then we return
     852                 :             :   // tl::nullopt
     853                 :             : 
     854                 :             :   // We then parse the asm arguments. If we fail, then we return
     855                 :             :   // tl::nullopt
     856                 :             : 
     857                 :             :   // We then validate. If we fail, then we return tl::nullopt
     858                 :             : 
     859                 :             :   // Done
     860                 :          58 :   MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
     861                 :          58 :   Parser<MacroInvocLexer> parser (lex);
     862                 :          58 :   auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
     863                 :             : 
     864                 :          58 :   AST::InlineAsm inline_asm (invoc_locus,
     865                 :          58 :                              is_global_asm == AST::AsmKind::Global);
     866                 :          58 :   auto inline_asm_ctx = InlineAsmContext (inline_asm, parser, last_token_id);
     867                 :             : 
     868                 :          58 :   auto resulting_context = parse_format_strings (inline_asm_ctx)
     869                 :          58 :                              .and_then (parse_asm_arg)
     870                 :          58 :                              .and_then (validate)
     871                 :          58 :                              .and_then (expand_inline_asm_strings);
     872                 :             : 
     873                 :             :   // TODO: I'm putting the validation here because the rust reference put
     874                 :             :   // it here Per Arthur's advice we would actually do the validation in a
     875                 :             :   // different stage. and visit on the InlineAsm AST instead of it's
     876                 :             :   // context.
     877                 :          58 :   if (resulting_context)
     878                 :             :     {
     879                 :          40 :       auto node = (*resulting_context).inline_asm.clone_expr_without_block ();
     880                 :             : 
     881                 :          40 :       std::vector<AST::SingleASTNode> single_vec = {};
     882                 :             : 
     883                 :             :       // If the macro invocation has a semicolon (`asm!("...");`), then we
     884                 :             :       // need to make it a statement. This way, it will be expanded
     885                 :             :       // properly.
     886                 :          40 :       if (semicolon == AST::InvocKind::Semicoloned)
     887                 :          36 :         single_vec.emplace_back (AST::SingleASTNode (
     888                 :          72 :           std::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
     889                 :             :                                            semicolon
     890                 :          72 :                                              == AST::InvocKind::Semicoloned)));
     891                 :             :       else
     892                 :           4 :         single_vec.emplace_back (AST::SingleASTNode (std::move (node)));
     893                 :             : 
     894                 :          40 :       AST::Fragment fragment_ast
     895                 :             :         = AST::Fragment (single_vec,
     896                 :          40 :                          std::vector<std::unique_ptr<AST::Token>> ());
     897                 :          40 :       return fragment_ast;
     898                 :          40 :     }
     899                 :             :   else
     900                 :             :     {
     901                 :          18 :       return tl::nullopt;
     902                 :             :     }
     903                 :         116 : }
     904                 :             : 
     905                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     906                 :          58 : parse_format_strings (InlineAsmContext inline_asm_ctx)
     907                 :             : {
     908                 :             :   // Parse the first ever formatted string, success or not, will skip 1
     909                 :             :   // token
     910                 :          58 :   auto &parser = inline_asm_ctx.parser;
     911                 :          58 :   auto last_token_id = inline_asm_ctx.last_token_id;
     912                 :          58 :   auto fm_string = parse_format_string (inline_asm_ctx);
     913                 :             : 
     914                 :          58 :   auto &inline_asm = inline_asm_ctx.inline_asm;
     915                 :          58 :   auto token = parser.peek_current_token ();
     916                 :          58 :   if (fm_string == tl::nullopt)
     917                 :             :     {
     918                 :           2 :       rust_error_at (parser.peek_current_token ()->get_locus (),
     919                 :             :                      "%s template must be a string literal", "asm");
     920                 :           2 :       return tl::unexpected<InlineAsmParseError> (COMMITTED);
     921                 :             :     }
     922                 :             :   else
     923                 :             :     {
     924                 :          56 :       auto template_str
     925                 :             :         = AST::TupleTemplateStr (token->get_locus (),
     926                 :         112 :                                  strip_double_quotes (fm_string.value ()));
     927                 :          56 :       inline_asm.template_strs.push_back (template_str);
     928                 :          56 :     }
     929                 :             : 
     930                 :             :   // formatted string stream
     931                 :             : 
     932                 :         112 :   while (parser.peek_current_token ()->get_id () != last_token_id)
     933                 :             :     {
     934                 :          48 :       if (!parser.skip_token (COMMA))
     935                 :             :         {
     936                 :             :           break;
     937                 :             :         }
     938                 :             :       // Ok after the comma is good, we better be parsing correctly
     939                 :             :       // everything in here, which is formatted string in ABNF
     940                 :          48 :       inline_asm_ctx.consumed_comma_without_formatted_string = false;
     941                 :             : 
     942                 :          48 :       token = parser.peek_current_token ();
     943                 :          48 :       fm_string = parse_format_string (inline_asm_ctx);
     944                 :          48 :       if (fm_string == tl::nullopt)
     945                 :             :         {
     946                 :          48 :           inline_asm_ctx.consumed_comma_without_formatted_string = true;
     947                 :          48 :           break;
     948                 :             :         }
     949                 :             :       else
     950                 :             :         {
     951                 :           0 :           auto template_str
     952                 :             :             = AST::TupleTemplateStr (token->get_locus (),
     953                 :           0 :                                      strip_double_quotes (fm_string.value ()));
     954                 :           0 :           inline_asm.template_strs.push_back (template_str);
     955                 :           0 :         }
     956                 :             :     }
     957                 :             : 
     958                 :          56 :   return inline_asm_ctx;
     959                 :          58 : }
     960                 :             : 
     961                 :             : // bool
     962                 :             : // is_label (const std::string &potential_label)
     963                 :             : // {
     964                 :             : 
     965                 :             : //   if (potential_label.empty () || potential_label.back () != ':')
     966                 :             : //     return false;
     967                 :             : 
     968                 :             : //   // Check if all characters before the last colon are digits
     969                 :             : //   for (size_t i = 0; i < potential_label.length () - 1; i++)
     970                 :             : //   {
     971                 :             : //     if (potential_label[i] < '0' || potential_label[i] > '9')
     972                 :             : //       return false;
     973                 :             : //   }
     974                 :             : 
     975                 :             : //   return true;
     976                 :             : // }
     977                 :             : 
     978                 :             : tl::expected<InlineAsmContext, InlineAsmParseError>
     979                 :          40 : validate (InlineAsmContext inline_asm_ctx)
     980                 :             : {
     981                 :          40 :   return tl::expected<InlineAsmContext, InlineAsmParseError> (inline_asm_ctx);
     982                 :             : }
     983                 :             : 
     984                 :             : tl::optional<LlvmAsmContext>
     985                 :           2 : parse_llvm_templates (LlvmAsmContext ctx)
     986                 :             : {
     987                 :           2 :   auto &parser = ctx.parser;
     988                 :             : 
     989                 :           2 :   auto token = parser.peek_current_token ();
     990                 :             : 
     991                 :           2 :   if (token->get_id () == ctx.last_token_id
     992                 :           2 :       || token->get_id () != STRING_LITERAL)
     993                 :             :     {
     994                 :           0 :       return tl::nullopt;
     995                 :             :     }
     996                 :             : 
     997                 :           4 :   ctx.llvm_asm.get_templates ().emplace_back (token->get_locus (),
     998                 :           4 :                                               strip_double_quotes (
     999                 :           2 :                                                 token->as_string ()));
    1000                 :           2 :   ctx.parser.skip_token ();
    1001                 :             : 
    1002                 :           2 :   token = parser.peek_current_token ();
    1003                 :           2 :   if (token->get_id () != ctx.last_token_id && token->get_id () != COLON
    1004                 :           2 :       && token->get_id () != SCOPE_RESOLUTION)
    1005                 :             :     {
    1006                 :             :       // We do not handle multiple template string, we provide minimal support
    1007                 :             :       // for the black_box intrinsics.
    1008                 :           0 :       rust_unreachable ();
    1009                 :             :     }
    1010                 :             : 
    1011                 :           2 :   return ctx;
    1012                 :           2 : }
    1013                 :             : 
    1014                 :             : tl::optional<LlvmAsmContext>
    1015                 :           2 : parse_llvm_arguments (LlvmAsmContext ctx)
    1016                 :             : {
    1017                 :           2 :   auto &parser = ctx.parser;
    1018                 :           2 :   enum State
    1019                 :             :   {
    1020                 :             :     Templates = 0,
    1021                 :             :     Output,
    1022                 :             :     Input,
    1023                 :             :     Clobbers,
    1024                 :             :     Options
    1025                 :           2 :   } current_state
    1026                 :             :     = State::Templates;
    1027                 :             : 
    1028                 :           2 :   while (parser.peek_current_token ()->get_id () != ctx.last_token_id
    1029                 :          40 :          && parser.peek_current_token ()->get_id () != END_OF_FILE)
    1030                 :             :     {
    1031                 :          16 :       if (parser.peek_current_token ()->get_id () == SCOPE_RESOLUTION)
    1032                 :             :         {
    1033                 :           0 :           parser.skip_token (SCOPE_RESOLUTION);
    1034                 :           0 :           current_state = static_cast<State> (current_state + 2);
    1035                 :             :         }
    1036                 :             :       else
    1037                 :             :         {
    1038                 :           8 :           parser.skip_token (COLON);
    1039                 :           8 :           current_state = static_cast<State> (current_state + 1);
    1040                 :             :         }
    1041                 :             : 
    1042                 :           8 :       switch (current_state)
    1043                 :             :         {
    1044                 :           2 :         case State::Output:
    1045                 :           2 :           parse_llvm_outputs (ctx);
    1046                 :           2 :           break;
    1047                 :           2 :         case State::Input:
    1048                 :           2 :           parse_llvm_inputs (ctx);
    1049                 :           2 :           break;
    1050                 :           2 :         case State::Clobbers:
    1051                 :           2 :           parse_llvm_clobbers (ctx);
    1052                 :           2 :           break;
    1053                 :           2 :         case State::Options:
    1054                 :           2 :           parse_llvm_options (ctx);
    1055                 :           2 :           break;
    1056                 :           0 :         case State::Templates:
    1057                 :           0 :         default:
    1058                 :           0 :           rust_unreachable ();
    1059                 :             :         }
    1060                 :             :     }
    1061                 :             : 
    1062                 :           2 :   return ctx;
    1063                 :             : }
    1064                 :             : 
    1065                 :             : void
    1066                 :           4 : parse_llvm_operands (LlvmAsmContext &ctx, std::vector<AST::LlvmOperand> &result)
    1067                 :             : {
    1068                 :           4 :   auto &parser = ctx.parser;
    1069                 :           4 :   auto token = parser.peek_current_token ();
    1070                 :           6 :   while (token->get_id () != COLON && token->get_id () != SCOPE_RESOLUTION
    1071                 :           8 :          && token->get_id () != ctx.last_token_id)
    1072                 :             :     {
    1073                 :           2 :       std::string constraint;
    1074                 :           2 :       if (token->get_id () == STRING_LITERAL)
    1075                 :             :         {
    1076                 :           2 :           constraint = strip_double_quotes (token->as_string ());
    1077                 :             :         }
    1078                 :           2 :       parser.skip_token (STRING_LITERAL);
    1079                 :           2 :       parser.skip_token (LEFT_PAREN);
    1080                 :             : 
    1081                 :           2 :       token = parser.peek_current_token ();
    1082                 :             : 
    1083                 :           2 :       ParseRestrictions restrictions;
    1084                 :           2 :       restrictions.expr_can_be_null = true;
    1085                 :           2 :       auto expr = parser.parse_expr ();
    1086                 :             : 
    1087                 :           2 :       parser.skip_token (RIGHT_PAREN);
    1088                 :             : 
    1089                 :           2 :       result.emplace_back (constraint, std::move (expr));
    1090                 :             : 
    1091                 :           4 :       if (parser.peek_current_token ()->get_id () == COMMA)
    1092                 :           0 :         parser.skip_token (COMMA);
    1093                 :             : 
    1094                 :           2 :       token = parser.peek_current_token ();
    1095                 :           2 :     }
    1096                 :           4 : }
    1097                 :             : 
    1098                 :             : void
    1099                 :           2 : parse_llvm_outputs (LlvmAsmContext &ctx)
    1100                 :             : {
    1101                 :           2 :   parse_llvm_operands (ctx, ctx.llvm_asm.get_outputs ());
    1102                 :           2 : }
    1103                 :             : 
    1104                 :             : void
    1105                 :           2 : parse_llvm_inputs (LlvmAsmContext &ctx)
    1106                 :             : {
    1107                 :           2 :   parse_llvm_operands (ctx, ctx.llvm_asm.get_inputs ());
    1108                 :           2 : }
    1109                 :             : 
    1110                 :             : void
    1111                 :           2 : parse_llvm_clobbers (LlvmAsmContext &ctx)
    1112                 :             : {
    1113                 :           2 :   auto &parser = ctx.parser;
    1114                 :           2 :   auto token = parser.peek_current_token ();
    1115                 :           4 :   while (token->get_id () != COLON && token->get_id () != ctx.last_token_id)
    1116                 :             :     {
    1117                 :           2 :       if (token->get_id () == STRING_LITERAL)
    1118                 :             :         {
    1119                 :           2 :           ctx.llvm_asm.get_clobbers ().push_back (
    1120                 :           6 :             {strip_double_quotes (token->as_string ()), token->get_locus ()});
    1121                 :             :         }
    1122                 :           2 :       parser.skip_token (STRING_LITERAL);
    1123                 :           2 :       token = parser.peek_current_token ();
    1124                 :             :     }
    1125                 :           2 : }
    1126                 :             : 
    1127                 :             : void
    1128                 :           2 : parse_llvm_options (LlvmAsmContext &ctx)
    1129                 :             : {
    1130                 :           2 :   auto &parser = ctx.parser;
    1131                 :           2 :   auto token = parser.peek_current_token ();
    1132                 :             : 
    1133                 :           4 :   while (token->get_id () != ctx.last_token_id)
    1134                 :             :     {
    1135                 :           2 :       if (token->get_id () == STRING_LITERAL)
    1136                 :             :         {
    1137                 :           2 :           auto token_str = strip_double_quotes (token->as_string ());
    1138                 :             : 
    1139                 :           2 :           if (token_str == "volatile")
    1140                 :           2 :             ctx.llvm_asm.set_volatile (true);
    1141                 :           0 :           else if (token_str == "alignstack")
    1142                 :           0 :             ctx.llvm_asm.set_align_stack (true);
    1143                 :           0 :           else if (token_str == "intel")
    1144                 :           0 :             ctx.llvm_asm.set_dialect (AST::LlvmInlineAsm::Dialect::Intel);
    1145                 :             :           else
    1146                 :           0 :             rust_error_at (token->get_locus (),
    1147                 :             :                            "Unknown llvm assembly option %qs",
    1148                 :             :                            token_str.c_str ());
    1149                 :           2 :         }
    1150                 :           2 :       parser.skip_token (STRING_LITERAL);
    1151                 :             : 
    1152                 :           2 :       token = parser.peek_current_token ();
    1153                 :             : 
    1154                 :           2 :       if (token->get_id () == ctx.last_token_id)
    1155                 :           2 :         continue;
    1156                 :           0 :       parser.skip_token (COMMA);
    1157                 :             :     }
    1158                 :             : 
    1159                 :           2 :   parser.skip_token ();
    1160                 :           2 : }
    1161                 :             : 
    1162                 :             : tl::optional<AST::Fragment>
    1163                 :           2 : parse_llvm_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
    1164                 :             :                 AST::InvocKind semicolon, AST::AsmKind is_global_asm)
    1165                 :             : {
    1166                 :           2 :   MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
    1167                 :           2 :   Parser<MacroInvocLexer> parser (lex);
    1168                 :           2 :   auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
    1169                 :             : 
    1170                 :           2 :   AST::LlvmInlineAsm llvm_asm{invoc_locus};
    1171                 :             : 
    1172                 :           2 :   auto asm_ctx = LlvmAsmContext (llvm_asm, parser, last_token_id);
    1173                 :             : 
    1174                 :           2 :   auto resulting_context
    1175                 :           2 :     = parse_llvm_templates (asm_ctx).and_then (parse_llvm_arguments);
    1176                 :             : 
    1177                 :           2 :   if (resulting_context)
    1178                 :             :     {
    1179                 :           2 :       auto node = (*resulting_context).llvm_asm.clone_expr_without_block ();
    1180                 :             : 
    1181                 :           2 :       std::vector<AST::SingleASTNode> single_vec = {};
    1182                 :             : 
    1183                 :             :       // If the macro invocation has a semicolon (`asm!("...");`), then we
    1184                 :             :       // need to make it a statement. This way, it will be expanded
    1185                 :             :       // properly.
    1186                 :           2 :       if (semicolon == AST::InvocKind::Semicoloned)
    1187                 :           2 :         single_vec.emplace_back (AST::SingleASTNode (
    1188                 :           4 :           std::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
    1189                 :             :                                            semicolon
    1190                 :           4 :                                              == AST::InvocKind::Semicoloned)));
    1191                 :             :       else
    1192                 :           0 :         single_vec.emplace_back (AST::SingleASTNode (std::move (node)));
    1193                 :             : 
    1194                 :           2 :       AST::Fragment fragment_ast
    1195                 :             :         = AST::Fragment (single_vec,
    1196                 :           2 :                          std::vector<std::unique_ptr<AST::Token>> ());
    1197                 :           2 :       return fragment_ast;
    1198                 :           2 :     }
    1199                 :           0 :   return tl::nullopt;
    1200                 :           4 : }
    1201                 :             : 
    1202                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.