From c0253f0424c2cb20b4553bb575839288922908d8 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 13 Jun 2022 22:14:31 +0200 Subject: [PATCH] Add hex support --- Windows.kl | 20 ++++++++++---------- base.kl | 20 ++++++++++++-------- ccodegen.cpp | 6 +++++- lexing.cpp | 46 +++++++++++++++++++++++++++++++++++++--------- main.kl | 6 ++++++ 5 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 main.kl diff --git a/Windows.kl b/Windows.kl index 31bf452..eac1023 100644 --- a/Windows.kl +++ b/Windows.kl @@ -10,22 +10,22 @@ BOOL :: int CreateWindowA :: #foreign (dwExStyle: DWORD, lpClassName: *char, lpWindowName: *char, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpParam: *void): HWND -MEM_COMMIT :: 1//0x00001000 -MEM_RESERVE :: 2//0x00002000 -MEM_RESET :: 3//0x00080000 -MEM_RESET_UNDO :: 4//0x1000000 +MEM_COMMIT :: 0x00001000 +MEM_RESERVE :: 0x00002000 +MEM_RESET :: 0x00080000 +MEM_RESET_UNDO :: 0x1000000 -MEM_DECOMMIT :: 5//0x00004000 -MEM_RELEASE :: 6//0x00008000 +MEM_DECOMMIT :: 0x00004000 +MEM_RELEASE :: 0x00008000 PAGE_NOACCESS :: 1 PAGE_READONLY :: 2 PAGE_READWRITE :: 4 PAGE_WRITECOPY :: 8 -PAGE_EXECUTE :: 10//0x10 -PAGE_EXECUTE_READ :: 20//0x20 -PAGE_EXECUTE_READWRITE :: 30//0x40 -PAGE_EXECUTE_WRITECOPY :: 40//0x80 +PAGE_EXECUTE :: 0x10 +PAGE_EXECUTE_READ :: 0x20 +PAGE_EXECUTE_READWRITE :: 0x40 +PAGE_EXECUTE_WRITECOPY :: 0x80 VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID VirtualFree :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, dwFreeType: DWORD): BOOL \ No newline at end of file diff --git a/base.kl b/base.kl index be7268e..b166720 100644 --- a/base.kl +++ b/base.kl @@ -13,6 +13,11 @@ Arena :: struct alignment: U64 len: SizeU +clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU + if val > max + return max + return val + get_align_offset :: (size: SizeU, align: SizeU): SizeU mask := align - 1 val := size & mask @@ -30,22 +35,21 @@ reserve :: (size: SizeU): OS_Memory flProtect = Windows.PAGE_READWRITE, dwSize = result.reserve, flAllocationType = Windows.MEM_RESERVE, - lpAddress = 0, - )->*U8 + lpAddress = 0)->*U8 return result -clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU - if val > max - return max - return val - 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.VirtualFree(m.data->*void, 0, Windows.MEM_RELEASE) + 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 7d61a55..e9fe0a2 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -557,12 +557,16 @@ resolve_everything_in_module(Ast_Module *module){ resolving_time_end = os_time(); } +global F64 init_ctx_time_begin; +global F64 init_ctx_time_end; function void begin_compilation(){ + init_ctx_time_begin = os_time(); OS_Heap *heap = exp_alloc_type(&pernament_arena, OS_Heap); - *heap = win32_os_heap_create(false, mib(16), 0); + *heap = win32_os_heap_create(false, mib(4), 0); Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx); parse_init(ctx, &pernament_arena, heap); + init_ctx_time_end = os_time(); } global F64 generating_time_begin; diff --git a/lexing.cpp b/lexing.cpp index 40f9911..6441395 100644 --- a/lexing.cpp +++ b/lexing.cpp @@ -37,6 +37,13 @@ lex_is_numeric(U8 c){ return result; } +function B32 +lex_is_numeric_base16(U8 c){ + B32 result = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || + (c >= 'a' && c <= 'f'); + return result; +} + function B32 lex_is_alphanumeric(U8 c){ B32 result = lex_is_numeric(c) || lex_is_alphabetic(c); @@ -73,20 +80,25 @@ token_error(Token *t, String error_val){ } function void -lex_parse_u64(Lexer *lexer, Token *t){ +lex_parse_u64(Lexer *lexer, Token *t, S64 base){ Scratch scratch; Set_BigInt_Allocator(scratch); t->kind = TK_Integer; - BigInt m = bigint_u64(1); // @leak, it accumulates and potentially needs allocation - BigInt val10 = bigint_u64(10); + BigInt m = bigint_u64(1); + BigInt base_mul = bigint_u64(base); BigInt result = bigint_u64(0); for(S64 i = t->len - 1; i >= 0; --i){ - BigInt val = bigint_u64(t->str[i] - '0'); // I dont think this is a leak, too small - BigInt new_val = bigint_mul(&val, &m); // @leak - result = bigint_add(&result, &new_val); // @leak - m = bigint_mul(&m, &val10); // @leak + U64 value = t->str[i]; + if(t->str[i] >= 'a') value = value - 'a' + 10; + else if(t->str[i] >= 'A') value = value - 'A' + 10; + else value -= '0'; + + BigInt val = bigint_u64(value); + BigInt new_val = bigint_mul(&val, &m); + result = bigint_add(&result, &new_val); + m = bigint_mul(&m, &base_mul); } t->int_val = bigint_copy(lexer->arena, &result); @@ -475,7 +487,23 @@ lex__stream(Lexer *lexer){ } } break; - case '0':case '1':case '2':case '3':case '4': + case '0':{ + if(lexc(s) == 'x'){ + lex_advance(s); + while(lex_is_numeric_base16(lexc(s))) + lex_advance(s); + lex_set_len(s, &t); + t.str += 2; + t.len -= 2; + if(t.len == 0) + token_error(&t, "Hex constant doesn't have value"_s); + else + lex_parse_u64(lexer, &t, 16); + break; + } + + } + case '1':case '2':case '3':case '4': case '5':case '6':case '7':case '8':case '9':{ B32 found_dot = false; for(;;){ @@ -494,7 +522,7 @@ lex__stream(Lexer *lexer){ } lex_set_len(s, &t); if(found_dot) lex_parse_f64(&t); - else lex_parse_u64(lexer, &t); + else lex_parse_u64(lexer, &t, 10); } break; diff --git a/main.kl b/main.kl new file mode 100644 index 0000000..9413cc2 --- /dev/null +++ b/main.kl @@ -0,0 +1,6 @@ +Base :: #load "base.kl" + +CONSTANT :: 0xabCAFAAABCDAEAA + +main :: (argc: int, argv: **char): int + memory := Base.reserve(size = 10000)