Skip to content

Commit

Permalink
compiler: improve --opt-access optimization flag for nil checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 22, 2024
1 parent 53b5d7a commit 10e0bce
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 98 deletions.
46 changes: 38 additions & 8 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,
StrCompExprModel,
EmptyCompareExprModel,
UnsafeDerefExprModel,
}
use conv for std::conv
use std::env::{Arch}
Expand Down Expand Up @@ -95,6 +96,7 @@ enum compExprModel: type {
&StrCompExprModel,
&RefExprModel,
&EmptyCompareExprModel,
&UnsafeDerefExprModel,
}

struct exprCoder {
Expand Down Expand Up @@ -598,6 +600,19 @@ impl exprCoder {
ret false
}

fn writeReceiver(mut &self, mut m: compExprModel): (safeDeref: bool) {
match type m {
| &UnaryExprModel:
self.possibleRefExpr((&UnaryExprModel)(m).Expr.Model)
ret true
| &UnsafeDerefExprModel:
self.possibleRefExpr((&UnsafeDerefExprModel)(m).Base.Expr.Model)
ret false
|:
panic("implementation mistake, this panic call should be unreachable")
}
}

fn pureFuncCall(mut &self, mut &m: &FnCallExprModel) {
wrapped := self.isWrapped(m)
self.modelForCall(m.Expr)
Expand All @@ -624,17 +639,19 @@ impl exprCoder {
match {
| m.Func.Decl.Params[0].IsRef():
// Ignore dereferencing, use data directly to call method.
self.possibleRefExpr((&UnaryExprModel)(ssie.Expr.Model).Expr.Model)
_ = self.writeReceiver(ssie.Expr.Model)
| ssie.Expr.Kind.Sptr() != nil:
// Ignore dereferencing, use data directly to call [ptr] method.
self.possibleRefExpr((&UnaryExprModel)(ssie.Expr.Model).Expr.Model)
self.oc.write(".ptr(")
if !env::Production {
self.oc.write("\"")
self.oc.locInfo(m.Token)
self.oc.write("\"")
safe := self.writeReceiver(ssie.Expr.Model)
if safe {
self.oc.write(".ptr(")
if !env::Production {
self.oc.write("\"")
self.oc.locInfo(m.Token)
self.oc.write("\"")
}
self.oc.write(")")
}
self.oc.write(")")
|:
if ssie.Expr.Kind.Struct() != nil {
match type ssie.Expr.Model {
Expand Down Expand Up @@ -1333,6 +1350,17 @@ impl exprCoder {
self.oc.write(").empty()")
}

fn unsafeDeref(mut &self, mut m: &UnsafeDerefExprModel) {
match {
| m.Base.Expr.Kind.Sptr() != nil:
self.oc.write("(*")
self.possibleRefExpr(m.Base.Expr.Model)
self.oc.write(".alloc)")
|:
panic("implementation mistake, this panic call should be unreachable")
}
}

fn model(mut &self, mut m: compExprModel) {
match type m {
| str:
Expand Down Expand Up @@ -1425,6 +1453,8 @@ impl exprCoder {
self.var((&RefExprModel)(m).Var)
| &EmptyCompareExprModel:
self.emptyCompare((&EmptyCompareExprModel)(m))
| &UnsafeDerefExprModel:
self.unsafeDeref((&UnsafeDerefExprModel)(m))
|:
self.oc.write("<unimplemented_expression_model>")
}
Expand Down
11 changes: 11 additions & 0 deletions src/julec/opt/data.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

static mut emptyData = new(data)

// All-in-one all analysis information structures.
struct data {
boundary: &boundary
nils: &nils
}
Loading

0 comments on commit 10e0bce

Please sign in to comment.