evoasm 0.0.2.pre7 → 0.1.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/.gdbinit +41 -0
  3. data/.gitignore +1 -2
  4. data/.gitmodules +3 -0
  5. data/.rubocop.yml +8 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.md +660 -0
  8. data/Makefile +1 -1
  9. data/README.md +17 -9
  10. data/Rakefile +39 -107
  11. data/bin/gdb +1 -1
  12. data/bin/gdb_loop +4 -0
  13. data/docs/FindingInstructions.md +17 -0
  14. data/docs/JIT.md +14 -0
  15. data/docs/SymbolicRegression.md +102 -0
  16. data/docs/Visualization.md +29 -0
  17. data/docs/examples/bit_insts.rb +44 -0
  18. data/docs/examples/jit.rb +26 -0
  19. data/docs/examples/loss.gif +0 -0
  20. data/docs/examples/program.png +0 -0
  21. data/docs/examples/sym_reg.rb +64 -0
  22. data/docs/examples/vis.rb +38 -0
  23. data/evoasm.gemspec +21 -15
  24. data/ext/evoasm_ext/Rakefile +3 -0
  25. data/ext/evoasm_ext/compile.rake +35 -0
  26. data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.c +226 -0
  27. data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.h +84 -0
  28. data/ext/evoasm_ext/libevoasm/src/evoasm-arch.c +52 -0
  29. data/ext/evoasm_ext/libevoasm/src/evoasm-arch.h +101 -0
  30. data/ext/evoasm_ext/libevoasm/src/evoasm-bitmap.h +158 -0
  31. data/ext/evoasm_ext/libevoasm/src/evoasm-buf.c +204 -0
  32. data/ext/evoasm_ext/libevoasm/src/evoasm-buf.h +109 -0
  33. data/ext/evoasm_ext/libevoasm/src/evoasm-domain.c +124 -0
  34. data/ext/evoasm_ext/libevoasm/src/evoasm-domain.h +279 -0
  35. data/ext/evoasm_ext/libevoasm/src/evoasm-error.c +65 -0
  36. data/ext/evoasm_ext/libevoasm/src/evoasm-error.h +108 -0
  37. data/ext/evoasm_ext/{evoasm-log.c → libevoasm/src/evoasm-log.c} +36 -18
  38. data/ext/evoasm_ext/libevoasm/src/evoasm-log.h +93 -0
  39. data/ext/evoasm_ext/libevoasm/src/evoasm-param.c +22 -0
  40. data/ext/evoasm_ext/libevoasm/src/evoasm-param.h +33 -0
  41. data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.c +192 -0
  42. data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.h +60 -0
  43. data/ext/evoasm_ext/libevoasm/src/evoasm-pop.c +1323 -0
  44. data/ext/evoasm_ext/libevoasm/src/evoasm-pop.h +107 -0
  45. data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.c +116 -0
  46. data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.h +60 -0
  47. data/ext/evoasm_ext/libevoasm/src/evoasm-program.c +1827 -0
  48. data/ext/evoasm_ext/libevoasm/src/evoasm-program.h +167 -0
  49. data/ext/evoasm_ext/libevoasm/src/evoasm-rand.c +65 -0
  50. data/ext/evoasm_ext/libevoasm/src/evoasm-rand.h +76 -0
  51. data/ext/evoasm_ext/libevoasm/src/evoasm-signal.c +106 -0
  52. data/ext/evoasm_ext/libevoasm/src/evoasm-signal.h +58 -0
  53. data/ext/evoasm_ext/libevoasm/src/evoasm-util.h +112 -0
  54. data/ext/evoasm_ext/libevoasm/src/evoasm-x64.c +925 -0
  55. data/ext/evoasm_ext/libevoasm/src/evoasm-x64.h +277 -0
  56. data/ext/evoasm_ext/libevoasm/src/evoasm.c +28 -0
  57. data/ext/evoasm_ext/libevoasm/src/evoasm.h +35 -0
  58. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-enums.h +2077 -0
  59. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.c +191203 -0
  60. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.h +1713 -0
  61. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.c +348 -0
  62. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.h +93 -0
  63. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.c +51 -0
  64. data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.h +509 -0
  65. data/lib/evoasm.rb +28 -11
  66. data/lib/evoasm/buffer.rb +105 -0
  67. data/lib/evoasm/capstone.rb +100 -0
  68. data/lib/evoasm/domain.rb +116 -0
  69. data/lib/evoasm/error.rb +37 -16
  70. data/lib/evoasm/exception_error.rb +19 -0
  71. data/lib/evoasm/ffi_ext.rb +53 -0
  72. data/lib/evoasm/libevoasm.rb +286 -0
  73. data/lib/evoasm/libevoasm/x64_enums.rb +1967 -0
  74. data/lib/evoasm/parameter.rb +20 -0
  75. data/lib/evoasm/population.rb +145 -0
  76. data/lib/evoasm/population/parameters.rb +227 -0
  77. data/lib/evoasm/population/plotter.rb +89 -0
  78. data/lib/evoasm/prng.rb +64 -0
  79. data/lib/evoasm/program.rb +195 -12
  80. data/lib/evoasm/program/io.rb +144 -0
  81. data/lib/evoasm/test.rb +8 -0
  82. data/lib/evoasm/version.rb +1 -1
  83. data/lib/evoasm/x64.rb +115 -0
  84. data/lib/evoasm/x64/cpu_state.rb +95 -0
  85. data/lib/evoasm/x64/instruction.rb +109 -0
  86. data/lib/evoasm/x64/operand.rb +156 -0
  87. data/lib/evoasm/x64/parameters.rb +211 -0
  88. data/test/helpers/population_helper.rb +128 -0
  89. data/test/helpers/test_helper.rb +1 -0
  90. data/test/helpers/x64_helper.rb +24 -0
  91. data/test/integration/bitwise_reverse_test.rb +41 -0
  92. data/test/integration/gcd_test.rb +52 -0
  93. data/test/integration/popcnt_test.rb +46 -0
  94. data/test/integration/sym_reg_test.rb +68 -0
  95. data/test/unit/evoasm/buffer_test.rb +48 -0
  96. data/test/unit/evoasm/capstone_test.rb +18 -0
  97. data/test/unit/evoasm/domain_test.rb +55 -0
  98. data/test/unit/evoasm/population/parameters_test.rb +106 -0
  99. data/test/unit/evoasm/population_test.rb +96 -0
  100. data/test/unit/evoasm/prng_test.rb +47 -0
  101. data/test/unit/evoasm/x64/cpu_state_test.rb +73 -0
  102. data/test/unit/evoasm/x64/encoding_test.rb +320 -0
  103. data/test/unit/evoasm/x64/instruction_access_test.rb +177 -0
  104. data/test/unit/evoasm/x64/instruction_encoding_test.rb +780 -0
  105. data/test/unit/evoasm/x64/instruction_test.rb +62 -0
  106. data/test/unit/evoasm/x64/parameters_test.rb +65 -0
  107. data/test/unit/evoasm/x64_test.rb +52 -0
  108. metadata +195 -89
  109. data/Gemfile.rake +0 -8
  110. data/Gemfile.rake.lock +0 -51
  111. data/LICENSE.txt +0 -373
  112. data/data/tables/README.md +0 -19
  113. data/data/tables/x64.csv +0 -1684
  114. data/data/templates/evoasm-x64.c.erb +0 -319
  115. data/data/templates/evoasm-x64.h.erb +0 -126
  116. data/examples/abs.yml +0 -20
  117. data/examples/popcnt.yml +0 -17
  118. data/examples/sym_reg.yml +0 -26
  119. data/exe/evoasm-search +0 -13
  120. data/ext/evoasm_ext/evoasm-alloc.c +0 -145
  121. data/ext/evoasm_ext/evoasm-alloc.h +0 -59
  122. data/ext/evoasm_ext/evoasm-arch.c +0 -44
  123. data/ext/evoasm_ext/evoasm-arch.h +0 -161
  124. data/ext/evoasm_ext/evoasm-bitmap.h +0 -114
  125. data/ext/evoasm_ext/evoasm-buf.c +0 -130
  126. data/ext/evoasm_ext/evoasm-buf.h +0 -47
  127. data/ext/evoasm_ext/evoasm-error.c +0 -31
  128. data/ext/evoasm_ext/evoasm-error.h +0 -75
  129. data/ext/evoasm_ext/evoasm-free-list.c.tmpl +0 -121
  130. data/ext/evoasm_ext/evoasm-free-list.h.tmpl +0 -86
  131. data/ext/evoasm_ext/evoasm-log.h +0 -69
  132. data/ext/evoasm_ext/evoasm-misc.c +0 -23
  133. data/ext/evoasm_ext/evoasm-misc.h +0 -282
  134. data/ext/evoasm_ext/evoasm-param.h +0 -37
  135. data/ext/evoasm_ext/evoasm-search.c +0 -2145
  136. data/ext/evoasm_ext/evoasm-search.h +0 -214
  137. data/ext/evoasm_ext/evoasm-util.h +0 -40
  138. data/ext/evoasm_ext/evoasm-x64.c +0 -275624
  139. data/ext/evoasm_ext/evoasm-x64.h +0 -5436
  140. data/ext/evoasm_ext/evoasm.c +0 -7
  141. data/ext/evoasm_ext/evoasm.h +0 -23
  142. data/ext/evoasm_ext/evoasm_ext.c +0 -1757
  143. data/ext/evoasm_ext/extconf.rb +0 -31
  144. data/lib/evoasm/cli.rb +0 -6
  145. data/lib/evoasm/cli/search.rb +0 -127
  146. data/lib/evoasm/core_ext.rb +0 -1
  147. data/lib/evoasm/core_ext/array.rb +0 -9
  148. data/lib/evoasm/core_ext/integer.rb +0 -10
  149. data/lib/evoasm/core_ext/kwstruct.rb +0 -13
  150. data/lib/evoasm/core_ext/range.rb +0 -5
  151. data/lib/evoasm/examples.rb +0 -27
  152. data/lib/evoasm/gen.rb +0 -8
  153. data/lib/evoasm/gen/enum.rb +0 -169
  154. data/lib/evoasm/gen/name_util.rb +0 -80
  155. data/lib/evoasm/gen/state.rb +0 -176
  156. data/lib/evoasm/gen/state_dsl.rb +0 -152
  157. data/lib/evoasm/gen/strio.rb +0 -27
  158. data/lib/evoasm/gen/translator.rb +0 -1102
  159. data/lib/evoasm/gen/version.rb +0 -5
  160. data/lib/evoasm/gen/x64.rb +0 -237
  161. data/lib/evoasm/gen/x64/funcs.rb +0 -495
  162. data/lib/evoasm/gen/x64/inst.rb +0 -781
  163. data/lib/evoasm/search.rb +0 -40
  164. data/lib/evoasm/tasks/gen_task.rb +0 -86
  165. data/lib/evoasm/tasks/template_task.rb +0 -52
  166. data/test/test_helper.rb +0 -1
  167. data/test/x64/test_helper.rb +0 -19
  168. data/test/x64/x64_test.rb +0 -87
data/examples/popcnt.yml DELETED
@@ -1,17 +0,0 @@
1
- instructions:
2
- grep_v: 'popc'
3
- reg_types: [gp]
4
- examples:
5
- 0b0: 0
6
- 0b1: 1
7
- 0b110: 2
8
- 0b101: 2
9
- 0b111: 3
10
- 0b100: 1
11
- 0b101010: 3
12
- 0b1010: 2
13
- 0b10000: 1
14
- 0b100001: 2
15
- 0b101011: 4
16
- program_size: 20..30
17
- population_size: 1600
data/examples/sym_reg.yml DELETED
@@ -1,26 +0,0 @@
1
- instructions:
2
- reg_types: [xmm]
3
- grep: (add|mul|sqrt).*?sd
4
- # sqrt(x**3 + 2 * x)
5
- examples:
6
- 0.0: 0.0
7
- 0.5: 1.0606601717798212
8
- 1.0: 1.7320508075688772
9
- 1.5: 2.5248762345905194
10
- 2.0: 3.4641016151377544
11
- 2.5: 4.541475531146237
12
- 3.0: 5.744562646538029
13
- 3.5: 7.0622234459127675
14
- 4.0: 8.48528137423857
15
- 4.5: 10.00624804809475
16
- 5.0: 11.61895003862225
17
- kernel_size: 5..15
18
- program_size: 1
19
- population_size: 1600
20
- max_loss: 0.0
21
- parameters: [reg0, reg1, reg2, reg3]
22
- domains:
23
- reg0: &regs [XMM0, XMM1, XMM2, XMM3]
24
- reg1: *regs
25
- reg2: *regs
26
- reg3: *regs
data/exe/evoasm-search DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # filetype=ruby
3
-
4
- begin
5
- require 'evoasm'
6
- require 'evoasm/cli'
7
- rescue LoadError
8
- $LOAD_PATH.unshift File.expand_path File.join(__dir__, '../lib')
9
- retry
10
- end
11
-
12
- search = Evoasm::Cli::Search.new ARGV[0], ARGV[1..-1]
13
- search.start!
@@ -1,145 +0,0 @@
1
- #include "evoasm-alloc.h"
2
-
3
- #include <stdlib.h>
4
- #include <errno.h>
5
-
6
- void *
7
- evoasm_malloc(size_t size) {
8
- void *ptr = malloc(size);
9
- if(EVOASM_UNLIKELY(!ptr)) {
10
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
11
- NULL, "Allocationg %zu bytes via malloc failed", size);
12
- return NULL;
13
- }
14
- return ptr;
15
- }
16
-
17
- void *
18
- evoasm_calloc(size_t n, size_t size) {
19
- void *ptr = calloc(n, size);
20
-
21
- if(EVOASM_UNLIKELY(!ptr)) {
22
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
23
- NULL, "Allocationg %zux%zu () bytes via calloc failed", n, size, n * size);
24
- return NULL;
25
- }
26
- return ptr;
27
- }
28
-
29
- void *
30
- evoasm_realloc(void *ptr, size_t size) {
31
- void *new_ptr = realloc(ptr, size);
32
-
33
- if(EVOASM_UNLIKELY(!ptr)) {
34
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
35
- NULL, "Allocating %zu bytes via realloc failed", size);
36
- return NULL;
37
- }
38
- return new_ptr;
39
- }
40
-
41
- void
42
- evoasm_free(void *ptr) {
43
- free(ptr);
44
- }
45
-
46
- void *
47
- evoasm_mmap(size_t size, void *p) {
48
- /* Note that mmap considers the pointer passed soley as a hint address
49
- * and returns a valid address (possibly at a different address) in any case.
50
- * VirtualAlloc, on the other hand, will return NULL if the address is
51
- * not available
52
- */
53
- void *mem;
54
-
55
- #if defined(_WIN32)
56
- retry:
57
- mem = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE);
58
- if(mem == NULL) {
59
- if(p != NULL) {
60
- goto retry;
61
- } else {
62
- goto error;
63
- }
64
- }
65
- return mem;
66
- #elif defined(_POSIX_VERSION)
67
- mem = mmap(p, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
68
- if(mem == MAP_FAILED) {
69
- goto error;
70
- }
71
- #else
72
- #error
73
- #endif
74
- return mem;
75
-
76
- error:
77
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
78
- NULL, "Allocationg %zu bytes via mmap failed: %s", size, strerror(errno));
79
- return NULL;
80
- }
81
-
82
- evoasm_success
83
- evoasm_munmap(void *p, size_t size) {
84
- bool ret;
85
- #if defined(_WIN32)
86
- ret = VirtualFree(p, size, MEM_DECOMMIT);
87
- #elif defined(_POSIX_VERSION)
88
- ret = (munmap(p, size) == 0);
89
- #else
90
- # error
91
- #endif
92
-
93
- if(!ret) {
94
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
95
- NULL, "Unmapping %zu bytes via munmap failed: %s", size, strerror(errno));
96
- }
97
-
98
- return ret;
99
- }
100
-
101
- evoasm_success
102
- evoasm_mprot(void *p, size_t size, int mode)
103
- {
104
-
105
- #if defined(_WIN32)
106
- if(VirtualProtect(p, size, mode, NULL) != 0) {
107
- goto error;
108
- }
109
- #elif defined(_POSIX_VERSION)
110
- if(mprotect(p, size, mode) != 0) {
111
- goto error;
112
- }
113
- #else
114
- #error
115
- #endif
116
- return true;
117
-
118
- error:
119
- evoasm_set_error(EVOASM_ERROR_TYPE_MEMORY, EVOASM_ERROR_CODE_NONE,
120
- NULL, "Changing memory protection failed: %s", strerror(errno));
121
- return false;
122
- }
123
-
124
- static long _evoasm_page_size = -1;
125
-
126
- static long
127
- evoasm_query_page_size() {
128
- #if defined(_WIN32)
129
- SYSTEM_INFO si;
130
- GetSystemInfo(&si);
131
- return si.dwPageSize;
132
- #elif defined(_POSIX_VERSION)
133
- return sysconf(_SC_PAGESIZE);
134
- #else
135
- #error
136
- #endif
137
- }
138
-
139
- long
140
- evoasm_page_size() {
141
- if(_evoasm_page_size == -1) {
142
- _evoasm_page_size = evoasm_query_page_size();
143
- }
144
- return _evoasm_page_size;
145
- }
@@ -1,59 +0,0 @@
1
- #pragma once
2
-
3
- #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
4
- # if !defined(_DEFAULT_SOURCE)
5
- # define _DEFAULT_SOURCE
6
- # endif
7
- # include <unistd.h>
8
- # include <sys/mman.h>
9
- # if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
10
- # define MAP_ANONYMOUS MAP_ANON
11
- # endif
12
- #endif
13
-
14
- #include <string.h>
15
- #include <alloca.h>
16
-
17
- #if defined(_WIN32)
18
- # include <malloc.h>
19
- #endif
20
-
21
- #include "evoasm-error.h"
22
-
23
- #ifdef __GNUC__
24
- # define EVOASM_MALLOC_ATTRS __attribute__((malloc))
25
- # define EVOASM_CALLOC_ATTRS __attribute__((malloc))
26
- # define EVOASM_REALLOC_ATTRS __attribute__((malloc))
27
- #else
28
- # define EVOASM_MALLOC_ATTRS
29
- # define EVOASM_CALLOC_ATTRS
30
- # define EVOASM_REALLOC_ATTRS
31
- #endif
32
-
33
- #if defined(_WIN32)
34
- #define EVOASM_MPROT_RW PAGE_READWRITE
35
- #define EVOASM_MPROT_RX PAGE_EXECUTE_READ
36
- #define EVOASM_MPROT_RWX PAGE_EXECUTE_READWRITE
37
- #elif defined(_POSIX_VERSION)
38
- #define EVOASM_MPROT_RW (PROT_READ|PROT_WRITE)
39
- #define EVOASM_MPROT_RX (PROT_READ|PROT_EXEC)
40
- #define EVOASM_MPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC)
41
- #else
42
- #error
43
- #endif
44
-
45
- void *evoasm_malloc(size_t) EVOASM_MALLOC_ATTRS;
46
- void *evoasm_calloc(size_t, size_t) EVOASM_CALLOC_ATTRS;
47
- void *evoasm_realloc(void *, size_t) EVOASM_REALLOC_ATTRS;
48
- void evoasm_free(void *);
49
-
50
- void *evoasm_mmap(size_t size, void *p);
51
- evoasm_success evoasm_munmap(void *p, size_t size);
52
- evoasm_success evoasm_mprot(void *p, size_t size, int mode);
53
- long evoasm_page_size();
54
-
55
- #if defined(_WIN32)
56
- #define evoasm_alloca(s) _malloca(s);
57
- #else
58
- #define evoasm_alloca(s) alloca(s);
59
- #endif
@@ -1,44 +0,0 @@
1
- #include "evoasm-arch.h"
2
- #include "evoasm-util.h"
3
- #include <string.h>
4
- #include <inttypes.h>
5
-
6
- EVOASM_DECL_LOG_TAG("arch")
7
-
8
- evoasm_success
9
- evoasm_inst_encode(evoasm_inst *inst, evoasm_arch *arch, evoasm_arch_param_val *param_vals, evoasm_bitmap *set_params) {
10
- return inst->encode_func(arch, param_vals, set_params);
11
- }
12
-
13
- uint16_t
14
- evoasm_arch_insts(evoasm_arch *arch, const evoasm_inst **insts) {
15
- return arch->cls->insts_func(arch, insts);
16
- }
17
-
18
- void
19
- evoasm_arch_reset(evoasm_arch *arch) {
20
- arch->buf_start = EVOASM_ARCH_BUF_CAPA / 2;
21
- arch->buf_end = EVOASM_ARCH_BUF_CAPA / 2;
22
- }
23
-
24
- void
25
- evoasm_arch_init(evoasm_arch *arch, evoasm_arch_cls *cls) {
26
- static evoasm_arch zero_arch = {0};
27
- *arch = zero_arch;
28
- evoasm_arch_reset(arch);
29
- arch->cls = cls;
30
- }
31
-
32
- void
33
- evoasm_arch_destroy(evoasm_arch *arch) {
34
- }
35
-
36
- void
37
- evoasm_arch_save(evoasm_arch *arch, evoasm_buf *buf) {
38
- uint8_t len = (uint8_t)(arch->buf_end - arch->buf_start);
39
-
40
- memcpy(buf->data + buf->pos, arch->buf + arch->buf_start, len);
41
- buf->pos += len;
42
-
43
- evoasm_arch_reset(arch);
44
- }
@@ -1,161 +0,0 @@
1
- #pragma once
2
-
3
- #include <stdint.h>
4
- #include "evoasm-error.h"
5
- #include "evoasm-param.h"
6
- #include "evoasm-buf.h"
7
- #include "evoasm-log.h"
8
-
9
- #define EVOASM_ARCH_BUF_CAPA 32
10
- #define EVOASM_ARCH_MAX_PARAMS 64
11
-
12
- typedef uint8_t evoasm_reg_id;
13
- #define EVOASM_REG_ID_MAX UINT8_MAX
14
- typedef uint16_t evoasm_inst_id;
15
-
16
- typedef enum {
17
- EVOASM_OPERAND_SIZE_1,
18
- EVOASM_OPERAND_SIZE_8,
19
- EVOASM_OPERAND_SIZE_16,
20
- EVOASM_OPERAND_SIZE_32,
21
- EVOASM_OPERAND_SIZE_64,
22
- EVOASM_OPERAND_SIZE_128,
23
- EVOASM_OPERAND_SIZE_256,
24
- EVOASM_OPERAND_SIZE_512,
25
- EVOASM_N_OPERAND_SIZES,
26
- } evoasm_operand_size;
27
-
28
- #define EVOASM_OPERAND_SIZE_BITSIZE 3
29
- #define EVOASM_OPERAND_SIZE_BITSIZE_WITH_N 4
30
-
31
- struct evoasm_arch;
32
- struct evoasm_inst;
33
-
34
- typedef uint16_t (*evoasm_arch_insts_func)(struct evoasm_arch *arch, const struct evoasm_inst **insts);
35
-
36
- typedef enum {
37
- EVOASM_ARCH_X64
38
- } evoasm_arch_id;
39
-
40
- typedef struct {
41
- evoasm_arch_id id : 8;
42
- uint16_t n_insts;
43
- uint8_t n_params;
44
- uint8_t max_inst_len;
45
- evoasm_arch_insts_func insts_func;
46
- } evoasm_arch_cls;
47
-
48
- typedef enum {
49
- EVOASM_ARCH_ERROR_CODE_NOT_ENCODABLE = EVOASM_N_ERROR_CODES,
50
- EVOASM_ARCH_ERROR_CODE_MISSING_PARAM,
51
- EVOASM_ARCH_ERROR_CODE_INVALID_ACCESS,
52
- EVOASM_ARCH_ERROR_CODE_MISSING_FEATURE,
53
- } evoasm_arch_error_code;
54
-
55
- struct evoasm_arch;
56
-
57
- typedef struct {
58
- struct evoasm_arch *arch;
59
- uint8_t reg;
60
- uint8_t param;
61
- uint16_t inst;
62
- } evoasm_arch_error_data;
63
-
64
- _Static_assert(sizeof(evoasm_error_data) >= sizeof(evoasm_arch_error_data), "evoasm_arch_error_data exceeds evoasm_error_data size limit");
65
-
66
- typedef struct {
67
- EVOASM_ERROR_HEADER
68
- evoasm_arch_error_data data;
69
- } evoasm_arch_error;
70
-
71
- typedef struct evoasm_arch {
72
- evoasm_arch_cls *cls;
73
- uint8_t buf_end;
74
- uint8_t buf_start;
75
- uint8_t buf[EVOASM_ARCH_BUF_CAPA];
76
- void *user_data;
77
- /* must have a bit for every
78
- * writable register */
79
- evoasm_bitmap128 acc;
80
- } evoasm_arch;
81
-
82
- typedef bool (*evoasm_inst_encode_func)(evoasm_arch *arch, evoasm_arch_param_val *param_vals, evoasm_bitmap *set_params);
83
-
84
- typedef struct evoasm_inst {
85
- evoasm_inst_id id;
86
- uint16_t params_len;
87
- evoasm_arch_param *params;
88
- evoasm_inst_encode_func encode_func;
89
- } evoasm_inst;
90
-
91
- uint16_t
92
- evoasm_arch_insts(evoasm_arch *arch, const evoasm_inst **insts);
93
-
94
- evoasm_success
95
- evoasm_inst_encode(evoasm_inst *inst, evoasm_arch *arch, evoasm_arch_param_val *param_vals, evoasm_bitmap *set_params);
96
-
97
- void
98
- evoasm_arch_reset(evoasm_arch *arch);
99
-
100
- void
101
- evoasm_arch_init(evoasm_arch *arch, evoasm_arch_cls *cls);
102
-
103
- void
104
- evoasm_arch_destroy(evoasm_arch *arch);
105
-
106
- void
107
- evoasm_arch_save(evoasm_arch *arch, evoasm_buf *buf);
108
-
109
- static inline void
110
- evoasm_arch_write8(evoasm_arch *arch, int64_t datum) {
111
- uint8_t new_end = (uint8_t)(arch->buf_end + 1);
112
- *((uint8_t *)(arch->buf + arch->buf_end)) = (uint8_t) datum;
113
- arch->buf_end = new_end;
114
- }
115
-
116
- static inline void
117
- evoasm_arch_write16(evoasm_arch *arch, int64_t datum) {
118
- uint8_t new_end = (uint8_t)(arch->buf_end + 2);
119
- *((int16_t *)(arch->buf + arch->buf_end)) = (int16_t) datum;
120
- arch->buf_end = new_end;
121
- }
122
-
123
- static inline void
124
- evoasm_arch_write32(evoasm_arch *arch, int64_t datum) {
125
- uint8_t new_end = (uint8_t)(arch->buf_end + 4);
126
- *((int32_t *)(arch->buf + arch->buf_end)) = (int32_t) datum;
127
- arch->buf_end = new_end;
128
- }
129
-
130
- static inline void
131
- evoasm_arch_write64(evoasm_arch *arch, int64_t datum) {
132
- uint8_t new_end = (uint8_t)(arch->buf_end + 8);
133
- *((int64_t *)(arch->buf + arch->buf_end)) = (int64_t) datum;
134
- arch->buf_end = new_end;
135
- }
136
-
137
- static inline void
138
- evoasm_arch_write_access(evoasm_arch *arch, evoasm_bitmap *acc, evoasm_reg_id reg) {
139
- evoasm_bitmap_set(acc, (unsigned) reg);
140
- }
141
-
142
- static inline void
143
- evoasm_arch_undefined_access(evoasm_arch *arch, evoasm_bitmap *acc, evoasm_reg_id reg) {
144
- evoasm_bitmap_unset(acc, (unsigned) reg);
145
- }
146
-
147
- static inline evoasm_success
148
- _evoasm_arch_read_access(evoasm_arch *arch, evoasm_bitmap *acc, evoasm_reg_id reg, evoasm_inst_id inst, const char *file, unsigned line) {
149
- if(!evoasm_bitmap_get(acc, (unsigned) reg)) {
150
- evoasm_arch_error_data error_data = {
151
- .reg = (uint8_t) reg,
152
- .inst = (uint16_t) inst,
153
- .arch = arch
154
- };
155
- evoasm_set_error(EVOASM_ERROR_TYPE_ARCH, EVOASM_ARCH_ERROR_CODE_INVALID_ACCESS, &error_data, file, line, "read access violation");
156
- return false;
157
- }
158
- return true;
159
- }
160
-
161
- #define evoasm_arch_read_access(arch, acc, reg, inst) _evoasm_arch_read_access(arch, acc, reg, inst, __FILE__, __LINE__)