core.odin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_Tuple
  77. results: ^Type_Info, // Type_Info_Tuple
  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_Tuple :: struct { // Only used for procedures parameters and results
  98. types: []^Type_Info,
  99. names: []string,
  100. }
  101. Type_Info_Struct :: struct {
  102. types: []^Type_Info,
  103. names: []string,
  104. offsets: []uintptr,
  105. usings: []bool,
  106. tags: []string,
  107. is_packed: bool,
  108. is_raw_union: bool,
  109. custom_align: bool,
  110. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  111. // These are only set iff this structure is an SOA structure
  112. soa_kind: Type_Info_Struct_Soa_Kind,
  113. soa_base_type: ^Type_Info,
  114. soa_len: int,
  115. }
  116. Type_Info_Union :: struct {
  117. variants: []^Type_Info,
  118. tag_offset: uintptr,
  119. tag_type: ^Type_Info,
  120. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  121. custom_align: bool,
  122. no_nil: bool,
  123. maybe: 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. generated_struct: ^Type_Info,
  135. key_equal: Equal_Proc,
  136. key_hasher: Hasher_Proc,
  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,
  151. base_integer: ^Type_Info,
  152. }
  153. Type_Info_Relative_Slice :: struct {
  154. slice: ^Type_Info,
  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_Flag :: enum u8 {
  166. Comparable = 0,
  167. Simple_Compare = 1,
  168. }
  169. Type_Info_Flags :: distinct bit_set[Type_Info_Flag; u32]
  170. Type_Info :: struct {
  171. size: int,
  172. align: int,
  173. flags: Type_Info_Flags,
  174. id: typeid,
  175. variant: union {
  176. Type_Info_Named,
  177. Type_Info_Integer,
  178. Type_Info_Rune,
  179. Type_Info_Float,
  180. Type_Info_Complex,
  181. Type_Info_Quaternion,
  182. Type_Info_String,
  183. Type_Info_Boolean,
  184. Type_Info_Any,
  185. Type_Info_Type_Id,
  186. Type_Info_Pointer,
  187. Type_Info_Multi_Pointer,
  188. Type_Info_Procedure,
  189. Type_Info_Array,
  190. Type_Info_Enumerated_Array,
  191. Type_Info_Dynamic_Array,
  192. Type_Info_Slice,
  193. Type_Info_Tuple,
  194. Type_Info_Struct,
  195. Type_Info_Union,
  196. Type_Info_Enum,
  197. Type_Info_Map,
  198. Type_Info_Bit_Set,
  199. Type_Info_Simd_Vector,
  200. Type_Info_Relative_Pointer,
  201. Type_Info_Relative_Slice,
  202. Type_Info_Matrix,
  203. },
  204. }
  205. // NOTE(bill): This must match the compiler's
  206. Typeid_Kind :: enum u8 {
  207. Invalid,
  208. Integer,
  209. Rune,
  210. Float,
  211. Complex,
  212. Quaternion,
  213. String,
  214. Boolean,
  215. Any,
  216. Type_Id,
  217. Pointer,
  218. Multi_Pointer,
  219. Procedure,
  220. Array,
  221. Enumerated_Array,
  222. Dynamic_Array,
  223. Slice,
  224. Tuple,
  225. Struct,
  226. Union,
  227. Enum,
  228. Map,
  229. Bit_Set,
  230. Simd_Vector,
  231. Relative_Pointer,
  232. Relative_Slice,
  233. Matrix,
  234. }
  235. #assert(len(Typeid_Kind) < 32)
  236. // Typeid_Bit_Field :: bit_field #align align_of(uintptr) {
  237. // index: 8*size_of(uintptr) - 8,
  238. // kind: 5, // Typeid_Kind
  239. // named: 1,
  240. // special: 1, // signed, cstring, etc
  241. // reserved: 1,
  242. // }
  243. // #assert(size_of(Typeid_Bit_Field) == size_of(uintptr));
  244. // NOTE(bill): only the ones that are needed (not all types)
  245. // This will be set by the compiler
  246. type_table: []Type_Info
  247. args__: []cstring
  248. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  249. Source_Code_Location :: struct {
  250. file_path: string,
  251. line, column: i32,
  252. procedure: string,
  253. }
  254. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !
  255. // Allocation Stuff
  256. Allocator_Mode :: enum byte {
  257. Alloc,
  258. Free,
  259. Free_All,
  260. Resize,
  261. Query_Features,
  262. Query_Info,
  263. }
  264. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
  265. Allocator_Query_Info :: struct {
  266. pointer: rawptr,
  267. size: Maybe(int),
  268. alignment: Maybe(int),
  269. }
  270. Allocator_Error :: enum byte {
  271. None = 0,
  272. Out_Of_Memory = 1,
  273. Invalid_Pointer = 2,
  274. Invalid_Argument = 3,
  275. Mode_Not_Implemented = 4,
  276. }
  277. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  278. size, alignment: int,
  279. old_memory: rawptr, old_size: int,
  280. location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error)
  281. Allocator :: struct {
  282. procedure: Allocator_Proc,
  283. data: rawptr,
  284. }
  285. // Logging stuff
  286. Logger_Level :: enum uint {
  287. Debug = 0,
  288. Info = 10,
  289. Warning = 20,
  290. Error = 30,
  291. Fatal = 40,
  292. }
  293. Logger_Option :: enum {
  294. Level,
  295. Date,
  296. Time,
  297. Short_File_Path,
  298. Long_File_Path,
  299. Line,
  300. Procedure,
  301. Terminal_Color,
  302. Thread_Id,
  303. }
  304. Logger_Options :: bit_set[Logger_Option]
  305. Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location)
  306. Logger :: struct {
  307. procedure: Logger_Proc,
  308. data: rawptr,
  309. lowest_level: Logger_Level,
  310. options: Logger_Options,
  311. }
  312. Context :: struct {
  313. allocator: Allocator,
  314. temp_allocator: Allocator,
  315. assertion_failure_proc: Assertion_Failure_Proc,
  316. logger: Logger,
  317. user_ptr: rawptr,
  318. user_index: int,
  319. // Internal use only
  320. _internal: rawptr,
  321. }
  322. Raw_String :: struct {
  323. data: [^]byte,
  324. len: int,
  325. }
  326. Raw_Slice :: struct {
  327. data: rawptr,
  328. len: int,
  329. }
  330. Raw_Dynamic_Array :: struct {
  331. data: rawptr,
  332. len: int,
  333. cap: int,
  334. allocator: Allocator,
  335. }
  336. Raw_Map :: struct {
  337. hashes: []int,
  338. entries: Raw_Dynamic_Array,
  339. }
  340. Raw_Any :: struct {
  341. data: rawptr,
  342. id: typeid,
  343. }
  344. Raw_Cstring :: struct {
  345. data: [^]byte,
  346. }
  347. /*
  348. // Defined internally by the compiler
  349. Odin_OS_Type :: enum int {
  350. Unknown,
  351. Windows,
  352. Darwin,
  353. Linux,
  354. Essence,
  355. FreeBSD,
  356. WASI,
  357. JS,
  358. Freestanding,
  359. }
  360. */
  361. Odin_OS_Type :: type_of(ODIN_OS)
  362. /*
  363. // Defined internally by the compiler
  364. Odin_Arch_Type :: enum int {
  365. Unknown,
  366. amd64,
  367. i386,
  368. arm64,
  369. wasm32,
  370. wasm64,
  371. }
  372. */
  373. Odin_Arch_Type :: type_of(ODIN_ARCH)
  374. /*
  375. // Defined internally by the compiler
  376. Odin_Build_Mode_Type :: enum int {
  377. Executable,
  378. Dynamic,
  379. Object,
  380. Assembly,
  381. LLVM_IR,
  382. }
  383. */
  384. Odin_Build_Mode_Type :: type_of(ODIN_BUILD_MODE)
  385. /*
  386. // Defined internally by the compiler
  387. Odin_Endian_Type :: enum int {
  388. Unknown,
  389. Little,
  390. Big,
  391. }
  392. */
  393. Odin_Endian_Type :: type_of(ODIN_ENDIAN)
  394. /////////////////////////////
  395. // Init Startup Procedures //
  396. /////////////////////////////
  397. // IMPORTANT NOTE(bill): Do not call this unless you want to explicitly set up the entry point and how it gets called
  398. // This is probably only useful for freestanding targets
  399. foreign {
  400. @(link_name="__$startup_runtime")
  401. _startup_runtime :: proc "odin" () ---
  402. }
  403. @(link_name="__$cleanup_runtime")
  404. _cleanup_runtime :: proc() {
  405. default_temp_allocator_destroy(&global_default_temp_allocator_data)
  406. }
  407. _cleanup_runtime_contextless :: proc "contextless" () {
  408. context = default_context()
  409. _cleanup_runtime()
  410. }
  411. /////////////////////////////
  412. /////////////////////////////
  413. /////////////////////////////
  414. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  415. if info == nil {
  416. return nil
  417. }
  418. base := info
  419. loop: for {
  420. #partial switch i in base.variant {
  421. case Type_Info_Named: base = i.base
  422. case: break loop
  423. }
  424. }
  425. return base
  426. }
  427. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  428. if info == nil {
  429. return nil
  430. }
  431. base := info
  432. loop: for {
  433. #partial switch i in base.variant {
  434. case Type_Info_Named: base = i.base
  435. case Type_Info_Enum: base = i.base
  436. case: break loop
  437. }
  438. }
  439. return base
  440. }
  441. type_info_base_without_enum :: type_info_core
  442. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
  443. MASK :: 1<<(8*size_of(typeid) - 8) - 1
  444. data := transmute(uintptr)id
  445. n := int(data & MASK)
  446. if n < 0 || n >= len(type_table) {
  447. n = 0
  448. }
  449. return &type_table[n]
  450. }
  451. when !ODIN_DISALLOW_RTTI {
  452. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  453. ti := type_info_of(id)
  454. ti = type_info_base(ti)
  455. return ti.id
  456. }
  457. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  458. ti := type_info_core(type_info_of(id))
  459. return ti.id
  460. }
  461. typeid_base_without_enum :: typeid_core
  462. }
  463. debug_trap :: intrinsics.debug_trap
  464. trap :: intrinsics.trap
  465. read_cycle_counter :: intrinsics.read_cycle_counter
  466. default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location) {
  467. // Nothing
  468. }
  469. default_logger :: proc() -> Logger {
  470. return Logger{default_logger_proc, nil, Logger_Level.Debug, nil}
  471. }
  472. default_context :: proc "contextless" () -> Context {
  473. c: Context
  474. __init_context(&c)
  475. return c
  476. }
  477. @private
  478. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  479. if c == nil {
  480. return
  481. }
  482. c^ = other^
  483. __init_context(c)
  484. }
  485. @private
  486. __init_context :: proc "contextless" (c: ^Context) {
  487. if c == nil {
  488. return
  489. }
  490. // NOTE(bill): Do not initialize these procedures with a call as they are not defined with the "contexless" calling convention
  491. c.allocator.procedure = default_allocator_proc
  492. c.allocator.data = nil
  493. c.temp_allocator.procedure = default_temp_allocator_proc
  494. c.temp_allocator.data = &global_default_temp_allocator_data
  495. when !ODIN_DISABLE_ASSERT {
  496. c.assertion_failure_proc = default_assertion_failure_proc
  497. }
  498. c.logger.procedure = default_logger_proc
  499. c.logger.data = nil
  500. }
  501. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
  502. when ODIN_OS == .Freestanding {
  503. // Do nothing
  504. } else {
  505. print_caller_location(loc)
  506. print_string(" ")
  507. print_string(prefix)
  508. if len(message) > 0 {
  509. print_string(": ")
  510. print_string(message)
  511. }
  512. print_byte('\n')
  513. }
  514. trap()
  515. }