internal.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package runtime
  2. import "core:mem"
  3. import "core:os"
  4. import "core:unicode/utf8"
  5. __print_u64 :: proc(fd: os.Handle, u: u64) {
  6. digits := "0123456789";
  7. a: [129]byte;
  8. i := len(a);
  9. b := u64(10);
  10. for u >= b {
  11. i -= 1; a[i] = digits[u % b];
  12. u /= b;
  13. }
  14. i -= 1; a[i] = digits[u % b];
  15. os.write(fd, a[i:]);
  16. }
  17. __print_i64 :: proc(fd: os.Handle, u: i64) {
  18. digits := "0123456789";
  19. neg := u < 0;
  20. u = abs(u);
  21. a: [129]byte;
  22. i := len(a);
  23. b := i64(10);
  24. for u >= b {
  25. i -= 1; a[i] = digits[u % b];
  26. u /= b;
  27. }
  28. i -= 1; a[i] = digits[u % b];
  29. if neg {
  30. i -= 1; a[i] = '-';
  31. }
  32. os.write(fd, a[i:]);
  33. }
  34. __print_caller_location :: proc(fd: os.Handle, using loc: Source_Code_Location) {
  35. os.write_string(fd, file_path);
  36. os.write_byte(fd, '(');
  37. __print_u64(fd, u64(line));
  38. os.write_byte(fd, ':');
  39. __print_u64(fd, u64(column));
  40. os.write_byte(fd, ')');
  41. }
  42. __print_typeid :: proc(fd: os.Handle, id: typeid) {
  43. ti := type_info_of(id);
  44. __print_type(fd, ti);
  45. }
  46. __print_type :: proc(fd: os.Handle, ti: ^Type_Info) {
  47. if ti == nil {
  48. os.write_string(fd, "nil");
  49. return;
  50. }
  51. switch info in ti.variant {
  52. case Type_Info_Named:
  53. os.write_string(fd, info.name);
  54. case Type_Info_Integer:
  55. a := any{typeid = typeid_of(ti)};
  56. switch _ in a {
  57. case int: os.write_string(fd, "int");
  58. case uint: os.write_string(fd, "uint");
  59. case uintptr: os.write_string(fd, "uintptr");
  60. case:
  61. os.write_byte(fd, info.signed ? 'i' : 'u');
  62. __print_u64(fd, u64(8*ti.size));
  63. }
  64. case Type_Info_Rune:
  65. os.write_string(fd, "rune");
  66. case Type_Info_Float:
  67. os.write_byte(fd, 'f');
  68. __print_u64(fd, u64(8*ti.size));
  69. case Type_Info_Complex:
  70. os.write_string(fd, "complex");
  71. __print_u64(fd, u64(8*ti.size));
  72. case Type_Info_String:
  73. os.write_string(fd, "string");
  74. case Type_Info_Boolean:
  75. a := any{typeid = typeid_of(ti)};
  76. switch _ in a {
  77. case bool: os.write_string(fd, "bool");
  78. case:
  79. os.write_byte(fd, 'b');
  80. __print_u64(fd, u64(8*ti.size));
  81. }
  82. case Type_Info_Any:
  83. os.write_string(fd, "any");
  84. case Type_Info_Type_Id:
  85. os.write_string(fd, "typeid");
  86. case Type_Info_Pointer:
  87. if info.elem == nil {
  88. os.write_string(fd, "rawptr");
  89. } else {
  90. os.write_string(fd, "^");
  91. __print_type(fd, info.elem);
  92. }
  93. case Type_Info_Procedure:
  94. os.write_string(fd, "proc");
  95. if info.params == nil {
  96. os.write_string(fd, "()");
  97. } else {
  98. t := info.params.variant.(Type_Info_Tuple);
  99. os.write_string(fd, "(");
  100. for t, i in t.types {
  101. if i > 0 do os.write_string(fd, ", ");
  102. __print_type(fd, t);
  103. }
  104. os.write_string(fd, ")");
  105. }
  106. if info.results != nil {
  107. os.write_string(fd, " -> ");
  108. __print_type(fd, info.results);
  109. }
  110. case Type_Info_Tuple:
  111. count := len(info.names);
  112. if count != 1 do os.write_string(fd, "(");
  113. for name, i in info.names {
  114. if i > 0 do os.write_string(fd, ", ");
  115. t := info.types[i];
  116. if len(name) > 0 {
  117. os.write_string(fd, name);
  118. os.write_string(fd, ": ");
  119. }
  120. __print_type(fd, t);
  121. }
  122. if count != 1 do os.write_string(fd, ")");
  123. case Type_Info_Array:
  124. os.write_string(fd, "[");
  125. __print_u64(fd, u64(info.count));
  126. os.write_string(fd, "]");
  127. __print_type(fd, info.elem);
  128. case Type_Info_Dynamic_Array:
  129. os.write_string(fd, "[dynamic]");
  130. __print_type(fd, info.elem);
  131. case Type_Info_Slice:
  132. os.write_string(fd, "[]");
  133. __print_type(fd, info.elem);
  134. case Type_Info_Map:
  135. os.write_string(fd, "map[");
  136. __print_type(fd, info.key);
  137. os.write_byte(fd, ']');
  138. __print_type(fd, info.value);
  139. case Type_Info_Struct:
  140. os.write_string(fd, "struct ");
  141. if info.is_packed do os.write_string(fd, "#packed ");
  142. if info.is_raw_union do os.write_string(fd, "#raw_union ");
  143. if info.custom_align {
  144. os.write_string(fd, "#align ");
  145. __print_u64(fd, u64(ti.align));
  146. os.write_byte(fd, ' ');
  147. }
  148. os.write_byte(fd, '{');
  149. for name, i in info.names {
  150. if i > 0 do os.write_string(fd, ", ");
  151. os.write_string(fd, name);
  152. os.write_string(fd, ": ");
  153. __print_type(fd, info.types[i]);
  154. }
  155. os.write_byte(fd, '}');
  156. case Type_Info_Union:
  157. os.write_string(fd, "union {");
  158. for variant, i in info.variants {
  159. if i > 0 do os.write_string(fd, ", ");
  160. __print_type(fd, variant);
  161. }
  162. os.write_string(fd, "}");
  163. case Type_Info_Enum:
  164. os.write_string(fd, "enum ");
  165. __print_type(fd, info.base);
  166. os.write_string(fd, " {");
  167. for name, i in info.names {
  168. if i > 0 do os.write_string(fd, ", ");
  169. os.write_string(fd, name);
  170. }
  171. os.write_string(fd, "}");
  172. case Type_Info_Bit_Field:
  173. os.write_string(fd, "bit_field ");
  174. if ti.align != 1 {
  175. os.write_string(fd, "#align ");
  176. __print_u64(fd, u64(ti.align));
  177. os.write_byte(fd, ' ');
  178. }
  179. os.write_string(fd, " {");
  180. for name, i in info.names {
  181. if i > 0 do os.write_string(fd, ", ");
  182. os.write_string(fd, name);
  183. os.write_string(fd, ": ");
  184. __print_u64(fd, u64(info.bits[i]));
  185. }
  186. os.write_string(fd, "}");
  187. }
  188. }
  189. __string_eq :: proc "contextless" (a, b: string) -> bool {
  190. switch {
  191. case len(a) != len(b): return false;
  192. case len(a) == 0: return true;
  193. case &a[0] == &b[0]: return true;
  194. }
  195. return __string_cmp(a, b) == 0;
  196. }
  197. __string_cmp :: proc "contextless" (a, b: string) -> int {
  198. return mem.compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b)));
  199. }
  200. __string_ne :: inline proc "contextless" (a, b: string) -> bool { return !__string_eq(a, b); }
  201. __string_lt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) < 0; }
  202. __string_gt :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) > 0; }
  203. __string_le :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) <= 0; }
  204. __string_ge :: inline proc "contextless" (a, b: string) -> bool { return __string_cmp(a, b) >= 0; }
  205. __cstring_len :: proc "contextless" (s: cstring) -> int {
  206. n := 0;
  207. for p := (^byte)(s); p != nil && p^ != 0; p = mem.ptr_offset(p, 1) {
  208. n += 1;
  209. }
  210. return n;
  211. }
  212. __cstring_to_string :: proc "contextless" (s: cstring) -> string {
  213. if s == nil do return "";
  214. ptr := (^byte)(s);
  215. n := __cstring_len(s);
  216. return transmute(string)mem.Raw_String{ptr, n};
  217. }
  218. __complex64_eq :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  219. __complex64_ne :: inline proc "contextless" (a, b: complex64) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  220. __complex128_eq :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) == real(b) && imag(a) == imag(b); }
  221. __complex128_ne :: inline proc "contextless" (a, b: complex128) -> bool { return real(a) != real(b) || imag(a) != imag(b); }
  222. bounds_check_error :: proc "contextless" (file: string, line, column: int, index, count: int) {
  223. if 0 <= index && index < count do return;
  224. fd := os.stderr;
  225. __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
  226. os.write_string(fd, " Index ");
  227. __print_i64(fd, i64(index));
  228. os.write_string(fd, " is out of bounds range 0:");
  229. __print_i64(fd, i64(count));
  230. os.write_byte(fd, '\n');
  231. debug_trap();
  232. }
  233. slice_expr_error :: proc "contextless" (file: string, line, column: int, lo, hi: int, len: int) {
  234. if 0 <= lo && lo <= hi && hi <= len do return;
  235. fd := os.stderr;
  236. __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
  237. os.write_string(fd, " Invalid slice indices: ");
  238. __print_i64(fd, i64(lo));
  239. os.write_string(fd, ":");
  240. __print_i64(fd, i64(hi));
  241. os.write_string(fd, ":");
  242. __print_i64(fd, i64(len));
  243. os.write_byte(fd, '\n');
  244. debug_trap();
  245. }
  246. dynamic_array_expr_error :: proc "contextless" (file: string, line, column: int, low, high, max: int) {
  247. if 0 <= low && low <= high && high <= max do return;
  248. fd := os.stderr;
  249. __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
  250. os.write_string(fd, " Invalid dynamic array values: ");
  251. __print_i64(fd, i64(low));
  252. os.write_string(fd, ":");
  253. __print_i64(fd, i64(high));
  254. os.write_string(fd, ":");
  255. __print_i64(fd, i64(max));
  256. os.write_byte(fd, '\n');
  257. debug_trap();
  258. }
  259. type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: int, from, to: typeid) {
  260. if ok do return;
  261. fd := os.stderr;
  262. __print_caller_location(fd, Source_Code_Location{file, line, column, ""});
  263. os.write_string(fd, " Invalid type assertion from ");
  264. __print_typeid(fd, from);
  265. os.write_string(fd, " to ");
  266. __print_typeid(fd, to);
  267. os.write_byte(fd, '\n');
  268. debug_trap();
  269. }
  270. __string_decode_rune :: inline proc "contextless" (s: string) -> (rune, int) {
  271. return utf8.decode_rune_from_string(s);
  272. }
  273. bounds_check_error_loc :: inline proc "contextless" (using loc := #caller_location, index, count: int) {
  274. bounds_check_error(file_path, int(line), int(column), index, count);
  275. }
  276. slice_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, lo, hi: int, len: int) {
  277. slice_expr_error(file_path, int(line), int(column), lo, hi, len);
  278. }
  279. dynamic_array_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, low, high, max: int) {
  280. dynamic_array_expr_error(file_path, int(line), int(column), low, high, max);
  281. }
  282. make_slice_error_loc :: inline proc "contextless" (using loc := #caller_location, len: int) {
  283. if 0 <= len do return;
  284. fd := os.stderr;
  285. __print_caller_location(fd, loc);
  286. os.write_string(fd, " Invalid slice length for make: ");
  287. __print_i64(fd, i64(len));
  288. os.write_byte(fd, '\n');
  289. debug_trap();
  290. }
  291. make_dynamic_array_error_loc :: inline proc "contextless" (using loc := #caller_location, len, cap: int) {
  292. if 0 <= len && len <= cap do return;
  293. fd := os.stderr;
  294. __print_caller_location(fd, loc);
  295. os.write_string(fd, " Invalid dynamic array parameters for make: ");
  296. __print_i64(fd, i64(len));
  297. os.write_byte(fd, ':');
  298. __print_i64(fd, i64(cap));
  299. os.write_byte(fd, '\n');
  300. debug_trap();
  301. }
  302. make_map_expr_error_loc :: inline proc "contextless" (using loc := #caller_location, cap: int) {
  303. if 0 <= cap do return;
  304. fd := os.stderr;
  305. __print_caller_location(fd, loc);
  306. os.write_string(fd, " Invalid map capacity for make: ");
  307. __print_i64(fd, i64(cap));
  308. os.write_byte(fd, '\n');
  309. debug_trap();
  310. }
  311. @(default_calling_convention = "c")
  312. foreign {
  313. @(link_name="llvm.sqrt.f32") __sqrt_f32 :: proc(x: f32) -> f32 ---
  314. @(link_name="llvm.sqrt.f64") __sqrt_f64 :: proc(x: f64) -> f64 ---
  315. @(link_name="llvm.sin.f32") __sin_f32 :: proc(θ: f32) -> f32 ---
  316. @(link_name="llvm.sin.f64") __sin_f64 :: proc(θ: f64) -> f64 ---
  317. @(link_name="llvm.cos.f32") __cos_f32 :: proc(θ: f32) -> f32 ---
  318. @(link_name="llvm.cos.f64") __cos_f64 :: proc(θ: f64) -> f64 ---
  319. @(link_name="llvm.pow.f32") __pow_f32 :: proc(x, power: f32) -> f32 ---
  320. @(link_name="llvm.pow.f64") __pow_f64 :: proc(x, power: f64) -> f64 ---
  321. @(link_name="llvm.fmuladd.f32") fmuladd32 :: proc(a, b, c: f32) -> f32 ---
  322. @(link_name="llvm.fmuladd.f64") fmuladd64 :: proc(a, b, c: f64) -> f64 ---
  323. }
  324. __abs_f32 :: inline proc "contextless" (x: f32) -> f32 {
  325. foreign {
  326. @(link_name="llvm.fabs.f32") _abs :: proc "c" (x: f32) -> f32 ---
  327. }
  328. return _abs(x);
  329. }
  330. __abs_f64 :: inline proc "contextless" (x: f64) -> f64 {
  331. foreign {
  332. @(link_name="llvm.fabs.f64") _abs :: proc "c" (x: f64) -> f64 ---
  333. }
  334. return _abs(x);
  335. }
  336. __min_f32 :: proc(a, b: f32) -> f32 {
  337. foreign {
  338. @(link_name="llvm.minnum.f32") _min :: proc "c" (a, b: f32) -> f32 ---
  339. }
  340. return _min(a, b);
  341. }
  342. __min_f64 :: proc(a, b: f64) -> f64 {
  343. foreign {
  344. @(link_name="llvm.minnum.f64") _min :: proc "c" (a, b: f64) -> f64 ---
  345. }
  346. return _min(a, b);
  347. }
  348. __max_f32 :: proc(a, b: f32) -> f32 {
  349. foreign {
  350. @(link_name="llvm.maxnum.f32") _max :: proc "c" (a, b: f32) -> f32 ---
  351. }
  352. return _max(a, b);
  353. }
  354. __max_f64 :: proc(a, b: f64) -> f64 {
  355. foreign {
  356. @(link_name="llvm.maxnum.f64") _max :: proc "c" (a, b: f64) -> f64 ---
  357. }
  358. return _max(a, b);
  359. }
  360. __abs_complex64 :: inline proc "contextless" (x: complex64) -> f32 {
  361. r, i := real(x), imag(x);
  362. return __sqrt_f32(r*r + i*i);
  363. }
  364. __abs_complex128 :: inline proc "contextless" (x: complex128) -> f64 {
  365. r, i := real(x), imag(x);
  366. return __sqrt_f64(r*r + i*i);
  367. }