2 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
4 # This file is part of the PyMite VM.
5 # The PyMite VM is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
8 # The PyMite VM is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
12 # is seen in the file COPYING in this directory.
28 /** Object descriptor field constants */
29 #define OD_MARK_SHIFT 14
30 #define OD_FREE_SHIFT 15
31 #define OD_MARK_BIT (uint16_t)(1 << OD_MARK_SHIFT)
32 #define OD_FREE_BIT (uint16_t)(1 << OD_FREE_SHIFT)
33 #define OD_SIZE_MASK (uint16_t)(0x01FF)
34 #define OD_TYPE_MASK (uint16_t)(0x3E00)
35 #define OD_TYPE_SHIFT 9
37 /** Heap descriptor size mask */
38 #define HD_SIZE_MASK (uint16_t)(0x3FFF)
42 * Gets the free bit of the given object to the given value.
43 * If the object is marked free, it is not being used by the VM.
45 #define OBJ_GET_FREE(pobj) \
46 ((((pPmObj_t)pobj)->od >> OD_FREE_SHIFT) & (uint8_t)1)
49 * Sets the free bit of the given object to the given value.
50 * Setting the free bit means that the object will use the heap descriptor
51 * structure instead of the object descriptor structure.
53 #define OBJ_SET_FREE(pobj, free) \
56 ((pPmObj_t)pobj)->od = ((uint8_t)free) \
57 ? ((pPmObj_t)pobj)->od | OD_FREE_BIT \
58 : ((pPmObj_t)pobj)->od & ~OD_FREE_BIT;\
63 * #99: od_size bits are shifted because size is a scaled value
64 * True size is always a multiple of 4, so the lower two bits are ignored
65 * and two more significant bits are gained.
68 * Gets the size of the chunk in bytes.
69 * Tests whether the object is free as that determines whether the chunk is
70 * using an object descriptor or a heap descriptor. Heap descriptors have
71 * a larger size field and use a different bit mask than object descriptors.
73 #define OBJ_GET_SIZE(pobj) \
74 ((((pPmObj_t)pobj)->od & OD_FREE_BIT) \
75 ? ((((pPmObj_t)pobj)->od & HD_SIZE_MASK) << 2) \
76 : ((((pPmObj_t)pobj)->od & OD_SIZE_MASK) << 2))
79 * Sets the size of the chunk in bytes.
80 * Tests whether the object is free as that determines whether the chunk is
81 * using an object descriptor or a heap descriptor. Heap descriptors have
82 * a larger size field and use a different bit mask than object descriptors.
84 #define OBJ_SET_SIZE(pobj, size) \
87 if (((pPmObj_t)pobj)->od & OD_FREE_BIT) \
89 ((pPmObj_t)pobj)->od &= ~HD_SIZE_MASK; \
90 ((pPmObj_t)pobj)->od |= (((size) >> 2) & HD_SIZE_MASK); \
94 ((pPmObj_t)pobj)->od &= ~OD_SIZE_MASK; \
95 ((pPmObj_t)pobj)->od |= (((size) >> 2) & OD_SIZE_MASK); \
101 * Gets the type of the object
102 * This MUST NOT be called on objects that are free.
104 #define OBJ_GET_TYPE(pobj) \
105 (((((pPmObj_t)pobj)->od) & OD_TYPE_MASK) >> OD_TYPE_SHIFT)
108 * Sets the type of the object
109 * This MUST NOT be called on objects that are free.
111 #define OBJ_SET_TYPE(pobj, type) \
114 ((pPmObj_t)pobj)->od &= ~OD_TYPE_MASK; \
115 ((pPmObj_t)pobj)->od |= (((type) << OD_TYPE_SHIFT) & OD_TYPE_MASK); \
123 * These values go in the od_type fields of the obj descriptor.
124 * Be sure these values correspond to those in the image creator
126 * The hashable types are grouped together for convenience.
128 * WARNING: od_type must be at most 5 bits! (must be < 0x20)
130 typedef enum PmType_e
132 OBJ_TYPE_HASHABLE_MIN
= 0x00,
137 /** Signed integer */
140 /** Floating point 32b */
146 /** Tuple (immutable sequence) */
158 /** Function obj (callable) */
161 /** Class instance */
164 /** Code image in static memory */
167 /** Native function image */
170 /** Native function object */
176 /** Boolean object */
177 OBJ_TYPE_BOOL
= 0x0F,
179 /** Code image object */
185 /* All types after this are not hashable */
186 OBJ_TYPE_HASHABLE_MAX
= 0x11,
188 /** List (mutable sequence) */
191 /** Dictionary (hash table) */
194 #ifdef HAVE_BYTEARRAY
195 /** Bytearray (mutable) */
197 #endif /* HAVE_BYTEARRAY */
199 /* All types after this are not accessible to the user */
200 OBJ_TYPE_ACCESSIBLE_MAX
= 0x18,
202 #ifdef HAVE_BYTEARRAY
203 /** Bytes (mutable container for Bytearray type) */
205 #endif /* HAVE_BYTEARRAY */
210 /** Block type (for,while,try,etc) */
213 /** Segment (within a seglist) */
219 /** Sequence iterator */
222 /** Native frame (there is only one) */
224 } PmType_t
, *pPmType_t
;
230 * All active PyMite "objects" must have this at the top of their struct.
231 * (CodeObj, Frame, Dict, List, Tuple, etc.).
233 * The following is a diagram of the object descriptor:
237 * pchunk-> +-+-+-+-+-+-+-+-+ S := Size of the chunk (2 LSbs dropped)
238 * | S[9:2] | F := Free bit
239 * +-+-+---------+-+ M := GC Mark bit
240 * |F|M| T |S| T := Object type (PyMite specific)
244 * | end data | Theoretical min size == 2
245 * +---------------+ Effective min size == 8
246 * (due to pmHeapDesc_t)
248 * Macros are used to get and set field values.
249 * Using macros eliminates declaring bit fields which fails on some compilers.
251 typedef uint16_t PmObjDesc_t
,
257 * The abstract empty object type for PyMite.
259 typedef struct PmObj_s
261 /** Object descriptor */
266 /** Boolean object */
267 typedef struct PmBoolean_s
269 /** Object descriptor */
275 PmBoolean_t
, *pPmBoolean_t
;
279 * Loads an object from an image in memory.
280 * Return pointer to object.
281 * Leave add pointing one byte past end of obj.
283 * The following lists the simple object types
284 * and their image structures:
286 * -type: int8_t - OBJ_TYPE_NON
289 * -type: int8_t - OBJ_TYPE_INT
290 * -value: int32_t - signed integer value
293 * -type: int8_t - OBJ_TYPE_FLOAT
294 * -value: float32_t - 32-bit floating point value
296 * -Slice (is this allowed in img?):
297 * -type: int8_t - OBJ_TYPE_SLICE
298 * -index1: int16_t - first index.
299 * -index2: int16_t - second index.
301 * @param memspace memory space/type
302 * @param paddr ptr to ptr to obj
303 * return by reference: paddr pts to
304 * first byte after obj
305 * @param r_pobj Return arg, the loaded object.
306 * @return Return status
308 PmReturn_t
obj_loadFromImg(PmMemSpace_t memspace
,
309 uint8_t const **paddr
, pPmObj_t
*r_pobj
);
312 * Loads a code object from a code image object
314 * @param pimg Ptr to a code image object
315 * @param r_pobj Return arg, the loaded object
316 * @return Returns status
318 PmReturn_t
obj_loadFromImgObj(pPmObj_t pimg
, pPmObj_t
*r_pobj
);
321 * Finds the boolean value of the given object.
323 * @param pobj Ptr to object to test.
324 * @return Nonzero value if object is False.
326 int8_t obj_isFalse(pPmObj_t pobj
);
329 * Returns the boolean true if the item is in the object
331 * @param pobj Ptr to container object
332 * @param pitem Ptr to item
334 PmReturn_t
obj_isIn(pPmObj_t pobj
, pPmObj_t pitem
);
337 * Compares two objects for equality.
339 * @param pobj1 Ptr to first object.
340 * @param pobj2 Ptr to second object.
341 * @return C_SAME if the items are equivalent, C_DIFFER otherwise.
343 int8_t obj_compare(pPmObj_t pobj1
, pPmObj_t pobj2
);
346 * Print an object, thereby using objects helpers.
348 * @param pobj Ptr to object for printing.
349 * @param is_expr_repr Influences the way None and strings are printed.
350 * If 0, None is printed, strings are printed.
351 * If 1, None is not printed and strings are printed
352 * surrounded with single quotes and unprintable
353 * characters are escaped.
354 * @param is_nested Influences the way None and strings are printed.
355 * If 1, None will be printed and strings will be
356 * surrounded with single quotes and escaped.
357 * This argument overrides the is_expr_repr argument.
358 * @return Return status
360 PmReturn_t
obj_print(pPmObj_t pobj
, uint8_t is_expr_repr
, uint8_t is_nested
);
364 * Returns by reference a string object that is the human-readable
365 * representation of the object. Used by the backtick operation (UNARY_CONVERT).
367 * @param pobj Ptr to object to represent
368 * @param r_pstr Return arg, the string object
369 * @return Return status
371 PmReturn_t
obj_repr(pPmObj_t pobj
, pPmObj_t
*r_pstr
);
372 #endif /* HAVE_BACKTICK */
374 #endif /* __OBJ_H__ */