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.
17 #define __FILE_ID__ 0x08
22 * \brief Integer Object Type
24 * Integer object type operations.
34 int_dup(pPmObj_t pint
, pPmObj_t
*r_pint
)
36 PmReturn_t retval
= PM_RET_OK
;
38 /* Allocate new int */
39 retval
= heap_getChunk(sizeof(PmInt_t
), (uint8_t **)r_pint
);
40 PM_RETURN_IF_ERROR(retval
);
43 OBJ_SET_TYPE(*r_pint
, OBJ_TYPE_INT
);
44 ((pPmInt_t
)*r_pint
)->val
= ((pPmInt_t
)pint
)->val
;
50 int_new(int32_t n
, pPmObj_t
*r_pint
)
52 PmReturn_t retval
= PM_RET_OK
;
54 /* If n is 0,1,-1, return static int objects from global struct */
71 /* Else create and return new int obj */
72 retval
= heap_getChunk(sizeof(PmInt_t
), (uint8_t **)r_pint
);
73 PM_RETURN_IF_ERROR(retval
);
74 OBJ_SET_TYPE(*r_pint
, OBJ_TYPE_INT
);
75 ((pPmInt_t
)*r_pint
)->val
= n
;
81 int_positive(pPmObj_t pobj
, pPmObj_t
*r_pint
)
85 /* Raise TypeError if obj is not an int */
86 if (OBJ_GET_TYPE(pobj
) != OBJ_TYPE_INT
)
88 PM_RAISE(retval
, PM_RET_EX_TYPE
);
92 /* Create new int obj */
93 return int_new(((pPmInt_t
)pobj
)->val
, r_pint
);
98 int_negative(pPmObj_t pobj
, pPmObj_t
*r_pint
)
102 /* Raise TypeError if obj is not an int */
103 if (OBJ_GET_TYPE(pobj
) != OBJ_TYPE_INT
)
105 PM_RAISE(retval
, PM_RET_EX_TYPE
);
109 /* Create new int obj */
110 return int_new(-((pPmInt_t
)pobj
)->val
, r_pint
);
115 int_bitInvert(pPmObj_t pobj
, pPmObj_t
*r_pint
)
119 /* Raise TypeError if obj is not an int */
120 if (OBJ_GET_TYPE(pobj
) != OBJ_TYPE_INT
)
122 PM_RAISE(retval
, PM_RET_EX_TYPE
);
126 /* Create new int obj */
127 return int_new(~((pPmInt_t
)pobj
)->val
, r_pint
);
133 int_print(pPmObj_t pint
)
135 /* 2^31-1 has 10 decimal digits, plus sign and zero byte */
136 uint8_t tBuffer
[10 + 1 + 1];
137 uint8_t bytesWritten
;
139 PmReturn_t retval
= PM_RET_OK
;
141 C_ASSERT(pint
!= C_NULL
);
143 /* Raise TypeError if obj is not an int */
144 if (OBJ_GET_TYPE(pint
) != OBJ_TYPE_INT
)
146 PM_RAISE(retval
, PM_RET_EX_TYPE
);
150 /* #196: Changed to use snprintf */
152 snprintf((char *)&tBuffer
, 12, "%li", (long int)((pPmInt_t
)pint
)->val
);
156 C_ASSERT(bytesWritten
!= 0);
157 C_ASSERT(bytesWritten
< sizeof(tBuffer
));
159 for (k
= (uint8_t)0; k
< bytesWritten
; k
++)
161 retval
= plat_putByte(tBuffer
[k
]);
162 PM_RETURN_IF_ERROR(retval
);
169 int_printHexByte(uint8_t b
)
174 nibble
= (b
>> 4) + '0';
176 nibble
+= ('a' - '0' - 10);
177 retval
= plat_putByte(nibble
);
178 PM_RETURN_IF_ERROR(retval
);
180 nibble
= (b
& (uint8_t)0x0F) + '0';
182 nibble
+= ('a' - '0' - (uint8_t)10);
183 retval
= plat_putByte(nibble
);
189 _int_printHex(intptr_t n
)
194 /* Print the hex value, most significant byte first */
195 for (i
= CHAR_BIT
* sizeof(intptr_t) - 8; i
>= 0; i
-= 8)
197 retval
= int_printHexByte((n
>> i
) & 0xFF);
198 PM_BREAK_IF_ERROR(retval
);
206 int_printHex(pPmObj_t pint
)
208 C_ASSERT(OBJ_GET_TYPE(pint
) == OBJ_TYPE_INT
);
210 /* Print the integer object */
211 return _int_printHex(((pPmInt_t
)pint
)->val
);
213 #endif /* HAVE_PRINT */
217 int_pow(pPmObj_t px
, pPmObj_t py
, pPmObj_t
*r_pn
)
224 /* Raise TypeError if args aren't ints */
225 if ((OBJ_GET_TYPE(px
) != OBJ_TYPE_INT
)
226 || (OBJ_GET_TYPE(py
) != OBJ_TYPE_INT
))
228 PM_RAISE(retval
, PM_RET_EX_TYPE
);
232 x
= ((pPmInt_t
)px
)->val
;
233 y
= ((pPmInt_t
)py
)->val
;
235 /* Raise Value error if exponent is negative */
238 PM_RAISE(retval
, PM_RET_EX_VAL
);
242 /* Calculate x raised to y */
249 retval
= int_new(n
, r_pn
);