|
| 1 | +/* |
| 2 | + __ __ .___ .__ |
| 3 | +/ \ / \_____ _______ __| _/_ __|__| ____ ____ |
| 4 | +\ \/\/ /\__ \\_ __ \/ __ | | \ |/ \ / _ \ |
| 5 | + \ / / __ \| | \/ /_/ | | / | | ( <_> ) |
| 6 | + \__/\ / (____ /__| \____ |____/|__|___| /\____/ |
| 7 | + \/ \/ \/ \/ |
| 8 | + |
| 9 | +WARDuino (c) by Christophe Scholliers & Robbert Gurdeep Singh |
| 10 | +
|
| 11 | +WARDuino is licensed under a |
| 12 | +Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. |
| 13 | +
|
| 14 | +You should have received a copy of the license along with this |
| 15 | +work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include "../WARDuino.h" |
| 19 | +#include <iostream> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +#define MAIN "_main" |
| 23 | +#define MAX_PATH 100 |
| 24 | +#define MAX_BYTE_CODE_SIZE 10000 |
| 25 | +#define BENCHMARK_PATH "./" |
| 26 | +#define append(buf, a, b) snprintf(buf, sizeof(buf), "%s%s", a, b) |
| 27 | +#define array_size(x) (sizeof(x) / sizeof((x)[0])) |
| 28 | + |
| 29 | +#define FOR_EACH(type, item, array) \ |
| 30 | + { \ |
| 31 | + for (size_t i = 0; i < array_size(array); i++) \ |
| 32 | + { \ |
| 33 | + type item = array[i]; |
| 34 | +#define END \ |
| 35 | + } \ |
| 36 | + } |
| 37 | + |
| 38 | +typedef char *string; |
| 39 | + |
| 40 | +void set_path(string path, string name) |
| 41 | +{ |
| 42 | + strncpy(path, BENCHMARK_PATH, array_size(path)); |
| 43 | + strncat(path, name, array_size(path)); |
| 44 | + strncat(path, "/wast/", array_size(path)); |
| 45 | + strncat(path, name, array_size(path)); |
| 46 | + strncat(path, ".wasm", array_size(path)); |
| 47 | +} |
| 48 | + |
| 49 | +unsigned int read_file_to_buf(unsigned char *bytes, string path) |
| 50 | +{ |
| 51 | + FILE *file = fopen(path, "r"); |
| 52 | + if (file == NULL) |
| 53 | + { |
| 54 | + fprintf(stderr, "Cannot open file: %s", path); |
| 55 | + exit(1); |
| 56 | + } |
| 57 | + fseek(file, 0L, SEEK_END); |
| 58 | + long num_bytes = ftell(file); |
| 59 | + if (num_bytes < MAX_BYTE_CODE_SIZE) |
| 60 | + { |
| 61 | + fseek(file, 0L, SEEK_SET); |
| 62 | + long result = fread(bytes, sizeof(char), num_bytes, file); |
| 63 | + if (result != num_bytes) |
| 64 | + { |
| 65 | + fprintf(stderr, "reading error while loading file %s", path); |
| 66 | + exit(1); |
| 67 | + } |
| 68 | + fclose(file); |
| 69 | + return (unsigned int)num_bytes; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + fprintf(stderr, "File < %s > is too big for buffer", path); |
| 74 | + exit(1); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void run_benchmarks(string benchmarks[]) |
| 79 | +{ |
| 80 | + char path[MAX_PATH]; |
| 81 | + unsigned char bytes[MAX_BYTE_CODE_SIZE]; |
| 82 | + unsigned int bytes_length; |
| 83 | + WARDuino *w = new WARDuino(); |
| 84 | + FOR_EACH(char *, name, benchmarks) |
| 85 | + set_path(path, name); |
| 86 | + printf("Starting test %s \n", path); |
| 87 | + bytes_length = read_file_to_buf(bytes, path); |
| 88 | + Options opt; |
| 89 | + Module* m = w->load_module(bytes, bytes_length,opt); |
| 90 | + int fidx = w->get_export_fidx(m,MAIN); |
| 91 | + w->invoke(m,fidx); |
| 92 | + END |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, const char *argv[]) |
| 96 | +{ |
| 97 | + string benchmarks[] = {"tak"}; |
| 98 | + run_benchmarks(benchmarks); |
| 99 | + return 0; |
| 100 | +} |
0 commit comments