Skip to content

Commit

Permalink
Merge pull request #300 from charles-rs/main
Browse files Browse the repository at this point in the history
add fastbrili to repo
  • Loading branch information
sampsyo authored Jan 2, 2024
2 parents 3413310 + 96aca05 commit 188cd38
Show file tree
Hide file tree
Showing 46 changed files with 8,436 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benchmarks/turnt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ hyperfine --warmup 3 --export-json bench.json \
rm -f {base}.json
rm {base}"""
output."bench.json" = "../bench.json"

[envs.fastbrili]
default = false
command = "bril2json < {filename} | ../fastbril/build/fastbrili {args}"
output.out = "-"
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`
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

0 comments on commit 188cd38

Please sign in to comment.