Emitting proper lines and files, coding in the language!

This commit is contained in:
Krzosa Karol
2022-06-14 14:15:21 +02:00
parent f885abe3f5
commit d63a327e3e
5 changed files with 62 additions and 49 deletions

39
base.kl
View File

@@ -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

View File

@@ -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;

View File

@@ -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));

11
main.kl
View File

@@ -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")

44
os.kl Normal file
View File

@@ -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)