unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: guile-devel@gnu.org
Subject: support for mips32r6
Date: Fri, 30 Aug 2019 10:09:47 +0200	[thread overview]
Message-ID: <92188107.04DtdhtvNo@omega> (raw)

[-- Attachment #1: Type: text/plain, Size: 2931 bytes --]

Hi,

A libffcall bug report [1] made me aware of an incompatible change in
the MIPS ISAs. Namely, for jumping to an address given in a register,
starting with mips32r6, the existing 'jr' instruction does not work
any more: it produces an 'illegal instruction'.

There are many documents of the MIPS ISAs, and before 2014 they
described two instructions JR and JALR:
  JR <rs>           jumps to the address in register <rs>
  JALR <rd>,<rs>    jumps to the address in register <rs> and puts
                    the old PC + 8 into register <rd>
In particular,
  JR <rs>           [SPECIAL, rs, 00000, 00000, hint, 001000]
is equivalent to
  JALR $0,<rs>      [SPECIAL, rs, 00000, 00000, hint, 001001]
(because writing to $0 is a no-op).

In the documents that consider the MIPS32 R6 architecture, such as [2]
from 2014 - look at the descriptions of JR and JALR - you see that
  - in order to get the effect of JR, the bit pattern of JALR with destination
    $0 has to be used,
  - in the JALR instruction, the destination $0 is no longer allowed;
    this is merely a hint for the disassembler.

You can see that the GNU assembler has been adjusted. First, see
the different bit patterns:

$ cat foo.s
        .text
        j       $25
        nop
        jal     $0,$25
        nop
# Assemble this using binutils 2.16.1:
$ mips64-linux-as -32 -o foo.o foo.s
$ mips64-linux-objdump --disassemble foo.o

foo.o:     file format elf32-tradbigmips

Disassembly of section .text:

00000000 <.text>:
   0:   03200008        jr      t9
   4:   00000000        nop
   8:   03200009        jalr    zero,t9
   c:   00000000        nop
        ...

# Now, see how a newer GNU assembler (binutils 2.27) and option -mips32r6
# produce a different result:
$ mips64-linux-as -32 -mips32r6 -o foo.o foo.s
$ mips64-linux-objdump --disassemble foo.o         [binutils 2.16.1]

foo.o:     file format elf32-tradbigmips

Disassembly of section .text:

00000000 <.text>:
   0:   03200009        jalr    zero,t9
   4:   00000000        nop
   8:   03200009        jalr    zero,t9
   c:   00000000        nop
        ...
$ mips64-linux-objdump --disassemble foo.o         [binutils 2.27]

foo.o:     file format elf32-tradbigmips


Disassembly of section .text:

00000000 <.text>:
   0:   03200009        jr      t9
   4:   00000000        nop
   8:   03200009        jr      t9
   c:   00000000        nop
        ...


Note that the JALR $0,<rs> instruction can be used on all architectures;
I verified this by looking at old MIPS ISA documentations from 1995,
and by looking at the source code of QEMU.

Find attached a fix for GNU guile, that mirrors the one I submitted
for GNU lightning [3] (untested).

Bruno


[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=925129
[2] http://hades.mech.northwestern.edu/images/1/16/MIPS32_Architecture_Volume_II-A_Instruction_Set.pdf
[3] https://lists.gnu.org/archive/html/lightning/2019-08/msg00010.html

[-- Attachment #2: mips32r6-fix.diff --]
[-- Type: text/x-patch, Size: 862 bytes --]

diff --git a/libguile/lightening/lightening/mips-cpu.c b/libguile/lightening/lightening/mips-cpu.c
index 7ab58b8..704a2cd 100644
--- a/libguile/lightening/lightening/mips-cpu.c
+++ b/libguile/lightening/lightening/mips-cpu.c
@@ -379,7 +379,11 @@ static void _nop(jit_state_t*,int32_t);
 #  define BGTZ(rs,im)			hrri(MIPS_BGTZ,rs,_ZERO_REGNO,im)
 #  define BNE(rs,rt,im)			hrri(MIPS_BNE,rs,rt,im)
 #  define JALR(r0)			hrrrit(MIPS_SPECIAL,r0,0,_RA_REGNO,0,MIPS_JALR)
-#  define JR(r0)			hrrrit(MIPS_SPECIAL,r0,0,0,0,MIPS_JR)
+#  if 1 /* supports MIPS32 R6 */
+#   define JR(r0)			hrrrit(MIPS_SPECIAL,r0,0,0,0,MIPS_JALR)
+#  else /* does not support MIPS32 R6 */
+#   define JR(r0)			hrrrit(MIPS_SPECIAL,r0,0,0,0,MIPS_JR)
+#  endif
 #  define J(i0)				hi(MIPS_J,i0)
 #  define MOVZ(rd,rs,rt)		hrrrit(0,rs,rt,rd,0,MIPS_MOVZ)
 #  define comr(r0,r1)			xori(r0,r1,-1)

             reply	other threads:[~2019-08-30  8:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-30  8:09 Bruno Haible [this message]
2020-01-06 21:01 ` support for mips32r6 Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=92188107.04DtdhtvNo@omega \
    --to=bruno@clisp.org \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).