summaryrefslogtreecommitdiff
path: root/src/opcode-code-table.h
diff options
context:
space:
mode:
authorBen Smith <binji@chromium.org>2018-09-10 13:04:22 -0700
committerBen Smith <binji@chromium.org>2018-09-10 19:37:14 -0700
commitad864f79b7340f60fc3a58448055f77cb9a6b3e3 (patch)
tree6025617b1914cc25a2e6413a5f488496e1eae2a9 /src/opcode-code-table.h
parent0b5f3c8af75422c1ba17f322b0ab816ef6bc0a58 (diff)
downloadwabt-ad864f79b7340f60fc3a58448055f77cb9a6b3e3.tar.gz
wabt-ad864f79b7340f60fc3a58448055f77cb9a6b3e3.tar.bz2
wabt-ad864f79b7340f60fc3a58448055f77cb9a6b3e3.zip
Optimize interpreter and `Opcode::FromCode`
`Opcode::FromCode` calculated the opcode given a prefix/code pair by using lower_bound over the list of all `OpcodeInfo`s. This was happening for every instruction, which is incredibly slow. Since the interpreter's format is internal only, we can use any encoding we want, so it's simpler and faster to use the `Opcode::Enum` directly without calling `Opcode::FromCode`. `Opcode::FromCode` is also used when reading a binary file, so it should be optimized anyway. Instead of using the `infos_` table, which is indexed by the opcode's `enum_` value, we create a new statically-defined table that maps from prefix-code pair to its enum value. Unfortunately, this can't be done easily in C++ because it does not currently support designated array initializers, so this table is created in a C file instead, `opcode-code-table.c`.
Diffstat (limited to 'src/opcode-code-table.h')
-rw-r--r--src/opcode-code-table.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/opcode-code-table.h b/src/opcode-code-table.h
new file mode 100644
index 00000000..b223e161
--- /dev/null
+++ b/src/opcode-code-table.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2018 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef WABT_OPCODE_CODE_TABLE_H_
+#define WABT_OPCODE_CODE_TABLE_H_
+
+#include <stdlib.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define WABT_OPCODE_CODE_TABLE_SIZE 65536
+
+/* This structure is defined in C because C++ doesn't (yet) allow you to use
+ * designated array initializers, i.e. [10] = {foo}.
+ */
+extern uint32_t WabtOpcodeCodeTable[WABT_OPCODE_CODE_TABLE_SIZE];
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* WABT_OPCODE_CODE_TABLE_H_ */