Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Support syntax and opcodes from the loongson-community/loongarch-opcodes project #7

Draft
wants to merge 5 commits into
base: loongarch-2_37
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions gas/config/tc-loongarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ enum options
OPTION_LA_GLOBAL_WITH_PCREL,
OPTION_LA_GLOBAL_WITH_ABS,

OPTION_SYNTAX,

OPTION_END_OF_ENUM,
};

Expand All @@ -121,6 +123,8 @@ struct option md_longopts[] =
{ "mla-global-with-pcrel", no_argument, NULL, OPTION_LA_GLOBAL_WITH_PCREL },
{ "mla-global-with-abs", no_argument, NULL, OPTION_LA_GLOBAL_WITH_ABS },

{ "msyntax", required_argument, NULL, OPTION_SYNTAX },

{ NULL, no_argument, NULL, 0 }
};

Expand Down Expand Up @@ -155,6 +159,14 @@ md_parse_option (int c, const char *arg)
case OPTION_LA_GLOBAL_WITH_ABS:
LARCH_opts.la_global_with_abs = 1;
break;
case OPTION_SYNTAX:
if (strcasecmp (arg, "vendor") == 0)
LARCH_opts.use_community_syntax = 0;
else if (strcasecmp (arg, "community") == 0)
LARCH_opts.use_community_syntax = 1;
else
as_fatal (_("invalid -msyntax= option: `%s'"), arg);
break;
case OPTION_IGNORE:
break;
}
Expand Down Expand Up @@ -338,14 +350,22 @@ s_dtprel (int bytes)
demand_empty_rest_of_line ();
}

static const pseudo_typeS loongarch_pseudo_table[] =
static void
s_community_syntax (int enabled)
{
LARCH_opts.use_community_syntax = enabled;
}

static const pseudo_typeS loongarch_pseudo_table[] =
{
{ "align", s_loongarch_align, -4 },
{ "dword", cons, 8 },
{ "word", cons, 4 },
{ "half", cons, 2 },
{ "dtprelword", s_dtprel, 4 },
{ "dtpreldword", s_dtprel, 8 },
{ "community_syntax", s_community_syntax, 1 },
{ "vendor_syntax", s_community_syntax, 0 },
{ NULL, NULL, 0 },
};

Expand Down Expand Up @@ -620,8 +640,22 @@ get_loongarch_opcode (struct loongarch_cl_insn *insn)
str_hash_insert (ase->name_hash_entry, it->name, (void *) it, 0);
}

if (LARCH_opts.use_community_syntax && !ase->community_name_hash_entry)
{
ase->community_name_hash_entry = str_htab_create ();
for (it = ase->opcodes; it->name; it++)
if (it->community_name)
str_hash_insert (ase->community_name_hash_entry,
it->community_name, (void *) it, 0);
}

if ((it = str_hash_find (ase->name_hash_entry, insn->name)) == NULL)
continue;
{
if (!LARCH_opts.use_community_syntax)
continue;
if ((it = str_hash_find (ase->community_name_hash_entry, insn->name)) == NULL)
continue;
}

do
{
Expand All @@ -631,7 +665,8 @@ get_loongarch_opcode (struct loongarch_cl_insn *insn)
insn->arg_num = 0;
insn->reloc_num = 0;
insn->insn_bin = loongarch_foreach_args (
it->format, insn->arg_strs,
(LARCH_opts.use_community_syntax && it->community_format) ? it->community_format : it->format,
insn->arg_strs,
loongarch_args_parser_can_match_arg_helper, insn);
if (insn->all_match && !(it->include && !*it->include) &&
!(it->exclude && *it->exclude))
Expand All @@ -641,7 +676,7 @@ get_loongarch_opcode (struct loongarch_cl_insn *insn)
}
it++;
}
while (it->name && strcasecmp (it->name, insn->name) == 0);
while (it->name && strcasecmp (LARCH_opts.use_community_syntax ? it->community_name : it->name, insn->name) == 0);
}
}

Expand Down Expand Up @@ -672,7 +707,19 @@ check_this_insn_before_appending (struct loongarch_cl_insn *ip)
(ip->insn_bin & 0xff800000) == 0x00800000))
{
/* for bstr(ins|pick).[wd] */
if (ip->args[2] < ip->args[3])
int msb, lsb;
if (LARCH_opts.use_community_syntax)
{
lsb = ip->args[2];
msb = ip->args[3];
}
else
{
msb = ip->args[2];
lsb = ip->args[3];
}

if (msb < lsb)
as_fatal (_ ("bstr(ins|pick).[wd] require msbd >= lsbd"));
}
else if (ip->insn->mask != 0 && (ip->insn_bin & 0xfe0003c0) == 0x04000000 &&
Expand Down
25 changes: 19 additions & 6 deletions include/opcode/loongarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ extern "C"
#define LARCH_INSN_OPC(insn) ((insn & 0xf0000000) >> 28)
const char *const name;

/*
/* The instruction name as given by the
* https://github.com/loongson-community/loongarch-opcodes repo.
* NULL if the name is identical to the manual version. */
const char *const community_name;

/*
ACTUAL PARAMETER:

// BNF with regular expression.
// BNF with regular expression.
args : token* end

// just few char separate 'iden'
Expand All @@ -63,7 +68,7 @@ That 'sr' means the instruction may need relocate. '10:16' means bit field of in
In a 'format', every 'escape's can be replaced to 'iden' or 'regname' acrroding to its meaning.
We fill all information needed by disassembing and assembing to 'format'.

// BNF with regular expression.
// BNF with regular expression.
format : escape (literal+ escape)* literal* end
| (literal+ escape)* literal* end

Expand All @@ -80,8 +85,8 @@ escape : esc_ch bit_field '<' '<' dec2
| esc_ch bit_field
| esc_ch // for MACRO. non-macro format must indicate 'bit_field'

// '|' means to concatenate nonadjacent bit fields
// For example, "10:16|0:4" means
// '|' means to concatenate nonadjacent bit fields
// For example, "10:16|0:4" means
// "16 bits starting from the 10th bit concatenating with 4 bits starting from the 0th bit".
// This is to say "[25..10]||[3..0]" (little endian).
b_field : dec2 ':' dec2
Expand All @@ -102,11 +107,16 @@ dec2 : [1-9][0-9]?
*/
const char *const format;

/* The same as above, but adhering to the format and ordering as specified
* by the https://github.com/loongson-community/loongarch-opcodes repo.
* NULL if the two syntaxes have the same format. */
const char *const community_format;

/*
MACRO: Indicate how a macro instruction expand for assembling.
The main is to replace the '%num'(means the 'num'th 'escape' in 'format') in 'macro' string to get the real instruction.

Maybe need
Maybe need
*/
const char *const macro;
const int *include;
Expand All @@ -131,6 +141,7 @@ Maybe need

/* for GAS to create hash table. */
struct htab *name_hash_entry;
struct htab *community_name_hash_entry;
};

extern int is_unsigned (const char *);
Expand Down Expand Up @@ -206,6 +217,8 @@ Maybe need

int abi_is_lp32;
int abi_is_lp64;

int use_community_syntax;
} LARCH_opts;

extern size_t loongarch_insn_length (insn_t insn);
Expand Down
7 changes: 7 additions & 0 deletions opcodes/loongarch-coder.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ loongarch_expand_macro_with_format_map (
const char *src;
char *dest;
char buffer[8192];
int old_syntax_flag;

old_syntax_flag = LARCH_opts.use_community_syntax;
LARCH_opts.use_community_syntax = 0;

if (format)
loongarch_parse_format (format, esc1s, esc2s, bit_fields);
Expand Down Expand Up @@ -496,6 +500,9 @@ loongarch_expand_macro_with_format_map (
*dest++ = *src++;

*dest = '\0';

LARCH_opts.use_community_syntax = old_syntax_flag;

return strdup (buffer);
}

Expand Down
Loading