Skip to content

Commit e85c389

Browse files
committed
Make "unix" target be crossplatform and support x86, x64, ARM hosts.
1 parent 732407f commit e85c389

7 files changed

Lines changed: 56 additions & 32 deletions

File tree

py/asmx64.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string.h>
66

77
#include "misc.h"
8+
#include "mpconfig.h"
89
#include "asmx64.h"
910

1011
/* all offsets are measured in multiples of 8 bytes */
@@ -602,7 +603,7 @@ void asm_x64_call_i1(asm_x64_t* as, void* func, int i1)
602603
*/
603604

604605
void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) {
605-
asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
606+
asm_x64_mov_i64_to_r64_optimised(as, (machine_int_t)ptr, temp_r64);
606607
asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
607608
// this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
608609
// doesn't work anymore because calls are 64 bits away

py/nlrthumb.s renamed to py/nlrthumb.S

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
@ thumb callee save: bx, bp, sp, r12, r14, r14, r15
1+
#ifdef __thumb2__
2+
/* thumb callee save: bx, bp, sp, r12, r14, r14, r15 */
23

34
.syntax unified
45
.cpu cortex-m4
56
.thumb
67
.text
78
.align 2
89

9-
@ uint nlr_push(r0=nlr_buf_t *nlr)
10+
/* uint nlr_push(r0=nlr_buf_t *nlr) */
1011
.global nlr_push
1112
.thumb
1213
.thumb_func
@@ -51,7 +52,7 @@ nlr_pop:
5152
.word .LANCHOR0
5253
.size nlr_pop, .-nlr_pop
5354

54-
@ void nlr_jump(r0=uint val)
55+
/* void nlr_jump(r0=uint val) */
5556
.global nlr_jump
5657
.thumb
5758
.thumb_func
@@ -81,11 +82,12 @@ nlr_jump:
8182
.word .LANCHOR0
8283
.size nlr_jump, .-nlr_jump
8384

84-
@ local variable nlr_top
85+
/* local variable nlr_top */
8586
.bss
8687
.align 2
8788
.set .LANCHOR0,. + 0
8889
.type nlr_top, %object
8990
.size nlr_top, 4
9091
nlr_top:
9192
.space 4
93+
#endif

py/nlrx64.s renamed to py/nlrx64.S

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# x64 callee save: bx, bp, sp, r12, r14, r14, r15
1+
#ifdef __x86_64__
2+
/* x64 callee save: bx, bp, sp, r12, r14, r14, r15 */
23

34
.file "nlr.s"
45
.text
56

6-
# uint nlr_push(rdi=nlr_buf_t *nlr)
7+
/* uint nlr_push(rdi=nlr_buf_t *nlr) */
78
.globl nlr_push
89
.type nlr_push, @function
910
nlr_push:
@@ -23,7 +24,7 @@ nlr_push:
2324
ret # return
2425
.size nlr_push, .-nlr_push
2526

26-
# void nlr_pop()
27+
/* void nlr_pop() */
2728
.globl nlr_pop
2829
.type nlr_pop, @function
2930
nlr_pop:
@@ -33,7 +34,7 @@ nlr_pop:
3334
ret # return
3435
.size nlr_pop, .-nlr_pop
3536

36-
# void nlr_jump(rdi=uint val)
37+
/* void nlr_jump(rdi=uint val) */
3738
.globl nlr_jump
3839
.type nlr_jump, @function
3940
nlr_jump:
@@ -58,3 +59,4 @@ nlr_jump:
5859

5960
.local nlr_top
6061
.comm nlr_top,8,8
62+
#endif

py/nlrx86.s renamed to py/nlrx86.S

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# x86 callee save: bx, di, si, bp, sp
1+
#ifdef __i386__
2+
/* x86 callee save: bx, di, si, bp, sp */
23

34
.file "nlr.s"
45
.text
56

6-
# uint nlr_push(4(%esp)=nlr_buf_t *nlr)
7+
/* uint nlr_push(4(%esp)=nlr_buf_t *nlr) */
78
.globl nlr_push
89
.type nlr_push, @function
910
nlr_push:
@@ -22,7 +23,7 @@ nlr_push:
2223
ret # return
2324
.size nlr_push, .-nlr_push
2425

25-
# void nlr_pop()
26+
/* void nlr_pop() */
2627
.globl nlr_pop
2728
.type nlr_pop, @function
2829
nlr_pop:
@@ -32,7 +33,7 @@ nlr_pop:
3233
ret # return
3334
.size nlr_pop, .-nlr_pop
3435

35-
# void nlr_jump(4(%esp)=uint val)
36+
/* void nlr_jump(4(%esp)=uint val) */
3637
.globl nlr_jump
3738
.type nlr_jump, @function
3839
nlr_jump:
@@ -55,3 +56,4 @@ nlr_jump:
5556

5657
.local nlr_top
5758
.comm nlr_top,4,4
59+
#endif

py/showbc.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void mp_show_byte_code(const byte *ip, int len) {
7070

7171
case MP_BC_LOAD_FAST_N:
7272
DECODE_UINT;
73-
printf("LOAD_FAST_N %lu", unum);
73+
printf("LOAD_FAST_N " UINT_FMT, unum);
7474
break;
7575

7676
case MP_BC_LOAD_NAME:
@@ -111,7 +111,7 @@ void mp_show_byte_code(const byte *ip, int len) {
111111

112112
case MP_BC_STORE_FAST_N:
113113
DECODE_UINT;
114-
printf("STORE_FAST_N %lu", unum);
114+
printf("STORE_FAST_N " UINT_FMT, unum);
115115
break;
116116

117117
case MP_BC_STORE_NAME:
@@ -161,17 +161,17 @@ void mp_show_byte_code(const byte *ip, int len) {
161161

162162
case MP_BC_JUMP:
163163
DECODE_SLABEL;
164-
printf("JUMP %lu", ip + unum - ip_start);
164+
printf("JUMP " UINT_FMT, ip + unum - ip_start);
165165
break;
166166

167167
case MP_BC_POP_JUMP_IF_TRUE:
168168
DECODE_SLABEL;
169-
printf("POP_JUMP_IF_TRUE %lu", ip + unum - ip_start);
169+
printf("POP_JUMP_IF_TRUE " UINT_FMT, ip + unum - ip_start);
170170
break;
171171

172172
case MP_BC_POP_JUMP_IF_FALSE:
173173
DECODE_SLABEL;
174-
printf("POP_JUMP_IF_FALSE %lu", ip + unum - ip_start);
174+
printf("POP_JUMP_IF_FALSE " UINT_FMT, ip + unum - ip_start);
175175
break;
176176

177177
/*
@@ -196,7 +196,7 @@ void mp_show_byte_code(const byte *ip, int len) {
196196

197197
case MP_BC_SETUP_EXCEPT:
198198
DECODE_ULABEL; // except labels are always forward
199-
printf("SETUP_EXCEPT %lu", ip + unum - ip_start);
199+
printf("SETUP_EXCEPT " UINT_FMT, ip + unum - ip_start);
200200
break;
201201

202202
case MP_BC_END_FINALLY:
@@ -213,7 +213,7 @@ void mp_show_byte_code(const byte *ip, int len) {
213213

214214
case MP_BC_FOR_ITER:
215215
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
216-
printf("FOR_ITER %lu", ip + unum - ip_start);
216+
printf("FOR_ITER " UINT_FMT, ip + unum - ip_start);
217217
break;
218218

219219
case MP_BC_POP_BLOCK:
@@ -235,22 +235,22 @@ void mp_show_byte_code(const byte *ip, int len) {
235235

236236
case MP_BC_BINARY_OP:
237237
unum = *ip++;
238-
printf("BINARY_OP %lu", unum);
238+
printf("BINARY_OP " UINT_FMT, unum);
239239
break;
240240

241241
case MP_BC_COMPARE_OP:
242242
unum = *ip++;
243-
printf("COMPARE_OP %lu", unum);
243+
printf("COMPARE_OP " UINT_FMT, unum);
244244
break;
245245

246246
case MP_BC_BUILD_TUPLE:
247247
DECODE_UINT;
248-
printf("BUILD_TUPLE %lu", unum);
248+
printf("BUILD_TUPLE " UINT_FMT, unum);
249249
break;
250250

251251
case MP_BC_BUILD_LIST:
252252
DECODE_UINT;
253-
printf("BUILD_LIST %lu", unum);
253+
printf("BUILD_LIST " UINT_FMT, unum);
254254
break;
255255

256256
/*
@@ -264,7 +264,7 @@ void mp_show_byte_code(const byte *ip, int len) {
264264

265265
case MP_BC_BUILD_MAP:
266266
DECODE_UINT;
267-
printf("BUILD_MAP %lu", unum);
267+
printf("BUILD_MAP " UINT_FMT, unum);
268268
break;
269269

270270
/*
@@ -297,22 +297,22 @@ void mp_show_byte_code(const byte *ip, int len) {
297297

298298
case MP_BC_UNPACK_SEQUENCE:
299299
DECODE_UINT;
300-
printf("UNPACK_SEQUENCE %lu", unum);
300+
printf("UNPACK_SEQUENCE " UINT_FMT, unum);
301301
break;
302302

303303
case MP_BC_MAKE_FUNCTION:
304304
DECODE_UINT;
305-
printf("MAKE_FUNCTION %lu", unum);
305+
printf("MAKE_FUNCTION " UINT_FMT, unum);
306306
break;
307307

308308
case MP_BC_CALL_FUNCTION:
309309
DECODE_UINT;
310-
printf("CALL_FUNCTION n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
310+
printf("CALL_FUNCTION n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
311311
break;
312312

313313
case MP_BC_CALL_METHOD:
314314
DECODE_UINT;
315-
printf("CALL_METHOD n=%lu nkw=%lu", unum & 0xff, (unum >> 8) & 0xff);
315+
printf("CALL_METHOD n=" UINT_FMT " nkw=" UINT_FMT, unum & 0xff, (unum >> 8) & 0xff);
316316
break;
317317

318318
case MP_BC_RETURN_VALUE:

unix/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ SRC_C = \
99
main.c \
1010

1111
PY_O = \
12+
nlrx86.o \
1213
nlrx64.o \
14+
nlrthumb.o \
1315
malloc.o \
1416
qstr.o \
1517
vstr.o \
@@ -73,6 +75,9 @@ $(BUILD)/%.o: %.c
7375
$(BUILD)/%.o: $(PYSRC)/%.s
7476
$(AS) -o $@ $<
7577

78+
$(BUILD)/%.o: $(PYSRC)/%.S
79+
$(CC) $(CFLAGS) -c -o $@ $<
80+
7681
$(BUILD)/%.o: $(PYSRC)/%.c mpconfig.h
7782
$(CC) $(CFLAGS) -c -o $@ $<
7883

unix/mpconfig.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88

99
// type definitions for the specific machine
1010

11-
#define BYTES_PER_WORD (8)
11+
#ifdef __LP64__
12+
typedef long machine_int_t; // must be pointer size
13+
typedef unsigned long machine_uint_t; // must be pointer size
14+
#define UINT_FMT "%lu"
15+
#define INT_FMT "%ld"
16+
#else
17+
// These are definitions for machines where sizeof(int) == sizeof(void*),
18+
// regardless for actual size.
19+
typedef int machine_int_t; // must be pointer size
20+
typedef unsigned int machine_uint_t; // must be pointer size
21+
#define UINT_FMT "%u"
22+
#define INT_FMT "%d"
23+
#endif
24+
25+
#define BYTES_PER_WORD sizeof(machine_int_t)
1226

13-
typedef int64_t machine_int_t; // must be pointer size
14-
typedef uint64_t machine_uint_t; // must be pointer size
1527
typedef void *machine_ptr_t; // must be of pointer size
1628
typedef const void *machine_const_ptr_t; // must be of pointer size
1729
typedef double machine_float_t;

0 commit comments

Comments
 (0)