core.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. //
  5. // Naming Conventions:
  6. // In general, Ada_Case for types and snake_case for values
  7. //
  8. // Package Name: snake_case (but prefer single word)
  9. // Import Name: snake_case (but prefer single word)
  10. // Types: Ada_Case
  11. // Enum Values: Ada_Case
  12. // Procedures: snake_case
  13. // Local Variables: snake_case
  14. // Constant Variables: SCREAMING_SNAKE_CASE
  15. //
  16. // IMPORTANT NOTE(bill): `type_info_of` cannot be used within a
  17. // #shared_global_scope due to the internals of the compiler.
  18. // This could change at a later date if the all these data structures are
  19. // implemented within the compiler rather than in this "preload" file
  20. //
  21. #+no-instrumentation
  22. package runtime
  23. import "base:intrinsics"
  24. // NOTE(bill): This must match the compiler's
  25. Calling_Convention :: enum u8 {
  26. Invalid = 0,
  27. Odin = 1,
  28. Contextless = 2,
  29. CDecl = 3,
  30. Std_Call = 4,
  31. Fast_Call = 5,
  32. None = 6,
  33. Naked = 7,
  34. _ = 8, // reserved
  35. Win64 = 9,
  36. SysV = 10,
  37. }
  38. Type_Info_Enum_Value :: distinct i64
  39. Platform_Endianness :: enum u8 {
  40. Platform = 0,
  41. Little = 1,
  42. Big = 2,
  43. }
  44. // Procedure type to test whether two values of the same type are equal
  45. Equal_Proc :: distinct proc "contextless" (rawptr, rawptr) -> bool
  46. // Procedure type to hash a value, default seed value is 0
  47. Hasher_Proc :: distinct proc "contextless" (data: rawptr, seed: uintptr = 0) -> uintptr
  48. Type_Info_Struct_Soa_Kind :: enum u8 {
  49. None = 0,
  50. Fixed = 1,
  51. Slice = 2,
  52. Dynamic = 3,
  53. }
  54. // Variant Types
  55. Type_Info_Named :: struct {
  56. name: string,
  57. base: ^Type_Info,
  58. pkg: string,
  59. loc: ^Source_Code_Location,
  60. }
  61. Type_Info_Integer :: struct {signed: bool, endianness: Platform_Endianness}
  62. Type_Info_Rune :: struct {}
  63. Type_Info_Float :: struct {endianness: Platform_Endianness}
  64. Type_Info_Complex :: struct {}
  65. Type_Info_Quaternion :: struct {}
  66. Type_Info_String :: struct {is_cstring: bool}
  67. Type_Info_Boolean :: struct {}
  68. Type_Info_Any :: struct {}
  69. Type_Info_Type_Id :: struct {}
  70. Type_Info_Pointer :: struct {
  71. elem: ^Type_Info, // nil -> rawptr
  72. }
  73. Type_Info_Multi_Pointer :: struct {
  74. elem: ^Type_Info,
  75. }
  76. Type_Info_Procedure :: struct {
  77. params: ^Type_Info, // Type_Info_Parameters
  78. results: ^Type_Info, // Type_Info_Parameters
  79. variadic: bool,
  80. convention: Calling_Convention,
  81. }
  82. Type_Info_Array :: struct {
  83. elem: ^Type_Info,
  84. elem_size: int,
  85. count: int,
  86. }
  87. Type_Info_Enumerated_Array :: struct {
  88. elem: ^Type_Info,
  89. index: ^Type_Info,
  90. elem_size: int,
  91. count: int,
  92. min_value: Type_Info_Enum_Value,
  93. max_value: Type_Info_Enum_Value,
  94. is_sparse: bool,
  95. }
  96. Type_Info_Dynamic_Array :: struct {elem: ^Type_Info, elem_size: int}
  97. Type_Info_Slice :: struct {elem: ^Type_Info, elem_size: int}
  98. Type_Info_Parameters :: struct { // Only used for procedures parameters and results
  99. types: []^Type_Info,
  100. names: []string,
  101. }
  102. Type_Info_Tuple :: Type_Info_Parameters // Will be removed eventually
  103. Type_Info_Struct_Flags :: distinct bit_set[Type_Info_Struct_Flag; u8]
  104. Type_Info_Struct_Flag :: enum u8 {
  105. packed = 0,
  106. raw_union = 1,
  107. no_copy = 2,
  108. align = 3,
  109. }
  110. Type_Info_Struct :: struct {
  111. // Slice these with `field_count`
  112. types: [^]^Type_Info `fmt:"v,field_count"`,
  113. names: [^]string `fmt:"v,field_count"`,
  114. offsets: [^]uintptr `fmt:"v,field_count"`,
  115. usings: [^]bool `fmt:"v,field_count"`,
  116. tags: [^]string `fmt:"v,field_count"`,
  117. field_count: i32,
  118. flags: Type_Info_Struct_Flags,
  119. // These are only set iff this structure is an SOA structure
  120. soa_kind: Type_Info_Struct_Soa_Kind,
  121. soa_len: i32,
  122. soa_base_type: ^Type_Info,
  123. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  124. }
  125. Type_Info_Union :: struct {
  126. variants: []^Type_Info,
  127. tag_offset: uintptr,
  128. tag_type: ^Type_Info,
  129. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  130. custom_align: bool,
  131. no_nil: bool,
  132. shared_nil: bool,
  133. }
  134. Type_Info_Enum :: struct {
  135. base: ^Type_Info,
  136. names: []string,
  137. values: []Type_Info_Enum_Value,
  138. }
  139. Type_Info_Map :: struct {
  140. key: ^Type_Info,
  141. value: ^Type_Info,
  142. map_info: ^Map_Info,
  143. }
  144. Type_Info_Bit_Set :: struct {
  145. elem: ^Type_Info,
  146. underlying: ^Type_Info, // Possibly nil
  147. lower: i64,
  148. upper: i64,
  149. }
  150. Type_Info_Simd_Vector :: struct {
  151. elem: ^Type_Info,
  152. elem_size: int,
  153. count: int,
  154. }
  155. Type_Info_Matrix :: struct {
  156. elem: ^Type_Info,
  157. elem_size: int,
  158. elem_stride: int, // elem_stride >= row_count
  159. row_count: int,
  160. column_count: int,
  161. // Total element count = column_count * elem_stride
  162. layout: enum u8 {
  163. Column_Major, // array of column vectors
  164. Row_Major, // array of row vectors
  165. },
  166. }
  167. Type_Info_Soa_Pointer :: struct {
  168. elem: ^Type_Info,
  169. }
  170. Type_Info_Bit_Field :: struct {
  171. backing_type: ^Type_Info,
  172. names: [^]string `fmt:"v,field_count"`,
  173. types: [^]^Type_Info `fmt:"v,field_count"`,
  174. bit_sizes: [^]uintptr `fmt:"v,field_count"`,
  175. bit_offsets: [^]uintptr `fmt:"v,field_count"`,
  176. tags: [^]string `fmt:"v,field_count"`,
  177. field_count: int,
  178. }
  179. Type_Info_Flag :: enum u8 {
  180. Comparable = 0,
  181. Simple_Compare = 1,
  182. }
  183. Type_Info_Flags :: distinct bit_set[Type_Info_Flag; u32]
  184. Type_Info :: struct {
  185. size: int,
  186. align: int,
  187. flags: Type_Info_Flags,
  188. id: typeid,
  189. variant: union {
  190. Type_Info_Named,
  191. Type_Info_Integer,
  192. Type_Info_Rune,
  193. Type_Info_Float,
  194. Type_Info_Complex,
  195. Type_Info_Quaternion,
  196. Type_Info_String,
  197. Type_Info_Boolean,
  198. Type_Info_Any,
  199. Type_Info_Type_Id,
  200. Type_Info_Pointer,
  201. Type_Info_Multi_Pointer,
  202. Type_Info_Procedure,
  203. Type_Info_Array,
  204. Type_Info_Enumerated_Array,
  205. Type_Info_Dynamic_Array,
  206. Type_Info_Slice,
  207. Type_Info_Parameters,
  208. Type_Info_Struct,
  209. Type_Info_Union,
  210. Type_Info_Enum,
  211. Type_Info_Map,
  212. Type_Info_Bit_Set,
  213. Type_Info_Simd_Vector,
  214. Type_Info_Matrix,
  215. Type_Info_Soa_Pointer,
  216. Type_Info_Bit_Field,
  217. },
  218. }
  219. // NOTE(bill): only the ones that are needed (not all types)
  220. // This will be set by the compiler
  221. type_table: []^Type_Info
  222. args__: []cstring
  223. when ODIN_OS == .Windows {
  224. // NOTE(Jeroen): If we're a Windows DLL, fwdReason will be populated.
  225. // This tells a DLL if it's first loaded, about to be unloaded, or a thread is joining/exiting.
  226. DLL_Forward_Reason :: enum u32 {
  227. Process_Detach = 0, // About to unload DLL
  228. Process_Attach = 1, // Entry point
  229. Thread_Attach = 2,
  230. Thread_Detach = 3,
  231. }
  232. dll_forward_reason: DLL_Forward_Reason
  233. dll_instance: rawptr
  234. }
  235. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  236. Source_Code_Location :: struct {
  237. file_path: string,
  238. line, column: i32,
  239. procedure: string,
  240. }
  241. /*
  242. Used by the built-in directory `#load_directory(path: string) -> []Load_Directory_File`
  243. */
  244. Load_Directory_File :: struct {
  245. name: string,
  246. data: []byte, // immutable data
  247. }
  248. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !
  249. // Allocation Stuff
  250. Allocator_Mode :: enum byte {
  251. Alloc,
  252. Free,
  253. Free_All,
  254. Resize,
  255. Query_Features,
  256. Query_Info,
  257. Alloc_Non_Zeroed,
  258. Resize_Non_Zeroed,
  259. }
  260. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
  261. Allocator_Query_Info :: struct {
  262. pointer: rawptr,
  263. size: Maybe(int),
  264. alignment: Maybe(int),
  265. }
  266. Allocator_Error :: enum byte {
  267. None = 0,
  268. Out_Of_Memory = 1,
  269. Invalid_Pointer = 2,
  270. Invalid_Argument = 3,
  271. Mode_Not_Implemented = 4,
  272. }
  273. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  274. size, alignment: int,
  275. old_memory: rawptr, old_size: int,
  276. location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error)
  277. Allocator :: struct {
  278. procedure: Allocator_Proc,
  279. data: rawptr,
  280. }
  281. Byte :: 1
  282. Kilobyte :: 1024 * Byte
  283. Megabyte :: 1024 * Kilobyte
  284. Gigabyte :: 1024 * Megabyte
  285. Terabyte :: 1024 * Gigabyte
  286. Petabyte :: 1024 * Terabyte
  287. Exabyte :: 1024 * Petabyte
  288. // Logging stuff
  289. Logger_Level :: enum uint {
  290. Debug = 0,
  291. Info = 10,
  292. Warning = 20,
  293. Error = 30,
  294. Fatal = 40,
  295. }
  296. Logger_Option :: enum {
  297. Level,
  298. Date,
  299. Time,
  300. Short_File_Path,
  301. Long_File_Path,
  302. Line,
  303. Procedure,
  304. Terminal_Color,
  305. Thread_Id,
  306. }
  307. Logger_Options :: bit_set[Logger_Option]
  308. Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location)
  309. Logger :: struct {
  310. procedure: Logger_Proc,
  311. data: rawptr,
  312. lowest_level: Logger_Level,
  313. options: Logger_Options,
  314. }
  315. Random_Generator_Mode :: enum {
  316. Read,
  317. Reset,
  318. Query_Info,
  319. }
  320. Random_Generator_Query_Info_Flag :: enum u32 {
  321. Cryptographic,
  322. Uniform,
  323. External_Entropy,
  324. Resettable,
  325. }
  326. Random_Generator_Query_Info :: distinct bit_set[Random_Generator_Query_Info_Flag; u32]
  327. Random_Generator_Proc :: #type proc(data: rawptr, mode: Random_Generator_Mode, p: []byte)
  328. Random_Generator :: struct {
  329. procedure: Random_Generator_Proc,
  330. data: rawptr,
  331. }
  332. Context :: struct {
  333. allocator: Allocator,
  334. temp_allocator: Allocator,
  335. assertion_failure_proc: Assertion_Failure_Proc,
  336. logger: Logger,
  337. random_generator: Random_Generator,
  338. user_ptr: rawptr,
  339. user_index: int,
  340. // Internal use only
  341. _internal: rawptr,
  342. }
  343. Raw_String :: struct {
  344. data: [^]byte,
  345. len: int,
  346. }
  347. Raw_Slice :: struct {
  348. data: rawptr,
  349. len: int,
  350. }
  351. Raw_Dynamic_Array :: struct {
  352. data: rawptr,
  353. len: int,
  354. cap: int,
  355. allocator: Allocator,
  356. }
  357. // The raw, type-erased representation of a map.
  358. //
  359. // 32-bytes on 64-bit
  360. // 16-bytes on 32-bit
  361. Raw_Map :: struct {
  362. // A single allocation spanning all keys, values, and hashes.
  363. // {
  364. // k: Map_Cell(K) * (capacity / ks_per_cell)
  365. // v: Map_Cell(V) * (capacity / vs_per_cell)
  366. // h: Map_Cell(H) * (capacity / hs_per_cell)
  367. // }
  368. //
  369. // The data is allocated assuming 64-byte alignment, meaning the address is
  370. // always a multiple of 64. This means we have 6 bits of zeros in the pointer
  371. // to store the capacity. We can store a value as large as 2^6-1 or 63 in
  372. // there. This conveniently is the maximum log2 capacity we can have for a map
  373. // as Odin uses signed integers to represent capacity.
  374. //
  375. // Since the hashes are backed by Map_Hash, which is just a 64-bit unsigned
  376. // integer, the cell structure for hashes is unnecessary because 64/8 is 8 and
  377. // requires no padding, meaning it can be indexed as a regular array of
  378. // Map_Hash directly, though for consistency sake it's written as if it were
  379. // an array of Map_Cell(Map_Hash).
  380. data: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  381. len: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  382. allocator: Allocator, // 16-bytes on 64-bits, 8-bytes on 32-bits
  383. }
  384. Raw_Any :: struct {
  385. data: rawptr,
  386. id: typeid,
  387. }
  388. when !ODIN_NO_RTTI {
  389. #assert(size_of(Raw_Any) == size_of(any))
  390. }
  391. Raw_Cstring :: struct {
  392. data: [^]byte,
  393. }
  394. #assert(size_of(Raw_Cstring) == size_of(cstring))
  395. Raw_Soa_Pointer :: struct {
  396. data: rawptr,
  397. index: int,
  398. }
  399. Raw_Complex32 :: struct {real, imag: f16}
  400. Raw_Complex64 :: struct {real, imag: f32}
  401. Raw_Complex128 :: struct {real, imag: f64}
  402. Raw_Quaternion64 :: struct {imag, jmag, kmag: f16, real: f16}
  403. Raw_Quaternion128 :: struct {imag, jmag, kmag: f32, real: f32}
  404. Raw_Quaternion256 :: struct {imag, jmag, kmag: f64, real: f64}
  405. Raw_Quaternion64_Vector_Scalar :: struct {vector: [3]f16, scalar: f16}
  406. Raw_Quaternion128_Vector_Scalar :: struct {vector: [3]f32, scalar: f32}
  407. Raw_Quaternion256_Vector_Scalar :: struct {vector: [3]f64, scalar: f64}
  408. /*
  409. // Defined internally by the compiler
  410. Odin_OS_Type :: enum int {
  411. Unknown,
  412. Windows,
  413. Darwin,
  414. Linux,
  415. Essence,
  416. FreeBSD,
  417. OpenBSD,
  418. NetBSD,
  419. Haiku,
  420. WASI,
  421. JS,
  422. Orca,
  423. Freestanding,
  424. }
  425. */
  426. Odin_OS_Type :: type_of(ODIN_OS)
  427. /*
  428. // Defined internally by the compiler
  429. Odin_Arch_Type :: enum int {
  430. Unknown,
  431. amd64,
  432. i386,
  433. arm32,
  434. arm64,
  435. wasm32,
  436. wasm64p32,
  437. riscv64,
  438. }
  439. */
  440. Odin_Arch_Type :: type_of(ODIN_ARCH)
  441. Odin_Arch_Types :: bit_set[Odin_Arch_Type]
  442. ALL_ODIN_ARCH_TYPES :: Odin_Arch_Types{
  443. .amd64,
  444. .i386,
  445. .arm32,
  446. .arm64,
  447. .wasm32,
  448. .wasm64p32,
  449. .riscv64,
  450. }
  451. /*
  452. // Defined internally by the compiler
  453. Odin_Build_Mode_Type :: enum int {
  454. Executable,
  455. Dynamic,
  456. Static,
  457. Object,
  458. Assembly,
  459. LLVM_IR,
  460. }
  461. */
  462. Odin_Build_Mode_Type :: type_of(ODIN_BUILD_MODE)
  463. /*
  464. // Defined internally by the compiler
  465. Odin_Endian_Type :: enum int {
  466. Unknown,
  467. Little,
  468. Big,
  469. }
  470. */
  471. Odin_Endian_Type :: type_of(ODIN_ENDIAN)
  472. Odin_OS_Types :: bit_set[Odin_OS_Type]
  473. ALL_ODIN_OS_TYPES :: Odin_OS_Types{
  474. .Windows,
  475. .Darwin,
  476. .Linux,
  477. .Essence,
  478. .FreeBSD,
  479. .OpenBSD,
  480. .NetBSD,
  481. .Haiku,
  482. .WASI,
  483. .JS,
  484. .Orca,
  485. .Freestanding,
  486. }
  487. /*
  488. // Defined internally by the compiler
  489. Odin_Platform_Subtarget_Type :: enum int {
  490. Default,
  491. iOS,
  492. }
  493. */
  494. Odin_Platform_Subtarget_Type :: type_of(ODIN_PLATFORM_SUBTARGET)
  495. /*
  496. // Defined internally by the compiler
  497. Odin_Sanitizer_Flag :: enum u32 {
  498. Address = 0,
  499. Memory = 1,
  500. Thread = 2,
  501. }
  502. Odin_Sanitizer_Flags :: distinct bit_set[Odin_Sanitizer_Flag; u32]
  503. ODIN_SANITIZER_FLAGS // is a constant
  504. */
  505. Odin_Sanitizer_Flags :: type_of(ODIN_SANITIZER_FLAGS)
  506. /*
  507. // Defined internally by the compiler
  508. Odin_Optimization_Mode :: enum int {
  509. None = -1,
  510. Minimal = 0,
  511. Size = 1,
  512. Speed = 2,
  513. Aggressive = 3,
  514. }
  515. ODIN_OPTIMIZATION_MODE // is a constant
  516. */
  517. Odin_Optimization_Mode :: type_of(ODIN_OPTIMIZATION_MODE)
  518. /////////////////////////////
  519. // Init Startup Procedures //
  520. /////////////////////////////
  521. // IMPORTANT NOTE(bill): Do not call this unless you want to explicitly set up the entry point and how it gets called
  522. // This is probably only useful for freestanding targets
  523. foreign {
  524. @(link_name="__$startup_runtime")
  525. _startup_runtime :: proc "odin" () ---
  526. @(link_name="__$cleanup_runtime")
  527. _cleanup_runtime :: proc "odin" () ---
  528. }
  529. _cleanup_runtime_contextless :: proc "contextless" () {
  530. context = default_context()
  531. _cleanup_runtime()
  532. }
  533. /////////////////////////////
  534. /////////////////////////////
  535. /////////////////////////////
  536. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  537. if info == nil {
  538. return nil
  539. }
  540. base := info
  541. loop: for {
  542. #partial switch i in base.variant {
  543. case Type_Info_Named: base = i.base
  544. case: break loop
  545. }
  546. }
  547. return base
  548. }
  549. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  550. if info == nil {
  551. return nil
  552. }
  553. base := info
  554. loop: for {
  555. #partial switch i in base.variant {
  556. case Type_Info_Named: base = i.base
  557. case Type_Info_Enum: base = i.base
  558. case Type_Info_Bit_Field: base = i.backing_type
  559. case: break loop
  560. }
  561. }
  562. return base
  563. }
  564. type_info_base_without_enum :: type_info_core
  565. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
  566. n := u64(len(type_table))
  567. i := transmute(u64)id % n
  568. for _ in 0..<n {
  569. ptr := type_table[i]
  570. if ptr != nil && ptr.id == id {
  571. return ptr
  572. }
  573. i = i+1 if i+1 < n else 0
  574. }
  575. return type_table[0]
  576. }
  577. when !ODIN_NO_RTTI {
  578. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  579. ti := type_info_of(id)
  580. ti = type_info_base(ti)
  581. return ti.id
  582. }
  583. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  584. ti := type_info_core(type_info_of(id))
  585. return ti.id
  586. }
  587. typeid_base_without_enum :: typeid_core
  588. }
  589. debug_trap :: intrinsics.debug_trap
  590. trap :: intrinsics.trap
  591. read_cycle_counter :: intrinsics.read_cycle_counter
  592. default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location) {
  593. // Nothing
  594. }
  595. default_logger :: proc() -> Logger {
  596. return Logger{default_logger_proc, nil, Logger_Level.Debug, nil}
  597. }
  598. default_context :: proc "contextless" () -> Context {
  599. c: Context
  600. __init_context(&c)
  601. return c
  602. }
  603. @private
  604. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  605. if c == nil {
  606. return
  607. }
  608. c^ = other^
  609. __init_context(c)
  610. }
  611. @private
  612. __init_context :: proc "contextless" (c: ^Context) {
  613. if c == nil {
  614. return
  615. }
  616. // NOTE(bill): Do not initialize these procedures with a call as they are not defined with the "contextless" calling convention
  617. c.allocator.procedure = default_allocator_proc
  618. c.allocator.data = nil
  619. c.temp_allocator.procedure = default_temp_allocator_proc
  620. when !NO_DEFAULT_TEMP_ALLOCATOR {
  621. c.temp_allocator.data = &global_default_temp_allocator_data
  622. }
  623. when !ODIN_DISABLE_ASSERT {
  624. c.assertion_failure_proc = default_assertion_failure_proc
  625. }
  626. c.logger.procedure = default_logger_proc
  627. c.logger.data = nil
  628. c.random_generator.procedure = default_random_generator_proc
  629. c.random_generator.data = nil
  630. }
  631. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
  632. default_assertion_contextless_failure_proc(prefix, message, loc)
  633. }
  634. default_assertion_contextless_failure_proc :: proc "contextless" (prefix, message: string, loc: Source_Code_Location) -> ! {
  635. when ODIN_OS == .Freestanding {
  636. // Do nothing
  637. } else {
  638. when ODIN_OS != .Orca && !ODIN_DISABLE_ASSERT {
  639. print_caller_location(loc)
  640. print_string(" ")
  641. }
  642. print_string(prefix)
  643. if len(message) > 0 {
  644. print_string(": ")
  645. print_string(message)
  646. }
  647. when ODIN_OS == .Orca {
  648. assert_fail(
  649. cstring(raw_data(loc.file_path)),
  650. cstring(raw_data(loc.procedure)),
  651. loc.line,
  652. "",
  653. cstring(raw_data(orca_stderr_buffer[:orca_stderr_buffer_idx])),
  654. )
  655. } else {
  656. print_byte('\n')
  657. }
  658. }
  659. trap()
  660. }