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