core.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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_Relative_Pointer :: struct {
  156. pointer: ^Type_Info, // ^T
  157. base_integer: ^Type_Info,
  158. }
  159. Type_Info_Relative_Multi_Pointer :: struct {
  160. pointer: ^Type_Info, // [^]T
  161. base_integer: ^Type_Info,
  162. }
  163. Type_Info_Matrix :: struct {
  164. elem: ^Type_Info,
  165. elem_size: int,
  166. elem_stride: int, // elem_stride >= row_count
  167. row_count: int,
  168. column_count: int,
  169. // Total element count = column_count * elem_stride
  170. layout: enum u8 {
  171. Column_Major, // array of column vectors
  172. Row_Major, // array of row vectors
  173. },
  174. }
  175. Type_Info_Soa_Pointer :: struct {
  176. elem: ^Type_Info,
  177. }
  178. Type_Info_Bit_Field :: struct {
  179. backing_type: ^Type_Info,
  180. names: [^]string `fmt:"v,field_count"`,
  181. types: [^]^Type_Info `fmt:"v,field_count"`,
  182. bit_sizes: [^]uintptr `fmt:"v,field_count"`,
  183. bit_offsets: [^]uintptr `fmt:"v,field_count"`,
  184. tags: [^]string `fmt:"v,field_count"`,
  185. field_count: int,
  186. }
  187. Type_Info_Flag :: enum u8 {
  188. Comparable = 0,
  189. Simple_Compare = 1,
  190. }
  191. Type_Info_Flags :: distinct bit_set[Type_Info_Flag; u32]
  192. Type_Info :: struct {
  193. size: int,
  194. align: int,
  195. flags: Type_Info_Flags,
  196. id: typeid,
  197. variant: union {
  198. Type_Info_Named,
  199. Type_Info_Integer,
  200. Type_Info_Rune,
  201. Type_Info_Float,
  202. Type_Info_Complex,
  203. Type_Info_Quaternion,
  204. Type_Info_String,
  205. Type_Info_Boolean,
  206. Type_Info_Any,
  207. Type_Info_Type_Id,
  208. Type_Info_Pointer,
  209. Type_Info_Multi_Pointer,
  210. Type_Info_Procedure,
  211. Type_Info_Array,
  212. Type_Info_Enumerated_Array,
  213. Type_Info_Dynamic_Array,
  214. Type_Info_Slice,
  215. Type_Info_Parameters,
  216. Type_Info_Struct,
  217. Type_Info_Union,
  218. Type_Info_Enum,
  219. Type_Info_Map,
  220. Type_Info_Bit_Set,
  221. Type_Info_Simd_Vector,
  222. Type_Info_Relative_Pointer,
  223. Type_Info_Relative_Multi_Pointer,
  224. Type_Info_Matrix,
  225. Type_Info_Soa_Pointer,
  226. Type_Info_Bit_Field,
  227. },
  228. }
  229. // NOTE(bill): This must match the compiler's
  230. Typeid_Kind :: enum u8 {
  231. Invalid,
  232. Integer,
  233. Rune,
  234. Float,
  235. Complex,
  236. Quaternion,
  237. String,
  238. Boolean,
  239. Any,
  240. Type_Id,
  241. Pointer,
  242. Multi_Pointer,
  243. Procedure,
  244. Array,
  245. Enumerated_Array,
  246. Dynamic_Array,
  247. Slice,
  248. Tuple,
  249. Struct,
  250. Union,
  251. Enum,
  252. Map,
  253. Bit_Set,
  254. Simd_Vector,
  255. Relative_Pointer,
  256. Relative_Multi_Pointer,
  257. Matrix,
  258. Soa_Pointer,
  259. Bit_Field,
  260. }
  261. #assert(len(Typeid_Kind) < 32)
  262. Typeid_Bit_Field :: bit_field uintptr {
  263. index: uintptr | 8*size_of(uintptr) - 8,
  264. kind: Typeid_Kind | 5, // Typeid_Kind
  265. named: bool | 1,
  266. special: bool | 1, // signed, cstring, etc
  267. reserved: bool | 1,
  268. }
  269. #assert(size_of(Typeid_Bit_Field) == size_of(uintptr))
  270. // NOTE(bill): only the ones that are needed (not all types)
  271. // This will be set by the compiler
  272. type_table: []^Type_Info
  273. args__: []cstring
  274. when ODIN_OS == .Windows {
  275. // NOTE(Jeroen): If we're a Windows DLL, fwdReason will be populated.
  276. // This tells a DLL if it's first loaded, about to be unloaded, or a thread is joining/exiting.
  277. DLL_Forward_Reason :: enum u32 {
  278. Process_Detach = 0, // About to unload DLL
  279. Process_Attach = 1, // Entry point
  280. Thread_Attach = 2,
  281. Thread_Detach = 3,
  282. }
  283. dll_forward_reason: DLL_Forward_Reason
  284. dll_instance: rawptr
  285. }
  286. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  287. Source_Code_Location :: struct {
  288. file_path: string,
  289. line, column: i32,
  290. procedure: string,
  291. }
  292. /*
  293. Used by the built-in directory `#load_directory(path: string) -> []Load_Directory_File`
  294. */
  295. Load_Directory_File :: struct {
  296. name: string,
  297. data: []byte, // immutable data
  298. }
  299. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !
  300. // Allocation Stuff
  301. Allocator_Mode :: enum byte {
  302. Alloc,
  303. Free,
  304. Free_All,
  305. Resize,
  306. Query_Features,
  307. Query_Info,
  308. Alloc_Non_Zeroed,
  309. Resize_Non_Zeroed,
  310. }
  311. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
  312. Allocator_Query_Info :: struct {
  313. pointer: rawptr,
  314. size: Maybe(int),
  315. alignment: Maybe(int),
  316. }
  317. Allocator_Error :: enum byte {
  318. None = 0,
  319. Out_Of_Memory = 1,
  320. Invalid_Pointer = 2,
  321. Invalid_Argument = 3,
  322. Mode_Not_Implemented = 4,
  323. }
  324. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  325. size, alignment: int,
  326. old_memory: rawptr, old_size: int,
  327. location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error)
  328. Allocator :: struct {
  329. procedure: Allocator_Proc,
  330. data: rawptr,
  331. }
  332. Byte :: 1
  333. Kilobyte :: 1024 * Byte
  334. Megabyte :: 1024 * Kilobyte
  335. Gigabyte :: 1024 * Megabyte
  336. Terabyte :: 1024 * Gigabyte
  337. Petabyte :: 1024 * Terabyte
  338. Exabyte :: 1024 * Petabyte
  339. // Logging stuff
  340. Logger_Level :: enum uint {
  341. Debug = 0,
  342. Info = 10,
  343. Warning = 20,
  344. Error = 30,
  345. Fatal = 40,
  346. }
  347. Logger_Option :: enum {
  348. Level,
  349. Date,
  350. Time,
  351. Short_File_Path,
  352. Long_File_Path,
  353. Line,
  354. Procedure,
  355. Terminal_Color,
  356. Thread_Id,
  357. }
  358. Logger_Options :: bit_set[Logger_Option]
  359. Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location)
  360. Logger :: struct {
  361. procedure: Logger_Proc,
  362. data: rawptr,
  363. lowest_level: Logger_Level,
  364. options: Logger_Options,
  365. }
  366. Random_Generator_Mode :: enum {
  367. Read,
  368. Reset,
  369. Query_Info,
  370. }
  371. Random_Generator_Query_Info_Flag :: enum u32 {
  372. Cryptographic,
  373. Uniform,
  374. External_Entropy,
  375. Resettable,
  376. }
  377. Random_Generator_Query_Info :: distinct bit_set[Random_Generator_Query_Info_Flag; u32]
  378. Random_Generator_Proc :: #type proc(data: rawptr, mode: Random_Generator_Mode, p: []byte)
  379. Random_Generator :: struct {
  380. procedure: Random_Generator_Proc,
  381. data: rawptr,
  382. }
  383. Context :: struct {
  384. allocator: Allocator,
  385. temp_allocator: Allocator,
  386. assertion_failure_proc: Assertion_Failure_Proc,
  387. logger: Logger,
  388. random_generator: Random_Generator,
  389. user_ptr: rawptr,
  390. user_index: int,
  391. // Internal use only
  392. _internal: rawptr,
  393. }
  394. Raw_String :: struct {
  395. data: [^]byte,
  396. len: int,
  397. }
  398. Raw_Slice :: struct {
  399. data: rawptr,
  400. len: int,
  401. }
  402. Raw_Dynamic_Array :: struct {
  403. data: rawptr,
  404. len: int,
  405. cap: int,
  406. allocator: Allocator,
  407. }
  408. // The raw, type-erased representation of a map.
  409. //
  410. // 32-bytes on 64-bit
  411. // 16-bytes on 32-bit
  412. Raw_Map :: struct {
  413. // A single allocation spanning all keys, values, and hashes.
  414. // {
  415. // k: Map_Cell(K) * (capacity / ks_per_cell)
  416. // v: Map_Cell(V) * (capacity / vs_per_cell)
  417. // h: Map_Cell(H) * (capacity / hs_per_cell)
  418. // }
  419. //
  420. // The data is allocated assuming 64-byte alignment, meaning the address is
  421. // always a multiple of 64. This means we have 6 bits of zeros in the pointer
  422. // to store the capacity. We can store a value as large as 2^6-1 or 63 in
  423. // there. This conveniently is the maximum log2 capacity we can have for a map
  424. // as Odin uses signed integers to represent capacity.
  425. //
  426. // Since the hashes are backed by Map_Hash, which is just a 64-bit unsigned
  427. // integer, the cell structure for hashes is unnecessary because 64/8 is 8 and
  428. // requires no padding, meaning it can be indexed as a regular array of
  429. // Map_Hash directly, though for consistency sake it's written as if it were
  430. // an array of Map_Cell(Map_Hash).
  431. data: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  432. len: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  433. allocator: Allocator, // 16-bytes on 64-bits, 8-bytes on 32-bits
  434. }
  435. Raw_Any :: struct {
  436. data: rawptr,
  437. id: typeid,
  438. }
  439. Raw_Cstring :: struct {
  440. data: [^]byte,
  441. }
  442. Raw_Soa_Pointer :: struct {
  443. data: rawptr,
  444. index: int,
  445. }
  446. Raw_Complex32 :: struct {real, imag: f16}
  447. Raw_Complex64 :: struct {real, imag: f32}
  448. Raw_Complex128 :: struct {real, imag: f64}
  449. Raw_Quaternion64 :: struct {imag, jmag, kmag: f16, real: f16}
  450. Raw_Quaternion128 :: struct {imag, jmag, kmag: f32, real: f32}
  451. Raw_Quaternion256 :: struct {imag, jmag, kmag: f64, real: f64}
  452. Raw_Quaternion64_Vector_Scalar :: struct {vector: [3]f16, scalar: f16}
  453. Raw_Quaternion128_Vector_Scalar :: struct {vector: [3]f32, scalar: f32}
  454. Raw_Quaternion256_Vector_Scalar :: struct {vector: [3]f64, scalar: f64}
  455. /*
  456. // Defined internally by the compiler
  457. Odin_OS_Type :: enum int {
  458. Unknown,
  459. Windows,
  460. Darwin,
  461. Linux,
  462. Essence,
  463. FreeBSD,
  464. OpenBSD,
  465. NetBSD,
  466. Haiku,
  467. WASI,
  468. JS,
  469. Orca,
  470. Freestanding,
  471. }
  472. */
  473. Odin_OS_Type :: type_of(ODIN_OS)
  474. /*
  475. // Defined internally by the compiler
  476. Odin_Arch_Type :: enum int {
  477. Unknown,
  478. amd64,
  479. i386,
  480. arm32,
  481. arm64,
  482. wasm32,
  483. wasm64p32,
  484. }
  485. */
  486. Odin_Arch_Type :: type_of(ODIN_ARCH)
  487. /*
  488. // Defined internally by the compiler
  489. Odin_Build_Mode_Type :: enum int {
  490. Executable,
  491. Dynamic,
  492. Static,
  493. Object,
  494. Assembly,
  495. LLVM_IR,
  496. }
  497. */
  498. Odin_Build_Mode_Type :: type_of(ODIN_BUILD_MODE)
  499. /*
  500. // Defined internally by the compiler
  501. Odin_Endian_Type :: enum int {
  502. Unknown,
  503. Little,
  504. Big,
  505. }
  506. */
  507. Odin_Endian_Type :: type_of(ODIN_ENDIAN)
  508. /*
  509. // Defined internally by the compiler
  510. Odin_Platform_Subtarget_Type :: enum int {
  511. Default,
  512. iOS,
  513. }
  514. */
  515. Odin_Platform_Subtarget_Type :: type_of(ODIN_PLATFORM_SUBTARGET)
  516. /*
  517. // Defined internally by the compiler
  518. Odin_Sanitizer_Flag :: enum u32 {
  519. Address = 0,
  520. Memory = 1,
  521. Thread = 2,
  522. }
  523. Odin_Sanitizer_Flags :: distinct bit_set[Odin_Sanitizer_Flag; u32]
  524. ODIN_SANITIZER_FLAGS // is a constant
  525. */
  526. Odin_Sanitizer_Flags :: type_of(ODIN_SANITIZER_FLAGS)
  527. /*
  528. // Defined internally by the compiler
  529. Odin_Optimization_Mode :: enum int {
  530. None = -1,
  531. Minimal = 0,
  532. Size = 1,
  533. Speed = 2,
  534. Aggressive = 3,
  535. }
  536. ODIN_OPTIMIZATION_MODE // is a constant
  537. */
  538. Odin_Optimization_Mode :: type_of(ODIN_OPTIMIZATION_MODE)
  539. /////////////////////////////
  540. // Init Startup Procedures //
  541. /////////////////////////////
  542. // IMPORTANT NOTE(bill): Do not call this unless you want to explicitly set up the entry point and how it gets called
  543. // This is probably only useful for freestanding targets
  544. foreign {
  545. @(link_name="__$startup_runtime")
  546. _startup_runtime :: proc "odin" () ---
  547. @(link_name="__$cleanup_runtime")
  548. _cleanup_runtime :: proc "odin" () ---
  549. }
  550. _cleanup_runtime_contextless :: proc "contextless" () {
  551. context = default_context()
  552. _cleanup_runtime()
  553. }
  554. /////////////////////////////
  555. /////////////////////////////
  556. /////////////////////////////
  557. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  558. if info == nil {
  559. return nil
  560. }
  561. base := info
  562. loop: for {
  563. #partial switch i in base.variant {
  564. case Type_Info_Named: base = i.base
  565. case: break loop
  566. }
  567. }
  568. return base
  569. }
  570. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  571. if info == nil {
  572. return nil
  573. }
  574. base := info
  575. loop: for {
  576. #partial switch i in base.variant {
  577. case Type_Info_Named: base = i.base
  578. case Type_Info_Enum: base = i.base
  579. case Type_Info_Bit_Field: base = i.backing_type
  580. case: break loop
  581. }
  582. }
  583. return base
  584. }
  585. type_info_base_without_enum :: type_info_core
  586. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
  587. MASK :: 1<<(8*size_of(typeid) - 8) - 1
  588. data := transmute(uintptr)id
  589. n := int(data & MASK)
  590. if n < 0 || n >= len(type_table) {
  591. n = 0
  592. }
  593. return type_table[n]
  594. }
  595. when !ODIN_NO_RTTI {
  596. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  597. ti := type_info_of(id)
  598. ti = type_info_base(ti)
  599. return ti.id
  600. }
  601. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  602. ti := type_info_core(type_info_of(id))
  603. return ti.id
  604. }
  605. typeid_base_without_enum :: typeid_core
  606. }
  607. debug_trap :: intrinsics.debug_trap
  608. trap :: intrinsics.trap
  609. read_cycle_counter :: intrinsics.read_cycle_counter
  610. default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location) {
  611. // Nothing
  612. }
  613. default_logger :: proc() -> Logger {
  614. return Logger{default_logger_proc, nil, Logger_Level.Debug, nil}
  615. }
  616. default_context :: proc "contextless" () -> Context {
  617. c: Context
  618. __init_context(&c)
  619. return c
  620. }
  621. @private
  622. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  623. if c == nil {
  624. return
  625. }
  626. c^ = other^
  627. __init_context(c)
  628. }
  629. @private
  630. __init_context :: proc "contextless" (c: ^Context) {
  631. if c == nil {
  632. return
  633. }
  634. // NOTE(bill): Do not initialize these procedures with a call as they are not defined with the "contextless" calling convention
  635. c.allocator.procedure = default_allocator_proc
  636. c.allocator.data = nil
  637. c.temp_allocator.procedure = default_temp_allocator_proc
  638. when !NO_DEFAULT_TEMP_ALLOCATOR {
  639. c.temp_allocator.data = &global_default_temp_allocator_data
  640. }
  641. when !ODIN_DISABLE_ASSERT {
  642. c.assertion_failure_proc = default_assertion_failure_proc
  643. }
  644. c.logger.procedure = default_logger_proc
  645. c.logger.data = nil
  646. c.random_generator.procedure = default_random_generator_proc
  647. c.random_generator.data = nil
  648. }
  649. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
  650. when ODIN_OS == .Freestanding {
  651. // Do nothing
  652. } else {
  653. when ODIN_OS != .Orca && !ODIN_DISABLE_ASSERT {
  654. print_caller_location(loc)
  655. print_string(" ")
  656. }
  657. print_string(prefix)
  658. if len(message) > 0 {
  659. print_string(": ")
  660. print_string(message)
  661. }
  662. when ODIN_OS == .Orca {
  663. assert_fail(
  664. cstring(raw_data(loc.file_path)),
  665. cstring(raw_data(loc.procedure)),
  666. loc.line,
  667. "",
  668. cstring(raw_data(orca_stderr_buffer[:orca_stderr_buffer_idx])),
  669. )
  670. } else {
  671. print_byte('\n')
  672. }
  673. }
  674. trap()
  675. }