Skip to content

Commit

Permalink
compiler: minor refactoring for empty string comparison optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 3, 2024
1 parent 00e0476 commit b65780f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use opt::{
AppendToSliceExprModel,
InlineStdInternalNosafeStobs,
StrCompExprModel,
EmptyCompareExprModel,
}
use conv for std::conv
use std::env::{Arch}
Expand Down Expand Up @@ -98,6 +99,7 @@ enum compExprModel: type {
&InlineStdInternalNosafeStobs,
&StrCompExprModel,
&RefExprModel,
&EmptyCompareExprModel,
}

struct exprCoder {
Expand Down Expand Up @@ -1328,6 +1330,15 @@ impl exprCoder {
self.oc.write(".fake_slice()")
}

fn emptyCompare(mut &self, mut m: &EmptyCompareExprModel) {
if m.Neg {
self.oc.write("!")
}
self.oc.write("(")
self.possibleRefExpr(m.Expr)
self.oc.write(").empty()")
}

fn model(mut &self, mut m: compExprModel) {
match type m {
| str:
Expand Down Expand Up @@ -1426,6 +1437,8 @@ impl exprCoder {
self.inlineStdInternalNosafeStobs((&InlineStdInternalNosafeStobs)(m))
| &RefExprModel:
self.var((&RefExprModel)(m).Var)
| &EmptyCompareExprModel:
self.emptyCompare((&EmptyCompareExprModel)(m))
|:
self.oc.write("<unimplemented_expression_model>")
}
Expand Down
26 changes: 9 additions & 17 deletions src/julec/opt/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ impl exprOptimizer {
match m.Op.Id {
| TokenId.Eqs:
if c.ReadStr() == "" {
*self.model = &CommonSubIdentExprModel{
ExprKind: m.Left.Kind,
let mut model = any(&EmptyCompareExprModel{
Expr: m.Left.Model,
Ident: "empty()",
}
Neg: false,
})
*self.model = unsafe { *(*ExprModel)(&model) }
break
}
let mut model = any(&StrCompExprModel{
Expand All @@ -104,19 +104,11 @@ impl exprOptimizer {
*self.model = unsafe { *(*ExprModel)(&model) }
| TokenId.NotEq:
if c.ReadStr() == "" {
let mut op = m.Op
op.Id = TokenId.Excl
op.Kind = TokenKind.Excl
*self.model = &UnaryExprModel{
Op: op,
Expr: &Data{
Model: &CommonSubIdentExprModel{
ExprKind: m.Left.Kind,
Expr: m.Left.Model,
Ident: "empty()",
},
},
}
let mut model = any(&EmptyCompareExprModel{
Expr: m.Left.Model,
Neg: true,
})
*self.model = unsafe { *(*ExprModel)(&model) }
break
}
let mut model = any(&StrCompExprModel{
Expand Down
6 changes: 6 additions & 0 deletions src/julec/opt/model.jule
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ use std::jule::lex::{Token}
use std::jule::constant::{Const}
use std::jule::sema::{
Var,
TypeKind,
ExprModel,
BinopExprModel,
IndexingExprModel,
BuiltinAppendCallExprModel,
SliceExprModel,
}

struct EmptyCompareExprModel {
Expr: ExprModel
Neg: bool
}

struct RefExprModel {
Var: &Var
}
Expand Down

0 comments on commit b65780f

Please sign in to comment.