From 301657738b670bfb87d1e87033d0b7dec2260c04 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 5 Jan 2024 17:52:15 +0900 Subject: [PATCH] [sample] add dump option --- sample/Makefile | 2 +- sample/bf.cpp | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/sample/Makefile b/sample/Makefile index 7e206a5..dad8c3e 100644 --- a/sample/Makefile +++ b/sample/Makefile @@ -1,6 +1,6 @@ PRE?=riscv64-linux-gnu- CXX?=g++-13 -CFLAGS?=-I../ -Wall -Wextra +CFLAGS?=-I../ -Wall -Wextra -g CFLAGS+=-DXBYAK_RISCV_V SRC=add.cpp cpuinfo.cpp fill.cpp vector_add_rvv.cpp vector_add_rvv_f32.cpp OBJ=$(SRC:.cpp=.o) diff --git a/sample/bf.cpp b/sample/bf.cpp index 691a01e..b9c304d 100644 --- a/sample/bf.cpp +++ b/sample/bf.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include class Brainfuck : public Xbyak_riscv::CodeGenerator { @@ -60,14 +61,10 @@ class Brainfuck : public Xbyak_riscv::CodeGenerator { } break; case '.': ld(a0, stack); - sd(ra, sp, 8); - jr(pPutchar); - ld(ra, sp, 8); + jalr(pPutchar); break; case ',': - sd(ra, sp, 8); - jr(pGetchar); - ld(ra, sp, 8); + jalr(pGetchar); sd(a0, stack); break; case '[': { @@ -100,20 +97,21 @@ class Brainfuck : public Xbyak_riscv::CodeGenerator { }; int main(int argc, char *argv[]) try { - if (argc != 2) { - fprintf(stderr, "bf filename.bf\n"); + if (argc < 2) { + fprintf(stderr, "bf filename.bf [-d]\n"); return 1; } + bool doDump = argc == 3 && strcmp(argv[2], "-d") == 0; std::ifstream ifs(argv[1]); Brainfuck bf(ifs); bf.ready(); static int64_t stack[128 * 1024]; auto f = bf.getCode(); -#if 0 - FILE *fp = fopen("bf.dump", "wb"); - fwrite((const void*)f, bf.getSize(), 1, fp); - fclose(fp); -#endif + if (doDump) { + FILE *fp = fopen("bf.dump", "wb"); + fwrite((const void*)f, bf.getSize(), 1, fp); + fclose(fp); + } f((const void *)putchar, (const void *)getchar, stack); } catch (std::exception &e) { printf("ERR:%s\n", e.what());