blob: 8b1fd97021edfb58a6096b6fd7aad39134a52ea1 [file] [log] [blame]
Damien George2326d522014-03-27 23:26:35 +00001// This code glues the code emitters to the runtime.
2
Damien George440f0412014-03-28 18:38:20 +00003#include <stdio.h>
Damien Georged1e443d2014-03-29 11:39:36 +00004#include <string.h>
Damien George2326d522014-03-27 23:26:35 +00005#include <assert.h>
6
7#include "misc.h"
8#include "mpconfig.h"
9#include "qstr.h"
10#include "obj.h"
11#include "runtime0.h"
12#include "runtime.h"
13#include "emitglue.h"
Damien George440f0412014-03-28 18:38:20 +000014#include "bc.h"
Damien George2326d522014-03-27 23:26:35 +000015
16#if 0 // print debugging info
17#define DEBUG_PRINT (1)
18#define WRITE_CODE (1)
19#define DEBUG_printf DEBUG_printf
20#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
21#else // don't print debugging info
22#define DEBUG_printf(...) (void)0
23#define DEBUG_OP_printf(...) (void)0
24#endif
25
Damien George440f0412014-03-28 18:38:20 +000026#ifdef WRITE_CODE
27FILE *fp_write_code = NULL;
28#endif
29
Damien George2326d522014-03-27 23:26:35 +000030void mp_emit_glue_init(void) {
Damien George440f0412014-03-28 18:38:20 +000031#ifdef WRITE_CODE
32 fp_write_code = fopen("out-code", "wb");
33#endif
Damien George2326d522014-03-27 23:26:35 +000034}
35
36void mp_emit_glue_deinit(void) {
Damien George440f0412014-03-28 18:38:20 +000037#ifdef WRITE_CODE
38 if (fp_write_code != NULL) {
39 fclose(fp_write_code);
40 }
41#endif
Damien George2326d522014-03-27 23:26:35 +000042}
43
Damien Georgedf8127a2014-04-13 11:04:33 +010044mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
45 mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
46 rc->kind = MP_CODE_RESERVED;
47 return rc;
Damien George2326d522014-03-27 23:26:35 +000048}
49
Damien Georgedf8127a2014-04-13 11:04:33 +010050void 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) {
51 rc->kind = MP_CODE_BYTE;
52 rc->scope_flags = scope_flags;
53 rc->n_args = n_args;
54 rc->u_byte.code = code;
55 rc->u_byte.len = len;
56 rc->arg_names = arg_names;
Damien George2326d522014-03-27 23:26:35 +000057
58#ifdef DEBUG_PRINT
Damien Georgedf8127a2014-04-13 11:04:33 +010059 DEBUG_printf("assign byte code: code=%p len=%u n_args=%d n_locals=%d\n", code, len, n_args, n_locals);
Damien George2326d522014-03-27 23:26:35 +000060 for (int i = 0; i < 128 && i < len; i++) {
61 if (i > 0 && i % 16 == 0) {
62 DEBUG_printf("\n");
63 }
64 DEBUG_printf(" %02x", code[i]);
65 }
66 DEBUG_printf("\n");
67#if MICROPY_DEBUG_PRINTERS
68 mp_byte_code_print(code, len);
69#endif
70#endif
71}
72
Damien Georgedf8127a2014-04-13 11:04:33 +010073void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
74 rc->kind = MP_CODE_NATIVE;
75 rc->scope_flags = 0;
76 rc->n_args = n_args;
77 rc->u_native.fun = fun;
Damien George2326d522014-03-27 23:26:35 +000078
79#ifdef DEBUG_PRINT
Damien Georgedf8127a2014-04-13 11:04:33 +010080 DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
Damien George2326d522014-03-27 23:26:35 +000081 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
82 for (int i = 0; i < 128 && i < len; i++) {
83 if (i > 0 && i % 16 == 0) {
84 DEBUG_printf("\n");
85 }
86 DEBUG_printf(" %02x", fun_data[i]);
87 }
88 DEBUG_printf("\n");
89
90#ifdef WRITE_CODE
91 if (fp_write_code != NULL) {
92 fwrite(fun_data, len, 1, fp_write_code);
93 fflush(fp_write_code);
94 }
95#endif
96#endif
97}
98
Damien Georgedf8127a2014-04-13 11:04:33 +010099void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
100 rc->kind = MP_CODE_INLINE_ASM;
101 rc->scope_flags = 0;
102 rc->n_args = n_args;
103 rc->u_inline_asm.fun = fun;
Damien George2326d522014-03-27 23:26:35 +0000104
105#ifdef DEBUG_PRINT
Damien Georgedf8127a2014-04-13 11:04:33 +0100106 DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
Damien George2326d522014-03-27 23:26:35 +0000107 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
108 for (int i = 0; i < 128 && i < len; i++) {
109 if (i > 0 && i % 16 == 0) {
110 DEBUG_printf("\n");
111 }
112 DEBUG_printf(" %02x", fun_data[i]);
113 }
114 DEBUG_printf("\n");
115
116#ifdef WRITE_CODE
117 if (fp_write_code != NULL) {
118 fwrite(fun_data, len, 1, fp_write_code);
Damien Georgea26dc502014-04-12 17:54:52 +0100119 fflush(fp_write_code);
Damien George2326d522014-03-27 23:26:35 +0000120 }
121#endif
122#endif
123}
124
Damien Georgedf8127a2014-04-13 11:04:33 +0100125mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
126 DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
127 assert(rc != NULL);
Damien George2326d522014-03-27 23:26:35 +0000128
Damien George69b89d22014-04-11 13:38:30 +0000129 // def_args must be MP_OBJ_NULL or a tuple
130 assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
131
Damien Georgee337f1e2014-03-31 15:18:37 +0100132 // TODO implement default kw args
133 assert(def_kw_args == MP_OBJ_NULL);
134
Damien Georgedf8127a2014-04-13 11:04:33 +0100135 // make the function, depending on the raw code kind
Damien George2326d522014-03-27 23:26:35 +0000136 mp_obj_t fun;
Damien Georgedf8127a2014-04-13 11:04:33 +0100137 switch (rc->kind) {
Damien George2326d522014-03-27 23:26:35 +0000138 case MP_CODE_BYTE:
Damien Georgedf8127a2014-04-13 11:04:33 +0100139 fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_args, def_args, rc->u_byte.code);
Damien George2326d522014-03-27 23:26:35 +0000140 break;
141 case MP_CODE_NATIVE:
Damien Georgedf8127a2014-04-13 11:04:33 +0100142 fun = mp_make_function_n(rc->n_args, rc->u_native.fun);
Damien George2326d522014-03-27 23:26:35 +0000143 break;
144 case MP_CODE_INLINE_ASM:
Damien Georgedf8127a2014-04-13 11:04:33 +0100145 fun = mp_obj_new_fun_asm(rc->n_args, rc->u_inline_asm.fun);
Damien George2326d522014-03-27 23:26:35 +0000146 break;
147 default:
Damien Georgedf8127a2014-04-13 11:04:33 +0100148 // raw code was never set (this should not happen)
Damien George2326d522014-03-27 23:26:35 +0000149 assert(0);
Damien Georged1e443d2014-03-29 11:39:36 +0000150 return mp_const_none;
Damien George2326d522014-03-27 23:26:35 +0000151 }
152
153 // check for generator functions and if so wrap in generator object
Damien Georgedf8127a2014-04-13 11:04:33 +0100154 if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien George2326d522014-03-27 23:26:35 +0000155 fun = mp_obj_new_gen_wrap(fun);
156 }
157
158 return fun;
159}
160
Damien George3558f622014-04-20 17:50:40 +0100161mp_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, argrs);
Damien George2326d522014-03-27 23:26:35 +0000163 // make function object
Damien George3558f622014-04-20 17:50:40 +0100164 mp_obj_t ffun;
165 if (n_closed_over & 0x100) {
166 // default positional and keyword args given
167 ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
168 } else {
169 // default positional and keyword args not given
170 ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
171 }
Damien George2326d522014-03-27 23:26:35 +0000172 // wrap function in closure object
Damien George3558f622014-04-20 17:50:40 +0100173 return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
Damien George2326d522014-03-27 23:26:35 +0000174}