emitcommon.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #if MICROPY_ENABLE_COMPILER
  29. void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst) {
  30. // name adding/lookup
  31. bool added;
  32. id_info_t *id = scope_find_or_add_id(scope, qst, &added);
  33. if (added) {
  34. scope_find_local_and_close_over(scope, id, qst);
  35. }
  36. }
  37. void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
  38. // name adding/lookup
  39. bool added;
  40. id_info_t *id = scope_find_or_add_id(scope, qst, &added);
  41. if (added) {
  42. if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
  43. id->kind = ID_INFO_KIND_LOCAL;
  44. } else {
  45. id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
  46. }
  47. } else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
  48. // rebind as a local variable
  49. id->kind = ID_INFO_KIND_LOCAL;
  50. }
  51. }
  52. 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) {
  53. // assumes pass is greater than 1, ie that all identifiers are defined in the scope
  54. id_info_t *id = scope_find(scope, qst);
  55. assert(id != NULL);
  56. // call the emit backend with the correct code
  57. if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
  58. emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_NAME);
  59. } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
  60. emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL);
  61. } else if (id->kind == ID_INFO_KIND_LOCAL) {
  62. emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_FAST);
  63. } else {
  64. assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE);
  65. emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_DEREF);
  66. }
  67. }
  68. #endif // MICROPY_ENABLE_COMPILER