-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Initial support for vex encoding for the new assembler. #10754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abrown
merged 3 commits into
bytecodealliance:main
from
jlb6740:add-initial-vex-encoding
May 23, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,15 @@ pub fn rex(opcode: impl Into<Opcodes>) -> Rex { | |
|
||
/// An abbreviated constructor for VEX-encoded instructions. | ||
#[must_use] | ||
pub fn vex() -> Vex { | ||
Vex {} | ||
pub fn vex(opcode: impl Into<Opcodes>) -> Vex { | ||
Vex { | ||
opcodes: opcode.into(), | ||
w: false, | ||
length: VexLength::_128, | ||
mmmmm: VexMMMMM::None, | ||
pp: VexPP::None, | ||
imm: None, | ||
} | ||
} | ||
|
||
/// Enumerate the ways x64 encodes instructions. | ||
|
@@ -48,7 +55,7 @@ impl Encoding { | |
pub fn validate(&self, operands: &[Operand]) { | ||
match self { | ||
Encoding::Rex(rex) => rex.validate(operands), | ||
Encoding::Vex(vex) => vex.validate(), | ||
Encoding::Vex(vex) => vex.validate(operands), | ||
} | ||
} | ||
} | ||
|
@@ -57,7 +64,7 @@ impl fmt::Display for Encoding { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Encoding::Rex(rex) => write!(f, "{rex}"), | ||
Encoding::Vex(_vex) => todo!(), | ||
Encoding::Vex(vex) => write!(f, "{vex}"), | ||
} | ||
} | ||
} | ||
|
@@ -563,7 +570,7 @@ pub enum Imm { | |
} | ||
|
||
impl Imm { | ||
fn bits(&self) -> u8 { | ||
fn bits(&self) -> u16 { | ||
match self { | ||
Imm::None => 0, | ||
Imm::ib => 8, | ||
|
@@ -586,10 +593,113 @@ impl fmt::Display for Imm { | |
} | ||
} | ||
|
||
pub struct Vex {} | ||
pub struct Vex { | ||
pub opcodes: Opcodes, | ||
pub w: bool, | ||
pub length: VexLength, | ||
pub mmmmm: VexMMMMM, | ||
pub pp: VexPP, | ||
pub imm: Option<u8>, | ||
} | ||
|
||
#[derive(PartialEq)] | ||
pub enum VexPP { | ||
None, | ||
/// Operand size override -- here, denoting "16-bit operation". | ||
_66, | ||
/// REPNE, but no specific meaning here -- is just an opcode extension. | ||
_F2, | ||
/// REP/REPE, but no specific meaning here -- is just an opcode extension. | ||
_F3, | ||
} | ||
|
||
impl fmt::Display for VexPP { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
VexPP::None => write!(f, "None"), | ||
VexPP::_66 => write!(f, "_66"), | ||
VexPP::_F3 => write!(f, "_F3"), | ||
VexPP::_F2 => write!(f, "_F2"), | ||
} | ||
} | ||
} | ||
|
||
pub enum VexLength { | ||
_128, | ||
} | ||
|
||
impl fmt::Display for VexLength { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
VexLength::_128 => write!(f, "_128"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(PartialEq)] | ||
pub enum VexMMMMM { | ||
None, | ||
_OF, | ||
/// Operand size override -- here, denoting "16-bit operation". | ||
_OF3A, | ||
/// The lock prefix. | ||
_OF38, | ||
} | ||
|
||
impl fmt::Display for VexMMMMM { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
VexMMMMM::None => write!(f, "None"), | ||
VexMMMMM::_OF => write!(f, "_0F"), | ||
VexMMMMM::_OF3A => write!(f, "_OF3A"), | ||
VexMMMMM::_OF38 => write!(f, "_OF38"), | ||
} | ||
} | ||
} | ||
|
||
/// Describe the register index to use. This wrapper is a type-safe way to pass | ||
/// around the registers defined in `inst/regs.rs`. | ||
#[derive(Debug, Copy, Clone, Default)] | ||
pub struct Register(u8); | ||
impl From<u8> for Register { | ||
fn from(reg: u8) -> Self { | ||
debug_assert!(reg < 16); | ||
Self(reg) | ||
} | ||
} | ||
impl Into<u8> for Register { | ||
fn into(self) -> u8 { | ||
self.0 | ||
} | ||
} | ||
Comment on lines
+660
to
+674
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this, right? |
||
|
||
impl Vex { | ||
fn validate(&self) { | ||
todo!() | ||
pub fn length(self, length: VexLength) -> Self { | ||
Self { length, ..self } | ||
} | ||
pub fn pp(self, pp: VexPP) -> Self { | ||
Self { pp, ..self } | ||
} | ||
pub fn mmmmm(self, mmmmm: VexMMMMM) -> Self { | ||
Self { mmmmm, ..self } | ||
} | ||
|
||
fn validate(&self, _operands: &[Operand]) {} | ||
} | ||
|
||
impl From<Vex> for Encoding { | ||
fn from(vex: Vex) -> Encoding { | ||
Encoding::Vex(vex) | ||
} | ||
} | ||
|
||
impl fmt::Display for Vex { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "VEX")?; | ||
match self.length { | ||
VexLength::_128 => write!(f, ".128")?, | ||
} | ||
write!(f, " {:#04x}", self.opcodes.primary)?; | ||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For printing
VexPP
andVexMMMMM
, if we print using.
instead of_
then we can match the manual a bit more closely. E.g.,VEX.128.0F.WIG 58 /r
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the underscore is needed because this naming is not just used for printing, but it also used as part of syntax for an associated item:
where here the
LegacyPrefix::.66
is not valid syntax.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I can get around this. Will see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes .. could not remove but any suggestions to overcome the "expected identifier" error welcomed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you
write!(f, "66")
here, then we can add whatever prefix we want later,fmtln!(f, "_{}", vex.pp)
orfmtln!(f, ".{}", vex.pp)
.