core_builtin.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. package runtime
  2. import "core:intrinsics"
  3. @builtin
  4. Maybe :: union($T: typeid) {T}
  5. @builtin
  6. container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: typeid, $field_name: string) -> ^T
  7. where intrinsics.type_has_field(T, field_name),
  8. intrinsics.type_field_type(T, field_name) == Field_Type {
  9. offset :: offset_of_by_string(T, field_name)
  10. return (^T)(uintptr(ptr) - offset) if ptr != nil else nil
  11. }
  12. @thread_local global_default_temp_allocator_data: Default_Temp_Allocator
  13. @builtin
  14. init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
  15. default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator)
  16. }
  17. @builtin
  18. copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
  19. n := max(0, min(len(dst), len(src)))
  20. if n > 0 {
  21. intrinsics.mem_copy(raw_data(dst), raw_data(src), n*size_of(E))
  22. }
  23. return n
  24. }
  25. @builtin
  26. copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
  27. n := max(0, min(len(dst), len(src)))
  28. if n > 0 {
  29. intrinsics.mem_copy(raw_data(dst), raw_data(src), n)
  30. }
  31. return n
  32. }
  33. @builtin
  34. copy :: proc{copy_slice, copy_from_string}
  35. @builtin
  36. unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  37. bounds_check_error_loc(loc, index, len(array))
  38. n := len(array)-1
  39. if index != n {
  40. array[index] = array[n]
  41. }
  42. (^Raw_Dynamic_Array)(array).len -= 1
  43. }
  44. @builtin
  45. ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  46. bounds_check_error_loc(loc, index, len(array))
  47. if index+1 < len(array) {
  48. copy(array[index:], array[index+1:])
  49. }
  50. (^Raw_Dynamic_Array)(array).len -= 1
  51. }
  52. @builtin
  53. remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
  54. slice_expr_error_lo_hi_loc(loc, lo, hi, len(array))
  55. n := max(hi-lo, 0)
  56. if n > 0 {
  57. if hi != len(array) {
  58. copy(array[lo:], array[hi:])
  59. }
  60. (^Raw_Dynamic_Array)(array).len -= n
  61. }
  62. }
  63. @builtin
  64. pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  65. assert(len(array) > 0, "", loc)
  66. res = array[len(array)-1]
  67. (^Raw_Dynamic_Array)(array).len -= 1
  68. return res
  69. }
  70. @builtin
  71. pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  72. if len(array) == 0 {
  73. return
  74. }
  75. res, ok = array[len(array)-1], true
  76. (^Raw_Dynamic_Array)(array).len -= 1
  77. return
  78. }
  79. @builtin
  80. pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  81. assert(len(array) > 0, "", loc)
  82. res = array[0]
  83. if len(array) > 1 {
  84. copy(array[0:], array[1:])
  85. }
  86. (^Raw_Dynamic_Array)(array).len -= 1
  87. return res
  88. }
  89. @builtin
  90. pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  91. if len(array) == 0 {
  92. return
  93. }
  94. res, ok = array[0], true
  95. if len(array) > 1 {
  96. copy(array[0:], array[1:])
  97. }
  98. (^Raw_Dynamic_Array)(array).len -= 1
  99. return
  100. }
  101. @builtin
  102. clear :: proc{clear_dynamic_array, clear_map}
  103. @builtin
  104. reserve :: proc{reserve_dynamic_array, reserve_map}
  105. @builtin
  106. resize :: proc{resize_dynamic_array}
  107. // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
  108. @builtin
  109. shrink :: proc{shrink_dynamic_array, shrink_map}
  110. @builtin
  111. free :: proc{mem_free}
  112. @builtin
  113. free_all :: proc{mem_free_all}
  114. @builtin
  115. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  116. return mem_free(raw_data(str), allocator, loc)
  117. }
  118. @builtin
  119. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  120. return mem_free((^byte)(str), allocator, loc)
  121. }
  122. @builtin
  123. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  124. return mem_free(raw_data(array), array.allocator, loc)
  125. }
  126. @builtin
  127. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  128. return mem_free(raw_data(array), allocator, loc)
  129. }
  130. @builtin
  131. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  132. raw := transmute(Raw_Map)m
  133. err := delete_slice(raw.hashes, raw.entries.allocator, loc)
  134. err1 := mem_free(raw.entries.data, raw.entries.allocator, loc)
  135. if err == nil {
  136. err = err1
  137. }
  138. return err
  139. }
  140. @builtin
  141. delete :: proc{
  142. delete_string,
  143. delete_cstring,
  144. delete_dynamic_array,
  145. delete_slice,
  146. delete_map,
  147. }
  148. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  149. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  150. @builtin
  151. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
  152. return new_aligned(T, align_of(T), allocator, loc)
  153. }
  154. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  155. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  156. t = (^T)(raw_data(data))
  157. return
  158. }
  159. @builtin
  160. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_second {
  161. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  162. t = (^T)(raw_data(t_data))
  163. if t != nil {
  164. t^ = data
  165. }
  166. return
  167. }
  168. DEFAULT_RESERVE_CAPACITY :: 16
  169. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  170. make_slice_error_loc(loc, len)
  171. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  172. if data == nil && size_of(E) != 0 {
  173. return nil, err
  174. }
  175. s := Raw_Slice{raw_data(data), len}
  176. return transmute(T)s, err
  177. }
  178. @(builtin)
  179. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  180. return make_aligned(T, len, align_of(E), allocator, loc)
  181. }
  182. @(builtin)
  183. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  184. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  185. }
  186. @(builtin)
  187. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
  188. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  189. }
  190. @(builtin)
  191. make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
  192. make_dynamic_array_error_loc(loc, len, cap)
  193. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  194. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  195. if data == nil && size_of(E) != 0 {
  196. s.len, s.cap = 0, 0
  197. }
  198. array = transmute(T)s
  199. return
  200. }
  201. @(builtin)
  202. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  203. make_map_expr_error_loc(loc, cap)
  204. context.allocator = allocator
  205. m: T
  206. reserve_map(&m, cap)
  207. return m
  208. }
  209. @(builtin)
  210. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_second {
  211. make_slice_error_loc(loc, len)
  212. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  213. if data == nil && size_of(E) != 0 {
  214. return
  215. }
  216. mp = cast(T)raw_data(data)
  217. return
  218. }
  219. // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
  220. // Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  221. // type of its argument, not a pointer to it.
  222. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  223. @builtin
  224. make :: proc{
  225. make_slice,
  226. make_dynamic_array,
  227. make_dynamic_array_len,
  228. make_dynamic_array_len_cap,
  229. make_map,
  230. make_multi_pointer,
  231. }
  232. @builtin
  233. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  234. if m == nil {
  235. return
  236. }
  237. raw_map := (^Raw_Map)(m)
  238. entries := (^Raw_Dynamic_Array)(&raw_map.entries)
  239. entries.len = 0
  240. for _, i in raw_map.hashes {
  241. raw_map.hashes[i] = -1
  242. }
  243. }
  244. @builtin
  245. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
  246. if m != nil {
  247. __dynamic_map_reserve(__get_map_header(m), capacity, loc)
  248. }
  249. }
  250. /*
  251. Shrinks the capacity of a map down to the current length, or the given capacity.
  252. If `new_cap` is negative, then `len(m)` is used.
  253. Returns false if `cap(m) < new_cap`, or the allocator report failure.
  254. If `len(m) < new_cap`, then `len(m)` will be left unchanged.
  255. */
  256. @builtin
  257. shrink_map :: proc(m: ^$T/map[$K]$V, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  258. if m != nil {
  259. new_cap := new_cap if new_cap >= 0 else len(m)
  260. return __dynamic_map_shrink(__get_map_header(m), new_cap, loc)
  261. }
  262. return
  263. }
  264. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  265. // If m is nil, or there is no such element, this procedure is a no-op
  266. @builtin
  267. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  268. if m != nil {
  269. key := key
  270. h := __get_map_header(m)
  271. hash := __get_map_hash(&key)
  272. fr := __dynamic_map_find(h, hash)
  273. if fr.entry_index >= 0 {
  274. entry := __dynamic_map_get_entry(h, fr.entry_index)
  275. deleted_key = (^K)(uintptr(entry)+h.key_offset)^
  276. deleted_value = (^V)(uintptr(entry)+h.value_offset)^
  277. __dynamic_map_erase(h, fr)
  278. }
  279. }
  280. return
  281. }
  282. @builtin
  283. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
  284. if array == nil {
  285. return
  286. }
  287. when size_of(E) == 0 {
  288. a.len += 1
  289. } else {
  290. if cap(array) < len(array)+1 {
  291. cap := 2 * cap(array) + max(8, 1)
  292. _ = reserve(array, cap, loc)
  293. }
  294. if cap(array)-len(array) > 0 {
  295. a := (^Raw_Dynamic_Array)(array)
  296. when size_of(E) != 0 {
  297. data := ([^]E)(a.data)
  298. assert(condition=data != nil, loc=loc)
  299. data[a.len] = arg
  300. }
  301. a.len += 1
  302. }
  303. }
  304. }
  305. @builtin
  306. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
  307. if array == nil {
  308. return
  309. }
  310. arg_len := len(args)
  311. if arg_len <= 0 {
  312. return
  313. }
  314. when size_of(E) == 0 {
  315. a.len += arg_len
  316. } else {
  317. if cap(array) < len(array)+arg_len {
  318. cap := 2 * cap(array) + max(8, arg_len)
  319. _ = reserve(array, cap, loc)
  320. }
  321. arg_len = min(cap(array)-len(array), arg_len)
  322. if arg_len > 0 {
  323. a := (^Raw_Dynamic_Array)(array)
  324. when size_of(E) != 0 {
  325. data := ([^]E)(a.data)
  326. assert(condition=data != nil, loc=loc)
  327. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  328. }
  329. a.len += arg_len
  330. }
  331. }
  332. }
  333. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  334. @builtin
  335. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
  336. args := transmute([]E)arg
  337. append_elems(array=array, args=args, loc=loc)
  338. }
  339. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  340. @builtin
  341. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
  342. for arg in args {
  343. append(array = array, args = transmute([]E)(arg), loc = loc)
  344. }
  345. }
  346. // The append built-in procedure appends elements to the end of a dynamic array
  347. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  348. @builtin
  349. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) {
  350. if array == nil {
  351. return
  352. }
  353. resize(array, len(array)+1)
  354. }
  355. @builtin
  356. inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  357. if array == nil {
  358. return
  359. }
  360. n := max(len(array), index)
  361. m :: 1
  362. new_size := n + m
  363. if resize(array, new_size, loc) {
  364. when size_of(E) != 0 {
  365. copy(array[index + m:], array[index:])
  366. array[index] = arg
  367. }
  368. ok = true
  369. }
  370. return
  371. }
  372. @builtin
  373. inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  374. if array == nil {
  375. return
  376. }
  377. if len(args) == 0 {
  378. ok = true
  379. return
  380. }
  381. n := max(len(array), index)
  382. m := len(args)
  383. new_size := n + m
  384. if resize(array, new_size, loc) {
  385. when size_of(E) != 0 {
  386. copy(array[index + m:], array[index:])
  387. copy(array[index:], args)
  388. }
  389. ok = true
  390. }
  391. return
  392. }
  393. @builtin
  394. inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  395. if array == nil {
  396. return
  397. }
  398. if len(arg) == 0 {
  399. ok = true
  400. return
  401. }
  402. n := max(len(array), index)
  403. m := len(arg)
  404. new_size := n + m
  405. if resize(array, new_size, loc) {
  406. copy(array[index+m:], array[index:])
  407. copy(array[index:], arg)
  408. ok = true
  409. }
  410. return
  411. }
  412. @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
  413. @builtin
  414. assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  415. if index < len(array) {
  416. array[index] = arg
  417. ok = true
  418. } else if resize(array, index+1, loc) {
  419. array[index] = arg
  420. ok = true
  421. }
  422. return
  423. }
  424. @builtin
  425. assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  426. if index+len(args) < len(array) {
  427. copy(array[index:], args)
  428. ok = true
  429. } else if resize(array, index+1+len(args), loc) {
  430. copy(array[index:], args)
  431. ok = true
  432. }
  433. return
  434. }
  435. @builtin
  436. assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  437. if len(args) == 0 {
  438. ok = true
  439. } else if index+len(args) < len(array) {
  440. copy(array[index:], args)
  441. ok = true
  442. } else if resize(array, index+1+len(args), loc) {
  443. copy(array[index:], args)
  444. ok = true
  445. }
  446. return
  447. }
  448. @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
  449. @builtin
  450. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  451. if array != nil {
  452. (^Raw_Dynamic_Array)(array).len = 0
  453. }
  454. }
  455. @builtin
  456. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  457. if array == nil {
  458. return false
  459. }
  460. a := (^Raw_Dynamic_Array)(array)
  461. if capacity <= a.cap {
  462. return true
  463. }
  464. if a.allocator.procedure == nil {
  465. a.allocator = context.allocator
  466. }
  467. assert(a.allocator.procedure != nil)
  468. old_size := a.cap * size_of(E)
  469. new_size := capacity * size_of(E)
  470. allocator := a.allocator
  471. new_data, err := allocator.procedure(
  472. allocator.data, .Resize, new_size, align_of(E),
  473. a.data, old_size, loc,
  474. )
  475. if new_data == nil || err != nil {
  476. return false
  477. }
  478. a.data = raw_data(new_data)
  479. a.cap = capacity
  480. return true
  481. }
  482. @builtin
  483. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  484. if array == nil {
  485. return false
  486. }
  487. a := (^Raw_Dynamic_Array)(array)
  488. if length <= a.cap {
  489. a.len = max(length, 0)
  490. return true
  491. }
  492. if a.allocator.procedure == nil {
  493. a.allocator = context.allocator
  494. }
  495. assert(a.allocator.procedure != nil)
  496. old_size := a.cap * size_of(E)
  497. new_size := length * size_of(E)
  498. allocator := a.allocator
  499. new_data, err := allocator.procedure(
  500. allocator.data, .Resize, new_size, align_of(E),
  501. a.data, old_size, loc,
  502. )
  503. if new_data == nil || err != nil {
  504. return false
  505. }
  506. a.data = raw_data(new_data)
  507. a.len = length
  508. a.cap = length
  509. return true
  510. }
  511. /*
  512. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  513. If `new_cap` is negative, then `len(array)` is used.
  514. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  515. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  516. */
  517. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  518. if array == nil {
  519. return
  520. }
  521. a := (^Raw_Dynamic_Array)(array)
  522. new_cap := new_cap if new_cap >= 0 else a.len
  523. if new_cap > a.cap {
  524. return
  525. }
  526. if a.allocator.procedure == nil {
  527. a.allocator = context.allocator
  528. }
  529. assert(a.allocator.procedure != nil)
  530. old_size := a.cap * size_of(E)
  531. new_size := new_cap * size_of(E)
  532. new_data, err := a.allocator.procedure(
  533. a.allocator.data,
  534. .Resize,
  535. new_size,
  536. align_of(E),
  537. a.data,
  538. old_size,
  539. loc,
  540. )
  541. if err != nil {
  542. return
  543. }
  544. a.data = raw_data(new_data)
  545. a.len = min(new_cap, a.len)
  546. a.cap = new_cap
  547. return true
  548. }
  549. @builtin
  550. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  551. key, value := key, value
  552. h := __get_map_header(m)
  553. hash := __get_map_hash(&key)
  554. data := uintptr(__dynamic_map_set(h, hash, &value, loc))
  555. return (^V)(data + h.value_offset)
  556. }
  557. @builtin
  558. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  559. s^ |= {elem}
  560. }
  561. @builtin
  562. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  563. for elem in elems {
  564. s^ |= {elem}
  565. }
  566. }
  567. @builtin
  568. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  569. s^ |= other
  570. }
  571. @builtin
  572. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  573. s^ &~= {elem}
  574. }
  575. @builtin
  576. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  577. for elem in elems {
  578. s^ &~= {elem}
  579. }
  580. }
  581. @builtin
  582. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  583. s^ &~= other
  584. }
  585. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
  586. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
  587. @builtin
  588. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  589. when size_of(S) == 1 {
  590. return int(intrinsics.count_ones(transmute(u8)s))
  591. } else when size_of(S) == 2 {
  592. return int(intrinsics.count_ones(transmute(u16)s))
  593. } else when size_of(S) == 4 {
  594. return int(intrinsics.count_ones(transmute(u32)s))
  595. } else when size_of(S) == 8 {
  596. return int(intrinsics.count_ones(transmute(u64)s))
  597. } else when size_of(S) == 16 {
  598. return int(intrinsics.count_ones(transmute(u128)s))
  599. } else {
  600. #panic("Unhandled card bit_set size")
  601. }
  602. }
  603. @builtin
  604. raw_array_data :: proc "contextless" (a: $P/^($T/[$N]$E)) -> [^]E {
  605. return ([^]E)(a)
  606. }
  607. @builtin
  608. raw_simd_data :: proc "contextless" (a: $P/^($T/#simd[$N]$E)) -> [^]E {
  609. return ([^]E)(a)
  610. }
  611. @builtin
  612. raw_slice_data :: proc "contextless" (s: $S/[]$E) -> [^]E {
  613. ptr := (transmute(Raw_Slice)s).data
  614. return ([^]E)(ptr)
  615. }
  616. @builtin
  617. raw_dynamic_array_data :: proc "contextless" (s: $S/[dynamic]$E) -> [^]E {
  618. ptr := (transmute(Raw_Dynamic_Array)s).data
  619. return ([^]E)(ptr)
  620. }
  621. @builtin
  622. raw_string_data :: proc "contextless" (s: $S/string) -> [^]u8 {
  623. return (transmute(Raw_String)s).data
  624. }
  625. @builtin
  626. raw_data :: proc{raw_array_data, raw_slice_data, raw_dynamic_array_data, raw_string_data, raw_simd_data}
  627. @builtin
  628. @(disabled=ODIN_DISABLE_ASSERT)
  629. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  630. if !condition {
  631. // NOTE(bill): This is wrapped in a procedure call
  632. // to improve performance to make the CPU not
  633. // execute speculatively, making it about an order of
  634. // magnitude faster
  635. @(cold)
  636. internal :: proc(message: string, loc: Source_Code_Location) {
  637. p := context.assertion_failure_proc
  638. if p == nil {
  639. p = default_assertion_failure_proc
  640. }
  641. p("runtime assertion", message, loc)
  642. }
  643. internal(message, loc)
  644. }
  645. }
  646. @builtin
  647. @(disabled=ODIN_DISABLE_ASSERT)
  648. panic :: proc(message: string, loc := #caller_location) -> ! {
  649. p := context.assertion_failure_proc
  650. if p == nil {
  651. p = default_assertion_failure_proc
  652. }
  653. p("panic", message, loc)
  654. }
  655. @builtin
  656. @(disabled=ODIN_DISABLE_ASSERT)
  657. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  658. p := context.assertion_failure_proc
  659. if p == nil {
  660. p = default_assertion_failure_proc
  661. }
  662. p("not yet implemented", message, loc)
  663. }
  664. @builtin
  665. @(disabled=ODIN_DISABLE_ASSERT)
  666. unreachable :: proc(message := "", loc := #caller_location) -> ! {
  667. p := context.assertion_failure_proc
  668. if p == nil {
  669. p = default_assertion_failure_proc
  670. }
  671. if message != "" {
  672. p("internal error", message, loc)
  673. } else {
  674. p("internal error", "entered unreachable code", loc)
  675. }
  676. }