1 /// Object-oriented wrapper of registers
2 module capstone.register;
3 
4 import std.conv: to;
5 
6 import capstone.capstone: Capstone;
7 
8 import capstone.api: diet;
9 import capstone.error;
10 import capstone.internal.api: cs_reg_name;
11 
12 /// Architecture-independent register base class
13 abstract class Register {
14     private const Capstone cs;
15     package const int _id;
16 
17     private this(in Capstone cs, in int id) {
18         this.cs = cs;
19         this._id = id;
20     }
21 
22     /// Retrieves register's id as plain integer
23     auto idAsInt() const {return _id;}
24 
25     /// Returns friendly string representation of the registers's name
26     string name() const {
27         if(diet)
28             throw new CapstoneException("Register names are not stored when running Capstone in diet mode",
29                 ErrorCode.IrrelevantDataAccessInDietEngine);
30         return cs_reg_name(cs.handle, _id).to!string; // TODO: Error handling
31     }
32 }
33 
34 /** Class template for architecture-specific registers
35 
36 Note that all architecture-specific instances, like `X86Register`, instantiate and derive from this one.
37 */
38 abstract class RegisterImpl(TId) : Register if(is(TId == enum)) {
39     package this(in Capstone cs, in int id) {
40         super(cs, id);
41     }
42 
43     /// Retrieves register's id
44     auto id() const {return _id.to!TId;}
45 }