From 5bdb424c6b0daa3976546db79c89b1f47f910536 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 15 Sep 2018 10:14:24 +0100 Subject: [PATCH] context.allocator = a; Remove __ from runtime procs; improve division for complex numbers --- core/mem/alloc.odin | 5 +- core/mem/mem.odin | 7 +- core/runtime/core.odin | 2 +- core/runtime/internal.odin | 203 ++++++++++++++++++++++--------------- src/check_expr.cpp | 66 ++++++------ src/check_stmt.cpp | 2 +- src/checker.cpp | 3 + src/ir.cpp | 101 +++++++++--------- 8 files changed, 212 insertions(+), 177 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index fa2f73767..f2ffeb323 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -110,10 +110,7 @@ make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, a } make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T { runtime.make_map_expr_error_loc(loc, cap); - - c := context; - c.allocator = allocator; - context = c; + context.allocator = allocator; m: T; reserve_map(&m, cap); diff --git a/core/mem/mem.odin b/core/mem/mem.odin index d3825f3b4..a430e0124 100644 --- a/core/mem/mem.odin +++ b/core/mem/mem.odin @@ -213,14 +213,13 @@ init_arena_from_context :: proc(using a: ^Arena, size: int) { context_from_allocator :: proc(a: Allocator) -> type_of(context) { - c := context; - c.allocator = a; - return c; + context.allocator = a; + return context; } destroy_arena :: proc(using a: ^Arena) { if backing.procedure != nil { - context = context_from_allocator(backing); + context.allocator = backing; if memory != nil { free(&memory[0]); } diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 2e2f63ef7..23b462c79 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -331,7 +331,7 @@ init_global_temporary_allocator :: proc(data: []byte, backup_allocator := contex default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) { fd := os.stderr; - __print_caller_location(fd, loc); + print_caller_location(fd, loc); os.write_string(fd, " "); os.write_string(fd, prefix); if len(message) > 0 { diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 5b1c6b434..3120be6cf 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -5,7 +5,7 @@ import "core:os" import "core:unicode/utf8" -__print_u64 :: proc(fd: os.Handle, u: u64) { +print_u64 :: proc(fd: os.Handle, u: u64) { digits := "0123456789"; a: [129]byte; @@ -20,7 +20,7 @@ __print_u64 :: proc(fd: os.Handle, u: u64) { os.write(fd, a[i:]); } -__print_i64 :: proc(fd: os.Handle, u: i64) { +print_i64 :: proc(fd: os.Handle, u: i64) { digits := "0123456789"; neg := u < 0; @@ -41,19 +41,19 @@ __print_i64 :: proc(fd: os.Handle, u: i64) { os.write(fd, a[i:]); } -__print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) { +print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) { os.write_string(fd, file_path); os.write_byte(fd, '('); - __print_u64(fd, u64(line)); + print_u64(fd, u64(line)); os.write_byte(fd, ':'); - __print_u64(fd, u64(column)); + print_u64(fd, u64(column)); os.write_byte(fd, ')'); } -__print_typeid :: proc(fd: os.Handle, id: typeid) { +print_typeid :: proc(fd: os.Handle, id: typeid) { ti := type_info_of(id); - __print_type(fd, ti); + print_type(fd, ti); } -__print_type :: proc(fd: os.Handle, ti: ^Type_Info) { +print_type :: proc(fd: os.Handle, ti: ^Type_Info) { if ti == nil { os.write_string(fd, "nil"); return; @@ -70,16 +70,16 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { case uintptr: os.write_string(fd, "uintptr"); case: os.write_byte(fd, info.signed ? 'i' : 'u'); - __print_u64(fd, u64(8*ti.size)); + print_u64(fd, u64(8*ti.size)); } case Type_Info_Rune: os.write_string(fd, "rune"); case Type_Info_Float: os.write_byte(fd, 'f'); - __print_u64(fd, u64(8*ti.size)); + print_u64(fd, u64(8*ti.size)); case Type_Info_Complex: os.write_string(fd, "complex"); - __print_u64(fd, u64(8*ti.size)); + print_u64(fd, u64(8*ti.size)); case Type_Info_String: os.write_string(fd, "string"); case Type_Info_Boolean: @@ -88,7 +88,7 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { case bool: os.write_string(fd, "bool"); case: os.write_byte(fd, 'b'); - __print_u64(fd, u64(8*ti.size)); + print_u64(fd, u64(8*ti.size)); } case Type_Info_Any: os.write_string(fd, "any"); @@ -100,7 +100,7 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { os.write_string(fd, "rawptr"); } else { os.write_string(fd, "^"); - __print_type(fd, info.elem); + print_type(fd, info.elem); } case Type_Info_Procedure: os.write_string(fd, "proc"); @@ -111,13 +111,13 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { os.write_string(fd, "("); for t, i in t.types { if i > 0 do os.write_string(fd, ", "); - __print_type(fd, t); + print_type(fd, t); } os.write_string(fd, ")"); } if info.results != nil { os.write_string(fd, " -> "); - __print_type(fd, info.results); + print_type(fd, info.results); } case Type_Info_Tuple: count := len(info.names); @@ -131,27 +131,27 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { os.write_string(fd, name); os.write_string(fd, ": "); } - __print_type(fd, t); + print_type(fd, t); } if count != 1 do os.write_string(fd, ")"); case Type_Info_Array: os.write_string(fd, "["); - __print_u64(fd, u64(info.count)); + print_u64(fd, u64(info.count)); os.write_string(fd, "]"); - __print_type(fd, info.elem); + print_type(fd, info.elem); case Type_Info_Dynamic_Array: os.write_string(fd, "[dynamic]"); - __print_type(fd, info.elem); + print_type(fd, info.elem); case Type_Info_Slice: os.write_string(fd, "[]"); - __print_type(fd, info.elem); + print_type(fd, info.elem); case Type_Info_Map: os.write_string(fd, "map["); - __print_type(fd, info.key); + print_type(fd, info.key); os.write_byte(fd, ']'); - __print_type(fd, info.value); + print_type(fd, info.value); case Type_Info_Struct: os.write_string(fd, "struct "); @@ -159,7 +159,7 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { if info.is_raw_union do os.write_string(fd, "#raw_union "); if info.custom_align { os.write_string(fd, "#align "); - __print_u64(fd, u64(ti.align)); + print_u64(fd, u64(ti.align)); os.write_byte(fd, ' '); } os.write_byte(fd, '{'); @@ -167,7 +167,7 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { if i > 0 do os.write_string(fd, ", "); os.write_string(fd, name); os.write_string(fd, ": "); - __print_type(fd, info.types[i]); + print_type(fd, info.types[i]); } os.write_byte(fd, '}'); @@ -175,13 +175,13 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { os.write_string(fd, "union {"); for variant, i in info.variants { if i > 0 do os.write_string(fd, ", "); - __print_type(fd, variant); + print_type(fd, variant); } os.write_string(fd, "}"); case Type_Info_Enum: os.write_string(fd, "enum "); - __print_type(fd, info.base); + print_type(fd, info.base); os.write_string(fd, " {"); for name, i in info.names { if i > 0 do os.write_string(fd, ", "); @@ -193,7 +193,7 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { os.write_string(fd, "bit_field "); if ti.align != 1 { os.write_string(fd, "#align "); - __print_u64(fd, u64(ti.align)); + print_u64(fd, u64(ti.align)); os.write_byte(fd, ' '); } os.write_string(fd, " {"); @@ -201,32 +201,32 @@ __print_type :: proc(fd: os.Handle, ti: ^Type_Info) { if i > 0 do os.write_string(fd, ", "); os.write_string(fd, name); os.write_string(fd, ": "); - __print_u64(fd, u64(info.bits[i])); + print_u64(fd, u64(info.bits[i])); } os.write_string(fd, "}"); } } -__string_eq :: proc "contextless" (a, b: string) -> bool { +string_eq :: proc "contextless" (a, b: string) -> bool { switch { case len(a) != len(b): return false; case len(a) == 0: return true; case &a[0] == &b[0]: return true; } - return __string_cmp(a, b) == 0; + return string_cmp(a, b) == 0; } -__string_cmp :: proc "contextless" (a, b: string) -> int { +string_cmp :: proc "contextless" (a, b: string) -> int { return mem.compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b))); } -__string_ne :: inline proc "contextless" (a, b: string) -> bool { return !__string_eq(a, b); } -__string_lt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) < 0; } -__string_gt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) > 0; } -__string_le :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) <= 0; } -__string_ge :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) >= 0; } +string_ne :: inline proc "contextless" (a, b: string) -> bool { return !string_eq(a, b); } +string_lt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) < 0; } +string_gt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) > 0; } +string_le :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) <= 0; } +string_ge :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) >= 0; } -__cstring_len :: proc "contextless" (s: cstring) -> int { +cstring_len :: proc "contextless" (s: cstring) -> int { n := 0; for p := (^byte)(s); p != nil && p^ != 0; p = mem.ptr_offset(p, 1) { n += 1; @@ -234,30 +234,30 @@ __cstring_len :: proc "contextless" (s: cstring) -> int { return n; } -__cstring_to_string :: proc "contextless" (s: cstring) -> string { +cstring_to_string :: proc "contextless" (s: cstring) -> string { if s == nil do return ""; ptr := (^byte)(s); - n := __cstring_len(s); + n := cstring_len(s); return transmute(string)mem.Raw_String{ptr, n}; } -__complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); } -__complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); } +complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); } +complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); } -__complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b); } -__complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); } +complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b); } +complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); } bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) { if 0 <= index && index < count do return; fd := os.stderr; - __print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); os.write_string(fd, " Index "); - __print_i64(fd, i64(index)); + print_i64(fd, i64(index)); os.write_string(fd, " is out of bounds range 0:"); - __print_i64(fd, i64(count)); + print_i64(fd, i64(count)); os.write_byte(fd, '\n'); debug_trap(); } @@ -267,13 +267,13 @@ slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi: fd := os.stderr; - __print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); os.write_string(fd, " Invalid slice indices: "); - __print_i64(fd, i64(lo)); + print_i64(fd, i64(lo)); os.write_string(fd, ":"); - __print_i64(fd, i64(hi)); + print_i64(fd, i64(hi)); os.write_string(fd, ":"); - __print_i64(fd, i64(len)); + print_i64(fd, i64(len)); os.write_byte(fd, '\n'); debug_trap(); } @@ -282,13 +282,13 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, if 0 <= low && low <= high && high <= max do return; fd := os.stderr; - __print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); os.write_string(fd, " Invalid dynamic array values: "); - __print_i64(fd, i64(low)); + print_i64(fd, i64(low)); os.write_string(fd, ":"); - __print_i64(fd, i64(high)); + print_i64(fd, i64(high)); os.write_string(fd, ":"); - __print_i64(fd, i64(max)); + print_i64(fd, i64(max)); os.write_byte(fd, '\n'); debug_trap(); } @@ -298,16 +298,16 @@ type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column if ok do return; fd := os.stderr; - __print_caller_location(fd, Source_Code_Location{file, line, column, ""}); + print_caller_location(fd, Source_Code_Location{file, line, column, ""}); os.write_string(fd, " Invalid type assertion from "); - __print_typeid(fd, from); + print_typeid(fd, from); os.write_string(fd, " to "); - __print_typeid(fd, to); + print_typeid(fd, to); os.write_byte(fd, '\n'); debug_trap(); } -__string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { +string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) { return utf8.decode_rune_from_string(s); } @@ -328,9 +328,9 @@ make_slice_error_loc :: inline proc "contextless" (using loc := #caller_location if 0 <= len do return; fd := os.stderr; - __print_caller_location(fd, loc); + print_caller_location(fd, loc); os.write_string(fd, " Invalid slice length for make: "); - __print_i64(fd, i64(len)); + print_i64(fd, i64(len)); os.write_byte(fd, '\n'); debug_trap(); } @@ -339,11 +339,11 @@ make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_ if 0 <= len && len <= cap do return; fd := os.stderr; - __print_caller_location(fd, loc); + print_caller_location(fd, loc); os.write_string(fd, " Invalid dynamic array parameters for make: "); - __print_i64(fd, i64(len)); + print_i64(fd, i64(len)); os.write_byte(fd, ':'); - __print_i64(fd, i64(cap)); + print_i64(fd, i64(cap)); os.write_byte(fd, '\n'); debug_trap(); } @@ -352,9 +352,9 @@ make_map_expr_error_loc :: inline proc "contextless" (using loc := #caller_locat if 0 <= cap do return; fd := os.stderr; - __print_caller_location(fd, loc); + print_caller_location(fd, loc); os.write_string(fd, " Invalid map capacity for make: "); - __print_i64(fd, i64(cap)); + print_i64(fd, i64(cap)); os.write_byte(fd, '\n'); debug_trap(); } @@ -364,64 +364,101 @@ make_map_expr_error_loc :: inline proc "contextless" (using loc := #caller_locat @(default_calling_convention = "c") foreign { - @(link_name="llvm.sqrt.f32") __sqrt_f32 :: proc(x: f32) -> f32 --- - @(link_name="llvm.sqrt.f64") __sqrt_f64 :: proc(x: f64) -> f64 --- + @(link_name="llvm.sqrt.f32") sqrt_f32 :: proc(x: f32) -> f32 --- + @(link_name="llvm.sqrt.f64") sqrt_f64 :: proc(x: f64) -> f64 --- - @(link_name="llvm.sin.f32") __sin_f32 :: proc(θ: f32) -> f32 --- - @(link_name="llvm.sin.f64") __sin_f64 :: proc(θ: f64) -> f64 --- + @(link_name="llvm.sin.f32") sin_f32 :: proc(θ: f32) -> f32 --- + @(link_name="llvm.sin.f64") sin_f64 :: proc(θ: f64) -> f64 --- - @(link_name="llvm.cos.f32") __cos_f32 :: proc(θ: f32) -> f32 --- - @(link_name="llvm.cos.f64") __cos_f64 :: proc(θ: f64) -> f64 --- + @(link_name="llvm.cos.f32") cos_f32 :: proc(θ: f32) -> f32 --- + @(link_name="llvm.cos.f64") cos_f64 :: proc(θ: f64) -> f64 --- - @(link_name="llvm.pow.f32") __pow_f32 :: proc(x, power: f32) -> f32 --- - @(link_name="llvm.pow.f64") __pow_f64 :: proc(x, power: f64) -> f64 --- + @(link_name="llvm.pow.f32") pow_f32 :: proc(x, power: f32) -> f32 --- + @(link_name="llvm.pow.f64") pow_f64 :: proc(x, power: f64) -> f64 --- @(link_name="llvm.fmuladd.f32") fmuladd32 :: proc(a, b, c: f32) -> f32 --- @(link_name="llvm.fmuladd.f64") fmuladd64 :: proc(a, b, c: f64) -> f64 --- } -__abs_f32 :: inline proc "contextless" (x: f32) -> f32 { +abs_f32 :: inline proc "contextless" (x: f32) -> f32 { foreign { @(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 --- } return _abs(x); } -__abs_f64 :: inline proc "contextless" (x: f64) -> f64 { +abs_f64 :: inline proc "contextless" (x: f64) -> f64 { foreign { @(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 --- } return _abs(x); } -__min_f32 :: proc(a, b: f32) -> f32 { +min_f32 :: proc(a, b: f32) -> f32 { foreign { @(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 --- } return _min(a, b); } -__min_f64 :: proc(a, b: f64) -> f64 { +min_f64 :: proc(a, b: f64) -> f64 { foreign { @(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 --- } return _min(a, b); } -__max_f32 :: proc(a, b: f32) -> f32 { +max_f32 :: proc(a, b: f32) -> f32 { foreign { @(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 --- } return _max(a, b); } -__max_f64 :: proc(a, b: f64) -> f64 { +max_f64 :: proc(a, b: f64) -> f64 { foreign { @(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 --- } return _max(a, b); } -__abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 { +abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 { r, i := real(x), imag(x); - return __sqrt_f32(r*r + i*i); + return sqrt_f32(r*r + i*i); } -__abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 { +abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 { r, i := real(x), imag(x); - return __sqrt_f64(r*r + i*i); + return sqrt_f64(r*r + i*i); +} + + +quo_complex64 :: proc(n, m: complex64) -> complex64 { + e, f: f32; + + if abs(real(m)) >= abs(imag(m)) { + ratio := imag(m) / real(m); + denom := real(m) + ratio*imag(m); + e = (real(n) + imag(n)*ratio) / denom; + f = (imag(n) - real(n)*ratio) / denom; + } else { + ratio := real(m) / imag(m); + denom := imag(m) + ratio*real(m); + e = (real(n)*ratio + imag(n)) / denom; + f = (imag(n)*ratio - real(n)) / denom; + } + + return complex(e, f); +} + +quo_complex128 :: proc(n, m: complex128) -> complex128 { + e, f: f64; + + if abs(real(m)) >= abs(imag(m)) { + ratio := imag(m) / real(m); + denom := real(m) + ratio*imag(m); + e = (real(n) + imag(n)*ratio) / denom; + f = (imag(n) - real(n)*ratio) / denom; + } else { + ratio := real(m) / imag(m); + denom := imag(m) + ratio*real(m); + e = (real(n)*ratio + imag(n)) / denom; + f = (imag(n)*ratio - real(n)) / denom; + } + + return complex(e, f); } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 6af512c04..4f825b32f 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1607,25 +1607,25 @@ void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) { if (is_type_string(x->type) || is_type_string(y->type)) { switch (op) { - case Token_CmpEq: add_package_dependency(c, "runtime", "__string_eq"); break; - case Token_NotEq: add_package_dependency(c, "runtime", "__string_ne"); break; - case Token_Lt: add_package_dependency(c, "runtime", "__string_lt"); break; - case Token_Gt: add_package_dependency(c, "runtime", "__string_gt"); break; - case Token_LtEq: add_package_dependency(c, "runtime", "__string_le"); break; - case Token_GtEq: add_package_dependency(c, "runtime", "__string_gt"); break; + case Token_CmpEq: add_package_dependency(c, "runtime", "string_eq"); break; + case Token_NotEq: add_package_dependency(c, "runtime", "string_ne"); break; + case Token_Lt: add_package_dependency(c, "runtime", "string_lt"); break; + case Token_Gt: add_package_dependency(c, "runtime", "string_gt"); break; + case Token_LtEq: add_package_dependency(c, "runtime", "string_le"); break; + case Token_GtEq: add_package_dependency(c, "runtime", "string_gt"); break; } } else if (is_type_complex(x->type) || is_type_complex(y->type)) { switch (op) { case Token_CmpEq: switch (8*size) { - case 64: add_package_dependency(c, "runtime", "__complex64_eq"); break; - case 128: add_package_dependency(c, "runtime", "__complex128_eq"); break; + case 64: add_package_dependency(c, "runtime", "complex64_eq"); break; + case 128: add_package_dependency(c, "runtime", "complex128_eq"); break; } break; case Token_NotEq: switch (8*size) { - case 64: add_package_dependency(c, "runtime", "__complex64_ne"); break; - case 128: add_package_dependency(c, "runtime", "__complex128_ne"); break; + case 64: add_package_dependency(c, "runtime", "complex64_ne"); break; + case 128: add_package_dependency(c, "runtime", "complex128_ne"); break; } break; } @@ -1876,7 +1876,7 @@ bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y) { // cstring -> string if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) { if (operand->mode != Addressing_Constant) { - add_package_dependency(c, "runtime", "__cstring_to_string"); + add_package_dependency(c, "runtime", "cstring_to_string"); } return true; } @@ -2069,7 +2069,7 @@ void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, bool use_lhs_as Type *yt = base_type(y->type); check_assignment(c, x, yt->Map.key, str_lit("map 'in'")); - add_package_dependency(c, "runtime", "__dynamic_map_get"); + add_package_dependency(c, "runtime", "dynamic_map_get"); } else if (is_type_bit_set(y->type)) { Type *yt = base_type(y->type); check_assignment(c, x, yt->BitSet.elem, str_lit("bit_set 'in'")); @@ -2833,7 +2833,9 @@ Entity *check_selector(CheckerContext *c, Operand *operand, Ast *node, Type *typ if (operand->mode == Addressing_Immutable) { // Okay } else if (operand->mode == Addressing_Context) { - operand->mode = Addressing_Value; // TODO(bill): Should this be Value or Immutable? + if (sel.indirect) { + operand->mode = Addressing_Variable; + } } else if (operand->mode == Addressing_MapIndex) { operand->mode = Addressing_Value; } else if (sel.indirect || operand->mode != Addressing_Value) { @@ -2999,7 +3001,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 } else { mode = Addressing_Value; if (is_type_cstring(op_type)) { - add_package_dependency(c, "runtime", "__cstring_len"); + add_package_dependency(c, "runtime", "cstring_len"); } } } else if (is_type_array(op_type)) { @@ -3545,8 +3547,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 { Type *bt = base_type(operands[0].type); - if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "__min_f32"); - if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "__min_f64"); + if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "min_f32"); + if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "min_f64"); } } break; @@ -3648,8 +3650,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 { Type *bt = base_type(operands[0].type); - if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "__max_f32"); - if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "__max_f64"); + if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "max_f32"); + if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "max_f64"); } } break; @@ -3688,10 +3690,10 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 { Type *bt = base_type(operand->type); - if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "__abs_f32"); - if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "__abs_f64"); - if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "__abs_complex64"); - if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "__abs_complex128"); + if (are_types_identical(bt, t_f32)) add_package_dependency(c, "runtime", "abs_f32"); + if (are_types_identical(bt, t_f64)) add_package_dependency(c, "runtime", "abs_f64"); + if (are_types_identical(bt, t_complex64)) add_package_dependency(c, "runtime", "abs_complex64"); + if (are_types_identical(bt, t_complex128)) add_package_dependency(c, "runtime", "abs_complex128"); } } @@ -3790,12 +3792,12 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 { Type *bt = base_type(x.type); if (are_types_identical(bt, t_f32)) { - add_package_dependency(c, "runtime", "__min_f32"); - add_package_dependency(c, "runtime", "__max_f32"); + add_package_dependency(c, "runtime", "min_f32"); + add_package_dependency(c, "runtime", "max_f32"); } if (are_types_identical(bt, t_f64)) { - add_package_dependency(c, "runtime", "__min_f64"); - add_package_dependency(c, "runtime", "__max_f64"); + add_package_dependency(c, "runtime", "min_f64"); + add_package_dependency(c, "runtime", "max_f64"); } } } @@ -5515,8 +5517,8 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type context_name = str_lit("dynamic array literal"); is_constant = false; - add_package_dependency(c, "runtime", "__dynamic_array_reserve"); - add_package_dependency(c, "runtime", "__dynamic_array_append"); + add_package_dependency(c, "runtime", "dynamic_array_reserve"); + add_package_dependency(c, "runtime", "dynamic_array_append"); } else { GB_PANIC("unreachable"); } @@ -5675,8 +5677,8 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type } } - add_package_dependency(c, "runtime", "__dynamic_map_reserve"); - add_package_dependency(c, "runtime", "__dynamic_map_set"); + add_package_dependency(c, "runtime", "dynamic_map_reserve"); + add_package_dependency(c, "runtime", "dynamic_map_set"); break; } @@ -5957,8 +5959,8 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type o->type = t->Map.value; o->expr = node; - add_package_dependency(c, "runtime", "__dynamic_map_get"); - add_package_dependency(c, "runtime", "__dynamic_map_set"); + add_package_dependency(c, "runtime", "dynamic_map_get"); + add_package_dependency(c, "runtime", "dynamic_map_set"); return Expr_Expr; } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 722c73d0a..51b392de1 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1398,7 +1398,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { if (is_type_string(t) && t->Basic.kind != Basic_cstring) { val0 = t_rune; val1 = t_int; - add_package_dependency(ctx, "runtime", "__string_decode_rune"); + add_package_dependency(ctx, "runtime", "string_decode_rune"); } break; case Type_Array: diff --git a/src/checker.cpp b/src/checker.cpp index 6e8e9fadf..97f7bf4e8 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1318,6 +1318,9 @@ void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("Type_Info"), str_lit("Source_Code_Location"), str_lit("Context"), + + str_lit("quo_complex64"), + str_lit("quo_complex128"), }; for (isize i = 0; i < gb_count_of(required_runtime_entities); i++) { add_dependency_to_set(c, scope_lookup(c->info.runtime_package->scope, required_runtime_entities[i])); diff --git a/src/ir.cpp b/src/ir.cpp index ebeab9869..dbde6242f 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1304,7 +1304,7 @@ irValue *ir_add_module_constant(irModule *m, Type *type, ExactValue value) { isize max_len = 7+8+1; u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len); - isize len = gb_snprintf(cast(char *)str, max_len, "__csba$%x", m->global_array_index); + isize len = gb_snprintf(cast(char *)str, max_len, "csba$%x", m->global_array_index); m->global_array_index++; String name = make_string(str, len-1); @@ -1323,7 +1323,7 @@ irValue *ir_add_module_constant(irModule *m, Type *type, ExactValue value) { irValue *ir_add_global_string_array(irModule *m, String string) { isize max_len = 6+8+1; u8 *str = cast(u8 *)gb_alloc_array(ir_allocator(), u8, max_len); - isize len = gb_snprintf(cast(char *)str, max_len, "__str$%x", m->global_string_index); + isize len = gb_snprintf(cast(char *)str, max_len, "str$%x", m->global_string_index); m->global_string_index++; String name = make_string(str, len-1); @@ -1448,7 +1448,7 @@ irValue *ir_add_global_generated(irModule *m, Type *type, irValue *value) { isize max_len = 7+8+1; u8 *str = cast(u8 *)gb_alloc_array(ir_allocator(), u8, max_len); - isize len = gb_snprintf(cast(char *)str, max_len, "__ggv$%x", m->global_generated_index); + isize len = gb_snprintf(cast(char *)str, max_len, "ggv$%x", m->global_generated_index); m->global_generated_index++; String name = make_string(str, len-1); @@ -1944,7 +1944,7 @@ irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) { } else { auto args = array_make(ir_allocator(), 1); args[0] = str; - hashed_str = ir_emit_runtime_call(proc, "__default_hash_string", args); + hashed_str = ir_emit_runtime_call(proc, "default_hash_string", args); } ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), hashed_str); ir_emit_store(proc, ir_emit_struct_ep(proc, v, 1), str); @@ -2430,8 +2430,22 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue * if (is_type_complex(t_left)) { ir_emit_comment(proc, str_lit("complex.arith.begin")); + defer (ir_emit_comment(proc, str_lit("complex.arith.end"))); + Type *ft = base_complex_elem_type(t_left); + if (op == Token_Quo) { + auto args = array_make(heap_allocator(), 2); + args[0] = left; + args[1] = right; + + switch (type_size_of(ft)) { + case 4: return ir_emit_runtime_call(proc, "quo_complex64", args); + case 8: return ir_emit_runtime_call(proc, "quo_complex128", args); + default: GB_PANIC("Unknown float type"); break; + } + } + irValue *res = ir_add_local_generated(proc, type); irValue *a = ir_emit_struct_ev(proc, left, 0); irValue *b = ir_emit_struct_ev(proc, left, 1); @@ -2459,28 +2473,11 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue * imag = ir_emit_arith(proc, Token_Add, z, w, ft); break; } - case Token_Quo: { - irValue *s1 = ir_emit_arith(proc, Token_Mul, c, c, ft); - irValue *s2 = ir_emit_arith(proc, Token_Mul, d, d, ft); - irValue *s = ir_emit_arith(proc, Token_Add, s1, s2, ft); - - irValue *x = ir_emit_arith(proc, Token_Mul, a, c, ft); - irValue *y = ir_emit_arith(proc, Token_Mul, b, d, ft); - real = ir_emit_arith(proc, Token_Add, x, y, ft); - real = ir_emit_arith(proc, Token_Quo, real, s, ft); - - irValue *z = ir_emit_arith(proc, Token_Mul, b, c, ft); - irValue *w = ir_emit_arith(proc, Token_Mul, a, d, ft); - imag = ir_emit_arith(proc, Token_Sub, z, w, ft); - imag = ir_emit_arith(proc, Token_Quo, imag, s, ft); - break; - } } ir_emit_store(proc, ir_emit_struct_ep(proc, res, 0), real); ir_emit_store(proc, ir_emit_struct_ep(proc, res, 1), imag); - ir_emit_comment(proc, str_lit("complex.end.begin")); return ir_emit_load(proc, res); } @@ -2710,12 +2707,12 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal if (is_type_string(a)) { char *runtime_proc = nullptr; switch (op_kind) { - case Token_CmpEq: runtime_proc = "__string_eq"; break; - case Token_NotEq: runtime_proc = "__string_ne"; break; - case Token_Lt: runtime_proc = "__string_lt"; break; - case Token_Gt: runtime_proc = "__string_gt"; break; - case Token_LtEq: runtime_proc = "__string_le"; break; - case Token_GtEq: runtime_proc = "__string_gt"; break; + case Token_CmpEq: runtime_proc = "string_eq"; break; + case Token_NotEq: runtime_proc = "string_ne"; break; + case Token_Lt: runtime_proc = "string_lt"; break; + case Token_Gt: runtime_proc = "string_gt"; break; + case Token_LtEq: runtime_proc = "string_le"; break; + case Token_GtEq: runtime_proc = "string_gt"; break; } GB_ASSERT(runtime_proc != nullptr); @@ -2731,14 +2728,14 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal switch (sz) { case 64: switch (op_kind) { - case Token_CmpEq: runtime_proc = "__complex64_eq"; break; - case Token_NotEq: runtime_proc = "__complex64_ne"; break; + case Token_CmpEq: runtime_proc = "complex64_eq"; break; + case Token_NotEq: runtime_proc = "complex64_ne"; break; } break; case 128: switch (op_kind) { - case Token_CmpEq: runtime_proc = "__complex128_eq"; break; - case Token_NotEq: runtime_proc = "__complex128_ne"; break; + case Token_CmpEq: runtime_proc = "complex128_eq"; break; + case Token_NotEq: runtime_proc = "complex128_ne"; break; } break; } @@ -3068,7 +3065,7 @@ irValue *ir_cstring_len(irProcedure *proc, irValue *value) { GB_ASSERT(is_type_cstring(ir_type(value))); auto args = array_make(ir_allocator(), 1); args[0] = ir_emit_conv(proc, value, t_cstring); - return ir_emit_runtime_call(proc, "__cstring_len", args); + return ir_emit_runtime_call(proc, "cstring_len", args); } @@ -3313,7 +3310,7 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) { irValue *c = ir_emit_conv(proc, value, t_cstring); auto args = array_make(ir_allocator(), 1); args[0] = c; - irValue *s = ir_emit_runtime_call(proc, "__cstring_to_string", args); + irValue *s = ir_emit_runtime_call(proc, "cstring_to_string", args); return ir_emit_conv(proc, s, dst); } @@ -3334,13 +3331,13 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) { // case 4: { // auto args = array_make(ir_allocator(), 1); // args[0] = value; - // return ir_emit_runtime_call(proc, "__gnu_h2f_ieee", args); + // return ir_emit_runtime_call(proc, "gnu_h2f_ieee", args); // break; // } // case 8: { // auto args = array_make(ir_allocator(), 1); // args[0] = value; - // return ir_emit_runtime_call(proc, "__f16_to_f64", args); + // return ir_emit_runtime_call(proc, "f16_to_f64", args); // break; // } // } @@ -3350,13 +3347,13 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) { // case 4: { // auto args = array_make(ir_allocator(), 1); // args[0] = value; - // return ir_emit_runtime_call(proc, "__gnu_f2h_ieee", args); + // return ir_emit_runtime_call(proc, "gnu_f2h_ieee", args); // break; // } // case 8: { // auto args = array_make(ir_allocator(), 1); // args[0] = value; - // return ir_emit_runtime_call(proc, "__truncdfhf2", args); + // return ir_emit_runtime_call(proc, "truncdfhf2", args); // break; // } // } @@ -4306,8 +4303,8 @@ irValue *ir_emit_min(irProcedure *proc, Type *t, irValue *x, irValue *y) { args[0] = x; args[1] = y; switch (sz) { - case 32: return ir_emit_runtime_call(proc, "__min_f32", args); - case 64: return ir_emit_runtime_call(proc, "__min_f64", args); + case 32: return ir_emit_runtime_call(proc, "min_f32", args); + case 64: return ir_emit_runtime_call(proc, "min_f64", args); } GB_PANIC("Unknown float type"); } @@ -4324,8 +4321,8 @@ irValue *ir_emit_max(irProcedure *proc, Type *t, irValue *x, irValue *y) { args[0] = x; args[1] = y; switch (sz) { - case 32: return ir_emit_runtime_call(proc, "__max_f32", args); - case 64: return ir_emit_runtime_call(proc, "__max_f64", args); + case 32: return ir_emit_runtime_call(proc, "max_f32", args); + case 64: return ir_emit_runtime_call(proc, "max_f64", args); } GB_PANIC("Unknown float type"); } @@ -4676,8 +4673,8 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu auto args = array_make(ir_allocator(), 1); args[0] = x; switch (sz) { - case 64: return ir_emit_runtime_call(proc, "__abs_complex64", args); - case 128: return ir_emit_runtime_call(proc, "__abs_complex128", args); + case 64: return ir_emit_runtime_call(proc, "abs_complex64", args); + case 128: return ir_emit_runtime_call(proc, "abs_complex128", args); } GB_PANIC("Unknown complex type"); } else if (is_type_float(t)) { @@ -4685,8 +4682,8 @@ irValue *ir_build_builtin_proc(irProcedure *proc, Ast *expr, TypeAndValue tv, Bu auto args = array_make(ir_allocator(), 1); args[0] = x; switch (sz) { - case 32: return ir_emit_runtime_call(proc, "__abs_f32", args); - case 64: return ir_emit_runtime_call(proc, "__abs_f64", args); + case 32: return ir_emit_runtime_call(proc, "abs_f32", args); + case 64: return ir_emit_runtime_call(proc, "abs_f64", args); } GB_PANIC("Unknown float type"); } @@ -5903,7 +5900,7 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) { } i64 item_count = cl->elems.count; - irValue *items = ir_generate_array(proc->module, elem, item_count, str_lit("__dacl$"), cast(i64)cast(intptr)expr); + irValue *items = ir_generate_array(proc->module, elem, item_count, str_lit("dacl$"), cast(i64)cast(intptr)expr); for_array(field_index, cl->elems) { Ast *f = cl->elems[field_index]; @@ -6480,7 +6477,7 @@ void ir_build_range_string(irProcedure *proc, irValue *expr, Type *val_type, irValue *str_len = ir_emit_arith(proc, Token_Sub, count, offset, t_int); auto args = array_make(ir_allocator(), 1); args[0] = ir_emit_string(proc, str_elem, str_len); - irValue *rune_and_len = ir_emit_runtime_call(proc, "__string_decode_rune", args); + irValue *rune_and_len = ir_emit_runtime_call(proc, "string_decode_rune", args); irValue *len = ir_emit_struct_ev(proc, rune_and_len, 1); ir_emit_store(proc, offset_, ir_emit_arith(proc, Token_Add, offset, len, t_int)); @@ -8075,9 +8072,9 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info if (t->Enum.fields.count > 0) { auto fields = t->Enum.fields; irValue *name_array = ir_generate_array(m, t_string, fields.count, - str_lit("__$enum_names"), cast(i64)entry_index); + str_lit("$enum_names"), cast(i64)entry_index); irValue *value_array = ir_generate_array(m, t_type_info_enum_value, fields.count, - str_lit("__$enum_values"), cast(i64)entry_index); + str_lit("$enum_values"), cast(i64)entry_index); GB_ASSERT(is_type_integer(t->Enum.base_type)); @@ -8223,9 +8220,9 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info isize count = t->BitField.fields.count; if (count > 0) { auto fields = t->BitField.fields; - irValue *name_array = ir_generate_array(m, t_string, count, str_lit("__$bit_field_names"), cast(i64)entry_index); - irValue *bit_array = ir_generate_array(m, t_i32, count, str_lit("__$bit_field_bits"), cast(i64)entry_index); - irValue *offset_array = ir_generate_array(m, t_i32, count, str_lit("__$bit_field_offsets"), cast(i64)entry_index); + irValue *name_array = ir_generate_array(m, t_string, count, str_lit("$bit_field_names"), cast(i64)entry_index); + irValue *bit_array = ir_generate_array(m, t_i32, count, str_lit("$bit_field_bits"), cast(i64)entry_index); + irValue *offset_array = ir_generate_array(m, t_i32, count, str_lit("$bit_field_offsets"), cast(i64)entry_index); for (isize i = 0; i < count; i++) { Entity *f = fields[i];