From c5539276aef4e7121bb5cacbd3aaec893cbb1e9b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 1 Jan 2023 12:40:58 +0100 Subject: [PATCH] Working on simplifying configurable allocation scheme --- base.cpp | 106 ------------------------------------ base_string.cpp | 39 +++++-------- base_unicode.cpp | 105 ----------------------------------- c3_big_int.cpp | 13 ++--- c3_big_int.h | 3 +- core_arena.cpp | 9 +++ core_ast.cpp | 6 +- core_codegen_c_language.cpp | 48 ++++++++-------- core_compiler.cpp | 25 +++++---- core_globals.cpp | 3 +- core_lexing.cpp | 8 +-- core_main.cpp | 19 ++----- core_parsing.cpp | 50 ++++++++++++----- core_typechecking.cpp | 37 +++++++++---- core_typechecking.h | 1 - core_types.cpp | 4 +- os_linux.cpp | 16 +++--- os_windows.cpp | 24 ++++---- 18 files changed, 169 insertions(+), 347 deletions(-) diff --git a/base.cpp b/base.cpp index a09ff30..e71163a 100644 --- a/base.cpp +++ b/base.cpp @@ -528,66 +528,6 @@ arena_sub(Arena *base, size_t size, String debug_name) { return result; } -enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; -typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); -//----------------------------------------------------------------------------- -// Thread Context -//----------------------------------------------------------------------------- -struct Thread_Ctx{ - Arena scratch[2]; - Log_Proc *log_proc; - int thread_index; - - int line; - char *file; -}; - -thread_local Thread_Ctx thread_ctx; - -#define REPORT_ALLOCATIONS 0 -#define report_file_and_line() report__file_and_line(__FILE__, __LINE__) -force_inline void -report__file_and_line(const char *file, int line){ - thread_ctx.file = (char *)file; - thread_ctx.line = line; -} - -//----------------------------------------------------------------------------- -// Implicit scratch stack -//----------------------------------------------------------------------------- -struct Scratch{ - size_t saved_pos; - Arena *arena; - - Scratch(Arena *conflict = 0){ - if(conflict == thread_ctx.scratch){ - arena = thread_ctx.scratch + 1; - } - else { - arena = thread_ctx.scratch; - } - saved_pos = arena->len; - } - ~Scratch(){ - arena_pop_pos(arena, saved_pos); - } - force_inline operator Arena*(){ return arena; } - force_inline operator Allocator*(){ return arena; } - - // @Note: Disable copy constructors, cause it caused lots of confusing errors - // Where it passed scratch instead of the arena into the constructor - // which is an error - private: - Scratch(Scratch &arena); - Scratch(Scratch &arena, Scratch &a2); -}; - -CORE_Static void -thread_ctx_init(){ - arena_init(thread_ctx.scratch, "Scratch1"_s); - arena_init(thread_ctx.scratch+1, "Scratch2"_s); -} - //----------------------------------------------------------------------------- // Array //----------------------------------------------------------------------------- @@ -716,27 +656,6 @@ array_make(Allocator *a, S64 size = 16){ } #include "base_string.cpp" -//----------------------------------------------------------------------------- -// Logging -//----------------------------------------------------------------------------- -#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__) -#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__) -#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__) -#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__) -CORE_Static void -handle_log_message(Log_Kind kind, int line, const char *file, const char *str, ...){ - if(kind == Log_Kind_Trace) return; - - Scratch scratch; - STRING_FMT(scratch, str, message); - if(thread_ctx.log_proc) thread_ctx.log_proc(kind, message, (char *)file, line); - else{ - printf("%s", message.str); - if(kind != Log_Kind_Normal_No_NewLine){ - printf("\n"); - } - } -} //----------------------------------------------------------------------------- // Defer @@ -896,20 +815,6 @@ map_insert(Map *map, Intern_String key, void *value){ map_insert(map, hash_string(key.s), value); } -CORE_Static void -map_test(){ - Scratch scratch; - Map map = {scratch}; - const size_t size = 1025; - for(size_t i = 1; i < size; i++){ - map_insert(&map, i, (void *)i); - } - for(size_t i = 1; i < size; i++){ - size_t val = (size_t)map_get(&map, i); - assert(val == i); - } -} - //----------------------------------------------------------------------------- // String intern //----------------------------------------------------------------------------- @@ -952,17 +857,6 @@ intern_string(Intern_Table *t, String string){ return result; } -CORE_Static void -test_intern_table(){ - Scratch scratch; - Intern_Table table = intern_table_make(scratch, scratch); - Intern_String intern1 = intern_string(&table, "Thing"_s); - Intern_String intern2 = intern_string(&table, "Thing"_s); - Intern_String intern3 = intern_string(&table, "Not Thing"_s); - assert(intern1.str == intern2.str); - assert(intern3.str != intern2.str); -} - //----------------------------------------------------------------------------- // Array List // @todo(krzosa): If even one item got removed from block diff --git a/base_string.cpp b/base_string.cpp index 24d7c4d..a54e403 100644 --- a/base_string.cpp +++ b/base_string.cpp @@ -76,7 +76,7 @@ operator==(String a, String b){ } CORE_Static String -string_copy(Arena *a, String string){ +string_copy(Allocator *a, String string){ U8 *copy = allocate_array(a, U8, string.len+1); memory_copy(copy, string.str, string.len); copy[string.len] = 0; @@ -84,7 +84,7 @@ string_copy(Arena *a, String string){ } CORE_Static String -string_fmtv(Arena *a, const char *str, va_list args1) { +string_fmtv(Allocator *a, const char *str, va_list args1) { va_list args2; va_copy(args2, args1); S64 len = stbsp_vsnprintf(0, 0, str, args2); @@ -104,7 +104,7 @@ String result = string_fmtv(alloc, str, args1); \ va_end(args1) CORE_Static String -string_fmt(Arena *a, const char *str, ...) { +string_fmt(Allocator *a, const char *str, ...) { STRING_FMT(a, str, result); return result; } @@ -120,7 +120,7 @@ struct String_Builder_Block{ }; struct String_Builder{ - Arena *allocator; + Allocator *allocator; String_Builder_Block *first_free; String_Builder_Block *first; String_Builder_Block *last; @@ -147,7 +147,7 @@ struct String_Builder{ block = first_free; first_free = first_free->next; } else{ - block = (String_Builder_Block *)arena_push_size(allocator, sizeof(String_Builder_Block) + size); + block = (String_Builder_Block *)allocate_size(allocator, sizeof(String_Builder_Block) + size, false); } memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data block->cap = size; @@ -203,7 +203,7 @@ struct String_Builder{ }; CORE_Static String_Builder -string_builder_make(Arena *a, S64 first_block_size = 4096){ +string_builder_make(Allocator *a, S64 first_block_size = 4096){ String_Builder sb = {a}; sb.init(first_block_size); return sb; @@ -215,7 +215,7 @@ enum String_Builder_Flag{ }; CORE_Static String -string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){ +string_flatten(Allocator *a, String_Builder *b, String_Builder_Flag flags = String_Builder_Flag_None){ // @Note(Krzosa): Compute size to allocate S64 size = 1; if(is_flag_set(flags, String_Builder_Flag_AddSize)) size += sizeof(size_t); @@ -224,7 +224,7 @@ string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_B } String result = {}; - result.str = (U8 *)arena_push_size(a, size); + result.str = (U8 *)allocate_size(a, size, false); if(is_flag_set(flags, String_Builder_Flag_AddSize)) { memory_copy(result.str + result.len, &size, sizeof(S64)); result.len += sizeof(S64); @@ -240,18 +240,6 @@ string_flatten(Arena *a, String_Builder *b, String_Builder_Flag flags = String_B return result; } -CORE_Static void -test_string_builder(){ - Scratch scratch; - String_Builder sb = string_builder_make(scratch, 4); - sb.addf("Thing, %d", 242252); - String f = string_flatten(scratch, &sb); - assert(string_compare(f, "Thing, 242252"_s)); - sb.addf("-%f %f %f", 23.0, 42.29, 2925.2); - f = string_flatten(scratch, &sb); - sb.reset(); -} - //----------------------------------------------------------------------------- // String ops //----------------------------------------------------------------------------- @@ -368,7 +356,7 @@ string_trim_end(String string) { } CORE_Static String -string_to_lower_case(Arena *arena, String s) { +string_to_lower_case(Allocator *arena, String s) { String copy = string_copy(arena, s); for (U64 i = 0; i < copy.len; i++) { copy.str[i] = to_lower_case(copy.str[i]); @@ -377,7 +365,7 @@ string_to_lower_case(Arena *arena, String s) { } CORE_Static String -string_to_upper_case(Arena *arena, String s) { +string_to_upper_case(Allocator *arena, String s) { String copy = string_copy(arena, s); for (U64 i = 0; i < copy.len; i++) { copy.str[i] = to_upper_case(copy.str[i]); @@ -471,14 +459,15 @@ string_from_cstring(char *string){ return result; } +#include "core_arena.cpp" // @! Move this include struct String_Replace { String find; String replace; }; CORE_Static String -string_replace(Arena *arena, String string, Array pairs){ - Scratch scratch(arena); +string_replace(Scratch_Arena *scratch, Allocator *allocator, String string, Array pairs){ + Scratch_Scope _scope(scratch); String_Builder builder = {scratch}; for(int i = 0; i < string.len; i++){ For(pairs){ @@ -493,5 +482,5 @@ string_replace(Arena *arena, String string, Array pairs){ builder.append_data(string.str + i, 1); } - return string_flatten(arena, &builder); + return string_flatten(allocator, &builder); } \ No newline at end of file diff --git a/base_unicode.cpp b/base_unicode.cpp index bd30322..b2b33aa 100644 --- a/base_unicode.cpp +++ b/base_unicode.cpp @@ -272,108 +272,3 @@ string16_copy(Allocator *a, String string){ copy[string.len] = 0; return String{copy, string.len}; } - -CORE_Static void -test_unicode(){ - assert(utf8_to_utf32((U8 *)"A", 1).out_str == 'A'); - assert(utf8_to_utf32((U8 *)"ć", 2).out_str == 0x107); - assert(utf8_to_utf32((U8 *)"ó", 2).out_str == 0x000000f3); - - Scratch scratch; - String32 result = string8_to_string32(scratch, "Thing"_s); - assert(result.len == 5); - assert(result.str[0] == 'T'); - assert(result.str[4] == 'g'); - result = string8_to_string32(scratch, "óźćłŁQ42-=?"_s); - { - U16 u16_code[] = {0x0054, 0x0068, 0x0069, 0x006e, 0x0067, 0x0020, 0x0069, 0x0073}; - String16 thing_is16 = {u16_code, buff_cap(u16_code)}; - String string = "Thing is"_s; - String16 thing_is_convert = string8_to_string16(scratch, string); - assert(string_compare(thing_is16, thing_is_convert)); - - String32 a = string16_to_string32(scratch, thing_is16); - String32 b = string16_to_string32(scratch, thing_is_convert); - String32 c = string8_to_string32 (scratch, string); - assert(string_compare(a, b) && string_compare(b,c)); - } - - { - U16 code[] = {0x015a, 0x0104, 0x00f3, 0x015b, 0x017a, 0x0107, 0x0144, 0x0143, 0x0119, 0x00f3}; - String16 code_str = {code, buff_cap(code)}; - String string = "ŚĄóśźćńŃęó"_s; - String16 convert = string8_to_string16(scratch, string); - assert(string_compare(convert, code_str)); - - String32 a = string16_to_string32(scratch, code_str); - String32 b = string16_to_string32(scratch, convert); - String32 c = string8_to_string32 (scratch, string); - assert(string_compare(a, b) && string_compare(b,c)); - } - - { - String str = " ം ഃ അ ആ ഇ ഈ ഉ ഊ ഋ ഌ എ ഏ ഐ ഒ ഓ ഔ ക ഖ ഗ ഘ ങ ച ഛ ജ ഝ ഞ ട ഠ ഡ ഢ ണ ത ഥ ദ ധ ന പ ഫ ബ ഭ മ യ ര റ ല ള ഴ വ ശ ഷ സ ഹ ാ ി ീ ു ൂ ൃ െ േ ൈ ൊ ോ ൌ ് ൗ ൠ ൡ ൦ ൧ ൨ ൩ ൪ ൫ ൬ ൭ ൮ ൯ "_s; - U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0x0d02, 0x0020, 0x0d03, 0x0020, 0x0d05, 0x0020, 0x0d06, 0x0020, 0x0d07, 0x0020, 0x0d08, 0x0020, 0x0d09, 0x0020, 0x0d0a, 0x0020, 0x0d0b, 0x0020, 0x0d0c, 0x0020, 0x0d0e, 0x0020, 0x0d0f, 0x0020, 0x0d10, 0x0020, 0x0d12, 0x0020, 0x0d13, 0x0020, 0x0d14, 0x0020, 0x0d15, 0x0020, 0x0d16, 0x0020, 0x0d17, 0x0020, 0x0d18, 0x0020, 0x0d19, 0x0020, 0x0d1a, 0x0020, 0x0d1b, 0x0020, 0x0d1c, 0x0020, 0x0d1d, 0x0020, 0x0d1e, 0x0020, 0x0d1f, 0x0020, 0x0d20, 0x0020, 0x0d21, 0x0020, 0x0d22, 0x0020, 0x0d23, 0x0020, 0x0d24, 0x0020, 0x0d25, 0x0020, 0x0d26, 0x0020, 0x0d27, 0x0020, 0x0d28, 0x0020, 0x0d2a, 0x0020, 0x0d2b, 0x0020, 0x0d2c, 0x0020, 0x0d2d, 0x0020, 0x0d2e, 0x0020, 0x0d2f, 0x0020, 0x0d30, 0x0020, 0x0d31, 0x0020, 0x0d32, 0x0020, 0x0d33, 0x0020, 0x0d34, 0x0020, 0x0d35, 0x0020, 0x0d36, 0x0020, 0x0d37, 0x0020, 0x0d38, 0x0020, 0x0d39, 0x0020, 0x0d3e, 0x0020, 0x0d3f, 0x0020, 0x0d40, 0x0020, 0x0d41, 0x0020, 0x0d42, 0x0020, 0x0d43, 0x0020, 0x0d46, 0x0020, 0x0d47, 0x0020, 0x0d48, 0x0020, 0x0d4a, 0x0020, 0x0d4b, 0x0020, 0x0d4c, 0x0020, 0x0d4d, 0x0020, 0x0d57, 0x0020, 0x0d60, 0x0020, 0x0d61, 0x0020, 0x0d66, 0x0020, 0x0d67, 0x0020, 0x0d68, 0x0020, 0x0d69, 0x0020, 0x0d6a, 0x0020, 0x0d6b, 0x0020, 0x0d6c, 0x0020, 0x0d6d, 0x0020, 0x0d6e, 0x0020, 0x0d6f, 0x0020}; - String16 a = {arr, buff_cap(arr)}; - String16 b = string8_to_string16(scratch, str); - assert(string_compare(a,b)); - - String32 c = string16_to_string32(scratch, a); - String32 d = string16_to_string32(scratch, b); - assert(string_compare(c,d)); - - String e = string16_to_string8(scratch, a); - String f = string16_to_string8(scratch, b); - assert(string_compare(e,f)); - } - - { - String str = " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."_s; - U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0xf900, 0x0020, 0xf901, 0x0020, 0xf902, 0x0020, 0xf903, 0x0020, 0xf904, 0x0020, 0xf905, 0x0020, 0xf906, 0x0020, 0xf907, 0x0020, 0xf908, 0x0020, 0xf909, 0x0020, 0xf90a, 0x0020, 0xf90b, 0x0020, 0xf90c, 0x0020, 0xf90d, 0x0020, 0xf90e, 0x0020, 0xf90f, 0x0020, 0xf910, 0x0020, 0xf911, 0x0020, 0xf912, 0x0020, 0xf913, 0x0020, 0xf914, 0x0020, 0xf915, 0x0020, 0xf916, 0x0020, 0xf917, 0x0020, 0xf918, 0x0020, 0xf919, 0x0020, 0xf91a, 0x0020, 0xf91b, 0x0020, 0xf91c, 0x0020, 0xf91d, 0x0020, 0xf91e, 0x0020, 0xf91f, 0x0020, 0xf920, 0x0020, 0xf921, 0x0020, 0xf922, 0x0020, 0xf923, 0x0020, 0xf924, 0x0020, 0xf925, 0x0020, 0xf926, 0x0020, 0xf927, 0x0020, 0xf928, 0x0020, 0xf929, 0x0020, 0xf92a, 0x0020, 0xf92b, 0x0020, 0xf92c, 0x0020, 0xf92d, 0x0020, 0xf92e, 0x0020, 0xf92f, 0x0020, 0xf930, 0x0020, 0xf931, 0x0020, 0xf932, 0x0020, 0xf933, 0x0020, 0xf934, 0x0020, 0xf935, 0x0020, 0xf936, 0x0020, 0xf937, 0x0020, 0xf938, 0x0020, 0xf939, 0x0020, 0xf93a, 0x0020, 0xf93b, 0x0020, 0xf93c, 0x0020, 0xf93d, 0x0020, 0xf93e, 0x0020, 0xf93f, 0x0020, 0xf940, 0x0020, 0xf941, 0x0020, 0xf942, 0x0020, 0xf943, 0x0020, 0xf944, 0x0020, 0xf945, 0x0020, 0xf946, 0x0020, 0xf947, 0x0020, 0xf948, 0x0020, 0xf949, 0x0020, 0xf94a, 0x0020, 0xf94b, 0x0020, 0xf94c, 0x0020, 0xf94d, 0x0020, 0xf94e, 0x0020, 0xf94f, 0x0020, 0xf950, 0x0020, 0xf951, 0x0020, 0xf952, 0x0020, 0xf953, 0x0020, 0xf954, 0x0020, 0xf955, 0x0020, 0xf956, 0x0020, 0xf957, 0x0020, 0xf958, 0x0020, 0xf959, 0x0020, 0xf95a, 0x0020, 0xf95b, 0x0020, 0xf95c, 0x0020, 0xf95d, 0x0020, 0xf95e, 0x0020, 0xf95f, 0x0020, 0xf960, 0x0020, 0xf961, 0x0020, 0xf962, 0x0020, 0xf963, 0x0020, 0xf964, 0x0020, 0xf965, 0x0020, 0xf966, 0x0020, 0xf967, 0x0020, 0xf968, 0x0020, 0xf969, 0x0020, 0xf96a, 0x0020, 0xf96b, 0x0020, 0xf96c, 0x0020, 0xf96d, 0x0020, 0xf96e, 0x0020, 0xf96f, 0x0020, 0xf970, 0x0020, 0xf971, 0x0020, 0xf972, 0x0020, 0xf973, 0x0020, 0xf974, 0x0020, 0xf975, 0x0020, 0xf976, 0x0020, 0xf977, 0x0020, 0xf978, 0x0020, 0xf979, 0x0020, 0xf97a, 0x0020, 0xf97b, 0x0020, 0xf97c, 0x0020, 0xf97d, 0x0020, 0xf97e, 0x0020, 0xf97f, 0x0020, 0x002e, 0x002e, 0x002e}; - String16 a = {arr, buff_cap(arr)}; - String16 b = string8_to_string16(scratch, str); - assert(string_compare(a,b)); - - String32 c = string16_to_string32(scratch, a); - String32 d = string16_to_string32(scratch, b); - assert(string_compare(c,d)); - - String e = string16_to_string8(scratch, a); - String f = string16_to_string8(scratch, b); - assert(string_compare(e,f)); - } - - { - String str = " ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ 。 「 」 、 ・ ヲ ァ ィ ゥ ェ ォ ャ ュ ョ ッ ー ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ ... "_s; - U16 arr[] = {0x0020, 0x0020, 0x0020, 0x0020, 0xff01, 0x0020, 0xff02, 0x0020, 0xff03, 0x0020, 0xff04, 0x0020, 0xff05, 0x0020, 0xff06, 0x0020, 0xff07, 0x0020, 0xff08, 0x0020, 0xff09, 0x0020, 0xff0a, 0x0020, 0xff0b, 0x0020, 0xff0c, 0x0020, 0xff0d, 0x0020, 0xff0e, 0x0020, 0xff0f, 0x0020, 0xff10, 0x0020, 0xff11, 0x0020, 0xff12, 0x0020, 0xff13, 0x0020, 0xff14, 0x0020, 0xff15, 0x0020, 0xff16, 0x0020, 0xff17, 0x0020, 0xff18, 0x0020, 0xff19, 0x0020, 0xff1a, 0x0020, 0xff1b, 0x0020, 0xff1c, 0x0020, 0xff1d, 0x0020, 0xff1e, 0x0020, 0xff1f, 0x0020, 0xff20, 0x0020, 0xff21, 0x0020, 0xff22, 0x0020, 0xff23, 0x0020, 0xff24, 0x0020, 0xff25, 0x0020, 0xff26, 0x0020, 0xff27, 0x0020, 0xff28, 0x0020, 0xff29, 0x0020, 0xff2a, 0x0020, 0xff2b, 0x0020, 0xff2c, 0x0020, 0xff2d, 0x0020, 0xff2e, 0x0020, 0xff2f, 0x0020, 0xff30, 0x0020, 0xff31, 0x0020, 0xff32, 0x0020, 0xff33, 0x0020, 0xff34, 0x0020, 0xff35, 0x0020, 0xff36, 0x0020, 0xff37, 0x0020, 0xff38, 0x0020, 0xff39, 0x0020, 0xff3a, 0x0020, 0xff3b, 0x0020, 0xff3c, 0x0020, 0xff3d, 0x0020, 0xff3e, 0x0020, 0xff3f, 0x0020, 0xff40, 0x0020, 0xff41, 0x0020, 0xff42, 0x0020, 0xff43, 0x0020, 0xff44, 0x0020, 0xff45, 0x0020, 0xff46, 0x0020, 0xff47, 0x0020, 0xff48, 0x0020, 0xff49, 0x0020, 0xff4a, 0x0020, 0xff4b, 0x0020, 0xff4c, 0x0020, 0xff4d, 0x0020, 0xff4e, 0x0020, 0xff4f, 0x0020, 0xff50, 0x0020, 0xff51, 0x0020, 0xff52, 0x0020, 0xff53, 0x0020, 0xff54, 0x0020, 0xff55, 0x0020, 0xff56, 0x0020, 0xff57, 0x0020, 0xff58, 0x0020, 0xff59, 0x0020, 0xff5a, 0x0020, 0xff5b, 0x0020, 0xff5c, 0x0020, 0xff5d, 0x0020, 0xff5e, 0x0020, 0xff61, 0x0020, 0xff62, 0x0020, 0xff63, 0x0020, 0xff64, 0x0020, 0xff65, 0x0020, 0xff66, 0x0020, 0xff67, 0x0020, 0xff68, 0x0020, 0xff69, 0x0020, 0xff6a, 0x0020, 0xff6b, 0x0020, 0xff6c, 0x0020, 0xff6d, 0x0020, 0xff6e, 0x0020, 0xff6f, 0x0020, 0xff70, 0x0020, 0xff71, 0x0020, 0xff72, 0x0020, 0xff73, 0x0020, 0xff74, 0x0020, 0xff75, 0x0020, 0xff76, 0x0020, 0xff77, 0x0020, 0xff78, 0x0020, 0xff79, 0x0020, 0xff7a, 0x0020, 0xff7b, 0x0020, 0xff7c, 0x0020, 0xff7d, 0x0020, 0xff7e, 0x0020, 0xff7f, 0x0020, 0xff80, 0x0020, 0xff81, 0x0020, 0xff82, 0x0020, 0x002e, 0x002e, 0x002e, 0x0020}; - String16 a = {arr, buff_cap(arr)}; - String16 b = string8_to_string16(scratch, str); - assert(string_compare(a,b)); - - String32 c = string16_to_string32(scratch, a); - String32 d = string16_to_string32(scratch, b); - assert(string_compare(c,d)); - - String e = string16_to_string8(scratch, a); - String f = string16_to_string8(scratch, b); - assert(string_compare(e,f)); - } - - { // With surrogate pairs - U8 arr8[] = {0xf0, 0x90, 0x90, 0x90, 0xf0, 0x90, 0x90, 0x91}; - String str = {arr8, buff_cap(arr8)}; - U16 arr[] = {0xd801, 0xdc10, 0xd801, 0xdc11}; - String16 a = {arr, buff_cap(arr)}; - String16 b = string8_to_string16(scratch, str); - assert(string_compare(a,b)); - - String32 c = string16_to_string32(scratch, a); - String32 d = string16_to_string32(scratch, b); - assert(string_compare(c,d)); - - String e = string16_to_string8(scratch, a); - String f = string16_to_string8(scratch, b); - assert(string_compare(e,f)); - } - -} diff --git a/c3_big_int.cpp b/c3_big_int.cpp index 66f457d..115067f 100644 --- a/c3_big_int.cpp +++ b/c3_big_int.cpp @@ -5,8 +5,8 @@ #define Set_BigInt_Arena(x) BigInt_Arena bigint_allocator(x) struct BigInt_Arena{ - Arena *old; - BigInt_Arena(Arena *allocator){old = bigint_allocator; bigint_allocator = allocator;} + Allocator *old; + BigInt_Arena(Allocator *allocator){old = bigint_allocator; bigint_allocator = allocator;} ~BigInt_Arena(){bigint_allocator = old;} }; @@ -39,7 +39,7 @@ bigint_add(const BigInt *a, const BigInt *b){ } CORE_Static BigInt -bigint_copy(Arena *allocator, BigInt *src){ +bigint_copy(Allocator *allocator, BigInt *src){ BigInt dest = {}; if (src->digit_count == 0){ bigint_init_unsigned(&dest, 0); @@ -54,7 +54,6 @@ bigint_copy(Arena *allocator, BigInt *src){ dest.is_negative = src->is_negative; dest.digit_count = src->digit_count; - count_bigint_alloc(); dest.digits = allocate_array(allocator, uint64_t, dest.digit_count); memcpy(dest.digits, src->digits, sizeof(uint64_t) * dest.digit_count); return dest; @@ -1925,7 +1924,7 @@ void bigint_print(BigInt *bigint, uint64_t base) } } -const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint64_t base) +const char *bigint_to_error_string(Allocator *allocator, const BigInt *bigint, uint64_t base) { Set_BigInt_Arena(allocator); if (bigint->digit_count == 0) @@ -1948,7 +1947,7 @@ const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint6 return res; } size_t len = bigint->digit_count * 64; - char *start = (char *)arena_push_size(allocator, len); + char *start = (char *)allocate_size(allocator, len); char *buf = start; BigInt digit_bi = { 0 }; @@ -1980,7 +1979,7 @@ const char *bigint_to_error_string(Arena *allocator, const BigInt *bigint, uint6 } // reverse - char *out = (char *)arena_push_size(allocator, buf - start + 2); + char *out = (char *)allocate_size(allocator, buf - start + 2); char *current = out; if (bigint->is_negative) { diff --git a/c3_big_int.h b/c3_big_int.h index 18d7bf9..013666e 100644 --- a/c3_big_int.h +++ b/c3_big_int.h @@ -8,8 +8,7 @@ enum CmpRes CMP_EQ, }; -#define count_bigint_alloc() (bigint_allocator != thread_ctx.scratch ? bigint_allocation_count++ : 0) -#define malloc_arena(x) (count_bigint_alloc(), allocate_size(bigint_allocator, x)) +#define malloc_arena(x) allocate_size(bigint_allocator, x) #define ALLOC_DIGITS(_digits) (uint64_t *)((_digits) ? malloc_arena(sizeof(uint64_t) * (_digits)) : NULL) #define FATAL_ERROR(x) compiler_error(0, x) diff --git a/core_arena.cpp b/core_arena.cpp index ac362cf..614264f 100644 --- a/core_arena.cpp +++ b/core_arena.cpp @@ -31,6 +31,15 @@ static void *scratch_arena_push_size(Scratch_Arena *arena, size_t size) { return (void *)result; } +static Scratch_Arena *make_scratch_arena(void *buff, size_t size) { + Scratch_Arena *scratch = (Scratch_Arena *)buff; + scratch->len = 0; + scratch->cap = size - sizeof(Scratch_Arena); + scratch->allocate = (Allocator::Allocate *)scratch_arena_push_size; + scratch->deallocate = deallocate_stub; + return scratch; +} + static Scratch_Arena *allocate_scratch_arena(Allocator *allocator, size_t size_without_members) { Scratch_Arena *scratch = (Scratch_Arena *)allocate_size(allocator, size_without_members + sizeof(Scratch_Arena), false); scratch->cap = size_without_members; diff --git a/core_ast.cpp b/core_ast.cpp index 13ba4cf..a5b34f4 100644 --- a/core_ast.cpp +++ b/core_ast.cpp @@ -178,7 +178,7 @@ ast_array(Token *pos, Ast_Expr *expr){ } CORE_Static Ast_Scope * -begin_decl_scope(Arena *scratch, Token *pos){ +begin_decl_scope(Allocator *scratch, Token *pos){ AST_NEW(Scope, SCOPE, pos, AST_DECL); result->file = pctx->currently_parsed_file; result->module = pctx->currently_parsed_file->module; @@ -195,7 +195,7 @@ finalize_decl_scope(Ast_Scope *scope){ } CORE_Static Ast_Scope * -begin_stmt_scope(Arena *scratch, Token *pos){ +begin_stmt_scope(Allocator *scratch, Token *pos){ AST_NEW(Scope, SCOPE, pos, AST_STMT); result->stmts = {scratch}; result->file = pctx->currently_parsed_file; @@ -263,7 +263,7 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){ } CORE_Static Ast_Scope * -ast_decl_scope(Token *pos, Arena *allocator, Ast_File *file){ +ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){ AST_NEW(Scope, SCOPE, pos, AST_DECL); result->file = file; diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 3b5306b..f71474d 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -34,7 +34,7 @@ gen_last_line(){ } CORE_Static String -string_scope_name(Arena *a, Ast_Scope *scope){ +string_scope_name(Allocator *a, Ast_Scope *scope){ String string = {}; if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope); assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id"); @@ -44,17 +44,17 @@ string_scope_name(Arena *a, Ast_Scope *scope){ CORE_Static void gen_scope_name(Ast_Scope *scope){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); String string = string_scope_name(scratch, scope); gen("%.*s", (int)string.len, string.str); } CORE_Static String -unique_name(Arena *allocator, Ast *ast){ - Scratch scratch(allocator); +unique_name_scratch(Allocator *scratch, Ast *ast){ String result = string_scope_name(scratch, ast->parent_scope); assert(result.len); - result = string_fmt(allocator, "%Q%d", result, ast->pos->line); + result = string_fmt(scratch, "%Q%d", result, ast->pos->line); return result; } @@ -87,7 +87,7 @@ get_ctype_name_for_type(Ast_Type *type){ } CORE_Static String -string_simple_decl_prefix(Arena *a, Ast_Type *ast){ +string_simple_decl_prefix(Allocator *a, Ast_Type *ast){ switch(ast->kind){ case TYPE_POINTER:{ String string = string_simple_decl_prefix(a, ast->base); @@ -124,7 +124,7 @@ string_simple_decl_prefix(Arena *a, Ast_Type *ast){ } CORE_Static String -string_simple_decl_postfix(Arena *a, Ast_Type *ast){ +string_simple_decl_postfix(Allocator *a, Ast_Type *ast){ switch(ast->kind){ case TYPE_POINTER: return string_simple_decl_postfix(a, ast->base); @@ -143,7 +143,7 @@ string_simple_decl_postfix(Arena *a, Ast_Type *ast){ } CORE_Static String -string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){ +string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}){ if(ast->kind == TYPE_LAMBDA) { String prefix = string_simple_decl_prefix(a, ast->func.ret); String string = string_fmt(a, "%Q(*%Q)(", prefix, name); @@ -169,19 +169,13 @@ string_simple_decl(Arena *a, Ast_Type *ast, Intern_String name = {}){ CORE_Static void gen_simple_decl(Ast_Type *ast, Intern_String name = {}){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + String string = string_simple_decl(scratch, ast, name); gen("%.*s", (int)string.len, string.str); } -CORE_Static String -gen_string_simple_decl(Arena *a, Ast_Type *ast, String name){ - Scratch scratch; - String string = string_simple_decl(scratch, ast, pctx->intern(name)); - String result = string_copy(a, string); - return result; -} - CORE_Static String get_type_postfix(Ast_Type *type){ switch(type->kind) { @@ -211,7 +205,9 @@ gen_value(Token *pos, Value a){ switch(type->kind){ CASE_INT: { - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + String postfix = get_type_postfix(type); const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); gen("%s%Q", string, postfix); @@ -535,8 +531,10 @@ gen_ast(Ast *ast){ CASE(RETURN, Return){ if(is_tuple(node->resolved_type)) { - Scratch scratch; - Intern_String var_name = pctx->intern(unique_name(scratch, node)); + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + + Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node)); gen_simple_decl(node->resolved_type, var_name); gen(";"); @@ -744,8 +742,10 @@ gen_ast(Ast *ast){ For(node->vars) gen_ast(it); - Scratch scratch; - Intern_String var_name = pctx->intern(unique_name(scratch, node)); + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + + Intern_String var_name = pctx->intern(unique_name_scratch(scratch, node)); gen_simple_decl(node->resolved_type, var_name); gen(" = "); gen_expr(node->expr); @@ -845,7 +845,9 @@ typedef struct String{ // Generate slice and tuple types Iter(&pctx->all_types){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + Ast_Type *type = it.item[0]; if(type->kind == TYPE_SLICE){ diff --git a/core_compiler.cpp b/core_compiler.cpp index 314c551..ab222cd 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -138,7 +138,8 @@ parse_all_modules() { CORE_Static Ast_Module * add_module(Token *pos, Intern_String filename, B32 command_line_module) { - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); String absolute_file_path = {}; String absolute_base_folder = {}; @@ -292,14 +293,17 @@ const U32 COMPILE_TESTING = 0x8; CORE_Static void compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) { + String result = compile_file_to_string(arena, filename); if (is_flag_set(compile_flags, COMPILE_AND_RUN)) { log_info_no_nl("%Q - ", filename); } - String result = compile_file_to_string(arena, filename); - assert(os_write_file("program.c"_s, result)); + B32 r = os_write_file("program.c"_s, result); + assert(r); + + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); - Scratch scratch; F64 begin = os_time(); String_Builder builder = {scratch}; builder.addf("clang program.c -Wall -Wno-unused-function -Wno-parentheses-equality -g -o a" OS_EXE " "); @@ -321,12 +325,13 @@ compile_file(Arena *arena, String filename, U32 compile_flags = COMPILE_NULL) { } if (is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)) { - Arena *p = (Arena *)pctx->perm; - Arena *stage = (Arena *)&pctx->stage_arena; - log_info("Pernament arena len: %llu commit: %llu", p->len, p->memory.commit); - log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit); - log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); - log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); + // @! allocator stats + //Arena *p = (Arena *)pctx->perm; + //Arena *stage = (Arena *)&pctx->stage_arena; + //log_info("Pernament arena len: %llu commit: %llu", pct, p->memory.commit); + //log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit); + //log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); + //log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); } if (is_flag_set(compile_flags, COMPILE_AND_RUN)) { diff --git a/core_globals.cpp b/core_globals.cpp index a807ff5..df25a16 100644 --- a/core_globals.cpp +++ b/core_globals.cpp @@ -7,8 +7,7 @@ global bool color_codes_enabled; thread_local Core_Ctx *pctx; - -Arena *bigint_allocator; +Allocator *bigint_allocator; global S64 bigint_allocation_count; global Token token_null = {SAME_SCOPE}; diff --git a/core_lexing.cpp b/core_lexing.cpp index 09d6e8d..e4c769f 100644 --- a/core_lexing.cpp +++ b/core_lexing.cpp @@ -80,8 +80,8 @@ token_error(Token *t, String error_val){ CORE_Static void lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){ - Scratch scratch; - Set_BigInt_Arena(scratch); + Scratch_Scope _scope(lexer->scratch); + Set_BigInt_Arena(lexer->scratch); t->kind = TK_Integer; BigInt m = bigint_u64(1); @@ -592,8 +592,8 @@ lex_restream(Core_Ctx *lexer, String istream, String file){ lexer->stream.line_begin = istream.str; lexer->stream.file = lexer->intern(file); - Scratch scratch; - lexer->stream.indent_stack.allocator = scratch; + Scratch_Scope _scope(lexer->scratch); + lexer->stream.indent_stack.allocator = lexer->scratch; lexer->stream.indent_stack.add(&token_null); lex__stream(lexer); } diff --git a/core_main.cpp b/core_main.cpp index 9f74d1a..e89601b 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -259,7 +259,6 @@ For modules it's a bit different cause they should be distributed as valid. #include "base.cpp" #include "base_unicode.cpp" -#include "core_arena.cpp" #include "os.h" #if OS_WINDOWS #include "os_windows.cpp" @@ -287,13 +286,11 @@ For modules it's a bit different cause they should be distributed as valid. int main(int argument_count, char **arguments){ - thread_ctx_init(); - Arena arena = {}; arena_init(&arena, "Pernament arena"_s); + Scratch_Arena *scratch_arena = allocate_scratch_arena(&arena, mib(1)); - Scratch scratch; - Array args = {scratch}; + Array args = {scratch_arena}; for(int i = 1; i < argument_count; i+=1){ String arg = string_from_cstring(arguments[i]); args.add(arg); @@ -332,20 +329,14 @@ int main(int argument_count, char **arguments){ test_os_memory(); #endif - test_unicode(); - - map_test(); - test_string_builder(); - test_intern_table(); - // emit_line_directives = false; // emit_type_info = false; For(args){ if(it == "-testing"_s){ // @copy_paste - Scratch scratch; - Array examples = os_list_dir(scratch, "examples"_s); - Array tests = os_list_dir(scratch, "tests"_s); + Scratch_Scope _scope(scratch_arena); + Array examples = os_list_dir(scratch_arena, scratch_arena, "examples"_s); + Array tests = os_list_dir(scratch_arena, scratch_arena, "tests"_s); For(examples){ if(it.is_directory) continue; compile_file(&arena, it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); diff --git a/core_parsing.cpp b/core_parsing.cpp index c70514b..fac010d 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -1,5 +1,25 @@ CORE_Static Ast_Decl *parse_decl(B32 is_global); +enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; +typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); + +#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__) +#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__) +#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__) +#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__) +CORE_Static void + handle_log_message(Log_Kind kind, int line, const char *file, const char *str, ...){ + if(kind == Log_Kind_Trace) return; + + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + STRING_FMT(scratch, str, message); + printf("%s", message.str); + if(kind != Log_Kind_Normal_No_NewLine){ + printf("\n"); + } +} + CORE_Static void print_token_line(Token *token){ if(!token) return; @@ -37,8 +57,7 @@ print_token_context(Token *token){ CORE_Static void compiler_error(Token *token1, Token *token2, const char *str, ...){ - Scratch scratch; - STRING_FMT(scratch, str, string); + STRING_FMT(pctx->perm, str, string); log_info_no_nl("\n%s", string.str); if(token1){ @@ -64,8 +83,7 @@ compiler_error(Token *token1, Token *token2, const char *str, ...){ CORE_Static void compiler_error(Token *token, const char *str, ...){ - Scratch scratch; - STRING_FMT(scratch, str, string); + STRING_FMT(pctx->perm, str, string); if(token) log_info_no_nl("\n%s:%d %Q", token->file.str, (S32)token->line + 1, string); else log_info_no_nl("\n%s", string.str); @@ -205,7 +223,8 @@ parse_init_stmt(Ast_Expr *expr){ CORE_Static Ast_Call * parse_expr_call(Ast_Expr *left, Token_Kind close_kind){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); Token *pos = token_get(); Array exprs = {scratch}; @@ -257,7 +276,8 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){ if(token_expect(OPEN_SCOPE)){ // @todo: Fix error message here, it doesn't show proper token context Token *token_block = token_get(); - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); if(!scope_defined_outside) scope = begin_stmt_scope(scratch, token_block); do{ Token *token = token_get(); @@ -441,7 +461,8 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){ CORE_Static Ast_Lambda * parse_lambda(Token *token){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); Array params = {scratch}; if(!token_is(TK_CloseParen)){ @@ -668,7 +689,8 @@ parse_assign_expr(){ CORE_Static Ast_Decl * parse_struct(Token *pos){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); token_match(OPEN_SCOPE); Ast_Scope *scope = begin_decl_scope(scratch, token_get()); @@ -692,7 +714,8 @@ parse_struct(Token *pos){ CORE_Static Ast_Decl * parse_enum(Token *pos){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); Ast_Expr *typespec = parse_optional_type(); Token *flag = token_match_pound(intern_flag); @@ -772,12 +795,12 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32 CORE_Static Intern_String preprocess_filename(Token *token_filename){ - Scratch scratch; + Scratch_Scope _scope(pctx->scratch); String filename = token_filename->intern_val.s; - Array replace = {scratch}; + Array replace = {pctx->scratch}; replace.add({"$OS"_s, OS_NAME}); replace.add({"$os"_s, OS_NAME_LOWER}); - String result0 = string_replace(scratch, filename, replace); + String result0 = string_replace(pctx->scratch, pctx->scratch, filename, replace); Intern_String result = pctx->intern(result0); return result; } @@ -918,7 +941,8 @@ CORE_Static void parse_file(Ast_File *file){ assert(file); - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); file->filecontent = os_read_file(pctx->perm, file->absolute_file_path); if(file->filecontent.len == 0){ compiler_error(file->pos, "Failed to open file \"%Q\"", file->absolute_file_path); diff --git a/core_typechecking.cpp b/core_typechecking.cpp index 7cdafd0..e1c5c28 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -114,7 +114,8 @@ CORE_Static void check_value_bounds(Token *pos, Value *a){ if(!is_int(a->type)) return; - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); if(!bigint_fits_in_bits(&a->big_int_val, a->type->size*8, is_signed_int(a->type))){ const char *string = bigint_to_error_string(scratch, &a->big_int_val, 10); compiler_error(pos, "Value %s doesn't fit in type %Q", string, typestring(a->type)); @@ -489,7 +490,7 @@ scope_search(Scope_Search *search){ } CORE_Static Scope_Search -make_scope_search(Arena *arena, Ast_Scope *scope, Intern_String name){ +make_scope_search(Allocator *arena, Ast_Scope *scope, Intern_String name){ Scope_Search result = {}; result.results.allocator = arena; result.name = name; @@ -502,7 +503,9 @@ make_scope_search(Arena *arena, Ast_Scope *scope, Intern_String name){ CORE_Static Ast_Decl * search_for_single_decl(Ast_Scope *scope, Intern_String name){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + Scope_Search search = make_scope_search(scratch, scope, name); scope_search(&search); @@ -518,7 +521,8 @@ search_for_single_decl(Ast_Scope *scope, Intern_String name){ CORE_Static Ast_Decl * resolve_name(Ast_Scope *scope, Token *pos, Intern_String name, Search_Flag search_flags){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Scope_Search search = make_scope_search(scratch, scope, name); search.search_only_current_scope = search_flags & SEARCH_ONLY_CURRENT_SCOPE; scope_search(&search); @@ -551,7 +555,8 @@ resolve_operator_overload(Ast_Scope *scope, Ast_Type *left, Ast_Type *right, Tok // Search for all possible candidates in three scopes // The current module, left type definition module, right type definition module - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Scope_Search search = make_scope_search(scratch, scope, op_info->op); if( left->ast && left->ast->parent_scope) search.scopes.add(left->ast->parent_scope); if(right && right->ast && right->ast->parent_scope) search.scopes.add(right->ast->parent_scope); @@ -585,7 +590,8 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ // // It's also called when scanning top level declarations of a module // as such we probably don't want to call any resolve stuff here - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Scope_Search search = make_scope_search(scratch, scope, decl->name); search.search_only_current_scope = true; scope_search(&search); @@ -670,7 +676,8 @@ resolve_typespec(Ast_Expr *ast, Resolve_Flag flags){ if(!ast && is_flag_set(flags, AST_CAN_BE_NULL)) return 0; - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Operand resolved = resolve_expr(ast, flags | RESOLVE_TYPESPEC, 0, 0); if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE)) type_complete(resolved.type_val); @@ -687,7 +694,8 @@ resolve_and_require_bool(const char *error, Ast_Expr *expr, Resolve_Flag flags){ else compiler_error(0, "Compiler error: Null expression"); } - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Operand op = resolve_expr(expr, flags, 0, 0); if(!is_bool(op.type)){ compiler_error(expr->pos, "Expected type [Bool] got instead type %Q :: %s", typestring(op.type), error); @@ -723,7 +731,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ switch(ast->kind){ CASE(RETURN, Return){ // @todo: need to check if all paths return a value - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Array types = {scratch}; For_It(node->expr){ @@ -885,7 +894,8 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ CORE_Static Ast_Type * resolve_lambda_type(Ast_Lambda *lambda){ - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Array args = {scratch}; Array ret = {scratch}; For(lambda->ret) { @@ -1508,7 +1518,8 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str } node->resolved_decl = name.resolved_decl; - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); Array items = {scratch}; S64 default_iter = 0; @@ -1631,7 +1642,9 @@ resolve_decl(Ast_Decl *ast){ try_resolving_lambda_scope(&result, lambda, node->type); node->value = result.value; - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope _scope(scratch); + node->unique_name = node->name; if(!is_flag_set(node->expr->flags, AST_FOREIGN)){ node->unique_name = pctx->intern(string_fmt(scratch, "%Q%Q%d", symbol_prefix, node->name, pctx->lambda_ids++)); diff --git a/core_typechecking.h b/core_typechecking.h index 5e67f36..5520b44 100644 --- a/core_typechecking.h +++ b/core_typechecking.h @@ -60,6 +60,5 @@ enum{ CORE_Static Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope); CORE_Static void resolve_decl(Ast_Decl *ast); CORE_Static Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0); -CORE_Static String gen_string_simple_decl(Arena *a, Ast_Type *ast, String name = {}, Ast_Scope *scope = 0, bool scope_names = true); #define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast; #define BREAK() } break diff --git a/core_types.cpp b/core_types.cpp index a39a29d..c3966d3 100644 --- a/core_types.cpp +++ b/core_types.cpp @@ -226,7 +226,9 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){ assert(node->kind == AST_STRUCT); // @todo: compute size, alignement, offset !!! // @note: resolve all the struct members first - Scratch scratch; + Scratch_Arena *scratch = pctx->scratch; + Scratch_Scope __scope(scratch); + Array members = {scratch}; type->kind = TYPE_COMPLETING; size_t members_size = 0; diff --git a/os_linux.cpp b/os_linux.cpp index 4235475..79b6727 100644 --- a/os_linux.cpp +++ b/os_linux.cpp @@ -21,14 +21,14 @@ os_write_file(String filename, String filecontent){ } CORE_Static String -os_read_file(Arena *a, String name){ +os_read_file(Alloator *a, String name){ String result = {0}; FILE *f = fopen((char *)name.str, "rb"); if(f){ fseek(f, 0, SEEK_END); result.len = ftell(f); fseek(f, 0, SEEK_SET); - result.str = (U8 *)arena_push_size(a, result.len + 1); + result.str = (U8 *)allocate_size(a, result.len + 1, false); fread(result.str, result.len, 1, f); fclose(f); result.str[result.len] = 0; @@ -38,7 +38,7 @@ os_read_file(Arena *a, String name){ } CORE_Static String -os_get_exe_dir(Arena *a){ +os_get_exe_dir(Allocator *a){ char buffer[PATH_MAX] = {}; if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) { log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported"); @@ -52,7 +52,7 @@ os_get_exe_dir(Arena *a){ } CORE_Static String -os_get_absolute_path(Arena *a, String path){ +os_get_absolute_path(Allocator *a, String path){ assert(path.str[path.len] == 0); char buffer[PATH_MAX] = {}; realpath((char *)path.str, buffer); @@ -73,15 +73,15 @@ os_does_file_exist(String path){ } CORE_Static String -os_get_working_dir(Arena *allocator){ - char *buffer = arena_push_array(allocator, char, PATH_MAX); +os_get_working_dir(Allocator *allocator){ + char *buffer = allocate_array(allocator, char, PATH_MAX, false); char *result = getcwd(buffer, PATH_MAX); return string_from_cstring(result); } CORE_Static Array -os_list_dir(Arena *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ - Scratch scratch(a); +os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ + Scratch_Scope _scope(scratch); Array dirs_to_read = {scratch}; dirs_to_read.add(dir); diff --git a/os_windows.cpp b/os_windows.cpp index 6d7a315..b5d91c6 100644 --- a/os_windows.cpp +++ b/os_windows.cpp @@ -115,14 +115,14 @@ os_write_file(String filename, String filecontent){ } CORE_Static String -os_read_file(Arena *a, String name){ +os_read_file(Allocator *a, String name){ String result = {0}; FILE *f = fopen((char *)name.str, "rb"); if(f){ fseek(f, 0, SEEK_END); result.len = ftell(f); fseek(f, 0, SEEK_SET); - result.str = (U8 *)arena_push_size(a, result.len + 1); + result.str = (U8 *)allocate_size(a, result.len + 1, false); fread(result.str, result.len, 1, f); fclose(f); result.str[result.len] = 0; @@ -132,7 +132,7 @@ os_read_file(Arena *a, String name){ } CORE_Static String -os_get_working_dir(Arena *a){ +os_get_working_dir(Allocator *a){ wchar_t buffer[2048]; DWORD written = GetCurrentDirectoryW(2048, buffer); assert(written != 0); @@ -143,7 +143,7 @@ os_get_working_dir(Arena *a){ } CORE_Static String -os_get_exe_dir(Arena *a){ +os_get_exe_dir(Allocator *a){ wchar_t buffer[2048]; DWORD written = GetModuleFileNameW(0, buffer, 2048); assert(written != 0); @@ -158,12 +158,13 @@ os_get_exe_dir(Arena *a){ } CORE_Static String -os_get_absolute_path(Arena *a, String path){ - Scratch scratch(a); +os_get_absolute_path(Allocator *a, String path){ + char buff[2048]; + Scratch_Arena *scratch = make_scratch_arena(buff, 2048); String16 path16 = string8_to_string16(scratch, path); - wchar_t *buffer = allocate_array(scratch, wchar_t, 2048); - DWORD written = GetFullPathNameW((wchar_t *)path16.str, 2048, buffer, 0); + wchar_t *buffer = allocate_array(scratch, wchar_t, 512); + DWORD written = GetFullPathNameW((wchar_t *)path16.str, 512, buffer, 0); if(written == 0) return {}; String16 absolute16 = string16_from_widechar(buffer); @@ -174,7 +175,8 @@ os_get_absolute_path(Arena *a, String path){ CORE_Static B32 os_does_file_exist(String path){ - Scratch scratch; + char buff[2048]; + Scratch_Arena *scratch = make_scratch_arena(buff, buff_cap(buff)); String16 path16 = string8_to_string16(scratch, path); DWORD attribs = GetFileAttributesW((wchar_t *)path16.str); B32 result = attribs == INVALID_FILE_ATTRIBUTES ? false : true; @@ -182,8 +184,8 @@ os_does_file_exist(String path){ } CORE_Static Array -os_list_dir(Arena *a, String dir, U32 flags = LIST_NO_FLAGS){ - Scratch scratch(a); +os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ + Scratch_Scope _scope(scratch); Array dirs_to_read = {scratch}; dirs_to_read.add(dir);