diff options
author | Mike Pall <mike> | 2019-12-08 20:53:31 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2019-12-08 20:53:31 +0100 |
commit | 70f4b15ee45a6137fe6b48b941faea79d72f7159 (patch) | |
tree | ee2fa03a0ee8dd1ec712820682ad2ed09a03e40b /src | |
parent | 92fa45f9eb5d9aa732c6b972a85c26120d7612b4 (diff) | |
download | luajit-70f4b15ee45a6137fe6b48b941faea79d72f7159.tar.gz luajit-70f4b15ee45a6137fe6b48b941faea79d72f7159.tar.bz2 luajit-70f4b15ee45a6137fe6b48b941faea79d72f7159.zip |
FFI: Eliminate hardcoded string hashes.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib_ffi.c | 35 | ||||
-rw-r--r-- | src/lj_cparse.c | 77 | ||||
-rw-r--r-- | src/lj_cparse.h | 2 |
3 files changed, 67 insertions, 47 deletions
diff --git a/src/lib_ffi.c b/src/lib_ffi.c index 8032411e..b3da1f3e 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c | |||
@@ -720,50 +720,47 @@ LJLIB_CF(ffi_fill) LJLIB_REC(.) | |||
720 | return 0; | 720 | return 0; |
721 | } | 721 | } |
722 | 722 | ||
723 | #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) | ||
724 | |||
725 | /* Test ABI string. */ | 723 | /* Test ABI string. */ |
726 | LJLIB_CF(ffi_abi) LJLIB_REC(.) | 724 | LJLIB_CF(ffi_abi) LJLIB_REC(.) |
727 | { | 725 | { |
728 | GCstr *s = lj_lib_checkstr(L, 1); | 726 | GCstr *s = lj_lib_checkstr(L, 1); |
729 | int b = 0; | 727 | int b = lj_cparse_case(s, |
730 | switch (s->hash) { | ||
731 | #if LJ_64 | 728 | #if LJ_64 |
732 | case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */ | 729 | "\00564bit" |
733 | #else | 730 | #else |
734 | case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */ | 731 | "\00532bit" |
735 | #endif | 732 | #endif |
736 | #if LJ_ARCH_HASFPU | 733 | #if LJ_ARCH_HASFPU |
737 | case H_(e33ee463,e33ee463): b = 1; break; /* fpu */ | 734 | "\003fpu" |
738 | #endif | 735 | #endif |
739 | #if LJ_ABI_SOFTFP | 736 | #if LJ_ABI_SOFTFP |
740 | case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */ | 737 | "\006softfp" |
741 | #else | 738 | #else |
742 | case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */ | 739 | "\006hardfp" |
743 | #endif | 740 | #endif |
744 | #if LJ_ABI_EABI | 741 | #if LJ_ABI_EABI |
745 | case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */ | 742 | "\004eabi" |
746 | #endif | 743 | #endif |
747 | #if LJ_ABI_WIN | 744 | #if LJ_ABI_WIN |
748 | case H_(4ab624a8,4ab624a8): b = 1; break; /* win */ | 745 | "\003win" |
749 | #endif | 746 | #endif |
750 | #if LJ_TARGET_UWP | 747 | #if LJ_TARGET_UWP |
751 | case H_(a40f0bcb,a40f0bcb): b = 1; break; /* uwp */ | 748 | "\003uwp" |
749 | #endif | ||
750 | #if LJ_LE | ||
751 | "\002le" | ||
752 | #else | ||
753 | "\002be" | ||
752 | #endif | 754 | #endif |
753 | case H_(3af93066,1f001464): b = 1; break; /* le/be */ | ||
754 | #if LJ_GC64 | 755 | #if LJ_GC64 |
755 | case H_(9e89d2c9,13c83c92): b = 1; break; /* gc64 */ | 756 | "\004gc64" |
756 | #endif | 757 | #endif |
757 | default: | 758 | ) >= 0; |
758 | break; | ||
759 | } | ||
760 | setboolV(L->top-1, b); | 759 | setboolV(L->top-1, b); |
761 | setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ | 760 | setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ |
762 | return 1; | 761 | return 1; |
763 | } | 762 | } |
764 | 763 | ||
765 | #undef H_ | ||
766 | |||
767 | LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ | 764 | LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ |
768 | 765 | ||
769 | LJLIB_CF(ffi_metatype) | 766 | LJLIB_CF(ffi_metatype) |
diff --git a/src/lj_cparse.c b/src/lj_cparse.c index 19f632ff..a5a15da0 100644 --- a/src/lj_cparse.c +++ b/src/lj_cparse.c | |||
@@ -28,6 +28,24 @@ | |||
28 | ** If in doubt, please check the input against your favorite C compiler. | 28 | ** If in doubt, please check the input against your favorite C compiler. |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /* -- Miscellaneous ------------------------------------------------------- */ | ||
32 | |||
33 | /* Match string against a C literal. */ | ||
34 | #define cp_str_is(str, k) \ | ||
35 | ((str)->len == sizeof(k)-1 && !memcmp(strdata(str), k, sizeof(k)-1)) | ||
36 | |||
37 | /* Check string against a linear list of matches. */ | ||
38 | int lj_cparse_case(GCstr *str, const char *match) | ||
39 | { | ||
40 | MSize len; | ||
41 | int n; | ||
42 | for (n = 0; (len = (MSize)*match++); n++, match += len) { | ||
43 | if (str->len == len && !memcmp(match, strdata(str), len)) | ||
44 | return n; | ||
45 | } | ||
46 | return -1; | ||
47 | } | ||
48 | |||
31 | /* -- C lexer ------------------------------------------------------------- */ | 49 | /* -- C lexer ------------------------------------------------------------- */ |
32 | 50 | ||
33 | /* C lexer token names. */ | 51 | /* C lexer token names. */ |
@@ -939,8 +957,6 @@ static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl) | |||
939 | 957 | ||
940 | /* -- C declaration parser ------------------------------------------------ */ | 958 | /* -- C declaration parser ------------------------------------------------ */ |
941 | 959 | ||
942 | #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) | ||
943 | |||
944 | /* Reset declaration state to declaration specifier. */ | 960 | /* Reset declaration state to declaration specifier. */ |
945 | static void cp_decl_reset(CPDecl *decl) | 961 | static void cp_decl_reset(CPDecl *decl) |
946 | { | 962 | { |
@@ -1069,44 +1085,57 @@ static void cp_decl_gccattribute(CPState *cp, CPDecl *decl) | |||
1069 | if (cp->tok == CTOK_IDENT) { | 1085 | if (cp->tok == CTOK_IDENT) { |
1070 | GCstr *attrstr = cp->str; | 1086 | GCstr *attrstr = cp->str; |
1071 | cp_next(cp); | 1087 | cp_next(cp); |
1072 | switch (attrstr->hash) { | 1088 | switch (lj_cparse_case(attrstr, |
1073 | case H_(64a9208e,8ce14319): case H_(8e6331b2,95a282af): /* aligned */ | 1089 | "\007aligned" "\013__aligned__" |
1090 | "\006packed" "\012__packed__" | ||
1091 | "\004mode" "\010__mode__" | ||
1092 | "\013vector_size" "\017__vector_size__" | ||
1093 | #if LJ_TARGET_X86 | ||
1094 | "\007regparm" "\013__regparm__" | ||
1095 | "\005cdecl" "\011__cdecl__" | ||
1096 | "\010thiscall" "\014__thiscall__" | ||
1097 | "\010fastcall" "\014__fastcall__" | ||
1098 | "\007stdcall" "\013__stdcall__" | ||
1099 | "\012sseregparm" "\016__sseregparm__" | ||
1100 | #endif | ||
1101 | )) { | ||
1102 | case 0: case 1: /* aligned */ | ||
1074 | cp_decl_align(cp, decl); | 1103 | cp_decl_align(cp, decl); |
1075 | break; | 1104 | break; |
1076 | case H_(42eb47de,f0ede26c): case H_(29f48a09,cf383e0c): /* packed */ | 1105 | case 2: case 3: /* packed */ |
1077 | decl->attr |= CTFP_PACKED; | 1106 | decl->attr |= CTFP_PACKED; |
1078 | break; | 1107 | break; |
1079 | case H_(0a84eef6,8dfab04c): case H_(995cf92c,d5696591): /* mode */ | 1108 | case 4: case 5: /* mode */ |
1080 | cp_decl_mode(cp, decl); | 1109 | cp_decl_mode(cp, decl); |
1081 | break; | 1110 | break; |
1082 | case H_(0ab31997,2d5213fa): case H_(bf875611,200e9990): /* vector_size */ | 1111 | case 6: case 7: /* vector_size */ |
1083 | { | 1112 | { |
1084 | CTSize vsize = cp_decl_sizeattr(cp); | 1113 | CTSize vsize = cp_decl_sizeattr(cp); |
1085 | if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); | 1114 | if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); |
1086 | } | 1115 | } |
1087 | break; | 1116 | break; |
1088 | #if LJ_TARGET_X86 | 1117 | #if LJ_TARGET_X86 |
1089 | case H_(5ad22db8,c689b848): case H_(439150fa,65ea78cb): /* regparm */ | 1118 | case 8: case 9: /* regparm */ |
1090 | CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); | 1119 | CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); |
1091 | decl->fattr |= CTFP_CCONV; | 1120 | decl->fattr |= CTFP_CCONV; |
1092 | break; | 1121 | break; |
1093 | case H_(18fc0b98,7ff4c074): case H_(4e62abed,0a747424): /* cdecl */ | 1122 | case 10: case 11: /* cdecl */ |
1094 | CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); | 1123 | CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); |
1095 | decl->fattr |= CTFP_CCONV; | 1124 | decl->fattr |= CTFP_CCONV; |
1096 | break; | 1125 | break; |
1097 | case H_(72b2e41b,494c5a44): case H_(f2356d59,f25fc9bd): /* thiscall */ | 1126 | case 12: case 13: /* thiscall */ |
1098 | CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); | 1127 | CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); |
1099 | decl->fattr |= CTFP_CCONV; | 1128 | decl->fattr |= CTFP_CCONV; |
1100 | break; | 1129 | break; |
1101 | case H_(0d0ffc42,ab746f88): case H_(21c54ba1,7f0ca7e3): /* fastcall */ | 1130 | case 14: case 15: /* fastcall */ |
1102 | CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); | 1131 | CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); |
1103 | decl->fattr |= CTFP_CCONV; | 1132 | decl->fattr |= CTFP_CCONV; |
1104 | break; | 1133 | break; |
1105 | case H_(ef76b040,9412e06a): case H_(de56697b,c750e6e1): /* stdcall */ | 1134 | case 16: case 17: /* stdcall */ |
1106 | CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); | 1135 | CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); |
1107 | decl->fattr |= CTFP_CCONV; | 1136 | decl->fattr |= CTFP_CCONV; |
1108 | break; | 1137 | break; |
1109 | case H_(ea78b622,f234bd8e): case H_(252ffb06,8d50f34b): /* sseregparm */ | 1138 | case 18: case 19: /* sseregparm */ |
1110 | decl->fattr |= CTF_SSEREGPARM; | 1139 | decl->fattr |= CTF_SSEREGPARM; |
1111 | decl->fattr |= CTFP_CCONV; | 1140 | decl->fattr |= CTFP_CCONV; |
1112 | break; | 1141 | break; |
@@ -1138,16 +1167,13 @@ static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl) | |||
1138 | while (cp->tok == CTOK_IDENT) { | 1167 | while (cp->tok == CTOK_IDENT) { |
1139 | GCstr *attrstr = cp->str; | 1168 | GCstr *attrstr = cp->str; |
1140 | cp_next(cp); | 1169 | cp_next(cp); |
1141 | switch (attrstr->hash) { | 1170 | if (cp_str_is(attrstr, "align")) { |
1142 | case H_(bc2395fa,98f267f8): /* align */ | ||
1143 | cp_decl_align(cp, decl); | 1171 | cp_decl_align(cp, decl); |
1144 | break; | 1172 | } else { /* Ignore all other attributes. */ |
1145 | default: /* Ignore all other attributes. */ | ||
1146 | if (cp_opt(cp, '(')) { | 1173 | if (cp_opt(cp, '(')) { |
1147 | while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); | 1174 | while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); |
1148 | cp_check(cp, ')'); | 1175 | cp_check(cp, ')'); |
1149 | } | 1176 | } |
1150 | break; | ||
1151 | } | 1177 | } |
1152 | } | 1178 | } |
1153 | cp_check(cp, ')'); | 1179 | cp_check(cp, ')'); |
@@ -1727,17 +1753,16 @@ static CTypeID cp_decl_abstract(CPState *cp) | |||
1727 | static void cp_pragma(CPState *cp, BCLine pragmaline) | 1753 | static void cp_pragma(CPState *cp, BCLine pragmaline) |
1728 | { | 1754 | { |
1729 | cp_next(cp); | 1755 | cp_next(cp); |
1730 | if (cp->tok == CTOK_IDENT && | 1756 | if (cp->tok == CTOK_IDENT && cp_str_is(cp->str, "pack")) { |
1731 | cp->str->hash == H_(e79b999f,42ca3e85)) { /* pack */ | ||
1732 | cp_next(cp); | 1757 | cp_next(cp); |
1733 | cp_check(cp, '('); | 1758 | cp_check(cp, '('); |
1734 | if (cp->tok == CTOK_IDENT) { | 1759 | if (cp->tok == CTOK_IDENT) { |
1735 | if (cp->str->hash == H_(738e923c,a1b65954)) { /* push */ | 1760 | if (cp_str_is(cp->str, "push")) { |
1736 | if (cp->curpack < CPARSE_MAX_PACKSTACK) { | 1761 | if (cp->curpack < CPARSE_MAX_PACKSTACK) { |
1737 | cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; | 1762 | cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; |
1738 | cp->curpack++; | 1763 | cp->curpack++; |
1739 | } | 1764 | } |
1740 | } else if (cp->str->hash == H_(6c71cf27,6c71cf27)) { /* pop */ | 1765 | } else if (cp_str_is(cp->str, "pop")) { |
1741 | if (cp->curpack > 0) cp->curpack--; | 1766 | if (cp->curpack > 0) cp->curpack--; |
1742 | } else { | 1767 | } else { |
1743 | cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); | 1768 | cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); |
@@ -1786,13 +1811,11 @@ static void cp_decl_multi(CPState *cp) | |||
1786 | if (tok == CTOK_INTEGER) { | 1811 | if (tok == CTOK_INTEGER) { |
1787 | cp_line(cp, hashline); | 1812 | cp_line(cp, hashline); |
1788 | continue; | 1813 | continue; |
1789 | } else if (tok == CTOK_IDENT && | 1814 | } else if (tok == CTOK_IDENT && cp_str_is(cp->str, "line")) { |
1790 | cp->str->hash == H_(187aab88,fcb60b42)) { /* line */ | ||
1791 | if (cp_next(cp) != CTOK_INTEGER) cp_err_token(cp, tok); | 1815 | if (cp_next(cp) != CTOK_INTEGER) cp_err_token(cp, tok); |
1792 | cp_line(cp, hashline); | 1816 | cp_line(cp, hashline); |
1793 | continue; | 1817 | continue; |
1794 | } else if (tok == CTOK_IDENT && | 1818 | } else if (tok == CTOK_IDENT && cp_str_is(cp->str, "pragma")) { |
1795 | cp->str->hash == H_(f5e6b4f8,1d509107)) { /* pragma */ | ||
1796 | cp_pragma(cp, hashline); | 1819 | cp_pragma(cp, hashline); |
1797 | continue; | 1820 | continue; |
1798 | } else { | 1821 | } else { |
@@ -1863,8 +1886,6 @@ static void cp_decl_single(CPState *cp) | |||
1863 | if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); | 1886 | if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); |
1864 | } | 1887 | } |
1865 | 1888 | ||
1866 | #undef H_ | ||
1867 | |||
1868 | /* ------------------------------------------------------------------------ */ | 1889 | /* ------------------------------------------------------------------------ */ |
1869 | 1890 | ||
1870 | /* Protected callback for C parser. */ | 1891 | /* Protected callback for C parser. */ |
diff --git a/src/lj_cparse.h b/src/lj_cparse.h index bad1060b..e40b4047 100644 --- a/src/lj_cparse.h +++ b/src/lj_cparse.h | |||
@@ -60,6 +60,8 @@ typedef struct CPState { | |||
60 | 60 | ||
61 | LJ_FUNC int lj_cparse(CPState *cp); | 61 | LJ_FUNC int lj_cparse(CPState *cp); |
62 | 62 | ||
63 | LJ_FUNC int lj_cparse_case(GCstr *str, const char *match); | ||
64 | |||
63 | #endif | 65 | #endif |
64 | 66 | ||
65 | #endif | 67 | #endif |