Numworks Epsilon  1.4.1
Graphing Calculator Operating System
asmbase.c
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) 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 
27 #include <assert.h>
28 #include <string.h>
29 
30 #include "py/obj.h"
31 #include "py/misc.h"
32 #include "py/asmbase.h"
33 
34 #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
35 
36 void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
37  as->max_num_labels = max_num_labels;
38  as->label_offsets = m_new(size_t, max_num_labels);
39 }
40 
41 void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code) {
42  if (free_code) {
44  }
45  m_del(size_t, as->label_offsets, as->max_num_labels);
46 }
47 
48 void mp_asm_base_start_pass(mp_asm_base_t *as, int pass) {
49  if (pass == MP_ASM_PASS_COMPUTE) {
50  // reset all labels
51  memset(as->label_offsets, -1, as->max_num_labels * sizeof(size_t));
52  } else if (pass == MP_ASM_PASS_EMIT) {
53  // allocating executable RAM is platform specific
54  MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
55  assert(as->code_base != NULL);
56  }
57  as->pass = pass;
58  as->code_offset = 0;
59 }
60 
61 // all functions must go through this one to emit bytes
62 // if as->pass < MP_ASM_PASS_EMIT, then this function just counts the number
63 // of bytes needed and returns NULL, and callers should not store any data
64 uint8_t *mp_asm_base_get_cur_to_write_bytes(mp_asm_base_t *as, size_t num_bytes_to_write) {
65  uint8_t *c = NULL;
66  if (as->pass == MP_ASM_PASS_EMIT) {
67  assert(as->code_offset + num_bytes_to_write <= as->code_size);
68  c = as->code_base + as->code_offset;
69  }
70  as->code_offset += num_bytes_to_write;
71  return c;
72 }
73 
74 void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label) {
75  assert(label < as->max_num_labels);
76  if (as->pass < MP_ASM_PASS_EMIT) {
77  // assign label offset
78  assert(as->label_offsets[label] == (size_t)-1);
79  as->label_offsets[label] = as->code_offset;
80  } else {
81  // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
82  assert(as->label_offsets[label] == as->code_offset);
83  }
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 
91 // this function assumes a little endian machine
92 void mp_asm_base_data(mp_asm_base_t* as, unsigned int bytesize, uintptr_t val) {
94  if (c != NULL) {
95  for (unsigned int i = 0; i < bytesize; i++) {
96  *c++ = val;
97  val >>= 8;
98  }
99  }
100 }
101 
102 #endif // MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
#define MP_ASM_PASS_COMPUTE
Definition: asmbase.h:32
uint8_t * mp_asm_base_get_cur_to_write_bytes(mp_asm_base_t *as, size_t num_bytes_to_write)
size_t code_size
Definition: asmbase.h:38
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
uint8_t * code_base
Definition: asmbase.h:39
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size)
Definition: mpconfig.h:1234
#define assert(e)
Definition: assert.h:9
size_t code_offset
Definition: asmbase.h:37
void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label)
#define m_del(type, ptr, num)
Definition: misc.h:77
size_t * label_offsets
Definition: asmbase.h:42
#define MP_PLAT_FREE_EXEC(ptr, size)
Definition: mpconfig.h:1238
unsigned int uintptr_t
Definition: stdint.h:14
unsigned char uint8_t
Definition: stdint.h:4
void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels)
#define MP_ASM_PASS_EMIT
Definition: asmbase.h:33
void mp_asm_base_align(mp_asm_base_t *as, unsigned int align)
void mp_asm_base_data(mp_asm_base_t *as, unsigned int bytesize, uintptr_t val)
c(generic_all_nodes)
void mp_asm_base_start_pass(mp_asm_base_t *as, int pass)
#define NULL
Definition: stddef.h:4
size_t max_num_labels
Definition: asmbase.h:41
void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code)
#define m_new(type, num)
Definition: misc.h:57