core.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 :: struct {
  104. types: []^Type_Info,
  105. names: []string,
  106. offsets: []uintptr,
  107. usings: []bool,
  108. tags: []string,
  109. is_packed: bool,
  110. is_raw_union: bool,
  111. is_no_copy: bool,
  112. custom_align: bool,
  113. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  114. // These are only set iff this structure is an SOA structure
  115. soa_kind: Type_Info_Struct_Soa_Kind,
  116. soa_base_type: ^Type_Info,
  117. soa_len: int,
  118. }
  119. Type_Info_Union :: struct {
  120. variants: []^Type_Info,
  121. tag_offset: uintptr,
  122. tag_type: ^Type_Info,
  123. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  124. custom_align: bool,
  125. no_nil: bool,
  126. shared_nil: bool,
  127. }
  128. Type_Info_Enum :: struct {
  129. base: ^Type_Info,
  130. names: []string,
  131. values: []Type_Info_Enum_Value,
  132. }
  133. Type_Info_Map :: struct {
  134. key: ^Type_Info,
  135. value: ^Type_Info,
  136. map_info: ^Map_Info,
  137. }
  138. Type_Info_Bit_Set :: struct {
  139. elem: ^Type_Info,
  140. underlying: ^Type_Info, // Possibly nil
  141. lower: i64,
  142. upper: i64,
  143. }
  144. Type_Info_Simd_Vector :: struct {
  145. elem: ^Type_Info,
  146. elem_size: int,
  147. count: int,
  148. }
  149. Type_Info_Relative_Pointer :: struct {
  150. pointer: ^Type_Info, // ^T
  151. base_integer: ^Type_Info,
  152. }
  153. Type_Info_Relative_Multi_Pointer :: struct {
  154. pointer: ^Type_Info, // [^]T
  155. base_integer: ^Type_Info,
  156. }
  157. Type_Info_Matrix :: struct {
  158. elem: ^Type_Info,
  159. elem_size: int,
  160. elem_stride: int, // elem_stride >= row_count
  161. row_count: int,
  162. column_count: int,
  163. // Total element count = column_count * elem_stride
  164. }
  165. Type_Info_Soa_Pointer :: struct {
  166. elem: ^Type_Info,
  167. }
  168. Type_Info_Flag :: enum u8 {
  169. Comparable = 0,
  170. Simple_Compare = 1,
  171. }
  172. Type_Info_Flags :: distinct bit_set[Type_Info_Flag; u32]
  173. Type_Info :: struct {
  174. size: int,
  175. align: int,
  176. flags: Type_Info_Flags,
  177. id: typeid,
  178. variant: union {
  179. Type_Info_Named,
  180. Type_Info_Integer,
  181. Type_Info_Rune,
  182. Type_Info_Float,
  183. Type_Info_Complex,
  184. Type_Info_Quaternion,
  185. Type_Info_String,
  186. Type_Info_Boolean,
  187. Type_Info_Any,
  188. Type_Info_Type_Id,
  189. Type_Info_Pointer,
  190. Type_Info_Multi_Pointer,
  191. Type_Info_Procedure,
  192. Type_Info_Array,
  193. Type_Info_Enumerated_Array,
  194. Type_Info_Dynamic_Array,
  195. Type_Info_Slice,
  196. Type_Info_Parameters,
  197. Type_Info_Struct,
  198. Type_Info_Union,
  199. Type_Info_Enum,
  200. Type_Info_Map,
  201. Type_Info_Bit_Set,
  202. Type_Info_Simd_Vector,
  203. Type_Info_Relative_Pointer,
  204. Type_Info_Relative_Multi_Pointer,
  205. Type_Info_Matrix,
  206. Type_Info_Soa_Pointer,
  207. },
  208. }
  209. // NOTE(bill): This must match the compiler's
  210. Typeid_Kind :: enum u8 {
  211. Invalid,
  212. Integer,
  213. Rune,
  214. Float,
  215. Complex,
  216. Quaternion,
  217. String,
  218. Boolean,
  219. Any,
  220. Type_Id,
  221. Pointer,
  222. Multi_Pointer,
  223. Procedure,
  224. Array,
  225. Enumerated_Array,
  226. Dynamic_Array,
  227. Slice,
  228. Tuple,
  229. Struct,
  230. Union,
  231. Enum,
  232. Map,
  233. Bit_Set,
  234. Simd_Vector,
  235. Relative_Pointer,
  236. Relative_Multi_Pointer,
  237. Matrix,
  238. Soa_Pointer,
  239. }
  240. #assert(len(Typeid_Kind) < 32)
  241. // Typeid_Bit_Field :: bit_field #align(align_of(uintptr)) {
  242. // index: 8*size_of(uintptr) - 8,
  243. // kind: 5, // Typeid_Kind
  244. // named: 1,
  245. // special: 1, // signed, cstring, etc
  246. // reserved: 1,
  247. // }
  248. // #assert(size_of(Typeid_Bit_Field) == size_of(uintptr));
  249. // NOTE(bill): only the ones that are needed (not all types)
  250. // This will be set by the compiler
  251. type_table: []Type_Info
  252. args__: []cstring
  253. when ODIN_OS == .Windows {
  254. // NOTE(Jeroen): If we're a Windows DLL, fwdReason will be populated.
  255. // This tells a DLL if it's first loaded, about to be unloaded, or a thread is joining/exiting.
  256. DLL_Forward_Reason :: enum u32 {
  257. Process_Detach = 0, // About to unload DLL
  258. Process_Attach = 1, // Entry point
  259. Thread_Attach = 2,
  260. Thread_Detach = 3,
  261. }
  262. dll_forward_reason: DLL_Forward_Reason
  263. }
  264. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  265. Source_Code_Location :: struct {
  266. file_path: string,
  267. line, column: i32,
  268. procedure: string,
  269. }
  270. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !
  271. // Allocation Stuff
  272. Allocator_Mode :: enum byte {
  273. Alloc,
  274. Free,
  275. Free_All,
  276. Resize,
  277. Query_Features,
  278. Query_Info,
  279. Alloc_Non_Zeroed,
  280. Resize_Non_Zeroed,
  281. }
  282. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
  283. Allocator_Query_Info :: struct {
  284. pointer: rawptr,
  285. size: Maybe(int),
  286. alignment: Maybe(int),
  287. }
  288. Allocator_Error :: enum byte {
  289. None = 0,
  290. Out_Of_Memory = 1,
  291. Invalid_Pointer = 2,
  292. Invalid_Argument = 3,
  293. Mode_Not_Implemented = 4,
  294. }
  295. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  296. size, alignment: int,
  297. old_memory: rawptr, old_size: int,
  298. location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error)
  299. Allocator :: struct {
  300. procedure: Allocator_Proc,
  301. data: rawptr,
  302. }
  303. Byte :: 1
  304. Kilobyte :: 1024 * Byte
  305. Megabyte :: 1024 * Kilobyte
  306. Gigabyte :: 1024 * Megabyte
  307. Terabyte :: 1024 * Gigabyte
  308. Petabyte :: 1024 * Terabyte
  309. Exabyte :: 1024 * Petabyte
  310. // Logging stuff
  311. Logger_Level :: enum uint {
  312. Debug = 0,
  313. Info = 10,
  314. Warning = 20,
  315. Error = 30,
  316. Fatal = 40,
  317. }
  318. Logger_Option :: enum {
  319. Level,
  320. Date,
  321. Time,
  322. Short_File_Path,
  323. Long_File_Path,
  324. Line,
  325. Procedure,
  326. Terminal_Color,
  327. Thread_Id,
  328. }
  329. Logger_Options :: bit_set[Logger_Option]
  330. Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location)
  331. Logger :: struct {
  332. procedure: Logger_Proc,
  333. data: rawptr,
  334. lowest_level: Logger_Level,
  335. options: Logger_Options,
  336. }
  337. Context :: struct {
  338. allocator: Allocator,
  339. temp_allocator: Allocator,
  340. assertion_failure_proc: Assertion_Failure_Proc,
  341. logger: Logger,
  342. user_ptr: rawptr,
  343. user_index: int,
  344. // Internal use only
  345. _internal: rawptr,
  346. }
  347. Raw_String :: struct {
  348. data: [^]byte,
  349. len: int,
  350. }
  351. Raw_Slice :: struct {
  352. data: rawptr,
  353. len: int,
  354. }
  355. Raw_Dynamic_Array :: struct {
  356. data: rawptr,
  357. len: int,
  358. cap: int,
  359. allocator: Allocator,
  360. }
  361. // The raw, type-erased representation of a map.
  362. //
  363. // 32-bytes on 64-bit
  364. // 16-bytes on 32-bit
  365. Raw_Map :: struct {
  366. // A single allocation spanning all keys, values, and hashes.
  367. // {
  368. // k: Map_Cell(K) * (capacity / ks_per_cell)
  369. // v: Map_Cell(V) * (capacity / vs_per_cell)
  370. // h: Map_Cell(H) * (capacity / hs_per_cell)
  371. // }
  372. //
  373. // The data is allocated assuming 64-byte alignment, meaning the address is
  374. // always a multiple of 64. This means we have 6 bits of zeros in the pointer
  375. // to store the capacity. We can store a value as large as 2^6-1 or 63 in
  376. // there. This conveniently is the maximum log2 capacity we can have for a map
  377. // as Odin uses signed integers to represent capacity.
  378. //
  379. // Since the hashes are backed by Map_Hash, which is just a 64-bit unsigned
  380. // integer, the cell structure for hashes is unnecessary because 64/8 is 8 and
  381. // requires no padding, meaning it can be indexed as a regular array of
  382. // Map_Hash directly, though for consistency sake it's written as if it were
  383. // an array of Map_Cell(Map_Hash).
  384. data: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  385. len: uintptr, // 8-bytes on 64-bits, 4-bytes on 32-bits
  386. allocator: Allocator, // 16-bytes on 64-bits, 8-bytes on 32-bits
  387. }
  388. Raw_Any :: struct {
  389. data: rawptr,
  390. id: typeid,
  391. }
  392. Raw_Cstring :: struct {
  393. data: [^]byte,
  394. }
  395. Raw_Soa_Pointer :: struct {
  396. data: rawptr,
  397. index: int,
  398. }
  399. /*
  400. // Defined internally by the compiler
  401. Odin_OS_Type :: enum int {
  402. Unknown,
  403. Windows,
  404. Darwin,
  405. Linux,
  406. Essence,
  407. FreeBSD,
  408. OpenBSD,
  409. WASI,
  410. JS,
  411. Freestanding,
  412. }
  413. */
  414. Odin_OS_Type :: type_of(ODIN_OS)
  415. /*
  416. // Defined internally by the compiler
  417. Odin_Arch_Type :: enum int {
  418. Unknown,
  419. amd64,
  420. i386,
  421. arm32,
  422. arm64,
  423. wasm32,
  424. wasm64p32,
  425. }
  426. */
  427. Odin_Arch_Type :: type_of(ODIN_ARCH)
  428. /*
  429. // Defined internally by the compiler
  430. Odin_Build_Mode_Type :: enum int {
  431. Executable,
  432. Dynamic,
  433. Object,
  434. Assembly,
  435. LLVM_IR,
  436. }
  437. */
  438. Odin_Build_Mode_Type :: type_of(ODIN_BUILD_MODE)
  439. /*
  440. // Defined internally by the compiler
  441. Odin_Endian_Type :: enum int {
  442. Unknown,
  443. Little,
  444. Big,
  445. }
  446. */
  447. Odin_Endian_Type :: type_of(ODIN_ENDIAN)
  448. /*
  449. // Defined internally by the compiler
  450. Odin_Platform_Subtarget_Type :: enum int {
  451. Default,
  452. iOS,
  453. }
  454. */
  455. Odin_Platform_Subtarget_Type :: type_of(ODIN_PLATFORM_SUBTARGET)
  456. /*
  457. // Defined internally by the compiler
  458. Odin_Sanitizer_Flag :: enum u32 {
  459. Address = 0,
  460. Memory = 1,
  461. Thread = 2,
  462. }
  463. Odin_Sanitizer_Flags :: distinct bitset[Odin_Sanitizer_Flag; u32]
  464. ODIN_SANITIZER_FLAGS // is a constant
  465. */
  466. Odin_Sanitizer_Flags :: type_of(ODIN_SANITIZER_FLAGS)
  467. /////////////////////////////
  468. // Init Startup Procedures //
  469. /////////////////////////////
  470. // IMPORTANT NOTE(bill): Do not call this unless you want to explicitly set up the entry point and how it gets called
  471. // This is probably only useful for freestanding targets
  472. foreign {
  473. @(link_name="__$startup_runtime")
  474. _startup_runtime :: proc "odin" () ---
  475. @(link_name="__$cleanup_runtime")
  476. _cleanup_runtime :: proc "odin" () ---
  477. }
  478. _cleanup_runtime_contextless :: proc "contextless" () {
  479. context = default_context()
  480. _cleanup_runtime()
  481. }
  482. /////////////////////////////
  483. /////////////////////////////
  484. /////////////////////////////
  485. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  486. if info == nil {
  487. return nil
  488. }
  489. base := info
  490. loop: for {
  491. #partial switch i in base.variant {
  492. case Type_Info_Named: base = i.base
  493. case: break loop
  494. }
  495. }
  496. return base
  497. }
  498. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  499. if info == nil {
  500. return nil
  501. }
  502. base := info
  503. loop: for {
  504. #partial switch i in base.variant {
  505. case Type_Info_Named: base = i.base
  506. case Type_Info_Enum: base = i.base
  507. case: break loop
  508. }
  509. }
  510. return base
  511. }
  512. type_info_base_without_enum :: type_info_core
  513. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
  514. MASK :: 1<<(8*size_of(typeid) - 8) - 1
  515. data := transmute(uintptr)id
  516. n := int(data & MASK)
  517. if n < 0 || n >= len(type_table) {
  518. n = 0
  519. }
  520. return &type_table[n]
  521. }
  522. when !ODIN_NO_RTTI {
  523. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  524. ti := type_info_of(id)
  525. ti = type_info_base(ti)
  526. return ti.id
  527. }
  528. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  529. ti := type_info_core(type_info_of(id))
  530. return ti.id
  531. }
  532. typeid_base_without_enum :: typeid_core
  533. }
  534. debug_trap :: intrinsics.debug_trap
  535. trap :: intrinsics.trap
  536. read_cycle_counter :: intrinsics.read_cycle_counter
  537. default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location) {
  538. // Nothing
  539. }
  540. default_logger :: proc() -> Logger {
  541. return Logger{default_logger_proc, nil, Logger_Level.Debug, nil}
  542. }
  543. default_context :: proc "contextless" () -> Context {
  544. c: Context
  545. __init_context(&c)
  546. return c
  547. }
  548. @private
  549. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  550. if c == nil {
  551. return
  552. }
  553. c^ = other^
  554. __init_context(c)
  555. }
  556. @private
  557. __init_context :: proc "contextless" (c: ^Context) {
  558. if c == nil {
  559. return
  560. }
  561. // NOTE(bill): Do not initialize these procedures with a call as they are not defined with the "contextless" calling convention
  562. c.allocator.procedure = default_allocator_proc
  563. c.allocator.data = nil
  564. c.temp_allocator.procedure = default_temp_allocator_proc
  565. when !NO_DEFAULT_TEMP_ALLOCATOR {
  566. c.temp_allocator.data = &global_default_temp_allocator_data
  567. }
  568. when !ODIN_DISABLE_ASSERT {
  569. c.assertion_failure_proc = default_assertion_failure_proc
  570. }
  571. c.logger.procedure = default_logger_proc
  572. c.logger.data = nil
  573. }
  574. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
  575. when ODIN_OS == .Freestanding {
  576. // Do nothing
  577. } else {
  578. when !ODIN_DISABLE_ASSERT {
  579. print_caller_location(loc)
  580. print_string(" ")
  581. }
  582. print_string(prefix)
  583. if len(message) > 0 {
  584. print_string(": ")
  585. print_string(message)
  586. }
  587. print_byte('\n')
  588. }
  589. trap()
  590. }