Compare commits

...

24 Commits

Author SHA1 Message Date
Ed_
bd9d2b3a7b misc 2025-11-08 10:42:04 -05:00
Ed_
f5330c686b llm refinement attempt 2025-11-07 14:53:45 -05:00
Ed_
a48681fc00 preparing for curation and runtime testing 2025-11-07 14:32:04 -05:00
Ed_
acb5e916c1 missing changes (llm) 2025-11-07 13:56:15 -05:00
Ed_
5a44788b4a WIP(compiles, untested, to review): Another attempt at using llms to codegen very tedius stuff. 2025-11-07 13:51:12 -05:00
Ed_
dbb1367acb oops 2 2025-11-06 19:39:33 -05:00
Ed_
aab3a3f689 oops 2025-11-06 19:25:28 -05:00
Ed_
d7790795dd stuff 2025-11-06 19:23:58 -05:00
Ed_
ac05262c8d finished arena (lottes.c) 2025-11-05 22:21:44 -05:00
Ed_
3bb46692e1 Update lottes hybrid 2025-11-05 20:43:42 -05:00
Ed_
a7d17a8b70 changes to watl.v0.msvc.c
Made everything internal linkage.
Moved memory operations impl to header section (keeping same loc as I have for the lottes variants).
arena__grow && arena__shirnk lifted to definitions.
2025-11-05 20:00:41 -05:00
Ed_
7aaf617b3c progress on lottes.c 2025-11-05 00:17:38 -05:00
Ed_
5e3e8970d8 get rid of register qualifier 2025-11-04 21:35:58 -05:00
Ed_
8269ea9cc5 added debug early to start testing 2025-11-04 21:23:18 -05:00
Ed_
d9bce18ccb progress on strict lottes version 2025-11-04 21:18:59 -05:00
Ed_
3554615244 compile fixes (lottes hybrid and msvc) 2025-11-04 16:20:05 -05:00
Ed_
aad7b59179 WIP(untesed, not-compiled): Still curating, Ai code was terrible, scrapping most of it. 2025-11-04 15:17:11 -05:00
Ed_
9179f77f05 WIP(untesed, not-compiled): Curating varena code for pure lottes c-- variant, misc changes 2025-11-04 14:21:13 -05:00
Ed_
816ed5debd watl.v0.llvm.lottes.c: Attempted to let Codex LLM do OS and VArena sections via sampling manual prior code.
This is to try to replicate Sebastian Aaltonen "leaf codegen" experience with llms.
I'll have to review/curate it when I get the chance and then correct deviations from the convention.
2025-11-04 02:40:15 -05:00
Ed_
81328819c6 watl.v0.llvm.lottes.c: Did FArena 2025-11-04 02:07:38 -05:00
Ed_
f437be32e2 WIP: watl.v0.llvm.lottes.c fleshing out 2025-11-03 23:14:40 -05:00
Ed_
2ff49a188d Add LICENSE
Added a license file for public domain software.
2025-11-01 12:02:30 -04:00
Ed_
18274f5785 hash64_djb8 -> hash64_fnv1a; kt1l -> ktl and made the populate procedure only for strings + misc changes
For this exercise that doens't need to be generic, kt1cx does the job.
2025-11-01 01:38:37 -04:00
Ed_
9e4bc141d0 Remove reundeant cell pool slice in kt1cx impl across c and odin versions
wasn't being used, don't plan on using it.
2025-10-12 00:39:06 -04:00
14 changed files with 4279 additions and 1427 deletions

View File

@@ -1,2 +0,0 @@
#pragma once

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

24
LICENSE Normal file
View File

@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>

View File

@@ -0,0 +1,61 @@
package watl
import "core:os/os2"
import "core:mem/virtual"
import "core:mem"
main :: proc()
{
os_init()
// Note(Ed): Possible compiler bug, cannot resolve proc map with named arguments.
vm_file: virtual.Arena; virtual.arena_init_static(& vm_file, reserved = mem.Gigabytes * 4)
data, err := os2.read_entire_file_from_path("watl.v0.ideomatic.odin", virtual.arena_allocator(& vm_file), )
assert(err != .None)
a_msgs := arena_make()
a_toks := arena_make()
// lex_res := watl_lex(transmute(string) file.content,
// ainfo_msgs = ainfo(a_msgs),
// ainfo_toks = ainfo(a_toks),
// )
lex_res := watl_lex(transmute(string) file.content,
ainfo(a_msgs),
ainfo(a_toks),
)
assert(lex_res.signal & { .MemFail_SliceConstraintFail } == {})
str8_cache_kt1_ainfo := arena_make()
str_cache := str8cache_make(
str_reserve = ainfo(arena_make()),
cell_reserve = ainfo(str8_cache_kt1_ainfo),
tbl_backing = ainfo(str8_cache_kt1_ainfo),
cell_pool_size = Kilo * 4,
table_size = Kilo * 32,
)
a_lines := arena_make()
// parse_res := watl_parse(lex_res.toks,
// ainfo_msgs = ainfo(a_msgs),
// ainfo_nodes = ainfo(a_toks),
// ainfo_lines = ainfo(a_lines),
// str_cache = & str_cache
// )
parse_res := watl_parse(lex_res.toks,
ainfo(a_msgs),
ainfo(a_toks),
ainfo(a_lines),
& str_cache
)
assert(parse_res.signal & { .MemFail_SliceConstraintFail } == {})
arena_reset(a_msgs)
arena_reset(a_toks)
listing := watl_dump_listing(ainfo(a_msgs), parse_res.lines)
file_write_str8("watl.v0.win32.odin.listing.txt", listing)
return
}

View File

@@ -95,28 +95,19 @@ Tera :: Giga * 1024
ptr_cursor :: #force_inline proc "contextless" (ptr: ^$Type) -> [^]Type { return transmute([^]Type) ptr } ptr_cursor :: #force_inline proc "contextless" (ptr: ^$Type) -> [^]Type { return transmute([^]Type) ptr }
align_pow2 :: proc(x: int, b: int) -> int { align_pow2 :: #force_inline proc(x: int, b: int) -> int {
assert(b != 0) assert(b != 0)
assert((b & (b - 1)) == 0) // Check power of 2 assert((b & (b - 1)) == 0) // Check power of 2
return ((x + b - 1) & ~(b - 1)) return ((x + b - 1) & ~(b - 1))
} }
memory_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr { memory_zero :: #force_inline proc "contextless" (data: rawptr, len: int) -> rawptr { intrinsics.mem_zero(data, len); return data }
intrinsics.mem_zero(data, len) memory_zero_explicit :: #force_inline proc "contextless" (data: rawptr, len: int) -> rawptr {
return data
}
memory_zero_explicit :: proc "contextless" (data: rawptr, len: int) -> rawptr {
intrinsics.mem_zero_volatile(data, len) // Use the volatile mem_zero intrinsics.mem_zero_volatile(data, len) // Use the volatile mem_zero
intrinsics.atomic_thread_fence(.Seq_Cst) // Prevent reordering intrinsics.atomic_thread_fence(.Seq_Cst) // Prevent reordering
return data return data
} }
memory_copy_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr { memory_copy_overlapping :: #force_inline proc "contextless" (dst, src: rawptr, len: int) -> rawptr { intrinsics.mem_copy(dst, src, len); return dst }
intrinsics.mem_copy(dst, src, len) memory_copy :: #force_inline proc "contextless" (dst, src: rawptr, len: int) -> rawptr { intrinsics.mem_copy_non_overlapping(dst, src, len); return dst }
return dst
}
memory_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
intrinsics.mem_copy_non_overlapping(dst, src, len)
return dst
}
sll_stack_push_n :: proc "contextless" (curr, n, n_link: ^^$Type) { sll_stack_push_n :: proc "contextless" (curr, n, n_link: ^^$Type) {
(n_link ^) = (curr ^) (n_link ^) = (curr ^)
@@ -136,34 +127,26 @@ sll_queue_push_nz :: proc "contextless" (first: ^$ParentType, last, n: ^^$Type,
} }
sll_queue_push_n :: #force_inline proc "contextless" (first: $ParentType, last, n: ^^$Type) { sll_queue_push_nz(first, last, n, nil) } sll_queue_push_n :: #force_inline proc "contextless" (first: $ParentType, last, n: ^^$Type) { sll_queue_push_nz(first, last, n, nil) }
SliceByte :: struct { SliceByte :: struct { data: [^]byte, len: int }
data: [^]byte, SliceRaw :: struct($Type: typeid) { data: [^]Type, len: int, }
len: int
}
SliceRaw :: struct ($Type: typeid) {
data: [^]Type,
len: int,
}
slice :: #force_inline proc "contextless" (s: [^] $Type, num: $Some_Integer) -> [ ]Type { return transmute([]Type) SliceRaw(Type) { s, cast(int) num } } slice :: #force_inline proc "contextless" (s: [^] $Type, num: $Some_Integer) -> [ ]Type { return transmute([]Type) SliceRaw(Type) { s, cast(int) num } }
slice_cursor :: #force_inline proc "contextless" (s: []$Type) -> [^]Type { return transmute([^]Type) raw_data(s) } slice_cursor :: #force_inline proc "contextless" (s: []$Type) -> [^]Type { return transmute([^]Type) raw_data(s) }
slice_assert :: #force_inline proc (s: $SliceType / []$Type) { slice_end :: #force_inline proc "contextless" (s : $SliceType / []$Type) -> ^Type { return & cursor(s)[len(s)] }
assert(len(s) > 0)
assert(s != nil) slice_assert :: #force_inline proc (s: $SliceType / []$Type) { assert(len(s) > 0); assert(s != nil) }
}
slice_end :: #force_inline proc "contextless" (s : $SliceType / []$Type) -> ^Type { return & cursor(s)[len(s)] }
@(require_results) slice_to_bytes :: proc "contextless" (s: []$Type) -> []byte { return ([^]byte)(raw_data(s))[:len(s) * size_of(Type)] } @(require_results) slice_to_bytes :: proc "contextless" (s: []$Type) -> []byte { return ([^]byte)(raw_data(s))[:len(s) * size_of(Type)] }
@(require_results) slice_raw :: proc "contextless" (s: []$Type) -> SliceRaw(Type) { return transmute(SliceRaw(Type)) s } @(require_results) slice_raw :: proc "contextless" (s: []$Type) -> SliceRaw(Type) { return transmute(SliceRaw(Type)) s }
slice_zero :: proc "contextless" (data: $SliceType / []$Type) { memory_zero(raw_data(data), size_of(Type) * len(data)) } slice_zero :: #force_inline proc "contextless" (data: $SliceType / []$Type) { memory_zero(raw_data(data), size_of(Type) * len(data)) }
slice_copy :: proc "contextless" (dst, src: $SliceType / []$Type) -> int { slice_copy :: #force_inline proc "contextless" (dst, src: $SliceType / []$Type) -> int {
n := max(0, min(len(dst), len(src))) n := max(0, min(len(dst), len(src)))
if n > 0 { if n > 0 {
intrinsics.mem_copy_non_overlapping(raw_data(dst), raw_data(src), n * size_of(Type)) intrinsics.mem_copy_non_overlapping(raw_data(dst), raw_data(src), n * size_of(Type))
} }
return n return n
} }
slice_copy_overlapping :: proc "contextless" (dst, src: $SliceType / []$Type) -> int { slice_copy_overlapping :: #force_inline proc "contextless" (dst, src: $SliceType / []$Type) -> int {
n := max(0, min(len(dst), len(src))) n := max(0, min(len(dst), len(src)))
if n > 0 { if n > 0 {
intrinsics.mem_copy(raw_data(dst), raw_data(src), n * size_of(Type)) intrinsics.mem_copy(raw_data(dst), raw_data(src), n * size_of(Type))
@@ -270,11 +253,10 @@ mem_alloc :: proc(ainfo: AllocatorInfo, size: int, alignment: int = MEMORY_ALIGN
requested_size = size, requested_size = size,
alignment = alignment, alignment = alignment,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output)
return output.allocation return output.allocation
} }
mem_grow :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []byte { mem_grow :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false, give_actual: b32 = false) -> []byte {
assert(ainfo.procedure != nil) assert(ainfo.procedure != nil)
input := AllocatorProc_In { input := AllocatorProc_In {
data = ainfo.data, data = ainfo.data,
@@ -283,11 +265,10 @@ mem_grow :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int =
alignment = alignment, alignment = alignment,
old_allocation = mem, old_allocation = mem,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output) return slice(cursor(output.allocation), give_actual ? len(output.allocation) : size)
return output.allocation
} }
mem_resize :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []byte { mem_resize :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false, give_actual: b32 = false) -> []byte {
assert(ainfo.procedure != nil) assert(ainfo.procedure != nil)
input := AllocatorProc_In { input := AllocatorProc_In {
data = ainfo.data, data = ainfo.data,
@@ -296,9 +277,8 @@ mem_resize :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int
alignment = alignment, alignment = alignment,
old_allocation = mem, old_allocation = mem,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output) return slice(cursor(output.allocation), give_actual ? len(output.allocation) : size)
return output.allocation
} }
mem_shrink :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []byte { mem_shrink :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []byte {
assert(ainfo.procedure != nil) assert(ainfo.procedure != nil)
@@ -309,8 +289,7 @@ mem_shrink :: proc(ainfo: AllocatorInfo, mem: []byte, size: int, alignment: int
alignment = alignment, alignment = alignment,
old_allocation = mem, old_allocation = mem,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output)
return output.allocation return output.allocation
} }
@@ -322,8 +301,7 @@ alloc_type :: proc(ainfo: AllocatorInfo, $Type: typeid, alignment: int = MEMORY
requested_size = size_of(Type), requested_size = size_of(Type),
alignment = alignment, alignment = alignment,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output)
return transmute(^Type) raw_data(output.allocation) return transmute(^Type) raw_data(output.allocation)
} }
alloc_slice :: proc(ainfo: AllocatorInfo, $SliceType: typeid / []$Type, num : int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []Type { alloc_slice :: proc(ainfo: AllocatorInfo, $SliceType: typeid / []$Type, num : int, alignment: int = MEMORY_ALIGNMENT_DEFAULT, no_zero: b32 = false) -> []Type {
@@ -334,17 +312,13 @@ alloc_slice :: proc(ainfo: AllocatorInfo, $SliceType: typeid / []$Type, num : in
requested_size = size_of(Type) * num, requested_size = size_of(Type) * num,
alignment = alignment, alignment = alignment,
} }
output: AllocatorProc_Out output: AllocatorProc_Out; ainfo.procedure(input, & output)
ainfo.procedure(input, & output)
return transmute([]Type) slice(raw_data(output.allocation), num) return transmute([]Type) slice(raw_data(output.allocation), num)
} }
//endregion Allocator Interface //endregion Allocator Interface
//region Strings //region Strings
Raw_String :: struct { Raw_String :: struct { data: [^]byte, len: int, }
data: [^]byte,
len: int,
}
string_cursor :: proc(s: string) -> [^]u8 { return slice_cursor(transmute([]byte) s) } string_cursor :: proc(s: string) -> [^]u8 { return slice_cursor(transmute([]byte) s) }
string_copy :: proc(dst, src: string) { slice_copy (transmute([]byte) dst, transmute([]byte) src) } string_copy :: proc(dst, src: string) { slice_copy (transmute([]byte) dst, transmute([]byte) src) }
string_end :: proc(s: string) -> ^u8 { return slice_end (transmute([]byte) s) } string_end :: proc(s: string) -> ^u8 { return slice_end (transmute([]byte) s) }
@@ -356,10 +330,7 @@ FArena :: struct {
mem: []byte, mem: []byte,
used: int, used: int,
} }
farena_make :: proc(backing: []byte) -> FArena { farena_make :: proc(backing: []byte) -> FArena { return {mem = backing} }
arena := FArena {mem = backing}
return arena
}
farena_init :: proc(arena: ^FArena, backing: []byte) { farena_init :: proc(arena: ^FArena, backing: []byte) {
assert(arena != nil) assert(arena != nil)
arena.mem = backing arena.mem = backing
@@ -367,20 +338,15 @@ farena_init :: proc(arena: ^FArena, backing: []byte) {
} }
farena_push :: proc(arena: ^FArena, $Type: typeid, amount: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT) -> []Type { farena_push :: proc(arena: ^FArena, $Type: typeid, amount: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT) -> []Type {
assert(arena != nil) assert(arena != nil)
if amount == 0 { if amount == 0 { return {} }
return {}
}
desired := size_of(Type) * amount desired := size_of(Type) * amount
to_commit := align_pow2(desired, alignment) to_commit := align_pow2(desired, alignment)
unused := len(arena.mem) - arena.used unused := len(arena.mem) - arena.used; assert(to_commit <= unused)
assert(to_commit <= unused)
ptr := cursor(arena.mem[arena.used:]) ptr := cursor(arena.mem[arena.used:])
arena.used += to_commit arena.used += to_commit
return slice(ptr, amount) return slice(ptr, amount)
} }
farena_reset :: proc(arena: ^FArena) { farena_reset :: #force_inline proc(arena: ^FArena) { arena.used = 0 }
arena.used = 0
}
farena_rewind :: proc(arena: ^FArena, save_point: AllocatorSP) { farena_rewind :: proc(arena: ^FArena, save_point: AllocatorSP) {
assert(save_point.type_sig == farena_allocator_proc) assert(save_point.type_sig == farena_allocator_proc)
assert(save_point.slot >= 0 && save_point.slot <= arena.used) assert(save_point.slot >= 0 && save_point.slot <= arena.used)
@@ -391,7 +357,6 @@ farena_allocator_proc :: proc(input: AllocatorProc_In, output: ^AllocatorProc_Ou
assert(output != nil) assert(output != nil)
assert(input.data != nil) assert(input.data != nil)
arena := transmute(^FArena) input.data arena := transmute(^FArena) input.data
switch input.op switch input.op
{ {
case .Alloc, .Alloc_NoZero: case .Alloc, .Alloc_NoZero:
@@ -399,12 +364,9 @@ farena_allocator_proc :: proc(input: AllocatorProc_In, output: ^AllocatorProc_Ou
if input.op == .Alloc { if input.op == .Alloc {
zero(output.allocation) zero(output.allocation)
} }
case .Free: case .Free: // No-op for arena
// No-op for arena case .Reset: farena_reset(arena)
case .Reset:
farena_reset(arena)
case .Grow, .Grow_NoZero: case .Grow, .Grow_NoZero:
// Check if the allocation is at the end of the arena // Check if the allocation is at the end of the arena
@@ -453,11 +415,8 @@ farena_allocator_proc :: proc(input: AllocatorProc_In, output: ^AllocatorProc_Ou
arena.used -= (aligned_original - aligned_new) arena.used -= (aligned_original - aligned_new)
output.allocation = input.old_allocation[:input.requested_size] output.allocation = input.old_allocation[:input.requested_size]
case .Rewind: case .Rewind: farena_rewind(arena, input.save_point)
farena_rewind(arena, input.save_point) case .SavePoint: output.save_point = farena_save(arena^)
case .SavePoint:
output.save_point = farena_save(arena^)
case .Query: case .Query:
output.features = {.Alloc, .Reset, .Grow, .Shrink, .Rewind} output.features = {.Alloc, .Reset, .Grow, .Shrink, .Rewind}
@@ -471,14 +430,9 @@ farena_ainfo :: #force_inline proc "contextless" (arena : ^FArena) -> AllocatorI
//endregion FArena //endregion FArena
//region OS //region OS
OS_SystemInfo :: struct { OS_SystemInfo :: struct { target_page_size: int }
target_page_size: int, OS_Windows_State :: struct { system_info: OS_SystemInfo }
} @(private) os_windows_info: OS_Windows_State
OS_Windows_State :: struct {
system_info: OS_SystemInfo,
}
@(private)
os_windows_info: OS_Windows_State
// Windows API constants // Windows API constants
MS_INVALID_HANDLE_VALUE :: ~uintptr(0) MS_INVALID_HANDLE_VALUE :: ~uintptr(0)
@@ -537,12 +491,7 @@ os_enable_large_pages :: proc() {
{ {
priv := MS_TOKEN_PRIVILEGES { priv := MS_TOKEN_PRIVILEGES {
privilege_count = 1, privilege_count = 1,
privileges = { privileges = { { luid = luid, attributes = MS_SE_PRIVILEGE_ENABLED, }, },
{
luid = luid,
attributes = MS_SE_PRIVILEGE_ENABLED,
},
},
} }
AdjustTokenPrivileges(token, 0, &priv, size_of(MS_TOKEN_PRIVILEGES), nil, nil) AdjustTokenPrivileges(token, 0, &priv, size_of(MS_TOKEN_PRIVILEGES), nil, nil)
} }
@@ -554,25 +503,19 @@ os_init :: proc() {
info := &os_windows_info.system_info info := &os_windows_info.system_info
info.target_page_size = int(GetLargePageMinimum()) info.target_page_size = int(GetLargePageMinimum())
} }
os_system_info :: proc() -> ^OS_SystemInfo { os_system_info :: #force_inline proc "contextless" () -> ^OS_SystemInfo { return & os_windows_info.system_info }
return &os_windows_info.system_info os_vmem_commit :: #force_inline proc "contextless" (vm: rawptr, size: int, no_large_pages: b32 = false) -> b32 {
}
os_vmem_commit :: proc(vm: rawptr, size: int, no_large_pages: b32 = false) -> b32 {
// Large pages disabled for now (not failing gracefully in original C) // Large pages disabled for now (not failing gracefully in original C)
result := VirtualAlloc(vm, uintptr(size), MS_MEM_COMMIT, MS_PAGE_READWRITE) != nil return cast(b32) VirtualAlloc(vm, uintptr(size), MS_MEM_COMMIT, MS_PAGE_READWRITE) != nil
return b32(result)
} }
os_vmem_reserve :: proc(size: int, base_addr: int = 0, no_large_pages: b32 = false) -> rawptr { os_vmem_reserve :: #force_inline proc "contextless" (size: int, base_addr: int = 0, no_large_pages: b32 = false) -> rawptr {
result := VirtualAlloc(rawptr(uintptr(base_addr)), uintptr(size), return VirtualAlloc(rawptr(uintptr(base_addr)), uintptr(size),
MS_MEM_RESERVE, MS_MEM_RESERVE,
// MS_MEM_COMMIT // MS_MEM_COMMIT
// | (no_large_pages ? 0 : MS_MEM_LARGE_PAGES), // Large pages disabled // | (no_large_pages ? 0 : MS_MEM_LARGE_PAGES), // Large pages disabled
MS_PAGE_READWRITE) MS_PAGE_READWRITE)
return result
}
os_vmem_release :: proc(vm: rawptr, size: int) {
VirtualFree(vm, 0, MS_MEM_RELEASE)
} }
os_vmem_release :: #force_inline proc "contextless" (vm: rawptr, size: int) { VirtualFree(vm, 0, MS_MEM_RELEASE) }
//endregion OS //endregion OS
//region VArena //region VArena
@@ -646,17 +589,6 @@ varena_push :: proc(va: ^VArena, $Type: typeid, amount: int, alignment: int = ME
va.commit_used = to_be_used va.commit_used = to_be_used
return slice(transmute([^]Type) uintptr(current_offset), amount) return slice(transmute([^]Type) uintptr(current_offset), amount)
} }
varena_release :: proc(va: ^VArena) {
os_vmem_release(va, va.reserve)
}
varena_rewind :: proc(va: ^VArena, save_point: AllocatorSP) {
assert(va != nil)
assert(save_point.type_sig == varena_allocator_proc)
va.commit_used = max(save_point.slot, size_of(VArena))
}
varena_reset :: proc(va: ^VArena) {
va.commit_used = size_of(VArena)
}
varena_shrink :: proc(va: ^VArena, old_allocation: []byte, requested_size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT) -> []byte { varena_shrink :: proc(va: ^VArena, old_allocation: []byte, requested_size: int, alignment: int = MEMORY_ALIGNMENT_DEFAULT) -> []byte {
assert(va != nil) assert(va != nil)
current_offset := va.reserve_start + va.commit_used current_offset := va.reserve_start + va.commit_used
@@ -668,6 +600,13 @@ varena_shrink :: proc(va: ^VArena, old_allocation: []byte, requested_size: int,
va.commit_used -= shrink_amount va.commit_used -= shrink_amount
return old_allocation[:requested_size] return old_allocation[:requested_size]
} }
varena_release :: #force_inline proc(va: ^VArena) { os_vmem_release(va, va.reserve) }
varena_reset :: #force_inline proc(va: ^VArena) { va.commit_used = size_of(VArena) }
varena_rewind :: #force_inline proc(va: ^VArena, save_point: AllocatorSP) {
assert(va != nil)
assert(save_point.type_sig == varena_allocator_proc)
va.commit_used = max(save_point.slot, size_of(VArena))
}
varena_save :: #force_inline proc "contextless" (va: ^VArena) -> AllocatorSP { return AllocatorSP { type_sig = varena_allocator_proc, slot = va.commit_used } } varena_save :: #force_inline proc "contextless" (va: ^VArena) -> AllocatorSP { return AllocatorSP { type_sig = varena_allocator_proc, slot = va.commit_used } }
varena_allocator_proc :: proc(input: AllocatorProc_In, output: ^AllocatorProc_Out) { varena_allocator_proc :: proc(input: AllocatorProc_In, output: ^AllocatorProc_Out) {
assert(output != nil) assert(output != nil)
@@ -785,7 +724,7 @@ arena_push :: proc(arena: ^Arena, $Type: typeid, amount: int, alignment: int = M
active.pos = pos_pst active.pos = pos_pst
return slice(result_ptr, amount) return slice(result_ptr, amount)
} }
arena_release :: proc(arena: ^Arena) { arena_release :: #force_inline proc(arena: ^Arena) {
assert(arena != nil) assert(arena != nil)
curr := arena.current curr := arena.current
for curr != nil { for curr != nil {
@@ -794,9 +733,7 @@ arena_release :: proc(arena: ^Arena) {
curr = prev curr = prev
} }
} }
arena_reset :: proc(arena: ^Arena) { arena_reset :: #force_inline proc(arena: ^Arena) { arena_rewind(arena, AllocatorSP { type_sig = arena_allocator_proc, slot = 0 }) }
arena_rewind(arena, AllocatorSP { type_sig = arena_allocator_proc, slot = 0 })
}
arena_rewind :: proc(arena: ^Arena, save_point: AllocatorSP) { arena_rewind :: proc(arena: ^Arena, save_point: AllocatorSP) {
assert(arena != nil) assert(arena != nil)
assert(save_point.type_sig == arena_allocator_proc) assert(save_point.type_sig == arena_allocator_proc)
@@ -913,54 +850,33 @@ arena_ainfo :: #force_inline proc "contextless" (arena : ^Arena) -> AllocatorInf
//endregion Arena (Casey-Ryan Composite Arena) //endregion Arena (Casey-Ryan Composite Arena)
//region Hashing //region Hashing
hash64_djb8 :: proc(hash: ^u64, bytes: []byte) { // Ripped from core:hash, fnv64a
for elem in bytes { @(optimization_mode="favor_size")
hash^ = ((hash^ << 8) + hash^) + u64(elem) hash64_fnv1a :: #force_inline proc "contextless" (hash: ^u64, data: []byte, seed := u64(0xcbf29ce484222325)) {
} hash^ = seed; for b in data { hash^ = (hash^ ~ u64(b)) * 0x100000001b3 }
} }
//endregion Hashing //endregion Hashing
//region Key Table 1-Layer Linear (KT1L) //region Key Table 1-Layer Linear (KT1L)
KT1L_Slot :: struct($Type: typeid) { KTL_Slot :: struct($Type: typeid) {
key: u64, key: u64,
value: Type, value: Type,
} }
KT1L_Meta :: struct { KTL_Meta :: struct {
slot_size: uintptr, slot_size: int,
kt_value_offset: uintptr, kt_value_offset: int,
type_width: uintptr, type_width: int,
type: typeid, type: typeid,
} }
kt1l_populate_slice_a2_Slice_Byte :: proc(kt: ^[]byte, backing: AllocatorInfo, values: []byte, num_values: int, m: KT1L_Meta) { ktl_populate_slice_a2_str :: #force_inline proc(kt: ^[]KTL_Slot(string), backing: AllocatorInfo, values: [][2]string) {
assert(kt != nil) assert(kt != nil)
if num_values == 0 { return } if len(values) == 0 { return }
table_size_bytes := num_values * int(m.slot_size) raw_bytes := mem_alloc(backing, size_of(KTL_Slot(string)) * len(values));
kt^ = mem_alloc(backing, table_size_bytes) kt^ = slice( transmute([^]KTL_Slot(string)) cursor(raw_bytes), len(raw_bytes) / size_of(KTL_Slot(string)) )
slice_assert(kt ^) for id in 0 ..< len(values) {
kt_raw : SliceByte = transmute(SliceByte) kt^ memory_copy(& kt[id].value, & values[id][1], size_of(string))
for id in 0 ..< cast(uintptr) num_values { hash64_fnv1a(& kt[id].key, transmute([]byte) values[id][0])
slot_offset := id * m.slot_size // slot id
slot_cursor := kt_raw.data[slot_offset:] // slots[id] type: KT1L_<Type>
slot_key := cast(^u64) slot_cursor // slots[id].key type: U64
slot_value := slice(slot_cursor[m.kt_value_offset:], m.type_width) // slots[id].value type: <Type>
a2_offset := id * m.type_width * 2 // a2 entry id
a2_cursor := cursor(values)[a2_offset:] // a2_entries[id] type: A2_<Type>
a2_key := (transmute(^[]byte) a2_cursor) ^ // a2_entries[id].key type: <Type>
a2_value := slice(a2_cursor[m.type_width:], m.type_width) // a2_entries[id].value type: <Type>
copy(slot_value, a2_value) // slots[id].value = a2_entries[id].value
slot_key^ = 0; hash64_djb8(slot_key, a2_key) // slots[id].key = hash64_djb8(a2_entries[id].key)
} }
kt_raw.len = num_values
}
kt1l_populate_slice_a2 :: proc($Type: typeid, kt: ^[]KT1L_Slot(Type), backing: AllocatorInfo, values: [][2]Type) {
assert(kt != nil)
values_bytes := slice(transmute([^]u8) raw_data(values), len(values) * size_of([2]Type))
kt1l_populate_slice_a2_Slice_Byte(transmute(^[]byte) kt, backing, values_bytes, len(values), {
slot_size = size_of(KT1L_Slot(Type)),
kt_value_offset = offset_of(KT1L_Slot(Type), value),
type_width = size_of(Type),
type = Type,
})
} }
//endregion Key Table 1-Layer Linear (KT1L) //endregion Key Table 1-Layer Linear (KT1L)
@@ -975,8 +891,7 @@ KT1CX_Cell :: struct($type: typeid, $depth: int) {
next: ^KT1CX_Cell(type, depth), next: ^KT1CX_Cell(type, depth),
} }
KT1CX :: struct($cell: typeid / KT1CX_Cell($type, $depth)) { KT1CX :: struct($cell: typeid / KT1CX_Cell($type, $depth)) {
cell_pool: []cell, table: []cell,
table: []cell,
} }
KT1CX_Byte_Slot :: struct { KT1CX_Byte_Slot :: struct {
key: u64, key: u64,
@@ -986,8 +901,7 @@ KT1CX_Byte_Cell :: struct {
next: ^byte, next: ^byte,
} }
KT1CX_Byte :: struct { KT1CX_Byte :: struct {
cell_pool: []byte, table: []byte,
table: []byte,
} }
KT1CX_ByteMeta :: struct { KT1CX_ByteMeta :: struct {
slot_size: int, slot_size: int,
@@ -1021,10 +935,8 @@ kt1cx_init :: proc(info: KT1CX_Info, m: KT1CX_InfoMeta, result: ^KT1CX_Byte) {
assert(m.cell_pool_size >= 4 * Kilo) assert(m.cell_pool_size >= 4 * Kilo)
assert(m.table_size >= 4 * Kilo) assert(m.table_size >= 4 * Kilo)
assert(m.type_width > 0) assert(m.type_width > 0)
table_raw := transmute(SliceByte) mem_alloc(info.backing_table, m.table_size * m.cell_size) table_raw := transmute(SliceByte) mem_alloc(info.backing_table, m.table_size * m.cell_size)
slice_assert(transmute([]byte) table_raw) slice_assert(transmute([]byte) table_raw)
result.cell_pool = mem_alloc(info.backing_cells, m.cell_size * m.cell_pool_size)
slice_assert(result.cell_pool)
table_raw.len = m.table_size table_raw.len = m.table_size
result.table = transmute([]byte) table_raw result.table = transmute([]byte) table_raw
} }
@@ -1050,11 +962,7 @@ kt1cx_clear :: proc(kt: KT1CX_Byte, m: KT1CX_ByteMeta) {
} }
} }
} }
kt1cx_slot_id :: proc(kt: KT1CX_Byte, key: u64, m: KT1CX_ByteMeta) -> u64 { kt1cx_slot_id :: #force_inline proc(kt: KT1CX_Byte, key: u64, m: KT1CX_ByteMeta) -> u64 { return key % u64(len(kt.table)) }
cell_size := m.cell_size // dummy value
hash_index := key % u64(len(kt.table))
return hash_index
}
kt1cx_get :: proc(kt: KT1CX_Byte, key: u64, m: KT1CX_ByteMeta) -> ^byte { kt1cx_get :: proc(kt: KT1CX_Byte, key: u64, m: KT1CX_ByteMeta) -> ^byte {
hash_index := kt1cx_slot_id(kt, key, m) hash_index := kt1cx_slot_id(kt, key, m)
cell_offset := uintptr(hash_index) * uintptr(m.cell_size) cell_offset := uintptr(hash_index) * uintptr(m.cell_size)
@@ -1125,27 +1033,22 @@ kt1cx_set :: proc(kt: KT1CX_Byte, key: u64, value: []byte, backing_cells: Alloca
return nil return nil
} }
} }
kt1cx_assert :: proc(kt: $type / KT1CX) { kt1cx_assert :: #force_inline proc(kt: $type / KT1CX) { slice_assert(kt.table) }
slice_assert(kt.cell_pool) kt1cx_byte :: #force_inline proc(kt: $type / KT1CX) -> KT1CX_Byte { return { slice( transmute([^]byte) cursor(kt.table), len(kt.table)) } }
slice_assert(kt.table)
}
kt1cx_byte :: proc(kt: $type / KT1CX) -> KT1CX_Byte { return { slice_to_bytes(kt.cell_pool), slice( transmute([^]byte) cursor(kt.table), len(kt.table)) } }
//endregion Key Table 1-Layer Chained-Chunked-Cells (KT1CX) //endregion Key Table 1-Layer Chained-Chunked-Cells (KT1CX)
//region String Operations //region String Operations
char_is_upper :: proc(c: u8) -> b32 { return('A' <= c && c <= 'Z') } char_is_upper :: #force_inline proc(c: u8) -> b32 { return('A' <= c && c <= 'Z') }
char_to_lower :: proc(c: u8) -> u8 { c:=c; if (char_is_upper(c)) { c += ('a' - 'A') }; return (c) } char_to_lower :: #force_inline proc(c: u8) -> u8 { c:=c; if (char_is_upper(c)) { c += ('a' - 'A') }; return (c) }
integer_symbols :: proc(value: u8) -> u8 { integer_symbols :: #force_inline proc(value: u8) -> u8 {
@static lookup_table: [16]u8 = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', }; @static lookup_table: [16]u8 = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', };
return lookup_table[value]; return lookup_table[value];
} }
str8_to_cstr_capped :: proc(content: string, mem: []byte) -> cstring { str8_to_cstr_capped :: #force_inline proc(content: string, mem: []byte) -> cstring {
copy_len := min(len(content), len(mem) - 1) copy_len := min(len(content), len(mem) - 1)
if copy_len > 0 { if copy_len > 0 { copy(mem[:copy_len], transmute([]byte) content) }
copy(mem[:copy_len], transmute([]byte) content)
}
mem[copy_len] = 0 mem[copy_len] = 0
return transmute(cstring) raw_data(mem) return transmute(cstring) raw_data(mem)
} }
@@ -1208,8 +1111,7 @@ str8_from_u32 :: proc(ainfo: AllocatorInfo, num: u32, radix: u32 = 10, min_digit
} }
return result return result
} }
str8_fmt_kt1l :: proc(ainfo: AllocatorInfo, _buffer: ^[]byte, table: []KTL_Slot(string), fmt_template: string) -> string {
str8_fmt_kt1l :: proc(ainfo: AllocatorInfo, _buffer: ^[]byte, table: []KT1L_Slot(string), fmt_template: string) -> string {
buffer := _buffer^ buffer := _buffer^
slice_assert(buffer) slice_assert(buffer)
slice_assert(table) slice_assert(table)
@@ -1249,7 +1151,7 @@ str8_fmt_kt1l :: proc(ainfo: AllocatorInfo, _buffer: ^[]byte, table: []KT1L_Slot
} }
if fmt_overflow do continue if fmt_overflow do continue
// Hashing the potential token and cross checking it with our token table // Hashing the potential token and cross checking it with our token table
key : u64 = 0; hash64_djb8(& key, slice(potential_token_cursor, potential_token_len)) key : u64 = 0; hash64_fnv1a(& key, slice(potential_token_cursor, potential_token_len))
value : ^string = nil value : ^string = nil
for & token in table for & token in table
{ {
@@ -1288,18 +1190,17 @@ str8_fmt_kt1l :: proc(ainfo: AllocatorInfo, _buffer: ^[]byte, table: []KT1L_Slot
result := transmute(string) slice(cursor(buffer), len(buffer) - buffer_remaining) result := transmute(string) slice(cursor(buffer), len(buffer) - buffer_remaining)
return result return result
} }
str8_fmt_backed :: #force_inline proc(tbl_ainfo, buf_ainfo: AllocatorInfo, fmt_template: string, entries: [][2]string) -> string {
str8_fmt_backed :: proc(tbl_ainfo, buf_ainfo: AllocatorInfo, fmt_template: string, entries: [][2]string) -> string { kt: []KTL_Slot(string); ktl_populate_slice_a2_str(& kt, tbl_ainfo, entries)
kt: []KT1L_Slot(string); kt1l_populate_slice_a2(string, & kt, tbl_ainfo, entries)
buf_size := Kilo * 64 buf_size := Kilo * 64
buffer := mem_alloc(buf_ainfo, buf_size) buffer := mem_alloc(buf_ainfo, buf_size)
result := str8_fmt_kt1l(buf_ainfo, & buffer, kt, fmt_template) result := str8_fmt_kt1l(buf_ainfo, & buffer, kt, fmt_template)
return result return result
} }
str8_fmt_tmp :: proc(fmt_template: string, entries: [][2]string) -> string { str8_fmt_tmp :: #force_inline proc(fmt_template: string, entries: [][2]string) -> string {
@static tbl_mem: [Kilo * 32]byte; tbl_arena := farena_make(tbl_mem[:]) @static tbl_mem: [Kilo * 32]byte; tbl_arena := farena_make(tbl_mem[:])
@static buf_mem: [Kilo * 64]byte; buffer := buf_mem[:] @static buf_mem: [Kilo * 64]byte; buffer := buf_mem[:]
kt: []KT1L_Slot(string); kt1l_populate_slice_a2(string, & kt, ainfo(& tbl_arena), entries) kt: []KTL_Slot(string); ktl_populate_slice_a2_str(& kt, ainfo(& tbl_arena), entries)
result := str8_fmt_kt1l({}, & buffer, kt, fmt_template) result := str8_fmt_kt1l({}, & buffer, kt, fmt_template)
return result return result
} }
@@ -1341,7 +1242,7 @@ str8cache_init :: proc(cache: ^Str8Cache, str_reserve, cell_reserve, tbl_backing
kt1cx_init(info, m, transmute(^KT1CX_Byte) & cache.kt) kt1cx_init(info, m, transmute(^KT1CX_Byte) & cache.kt)
return return
} }
str8cache_make :: proc(str_reserve, cell_reserve, tbl_backing: AllocatorInfo, cell_pool_size, table_size: int) -> Str8Cache { str8cache_make :: #force_inline proc(str_reserve, cell_reserve, tbl_backing: AllocatorInfo, cell_pool_size, table_size: int) -> Str8Cache {
cache : Str8Cache; str8cache_init(& cache, str_reserve, cell_reserve, tbl_backing, cell_pool_size, table_size); return cache cache : Str8Cache; str8cache_init(& cache, str_reserve, cell_reserve, tbl_backing, cell_pool_size, table_size); return cache
} }
str8cache_clear :: proc(kt: KT1CX_Str8) { str8cache_clear :: proc(kt: KT1CX_Str8) {
@@ -1392,11 +1293,10 @@ str8cache_set :: proc(kt: KT1CX_Str8, key: u64, value: string, str_reserve, cell
} }
return result return result
} }
cache_str8 :: proc(cache: ^Str8Cache, str: string) -> string { cache_str8 :: #force_inline proc(cache: ^Str8Cache, str: string) -> string {
assert(cache != nil) assert(cache != nil)
key: u64 = 0; hash64_djb8(& key, transmute([]byte) str) key: u64 = 0; hash64_fnv1a(& key, transmute([]byte) str)
result := str8cache_set(cache.kt, key, str, cache.str_reserve, cache.cell_reserve) return str8cache_set(cache.kt, key, str, cache.str_reserve, cache.cell_reserve) ^
return result ^
} }
Str8Gen :: struct { Str8Gen :: struct {
@@ -1413,9 +1313,9 @@ str8gen_init :: proc(gen: ^Str8Gen, ainfo: AllocatorInfo) {
gen.len = 0 gen.len = 0
gen.cap = Kilo * 4 gen.cap = Kilo * 4
} }
str8gen_make :: proc(ainfo: AllocatorInfo) -> Str8Gen { gen: Str8Gen; str8gen_init(& gen, ainfo); return gen } str8gen_make :: #force_inline proc(ainfo: AllocatorInfo) -> Str8Gen { gen: Str8Gen; str8gen_init(& gen, ainfo); return gen }
str8gen_to_bytes :: proc(gen: Str8Gen) -> []byte { return transmute([]byte) SliceByte {data = gen.ptr, len = gen.cap} } str8gen_to_bytes :: #force_inline proc(gen: Str8Gen) -> []byte { return transmute([]byte) SliceByte {data = gen.ptr, len = gen.cap} }
str8_from_str8gen :: proc(gen: Str8Gen) -> string { return transmute(string) SliceByte {data = gen.ptr, len = gen.len} } str8_from_str8gen :: #force_inline proc(gen: Str8Gen) -> string { return transmute(string) SliceByte {data = gen.ptr, len = gen.len} }
str8gen_append_str8 :: proc(gen: ^Str8Gen, str: string) { str8gen_append_str8 :: proc(gen: ^Str8Gen, str: string) {
result := mem_grow(gen.backing, str8gen_to_bytes(gen ^), len(str) + gen.len) result := mem_grow(gen.backing, str8gen_to_bytes(gen ^), len(str) + gen.len)
@@ -1423,12 +1323,12 @@ str8gen_append_str8 :: proc(gen: ^Str8Gen, str: string) {
to_copy := slice(cursor(result)[gen.len:], len(result) - gen.len) to_copy := slice(cursor(result)[gen.len:], len(result) - gen.len)
copy(to_copy, transmute([]byte) str) copy(to_copy, transmute([]byte) str)
gen.ptr = transmute(^u8) raw_data(result) gen.ptr = transmute(^u8) raw_data(result)
gen.len = len(result) gen.len = len(str) + gen.len
gen.cap = max(gen.len, gen.cap) // TODO(Ed): Arenas currently hide total capacity before growth. Problably better todo classic append to actually track this. gen.cap = len(result)
} }
str8gen_append_fmt :: proc(gen: ^Str8Gen, fmt_template: string, tokens: [][2]string) { str8gen_append_fmt :: proc(gen: ^Str8Gen, fmt_template: string, tokens: [][2]string) {
@static tbl_mem: [Kilo * 32]byte; tbl_arena := farena_make(tbl_mem[:]) @static tbl_mem: [Kilo * 32]byte; tbl_arena := farena_make(tbl_mem[:])
kt: []KT1L_Slot(string); kt1l_populate_slice_a2(string, & kt, ainfo(& tbl_arena), tokens) kt: []KTL_Slot(string); ktl_populate_slice_a2_str(& kt, ainfo(& tbl_arena), tokens)
buffer := slice(gen.ptr[gen.len:], gen.cap - gen.len) buffer := slice(gen.ptr[gen.len:], gen.cap - gen.len)
if len(buffer) < Kilo * 16 { if len(buffer) < Kilo * 16 {
result := mem_grow(gen.backing, str8gen_to_bytes(gen ^), Kilo * 16 + gen.cap) result := mem_grow(gen.backing, str8gen_to_bytes(gen ^), Kilo * 16 + gen.cap)
@@ -1539,9 +1439,8 @@ api_file_read_contents :: proc(result: ^FileOpInfo, path: string, backing: Alloc
result.content = slice(cursor(buffer), cast(int) file_size.QuadPart) result.content = slice(cursor(buffer), cast(int) file_size.QuadPart)
return return
} }
file_read_contents_stack :: proc(path: string, backing: AllocatorInfo, zero_backing: b32 = false) -> FileOpInfo { file_read_contents_stack :: #force_inline proc(path: string, backing: AllocatorInfo, zero_backing: b32 = false) -> FileOpInfo {
result : FileOpInfo; api_file_read_contents(& result, path, backing, zero_backing) result: FileOpInfo; api_file_read_contents(& result, path, backing, zero_backing) return result
return result
} }
file_write_str8 :: proc(path, content: string) { file_write_str8 :: proc(path, content: string) {
string_assert(path) string_assert(path)
@@ -1628,55 +1527,54 @@ api_watl_lex :: proc(info: ^WATL_LexInfo, source: string,
alloc_tok :: #force_inline proc(ainfo: AllocatorInfo) -> ^Raw_String { alloc_tok :: #force_inline proc(ainfo: AllocatorInfo) -> ^Raw_String {
return alloc_type(ainfo, Raw_String, align_of(Raw_String), true) return alloc_type(ainfo, Raw_String, align_of(Raw_String), true)
} }
#partial switch cast(WATL_TokKind) code #partial switch cast(WATL_TokKind) code {
{ case .Space: fallthrough
case .Space: fallthrough case .Tab:
case .Tab: if prev[0] != src_cursor[0] {
if prev[0] != src_cursor[0] { new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil {
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil { slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return
}
tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 0)
was_formatting = true
num += 1
}
src_cursor = src_cursor[1:]
tok.len += 1
case .Line_Feed:
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil{
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return return
} }
tok = new_tok tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 1) tok^ = transmute(Raw_String) slice(src_cursor, 0)
src_cursor = src_cursor[1:]
was_formatting = true was_formatting = true
num += 1 num += 1
case .Carriage_Return: }
src_cursor = src_cursor[1:]
tok.len += 1
case .Line_Feed:
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil{
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return
}
tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 1)
src_cursor = src_cursor[1:]
was_formatting = true
num += 1
case .Carriage_Return:
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil {
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return
}
tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 2)
src_cursor = src_cursor[1:]
was_formatting = true
num += 1
case:
if (was_formatting) {
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil { new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil {
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last); slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return return
} }
tok = new_tok tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 2) tok^ = transmute(Raw_String) slice(src_cursor, 0)
src_cursor = src_cursor[1:] was_formatting = false;
was_formatting = true
num += 1 num += 1
case: }
if (was_formatting) { src_cursor = src_cursor[1:]
new_tok := alloc_tok(ainfo_toks); if cursor(new_tok)[-1:] != tok && tok != nil { tok.len += 1
slice_constraint_fail(info, ainfo_msgs, new_tok, & msg_last);
return
}
tok = new_tok
tok^ = transmute(Raw_String) slice(src_cursor, 0)
was_formatting = false;
num += 1
}
src_cursor = src_cursor[1:]
tok.len += 1
} }
prev = src_cursor[-1:] prev = src_cursor[-1:]
code = src_cursor[0] code = src_cursor[0]
@@ -1753,28 +1651,25 @@ api_watl_parse :: proc(info: ^WATL_ParseInfo, tokens: []WATL_Tok,
info_lines ^ = { transmute([^]WATL_Node) line, 0 } info_lines ^ = { transmute([^]WATL_Node) line, 0 }
for & token in tokens for & token in tokens
{ {
#partial switch cast(WATL_TokKind) token[0] #partial switch cast(WATL_TokKind) token[0] {
{ case .Carriage_Return: fallthrough
case .Carriage_Return: fallthrough case .Line_Feed:
case .Line_Feed: new_line := alloc_type(ainfo_lines, WATL_Line); if cursor(new_line)[-1:] != transmute(^[]string)line {
new_line := alloc_type(ainfo_lines, WATL_Line); if cursor(new_line)[-1:] != transmute(^[]string)line { info.signal |= { .MemFail_SliceConstraintFail }
info.signal |= { .MemFail_SliceConstraintFail } msg := alloc_type(ainfo_msgs, WATL_ParseMsg)
msg := alloc_type(ainfo_msgs, WATL_ParseMsg) msg.content = "Line slice allocation was not contiguous"
msg.content = "Line slice allocation was not contiguous" msg.pos = { cast(i32) len(info.lines), cast(i32) line.len }
msg.pos = { cast(i32) len(info.lines), cast(i32) line.len } msg.line = transmute(^[]WATL_Node) line
msg.line = transmute(^[]WATL_Node) line msg.tok = & token
msg.tok = & token sll_queue_push_n(& info.msgs, & msg_last, & msg)
sll_queue_push_n(& info.msgs, & msg_last, & msg) assert(failon_slice_constraint_fail == false)
assert(failon_slice_constraint_fail == false) return
return }
} line = transmute(^SliceRaw(WATL_Node)) new_line
line = transmute(^SliceRaw(WATL_Node)) new_line line.data = curr
line.data = curr info_lines.len += 1
info_lines.len += 1 continue
continue case: break;
case:
break;
} }
curr ^ = cache_str8(str_cache, token) curr ^ = cache_str8(str_cache, token)
new_node := alloc_type(ainfo_nodes, WATL_Node); if cursor(new_node)[-1:] != curr { new_node := alloc_type(ainfo_nodes, WATL_Node); if cursor(new_node)[-1:] != curr {
@@ -1823,11 +1718,10 @@ watl_dump_listing :: proc(buffer: AllocatorInfo, lines: []WATL_Line) -> string {
for chunk in line for chunk in line
{ {
id : string id : string
#partial switch cast(WATL_TokKind) chunk[0] #partial switch cast(WATL_TokKind) chunk[0] {
{ case .Space: id = "Space"
case .Space: id = "Space" case .Tab: id = "Tab"
case .Tab: id = "Tab" case: id = "Visible"
case: id = "Visible"
} }
str8gen_append_fmt(& result, "\t<id>(<size>): '<chunk>'\n", { str8gen_append_fmt(& result, "\t<id>(<size>): '<chunk>'\n", {
{ "id", id }, { "id", id },

View File

@@ -34,6 +34,8 @@ Embeddable scripting languages will be embedded as they should be.
* Fix large-pages not working (at least on my system). * Fix large-pages not working (at least on my system).
Goals:
* [x] Single-threaded C example * [x] Single-threaded C example
* [] Multi-threaded C example * [] Multi-threaded C example
* [] Add basic timing benchmark to C examples * [] Add basic timing benchmark to C examples

200
scripts/build.c.lottes.ps1 Normal file
View File

@@ -0,0 +1,200 @@
$misc = Join-Path $PSScriptRoot 'helpers/misc.psm1'
import-module $misc
# This script now uses the LLVM toolchain (clang-cl, lld-link).
# Ensure these tools are available in your PATH.
# The original call to the MSVC devshell has been removed.
# & (join-path $PSScriptRoot 'helpers/devshell.ps1') -arch amd64
$path_root = Get-ScriptRepoRoot
$path_root = split-path -Path $PSScriptRoot -Parent
$path_toolchain = join-path $path_root 'toolchain'
$path_rad = join-path $path_toolchain 'rad'
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170
# Most cl.exe flags are compatible with clang-cl.exe
$flag_all_c = '/TC'
$flag_c11 = '/std:c11'
$flag_c23 = '/std:c23'
$flag_all_cpp = '/TP'
$flag_compile = '/c'
$flag_charset_utf8 = '/utf-8'
$flag_debug = '/Zi'
$flag_define = '/D'
$flag_exceptions_disabled = '/EHsc-'
$flag_RTTI_disabled = '/GR-'
$flag_include = '/I'
$flag_full_src_path = '/FC'
$flag_asm_listing_file = '/FAs'
$flag_nologo = '/nologo'
$flag_dll = '/LD'
$flag_dll_debug = '/LDd'
$flag_linker = '/link'
# $flag_link_lib = '/lib'
$flag_link_dll = '/DLL'
$flag_link_no_incremental = '/INCREMENTAL:NO'
$flag_link_mapfile = '/MAP:'
$flag_link_optimize_references = '/OPT:REF'
$flag_link_win_debug = '/DEBUG'
$flag_link_win_pdb = '/PDB:'
$flag_link_win_machine_32 = '/MACHINE:X86'
$flag_link_win_machine_64 = '/MACHINE:X64'
$flag_link_win_path_output = '/OUT:'
$flag_link_win_rt_dll = '/MD'
$flag_link_win_rt_dll_debug = '/MDd'
$flag_link_win_rt_static = '/MT'
$flag_link_win_rt_static_debug = '/MTd'
$flag_link_win_subsystem_console = '/SUBSYSTEM:CONSOLE'
$flag_link_win_subsystem_windows = '/SUBSYSTEM:WINDOWS'
$flag_no_optimization = '/Od'
$flag_optimize_speed_max = '/Ox'
$flag_optimize_fast = '/O2'
$flag_optimize_size = '/O1'
$flag_optimize_intrinsics = '/Oi'
$flag_optimized_debug_forceinline = '/d2Obforceinline'
$flag_optimized_debug = '/Zo'
$flag_preprocess_to_file = '/P'
$flag_preprocess_preserve_comments = '/C'
# $flag_out_name = '/OUT:'
$flag_path_interm = '/Fo'
$flag_path_debug = '/Fd'
$flag_path_output = '/Fe'
$flag_preprocess_conform = '/Zc:preprocessor'
$flag_sanitize_address = '/fsanitize=address'
$flag_updated_cpp_macro = "/Zc:__cplusplus"
$flag_set_stack_size = '/F'
$flag_syntax_only = '/Zs'
$flag_wall = '/Wall'
$flag_warnings_as_errors = '/WX'
$flag_lib_list = '/LIST'
$archiver = 'llvm-lib'
$compiler = 'clang-cl'
$linker = 'lld-link'
$radbin = join-path $path_rad 'radbin.exe'
$radlink = join-path $path_rad 'radlink.exe'
$path_build = join-path $path_root 'build'
if ( -not(test-path -Path $path_build) ) {
new-item -ItemType Directory -Path $path_build
}
push-location $path_build
write-host "Compiling with clang-cl"
$compiler_args = @()
$compiler_args += $flag_nologo
# Constraints on interpeting all files as C code
$compiler_args += $flag_all_c
$compiler_args += $flag_c11
# Constraints on C program code-gen
$compiler_args += $flag_exceptions_disabled
$compiler_args += $flag_RTTI_disabled
# $compiler_args += $flag_preprocess_conform
# $compiler_args += $flag_sanitize_address
$compiler_args += $flag_wall
# Set charset encoding for both execution and source to UTF-8
$compiler_args += $flag_charset_utf8
# Specifing output pathing
$compiler_args += ( $flag_path_interm + $path_build + '\' )
# $compiler_args += ( $flag_path_output + $path_build + '\' )
# Dump preprocess file
if ($false) {
$compiler_args += $flag_preprocess_to_file
$compiler_args += $flag_preprocess_preserve_comments
}
# Diagnostic logging
$compiler_args += $flag_full_src_path
# $compiler_args += $flag_asm_listing_file
# $compiler_args += $flag_optimize_speed_max
# $compiler_args += $flag_optimize_fast
# $compiler_args += $flag_optimize_size
$compiler_args += $flag_optimize_intrinsics
# $compiler_args += $flag_no_optimization
# Debug setup
$compiler_args += ($flag_define + 'BUILD_DEBUG')
$compiler_args += $flag_debug
$compiler_args += ( $flag_path_debug + $path_build + '\' )
# Use the static, multithreaded, debug runtime library
# $compiler_args += $flag_link_win_rt_static_debug
# Include setup
$compiler_args += ($flag_include + $path_root)
$unit_name = "watl.v0.llvm.lottes"
# Specify unit to compile
$unit = join-path $path_root "C\$unit_name.c"
$compiler_args += $flag_compile, $unit
# Diagnoistc print for the args
$compiler_args | ForEach-Object { Write-Host $_ }
# Compile the unit
$compilation_time = Measure-Command {
& $compiler $compiler_args
}
write-host "Compilation took $($compilation_time.TotalMilliseconds)ms"
write-host
$binary = join-path $path_build "$unit_name.exe"
$object = join-path $path_build "$unit_name.obj"
$pdb = join-path $path_build "$unit_name.pdb"
$map = join-path $path_build "$unit_name.map"
$rdi = join-path $path_build "$unit_name.rdi"
$rdi_listing = join-path $path_build "$unit_name.rdi.list"
if ($true) {
write-host "Linking with lld-link"
$linker_args = @()
$linker_args += $flag_nologo
$linker_args += $flag_link_win_machine_64
$linker_args += $flag_link_no_incremental
$linker_args += ($flag_link_win_path_output + $binary)
$linker_args += "$flag_link_win_debug"
$linker_args += $flag_link_win_pdb + $pdb
$linker_args += $flag_link_mapfile + $map
$linker_args += $flag_link_win_subsystem_console
$linker_args += $object
# Add necessary libraries for a basic Windows application
$linker_args += "kernel32.lib", "user32.lib", "gdi32.lib"
# Diagnoistc print for the args
$linker_args | ForEach-Object { Write-Host $_ }
$linking_time = Measure-Command { & $linker $linker_args }
# & $radlink $linker_args
write-host "Linking took $($linking_time.TotalMilliseconds)ms"
write-host
}
if ($false) {
write-host "Dumping Debug Info"
$rbin_out = '--out:'
$rbin_dump = '--dump'
$nargs = @($pdb, ($rbin_out + $rdi))
& $radbin $nargs
$nargs = @($rbin_dump, $rdi)
$dump = & $radbin $nargs
$dump > $rdi_listing
}
Pop-Location

View File

@@ -118,8 +118,8 @@ $compiler_args += $flag_full_src_path
# $compiler_args += $flag_optimize_speed_max # $compiler_args += $flag_optimize_speed_max
# $compiler_args += $flag_optimize_fast # $compiler_args += $flag_optimize_fast
# $compiler_args += $flag_optimize_size # $compiler_args += $flag_optimize_size
# $compiler_args += $flag_optimize_intrinsics $compiler_args += $flag_optimize_intrinsics
$compiler_args += $flag_no_optimization # $compiler_args += $flag_no_optimization
# Debug setup # Debug setup
$compiler_args += ($flag_define + 'BUILD_DEBUG') $compiler_args += ($flag_define + 'BUILD_DEBUG')

View File

@@ -117,8 +117,8 @@ $compiler_args += $flag_full_src_path
# $compiler_args += $flag_optimize_speed_max # $compiler_args += $flag_optimize_speed_max
# $compiler_args += $flag_optimize_fast # $compiler_args += $flag_optimize_fast
# $compiler_args += $flag_optimize_size # $compiler_args += $flag_optimize_size
# $compiler_args += $flag_optimize_intrinsics $compiler_args += $flag_optimize_intrinsics
$compiler_args += $flag_no_optimization # $compiler_args += $flag_no_optimization
# Debug setup # Debug setup
$compiler_args += ($flag_define + 'BUILD_DEBUG') $compiler_args += ($flag_define + 'BUILD_DEBUG')
@@ -139,7 +139,10 @@ $compiler_args += $flag_compile, $unit
$compiler_args | ForEach-Object { Write-Host $_ } $compiler_args | ForEach-Object { Write-Host $_ }
# Compile the unit # Compile the unit
& $compiler $compiler_args $compilation_time = Measure-Command {
& $compiler $compiler_args
}
write-host "Compilation took $($compilation_time.TotalMilliseconds)ms"
write-host write-host
$binary = join-path $path_build "$unit_name.exe" $binary = join-path $path_build "$unit_name.exe"
@@ -168,8 +171,9 @@ if ($true) {
# Diagnoistc print for the args # Diagnoistc print for the args
$linker_args | ForEach-Object { Write-Host $_ } $linker_args | ForEach-Object { Write-Host $_ }
& $linker $linker_args $linking_time = Measure-Command { & $linker $linker_args }
# & $radlink $linker_args # & $radlink $linker_args
write-host "Linking took $($linking_time.TotalMilliseconds)ms"
write-host write-host
} }

View File

@@ -106,6 +106,8 @@ main :: proc() {
flag_file, flag_file,
join_str(flag_output_path, file_exe), join_str(flag_output_path, file_exe),
flag_optimize_none, flag_optimize_none,
// flag_optimize_minimal,
// flag_optimize_speed,
// falg_optimize_aggressive, // falg_optimize_aggressive,
flag_default_allocator_nil, flag_default_allocator_nil,
flag_debug, flag_debug,