emitglue.c 5.05 KB
Newer Older
1
2
// This code glues the code emitters to the runtime.

3
#include <stdio.h>
4
#include <string.h>
5
6
7
8
9
10
11
12
13
#include <assert.h>

#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
#include "runtime.h"
#include "emitglue.h"
14
#include "bc.h"
15
16
17
18
19
20
21
22
23
24
25

#if 0 // print debugging info
#define DEBUG_PRINT (1)
#define WRITE_CODE (1)
#define DEBUG_printf DEBUG_printf
#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
#else // don't print debugging info
#define DEBUG_printf(...) (void)0
#define DEBUG_OP_printf(...) (void)0
#endif

26
27
28
29
#ifdef WRITE_CODE
FILE *fp_write_code = NULL;
#endif

30
void mp_emit_glue_init(void) {
31
32
33
#ifdef WRITE_CODE
    fp_write_code = fopen("out-code", "wb");
#endif
34
35
36
}

void mp_emit_glue_deinit(void) {
37
38
39
40
41
#ifdef WRITE_CODE
    if (fp_write_code != NULL) {
        fclose(fp_write_code);
    }
#endif
42
43
}

44
45
46
47
mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
    mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
    rc->kind = MP_CODE_RESERVED;
    return rc;
48
49
}

50
51
52
53
54
55
56
void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, int n_args, int n_locals, uint scope_flags, qstr *arg_names) {
    rc->kind = MP_CODE_BYTE;
    rc->scope_flags = scope_flags;
    rc->n_args = n_args;
    rc->u_byte.code = code;
    rc->u_byte.len = len;
    rc->arg_names = arg_names;
57
58

#ifdef DEBUG_PRINT
59
    DEBUG_printf("assign byte code: code=%p len=%u n_args=%d n_locals=%d\n", code, len, n_args, n_locals);
60
61
62
63
64
65
66
67
68
69
70
71
72
    for (int i = 0; i < 128 && i < len; i++) {
        if (i > 0 && i % 16 == 0) {
            DEBUG_printf("\n");
        }
        DEBUG_printf(" %02x", code[i]);
    }
    DEBUG_printf("\n");
#if MICROPY_DEBUG_PRINTERS
    mp_byte_code_print(code, len);
#endif
#endif
}

73
74
75
76
77
void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
    rc->kind = MP_CODE_NATIVE;
    rc->scope_flags = 0;
    rc->n_args = n_args;
    rc->u_native.fun = fun;
78
79

#ifdef DEBUG_PRINT
80
    DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
    for (int i = 0; i < 128 && i < len; i++) {
        if (i > 0 && i % 16 == 0) {
            DEBUG_printf("\n");
        }
        DEBUG_printf(" %02x", fun_data[i]);
    }
    DEBUG_printf("\n");

#ifdef WRITE_CODE
    if (fp_write_code != NULL) {
        fwrite(fun_data, len, 1, fp_write_code);
        fflush(fp_write_code);
    }
#endif
#endif
}

99
100
101
102
103
void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
    rc->kind = MP_CODE_INLINE_ASM;
    rc->scope_flags = 0;
    rc->n_args = n_args;
    rc->u_inline_asm.fun = fun;
104
105

#ifdef DEBUG_PRINT
106
    DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
107
108
109
110
111
112
113
114
115
116
117
118
    byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
    for (int i = 0; i < 128 && i < len; i++) {
        if (i > 0 && i % 16 == 0) {
            DEBUG_printf("\n");
        }
        DEBUG_printf(" %02x", fun_data[i]);
    }
    DEBUG_printf("\n");

#ifdef WRITE_CODE
    if (fp_write_code != NULL) {
        fwrite(fun_data, len, 1, fp_write_code);
119
        fflush(fp_write_code);
120
121
122
123
124
    }
#endif
#endif
}

125
126
127
mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
    DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
    assert(rc != NULL);
128

129
130
131
    // def_args must be MP_OBJ_NULL or a tuple
    assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));

132
133
134
    // TODO implement default kw args
    assert(def_kw_args == MP_OBJ_NULL);

135
    // make the function, depending on the raw code kind
136
    mp_obj_t fun;
137
    switch (rc->kind) {
138
        case MP_CODE_BYTE:
139
            fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_args, def_args, rc->u_byte.code);
140
141
            break;
        case MP_CODE_NATIVE:
142
            fun = mp_make_function_n(rc->n_args, rc->u_native.fun);
143
144
            break;
        case MP_CODE_INLINE_ASM:
145
            fun = mp_obj_new_fun_asm(rc->n_args, rc->u_inline_asm.fun);
146
147
            break;
        default:
148
            // raw code was never set (this should not happen)
149
            assert(0);
150
            return mp_const_none;
151
152
153
    }

    // check for generator functions and if so wrap in generator object
154
    if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
155
156
157
        fun = mp_obj_new_gen_wrap(fun);
    }

158
159
160
    return fun;
}

161
mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, uint n_closed_over, const mp_obj_t *args) {
162
    DEBUG_OP_printf("make_closure_from_raw_code %p %u %p\n", rc, n_closed_over, args);
163
    // make function object
164
165
166
167
168
169
170
171
    mp_obj_t ffun;
    if (n_closed_over & 0x100) {
        // default positional and keyword args given
        ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
    } else {
        // default positional and keyword args not given
        ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
    }
172
    // wrap function in closure object
173
    return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
174
}