Rust-GCC / gccrs

GCC Front-End for Rust
https://rust-gcc.github.io/
GNU General Public License v2.0
2.34k stars 151 forks source link

Implement `asm!` macro #1566

Open CohenArthur opened 1 year ago

CohenArthur commented 1 year ago

There are a few things needed for proper inline assembly handling

The HIR node will definitely help for the unsafe checks regarding asm!()

liushuyu commented 1 year ago

References:

mvvsmk commented 7 months ago

@CohenArthur , I am trying to understand how macro parsing work in order to impliment Implement proper parsing of the asm string could you give me some pointers as to which files I should take a look at ? and at which stage macro parsing begins, because as I understand it it happnes lazy-ly but at which stage?

CohenArthur commented 7 months ago

@mvvsmk take a look at gcc/rust/expand/rust-macro-builtins.cc. The tables at the top help understand the structure of builtin macro handlers, so for example for include_str we have the following entry:

    {"include_str", MacroBuiltin::include_str_handler},

meaning that the include_str_handler will be called upon seeing a call to include_str!(). If you have a look at the handler, it is responsible for parsing its input and transforming it:

tl::optional<AST::Fragment>
MacroBuiltin::include_str_handler (location_t invoc_locus,
                   AST::MacroInvocData &invoc)
{
  /* Get target filename from the macro invocation, which is treated as a path
     relative to the include!-ing file (currently being compiled).  */
  auto lit_expr
    = parse_single_string_literal (BuiltinMacro::IncludeStr,
                   invoc.get_delim_tok_tree (), invoc_locus,
                   invoc.get_expander ());
  if (lit_expr == nullptr)
    return AST::Fragment::create_error ();

so the goal would be to do the same for asm!() and implement proper parsing of the inline assembly format. at the moment this is the entry for asm!():

    {"asm", MacroBuiltin::sorry},

because it's not implemented yet and there is no handler for it