-
Notifications
You must be signed in to change notification settings - Fork 78
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
Proposal: add generic parameters to types to distinguish different data structures on different architectures #118
Comments
Thanks for filing the issue! I have thought about doing something like this before. I did not do this originally because I wanted to support "dynamic architecture". I like your idea of a I do not have the bandwidth to implement this myself, but feel free to start a PR (even if it's just a work-in-progress). I am happy to help without pointers/review. |
Thanks for your reply. I'm glad to try to help implement this draft. I think I can work on a new sub-module named |
There's not need to make a separate submodule--just put new code in whatever module makes sense. It's always easy to move it around anyway. |
This commit is the first commit among a series of patches that implement the compile-time architecture tag proposal in capstone-rust#118. It contains the following major changes: - Add the ArchTag trait. This trait indicates a "tag" type that represents a specific architecture. Its associated types specify specific data types corresponding to the architecture it represents. - Add ArchTag implementations for supported architectures, and a special DynamicArchTag that indicates the target architecture is unknown at compile time. - Update existing general type definitions (e.g. Capstone, Insn, etc.) to acquire a general arch tag type parameter.
Motivation
The link between core data types (such as
Capstone
,Insn
,InsnDetail
, etc.) and architecture-specific data types (the data types undercrate::arch
) is not that "explicit" on the typing. For example, to get the instruction ID on the x86 arch, we have to code:which is not easier for beginners to catch.
We can simply resolve the problem above by adding a method
arch_insn_id
that returns the corresponding instruction ID enum variants just like theInsnDetail::arch_detail
method:This leads to another slightly disturbing problem. We have to
match
against the return value ofarch_insn_id
to extract the x86-specific instruction ID, given that we are already confident about the architecture. This problem also arises when we call theInsnDetail::arch_detail
method or other methods with a similarly-typed return value.The Proposal
First of all, we can add a new trait that abstracts a specific architecture:
Then, we add a generic parameter to
Capstone
,Insn
andInsnDetail
that represents the architecture:Then, the methods mentioned in the motivation section can be typed in a more straight-forward way:
No more
match
es, as long as we're targeting a specific architecture. Also, beginners can find corresponding architecture-specific implementations just by looking at the typings. The instruction ID problem can be resolved accordingly.Unresolved Problems
When the target architecture cannot be determined during compile-time (when the disassembler is created by the
Capstone::new_raw
method), the generic parameters cannot be set to represent specific architecture. To resolve this problem, maybe we need to introduce aDynamicArch
that implementsArch
and represents the target architecture is determined during runtime.This proposal may be pre-mature, but I do think that it reveals some (possibly minor) problems.
The text was updated successfully, but these errors were encountered: