emitcommon.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <assert.h>
  27. #include "py/emit.h"
  28. #include "py/nativeglue.h"
  29. #if MICROPY_ENABLE_COMPILER
  30. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  31. qstr_short_t mp_emit_common_use_qstr(mp_emit_common_t *emit, qstr qst) {
  32. mp_map_elem_t *elem = mp_map_lookup(&emit->qstr_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  33. if (elem->value == MP_OBJ_NULL) {
  34. elem->value = MP_OBJ_NEW_SMALL_INT(emit->qstr_map.used - 1);
  35. }
  36. return MP_OBJ_SMALL_INT_VALUE(elem->value);
  37. }
  38. #endif
  39. // Compare two objects for strict equality, including equality of type. This is
  40. // different to the semantics of mp_obj_equal which, eg, has (True,) == (1.0,).
  41. static bool strictly_equal(mp_obj_t a, mp_obj_t b) {
  42. if (a == b) {
  43. return true;
  44. }
  45. #if MICROPY_EMIT_NATIVE
  46. if (a == MP_OBJ_FROM_PTR(&mp_fun_table) || b == MP_OBJ_FROM_PTR(&mp_fun_table)) {
  47. return false;
  48. }
  49. #endif
  50. const mp_obj_type_t *a_type = mp_obj_get_type(a);
  51. const mp_obj_type_t *b_type = mp_obj_get_type(b);
  52. if (a_type != b_type) {
  53. return false;
  54. }
  55. if (a_type == &mp_type_tuple) {
  56. mp_obj_tuple_t *a_tuple = MP_OBJ_TO_PTR(a);
  57. mp_obj_tuple_t *b_tuple = MP_OBJ_TO_PTR(b);
  58. if (a_tuple->len != b_tuple->len) {
  59. return false;
  60. }
  61. for (size_t i = 0; i < a_tuple->len; ++i) {
  62. if (!strictly_equal(a_tuple->items[i], b_tuple->items[i])) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. } else {
  68. return mp_obj_equal(a, b);
  69. }
  70. }
  71. size_t mp_emit_common_use_const_obj(mp_emit_common_t *emit, mp_obj_t const_obj) {
  72. for (size_t i = 0; i < emit->const_obj_list.len; ++i) {
  73. if (strictly_equal(emit->const_obj_list.items[i], const_obj)) {
  74. return i;
  75. }
  76. }
  77. mp_obj_list_append(MP_OBJ_FROM_PTR(&emit->const_obj_list), const_obj);
  78. return emit->const_obj_list.len - 1;
  79. }
  80. id_info_t *mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
  81. // name adding/lookup
  82. id_info_t *id = scope_find_or_add_id(scope, qst, ID_INFO_KIND_GLOBAL_IMPLICIT);
  83. if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
  84. if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
  85. // rebind as a local variable
  86. id->kind = ID_INFO_KIND_LOCAL;
  87. } else {
  88. // mark this as assigned, to prevent it from being closed over
  89. id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT_ASSIGNED;
  90. }
  91. }
  92. return id;
  93. }
  94. void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst) {
  95. // assumes pass is greater than 1, ie that all identifiers are defined in the scope
  96. id_info_t *id = scope_find(scope, qst);
  97. assert(id != NULL);
  98. // call the emit backend with the correct code
  99. if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT_ASSIGNED) {
  100. emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_NAME);
  101. } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
  102. emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL);
  103. } else if (id->kind == ID_INFO_KIND_LOCAL) {
  104. emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_FAST);
  105. } else {
  106. assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE);
  107. emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_DEREF);
  108. }
  109. }
  110. #endif // MICROPY_ENABLE_COMPILER