blob: 08f4ef0d4f42abef16e8d338fb2100472bfe2e7c [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovskyc0abc282014-03-22 13:49:31 +020027// Exception stack entry
28typedef struct _mp_exc_stack {
29 const byte *handler;
30 // bit 0 is saved currently_in_except_block value
Paul Sokolovsky16734202014-03-22 23:20:07 +020031 mp_obj_t *val_sp;
Paul Sokolovsky0c904df2014-03-30 00:48:21 +020032 // Saved exception, valid if currently_in_except_block bit is 1
33 mp_obj_t prev_exc;
Paul Sokolovskyc0abc282014-03-22 13:49:31 +020034 // We might only have 2 interesting cases here: SETUP_EXCEPT & SETUP_FINALLY,
35 // consider storing it in bit 1 of val_sp. TODO: SETUP_WITH?
36 byte opcode;
Damien George89f94b52014-03-30 00:57:09 +000037} mp_exc_stack_t;
Paul Sokolovskyc0abc282014-03-22 13:49:31 +020038
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +030039typedef struct _mp_code_state {
40 const byte *code_info;
41 const byte *ip;
42 mp_obj_t *sp;
43 // bit 0 is saved currently_in_except_block value
44 mp_exc_stack_t *exc_sp;
45 uint n_state;
46 // Variable-length
47 mp_obj_t state[0];
48 // Variable-length, never accessed by name, only as (void*)(state + n_state)
49 //mp_exc_stack_t exc_state[0];
50} mp_code_state;
51
Damien Georgeb534e1b2014-09-04 14:44:01 +010052mp_uint_t mp_decode_uint(const byte **ptr);
53
Damien Georgeaabd83e2014-06-07 14:16:08 +010054mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc);
Damien George3c658a42014-08-24 16:28:17 +010055void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
Damien George42f3de92014-10-03 17:44:14 +000056void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len);
57void mp_bytecode_print2(const byte *code, mp_uint_t len);
Paul Sokolovsky16734202014-03-22 23:20:07 +020058
59// Helper macros to access pointer with least significant bit holding a flag
Damien George40f3c022014-07-03 13:25:24 +010060#define MP_TAGPTR_PTR(x) ((void*)((mp_uint_t)(x) & ~((mp_uint_t)1)))
61#define MP_TAGPTR_TAG(x) ((mp_uint_t)(x) & 1)
62#define MP_TAGPTR_MAKE(ptr, tag) ((void*)((mp_uint_t)(ptr) | tag))