Bug 14480 - PDP11 gas generates invalid code for deferred indirect JSR with 0 index
Summary: PDP11 gas generates invalid code for deferred indirect JSR with 0 index
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: 2.22
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-16 16:40 UTC by Jordi Guillaumes Pons
Modified: 2018-08-01 14:18 UTC (History)
4 users (show)

See Also:
Host:
Target: pdp11-aout
Build:
Last reconfirmed:


Attachments
Assembly source file (97 bytes, application/octet-stream)
2012-08-16 16:40 UTC, Jordi Guillaumes Pons
Details
Modified Assembly Program illustrating the problem (107 bytes, text/plain)
2018-07-19 22:35 UTC, James Patrick Conlon
Details
Patch which solves this specific problem (332 bytes, patch)
2018-07-19 22:37 UTC, James Patrick Conlon
Details | Diff
Fixes implicit index deferred (433 bytes, patch)
2018-07-28 04:22 UTC, James Patrick Conlon
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jordi Guillaumes Pons 2012-08-16 16:40:31 UTC
Created attachment 6582 [details]
Assembly source file

As seen at the following assembly listing:


GNU assembler version 2.22.0 (pdp11-aout) using BFD version (GNU Binutils) 2.22.0.20120725
GAS LISTING dic.s 			page 1


   1 0000 C0151000 	start:	mov	$ind,r0
   2 0004 C809     		jsr	pc,@(r0)
   3 0006 F8090000 		jsr	pc,@0(r0)
   4 000a F8090200 		jsr	pc,@2(r0)
   5 000e 0000     		halt
   6              	
   7              	
   8 0010 1400     	ind:	.WORD	dest
   9 0012 1600     		.WORD	dest2
  10              	
  11 0014 8700     	dest:	rts	pc
  12              	
  13 0016 8700     	dest2:	rts	pc
  14              		
  15              		.END

The instructions in lines 2 and 3 should both assemble to F8090000, but line 2 assembles like it was JSR PC,(R0).
Comment 1 Larry Baker 2012-08-22 00:39:29 UTC
See the NOTE at the bottom of page 5-5 in Section 5.8, INDEX DEFERRED MODE, in the PDP-11 MACRO-11 Language Reference Manual on the BitSavers web site (http://www.bitsavers.org/pdf/dec/pdp11/rsx11/RSX11M_V4.1_Apr83/4_ProgramDevelopment/AA-V027A-TC_macro11_Mar83.pdf):

   The expression @(ER) may be used, but it
   will be assembled as if it were written
   @0(ER), and a word will be used to store
   the 0.
Comment 2 Jordi Guillaumes Pons 2012-08-22 12:41:22 UTC
Additional information: the gcc compiler generates a JSR PC,@(Rx) to implement an indirect call thru a function pointer table, so this bug causes gcc generating invalid code.
Comment 3 James Patrick Conlon 2018-07-19 22:35:31 UTC
Created attachment 11146 [details]
Modified Assembly Program illustrating the problem

Added a line to show that the current behavior does produce output matching something it shouldn't match.
Comment 4 James Patrick Conlon 2018-07-19 22:37:21 UTC
Created attachment 11147 [details]
Patch which solves this specific problem
Comment 5 James Patrick Conlon 2018-07-19 22:41:16 UTC
Confirming that this bug still exists in as 2.31.51.20180719.

Attached is a slightly modified version of Jordi's dic.s called dic-mod.s.  The output of pdp11-aout-as -a dic-mod.s is:
GAS LISTING /home/cptnapalm/Downloads/dic-mod.s page 1
                             
   1 0000 C0151200 start:mov$ind,r0
   2 0004 C809     jsr      pc,(r0)
   3 0006 C809     jsr      pc,@(r0)
   4 0008 F8090000 jsr      pc,@0(r0)
   5 000c F8090200 jsr      pc,@2(r0)
   6 0010 0000     halt
   7              
   8              
   9 0012 1600     ind:.WORDdest
  10 0014 1800     .WORDdest2
  11              
  12 0016 8700     dest:rtspc
  13              
  14 0018 8700     dest2:rtspc
  15              
  16              .END
    
    GAS LISTING /home/cptnapalm/Downloads/dic-mod.s page 2
    
    
    DEFINED SYMBOLS
    /home/cptnapalm/Downloads/dic-mod.s:1      .text:0000000000000000 start
    /home/cptnapalm/Downloads/dic-mod.s:9      .text:0000000000000012 ind
    /home/cptnapalm/Downloads/dic-mod.s:12     .text:0000000000000016 dest
    /home/cptnapalm/Downloads/dic-mod.s:14     .text:0000000000000018 dest2
    
    NO UNDEFINED SYMBOLS


As can be seen, jsr pc,@(r0) assembles the same as jsr pc,(r0), which it shouldn't do.  I created a patch, which is attached, that solves this problem.  As adding a '0' would create a new string and since it already knows that it's deferred, I just replace the '@' with a '0' before sending it along.

The new output:
GAS LISTING /home/cptnapalm/Downloads/dic-mod.s 			page 1


   1 0000 C0151400 	start:	mov	$ind,r0
   2 0004 C809     	        jsr     pc,(r0)
   3 0006 F8090000 		jsr	pc,@(r0)
   4 000a F8090000 		jsr	pc,@0(r0)
   5 000e F8090200 		jsr	pc,@2(r0)
   6 0012 0000     		halt
   7              	
   8              	
   9 0014 1800     	ind:	.WORD	dest
  10 0016 1A00     		.WORD	dest2
  11              	
  12 0018 8700     	dest:	rts	pc
  13              	
  14 001a 8700     	dest2:	rts	pc
  15              		
  16              		.END

GAS LISTING /home/cptnapalm/Downloads/dic-mod.s 			page 2


DEFINED SYMBOLS
/home/cptnapalm/Downloads/dic-mod.s:1      .text:0000000000000000 start
/home/cptnapalm/Downloads/dic-mod.s:9      .text:0000000000000014 ind
/home/cptnapalm/Downloads/dic-mod.s:12     .text:0000000000000018 dest
/home/cptnapalm/Downloads/dic-mod.s:14     .text:000000000000001a dest2

NO UNDEFINED SYMBOLS


It now does the right thing in this case.
Comment 6 James Patrick Conlon 2018-07-25 19:59:17 UTC
Hold off on the 2018-07-19 patch.  There are some additional fixes necessary.
Comment 7 James Patrick Conlon 2018-07-28 04:22:18 UTC
Created attachment 11156 [details]
Fixes implicit index deferred

@(Rn) now changes to @0(Rn) as it should.  @(Rn)+ is explicitly tested for and remains unchanged.
Comment 8 Sourceware Commits 2018-08-01 14:16:08 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3cf2b6691cef024f7cdb48aaec5fab5189e1cffa

commit 3cf2b6691cef024f7cdb48aaec5fab5189e1cffa
Author: James Patrick Conlon <cptjustice@gmail.com>
Date:   Wed Aug 1 15:14:46 2018 +0100

    Fix bug in PDP11 assembler when handling a JSr instruction with deferred auto increment.
    
    	PR 14480
    	* config/tc-pdp11.c (parse_op_noreg): Check for and handle auto
    	increment deferred.
    	* testsuite/gas/pdp11/pr14480.d: New test driver file.
    	* testsuite/gas/pdp11/pr14480.s: New test source file file.
    	* testsuite/gas/pdp11/pdp11.exp: Run the new test.
Comment 9 Nick Clifton 2018-08-01 14:18:13 UTC
Hi James,

  Thanks for the patch.  I have applied it, along with an addition to the
  PDP11 assembler testsuite, to the mainline sources.

  I did make one addition to the patch.  Just a small paranoia check to 
  make sure that the bytes between str[1] and str[5] are not NUL.

Cheers
  Nick