Skip to content

Commit

Permalink
ast: Add astOpIsRelational() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dkl committed May 2, 2015
1 parent 6e27852 commit 73a72f2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 47 deletions.
6 changes: 1 addition & 5 deletions src/compiler/ast-misc.bas
Expand Up @@ -1269,10 +1269,6 @@ end function

function astIsRelationalBop( byval n as ASTNODE ptr ) as integer
if( n->class = AST_NODECLASS_BOP ) then
select case( n->op.op )
case AST_OP_EQ, AST_OP_NE, AST_OP_GT, _
AST_OP_LT, AST_OP_GE, AST_OP_LE
function = TRUE
end select
function = astOpIsRelational( n->op.op )
end if
end function
27 changes: 10 additions & 17 deletions src/compiler/ast-node-bop.bas
Expand Up @@ -406,12 +406,8 @@ private function hCheckPointer _
return TRUE

'' relational op?
case AST_OP_EQ, AST_OP_GT, AST_OP_LT, AST_OP_NE, AST_OP_LE, AST_OP_GE

return TRUE

case else
return FALSE
case else
return astOpIsRelational( op )
end select

end function
Expand Down Expand Up @@ -807,10 +803,8 @@ function astNewBOP _
end if
end select

select case as const op
'' concatenation?
case AST_OP_ADD

if( op = AST_OP_ADD ) then
'' both literals?
if( litsym <> NULL ) then
'' ok to convert at compile-time?
Expand All @@ -836,7 +830,7 @@ function astNewBOP _
'' to allow optimizations..

'' comparison?
case AST_OP_EQ, AST_OP_GT, AST_OP_LT, AST_OP_NE, AST_OP_LE, AST_OP_GE
elseif( astOpIsRelational( op ) ) then
'' both literals?
if( litsym <> NULL ) then
return hWstrLiteralCompare( op, l, r )
Expand All @@ -852,9 +846,9 @@ function astNewBOP _
rdclass = FB_DATACLASS_INTEGER

'' no other operation allowed
case else
else
exit function
end select
end if

'' One is not a string, but e.g. an integer. Disallow if the
'' other is not a DEREF'ed wchar ptr - this allows comparisons
Expand Down Expand Up @@ -902,9 +896,8 @@ function astNewBOP _
end if
end if

select case as const op
'' concatenation?
case AST_OP_ADD
if( op = AST_OP_ADD ) then
'' both literals?
if( litsym <> NULL ) then
return hStrLiteralConcat( l, r )
Expand All @@ -921,7 +914,7 @@ function astNewBOP _
'' to allow optimizations..

'' comparison?
case AST_OP_EQ, AST_OP_GT, AST_OP_LT, AST_OP_NE, AST_OP_LE, AST_OP_GE
elseif( astOpIsRelational( op ) ) then
'' both literals?
if( litsym <> NULL ) then
return hStrLiteralCompare( op, l, r )
Expand All @@ -937,9 +930,9 @@ function astNewBOP _
rdclass = FB_DATACLASS_INTEGER

'' no other operation allowed
case else
else
exit function
end select
end if

'' zstrings?
elseif( (typeGet( ldtype ) = FB_DATATYPE_CHAR) or _
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/ast-optimize.bas
Expand Up @@ -1590,10 +1590,9 @@ function astOptAssignment( byval n as ASTNODE ptr ) as ASTNODE ptr
case AST_NODECLASS_BOP
'' can't be a relative op -- unless EMIT is changed to not assume
'' the res operand is a reg
select case as const r->op.op
case AST_OP_EQ, AST_OP_GT, AST_OP_LT, AST_OP_NE, AST_OP_LE, AST_OP_GE
if( astOpIsRelational( r->op.op ) ) then
exit function
end select
end if

case else
exit function
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/ast.bas
Expand Up @@ -116,12 +116,12 @@ dim shared ast_opTB( 0 to AST_OPCODES-1 ) as AST_OPINFO => _
(AST_NODECLASS_BOP , AST_OPFLAGS_NONE, @"shr" , AST_OP_SHR_SELF ), _ '' AST_OP_SHR
(AST_NODECLASS_BOP , AST_OPFLAGS_NONE, @"pow" , AST_OP_POW_SELF ), _ '' AST_OP_POW
(AST_NODECLASS_BOP , AST_OPFLAGS_NONE, @"&" , AST_OP_CONCAT_SELF ), _ '' AST_OP_CONCAT
(AST_NODECLASS_COMP , AST_OPFLAGS_COMM, @"=" ), _ '' AST_OP_EQ
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE, @">" ), _ '' AST_OP_GT
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE, @"<" ), _ '' AST_OP_LT
(AST_NODECLASS_COMP , AST_OPFLAGS_COMM, @"<>" ), _ '' AST_OP_NE
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE, @">=" ), _ '' AST_OP_GE
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE, @"<=" ), _ '' AST_OP_LE
(AST_NODECLASS_COMP , AST_OPFLAGS_COMM or AST_OPFLAGS_RELATIONAL, @"=" ), _ '' AST_OP_EQ
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE or AST_OPFLAGS_RELATIONAL, @">" ), _ '' AST_OP_GT
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE or AST_OPFLAGS_RELATIONAL, @"<" ), _ '' AST_OP_LT
(AST_NODECLASS_COMP , AST_OPFLAGS_COMM or AST_OPFLAGS_RELATIONAL, @"<>" ), _ '' AST_OP_NE
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE or AST_OPFLAGS_RELATIONAL, @">=" ), _ '' AST_OP_GE
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE or AST_OPFLAGS_RELATIONAL, @"<=" ), _ '' AST_OP_LE
(AST_NODECLASS_COMP , AST_OPFLAGS_NONE, @"is" ), _ '' AST_OP_IS
(AST_NODECLASS_UOP , AST_OPFLAGS_NONE, @"not" ), _ '' AST_OP_NOT
(AST_NODECLASS_UOP , AST_OPFLAGS_NONE, @"+" ), _ '' AST_OP_PLUS
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/ast.bi
Expand Up @@ -393,6 +393,7 @@ enum AST_OPFLAGS
AST_OPFLAGS_SELF = &h00000001 '' op=
AST_OPFLAGS_COMM = &h00000002 '' commutative
AST_OPFLAGS_NORES = &h00000004 '' no result (it's a SUB)
AST_OPFLAGS_RELATIONAL = &h00000008 '' whether it's one of the relational BOPs
end enum

type AST_OPINFO
Expand Down Expand Up @@ -1443,13 +1444,10 @@ declare function astLoadNIDXARRAY( byval n as ASTNODE ptr ) as IRVREG ptr
#define astTypeIniGetOfs( n ) n->typeini.ofs

#define astGetOpClass( op ) ast_opTB(op).class

#define astGetOpIsCommutative( op ) ((ast_opTB(op).flags and AST_OPFLAGS_COMM) <> 0)

#define astGetOpIsSelf( op ) ((ast_opTB(op).flags and AST_OPFLAGS_SELF) <> 0)

#define astGetOpNoResult( op ) ((ast_opTB(op).flags and AST_OPFLAGS_NORES) <> 0)

#define astOpIsRelational( op ) ((ast_opTB(op).flags and AST_OPFLAGS_RELATIONAL) <> 0)
#define astGetOpSelfVer( op ) ast_opTB(op).selfop

#define astGetOpId( op ) ast_opTB(op).id
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/ir-hlc.bas
Expand Up @@ -1706,10 +1706,9 @@ private function typeCBop _
) as integer

'' Result of relational/comparison operators is int
select case( op )
case AST_OP_EQ, AST_OP_NE, AST_OP_GT, AST_OP_LT, AST_OP_GE, AST_OP_LE
if( astOpIsRelational( op ) ) then
return FB_DATATYPE_LONG
end select
end if

'' This tries to do C operand type promotion (and is probably not
'' 100% accurate), in order to figure out the result type of BOP/UOP
Expand Down
10 changes: 1 addition & 9 deletions src/compiler/ir-llvm.bas
Expand Up @@ -1354,14 +1354,6 @@ private sub _emitBop _

dim as string ln, falselabel
dim as IRVREG ptr vresult = any, vtemp = any
dim as integer is_comparison = any

select case( op )
case AST_OP_EQ, AST_OP_NE, AST_OP_GT, AST_OP_LT, AST_OP_GE, AST_OP_LE
is_comparison = TRUE
case else
is_comparison = FALSE
end select

'' Conditional branch?
if( label ) then
Expand Down Expand Up @@ -1431,7 +1423,7 @@ private sub _emitBop _

'' LLVM comparison ops return i1, but we usually want i32,
'' so do an sign-extending cast (i1 -1 to i32 -1).
if( is_comparison ) then
if( astOpIsRelational( op ) ) then
vtemp = irhlAllocVreg( vresult->dtype, vresult->subtype )
ln = hVregToStr( vtemp )
ln += " = sext "
Expand Down

0 comments on commit 73a72f2

Please sign in to comment.