Skip to content

Commit

Permalink
Fix emitting of boolean constants in global/static initializers
Browse files Browse the repository at this point in the history
Booleans store 0/1 behind the scenes to be compatible with gcc, but fbc
stores boolean constants as 0/-1 internally. So it needs to do a
conversion to 0/1 when emitting the values. This was missing from
global/static variable initializer emitting for all backends.

http://www.freebasic.net/forum/viewtopic.php?f=3&t=24756
  • Loading branch information
dkl committed Jun 9, 2016
1 parent 764ed85 commit da85457
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Version 1.06.0
- Accesses to 64bit bitfields could be miscompiled or trigger a compiler crash
- #732: Virtual destructors couldn't be called on const objects
- #790: Windows API and X11 bindings: Fixed some macros where macro parameters conflicted with other identifiers or FB keywords used in the macro body
- boolean initializer values for global or static variables were not emitted properly


Version 1.05.0
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/emit_x86.bas
Original file line number Diff line number Diff line change
Expand Up @@ -6555,11 +6555,18 @@ end sub
sub emitVARINIi( byval dtype as integer, byval value as longint )
dim s as string
s = *_getTypeString( dtype ) + " "

'' AST stores boolean true as -1, but we emit it as 1 for gcc compatibility
if( (dtype = FB_DATATYPE_BOOLEAN) and (value <> 0) ) then
value = 1
end if

if( ISLONGINT( dtype ) ) then
s += "0x" + hex( value )
else
s += str( value )
end if

s += NEWLINE
outEx( s )
end sub
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/ir-hlc.bas
Original file line number Diff line number Diff line change
Expand Up @@ -3461,11 +3461,18 @@ end sub

private sub _emitVarIniI( byval sym as FBSYMBOL ptr, byval value as longint )
var dtype = symbGetType( sym )

'' AST stores boolean true as -1, but we emit it as 1 for gcc compatibility
if( (dtype = FB_DATATYPE_BOOLEAN) and (value <> 0) ) then
value = 1
end if

var l = exprNewIMMi( value, dtype )
if( symbIsRef( sym ) ) then
dtype = typeAddrOf( dtype )
end if
l = exprNewCAST( dtype, sym->subtype, l )

ctx.varini += exprFlush( l )
hVarIniSeparator( )
end sub
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/ir-llvm.bas
Original file line number Diff line number Diff line change
Expand Up @@ -2166,11 +2166,18 @@ end sub
private sub _emitVarIniI( byval sym as FBSYMBOL ptr, byval value as longint )
hVarIniElementType( sym )
var dtype = symbGetType( sym )

'' AST stores boolean true as -1, but we emit it as 1 for gcc compatibility
if( (dtype = FB_DATATYPE_BOOLEAN) and (value <> 0) ) then
value = 1
end if

if( typeGetSize( dtype ) = 8 ) then
ctx.varini += hEmitLong( value )
else
ctx.varini += hEmitInt( dtype, sym->subtype, value )
end if

hVarIniSeparator( )
end sub

Expand Down
11 changes: 11 additions & 0 deletions tests/boolean/boolean.bas
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,18 @@ namespace fbc_tests.boolean_.boolean_
CU_ASSERT_EQUAL( b, TRUE )
end sub

dim shared global1 as boolean = TRUE
dim shared global2 as boolean = FALSE

sub initializer_literal cdecl ( )
CU_ASSERT( global1 = TRUE )
CU_ASSERT( global2 = FALSE )

static static1 as boolean = TRUE
static static2 as boolean = FALSE
CU_ASSERT( static1 = TRUE )
CU_ASSERT( static2 = FALSE )

dim as boolean a = 0
CU_ASSERT_EQUAL( a, FALSE )

Expand Down

0 comments on commit da85457

Please sign in to comment.