Factor and simplify Makefile's and mpconfig.

This commit is contained in:
Damien George 2014-01-07 14:54:15 +00:00
parent 880ce2d7fa
commit 136f67523b
11 changed files with 278 additions and 259 deletions

View File

@ -4,8 +4,65 @@
#include <mpconfigport.h>
#ifndef INT_FMT
// Any options not explicitly set in mpconfigport.h will get default
// values below.
/*****************************************************************************/
/* Micro Python emitters */
// Whether to emit CPython byte codes (for debugging/testing)
// Enabling this overrides all other emitters
#ifndef MICROPY_EMIT_CPYTHON
#define MICROPY_EMIT_CPYTHON (0)
#endif
// Whether to emit x64 native code
#ifndef MICROPY_EMIT_X64
#define MICROPY_EMIT_X64 (0)
#endif
// Whether to emit thumb native code
#ifndef MICROPY_EMIT_THUMB
#define MICROPY_EMIT_THUMB (0)
#endif
// Whether to enable the thumb inline assembler
#ifndef MICROPY_EMIT_INLINE_THUMB
#define MICROPY_EMIT_INLINE_THUMB (0)
#endif
/*****************************************************************************/
/* Internal debugging stuff */
// Whether to collect memory allocation stats
#ifndef MICROPY_MEM_STATS
#define MICROPY_MEM_STATS (0)
#endif
/*****************************************************************************/
/* Fine control over Python features */
// Whether to include REPL helper function
#ifndef MICROPY_ENABLE_REPL_HELPERS
#define MICROPY_ENABLE_REPL_HELPERS (0)
#endif
// Whether to support float and complex types
#ifndef MICROPY_ENABLE_FLOAT
#define MICROPY_ENABLE_FLOAT (0)
#endif
// Whether to support slice object and correspondingly
// slice subscript operators
#ifndef MICROPY_ENABLE_SLICE
#define MICROPY_ENABLE_SLICE (1)
#endif
/*****************************************************************************/
/* Miscellaneous settings */
// printf format spec to use for machine_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__
// Archs where machine_int_t == long, long != int
#define UINT_FMT "%lu"
@ -16,18 +73,3 @@
#define INT_FMT "%d"
#endif
#endif //INT_FMT
// Any options not explicitly set in mpconfigport.h will get default
// values below.
// Whether to collect memory allocation stats
#ifndef MICROPY_MEM_STATS
#define MICROPY_MEM_STATS (1)
#endif
// Whether to support slice object and correspondingly
// slice subscript operators
#ifndef MICROPY_ENABLE_SLICE
#define MICROPY_ENABLE_SLICE (1)
#endif

100
py/py.mk Normal file
View File

@ -0,0 +1,100 @@
# default settings; can be overriden in main Makefile
ifndef PY_SRC
PY_SRC = ../py
endif
ifndef BUILD
BUILD = build
endif
# to create the build directory
$(BUILD):
mkdir -p $@
# where py object files go (they have a name prefix to prevent filename clashes)
PY_BUILD = $(BUILD)/py.
# py object files
PY_O_BASENAME = \
nlrx86.o \
nlrx64.o \
nlrthumb.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
emitbc.o \
asmx64.o \
emitnx64.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
# prepend the build destination prefix to the py object files
PY_O = $(addprefix $(PY_BUILD), $(PY_O_BASENAME))
$(PY_BUILD)emitnx64.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(PY_BUILD)emitnthumb.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
$(PY_BUILD)%.o: $(PY_SRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(PY_BUILD)vm.o: $(PY_SRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
# header dependencies
$(PY_BUILD)parse.o: $(PY_SRC)/grammar.h
$(PY_BUILD)compile.o: $(PY_SRC)/grammar.h
$(PY_BUILD)/emitcpy.o: $(PY_SRC)/emit.h
$(PY_BUILD)emitbc.o: $(PY_SRC)/emit.h

View File

@ -1,6 +1,9 @@
#include "misc.h"
#include "mpconfig.h"
#include "repl.h"
#if MICROPY_ENABLE_REPL_HELPERS
bool str_startswith_word(const char *str, const char *head) {
int i;
for (i = 0; str[i] && head[i]; i++) {
@ -42,3 +45,5 @@ bool mp_repl_is_compound_stmt(const char *line) {
}
return n_paren > 0 || n_brack > 0 || n_brace > 0;
}
#endif // MICROPY_ENABLE_REPL_HELPERS

View File

@ -1 +1,3 @@
#if MICROPY_ENABLE_REPL_HELPERS
bool mp_repl_is_compound_stmt(const char *line);
#endif

View File

@ -2,11 +2,10 @@
// options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (1)
#define MICROPY_EMIT_INLINE_THUMB (1)
#define MICROPY_ENABLE_REPL_HELPERS (1)
#define MICROPY_ENABLE_FLOAT (1)
// type definitions for the specific machine

View File

@ -1,95 +1,37 @@
PYSRC=../py
BUILD=build
# define main target
PROG = cpy
all: $(PROG)
# include py core make definitions
include ../py/py.mk
# program for deletion
RM = /bin/rm
# compiler settings
CC = gcc
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
LDFLAGS = -lm
# source files
SRC_C = \
main.c \
PY_O = \
nlrx86.o \
nlrx64.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O)
LIB =
PROG = cpy
$(PROG): $(BUILD) $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
$(BUILD):
mkdir -p $@
strip $(PROG)
size $(PROG)
$(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean:
/bin/rm -rf $(BUILD)
$(RM) -f $(PROG)
$(RM) -rf $(BUILD)
.PHONY: clean
.PHONY: all clean

View File

@ -11,7 +11,6 @@
#include "obj.h"
#include "compile.h"
#include "runtime0.h"
#include "runtime.h"
void do_file(const char *file) {
mp_lexer_t *lex = mp_lexer_new_from_file(file);

View File

@ -2,9 +2,6 @@
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (1)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
// type definitions for the specific machine

View File

@ -1,106 +1,39 @@
PYSRC=../py
BUILD=build
# define main target
PROG = py
all: $(PROG)
# include py core make definitions
include ../py/py.mk
# program for deletion
RM = /bin/rm
# compiler settings
CC = gcc
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
LDFLAGS = -lm
# source files
SRC_C = \
main.c \
PY_O = \
nlrx86.o \
nlrx64.o \
nlrthumb.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitcpy.o \
emitbc.o \
asmx64.o \
emitnx64.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O)
LIB = -lreadline
# the following is needed for BSD
#LIB += -ltermcap
PROG = py
$(PROG): $(BUILD) $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
strip $(PROG)
size $(PROG)
$(BUILD):
mkdir -p $@
$(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean:
/bin/rm -rf $(BUILD)
$(RM) -f $(PROG)
$(RM) -rf $(BUILD)
.PHONY: clean
.PHONY: all clean

View File

@ -20,6 +20,52 @@
#include <readline/history.h>
#endif
static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
if (lex == NULL) {
return;
}
if (0) {
// just tokenise
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
mp_token_show(mp_lexer_cur(lex));
mp_lexer_to_next(lex);
}
mp_lexer_free(lex);
return;
}
mp_parse_node_t pn = mp_parse(lex, input_kind);
mp_lexer_free(lex);
if (pn == MP_PARSE_NODE_NULL) {
// parse error
return;
}
//printf("----------------\n");
//parse_node_show(pn, 0);
//printf("----------------\n");
mp_obj_t module_fun = mp_compile(pn, is_repl);
if (module_fun == mp_const_none) {
// compile error
return;
}
// execute it
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
static char *str_join(const char *s1, int sep_char, const char *s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
@ -80,28 +126,11 @@ static void do_repl(void) {
}
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", line, strlen(line), false);
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) {
//mp_parse_node_show(pn, 0);
mp_obj_t module_fun = mp_compile(pn, true);
if (module_fun != mp_const_none) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
}
execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
}
}
void do_file(const char *file) {
static void do_file(const char *file) {
// hack: set dir for import based on where this file is
{
const char * s = strrchr(file, '/');
@ -115,53 +144,12 @@ void do_file(const char *file) {
}
mp_lexer_t *lex = mp_lexer_new_from_file(file);
//const char *pysrc = "def f():\n x=x+1\n print(42)\n";
//mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false);
if (lex == NULL) {
return;
}
execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
}
if (0) {
// just tokenise
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
mp_token_show(mp_lexer_cur(lex));
mp_lexer_to_next(lex);
}
mp_lexer_free(lex);
} else {
// compile
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) {
//printf("----------------\n");
//parse_node_show(pn, 0);
//printf("----------------\n");
mp_obj_t module_fun = mp_compile(pn, false);
//printf("----------------\n");
#if MICROPY_EMIT_CPYTHON
if (!comp_ok) {
printf("compile error\n");
}
#else
if (1 && module_fun != mp_const_none) {
// execute it
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\n");
}
}
#endif
}
}
static void do_str(const char *str) {
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", str, strlen(str), false);
execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
}
typedef struct _test_obj_t {
@ -192,12 +180,6 @@ static const mp_obj_type_t test_type = {
{ &mp_const_type },
"Test",
.print = test_print,
.make_new = NULL,
.call_n = NULL,
.unary_op = NULL,
.binary_op = NULL,
.getiter = NULL,
.iternext = NULL,
.methods = {
{ "get", &test_get_obj },
{ "set", &test_set_obj },
@ -212,6 +194,11 @@ mp_obj_t test_obj_new(int value) {
return o;
}
int usage(void) {
printf("usage: py [-c <command>] [<filename>]\n");
return 1;
}
int main(int argc, char **argv) {
qstr_init();
rt_init();
@ -227,12 +214,24 @@ int main(int argc, char **argv) {
if (argc == 1) {
do_repl();
} else if (argc == 2) {
do_file(argv[1]);
} else {
printf("usage: py [<file>]\n");
return 1;
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') {
if (strcmp(argv[a], "-c") == 0) {
if (a + 1 >= argc) {
return usage();
}
do_str(argv[a + 1]);
a += 1;
} else {
return usage();
}
} else {
do_file(argv[a]);
}
}
}
rt_deinit();
//printf("total bytes = %d\n", m_get_total_bytes_allocated());

View File

@ -5,11 +5,12 @@
#define MICROPY_USE_READLINE (1)
#endif
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (1)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_MEM_STATS (1)
#define MICROPY_ENABLE_REPL_HELPERS (1)
#define MICROPY_ENABLE_FLOAT (1)
// type definitions for the specific machine