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

LoongArch: gas: add support using variable for li.w/d #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions gas/config/loongarch-parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ my_getExpression (expressionS *ep, const char *str)
return ret;
}

/* op is var name? */
static void
emit_const_var (const char *op)
{
expressionS ep;
if (end <= top)
as_fatal (_("expr too huge"));
my_getExpression(&ep, op);
if (ep.X_op != O_constant)
as_bad("illegal operand: %s", op);
top->value.X_op = O_constant;
top->value.X_add_number = ep.X_add_number;
top->type = BFD_RELOC_LARCH_SOP_PUSH_ABSOLUTE;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the resulting object file will have this reloc record? Because we may be working with lld/mold that cannot process stack-based relocations, in which case we actually want to substitute the value in, as if the user had written the concrete value in the respective slot. Otherwise the approach is mostly okay.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once it is recognized as a constant, it will discarded this reloc in subsequent processing, and obviously object file will have not this reloc record.

top++;
}


static void
reloc (const char *op_c_str, const char *id_c_str, offsetT addend)
{
Expand Down Expand Up @@ -318,6 +335,7 @@ offsetT imm;

primary_expression
: INTEGER {emit_const ($1);}
| IDENTIFIER {emit_const_var ($1);}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging from this, isn't the change applicable to more insns than li.[wd] alone?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're exactly right. Let me fix this again to only works for li.w./d

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're exactly right. Let me fix this again to only works for li.w./d

No no no. In its current form it's way more ergonomic. I for example would be very surprised to find out that variable substitution only works for li.[wd], I'd curse loudly for the anti-feature.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you'd simply write more test cases to showcase the usage with a wide variety of insns, and reword the commit message. Just don't restrict to li only.

Copy link
Author

@Calring Calring Aug 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we know, the li.[wd] is a macro instruction used to load immediate. li.w can load 32 bits immediate, and li.d can load 64 bits immediate. If a programmer need to load immediate, li.[wd] is completely competent. If other instructions also support, for programmer who don't know instructions immediate bits width, It is easy to make mistakes . For eaxmple, addi.[wd] rd, rj, var can only load 12 bits width immediate, if var bits width exceeds 12, it will be not work. So support li.[wd] is enough, that's my thought.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that case you may want to improve the error message so the user is hinted towards li.[wd]. Inconsistency is not good for learners, despite your good intention. It is bound to cause confusion, believe me...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I got you, you're right. I will add more testcases for other instructions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this feature of other ARCHs like x86, their behavior is really the same as you said!!!That's cool!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this feature of other ARCHs like x86, their behavior is really the same as you said!!!That's cool!

Told you. ;-)

| '(' expression ')'
| '%' IDENTIFIER '(' IDENTIFIER addend ')' {reloc ($2, $4, $5); free ($2); free ($4);}
| '%' IDENTIFIER '(' INTEGER addend ')' {reloc ($2, NULL, $4 + $5); free ($2);}
Expand Down
18 changes: 0 additions & 18 deletions gas/config/tc-loongarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,24 +582,6 @@ loongarch_args_parser_can_match_arg_helper (char esc_ch1, char esc_ch2,
if (!ip->match_now)
break;

if (esc_ch1 == 's')
switch (esc_ch2)
{
case 'c':
ip->match_now = reloc_num == 0;
break;
}
else
switch (esc_ch2)
{
case 'c':
ip->match_now = reloc_num == 0 && 0 <= imm;
break;
}

if (!ip->match_now)
break;

ret = imm;
if (reloc_num)
{
Expand Down
79 changes: 79 additions & 0 deletions gas/testsuite/gas/loongarch/imm_ins.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#as:
#objdump: -dr

.*:[ ]+file format .*


Disassembly of section .text:

00000000.* <.text>:
[ ]+0:[ ]+03848c0c[ ]+ori[ ]+\$t0,[ ]+\$zero,[ ]+0x123
[ ]+4:[ ]+15ffe00d[ ]+lu12i.w[ ]+\$t1,[ ]+-256\(0xfff00\)
[ ]+8:[ ]+16001fed[ ]+lu32i.d[ ]+\$t1,[ ]+255\(0xff\)
[ ]+c:[ ]+02bffc0e[ ]+addi.w[ ]+\$t2,[ ]+\$zero,[ ]+-1\(0xfff\)
[ ]+10:[ ]+1601ffee[ ]+lu32i.d[ ]+\$t2,[ ]+4095\(0xfff\)
[ ]+14:[ ]+0004b58b[ ]+alsl.w[ ]+\$a7,[ ]+\$t0,[ ]+\$t1,[ ]+0x2
[ ]+18:[ ]+0006b58b[ ]+alsl.wu[ ]+\$a7,[ ]+\$t0,[ ]+\$t1,[ ]+0x2
[ ]+1c:[ ]+0009358b[ ]+bytepick.w[ ]+\$a7,[ ]+\$t0,[ ]+\$t1,[ ]+0x2
[ ]+20:[ ]+000d358b[ ]+bytepick.d[ ]+\$a7,[ ]+\$t0,[ ]+\$t1,[ ]+0x2
[ ]+24:[ ]+002a0002[ ]+break[ ]+0x2
[ ]+28:[ ]+002a8002[ ]+dbcl[ ]+0x2
[ ]+2c:[ ]+002b0002[ ]+syscall[ ]+0x2
[ ]+30:[ ]+002cb58b[ ]+alsl.d[ ]+\$a7,[ ]+\$t0,[ ]+\$t1,[ ]+0x2
[ ]+34:[ ]+0040898b[ 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 ]+291\(0x123\)
[ ]+10c:[ ]+2b448d8a[ ]+fst.s[ ]+\$ft2,[ ]+\$t0,[ ]+291\(0x123\)
[ ]+110:[ ]+2b848d8a[ ]+fld.d[ ]+\$ft2,[ ]+\$t0,[ ]+291\(0x123\)
[ ]+114:[ ]+2bc48d8a[ ]+fst.d[ ]+\$ft2,[ ]+\$t0,[ ]+291\(0x123\)
85 changes: 85 additions & 0 deletions gas/testsuite/gas/loongarch/imm_ins.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.equ a, 0x123
.equ b, 0xfffff00000
.equ c, 0xfffffffffff
.equ d, 2
.equ e,0x100

li.w $r12, a
li.d $r13, b
li.d $r14, c

alsl.w $r11,$r12,$r13,d
alsl.wu $r11,$r12,$r13,d
bytepick.w $r11,$r12,$r13,d
bytepick.d $r11,$r12,$r13,d

break d
dbcl d
syscall d

alsl.d $r11,$r12, $r13,d
slli.w $r11,$r12,d
slli.d $r11,$r12,d
srli.w $r11,$r12,d
srli.d $r12,$r13,d
srai.w $r12,$r13,d
srai.d $r12,$r13,d

bstrins.w $r12,$r13,d,d
bstrins.d $r12,$r13,d,d
bstrpick.d $r12,$r13,d,d
bstrpick.d $r12,$r13,d,d

slti $r12,$r13,a
sltui $r12,$r13,a
addi.w $r12,$r13,a
addi.d $r12,$r13,a
lu52i.d $r12,$r13,a
andi $r12,$r13,d
ori $r12,$r13,d
xori $r12,$r13,d
addu16i.d $r12,$r13,d
lu12i.w $r12,a
lu32i.d $r12,a
pcaddi $r12,a
pcalau12i $r12,a
pcaddu12i $r12,a
pcaddu18i $r12,a

csrrd $r12,a
csrwr $r12,a
csrxchg $r12,$r13,d
cacop d,$r13,d
lddir $r12,$r13,d
ldpte $r12,d

invtlb d,$r13,$r14

ll.w $r12,$r13,e
sc.w $r12,$r13,e
ll.d $r12,$r13,e
sc.d $r12,$r13,e
ldptr.w $r12,$r13,e
stptr.w $r12,$r13,e
ldptr.d $r12,$r13,e
stptr.d $r12,$r13,e
ld.b $r12,$r13,e
ld.h $r12,$r13,e
ld.w $r12,$r13,e
ld.d $r12,$r13,e
st.b $r12,$r13,e
st.h $r12,$r13,e
st.w $r12,$r13,e
st.d $r12,$r13,e
ld.bu $r12,$r13,e
ld.hu $r12,$r13,e
ld.wu $r12,$r13,e
preld d,$r13,e
preldx d,$r13,$r14

fld.s $f10,$r12,a
fst.s $f10,$r12,a
fld.d $f10,$r12,a
fst.d $f10,$r12,a


20 changes: 20 additions & 0 deletions gas/testsuite/gas/loongarch/li.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#as:
#objdump: -dr

.*:[ ]+file format .*


Disassembly of section .text:

00000000.* <_start>:
[ ]+0:[ ]+03803c06[ ]+ori[ ]+\$a2,[ ]+\$zero,[ ]+0xf
[ ]+4:[ ]+1a000005[ ]+pcalau12i[ ]+\$a1,[ ]+0
[ ]+4:[ ]+R_LARCH_PCALA_HI20[ ]+.rodata
[ ]+8:[ ]+02c000a5[ ]+addi.d[ ]+\$a1,[ ]+\$a1,[ ]+0
[ ]+8:[ ]+R_LARCH_PCALA_LO12[ ]+.rodata
[ ]+c:[ ]+03800404[ ]+ori[ ]+\$a0,[ ]+\$zero,[ ]+0x1
[ ]+10:[ ]+0381000b[ ]+ori[ ]+\$a7,[ ]+\$zero,[ ]+0x40
[ ]+14:[ ]+002b0000[ ]+syscall[ ]+0x0
[ ]+18:[ ]+00150004[ ]+move[ ]+\$a0,[ ]+\$zero
[ ]+1c:[ ]+0381740b[ ]+ori[ ]+\$a7,[ ]+\$zero,[ ]+0x5d
[ ]+20:[ ]+002b0000[ ]+syscall[ ]+0x0
22 changes: 22 additions & 0 deletions gas/testsuite/gas/loongarch/li.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.equ EXIT_SUCCESS, 0
.equ STDOUT, 1
.equ SYS_exit, 93
.equ SYS_write, 64

.section .rodata
msg:
.string "hello, world!\n"
len = . - msg

.text
.globl _start
_start:
li.w $a2, len
la.local $a1, msg
li.w $a0, STDOUT
li.w $a7, SYS_write
syscall 0x0

li.w $a0, EXIT_SUCCESS
li.w $a7, SYS_exit
syscall 0x0