print.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package runtime
  2. _INTEGER_DIGITS :: "0123456789abcdefghijklmnopqrstuvwxyz"
  3. @(private="file")
  4. _INTEGER_DIGITS_VAR := _INTEGER_DIGITS
  5. when !ODIN_NO_RTTI {
  6. print_any_single :: #force_no_inline proc "contextless" (arg: any) {
  7. x := arg
  8. if x.data == nil {
  9. print_string("nil")
  10. return
  11. }
  12. if loc, ok := x.(Source_Code_Location); ok {
  13. print_caller_location(loc)
  14. return
  15. }
  16. x.id = typeid_base(x.id)
  17. switch v in x {
  18. case typeid: print_typeid(v)
  19. case ^Type_Info: print_type(v)
  20. case string: print_string(v)
  21. case cstring: print_string(string(v))
  22. case []byte: print_string(string(v))
  23. case rune: print_rune(v)
  24. case u8: print_u64(u64(v))
  25. case u16: print_u64(u64(v))
  26. case u16le: print_u64(u64(v))
  27. case u16be: print_u64(u64(v))
  28. case u32: print_u64(u64(v))
  29. case u32le: print_u64(u64(v))
  30. case u32be: print_u64(u64(v))
  31. case u64: print_u64(u64(v))
  32. case u64le: print_u64(u64(v))
  33. case u64be: print_u64(u64(v))
  34. case i8: print_i64(i64(v))
  35. case i16: print_i64(i64(v))
  36. case i16le: print_i64(i64(v))
  37. case i16be: print_i64(i64(v))
  38. case i32: print_i64(i64(v))
  39. case i32le: print_i64(i64(v))
  40. case i32be: print_i64(i64(v))
  41. case i64: print_i64(i64(v))
  42. case i64le: print_i64(i64(v))
  43. case i64be: print_i64(i64(v))
  44. case int: print_int(v)
  45. case uint: print_uint(v)
  46. case uintptr: print_uintptr(v)
  47. case rawptr: print_uintptr(uintptr(v))
  48. case bool: print_string("true" if v else "false")
  49. case b8: print_string("true" if v else "false")
  50. case b16: print_string("true" if v else "false")
  51. case b32: print_string("true" if v else "false")
  52. case b64: print_string("true" if v else "false")
  53. case:
  54. ti := type_info_of(x.id)
  55. #partial switch v in ti.variant {
  56. case Type_Info_Pointer, Type_Info_Multi_Pointer:
  57. print_uintptr((^uintptr)(x.data)^)
  58. return
  59. }
  60. print_string("<invalid-value>")
  61. }
  62. }
  63. println_any :: #force_no_inline proc "contextless" (args: ..any) {
  64. context = default_context()
  65. loop: for arg, i in args {
  66. assert(arg.id != nil)
  67. if i != 0 {
  68. print_string(" ")
  69. }
  70. print_any_single(arg)
  71. }
  72. print_string("\n")
  73. }
  74. }
  75. encode_rune :: proc "contextless" (c: rune) -> ([4]u8, int) {
  76. r := c
  77. buf: [4]u8
  78. i := u32(r)
  79. mask :: u8(0x3f)
  80. if i <= 1<<7-1 {
  81. buf[0] = u8(r)
  82. return buf, 1
  83. }
  84. if i <= 1<<11-1 {
  85. buf[0] = 0xc0 | u8(r>>6)
  86. buf[1] = 0x80 | u8(r) & mask
  87. return buf, 2
  88. }
  89. // Invalid or Surrogate range
  90. if i > 0x0010ffff ||
  91. (0xd800 <= i && i <= 0xdfff) {
  92. r = 0xfffd
  93. }
  94. if i <= 1<<16-1 {
  95. buf[0] = 0xe0 | u8(r>>12)
  96. buf[1] = 0x80 | u8(r>>6) & mask
  97. buf[2] = 0x80 | u8(r) & mask
  98. return buf, 3
  99. }
  100. buf[0] = 0xf0 | u8(r>>18)
  101. buf[1] = 0x80 | u8(r>>12) & mask
  102. buf[2] = 0x80 | u8(r>>6) & mask
  103. buf[3] = 0x80 | u8(r) & mask
  104. return buf, 4
  105. }
  106. print_string :: #force_no_inline proc "contextless" (str: string) -> (n: int) {
  107. n, _ = stderr_write(transmute([]byte)str)
  108. return
  109. }
  110. print_strings :: #force_no_inline proc "contextless" (args: ..string) -> (n: int) {
  111. for str in args {
  112. m, err := stderr_write(transmute([]byte)str)
  113. n += m
  114. if err != 0 {
  115. break
  116. }
  117. }
  118. return
  119. }
  120. print_byte :: #force_no_inline proc "contextless" (b: byte) -> (n: int) {
  121. n, _ = stderr_write([]byte{b})
  122. return
  123. }
  124. print_encoded_rune :: #force_no_inline proc "contextless" (r: rune) {
  125. print_byte('\'')
  126. switch r {
  127. case '\a': print_string("\\a")
  128. case '\b': print_string("\\b")
  129. case '\e': print_string("\\e")
  130. case '\f': print_string("\\f")
  131. case '\n': print_string("\\n")
  132. case '\r': print_string("\\r")
  133. case '\t': print_string("\\t")
  134. case '\v': print_string("\\v")
  135. case:
  136. if r <= 0 {
  137. print_string("\\x00")
  138. } else if r < 32 {
  139. n0, n1 := u8(r) >> 4, u8(r) & 0xf
  140. print_string("\\x")
  141. print_byte(_INTEGER_DIGITS_VAR[n0])
  142. print_byte(_INTEGER_DIGITS_VAR[n1])
  143. } else {
  144. print_rune(r)
  145. }
  146. }
  147. print_byte('\'')
  148. }
  149. print_rune :: #force_no_inline proc "contextless" (r: rune) -> int #no_bounds_check {
  150. RUNE_SELF :: 0x80
  151. if r < RUNE_SELF {
  152. return print_byte(byte(r))
  153. }
  154. b, n := encode_rune(r)
  155. m, _ := stderr_write(b[:n])
  156. return m
  157. }
  158. print_u64 :: #force_no_inline proc "contextless" (x: u64) #no_bounds_check {
  159. a: [129]byte
  160. i := len(a)
  161. b := u64(10)
  162. u := x
  163. for u >= b {
  164. i -= 1; a[i] = _INTEGER_DIGITS_VAR[u % b]
  165. u /= b
  166. }
  167. i -= 1; a[i] = _INTEGER_DIGITS_VAR[u % b]
  168. stderr_write(a[i:])
  169. }
  170. print_i64 :: #force_no_inline proc "contextless" (x: i64) #no_bounds_check {
  171. b :: i64(10)
  172. u := x
  173. neg := u < 0
  174. u = abs(u)
  175. a: [129]byte
  176. i := len(a)
  177. for u >= b {
  178. i -= 1; a[i] = _INTEGER_DIGITS_VAR[u % b]
  179. u /= b
  180. }
  181. i -= 1; a[i] = _INTEGER_DIGITS_VAR[u % b]
  182. if neg {
  183. i -= 1; a[i] = '-'
  184. }
  185. stderr_write(a[i:])
  186. }
  187. print_uint :: proc "contextless" (x: uint) { print_u64(u64(x)) }
  188. print_uintptr :: proc "contextless" (x: uintptr) { print_u64(u64(x)) }
  189. print_int :: proc "contextless" (x: int) { print_i64(i64(x)) }
  190. print_caller_location :: #force_no_inline proc "contextless" (loc: Source_Code_Location) {
  191. print_string(loc.file_path)
  192. when ODIN_ERROR_POS_STYLE == .Default {
  193. print_byte('(')
  194. print_u64(u64(loc.line))
  195. if loc.column != 0 {
  196. print_byte(':')
  197. print_u64(u64(loc.column))
  198. }
  199. print_byte(')')
  200. } else when ODIN_ERROR_POS_STYLE == .Unix {
  201. print_byte(':')
  202. print_u64(u64(loc.line))
  203. if loc.column != 0 {
  204. print_byte(':')
  205. print_u64(u64(loc.column))
  206. }
  207. print_byte(':')
  208. } else {
  209. #panic("unhandled ODIN_ERROR_POS_STYLE")
  210. }
  211. }
  212. print_typeid :: #force_no_inline proc "contextless" (id: typeid) {
  213. when ODIN_NO_RTTI {
  214. if id == nil {
  215. print_string("nil")
  216. } else {
  217. print_string("<unknown type>")
  218. }
  219. } else {
  220. if id == nil {
  221. print_string("nil")
  222. } else {
  223. ti := type_info_of(id)
  224. print_type(ti)
  225. }
  226. }
  227. }
  228. @(optimization_mode="size")
  229. print_type :: #force_no_inline proc "contextless" (ti: ^Type_Info) {
  230. if ti == nil {
  231. print_string("nil")
  232. return
  233. }
  234. switch info in ti.variant {
  235. case Type_Info_Named:
  236. print_string(info.name)
  237. case Type_Info_Integer:
  238. switch ti.id {
  239. case int: print_string("int")
  240. case uint: print_string("uint")
  241. case uintptr: print_string("uintptr")
  242. case:
  243. print_byte('i' if info.signed else 'u')
  244. print_u64(u64(8*ti.size))
  245. }
  246. case Type_Info_Rune:
  247. print_string("rune")
  248. case Type_Info_Float:
  249. print_byte('f')
  250. print_u64(u64(8*ti.size))
  251. case Type_Info_Complex:
  252. print_string("complex")
  253. print_u64(u64(8*ti.size))
  254. case Type_Info_Quaternion:
  255. print_string("quaternion")
  256. print_u64(u64(8*ti.size))
  257. case Type_Info_String:
  258. print_string("string")
  259. case Type_Info_Boolean:
  260. switch ti.id {
  261. case bool: print_string("bool")
  262. case:
  263. print_byte('b')
  264. print_u64(u64(8*ti.size))
  265. }
  266. case Type_Info_Any:
  267. print_string("any")
  268. case Type_Info_Type_Id:
  269. print_string("typeid")
  270. case Type_Info_Pointer:
  271. if info.elem == nil {
  272. print_string("rawptr")
  273. } else {
  274. print_string("^")
  275. print_type(info.elem)
  276. }
  277. case Type_Info_Multi_Pointer:
  278. print_string("[^]")
  279. print_type(info.elem)
  280. case Type_Info_Soa_Pointer:
  281. print_string("#soa ^")
  282. print_type(info.elem)
  283. case Type_Info_Procedure:
  284. print_string("proc")
  285. if info.params == nil {
  286. print_string("()")
  287. } else {
  288. t := info.params.variant.(Type_Info_Parameters)
  289. print_byte('(')
  290. for t, i in t.types {
  291. if i > 0 { print_string(", ") }
  292. print_type(t)
  293. }
  294. print_string(")")
  295. }
  296. if info.results != nil {
  297. print_string(" -> ")
  298. print_type(info.results)
  299. }
  300. case Type_Info_Parameters:
  301. count := len(info.names)
  302. if count != 1 { print_byte('(') }
  303. for name, i in info.names {
  304. if i > 0 { print_string(", ") }
  305. t := info.types[i]
  306. if len(name) > 0 {
  307. print_string(name)
  308. print_string(": ")
  309. }
  310. print_type(t)
  311. }
  312. if count != 1 { print_string(")") }
  313. case Type_Info_Array:
  314. print_byte('[')
  315. print_u64(u64(info.count))
  316. print_byte(']')
  317. print_type(info.elem)
  318. case Type_Info_Enumerated_Array:
  319. if info.is_sparse {
  320. print_string("#sparse")
  321. }
  322. print_byte('[')
  323. print_type(info.index)
  324. print_byte(']')
  325. print_type(info.elem)
  326. case Type_Info_Dynamic_Array:
  327. print_string("[dynamic]")
  328. print_type(info.elem)
  329. case Type_Info_Slice:
  330. print_string("[]")
  331. print_type(info.elem)
  332. case Type_Info_Map:
  333. print_string("map[")
  334. print_type(info.key)
  335. print_byte(']')
  336. print_type(info.value)
  337. case Type_Info_Struct:
  338. switch info.soa_kind {
  339. case .None: // Ignore
  340. case .Fixed:
  341. print_string("#soa[")
  342. print_u64(u64(info.soa_len))
  343. print_byte(']')
  344. print_type(info.soa_base_type)
  345. return
  346. case .Slice:
  347. print_string("#soa[]")
  348. print_type(info.soa_base_type)
  349. return
  350. case .Dynamic:
  351. print_string("#soa[dynamic]")
  352. print_type(info.soa_base_type)
  353. return
  354. }
  355. print_string("struct ")
  356. if info.is_packed { print_string("#packed ") }
  357. if info.is_raw_union { print_string("#raw_union ") }
  358. if info.custom_align {
  359. print_string("#align(")
  360. print_u64(u64(ti.align))
  361. print_string(") ")
  362. }
  363. print_byte('{')
  364. for name, i in info.names {
  365. if i > 0 { print_string(", ") }
  366. print_string(name)
  367. print_string(": ")
  368. print_type(info.types[i])
  369. }
  370. print_byte('}')
  371. case Type_Info_Union:
  372. print_string("union ")
  373. if info.custom_align {
  374. print_string("#align(")
  375. print_u64(u64(ti.align))
  376. print_string(") ")
  377. }
  378. if info.no_nil {
  379. print_string("#no_nil ")
  380. }
  381. print_byte('{')
  382. for variant, i in info.variants {
  383. if i > 0 { print_string(", ") }
  384. print_type(variant)
  385. }
  386. print_string("}")
  387. case Type_Info_Enum:
  388. print_string("enum ")
  389. print_type(info.base)
  390. print_string(" {")
  391. for name, i in info.names {
  392. if i > 0 { print_string(", ") }
  393. print_string(name)
  394. }
  395. print_string("}")
  396. case Type_Info_Bit_Set:
  397. print_string("bit_set[")
  398. #partial switch elem in type_info_base(info.elem).variant {
  399. case Type_Info_Enum:
  400. print_type(info.elem)
  401. case Type_Info_Rune:
  402. print_encoded_rune(rune(info.lower))
  403. print_string("..")
  404. print_encoded_rune(rune(info.upper))
  405. case:
  406. print_i64(info.lower)
  407. print_string("..")
  408. print_i64(info.upper)
  409. }
  410. if info.underlying != nil {
  411. print_string("; ")
  412. print_type(info.underlying)
  413. }
  414. print_byte(']')
  415. case Type_Info_Bit_Field:
  416. print_string("bit_field ")
  417. print_type(info.backing_type)
  418. print_string(" {")
  419. for name, i in info.names {
  420. if i > 0 { print_string(", ") }
  421. print_string(name)
  422. print_string(": ")
  423. print_type(info.types[i])
  424. print_string(" | ")
  425. print_u64(u64(info.bit_sizes[i]))
  426. }
  427. print_byte('}')
  428. case Type_Info_Simd_Vector:
  429. print_string("#simd[")
  430. print_u64(u64(info.count))
  431. print_byte(']')
  432. print_type(info.elem)
  433. case Type_Info_Relative_Pointer:
  434. print_string("#relative(")
  435. print_type(info.base_integer)
  436. print_string(") ")
  437. print_type(info.pointer)
  438. case Type_Info_Relative_Multi_Pointer:
  439. print_string("#relative(")
  440. print_type(info.base_integer)
  441. print_string(") ")
  442. print_type(info.pointer)
  443. case Type_Info_Matrix:
  444. print_string("matrix[")
  445. print_u64(u64(info.row_count))
  446. print_string(", ")
  447. print_u64(u64(info.column_count))
  448. print_string("]")
  449. print_type(info.elem)
  450. }
  451. }