Skip to content

Commit de690d1

Browse files
committed
Add unix-cpy, used to test Micro Python byte code against CPython.
1 parent b86e3f9 commit de690d1

File tree

9 files changed

+187
-2
lines changed

9 files changed

+187
-2
lines changed

py/emitcpy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static void emit_cpy_import_star(emit_t *emit) {
131131
}
132132
}
133133

134-
static void emit_cpy_load_const_tok(emit_t *emit, py_token_kind_t tok) {
134+
static void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
135135
emit_pre(emit, 1, 3);
136136
if (emit->pass == PASS_3) {
137137
printf("LOAD_CONST ");
File renamed without changes.
File renamed without changes.

unix-cpy/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
cpy

unix-cpy/Makefile

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
PYSRC=../py
2+
BUILD=build
3+
4+
CC = gcc
5+
CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
6+
LDFLAGS = -lm
7+
8+
SRC_C = \
9+
main.c \
10+
11+
PY_O = \
12+
nlrx64.o \
13+
malloc.o \
14+
qstr.o \
15+
vstr.o \
16+
misc.o \
17+
lexer.o \
18+
lexerunix.o \
19+
parse.o \
20+
scope.o \
21+
compile.o \
22+
emitcommon.o \
23+
emitpass1.o \
24+
emitcpy.o \
25+
runtime.o \
26+
map.o \
27+
obj.o \
28+
objbool.o \
29+
objboundmeth.o \
30+
objcell.o \
31+
objclass.o \
32+
objclosure.o \
33+
objcomplex.o \
34+
objdict.o \
35+
objexcept.o \
36+
objfloat.o \
37+
objfun.o \
38+
objgenerator.o \
39+
objinstance.o \
40+
objlist.o \
41+
objnone.o \
42+
objrange.o \
43+
objset.o \
44+
objstr.o \
45+
objtuple.o \
46+
objtype.o \
47+
builtin.o \
48+
vm.o \
49+
showbc.o \
50+
repl.o \
51+
52+
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O))
53+
LIB =
54+
PROG = cpy
55+
56+
$(PROG): $(BUILD) $(OBJ)
57+
$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
58+
59+
$(BUILD):
60+
mkdir $@
61+
62+
$(BUILD)/%.o: %.c
63+
$(CC) $(CFLAGS) -c -o $@ $<
64+
65+
$(BUILD)/%.o: $(PYSRC)/%.s
66+
$(AS) -o $@ $<
67+
68+
$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h
69+
$(CC) $(CFLAGS) -c -o $@ $<
70+
71+
$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
72+
$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
73+
74+
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
75+
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
76+
77+
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
78+
$(BUILD)/vm.o: $(PYSRC)/vm.c
79+
$(CC) $(CFLAGS) -O3 -c -o $@ $<
80+
81+
$(BUILD)/main.o: mpconfig.h
82+
$(BUILD)/parse.o: $(PYSRC)/grammar.h
83+
$(BUILD)/compile.o: $(PYSRC)/grammar.h
84+
$(BUILD)/emitcpy.o: $(PYSRC)/emit.h
85+
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
86+
87+
clean:
88+
/bin/rm -r $(BUILD)
89+
90+
.PHONY: clean

unix-cpy/main.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include "nlr.h"
6+
#include "misc.h"
7+
#include "mpconfig.h"
8+
#include "lexer.h"
9+
#include "lexerunix.h"
10+
#include "parse.h"
11+
#include "compile.h"
12+
#include "obj.h"
13+
#include "runtime0.h"
14+
#include "runtime.h"
15+
16+
void do_file(const char *file) {
17+
mp_lexer_t *lex = mp_lexer_new_from_file(file);
18+
if (lex == NULL) {
19+
return;
20+
}
21+
22+
if (0) {
23+
// just tokenise
24+
while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
25+
mp_token_show(mp_lexer_cur(lex));
26+
mp_lexer_to_next(lex);
27+
}
28+
mp_lexer_free(lex);
29+
30+
} else {
31+
// compile
32+
33+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
34+
mp_lexer_free(lex);
35+
36+
if (pn != MP_PARSE_NODE_NULL) {
37+
//printf("----------------\n");
38+
//parse_node_show(pn, 0);
39+
//printf("----------------\n");
40+
bool comp_ok = mp_compile(pn, false);
41+
//printf("----------------\n");
42+
43+
if (!comp_ok) {
44+
printf("compile error\n");
45+
}
46+
}
47+
}
48+
}
49+
50+
int main(int argc, char **argv) {
51+
qstr_init();
52+
rt_init();
53+
54+
if (argc == 2) {
55+
do_file(argv[1]);
56+
} else {
57+
printf("usage: py [<file>]\n");
58+
return 1;
59+
}
60+
rt_deinit();
61+
62+
return 0;
63+
}
64+
65+
// for sqrt
66+
#include <math.h>
67+
machine_float_t machine_sqrt(machine_float_t x) {
68+
return sqrt(x);
69+
}

unix-cpy/mpconfig.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// options to control how Micro Python is built
2+
3+
#define MICROPY_ENABLE_FLOAT (1)
4+
#define MICROPY_EMIT_CPYTHON (1)
5+
#define MICROPY_EMIT_X64 (0)
6+
#define MICROPY_EMIT_THUMB (0)
7+
#define MICROPY_EMIT_INLINE_THUMB (0)
8+
9+
// type definitions for the specific machine
10+
11+
#define BYTES_PER_WORD (8)
12+
13+
typedef int64_t machine_int_t; // must be pointer size
14+
typedef uint64_t machine_uint_t; // must be pointer size
15+
typedef void *machine_ptr_t; // must be of pointer size
16+
typedef const void *machine_const_ptr_t; // must be of pointer size
17+
typedef double machine_float_t;
18+
19+
machine_float_t machine_sqrt(machine_float_t x);

unix/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
py
3+
*.py

unix/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LDFLAGS = -lm
77

88
SRC_C = \
99
main.c \
10-
lexerunix.c \
1110

1211
PY_O = \
1312
nlrx64.o \
@@ -16,6 +15,7 @@ PY_O = \
1615
vstr.o \
1716
misc.o \
1817
lexer.o \
18+
lexerunix.o \
1919
parse.o \
2020
scope.o \
2121
compile.o \
@@ -94,3 +94,5 @@ $(BUILD)/emitbc.o: $(PYSRC)/emit.h
9494

9595
clean:
9696
/bin/rm -r $(BUILD)
97+
98+
.PHONY: clean

0 commit comments

Comments
 (0)