internal.odin 26 KB

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