Discussion:
how to inline assembler into gcc-FORTRAN program?
David Livshin
2007-05-10 07:40:26 UTC
Permalink
Hi,

Is there a way to inline "asm" code inside gcc-FORTRAN program?

Thank you in advance,

David
--
David Livshin

http://www.dalsoft.com
Tobias Burnus
2007-05-10 07:58:44 UTC
Permalink
Hi,
Post by David Livshin
Is there a way to inline "asm" code inside gcc-FORTRAN program?
To my knowledge not; I think there are only few Fortran compilers which
handle assembler in a Fortran program and, contrary to other vendor
extensions, there does not seem to be any standard way.

One way to do it is to implement the routine in C and embed the asm
directives there. Otherwise one could investigate how other compilers
are doing it and implement some similar in gfortran.

Other than that it would be interesting to see the code where
GCC/gfortran generates significant less efficient code then hand written
assembler. Maybe there is something to optimize in the compiler itself.

Tobias
David Livshin
2007-07-25 08:40:56 UTC
Permalink
Post by Tobias Burnus
Hi,
Post by David Livshin
Is there a way to inline "asm" code inside gcc-FORTRAN program?
To my knowledge not; I think there are only few Fortran compilers which
handle assembler in a Fortran program and, contrary to other vendor
extensions, there does not seem to be any standard way.
One way to do it is to implement the routine in C and embed the asm
directives there. Otherwise one could investigate how other compilers
are doing it and implement some similar in gfortran.
Other than that it would be interesting to see the code where
GCC/gfortran generates significant less efficient code then hand written
assembler. Maybe there is something to optimize in the compiler itself.
Tobias
What I really need is to be able to specify portions of ( fortran ) code
in such a way that it would be possible to determine their assembly code
in the compiler-generated assembly output. For C it could be done by
preceding and following the desirable code segments with calls to "asm"
with the parameter being a specially constructed comment - the compiler
generated assembly file is searched for these comments.

Any ideas how can it be done in gfortran?

Thank you in advance,

David
--
David Livshin

http://www.dalsoft.com
Daniel Franke
2007-07-25 08:49:50 UTC
Permalink
Post by David Livshin
Any ideas how can it be done in gfortran?
Create a C-function
asm_marker() {
/* your asm code here */
}

In Fortran:
CALL asm_marker_()
! your fortran code here
CALL asm_marker_()

(Or use the new C binding feature to omit the decorating '_')

This also adds the function call to asm_marker() to your marker code,
but if you only want to identify a region of assembler code, that
shouldn't make a difference.

Regards
Daniel
David Livshin
2007-07-25 09:28:44 UTC
Permalink
Post by Daniel Franke
Post by David Livshin
Any ideas how can it be done in gfortran?
Create a C-function
asm_marker() {
/* your asm code here */
}
CALL asm_marker_()
! your fortran code here
CALL asm_marker_()
(Or use the new C binding feature to omit the decorating '_')
This also adds the function call to asm_marker() to your marker code,
but if you only want to identify a region of assembler code, that
shouldn't make a difference.
Regards
Daniel
Thanks, but this solution is less than satisfactory, because:

1. It requires an extra function ( even dummy one ) to be supplied at
the link time

2. It doesn't allow to specify "parameters" to the delimiter - in my
case the delimiter is any comment that begins with ".start_dco" -
".start_dco" is used to recognize the delimiter and anything that
follows it is parsed as "parameters" ( see
http://www.dalsoft.com/documentation_manual.html#mozTocId452659 and
http://www.dalsoft.com/documentation_manual.html#mozTocId235475 for
details ).
--
David Livshin

http://www.dalsoft.com
Tobias Schlüter
2007-07-25 10:17:43 UTC
Permalink
Post by David Livshin
1. It requires an extra function ( even dummy one ) to be supplied at
the link time
Not necessarily: since you're looking at the assembly files before
proceeding, you might as well run them through a script that replaces
all references to the marker function(s) with NOPs.
Post by David Livshin
2. It doesn't allow to specify "parameters" to the delimiter - in my
case the delimiter is any comment that begins with ".start_dco" -
".start_dco" is used to recognize the delimiter and anything that
follows it is parsed as "parameters" ( see
http://www.dalsoft.com/documentation_manual.html#mozTocId452659 and
http://www.dalsoft.com/documentation_manual.html#mozTocId235475 for
details ).
You could call asm_func_param1_param2 instead. This puts some
restricitons on your parameter syntax of course.

- Tobi
David Livshin
2007-07-25 10:31:09 UTC
Permalink
Post by Tobias Schlüter
Post by David Livshin
1. It requires an extra function ( even dummy one ) to be supplied at
the link time
Not necessarily: since you're looking at the assembly files before
proceeding, you might as well run them through a script that replaces
all references to the marker function(s) with NOPs.
And what if user will decide not to run my package ( in the case of
using "asm" he will end up just with a few extra comments )?
Post by Tobias Schlüter
Post by David Livshin
2. It doesn't allow to specify "parameters" to the delimiter - in my
case the delimiter is any comment that begins with ".start_dco" -
".start_dco" is used to recognize the delimiter and anything that
follows it is parsed as "parameters" ( see
http://www.dalsoft.com/documentation_manual.html#mozTocId452659 and
http://www.dalsoft.com/documentation_manual.html#mozTocId235475 for
details ).
You could call asm_func_param1_param2 instead. This puts some
restricitons on your parameter syntax of course.
"I" could do it, but expecting this to be done by a user is not practical.

I am looking for fortran-specific solution to this problem.

Why ( something like ) "asm" is not supported by gfortran?
--
David Livshin

http://www.dalsoft.com
Steve Kargl
2007-07-25 14:33:04 UTC
Permalink
Post by David Livshin
I am looking for fortran-specific solution to this problem.
Why ( something like ) "asm" is not supported by gfortran?
Because the people actually writing gfortran are spending
their time implementing the Fortran Standard and fixing
bugs. "asm" is neither Fortran nor a bug.
--
Steve
Tobias Burnus
2007-07-25 11:25:19 UTC
Permalink
Hi,
Post by David Livshin
Post by David Livshin
Any ideas how can it be done in gfortran?
1. It requires an extra function ( even dummy one ) to be supplied at
the link time
Another possibility is to create an assembler file (option -S) and
modify that one - as it has been suggested here:

http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/dfb2ce232677364e


Seemingly there is very little support of inline assembler in any
Fortran compiler. Fortran III (of 1958) seemingly had it (but it was
never released); Salford's FTN77 seems to have "CODE ... EDOC" according
to comp.lang.fortran. None of the major compilers seems to support it,
which reduces the chance that any gfortran programmer will implement it.
If someone would write the needed patches, one could *consider* to
include it, however.

Tobias
Tim Prince
2007-07-25 13:32:38 UTC
Permalink
Post by Tobias Burnus
Seemingly there is very little support of inline assembler in any
Fortran compiler. Fortran III (of 1958) seemingly had it (but it was
never released); Salford's FTN77 seems to have "CODE ... EDOC" according
to comp.lang.fortran. None of the major compilers seems to support it,
which reduces the chance that any gfortran programmer will implement it.
If someone would write the needed patches, one could *consider* to
include it, however.
AMD sponsored inclusion of <?intrin.h> stuff in pgf90. It saw very
little usage, so they had to bite the bullet and implement vectorization.
Brian Dessent
2007-07-25 11:44:12 UTC
Permalink
Post by David Livshin
What I really need is to be able to specify portions of ( fortran ) code
in such a way that it would be possible to determine their assembly code
in the compiler-generated assembly output. For C it could be done by
If that's all you need then why not use the debug info? Using objdump -dS and a binary
compiled with -g you get an output that has the source and generated assembly intermixed,
so it's simple to just search for marker comments. e.g.

$ cat hello.f90
PROGRAM Hello_world
! marker-begin
PRINT *,"Hello"
! marker-end
PRINT *,"World"
END PROGRAM Hello_world

$ gfortran hello.f90 -O2 -g -o hello

$ objdump -dSw hello.exe | sed -ne '/! marker-begin/,/! marker-end/ p'
! marker-begin
PRINT *,"Hello"
40104a: 8d 9d d8 fe ff ff lea 0xfffffed8(%ebp),%ebx
401050: c7 44 24 0c 00 00 00 00 movl $0x0,0xc(%esp)
401058: c7 44 24 08 00 00 00 00 movl $0x0,0x8(%esp)
401060: c7 44 24 04 7f 00 00 00 movl $0x7f,0x4(%esp)
401068: c7 04 24 46 00 00 00 movl $0x46,(%esp)
40106f: e8 fc 00 00 00 call 401170 <__gfortran_set_std>
401074: 89 1c 24 mov %ebx,(%esp)
401077: c7 85 e0 fe ff ff 00 60 41 00 movl $0x416000,0xfffffee0(%ebp)
401081: c7 85 e4 fe ff ff 03 00 00 00 movl $0x3,0xfffffee4(%ebp)
40108b: c7 85 dc fe ff ff 06 00 00 00 movl $0x6,0xfffffedc(%ebp)
401095: c7 85 d8 fe ff ff 80 00 00 00 movl $0x80,0xfffffed8(%ebp)
40109f: e8 4c 3e 00 00 call 404ef0 <__gfortran_st_write>
4010a4: 89 1c 24 mov %ebx,(%esp)
4010a7: c7 44 24 08 05 00 00 00 movl $0x5,0x8(%esp)
4010af: c7 44 24 04 16 60 41 00 movl $0x416016,0x4(%esp)
4010b7: e8 54 02 00 00 call 401310 <__gfortran_transfer_character>
4010bc: 89 1c 24 mov %ebx,(%esp)
4010bf: e8 8c 26 00 00 call 403750 <__gfortran_st_write_done>
! marker-end

(Note that I only split up the print into two parts as otherwise the marker-end would be
at the end of the program and have no line number associated with it in the debug info,
but in a non-trivial example this wouldn't be a problem.)

Brian
François-Xavier Coudert
2007-05-10 07:58:53 UTC
Permalink
Post by David Livshin
Is there a way to inline "asm" code inside gcc-FORTRAN program?
Not inside a Fortran program. (I think many Fortran compilers don't.)
For all practical purposes, you might want to wrap that assembly in a
C function.

FX
Continue reading on narkive:
Search results for 'how to inline assembler into gcc-FORTRAN program?' (Questions and Answers)
4
replies
Where do i found turbo c Compiler Free of cost?
started 2007-05-03 04:59:54 UTC
programming & design
Loading...