asmjit 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/Rakefile +5 -3
  4. data/asmjit.gemspec +1 -3
  5. data/ext/asmjit/asmjit/.editorconfig +10 -0
  6. data/ext/asmjit/asmjit/.github/FUNDING.yml +1 -0
  7. data/ext/asmjit/asmjit/.github/workflows/build-config.json +47 -0
  8. data/ext/asmjit/asmjit/.github/workflows/build.yml +156 -0
  9. data/ext/asmjit/asmjit/.gitignore +6 -0
  10. data/ext/asmjit/asmjit/CMakeLists.txt +611 -0
  11. data/ext/asmjit/asmjit/LICENSE.md +17 -0
  12. data/ext/asmjit/asmjit/README.md +69 -0
  13. data/ext/asmjit/asmjit/src/asmjit/a64.h +62 -0
  14. data/ext/asmjit/asmjit/src/asmjit/arm/a64archtraits_p.h +81 -0
  15. data/ext/asmjit/asmjit/src/asmjit/arm/a64assembler.cpp +5115 -0
  16. data/ext/asmjit/asmjit/src/asmjit/arm/a64assembler.h +72 -0
  17. data/ext/asmjit/asmjit/src/asmjit/arm/a64builder.cpp +51 -0
  18. data/ext/asmjit/asmjit/src/asmjit/arm/a64builder.h +57 -0
  19. data/ext/asmjit/asmjit/src/asmjit/arm/a64compiler.cpp +60 -0
  20. data/ext/asmjit/asmjit/src/asmjit/arm/a64compiler.h +247 -0
  21. data/ext/asmjit/asmjit/src/asmjit/arm/a64emithelper.cpp +464 -0
  22. data/ext/asmjit/asmjit/src/asmjit/arm/a64emithelper_p.h +50 -0
  23. data/ext/asmjit/asmjit/src/asmjit/arm/a64emitter.h +1228 -0
  24. data/ext/asmjit/asmjit/src/asmjit/arm/a64formatter.cpp +298 -0
  25. data/ext/asmjit/asmjit/src/asmjit/arm/a64formatter_p.h +59 -0
  26. data/ext/asmjit/asmjit/src/asmjit/arm/a64func.cpp +189 -0
  27. data/ext/asmjit/asmjit/src/asmjit/arm/a64func_p.h +33 -0
  28. data/ext/asmjit/asmjit/src/asmjit/arm/a64globals.h +1894 -0
  29. data/ext/asmjit/asmjit/src/asmjit/arm/a64instapi.cpp +278 -0
  30. data/ext/asmjit/asmjit/src/asmjit/arm/a64instapi_p.h +41 -0
  31. data/ext/asmjit/asmjit/src/asmjit/arm/a64instdb.cpp +1957 -0
  32. data/ext/asmjit/asmjit/src/asmjit/arm/a64instdb.h +74 -0
  33. data/ext/asmjit/asmjit/src/asmjit/arm/a64instdb_p.h +876 -0
  34. data/ext/asmjit/asmjit/src/asmjit/arm/a64operand.cpp +85 -0
  35. data/ext/asmjit/asmjit/src/asmjit/arm/a64operand.h +312 -0
  36. data/ext/asmjit/asmjit/src/asmjit/arm/a64rapass.cpp +852 -0
  37. data/ext/asmjit/asmjit/src/asmjit/arm/a64rapass_p.h +105 -0
  38. data/ext/asmjit/asmjit/src/asmjit/arm/a64utils.h +179 -0
  39. data/ext/asmjit/asmjit/src/asmjit/arm/armformatter.cpp +143 -0
  40. data/ext/asmjit/asmjit/src/asmjit/arm/armformatter_p.h +44 -0
  41. data/ext/asmjit/asmjit/src/asmjit/arm/armglobals.h +21 -0
  42. data/ext/asmjit/asmjit/src/asmjit/arm/armoperand.h +621 -0
  43. data/ext/asmjit/asmjit/src/asmjit/arm.h +62 -0
  44. data/ext/asmjit/asmjit/src/asmjit/asmjit-scope-begin.h +17 -0
  45. data/ext/asmjit/asmjit/src/asmjit/asmjit-scope-end.h +9 -0
  46. data/ext/asmjit/asmjit/src/asmjit/asmjit.h +33 -0
  47. data/ext/asmjit/asmjit/src/asmjit/core/api-build_p.h +55 -0
  48. data/ext/asmjit/asmjit/src/asmjit/core/api-config.h +613 -0
  49. data/ext/asmjit/asmjit/src/asmjit/core/archcommons.h +229 -0
  50. data/ext/asmjit/asmjit/src/asmjit/core/archtraits.cpp +160 -0
  51. data/ext/asmjit/asmjit/src/asmjit/core/archtraits.h +290 -0
  52. data/ext/asmjit/asmjit/src/asmjit/core/assembler.cpp +406 -0
  53. data/ext/asmjit/asmjit/src/asmjit/core/assembler.h +129 -0
  54. data/ext/asmjit/asmjit/src/asmjit/core/builder.cpp +889 -0
  55. data/ext/asmjit/asmjit/src/asmjit/core/builder.h +1391 -0
  56. data/ext/asmjit/asmjit/src/asmjit/core/codebuffer.h +113 -0
  57. data/ext/asmjit/asmjit/src/asmjit/core/codeholder.cpp +1149 -0
  58. data/ext/asmjit/asmjit/src/asmjit/core/codeholder.h +1035 -0
  59. data/ext/asmjit/asmjit/src/asmjit/core/codewriter.cpp +175 -0
  60. data/ext/asmjit/asmjit/src/asmjit/core/codewriter_p.h +179 -0
  61. data/ext/asmjit/asmjit/src/asmjit/core/compiler.cpp +582 -0
  62. data/ext/asmjit/asmjit/src/asmjit/core/compiler.h +737 -0
  63. data/ext/asmjit/asmjit/src/asmjit/core/compilerdefs.h +173 -0
  64. data/ext/asmjit/asmjit/src/asmjit/core/constpool.cpp +363 -0
  65. data/ext/asmjit/asmjit/src/asmjit/core/constpool.h +250 -0
  66. data/ext/asmjit/asmjit/src/asmjit/core/cpuinfo.cpp +1162 -0
  67. data/ext/asmjit/asmjit/src/asmjit/core/cpuinfo.h +813 -0
  68. data/ext/asmjit/asmjit/src/asmjit/core/emithelper.cpp +323 -0
  69. data/ext/asmjit/asmjit/src/asmjit/core/emithelper_p.h +58 -0
  70. data/ext/asmjit/asmjit/src/asmjit/core/emitter.cpp +333 -0
  71. data/ext/asmjit/asmjit/src/asmjit/core/emitter.h +741 -0
  72. data/ext/asmjit/asmjit/src/asmjit/core/emitterutils.cpp +129 -0
  73. data/ext/asmjit/asmjit/src/asmjit/core/emitterutils_p.h +89 -0
  74. data/ext/asmjit/asmjit/src/asmjit/core/environment.cpp +46 -0
  75. data/ext/asmjit/asmjit/src/asmjit/core/environment.h +508 -0
  76. data/ext/asmjit/asmjit/src/asmjit/core/errorhandler.cpp +14 -0
  77. data/ext/asmjit/asmjit/src/asmjit/core/errorhandler.h +228 -0
  78. data/ext/asmjit/asmjit/src/asmjit/core/formatter.cpp +584 -0
  79. data/ext/asmjit/asmjit/src/asmjit/core/formatter.h +247 -0
  80. data/ext/asmjit/asmjit/src/asmjit/core/formatter_p.h +34 -0
  81. data/ext/asmjit/asmjit/src/asmjit/core/func.cpp +286 -0
  82. data/ext/asmjit/asmjit/src/asmjit/core/func.h +1445 -0
  83. data/ext/asmjit/asmjit/src/asmjit/core/funcargscontext.cpp +293 -0
  84. data/ext/asmjit/asmjit/src/asmjit/core/funcargscontext_p.h +199 -0
  85. data/ext/asmjit/asmjit/src/asmjit/core/globals.cpp +133 -0
  86. data/ext/asmjit/asmjit/src/asmjit/core/globals.h +393 -0
  87. data/ext/asmjit/asmjit/src/asmjit/core/inst.cpp +113 -0
  88. data/ext/asmjit/asmjit/src/asmjit/core/inst.h +772 -0
  89. data/ext/asmjit/asmjit/src/asmjit/core/jitallocator.cpp +1242 -0
  90. data/ext/asmjit/asmjit/src/asmjit/core/jitallocator.h +261 -0
  91. data/ext/asmjit/asmjit/src/asmjit/core/jitruntime.cpp +80 -0
  92. data/ext/asmjit/asmjit/src/asmjit/core/jitruntime.h +89 -0
  93. data/ext/asmjit/asmjit/src/asmjit/core/logger.cpp +69 -0
  94. data/ext/asmjit/asmjit/src/asmjit/core/logger.h +198 -0
  95. data/ext/asmjit/asmjit/src/asmjit/core/misc_p.h +33 -0
  96. data/ext/asmjit/asmjit/src/asmjit/core/operand.cpp +132 -0
  97. data/ext/asmjit/asmjit/src/asmjit/core/operand.h +1611 -0
  98. data/ext/asmjit/asmjit/src/asmjit/core/osutils.cpp +84 -0
  99. data/ext/asmjit/asmjit/src/asmjit/core/osutils.h +61 -0
  100. data/ext/asmjit/asmjit/src/asmjit/core/osutils_p.h +68 -0
  101. data/ext/asmjit/asmjit/src/asmjit/core/raassignment_p.h +418 -0
  102. data/ext/asmjit/asmjit/src/asmjit/core/rabuilders_p.h +612 -0
  103. data/ext/asmjit/asmjit/src/asmjit/core/radefs_p.h +1204 -0
  104. data/ext/asmjit/asmjit/src/asmjit/core/ralocal.cpp +1166 -0
  105. data/ext/asmjit/asmjit/src/asmjit/core/ralocal_p.h +254 -0
  106. data/ext/asmjit/asmjit/src/asmjit/core/rapass.cpp +1969 -0
  107. data/ext/asmjit/asmjit/src/asmjit/core/rapass_p.h +1183 -0
  108. data/ext/asmjit/asmjit/src/asmjit/core/rastack.cpp +184 -0
  109. data/ext/asmjit/asmjit/src/asmjit/core/rastack_p.h +171 -0
  110. data/ext/asmjit/asmjit/src/asmjit/core/string.cpp +559 -0
  111. data/ext/asmjit/asmjit/src/asmjit/core/string.h +372 -0
  112. data/ext/asmjit/asmjit/src/asmjit/core/support.cpp +494 -0
  113. data/ext/asmjit/asmjit/src/asmjit/core/support.h +1773 -0
  114. data/ext/asmjit/asmjit/src/asmjit/core/target.cpp +14 -0
  115. data/ext/asmjit/asmjit/src/asmjit/core/target.h +53 -0
  116. data/ext/asmjit/asmjit/src/asmjit/core/type.cpp +74 -0
  117. data/ext/asmjit/asmjit/src/asmjit/core/type.h +419 -0
  118. data/ext/asmjit/asmjit/src/asmjit/core/virtmem.cpp +722 -0
  119. data/ext/asmjit/asmjit/src/asmjit/core/virtmem.h +242 -0
  120. data/ext/asmjit/asmjit/src/asmjit/core/zone.cpp +353 -0
  121. data/ext/asmjit/asmjit/src/asmjit/core/zone.h +615 -0
  122. data/ext/asmjit/asmjit/src/asmjit/core/zonehash.cpp +309 -0
  123. data/ext/asmjit/asmjit/src/asmjit/core/zonehash.h +186 -0
  124. data/ext/asmjit/asmjit/src/asmjit/core/zonelist.cpp +163 -0
  125. data/ext/asmjit/asmjit/src/asmjit/core/zonelist.h +209 -0
  126. data/ext/asmjit/asmjit/src/asmjit/core/zonestack.cpp +176 -0
  127. data/ext/asmjit/asmjit/src/asmjit/core/zonestack.h +239 -0
  128. data/ext/asmjit/asmjit/src/asmjit/core/zonestring.h +120 -0
  129. data/ext/asmjit/asmjit/src/asmjit/core/zonetree.cpp +99 -0
  130. data/ext/asmjit/asmjit/src/asmjit/core/zonetree.h +380 -0
  131. data/ext/asmjit/asmjit/src/asmjit/core/zonevector.cpp +356 -0
  132. data/ext/asmjit/asmjit/src/asmjit/core/zonevector.h +690 -0
  133. data/ext/asmjit/asmjit/src/asmjit/core.h +1861 -0
  134. data/ext/asmjit/asmjit/src/asmjit/x86/x86archtraits_p.h +148 -0
  135. data/ext/asmjit/asmjit/src/asmjit/x86/x86assembler.cpp +5110 -0
  136. data/ext/asmjit/asmjit/src/asmjit/x86/x86assembler.h +685 -0
  137. data/ext/asmjit/asmjit/src/asmjit/x86/x86builder.cpp +52 -0
  138. data/ext/asmjit/asmjit/src/asmjit/x86/x86builder.h +351 -0
  139. data/ext/asmjit/asmjit/src/asmjit/x86/x86compiler.cpp +61 -0
  140. data/ext/asmjit/asmjit/src/asmjit/x86/x86compiler.h +721 -0
  141. data/ext/asmjit/asmjit/src/asmjit/x86/x86emithelper.cpp +619 -0
  142. data/ext/asmjit/asmjit/src/asmjit/x86/x86emithelper_p.h +60 -0
  143. data/ext/asmjit/asmjit/src/asmjit/x86/x86emitter.h +4315 -0
  144. data/ext/asmjit/asmjit/src/asmjit/x86/x86formatter.cpp +944 -0
  145. data/ext/asmjit/asmjit/src/asmjit/x86/x86formatter_p.h +58 -0
  146. data/ext/asmjit/asmjit/src/asmjit/x86/x86func.cpp +503 -0
  147. data/ext/asmjit/asmjit/src/asmjit/x86/x86func_p.h +33 -0
  148. data/ext/asmjit/asmjit/src/asmjit/x86/x86globals.h +2169 -0
  149. data/ext/asmjit/asmjit/src/asmjit/x86/x86instapi.cpp +1732 -0
  150. data/ext/asmjit/asmjit/src/asmjit/x86/x86instapi_p.h +41 -0
  151. data/ext/asmjit/asmjit/src/asmjit/x86/x86instdb.cpp +4427 -0
  152. data/ext/asmjit/asmjit/src/asmjit/x86/x86instdb.h +563 -0
  153. data/ext/asmjit/asmjit/src/asmjit/x86/x86instdb_p.h +311 -0
  154. data/ext/asmjit/asmjit/src/asmjit/x86/x86opcode_p.h +436 -0
  155. data/ext/asmjit/asmjit/src/asmjit/x86/x86operand.cpp +231 -0
  156. data/ext/asmjit/asmjit/src/asmjit/x86/x86operand.h +1085 -0
  157. data/ext/asmjit/asmjit/src/asmjit/x86/x86rapass.cpp +1509 -0
  158. data/ext/asmjit/asmjit/src/asmjit/x86/x86rapass_p.h +94 -0
  159. data/ext/asmjit/asmjit/src/asmjit/x86.h +93 -0
  160. data/ext/asmjit/asmjit/src/asmjit.natvis +245 -0
  161. data/ext/asmjit/asmjit/test/asmjit_test_assembler.cpp +84 -0
  162. data/ext/asmjit/asmjit/test/asmjit_test_assembler.h +85 -0
  163. data/ext/asmjit/asmjit/test/asmjit_test_assembler_a64.cpp +4006 -0
  164. data/ext/asmjit/asmjit/test/asmjit_test_assembler_x64.cpp +17833 -0
  165. data/ext/asmjit/asmjit/test/asmjit_test_assembler_x86.cpp +8300 -0
  166. data/ext/asmjit/asmjit/test/asmjit_test_compiler.cpp +253 -0
  167. data/ext/asmjit/asmjit/test/asmjit_test_compiler.h +73 -0
  168. data/ext/asmjit/asmjit/test/asmjit_test_compiler_a64.cpp +690 -0
  169. data/ext/asmjit/asmjit/test/asmjit_test_compiler_x86.cpp +4317 -0
  170. data/ext/asmjit/asmjit/test/asmjit_test_emitters.cpp +197 -0
  171. data/ext/asmjit/asmjit/test/asmjit_test_instinfo.cpp +181 -0
  172. data/ext/asmjit/asmjit/test/asmjit_test_misc.h +257 -0
  173. data/ext/asmjit/asmjit/test/asmjit_test_perf.cpp +62 -0
  174. data/ext/asmjit/asmjit/test/asmjit_test_perf.h +61 -0
  175. data/ext/asmjit/asmjit/test/asmjit_test_perf_a64.cpp +699 -0
  176. data/ext/asmjit/asmjit/test/asmjit_test_perf_x86.cpp +5032 -0
  177. data/ext/asmjit/asmjit/test/asmjit_test_unit.cpp +172 -0
  178. data/ext/asmjit/asmjit/test/asmjit_test_x86_sections.cpp +172 -0
  179. data/ext/asmjit/asmjit/test/asmjitutils.h +38 -0
  180. data/ext/asmjit/asmjit/test/broken.cpp +312 -0
  181. data/ext/asmjit/asmjit/test/broken.h +148 -0
  182. data/ext/asmjit/asmjit/test/cmdline.h +61 -0
  183. data/ext/asmjit/asmjit/test/performancetimer.h +41 -0
  184. data/ext/asmjit/asmjit/tools/configure-makefiles.sh +13 -0
  185. data/ext/asmjit/asmjit/tools/configure-ninja.sh +13 -0
  186. data/ext/asmjit/asmjit/tools/configure-sanitizers.sh +13 -0
  187. data/ext/asmjit/asmjit/tools/configure-vs2019-x64.bat +2 -0
  188. data/ext/asmjit/asmjit/tools/configure-vs2019-x86.bat +2 -0
  189. data/ext/asmjit/asmjit/tools/configure-vs2022-x64.bat +2 -0
  190. data/ext/asmjit/asmjit/tools/configure-vs2022-x86.bat +2 -0
  191. data/ext/asmjit/asmjit/tools/configure-xcode.sh +8 -0
  192. data/ext/asmjit/asmjit/tools/enumgen.js +417 -0
  193. data/ext/asmjit/asmjit/tools/enumgen.sh +3 -0
  194. data/ext/asmjit/asmjit/tools/tablegen-arm.js +365 -0
  195. data/ext/asmjit/asmjit/tools/tablegen-arm.sh +3 -0
  196. data/ext/asmjit/asmjit/tools/tablegen-x86.js +2638 -0
  197. data/ext/asmjit/asmjit/tools/tablegen-x86.sh +3 -0
  198. data/ext/asmjit/asmjit/tools/tablegen.js +947 -0
  199. data/ext/asmjit/asmjit/tools/tablegen.sh +4 -0
  200. data/ext/asmjit/asmjit.cc +167 -30
  201. data/ext/asmjit/extconf.rb +9 -9
  202. data/lib/asmjit/version.rb +1 -1
  203. data/lib/asmjit.rb +14 -4
  204. metadata +198 -17
@@ -0,0 +1,417 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ const hasOwn = Object.prototype.hasOwnProperty;
7
+
8
+ // ============================================================================
9
+ // [Tokenizer]
10
+ // ============================================================================
11
+
12
+ // The list of "token types" which our lexer understands:
13
+ const tokenizerPatterns = [
14
+ { type: "space" , re: /^\s+/ },
15
+ { type: "comment" , re: /^(\/\/.*(\n|$)|\/\*.*\*\/)/ },
16
+ { type: "symbol" , re: /^[a-zA-Z_]\w*/ },
17
+ { type: "integer" , re: /^(-?\d+|0[x|X][0-9A-Fa-f]+)(l)?(l)?(u)?\b/ },
18
+ { type: "comma" , re: /^,/ },
19
+ { type: "operator", re: /(\+|\+\+|-|--|\/|\*|<<|>>|=|==|<|<=|>|>=|&|&&|\||\|\||\^|~|!)/ },
20
+ { type: "paren" , re: /^[\(\)\{\}\[\]]/ }
21
+ ];
22
+
23
+ function nextToken(input, from, patterns) {
24
+ if (from >= input.length) {
25
+ return {
26
+ type: "end",
27
+ begin: from,
28
+ end: from,
29
+ content: ""
30
+ }
31
+ }
32
+
33
+ const s = input.slice(from);
34
+ for (var i = 0; i < patterns.length; i++) {
35
+ const pattern = patterns[i];
36
+ const result = s.match(pattern.re);
37
+
38
+ if (result !== null) {
39
+ const content = result[0];
40
+ return {
41
+ type: pattern.type,
42
+ begin: from,
43
+ end: from + content.length,
44
+ content: content
45
+ };
46
+ }
47
+ }
48
+
49
+ return {
50
+ type: "invalid",
51
+ begin: from,
52
+ end: from + 1,
53
+ content: input[from]
54
+ };
55
+ }
56
+
57
+ class Tokenizer {
58
+ constructor(input, patterns) {
59
+ this.input = input;
60
+ this.index = 0;
61
+ this.patterns = patterns;
62
+ }
63
+
64
+ next() {
65
+ for (;;) {
66
+ const token = nextToken(this.input, this.index, this.patterns);
67
+ this.index = token.end;
68
+ if (token.type === "space" || token.type === "comment")
69
+ continue;
70
+ return token;
71
+ }
72
+ }
73
+
74
+ revert(token) {
75
+ this.index = token.begin;
76
+ }
77
+ }
78
+
79
+ // ============================================================================
80
+ // [Parser]
81
+ // ============================================================================
82
+
83
+ function parseEnum(input) {
84
+ const map = Object.create(null);
85
+ const hasOwn = Object.prototype.hasOwnProperty;
86
+ const tokenizer = new Tokenizer(input, tokenizerPatterns);
87
+
88
+ var value = -1;
89
+
90
+ for (;;) {
91
+ var token = tokenizer.next();
92
+ if (token.type === "end")
93
+ break;
94
+
95
+ if (token.type === "symbol") {
96
+ const symbol = token.content;
97
+ token = tokenizer.next();
98
+ if (token.content === "=") {
99
+ token = tokenizer.next();
100
+ if (token.type !== "integer")
101
+ throw Error(`Expected an integer after symbol '${symbol} = '`);
102
+ value = parseInt(token.content);
103
+ }
104
+ else {
105
+ value++;
106
+ }
107
+
108
+ if (!hasOwn.call(map, symbol))
109
+ map[symbol] = value;
110
+ else
111
+ console.log(`${symbol} already defined, skipping...`);
112
+
113
+ token = tokenizer.next();
114
+ if (token.type !== "comma")
115
+ tokenizer.revert(token);
116
+ continue;
117
+ }
118
+
119
+ throw Error(`Unexpected token ${token.type} (${token.content})`);
120
+ }
121
+
122
+ return map;
123
+ }
124
+
125
+ // ============================================================================
126
+ // [Stringify]
127
+ // ============================================================================
128
+
129
+ function compare(a, b) {
130
+ return a < b ? -1 : a == b ? 0 : 1;
131
+ }
132
+
133
+ function compactedSize(table) {
134
+ var size = 0;
135
+ for (var i = 0; i < table.length; i++)
136
+ size += table[i].name.length + 1;
137
+ return size;
138
+ }
139
+
140
+ function indexTypeFromSize(size) {
141
+ if (size <= 256)
142
+ return 'uint8_t';
143
+ else if (size <= 65536)
144
+ return 'uint16_t';
145
+ else
146
+ return 'uint32_t';
147
+ }
148
+
149
+ function indent(s, indentation) {
150
+ var lines = s.split(/\r?\n/g);
151
+ if (indentation) {
152
+ for (var i = 0; i < lines.length; i++) {
153
+ var line = lines[i];
154
+ if (line) lines[i] = indentation + line;
155
+ }
156
+ }
157
+
158
+ return lines.join("\n");
159
+ }
160
+
161
+ function stringifyEnum(map, options) {
162
+ var output = "";
163
+
164
+ const stripPrefix = options.strip;
165
+ const outputPrefix = options.output;
166
+
167
+ var max = -1;
168
+ var table = [];
169
+
170
+ for (var k in map) {
171
+ var name = k;
172
+ if (stripPrefix) {
173
+ if (name.startsWith(stripPrefix))
174
+ name = name.substring(stripPrefix.length);
175
+ else
176
+ throw Error(`Cannot strip prefix '${stripPrefix}' in '${key}'`);
177
+ }
178
+
179
+ table.push({ name: name, value: map[k] });
180
+ max = Math.max(max, map[k]);
181
+ }
182
+
183
+ table.sort(function(a, b) { return compare(a.value, b.value); });
184
+
185
+ const unknownIndex = compactedSize(table);
186
+ table.push({ name: "<Unknown>", value: max + 1 });
187
+
188
+ const indexType = indexTypeFromSize(compactedSize(table));
189
+
190
+ function buildStringData() {
191
+ var s = "";
192
+ for (var i = 0; i < table.length; i++) {
193
+ s += ` "${table[i].name}\\0"`;
194
+ if (i == table.length - 1)
195
+ s += `;`;
196
+ s += `\n`;
197
+ }
198
+ return s;
199
+ }
200
+
201
+ function buildIndexData() {
202
+ var index = 0;
203
+ var indexArray = [];
204
+
205
+ for (var i = 0; i < table.length; i++) {
206
+ while (indexArray.length < table[i].value)
207
+ indexArray.push(unknownIndex);
208
+
209
+ indexArray.push(index);
210
+ index += table[i].name.length + 1;
211
+ }
212
+
213
+ var s = "";
214
+ var line = "";
215
+ var pos = 0;
216
+
217
+ for (var i = 0; i < indexArray.length; i++) {
218
+ if (line)
219
+ line += " ";
220
+
221
+ line += `${indexArray[i]}`;
222
+ if (i != indexArray.length - 1)
223
+ line += `,`;
224
+
225
+ if (i == indexArray.length - 1 || line.length >= 72) {
226
+ s += ` ${line}\n`;
227
+ line = "";
228
+ }
229
+ }
230
+
231
+ return s;
232
+ }
233
+
234
+ output += `static const char ${outputPrefix}String[] =\n` + buildStringData() + `\n`;
235
+ output += `static const ${indexType} ${outputPrefix}Index[] = {\n` + buildIndexData() + `};\n`;
236
+
237
+ return output;
238
+ }
239
+
240
+ // ============================================================================
241
+ // [FileSystem]
242
+ // ============================================================================
243
+
244
+ function walkDir(baseDir) {
245
+ function walk(baseDir, nestedPath, out) {
246
+ fs.readdirSync(baseDir).forEach((file) => {
247
+ const stat = fs.statSync(path.join(baseDir, file));
248
+ if (stat.isDirectory()) {
249
+ if (!stat.isSymbolicLink())
250
+ walk(path.join(baseDir, file), path.join(nestedPath, file), out)
251
+ }
252
+ else {
253
+ out.push(path.join(nestedPath, file));
254
+ }
255
+ });
256
+ return out;
257
+ }
258
+
259
+ return walk(baseDir, "", []);
260
+ }
261
+
262
+ // ============================================================================
263
+ // [Generator]
264
+ // ============================================================================
265
+
266
+ class Generator {
267
+ constructor(options) {
268
+ this.enumMap = Object.create(null);
269
+ this.outputs = [];
270
+
271
+ this.verify = options.verify;
272
+ this.baseDir = options.baseDir;
273
+ this.noBackup = options.noBackup;
274
+ }
275
+
276
+ readEnums() {
277
+ console.log(`Scanning: ${this.baseDir}`);
278
+ walkDir(this.baseDir).forEach((fileName) => {
279
+ if (/\.(cc|cpp|h|hpp)$/.test(fileName)) {
280
+ const content = fs.readFileSync(path.join(this.baseDir, fileName), "utf8");
281
+ this.addEnumsFromSource(fileName, content);
282
+
283
+ if (/@EnumStringBegin(\{.*\})@/.test(content))
284
+ this.outputs.push(fileName);
285
+ }
286
+ });
287
+ }
288
+
289
+ writeEnums() {
290
+ this.outputs.forEach((fileName) => {
291
+ console.log(`Output: ${fileName}`);
292
+
293
+ const oldContent = fs.readFileSync(path.join(this.baseDir, fileName), "utf8");
294
+ const newContent = this.injectEnumsToSource(oldContent);
295
+
296
+ if (oldContent != newContent) {
297
+ if (this.verify) {
298
+ console.log(` FAILED: File is not up to date.`);
299
+ process.exit(1);
300
+ }
301
+ else {
302
+ if (!this.noBackup) {
303
+ fs.writeFileSync(path.join(this.baseDir, fileName + ".backup"), oldContent, "utf8");
304
+ console.log(` Created ${fileName}.backup`);
305
+ }
306
+ fs.writeFileSync(path.join(this.baseDir, fileName), newContent, "utf8");
307
+ console.log(` Updated ${fileName}`);
308
+ }
309
+ }
310
+ else {
311
+ console.log(` File is up to date.`);
312
+ }
313
+ });
314
+ }
315
+
316
+ addEnumsFromSource(fileName, src) {
317
+ var found = false;
318
+ const matches = [...src.matchAll(/(?:@EnumValuesBegin(\{.*\})@|@EnumValuesEnd@)/g)];
319
+
320
+ for (var i = 0; i < matches.length; i += 2) {
321
+ const def = matches[i];
322
+ const end = matches[i + 1];
323
+
324
+ if (!def[0].startsWith("@EnumValuesBegin"))
325
+ throw new Error(`Cannot start with '${def[0]}'`);
326
+
327
+ if (!end)
328
+ throw new Error(`Missing @EnumValuesEnd for '${def[0]}'`);
329
+
330
+ if (!end[0].startsWith("@EnumValuesEnd@"))
331
+ throw new Error(`Expected @EnumValuesEnd@ for '${def[0]}' and not '${end[0]}'`);
332
+
333
+ const options = JSON.parse(def[1]);
334
+ const enumName = options.enum;
335
+
336
+ if (!enumName)
337
+ throw Error(`Missing 'enum' in '${def[0]}`);
338
+
339
+ if (hasOwn.call(this.enumMap, enumName))
340
+ throw new Error(`Enumeration '${enumName}' is already defined`);
341
+
342
+ const startIndex = src.lastIndexOf("\n", def.index) + 1;
343
+ const endIndex = end.index + end[0].length;
344
+
345
+ if (startIndex === -1 || startIndex > endIndex)
346
+ throw new Error(`Internal Error, indexes have unexpected values: startIndex=${startIndex} endIndex=${endIndex}`);
347
+
348
+ if (!found) {
349
+ found = true;
350
+ console.log(`Found: ${fileName}`);
351
+ }
352
+
353
+ console.log(` Parsing Enum: ${enumName}`);
354
+ this.enumMap[enumName] = parseEnum(src.substring(startIndex, endIndex));
355
+ }
356
+ }
357
+
358
+ injectEnumsToSource(src) {
359
+ const matches = [...src.matchAll(/(?:@EnumStringBegin(\{.*\})@|@EnumStringEnd@)/g)];
360
+ var delta = 0;
361
+
362
+ for (var i = 0; i < matches.length; i += 2) {
363
+ const def = matches[i];
364
+ const end = matches[i + 1];
365
+
366
+ if (!def[0].startsWith("@EnumStringBegin"))
367
+ throw new Error(`Cannot start with '${def[0]}'`);
368
+
369
+ if (!end)
370
+ throw new Error(`Missing @EnumStringEnd@ for '${def[0]}'`);
371
+
372
+ if (!end[0].startsWith("@EnumStringEnd@"))
373
+ throw new Error(`Expected @EnumStringEnd@ for '${def[0]}' and not '${end[0]}'`);
374
+
375
+ const options = JSON.parse(def[1]);
376
+ const enumName = options.enum;
377
+
378
+ if (!enumName)
379
+ throwError(`Missing 'name' in '${def[0]}`);
380
+
381
+ if (!hasOwn.call(this.enumMap, enumName))
382
+ throw new Error(`Enumeration '${enumName}' not found`);
383
+
384
+ console.log(` Injecting Enum: ${enumName}`);
385
+
386
+ const startIndex = src.indexOf("\n", def.index + delta) + 1;
387
+ const endIndex = src.lastIndexOf("\n", end.index + delta) + 1;
388
+
389
+ if (startIndex === -1 || endIndex === -1 || startIndex > endIndex)
390
+ throw new Error(`Internal Error, indexes have unexpected values: startIndex=${startIndex} endIndex=${endIndex}`);
391
+
392
+ // Calculate the indentation.
393
+ const indentation = (function() {
394
+ const begin = src.lastIndexOf("\n", def.index + delta) + 1;
395
+ const end = src.indexOf("/", begin);
396
+ return src.substring(begin, end);
397
+ })();
398
+
399
+ const newContent = indent(stringifyEnum(this.enumMap[enumName], options), indentation);
400
+ src = src.substring(0, startIndex) + newContent + src.substring(endIndex);
401
+
402
+ delta -= endIndex - startIndex;
403
+ delta += newContent.length;
404
+ }
405
+
406
+ return src;
407
+ }
408
+ }
409
+
410
+ const generator = new Generator({
411
+ baseDir : path.resolve(__dirname, "../src"),
412
+ verify : process.argv.indexOf("--verify") !== -1,
413
+ noBackup: process.argv.indexOf("--no-backup") !== -1
414
+ });
415
+
416
+ generator.readEnums();
417
+ generator.writeEnums();
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env sh
2
+ set -e
3
+ node ./enumgen.js $@