Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: src/trusted/validator_ragel/unreviewed/gen-dfa.cc

Issue 9968039: Add ragel machine generators to SCONS (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <getopt.h> 8 #include <getopt.h>
9 #include <libgen.h> 9 #include <libgen.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <stdlib.h> 11 #include <stdlib.h>
12 #include <string.h> 12 #include <string.h>
13 13
14 #include <algorithm> 14 #include <algorithm>
15 #include <iterator>
15 #include <map> 16 #include <map>
16 #include <set> 17 #include <set>
17 #include <string> 18 #include <string>
18 #include <tuple> 19 #include <tuple>
19 #include <vector> 20 #include <vector>
20 21
22 #ifndef NACL_TRUSTED_BUT_NOT_TCB
23 #error("This file is not meant for use in the TCB")
24 #endif
25
26 #ifdef __GNUC__
27 /* We use operand number formats here. They are defined in SUSv2 and supported
28 on all POSIX systems (including all versions of Linux and MacOS), yet
29 -pedantic warns about them. */
30 #pragma GCC diagnostic ignored "-Wformat"
31 /* GCC complains even when we explicitly use empty initializer list. Can be
32 easily fixed with data member initializers, but this requires GCC 4.7+. */
33 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
34 /* Default is perfectly valid way to handle missing cases. Especially if we
35 only care about very few non-default ones as often here. */
36 #pragma GCC diagnostic ignored "-Wswitch-enum"
37 #pragma GCC diagnostic error "-Wswitch"
38 #endif
39
21 template <typename T, size_t N> 40 template <typename T, size_t N>
22 char (&ArraySizeHelper(T (&array)[N]))[N]; 41 char (&ArraySizeHelper(T (&array)[N]))[N];
23 #define arraysize(array) (sizeof(ArraySizeHelper(array))) 42 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
24 43
25 namespace { 44 namespace {
26 const char* short_program_name; 45 const char* short_program_name;
27 46
28 const struct option kProgramOptions[] = { 47 const struct option kProgramOptions[] = {
29 {"mode", required_argument, NULL, 'm'}, 48 {"mode", required_argument, NULL, 'm'},
30 {"disable", required_argument, NULL, 'd'}, 49 {"disable", required_argument, NULL, 'd'},
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 /* Flags for enabling/disabling based on architecture and validity. */ 201 /* Flags for enabling/disabling based on architecture and validity. */
183 "ia32", 202 "ia32",
184 "amd64", 203 "amd64",
185 "nacl-ia32-forbidden", 204 "nacl-ia32-forbidden",
186 "nacl-amd64-forbidden", 205 "nacl-amd64-forbidden",
187 "nacl-forbidden" 206 "nacl-forbidden"
188 }; 207 };
189 208
190 class Instruction { 209 class Instruction {
191 public: 210 public:
192 static bool check_flag_valid(const std::string &flag) { 211 static void check_flag_valid(const std::string &flag) {
193 if (all_instruction_flags.find(flag) == all_instruction_flags.end()) { 212 if (all_instruction_flags.find(flag) == all_instruction_flags.end()) {
194 fprintf(stderr, "%s: unknown flag: '%s'\n", 213 fprintf(stderr, "%s: unknown flag: '%s'\n",
195 short_program_name, flag.c_str()); 214 short_program_name, flag.c_str());
196 exit(1); 215 exit(1);
197 } 216 }
198 } 217 }
199 218
200 void add_flag(const std::string &flag) { 219 void add_flag(const std::string &flag) {
201 check_flag_valid(flag); 220 check_flag_valid(flag);
202 flags.insert(flag); 221 flags.insert(flag);
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 } 580 }
562 } 581 }
563 return name; 582 return name;
564 } 583 }
565 584
566 void print_common_decoding(void) { 585 void print_common_decoding(void) {
567 if (enabled(Actions::kRelOperandAction)) { 586 if (enabled(Actions::kRelOperandAction)) {
568 fprintf(out_file, " action rel8_operand {\n" 587 fprintf(out_file, " action rel8_operand {\n"
569 " operand0 = JMP_TO;\n" 588 " operand0 = JMP_TO;\n"
570 " base = REG_RIP;\n" 589 " base = REG_RIP;\n"
571 " index = REG_NONE;\n" 590 " index = NO_REG;\n"
572 " scale = 0;\n" 591 " scale = 0;\n"
573 " disp_type = DISP8;\n" 592 " disp_type = DISP8;\n"
574 " disp = p;\n" 593 " disp = p;\n"
575 " }\n" 594 " }\n"
576 " action rel16_operand {\n" 595 " action rel16_operand {\n"
577 " operand0 = JMP_TO;\n" 596 " operand0 = JMP_TO;\n"
578 " base = REG_RIP;\n" 597 " base = REG_RIP;\n"
579 " index = REG_NONE;\n" 598 " index = NO_REG;\n"
580 " scale = 0;\n" 599 " scale = 0;\n"
581 " disp_type = DISP16;\n" 600 " disp_type = DISP16;\n"
582 " disp = p - 1;\n" 601 " disp = p - 1;\n"
583 " }\n" 602 " }\n"
584 " action rel32_operand {\n" 603 " action rel32_operand {\n"
585 " operand0 = JMP_TO;\n" 604 " operand0 = JMP_TO;\n"
586 " base = REG_RIP;\n" 605 " base = REG_RIP;\n"
587 " index = REG_NONE;\n" 606 " index = NO_REG;\n"
588 " scale = 0;\n" 607 " scale = 0;\n"
589 " disp_type = DISP32;\n" 608 " disp_type = DISP32;\n"
590 " disp = p - 3;\n" 609 " disp = p - 3;\n"
591 " }\n" 610 " }\n"
592 ""); 611 "");
593 } 612 }
594 fprintf(out_file, 613 fprintf(out_file,
595 " action branch_not_taken {\n" 614 " action branch_not_taken {\n"
596 " branch_not_taken = TRUE;\n" 615 " branch_not_taken = TRUE;\n"
597 " }\n" 616 " }\n"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 " action imm64_second_operand {\n" 697 " action imm64_second_operand {\n"
679 " imm2_operand = IMM64;\n" 698 " imm2_operand = IMM64;\n"
680 " imm2 = p - 7;\n" 699 " imm2 = p - 7;\n"
681 " }\n" 700 " }\n"
682 ""); 701 "");
683 } 702 }
684 if (enabled(Actions::kParseOperands)) { 703 if (enabled(Actions::kParseOperands)) {
685 if (ia32_mode) { 704 if (ia32_mode) {
686 fprintf(out_file, " action modrm_only_base {\n" 705 fprintf(out_file, " action modrm_only_base {\n"
687 " disp_type = DISPNONE;\n" 706 " disp_type = DISPNONE;\n"
688 " index = REG_NONE;\n" 707 " index = NO_REG;\n"
689 " base = (*p) & 0x07;\n" 708 " base = (*p) & 0x07;\n"
690 " scale = 0;\n" 709 " scale = 0;\n"
691 " }\n" 710 " }\n"
692 " action modrm_base_disp {\n" 711 " action modrm_base_disp {\n"
693 " index = REG_NONE;\n" 712 " index = NO_REG;\n"
694 " base = (*p) & 0x07;\n" 713 " base = (*p) & 0x07;\n"
695 " scale = 0;\n" 714 " scale = 0;\n"
696 " }\n" 715 " }\n"
697 " action modrm_pure_disp {\n" 716 " action modrm_pure_disp {\n"
698 " base = REG_NONE;\n" 717 " base = NO_REG;\n"
699 " index = REG_NONE;\n" 718 " index = NO_REG;\n"
700 " scale = 0;\n" 719 " scale = 0;\n"
701 " }\n" 720 " }\n"
702 " action modrm_pure_index {\n" 721 " action modrm_pure_index {\n"
703 " disp_type = DISPNONE;\n" 722 " disp_type = DISPNONE;\n"
704 " base = REG_NONE;\n" 723 " base = NO_REG;\n"
705 " index = index_registers[((*p) & 0x38) >> 3];\n" 724 " index = index_registers[((*p) & 0x38) >> 3];\n"
706 " scale = ((*p) & 0xc0) >> 6;\n" 725 " scale = ((*p) & 0xc0) >> 6;\n"
707 " }\n" 726 " }\n"
708 " action modrm_parse_sib {\n" 727 " action modrm_parse_sib {\n"
709 " disp_type = DISPNONE;\n" 728 " disp_type = DISPNONE;\n"
710 " base = (*p) & 0x7;\n" 729 " base = (*p) & 0x7;\n"
711 " index = index_registers[((*p) & 0x38) >> 3];\n" 730 " index = index_registers[((*p) & 0x38) >> 3];\n"
712 " scale = ((*p) & 0xc0) >> 6;\n" 731 " scale = ((*p) & 0xc0) >> 6;\n"
713 " }\n"); 732 " }\n");
714 } else { 733 } else {
715 fprintf(out_file, " action modrm_only_base {\n" 734 fprintf(out_file, " action modrm_only_base {\n"
716 " disp_type = DISPNONE;\n" 735 " disp_type = DISPNONE;\n"
717 " index = REG_NONE;\n" 736 " index = NO_REG;\n"
718 " base = ((*p) & 0x07) |\n" 737 " base = ((*p) & 0x07) |\n"
719 " ((rex_prefix & 0x01) << 3) |\n" 738 " ((rex_prefix & 0x01) << 3) |\n"
720 " (((~vex_prefix2) & 0x20) >> 2);\n" 739 " (((~vex_prefix2) & 0x20) >> 2);\n"
721 " scale = 0;\n" 740 " scale = 0;\n"
722 " }\n" 741 " }\n"
723 " action modrm_base_disp {\n" 742 " action modrm_base_disp {\n"
724 " index = REG_NONE;\n" 743 " index = NO_REG;\n"
725 " base = ((*p) & 0x07) |\n" 744 " base = ((*p) & 0x07) |\n"
726 " ((rex_prefix & 0x01) << 3) |\n" 745 " ((rex_prefix & 0x01) << 3) |\n"
727 " (((~vex_prefix2) & 0x20) >> 2);\n" 746 " (((~vex_prefix2) & 0x20) >> 2);\n"
728 " scale = 0;\n" 747 " scale = 0;\n"
729 " }\n" 748 " }\n"
730 " action modrm_rip {\n" 749 " action modrm_rip {\n"
731 " index = REG_NONE;\n" 750 " index = NO_REG;\n"
732 " base = REG_RIP;\n" 751 " base = REG_RIP;\n"
733 " scale = 0;\n" 752 " scale = 0;\n"
734 " }\n" 753 " }\n"
735 " action modrm_pure_index {\n" 754 " action modrm_pure_index {\n"
736 " disp_type = DISPNONE;\n" 755 " disp_type = DISPNONE;\n"
737 " base = REG_NONE;\n" 756 " base = NO_REG;\n"
738 " index = index_registers[(((*p) & 0x38) >> 3) |\n" 757 " index = index_registers[(((*p) & 0x38) >> 3) |\n"
739 " ((rex_prefix & 0x02) << 2) |\n" 758 " ((rex_prefix & 0x02) << 2) |\n"
740 " (((~vex_prefix2) & 0x40) >> 3)];\n" 759 " (((~vex_prefix2) & 0x40) >> 3)];\n"
741 " scale = ((*p) & 0xc0) >> 6;\n" 760 " scale = ((*p) & 0xc0) >> 6;\n"
742 " }\n" 761 " }\n"
743 " action modrm_parse_sib {\n" 762 " action modrm_parse_sib {\n"
744 " disp_type = DISPNONE;\n" 763 " disp_type = DISPNONE;\n"
745 " base = ((*p) & 0x7) |\n" 764 " base = ((*p) & 0x7) |\n"
746 " ((rex_prefix & 0x01) << 3) |\n" 765 " ((rex_prefix & 0x01) << 3) |\n"
747 " (((~vex_prefix2) & 0x20) >> 2);\n" 766 " (((~vex_prefix2) & 0x20) >> 2);\n"
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 static const T vex_fields[] = { 999 static const T vex_fields[] = {
981 T { "NONE", 0xe0 }, 1000 T { "NONE", 0xe0 },
982 T { "R", 0x60 }, 1001 T { "R", 0x60 },
983 T { "X", 0xa0 }, 1002 T { "X", 0xa0 },
984 T { "B", 0xc0 }, 1003 T { "B", 0xc0 },
985 T { "RX", 0x20 }, 1004 T { "RX", 0x20 },
986 T { "RB", 0x40 }, 1005 T { "RB", 0x40 },
987 T { "XB", 0x80 }, 1006 T { "XB", 0x80 },
988 T { "RXB", 0x00 } 1007 T { "RXB", 0x00 }
989 }; 1008 };
990 for (int vex_it = 0; vex_it < arraysize(vex_fields); ++vex_it) { 1009 for (size_t vex_it = 0; vex_it < arraysize(vex_fields); ++vex_it) {
991 auto vex = vex_fields[vex_it]; 1010 auto vex = vex_fields[vex_it];
992 fprintf(out_file, " VEX_%2$s = %3$s%1$s;\n" 1011 fprintf(out_file, " VEX_%2$s = %3$s%1$s;\n"
993 "", enabled(Actions::kVexPrefix) && !ia32_mode ? " @vex_prefix2" : "", 1012 "", enabled(Actions::kVexPrefix) && !ia32_mode ? " @vex_prefix2" : "",
994 vex.first, chartest((c & vex.second) == vex.second)); 1013 vex.first, chartest((c & vex.second) == vex.second));
995 } 1014 }
996 static const T vex_map[] = { 1015 static const T vex_map[] = {
997 T { "01", 1 }, 1016 T { "01", 1 },
998 T { "02", 2 }, 1017 T { "02", 2 },
999 T { "03", 3 }, 1018 T { "03", 3 },
1000 T { "08", 8 }, 1019 T { "08", 8 },
1001 T { "09", 9 }, 1020 T { "09", 9 },
1002 T { "0A", 10 }, 1021 T { "0A", 10 },
1003 T { "00001", 1 }, 1022 T { "00001", 1 },
1004 T { "00010", 2 }, 1023 T { "00010", 2 },
1005 T { "00011", 3 }, 1024 T { "00011", 3 },
1006 T { "01000", 8 }, 1025 T { "01000", 8 },
1007 T { "01001", 9 }, 1026 T { "01001", 9 },
1008 T { "01010", 10 }, 1027 T { "01010", 10 },
1009 }; 1028 };
1010 for (int vex_it = 0; vex_it < arraysize(vex_map); ++vex_it) { 1029 for (size_t vex_it = 0; vex_it < arraysize(vex_map); ++vex_it) {
1011 auto vex = vex_map[vex_it]; 1030 auto vex = vex_map[vex_it];
1012 fprintf(out_file, " VEX_map%1$s = %2$s;\n" 1031 fprintf(out_file, " VEX_map%1$s = %2$s;\n"
1013 "", vex.first, chartest((c & 0x1f) == vex.second)); 1032 "", vex.first, chartest((c & 0x1f) == vex.second));
1014 } 1033 }
1015 if (enabled(Actions::kOpcode)) { 1034 if (enabled(Actions::kOpcode)) {
1016 fprintf(out_file, "\n" 1035 fprintf(out_file, "\n"
1017 " action begin_opcode {\n" 1036 " action begin_opcode {\n"
1018 " begin_opcode = p;\n" 1037 " begin_opcode = p;\n"
1019 " }\n" 1038 " }\n"
1020 " action end_opcode {\n" 1039 " action end_opcode {\n"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 T { "x87", "ST" }, 1074 T { "x87", "ST" },
1056 T { "mmx", "MMX" }, 1075 T { "mmx", "MMX" },
1057 T { "xmm", "XMM" }, 1076 T { "xmm", "XMM" },
1058 T { "ymm", "YMM" }, 1077 T { "ymm", "YMM" },
1059 T { "farptr", "FarPtr" }, 1078 T { "farptr", "FarPtr" },
1060 T { "segreg", "SegmentRegister" }, 1079 T { "segreg", "SegmentRegister" },
1061 T { "creg", "ControlRegister" }, 1080 T { "creg", "ControlRegister" },
1062 T { "dreg", "DebugRegister" }, 1081 T { "dreg", "DebugRegister" },
1063 T { "selector", "Selector" } 1082 T { "selector", "Selector" }
1064 }; 1083 };
1065 for (int size_it = 0; size_it < arraysize(sizes); ++size_it) { 1084 for (size_t size_it = 0; size_it < arraysize(sizes); ++size_it) {
1066 auto size = sizes[size_it]; 1085 auto size = sizes[size_it];
1067 fprintf(out_file, " action operand%1$d_%2$s {\n" 1086 fprintf(out_file, " action operand%1$d_%2$s {\n"
1068 " operand%1$d_type = Operand%3$s;\n" 1087 " operand%1$d_type = Operand%3$s;\n"
1069 " }\n" 1088 " }\n"
1070 "", i, size.first, size.second); 1089 "", i, size.first, size.second);
1071 } 1090 }
1072 if (ia32_mode) { 1091 if (ia32_mode) {
1073 fprintf(out_file, " action operand%1$d_absolute_disp {\n" 1092 fprintf(out_file, " action operand%1$d_absolute_disp {\n"
1074 " operand%1$d = REG_RM;\n" 1093 " operand%1$d = REG_RM;\n"
1075 " base = REG_NONE;\n" 1094 " base = NO_REG;\n"
1076 " index = REG_NONE;\n" 1095 " index = NO_REG;\n"
1077 " scale = 0;\n" 1096 " scale = 0;\n"
1078 " }\n" 1097 " }\n"
1079 " action operand%1$d_from_opcode {\n" 1098 " action operand%1$d_from_opcode {\n"
1080 " operand%1$d = (*p) & 0x7;\n" 1099 " operand%1$d = (*p) & 0x7;\n"
1081 " }\n" 1100 " }\n"
1082 " action operand%1$d_from_is4 {\n" 1101 " action operand%1$d_from_is4 {\n"
1083 " operand%1$d = p[0] >> 4;\n" 1102 " operand%1$d = p[0] >> 4;\n"
1084 " }\n" 1103 " }\n"
1085 " action operand%1$d_from_modrm_rm {\n" 1104 " action operand%1$d_from_modrm_rm {\n"
1086 " operand%1$d = (*p) & 0x07;\n" 1105 " operand%1$d = (*p) & 0x07;\n"
1087 " }\n" 1106 " }\n"
1088 " action operand%1$d_from_modrm_reg {\n" 1107 " action operand%1$d_from_modrm_reg {\n"
1089 " operand%1$d = ((*p) & 0x38) >> 3;\n" 1108 " operand%1$d = ((*p) & 0x38) >> 3;\n"
1090 " }\n" 1109 " }\n"
1091 " action operand%1$d_from_modrm_reg_norex {\n" 1110 " action operand%1$d_from_modrm_reg_norex {\n"
1092 " operand%1$d = ((*p) & 0x38) >> 3;\n" 1111 " operand%1$d = ((*p) & 0x38) >> 3;\n"
1093 " }\n" 1112 " }\n"
1094 " action operand%1$d_from_vex {\n" 1113 " action operand%1$d_from_vex {\n"
1095 " operand%1$d = ((~vex_prefix3) & 0x38) >> 3;\n" 1114 " operand%1$d = ((~vex_prefix3) & 0x38) >> 3;\n"
1096 " }\n" 1115 " }\n"
1097 "", i); 1116 "", i);
1098 } else { 1117 } else {
1099 fprintf(out_file, " action operand%1$d_absolute_disp {\n" 1118 fprintf(out_file, " action operand%1$d_absolute_disp {\n"
1100 " operand%1$d = REG_RM;\n" 1119 " operand%1$d = REG_RM;\n"
1101 " base = REG_NONE;\n" 1120 " base = NO_REG;\n"
1102 " index = REG_RIZ;\n" 1121 " index = REG_RIZ;\n"
1103 " scale = 0;\n" 1122 " scale = 0;\n"
1104 " }\n" 1123 " }\n"
1105 " action operand%1$d_from_opcode {\n" 1124 " action operand%1$d_from_opcode {\n"
1106 " operand%1$d = ((*p) & 0x7) |\n" 1125 " operand%1$d = ((*p) & 0x7) |\n"
1107 " ((rex_prefix & 0x01) << 3) |\n" 1126 " ((rex_prefix & 0x01) << 3) |\n"
1108 " (((~vex_prefix2) & 0x20) >> 2);\n" 1127 " (((~vex_prefix2) & 0x20) >> 2);\n"
1109 " }\n" 1128 " }\n"
1110 " action operand%1$d_from_is4 {\n" 1129 " action operand%1$d_from_is4 {\n"
1111 " operand%1$d = p[0] >> 4;\n" 1130 " operand%1$d = p[0] >> 4;\n"
(...skipping 22 matching lines...) Expand all
1134 T { "es_rdi", "REG_ES_RDI" }, 1153 T { "es_rdi", "REG_ES_RDI" },
1135 T { "immediate", "REG_IMM" }, 1154 T { "immediate", "REG_IMM" },
1136 T { "second_immediate", "REG_IMM2" }, 1155 T { "second_immediate", "REG_IMM2" },
1137 T { "port_dx", "REG_PORT_DX" }, 1156 T { "port_dx", "REG_PORT_DX" },
1138 T { "rax", "REG_RAX" }, 1157 T { "rax", "REG_RAX" },
1139 T { "rcx", "REG_RCX" }, 1158 T { "rcx", "REG_RCX" },
1140 T { "rdx", "REG_RDX" }, 1159 T { "rdx", "REG_RDX" },
1141 T { "rm", "REG_RM" }, 1160 T { "rm", "REG_RM" },
1142 T { "st", "REG_ST" } 1161 T { "st", "REG_ST" }
1143 }; 1162 };
1144 for (int type_it = 0; type_it < arraysize(types); ++type_it) { 1163 for (size_t type_it = 0; type_it < arraysize(types); ++type_it) {
1145 auto type = types[type_it]; 1164 auto type = types[type_it];
1146 fprintf(out_file, " action operand%1$d_%2$s {\n" 1165 fprintf(out_file, " action operand%1$d_%2$s {\n"
1147 " operand%1$d = %3$s;\n" 1166 " operand%1$d = %3$s;\n"
1148 " }\n" 1167 " }\n"
1149 "", i, type.first, type.second); 1168 "", i, type.first, type.second);
1150 } 1169 }
1151 } 1170 }
1152 } 1171 }
1153 if (enabled(Actions::kParseOperandsStates)) { 1172 if (enabled(Actions::kParseOperandsStates)) {
1154 for (auto i = 0 ; i < 5; ++i) { 1173 for (auto i = 0 ; i < 5; ++i) {
1155 fprintf(out_file, " action operand%1$d_unused {\n" 1174 fprintf(out_file, " action operand%1$d_unused {\n"
1156 " operand%1$d_read = false;\n" 1175 " operand%1$d_read = FALSE;\n"
1157 " operand%1$d_write = false;\n" 1176 " operand%1$d_write = FALSE;\n"
1158 " }\n" 1177 " }\n"
1159 " action operand%1$d_read {\n" 1178 " action operand%1$d_read {\n"
1160 " operand%1$d_read = true;\n" 1179 " operand%1$d_read = TRUE;\n"
1161 " operand%1$d_write = false;\n" 1180 " operand%1$d_write = FALSE;\n"
1162 " }\n" 1181 " }\n"
1163 " action operand%1$d_write {\n" 1182 " action operand%1$d_write {\n"
1164 " operand%1$d_read = false;\n" 1183 " operand%1$d_read = FALSE;\n"
1165 " operand%1$d_write = true;\n" 1184 " operand%1$d_write = TRUE;\n"
1166 " }\n" 1185 " }\n"
1167 " action operand%1$d_readwrite {\n" 1186 " action operand%1$d_readwrite {\n"
1168 " operand%1$d_read = true;\n" 1187 " operand%1$d_read = TRUE;\n"
1169 " operand%1$d_write = true;\n" 1188 " operand%1$d_write = TRUE;\n"
1170 " }\n" 1189 " }\n"
1171 "", i); 1190 "", i);
1172 } 1191 }
1173 } 1192 }
1174 } 1193 }
1175 1194
1176 void print_name_actions(void) { 1195 void print_name_actions(void) {
1177 for (auto pair_it = instruction_names.begin(); 1196 for (auto pair_it = instruction_names.begin();
1178 pair_it != instruction_names.end(); ++pair_it) { 1197 pair_it != instruction_names.end(); ++pair_it) {
1179 auto &pair = *pair_it; 1198 auto &pair = *pair_it;
1180 fprintf(out_file, " action instruction_%s" 1199 fprintf(out_file, " action instruction_%s"
1181 " { instruction_name = instruction_names + %zd; }\n", 1200 " { instruction_name = instruction_names + %zd; }\n",
1182 c_identifier(pair.first).c_str(), pair.second); 1201 c_identifier(pair.first).c_str(), pair.second);
1183 } 1202 }
1184 } 1203 }
1185 1204
1186 class MarkedInstruction : Instruction { 1205 class MarkedInstruction : Instruction {
1187 public: 1206 public:
1188 /* Additional marks are created in the process of parsing. */ 1207 /* Additional marks are created in the process of parsing. */
1189 explicit MarkedInstruction(Instruction instruction_) : 1208 explicit MarkedInstruction(Instruction instruction_) :
1190 Instruction(instruction_), 1209 Instruction(instruction_),
1191 instruction_class(get_instruction_class(instruction_)), 1210 instruction_class(get_instruction_class(instruction_)),
1192 opcode_in_modrm(false), opcode_in_imm(false), rex { } { 1211 rex { }, opcode_in_modrm(false), opcode_in_imm(false) {
1193 if (has_flag("branch_hint")) { 1212 if (has_flag("branch_hint")) {
1194 optional_prefixes.insert("branch_hint"); 1213 optional_prefixes.insert("branch_hint");
1195 } 1214 }
1196 if (has_flag("condrep")) { 1215 if (has_flag("condrep")) {
1197 optional_prefixes.insert("condrep"); 1216 optional_prefixes.insert("condrep");
1198 } 1217 }
1199 if (has_flag("rep")) { 1218 if (has_flag("rep")) {
1200 optional_prefixes.insert("rep"); 1219 optional_prefixes.insert("rep");
1201 } 1220 }
1202 for (auto opcode_it = opcodes.begin(); 1221 for (auto opcode_it = opcodes.begin();
(...skipping 27 matching lines...) Expand all
1230 } 1249 }
1231 opcode->push_back(')'); 1250 opcode->push_back(')');
1232 if (saved_opcode == "0x97") { 1251 if (saved_opcode == "0x97") {
1233 opcode->erase(1, 5); 1252 opcode->erase(1, 5);
1234 } 1253 }
1235 break; 1254 break;
1236 case '8': 1255 case '8':
1237 (*opcode) = "("; 1256 (*opcode) = "(";
1238 opcode->append(saved_opcode); 1257 opcode->append(saved_opcode);
1239 static const char cc[] = {'9', 'a', 'b', 'c', 'd', 'e', 'f'}; 1258 static const char cc[] = {'9', 'a', 'b', 'c', 'd', 'e', 'f'};
1240 for (int c_it = 0; c_it < arraysize(cc); ++c_it) { 1259 for (size_t c_it = 0; c_it < arraysize(cc); ++c_it) {
1241 auto c = cc[c_it]; 1260 auto c = cc[c_it];
1242 opcode->push_back('|'); 1261 opcode->push_back('|');
1243 (*(saved_opcode.rbegin())) = c; 1262 (*(saved_opcode.rbegin())) = c;
1244 opcode->append(saved_opcode); 1263 opcode->append(saved_opcode);
1245 } 1264 }
1246 opcode->push_back(')'); 1265 opcode->push_back(')');
1247 break; 1266 break;
1248 default: 1267 default:
1249 fprintf(stderr, "%s: error - can not use 'r' operand in " 1268 fprintf(stderr, "%s: error - can not use 'r' operand in "
1250 "instruction '%s'", short_program_name, name.c_str()); 1269 "instruction '%s'", short_program_name, name.c_str());
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 if (saved_class == InstructionClass::kLSetUnsetDefaultRexW) { 1521 if (saved_class == InstructionClass::kLSetUnsetDefaultRexW) {
1503 instruction_class = InstructionClass::kRexW; 1522 instruction_class = InstructionClass::kRexW;
1504 print_one_size_definition_rexw(); 1523 print_one_size_definition_rexw();
1505 } 1524 }
1506 opcode[Lbit] = '0'; 1525 opcode[Lbit] = '0';
1507 auto saved_operands = operands; 1526 auto saved_operands = operands;
1508 for (auto operand_it = operands.begin(); 1527 for (auto operand_it = operands.begin();
1509 operand_it != operands.end(); ++operand_it) { 1528 operand_it != operands.end(); ++operand_it) {
1510 auto &operand = *operand_it; 1529 auto &operand = *operand_it;
1511 static const char cc[] = {'H', 'L', 'U', 'V', 'W'}; 1530 static const char cc[] = {'H', 'L', 'U', 'V', 'W'};
1512 for (int c_it = 0; c_it < arraysize(cc); ++c_it) { 1531 for (size_t c_it = 0; c_it < arraysize(cc); ++c_it) {
1513 auto c = cc[c_it]; 1532 auto c = cc[c_it];
1514 if ((operand.source == c) && 1533 if ((operand.source == c) &&
1515 (*(operand.size.rbegin()) == 'x') && 1534 (*(operand.size.rbegin()) == 'x') &&
1516 (((operand.size.length() > 1) && 1535 (((operand.size.length() > 1) &&
1517 (operand.size[0] == 'p')) || 1536 (operand.size[0] == 'p')) ||
1518 (operand.size.length() == 1))) { 1537 (operand.size.length() == 1))) {
1519 operand.size.resize(operand.size.length() - 1); 1538 operand.size.resize(operand.size.length() - 1);
1520 } 1539 }
1521 } 1540 }
1522 } 1541 }
(...skipping 18 matching lines...) Expand all
1541 } 1560 }
1542 } 1561 }
1543 1562
1544 void print_one_size_definition(void) { 1563 void print_one_size_definition(void) {
1545 /* 64bit commands are not supported in ia32 mode. */ 1564 /* 64bit commands are not supported in ia32 mode. */
1546 if (ia32_mode && rex.w) { 1565 if (ia32_mode && rex.w) {
1547 return; 1566 return;
1548 } 1567 }
1549 bool modrm_memory = false; 1568 bool modrm_memory = false;
1550 bool modrm_register = false; 1569 bool modrm_register = false;
1551 char operand_source; 1570 char operand_source = ' ';
1552 for (auto operand_it = operands.begin(); 1571 for (auto operand_it = operands.begin();
1553 operand_it != operands.end(); ++operand_it) { 1572 operand_it != operands.end(); ++operand_it) {
1554 auto &operand = *operand_it; 1573 auto &operand = *operand_it;
1555 static std::map<char, std::pair<bool, bool> > operand_map { 1574 static std::map<char, std::pair<bool, bool> > operand_map {
1556 { 'E', std::make_pair(true, true) }, 1575 { 'E', std::make_pair(true, true) },
1557 { 'M', std::make_pair(true, false) }, 1576 { 'M', std::make_pair(true, false) },
1558 { 'N', std::make_pair(false, true) }, 1577 { 'N', std::make_pair(false, true) },
1559 { 'Q', std::make_pair(true, true) }, 1578 { 'Q', std::make_pair(true, true) },
1560 { 'R', std::make_pair(false, true) }, 1579 { 'R', std::make_pair(false, true) },
1561 { 'U', std::make_pair(false, true) }, 1580 { 'U', std::make_pair(false, true) },
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1664 1683
1665 void print_one_size_definition_modrm_memory(void) { 1684 void print_one_size_definition_modrm_memory(void) {
1666 typedef std::tuple<const char *, bool, bool> T; 1685 typedef std::tuple<const char *, bool, bool> T;
1667 static const T modes[] = { 1686 static const T modes[] = {
1668 T { " operand_disp", false, true }, 1687 T { " operand_disp", false, true },
1669 T { " operand_rip", false, false }, 1688 T { " operand_rip", false, false },
1670 T { " single_register_memory", false, true }, 1689 T { " single_register_memory", false, true },
1671 T { " operand_sib_pure_index", true, false }, 1690 T { " operand_sib_pure_index", true, false },
1672 T { " operand_sib_base_index", true, true } 1691 T { " operand_sib_base_index", true, true }
1673 }; 1692 };
1674 for (int mode_it = 0; mode_it < arraysize(modes); ++mode_it) { 1693 for (size_t mode_it = 0; mode_it < arraysize(modes); ++mode_it) {
1675 auto mode = modes[mode_it]; 1694 auto mode = modes[mode_it];
1676 print_operator_delimiter(); 1695 print_operator_delimiter();
1677 if (mod_reg_is_used()) { 1696 if (mod_reg_is_used()) {
1678 rex.r = true; 1697 rex.r = true;
1679 } 1698 }
1680 if (mod_rm_is_used()) { 1699 if (mod_rm_is_used()) {
1681 rex.x = std::get<1>(mode); 1700 rex.x = std::get<1>(mode);
1682 rex.b = std::get<2>(mode); 1701 rex.b = std::get<2>(mode);
1683 } 1702 }
1684 print_legacy_prefixes(); 1703 print_legacy_prefixes();
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1744 1763
1745 bool mod_reg_is_used() { 1764 bool mod_reg_is_used() {
1746 for (auto operand_it = operands.begin(); 1765 for (auto operand_it = operands.begin();
1747 operand_it != operands.end(); ++operand_it) { 1766 operand_it != operands.end(); ++operand_it) {
1748 auto &operand = *operand_it; 1767 auto &operand = *operand_it;
1749 if (operand.source == 'C' && 1768 if (operand.source == 'C' &&
1750 required_prefixes.find("0xf0") == required_prefixes.end()) { 1769 required_prefixes.find("0xf0") == required_prefixes.end()) {
1751 return true; 1770 return true;
1752 } 1771 }
1753 static const char cc[] = { 'G', 'P', 'V' }; 1772 static const char cc[] = { 'G', 'P', 'V' };
1754 for (int c_it = 0; c_it < arraysize(cc); ++c_it) { 1773 for (size_t c_it = 0; c_it < arraysize(cc); ++c_it) {
1755 auto c = cc[c_it]; 1774 auto c = cc[c_it];
1756 if (operand.source == c) { 1775 if (operand.source == c) {
1757 return true; 1776 return true;
1758 } 1777 }
1759 } 1778 }
1760 } 1779 }
1761 return false; 1780 return false;
1762 } 1781 }
1763 1782
1764 bool mod_rm_is_used() { 1783 bool mod_rm_is_used() {
1765 for (auto operand_it = operands.begin(); 1784 for (auto operand_it = operands.begin();
1766 operand_it != operands.end(); ++operand_it) { 1785 operand_it != operands.end(); ++operand_it) {
1767 auto &operand = *operand_it; 1786 auto &operand = *operand_it;
1768 static const char cc[] = { 'E', 'M', 'N', 'Q', 'R', 'U', 'W' }; 1787 static const char cc[] = { 'E', 'M', 'N', 'Q', 'R', 'U', 'W' };
1769 for (int c_it = 0; c_it < arraysize(cc); ++c_it) { 1788 for (size_t c_it = 0; c_it < arraysize(cc); ++c_it) {
1770 auto c = cc[c_it]; 1789 auto c = cc[c_it];
1771 if (operand.source == c) { 1790 if (operand.source == c) {
1772 return true; 1791 return true;
1773 } 1792 }
1774 } 1793 }
1775 } 1794 }
1776 return false; 1795 return false;
1777 } 1796 }
1778 1797
1779 void print_legacy_prefixes(void) { 1798 void print_legacy_prefixes(void) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 if (ia32_mode || (!rex.r && !rex.x & !rex.b)) { 1929 if (ia32_mode || (!rex.r && !rex.x & !rex.b)) {
1911 fprintf(out_file, "NONE"); 1930 fprintf(out_file, "NONE");
1912 } else { 1931 } else {
1913 if (rex.r) fprintf(out_file, "R"); 1932 if (rex.r) fprintf(out_file, "R");
1914 if (rex.x) fprintf(out_file, "X"); 1933 if (rex.x) fprintf(out_file, "X");
1915 if (rex.b) fprintf(out_file, "B"); 1934 if (rex.b) fprintf(out_file, "B");
1916 } 1935 }
1917 fprintf(out_file, " & VEX_map%s) ", opcodes[1].c_str() + 4); 1936 fprintf(out_file, " & VEX_map%s) ", opcodes[1].c_str() + 4);
1918 auto third_byte = opcodes[2]; 1937 auto third_byte = opcodes[2];
1919 static const char* symbolic_names[] = { "cntl", "dest", "src1", "src" }; 1938 static const char* symbolic_names[] = { "cntl", "dest", "src1", "src" };
1920 for (int symbolic_it = 0; 1939 for (size_t symbolic_it = 0;
1921 symbolic_it < arraysize(symbolic_names); ++symbolic_it) { 1940 symbolic_it < arraysize(symbolic_names); ++symbolic_it) {
1922 auto symbolic = symbolic_names[symbolic_it]; 1941 auto symbolic = symbolic_names[symbolic_it];
1923 for (auto it = third_byte.begin(); it != third_byte.end(); ++it) { 1942 for (auto it = third_byte.begin(); it != third_byte.end(); ++it) {
1924 if ((third_byte.end() - it) >= strlen(symbolic) && 1943 auto symbolic_len = strlen(symbolic);
1944 if (static_cast<size_t>(third_byte.end() - it) >= symbolic_len &&
1925 !strncmp(&*it, symbolic, strlen(symbolic))) { 1945 !strncmp(&*it, symbolic, strlen(symbolic))) {
1926 third_byte.replace(it, it + strlen(symbolic), "XXXX"); 1946 third_byte.replace(it, it + strlen(symbolic), "XXXX");
1927 break; 1947 break;
1928 } 1948 }
1929 } 1949 }
1930 } 1950 }
1931 static const std::set<char> third_byte_check[] { 1951 static const std::set<char> third_byte_check[] {
1932 { '0', '1', 'x', 'X', 'W' }, 1952 { '0', '1', 'x', 'X', 'W' },
1933 { '.' }, 1953 { '.' },
1934 { '0', '1', 'x', 'X' }, 1954 { '0', '1', 'x', 'X' },
1935 { '0', '1', 'x', 'X' }, 1955 { '0', '1', 'x', 'X' },
1936 { '0', '1', 'x', 'X' }, 1956 { '0', '1', 'x', 'X' },
1937 { '0', '1', 'x', 'X' }, 1957 { '0', '1', 'x', 'X' },
1938 { '.' }, 1958 { '.' },
1939 { '0', '1', 'x', 'X' }, 1959 { '0', '1', 'x', 'X' },
1940 { '.' }, 1960 { '.' },
1941 { '0', '1' }, 1961 { '0', '1' },
1942 { '0', '1' } 1962 { '0', '1' }
1943 }; 1963 };
1944 auto third_byte_ok = (arraysize(third_byte_check) == 1964 auto third_byte_ok = (arraysize(third_byte_check) ==
1945 third_byte.length()); 1965 third_byte.length());
1946 if (third_byte_ok) { 1966 if (third_byte_ok) {
1947 for (int set_it = 0; set_it < arraysize(third_byte_check); ++set_it) { 1967 for (size_t set_it = 0; set_it < arraysize(third_byte_check);
1968 ++set_it) {
1948 auto &set = third_byte_check[set_it]; 1969 auto &set = third_byte_check[set_it];
1949 if (set.find(third_byte[&set - third_byte_check]) == set.end()) { 1970 if (set.find(third_byte[&set - third_byte_check]) == set.end()) {
1950 third_byte_ok = false; 1971 third_byte_ok = false;
1951 break; 1972 break;
1952 } 1973 }
1953 } 1974 }
1954 } 1975 }
1955 if (third_byte_ok) { 1976 if (third_byte_ok) {
1956 if (ia32_mode && third_byte[2] == 'X') { 1977 if (ia32_mode && third_byte[2] == 'X') {
1957 third_byte[2] = '1'; 1978 third_byte[2] = '1';
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
2486 2507
2487 fprintf(out_file, "\n one_instruction ="); 2508 fprintf(out_file, "\n one_instruction =");
2488 2509
2489 print_one_instruction_definition(); 2510 print_one_instruction_definition();
2490 2511
2491 fprintf(out_file, ");\n" 2512 fprintf(out_file, ");\n"
2492 "}%%%%\n" 2513 "}%%%%\n"
2493 ""); 2514 "");
2494 return 0; 2515 return 0;
2495 } 2516 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698