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

COMMON block elimination misbinds variables sometimes #141

Open
raehik opened this issue Sep 20, 2021 · 1 comment
Open

COMMON block elimination misbinds variables sometimes #141

raehik opened this issue Sep 20, 2021 · 1 comment

Comments

@raehik
Copy link
Collaborator

raehik commented Sep 20, 2021

When updating to use fortran-src 0.6.0 with Declarators in CommonGroups, I confirmed that array types are now handled correctly in COMMON block elimination. However, the example I was using had a weird error where the generated binds are sometimes the wrong way round.

Following is a copy-paste from my comment in #136 .


This program (from riptutorial.com) now works (after annotating ICOUNT and ISTACK)...

       PROGRAM STACKING
       INTEGER ICOUNT
       INTEGER ISTACK
       COMMON /HEAP/ ICOUNT, ISTACK(1023)
       ICOUNT = 0
       READ *, IVAL
       CALL PUSH(IVAL)
       CALL POP(IVAL)
       END

       SUBROUTINE PUSH(IVAL)
       COMMON /HEAP/ ICOUNT, ISTACK(1023)
       ICOUNT = ICOUNT + 1
       ISTACK(ICOUNT) = IVAL
       RETURN
       END

       SUBROUTINE POP(IVAL)
       COMMON /HEAP/ ICOUNT, ISTACK(1023)
       IVAL = ISTACK(ICOUNT)
       ICOUNT = ICOUNT - 1
       RETURN
       END

...almost. Somehow, the generated code binds the module variables incorrectly 2/3 times:

       PROGRAM STACKING
       use Heap, icount => Heap_istack, istack => Heap_icount
       ICOUNT = 0
       READ *, IVAL
       CALL PUSH(IVAL)
       CALL POP(IVAL)
       END

       SUBROUTINE PUSH(IVAL)
       use Heap, icount => Heap_istack, istack => Heap_icount
       ICOUNT = ICOUNT + 1
       ISTACK(ICOUNT) = IVAL
       RETURN
       END

       SUBROUTINE POP(IVAL)
       use Heap, icount => Heap_icount, istack => Heap_istack
       IVAL = ISTACK(ICOUNT)
       ICOUNT = ICOUNT - 1
       RETURN
       END

Fixing the binds grants a compiling program (with apparently the same behaviour).

*Previously, it would fail with a strange error due to ISTACK(1023) being parsed as a subscript, which then gets converted to a function call.

@raehik
Copy link
Collaborator Author

raehik commented Sep 20, 2021

I've tried adding type annotations to every subroutine instead of just the top one (the original program was implicitly typed, which fortran-src doesn't support). Weirdly, swapping top-level defs doesn't swap their bindings: the bottom def is always correctly bound, and the other two are wrong.

I didn't get a good idea for how the bindings are generated when I looked before. Surely it must be related to that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant