Commit e9dac3b4 authored by Damien George's avatar Damien George
Browse files

py: Add casting to viper; add native mem stores to viper.

Viper can now do the following:

def store(p:ptr8, c:int):
    p[0] = c

This does a store of c to the memory pointed to by p using a machine
instructions inline in the code.
parent 44c96b23
This diff is collapsed.
......@@ -51,8 +51,16 @@ mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
switch (type & 3) {
case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
case MP_NATIVE_TYPE_BOOL:
case MP_NATIVE_TYPE_INT:
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int(obj);
case MP_NATIVE_TYPE_INT: return mp_obj_get_int(obj);
case MP_NATIVE_TYPE_UINT: {
mp_buffer_info_t bufinfo;
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
return (mp_uint_t)bufinfo.buf;
} else {
// TODO should be mp_obj_get_uint_truncated or something
return mp_obj_get_int(obj);
}
}
default: assert(0); return 0;
}
}
......
......@@ -77,6 +77,9 @@ Q(const)
Q(native)
Q(viper)
Q(uint)
Q(ptr)
Q(ptr8)
Q(ptr16)
#endif
#if MICROPY_EMIT_INLINE_THUMB
......
# test ptr8 type
@micropython.viper
def set(dest:ptr8, val:int):
dest[0] = val
@micropython.viper
def memset(dest:ptr8, val:int, n:int):
for i in range(n):
dest[i] = val
@micropython.viper
def memset2(dest_in, val:int):
dest = ptr8(dest_in)
n = int(len(dest_in))
for i in range(n):
dest[i] = val
b = bytearray(4)
print(b)
set(b, 42)
print(b)
memset(b, 43, len(b))
print(b)
memset2(b, 44)
print(b)
bytearray(b'\x00\x00\x00\x00')
bytearray(b'*\x00\x00\x00')
bytearray(b'++++')
bytearray(b',,,,')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment