Add hex support

This commit is contained in:
Krzosa Karol
2022-06-13 22:14:31 +02:00
parent e73820c6f5
commit c0253f0424
5 changed files with 70 additions and 28 deletions

View File

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

20
base.kl
View File

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

View File

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

View File

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

6
main.kl Normal file
View File

@@ -0,0 +1,6 @@
Base :: #load "base.kl"
CONSTANT :: 0xabCAFAAABCDAEAA
main :: (argc: int, argv: **char): int
memory := Base.reserve(size = 10000)