core_builtin.odin 15 KB

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