core.odin 14 KB

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