Skip to content

Commit

Permalink
compiler: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 16, 2024
1 parent 4342988 commit 36139fb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
34 changes: 8 additions & 26 deletions src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use env
use optimizing::{
UnsafeBinopExprModel,
UnsafeIndexingExprModel,
PushToSliceExprModel,
}

use conv for std::conv
Expand Down Expand Up @@ -820,16 +821,16 @@ impl ExprCoder {
ret obj
}

fn __append_call_assign(mut self, &dest_expr: str, mut &dest_kind: &TypeKind,
mut &s: &SliceExprModel, mut &m: &BuiltinAppendCallExprModel): str {
let mut obj = dest_expr
fn push_to_slice(mut self, mut m: &PushToSliceExprModel): str {
let dest = self.model(m.dest)
let mut obj = dest
obj += " = jule::alloc_for_append("
obj += self.model(m.dest)
obj += dest
obj += ","
obj += conv::itoa(s.elems.len)
obj += conv::itoa(m.elems.elems.len)
obj += ");"
for (_, mut e) in s.elems {
obj += dest_expr
for (_, mut e) in m.elems.elems {
obj += dest
// Use the "__push" function to skip allocation boundary checking.
obj += ".__push("
obj += self.model(e)
Expand All @@ -838,25 +839,6 @@ impl ExprCoder {
ret obj
}

fn append_call_assign(mut self, mut dest: ExprModel, mut m: &BuiltinAppendCallExprModel): (str, optimized: bool) {
match type m.elements {
| &SliceExprModel:
match type dest {
| &Var:
let mut s = (&SliceExprModel)(m.elements)
let dest_expr = self.model(dest)
let mut dest_var = (&Var)(dest)
ret self.__append_call_assign(dest_expr, dest_var.kind.kind, s, m), true
| &StructSubIdentExprModel:
let mut s = (&SliceExprModel)(m.elements)
let dest_expr = self.model(dest)
let mut dest_field = (&StructSubIdentExprModel)(dest).field
ret self.__append_call_assign(dest_expr, dest_field.kind, s, m), true
}
}
ret self.append_call(m), false
}

fn append_call(mut self, mut m: &BuiltinAppendCallExprModel): str {
let mut obj = "jule::append("
obj += self.model(m.dest)
Expand Down
21 changes: 6 additions & 15 deletions src/julec/obj/cxx/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// license that can be found in the LICENSE file.

use env
use optimizing::{
PushToSliceExprModel,
}

use std::jule::lex::{TokenKind, is_ignore_ident}
use std::jule::sema::{
Expand Down Expand Up @@ -315,21 +318,7 @@ impl ScopeCoder {
fn assign(mut self, mut a: &Assign): str {
let mut obj = self.oc.ec.expr(a.l.model)
obj += a.op.kind
if env::OPT_APPEND {
match type a.r.model {
| &BuiltinAppendCallExprModel:
let mut model = (&BuiltinAppendCallExprModel)(a.r.model)
let (expr, optimized) = self.oc.ec.append_call_assign(a.l.model, model)
if optimized {
ret expr
}
obj += expr
|:
obj += self.oc.ec.expr(a.r.model)
}
} else {
obj += self.oc.ec.expr(a.r.model)
}
obj += self.oc.ec.expr(a.r.model)
obj += ";"
ret obj
}
Expand Down Expand Up @@ -709,6 +698,8 @@ impl ScopeCoder {
ret self.break_st((&BreakSt)(st))
| &RetSt:
ret self.ret_st((&RetSt)(st))
| &PushToSliceExprModel:
ret self.oc.ec.push_to_slice((&PushToSliceExprModel)(st))
|:
ret "<unimplemented_stmt>"
}
Expand Down
8 changes: 8 additions & 0 deletions src/julec/optimizing/model.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// license that can be found in the LICENSE file.

use std::jule::sema::{
ExprModel,
BinopExprModel,
IndexingExprModel,
BuiltinAppendCallExprModel,
SliceExprModel,
}

pub struct UnsafeBinopExprModel {
Expand All @@ -14,3 +17,8 @@ pub struct UnsafeBinopExprModel {
pub struct UnsafeIndexingExprModel {
pub node: &IndexingExprModel
}

pub struct PushToSliceExprModel{
pub dest: ExprModel
pub elems: &SliceExprModel
}
3 changes: 2 additions & 1 deletion src/julec/optimizing/optimizer.jule
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl Optimizer {
pub fn optimize(mut self) {
// Do optimizatitons if any enabled.
match {
| env::OPT_COND:
| env::OPT_COND
| env::OPT_APPEND:
break
|:
ret
Expand Down
23 changes: 23 additions & 0 deletions src/julec/optimizing/scope.jule
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use std::jule::sema::{
RetSt,
BinopExprModel,
OperandExprModel,
BuiltinAppendCallExprModel,
SliceExprModel,
StructSubIdentExprModel,
}
use strings for std::strings

Expand Down Expand Up @@ -213,6 +216,7 @@ impl ScopeOptimizer {
fn optimize_assign(mut self, mut assign: &Assign) {
ExprOptimizer.optimize(assign.l.model)
ExprOptimizer.optimize(assign.r.model)

match assign.op.kind {
| TokenKind.SolidusEq | TokenKind.PercentEq:
// Do not check division of structures safety.
Expand All @@ -232,6 +236,25 @@ impl ScopeOptimizer {
ret
}
assign.op.kind += "="
ret
}
}

if env::OPT_APPEND {
match type assign.r.model {
| &BuiltinAppendCallExprModel:
let mut m = (&BuiltinAppendCallExprModel)(assign.r.model)
match type m.elements {
| &SliceExprModel:
match type m.dest {
| &Var
| &StructSubIdentExprModel:
self.set_current_stmt(&PushToSliceExprModel{
dest: m.dest,
elems: (&SliceExprModel)(m.elements),
})
}
}
}
}
}
Expand Down

0 comments on commit 36139fb

Please sign in to comment.