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.
- checksums.yaml +4 -4
- data/.gdbinit +41 -0
- data/.gitignore +1 -2
- data/.gitmodules +3 -0
- data/.rubocop.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.md +660 -0
- data/Makefile +1 -1
- data/README.md +17 -9
- data/Rakefile +39 -107
- data/bin/gdb +1 -1
- data/bin/gdb_loop +4 -0
- data/docs/FindingInstructions.md +17 -0
- data/docs/JIT.md +14 -0
- data/docs/SymbolicRegression.md +102 -0
- data/docs/Visualization.md +29 -0
- data/docs/examples/bit_insts.rb +44 -0
- data/docs/examples/jit.rb +26 -0
- data/docs/examples/loss.gif +0 -0
- data/docs/examples/program.png +0 -0
- data/docs/examples/sym_reg.rb +64 -0
- data/docs/examples/vis.rb +38 -0
- data/evoasm.gemspec +21 -15
- data/ext/evoasm_ext/Rakefile +3 -0
- data/ext/evoasm_ext/compile.rake +35 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.c +226 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-alloc.h +84 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-arch.c +52 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-arch.h +101 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-bitmap.h +158 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-buf.c +204 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-buf.h +109 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-domain.c +124 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-domain.h +279 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-error.c +65 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-error.h +108 -0
- data/ext/evoasm_ext/{evoasm-log.c → libevoasm/src/evoasm-log.c} +36 -18
- data/ext/evoasm_ext/libevoasm/src/evoasm-log.h +93 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-param.c +22 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-param.h +33 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.c +192 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop-params.h +60 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop.c +1323 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-pop.h +107 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.c +116 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program-io.h +60 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program.c +1827 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-program.h +167 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-rand.c +65 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-rand.h +76 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-signal.c +106 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-signal.h +58 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-util.h +112 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-x64.c +925 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm-x64.h +277 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm.c +28 -0
- data/ext/evoasm_ext/libevoasm/src/evoasm.h +35 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-enums.h +2077 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.c +191203 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-insts.h +1713 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.c +348 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-misc.h +93 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.c +51 -0
- data/ext/evoasm_ext/libevoasm/src/gen/evoasm-x64-params.h +509 -0
- data/lib/evoasm.rb +28 -11
- data/lib/evoasm/buffer.rb +105 -0
- data/lib/evoasm/capstone.rb +100 -0
- data/lib/evoasm/domain.rb +116 -0
- data/lib/evoasm/error.rb +37 -16
- data/lib/evoasm/exception_error.rb +19 -0
- data/lib/evoasm/ffi_ext.rb +53 -0
- data/lib/evoasm/libevoasm.rb +286 -0
- data/lib/evoasm/libevoasm/x64_enums.rb +1967 -0
- data/lib/evoasm/parameter.rb +20 -0
- data/lib/evoasm/population.rb +145 -0
- data/lib/evoasm/population/parameters.rb +227 -0
- data/lib/evoasm/population/plotter.rb +89 -0
- data/lib/evoasm/prng.rb +64 -0
- data/lib/evoasm/program.rb +195 -12
- data/lib/evoasm/program/io.rb +144 -0
- data/lib/evoasm/test.rb +8 -0
- data/lib/evoasm/version.rb +1 -1
- data/lib/evoasm/x64.rb +115 -0
- data/lib/evoasm/x64/cpu_state.rb +95 -0
- data/lib/evoasm/x64/instruction.rb +109 -0
- data/lib/evoasm/x64/operand.rb +156 -0
- data/lib/evoasm/x64/parameters.rb +211 -0
- data/test/helpers/population_helper.rb +128 -0
- data/test/helpers/test_helper.rb +1 -0
- data/test/helpers/x64_helper.rb +24 -0
- data/test/integration/bitwise_reverse_test.rb +41 -0
- data/test/integration/gcd_test.rb +52 -0
- data/test/integration/popcnt_test.rb +46 -0
- data/test/integration/sym_reg_test.rb +68 -0
- data/test/unit/evoasm/buffer_test.rb +48 -0
- data/test/unit/evoasm/capstone_test.rb +18 -0
- data/test/unit/evoasm/domain_test.rb +55 -0
- data/test/unit/evoasm/population/parameters_test.rb +106 -0
- data/test/unit/evoasm/population_test.rb +96 -0
- data/test/unit/evoasm/prng_test.rb +47 -0
- data/test/unit/evoasm/x64/cpu_state_test.rb +73 -0
- data/test/unit/evoasm/x64/encoding_test.rb +320 -0
- data/test/unit/evoasm/x64/instruction_access_test.rb +177 -0
- data/test/unit/evoasm/x64/instruction_encoding_test.rb +780 -0
- data/test/unit/evoasm/x64/instruction_test.rb +62 -0
- data/test/unit/evoasm/x64/parameters_test.rb +65 -0
- data/test/unit/evoasm/x64_test.rb +52 -0
- metadata +195 -89
- data/Gemfile.rake +0 -8
- data/Gemfile.rake.lock +0 -51
- data/LICENSE.txt +0 -373
- data/data/tables/README.md +0 -19
- data/data/tables/x64.csv +0 -1684
- data/data/templates/evoasm-x64.c.erb +0 -319
- data/data/templates/evoasm-x64.h.erb +0 -126
- data/examples/abs.yml +0 -20
- data/examples/popcnt.yml +0 -17
- data/examples/sym_reg.yml +0 -26
- data/exe/evoasm-search +0 -13
- data/ext/evoasm_ext/evoasm-alloc.c +0 -145
- data/ext/evoasm_ext/evoasm-alloc.h +0 -59
- data/ext/evoasm_ext/evoasm-arch.c +0 -44
- data/ext/evoasm_ext/evoasm-arch.h +0 -161
- data/ext/evoasm_ext/evoasm-bitmap.h +0 -114
- data/ext/evoasm_ext/evoasm-buf.c +0 -130
- data/ext/evoasm_ext/evoasm-buf.h +0 -47
- data/ext/evoasm_ext/evoasm-error.c +0 -31
- data/ext/evoasm_ext/evoasm-error.h +0 -75
- data/ext/evoasm_ext/evoasm-free-list.c.tmpl +0 -121
- data/ext/evoasm_ext/evoasm-free-list.h.tmpl +0 -86
- data/ext/evoasm_ext/evoasm-log.h +0 -69
- data/ext/evoasm_ext/evoasm-misc.c +0 -23
- data/ext/evoasm_ext/evoasm-misc.h +0 -282
- data/ext/evoasm_ext/evoasm-param.h +0 -37
- data/ext/evoasm_ext/evoasm-search.c +0 -2145
- data/ext/evoasm_ext/evoasm-search.h +0 -214
- data/ext/evoasm_ext/evoasm-util.h +0 -40
- data/ext/evoasm_ext/evoasm-x64.c +0 -275624
- data/ext/evoasm_ext/evoasm-x64.h +0 -5436
- data/ext/evoasm_ext/evoasm.c +0 -7
- data/ext/evoasm_ext/evoasm.h +0 -23
- data/ext/evoasm_ext/evoasm_ext.c +0 -1757
- data/ext/evoasm_ext/extconf.rb +0 -31
- data/lib/evoasm/cli.rb +0 -6
- data/lib/evoasm/cli/search.rb +0 -127
- data/lib/evoasm/core_ext.rb +0 -1
- data/lib/evoasm/core_ext/array.rb +0 -9
- data/lib/evoasm/core_ext/integer.rb +0 -10
- data/lib/evoasm/core_ext/kwstruct.rb +0 -13
- data/lib/evoasm/core_ext/range.rb +0 -5
- data/lib/evoasm/examples.rb +0 -27
- data/lib/evoasm/gen.rb +0 -8
- data/lib/evoasm/gen/enum.rb +0 -169
- data/lib/evoasm/gen/name_util.rb +0 -80
- data/lib/evoasm/gen/state.rb +0 -176
- data/lib/evoasm/gen/state_dsl.rb +0 -152
- data/lib/evoasm/gen/strio.rb +0 -27
- data/lib/evoasm/gen/translator.rb +0 -1102
- data/lib/evoasm/gen/version.rb +0 -5
- data/lib/evoasm/gen/x64.rb +0 -237
- data/lib/evoasm/gen/x64/funcs.rb +0 -495
- data/lib/evoasm/gen/x64/inst.rb +0 -781
- data/lib/evoasm/search.rb +0 -40
- data/lib/evoasm/tasks/gen_task.rb +0 -86
- data/lib/evoasm/tasks/template_task.rb +0 -52
- data/test/test_helper.rb +0 -1
- data/test/x64/test_helper.rb +0 -19
- data/test/x64/x64_test.rb +0 -87
data/ext/evoasm_ext/extconf.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'mkmf'
|
2
|
-
|
3
|
-
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
|
4
|
-
|
5
|
-
if have_header('capstone/capstone.h')
|
6
|
-
$LDFLAGS << ' -lcapstone'
|
7
|
-
end
|
8
|
-
|
9
|
-
$warnflags.gsub! '-Wdeclaration-after-statement', ''
|
10
|
-
|
11
|
-
$CFLAGS << ' -std=c11 -pedantic -fstrict-aliasing'
|
12
|
-
$warnflags << ' -Wextra -Wall -Wno-unused-label -Wuninitialized'\
|
13
|
-
' -Wswitch-default -Wstrict-aliasing=3 -Wunreachable-code'\
|
14
|
-
' -Wundef -Wpointer-arith -Wwrite-strings -Wconversion -Winit-self -Wno-unused-parameter'
|
15
|
-
|
16
|
-
$LDFLAGS << ''
|
17
|
-
|
18
|
-
if RbConfig::MAKEFILE_CONFIG['CC'] =~ /clang/
|
19
|
-
$warnflags << ' -Wno-unknown-warning-option -Wno-parentheses-equality -Wno-error=ignored-attributes'\
|
20
|
-
' -Wno-missing-field-initializers -Wno-missing-braces'
|
21
|
-
end
|
22
|
-
|
23
|
-
if enable_config('debug')
|
24
|
-
$warnflags << ' -Werror -Wno-error=unused-function -Wno-error=pedantic'\
|
25
|
-
' -Wno-error=implicit-function-declaration'
|
26
|
-
$defs.push('-DEVOASM_MIN_LOG_LEVEL=EVOASM_LOG_LEVEL_DEBUG')
|
27
|
-
$CFLAGS.gsub!(/-O\d/, '')
|
28
|
-
$CFLAGS << ' -O0 -g3 -fno-omit-frame-pointer'
|
29
|
-
end
|
30
|
-
|
31
|
-
create_makefile('evoasm_ext')
|
data/lib/evoasm/cli.rb
DELETED
data/lib/evoasm/cli/search.rb
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'pastel'
|
3
|
-
require 'pry'
|
4
|
-
|
5
|
-
module Evoasm
|
6
|
-
module Cli
|
7
|
-
class Search
|
8
|
-
attr_reader :filename
|
9
|
-
|
10
|
-
def initialize(filename, options)
|
11
|
-
@filename = filename
|
12
|
-
raise ArgumentError, 'filename is nil' if filename.nil?
|
13
|
-
|
14
|
-
if options.any?{|o| o =~ /\--log-level=(\d)/}
|
15
|
-
Evoasm.log_level = $1.to_i
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def start!
|
20
|
-
x64 = X64.new
|
21
|
-
params = YAML.load(File.read filename)
|
22
|
-
insts = filter_insts x64.instructions, params['instructions']
|
23
|
-
|
24
|
-
p insts.map(&:name)
|
25
|
-
pastel = Pastel.new
|
26
|
-
|
27
|
-
program_size = parse_range params['program_size']
|
28
|
-
kernel_size = parse_range params['kernel_size']
|
29
|
-
program_counter = 0
|
30
|
-
max_programs = params['max_programs']
|
31
|
-
recur_limit = params['recur_limit'] || 0
|
32
|
-
|
33
|
-
domains = convert_domains_hash params['domains']
|
34
|
-
parameters = (params['parameters'] || %i(reg0 reg1 reg2 imm0 imm1)).map(&:to_sym)
|
35
|
-
|
36
|
-
start_ts = Time.now
|
37
|
-
|
38
|
-
search = Evoasm::Search.new x64,
|
39
|
-
examples: params['examples'],
|
40
|
-
instructions: insts,
|
41
|
-
kernel_size: kernel_size,
|
42
|
-
program_size: program_size,
|
43
|
-
population_size: params['population_size'],
|
44
|
-
parameters: parameters,
|
45
|
-
domains: domains,
|
46
|
-
recur_limit: recur_limit
|
47
|
-
|
48
|
-
search.start!(params['max_loss'] || 0.0) do |program, loss|
|
49
|
-
ts = Time.now
|
50
|
-
puts pastel.bold "Program #{program_counter}, #{ts.strftime '%H:%M:%S'} (found after #{(ts - start_ts).to_i} seconds)"
|
51
|
-
|
52
|
-
if program.buffer.respond_to? :disassemble
|
53
|
-
puts program.buffer.disassemble.join "\n"
|
54
|
-
else
|
55
|
-
puts program.instructions.map(&:name)
|
56
|
-
end
|
57
|
-
|
58
|
-
puts
|
59
|
-
|
60
|
-
if params['console'] != false
|
61
|
-
binding.pry
|
62
|
-
end
|
63
|
-
|
64
|
-
program_counter += 1
|
65
|
-
|
66
|
-
if program_counter == max_programs
|
67
|
-
# stops search
|
68
|
-
return false
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
def filter_insts(insts, params)
|
75
|
-
op_types = %i(rm reg imm)
|
76
|
-
reg_types = %i(rflags)
|
77
|
-
bad_regs = %i(SP IP)
|
78
|
-
|
79
|
-
grep_regexp = params && Regexp.new(params['grep']) rescue nil
|
80
|
-
grep_v_regexp = params && Regexp.new(params['grep_v']) rescue nil
|
81
|
-
|
82
|
-
reg_types.concat params['reg_types'].map(&:to_sym)
|
83
|
-
|
84
|
-
insts.select do |inst|
|
85
|
-
next false if grep_regexp && inst.name !~ grep_regexp
|
86
|
-
next false if grep_v_regexp && inst.name =~ grep_v_regexp
|
87
|
-
next false if inst.operands.size == 0
|
88
|
-
|
89
|
-
inst.operands.all? do |op|
|
90
|
-
next false unless op_types.include? op.type
|
91
|
-
if op.register
|
92
|
-
next false unless reg_types.include?(op.register.type)
|
93
|
-
next false if bad_regs.include? op.register.name
|
94
|
-
end
|
95
|
-
|
96
|
-
true
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def convert_domains_hash(hash)
|
102
|
-
Hash(hash).map do |k, v|
|
103
|
-
new_k = k.to_sym
|
104
|
-
new_v =
|
105
|
-
case v
|
106
|
-
when Array
|
107
|
-
v.map {|e| e.is_a?(String) ? e.to_sym : e }
|
108
|
-
else
|
109
|
-
v
|
110
|
-
end
|
111
|
-
[new_k, new_v]
|
112
|
-
end.to_h
|
113
|
-
end
|
114
|
-
|
115
|
-
def parse_range(str)
|
116
|
-
case str
|
117
|
-
when Integer
|
118
|
-
str
|
119
|
-
when /^\(?(\d+)\.\.(\.?)(\d+)\)?$/
|
120
|
-
Range.new($1.to_i, $3.to_i, !$2.empty?)
|
121
|
-
else
|
122
|
-
raise ArgumentError, "invalid range '#{str}'"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
data/lib/evoasm/core_ext.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'evoasm/core_ext/range'
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# The MIT License (MIT)
|
2
|
-
# Copyright (c) 2015 Maxim Chernyak
|
3
|
-
class KwStruct < Struct
|
4
|
-
def self.new(*members, &block)
|
5
|
-
super.tap do |struct_class|
|
6
|
-
struct_class.class_eval <<-RUBY
|
7
|
-
def initialize(#{members.map { |m| "#{m}: nil" }.join(', ')})
|
8
|
-
super(#{members.join(', ')})
|
9
|
-
end
|
10
|
-
RUBY
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
data/lib/evoasm/examples.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Evoasm
|
2
|
-
module Examples
|
3
|
-
|
4
|
-
class << self
|
5
|
-
def convert(examples)
|
6
|
-
examples.inject(nil) do |(in_arity, out_arity), example|
|
7
|
-
inputs, outputs = example
|
8
|
-
example_in_arity = inputs.size
|
9
|
-
example_out_arity = outputs.size
|
10
|
-
|
11
|
-
validate_example inputs, example_in_arity, in_arity
|
12
|
-
validate_example outputs, example_out_arity, out_arity
|
13
|
-
|
14
|
-
[example_in_arity, example_out_arity]
|
15
|
-
end.unshift(examples.flatten)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
def validate_example(example, example_arity, arity)
|
20
|
-
if arity && arity != example_arity
|
21
|
-
raise ArgumentError, "invalid arity for example '#{example}'"\
|
22
|
-
" (#{example_arity} for #{arity})"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/evoasm/gen.rb
DELETED
data/lib/evoasm/gen/enum.rb
DELETED
@@ -1,169 +0,0 @@
|
|
1
|
-
require 'evoasm/gen/strio'
|
2
|
-
require 'evoasm/gen/name_util'
|
3
|
-
|
4
|
-
module Evoasm
|
5
|
-
module Gen
|
6
|
-
class Enum
|
7
|
-
include NameUtil
|
8
|
-
|
9
|
-
attr_reader :name, :flags
|
10
|
-
alias_method :flags?, :flags
|
11
|
-
|
12
|
-
def initialize(name = nil, elems = [], prefix: nil, flags: false)
|
13
|
-
@name = name
|
14
|
-
@prefix = prefix
|
15
|
-
@map = {}
|
16
|
-
@counter = 0
|
17
|
-
@flags = flags
|
18
|
-
add_all elems
|
19
|
-
end
|
20
|
-
|
21
|
-
def n
|
22
|
-
@counter
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_c(io = StrIO.new, typedef: true)
|
26
|
-
raise 'name missing' if !name
|
27
|
-
|
28
|
-
type_name = c_type_name
|
29
|
-
|
30
|
-
io.puts "#{typedef ? 'typedef ' : ''}enum #{type_name} {"
|
31
|
-
io.indent do
|
32
|
-
each do |elem, value|
|
33
|
-
elem_name = elem_name_to_c elem
|
34
|
-
c_value =
|
35
|
-
if valid_elem?(value)
|
36
|
-
elem_name_to_c value
|
37
|
-
else
|
38
|
-
if flags?
|
39
|
-
"1 << #{value}"
|
40
|
-
else
|
41
|
-
"#{value}"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
io.puts "#{elem_name} = #{c_value},"
|
45
|
-
end
|
46
|
-
if !flags?
|
47
|
-
io.puts n_elem_to_c
|
48
|
-
end
|
49
|
-
end
|
50
|
-
io.write '}'
|
51
|
-
io.write " #{type_name}" if typedef
|
52
|
-
io.puts ';'
|
53
|
-
io.puts "#define #{bitsize_to_c} #{bitsize}"
|
54
|
-
unless flags?
|
55
|
-
io.puts "#define #{bitsize_to_c true} #{bitsize true}"
|
56
|
-
else
|
57
|
-
io.puts "#define #{all_to_c} #{all_value}"
|
58
|
-
end
|
59
|
-
|
60
|
-
io.string
|
61
|
-
end
|
62
|
-
|
63
|
-
def bitsize(with_n = false)
|
64
|
-
if flags?
|
65
|
-
@map.size
|
66
|
-
else
|
67
|
-
Math.log2(max + 1 + (with_n ? 1 : 0)).ceil.to_i
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def max
|
72
|
-
@map.each_with_index.inject(0) do |acc, (index, (k, v))|
|
73
|
-
if v
|
74
|
-
[v + 1, acc + 1].max
|
75
|
-
else
|
76
|
-
acc + 1
|
77
|
-
end
|
78
|
-
end - 1
|
79
|
-
end
|
80
|
-
|
81
|
-
def c_type(typedef = false)
|
82
|
-
"#{typedef ? '' : 'enum '}#{c_type_name}"
|
83
|
-
end
|
84
|
-
|
85
|
-
def c_type_name
|
86
|
-
name_to_c name, @prefix
|
87
|
-
end
|
88
|
-
|
89
|
-
def all_to_c
|
90
|
-
name_to_c "#{prefix_name}_all", @prefix, const: true
|
91
|
-
end
|
92
|
-
|
93
|
-
def all_value
|
94
|
-
(2**@map.size) - 1
|
95
|
-
end
|
96
|
-
|
97
|
-
def bitsize_to_c(with_n = false)
|
98
|
-
name_to_c "#{prefix_name}_bitsize#{with_n ? '_WITH_N' : ''}", @prefix, const: true
|
99
|
-
end
|
100
|
-
|
101
|
-
def n_elem_to_c
|
102
|
-
name_to_c "n_#{prefix_name}s", @prefix, const: true
|
103
|
-
end
|
104
|
-
|
105
|
-
def keys
|
106
|
-
@map.keys
|
107
|
-
end
|
108
|
-
|
109
|
-
def add(elem, alias_elem = nil)
|
110
|
-
fail ArgumentError, 'can only add symbols or strings' \
|
111
|
-
unless valid_elem?(elem) && (!alias_elem || valid_elem?(alias_elem))
|
112
|
-
|
113
|
-
return if @map.key? elem
|
114
|
-
|
115
|
-
value = alias_elem || @counter
|
116
|
-
@counter += 1 if alias_elem.nil?
|
117
|
-
|
118
|
-
@map[elem] = value
|
119
|
-
end
|
120
|
-
|
121
|
-
def add_all(elems)
|
122
|
-
elems.each do |elem|
|
123
|
-
add elem
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def each(&block)
|
128
|
-
return to_enum(:each) if block.nil?
|
129
|
-
@map.each_key do |k|
|
130
|
-
block[k, self[k]]
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def alias(key)
|
135
|
-
key = @map[key]
|
136
|
-
case key
|
137
|
-
when Symbol, String
|
138
|
-
key
|
139
|
-
else
|
140
|
-
nil
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
def [](elem)
|
145
|
-
value = @map[elem]
|
146
|
-
|
147
|
-
if @map.key? value
|
148
|
-
@map.fetch value
|
149
|
-
else
|
150
|
-
value
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
private
|
155
|
-
def prefix_name
|
156
|
-
name.to_s.sub(/_id$/, '')
|
157
|
-
end
|
158
|
-
|
159
|
-
def elem_name_to_c(elem_name)
|
160
|
-
# convention: _id does not appear in element's name
|
161
|
-
name_to_c elem_name, Array(@prefix) + [prefix_name], const: true
|
162
|
-
end
|
163
|
-
|
164
|
-
def valid_elem?(elem)
|
165
|
-
elem.is_a?(Symbol) || elem.is_a?(String)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
data/lib/evoasm/gen/name_util.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
module Evoasm
|
2
|
-
module Gen
|
3
|
-
module NameUtil
|
4
|
-
def namespace
|
5
|
-
'evoasm'
|
6
|
-
end
|
7
|
-
|
8
|
-
def const_name_to_c(name, prefix)
|
9
|
-
name_to_c name, prefix, const: true
|
10
|
-
end
|
11
|
-
|
12
|
-
def name_to_c(name, prefix = nil, const: false)
|
13
|
-
c_name = [namespace, *prefix, name.to_s.sub(/\?$/, '')].compact.join '_'
|
14
|
-
if const
|
15
|
-
c_name.upcase
|
16
|
-
else
|
17
|
-
c_name
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def indep_arch_prefix(name = nil)
|
22
|
-
['arch', name]
|
23
|
-
end
|
24
|
-
|
25
|
-
def arch_prefix(name = nil)
|
26
|
-
[arch, name]
|
27
|
-
end
|
28
|
-
|
29
|
-
def error_code_to_c(name)
|
30
|
-
prefix = name == :ok ? :error_code : indep_arch_prefix(:error_code)
|
31
|
-
const_name_to_c name, prefix
|
32
|
-
end
|
33
|
-
|
34
|
-
def reg_name_to_c(name)
|
35
|
-
const_name_to_c name, arch_prefix(:reg)
|
36
|
-
end
|
37
|
-
|
38
|
-
def exception_to_c(name)
|
39
|
-
const_name_to_c name, arch_prefix(:exception)
|
40
|
-
end
|
41
|
-
|
42
|
-
def reg_type_to_c(name)
|
43
|
-
const_name_to_c name, arch_prefix(:reg_type)
|
44
|
-
end
|
45
|
-
|
46
|
-
def operand_type_to_c(name)
|
47
|
-
const_name_to_c name, arch_prefix(:operand_type)
|
48
|
-
end
|
49
|
-
|
50
|
-
def inst_name_to_c(inst)
|
51
|
-
const_name_to_c inst.name, arch_prefix(:inst)
|
52
|
-
end
|
53
|
-
|
54
|
-
def operand_size_to_c(size)
|
55
|
-
const_name_to_c size, :operand_size
|
56
|
-
end
|
57
|
-
|
58
|
-
def bit_mask_to_c(mask)
|
59
|
-
name =
|
60
|
-
case mask
|
61
|
-
when Range then"#{mask.min}_#{mask.max}"
|
62
|
-
else mask.to_s
|
63
|
-
end
|
64
|
-
const_name_to_c name, arch_prefix(:bit_mask)
|
65
|
-
end
|
66
|
-
|
67
|
-
def feature_name_to_c(name)
|
68
|
-
const_name_to_c name, arch_prefix(:feature)
|
69
|
-
end
|
70
|
-
|
71
|
-
def inst_flag_to_c(flag)
|
72
|
-
const_name_to_c flag, arch_prefix(:inst_flag)
|
73
|
-
end
|
74
|
-
|
75
|
-
def param_name_to_c(name)
|
76
|
-
const_name_to_c name, arch_prefix(:param)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|