Skip to content

Commit

Permalink
Partial second layer of expected in parsing asm
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* expand/rust-macro-builtins-asm.cc (parseDirSpec):
	Partial second layer of expected in parsing asm
	(parse_clobber_abi): Likewise
	(parse_operand): Likewise
	(parse_reg_operand): Likewise
	(parse_asm_arg): Likewise
	* expand/rust-macro-builtins-asm.h (parse_clobber_abi): Likewise
	(parse_reg_operand): Likewise
	(parse_operand): Likewise
  • Loading branch information
badumbatish committed Jun 12, 2024
1 parent d97d5cc commit 9d6e180
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 69 deletions.
81 changes: 30 additions & 51 deletions gcc/rust/expand/rust-macro-builtins-asm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ std::map<AST::InlineAsmOption, std::string> InlineAsmOptionMap{
{AST::InlineAsmOption::RAW, "raw"},
};

int
parseDirSpec (Parser<MacroInvocLexer> &parser, TokenId last_token_id)
{
return 0;
}

int
parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
tl::expected<InlineAsmContext, std::string>
parse_clobber_abi (InlineAsmContext inline_asm_ctx)
{
// clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
// PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA clobber_abi
Expand All @@ -59,14 +53,16 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
{
rust_error_at (token->get_locus (),
"expected %<(%>, found end of macro arguments");
return -1;
return tl::unexpected<std::string> (
"Expected something else instead of end of macro arguments");
}
else
{
rust_error_at (token->get_locus (), "expected %<(%>, found %qs",
token->get_token_description ());
}
return -1;
return tl::unexpected<std::string> (
"Expected left parenthesis instead of something else");
}

if (parser.skip_token (RIGHT_PAREN))
Expand All @@ -76,7 +72,8 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
rust_error_at (
parser.peek_current_token ()->get_locus (),
"at least one abi must be provided as an argument to %<clobber_abi%>");
return -1;
return tl::unexpected<std::string> (
"at least one abi must be provided as an argument to %<clobber_abi%>");
}

std::vector<AST::TupleClobber> new_abis;
Expand Down Expand Up @@ -109,7 +106,7 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
// TODO: If the skip of comma is unsuccessful, which should be
// illegal, pleaes emit the correct error.
rust_unreachable ();
return -1;
return tl::unexpected<std::string> ("SKIP OF COMMA UNSUCCESSFUL");
}

token = parser.peek_current_token ();
Expand All @@ -123,7 +120,7 @@ parse_clobber_abi (InlineAsmContext &inline_asm_ctx)
inline_asm.clobber_abi.push_back (abi);
}

return 0;
return inline_asm_ctx;
}

tl::optional<AST::InlineAsmRegOrRegClass>
Expand Down Expand Up @@ -183,15 +180,9 @@ parse_reg (InlineAsmContext &inline_asm_ctx)
return reg_class;
}

int
parse_operand (InlineAsmContext &inline_asm_ctx)
{
return 0;
}

// From rustc
tl::optional<AST::InlineAsmOperand>
parse_reg_operand (InlineAsmContext &inline_asm_ctx)
tl::expected<InlineAsmContext, std::string>
parse_reg_operand (InlineAsmContext inline_asm_ctx)
{
// let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
// let (ident, _) = p.token.ident().unwrap();
Expand All @@ -204,15 +195,11 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// };
auto &parser = inline_asm_ctx.parser;
AST::InlineAsmOperand reg_operand;
rust_debug ("Enter parse_reg_operand");
auto token = parser.peek_current_token ();
auto iden_token = parser.peek_current_token ();
auto &inline_asm = inline_asm_ctx.inline_asm;
if (check_identifier (parser, ""))
{
rust_debug ("Didn't get passed identifier checking, %s",
token->as_string ().c_str ());

auto equal_token = parser.peek_current_token ();
if (!parser.skip_token (EQUAL))
{
Expand All @@ -227,8 +214,6 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// Rust::IN search for #define RS_TOKEN_LIST in code base.
if (!is_global_asm && parser.skip_token (IN))
{
rust_debug ("Enter parse_reg_operand in");

auto reg = parse_reg (inline_asm_ctx);

if (parser.skip_token (UNDERSCORE))
Expand All @@ -244,13 +229,11 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// instead of nullptr
struct AST::InlineAsmOperand::In in (reg, nullptr);
reg_operand.set_in (in);

return reg_operand;
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
return inline_asm_ctx;
}
else if (!is_global_asm && check_identifier (parser, "out"))
{
rust_debug ("Enter parse_reg_operand out");

auto reg = parse_reg (inline_asm_ctx);

auto expr = parse_format_string (inline_asm_ctx);
Expand All @@ -259,18 +242,17 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// instead of nullptr
struct AST::InlineAsmOperand::Out out (reg, false, nullptr);
reg_operand.set_out (out);
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);

return reg_operand;
return inline_asm_ctx;
}
else if (!is_global_asm && check_identifier (parser, "lateout"))
{
rust_unreachable ();
return tl::nullopt;
return inline_asm_ctx;
}
else if (!is_global_asm && check_identifier (parser, "inout"))
{
rust_debug ("Enter parse_reg_operand inout");

auto reg = parse_reg (inline_asm_ctx);

if (parser.skip_token (UNDERSCORE))
Expand All @@ -289,7 +271,6 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)

if (parser.skip_token (MATCH_ARROW))
{
rust_debug ("Matched MATCH_ARROW");
if (!parser.skip_token (UNDERSCORE))
{
parse_format_string (inline_asm_ctx);
Expand All @@ -304,8 +285,9 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
nullptr,
nullptr);
reg_operand.set_split_in_out (split_in_out);
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);

return reg_operand;
return inline_asm_ctx;
}
else
{
Expand All @@ -314,16 +296,14 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// }
struct AST::InlineAsmOperand::InOut inout (reg, false, nullptr);
reg_operand.set_in_out (inout);

return reg_operand;
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
return inline_asm_ctx;
}
}
else if (!is_global_asm && check_identifier (parser, "inlateout"))
// For reviewers, the parsing of inout and inlateout is exactly the same,
// Except here, the late flag is set to true.
{
rust_debug ("Enter parse_reg_operand inout");

auto reg = parse_reg (inline_asm_ctx);

if (parser.skip_token (UNDERSCORE))
Expand All @@ -342,7 +322,6 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)

if (parser.skip_token (MATCH_ARROW))
{
rust_debug ("Matched MATCH_ARROW");
if (!parser.skip_token (UNDERSCORE))
{
parse_format_string (inline_asm_ctx);
Expand All @@ -357,8 +336,8 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
nullptr,
nullptr);
reg_operand.set_split_in_out (split_in_out);

return reg_operand;
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
return inline_asm_ctx;
}
else
{
Expand All @@ -367,32 +346,33 @@ parse_reg_operand (InlineAsmContext &inline_asm_ctx)
// }
struct AST::InlineAsmOperand::InOut inout (reg, true, nullptr);
reg_operand.set_in_out (inout);

return reg_operand;
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
return inline_asm_ctx;
}
}
else if (parser.peek_current_token ()->get_id () == CONST)
{
// TODO: Please handle const with parse_expr instead.
auto anon_const = parse_format_string (inline_asm_ctx);
reg_operand.set_cnst (tl::nullopt);
return reg_operand;
rust_unreachable ();
return inline_asm_ctx;
}
else if (check_identifier (parser, "sym"))
{
// TODO: Please handle sym, which needs ExprKind::Path in Rust's asm.rs
rust_unreachable ();
return tl::nullopt;
return inline_asm_ctx;
}
else
{
// TODO: It is weird that we can't seem to match any identifier,
// something must be wrong. consult compiler code in asm.rs or rust online
// compiler.
rust_unreachable ();
return tl::nullopt;
return inline_asm_ctx;
}
return reg_operand;
return inline_asm_ctx;
}
void
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option)
Expand Down Expand Up @@ -593,7 +573,6 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)
}

// And if that token comma is also the trailing comma, we break
// TODO: Check with mentor see what last_token_id means
token = parser.peek_current_token ();
if (token->get_id () == COMMA && token->get_id () == last_token_id)
{
Expand Down
25 changes: 7 additions & 18 deletions gcc/rust/expand/rust-macro-builtins-asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class InlineAsmContext
parser (parser), last_token_id (last_token_id)
{}

// InlineAsmContext (InlineAsmContext && inline_asm_ctx)
// : allow_templates(inline_asm_ctx.allow_templates),
// is_explicit(inline_asm_ctx.is_explicit),
// consumed_comma_without_formatted_string(inline_asm_ctx.consumed_comma_without_formatted_string),
// inline_asm(inline_asm_ctx.inline_asm),
// parser(inline_asm_ctx.parser),
// last_token_id(inline_asm_ctx.last_token_id) {}

bool is_global_asm () { return inline_asm.is_global_asm; }

bool allows_templates () { return allow_templates; }
Expand All @@ -58,6 +50,13 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx);
tl::expected<InlineAsmContext, std::string>
parse_format_strings (InlineAsmContext inline_asm_ctx);

tl::expected<InlineAsmContext, std::string>
parse_clobber_abi (InlineAsmContext inline_asm_ctx);

// From rustc
tl::expected<InlineAsmContext, std::string>
parse_reg_operand (InlineAsmContext inline_asm_ctx);

tl::optional<AST::Fragment>
parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
bool is_global_asm);
Expand All @@ -67,13 +66,6 @@ check_identifier (Parser<MacroInvocLexer> &parser, std::string ident);

void
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option);
// From rustc
int
parse_operand (InlineAsmContext &inline_asm_ctx);

// From rustc
tl::optional<AST::InlineAsmOperand>
parse_reg_operand (InlineAsmContext &inline_asm_ctx);

// From rustc
int
Expand All @@ -83,9 +75,6 @@ parse_options (InlineAsmContext &inline_asm_ctx);
tl::optional<AST::InlineAsmRegOrRegClass>
parse_reg (InlineAsmContext &inline_asm_ctx);

int
parse_clobber_abi (InlineAsmContext &inline_asm_ctx);

tl::optional<std::string>
parse_format_string (InlineAsmContext &inline_asm_ctx);

Expand Down

0 comments on commit 9d6e180

Please sign in to comment.