1 /* VM library: native code patching, machine-independent part.
2
3 Copyright (C) 2017, 2019, 2020 Luca Saiu
4 Written by Luca Saiu
5
6 This file is part of Jitter.
7
8 Jitter is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Jitter is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Jitter. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 /* Ignore the rest of this file if the dispatch is not no-threading.
23 * ************************************************************************** */
24
25 #include <jitter/jitter.h>
26
27 /* If we are not using no-threading ignore the rest of this file. */
28 #if defined (JITTER_DISPATCH_NO_THREADING)
29
30
31
32
33 /* Include headers.
34 * ************************************************************************** */
35
36 #include <jitter/jitter-patch.h>
37 #include <string.h>
38 #include <limits.h>
39
40 #include <jitter/machine/jitter-machine.h>
41
42
43
44
45 /* Machine-independent convenience functions.
46 * ************************************************************************** */
47
48 enum jitter_snippet_to_patch
jitter_snippet_for_loading(const char * immediate_pointer,unsigned int residual_index,const char * loading_code_to_write)49 jitter_snippet_for_loading (const char *immediate_pointer,
50 unsigned int residual_index,
51 const char *loading_code_to_write)
52 {
53 if (residual_index < JITTER_RESIDUAL_REGISTER_NO)
54 return jitter_snippet_for_loading_register (immediate_pointer,
55 residual_index,
56 loading_code_to_write);
57 else
58 return jitter_snippet_for_loading_memory (immediate_pointer,
59 residual_index
60 - JITTER_RESIDUAL_REGISTER_NO,
61 loading_code_to_write);
62 }
63
64 void
jitter_patch_load_immediate(char * native_code,unsigned int residual_index,const char * immediate_pointer,enum jitter_snippet_to_patch snippet)65 jitter_patch_load_immediate (char *native_code,
66 unsigned int residual_index,
67 const char *immediate_pointer,
68 enum jitter_snippet_to_patch snippet)
69 {
70 size_t native_code_size = jitter_snippet_size (snippet);
71 if (residual_index < JITTER_RESIDUAL_REGISTER_NO)
72 jitter_patch_load_immediate_to_register (native_code, native_code_size,
73 immediate_pointer, snippet);
74 else
75 jitter_patch_load_immediate_to_memory (native_code, native_code_size,
76 residual_index
77 - JITTER_RESIDUAL_REGISTER_NO,
78 immediate_pointer,
79 snippet);
80 }
81
82 void
jitter_copy_snippet(char * to_native_code,enum jitter_snippet_to_patch snippet)83 jitter_copy_snippet (char *to_native_code, enum jitter_snippet_to_patch snippet)
84 {
85 size_t snippet_length = jitter_snippet_size (snippet);
86 const char *from_native_code = jitter_snippet_code (snippet);
87 memcpy (to_native_code, from_native_code, snippet_length);
88 }
89
90
91
92
93 /* Machine-independent convenience functions.
94 * ************************************************************************** */
95
96 const char*
jitter_snippet_code(enum jitter_snippet_to_patch snippet)97 jitter_snippet_code (enum jitter_snippet_to_patch snippet)
98 {
99 return jitter_native_snippet_pointers [snippet];
100 }
101
102 size_t
jitter_snippet_size(enum jitter_snippet_to_patch snippet)103 jitter_snippet_size (enum jitter_snippet_to_patch snippet)
104 {
105 return jitter_native_snippet_sizes [snippet];
106 }
107
108 const char*
jitter_snippet_name(enum jitter_snippet_to_patch snippet)109 jitter_snippet_name (enum jitter_snippet_to_patch snippet)
110 {
111 return jitter_native_snippet_names [snippet];
112 }
113
114
115 #endif // #if defined (JITTER_DISPATCH_NO_THREADING)
116