obj.h 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_OBJ_H
  27. #define MICROPY_INCLUDED_PY_OBJ_H
  28. #include <assert.h>
  29. #include "py/mpconfig.h"
  30. #include "py/misc.h"
  31. #include "py/qstr.h"
  32. #include "py/mpprint.h"
  33. #include "py/runtime0.h"
  34. // This is the definition of the opaque MicroPython object type.
  35. // All concrete objects have an encoding within this type and the
  36. // particular encoding is specified by MICROPY_OBJ_REPR.
  37. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  38. typedef uint64_t mp_obj_t;
  39. typedef uint64_t mp_const_obj_t;
  40. #else
  41. typedef void *mp_obj_t;
  42. typedef const void *mp_const_obj_t;
  43. #endif
  44. // This mp_obj_type_t struct is a concrete MicroPython object which holds info
  45. // about a type. See below for actual definition of the struct.
  46. typedef struct _mp_obj_type_t mp_obj_type_t;
  47. // Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
  48. // as its first member (small ints, qstr objs and inline floats are not concrete).
  49. struct _mp_obj_base_t {
  50. const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
  51. };
  52. typedef struct _mp_obj_base_t mp_obj_base_t;
  53. // These fake objects are used to indicate certain things in arguments or return
  54. // values, and should only be used when explicitly allowed.
  55. //
  56. // - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
  57. // - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
  58. // - MP_OBJ_SENTINEL : used for various internal purposes where one needs
  59. // an object which is unique from all other objects, including MP_OBJ_NULL.
  60. //
  61. // For debugging purposes they are all different. For non-debug mode, we alias
  62. // as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
  63. #if MICROPY_DEBUG_MP_OBJ_SENTINELS
  64. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void *)0))
  65. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void *)4))
  66. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void *)8))
  67. #else
  68. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void *)0))
  69. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void *)0))
  70. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void *)4))
  71. #endif
  72. // These macros/inline functions operate on objects and depend on the
  73. // particular object representation. They are used to query, pack and
  74. // unpack small ints, qstrs and full object pointers.
  75. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
  76. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  77. return (((mp_int_t)(o)) & 1) != 0;
  78. }
  79. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  80. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  81. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  82. return (((mp_int_t)(o)) & 7) == 2;
  83. }
  84. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
  85. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 2))
  86. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  87. return (((mp_int_t)(o)) & 7) == 6;
  88. }
  89. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
  90. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 6))
  91. #if MICROPY_PY_BUILTINS_FLOAT
  92. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  93. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  94. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  95. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  96. #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
  97. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  98. mp_obj_t mp_obj_new_float(mp_float_t value);
  99. #endif
  100. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  101. return (((mp_int_t)(o)) & 3) == 0;
  102. }
  103. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
  104. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  105. return (((mp_int_t)(o)) & 3) == 1;
  106. }
  107. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
  108. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
  109. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  110. return (((mp_int_t)(o)) & 7) == 3;
  111. }
  112. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
  113. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 3))
  114. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  115. return (((mp_int_t)(o)) & 7) == 7;
  116. }
  117. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
  118. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 7))
  119. #if MICROPY_PY_BUILTINS_FLOAT
  120. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  121. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  122. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  123. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  124. #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
  125. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  126. mp_obj_t mp_obj_new_float(mp_float_t value);
  127. #endif
  128. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  129. return (((mp_int_t)(o)) & 1) == 0;
  130. }
  131. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
  132. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  133. return (((mp_int_t)(o)) & 1) != 0;
  134. }
  135. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  136. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  137. #if MICROPY_PY_BUILTINS_FLOAT
  138. #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
  139. #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
  140. static inline bool mp_obj_is_float(mp_const_obj_t o) {
  141. return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006;
  142. }
  143. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  144. union {
  145. mp_float_t f;
  146. mp_uint_t u;
  147. } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
  148. return num.f;
  149. }
  150. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  151. union {
  152. mp_float_t f;
  153. mp_uint_t u;
  154. } num = {.f = f};
  155. return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
  156. }
  157. #endif
  158. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  159. return (((mp_uint_t)(o)) & 0xff80000f) == 0x00000006;
  160. }
  161. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 4)
  162. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 4) | 0x00000006))
  163. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  164. return (((mp_uint_t)(o)) & 0xff80000f) == 0x0000000e;
  165. }
  166. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 4)
  167. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 4) | 0xe))
  168. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  169. return (((mp_int_t)(o)) & 3) == 0;
  170. }
  171. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  172. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  173. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000;
  174. }
  175. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
  176. #define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
  177. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  178. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000;
  179. }
  180. #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
  181. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)(((uint64_t)(((uint32_t)(qst)) << 1)) | 0x0002000000000001))
  182. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  183. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0003000000000000;
  184. }
  185. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) ((((uint32_t)(o)) >> 46) & 3)
  186. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) (((uint64_t)(val) << 46) | 0x0003000000000000)
  187. #if MICROPY_PY_BUILTINS_FLOAT
  188. #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
  189. #error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
  190. #endif
  191. #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
  192. #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
  193. static inline bool mp_obj_is_float(mp_const_obj_t o) {
  194. return ((uint64_t)(o) & 0xfffc000000000000) != 0;
  195. }
  196. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  197. union {
  198. mp_float_t f;
  199. uint64_t r;
  200. } num = {.r = o - 0x8004000000000000};
  201. return num.f;
  202. }
  203. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  204. union {
  205. mp_float_t f;
  206. uint64_t r;
  207. } num = {.f = f};
  208. return num.r + 0x8004000000000000;
  209. }
  210. #endif
  211. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  212. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000;
  213. }
  214. #define MP_OBJ_TO_PTR(o) ((void *)(uintptr_t)(o))
  215. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
  216. // rom object storage needs special handling to widen 32-bit pointer to 64-bits
  217. typedef union _mp_rom_obj_t { uint64_t u64;
  218. struct { const void *lo, *hi;
  219. } u32;
  220. } mp_rom_obj_t;
  221. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  222. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  223. #if MP_ENDIANNESS_LITTLE
  224. #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
  225. #else
  226. #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
  227. #endif
  228. #endif
  229. // Macros to convert between mp_obj_t and concrete object types.
  230. // These are identity operations in MicroPython, but ability to override
  231. // these operations are provided to experiment with other methods of
  232. // object representation and memory management.
  233. // Cast mp_obj_t to object pointer
  234. #ifndef MP_OBJ_TO_PTR
  235. #define MP_OBJ_TO_PTR(o) ((void *)o)
  236. #endif
  237. // Cast object pointer to mp_obj_t
  238. #ifndef MP_OBJ_FROM_PTR
  239. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)p)
  240. #endif
  241. // Macros to create objects that are stored in ROM.
  242. #ifndef MP_ROM_NONE
  243. #if MICROPY_OBJ_IMMEDIATE_OBJS
  244. #define MP_ROM_NONE mp_const_none
  245. #else
  246. #define MP_ROM_NONE MP_ROM_PTR(&mp_const_none_obj)
  247. #endif
  248. #endif
  249. #ifndef MP_ROM_FALSE
  250. #if MICROPY_OBJ_IMMEDIATE_OBJS
  251. #define MP_ROM_FALSE mp_const_false
  252. #define MP_ROM_TRUE mp_const_true
  253. #else
  254. #define MP_ROM_FALSE MP_ROM_PTR(&mp_const_false_obj)
  255. #define MP_ROM_TRUE MP_ROM_PTR(&mp_const_true_obj)
  256. #endif
  257. #endif
  258. #ifndef MP_ROM_INT
  259. typedef mp_const_obj_t mp_rom_obj_t;
  260. #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
  261. #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
  262. #define MP_ROM_PTR(p) (p)
  263. /* for testing
  264. typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
  265. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  266. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  267. #define MP_ROM_PTR(p) {.o = p}
  268. */
  269. #endif
  270. // These macros are used to declare and define constant function objects
  271. // You can put "static" in front of the definitions to make them local
  272. #define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  273. #define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  274. #define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  275. #define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  276. #define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  277. #define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  278. #define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  279. #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
  280. #define MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw) ((uint32_t)((((uint32_t)(n_args_min)) << 17) | (((uint32_t)(n_args_max)) << 1) | ((takes_kw) ? 1 : 0)))
  281. #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
  282. const mp_obj_fun_builtin_fixed_t obj_name = \
  283. {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
  284. #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
  285. const mp_obj_fun_builtin_fixed_t obj_name = \
  286. {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
  287. #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
  288. const mp_obj_fun_builtin_fixed_t obj_name = \
  289. {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
  290. #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
  291. const mp_obj_fun_builtin_fixed_t obj_name = \
  292. {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
  293. #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
  294. const mp_obj_fun_builtin_var_t obj_name = \
  295. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
  296. #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
  297. const mp_obj_fun_builtin_var_t obj_name = \
  298. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
  299. #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
  300. const mp_obj_fun_builtin_var_t obj_name = \
  301. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
  302. // These macros are used to define constant map/dict objects
  303. // You can put "static" in front of the definition to make it local
  304. #define MP_DEFINE_CONST_MAP(map_name, table_name) \
  305. const mp_map_t map_name = { \
  306. .all_keys_are_qstrs = 1, \
  307. .is_fixed = 1, \
  308. .is_ordered = 1, \
  309. .used = MP_ARRAY_SIZE(table_name), \
  310. .alloc = MP_ARRAY_SIZE(table_name), \
  311. .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
  312. }
  313. #define MP_DEFINE_CONST_DICT(dict_name, table_name) \
  314. const mp_obj_dict_t dict_name = { \
  315. .base = {&mp_type_dict}, \
  316. .map = { \
  317. .all_keys_are_qstrs = 1, \
  318. .is_fixed = 1, \
  319. .is_ordered = 1, \
  320. .used = MP_ARRAY_SIZE(table_name), \
  321. .alloc = MP_ARRAY_SIZE(table_name), \
  322. .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
  323. }, \
  324. }
  325. // These macros are used to declare and define constant staticmethond and classmethod objects
  326. // You can put "static" in front of the definitions to make them local
  327. #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  328. #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  329. #define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
  330. #define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
  331. // Declare a module as a builtin, processed by makemoduledefs.py
  332. // param module_name: MP_QSTR_<module name>
  333. // param obj_module: mp_obj_module_t instance
  334. // prarm enabled_define: used as `#if (enabled_define) around entry`
  335. #define MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
  336. // Underlying map/hash table implementation (not dict object or map function)
  337. typedef struct _mp_map_elem_t {
  338. mp_obj_t key;
  339. mp_obj_t value;
  340. } mp_map_elem_t;
  341. typedef struct _mp_rom_map_elem_t {
  342. mp_rom_obj_t key;
  343. mp_rom_obj_t value;
  344. } mp_rom_map_elem_t;
  345. typedef struct _mp_map_t {
  346. size_t all_keys_are_qstrs : 1;
  347. size_t is_fixed : 1; // if set, table is fixed/read-only and can't be modified
  348. size_t is_ordered : 1; // if set, table is an ordered array, not a hash map
  349. size_t used : (8 * sizeof(size_t) - 3);
  350. size_t alloc;
  351. mp_map_elem_t *table;
  352. } mp_map_t;
  353. // mp_set_lookup requires these constants to have the values they do
  354. typedef enum _mp_map_lookup_kind_t {
  355. MP_MAP_LOOKUP = 0,
  356. MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
  357. MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
  358. MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
  359. } mp_map_lookup_kind_t;
  360. static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) {
  361. assert(pos < map->alloc);
  362. return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL;
  363. }
  364. void mp_map_init(mp_map_t *map, size_t n);
  365. void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
  366. mp_map_t *mp_map_new(size_t n);
  367. void mp_map_deinit(mp_map_t *map);
  368. void mp_map_free(mp_map_t *map);
  369. mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  370. void mp_map_clear(mp_map_t *map);
  371. void mp_map_dump(mp_map_t *map);
  372. // Underlying set implementation (not set object)
  373. typedef struct _mp_set_t {
  374. size_t alloc;
  375. size_t used;
  376. mp_obj_t *table;
  377. } mp_set_t;
  378. static inline bool mp_set_slot_is_filled(const mp_set_t *set, size_t pos) {
  379. return (set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL;
  380. }
  381. void mp_set_init(mp_set_t *set, size_t n);
  382. mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  383. mp_obj_t mp_set_remove_first(mp_set_t *set);
  384. void mp_set_clear(mp_set_t *set);
  385. // Type definitions for methods
  386. typedef mp_obj_t (*mp_fun_0_t)(void);
  387. typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
  388. typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
  389. typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
  390. typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
  391. // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
  392. // this arg to mp_map_lookup().
  393. typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
  394. // Flags for type behaviour (mp_obj_type_t.flags)
  395. // If MP_TYPE_FLAG_EQ_NOT_REFLEXIVE is clear then __eq__ is reflexive (A==A returns True).
  396. // If MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE is clear then the type can't be equal to an
  397. // instance of any different class that also clears this flag. If this flag is set
  398. // then the type may check for equality against a different type.
  399. // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__
  400. // operator and not the __ne__ operator. If it's set then __ne__ may be implemented.
  401. // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg.
  402. // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type.
  403. #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
  404. #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
  405. #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004)
  406. #define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008)
  407. #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010)
  408. #define MP_TYPE_FLAG_BINDS_SELF (0x0020)
  409. #define MP_TYPE_FLAG_BUILTIN_FUN (0x0040)
  410. typedef enum {
  411. PRINT_STR = 0,
  412. PRINT_REPR = 1,
  413. PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
  414. PRINT_JSON = 3,
  415. PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
  416. PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
  417. } mp_print_kind_t;
  418. typedef struct _mp_obj_iter_buf_t {
  419. mp_obj_base_t base;
  420. mp_obj_t buf[3];
  421. } mp_obj_iter_buf_t;
  422. // The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
  423. // It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
  424. #define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
  425. typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
  426. typedef mp_obj_t (*mp_make_new_fun_t)(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
  427. typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
  428. typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
  429. typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
  430. typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
  431. typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  432. typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
  433. // Buffer protocol
  434. typedef struct _mp_buffer_info_t {
  435. void *buf; // can be NULL if len == 0
  436. size_t len; // in bytes
  437. int typecode; // as per binary.h
  438. } mp_buffer_info_t;
  439. #define MP_BUFFER_READ (1)
  440. #define MP_BUFFER_WRITE (2)
  441. #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
  442. typedef struct _mp_buffer_p_t {
  443. mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  444. } mp_buffer_p_t;
  445. bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  446. void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  447. struct _mp_obj_type_t {
  448. // A type is an object so must start with this entry, which points to mp_type_type.
  449. mp_obj_base_t base;
  450. // Flags associated with this type.
  451. uint16_t flags;
  452. // The name of this type, a qstr.
  453. uint16_t name;
  454. // Corresponds to __repr__ and __str__ special methods.
  455. mp_print_fun_t print;
  456. // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
  457. mp_make_new_fun_t make_new;
  458. // Corresponds to __call__ special method, ie T(...).
  459. mp_call_fun_t call;
  460. // Implements unary and binary operations.
  461. // Can return MP_OBJ_NULL if the operation is not supported.
  462. mp_unary_op_fun_t unary_op;
  463. mp_binary_op_fun_t binary_op;
  464. // Implements load, store and delete attribute.
  465. //
  466. // dest[0] = MP_OBJ_NULL means load
  467. // return: for fail, do nothing
  468. // for attr, dest[0] = value
  469. // for method, dest[0] = method, dest[1] = self
  470. //
  471. // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
  472. // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
  473. // return: for fail, do nothing
  474. // for success set dest[0] = MP_OBJ_NULL
  475. mp_attr_fun_t attr;
  476. // Implements load, store and delete subscripting:
  477. // - value = MP_OBJ_SENTINEL means load
  478. // - value = MP_OBJ_NULL means delete
  479. // - all other values mean store the value
  480. // Can return MP_OBJ_NULL if operation not supported.
  481. mp_subscr_fun_t subscr;
  482. // Corresponds to __iter__ special method.
  483. // Can use the given mp_obj_iter_buf_t to store iterator object,
  484. // otherwise can return a pointer to an object on the heap.
  485. mp_getiter_fun_t getiter;
  486. // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
  487. // as an optimisation instead of raising StopIteration() with no args.
  488. mp_fun_1_t iternext;
  489. // Implements the buffer protocol if supported by this type.
  490. mp_buffer_p_t buffer_p;
  491. // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
  492. const void *protocol;
  493. // A pointer to the parents of this type:
  494. // - 0 parents: pointer is NULL (object is implicitly the single parent)
  495. // - 1 parent: a pointer to the type of that parent
  496. // - 2 or more parents: pointer to a tuple object containing the parent types
  497. const void *parent;
  498. // A dict mapping qstrs to objects local methods/constants/etc.
  499. struct _mp_obj_dict_t *locals_dict;
  500. };
  501. // Constant types, globally accessible
  502. extern const mp_obj_type_t mp_type_type;
  503. extern const mp_obj_type_t mp_type_object;
  504. extern const mp_obj_type_t mp_type_NoneType;
  505. extern const mp_obj_type_t mp_type_bool;
  506. extern const mp_obj_type_t mp_type_int;
  507. extern const mp_obj_type_t mp_type_str;
  508. extern const mp_obj_type_t mp_type_bytes;
  509. extern const mp_obj_type_t mp_type_bytearray;
  510. extern const mp_obj_type_t mp_type_memoryview;
  511. extern const mp_obj_type_t mp_type_float;
  512. extern const mp_obj_type_t mp_type_complex;
  513. extern const mp_obj_type_t mp_type_tuple;
  514. extern const mp_obj_type_t mp_type_list;
  515. extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
  516. extern const mp_obj_type_t mp_type_enumerate;
  517. extern const mp_obj_type_t mp_type_filter;
  518. extern const mp_obj_type_t mp_type_deque;
  519. extern const mp_obj_type_t mp_type_dict;
  520. extern const mp_obj_type_t mp_type_ordereddict;
  521. extern const mp_obj_type_t mp_type_range;
  522. extern const mp_obj_type_t mp_type_set;
  523. extern const mp_obj_type_t mp_type_frozenset;
  524. extern const mp_obj_type_t mp_type_slice;
  525. extern const mp_obj_type_t mp_type_zip;
  526. extern const mp_obj_type_t mp_type_array;
  527. extern const mp_obj_type_t mp_type_super;
  528. extern const mp_obj_type_t mp_type_gen_wrap;
  529. extern const mp_obj_type_t mp_type_native_gen_wrap;
  530. extern const mp_obj_type_t mp_type_gen_instance;
  531. extern const mp_obj_type_t mp_type_fun_builtin_0;
  532. extern const mp_obj_type_t mp_type_fun_builtin_1;
  533. extern const mp_obj_type_t mp_type_fun_builtin_2;
  534. extern const mp_obj_type_t mp_type_fun_builtin_3;
  535. extern const mp_obj_type_t mp_type_fun_builtin_var;
  536. extern const mp_obj_type_t mp_type_fun_bc;
  537. extern const mp_obj_type_t mp_type_module;
  538. extern const mp_obj_type_t mp_type_staticmethod;
  539. extern const mp_obj_type_t mp_type_classmethod;
  540. extern const mp_obj_type_t mp_type_property;
  541. extern const mp_obj_type_t mp_type_stringio;
  542. extern const mp_obj_type_t mp_type_bytesio;
  543. extern const mp_obj_type_t mp_type_reversed;
  544. extern const mp_obj_type_t mp_type_polymorph_iter;
  545. // Exceptions
  546. extern const mp_obj_type_t mp_type_BaseException;
  547. extern const mp_obj_type_t mp_type_ArithmeticError;
  548. extern const mp_obj_type_t mp_type_AssertionError;
  549. extern const mp_obj_type_t mp_type_AttributeError;
  550. extern const mp_obj_type_t mp_type_EOFError;
  551. extern const mp_obj_type_t mp_type_Exception;
  552. extern const mp_obj_type_t mp_type_GeneratorExit;
  553. extern const mp_obj_type_t mp_type_ImportError;
  554. extern const mp_obj_type_t mp_type_IndentationError;
  555. extern const mp_obj_type_t mp_type_IndexError;
  556. extern const mp_obj_type_t mp_type_KeyboardInterrupt;
  557. extern const mp_obj_type_t mp_type_KeyError;
  558. extern const mp_obj_type_t mp_type_LookupError;
  559. extern const mp_obj_type_t mp_type_MemoryError;
  560. extern const mp_obj_type_t mp_type_NameError;
  561. extern const mp_obj_type_t mp_type_NotImplementedError;
  562. extern const mp_obj_type_t mp_type_OSError;
  563. extern const mp_obj_type_t mp_type_OverflowError;
  564. extern const mp_obj_type_t mp_type_RuntimeError;
  565. extern const mp_obj_type_t mp_type_StopAsyncIteration;
  566. extern const mp_obj_type_t mp_type_StopIteration;
  567. extern const mp_obj_type_t mp_type_SyntaxError;
  568. extern const mp_obj_type_t mp_type_SystemExit;
  569. extern const mp_obj_type_t mp_type_TypeError;
  570. extern const mp_obj_type_t mp_type_UnicodeError;
  571. extern const mp_obj_type_t mp_type_ValueError;
  572. extern const mp_obj_type_t mp_type_ViperTypeError;
  573. extern const mp_obj_type_t mp_type_ZeroDivisionError;
  574. // Constant objects, globally accessible: None, False, True
  575. // These should always be accessed via the below macros.
  576. #if MICROPY_OBJ_IMMEDIATE_OBJS
  577. // None is even while False/True are odd so their types can be distinguished with 1 bit.
  578. #define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
  579. #define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
  580. #define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
  581. #else
  582. #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
  583. #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
  584. #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
  585. extern const struct _mp_obj_none_t mp_const_none_obj;
  586. extern const struct _mp_obj_bool_t mp_const_false_obj;
  587. extern const struct _mp_obj_bool_t mp_const_true_obj;
  588. #endif
  589. // Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit()
  590. // The below macros are for convenience only.
  591. #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
  592. #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
  593. #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
  594. extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
  595. extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
  596. extern const struct _mp_obj_dict_t mp_const_empty_dict_obj;
  597. extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
  598. extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
  599. extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
  600. // Fixed empty map. Useful when calling keyword-receiving functions
  601. // without any keywords from C, etc.
  602. #define mp_const_empty_map (mp_const_empty_dict_obj.map)
  603. // General API for objects
  604. // These macros are derived from more primitive ones and are used to
  605. // check for more specific object types.
  606. // Note: these are kept as macros because inline functions sometimes use much
  607. // more code space than the equivalent macros, depending on the compiler.
  608. #define mp_obj_is_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
  609. #if MICROPY_OBJ_IMMEDIATE_OBJS
  610. // bool's are immediates, not real objects, so test for the 2 possible values.
  611. #define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
  612. #else
  613. #define mp_obj_is_bool(o) mp_obj_is_type(o, &mp_type_bool)
  614. #endif
  615. #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
  616. #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
  617. #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
  618. #define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new)
  619. #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
  620. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
  621. static inline mp_obj_t mp_obj_new_bool(mp_int_t x) {
  622. return x ? mp_const_true : mp_const_false;
  623. }
  624. mp_obj_t mp_obj_new_cell(mp_obj_t obj);
  625. mp_obj_t mp_obj_new_int(mp_int_t value);
  626. mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
  627. mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
  628. mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
  629. mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
  630. mp_obj_t mp_obj_new_str(const char *data, size_t len);
  631. mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len);
  632. mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
  633. mp_obj_t mp_obj_new_bytes(const byte *data, size_t len);
  634. mp_obj_t mp_obj_new_bytearray(size_t n, void *items);
  635. mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
  636. #if MICROPY_PY_BUILTINS_FLOAT
  637. mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
  638. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
  639. #endif
  640. mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
  641. mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
  642. mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
  643. mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
  644. mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
  645. #ifdef va_start
  646. mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, va_list arg); // same fmt restrictions as above
  647. #endif
  648. mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table);
  649. mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table);
  650. mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig);
  651. mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
  652. mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
  653. mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
  654. mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
  655. mp_obj_t mp_obj_new_dict(size_t n_args);
  656. mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
  657. mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
  658. mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
  659. mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
  660. mp_obj_t mp_obj_new_module(qstr module_name);
  661. mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
  662. const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
  663. const char *mp_obj_get_type_str(mp_const_obj_t o_in);
  664. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
  665. mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
  666. void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
  667. void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
  668. void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
  669. bool mp_obj_is_true(mp_obj_t arg);
  670. bool mp_obj_is_callable(mp_obj_t o_in);
  671. mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2);
  672. bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
  673. static inline bool mp_obj_is_integer(mp_const_obj_t o) {
  674. return mp_obj_is_int(o) || mp_obj_is_bool(o);
  675. } // returns true if o is bool, small int or long int
  676. mp_int_t mp_obj_get_int(mp_const_obj_t arg);
  677. mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
  678. bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
  679. #if MICROPY_PY_BUILTINS_FLOAT
  680. mp_float_t mp_obj_get_float(mp_obj_t self_in);
  681. bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
  682. void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  683. bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  684. #endif
  685. void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
  686. void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items); // *items may point inside a GC block
  687. size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
  688. mp_obj_t mp_obj_id(mp_obj_t o_in);
  689. mp_obj_t mp_obj_len(mp_obj_t o_in);
  690. mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
  691. mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
  692. mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
  693. // cell
  694. mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
  695. void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
  696. // int
  697. // For long int, returns value truncated to mp_int_t
  698. mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
  699. // Will raise exception if value doesn't fit into mp_int_t
  700. mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
  701. // Will raise exception if value is negative or doesn't fit into mp_uint_t
  702. mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
  703. // exception
  704. #define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
  705. bool mp_obj_is_exception_type(mp_obj_t self_in);
  706. bool mp_obj_is_exception_instance(mp_obj_t self_in);
  707. bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
  708. void mp_obj_exception_clear_traceback(mp_obj_t self_in);
  709. void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
  710. void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
  711. mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
  712. mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
  713. mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
  714. void mp_init_emergency_exception_buf(void);
  715. // str
  716. bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
  717. qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
  718. const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
  719. const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
  720. mp_obj_t mp_obj_str_intern(mp_obj_t str);
  721. mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
  722. void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
  723. #if MICROPY_PY_BUILTINS_FLOAT
  724. // float
  725. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  726. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  727. return mp_obj_get_float(o);
  728. }
  729. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  730. return (double)mp_obj_get_float(o);
  731. }
  732. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  733. return mp_obj_new_float(o);
  734. }
  735. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  736. return mp_obj_new_float((mp_float_t)o);
  737. }
  738. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  739. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  740. return (float)mp_obj_get_float(o);
  741. }
  742. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  743. return mp_obj_get_float(o);
  744. }
  745. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  746. return mp_obj_new_float((mp_float_t)o);
  747. }
  748. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  749. return mp_obj_new_float(o);
  750. }
  751. #endif
  752. #if MICROPY_FLOAT_HIGH_QUALITY_HASH
  753. mp_int_t mp_float_hash(mp_float_t val);
  754. #else
  755. static inline mp_int_t mp_float_hash(mp_float_t val) {
  756. return (mp_int_t)val;
  757. }
  758. #endif
  759. mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
  760. // complex
  761. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  762. mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
  763. #else
  764. #define mp_obj_is_float(o) (false)
  765. #endif
  766. // tuple
  767. void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  768. void mp_obj_tuple_del(mp_obj_t self_in);
  769. mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
  770. // list
  771. mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
  772. mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
  773. void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  774. void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
  775. void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  776. mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
  777. // dict
  778. typedef struct _mp_obj_dict_t {
  779. mp_obj_base_t base;
  780. mp_map_t map;
  781. } mp_obj_dict_t;
  782. mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
  783. void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
  784. size_t mp_obj_dict_len(mp_obj_t self_in);
  785. mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
  786. mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
  787. mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
  788. mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
  789. static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
  790. return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
  791. }
  792. // set
  793. void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
  794. // slice indexes resolved to particular sequence
  795. typedef struct {
  796. mp_int_t start;
  797. mp_int_t stop;
  798. mp_int_t step;
  799. } mp_bound_slice_t;
  800. // slice
  801. typedef struct _mp_obj_slice_t {
  802. mp_obj_base_t base;
  803. mp_obj_t start;
  804. mp_obj_t stop;
  805. mp_obj_t step;
  806. } mp_obj_slice_t;
  807. void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
  808. // functions
  809. typedef struct _mp_obj_fun_builtin_fixed_t {
  810. mp_obj_base_t base;
  811. union {
  812. mp_fun_0_t _0;
  813. mp_fun_1_t _1;
  814. mp_fun_2_t _2;
  815. mp_fun_3_t _3;
  816. } fun;
  817. } mp_obj_fun_builtin_fixed_t;
  818. typedef struct _mp_obj_fun_builtin_var_t {
  819. mp_obj_base_t base;
  820. uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
  821. union {
  822. mp_fun_var_t var;
  823. mp_fun_kw_t kw;
  824. } fun;
  825. } mp_obj_fun_builtin_var_t;
  826. qstr mp_obj_fun_get_name(mp_const_obj_t fun);
  827. qstr mp_obj_code_get_name(const byte *code_info);
  828. mp_obj_t mp_identity(mp_obj_t self);
  829. MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
  830. mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
  831. // module
  832. typedef struct _mp_obj_module_t {
  833. mp_obj_base_t base;
  834. mp_obj_dict_t *globals;
  835. } mp_obj_module_t;
  836. static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
  837. return ((mp_obj_module_t *)MP_OBJ_TO_PTR(module))->globals;
  838. }
  839. // check if given module object is a package
  840. bool mp_obj_is_package(mp_obj_t module);
  841. // staticmethod and classmethod types; defined here so we can make const versions
  842. // this structure is used for instances of both staticmethod and classmethod
  843. typedef struct _mp_obj_static_class_method_t {
  844. mp_obj_base_t base;
  845. mp_obj_t fun;
  846. } mp_obj_static_class_method_t;
  847. typedef struct _mp_rom_obj_static_class_method_t {
  848. mp_obj_base_t base;
  849. mp_rom_obj_t fun;
  850. } mp_rom_obj_static_class_method_t;
  851. // property
  852. const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
  853. // sequence helpers
  854. void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
  855. #if MICROPY_PY_BUILTINS_SLICE
  856. bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
  857. #endif
  858. #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
  859. #define mp_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
  860. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
  861. bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2);
  862. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
  863. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
  864. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
  865. // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
  866. #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
  867. // Note: dest and slice regions may overlap
  868. #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
  869. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
  870. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
  871. // Note: dest and slice regions may overlap
  872. #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
  873. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
  874. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
  875. // Provide translation for legacy API
  876. #define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
  877. #define MP_OBJ_IS_QSTR mp_obj_is_qstr
  878. #define MP_OBJ_IS_OBJ mp_obj_is_obj
  879. #define MP_OBJ_IS_INT mp_obj_is_int
  880. #define MP_OBJ_IS_TYPE mp_obj_is_type
  881. #define MP_OBJ_IS_STR mp_obj_is_str
  882. #define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
  883. #define MP_OBJ_IS_FUN mp_obj_is_fun
  884. #define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
  885. #define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
  886. #endif // MICROPY_INCLUDED_PY_OBJ_H