Skip to content

Commit

Permalink
Move strip double quotes, add scaffold expand
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-asm.cc (strip_double_quotes):
	Move strip double quotes to parse phase
	(CompileAsm::asm_construct_string_tree): Likewise
	* backend/rust-compile-asm.h (strip_double_quotes): Likewise
	* expand/rust-macro-builtins-asm.cc (strip_double_quotes):
	Likewise
	(expand_inline_asm): Renamed to expand_inline_asm_strings
	(expand_inline_asm_strings): Renamed from expand_inline_asm
	(parse_asm): Move strip double quotes to parse phase
	(parse_format_strings): Likewise
	* expand/rust-macro-builtins-asm.h (strip_double_quotes):
	Likewise
	(expand_inline_asm_strings): Inline asm expansion fn
	(expand_inline_asm_string): Inline asm expansion fn
  • Loading branch information
badumbatish committed Jul 27, 2024
1 parent e84d9dc commit eb10f51
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
16 changes: 1 addition & 15 deletions gcc/rust/backend/rust-compile-asm.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
#include "rust-compile-asm.h"

#include "rust-system.h"
namespace Rust {
namespace Compile {

std::string
strip_double_quotes (const std::string &str)
{
// Helper function strips the beginning and ending double quotes from a
// string.
std::string result = str;

rust_assert (result.size () >= 3);
result.erase (0, 1);
result.erase (result.size () - 1, 1);
return result;
}

CompileAsm::CompileAsm (Context *ctx)
: HIRCompileBase (ctx), translated (error_mark_node)
{}
Expand Down Expand Up @@ -86,7 +72,7 @@ CompileAsm::asm_construct_string_tree (HIR::InlineAsm &expr)
// debugging and reading)
std::stringstream ss;
for (const auto &template_str : expr.template_strs)
ss << strip_double_quotes (template_str.symbol) << "\n\t";
ss << template_str.symbol << "\n\t";

std::string result = ss.str ();
return build_string (result.size () + 1, result.c_str ());
Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/backend/rust-compile-asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
namespace Rust {
namespace Compile {

std::string
strip_double_quotes (const std::string &);

class CompileAsm : private HIRCompileBase, protected HIR::HIRExpressionVisitor
{
private:
Expand Down
38 changes: 28 additions & 10 deletions gcc/rust/expand/rust-macro-builtins-asm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,21 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)
return tl::expected<InlineAsmContext, InlineAsmParseError> (inline_asm_ctx);
}

std::string
strip_double_quotes (const std::string &str)
{
// Helper function strips the beginning and ending double quotes from a
// string.
std::string result = str;

rust_assert (result.size () >= 3);
result.erase (0, 1);
result.erase (result.size () - 1, 1);
return result;
}

tl::expected<InlineAsmContext, InlineAsmParseError>
expand_inline_asm (InlineAsmContext &inline_asm_ctx)
expand_inline_asm_strings (InlineAsmContext &inline_asm_ctx)
{
auto &inline_asm = inline_asm_ctx.inline_asm;

Expand All @@ -762,15 +775,18 @@ expand_inline_asm (InlineAsmContext &inline_asm_ctx)
std::cout << template_str.symbol << std::endl;

auto pieces = Fmt::Pieces::collect (template_str.symbol, false,
Fmt::ffi::ParseMode::Format)
.get_pieces ();
Fmt::ffi::ParseMode::InlineAsm);
auto pieces_vec = pieces.get_pieces ();

for (size_t i = 0; i < pieces.size (); i++)
for (size_t i = 0; i < pieces_vec.size (); i++)
{
auto piece = pieces[i];
auto piece = pieces_vec[i];
if (piece.tag == Fmt::ffi::Piece::Tag::String)
std::cout << " " << i << ": " << piece.string._0.to_string ()
<< std::endl;
{
}
/*std::cout << " " << i << ": " << piece.string._0.to_string
* ()*/
/* << std::endl;*/
}
}

Expand Down Expand Up @@ -807,7 +823,7 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
auto is_valid = (bool) resulting_context;
if (is_valid)
{
expand_inline_asm (resulting_context.value ());
expand_inline_asm_strings (*resulting_context);
}
if (is_valid)
{
Expand Down Expand Up @@ -855,7 +871,8 @@ parse_format_strings (InlineAsmContext inline_asm_ctx)
else
{
auto template_str
= AST::TupleTemplateStr (token->get_locus (), fm_string.value ());
= AST::TupleTemplateStr (token->get_locus (),
strip_double_quotes (fm_string.value ()));
inline_asm.template_strs.push_back (template_str);
}

Expand All @@ -881,7 +898,8 @@ parse_format_strings (InlineAsmContext inline_asm_ctx)
else
{
auto template_str
= AST::TupleTemplateStr (token->get_locus (), fm_string.value ());
= AST::TupleTemplateStr (token->get_locus (),
strip_double_quotes (fm_string.value ()));
inline_asm.template_strs.push_back (template_str);
}
}
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/expand/rust-macro-builtins-asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "system.h"
namespace Rust {

std::string
strip_double_quotes (const std::string &str);

enum InlineAsmParseError
{
// Enum for InlineAsmParseError
Expand Down Expand Up @@ -84,6 +87,12 @@ class InlineAsmContext
}
};

tl::expected<InlineAsmContext, InlineAsmParseError>
expand_inline_asm_strings (InlineAsmContext &inline_asm_ctx);

tl::expected<InlineAsmContext, InlineAsmParseError>
expand_inline_asm_string (InlineAsmContext &inline_asm_ctx);

// Expected calls
WARN_UNUSED_RESULT
tl::expected<InlineAsmContext, InlineAsmParseError>
Expand Down

0 comments on commit eb10f51

Please sign in to comment.