Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

try to extract state type from rust source #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions compiler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@
file.write(c_interface)
file.close()

# BEGIN writing rust file
program = get_rust_file(streams_to_events_map, arbiter_event_source)

file = open(f"{args.dir}/src/monitor.rs", "r")
lines = file.readlines()
file.close()
# BEGIN writing rust file
program = get_rust_file(streams_to_events_map, arbiter_event_source, lines)

file = open(f"{args.dir}/src/monitor.rs", "w")
extern_keyword_found = False
Expand Down
61 changes: 56 additions & 5 deletions compiler/tessla_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from cfile_utils import *
import os

state_type = '''State<
x_state_type = 'aaa'
old_state_type = '''State<
(),
fn(TesslaValue<(TesslaInt, TesslaInt, TesslaInt)>, i64) -> Result<(), ()>,
fn(TesslaValue<(TesslaInt, TesslaInt, TesslaInt)>, i64) -> Result<(), ()>,
Expand Down Expand Up @@ -70,7 +71,7 @@ def get_rust_file_args(args):
return args_result, value_args


def get_rust_code(possible_events):
def get_rust_code(possible_events, state_type):
answer = ""

for (event_name, data) in possible_events.items():
Expand All @@ -82,15 +83,65 @@ def get_rust_code(possible_events):
Value_args = f"Value(({Value_args}))"
answer+=f'''
#[no_mangle]
extern "C" fn RUST_FFI_{event_name} (mut bs : &mut {state_type}, {args} ts : c_long) {"{"}
extern "C" fn RUST_FFI_{event_name} (bs : &mut {state_type}, {args} ts : c_long) {"{"}
bs.step(ts.into(), false).expect("Step failed");
bs.set_{event_name}({Value_args});
bs.flush().expect("Flush failed");
{"}"}
'''
return answer

def get_rust_file(mapping, arbiter_event_source) -> str:
def try_parse_state_type(lines) -> str:
found=0
bracketcount=0
buffer=""
ret=old_state_type
for line in lines:
if found==0:
if line.strip().startswith("impl Default"):
buffer=buffer+line.strip()[12:].strip()
found=1
continue
if found==1:
buffer=buffer+line.strip()
if(buffer.startswith("for State<")):
bracketcount=1
ret=buffer[4:]
buffer=buffer[10:]
found=2
while bracketcount>0 and len(buffer)>0:
if buffer[0:2]=="->":
ret=ret+buffer[0]
buffer=buffer[1:]
elif buffer[0]=="<":
bracketcount=bracketcount+1
elif buffer[0]==">":
bracketcount=bracketcount-1
ret=ret+buffer[0]
buffer=buffer[1:]
continue
elif len(buffer)>=10:
found=0
buffer=""
if found==2:
buffer=buffer+line.strip()
while bracketcount>0 and len(buffer)>0:
if buffer[0:2]=="->":
ret=ret+buffer[0]
buffer=buffer[1:]
elif buffer[0]=="<":
bracketcount=bracketcount+1
elif buffer[0]==">":
bracketcount=bracketcount-1
ret=ret+buffer[0]
buffer=buffer[1:]
if(bracketcount==0):
break
return ret

def get_rust_file(mapping, arbiter_event_source,origlines) -> str:
state_type = try_parse_state_type(origlines)
print("STATE TYPE: "+state_type)
possible_events = mapping[arbiter_event_source]
return f'''
#[no_mangle]
Expand All @@ -99,7 +150,7 @@ def get_rust_file(mapping, arbiter_event_source) -> str:
Box::new(State::default())
{"}"}

{get_rust_code(possible_events)}
{get_rust_code(possible_events, state_type)}
'''

def received_events_declare_args(event_name, data):
Expand Down
4 changes: 2 additions & 2 deletions experiments/bank-tessla/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ bankmonitor-$(BUFSIZE).c: bankmonitor-$(BUFSIZE).vl tesslamon/Cargo.toml
python3 ../../compiler/main.py -o bankmonitor-$(BUFSIZE).c -t -d tesslamon bankmonitor-$(BUFSIZE).vl

tesslamon/target/release/libmonitor.a: bankmonitor-$(BUFSIZE).c tesslamon/Cargo.toml
-cd ./tesslamon; cargo build
-cd ./tesslamon; cargo build --release

intmap.o: ../../compiler/cfiles/intmap.cpp ../../compiler/cfiles/intmap.h
g++ -c $< -o $@

monitor$(BUFSIZE): tesslamon/target/release/libmonitor.a bankmonitor-$(BUFSIZE).c intmap.o
../../gen/compile.sh bankmonitor-$(BUFSIZE).c intmap.o -lstdc++ tesslamon/target/debug/libmonitor.a -ldl
NOLTO=yes ../../gen/compile.sh bankmonitor-$(BUFSIZE).c intmap.o -lstdc++ tesslamon/target/release/libmonitor.a -ldl
mv monitor monitor$(BUFSIZE)

experiments: ../bank/interact ../bank/bank
Expand Down
6 changes: 3 additions & 3 deletions experiments/primes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ all: prepare
../../compiler/cfiles/intmap.o: ../../compiler/cfiles/intmap.cpp
$(CXX) -c $< -o $@

prepare: primes primes-bad primes-java primes-python ../../compiler/cfiles/compiler_utils.o ../../compiler/cfiles/intmap.o
prepare: primes primes-bad primes-python ../../compiler/cfiles/compiler_utils.o ../../compiler/cfiles/intmap.o
python3 shm_prepare_programs.py

primes: primes.c
Expand All @@ -18,8 +18,8 @@ primes-bad: primes-bad.c

#primes-2: primes-2.c

primes-java: primes.java
javac -Werror $<
#primes-java: primes.java
# javac -Werror $<

primes-python: primes.py
python3 -O -m compileall *.py
Expand Down
8 changes: 6 additions & 2 deletions gen/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ SHAMONDIR=$(readlink -f "$GENDIR/..")
CC=clang
CPPFLAGS="-D_POSIX_C_SOURCE=200809L -I${GENDIR} -I$SHAMONDIR\
-I$SHAMONDIR/streams -I$SHAMONDIR/core -I$SHAMONDIR/shmbuf"
LTOFLAGS=""
if grep -q 'CMAKE_BUILD_TYPE.*=Debug' $GENDIR/../CMakeCache.txt; then
CFLAGS="-g -O0"
# CFLAGS="$CFLAGS -fsanitize=address,undefined"
else
CFLAGS="-g3 -O3 -flto -fno-fat-lto-objects -fPIC -std=c11"
CFLAGS="-g3 -O3 -fPIC -std=c11"
if [ -z "$NOLTO" ]; then
LTOFLAGS="-flto -fno-fat-lto-objects"
fi
CPPFLAGS="$CPPFLAGS -DNDEBUG"
fi

Expand All @@ -39,4 +43,4 @@ LIBRARIES="$SHAMONDIR/core/libshamon-arbiter.a\
$SHAMONDIR/streams/libshamon-streams.a"

test -z $CC && CC=cc
${CC} $CFLAGS $CPPFLAGS -o $CURDIR/monitor $MONITORSRC $@ $LIBRARIES $LDFLAGS -DSHMBUF_ARBITER_BUFSIZE=$ARBITER_BUFSIZE
${CC} $CFLAGS $LTOFLAGS $CPPFLAGS -o $CURDIR/monitor $MONITORSRC $@ $LIBRARIES $LDFLAGS -DSHMBUF_ARBITER_BUFSIZE=$ARBITER_BUFSIZE
2 changes: 1 addition & 1 deletion gen/compile_primes6.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fi

LDFLAGS="-lpthread -ldl"
LIBRARIES="$SHAMONDIR/core/libshamon-arbiter.a\
$SHAMONDIR/core/libshamon-monitor.a\
$SHAMONDIR/core/libshamon-monitor-buffer.a\
$SHAMONDIR/core/libshamon-stream.a\
$SHAMONDIR/shmbuf/libshamon-shmbuf.a\
$SHAMONDIR/core/libshamon-parallel-queue.a\
Expand Down