Skip to content

Commit

Permalink
fbc: internal: add IR_EMITOPT to control backend options from AST/IR
Browse files Browse the repository at this point in the history
This commit adds some internal changes for tracking specific emit options in AST/IR
that can then be passed on to the backend / EMIT.  This will potentially provide a
generic way to extend AST options to the backend.

- add 'enum IR_EMITOPT' for options relating to EMIT options
- add 'AST_OPOPT.AST_OPOPT_DOINVERSE' for relational operators  to indicate that
  the AST expression needs to be inverted.  This allows AST to preserve the user's
  original expression and pass it on to the backend to solve.
- add 'IR_EMITOPT.IR_EMITOPT_REL_DOINVERSE' to indicate the option to the backend
- add 'EMIT_NODE.options' to track options emit options
- Update `EMIT_RELCB` to accept an options parameter
- For procedures handling relational operators in IR/EMIT, add parameters to the
  internal functions to accept 'options as IR_EMITOPT'.  This will then control
  how the backend handles code generation.
  • Loading branch information
jayrm committed Jun 25, 2023
1 parent 44451a9 commit 479d88e
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 85 deletions.
1 change: 1 addition & 0 deletions changelog.txt
@@ -1,6 +1,7 @@
Version 1.20.0

[changed]
- internal: (WIP) changes to all backends gcc/llvm/gas64/gas/gas+SSE for floating point comparison handling

[added]

Expand Down
10 changes: 8 additions & 2 deletions src/compiler/ast-node-bop.bas
Expand Up @@ -1687,7 +1687,10 @@ function astLoadBOP( byval n as ASTNODE ptr ) as IRVREG ptr
'' ex=label? Then this is an optimized conditional branch
if( n->op.ex <> NULL ) then
vr = NULL
irEmitBOP( op, v1, v2, NULL, n->op.ex )
irEmitBOP( op, v1, v2, NULL, n->op.ex, _
iif( n->op.options and AST_OPOPT_DOINVERSE, _
IR_EMITOPT_REL_DOINVERSE, _
IR_EMITOPT_NONE ) )
else
if( (n->op.options and AST_OPOPT_ALLOCRES) <> 0 ) then
'' Self-BOPs: Always re-use the lhs vreg to store the result,
Expand All @@ -1702,7 +1705,10 @@ function astLoadBOP( byval n as ASTNODE ptr ) as IRVREG ptr
v1->vector = n->vector
end if

irEmitBOP( op, v1, v2, vr, NULL )
irEmitBOP( op, v1, v2, vr, NULL, _
iif( n->op.options and AST_OPOPT_DOINVERSE, _
IR_EMITOPT_REL_DOINVERSE, _
IR_EMITOPT_NONE ) )

'' Return vr to parent even for self-BOPs - this is probably useless at the moment though,
'' because FB self-BOPs can only be used as statements, not in expressions...
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/ast-node-check.bas
Expand Up @@ -102,7 +102,7 @@ function astLoadBOUNDCHK _
vr, _
irAllocVRIMM( FB_DATATYPE_INTEGER, NULL, 0 ), _
NULL, _
label )
label, IR_EMITOPT_NONE )
irEmitJUMPPTR( vr )
irEmitLABELNF( label )
end if
Expand Down Expand Up @@ -195,7 +195,7 @@ function astLoadPTRCHK _
vr, _
irAllocVRIMM( FB_DATATYPE_INTEGER, NULL, 0 ), _
NULL, _
label )
label, IR_EMITOPT_NONE )
irEmitJUMPPTR( vr )
irEmitLABELNF( label )
end if
Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast.bi
Expand Up @@ -85,6 +85,7 @@ enum AST_OPOPT
AST_OPOPT_DONTCHKPTR = &h00000010
AST_OPOPT_DONTCHKOPOVL = &h00000020
AST_OPOPT_ISINI = &h00000040
AST_OPOPT_DOINVERSE = &h00000080 '' indicates that logic needs to be inverted by the backend (i.e. branching)

AST_OPOPT_DOPTRARITH = AST_OPOPT_LPTRARITH or AST_OPOPT_RPTRARITH

Expand Down
61 changes: 35 additions & 26 deletions src/compiler/emit.bas
Expand Up @@ -179,7 +179,8 @@ sub emitFlush( )
n->rel.rvreg, _
n->rel.label, _
n->rel.dvreg, _
n->rel.svreg )
n->rel.svreg, _
n->options )

case EMIT_NODECLASS_STK
cast( EMIT_STKCB, emit.opFnTb[n->stk.op] )( _
Expand Down Expand Up @@ -356,7 +357,8 @@ private function hNewREL _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

dim as EMIT_NODE ptr n
Expand All @@ -368,6 +370,7 @@ private function hNewREL _
n->rel.label = label
n->rel.dvreg = hNewVR( dvreg )
n->rel.svreg = hNewVR( svreg )
n->options = options

function = n

Expand Down Expand Up @@ -982,20 +985,21 @@ function emitGT _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CGTL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGTL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CGTF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGTF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CGTI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGTI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand All @@ -1006,20 +1010,21 @@ function emitLT _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CLTL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLTL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CLTF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLTF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CLTI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLTI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand All @@ -1030,20 +1035,21 @@ function emitEQ _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CEQL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CEQL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CEQF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CEQF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CEQI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CEQI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand All @@ -1054,20 +1060,21 @@ function emitNE _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CNEL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CNEL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CNEF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CNEF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CNEI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CNEI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand All @@ -1078,20 +1085,21 @@ function emitGE _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CGEL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGEL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CGEF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGEF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CGEI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CGEI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand All @@ -1102,20 +1110,21 @@ function emitLE _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr static

select case typeGetSizeType( dvreg->dtype )
'' longint?
case FB_SIZETYPE_INT64, FB_SIZETYPE_UINT64
function = hNewREL( EMIT_OP_CLEL, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLEL, rvreg, label, dvreg, svreg, options )

'' float?
case FB_SIZETYPE_FLOAT32, FB_SIZETYPE_FLOAT64
function = hNewREL( EMIT_OP_CLEF, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLEF, rvreg, label, dvreg, svreg, options )

case else
function = hNewREL( EMIT_OP_CLEI, rvreg, label, dvreg, svreg )
function = hNewREL( EMIT_OP_CLEI, rvreg, label, dvreg, svreg, options )
end select

end function
Expand Down
23 changes: 16 additions & 7 deletions src/compiler/emit.bi
Expand Up @@ -206,6 +206,8 @@ end type
type EMIT_NODE
class as EMIT_NODECLASS_ENUM

options as IR_EMITOPT

union
bop as EMIT_BOPNODE
uop as EMIT_UOPNODE
Expand Down Expand Up @@ -242,7 +244,8 @@ type EMIT_RELCB as sub _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
)

type EMIT_STKCB as sub _
Expand Down Expand Up @@ -624,47 +627,53 @@ declare function emitGT _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitLT _
( _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitEQ _
( _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitNE _
( _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitLE _
( _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitGE _
( _
byval rvreg as IRVREG ptr, _
byval label as FBSYMBOL ptr, _
byval dvreg as IRVREG ptr, _
byval svreg as IRVREG ptr _
byval svreg as IRVREG ptr, _
byval options as IR_EMITOPT _
) as EMIT_NODE ptr

declare function emitATN2 _
Expand Down

0 comments on commit 479d88e

Please sign in to comment.