Skip to content

Commit

Permalink
shared/readline: Use MP_REGISTER_ROOT_POINTER().
Browse files Browse the repository at this point in the history
This uses MP_REGISTER_ROOT_POINTER() to register the readline_history root
pointer array used by shared/readline.c and removes the registration from
all mpconfigport.h files.

This also required adding a new MICROPY_READLINE_HISTORY_SIZE config option
since not all ports used the same sized array.

Signed-off-by: David Lechner <[email protected]>
  • Loading branch information
dlech authored and dpgeorge committed Jul 18, 2022
1 parent fc3d7ae commit 81dbea1
Show file tree
Hide file tree
Showing 28 changed files with 31 additions and 46 deletions.
3 changes: 0 additions & 3 deletions docs/develop/porting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ The following is an example of an ``mpconfigport.h`` file:
#define MP_STATE_PORT MP_STATE_VM
#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
This configuration file contains machine-specific configurations including aspects like if different
MicroPython features should be enabled e.g. ``#define MICROPY_ENABLE_GC (1)``. Making this Setting
``(0)`` disables the feature.
Expand Down
2 changes: 0 additions & 2 deletions examples/embedding/mpconfigport_minimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
#define MICROPY_PY_UHASHLIB (0)
#define MICROPY_PY_UBINASCII (0)

#define MICROPY_PORT_ROOT_POINTERS \

//////////////////////////////////////////
// Do not change anything beyond this line
//////////////////////////////////////////
Expand Down
1 change: 0 additions & 1 deletion ports/cc3200/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
// vm state and root pointers for the gc
#define MP_STATE_PORT MP_STATE_VM
#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t mp_const_user_interrupt; \
mp_obj_t machine_config_main; \
mp_obj_list_t pyb_sleep_obj_list; \
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ struct mp_bluetooth_nimble_root_pointers_t;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t machine_pin_irq_handler[40]; \
struct _machine_timer_obj_t *machine_timer_obj_head; \
struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]; \
Expand Down
1 change: 0 additions & 1 deletion ports/esp8266/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ extern const struct _mp_print_t mp_debug_print;
#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t pin_irq_handler[16]; \
byte *uart0_rxbuf; \

Expand Down
2 changes: 1 addition & 1 deletion ports/javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SRC_C = \
mphalport.c \
modutime.c \

SRC_QSTR += $(SRC_C)
SRC_QSTR += $(SRC_C) $(SRC_SHARED)

OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED:.c=.o))
Expand Down
3 changes: 0 additions & 3 deletions ports/javascript/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,3 @@ typedef long mp_off_t;
#define MICROPY_HW_MCU_NAME "Emscripten"

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
1 change: 1 addition & 0 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ SRC_QSTR += \
modmimxrt.c \
modutime.c \
pin.c \
shared/readline/readline.c \
shared/runtime/mpirq.c \
shared/runtime/sys_stdio_mphal.c \
$(GEN_PINS_SRC)
Expand Down
1 change: 0 additions & 1 deletion ports/mimxrt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ extern const struct _mp_obj_type_t network_lan_type;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
struct _machine_timer_obj_t *timer_table[MICROPY_HW_PIT_NUM_CHANNELS]; \
void *machine_pin_irq_objects[MICROPY_HW_NUM_PIN_IRQS]; \
/* list of registered NICs */ \
Expand Down
1 change: 1 addition & 0 deletions ports/minimal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ SRC_C += $(SRC_MOD)

SRC_CXX += $(SRC_MOD_CXX)

SRC_QSTR += shared/readline/readline.c
SRC_QSTR += $(SRC_MOD) $(SRC_MOD_CXX)

OBJ += $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
Expand Down
3 changes: 0 additions & 3 deletions ports/minimal/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,3 @@ typedef long mp_off_t;
#endif

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
1 change: 0 additions & 1 deletion ports/nrf/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ typedef long mp_off_t;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t pin_class_mapper; \
mp_obj_t pin_class_map_dict; \
mp_obj_t pin_irq_handlers[NUM_OF_PINS]; \
Expand Down
1 change: 0 additions & 1 deletion ports/pic16bit/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ typedef int mp_off_t;
#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
char *readline_hist[8]; \
mp_obj_t keyboard_interrupt_obj; \

#define MICROPY_MPHALPORT_H "pic16bit_mphal.h"
Expand Down
2 changes: 2 additions & 0 deletions ports/powerpc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ SRC_C = \
shared/runtime/stdout_helpers.c \
$(BUILD)/_frozen_mpy.c \

SRC_QSTR += shared/readline/readline.c

OBJ = $(PY_CORE_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(BUILD)/head.o
Expand Down
3 changes: 0 additions & 3 deletions ports/powerpc/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ typedef long mp_off_t;

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];

// powerpc64 gcc doesn't seem to define these
// These are pointers, so make them 64 bit types
typedef long intptr_t;
Expand Down
2 changes: 0 additions & 2 deletions ports/renesas-ra/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
\
mp_obj_t pyb_hid_report_desc; \
\
mp_obj_t pyb_config_main; \
Expand Down
1 change: 1 addition & 0 deletions ports/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ set(MICROPY_SOURCE_PORT

set(MICROPY_SOURCE_QSTR
${MICROPY_SOURCE_PY}
${MICROPY_DIR}/shared/readline/readline.c
${MICROPY_DIR}/shared/runtime/mpirq.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${PROJECT_SOURCE_DIR}/machine_adc.c
Expand Down
1 change: 0 additions & 1 deletion ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
void *machine_pin_irq_obj[30]; \
void *rp2_pio_irq_obj[2]; \
void *rp2_state_machine_irq_obj[8]; \
Expand Down
1 change: 1 addition & 0 deletions ports/samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ SRC_QSTR += \
machine_led.c \
modsamd.c \
samd_flash.c \
shared/readline/readline.c \

SRC_QSTR += $(SRC_MOD) $(SRC_CXX)

Expand Down
3 changes: 0 additions & 3 deletions ports/samd/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@
#define mp_type_fileio mp_type_vfs_lfs1_fileio
#define mp_type_textio mp_type_vfs_lfs1_textio

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];

#define MP_STATE_PORT MP_STATE_VM

// Miscellaneous settings
Expand Down
2 changes: 0 additions & 2 deletions ports/stm32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ struct _mp_bluetooth_btstack_root_pointers_t;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
\
mp_obj_t pyb_hid_report_desc; \
\
mp_obj_t pyb_config_main; \
Expand Down
1 change: 0 additions & 1 deletion ports/teensy/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ extern const struct _mp_obj_module_t pyb_module;
#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t pin_class_mapper; \
mp_obj_t pin_class_map_dict; \
struct _pyb_uart_obj_t *pyb_stdio_uart; \
Expand Down
4 changes: 3 additions & 1 deletion ports/unix/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
#define MICROPY_READER_POSIX (1)
#define MICROPY_READER_VFS (1)
#define MICROPY_USE_READLINE_HISTORY (1)
#ifndef MICROPY_READLINE_HISTORY_SIZE
#define MICROPY_READLINE_HISTORY_SIZE 50
#endif
#define MICROPY_HELPER_LEXER_UNIX (1)
#ifndef MICROPY_FLOAT_IMPL
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
Expand Down Expand Up @@ -302,7 +305,6 @@ struct _mp_bluetooth_nimble_malloc_t;
#endif

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[50]; \
void *mmap_region_head; \
MICROPY_BLUETOOTH_ROOT_POINTERS \

Expand Down
12 changes: 6 additions & 6 deletions ports/windows/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#ifndef MICROPY_USE_READLINE
#define MICROPY_USE_READLINE (1)
#endif
#ifndef MICROPY_USE_READLINE_HISTORY
#define MICROPY_USE_READLINE_HISTORY (1)
#endif
#ifndef MICROPY_READLINE_HISTORY_SIZE
#define MICROPY_READLINE_HISTORY_SIZE (50)
#endif

#define MICROPY_ALLOC_PATH_MAX (260) // see minwindef.h for msvc or limits.h for mingw
#define MICROPY_PERSISTENT_CODE_LOAD (1)
Expand All @@ -52,7 +58,6 @@
#define MICROPY_DEBUG_PRINTERS (1)
#define MICROPY_READER_POSIX (1)
#define MICROPY_READER_VFS (1)
#define MICROPY_USE_READLINE_HISTORY (1)
#define MICROPY_HELPER_REPL (1)
#define MICROPY_REPL_EMACS_KEYS (1)
#define MICROPY_REPL_AUTO_INDENT (1)
Expand Down Expand Up @@ -210,11 +215,6 @@ typedef long long mp_off_t;
typedef long mp_off_t;
#endif

#if MICROPY_USE_READLINE == 1
#define MICROPY_PORT_ROOT_POINTERS \
char *readline_hist[50];
#endif

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_MPHALPORT_H "windows_mphal.h"
Expand Down
1 change: 0 additions & 1 deletion ports/zephyr/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ typedef long mp_off_t;
#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
void *machine_pin_irq_list; /* Linked list of pin irq objects */ \
struct _mp_bluetooth_zephyr_root_pointers_t *bluetooth_zephyr_root_pointers;

Expand Down
3 changes: 0 additions & 3 deletions ports/zephyr/mpconfigport_minimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,3 @@ typedef unsigned mp_uint_t; // must be pointer size
typedef long mp_off_t;

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
5 changes: 5 additions & 0 deletions py/mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@
#define MICROPY_REPL_EVENT_DRIVEN (0)
#endif

// The number of items to keep in the readline history.
#ifndef MICROPY_READLINE_HISTORY_SIZE
#define MICROPY_READLINE_HISTORY_SIZE (8)
#endif

// Whether to include lexer helper function for unix
#ifndef MICROPY_HELPER_LEXER_UNIX
#define MICROPY_HELPER_LEXER_UNIX (0)
Expand Down
15 changes: 10 additions & 5 deletions shared/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@
#define DEBUG_printf(...) (void)0
#endif

#define READLINE_HIST_SIZE (MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)))

// flags for readline_t.auto_indent_state
#define AUTO_INDENT_ENABLED (0x01)
#define AUTO_INDENT_JUST_ADDED (0x02)

enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O };

#ifdef _MSC_VER
// work around MSVC compiler bug: https://stackoverflow.com/q/62259834/1976323
#pragma warning(disable : 4090)
#endif

void readline_init0(void) {
memset(MP_STATE_PORT(readline_hist), 0, READLINE_HIST_SIZE * sizeof(const char*));
memset(MP_STATE_PORT(readline_hist), 0, MICROPY_READLINE_HISTORY_SIZE * sizeof(const char*));
}

STATIC char *str_dup_maybe(const char *str) {
Expand Down Expand Up @@ -334,7 +337,7 @@ int readline_process_char(int c) {
up_arrow_key:
#endif
// up arrow
if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) {
if (rl.hist_cur + 1 < MICROPY_READLINE_HISTORY_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) {
// increase hist num
rl.hist_cur += 1;
// set line to history
Expand Down Expand Up @@ -570,10 +573,12 @@ void readline_push_history(const char *line) {
// so update the history
char *most_recent_hist = str_dup_maybe(line);
if (most_recent_hist != NULL) {
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
for (int i = MICROPY_READLINE_HISTORY_SIZE - 1; i > 0; i--) {
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1];
}
MP_STATE_PORT(readline_hist)[0] = most_recent_hist;
}
}
}

MP_REGISTER_ROOT_POINTER(const char *readline_hist[MICROPY_READLINE_HISTORY_SIZE]);

0 comments on commit 81dbea1

Please sign in to comment.