[fpc-pascal] issue when enabling -O2

Matias Vara matiasevara at gmail.com
Wed Jan 10 17:23:35 CET 2018


Hi Karol and thanks you very much! I got confused with the function names,
I feel very sorry.
As I said in my previous email, I fixed by rewriting the assembler
function. However, I don't why it worked.
So I understand you correctly, if I use assembler in my procedures there
could be a risk that I trash a variable that the compiler is using. Is the
compiler warning me about this?

Matias

2018-01-10 17:14 GMT+01:00 Karoly Balogh (Charlie/SGR) <
charlie at scenergy.dfmk.hu>:

> Hi,
>
> On Wed, 10 Jan 2018, Matias Vara wrote:
>
> > I am getting an exception when I enable the -O2 optimization. More
> > precisaily, the line that stars with write_portd.... is corrupting the
> > data section. This is the pascal code:
> >
> > function PciReadDword(const bus, device, func, regnum: UInt32): UInt32;
> > var
> >   Send: DWORD;
> > begin
> >   Send := $80000000 or (bus shl 16) or (device shl 11) or (func shl 8)
> or (regnum shl 2);
> >   write_portd(@Send, PCI_CONF_PORT_INDEX);
> >   read_portd(@Send, PCI_CONF_PORT_DATA);
> >   Result := Send;
> > end;
> >
> > which generates (without -02):
> >
> > .section .text.n_arch_$$_pcireaddword$longword$longword$longword$
> longword$$longword,"x"
> > .balign 16,0x90
> > .globl ARCH_$$_PCIREADDWORD$LONGWORD$LONGWORD$LONGWORD$LONGWORD$$
> LONGWORD
> > ARCH_$$_PCIREADDWORD$LONGWORD$LONGWORD$LONGWORD$LONGWORD$$LONGWORD:
> >
> > (***shipp***)
> >
> > and with -O2:
> >
> > .section .text.n_arch_$$_pciwriteword$word$word$word$word$word,"x"
> > .balign 16,0x90
> > .globl ARCH_$$_PCIWRITEWORD$WORD$WORD$WORD$WORD$WORD
> > ARCH_$$_PCIWRITEWORD$WORD$WORD$WORD$WORD$WORD:
> >
> > The first thing that I realize was the the optimized version is not
> > generating the correct source when is exiting since it should return
> > "Send", but am I right? The assembler code of write_portd remains the
> > same, Am I missing something?
>
> The -O2 version of the function you sent is from a different one, it's
> from a Write function, not a Read... So no wonder it doesn't return
> anything... :)
>
> BTW, -O2 uses register variables, while -O- doesn't. If your assembler
> trashes one of them without preserving one, it can lead to crashes...
> But there could be many other reasons.
>
> Without seeing the actual assembler functions it's hard to tell.
>
> Charlie
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180110/1491f7c5/attachment.html>


More information about the fpc-pascal mailing list