Commit de690d12 authored by Damien's avatar Damien
Browse files

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

parent b86e3f92
......@@ -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 ");
......
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
#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);
}
// 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);
build
py
*.py
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment