Add unix-cpy, used to test Micro Python byte code against CPython.

This commit is contained in:
Damien 2013-12-29 18:01:01 +00:00
parent b86e3f9293
commit de690d128b
9 changed files with 187 additions and 2 deletions

View File

@ -131,7 +131,7 @@ static void emit_cpy_import_star(emit_t *emit) {
}
}
static void emit_cpy_load_const_tok(emit_t *emit, py_token_kind_t tok) {
static void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST ");

2
unix-cpy/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build
cpy

90
unix-cpy/Makefile Normal file
View File

@ -0,0 +1,90 @@
PYSRC=../py
BUILD=build
CC = gcc
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
LDFLAGS = -lm
SRC_C = \
main.c \
PY_O = \
nlrx64.o \
malloc.o \
qstr.o \
vstr.o \
misc.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 \
objlist.o \
objnone.o \
objrange.o \
objset.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
vm.o \
showbc.o \
repl.o \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
LIB =
PROG = cpy
$(PROG): $(BUILD) $(OBJ)
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
$(BUILD):
mkdir $@
$(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.s
$(AS) -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.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: mpconfig.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 -r $(BUILD)
.PHONY: clean

69
unix-cpy/main.c Normal file
View File

@ -0,0 +1,69 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "lexer.h"
#include "lexerunix.h"
#include "parse.h"
#include "compile.h"
#include "obj.h"
#include "runtime0.h"
#include "runtime.h"
void do_file(const char *file) {
mp_lexer_t *lex = mp_lexer_new_from_file(file);
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);
} 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");
bool comp_ok = mp_compile(pn, false);
//printf("----------------\n");
if (!comp_ok) {
printf("compile error\n");
}
}
}
}
int main(int argc, char **argv) {
qstr_init();
rt_init();
if (argc == 2) {
do_file(argv[1]);
} else {
printf("usage: py [<file>]\n");
return 1;
}
rt_deinit();
return 0;
}
// for sqrt
#include <math.h>
machine_float_t machine_sqrt(machine_float_t x) {
return sqrt(x);
}

19
unix-cpy/mpconfig.h Normal file
View File

@ -0,0 +1,19 @@
// options to control how Micro Python is built
#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
#define BYTES_PER_WORD (8)
typedef int64_t machine_int_t; // must be pointer size
typedef uint64_t machine_uint_t; // must be pointer size
typedef void *machine_ptr_t; // must be of pointer size
typedef const void *machine_const_ptr_t; // must be of pointer size
typedef double machine_float_t;
machine_float_t machine_sqrt(machine_float_t x);

3
unix/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build
py
*.py

View File

@ -7,7 +7,6 @@ LDFLAGS = -lm
SRC_C = \
main.c \
lexerunix.c \
PY_O = \
nlrx64.o \
@ -16,6 +15,7 @@ PY_O = \
vstr.o \
misc.o \
lexer.o \
lexerunix.o \
parse.o \
scope.o \
compile.o \
@ -94,3 +94,5 @@ $(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean:
/bin/rm -r $(BUILD)
.PHONY: clean