MICROPY_PERSISTENT_CODE discussion

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

MICROPY_PERSISTENT_CODE discussion

Post by jickster » Wed Oct 25, 2017 7:42 pm

mpconfig.h

Code: Select all

// Whether generated code can persist independently of the VM/runtime instance
// This is enabled automatically when needed by other features
#ifndef MICROPY_PERSISTENT_CODE
#define MICROPY_PERSISTENT_CODE (MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE || MICROPY_MODULE_FROZEN_MPY)
#endif
Please explain Whether generated code can persist independently of the VM/runtime instance

Doesn't all code persist independently? I don't get it.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MICROPY_PERSISTENT_CODE discussion

Post by pythoncoder » Thu Oct 26, 2017 7:09 am

I suggest you read http://docs.micropython.org/en/latest/p ... ained.html in particular the references to frozen bytecode.
Peter Hinch
Index to my micropython libraries.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: MICROPY_PERSISTENT_CODE discussion

Post by pfalcon » Sat Oct 28, 2017 6:06 pm

Doesn't all code persist independently? I don't get it.
What's exactly causing confusion? When someone tells you "yellow sun", they don't imply that there's also a green sun.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: MICROPY_PERSISTENT_CODE discussion

Post by jickster » Sat Oct 28, 2017 6:26 pm

pfalcon wrote:
Doesn't all code persist independently? I don't get it.
What's exactly causing confusion? When someone tells you "yellow sun", they don't imply that there's also a green sun.
I read the link. Nothing says anything about code persisting or not.


Sent from my iPhone using Tapatalk

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: MICROPY_PERSISTENT_CODE discussion

Post by jickster » Sun Nov 05, 2017 9:36 pm

pfalcon wrote:
Sat Oct 28, 2017 6:06 pm
Doesn't all code persist independently? I don't get it.
What's exactly causing confusion? When someone tells you "yellow sun", they don't imply that there's also a green sun.
I don't know what "persist" means in this case.

The commit says
py: Add MICROPY_PERSISTENT_CODE so code can persist beyond the runtime.
Main changes when MICROPY_PERSISTENT_CODE is enabled are:

- qstrs are encoded as 2-byte fixed width in the bytecode
- all pointers are removed from bytecode and put in const_table (this
includes const objects and raw code pointers)

Ultimately this option will enable persistence for not just bytecode but
also native code.
My question is: what does it mean for code to NOT persist?
If it's located on flash because it's frozen . . . isn't it by default persisted?

Obviously a slightly different definition of "persist" is being used that I'm missing.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: MICROPY_PERSISTENT_CODE discussion

Post by jickster » Sun Nov 05, 2017 10:32 pm

pfalcon wrote:
Sat Oct 28, 2017 6:06 pm
Doesn't all code persist independently? I don't get it.
What's exactly causing confusion? When someone tells you "yellow sun", they don't imply that there's also a green sun.
I tried to mpy-cross the following code

Code: Select all

import pyb

# print("Executing main.py")

def norm_add(a,b):
	return (a+b)

@micropython.native
def native_add(a,b):
	return (a+b)

ledObj = pyb.LED(1)
ledObj.toggle()

c = native_add(32,4532)

d = norm_add(c,123)
but I got the error
ValueError: can only save bytecode
Why does this happen?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MICROPY_PERSISTENT_CODE discussion

Post by Roberthh » Mon Nov 06, 2017 6:38 am

Functions decorated as @micropython.native, @micropython.viper or @micropython.asm_thumb are compiled into native machine code. Frozen bytecode must only contain ... bytecode, not machine code.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: MICROPY_PERSISTENT_CODE discussion

Post by jickster » Mon Nov 06, 2017 3:53 pm

Roberthh wrote:
Mon Nov 06, 2017 6:38 am
Functions decorated as @micropython.native, @micropython.viper or @micropython.asm_thumb are compiled into native machine code. Frozen bytecode must only contain ... bytecode, not machine code.
Cross-referencing my other post, viewtopic.php?f=3&t=4014&p=22999#p22999 I finally understand that @micropython.native code doesn't persist "outside of VM" . . . FINALLY

Post Reply