Skip to content

Commit

Permalink
[sample] add dump option
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jan 5, 2024
1 parent 6578cc4 commit 3016577
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion sample/Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 11 additions & 13 deletions sample/bf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xbyak_riscv/xbyak_riscv.hpp>

class Brainfuck : public Xbyak_riscv::CodeGenerator {
Expand Down Expand Up @@ -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 '[': {
Expand Down Expand Up @@ -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<void (*)(const void *, const void *, int64_t *)>();
#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());
Expand Down

0 comments on commit 3016577

Please sign in to comment.