Numworks Epsilon  1.4.1
Graphing Calculator Operating System
bc.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 #ifndef MICROPY_INCLUDED_PY_BC_H
27 #define MICROPY_INCLUDED_PY_BC_H
28 
29 #include "py/runtime.h"
30 #include "py/objfun.h"
31 
32 // bytecode layout:
33 //
34 // n_state : var uint
35 // n_exc_stack : var uint
36 // scope_flags : byte
37 // n_pos_args : byte number of arguments this function takes
38 // n_kwonly_args : byte number of keyword-only arguments this function takes
39 // n_def_pos_args : byte number of default positional arguments
40 //
41 // code_info_size : var uint | code_info_size counts bytes in this chunk
42 // simple_name : var qstr |
43 // source_file : var qstr |
44 // <line number info> |
45 // <word alignment padding> | only needed if bytecode contains pointers
46 //
47 // local_num0 : byte |
48 // ... : byte |
49 // local_numN : byte | N = num_cells
50 // 255 : byte | end of list sentinel
51 // <bytecode> |
52 //
53 //
54 // constant table layout:
55 //
56 // argname0 : obj (qstr)
57 // ... : obj (qstr)
58 // argnameN : obj (qstr) N = num_pos_args + num_kwonly_args
59 // const0 : obj
60 // constN : obj
61 
62 // Exception stack entry
63 typedef struct _mp_exc_stack_t {
64  const byte *handler;
65  // bit 0 is saved currently_in_except_block value
66  // bit 1 is whether the opcode was SETUP_WITH or SETUP_FINALLY
68  // Saved exception, valid if currently_in_except_block bit is 1
71 
72 typedef struct _mp_code_state_t {
73  // The fun_bc entry points to the underlying function object that is being executed.
74  // It is needed to access the start of bytecode and the const_table.
75  // It is also needed to prevent the GC from reclaiming the bytecode during execution,
76  // because the ip pointer below will always point to the interior of the bytecode.
78  const byte *ip;
80  // bit 0 is saved currently_in_except_block value
83  #if MICROPY_STACKLESS
84  struct _mp_code_state_t *prev;
85  #endif
86  // Variable-length
88  // Variable-length, never accessed by name, only as (void*)(state + n_state)
89  //mp_exc_stack_t exc_state[0];
91 
92 mp_uint_t mp_decode_uint(const byte **ptr);
94 const byte *mp_decode_uint_skip(const byte *ptr);
95 
96 mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc);
97 mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args);
98 void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
99 void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table);
100 void mp_bytecode_print2(const byte *code, size_t len, const mp_uint_t *const_table);
101 const byte *mp_bytecode_print_str(const byte *ip);
102 #define mp_bytecode_print_inst(code, const_table) mp_bytecode_print2(code, 1, const_table)
103 
104 // Helper macros to access pointer with least significant bits holding flags
105 #define MP_TAGPTR_PTR(x) ((void*)((uintptr_t)(x) & ~((uintptr_t)3)))
106 #define MP_TAGPTR_TAG0(x) ((uintptr_t)(x) & 1)
107 #define MP_TAGPTR_TAG1(x) ((uintptr_t)(x) & 2)
108 #define MP_TAGPTR_MAKE(ptr, tag) ((void*)((uintptr_t)(ptr) | (tag)))
109 
110 #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
111 
112 #define MP_OPCODE_BYTE (0)
113 #define MP_OPCODE_QSTR (1)
114 #define MP_OPCODE_VAR_UINT (2)
115 #define MP_OPCODE_OFFSET (3)
116 
117 uint mp_opcode_format(const byte *ip, size_t *opcode_size);
118 
119 #endif
120 
121 #endif // MICROPY_INCLUDED_PY_BC_H
mp_obj_dict_t * old_globals
Definition: bc.h:82
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
const byte * mp_decode_uint_skip(const byte *ptr)
Definition: bc.c:67
const byte * ip
Definition: bc.h:78
struct _mp_code_state_t mp_code_state_t
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc)
Definition: vm.c:127
mp_obj_t state[0]
Definition: bc.h:87
const byte * mp_bytecode_print_str(const byte *ip)
void mp_bytecode_print2(const byte *code, size_t len, const mp_uint_t *const_table)
mp_exc_stack_t * exc_sp
Definition: bc.h:81
mp_obj_fun_bc_t * fun_bc
Definition: bc.h:77
mp_uint_t mp_decode_uint(const byte **ptr)
Definition: bc.c:43
mp_obj_base_t * prev_exc
Definition: bc.h:69
mp_vm_return_kind_t
Definition: runtime.h:31
const byte * handler
Definition: bc.h:64
args
Definition: i18n.py:175
mp_obj_t * val_sp
Definition: bc.h:67
mp_obj_t * sp
Definition: bc.h:79
void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table)
unsigned char byte
Definition: misc.h:37
mp_code_state_t * mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args)
void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: bc.c:108
uint64_t mp_obj_t
Definition: obj.h:39
mp_uint_t mp_decode_uint_value(const byte *ptr)
Definition: bc.c:61
struct _mp_exc_stack_t mp_exc_stack_t
unsigned int uint
Definition: misc.h:38