Skip to content

Commit

Permalink
fbc: internal changes to track constants
Browse files Browse the repository at this point in the history
- add type FBS_CONST to extend FBVALUE for use in type FBSYMBOL.val
- add type AST_NODE_CONST to extend FBVALUE for use in type ASTNODE.val
- add FBTOKEN.hassuffix to track if the literal had an explicit suffix
- this tracking provides a means to making decisions about a literal constant
  based on it having an explicit suffix in addition to data type assigned
- probably not completely necessary since the data type assigned should
  tell us everything we need to know about the constant
- however, the small refactor and extension of FBVALUE is nice for future use.
  • Loading branch information
jayrm committed Jul 28, 2023
1 parent 05cf096 commit 043b273
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/compiler/ast-node-const.bas
Expand Up @@ -76,6 +76,7 @@ function astNewCONSTi _

n = astNewNode( AST_NODECLASS_CONST, FB_DATATYPE_LONGINT, NULL )
n->val.i = value
n->val.hassuffix = FALSE

n = astNewCONV( dtype, subtype, n, AST_CONVOPT_DONTCHKPTR )
assert( n )
Expand All @@ -94,6 +95,7 @@ function astNewCONSTf _

n = astNewNode( AST_NODECLASS_CONST, FB_DATATYPE_DOUBLE )
n->val.f = value
n->val.hassuffix = FALSE

function = astNewCONV( dtype, NULL, n )
end function
Expand Down
14 changes: 12 additions & 2 deletions src/compiler/ast.bi
Expand Up @@ -117,6 +117,16 @@ type AST_NODE_ARG
lgt as longint '' length, used to push UDT's by value
end type

type AST_NODE_CONST
union '' extends FBVALUE
value as FBVALUE
s as FBSYMBOL_ ptr
i as longint
f as double
end union
hassuffix as integer '' TRUE = original constant had a suffix
end type

type AST_NODE_VAR
ofs as longint '' offset
end type
Expand Down Expand Up @@ -270,7 +280,7 @@ type ASTNODE
vector as integer '' 0, 2, 3, or 4 (> 2 for single only)

union
val as FBVALUE '' CONST nodes
val as AST_NODE_CONST '' CONST nodes
var_ as AST_NODE_VAR
idx as AST_NODE_IDX
ptr as AST_NODE_PTR
Expand Down Expand Up @@ -1471,7 +1481,7 @@ declare function astLoadMACRO( byval n as ASTNODE ptr ) as IRVREG ptr

#define astIsCAST(n) (n->class = AST_NODECLASS_CONV)

#define astConstGetVal( n ) (@(n)->val)
#define astConstGetVal( n ) (@(n)->val.value)
#define astConstGetFloat( n ) ((n)->val.f)
#define astConstGetInt( n ) ((n)->val.i)
#define astConstGetUint( n ) cunsg( (n)->val.i )
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/lex.bas
Expand Up @@ -1128,6 +1128,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
var pnum = @t.text[0]
*pnum = 0
t.len = 0
t.hassuffix = false

select case as const lexCurrentChar( )
'' integer part
Expand Down Expand Up @@ -1177,6 +1178,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
if( (flags and LEXCHECK_NOLETTERSUFFIX) = 0 ) then
select case lexCurrentChar( )
case CHAR_UUPP, CHAR_ULOW
t.hassuffix = TRUE
lexEatChar( )
t.dtype = typeToUnsigned( t.dtype )
have_u_suffix = TRUE
Expand All @@ -1187,6 +1189,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
'' 'L' | 'l'
case CHAR_LUPP, CHAR_LLOW
if( (flags and LEXCHECK_NOLETTERSUFFIX) = 0 ) then
t.hassuffix = TRUE
lexEatChar( )
'' 'LL'?
var c = lexCurrentChar( )
Expand Down Expand Up @@ -1214,6 +1217,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
if( (flags and LEXCHECK_NOLETTERSUFFIX) = 0 ) then
if( have_u_suffix = FALSE ) then
t.dtype = FB_DATATYPE_SINGLE
t.hassuffix = TRUE
lexEatChar( )
end if
end if
Expand All @@ -1224,6 +1228,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
if( (flags and LEXCHECK_NOLETTERSUFFIX) = 0 ) then
if( have_u_suffix = FALSE ) then
t.dtype = FB_DATATYPE_DOUBLE
t.hassuffix = TRUE
lexEatChar( )
end if
end if
Expand Down Expand Up @@ -1259,13 +1264,14 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
end if
end if
t.dtype = FB_DATATYPE_LONG

t.hassuffix = TRUE
lexEatChar( )

'' '!'
case FB_TK_SNGTYPECHAR
if( have_u_suffix = FALSE ) then
t.dtype = FB_DATATYPE_SINGLE
t.hassuffix = TRUE
lexEatChar( )
end if

Expand All @@ -1275,6 +1281,7 @@ private sub hReadNumber( byref t as FBTOKEN, byval flags as LEXCHECK )
'' isn't it a '##'?
if( lexGetLookAheadChar( ) <> FB_TK_DBLTYPECHAR ) then
t.dtype = FB_DATATYPE_DOUBLE
t.hassuffix = TRUE
lexEatChar( )
end if
end if
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/lex.bi
Expand Up @@ -65,6 +65,7 @@ type FBTOKEN
union
prdpos as integer '' period '.' pos in symbol names
hasesc as integer '' any '\' in literals
hassuffix as integer '' numeric literal has suffix
end union
suffixchar as integer

Expand Down Expand Up @@ -288,6 +289,8 @@ declare function hMatch _

#define lexGetSuffixChar( ) lex.ctx->head->suffixchar

#define lexGetLiteralHasSuffix( ) lex.ctx->head->hassuffix

#define lexGetHead( ) lex.ctx->head

#define lexCurrLineGet( ) lex.ctx->currline.data
Expand Down
14 changes: 10 additions & 4 deletions src/compiler/parser-expr-constant.bas
Expand Up @@ -109,26 +109,32 @@ end function

function cNumLiteral( byval skiptoken as integer ) as ASTNODE ptr
dim as integer dtype = any
dim as ASTNODE ptr expr = any

dtype = lexGetType( )

select case( dtype )
case FB_DATATYPE_DOUBLE
function = astNewCONSTf( val( *lexGetText( ) ), dtype )
expr = astNewCONSTf( val( *lexGetText( ) ), dtype )

case FB_DATATYPE_SINGLE
dim fval as single = val( *lexGetText( ) )
function = astNewCONSTf( fval , dtype )
expr = astNewCONSTf( fval , dtype )

case else
if( typeIsSigned( dtype ) ) then
function = astNewCONSTi( clngint( *lexGetText( ) ), dtype )
expr = astNewCONSTi( clngint( *lexGetText( ) ), dtype )
else
function = astNewCONSTi( culngint( *lexGetText( ) ), dtype )
expr = astNewCONSTi( culngint( *lexGetText( ) ), dtype )
end if
end select

'' record that it is a suffixed constant
expr->val.hassuffix = lexGetLiteralHasSuffix()

if( skiptoken ) then
lexSkipToken( )
end if

function = expr
end function
3 changes: 2 additions & 1 deletion src/compiler/symb-const.bas
Expand Up @@ -36,7 +36,8 @@ function symbAddConst _
exit function
end if

sym->val = *value
sym->val.value = *value
sym->val.hassuffix = FALSE

function = sym
end function
Expand Down
16 changes: 13 additions & 3 deletions src/compiler/symb.bi
Expand Up @@ -728,6 +728,16 @@ type FBVAR_DATA
prev as FBSYMBOL_ ptr
end type

type FBS_CONST
union '' extends FBVALUE
value as FBVALUE
s as FBSYMBOL_ ptr
i as longint
f as double
end union
hassuffix as integer
end type

type FBS_VAR
union
littext as zstring ptr
Expand Down Expand Up @@ -787,11 +797,11 @@ type FBSYMBOL
mangling as short '' FB_MANGLING

lgt as longint
ofs as longint '' for local vars, args, UDT's and fields
ofs as longint '' for local vars, args, UDT's and fields

union
var_ as FBS_VAR
val as FBVALUE '' constants
val as FBS_CONST '' constants
udt as FBS_STRUCT
enum_ as FBS_ENUM
proc as FBS_PROC
Expand Down Expand Up @@ -2251,7 +2261,7 @@ declare function symbCloneSimpleStruct( byval sym as FBSYMBOL ptr ) as FBSYMBOL

#define symbIsNameSpace(s) (s->class = FB_SYMBCLASS_NAMESPACE)

#define symbGetConstVal( sym ) (@((sym)->val))
#define symbGetConstVal( sym ) (@((sym)->val.value))
#define symbGetConstStr( sym ) ((sym)->val.s)
#define symbGetConstInt( sym ) ((sym)->val.i)
#define symbGetConstFloat( sym ) ((sym)->val.f)
Expand Down

0 comments on commit 043b273

Please sign in to comment.