From d63a327e3ee7227568c5b867610db7f0a7613833 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 14 Jun 2022 14:15:21 +0200 Subject: [PATCH] Emitting proper lines and files, coding in the language! --- base.kl | 39 +++------------------------------------ ccodegen.cpp | 15 +++++++++------ main.cpp | 2 +- main.kl | 11 +++++------ os.kl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 os.kl diff --git a/base.kl b/base.kl index 19c77dc..458e039 100644 --- a/base.kl +++ b/base.kl @@ -1,18 +1,11 @@ -Windows :: #import "Windows.kl" -#import "Windows.kl" +Os :: #import "os.kl" SizeU :: #strict U64 -OS_PAGE_SIZE :: 4096 - -OS_Memory :: struct - commit : SizeU - reserve: SizeU - data : *U8 Arena :: struct - memory: OS_Memory + memory: Os.Memory alignment: U64 - len: SizeU + len: U64 clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU if val > max @@ -29,29 +22,3 @@ get_align_offset :: (size: SizeU, align: SizeU): SizeU align_up :: (size: SizeU, align: SizeU): SizeU result := size + get_align_offset(size, align) return result - -reserve :: (size: SizeU): OS_Memory - result := OS_Memory{reserve=align_up(size, OS_PAGE_SIZE)} - result.data = Windows.VirtualAlloc( - flProtect = Windows.PAGE_READWRITE, - dwSize = result.reserve, - flAllocationType = Windows.MEM_RESERVE, - lpAddress = 0)->*U8 - return result - -commit :: (m: *OS_Memory, size: SizeU): Bool - commit_size := align_up(size, OS_PAGE_SIZE) - total_commit := m.commit + commit_size - clamped_commit := clamp_top_sizeu(total_commit, m.reserve) - adjusted_commit := clamped_commit - m.commit - if adjusted_commit != 0 - result := Windows.VirtualAlloc( - lpAddress = (m.data + m.commit)->*void, - dwSize = adjusted_commit, - flAllocationType = Windows.MEM_COMMIT, - flProtect = Windows.PAGE_READWRITE, - ) - m.commit += adjusted_commit - return true - return false - diff --git a/ccodegen.cpp b/ccodegen.cpp index d7f0208..fe8a1fb 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -13,10 +13,16 @@ gen_indent(){ for(S32 i = 0; i < global_indent; i++) gen(" "); } +global Intern_String last_filename; function void gen_line(Ast *node){ - if(emit_line_directives) + if(emit_line_directives){ genln("#line %d", node->pos->line+1); + if(node->pos->file != last_filename){ + last_filename = node->pos->file; + gen(" \"%s\"", last_filename.str); + } + } } function String @@ -415,6 +421,8 @@ gen_ast(Ast *ast){ } CASE(LAMBDA, Decl){ + gen_line(node); + genln(""); if(is_flag_set(node->expr->flags, AST_FOREIGN)){ gen("/*foreign*/"); } @@ -662,11 +670,6 @@ typedef S32 Bool; #define true 1 #define false 0 -typedef struct Slice{ - S64 len; - void *data; -}Slice; - typedef struct String{ U8 *str; S64 len; diff --git a/main.cpp b/main.cpp index 45e9811..f625420 100644 --- a/main.cpp +++ b/main.cpp @@ -176,7 +176,7 @@ int main(int argument_count, char **arguments){ system((const char *)run_program.str); } #else - emit_line_directives = false; + emit_line_directives = true; F64 total_time = os_time(); begin_compilation(); Ast_Module *module = add_module(pctx->intern("main.kl"_s)); diff --git a/main.kl b/main.kl index ec754fe..3a91ce3 100644 --- a/main.kl +++ b/main.kl @@ -1,10 +1,9 @@ #import "base.kl" - -print :: (string: String) - handle := Windows.GetStdHandle(Windows.STD_OUTPUT_HANDLE) - Windows.WriteConsoleA(handle, &string[0]->*void, length_of(string)->Windows.DWORD, 0, 0) +os :: #import "os.kl" main :: (argc: int, argv: **char): int - memory := reserve(size = 10000) - print("Hello world") + memory := os.reserve(size = 10000) + os.commit(&memory, 1000) + os.release(&memory) + os.print("Hello world") diff --git a/os.kl b/os.kl new file mode 100644 index 0000000..14f0f62 --- /dev/null +++ b/os.kl @@ -0,0 +1,44 @@ +#import "Windows.kl" +#import "base.kl" + +PAGE_SIZE :: 4096 +Memory :: struct + commit : SizeU + reserve: SizeU + data : *U8 + +reserve :: (size: SizeU): Memory + result := Memory{reserve=align_up(size, PAGE_SIZE)} + result.data = VirtualAlloc( + flProtect = PAGE_READWRITE, + dwSize = result.reserve, + flAllocationType = MEM_RESERVE, + lpAddress = 0)->*U8 + return result + +commit :: (m: *Memory, size: SizeU): Bool + commit_size := align_up(size, PAGE_SIZE) + total_commit := m.commit + commit_size + clamped_commit := clamp_top_sizeu(total_commit, m.reserve) + adjusted_commit := clamped_commit - m.commit + if adjusted_commit != 0 + result := VirtualAlloc( + lpAddress = (m.data + m.commit)->*void, + dwSize = adjusted_commit, + flAllocationType = MEM_COMMIT, + flProtect = PAGE_READWRITE, + ) + m.commit += adjusted_commit + return true + return false + +release :: (m: *Memory) + result := VirtualFree(m.data->*void, 0, MEM_RELEASE) + if result != 0 + m.data = 0 + m.commit = 0 + m.reserve = 0 + +print :: (string: String) + handle := GetStdHandle(STD_OUTPUT_HANDLE) + WriteConsoleA(handle, &string[0]->*void, length_of(string)->DWORD, 0, 0)