Gas64 (no more use of gcc, only gas) WDS / LNX

User projects written in or related to FreeBASIC.
Post Reply
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by shadow008 »

I have a relatively simple udt that fails compilation with -gen gas64, but succeeds with -gen gas (non-64).

BitArray.bi

Code: Select all

#ifndef BitArray_bi
#define BitArray_bi

type BitArrayType
    
    dim Array as ubyte ptr
    dim Count as uinteger
    dim BitSize as ubyte
    
    declare constructor(inBitSize as ubyte, inCount as uinteger)
    declare destructor()
    
    declare sub Set(index as uinteger, inVal as ubyte)
    declare function Get(index as uinteger) as uinteger
    
    declare function SizeInBytes() as uinteger
    
    declare function BitMask() as const ubyte
    
    declare function GetUsedMemorySize() as uinteger
    
end type

#endif
BitArray.bas

Code: Select all

#ifndef BitArray_bas
#define BitArray_bas

#include once "BitArray.bi"

constructor BitArrayType(inBitSize as ubyte, inCount as uinteger)
    
    this.BitSize = inBitSize
    this.Count = inCount
    
    'Guarantee minimum for a short int
    dim byteSize as uinteger = this.SizeInBytes()
    
    this.Array = new ubyte[byteSize]

end constructor

destructor BitArrayType()

    delete [] this.Array

end destructor

function BitArrayType.BitMask() as const ubyte
    
    return (1 SHL this.BitSize) - 1
    
end function

function BitArrayType.Get(index as uinteger) as uinteger
    
    dim retVal as uinteger<16>
    
    retVal = *cast(uinteger<16> ptr, @this.Array[(this.BitSize * index) SHR 3])
    retVal SHR= ((this.BitSize * index) AND 7)
    retVal AND= this.BitMask()
    
    return retVal
    
end function

sub BitArrayType.Set(index as uinteger, inVal as ubyte)
    
    dim setVal as uinteger<16> ptr = cast(uinteger<16> ptr, @this.Array[(this.BitSize * index) SHR 3])
    
    *setVal AND= NOT (this.BitMask() SHL ((this.BitSize * index) AND 7))
    *setVal OR= ((inVal AND this.BitMask()) SHL ((this.BitSize * index) AND 7))
    
end sub

function BitArrayType.SizeInBytes() as uinteger
    
    return ((this.BitSize * this.Count) SHR 3) + 2
    
end function

function BitArrayType.GetUsedMemorySize() as uinteger
    
    dim retVal as uinteger
    
    retVal = sizeof(BitArrayType)
    retVal += sizeof(ubyte) + this.SizeInBytes()
    
    return retVal
    
end function

#endif
Compiling results in this error:
$ /mnt/c/Freebasic/FreeBASIC-1.10.0-win64/fbc.exe -c src/utilities/BitArray.bas -gen gas64
src/utilities/BitArray.asm: Assembler messages:
src/utilities/BitArray.asm:47: Error: no such instruction: `found AN ERROR:Memclear without an immediat as size'
As an aside, I like how the errors are actually right there in the assembly code :D
...
mov -72[rbp], rax #Optim 2
cmp QWORD PTR -72[rbp], 0
je .L_0007

FOUND AN ERROR : Memclear without an immediat as size

.L_0007:
mov r11, 16[rbp]
mov rax, QWORD PTR -72[rbp]
mov QWORD PTR [r11], rax
...
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

@shadow008
Thanks for the report.

shadow008 wrote: Oct 09, 2023 19:38 I have a relatively simple udt that fails compilation with -gen gas64, but succeeds with -gen gas (non-64).
You would have used any other datatype than byte/ubyte there would be no problem. :D

As an aside, I like how the errors are actually right there in the assembly code
There is no assembly code here because it's an internal problem in the emitter. The purpose is just to show it by causing an error during the assembly.


If you want to test : https://users.freebasic-portal.de/sarg/fbc64_fix.zip
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by shadow008 »

SARG wrote: Oct 10, 2023 0:27 If you want to test : https://users.freebasic-portal.de/sarg/fbc64_fix.zip
Yep, it's fixed!
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by TeeEmCee »

(Shouldn't fbc throw an error if an internal error in gas64 like that happens?)

I tried out gas64 (more comments and benchmarks here) and am very pleased to see that it now works most of the time! (Turns out it was already working in fbc 1.10 for me, while fbc 1.09 produced a program that segfaulted.) I'll be happy to be able to avoid slow -gen gcc 64-bit compiles. How close do you think it is to being stable enough to be made the default on x86_64? I just tried all FB's unit-tests and log-tests and see they pass.

But I found two bugs which I had to work around depending on compile flags:
sf.net#989 gas64: "rsi" replaced with "r11" in some symbols
sf.net#990 gas64: "operand type mismatch" due to optimising away reg64->stack->reg32 move
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

TeeEmCee wrote: Oct 11, 2023 5:54 (Shouldn't fbc throw an error if an internal error in gas64 like that happens?)
The problem is that is not an error in user's source code, the message is only for fbc's devs.
Maybe I could add a precision something like : Please report to developers.


It's very stable, each time a change is made Jeff runs all the tests but as I wrote in the other thread there still bugs waiting to be discovered (pleasure of coding).
About being the default option on x86_64 I don't know, I have to talk with Jeff.

It seems that you are using Linux not Windows, confirmed ?

I hope quickly fixing reported bugs.

PS On discord I sent you an invitation.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by TeeEmCee »

Yes, I'm using Linux, I didn't think it would matter.

I see there's an error number for internal errors...

Code: Select all

		errReport( FB_ERRMSG_INTERNAL, , " global variable/array initializer nesting level too deep (MAXVARINISCOPES=" & MAXVARINISCOPES & ")" )
But FB_ERRMSG_INTERNAL just adds the prefix "Internal!" plus the current line of code (probably the last in the file). Not useful.

I don't see anything on Discord...
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

All the bugs reported by TeeEmCee are fixed.
Thanks again to him which clearly described the problems. :D

Changes in my Github : https://github.com/SARG-FB/fbc , file ir-gas64.bas
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

I worked a bit on internal error report.
Using errReport( FB_ERRMSG_INTERNAL,... etc is not very usable as it reports the last compiled line. It's what I found years ago when I tried to report errors in gas64 emitter.

Message thrown when assembling could be changed like that :

Code: Select all

        
	   #
   Report to Freebasic devs, Internal Error in gas64 emitter : Memclear without an immediat as size
           #

What do you think ?

edit : currently

Code: Select all

 
FOUND AN ERROR : Memclear without an immediat as size
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by shadow008 »

Incorrect code is generated when using module constructor priorities

(Untested code, but it should be close)

mod1.bas

Code: Select all

sub init_mod1() constructor 102
print "102 should run second"
end sub
mod2.bas

Code: Select all

sub init_mod2() constructor 101
print "101 should run first"
end sub
mod3.bas (main module)

Code: Select all

print "This main module should be last"
With gen -gcc, the correct ordering is respected, with gen -gas64, the priority is not respected at all.
If that example above happens to work by chance, switch around the priority numbers and try again.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by fxm »

@SARG,
See definition and usage of module constructor priority at the CONSTRUCTOR (Module) documentation page.

@Jeff,
About this documentation page:
I think the STATIC attribute is useless for a module constructor => to remove this term in the syntax line only ?
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by coderJeff »

fxm wrote: Oct 13, 2023 8:34 @Jeff,
About this documentation page:
I think the STATIC attribute is useless for a module constructor => to remove this term in the syntax line only ?
Sounds ok to me. STATIC attribute does not have any special meaning with respect to module constructors over any other normal procedure.
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

@shadow008
Thanks for the report, fixed now. https://users.freebasic-portal.de/sarg/fbc64_fix.zip
That was easy to fix I just had to complete the existing code with the priority.
However a typo blocked me a long time I wrote 65565 instead 66535 causing crashes....

There's still a doubt concerning the handle of priorities of constructors/destructors in dll(.so file) on Linux.

@fxm
In ir-gas.bas the priority was just displayed in a comment as I didn't know how to use it.
Btw for the linker the value is 'inversed' : when you write priority 101 in reality it's 65535-101=65434, 102--> 65533, and so on.
Xusinboy Bekchanov
Posts: 791
Joined: Jul 26, 2018 18:28

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by Xusinboy Bekchanov »

Hello, If I compile VisualFBEditor IDE with -gen gas64, the IDE crashes when opening a file with a "stack overflow" message, if I compile with gcc or gas this does not happen.
To check, you can open this simple file:
https://github.com/XusinboyBekchanov/Vi ... titled.bas

This line is crashing: https://github.com/XusinboyBekchanov/My ... r.bas#L294
Xusinboy Bekchanov
Posts: 791
Joined: Jul 26, 2018 18:28

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by Xusinboy Bekchanov »

Xusinboy Bekchanov wrote: Feb 11, 2024 3:26 Hello, If I compile VisualFBEditor IDE with -gen gas64, the IDE crashes when opening a file with a "stack overflow" message, if I compile with gcc or gas this does not happen.
To check, you can open this simple file:
https://github.com/XusinboyBekchanov/Vi ... titled.bas

This line is crashing: https://github.com/XusinboyBekchanov/My ... r.bas#L294
After the latest changes in the compiler, the program no longer crashes in gas64, thank you very much.
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Gas64 (no more use of gcc, only gas) WDS / LNX

Post by SARG »

Xusinboy Bekchanov wrote: Mar 04, 2024 8:00 After the latest changes in the compiler, the program no longer crashes in gas64, thank you very much.
Thank you for the report and the help you gave me via discord ;-)
Post Reply