Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fastbrili to repo #300

Merged
merged 17 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions benchmarks/fastbril-turnt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# command = "bril2json < {filename} | ../build/fastbrili -p {args}"
command = "bril2json < {filename} | $FASTBRILI -ni -bo byte-out ; cat byte-out | $FASTBRILI -b {args} ; rm byte-out"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be it's own toml file compared to being added to benchmarks/turnt.toml?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no i just forgot you can put multiple things in one toml file 🤦

output.out = "-"
# output.prof = "2"
4 changes: 4 additions & 0 deletions fastbril/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/
*~
\#*\#
*.o
84 changes: 84 additions & 0 deletions fastbril/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
TARGET_EXEC ?= fastbrili

BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
DOC_DIR ?= ./doc
SHELL = /bin/sh

CONFIGS := $(wildcard config/*.cf)
GEN_HEAD := $(CONFIGS:config/%.cf=src/%.h)
GEN_TEX := $(CONFIGS:config/%.cf=doc/%.tex)

SRCS := $(wildcard $(SRC_DIRS)/*.c $(SRC_DIRS)/**/*.c)

OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))


CFLAGS += $(INC_FLAGS)

.PHONY: debug

debug: CFLAGS += -g -Og
debug: $(BUILD_DIR)/$(TARGET_EXEC)

.PHONY: coverage

coverage: CFLAGS += --coverage -DDEBUG -g3
coverage: $(GEN_HEAD)
mkdir $(BUILD_DIR) && cd $(BUILD_DIR) && $(CC) $(CFLAGS) $(abspath $(SRCS)) -o $(TARGET_EXEC)

.PHONY: cov-report

cov-report:
gcovr -r . --html --html-details -o cov-report/out.html

.PHONY: release

release: CFLAGS += -O3
release: $(BUILD_DIR)/$(TARGET_EXEC)

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) lib/lib.o
$(CC) $(OBJS) -o $@ $(LDFLAGS)

# # assembly
# $(BUILD_DIR)/%.s.o: %.s
# $(MKDIR_P) $(dir $@)
# $(AS) $(ASFLAGS) -c $< -o $@

# c source
$(BUILD_DIR)/%.c.o: %.c | $(GEN_HEAD)
$(MKDIR_P) $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@

# configured header files
src/%.h: config/%.cf srcgen.awk
./srcgen.awk < $< > $@

brb.pdf: $(GEN_TEX) $(DOC_DIR)/main.tex
cd $(DOC_DIR) && latex -output-format=pdf main.tex && mv main.pdf brb.pdf

lib/lib.o: lib/lib.c
cd lib && $(CC) -O3 -c lib.c

doc: brb.pdf

$(GEN_TEX): $(configs) docgen.sh docgen.awk
$(MKDIR_P) $(dir $@)
./docgen.sh $@

.PHONY: clean

clean:
find . -name "*.aux" -o -name "*.log" -o -name "*.pdf" -o -name "*~" -o \
-name "*.gcda" -o -name "*.gcno" -o -name "*.o" | xargs rm || true
$(RM) $(GEN_HEAD)
$(RM) $(GEN_TEX)
$(RM) -r $(BUILD_DIR)

-include $(DEPS)

MKDIR_P ?= mkdir -p
31 changes: 31 additions & 0 deletions fastbril/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The `fastbril` bytecode interpreter

This is a bytecode spec/generator/interpreter for `bril`. It aims to
be like the typescript or rust implementation, but faster.

## To build

+ binary: `make release`
the binary will be `./build/fastbrili`
+ doc: you need to have LaTeX installed. run `make doc`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I tried to run this and got:

Makefile:58: target `src/.h' given more than once in the same rule.
Makefile:58: target `src/.h' given more than once in the same rule.
Makefile:58: target `src/.h' given more than once in the same rule.
Makefile:58: target `src/.h' given more than once in the same rule.
Makefile:69: target `doc/.tex' given more than once in the same rule.
Makefile:69: target `doc/.tex' given more than once in the same rule.
Makefile:69: target `doc/.tex' given more than once in the same rule.
Makefile:69: target `doc/.tex' given more than once in the same rule.
mkdir -p doc/
./docgen.sh doc/.tex
awk: can't open file config/.cf
 source line number 0
make: *** [doc/.tex] Error 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds like find isn't working for you the same way it does for me...

the doc will be `./doc/brb.pdf`
there is a prebuilt pdf in case this is difficult



### Features
We support a superset of the behavior provided by `brili`, so options like `-p`
work exactly the same. We also support the following:
- `-b` will read in bytecode instead of the Bril json
- `-bo <file>` will output the bytecode to `<file>`
- `-pr` will print the program to standard out (probably more useful with the
`-b` option)
- `-ni` will NOT run the interpreter.
- `-e <file>` will emit assembly to `<file>`.

the current only supported assembly is armv8. sorry.

the compiler to asm is probably the least trustworthy part of this
whole project. The general interpreter should be pretty good, but it
is always possible there are bugs. Please report bugs to
`[email protected]`, and there's a chance that i will fix them :)
49 changes: 49 additions & 0 deletions fastbril/bytecode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Instructions are 64bit numbers
1 bit to mark labelled, 15 bits of opcode, 16bits of destination, 16 bits of
arg1, 16 bits of arg2

this is modified for other instructions

Opcodes:
nop: 0
const: 1
add: 2
mul: 3
sub: 4
div: 5
eq: 6
lt: 7
gt: 8
le: 9
ge: 10
not: 11
and: 12
or: 13
jmp: 14
br: 15
call: 16
ret: 17
print: 18
phi: 19
alloc: 20
free: 21
store: 22
load: 23
ptradd: 24
fadd: 25
fmul: 26
fsub: 27
fdiv: 28
feq: 29
flt: 30
fle: 31
fgt: 32
fge: 33
lconst: 34


types:
bool: 0
int: 1
float: 2
ptr: 3
22 changes: 22 additions & 0 deletions fastbril/config/base.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CONST 1
ADD 2
MUL 3
SUB 4
DIV 5
EQ 6
LT 7
GT 8
LE 9
GE 10
NOT 11
AND 12
OR 13
JMP 14
BR 15
CALL 16
RET 17
PRINT 18
LCONST 19
NOP 20
ID 21

9 changes: 9 additions & 0 deletions fastbril/config/float.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FADD 28
FMUL 29
FSUB 30
FDIV 31
FEQ 32
FLT 33
FLE 34
FGT 35
FGE 36
5 changes: 5 additions & 0 deletions fastbril/config/mem.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALLOC 23
FREE 24
STORE 25
LOAD 26
PTRADD 27
2 changes: 2 additions & 0 deletions fastbril/config/ssa.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PHI 22

4 changes: 4 additions & 0 deletions fastbril/config/types.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BRILINT 0
BRILBOOL 1
BRILFLOAT 2
BRILVOID 3
9 changes: 9 additions & 0 deletions fastbril/doc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.aux
*.out
*.log
base.tex
float.tex
mem.tex
ssa.tex
types.tex
brb.pdf
34 changes: 34 additions & 0 deletions fastbril/doc/auto/main.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(TeX-add-style-hook
"main"
(lambda ()
(TeX-add-to-alist 'LaTeX-provided-package-options
'(("appendix" "toc" "page") ("geometry" "margin=3cm")))
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "href")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "hyperref")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "hyperimage")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "hyperbaseurl")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "nolinkurl")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "url")
(add-to-list 'LaTeX-verbatim-macros-with-braces-local "path")
(add-to-list 'LaTeX-verbatim-macros-with-delims-local "path")
(TeX-run-style-hooks
"latex2e"
"base"
"ssa"
"mem"
"float"
"types"
"article"
"art10"
"appendix"
"hyperref"
"geometry")
(TeX-add-symbols
"bril"
"Bril"
"refint")
(LaTeX-add-labels
"app:opcodes"
"app:types"))
:latex)

Loading