bc.h 4.8 KB

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