1 #===-- AMDGPUGenInstrEnums.pl - Script for generating instruction enums ----===#
3 # The LLVM Compiler Infrastructure
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
8 #===-----------------------------------------------------------------------===#
10 # This perl script is used to generate the following files:
12 # 1. perl AMDGPUGenInstrEnums.pl td > AMDGPUInstrEnums.td
14 # This file contains Tablegen constants used for matching hw instructions
15 # from R600 and SI with functionally similar AMDIL instruction. It aslo
16 # contains definitions of floating point constants like pi (in hex notation)
17 # that are used in some of the shader patterns.
19 # 2. perl AMDGPUGenInstrEnums.pl h > AMDGPUInstrEnums.h
21 # This file contains cpp enums that match the constant values in
24 # 3. perl AMDGPUGenInstrEnums.pl inc > AMDGPUInstrEnums.include
26 # This file contains a function called GetRealAMDILOpcode which maps the
27 # constant values defined in AMDGPUInstrEnums.h to the corresponding AMDIL
29 #===-----------------------------------------------------------------------===#
34 my @F32_MULTICLASSES = qw
{
36 UnaryIntrinsicFloatScalar
41 my @I32_MULTICLASSES = qw
{
47 my @GENERATION_ENUM = qw
{
56 my $FILE_TYPE = $ARGV[0];
58 open AMDIL
, '<', 'AMDILInstructions.td';
60 my @INST_ENUMS = ('NONE', 'FEQ', 'FGE', 'FLT', 'FNE', 'MOVE_f32', 'MOVE_i32', 'FTOI', 'ITOF', 'UGT', 'IGE', 'INE', 'UGE', 'IEQ', 'BINARY_OR_i32', 'BINARY_NOT_i32', 'MIN_f32');
63 if ($_ =~ /defm\s+([A-Z_]+)\s+:\s+([A-Za-z0-9]+)</) {
64 if (grep {$_ eq $2} @F32_MULTICLASSES) {
65 push @INST_ENUMS, "$1\_f32";
67 } elsif (grep {$_ eq $2} @I32_MULTICLASSES) {
68 push @INST_ENUMS, "$1\_i32";
70 } elsif ($_ =~ /def\s+([A-Z_]+)(_[fi]32)/) {
71 push @INST_ENUMS, "$1$2";
75 if ($FILE_TYPE eq 'td') {
77 print_td_enum
('AMDILInst', 'AMDILInstEnums', 'field bits<16>', @INST_ENUMS);
79 print_td_enum
('AMDGPUGen', 'AMDGPUGenEnums', 'field bits<3>', @GENERATION_ENUM);
83 'TWO_PI' => '0x40c90fdb',
84 'TWO_PI_INV' => '0x3e22f983'
87 print "class Constants {\n";
88 foreach (keys(%constants)) {
89 print "int $_ = $constants{$_};\n";
92 print "def CONST : Constants;\n";
94 } elsif ($FILE_TYPE eq 'h') {
96 print "unsigned GetRealAMDILOpcode(unsigned internalOpcode) const;\n";
98 print_h_enum
('AMDILTblgenOpcode', @INST_ENUMS);
100 print_h_enum
('AMDGPUGen', @GENERATION_ENUM);
102 } elsif ($FILE_TYPE eq 'inc') {
103 print "unsigned AMDGPUInstrInfo::GetRealAMDILOpcode(unsigned internalOpcode) const\n{\n";
104 print " switch(internalOpcode) {\n";
105 #Start at 1 so we skip NONE
106 for (my $i = 1; $i < scalar(@INST_ENUMS); $i++) {
107 my $inst = $INST_ENUMS[$i];
108 print " case AMDGPUInstrInfo::$inst: return AMDIL::$inst;\n";
110 print " default: abort();\n";
116 my ($instance, $class, $field, @values) = @_;
118 print "class $class {\n";
120 for (my $i = 0; $i < scalar(@values); $i++) {
121 print " $field $values[$i] = $i;\n";
125 print "def $instance : $class;\n";
130 my ($enum, @list) = @_;
131 print "enum $enum {\n";
133 for (my $i = 0; $i < scalar(@list); $i++) {
134 print " $list[$i] = $i";