core_builtin.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package runtime
  2. @builtin
  3. Maybe :: union(T: typeid) #maybe {T};
  4. @thread_local global_default_temp_allocator_data: Default_Temp_Allocator;
  5. @builtin
  6. init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
  7. default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator);
  8. }
  9. @builtin
  10. copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
  11. n := max(0, min(len(dst), len(src)));
  12. if n > 0 {
  13. mem_copy(raw_data(dst), raw_data(src), n*size_of(E));
  14. }
  15. return n;
  16. }
  17. @builtin
  18. copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
  19. n := max(0, min(len(dst), len(src)));
  20. if n > 0 {
  21. mem_copy(raw_data(dst), raw_data(src), n);
  22. }
  23. return n;
  24. }
  25. @builtin
  26. copy :: proc{copy_slice, copy_from_string};
  27. @builtin
  28. unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
  29. bounds_check_error_loc(loc, index, len(array));
  30. n := len(array)-1;
  31. if index != n {
  32. array[index] = array[n];
  33. }
  34. pop(array);
  35. }
  36. @builtin
  37. ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) {
  38. bounds_check_error_loc(loc, index, len(array));
  39. if index+1 < len(array) {
  40. copy(array[index:], array[index+1:]);
  41. }
  42. pop(array);
  43. }
  44. @builtin
  45. remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) {
  46. slice_expr_error_lo_hi_loc(loc, lo, hi, len(array));
  47. n := max(hi-lo, 0);
  48. if n > 0 {
  49. if hi != len(array) {
  50. copy(array[lo:], array[hi:]);
  51. }
  52. (^Raw_Dynamic_Array)(array).len -= n;
  53. }
  54. }
  55. @builtin
  56. pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  57. assert(len(array) > 0, "", loc);
  58. res = array[len(array)-1];
  59. (^Raw_Dynamic_Array)(array).len -= 1;
  60. return res;
  61. }
  62. @builtin
  63. pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  64. if len(array) == 0 {
  65. return;
  66. }
  67. res, ok = array[len(array)-1], true;
  68. (^Raw_Dynamic_Array)(array).len -= 1;
  69. return;
  70. }
  71. @builtin
  72. pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  73. assert(len(array) > 0, "", loc);
  74. res = array[0];
  75. if len(array) > 1 {
  76. copy(array[0:], array[1:]);
  77. }
  78. (^Raw_Dynamic_Array)(array).len -= 1;
  79. return res;
  80. }
  81. @builtin
  82. pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  83. if len(array) == 0 {
  84. return;
  85. }
  86. res, ok = array[0], true;
  87. if len(array) > 1 {
  88. copy(array[0:], array[1:]);
  89. }
  90. (^Raw_Dynamic_Array)(array).len -= 1;
  91. return;
  92. }
  93. @builtin
  94. clear :: proc{clear_dynamic_array, clear_map};
  95. @builtin
  96. reserve :: proc{reserve_dynamic_array, reserve_map};
  97. @builtin
  98. resize :: proc{resize_dynamic_array};
  99. @builtin
  100. free :: proc{mem_free};
  101. @builtin
  102. free_all :: proc{mem_free_all};
  103. @builtin
  104. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  105. return mem_free(raw_data(str), allocator, loc);
  106. }
  107. @builtin
  108. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  109. return mem_free((^byte)(str), allocator, loc);
  110. }
  111. @builtin
  112. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  113. return mem_free(raw_data(array), array.allocator, loc);
  114. }
  115. @builtin
  116. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  117. return mem_free(raw_data(array), allocator, loc);
  118. }
  119. @builtin
  120. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  121. raw := transmute(Raw_Map)m;
  122. err := delete_slice(raw.hashes, raw.entries.allocator, loc);
  123. err1 := mem_free(raw.entries.data, raw.entries.allocator, loc);
  124. if err == nil {
  125. err = err1;
  126. }
  127. return err;
  128. }
  129. @builtin
  130. delete :: proc{
  131. delete_string,
  132. delete_cstring,
  133. delete_dynamic_array,
  134. delete_slice,
  135. delete_map,
  136. };
  137. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  138. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  139. @builtin
  140. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
  141. ptr, err := mem_alloc(size_of(T), align_of(T), allocator, loc);
  142. return (^T)(ptr), err;
  143. }
  144. @builtin
  145. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
  146. ptr, err := mem_alloc(size_of(T), align_of(T), allocator, loc);
  147. res := (^T)(ptr);
  148. if ptr != nil && err != .Out_Of_Memory {
  149. res^ = data;
  150. }
  151. return res, err;
  152. }
  153. DEFAULT_RESERVE_CAPACITY :: 16;
  154. make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  155. make_slice_error_loc(loc, len);
  156. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc);
  157. if data == nil && size_of(E) != 0 {
  158. return nil, err;
  159. }
  160. s := Raw_Slice{raw_data(data), len};
  161. return transmute(T)s, err;
  162. }
  163. @builtin
  164. make_slice :: proc($T: typeid/[]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  165. return make_aligned(T, len, align_of(E), allocator, loc);
  166. }
  167. @builtin
  168. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  169. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc);
  170. }
  171. @builtin
  172. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  173. return make_dynamic_array_len_cap(T, len, len, allocator, loc);
  174. }
  175. @builtin
  176. make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, auto_cast cap: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  177. make_dynamic_array_error_loc(loc, len, cap);
  178. data, err := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
  179. s := Raw_Dynamic_Array{data, len, cap, allocator};
  180. if data == nil && size_of(E) != 0 {
  181. s.len, s.cap = 0, 0;
  182. }
  183. return transmute(T)s, err;
  184. }
  185. @builtin
  186. make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  187. make_map_expr_error_loc(loc, cap);
  188. context.allocator = allocator;
  189. m: T;
  190. reserve_map(&m, cap);
  191. return m;
  192. }
  193. // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
  194. // Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  195. // type of its argument, not a pointer to it.
  196. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  197. @builtin
  198. make :: proc{
  199. make_slice,
  200. make_dynamic_array,
  201. make_dynamic_array_len,
  202. make_dynamic_array_len_cap,
  203. make_map,
  204. };
  205. @builtin
  206. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  207. if m == nil {
  208. return;
  209. }
  210. raw_map := (^Raw_Map)(m);
  211. entries := (^Raw_Dynamic_Array)(&raw_map.entries);
  212. entries.len = 0;
  213. for _, i in raw_map.hashes {
  214. raw_map.hashes[i] = -1;
  215. }
  216. }
  217. @builtin
  218. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
  219. if m != nil {
  220. __dynamic_map_reserve(__get_map_header(m), capacity);
  221. }
  222. }
  223. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  224. // If m is nil, or there is no such element, this procedure is a no-op
  225. @builtin
  226. delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
  227. if m != nil {
  228. key := key;
  229. __dynamic_map_delete_key(__get_map_header(m), __get_map_hash(&key));
  230. }
  231. }
  232. @builtin
  233. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
  234. if array == nil {
  235. return;
  236. }
  237. arg_len := 1;
  238. if cap(array) < len(array)+arg_len {
  239. cap := 2 * cap(array) + max(8, arg_len);
  240. _ = reserve(array, cap, loc);
  241. }
  242. arg_len = min(cap(array)-len(array), arg_len);
  243. if arg_len > 0 {
  244. a := (^Raw_Dynamic_Array)(array);
  245. if size_of(E) != 0 {
  246. data := (^E)(a.data);
  247. assert(data != nil);
  248. val := arg;
  249. mem_copy(ptr_offset(data, a.len), &val, size_of(E));
  250. }
  251. a.len += arg_len;
  252. }
  253. }
  254. @builtin
  255. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
  256. if array == nil {
  257. return;
  258. }
  259. arg_len := len(args);
  260. if arg_len <= 0 {
  261. return;
  262. }
  263. if cap(array) < len(array)+arg_len {
  264. cap := 2 * cap(array) + max(8, arg_len);
  265. _ = reserve(array, cap, loc);
  266. }
  267. arg_len = min(cap(array)-len(array), arg_len);
  268. if arg_len > 0 {
  269. a := (^Raw_Dynamic_Array)(array);
  270. if size_of(E) != 0 {
  271. data := (^E)(a.data);
  272. assert(data != nil);
  273. mem_copy(ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
  274. }
  275. a.len += arg_len;
  276. }
  277. }
  278. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  279. @builtin
  280. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
  281. args := transmute([]E)arg;
  282. append_elems(array=array, args=args, loc=loc);
  283. }
  284. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  285. @builtin
  286. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
  287. for arg in args {
  288. append(array = array, args = transmute([]E)(arg), loc = loc);
  289. }
  290. }
  291. // The append built-in procedure appends elements to the end of a dynamic array
  292. @builtin append :: proc{append_elem, append_elems, append_elem_string};
  293. @builtin
  294. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) {
  295. if array == nil {
  296. return;
  297. }
  298. resize(array, len(array)+1);
  299. }
  300. @builtin
  301. insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  302. if array == nil {
  303. return;
  304. }
  305. n := len(array);
  306. m :: 1;
  307. resize(array, n+m, loc);
  308. if n+m <= len(array) {
  309. when size_of(E) != 0 {
  310. copy(array[index+m:], array[index:]);
  311. array[index] = arg;
  312. }
  313. ok = true;
  314. }
  315. return;
  316. }
  317. @builtin
  318. insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  319. if array == nil {
  320. return;
  321. }
  322. if len(args) == 0 {
  323. ok = true;
  324. return;
  325. }
  326. n := len(array);
  327. m := len(args);
  328. resize(array, n+m, loc);
  329. if n+m <= len(array) {
  330. when size_of(E) != 0 {
  331. copy(array[index+m:], array[index:]);
  332. copy(array[index:], args);
  333. }
  334. ok = true;
  335. }
  336. return;
  337. }
  338. @builtin
  339. insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  340. if array == nil {
  341. return;
  342. }
  343. if len(args) == 0 {
  344. ok = true;
  345. return;
  346. }
  347. n := len(array);
  348. m := len(args);
  349. resize(array, n+m, loc);
  350. if n+m <= len(array) {
  351. copy(array[index+m:], array[index:]);
  352. copy(array[index:], args);
  353. ok = true;
  354. }
  355. return;
  356. }
  357. @builtin insert_at :: proc{insert_at_elem, insert_at_elems, insert_at_elem_string};
  358. @builtin
  359. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  360. if array != nil {
  361. (^Raw_Dynamic_Array)(array).len = 0;
  362. }
  363. }
  364. @builtin
  365. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  366. if array == nil {
  367. return false;
  368. }
  369. a := (^Raw_Dynamic_Array)(array);
  370. if capacity <= a.cap {
  371. return true;
  372. }
  373. if a.allocator.procedure == nil {
  374. a.allocator = context.allocator;
  375. }
  376. assert(a.allocator.procedure != nil);
  377. old_size := a.cap * size_of(E);
  378. new_size := capacity * size_of(E);
  379. allocator := a.allocator;
  380. new_data, err := allocator.procedure(
  381. allocator.data, .Resize, new_size, align_of(E),
  382. a.data, old_size, loc,
  383. );
  384. if new_data == nil || err != nil {
  385. return false;
  386. }
  387. a.data = raw_data(new_data);
  388. a.cap = capacity;
  389. return true;
  390. }
  391. @builtin
  392. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  393. if array == nil {
  394. return false;
  395. }
  396. a := (^Raw_Dynamic_Array)(array);
  397. if length <= a.cap {
  398. a.len = max(length, 0);
  399. return true;
  400. }
  401. if a.allocator.procedure == nil {
  402. a.allocator = context.allocator;
  403. }
  404. assert(a.allocator.procedure != nil);
  405. old_size := a.cap * size_of(E);
  406. new_size := length * size_of(E);
  407. allocator := a.allocator;
  408. new_data, err := allocator.procedure(
  409. allocator.data, .Resize, new_size, align_of(E),
  410. a.data, old_size, loc,
  411. );
  412. if new_data == nil || err != nil {
  413. return false;
  414. }
  415. a.data = raw_data(new_data);
  416. a.len = length;
  417. a.cap = length;
  418. return true;
  419. }
  420. @builtin
  421. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  422. s^ |= {elem};
  423. }
  424. @builtin
  425. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  426. for elem in elems {
  427. s^ |= {elem};
  428. }
  429. }
  430. @builtin
  431. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  432. s^ |= other;
  433. }
  434. @builtin
  435. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  436. s^ &~= {elem};
  437. }
  438. @builtin
  439. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  440. for elem in elems {
  441. s^ &~= {elem};
  442. }
  443. }
  444. @builtin
  445. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  446. s^ &~= other;
  447. }
  448. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set};
  449. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set};
  450. @builtin
  451. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  452. when size_of(S) == 1 {
  453. foreign { @(link_name="llvm.ctpop.i8") count_ones :: proc(i: u8) -> u8 --- }
  454. return int(count_ones(transmute(u8)s));
  455. } else when size_of(S) == 2 {
  456. foreign { @(link_name="llvm.ctpop.i16") count_ones :: proc(i: u16) -> u16 --- }
  457. return int(count_ones(transmute(u16)s));
  458. } else when size_of(S) == 4 {
  459. foreign { @(link_name="llvm.ctpop.i32") count_ones :: proc(i: u32) -> u32 --- }
  460. return int(count_ones(transmute(u32)s));
  461. } else when size_of(S) == 8 {
  462. foreign { @(link_name="llvm.ctpop.i64") count_ones :: proc(i: u64) -> u64 --- }
  463. return int(count_ones(transmute(u64)s));
  464. } else when size_of(S) == 16 {
  465. foreign { @(link_name="llvm.ctpop.i128") count_ones :: proc(i: u128) -> u128 --- }
  466. return int(count_ones(transmute(u128)s));
  467. } else {
  468. #panic("Unhandled card bit_set size");
  469. }
  470. }
  471. @builtin
  472. raw_array_data :: proc "contextless" (a: $P/^($T/[$N]$E)) -> ^E {
  473. return (^E)(a);
  474. }
  475. @builtin
  476. raw_slice_data :: proc "contextless" (s: $S/[]$E) -> ^E {
  477. ptr := (transmute(Raw_Slice)s).data;
  478. return (^E)(ptr);
  479. }
  480. @builtin
  481. raw_dynamic_array_data :: proc "contextless" (s: $S/[dynamic]$E) -> ^E {
  482. ptr := (transmute(Raw_Dynamic_Array)s).data;
  483. return (^E)(ptr);
  484. }
  485. @builtin
  486. raw_string_data :: proc "contextless" (s: $S/string) -> ^u8 {
  487. return (transmute(Raw_String)s).data;
  488. }
  489. @builtin
  490. raw_data :: proc{raw_array_data, raw_slice_data, raw_dynamic_array_data, raw_string_data};
  491. @builtin
  492. @(disabled=ODIN_DISABLE_ASSERT)
  493. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  494. if !condition {
  495. proc(message: string, loc: Source_Code_Location) {
  496. p := context.assertion_failure_proc;
  497. if p == nil {
  498. p = default_assertion_failure_proc;
  499. }
  500. p("runtime assertion", message, loc);
  501. }(message, loc);
  502. }
  503. }
  504. @builtin
  505. @(disabled=ODIN_DISABLE_ASSERT)
  506. panic :: proc(message: string, loc := #caller_location) -> ! {
  507. p := context.assertion_failure_proc;
  508. if p == nil {
  509. p = default_assertion_failure_proc;
  510. }
  511. p("panic", message, loc);
  512. }
  513. @builtin
  514. @(disabled=ODIN_DISABLE_ASSERT)
  515. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  516. p := context.assertion_failure_proc;
  517. if p == nil {
  518. p = default_assertion_failure_proc;
  519. }
  520. p("not yet implemented", message, loc);
  521. }
  522. @builtin
  523. @(disabled=ODIN_DISABLE_ASSERT)
  524. unreachable :: proc(message := "", loc := #caller_location) -> ! {
  525. p := context.assertion_failure_proc;
  526. if p == nil {
  527. p = default_assertion_failure_proc;
  528. }
  529. if message != "" {
  530. p("internal error", message, loc);
  531. } else {
  532. p("internal error", "entered unreachable code", loc);
  533. }
  534. }