Skip to content

Commit c47fd2d

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents bcc9298 + 1d75533 commit c47fd2d

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

py/emitglue.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This code glues the code emitters to the runtime.
22

3-
#include <stdlib.h>
3+
#include <stdio.h>
44
#include <assert.h>
55

66
#include "misc.h"
@@ -10,6 +10,7 @@
1010
#include "runtime0.h"
1111
#include "runtime.h"
1212
#include "emitglue.h"
13+
#include "bc.h"
1314

1415
#if 0 // print debugging info
1516
#define DEBUG_PRINT (1)
@@ -51,13 +52,27 @@ STATIC machine_uint_t unique_codes_alloc = 0;
5152
STATIC mp_code_t *unique_codes = NULL;
5253
STATIC uint next_unique_code_id;
5354

55+
#ifdef WRITE_CODE
56+
FILE *fp_write_code = NULL;
57+
#endif
58+
5459
void mp_emit_glue_init(void) {
5560
next_unique_code_id = 0;
5661
unique_codes_alloc = 0;
5762
unique_codes = NULL;
63+
64+
#ifdef WRITE_CODE
65+
fp_write_code = fopen("out-code", "wb");
66+
#endif
5867
}
5968

6069
void mp_emit_glue_deinit(void) {
70+
#ifdef WRITE_CODE
71+
if (fp_write_code != NULL) {
72+
fclose(fp_write_code);
73+
}
74+
#endif
75+
6176
m_del(mp_code_t, unique_codes, unique_codes_alloc);
6277
}
6378

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ extern const mp_obj_t mp_const_false;
231231
extern const mp_obj_t mp_const_true;
232232
extern const mp_obj_t mp_const_empty_tuple;
233233
extern const mp_obj_t mp_const_ellipsis;
234+
extern const mp_obj_t mp_const_GeneratorExit;
234235

235236
// General API for objects
236237

py/objexcept.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ typedef struct mp_obj_exception_t {
2121
mp_obj_tuple_t args;
2222
} mp_obj_exception_t;
2323

24+
// Instance of GeneratorExit exception - needed by generator.close()
25+
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
26+
// definition module-private so far, have it here.
27+
STATIC mp_obj_exception_t GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&tuple_type}, 0}};
28+
const mp_obj_t mp_const_GeneratorExit = (mp_obj_t)&GeneratorExit_obj;
29+
2430
STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2531
mp_obj_exception_t *o = o_in;
2632
if (o->msg != NULL) {

py/objgenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
171171

172172
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
173173
mp_obj_t ret;
174-
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret)) {
174+
switch (mp_obj_gen_resume(self_in, mp_const_none, mp_const_GeneratorExit, &ret)) {
175175
case MP_VM_RETURN_YIELD:
176176
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
177177

py/runtime.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#if 0 // print debugging info
2222
#define DEBUG_PRINT (1)
23-
#define WRITE_CODE (1)
2423
#define DEBUG_printf DEBUG_printf
2524
#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
2625
#else // don't print debugging info
@@ -33,10 +32,6 @@ STATIC mp_map_t *map_locals;
3332
STATIC mp_map_t *map_globals;
3433
STATIC mp_map_t map_builtins;
3534

36-
#ifdef WRITE_CODE
37-
FILE *fp_write_code = NULL;
38-
#endif
39-
4035
// a good optimising compiler will inline this if necessary
4136
STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
4237
mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
@@ -68,18 +63,9 @@ void rt_init(void) {
6863
// for efficiency, left to platform-specific startup code
6964
//sys_path = mp_obj_new_list(0, NULL);
7065
//rt_store_attr(m_sys, MP_QSTR_path, sys_path);
71-
72-
#ifdef WRITE_CODE
73-
fp_write_code = fopen("out-code", "wb");
74-
#endif
7566
}
7667

7768
void rt_deinit(void) {
78-
#ifdef WRITE_CODE
79-
if (fp_write_code != NULL) {
80-
fclose(fp_write_code);
81-
}
82-
#endif
8369
mp_map_free(map_globals);
8470
mp_map_deinit(&map_builtins);
8571
mp_module_deinit();

py/showbc.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ void mp_byte_code_print(const byte *ip, int len) {
3030
machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
3131
ip += code_info_size;
3232

33-
// decode prelude
33+
// bytecode prelude: state size and exception stack size; 16 bit uints
34+
{
35+
uint n_state = ip[0] | (ip[1] << 8);
36+
uint n_exc_stack = ip[2] | (ip[3] << 8);
37+
ip += 4;
38+
printf("(N_STATE %u)\n", n_state);
39+
printf("(N_EXC_STACK %u)\n", n_exc_stack);
40+
}
41+
42+
// bytecode prelude: initialise closed over variables
3443
{
3544
uint n_local = *ip++;
3645
printf("(NUM_LOCAL %u)\n", n_local);
@@ -244,6 +253,15 @@ void mp_byte_code_print(const byte *ip, int len) {
244253
printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start);
245254
break;
246255

256+
case MP_BC_SETUP_WITH:
257+
DECODE_ULABEL; // loop-like labels are always forward
258+
printf("SETUP_WITH " UINT_FMT, ip + unum - ip_start);
259+
break;
260+
261+
case MP_BC_WITH_CLEANUP:
262+
printf("WITH_CLEANUP");
263+
break;
264+
247265
case MP_BC_UNWIND_JUMP:
248266
DECODE_SLABEL;
249267
printf("UNWIND_JUMP " UINT_FMT " %d", ip + unum - ip_start, *ip);

0 commit comments

Comments
 (0)