Skip to content

Commit

Permalink
LoongArch: gas: add support using variable for li.w/d
Browse files Browse the repository at this point in the history
        Macro instruction li.w/d support using variable
        like ".equ var, 123    li.w/d resgister, var".

gas/
        * config/loongarch-parse.y
        * config/tc-loongarch.c

        Add two testcases.

gas/
        * testsuite/gas/loongarch/li.d
        * testsuite/gas/loongarch/li.s
        * testsuite/gas/loongarch/li2.d
        * testsuite/gas/loongarch/li2.s
  • Loading branch information
tangxiaolin committed Aug 12, 2022
1 parent fcbfb25 commit 2c43c99
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
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;
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);}
| '(' 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
14 changes: 14 additions & 0 deletions gas/testsuite/gas/loongarch/li.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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\)
7 changes: 7 additions & 0 deletions gas/testsuite/gas/loongarch/li.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.equ a, 0x123
.equ b, 0xfffff00000
.equ c, 0xfffffffffff

li.w $r12, a
li.d $r13, b
li.d $r14, c
20 changes: 20 additions & 0 deletions gas/testsuite/gas/loongarch/li2.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/li2.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

0 comments on commit 2c43c99

Please sign in to comment.