internal.odin 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. package runtime
  2. import "core:os"
  3. bswap_16 :: proc "none" (x: u16) -> u16 {
  4. return x>>8 | x<<8;
  5. }
  6. bswap_32 :: proc "none" (x: u32) -> u32 {
  7. return x>>24 | (x>>8)&0xff00 | (x<<8)&0xff0000 | x<<24;
  8. }
  9. bswap_64 :: proc "none" (x: u64) -> u64 {
  10. return u64(bswap_32(u32(x))) | u64(bswap_32(u32(x>>32)));
  11. }
  12. bswap_128 :: proc "none" (x: u128) -> u128 {
  13. return u128(bswap_64(u64(x))) | u128(bswap_64(u64(x>>64)));
  14. }
  15. bswap_f32 :: proc "none" (f: f32) -> f32 {
  16. x := transmute(u32)f;
  17. z := x>>24 | (x>>8)&0xff00 | (x<<8)&0xff0000 | x<<24;
  18. return transmute(f32)z;
  19. }
  20. bswap_f64 :: proc "none" (f: f64) -> f64 {
  21. x := transmute(u64)f;
  22. z := u64(bswap_32(u32(x))) | u64(bswap_32(u32(x>>32)));
  23. return transmute(f64)z;
  24. }
  25. ptr_offset :: inline proc "contextless" (ptr: $P/^$T, n: int) -> P {
  26. new := int(uintptr(ptr)) + size_of(T)*n;
  27. return P(uintptr(new));
  28. }
  29. is_power_of_two_int :: inline proc(x: int) -> bool {
  30. if x <= 0 do return false;
  31. return (x & (x-1)) == 0;
  32. }
  33. align_forward_int :: inline proc(ptr, align: int) -> int {
  34. assert(is_power_of_two_int(align));
  35. p := ptr;
  36. modulo := p & (align-1);
  37. if modulo != 0 do p += align - modulo;
  38. return p;
  39. }
  40. is_power_of_two_uintptr :: inline proc(x: uintptr) -> bool {
  41. if x <= 0 do return false;
  42. return (x & (x-1)) == 0;
  43. }
  44. align_forward_uintptr :: inline proc(ptr, align: uintptr) -> uintptr {
  45. assert(is_power_of_two_uintptr(align));
  46. p := ptr;
  47. modulo := p & (align-1);
  48. if modulo != 0 do p += align - modulo;
  49. return p;
  50. }
  51. mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
  52. if data == nil do return nil;
  53. if len < 0 do return data;
  54. memset(data, 0, len);
  55. return data;
  56. }
  57. mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  58. if src == nil do return dst;
  59. // NOTE(bill): This _must_ be implemented like C's memmove
  60. foreign _ {
  61. when ODIN_USE_LLVM_API {
  62. when size_of(rawptr) == 8 {
  63. @(link_name="llvm.memmove.p0i8.p0i8.i64")
  64. llvm_memmove :: proc(dst, src: rawptr, len: int, is_volatile: bool = false) ---;
  65. } else {
  66. @(link_name="llvm.memmove.p0i8.p0i8.i32")
  67. llvm_memmove :: proc(dst, src: rawptr, len: int, is_volatile: bool = false) ---;
  68. }
  69. } else {
  70. when size_of(rawptr) == 8 {
  71. @(link_name="llvm.memmove.p0i8.p0i8.i64")
  72. llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32 = 1, is_volatile: bool = false) ---;
  73. } else {
  74. @(link_name="llvm.memmove.p0i8.p0i8.i32")
  75. llvm_memmove :: proc(dst, src: rawptr, len: int, align: i32 = 1, is_volatile: bool = false) ---;
  76. }
  77. }
  78. }
  79. llvm_memmove(dst, src, len);
  80. return dst;
  81. }
  82. mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  83. if src == nil do return dst;
  84. // NOTE(bill): This _must_ be implemented like C's memcpy
  85. foreign _ {
  86. when ODIN_USE_LLVM_API {
  87. when size_of(rawptr) == 8 {
  88. @(link_name="llvm.memcpy.p0i8.p0i8.i64")
  89. llvm_memcpy :: proc(dst, src: rawptr, len: int, is_volatile: bool = false) ---;
  90. } else {
  91. @(link_name="llvm.memcpy.p0i8.p0i8.i32")
  92. llvm_memcpy :: proc(dst, src: rawptr, len: int, is_volatile: bool = false) ---;
  93. }
  94. } else {
  95. when size_of(rawptr) == 8 {
  96. @(link_name="llvm.memcpy.p0i8.p0i8.i64")
  97. llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32 = 1, is_volatile: bool = false) ---;
  98. } else {
  99. @(link_name="llvm.memcpy.p0i8.p0i8.i32")
  100. llvm_memcpy :: proc(dst, src: rawptr, len: int, align: i32 = 1, is_volatile: bool = false) ---;
  101. }
  102. }
  103. }
  104. llvm_memcpy(dst, src, len);
  105. return dst;
  106. }
  107. DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
  108. mem_alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  109. if size == 0 do return nil;
  110. if allocator.procedure == nil do return nil;
  111. return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, 0, loc);
  112. }
  113. mem_free :: inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) {
  114. if ptr == nil do return;
  115. if allocator.procedure == nil do return;
  116. allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, 0, loc);
  117. }
  118. mem_free_all :: inline proc(allocator := context.allocator, loc := #caller_location) {
  119. if allocator.procedure != nil {
  120. allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, 0, loc);
  121. }
  122. }
  123. mem_resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  124. switch {
  125. case allocator.procedure == nil:
  126. return nil;
  127. case new_size == 0:
  128. allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, 0, loc);
  129. return nil;
  130. case ptr == nil:
  131. return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, 0, loc);
  132. }
  133. return allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, 0, loc);
  134. }
  135. print_u64 :: proc(fd: os.Handle, x: u64) {
  136. digits := "0123456789";
  137. a: [129]byte;
  138. i := len(a);
  139. b := u64(10);
  140. u := x;
  141. for u >= b {
  142. i -= 1; a[i] = digits[u % b];
  143. u /= b;
  144. }
  145. i -= 1; a[i] = digits[u % b];
  146. os.write(fd, a[i:]);
  147. }
  148. print_i64 :: proc(fd: os.Handle, x: i64) {
  149. digits := "0123456789";
  150. b :: i64(10);
  151. u := x;
  152. neg := u < 0;
  153. u = abs(u);
  154. a: [129]byte;
  155. i := len(a);
  156. for u >= b {
  157. i -= 1; a[i] = digits[u % b];
  158. u /= b;
  159. }
  160. i -= 1; a[i] = digits[u % b];
  161. if neg {
  162. i -= 1; a[i] = '-';
  163. }
  164. os.write(fd, a[i:]);
  165. }
  166. print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) {
  167. os.write_string(fd, file_path);
  168. os.write_byte(fd, '(');
  169. print_u64(fd, u64(line));
  170. os.write_byte(fd, ':');
  171. print_u64(fd, u64(column));
  172. os.write_byte(fd, ')');
  173. }
  174. print_typeid :: proc(fd: os.Handle, id: typeid) {
  175. if id == nil {
  176. os.write_string(fd, "nil");
  177. } else {
  178. ti := type_info_of(id);
  179. print_type(fd, ti);
  180. }
  181. }
  182. print_type :: proc(fd: os.Handle, ti: ^Type_Info) {
  183. if ti == nil {
  184. os.write_string(fd, "nil");
  185. return;
  186. }
  187. switch info in ti.variant {
  188. case Type_Info_Named:
  189. os.write_string(fd, info.name);
  190. case Type_Info_Integer:
  191. switch ti.id {
  192. case int: os.write_string(fd, "int");
  193. case uint: os.write_string(fd, "uint");
  194. case uintptr: os.write_string(fd, "uintptr");
  195. case:
  196. os.write_byte(fd, 'i' if info.signed else 'u');
  197. print_u64(fd, u64(8*ti.size));
  198. }
  199. case Type_Info_Rune:
  200. os.write_string(fd, "rune");
  201. case Type_Info_Float:
  202. os.write_byte(fd, 'f');
  203. print_u64(fd, u64(8*ti.size));
  204. case Type_Info_Complex:
  205. os.write_string(fd, "complex");
  206. print_u64(fd, u64(8*ti.size));
  207. case Type_Info_Quaternion:
  208. os.write_string(fd, "quaternion");
  209. print_u64(fd, u64(8*ti.size));
  210. case Type_Info_String:
  211. os.write_string(fd, "string");
  212. case Type_Info_Boolean:
  213. switch ti.id {
  214. case bool: os.write_string(fd, "bool");
  215. case:
  216. os.write_byte(fd, 'b');
  217. print_u64(fd, u64(8*ti.size));
  218. }
  219. case Type_Info_Any:
  220. os.write_string(fd, "any");
  221. case Type_Info_Type_Id:
  222. os.write_string(fd, "typeid");
  223. case Type_Info_Pointer:
  224. if info.elem == nil {
  225. os.write_string(fd, "rawptr");
  226. } else {
  227. os.write_string(fd, "^");
  228. print_type(fd, info.elem);
  229. }
  230. case Type_Info_Procedure:
  231. os.write_string(fd, "proc");
  232. if info.params == nil {
  233. os.write_string(fd, "()");
  234. } else {
  235. t := info.params.variant.(Type_Info_Tuple);
  236. os.write_byte(fd, '(');
  237. for t, i in t.types {
  238. if i > 0 do os.write_string(fd, ", ");
  239. print_type(fd, t);
  240. }
  241. os.write_string(fd, ")");
  242. }
  243. if info.results != nil {
  244. os.write_string(fd, " -> ");
  245. print_type(fd, info.results);
  246. }
  247. case Type_Info_Tuple:
  248. count := len(info.names);
  249. if count != 1 do os.write_byte(fd, '(');
  250. for name, i in info.names {
  251. if i > 0 do os.write_string(fd, ", ");
  252. t := info.types[i];
  253. if len(name) > 0 {
  254. os.write_string(fd, name);
  255. os.write_string(fd, ": ");
  256. }
  257. print_type(fd, t);
  258. }
  259. if count != 1 do os.write_string(fd, ")");
  260. case Type_Info_Array:
  261. os.write_byte(fd, '[');
  262. print_u64(fd, u64(info.count));
  263. os.write_byte(fd, ']');
  264. print_type(fd, info.elem);
  265. case Type_Info_Enumerated_Array:
  266. os.write_byte(fd, '[');
  267. print_type(fd, info.index);
  268. os.write_byte(fd, ']');
  269. print_type(fd, info.elem);
  270. case Type_Info_Dynamic_Array:
  271. os.write_string(fd, "[dynamic]");
  272. print_type(fd, info.elem);
  273. case Type_Info_Slice:
  274. os.write_string(fd, "[]");
  275. print_type(fd, info.elem);
  276. case Type_Info_Map:
  277. os.write_string(fd, "map[");
  278. print_type(fd, info.key);
  279. os.write_byte(fd, ']');
  280. print_type(fd, info.value);
  281. case Type_Info_Struct:
  282. switch info.soa_kind {
  283. case .None: // Ignore
  284. case .Fixed:
  285. os.write_string(fd, "#soa[");
  286. print_u64(fd, u64(info.soa_len));
  287. os.write_byte(fd, ']');
  288. print_type(fd, info.soa_base_type);
  289. return;
  290. case .Slice:
  291. os.write_string(fd, "#soa[]");
  292. print_type(fd, info.soa_base_type);
  293. return;
  294. case .Dynamic:
  295. os.write_string(fd, "#soa[dynamic]");
  296. print_type(fd, info.soa_base_type);
  297. return;
  298. }
  299. os.write_string(fd, "struct ");
  300. if info.is_packed do os.write_string(fd, "#packed ");
  301. if info.is_raw_union do os.write_string(fd, "#raw_union ");
  302. if info.custom_align {
  303. os.write_string(fd, "#align ");
  304. print_u64(fd, u64(ti.align));
  305. os.write_byte(fd, ' ');
  306. }
  307. os.write_byte(fd, '{');
  308. for name, i in info.names {
  309. if i > 0 do os.write_string(fd, ", ");
  310. os.write_string(fd, name);
  311. os.write_string(fd, ": ");
  312. print_type(fd, info.types[i]);
  313. }
  314. os.write_byte(fd, '}');
  315. case Type_Info_Union:
  316. os.write_string(fd, "union ");
  317. if info.custom_align {
  318. os.write_string(fd, "#align ");
  319. print_u64(fd, u64(ti.align));
  320. }
  321. if info.no_nil {
  322. os.write_string(fd, "#no_nil ");
  323. }
  324. os.write_byte(fd, '{');
  325. for variant, i in info.variants {
  326. if i > 0 do os.write_string(fd, ", ");
  327. print_type(fd, variant);
  328. }
  329. os.write_string(fd, "}");
  330. case Type_Info_Enum:
  331. os.write_string(fd, "enum ");
  332. print_type(fd, info.base);
  333. os.write_string(fd, " {");
  334. for name, i in info.names {
  335. if i > 0 do os.write_string(fd, ", ");
  336. os.write_string(fd, name);
  337. }
  338. os.write_string(fd, "}");
  339. case Type_Info_Bit_Field:
  340. os.write_string(fd, "bit_field ");
  341. if ti.align != 1 {
  342. os.write_string(fd, "#align ");
  343. print_u64(fd, u64(ti.align));
  344. os.write_byte(fd, ' ');
  345. }
  346. os.write_string(fd, " {");
  347. for name, i in info.names {
  348. if i > 0 do os.write_string(fd, ", ");
  349. os.write_string(fd, name);
  350. os.write_string(fd, ": ");
  351. print_u64(fd, u64(info.bits[i]));
  352. }
  353. os.write_string(fd, "}");
  354. case Type_Info_Bit_Set:
  355. os.write_string(fd, "bit_set[");
  356. #partial switch elem in type_info_base(info.elem).variant {
  357. case Type_Info_Enum:
  358. print_type(fd, info.elem);
  359. case Type_Info_Rune:
  360. os.write_encoded_rune(fd, rune(info.lower));
  361. os.write_string(fd, "..");
  362. os.write_encoded_rune(fd, rune(info.upper));
  363. case:
  364. print_i64(fd, info.lower);
  365. os.write_string(fd, "..");
  366. print_i64(fd, info.upper);
  367. }
  368. if info.underlying != nil {
  369. os.write_string(fd, "; ");
  370. print_type(fd, info.underlying);
  371. }
  372. os.write_byte(fd, ']');
  373. case Type_Info_Opaque:
  374. os.write_string(fd, "opaque ");
  375. print_type(fd, info.elem);
  376. case Type_Info_Simd_Vector:
  377. if info.is_x86_mmx {
  378. os.write_string(fd, "intrinsics.x86_mmx");
  379. } else {
  380. os.write_string(fd, "#simd[");
  381. print_u64(fd, u64(info.count));
  382. os.write_byte(fd, ']');
  383. print_type(fd, info.elem);
  384. }
  385. case Type_Info_Relative_Pointer:
  386. os.write_string(fd, "#relative(");
  387. print_type(fd, info.base_integer);
  388. os.write_string(fd, ") ");
  389. print_type(fd, info.pointer);
  390. case Type_Info_Relative_Slice:
  391. os.write_string(fd, "#relative(");
  392. print_type(fd, info.base_integer);
  393. os.write_string(fd, ") ");
  394. print_type(fd, info.slice);
  395. }
  396. }
  397. memory_compare :: proc "contextless" (a, b: rawptr, n: int) -> int #no_bounds_check {
  398. x := uintptr(a);
  399. y := uintptr(b);
  400. n := uintptr(n);
  401. SU :: size_of(uintptr);
  402. fast := uintptr(n/SU + 1);
  403. offset := (fast-1)*SU;
  404. curr_block := uintptr(0);
  405. if n < SU {
  406. fast = 0;
  407. }
  408. for /**/; curr_block < fast; curr_block += 1 {
  409. va := (^uintptr)(x + curr_block * size_of(uintptr))^;
  410. vb := (^uintptr)(y + curr_block * size_of(uintptr))^;
  411. if va ~ vb != 0 {
  412. for pos := curr_block*SU; pos < n; pos += 1 {
  413. a := (^byte)(x+pos)^;
  414. b := (^byte)(y+pos)^;
  415. if a ~ b != 0 {
  416. return -1 if (int(a) - int(b)) < 0 else +1;
  417. }
  418. }
  419. }
  420. }
  421. for /**/; offset < n; offset += 1 {
  422. a := (^byte)(x+offset)^;
  423. b := (^byte)(y+offset)^;
  424. if a ~ b != 0 {
  425. return -1 if (int(a) - int(b)) < 0 else +1;
  426. }
  427. }
  428. return 0;
  429. }
  430. memory_compare_zero :: proc "contextless" (a: rawptr, n: int) -> int #no_bounds_check {
  431. x := uintptr(a);
  432. n := uintptr(n);
  433. SU :: size_of(uintptr);
  434. fast := uintptr(n/SU + 1);
  435. offset := (fast-1)*SU;
  436. curr_block := uintptr(0);
  437. if n < SU {
  438. fast = 0;
  439. }
  440. for /**/; curr_block < fast; curr_block += 1 {
  441. va := (^uintptr)(x + curr_block * size_of(uintptr))^;
  442. if va ~ 0 != 0 {
  443. for pos := curr_block*SU; pos < n; pos += 1 {
  444. a := (^byte)(x+pos)^;
  445. if a ~ 0 != 0 {
  446. return -1 if int(a) < 0 else +1;
  447. }
  448. }
  449. }
  450. }
  451. for /**/; offset < n; offset += 1 {
  452. a := (^byte)(x+offset)^;
  453. if a ~ 0 != 0 {
  454. return -1 if int(a) < 0 else +1;
  455. }
  456. }
  457. return 0;
  458. }
  459. string_eq :: proc "contextless" (a, b: string) -> bool {
  460. x := transmute(Raw_String)a;
  461. y := transmute(Raw_String)b;
  462. switch {
  463. case x.len != y.len: return false;
  464. case x.len == 0: return true;
  465. case x.data == y.data: return true;
  466. }
  467. return string_cmp(a, b) == 0;
  468. }
  469. string_cmp :: proc "contextless" (a, b: string) -> int {
  470. x := transmute(Raw_String)a;
  471. y := transmute(Raw_String)b;
  472. return memory_compare(x.data, y.data, min(x.len, y.len));
  473. }
  474. string_ne :: inline proc "contextless" (a, b: string) -> bool { return !string_eq(a, b); }
  475. string_lt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) < 0; }
  476. string_gt :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) > 0; }
  477. string_le :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) <= 0; }
  478. string_ge :: inline proc "contextless" (a, b: string) -> bool { return string_cmp(a, b) >= 0; }
  479. cstring_len :: proc "contextless" (s: cstring) -> int {
  480. p0 := uintptr((^byte)(s));
  481. p := p0;
  482. for p != 0 && (^byte)(p)^ != 0 {
  483. p += 1;
  484. }
  485. return int(p - p0);
  486. }
  487. cstring_to_string :: proc "contextless" (s: cstring) -> string {
  488. if s == nil do return "";
  489. ptr := (^byte)(s);
  490. n := cstring_len(s);
  491. return transmute(string)Raw_String{ptr, n};
  492. }
  493. complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  494. complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  495. complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  496. complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  497. quaternion128_eq :: inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b); }
  498. quaternion128_ne :: inline proc "contextless" (a, b: quaternion128) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b); }
  499. quaternion256_eq :: inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) == real(b) && imag(a) == imag(b) && jmag(a) == jmag(b) && kmag(a) == kmag(b); }
  500. quaternion256_ne :: inline proc "contextless" (a, b: quaternion256) -> bool { return real(a) != real(b) || imag(a) != imag(b) || jmag(a) != jmag(b) || kmag(a) != kmag(b); }
  501. bounds_trap :: proc "contextless" () -> ! {
  502. when ODIN_OS == "windows" {
  503. windows_trap_array_bounds();
  504. } else {
  505. trap();
  506. }
  507. }
  508. type_assertion_trap :: proc "contextless" () -> ! {
  509. when ODIN_OS == "windows" {
  510. windows_trap_type_assertion();
  511. } else {
  512. trap();
  513. }
  514. }
  515. bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
  516. if 0 <= index && index < count do return;
  517. handle_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
  518. context = default_context();
  519. fd := os.stderr;
  520. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  521. os.write_string(fd, " Index ");
  522. print_i64(fd, i64(index));
  523. os.write_string(fd, " is out of bounds range 0:");
  524. print_i64(fd, i64(count));
  525. os.write_byte(fd, '\n');
  526. bounds_trap();
  527. }
  528. handle_error(file, line, column, index, count);
  529. }
  530. slice_handle_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
  531. context = default_context();
  532. fd := os.stderr;
  533. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  534. os.write_string(fd, " Invalid slice indices: ");
  535. print_i64(fd, i64(lo));
  536. os.write_string(fd, ":");
  537. print_i64(fd, i64(hi));
  538. os.write_string(fd, ":");
  539. print_i64(fd, i64(len));
  540. os.write_byte(fd, '\n');
  541. bounds_trap();
  542. }
  543. slice_expr_error_hi :: proc "contextless" (file: string, line, column: int, hi: int, len: int) {
  544. if 0 <= hi && hi <= len do return;
  545. slice_handle_error(file, line, column, 0, hi, len);
  546. }
  547. slice_expr_error_lo_hi :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
  548. if 0 <= lo && lo <= len && lo <= hi && hi <= len do return;
  549. slice_handle_error(file, line, column, lo, hi, len);
  550. }
  551. dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
  552. if 0 <= low && low <= high && high <= max do return;
  553. handle_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
  554. context = default_context();
  555. fd := os.stderr;
  556. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  557. os.write_string(fd, " Invalid dynamic array values: ");
  558. print_i64(fd, i64(low));
  559. os.write_string(fd, ":");
  560. print_i64(fd, i64(high));
  561. os.write_string(fd, ":");
  562. print_i64(fd, i64(max));
  563. os.write_byte(fd, '\n');
  564. bounds_trap();
  565. }
  566. handle_error(file, line, column, low, high, max);
  567. }
  568. type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) {
  569. if ok do return;
  570. handle_error :: proc "contextless" (file: string, line, column: int, from, to: typeid) {
  571. context = default_context();
  572. fd := os.stderr;
  573. print_caller_location(fd, Source_Code_Location{file, line, column, "", 0});
  574. os.write_string(fd, " Invalid type assertion from ");
  575. print_typeid(fd, from);
  576. os.write_string(fd, " to ");
  577. print_typeid(fd, to);
  578. os.write_byte(fd, '\n');
  579. type_assertion_trap();
  580. }
  581. handle_error(file, line, column, from, to);
  582. }
  583. string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) {
  584. // NOTE(bill): Duplicated here to remove dependency on package unicode/utf8
  585. @static accept_sizes := [256]u8{
  586. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
  587. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
  588. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
  589. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x30-0x3f
  590. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x40-0x4f
  591. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x50-0x5f
  592. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x60-0x6f
  593. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x70-0x7f
  594. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x80-0x8f
  595. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x90-0x9f
  596. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xa0-0xaf
  597. 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xb0-0xbf
  598. 0xf1, 0xf1, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xc0-0xcf
  599. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf
  600. 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef
  601. 0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
  602. };
  603. Accept_Range :: struct {lo, hi: u8};
  604. @static accept_ranges := [5]Accept_Range{
  605. {0x80, 0xbf},
  606. {0xa0, 0xbf},
  607. {0x80, 0x9f},
  608. {0x90, 0xbf},
  609. {0x80, 0x8f},
  610. };
  611. MASKX :: 0b0011_1111;
  612. MASK2 :: 0b0001_1111;
  613. MASK3 :: 0b0000_1111;
  614. MASK4 :: 0b0000_0111;
  615. LOCB :: 0b1000_0000;
  616. HICB :: 0b1011_1111;
  617. RUNE_ERROR :: '\ufffd';
  618. n := len(s);
  619. if n < 1 {
  620. return RUNE_ERROR, 0;
  621. }
  622. s0 := s[0];
  623. x := accept_sizes[s0];
  624. if x >= 0xF0 {
  625. mask := rune(x) << 31 >> 31; // NOTE(bill): Create 0x0000 or 0xffff.
  626. return rune(s[0])&~mask | RUNE_ERROR&mask, 1;
  627. }
  628. sz := x & 7;
  629. accept := accept_ranges[x>>4];
  630. if n < int(sz) {
  631. return RUNE_ERROR, 1;
  632. }
  633. b1 := s[1];
  634. if b1 < accept.lo || accept.hi < b1 {
  635. return RUNE_ERROR, 1;
  636. }
  637. if sz == 2 {
  638. return rune(s0&MASK2)<<6 | rune(b1&MASKX), 2;
  639. }
  640. b2 := s[2];
  641. if b2 < LOCB || HICB < b2 {
  642. return RUNE_ERROR, 1;
  643. }
  644. if sz == 3 {
  645. return rune(s0&MASK3)<<12 | rune(b1&MASKX)<<6 | rune(b2&MASKX), 3;
  646. }
  647. b3 := s[3];
  648. if b3 < LOCB || HICB < b3 {
  649. return RUNE_ERROR, 1;
  650. }
  651. return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4;
  652. }
  653. bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) {
  654. bounds_check_error(file_path, int(line), int(column), index, count);
  655. }
  656. slice_expr_error_hi_loc :: inline proc "contextless" (using loc := #caller_location, hi: int, len: int) {
  657. slice_expr_error_hi(file_path, int(line), int(column), hi, len);
  658. }
  659. slice_expr_error_lo_hi_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
  660. slice_expr_error_lo_hi(file_path, int(line), int(column), lo, hi, len);
  661. }
  662. dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, low, high, max: int) {
  663. dynamic_array_expr_error(file_path, int(line), int(column), low, high, max);
  664. }
  665. make_slice_error_loc :: inline proc "contextless" (loc := #caller_location, len: int) {
  666. if 0 <= len do return;
  667. handle_error :: proc "contextless" (loc: Source_Code_Location, len: int) {
  668. context = default_context();
  669. fd := os.stderr;
  670. print_caller_location(fd, loc);
  671. os.write_string(fd, " Invalid slice length for make: ");
  672. print_i64(fd, i64(len));
  673. os.write_byte(fd, '\n');
  674. bounds_trap();
  675. }
  676. handle_error(loc, len);
  677. }
  678. make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) {
  679. if 0 <= len && len <= cap do return;
  680. handle_error :: proc "contextless" (loc: Source_Code_Location, len, cap: int) {
  681. context = default_context();
  682. fd := os.stderr;
  683. print_caller_location(fd, loc);
  684. os.write_string(fd, " Invalid dynamic array parameters for make: ");
  685. print_i64(fd, i64(len));
  686. os.write_byte(fd, ':');
  687. print_i64(fd, i64(cap));
  688. os.write_byte(fd, '\n');
  689. bounds_trap();
  690. }
  691. handle_error(loc, len, cap);
  692. }
  693. make_map_expr_error_loc :: inline proc "contextless" (loc := #caller_location, cap: int) {
  694. if 0 <= cap do return;
  695. handle_error :: proc "contextless" (loc: Source_Code_Location, cap: int) {
  696. context = default_context();
  697. fd := os.stderr;
  698. print_caller_location(fd, loc);
  699. os.write_string(fd, " Invalid map capacity for make: ");
  700. print_i64(fd, i64(cap));
  701. os.write_byte(fd, '\n');
  702. bounds_trap();
  703. }
  704. handle_error(loc, cap);
  705. }
  706. @(default_calling_convention = "c")
  707. foreign {
  708. @(link_name="llvm.sqrt.f32") _sqrt_f32 :: proc(x: f32) -> f32 ---
  709. @(link_name="llvm.sqrt.f64") _sqrt_f64 :: proc(x: f64) -> f64 ---
  710. }
  711. abs_f32 :: inline proc "contextless" (x: f32) -> f32 {
  712. foreign {
  713. @(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 ---
  714. }
  715. return _abs(x);
  716. }
  717. abs_f64 :: inline proc "contextless" (x: f64) -> f64 {
  718. foreign {
  719. @(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 ---
  720. }
  721. return _abs(x);
  722. }
  723. min_f32 :: proc(a, b: f32) -> f32 {
  724. foreign {
  725. @(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 ---
  726. }
  727. return _min(a, b);
  728. }
  729. min_f64 :: proc(a, b: f64) -> f64 {
  730. foreign {
  731. @(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 ---
  732. }
  733. return _min(a, b);
  734. }
  735. max_f32 :: proc(a, b: f32) -> f32 {
  736. foreign {
  737. @(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 ---
  738. }
  739. return _max(a, b);
  740. }
  741. max_f64 :: proc(a, b: f64) -> f64 {
  742. foreign {
  743. @(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 ---
  744. }
  745. return _max(a, b);
  746. }
  747. abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 {
  748. r, i := real(x), imag(x);
  749. return _sqrt_f32(r*r + i*i);
  750. }
  751. abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 {
  752. r, i := real(x), imag(x);
  753. return _sqrt_f64(r*r + i*i);
  754. }
  755. abs_quaternion128 :: inline proc "contextless" (x: quaternion128) -> f32 {
  756. r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
  757. return _sqrt_f32(r*r + i*i + j*j + k*k);
  758. }
  759. abs_quaternion256 :: inline proc "contextless" (x: quaternion256) -> f64 {
  760. r, i, j, k := real(x), imag(x), jmag(x), kmag(x);
  761. return _sqrt_f64(r*r + i*i + j*j + k*k);
  762. }
  763. quo_complex64 :: proc "contextless" (n, m: complex64) -> complex64 {
  764. e, f: f32;
  765. if abs(real(m)) >= abs(imag(m)) {
  766. ratio := imag(m) / real(m);
  767. denom := real(m) + ratio*imag(m);
  768. e = (real(n) + imag(n)*ratio) / denom;
  769. f = (imag(n) - real(n)*ratio) / denom;
  770. } else {
  771. ratio := real(m) / imag(m);
  772. denom := imag(m) + ratio*real(m);
  773. e = (real(n)*ratio + imag(n)) / denom;
  774. f = (imag(n)*ratio - real(n)) / denom;
  775. }
  776. return complex(e, f);
  777. }
  778. quo_complex128 :: proc "contextless" (n, m: complex128) -> complex128 {
  779. e, f: f64;
  780. if abs(real(m)) >= abs(imag(m)) {
  781. ratio := imag(m) / real(m);
  782. denom := real(m) + ratio*imag(m);
  783. e = (real(n) + imag(n)*ratio) / denom;
  784. f = (imag(n) - real(n)*ratio) / denom;
  785. } else {
  786. ratio := real(m) / imag(m);
  787. denom := imag(m) + ratio*real(m);
  788. e = (real(n)*ratio + imag(n)) / denom;
  789. f = (imag(n)*ratio - real(n)) / denom;
  790. }
  791. return complex(e, f);
  792. }
  793. mul_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  794. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  795. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  796. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3;
  797. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2;
  798. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1;
  799. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0;
  800. return quaternion(t0, t1, t2, t3);
  801. }
  802. mul_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  803. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  804. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  805. t0 := r0*q0 - r1*q1 - r2*q2 - r3*q3;
  806. t1 := r0*q1 + r1*q0 - r2*q3 + r3*q2;
  807. t2 := r0*q2 + r1*q3 + r2*q0 - r3*q1;
  808. t3 := r0*q3 - r1*q2 + r2*q1 + r3*q0;
  809. return quaternion(t0, t1, t2, t3);
  810. }
  811. quo_quaternion128 :: proc "contextless" (q, r: quaternion128) -> quaternion128 {
  812. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  813. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  814. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3);
  815. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2;
  816. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2;
  817. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2;
  818. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2;
  819. return quaternion(t0, t1, t2, t3);
  820. }
  821. quo_quaternion256 :: proc "contextless" (q, r: quaternion256) -> quaternion256 {
  822. q0, q1, q2, q3 := real(q), imag(q), jmag(q), kmag(q);
  823. r0, r1, r2, r3 := real(r), imag(r), jmag(r), kmag(r);
  824. invmag2 := 1.0 / (r0*r0 + r1*r1 + r2*r2 + r3*r3);
  825. t0 := (r0*q0 + r1*q1 + r2*q2 + r3*q3) * invmag2;
  826. t1 := (r0*q1 - r1*q0 - r2*q3 - r3*q2) * invmag2;
  827. t2 := (r0*q2 - r1*q3 - r2*q0 + r3*q1) * invmag2;
  828. t3 := (r0*q3 + r1*q2 + r2*q1 - r3*q0) * invmag2;
  829. return quaternion(t0, t1, t2, t3);
  830. }