Skip to content

Commit

Permalink
sema: minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 7, 2024
1 parent f74bfce commit 84c4e55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
62 changes: 33 additions & 29 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,28 @@ fn find_builtins_import(ident: str, imp: &ImportInfo): any {
// Data should be numerical constant of course.
fn fit_bitsize(mut &d: &Data) {
let mut prim = d.kind.prim()
let mut k = ""
let z = types::bitsize_of(prim.to_str())
if z == 1 << 6 {
// z is already equals to biggest bitsize.
ret
}
let mut k = 0
match {
| d.constant.is_i64():
k = types::int_from_bits(types::bitsize_of_int(d.constant.read_i64()))
k = types::bitsize_of_int(d.constant.read_i64())
if k > z {
prim.kind = types::int_from_bits(k)
}
| d.constant.is_u64():
k = types::uint_from_bits(types::bitsize_of_uint(d.constant.read_u64()))
k = types::bitsize_of_uint(d.constant.read_u64())
if k > z {
prim.kind = types::uint_from_bits(k)
}
| d.constant.is_f64():
k = types::float_from_bits(types::bitsize_of_float(d.constant.read_f64()))
}
if types::is_greater(k, types::real_kind_of(prim.kind)) {
prim.kind = k
k = types::bitsize_of_float(d.constant.read_f64())
if k > z {
prim.kind = types::float_from_bits(k)
}
}
}

Expand Down Expand Up @@ -172,10 +183,6 @@ fn is_ptr_arithmetic_compatible_int(mut &d: &Data): bool {
ret tcc.check()
}

fn normalize_type(mut &d: &Data) {

}

fn apply_cast_kind_model(mut &d: &Data) {
d.model = &CastingExprModel{
expr: d.model,
Expand Down Expand Up @@ -320,11 +327,18 @@ impl Eval {
self.s.push_suggestion(fmt, args...)
}

fn allow_builtin(mut self) { self.dis_builtin = false }
fn disallow_builtin(mut self) { self.dis_builtin = true }
fn allow_builtin(mut self) {
self.dis_builtin = false
}

fn disallow_builtin(mut self) {
self.dis_builtin = true
}

// Reports whether evaluation in unsafe scope.
fn is_unsafe(self): bool { ret self.unsafety }
fn is_unsafe(self): bool {
ret self.unsafety
}

// Reports whether evaluated expression is in global scope.
fn is_global(self): bool {
Expand Down Expand Up @@ -352,13 +366,13 @@ impl Eval {
d.kind = new(TypeKind, *self.prefix)
d.constant.set_f64(d.constant.as_f64())
| types::is_sig_int(prim.to_str()):
if !int_assignable(prim.to_str(), d) {
if !sig_assignable(prim.to_str(), d) {
ret false
}
d.kind = new(TypeKind, *self.prefix)
d.constant.set_i64(d.constant.as_i64())
| types::is_unsig_int(prim.to_str()):
if !int_assignable(prim.to_str(), d) {
if !unsig_assignable(prim.to_str(), d) {
ret false
}
d.kind = new(TypeKind, *self.prefix)
Expand Down Expand Up @@ -1663,10 +1677,8 @@ impl Eval {
match {
| types::is_float(prim.kind):
d.constant.set_f64(d.constant.as_f64())

| types::is_sig_int(prim.kind):
d.constant.set_i64(d.constant.as_i64())

| types::is_unsig_int(prim.kind):
d.constant.set_u64(d.constant.as_u64())
}
Expand All @@ -1692,16 +1704,12 @@ impl Eval {
match {
| prim.is_any():
// The any type supports casting to any data type.

| prim.is_str():
self.cast_str(d, error_token)

| types::is_int(prim.to_str()):
self.cast_int(t, d, error_token)

| types::is_num(prim.to_str()):
self.cast_num(t, d, error_token)

|:
self.push_err(error_token, LogMsg.TypeNotSupportsCasting, t.to_str())
}
Expand Down Expand Up @@ -1939,7 +1947,6 @@ impl Eval {
// Make empty types to generics for ordering.
f.generics = make([]&TypeKind, f.decl.generics.len)
ret true, true

| !self.s.check_generic_quantity(f.decl.generics.len, fc.generics.len, fc.token):
ret false, false

Expand Down Expand Up @@ -2123,10 +2130,8 @@ impl Eval {
| fc.unhandled():
self.push_err(fc.token, LogMsg.UnhandledExceptional)
self.push_suggestion(LogMsg.HandleExceptional)

| fc.ignored():
// Ok.

|: // Handled with scope.
self.process_exceptional_handler(f, fc, d)
}
Expand Down Expand Up @@ -2219,7 +2224,9 @@ impl Eval {
expr: d.model,
method: ins,
}
d.kind = &TypeKind{kind: ins}
d.kind = &TypeKind{
kind: ins,
}
ret d
}

Expand Down Expand Up @@ -3468,10 +3475,8 @@ impl UnaryEval {
ret
}
self.d.kind = t.elem

| self.d.kind.sptr() != nil:
self.d.kind = self.d.kind.sptr().elem

|:
self.d = nil
ret
Expand All @@ -3486,7 +3491,6 @@ impl UnaryEval {
| &StructLitExprModel:
let mut lit = (&StructLitExprModel)(self.d.model)
make_struct_lit_alloc(self.d, lit)

|:
match {
| can_get_ptr(self.d):
Expand Down
12 changes: 6 additions & 6 deletions std/jule/types/bits.jule
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn bitsize_of(k: str): int {
// Returns signed integer kind by bit-size.
// Possible bit-sizes are: 8, 16, 32, and 64.
// Returns empty string if bits is invalid.
pub fn int_from_bits(bits: u64): str {
pub fn int_from_bits(bits: int): str {
match bits {
| 1 << 3:
ret TypeKind.I8
Expand All @@ -101,7 +101,7 @@ pub fn int_from_bits(bits: u64): str {
// Returns unsigned integer kind by bit-size.
// Possible bit-sizes are: 8, 16, 32, and 64.
// Returns empty string if bits is invalid.
pub fn uint_from_bits(bits: u64): str {
pub fn uint_from_bits(bits: int): str {
match bits {
| 1 << 3:
ret TypeKind.U8
Expand All @@ -119,7 +119,7 @@ pub fn uint_from_bits(bits: u64): str {
// Returns floating-point kind by bit-size.
// Possible bit-sizes are: 32, and 64.
// Returns empty string if bits is invalid.
pub fn float_from_bits(bits: u64): str {
pub fn float_from_bits(bits: int): str {
match bits {
| 1 << 5:
ret TypeKind.F32
Expand Down Expand Up @@ -157,7 +157,7 @@ pub fn check_bit_float(val: str, bit: int): bool {
// Possible values are:
// - 32 for 32-bit
// - 64 for 64-bit
pub fn bitsize_of_float(x: f64): u64 {
pub fn bitsize_of_float(x: f64): int {
match {
| MIN_F32 <= x && x <= MAX_F32:
ret 1 << 5
Expand All @@ -173,7 +173,7 @@ pub fn bitsize_of_float(x: f64): u64 {
// - 16 for 16-bit
// - 32 for 32-bit
// - 64 for 64-bit
pub fn bitsize_of_int(x: i64): u64 {
pub fn bitsize_of_int(x: i64): int {
match {
| MIN_I8 <= x && x <= MAX_I8:
ret 1 << 3
Expand All @@ -193,7 +193,7 @@ pub fn bitsize_of_int(x: i64): u64 {
// - 16 for 16-bit
// - 32 for 32-bit
// - 64 for 64-bit
pub fn bitsize_of_uint(x: u64): u64 {
pub fn bitsize_of_uint(x: u64): int {
match {
| x <= MAX_U8:
ret 1 << 3
Expand Down

0 comments on commit 84c4e55

Please sign in to comment.