core.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. }
  34. Type_Info_Enum_Value :: distinct i64
  35. Platform_Endianness :: enum u8 {
  36. Platform = 0,
  37. Little = 1,
  38. Big = 2,
  39. }
  40. // Procedure type to test whether two values of the same type are equal
  41. Equal_Proc :: distinct proc "contextless" (rawptr, rawptr) -> bool
  42. // Procedure type to hash a value, default seed value is 0
  43. Hasher_Proc :: distinct proc "contextless" (data: rawptr, seed: uintptr = 0) -> uintptr
  44. Type_Info_Struct_Soa_Kind :: enum u8 {
  45. None = 0,
  46. Fixed = 1,
  47. Slice = 2,
  48. Dynamic = 3,
  49. }
  50. // Variant Types
  51. Type_Info_Named :: struct {
  52. name: string,
  53. base: ^Type_Info,
  54. pkg: string,
  55. loc: Source_Code_Location,
  56. }
  57. Type_Info_Integer :: struct {signed: bool, endianness: Platform_Endianness}
  58. Type_Info_Rune :: struct {}
  59. Type_Info_Float :: struct {endianness: Platform_Endianness}
  60. Type_Info_Complex :: struct {}
  61. Type_Info_Quaternion :: struct {}
  62. Type_Info_String :: struct {is_cstring: bool}
  63. Type_Info_Boolean :: struct {}
  64. Type_Info_Any :: struct {}
  65. Type_Info_Type_Id :: struct {}
  66. Type_Info_Pointer :: struct {
  67. elem: ^Type_Info, // nil -> rawptr
  68. }
  69. Type_Info_Multi_Pointer :: struct {
  70. elem: ^Type_Info,
  71. }
  72. Type_Info_Procedure :: struct {
  73. params: ^Type_Info, // Type_Info_Tuple
  74. results: ^Type_Info, // Type_Info_Tuple
  75. variadic: bool,
  76. convention: Calling_Convention,
  77. }
  78. Type_Info_Array :: struct {
  79. elem: ^Type_Info,
  80. elem_size: int,
  81. count: int,
  82. }
  83. Type_Info_Enumerated_Array :: struct {
  84. elem: ^Type_Info,
  85. index: ^Type_Info,
  86. elem_size: int,
  87. count: int,
  88. min_value: Type_Info_Enum_Value,
  89. max_value: Type_Info_Enum_Value,
  90. }
  91. Type_Info_Dynamic_Array :: struct {elem: ^Type_Info, elem_size: int}
  92. Type_Info_Slice :: struct {elem: ^Type_Info, elem_size: int}
  93. Type_Info_Tuple :: struct { // Only used for procedures parameters and results
  94. types: []^Type_Info,
  95. names: []string,
  96. }
  97. Type_Info_Struct :: struct {
  98. types: []^Type_Info,
  99. names: []string,
  100. offsets: []uintptr,
  101. usings: []bool,
  102. tags: []string,
  103. is_packed: bool,
  104. is_raw_union: bool,
  105. custom_align: bool,
  106. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  107. // These are only set iff this structure is an SOA structure
  108. soa_kind: Type_Info_Struct_Soa_Kind,
  109. soa_base_type: ^Type_Info,
  110. soa_len: int,
  111. }
  112. Type_Info_Union :: struct {
  113. variants: []^Type_Info,
  114. tag_offset: uintptr,
  115. tag_type: ^Type_Info,
  116. equal: Equal_Proc, // set only when the struct has .Comparable set but does not have .Simple_Compare set
  117. custom_align: bool,
  118. no_nil: bool,
  119. maybe: bool,
  120. }
  121. Type_Info_Enum :: struct {
  122. base: ^Type_Info,
  123. names: []string,
  124. values: []Type_Info_Enum_Value,
  125. }
  126. Type_Info_Map :: struct {
  127. key: ^Type_Info,
  128. value: ^Type_Info,
  129. generated_struct: ^Type_Info,
  130. key_equal: Equal_Proc,
  131. key_hasher: Hasher_Proc,
  132. }
  133. Type_Info_Bit_Set :: struct {
  134. elem: ^Type_Info,
  135. underlying: ^Type_Info, // Possibly nil
  136. lower: i64,
  137. upper: i64,
  138. }
  139. Type_Info_Simd_Vector :: struct {
  140. elem: ^Type_Info,
  141. elem_size: int,
  142. count: int,
  143. }
  144. Type_Info_Relative_Pointer :: struct {
  145. pointer: ^Type_Info,
  146. base_integer: ^Type_Info,
  147. }
  148. Type_Info_Relative_Slice :: struct {
  149. slice: ^Type_Info,
  150. base_integer: ^Type_Info,
  151. }
  152. Type_Info_Matrix :: struct {
  153. elem: ^Type_Info,
  154. elem_size: int,
  155. elem_stride: int, // elem_stride >= row_count
  156. row_count: int,
  157. column_count: int,
  158. // Total element count = column_count * elem_stride
  159. }
  160. Type_Info_Flag :: enum u8 {
  161. Comparable = 0,
  162. Simple_Compare = 1,
  163. }
  164. Type_Info_Flags :: distinct bit_set[Type_Info_Flag; u32]
  165. Type_Info :: struct {
  166. size: int,
  167. align: int,
  168. flags: Type_Info_Flags,
  169. id: typeid,
  170. variant: union {
  171. Type_Info_Named,
  172. Type_Info_Integer,
  173. Type_Info_Rune,
  174. Type_Info_Float,
  175. Type_Info_Complex,
  176. Type_Info_Quaternion,
  177. Type_Info_String,
  178. Type_Info_Boolean,
  179. Type_Info_Any,
  180. Type_Info_Type_Id,
  181. Type_Info_Pointer,
  182. Type_Info_Multi_Pointer,
  183. Type_Info_Procedure,
  184. Type_Info_Array,
  185. Type_Info_Enumerated_Array,
  186. Type_Info_Dynamic_Array,
  187. Type_Info_Slice,
  188. Type_Info_Tuple,
  189. Type_Info_Struct,
  190. Type_Info_Union,
  191. Type_Info_Enum,
  192. Type_Info_Map,
  193. Type_Info_Bit_Set,
  194. Type_Info_Simd_Vector,
  195. Type_Info_Relative_Pointer,
  196. Type_Info_Relative_Slice,
  197. Type_Info_Matrix,
  198. },
  199. }
  200. // NOTE(bill): This must match the compiler's
  201. Typeid_Kind :: enum u8 {
  202. Invalid,
  203. Integer,
  204. Rune,
  205. Float,
  206. Complex,
  207. Quaternion,
  208. String,
  209. Boolean,
  210. Any,
  211. Type_Id,
  212. Pointer,
  213. Multi_Pointer,
  214. Procedure,
  215. Array,
  216. Enumerated_Array,
  217. Dynamic_Array,
  218. Slice,
  219. Tuple,
  220. Struct,
  221. Union,
  222. Enum,
  223. Map,
  224. Bit_Set,
  225. Simd_Vector,
  226. Relative_Pointer,
  227. Relative_Slice,
  228. Matrix,
  229. }
  230. #assert(len(Typeid_Kind) < 32)
  231. // Typeid_Bit_Field :: bit_field #align align_of(uintptr) {
  232. // index: 8*size_of(uintptr) - 8,
  233. // kind: 5, // Typeid_Kind
  234. // named: 1,
  235. // special: 1, // signed, cstring, etc
  236. // reserved: 1,
  237. // }
  238. // #assert(size_of(Typeid_Bit_Field) == size_of(uintptr));
  239. // NOTE(bill): only the ones that are needed (not all types)
  240. // This will be set by the compiler
  241. type_table: []Type_Info
  242. args__: []cstring
  243. // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
  244. Source_Code_Location :: struct {
  245. file_path: string,
  246. line, column: i32,
  247. procedure: string,
  248. }
  249. Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location) -> !
  250. // Allocation Stuff
  251. Allocator_Mode :: enum byte {
  252. Alloc,
  253. Free,
  254. Free_All,
  255. Resize,
  256. Query_Features,
  257. Query_Info,
  258. }
  259. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode]
  260. Allocator_Query_Info :: struct {
  261. pointer: rawptr,
  262. size: Maybe(int),
  263. alignment: Maybe(int),
  264. }
  265. Allocator_Error :: enum byte {
  266. None = 0,
  267. Out_Of_Memory = 1,
  268. Invalid_Pointer = 2,
  269. Invalid_Argument = 3,
  270. Mode_Not_Implemented = 4,
  271. }
  272. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  273. size, alignment: int,
  274. old_memory: rawptr, old_size: int,
  275. location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error)
  276. Allocator :: struct {
  277. procedure: Allocator_Proc,
  278. data: rawptr,
  279. }
  280. // Logging stuff
  281. Logger_Level :: enum uint {
  282. Debug = 0,
  283. Info = 10,
  284. Warning = 20,
  285. Error = 30,
  286. Fatal = 40,
  287. }
  288. Logger_Option :: enum {
  289. Level,
  290. Date,
  291. Time,
  292. Short_File_Path,
  293. Long_File_Path,
  294. Line,
  295. Procedure,
  296. Terminal_Color,
  297. Thread_Id,
  298. }
  299. Logger_Options :: bit_set[Logger_Option]
  300. Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location)
  301. Logger :: struct {
  302. procedure: Logger_Proc,
  303. data: rawptr,
  304. lowest_level: Logger_Level,
  305. options: Logger_Options,
  306. }
  307. Context :: struct {
  308. allocator: Allocator,
  309. temp_allocator: Allocator,
  310. assertion_failure_proc: Assertion_Failure_Proc,
  311. logger: Logger,
  312. user_data: any,
  313. user_ptr: rawptr,
  314. user_index: int,
  315. // Internal use only
  316. _internal: rawptr,
  317. }
  318. Raw_String :: struct {
  319. data: [^]byte,
  320. len: int,
  321. }
  322. Raw_Slice :: struct {
  323. data: rawptr,
  324. len: int,
  325. }
  326. Raw_Dynamic_Array :: struct {
  327. data: rawptr,
  328. len: int,
  329. cap: int,
  330. allocator: Allocator,
  331. }
  332. Raw_Map :: struct {
  333. hashes: []int,
  334. entries: Raw_Dynamic_Array,
  335. }
  336. Raw_Any :: struct {
  337. data: rawptr,
  338. id: typeid,
  339. }
  340. Raw_Cstring :: struct {
  341. data: [^]byte,
  342. }
  343. /////////////////////////////
  344. // Init Startup Procedures //
  345. /////////////////////////////
  346. // IMPORTANT NOTE(bill): Do not call this unless you want to explicitly set up the entry point and how it gets called
  347. // This is probably only useful for freestanding targets
  348. foreign {
  349. @(link_name="__$startup_runtime")
  350. _startup_runtime :: proc() ---
  351. }
  352. @(link_name="__$cleanup_runtime")
  353. _cleanup_runtime :: proc() {
  354. default_temp_allocator_destroy(&global_default_temp_allocator_data)
  355. }
  356. /////////////////////////////
  357. /////////////////////////////
  358. /////////////////////////////
  359. type_info_base :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  360. if info == nil {
  361. return nil
  362. }
  363. base := info
  364. loop: for {
  365. #partial switch i in base.variant {
  366. case Type_Info_Named: base = i.base
  367. case: break loop
  368. }
  369. }
  370. return base
  371. }
  372. type_info_core :: proc "contextless" (info: ^Type_Info) -> ^Type_Info {
  373. if info == nil {
  374. return nil
  375. }
  376. base := info
  377. loop: for {
  378. #partial switch i in base.variant {
  379. case Type_Info_Named: base = i.base
  380. case Type_Info_Enum: base = i.base
  381. case: break loop
  382. }
  383. }
  384. return base
  385. }
  386. type_info_base_without_enum :: type_info_core
  387. __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check {
  388. MASK :: 1<<(8*size_of(typeid) - 8) - 1
  389. data := transmute(uintptr)id
  390. n := int(data & MASK)
  391. if n < 0 || n >= len(type_table) {
  392. n = 0
  393. }
  394. return &type_table[n]
  395. }
  396. typeid_base :: proc "contextless" (id: typeid) -> typeid {
  397. ti := type_info_of(id)
  398. ti = type_info_base(ti)
  399. return ti.id
  400. }
  401. typeid_core :: proc "contextless" (id: typeid) -> typeid {
  402. ti := type_info_core(type_info_of(id))
  403. return ti.id
  404. }
  405. typeid_base_without_enum :: typeid_core
  406. debug_trap :: intrinsics.debug_trap
  407. trap :: intrinsics.trap
  408. read_cycle_counter :: intrinsics.read_cycle_counter
  409. default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location) {
  410. // Nothing
  411. }
  412. default_logger :: proc() -> Logger {
  413. return Logger{default_logger_proc, nil, Logger_Level.Debug, nil}
  414. }
  415. default_context :: proc "contextless" () -> Context {
  416. c: Context
  417. __init_context(&c)
  418. return c
  419. }
  420. @private
  421. __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
  422. if c == nil {
  423. return
  424. }
  425. c^ = other^
  426. __init_context(c)
  427. }
  428. @private
  429. __init_context :: proc "contextless" (c: ^Context) {
  430. if c == nil {
  431. return
  432. }
  433. // NOTE(bill): Do not initialize these procedures with a call as they are not defined with the "contexless" calling convention
  434. c.allocator.procedure = default_allocator_proc
  435. c.allocator.data = nil
  436. c.temp_allocator.procedure = default_temp_allocator_proc
  437. c.temp_allocator.data = &global_default_temp_allocator_data
  438. when !ODIN_DISABLE_ASSERT {
  439. c.assertion_failure_proc = default_assertion_failure_proc
  440. }
  441. c.logger.procedure = default_logger_proc
  442. c.logger.data = nil
  443. }
  444. default_assertion_failure_proc :: proc(prefix, message: string, loc: Source_Code_Location) -> ! {
  445. when ODIN_OS == "freestanding" {
  446. // Do nothing
  447. } else {
  448. print_caller_location(loc)
  449. print_string(" ")
  450. print_string(prefix)
  451. if len(message) > 0 {
  452. print_string(": ")
  453. print_string(message)
  454. }
  455. print_byte('\n')
  456. }
  457. trap()
  458. }