core.odin 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // This is the runtime code required by the compiler
  2. // IMPORTANT NOTE(bill): Do not change the order of any of this data
  3. // The compiler relies upon this _exact_ order
  4. package runtime
  5. import "core:os"
  6. import "core:mem"
  7. import "core:log"
  8. import "intrinsics"
  9. // Naming Conventions:
  10. // In general, Ada_Case for types and snake_case for values
  11. //
  12. // Package Name: snake_case (but prefer single word)
  13. // Import Name: snake_case (but prefer single word)
  14. // Types: Ada_Case
  15. // Enum Values: Ada_Case
  16. // Procedures: snake_case
  17. // Local Variables: snake_case
  18. // Constant Variables: SCREAMING_SNAKE_CASE
  19. // IMPORTANT NOTE(bill): `type_info_of` cannot be used within a
  20. // #shared_global_scope due to the internals of the compiler.
  21. // This could change at a later date if the all these data structures are
  22. // implemented within the compiler rather than in this "preload" file
  23. // NOTE(bill): This must match the compiler's
  24. Calling_Convention :: enum {
  25. Invalid = 0,
  26. Odin = 1,
  27. Contextless = 2,
  28. C = 3,
  29. Std = 4,
  30. Fast = 5,
  31. }
  32. Type_Info_Enum_Value :: union {
  33. rune,
  34. i8, i16, i32, i64, int,
  35. u8, u16, u32, u64, uint, uintptr,
  36. };
  37. Platform_Endianness :: enum u8 {
  38. Platform = 0,
  39. Little = 1,
  40. Big = 2,
  41. }
  42. // Variant Types
  43. Type_Info_Named :: struct {name: string, base: ^Type_Info};
  44. Type_Info_Integer :: struct {signed: bool, endianness: Platform_Endianness};
  45. Type_Info_Rune :: struct {};
  46. Type_Info_Float :: struct {};
  47. Type_Info_Complex :: struct {};
  48. Type_Info_Quaternion :: struct {};
  49. Type_Info_String :: struct {is_cstring: bool};
  50. Type_Info_Boolean :: struct {};
  51. Type_Info_Any :: struct {};
  52. Type_Info_Type_Id :: struct {};
  53. Type_Info_Pointer :: struct {
  54. elem: ^Type_Info // nil -> rawptr
  55. };
  56. Type_Info_Procedure :: struct {
  57. params: ^Type_Info, // Type_Info_Tuple
  58. results: ^Type_Info, // Type_Info_Tuple
  59. variadic: bool,
  60. convention: Calling_Convention,
  61. };
  62. Type_Info_Array :: struct {
  63. elem: ^Type_Info,
  64. elem_size: int,
  65. count: int,
  66. };
  67. Type_Info_Dynamic_Array :: struct {elem: ^Type_Info, elem_size: int};
  68. Type_Info_Slice :: struct {elem: ^Type_Info, elem_size: int};
  69. Type_Info_Tuple :: struct { // Only really used for procedures
  70. types: []^Type_Info,
  71. names: []string,
  72. };
  73. Type_Info_Struct :: struct {
  74. types: []^Type_Info,
  75. names: []string,
  76. offsets: []uintptr,
  77. usings: []bool,
  78. tags: []string,
  79. is_packed: bool,
  80. is_raw_union: bool,
  81. custom_align: bool,
  82. // These are only set iff this structure is an SOA structure
  83. soa_base_type: ^Type_Info,
  84. soa_len: int,
  85. };
  86. Type_Info_Union :: struct {
  87. variants: []^Type_Info,
  88. tag_offset: uintptr,
  89. tag_type: ^Type_Info,
  90. custom_align: bool,
  91. no_nil: bool,
  92. };
  93. Type_Info_Enum :: struct {
  94. base: ^Type_Info,
  95. names: []string,
  96. values: []Type_Info_Enum_Value,
  97. };
  98. Type_Info_Map :: struct {
  99. key: ^Type_Info,
  100. value: ^Type_Info,
  101. generated_struct: ^Type_Info,
  102. };
  103. Type_Info_Bit_Field :: struct {
  104. names: []string,
  105. bits: []i32,
  106. offsets: []i32,
  107. };
  108. Type_Info_Bit_Set :: struct {
  109. elem: ^Type_Info,
  110. underlying: ^Type_Info, // Possibly nil
  111. lower: i64,
  112. upper: i64,
  113. };
  114. Type_Info_Opaque :: struct {
  115. elem: ^Type_Info,
  116. };
  117. Type_Info_Simd_Vector :: struct {
  118. elem: ^Type_Info,
  119. elem_size: int,
  120. count: int,
  121. is_x86_mmx: bool,
  122. }
  123. Type_Info :: struct {
  124. size: int,
  125. align: int,
  126. id: typeid,
  127. variant: union {
  128. Type_Info_Named,
  129. Type_Info_Integer,
  130. Type_Info_Rune,
  131. Type_Info_Float,
  132. Type_Info_Complex,
  133. Type_Info_Quaternion,
  134. Type_Info_String,
  135. Type_Info_Boolean,
  136. Type_Info_Any,
  137. Type_Info_Type_Id,
  138. Type_Info_Pointer,
  139. Type_Info_Procedure,
  140. Type_Info_Array,
  141. Type_Info_Dynamic_Array,
  142. Type_Info_Slice,
  143. Type_Info_Tuple,
  144. Type_Info_Struct,
  145. Type_Info_Union,
  146. Type_Info_Enum,
  147. Type_Info_Map,
  148. Type_Info_Bit_Field,
  149. Type_Info_Bit_Set,
  150. Type_Info_Opaque,
  151. Type_Info_Simd_Vector,
  152. },
  153. }
  154. // NOTE(bill): This must match the compiler's
  155. Typeid_Kind :: enum u8 {
  156. Invalid,
  157. Integer,
  158. Rune,
  159. Float,
  160. Complex,
  161. Quaternion,
  162. String,
  163. Boolean,
  164. Any,
  165. Type_Id,
  166. Pointer,
  167. Procedure,
  168. Array,
  169. Dynamic_Array,
  170. Slice,
  171. Tuple,
  172. Struct,
  173. Union,
  174. Enum,
  175. Map,
  176. Bit_Field,
  177. Bit_Set,
  178. Opaque,
  179. }
  180. #assert(len(Typeid_Kind) < 32);
  181. Typeid_Bit_Field :: bit_field #align align_of(uintptr) {
  182. index: 8*size_of(uintptr) - 8,
  183. kind: 5, // Typeid_Kind
  184. named: 1,
  185. special: 1, // signed, cstring, etc
  186. reserved: 1,
  187. }
  188. #assert(size_of(Typeid_Bit_Field) == size_of(uintptr));
  189. // NOTE(bill): only the ones that are needed (not all types)
  190. // This will be set by the compiler
  191. type_table: []Type_Info;
  192. args__: []cstring;
  193. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  194. Source_Code_Location :: struct {
  195. file_path: string,
  196. line, column: int,
  197. procedure: string,
  198. hash: u64,
  199. }
  200. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location);
  201. Context :: struct {
  202. allocator: mem.Allocator,
  203. temp_allocator: mem.Allocator,
  204. assertion_failure_proc: Assertion_Failure_Proc,
  205. logger: log.Logger,
  206. stdin: os.Handle,
  207. stdout: os.Handle,
  208. stderr: os.Handle,
  209. thread_id: int,
  210. user_data: any,
  211. user_ptr: rawptr,
  212. user_index: int,
  213. derived: any, // May be used for derived data types
  214. }
  215. global_scratch_allocator_data: mem.Scratch_Allocator;
  216. Raw_Slice :: struct {
  217. data: rawptr,
  218. len: int,
  219. }
  220. Raw_Dynamic_Array :: struct {
  221. data: rawptr,
  222. len: int,
  223. cap: int,
  224. allocator: mem.Allocator,
  225. }
  226. Raw_Map :: struct {
  227. hashes: []int,
  228. entries: Raw_Dynamic_Array,
  229. }
  230. INITIAL_MAP_CAP :: 16;
  231. Map_Key :: struct {
  232. hash: u64,
  233. str: string,
  234. }
  235. Map_Find_Result :: struct {
  236. hash_index: int,
  237. entry_prev: int,
  238. entry_index: int,
  239. }
  240. Map_Entry_Header :: struct {
  241. key: Map_Key,
  242. next: int,
  243. /*
  244. value: Value_Type,
  245. */
  246. }
  247. Map_Header :: struct {
  248. m: ^Raw_Map,
  249. is_key_string: bool,
  250. entry_size: int,
  251. entry_align: int,
  252. value_offset: uintptr,
  253. value_size: int,
  254. }
  255. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  256. if info == nil do return nil;
  257. base := info;
  258. loop: for {
  259. switch i in base.variant {
  260. case Type_Info_Named: base = i.base;
  261. case: break loop;
  262. }
  263. }
  264. return base;
  265. }
  266. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  267. if info == nil do return nil;
  268. base := info;
  269. loop: for {
  270. switch i in base.variant {
  271. case Type_Info_Named: base = i.base;
  272. case Type_Info_Enum: base = i.base;
  273. case Type_Info_Opaque: base = i.elem;
  274. case: break loop;
  275. }
  276. }
  277. return base;
  278. }
  279. type_info_base_without_enum :: type_info_core;
  280. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info {
  281. data := transmute(Typeid_Bit_Field)id;
  282. n := int(data.index);
  283. if n < 0 || n >= len(type_table) {
  284. n = 0;
  285. }
  286. return &type_table[n];
  287. }
  288. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  289. ti := type_info_of(id);
  290. ti = type_info_base(ti);
  291. return ti.id;
  292. }
  293. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  294. ti := type_info_base_without_enum(type_info_of(id));
  295. return ti.id;
  296. }
  297. typeid_base_without_enum :: typeid_core;
  298. @(default_calling_convention = "c")
  299. foreign {
  300. @(link_name="llvm.assume")
  301. assume :: proc(cond: bool) ---;
  302. @(link_name="llvm.debugtrap")
  303. debug_trap :: proc() ---;
  304. @(link_name="llvm.trap")
  305. trap :: proc() -> ! ---;
  306. @(link_name="llvm.readcyclecounter")
  307. read_cycle_counter :: proc() -> u64 ---;
  308. }
  309. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  310. if c == nil do return;
  311. c^ = other^;
  312. __init_context(c);
  313. }
  314. __init_context :: proc "contextless" (c: ^Context) {
  315. if c == nil do return;
  316. c.allocator.procedure = os.heap_allocator_proc;
  317. c.allocator.data = nil;
  318. c.temp_allocator.procedure = mem.scratch_allocator_proc;
  319. c.temp_allocator.data = &global_scratch_allocator_data;
  320. c.thread_id = os.current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call
  321. c.assertion_failure_proc = default_assertion_failure_proc;
  322. c.logger.procedure = log.nil_logger_proc;
  323. c.logger.data = nil;
  324. c.stdin = os.stdin;
  325. c.stdout = os.stdout;
  326. c.stderr = os.stderr;
  327. }
  328. @builtin
  329. init_global_temporary_allocator :: proc(data: []byte, backup_allocator := context.allocator) {
  330. mem.scratch_allocator_init(&global_scratch_allocator_data, data, backup_allocator);
  331. }
  332. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) {
  333. fd := context.stderr;
  334. print_caller_location(fd, loc);
  335. os.write_string(fd, " ");
  336. os.write_string(fd, prefix);
  337. if len(message) > 0 {
  338. os.write_string(fd, ": ");
  339. os.write_string(fd, message);
  340. }
  341. os.write_byte(fd, '\n');
  342. debug_trap();
  343. }
  344. @builtin
  345. copy :: proc "contextless" (dst, src: $T/[]$E) -> int {
  346. n := max(0, min(len(dst), len(src)));
  347. if n > 0 do mem_copy(&dst[0], &src[0], n*size_of(E));
  348. return n;
  349. }
  350. @builtin
  351. pop :: proc "contextless" (array: ^$T/[dynamic]$E) -> E {
  352. if array == nil do return E{};
  353. assert(len(array) > 0);
  354. res := array[len(array)-1];
  355. (^Raw_Dynamic_Array)(array).len -= 1;
  356. return res;
  357. }
  358. @builtin
  359. unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
  360. bounds_check_error_loc(loc, index, len(array));
  361. n := len(array)-1;
  362. if index != n {
  363. array[index] = array[n];
  364. }
  365. pop(array);
  366. }
  367. @builtin
  368. ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
  369. bounds_check_error_loc(loc, index, len(array));
  370. if index+1 < len(array) {
  371. copy(array[index:], array[index+1:]);
  372. }
  373. pop(array);
  374. }
  375. @builtin
  376. clear :: proc{clear_dynamic_array, clear_map};
  377. @builtin
  378. reserve :: proc{reserve_dynamic_array, reserve_map};
  379. @builtin
  380. resize :: proc{resize_dynamic_array};
  381. @builtin
  382. new :: proc{mem.new};
  383. @builtin
  384. new_clone :: proc{mem.new_clone};
  385. @builtin
  386. free :: proc{mem.free};
  387. @builtin
  388. free_all :: proc{mem.free_all};
  389. @builtin
  390. delete :: proc{
  391. mem.delete_string,
  392. mem.delete_cstring,
  393. mem.delete_dynamic_array,
  394. mem.delete_slice,
  395. mem.delete_map,
  396. };
  397. @builtin
  398. make :: proc{
  399. mem.make_slice,
  400. mem.make_dynamic_array,
  401. mem.make_dynamic_array_len,
  402. mem.make_dynamic_array_len_cap,
  403. mem.make_map,
  404. };
  405. @builtin
  406. clear_map :: inline proc "contextless" (m: ^$T/map[$K]$V) {
  407. if m == nil do return;
  408. raw_map := (^Raw_Map)(m);
  409. entries := (^Raw_Dynamic_Array)(&raw_map.entries);
  410. entries.len = 0;
  411. for _, i in raw_map.hashes {
  412. raw_map.hashes[i] = -1;
  413. }
  414. }
  415. @builtin
  416. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
  417. if m != nil do __dynamic_map_reserve(__get_map_header(m), capacity);
  418. }
  419. @builtin
  420. delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
  421. if m != nil do __dynamic_map_delete_key(__get_map_header(m), __get_map_key(key));
  422. }
  423. @builtin
  424. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
  425. if array == nil do return;
  426. arg_len := 1;
  427. if cap(array) <= len(array)+arg_len {
  428. cap := 2 * cap(array) + max(8, arg_len);
  429. _ = reserve(array, cap, loc);
  430. }
  431. arg_len = min(cap(array)-len(array), arg_len);
  432. if arg_len > 0 {
  433. a := (^Raw_Dynamic_Array)(array);
  434. data := (^E)(a.data);
  435. assert(data != nil);
  436. val := arg;
  437. mem_copy(mem.ptr_offset(data, a.len), &val, size_of(E));
  438. a.len += arg_len;
  439. }
  440. }
  441. @builtin
  442. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
  443. if array == nil do return;
  444. arg_len := len(args);
  445. if arg_len <= 0 do return;
  446. if cap(array) <= len(array)+arg_len {
  447. cap := 2 * cap(array) + max(8, arg_len);
  448. _ = reserve(array, cap, loc);
  449. }
  450. arg_len = min(cap(array)-len(array), arg_len);
  451. if arg_len > 0 {
  452. a := (^Raw_Dynamic_Array)(array);
  453. data := (^E)(a.data);
  454. assert(data != nil);
  455. mem_copy(mem.ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
  456. a.len += arg_len;
  457. }
  458. }
  459. @builtin append :: proc{append_elem, append_elems};
  460. @builtin
  461. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
  462. for arg in args {
  463. append(array = array, args = ([]E)(arg), loc = loc);
  464. }
  465. }
  466. @builtin
  467. clear_dynamic_array :: inline proc "contextless" (array: ^$T/[dynamic]$E) {
  468. if array != nil do (^Raw_Dynamic_Array)(array).len = 0;
  469. }
  470. @builtin
  471. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  472. if array == nil do return false;
  473. a := (^Raw_Dynamic_Array)(array);
  474. if capacity <= a.cap do return true;
  475. if a.allocator.procedure == nil {
  476. a.allocator = context.allocator;
  477. }
  478. assert(a.allocator.procedure != nil);
  479. old_size := a.cap * size_of(E);
  480. new_size := capacity * size_of(E);
  481. allocator := a.allocator;
  482. new_data := allocator.procedure(
  483. allocator.data, mem.Allocator_Mode.Resize, new_size, align_of(E),
  484. a.data, old_size, 0, loc,
  485. );
  486. if new_data == nil do return false;
  487. a.data = new_data;
  488. a.cap = capacity;
  489. return true;
  490. }
  491. @builtin
  492. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  493. if array == nil do return false;
  494. a := (^Raw_Dynamic_Array)(array);
  495. if length <= a.cap {
  496. a.len = max(length, 0);
  497. return true;
  498. }
  499. if a.allocator.procedure == nil {
  500. a.allocator = context.allocator;
  501. }
  502. assert(a.allocator.procedure != nil);
  503. old_size := a.cap * size_of(E);
  504. new_size := length * size_of(E);
  505. allocator := a.allocator;
  506. new_data := allocator.procedure(
  507. allocator.data, mem.Allocator_Mode.Resize, new_size, align_of(E),
  508. a.data, old_size, 0, loc,
  509. );
  510. if new_data == nil do return false;
  511. a.data = new_data;
  512. a.len = length;
  513. a.cap = length;
  514. return true;
  515. }
  516. @builtin
  517. incl_elem :: inline proc(s: ^$S/bit_set[$E; $U], elem: E) -> S {
  518. s^ |= {elem};
  519. return s^;
  520. }
  521. @builtin
  522. incl_elems :: inline proc(s: ^$S/bit_set[$E; $U], elems: ..E) -> S {
  523. for elem in elems do s^ |= {elem};
  524. return s^;
  525. }
  526. @builtin
  527. incl_bit_set :: inline proc(s: ^$S/bit_set[$E; $U], other: S) -> S {
  528. s^ |= other;
  529. return s^;
  530. }
  531. @builtin
  532. excl_elem :: inline proc(s: ^$S/bit_set[$E; $U], elem: E) -> S {
  533. s^ &~= {elem};
  534. return s^;
  535. }
  536. @builtin
  537. excl_elems :: inline proc(s: ^$S/bit_set[$E; $U], elems: ..E) -> S {
  538. for elem in elems do s^ &~= {elem};
  539. return s^;
  540. }
  541. @builtin
  542. excl_bit_set :: inline proc(s: ^$S/bit_set[$E; $U], other: S) -> S {
  543. s^ &~= other;
  544. return s^;
  545. }
  546. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set};
  547. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set};
  548. @builtin
  549. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  550. when size_of(S) == 1 {
  551. foreign { @(link_name="llvm.ctpop.i8") count_ones :: proc(i: u8) -> u8 --- }
  552. return int(count_ones(transmute(u8)s));
  553. } else when size_of(S) == 2 {
  554. foreign { @(link_name="llvm.ctpop.i16") count_ones :: proc(i: u16) -> u16 --- }
  555. return int(count_ones(transmute(u16)s));
  556. } else when size_of(S) == 4 {
  557. foreign { @(link_name="llvm.ctpop.i32") count_ones :: proc(i: u32) -> u32 --- }
  558. return int(count_ones(transmute(u32)s));
  559. } else when size_of(S) == 8 {
  560. foreign { @(link_name="llvm.ctpop.i64") count_ones :: proc(i: u64) -> u64 --- }
  561. return int(count_ones(transmute(u64)s));
  562. } else {
  563. #assert(false);
  564. return 0;
  565. }
  566. }
  567. @builtin
  568. assert :: proc(condition: bool, message := "", loc := #caller_location) -> bool {
  569. if !condition {
  570. proc(message: string, loc: Source_Code_Location) {
  571. p := context.assertion_failure_proc;
  572. if p == nil {
  573. p = default_assertion_failure_proc;
  574. }
  575. p("runtime assertion", message, loc);
  576. }(message, loc);
  577. }
  578. return condition;
  579. }
  580. @builtin
  581. panic :: proc(message: string, loc := #caller_location) -> ! {
  582. p := context.assertion_failure_proc;
  583. if p == nil {
  584. p = default_assertion_failure_proc;
  585. }
  586. p("panic", message, loc);
  587. }
  588. @builtin
  589. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  590. p := context.assertion_failure_proc;
  591. if p == nil {
  592. p = default_assertion_failure_proc;
  593. }
  594. p("not yet implemented", message, loc);
  595. }
  596. @builtin
  597. unreachable :: proc(message := "", loc := #caller_location) -> ! {
  598. p := context.assertion_failure_proc;
  599. if p == nil {
  600. p = default_assertion_failure_proc;
  601. }
  602. if message != "" {
  603. p("internal error", message, loc);
  604. } else {
  605. p("internal error", "entered unreachable code", loc);
  606. }
  607. }
  608. // Dynamic Array
  609. __dynamic_array_make :: proc(array_: rawptr, elem_size, elem_align: int, len, cap: int, loc := #caller_location) {
  610. array := (^Raw_Dynamic_Array)(array_);
  611. array.allocator = context.allocator;
  612. assert(array.allocator.procedure != nil);
  613. if cap > 0 {
  614. __dynamic_array_reserve(array_, elem_size, elem_align, cap, loc);
  615. array.len = len;
  616. }
  617. }
  618. __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: int, loc := #caller_location) -> bool {
  619. array := (^Raw_Dynamic_Array)(array_);
  620. if cap <= array.cap do return true;
  621. if array.allocator.procedure == nil {
  622. array.allocator = context.allocator;
  623. }
  624. assert(array.allocator.procedure != nil);
  625. old_size := array.cap * elem_size;
  626. new_size := cap * elem_size;
  627. allocator := array.allocator;
  628. new_data := allocator.procedure(allocator.data, mem.Allocator_Mode.Resize, new_size, elem_align, array.data, old_size, 0, loc);
  629. if new_data == nil do return false;
  630. array.data = new_data;
  631. array.cap = cap;
  632. return true;
  633. }
  634. __dynamic_array_resize :: proc(array_: rawptr, elem_size, elem_align: int, len: int, loc := #caller_location) -> bool {
  635. array := (^Raw_Dynamic_Array)(array_);
  636. ok := __dynamic_array_reserve(array_, elem_size, elem_align, len, loc);
  637. if ok do array.len = len;
  638. return ok;
  639. }
  640. __dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int,
  641. items: rawptr, item_count: int, loc := #caller_location) -> int {
  642. array := (^Raw_Dynamic_Array)(array_);
  643. if items == nil do return 0;
  644. if item_count <= 0 do return 0;
  645. ok := true;
  646. if array.cap <= array.len+item_count {
  647. cap := 2 * array.cap + max(8, item_count);
  648. ok = __dynamic_array_reserve(array, elem_size, elem_align, cap, loc);
  649. }
  650. // TODO(bill): Better error handling for failed reservation
  651. if !ok do return array.len;
  652. assert(array.data != nil);
  653. data := uintptr(array.data) + uintptr(elem_size*array.len);
  654. mem_copy(rawptr(data), items, elem_size * item_count);
  655. array.len += item_count;
  656. return array.len;
  657. }
  658. __dynamic_array_append_nothing :: proc(array_: rawptr, elem_size, elem_align: int, loc := #caller_location) -> int {
  659. array := (^Raw_Dynamic_Array)(array_);
  660. ok := true;
  661. if array.cap <= array.len+1 {
  662. cap := 2 * array.cap + max(8, 1);
  663. ok = __dynamic_array_reserve(array, elem_size, elem_align, cap, loc);
  664. }
  665. // TODO(bill): Better error handling for failed reservation
  666. if !ok do return array.len;
  667. assert(array.data != nil);
  668. data := uintptr(array.data) + uintptr(elem_size*array.len);
  669. mem.zero(rawptr(data), elem_size);
  670. array.len += 1;
  671. return array.len;
  672. }
  673. // Map
  674. __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
  675. header := Map_Header{m = (^Raw_Map)(m)};
  676. Entry :: struct {
  677. key: Map_Key,
  678. next: int,
  679. value: V,
  680. };
  681. header.is_key_string = intrinsics.type_is_string(K);
  682. header.entry_size = int(size_of(Entry));
  683. header.entry_align = int(align_of(Entry));
  684. header.value_offset = uintptr(offset_of(Entry, value));
  685. header.value_size = int(size_of(V));
  686. return header;
  687. }
  688. __get_map_key :: proc "contextless" (k: $K) -> Map_Key {
  689. key := k;
  690. map_key: Map_Key;
  691. T :: intrinsics.type_core_type(K);
  692. when intrinsics.type_is_integer(T) {
  693. sz :: 8*size_of(T);
  694. when sz == 8 do map_key.hash = u64(( ^u8)(&key)^);
  695. else when sz == 16 do map_key.hash = u64((^u16)(&key)^);
  696. else when sz == 32 do map_key.hash = u64((^u32)(&key)^);
  697. else when sz == 64 do map_key.hash = u64((^u64)(&key)^);
  698. else do #assert(false, "Unhandled integer size");
  699. } else when intrinsics.type_is_rune(T) {
  700. map_key.hash = u64((^rune)(&key)^);
  701. } else when intrinsics.type_is_pointer(T) {
  702. map_key.hash = u64(uintptr((^rawptr)(&key)^));
  703. } else when intrinsics.type_is_float(T) {
  704. sz :: 8*size_of(T);
  705. when sz == 32 do map_key.hash = u64((^u32)(&key)^);
  706. else when sz == 64 do map_key.hash = u64((^u64)(&key)^);
  707. else do #assert(false, "Unhandled float size");
  708. } else when intrinsics.type_is_string(T) {
  709. #assert(T == string);
  710. str := (^string)(&key)^;
  711. map_key.hash = default_hash_string(str);
  712. map_key.str = str;
  713. } else {
  714. #assert(false, "Unhandled map key type");
  715. }
  716. return map_key;
  717. }
  718. _fnv64a :: proc(data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 {
  719. h: u64 = seed;
  720. for b in data {
  721. h = (h ~ u64(b)) * 0x100000001b3;
  722. }
  723. return h;
  724. }
  725. default_hash :: proc(data: []byte) -> u64 {
  726. return _fnv64a(data);
  727. }
  728. default_hash_string :: proc(s: string) -> u64 do return default_hash(([]byte)(s));
  729. source_code_location_hash :: proc(s: Source_Code_Location) -> u64 {
  730. hash := _fnv64a(cast([]byte)s.file_path);
  731. hash = hash ~ (u64(s.line) * 0x100000001b3);
  732. hash = hash ~ (u64(s.column) * 0x100000001b3);
  733. return hash;
  734. }
  735. __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: mem.Allocator, loc := #caller_location) -> bool {
  736. array := (^Raw_Slice)(array_);
  737. if new_count < array.len do return true;
  738. assert(allocator.procedure != nil);
  739. old_size := array.len*size_of(T);
  740. new_size := new_count*size_of(T);
  741. new_data := mem.resize(array.data, old_size, new_size, align_of(T), allocator, loc);
  742. if new_data == nil do return false;
  743. array.data = new_data;
  744. array.len = new_count;
  745. return true;
  746. }
  747. __dynamic_map_reserve :: proc(using header: Map_Header, cap: int, loc := #caller_location) {
  748. __dynamic_array_reserve(&m.entries, entry_size, entry_align, cap, loc);
  749. old_len := len(m.hashes);
  750. __slice_resize(&m.hashes, cap, m.entries.allocator, loc);
  751. for i in old_len..<len(m.hashes) do m.hashes[i] = -1;
  752. }
  753. __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #caller_location) #no_bounds_check {
  754. new_header: Map_Header = header;
  755. nm := Raw_Map{};
  756. nm.entries.allocator = m.entries.allocator;
  757. new_header.m = &nm;
  758. c := context;
  759. if m.entries.allocator.procedure != nil {
  760. c.allocator = m.entries.allocator;
  761. }
  762. context = c;
  763. __dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc);
  764. __slice_resize(&nm.hashes, new_count, m.entries.allocator, loc);
  765. for i in 0 ..< new_count do nm.hashes[i] = -1;
  766. for i in 0 ..< m.entries.len {
  767. if len(nm.hashes) == 0 do __dynamic_map_grow(new_header, loc);
  768. entry_header := __dynamic_map_get_entry(header, i);
  769. data := uintptr(entry_header);
  770. fr := __dynamic_map_find(new_header, entry_header.key);
  771. j := __dynamic_map_add_entry(new_header, entry_header.key, loc);
  772. if fr.entry_prev < 0 {
  773. nm.hashes[fr.hash_index] = j;
  774. } else {
  775. e := __dynamic_map_get_entry(new_header, fr.entry_prev);
  776. e.next = j;
  777. }
  778. e := __dynamic_map_get_entry(new_header, j);
  779. e.next = fr.entry_index;
  780. ndata := uintptr(e);
  781. mem_copy(rawptr(ndata+value_offset), rawptr(data+value_offset), value_size);
  782. if __dynamic_map_full(new_header) do __dynamic_map_grow(new_header, loc);
  783. }
  784. delete(m.hashes, m.entries.allocator, loc);
  785. free(m.entries.data, m.entries.allocator, loc);
  786. header.m^ = nm;
  787. }
  788. __dynamic_map_get :: proc(h: Map_Header, key: Map_Key) -> rawptr {
  789. index := __dynamic_map_find(h, key).entry_index;
  790. if index >= 0 {
  791. data := uintptr(__dynamic_map_get_entry(h, index));
  792. return rawptr(data + h.value_offset);
  793. }
  794. return nil;
  795. }
  796. __dynamic_map_set :: proc(h: Map_Header, key: Map_Key, value: rawptr, loc := #caller_location) #no_bounds_check {
  797. index: int;
  798. assert(value != nil);
  799. if len(h.m.hashes) == 0 {
  800. __dynamic_map_reserve(h, INITIAL_MAP_CAP, loc);
  801. __dynamic_map_grow(h, loc);
  802. }
  803. fr := __dynamic_map_find(h, key);
  804. if fr.entry_index >= 0 {
  805. index = fr.entry_index;
  806. } else {
  807. index = __dynamic_map_add_entry(h, key, loc);
  808. if fr.entry_prev >= 0 {
  809. entry := __dynamic_map_get_entry(h, fr.entry_prev);
  810. entry.next = index;
  811. } else {
  812. h.m.hashes[fr.hash_index] = index;
  813. }
  814. }
  815. {
  816. e := __dynamic_map_get_entry(h, index);
  817. e.key = key;
  818. val := (^byte)(uintptr(e) + h.value_offset);
  819. mem_copy(val, value, h.value_size);
  820. }
  821. if __dynamic_map_full(h) {
  822. __dynamic_map_grow(h, loc);
  823. }
  824. }
  825. __dynamic_map_grow :: proc(using h: Map_Header, loc := #caller_location) {
  826. // TODO(bill): Determine an efficient growing rate
  827. new_count := max(4*m.entries.cap + 7, INITIAL_MAP_CAP);
  828. __dynamic_map_rehash(h, new_count, loc);
  829. }
  830. __dynamic_map_full :: inline proc(using h: Map_Header) -> bool {
  831. return int(0.75 * f64(len(m.hashes))) <= m.entries.cap;
  832. }
  833. __dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Key) -> bool {
  834. if a.hash == b.hash {
  835. if h.is_key_string do return a.str == b.str;
  836. return true;
  837. }
  838. return false;
  839. }
  840. __dynamic_map_find :: proc(using h: Map_Header, key: Map_Key) -> Map_Find_Result #no_bounds_check {
  841. fr := Map_Find_Result{-1, -1, -1};
  842. if n := u64(len(m.hashes)); n > 0 {
  843. fr.hash_index = int(key.hash % n);
  844. fr.entry_index = m.hashes[fr.hash_index];
  845. for fr.entry_index >= 0 {
  846. entry := __dynamic_map_get_entry(h, fr.entry_index);
  847. if __dynamic_map_hash_equal(h, entry.key, key) do return fr;
  848. fr.entry_prev = fr.entry_index;
  849. fr.entry_index = entry.next;
  850. }
  851. }
  852. return fr;
  853. }
  854. __dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Key, loc := #caller_location) -> int {
  855. prev := m.entries.len;
  856. c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc);
  857. if c != prev {
  858. end := __dynamic_map_get_entry(h, c-1);
  859. end.key = key;
  860. end.next = -1;
  861. }
  862. return prev;
  863. }
  864. __dynamic_map_delete_key :: proc(using h: Map_Header, key: Map_Key) {
  865. fr := __dynamic_map_find(h, key);
  866. if fr.entry_index >= 0 {
  867. __dynamic_map_erase(h, fr);
  868. }
  869. }
  870. __dynamic_map_get_entry :: proc(using h: Map_Header, index: int) -> ^Map_Entry_Header {
  871. assert(0 <= index && index < m.entries.len);
  872. return (^Map_Entry_Header)(uintptr(m.entries.data) + uintptr(index*entry_size));
  873. }
  874. __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds_check {
  875. if fr.entry_prev < 0 {
  876. m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next;
  877. } else {
  878. prev := __dynamic_map_get_entry(h, fr.entry_prev);
  879. curr := __dynamic_map_get_entry(h, fr.entry_index);
  880. prev.next = curr.next;
  881. }
  882. if (fr.entry_index == m.entries.len-1) {
  883. // NOTE(bill): No need to do anything else, just pop
  884. } else {
  885. old := __dynamic_map_get_entry(h, fr.entry_index);
  886. end := __dynamic_map_get_entry(h, m.entries.len-1);
  887. mem_copy(old, end, entry_size);
  888. if last := __dynamic_map_find(h, old.key); last.entry_prev >= 0 {
  889. last_entry := __dynamic_map_get_entry(h, last.entry_prev);
  890. last_entry.next = fr.entry_index;
  891. } else {
  892. m.hashes[last.hash_index] = fr.entry_index;
  893. }
  894. }
  895. // TODO(bill): Is this correct behaviour?
  896. m.entries.len -= 1;
  897. }