1 /// Types and constants of XCore architecture
2 module capstone.xcore;
3 
4 import std.variant;
5 import std.exception: enforce;
6 import std.conv: to;
7 
8 import capstone.internal;
9 import capstone.utils;
10 
11 /** Instruction's operand referring to memory
12 
13 This is associated with the `XCoreOpType.mem` operand type
14 */
15 struct XCoreOpMem {
16 	XCoreRegister base;	 /// Base register
17 	XCoreRegister index; /// Index register
18 	int disp;			 /// Displacement/offset value
19 	int direct;			 /// +1: forward, -1: backward
20 	this(xcore_op_mem internal){
21 		base = internal.base.to!XCoreRegister;
22 		index = internal.index.to!XCoreRegister;
23 		disp = internal.disp;
24 		direct = internal.direct;
25 	}
26 }
27 
28 /// Tagged union of possible operand types
29 alias XCoreOpValue = TaggedUnion!(XCoreRegister, "reg", long, "imm", XCoreOpMem, "mem");
30 
31 /// Instruction's operand
32 struct XCoreOp {
33     XCoreOpType type;   /// Operand type
34     XCoreOpValue value; /// Operand value of type `type`
35     alias value this;  /// Convenient access to value (as in original bindings)
36 
37     package this(cs_xcore_op internal){
38         type = internal.type;
39         final switch(internal.type) {
40             case XCoreOpType.invalid:
41                 break;
42             case XCoreOpType.reg:
43                 value.reg = internal.reg;
44                 break;
45             case XCoreOpType.imm:
46                 value.imm = internal.imm;
47                 break;
48             case XCoreOpType.mem:
49                 value.mem = XCoreOpMem(internal.mem);
50                 break;
51         }
52     }
53 }
54 
55 /// XCore-specific information about an instruction
56 struct XCoreInstructionDetail {
57     XCoreOp[] operands;          /// Operands for this instruction.
58 
59     package this(cs_arch_detail arch_detail){
60 		this(arch_detail.xcore);
61 	}
62     package this(cs_xcore internal){
63         foreach(op; internal.operands[0..internal.op_count])
64             operands ~= XCoreOp(op);
65     }
66 }
67 
68 //=============================================================================
69 // Constants
70 //=============================================================================
71 
72 /// Operand type for instruction's operands
73 enum XCoreOpType {
74 	invalid = 0, /// Uninitialized
75 	reg, 		 /// Register operand
76 	imm, 		 /// Immediate operand
77 	mem, 		 /// Memory operand
78 }
79 
80 /// XCore registers
81 enum XCoreRegister {
82 	invalid = 0,
83 
84 	cp,
85 	dp,
86 	lr,
87 	sp,
88 	r0,
89 	r1,
90 	r2,
91 	r3,
92 	r4,
93 	r5,
94 	r6,
95 	r7,
96 	r8,
97 	r9,
98 	r10,
99 	r11,
100 
101 	// Pseudo registers
102 	pc,	/// Program counter
103 
104 	// Internal thread registers
105 	// See The-XMOS-XS1-Architecture(X7879A).pdf
106 	scp, /// Save pc
107 	ssr, /// Save status
108 	et,	 /// Exception type
109 	ed,	 /// Exception data
110 	sed, /// Save exception data
111 	kep, /// Kernel entry pointer
112 	ksp, /// Kernel stack pointer
113 	id,	 /// Thread ID
114 }
115 
116 /// XCore instruction
117 enum XCoreInstructionId {
118 	invalid = 0,
119 
120 	add,
121 	andnot,
122 	and,
123 	ashr,
124 	bau,
125 	bitrev,
126 	bla,
127 	blat,
128 	bl,
129 	bf,
130 	bt,
131 	bu,
132 	bru,
133 	byterev,
134 	chkct,
135 	clre,
136 	clrpt,
137 	clrsr,
138 	clz,
139 	crc8,
140 	crc32,
141 	dcall,
142 	dentsp,
143 	dgetreg,
144 	divs,
145 	divu,
146 	drestsp,
147 	dret,
148 	ecallf,
149 	ecallt,
150 	edu,
151 	eef,
152 	eet,
153 	eeu,
154 	endin,
155 	entsp,
156 	eq,
157 	extdp,
158 	extsp,
159 	freer,
160 	freet,
161 	getd,
162 	get,
163 	getn,
164 	getr,
165 	getsr,
166 	getst,
167 	getts,
168 	inct,
169 	init,
170 	inpw,
171 	inshr,
172 	int_,
173 	in_,
174 	kcall,
175 	kentsp,
176 	krestsp,
177 	kret,
178 	ladd,
179 	ld16s,
180 	ld8u,
181 	lda16,
182 	ldap,
183 	ldaw,
184 	ldc,
185 	ldw,
186 	ldivu,
187 	lmul,
188 	lss,
189 	lsub,
190 	lsu,
191 	maccs,
192 	maccu,
193 	mjoin,
194 	mkmsk,
195 	msync,
196 	mul,
197 	neg,
198 	not,
199 	or,
200 	outct,
201 	outpw,
202 	outshr,
203 	outt,
204 	out_,
205 	peek,
206 	rems,
207 	remu,
208 	retsp,
209 	setclk,
210 	set,
211 	setc,
212 	setd,
213 	setev,
214 	setn,
215 	setpsc,
216 	setpt,
217 	setrdy,
218 	setsr,
219 	settw,
220 	setv,
221 	sext,
222 	shl,
223 	shr,
224 	ssync,
225 	st16,
226 	st8,
227 	stw,
228 	sub,
229 	syncr,
230 	testct,
231 	testlcl,
232 	testwct,
233 	tsetmr,
234 	start,
235 	waitef,
236 	waitet,
237 	waiteu,
238 	xor,
239 	zext,
240 }
241 
242 /// Group of XCore instructions
243 enum XCoreInstructionGroup {
244 	invalid = 0,
245 
246 	// Generic groups
247 	// All jump instructions (conditional+direct+indirect jumps)
248 	jump,
249 }