asmbase.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 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 <string.h>
  28. #include "py/obj.h"
  29. #include "py/misc.h"
  30. #include "py/asmbase.h"
  31. #if MICROPY_EMIT_MACHINE_CODE
  32. void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
  33. as->max_num_labels = max_num_labels;
  34. as->label_offsets = m_new(size_t, max_num_labels);
  35. }
  36. void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code) {
  37. if (free_code) {
  38. MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
  39. }
  40. m_del(size_t, as->label_offsets, as->max_num_labels);
  41. }
  42. void mp_asm_base_start_pass(mp_asm_base_t *as, int pass) {
  43. if (pass < MP_ASM_PASS_EMIT) {
  44. // Reset labels so we can detect backwards jumps (and verify unique assignment)
  45. memset(as->label_offsets, -1, as->max_num_labels * sizeof(size_t));
  46. } else {
  47. // allocating executable RAM is platform specific
  48. MP_PLAT_ALLOC_EXEC(as->code_offset, (void **)&as->code_base, &as->code_size);
  49. assert(as->code_base != NULL);
  50. }
  51. as->pass = pass;
  52. as->suppress = false;
  53. as->code_offset = 0;
  54. }
  55. // all functions must go through this one to emit bytes
  56. // if as->pass < MP_ASM_PASS_EMIT, then this function just counts the number
  57. // of bytes needed and returns NULL, and callers should not store any data
  58. // It also returns NULL if generated code should be suppressed at this point.
  59. uint8_t *mp_asm_base_get_cur_to_write_bytes(void *as_in, size_t num_bytes_to_write) {
  60. mp_asm_base_t *as = as_in;
  61. uint8_t *c = NULL;
  62. if (as->suppress) {
  63. return c;
  64. }
  65. if (as->pass == MP_ASM_PASS_EMIT) {
  66. assert(as->code_offset + num_bytes_to_write <= as->code_size);
  67. c = as->code_base + as->code_offset;
  68. }
  69. as->code_offset += num_bytes_to_write;
  70. return c;
  71. }
  72. void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label) {
  73. assert(label < as->max_num_labels);
  74. // Assigning a label ends any dead-code region, and all following machine
  75. // code should be emitted (until another mp_asm_base_suppress_code() call).
  76. as->suppress = false;
  77. if (as->pass < MP_ASM_PASS_EMIT) {
  78. // assign label offset
  79. assert(as->label_offsets[label] == (size_t)-1);
  80. as->label_offsets[label] = as->code_offset;
  81. } else {
  82. // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
  83. assert(as->label_offsets[label] == as->code_offset);
  84. }
  85. }
  86. // align must be a multiple of 2
  87. void mp_asm_base_align(mp_asm_base_t *as, unsigned int align) {
  88. as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
  89. }
  90. // this function assumes a little endian machine
  91. void mp_asm_base_data(mp_asm_base_t *as, unsigned int bytesize, uintptr_t val) {
  92. uint8_t *c = mp_asm_base_get_cur_to_write_bytes(as, bytesize);
  93. if (c != NULL) {
  94. for (unsigned int i = 0; i < bytesize; i++) {
  95. *c++ = val;
  96. val >>= 8;
  97. }
  98. }
  99. }
  100. #endif // MICROPY_EMIT_MACHINE_CODE