Some architectures have processor extensions not defined in upstream LLVM.
If we want to have them in Capstone, we can write the .td files for them here.
The process is rather straight forward.
- Write all definitions into a new
llvm/lib/Target/<ARCH>/<ARCH>NewExtension.tdfile. - Define a feature and property for the new extension:
def FeatureExtensionName : SubtargetFeature<"extenion_name", "HasExtensionName", "true", "Enable the new extension bla bla bla.">; def HasExtensionName : Predicate<"Subtarget->hasExtensionName()">, AssemblerPredicate<(all_of FeatureExtensionName), "extenion_name">;
- Ensure the newly defined instructions have
let Predicates = [HasExtensionName];set. - Add or exclude the extension from existing processor models:
- Search for
list<Predicate> UnsupportedFeaturesin the target directory. AddHasExtensionNameto all processor models which don't support it. - The processor models which do support it, need the instructions in the scheduler. Check how it is done in the processor's scheduler file.
- Search for
- Include the
<ARCH>NewExtension.tdat the bottom of<ARCH>InstrInfo.tdwithinclude "<ARCH>NewExtension.td".
Note: As an example you can refer to AArch64AppleProprietary.td.
Also search for HasAMX and FeatureAMX to see how those flags are used in the files.
Capstone needs to support features which were removed by LLVM in the past. Here we explain how to reintroduce them.
To get the old features back we copy them from the old .td files and include them in the new ones.
To include removed features from previous LLVM versions do the following:
-
Checkout the last LLVM version the feature was present.
-
Copy all feature related definitions into a
<ARCH>Deprecated.tdfile. -
Checkout the newest LLVM version again.
-
Wrap the different definition types in include guards. For example the
InstrInfodefinitions could be included in:#ifndef INCLUDED_CAPSTONE_DEPR_INSTR #ifdef CAPSTONE_DEPR_INSTR #define INCLUDED_CAPSTONE_DEPR_INSTR // Ensures it is only included once [Instruction definitions of removed feature] #endif // INCLUDED_CAPSTONE_DEPR_INSTR #endif // CAPSTONE_DEPR_INSTRNote that the order of
#ifndefand#ifdefmatters (otherwise you'll get an error fromtblgen).This step is somewhat optional. It might be enough to write all definitions in a single file and
#includethem once intoARCHInstrInfo.tdat the bottom. -
Include the definitions in the current definition files with:
#define CAPSTONE_DEPR_INSTR include "<ARCH>Deprecated.td"
- It is possible that you have to change some definitions slightly.
Because certain classes no longer exist or were replaced (e.g.:
GCCBuiltin->ClangBuiltin). - Some processors might need the feature flag (
Has<DeprecatedFeature>) added in theirUnsupportedFeatureslist.