authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-05-17 05:35:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-17 12:03:01-04:00
loge198eec76a478fa9d1cb7b6d7488e273a0d15ff1
tree62a731409597fd709b03004b8605aa6fff144a3d
parent773281c1f47f4e0379d6a3d314c00f752151d89f

Document register functions


2 files changed, 15 insertions(+), 0 deletions(-)

src-self-hosted/backend/x86.zig+4
...@@ -9,6 +9,7 @@ pub const Register = enum(u8) {...@@ -9,6 +9,7 @@ pub const Register = enum(u8) {
9 // 16-23, 8-bit registers. id is int value - 16.9 // 16-23, 8-bit registers. id is int value - 16.
10 al, bl, cl, dl, ah, ch, dh, bh,10 al, bl, cl, dl, ah, ch, dh, bh,
1111
12 /// Returns the bit-width of the register.
12 pub fn size(self: @This()) u7 {13 pub fn size(self: @This()) u7 {
13 return switch (@enumToInt(self)) {14 return switch (@enumToInt(self)) {
14 0...7 => 32,15 0...7 => 32,
...@@ -18,6 +19,9 @@ pub const Register = enum(u8) {...@@ -18,6 +19,9 @@ pub const Register = enum(u8) {
18 };19 };
19 }20 }
2021
22 /// Returns the register's id. This is used in practically every opcode the
23 /// x86 has. It is embedded in some instructions, such as the `B8 +rd` move
24 /// instruction, and is used in the R/M byte.
21 pub fn id(self: @This()) u3 {25 pub fn id(self: @This()) u3 {
22 return @truncate(u3, @enumToInt(self));26 return @truncate(u3, @enumToInt(self));
23 }27 }
src-self-hosted/backend/x86_64.zig+11
...@@ -20,6 +20,7 @@ pub const Register = enum(u8) {...@@ -20,6 +20,7 @@ pub const Register = enum(u8) {
20 al, bl, cl, dl, ah, ch, dh, bh,20 al, bl, cl, dl, ah, ch, dh, bh,
21 r8b, r9b, r10b, r11b, r12b, r13b, r14b, r15b,21 r8b, r9b, r10b, r11b, r12b, r13b, r14b, r15b,
2222
23 /// Returns the bit-width of the register.
23 pub fn size(self: @This()) u7 {24 pub fn size(self: @This()) u7 {
24 return switch (@enumToInt(self)) {25 return switch (@enumToInt(self)) {
25 0...15 => 64,26 0...15 => 64,
...@@ -30,10 +31,20 @@ pub const Register = enum(u8) {...@@ -30,10 +31,20 @@ pub const Register = enum(u8) {
30 };31 };
31 }32 }
3233
34 /// Returns whether the register is *extended*. Extended registers are the
35 /// new registers added with amd64, r8 through r15. This also includes any
36 /// other variant of access to those registers, such as r8b, r15d, and so
37 /// on. This is needed because access to these registers requires special
38 /// handling via the REX prefix, via the B or R bits, depending on context.
33 pub fn isExtended(self: @This()) bool {39 pub fn isExtended(self: @This()) bool {
34 return @enumToInt(self) & 0x08 != 0;40 return @enumToInt(self) & 0x08 != 0;
35 }41 }
3642
43 /// This returns the 4-bit register ID, which is used in practically every
44 /// opcode. Note that bit 3 (the highest bit) is *never* used directly in
45 /// an instruction (@see isExtended), and requires special handling. The
46 /// lower three bits are often embedded directly in instructions (such as
47 /// the B8 variant of moves), or used in R/M bytes.
37 pub fn id(self: @This()) u4 {48 pub fn id(self: @This()) u4 {
38 return @truncate(u4, @enumToInt(self));49 return @truncate(u4, @enumToInt(self));
39 }50 }