Skip to content

py: Put all global state together in state structures. #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Jan 1, 2015

This is a second attempt at consolidating global state into one place. It supersedes #1017.

Improvements upon previous attempt:

  • guarded includes now fully implemented by previous patches
  • most nlr changes pushed in separate patch already to master (they were nice clean changes)
  • state separated into 3 structure: mem (for common memory stuff, like GC), vm (VM specific), ctx (context specific, globals and locals dict)
  • state accessed by macros so that it can easily be changed from a global structure to one passed by argument
  • bss no longer needed for GC! root pointers are now known because they live in a specific part of the vm state (only implemented properly for unix, stmhal requires some work to separate root pointers from other bss data)

@@ -64,8 +64,6 @@ struct _nlr_buf_t {
#endif
};

extern nlr_buf_t *nlr_top;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this declaration makes the code below fail when MICROPY_NLR_SETJMP == 1. Should probably #include "py/mpstate.h" in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's part of the global structure (btw what does ctx stand for?) you now also need something like

diff --git a/py/nlr.h b/py/nlr.h
index a8a8315..679cab8 100644
--- a/py/nlr.h
+++ b/py/nlr.h
@@ -68,8 +68,8 @@ struct _nlr_buf_t {
 NORETURN void nlr_setjmp_jump(void *val);
 // nlr_push() must be defined as a macro, because "The stack context will be
 // invalidated if the function which called setjmp() returns."
-#define nlr_push(buf) ((buf)->prev = nlr_top, nlr_top = (buf), setjmp((buf)->jmpbuf))
-#define nlr_pop() { nlr_top = nlr_top->prev; }
+#define nlr_push(buf) ((buf)->prev = MP_STATE_CTX(nlr_top), MP_STATE_CTX(nlr_top) = (buf), setjmp((buf)->jmpbuf))
+#define nlr_pop() { MP_STATE_CTX(nlr_top) = MP_STATE_CTX(nlr_top)->prev; }
 #define nlr_jump(val) nlr_setjmp_jump(val)
 #else
 unsigned int nlr_push(nlr_buf_t *);
diff --git a/py/nlrsetjmp.c b/py/nlrsetjmp.c
index 8b8e8d9..efd99ee 100644
--- a/py/nlrsetjmp.c
+++ b/py/nlrsetjmp.c
@@ -29,8 +29,8 @@
 #if MICROPY_NLR_SETJMP

 void nlr_setjmp_jump(void *val) {
-    nlr_buf_t *buf = nlr_top;
-    nlr_top = buf->prev;
+    nlr_buf_t *buf = MP_STATE_CTX(nlr_top);
+    MP_STATE_CTX(nlr_top) = buf->prev;
     buf->ret_val = val;
     longjmp(buf->jmpbuf, 1);
 }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx is common abbreviation for "context".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll fix that nlr_top reference.

@dpgeorge
Copy link
Member Author

dpgeorge commented Jan 2, 2015

I updated the PR: added some comments to better explain what is going on, and put all the global state structs into 1 big struct. So now there really is only 1 structure.

nlr seems to work for x64 and thumb, but not xtensa (esp8266 port).

@pfalcon
Copy link
Contributor

pfalcon commented Jan 6, 2015

Looks good. You surely checked code size after these changes to exclude gcc codegeneration wonders? Otherwise, let's have this merged?

@dpgeorge
Copy link
Member Author

dpgeorge commented Jan 6, 2015

You surely checked code size after these changes to exclude gcc codegeneration wonders?

I did, but I want to check again.

Otherwise, let's have this merged?

Yes, I'll just tidy a few more little things, fix nlrxtensa, then merge it.

@dpgeorge
Copy link
Member Author

dpgeorge commented Jan 7, 2015

Rebased and merged in b4b10fd.

unix, stmhal and esp8266 port are all tested and working. Assuming teensy and windows ports work.

Code size changes:

  • unix x64 up by 32 bytes
  • unix x86 up by 72 bytes
  • stmhal down by 260 bytes
  • bare-arm down by 36 bytes
  • esp8266 down by 32 bytes

All-in-all a good change, especially since GC can be more efficient and precise, and global state is all in one place.

@dpgeorge dpgeorge closed this Jan 7, 2015
@dpgeorge dpgeorge deleted the global-state-v2 branch January 7, 2015 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants