metasm 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.hgtags +3 -0
  4. data/Gemfile +1 -0
  5. data/INSTALL +61 -0
  6. data/LICENCE +458 -0
  7. data/README +29 -21
  8. data/Rakefile +10 -0
  9. data/TODO +10 -12
  10. data/doc/code_organisation.txt +2 -0
  11. data/doc/core/DynLdr.txt +247 -0
  12. data/doc/core/ExeFormat.txt +43 -0
  13. data/doc/core/Expression.txt +220 -0
  14. data/doc/core/GNUExports.txt +27 -0
  15. data/doc/core/Ia32.txt +236 -0
  16. data/doc/core/SerialStruct.txt +108 -0
  17. data/doc/core/VirtualString.txt +145 -0
  18. data/doc/core/WindowsExports.txt +61 -0
  19. data/doc/core/index.txt +1 -0
  20. data/doc/style.css +6 -3
  21. data/doc/usage/debugger.txt +327 -0
  22. data/doc/usage/index.txt +1 -0
  23. data/doc/use_cases.txt +2 -2
  24. data/metasm.gemspec +22 -0
  25. data/{lib/metasm.rb → metasm.rb} +11 -3
  26. data/{lib/metasm → metasm}/compile_c.rb +13 -7
  27. data/metasm/cpu/arc.rb +8 -0
  28. data/metasm/cpu/arc/decode.rb +425 -0
  29. data/metasm/cpu/arc/main.rb +191 -0
  30. data/metasm/cpu/arc/opcodes.rb +588 -0
  31. data/{lib/metasm → metasm/cpu}/arm.rb +7 -5
  32. data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
  33. data/{lib/metasm → metasm/cpu}/arm/decode.rb +13 -12
  34. data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
  35. data/{lib/metasm → metasm/cpu}/arm/main.rb +0 -3
  36. data/metasm/cpu/arm/opcodes.rb +324 -0
  37. data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
  38. data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
  39. data/metasm/cpu/arm64.rb +15 -0
  40. data/metasm/cpu/arm64/debug.rb +38 -0
  41. data/metasm/cpu/arm64/decode.rb +289 -0
  42. data/metasm/cpu/arm64/encode.rb +41 -0
  43. data/metasm/cpu/arm64/main.rb +105 -0
  44. data/metasm/cpu/arm64/opcodes.rb +232 -0
  45. data/metasm/cpu/arm64/parse.rb +20 -0
  46. data/metasm/cpu/arm64/render.rb +95 -0
  47. data/{lib/metasm/ppc.rb → metasm/cpu/bpf.rb} +2 -4
  48. data/metasm/cpu/bpf/decode.rb +142 -0
  49. data/metasm/cpu/bpf/main.rb +60 -0
  50. data/metasm/cpu/bpf/opcodes.rb +81 -0
  51. data/metasm/cpu/bpf/render.rb +41 -0
  52. data/metasm/cpu/cy16.rb +9 -0
  53. data/metasm/cpu/cy16/decode.rb +253 -0
  54. data/metasm/cpu/cy16/main.rb +63 -0
  55. data/metasm/cpu/cy16/opcodes.rb +78 -0
  56. data/metasm/cpu/cy16/render.rb +41 -0
  57. data/metasm/cpu/dalvik.rb +11 -0
  58. data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +35 -13
  59. data/{lib/metasm → metasm/cpu}/dalvik/main.rb +51 -2
  60. data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +19 -11
  61. data/metasm/cpu/ia32.rb +17 -0
  62. data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +5 -7
  63. data/{lib/metasm → metasm/cpu}/ia32/debug.rb +5 -5
  64. data/{lib/metasm → metasm/cpu}/ia32/decode.rb +246 -59
  65. data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +7 -7
  66. data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
  67. data/{lib/metasm → metasm/cpu}/ia32/main.rb +51 -8
  68. data/metasm/cpu/ia32/opcodes.rb +1424 -0
  69. data/{lib/metasm → metasm/cpu}/ia32/parse.rb +47 -16
  70. data/{lib/metasm → metasm/cpu}/ia32/render.rb +31 -4
  71. data/metasm/cpu/mips.rb +14 -0
  72. data/{lib/metasm → metasm/cpu}/mips/compile_c.rb +1 -1
  73. data/metasm/cpu/mips/debug.rb +42 -0
  74. data/{lib/metasm → metasm/cpu}/mips/decode.rb +46 -16
  75. data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
  76. data/{lib/metasm → metasm/cpu}/mips/main.rb +11 -4
  77. data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +86 -17
  78. data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
  79. data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
  80. data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
  81. data/metasm/cpu/msp430/decode.rb +247 -0
  82. data/metasm/cpu/msp430/main.rb +62 -0
  83. data/metasm/cpu/msp430/opcodes.rb +101 -0
  84. data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
  85. data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
  86. data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
  87. data/{lib/metasm/mips.rb → metasm/cpu/ppc.rb} +4 -4
  88. data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -12
  89. data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
  90. data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
  91. data/{lib/metasm → metasm/cpu}/ppc/main.rb +17 -12
  92. data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -5
  93. data/metasm/cpu/ppc/parse.rb +55 -0
  94. data/metasm/cpu/python.rb +8 -0
  95. data/metasm/cpu/python/decode.rb +136 -0
  96. data/metasm/cpu/python/main.rb +36 -0
  97. data/metasm/cpu/python/opcodes.rb +180 -0
  98. data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
  99. data/{lib/metasm → metasm/cpu}/sh4/decode.rb +48 -17
  100. data/{lib/metasm → metasm/cpu}/sh4/main.rb +13 -4
  101. data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
  102. data/metasm/cpu/x86_64.rb +15 -0
  103. data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +28 -17
  104. data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
  105. data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +57 -15
  106. data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +55 -26
  107. data/{lib/metasm → metasm/cpu}/x86_64/main.rb +14 -6
  108. data/metasm/cpu/x86_64/opcodes.rb +136 -0
  109. data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +10 -2
  110. data/metasm/cpu/x86_64/render.rb +35 -0
  111. data/metasm/cpu/z80.rb +9 -0
  112. data/metasm/cpu/z80/decode.rb +313 -0
  113. data/metasm/cpu/z80/main.rb +67 -0
  114. data/metasm/cpu/z80/opcodes.rb +224 -0
  115. data/metasm/cpu/z80/render.rb +59 -0
  116. data/{lib/metasm/os/main.rb → metasm/debug.rb} +160 -401
  117. data/{lib/metasm → metasm}/decode.rb +35 -4
  118. data/{lib/metasm → metasm}/decompile.rb +15 -16
  119. data/{lib/metasm → metasm}/disassemble.rb +201 -45
  120. data/{lib/metasm → metasm}/disassemble_api.rb +651 -87
  121. data/{lib/metasm → metasm}/dynldr.rb +220 -133
  122. data/{lib/metasm → metasm}/encode.rb +10 -1
  123. data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
  124. data/{lib/metasm → metasm}/exe_format/autoexe.rb +1 -0
  125. data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
  126. data/{lib/metasm → metasm}/exe_format/coff.rb +11 -3
  127. data/{lib/metasm → metasm}/exe_format/coff_decode.rb +53 -20
  128. data/{lib/metasm → metasm}/exe_format/coff_encode.rb +11 -13
  129. data/{lib/metasm → metasm}/exe_format/dex.rb +13 -5
  130. data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
  131. data/{lib/metasm → metasm}/exe_format/elf.rb +93 -57
  132. data/{lib/metasm → metasm}/exe_format/elf_decode.rb +143 -34
  133. data/{lib/metasm → metasm}/exe_format/elf_encode.rb +122 -31
  134. data/metasm/exe_format/gb.rb +65 -0
  135. data/metasm/exe_format/javaclass.rb +424 -0
  136. data/{lib/metasm → metasm}/exe_format/macho.rb +204 -16
  137. data/{lib/metasm → metasm}/exe_format/main.rb +26 -3
  138. data/{lib/metasm → metasm}/exe_format/mz.rb +1 -0
  139. data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
  140. data/{lib/metasm → metasm}/exe_format/pe.rb +71 -8
  141. data/metasm/exe_format/pyc.rb +167 -0
  142. data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
  143. data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
  144. data/metasm/exe_format/shellcode_rwx.rb +114 -0
  145. data/metasm/exe_format/swf.rb +205 -0
  146. data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
  147. data/metasm/exe_format/zip.rb +335 -0
  148. data/metasm/gui.rb +13 -0
  149. data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
  150. data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
  151. data/{lib/metasm → metasm}/gui/dasm_decomp.rb +7 -20
  152. data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
  153. data/metasm/gui/dasm_graph.rb +1695 -0
  154. data/{lib/metasm → metasm}/gui/dasm_hex.rb +12 -8
  155. data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
  156. data/{lib/metasm → metasm}/gui/dasm_main.rb +310 -53
  157. data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
  158. data/{lib/metasm → metasm}/gui/debug.rb +93 -27
  159. data/{lib/metasm → metasm}/gui/gtk.rb +162 -40
  160. data/{lib/metasm → metasm}/gui/qt.rb +12 -2
  161. data/{lib/metasm → metasm}/gui/win32.rb +179 -42
  162. data/{lib/metasm → metasm}/gui/x11.rb +59 -59
  163. data/{lib/metasm → metasm}/main.rb +389 -264
  164. data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
  165. data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
  166. data/{lib/metasm → metasm}/os/linux.rb +628 -151
  167. data/metasm/os/main.rb +330 -0
  168. data/{lib/metasm → metasm}/os/windows.rb +132 -42
  169. data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
  170. data/{lib/metasm → metasm}/parse.rb +26 -24
  171. data/{lib/metasm → metasm}/parse_c.rb +221 -116
  172. data/{lib/metasm → metasm}/preprocessor.rb +55 -40
  173. data/{lib/metasm → metasm}/render.rb +14 -38
  174. data/misc/hexdump.rb +2 -1
  175. data/misc/lint.rb +58 -0
  176. data/misc/txt2html.rb +9 -7
  177. data/samples/bindiff.rb +3 -4
  178. data/samples/dasm-plugins/bindiff.rb +15 -0
  179. data/samples/dasm-plugins/bookmark.rb +133 -0
  180. data/samples/dasm-plugins/c_constants.rb +57 -0
  181. data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
  182. data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
  183. data/samples/dasm-plugins/dasm_all.rb +70 -0
  184. data/samples/dasm-plugins/demangle_cpp.rb +31 -0
  185. data/samples/dasm-plugins/deobfuscate.rb +251 -0
  186. data/samples/dasm-plugins/dump_text.rb +35 -0
  187. data/samples/dasm-plugins/export_graph_svg.rb +86 -0
  188. data/samples/dasm-plugins/findgadget.rb +75 -0
  189. data/samples/dasm-plugins/hl_opcode.rb +32 -0
  190. data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
  191. data/samples/dasm-plugins/imm2off.rb +34 -0
  192. data/samples/dasm-plugins/match_libsigs.rb +93 -0
  193. data/samples/dasm-plugins/patch_file.rb +95 -0
  194. data/samples/dasm-plugins/scanfuncstart.rb +36 -0
  195. data/samples/dasm-plugins/scanxrefs.rb +26 -0
  196. data/samples/dasm-plugins/selfmodify.rb +197 -0
  197. data/samples/dasm-plugins/stringsxrefs.rb +28 -0
  198. data/samples/dasmnavig.rb +1 -1
  199. data/samples/dbg-apihook.rb +24 -9
  200. data/samples/dbg-plugins/heapscan.rb +283 -0
  201. data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
  202. data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
  203. data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
  204. data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
  205. data/samples/dbg-plugins/heapscan/winheap.h +174 -0
  206. data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
  207. data/samples/dbg-plugins/trace_func.rb +214 -0
  208. data/samples/disassemble-gui.rb +35 -5
  209. data/samples/disassemble.rb +31 -6
  210. data/samples/dump_upx.rb +24 -12
  211. data/samples/dynamic_ruby.rb +12 -3
  212. data/samples/exeencode.rb +6 -5
  213. data/samples/factorize-headers-peimports.rb +1 -1
  214. data/samples/lindebug.rb +175 -381
  215. data/samples/metasm-shell.rb +1 -2
  216. data/samples/peldr.rb +2 -2
  217. data/tests/all.rb +1 -1
  218. data/tests/arc.rb +26 -0
  219. data/tests/dynldr.rb +22 -4
  220. data/tests/expression.rb +55 -0
  221. data/tests/graph_layout.rb +285 -0
  222. data/tests/ia32.rb +79 -26
  223. data/tests/mips.rb +9 -2
  224. data/tests/x86_64.rb +66 -18
  225. metadata +330 -218
  226. data/lib/metasm/arm/opcodes.rb +0 -177
  227. data/lib/metasm/gui.rb +0 -23
  228. data/lib/metasm/gui/dasm_graph.rb +0 -1354
  229. data/lib/metasm/ia32.rb +0 -14
  230. data/lib/metasm/ia32/opcodes.rb +0 -873
  231. data/lib/metasm/ppc/parse.rb +0 -52
  232. data/lib/metasm/x86_64.rb +0 -12
  233. data/lib/metasm/x86_64/opcodes.rb +0 -118
  234. data/samples/gdbclient.rb +0 -583
  235. data/samples/rubstop.rb +0 -399
@@ -0,0 +1,191 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2010 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+ require 'metasm/main'
7
+
8
+ module Metasm
9
+ class ARC < CPU
10
+ def initialize(e = :little)
11
+ super()
12
+ @endianness = e
13
+ @size = 32
14
+ end
15
+
16
+ class Reg
17
+ include Renderable
18
+
19
+ attr_accessor :i
20
+
21
+ def initialize(i); @i = i end
22
+
23
+ def ==(o)
24
+ o.class == self.class and o.i == i
25
+ end
26
+ end
27
+
28
+ # general purpose reg
29
+ # Result R0-R1
30
+ # Arguments R0-R7
31
+ # Caller Saved Registers R0-R12
32
+ # Callee Saved Registers R13-R25
33
+ # Static chain pointer (if required) R11
34
+ # Register for temp calculation R12
35
+ # Global Pointer R26 (GP)
36
+ # Frame Pointer R27 (FP)
37
+ # Stack Pointer R28 (SP)
38
+ # Interrupt Link Register 1 R29 (ILINK1)
39
+ # Interrupt Link Register 2 R30 (ILINK2)
40
+ # Branch Link Register R31 (BLINK)
41
+ class GPR < Reg
42
+ Sym = (0..64).map { |i| "r#{i}".to_sym }
43
+ def symbolic; Sym[@i] end
44
+
45
+ Render = {
46
+ 26 => 'gp', # global pointer, used to point to small sets of shared data throughout execution of a program
47
+ 27 => 'fp', # frame pointer
48
+ 28 => 'sp', # stak pointer
49
+ 29 => 'ilink1', # maskable interrupt link register
50
+ 30 => 'ilink2', # maskable interrupt link register 2
51
+ 31 => 'blink', # branch link register
52
+ 60 => 'lp_count', # loop count register (24 bits)
53
+ # "When a destination register is set to r62 there is no destination for the result of the instruction so the
54
+ # result is discarded. Any flag updates will still occur according to the set flags directive (.F or implicit
55
+ # in the instruction)."
56
+ 62 => 'zero'
57
+ }
58
+
59
+ def render
60
+ if s = Render[i]
61
+ [s]
62
+ else
63
+ # r0-r28 general purpose registers
64
+ # r32-r59 reserved for extentions
65
+ ["r#@i"]
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ class AUX < Reg
72
+ def symbolic; "aux#{i}".to_sym end
73
+
74
+ Render = {
75
+ 0x00 => 'status', # Status register (Original ARCtangent-A4 processor format)
76
+ 0x01 => 'semaphore', # Inter-process/Host semaphore register
77
+ 0x02 => 'lp_start', # Loop start address (32-bit)
78
+ 0x03 => 'lp_end', # Loop end address (32-bit)
79
+ 0x04 => 'identity', # Processor Identification register
80
+ 0x05 => 'debug', # Debug register
81
+ 0x06 => 'pc', # PC register (32-bit)
82
+ 0x0A => 'status32', # Status register (32-bit)
83
+ 0x0B => 'status32_l1', # Status register save for level 1 interrupts
84
+ 0x0C => 'status32_l2', # Status register save for level 2 interrupts
85
+ 0x10 => 'ic_ivic', # Cache invalidate
86
+ 0x11 => 'ic_ctrl', # Mode bits for cache controller
87
+ 0x12 => 'mulhi', # High part of Multiply
88
+ 0x19 => 'ic_ivil',
89
+ 0x21 => 'timer0_cnt', # Processor Timer 0 Count value
90
+ 0x22 => 'timer0_ctrl', # Processor Timer 0 Control value
91
+ 0x23 => 'timer0_limit', # Processor Timer 0 Limit value
92
+ 0x25 => 'int_vector_base', # Interrupt Vector Base address
93
+ 0x40 => 'im_set_dc_ctrl',
94
+ 0x41 => 'aux_macmode', # Extended Arithmetic Status and Mode
95
+ 0x43 => 'aux_irq_lv12', # Interrupt Level Status
96
+ 0x47 => 'dc_ivdc', # Invalidate cache
97
+ 0x48 => 'dc_ctrl', # Cache control register
98
+ 0x49 => 'dc_ldl', # Lock data line
99
+ 0x4A => 'dc_ivdl', # Invalidate data line
100
+ 0x4B => 'dc_flsh', # Flush data cache
101
+ 0x4C => 'dc_fldl', # Flush data line
102
+ 0x58 => 'dc_ram_addr', # Access RAM address
103
+ 0x59 => 'dc_tag', # Tag Access
104
+ 0x5A => 'dc_wp', # Way Pointer Access
105
+ 0x5B => 'dc_data', # Data Access
106
+ 0x62 => 'crc_bcr',
107
+ 0x64 => 'dvfb_bcr',
108
+ 0x65 => 'extarith_bcr',
109
+ 0x68 => 'vecbase_bcr',
110
+ 0x69 => 'perbase_bcr',
111
+ 0x6f => 'mmu_bcr',
112
+ 0x72 => 'd_cache_build', # Build: Data Cache
113
+ 0x73 => 'madi_build', # Build: Multiple ARC Debug I/F
114
+ 0x74 => 'ldstram_build', # Build: LD/ST RAM
115
+ 0x75 => 'timer_build', # Build: Timer
116
+ 0x76 => 'ap_build', # Build: Actionpoints
117
+ 0x77 => 'i_cache_build', # Build: I-Cache
118
+ 0x78 => 'addsub_build', # Build: Saturated Add/Sub
119
+ 0x79 => 'dspram_build', # Build: Scratch RAM & XY Memory
120
+ 0x7B => 'multiply_build', # Build: Multiply
121
+ 0x7C => 'swap_build', # Build: Swap
122
+ 0x7D => 'norm_build', # Build: Normalise
123
+ 0x7E => 'minmax_build', # Build: Min/Max
124
+ 0x7F => 'barrel_build', # Build: Barrel Shift
125
+ 0x100 => 'timer1_cnt', # Processor Timer 1 Count value
126
+ 0x101 => 'timer1_ctrl', # Processor Timer 1 Control value
127
+ 0x102 => 'timer1_limit', # Processor Timer 1 Limit value
128
+ 0x200 => 'aux_irq_lev', # Interrupt Level Programming
129
+ 0x201 => 'aux_irq_hint', # Software Triggered Interrupt
130
+ 0x202 => 'aux_irq_mask', # Masked bits for Interrupts
131
+ 0x203 => 'aux_irq_base', # Interrupt Vector base address
132
+ 0x400 => 'eret', # Exception Return Address
133
+ 0x401 => 'erbta', # Exception Return Branch Target Address
134
+ 0x402 => 'erstatus', # Exception Return Status
135
+ 0x403 => 'ecr', # Exception Cause Register
136
+ 0x404 => 'efa', # Exception Fault Address
137
+ 0x40A => 'icause1', # Level 1 Interrupt Cause Register
138
+ 0x40B => 'icause2', # Level 2 Interrupt Cause Register
139
+ 0x40C => 'aux_ienable', # Interrupt Mask Programming
140
+ 0x40D => 'aux_itrigger', # Interrupt Sensitivity Programming
141
+ 0x410 => 'xpu', # User Mode Extension Enables
142
+ 0x412 => 'bta', # Branch Target Address
143
+ 0x413 => 'bta_l1', # Level 1 Return Branch Target
144
+ 0x414 => 'bta_l2', # Level 2 Return Branch Target
145
+ 0x415 => 'aux_irq_pulse_cancel', # Interrupt Pulse Cancel
146
+ 0x416 => 'aux_irq_pending', # Interrupt Pending Register
147
+ }
148
+
149
+ def render
150
+ if s = Render[i]
151
+ [s]
152
+ else
153
+ ["aux#@i"]
154
+ end
155
+ end
156
+ end
157
+
158
+ class Memref
159
+ attr_accessor :base, :disp
160
+
161
+ def initialize(base, disp, sz)
162
+ @base, @disp, @size = base, disp, sz
163
+ end
164
+
165
+ def symbolic(orig)
166
+ b = @base
167
+ b = b.symbolic if b.kind_of? Reg
168
+
169
+ if disp
170
+ o = @disp
171
+ o = o.symbolic if o.kind_of? Reg
172
+ e = Expression[b, :+, o].reduce
173
+ else
174
+ e = Expression[b].reduce
175
+ end
176
+
177
+ Indirection[e, @size, orig]
178
+ end
179
+
180
+ include Renderable
181
+
182
+ def render
183
+ if @disp and @disp != 0
184
+ ['[', @base, ', ', @disp, ']']
185
+ else
186
+ ['[', @base, ']']
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,588 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2010 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+ require 'metasm/cpu/arc/main'
7
+
8
+ module Metasm
9
+ class ARC
10
+ def addop32(name, bin, *args)
11
+ addop(:ac32, name, bin, *args)
12
+ end
13
+
14
+ def addop16(name, bin, *args)
15
+ addop(:ac16, name, bin, *args)
16
+ end
17
+
18
+ def addop(mode, name, bin, *args)
19
+ o = Opcode.new(name)
20
+ o.bin = bin
21
+ args.each { |a|
22
+ o.args << a if @fields_mask[a]
23
+ o.props[a] = true if @valid_props[a]
24
+ o.fields[a] = [@fields_mask[a], @fields_shift[a]] if @fields_mask[a]
25
+ }
26
+ (mode == :ac16) ? (@opcode_list16 << o) : (@opcode_list32 << o)
27
+ end
28
+
29
+ def init_opcode_list
30
+ @opcode_list16 = []
31
+ @opcode_list32 = []
32
+
33
+ @valid_props.update :flag_update => true, :delay_slot => true
34
+ @cond_suffix = [''] + %w[z nz p n cs cc vs vc gt ge lt le hi ls pnz]
35
+ #The remaining 16 condition codes (10-1F) are available for extension
36
+ @cond_suffix += (0x10..0x1f).map{ |i| "extcc#{i.to_s(16)}" }
37
+
38
+ # Compact 16-bits operands field masks
39
+ fields_mask16 = {
40
+ :ca => 0x7, :cb => 0x7, :cb2 => 0x7, :cb3 => 0x7, :cc => 0x7,
41
+ :cu => 0x1f,
42
+ :ch => 0b11100111,
43
+
44
+ # immediate (un)signed
45
+ :cu3 => 0x7, :cu8 => 0xff,
46
+ # cu7 is 32-bit aligned, cu6 is 16-bit aligned, cu6 is 8-bit aligned
47
+ :cu5 => 0x1f, :cu5ee => 0x1f, :cu6 => 0x3f, :cu7 => 0x7f,
48
+
49
+ :cs9 => 0x1ff, :cs9ee => 0x1ff, :cs10 => 0x1ff, :cs11 => 0x1ff,
50
+
51
+ # signed displacement
52
+ :cdisps7=> 0x3f, :cdisps8 => 0x7f, :cdisps10 => 0x1ff, :cdisps13 => 0x7FF,
53
+
54
+ # memref [b+u], [sp,u], etc.
55
+ :@cb => 0x7, :@cbu7 => 0b11100011111, :@cbu6 => 0b11100011111, :@cbu5 => 0b11100011111,
56
+ :@cspu7 => 0b11111, :@cbcc => 0b111111,
57
+ :@gps9 => 0x1ff, :@gps10 => 0x1ff, :@gps11 => 0x1ff,
58
+
59
+ # implicit operands
60
+ :climm => 0x0, :cr0 => 0x0,
61
+ :blink => 0x0, :@blink => 0x0, :gp => 0x0, :sp => 0x0, :sp2 => 0x0, :zero => 0x0
62
+ }
63
+
64
+ fields_shift16 = {
65
+ :ca => 0x0, :cb => 0x8, :cb2 => 0x8, :cb3 => 0x8, :cc => 0x5,
66
+ :cu => 0x0,
67
+
68
+ # immediate (un)signed
69
+ :ch => 0x0,
70
+ :cu3 => 0x0, :cu5 => 0, :cu5ee => 0, :cu6 => 5, :cu7 => 0x0, :cu8 => 0x0,
71
+ :cs9 => 0x0, :cs9ee => 0x0, :cs10 => 0x0, :cs11 => 0x0,
72
+
73
+ # signed displacement
74
+ :cdisps7=> 0x0, :cdisps8 => 0x0, :cdisps10 => 0x0, :cdisps13 => 0x0,
75
+
76
+ # memref [b+u]
77
+ :@cb => 0x8, :@cbu7 => 0x0, :@cbu6 => 0x0, :@cbu5 => 0x0,
78
+ :@cspu7 => 0x0, :@cbcc => 0x5,
79
+ :@gps9 => 0x0, :@gps10 => 0x0, :@gps11 => 0x0,
80
+
81
+ # implicit operands
82
+ :climm => 0x0, :cr0 => 0x0,
83
+ :blink => 0x0, :@blink => 0x0, :gp => 0x0, :sp => 0x0, :sp2 => 0x0, :zero => 0x0,
84
+ }
85
+
86
+ fields_mask32 = {
87
+ :a => 0x3f, :b => 0b111000000000111, :bext => 0b111000000000111,
88
+ :c => 0x3f, :@c => 0x3f, :cext => 0x3f, :@cext => 0x3f,
89
+
90
+ :u6 => 0x3f, :u6e => 0x3f,
91
+ :s8e => 0x1fd, :s9 => 0x7f,
92
+ :s12 => 0xfff, :s12e => 0xfff,
93
+ :s21e => 0x1ffBff, :s21ee => 0x1ff3ff,
94
+ :s25e => 0x7feffcf, :s25ee => 0x7fcffcf,
95
+
96
+ :@bs9 => 0x7fff, :@bc => 0x1ff, :@bextcext => 0x1C01FF,
97
+
98
+ :limm => 0x0, :@limm => 0x0,
99
+ :@limmc => 0x3f, :@blimm => 0x7,
100
+
101
+ :auxlimm => 0x0, :auxs12 => 0xfff,
102
+
103
+ :ccond => 0x1f, #condition codes
104
+ :delay5 => 1, :delay16 => 1,# delay slot
105
+ :flags15 => 0x1, :flags16 => 0x1,
106
+ :signext6 => 0x1, :signext16 => 0x1,
107
+ :cache5 => 0x1, :cache11 => 0x1, :cache16 => 0x1, # data cache mode field
108
+ :sz1 => 0x3, :sz7 => 0x3, :sz16 => 0x3, :sz17 => 0x3, #data size field
109
+ :wb3 => 0x3, :wb9 => 0x3, :wb22 => 0x3, #write-back flag
110
+ :zero => 0x0, :b2 => 0x0, :@ilink1 => 0x0, :@ilink2 => 0x0
111
+ }
112
+ #FIXME
113
+
114
+ fields_shift32 = {
115
+ :a => 0x0, :b => 0xC, :bext => 0xC,
116
+ :c => 0x6, :@c => 0x6, :cext => 0x6, :@cext => 0x6,
117
+
118
+ :u6 => 0x6, :u6e =>0x6,
119
+ :s8e => 15, :s9 => 0x11,
120
+ :s12 => 0x0, :s12e => 0,
121
+ :s21e => 0x6, :s21ee => 0x6,
122
+ :s25e => 0, :s25ee => 0,
123
+
124
+ :limm => 0x0, :@limm => 0x0,
125
+ :@limmc => 0x6, :@blimm => 0x18,
126
+
127
+ :auxlimm => 0x0, :auxs12 => 0,
128
+
129
+ :@bs9 => 12, :@bc => 6, :@bextcext => 6,
130
+
131
+ :ccond => 0, #condition codes
132
+ :delay5 => 5, :delay16 => 16,# delay slot
133
+ :flags15 => 15, :flags16 => 16,
134
+ :signext6 => 6, :signext16 => 16,
135
+ :cache5 => 5, :cache11 => 11, :cache16 => 16, # data cache mode field
136
+ :sz1 => 1, :sz7 => 7, :sz16 => 16, :sz17 => 17, #data size field
137
+ :wb3 => 3, :wb9 => 9, :wb22 => 22, #write-back flag
138
+ :zero => 0x0, :b2 => 0x0, :@ilink1 => 0, :@ilink2 => 0,
139
+ }
140
+
141
+ @fields_mask = fields_mask16.merge(fields_mask32)
142
+ @fields_shift = fields_shift16.merge(fields_shift32)
143
+
144
+ init_arc_compact16()
145
+ init_arc_compact32()
146
+
147
+ {16 => @opcode_list16, 32 => @opcode_list32}
148
+ end
149
+
150
+ def add_artihm_op(op, majorcode, subcode, *flags)
151
+ # 0bxxxxxbbb00xxxxxxFBBBCCCCCCAAAAAA
152
+ addop32 op, 0b00000000000000000000000000000000 | majorcode << 0x1b | subcode << 16, :a, :bext, :cext, :flags15
153
+ # 0bxxxxxbbb01xxxxxxFBBBuuuuuuAAAAAA
154
+ addop32 op, 0b00000000010000000000000000000000 | majorcode << 0x1b | subcode << 16, :a, :b, :u6, :flags15
155
+ # 0bxxxxxbbb10xxxxxxFBBBssssssSSSSSS
156
+ addop32 op, 0b00000000100000000000000000000000 | majorcode << 0x1b | subcode << 16, :b, :b2, :s12, :flags15
157
+ # 0bxxxxxbbb11xxxxxxFBBBCCCCCC0QQQQQ
158
+ addop32 op, 0b00000000110000000000000000000000 | majorcode << 0x1b | subcode << 16, :b, :b2, :cext, :ccond, :flags15
159
+ # 0bxxxxxbbb11xxxxxxFBBBuuuuuu1QQQQQ
160
+ addop32 op, 0b00000000110000000000000000100000 | majorcode << 0x1b | subcode << 16, :b, :b2, :u6, :ccond, :flags15
161
+ end
162
+
163
+ def add_logical_op(op, majorcode, subcode, *flags)
164
+ # 0b00100bbb00xxxxxxFBBBCCCCCCAAAAAA
165
+ addop32 op, 0b00100000000000000000000000000000 | majorcode << 0x1b | subcode << 16, :a, :bext, :c, :flags15
166
+ # 0b00100bbb01xxxxxxFBBBuuuuuuAAAAAA
167
+ addop32 op, 0b00100000010000000000000000000000 | majorcode << 0x1b | subcode << 16, :a, :b, :u6, :flags15
168
+ # 0b00100bbb11xxxxxxFBBBCCCCCC0QQQQQ
169
+ # WTF
170
+ addop32 op, 0b00100000110000000000000000000000 | majorcode << 0x1b | subcode << 16, :b, :b2, :c, :ccond, :flags15
171
+ # 0b00100bbb11xxxxxxFBBBuuuuuu1QQQQQ
172
+ addop32 op, 0b00100000110000000000000000100000 | majorcode << 0x1b | subcode << 16, :b, :b2, :u6, :ccond, :flags15
173
+ end
174
+
175
+ def add_artihm_op_reduce(op, majorcode, subcode)
176
+ # 0bxxxxxbbb00101111FBBBCCCCCCxxxxxx
177
+ addop32 op, 0b00000000001011110000000000000000 | majorcode << 0x1b | subcode, :b, :cext, :flags15
178
+ # 0bxxxxxbbb01101111FBBBuuuuuuxxxxxx
179
+ addop32 op, 0b00000000011011110000000000000000 | majorcode << 0x1b | subcode, :b, :u6, :flags15
180
+ end
181
+
182
+ def add_condbranch_op(op, ccond)
183
+ # 0b00001bbbsssssss1SBBBUUUUUUN0xxxx
184
+ addop32 op, 0b00001000000000010000000000000000 | ccond, :bext, :cext, :s8e, :setip, :delay5
185
+ # 0b00001bbbsssssss1SBBBUUUUUUN1xxxx
186
+ addop32 op, 0b00001000000000010000000000010000 | ccond, :b, :u6, :s8e, :setip, :delay5
187
+ end
188
+
189
+ def add_condjmp_op()
190
+ # 0b00100RRR1110000D0RRRCCCCCC0QQQQQ
191
+ addop32 'j', 0b00100000111000000000000000000000, :@cext, :ccond, :setip, :delay16
192
+ # 0b00100RRR1110000D0RRRuuuuuu1QQQQQ
193
+ addop32 'j', 0b00100000111000000000000000100000, :u6, :ccond, :setip, :delay16
194
+ # 0b00100RRR111000001RRR0111010QQQQQ
195
+ addop32 'j', 0b00100000111000001000011101000000, :@ilink1, :ccond, :setip, :flag_update
196
+ # 0b00100RRR111000001RRR0111100QQQQQ
197
+ addop32 'j', 0b00100000111000001000011110000000, :@ilink2, :ccond, :setip, :flag_update
198
+ end
199
+
200
+ def add_condjmplink_op()
201
+ # 0b00100RRR111000100RRRCCCCCC0QQQQQ
202
+ addop32 'jl', 0b00100000111000100000000000000000, :@cext, :ccond, :setip, :saveip, :delay16
203
+ # 0b00100RRR111000100RRRuuuuuu1QQQQQ
204
+ addop32 'jl', 0b00100000111000100000000000100000, :u6, :ccond, :setip, :saveip, :delay16
205
+ end
206
+
207
+ def init_arc_compact32
208
+
209
+ add_artihm_op_reduce 'abs', 0b00100, 0b001001
210
+ add_artihm_op_reduce 'abss', 0b00101, 0b000101
211
+ add_artihm_op_reduce 'abssw', 0b00101, 0b000100
212
+
213
+ add_artihm_op 'adc', 0b00100, 0b000001
214
+ add_artihm_op 'add', 0b00100, 0b000000
215
+ add_artihm_op 'add1', 0b00100, 0b010100
216
+ add_artihm_op 'add2', 0b00100, 0b010101
217
+ add_artihm_op 'add3', 0b00100, 0b010110
218
+ add_artihm_op 'adds', 0b00101, 0b000110
219
+ add_artihm_op 'addsw', 0b00101, 0b010101, :extended
220
+ add_artihm_op 'addsdw',0b00101, 0b101000, :extended
221
+ add_artihm_op 'and' ,0b00100, 0b000100
222
+
223
+ add_artihm_op_reduce 'asl', 0b00100, 0b000000
224
+
225
+ add_artihm_op 'asl', 0b00101, 0b000000, :extended
226
+ add_artihm_op 'asls', 0b00101, 0b001010, :extended
227
+
228
+ add_artihm_op_reduce 'asr', 0b00100, 0b000001
229
+
230
+ add_artihm_op 'asr', 0b00101, 0b000010
231
+ add_artihm_op 'asrs', 0b00101, 0b001011
232
+
233
+ # 0b00001bbbsssssss1SBBBCCCCCCN01110
234
+ addop32 'bbit0', 0b00001000000000010000000000001110, :b, :c, :s9, :delay5, :setip
235
+ # 0b00001bbbsssssss1SBBBuuuuuuN11110
236
+ addop32 'bbit0', 0b00001000000000010000000000011110, :b, :u6, :s9, :delay5, :setip
237
+ # 0b00001bbbsssssss1SBBBCCCCCCN01111
238
+ addop32 'bbit1', 0b00001000000000010000000000001111, :b, :c, :s9, :delay5, :setip
239
+ # 0b00001bbbsssssss1SBBBuuuuuuN11111
240
+ addop32 'bbit1', 0b00001000000000010000000000011111, :b, :u6, :s9, :delay5, :setip
241
+
242
+ # 0b00000ssssssssss0SSSSSSSSSSNQQQQQ
243
+ addop32 'b', 0b00000000000000000000000000000000, :s21e, :ccond, :delay5, :setip
244
+ # 0b00000ssssssssss1SSSSSSSSSSNRtttt
245
+ addop32 'b', 0b00000000000000010000000000000000, :s25e, :delay5, :setip, :stopexec
246
+ # WTF: unknown encoding, bit 5 should be reserved
247
+ addop32 'b', 0b00000000000000010000000000010000, :s25e, :delay5, :setip, :stopexec
248
+
249
+ add_logical_op 'bclr', 0b00100, 0b010000
250
+ add_artihm_op 'bic', 0b00100, 0b000110
251
+
252
+ # 0b00001sssssssss00SSSSSSSSSSNQQQQQ
253
+ addop32 'bl', 0b00001000000000000000000000000000, :s21ee, :ccond, :delay5, :setip, :saveip
254
+ # 0b00001sssssssss10SSSSSSSSSSNRtttt
255
+ addop32 'bl', 0b00001000000000100000000000000000, :s25ee, :delay5, :setip, :saveip, :stopexec
256
+
257
+ add_logical_op 'bmsk', 0b00100, 0b010011
258
+
259
+ add_condbranch_op 'breq', 0b0000
260
+ add_condbranch_op 'brne', 0b0001
261
+ add_condbranch_op 'brlt', 0b0010
262
+ add_condbranch_op 'brge', 0b0011
263
+ add_condbranch_op 'brlo', 0b0100
264
+ add_condbranch_op 'brhs', 0b0101
265
+
266
+ addop32 'brk', 0b00100101011011110000000000111111, :stopexec
267
+
268
+ add_logical_op 'bset', 0b00100, 0b001111
269
+
270
+ # 0b00100bbb110100011BBBCCCCCC0QQQQQ
271
+ addop32 'btst', 0b00100000110100011000000000000000, :bext, :c, :ccond
272
+ # 0b00100bbb110100011BBBuuuuuu1QQQQQ
273
+ addop32 'btst', 0b00100000110100011000000000100000, :b, :u6, :ccond
274
+ # WTF 0b00100bbb010100011BBBuuuuuu0QQQQQ
275
+ addop32 'btst', 0b00100000010100011000000000000000, :b, :u6, :ccond
276
+
277
+ add_logical_op 'bxor', 0b00100, 0b010010
278
+
279
+ # 0b00100bbb100011001BBBssssssSSSSSS
280
+ addop32 'cmp', 0b00100000100011001000000000000000, :b, :s12
281
+ # WTF unknown encoding ...
282
+ # 0b00100bbb010011001BBBssssssSSSSSS
283
+ addop32 'cmp', 0b00100000010011001000000000000000, :b, :s12
284
+ # 0b00100bbb110011001BBBuuuuuu1QQQQQ
285
+ addop32 'cmp', 0b00100000110011001000000000100000, :b, :u6, :ccond
286
+ # WTF unknown encoding ...
287
+ # 0b00100bbb010011001BBBssssssSSSSSS
288
+ addop32 'cmp', 0b00100000000011001000000000000000, :bext, :cext, :ccond
289
+ # 0b00100bbb110011001BBBCCCCCC0QQQQQ
290
+ addop32 'cmp', 0b00100000110011001000000000000000, :bext, :cext, :ccond
291
+
292
+ add_artihm_op 'divaw', 0b00101, 0b001000, :extended
293
+
294
+ # 0b00100bbb00101111DBBBCCCCCC001100
295
+ addop32 'ex', 0b00100000001011110000000000001100, :b, :@cext, :cache16
296
+ # 0b00100bbb01101111DBBBuuuuuu001100
297
+ addop32 'ex', 0b00100000011011110000000000001100, :b, :@u6, :cache16
298
+
299
+ add_artihm_op_reduce 'extb', 0b00100, 0b000111
300
+ add_artihm_op_reduce 'extw', 0b00100, 0b001000
301
+
302
+ # WTF unknown encoding ...
303
+ # 0b00100rrr111010010RRRCCCCCC0QQQQQ
304
+ addop32 'flag', 0b00100000001010010000000000000000, :cext, :ccond, :flag_update
305
+ # 0b00100rrr111010010RRRuuuuuu1QQQQQ
306
+ addop32 'flag', 0b00100000001010010000000000100000, :u6, :ccond, :flag_update
307
+ # 0b00100rrr101010010RRRssssssSSSSSS
308
+ addop32 'flag', 0b00100000011010010000000000000000, :s12, :flag_update
309
+
310
+ add_condjmp_op()
311
+ add_condjmplink_op()
312
+
313
+ # 0b00100RRR001000000RRRCCCCCCRRRRRR
314
+ addop32 'j', 0b00100000001000000000000000000000, :@cext, :delay16, :setip, :stopexec
315
+ # 0b00100RRR011000000RRRuuuuuuRRRRRR
316
+ addop32 'j', 0b00100000011000000000000000000000, :u6, :delay16, :setip, :stopexec
317
+ # 0b00100RRR101000000RRRssssssSSSSSS
318
+ addop32 'j', 0b00100000101000000000000000000000, :s12, :delay16, :setip, :stopexec
319
+ # 0b00100RRR001000001RRR011101RRRRRR
320
+ addop32 'j.f', 0b00100000001000001000011101000000, :@ilink1, :flag_update, :setip, :stopexec
321
+ # 0b00100RRR001000001RRR011110RRRRRR
322
+ addop32 'j.f', 0b00100000001000001000011110000000, :@ilink2, :flag_update, :setip, :stopexec
323
+
324
+ # 0b00100RRR0010001D0RRRCCCCCCRRRRRR
325
+ addop32 'jl', 0b00100000001000100000000000000000, :@cext, :delay16, :setip, :saveip, :stopexec
326
+ # 0b00100RRR0110001D0RRRuuuuuuRRRRRR
327
+ addop32 'jl', 0b00100000011000100000000000000000, :u6, :delay16, :setip, :saveip, :stopexec
328
+ # 0b00100RRR1010001D0RRRssssssSSSSSS
329
+ addop32 'jl', 0b00100000101000100000000000000000, :s12, :delay16, :setip, :saveip, :stopexec
330
+
331
+ # 0b00010bbbssssssssSBBBDaaZZXAAAAAA
332
+ addop32 'ld', 0b00010000000000000000000000000000, :a, :@bs9, :sz7, :signext6, :wb9, :cache11
333
+
334
+ # 0b00100bbbaa110ZZXDBBBCCCCCCAAAAAA
335
+ addop32 'ld', 0b00100000001100000000000000000000, :a, :@bextcext, :sz17, :signext16, :wb22, :cache11
336
+
337
+ # 0b00100RRR111010000RRRuuuuuu1QQQQQ
338
+ addop32 'lp', 0b00100000111010000000000000100000, :u6e, :ccond, :setip
339
+ # 0b00100RRR101010000RRRssssssSSSSSS
340
+ addop32 'lp', 0b00100000101010000000000000000000, :s12e, :setip
341
+
342
+ # 0b00100bbb001010100BBBCCCCCCRRRRRR
343
+ addop32 'lr', 0b00100000101010100000000000000000, :b, :@c
344
+ # 0b00100bbb001010100BBB111110RRRRRR
345
+ addop32 'lr', 0b00100000001010100000111110000000, :b, :auxlimm
346
+ # 0b00100bbb101010100BBBssssssSSSSSS
347
+ addop32 'lr', 0b00100000011010100000000000000000, :b, :auxs12
348
+ # WTF unknown encoding ...
349
+ # 0b00100bbb101010100BBBssssssSSSSSS
350
+ addop32 'lr', 0b00100000101010100000000000000000, :b, :auxs12
351
+
352
+ add_artihm_op_reduce 'lsr', 0b00100, 0b000010
353
+
354
+ add_artihm_op 'lsr', 0b00101, 0b000001
355
+ add_artihm_op 'max', 0b00100, 0b001000
356
+ add_artihm_op 'min', 0b00100, 0b001001
357
+
358
+ # 0b00100bbb10001010FBBBssssssSSSSSS
359
+ addop32 'mov', 0b00100000100010100000000000000000, :b, :s12, :flags15
360
+ # WTF unknown encoding ...
361
+ # 0b00100bbb01001010FBBBssssssSSSSSS
362
+ addop32 'mov', 0b00100000010010100000000000000000, :b, :s12, :flags15
363
+ # 0b00100bbb11001010FBBBCCCCCC0QQQQQ
364
+ addop32 'mov', 0b00100000110010100000000000000000, :b, :cext, :ccond , :flags15
365
+ # WTF unknown encoding ..
366
+ # 0b00100bbb00001010FBBBCCCCCC0QQQQQ
367
+ addop32 'mov', 0b00100000000010100000000000000000, :b, :cext, :ccond , :flags15
368
+ # 0b00100bbb11001010FBBBuuuuuu1QQQQQ
369
+ addop32 'mov', 0b00100000110010100000000000100000, :b, :u6, :ccond , :flags15
370
+
371
+ add_artihm_op 'mpy', 0b00100, 0b011010, :extended
372
+ add_artihm_op 'mpyh', 0b00100, 0b011011, :extended
373
+ add_artihm_op 'mpyhu', 0b00100, 0b011100, :extended
374
+ add_artihm_op 'mpyu', 0b00100, 0b011101, :extended
375
+
376
+ # WTF: neg instruction is not differenciated from a rsub :a, :b, :u6
377
+ # : 0b00100bbb01001110FBBB000000AAAAAA
378
+ #addop32 'neg', 0b00100000010011100000000000000000, :a, :b, :flags15
379
+
380
+ # WTF: neg instruction is not differenciated from a rsub :b, :b2, :u6
381
+ # 0b00100bbb11001110FBBB0000001QQQQQ
382
+ #addop32 'neg', 0b00100000110011100000000000100000, :b, :b2, :ccond , :flags15
383
+
384
+ add_artihm_op_reduce 'negs', 0b00101, 0b000111
385
+ add_artihm_op_reduce 'negsw', 0b00101, 0b000110
386
+
387
+ # nop is an alias over mov null, 0 (mov - [:b, :s12, :flags15])
388
+ addop32 'nop', 0b00100110010010100111000000000000
389
+
390
+ add_artihm_op_reduce 'norm', 0b00101, 0b000001
391
+ add_artihm_op_reduce 'normw', 0b00101, 0b001000
392
+ add_artihm_op_reduce 'not', 0b00100, 0b001010
393
+
394
+ add_artihm_op 'or', 0b00100, 0b000101
395
+
396
+ # 0b00010bbbssssssssSBBB0aa000111110
397
+ addop32 'prefetch', 0b00010000000000000000000000111110, :@bs9, :wb
398
+ # 0b00100bbbaa1100000BBBCCCCCC111110
399
+ addop32 'prefetch', 0b00100000001100000000000000111110, :@bextcext, :wb22
400
+
401
+ # 0b00100bbb100011011BBBssssssSSSSSS
402
+ addop32 'rcmp', 0b00100000100011011000000000000000, :b, :s12
403
+ # 0b00100bbb110011011BBBCCCCCC0QQQQQ
404
+ addop32 'rcmp', 0b00100000110011011000000000000000, :bext, :cext, :ccond
405
+ # 0b00100bbb110011011BBBuuuuuu1QQQQQ
406
+ addop32 'rcmp', 0b00100000110011011000000000100000, :b, :u6, :ccond
407
+
408
+ add_artihm_op_reduce 'rlc', 0b00100, 0b001011
409
+ add_artihm_op_reduce 'rnd16', 0b00101, 0b000011
410
+ add_artihm_op_reduce 'ror', 0b00100, 0b000011
411
+
412
+ add_artihm_op 'ror', 0b00101, 0b000011, :extended
413
+
414
+ add_artihm_op_reduce 'rrc', 0b00100, 0b000100
415
+
416
+ add_artihm_op 'rsub', 0b00100, 0b001110
417
+
418
+ addop32 'rtie', 0b00100100011011110000000000111111, :setip, :stopexec
419
+
420
+ add_artihm_op_reduce 'sat16', 0b00101, 0b000010
421
+
422
+ add_artihm_op 'sbc', 0b00100, 0b000011
423
+
424
+ add_artihm_op_reduce 'sexb', 0b00100, 0b000101
425
+ add_artihm_op_reduce 'sexbw', 0b00100, 0b000110
426
+
427
+ # 0b00100001011011110000uuuuuu111111
428
+ addop32 'sleep', 0b00100001011011110000000000111111, :u6
429
+
430
+ # 0b00100bbb001010110BBBCCCCCCRRRRRR
431
+ addop32 'sr', 0b00100000001010110000000000000000, :bext, :@cext
432
+ # 0b00100110101010110111CCCCCCRRRRRR
433
+ addop32 'sr', 0b00100000101010110000000000000000, :bext, :auxs12
434
+ # WTF: unknown encoding
435
+ addop32 'sr', 0b00100000011010110000000000000000, :bext, :auxs12
436
+
437
+ # 0b00011bbbssssssssSBBBCCCCCCDaaZZR
438
+ addop32 'st', 0b00011000000000000000000000000000, :cext, :@bs9, :sz1, :wb3, :cache5
439
+
440
+ add_artihm_op 'sub', 0b00100, 0b000010
441
+ add_artihm_op 'sub1', 0b00100, 0b010111
442
+ add_artihm_op 'sub2', 0b00100, 0b011000
443
+ add_artihm_op 'sub3', 0b00100, 0b011001
444
+
445
+ # WTF: same encoding as xor instructions
446
+ #add_artihm_op 'subs', 0b00100, 0b000111
447
+
448
+ add_artihm_op 'subsdw', 0b00101, 0b101001, :extended
449
+
450
+ add_artihm_op_reduce 'swap', 0b00101, 0b000000
451
+
452
+ addop32 'swi', 0b00100010011011110000000000111111, :setip, :stopexec
453
+ addop32 'sync', 0b00100011011011110000000000111111
454
+
455
+ # 0b00100bbb100010111BBBssssssSSSSSS
456
+ addop32 'tst', 0b00100000100010111000000000000000, :b, :s12
457
+ # 0b00100bbb110010111BBBCCCCCC0QQQQQ
458
+ addop32 'tst', 0b00100000110010111000000000000000, :bext, :cext, :ccond
459
+ # 0b00100bbb110010111BBBuuuuuu1QQQQQ
460
+ addop32 'tst', 0b00100000110010111000000000100000, :b, :u6, :ccond
461
+
462
+ add_artihm_op 'xor', 0b00100, 0b000111
463
+ end
464
+
465
+ # ARCompact 16-bit instructions
466
+ def init_arc_compact16
467
+ addop16 'abs_s', 0x7811, :cb, :cc
468
+ addop16 'add_s', 0x6018, :ca, :cb, :cc
469
+ addop16 'add_s', 0x7000, :cb, :cb2, :ch
470
+ addop16 'add_s', 0x6800, :cc, :cb, :cu3
471
+ addop16 'add_s', 0xe000, :cb, :cb2, :cu7
472
+
473
+ # same encoding as add_s b,b,h
474
+ #addop16 'add_s', 0x70c7, :cb, :cb2, :climm
475
+
476
+ addop16 'add_s', 0xc080, :cb, :sp, :cu5ee
477
+ addop16 'add_s', 0xc0a0, :sp, :sp2, :cu5ee
478
+ addop16 'add_s', 0xce00, :cr0, :gp, :cs9
479
+ addop16 'add1_s', 0x7814, :cb, :cb2, :cc
480
+ addop16 'add2_s', 0x7815, :cb, :cb2, :cc
481
+ addop16 'add3_s', 0x7816, :cb, :cb2, :cc
482
+ addop16 'and_s', 0x7804, :cb, :cb2, :cc
483
+ addop16 'asl_s', 0x7818, :cb, :cb2, :cc
484
+ addop16 'asl_s', 0x6810, :cc, :cb, :cu3
485
+ addop16 'asl_s', 0xb800, :cb, :cb2, :cu5
486
+ addop16 'asl_s', 0x781b, :cb, :cc
487
+ addop16 'asr_s', 0x781a, :cb, :cb2, :cc
488
+ addop16 'asr_s', 0x6818, :cc, :cb, :cu3
489
+ addop16 'asr_s', 0xb840, :cb, :cb2, :cu5
490
+ addop16 'asr_s', 0x781c, :cb, :cc
491
+ addop16 'b_s', 0xf000, :cdisps10, :setip, :stopexec
492
+ addop16 'beq_s', 0xf200, :cdisps10, :setip
493
+ addop16 'bne_s', 0xf400, :cdisps10, :setip
494
+ addop16 'bgt_s', 0xf600, :cdisps7, :setip
495
+ addop16 'bge_s', 0xf640, :cdisps7, :setip
496
+ addop16 'blt_s', 0xf680, :cdisps7, :setip
497
+ addop16 'ble_s', 0xf6c0, :cdisps7, :setip
498
+ addop16 'bhi_s', 0xf700, :cdisps7, :setip
499
+ addop16 'bhs_s', 0xf740, :cdisps7, :setip
500
+ addop16 'blo_s', 0xf780, :cdisps7, :setip
501
+ addop16 'bls_s', 0xf7c0, :cdisps7, :setip
502
+ addop16 'bclr_s', 0xb8a0, :cb, :cb2, :cu5
503
+ addop16 'bic_s', 0x7806, :cb, :cb2, :cc
504
+ addop16 'bl_s', 0xf800, :cdisps13, :setip, :saveip, :stopexec
505
+ addop16 'bmsk_s', 0xb8c0, :cb, :cb2, :cu5
506
+ addop16 'breq_s', 0xe800, :cb, :zero, :cdisps8, :setip
507
+ addop16 'brne_s', 0xe880, :cb, :zero, :cdisps8, :setip
508
+ addop16 'brk_s', 0x7fff
509
+ addop16 'bset_s', 0xb880, :cb, :cb2, :cu5
510
+ addop16 'btst_s', 0xb8e0, :cb, :cu5
511
+ addop16 'cmp_s', 0x7010, :cb, :ch
512
+ addop16 'cmp_s', 0xe080, :cb, :cu7
513
+
514
+ # encoded over cmp_s b,h
515
+ # addop16 'cmp_s', 0x70d7, :cb, :limm
516
+
517
+ addop16 'extb_s', 0x780f, :cb, :cc
518
+ addop16 'extw_s', 0x7810, :cb, :cc
519
+ addop16 'j_s', 0x7800, :@cb, :setip, :stopexec
520
+ addop16 'j_s.d', 0x7820, :@cb, :setip, :stopexec, :delay_slot
521
+ addop16 'j_s', 0x7ee0, :@blink, :setip, :stopexec
522
+ addop16 'j_s.d', 0x7fe0, :@blink, :setip, :stopexec, :delay_slot
523
+ addop16 'jeq_s', 0x7ce0, :@blink, :setip
524
+ addop16 'jne_s', 0x7de0, :@blink, :setip
525
+ addop16 'jl_s', 0x7840, :@cb, :setip, :saveip, :stopexec
526
+ addop16 'jl_s.d', 0x7860, :@cb, :setip, :saveip, :stopexec, :delay_slot
527
+ addop16 'ld_s', 0x6000, :ca, :@cbcc
528
+ addop16 'ldb_s', 0x6008, :ca, :@cbcc
529
+ addop16 'ldw_s', 0x6010, :ca, :@cbcc
530
+ addop16 'ld_s', 0x8000, :cc, :@cbu7
531
+ addop16 'ldb_s', 0x8800, :cc, :@cbu5
532
+ addop16 'ldw_s', 0x9000, :cc, :@cbu6
533
+ addop16 'ldw_s.x', 0x9800, :cc, :@cbu6
534
+ addop16 'ld_s', 0xc000, :cb, :@cspu7
535
+ addop16 'ldb_s', 0xc020, :cb, :@cspu7
536
+ addop16 'ld_s', 0xc800, :cr0, :@gps11
537
+ addop16 'ldb_s', 0xca00, :cr0, :@gps9
538
+ addop16 'ldw_s', 0xcc00, :cr0, :@gps10
539
+ addop16 'ld_s', 0xd000, :cb, :@pclu10
540
+
541
+ # FIXME: exact same encoding as asl_s instructions
542
+ #addop16 'lsl_s', 0x7818, :cb, :cb2, :cc
543
+ #addop16 'lsl_s', 0x6810, :cc, :cb, :cu3
544
+ #addop16 'lsl_s', 0xb800, :cb, :cb2, :cu5
545
+ #addop16 'lsl_s', 0x781d, :cb, :cc
546
+
547
+ addop16 'lsr_s', 0x7819, :cb, :cb2, :cc
548
+ addop16 'lsr_s', 0xb820, :cb, :cb2, :cu5
549
+ addop16 'lsr_s', 0x781d, :cb, :cc
550
+ addop16 'mov_s', 0x7008, :cb, :ch
551
+
552
+ # FIXME: same encoding as previous instruction
553
+ #addop16 'mov_s', 0x70cf, :cb, :limm
554
+
555
+ addop16 'mov_s', 0xd800, :cb, :cu8
556
+ addop16 'mov_s', 0x7018, :ch, :cb
557
+
558
+ # TODO seems to overlap with previous instruction
559
+ addop16 'mov_s', 0x70df, :zero, :cb
560
+ addop16 'mul64_s', 0x780c, :zero, :cb, :cc
561
+ addop16 'neg_s', 0x7813, :cb, :cc
562
+ addop16 'not_s', 0x7812, :cb, :cc
563
+ addop16 'nop_s',0x78e0
564
+ addop16 'unimp_s', 0x79e0
565
+ addop16 'or_s', 0x7805, :cb, :cb2, :cc
566
+ addop16 'pop_s', 0xc0c1, :cb
567
+ addop16 'pop_s', 0xc0d1, :blink
568
+ addop16 'push_s', 0xc0e1, :cb
569
+ addop16 'push_s', 0xc0f1, :blink
570
+ addop16 'sexb_s', 0x780d, :cb, :cc
571
+ addop16 'sexw_s', 0x780e, :cb, :cc
572
+ addop16 'st_s', 0xc040, :cb, :@cspu7
573
+ addop16 'stb_s', 0xc060, :cb, :@cspu7
574
+ addop16 'st_s', 0xa000, :cc, :@cbu7
575
+ addop16 'stb_s', 0xa800, :cc, :@cbu5
576
+ addop16 'stw_s', 0xb000, :cc, :@cbu6
577
+ addop16 'sub_s', 0x7802, :cb, :cb2, :cc
578
+ addop16 'sub_s', 0x6808, :cc, :cb, :cu3
579
+ addop16 'sub_s', 0xb860, :cb, :cb2, :cu5
580
+ addop16 'sub_s', 0xc1a0, :sp, :sp2, :cu5ee
581
+ addop16 'sub_s.ne', 0x78c0, :cb, :c2, :cb3
582
+ addop16 'trap_s', 0x781E, :cu6, :setip, :stopexec
583
+ addop16 'tst_s', 0x780b, :cb, :cc
584
+ addop16 'xor_s', 0x7807, :cb, :cb2, :cc
585
+ end
586
+
587
+ end
588
+ end