Skip to content

Commit

Permalink
tmp: sep 27
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Sep 27, 2024
1 parent e68308f commit 45bf363
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 584 deletions.
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@
"*.inc": "c",
"*.c": "c",
"*.h": "c",
"array": "c",
"*.tcc": "c",
"algorithm": "c",
"memory": "c",
"random": "c",
"limits": "c",
"numeric": "c",
"stdexcept": "c",
"tuple": "c",
"atomic": "c"
}
}
4 changes: 3 additions & 1 deletion main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) {
exit(1);
}

vm_ir_block_t *entry = vm_lang_eb_compile(vm, src, name ? name : "__expr__");
vm_ir_block_t *entry = vm_lang_lua_compile(vm, src, name ? name : "__expr__");

vm_obj_t value = vm_run_main(vm, entry);
if (vm_obj_is_error(value)) {
Expand All @@ -83,6 +83,8 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) {
}
}

vm_state_delete(vm);

#if VM_USE_SPALL
spall_auto_thread_quit();
spall_auto_quit();
Expand Down
11 changes: 8 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ EXE ?=

TIME_CMD ?= $(shell which gdate || which date) +%s%3N

LIBM ?= YES
LIBM_NO :=
LIBM_YES := -lm
LIBM_FLAGS := ${LIBM_${LIBM}}

# Reset flags for base
BASE_CFLAGS := ${OPT} -Ivendor/tree-sitter/lib/include -Ivendor/tree-sitter/lib/src ${CFLAGS}
BASE_LDFLAGS := ${OPT} ${LDFLAGS} -lm
BASE_LDFLAGS := ${OPT} ${LDFLAGS} ${LIBM_FLAGS}

# object files and depends
MAIN_SRCS = main/minivm.c
Expand All @@ -26,8 +31,8 @@ MAKE_INCLUDE ?=

# tests

TEST_LUAS := $(shell find test -name '*.lua')
TEST_TXTS = ${TEST_LUAS:%.lua=build/test/%.log}
TEST_SRCS := $(wildcard test/lua/*/*.lua)
TEST_TXTS = ${TEST_SRCS:%.lua=build/test/%.log}

# setup targets
default: all
Expand Down
43 changes: 0 additions & 43 deletions test/ebrew/fib.eb

This file was deleted.

83 changes: 56 additions & 27 deletions vendor/spall/auto.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ void spall_auto_set_thread_instrumenting(bool on);
#else
#include <stdatomic.h>
#endif
#else
#pragma comment(lib, "Synchronization")
#include <stdatomic.h>
#endif

#ifdef __cplusplus
Expand Down Expand Up @@ -128,7 +131,8 @@ extern "C" {
#endif

#define SPALL_NOINSTRUMENT __attribute__((no_instrument_function))
#define SPALL_FORCEINLINE __attribute__((always_inline))
#define SPALL_FORCE_INLINE __attribute__((always_inline)) inline
#define SPALL_FORCE_NOINLINE __attribute__((noinline))
#define __debugbreak() __builtin_trap()

#if SPALL_IS_CPP
Expand Down Expand Up @@ -162,9 +166,9 @@ SPALL_FN void spall_pause(void) {

typedef struct SpallHeader {
uint64_t magic_header; // = 0xABADF00D
uint64_t version; // = 2
uint64_t version; // = 3
double timestamp_unit;
uint64_t known_address; // Address for spall_auto_init, for skew-correction
uint64_t base_address;
uint16_t program_path_len;
} SpallHeader;

Expand Down Expand Up @@ -198,7 +202,7 @@ typedef struct SpallBufferHeader {

#pragma pack(pop)

SPALL_FN SPALL_FORCEINLINE uint64_t spall_delta_to_bits(uint64_t dt) {
SPALL_FN SPALL_FORCE_INLINE uint64_t spall_delta_to_bits(uint64_t dt) {
uint32_t count = 0;
count += (dt >= 0x100);
count += (dt >= 0x10000);
Expand Down Expand Up @@ -254,9 +258,24 @@ typedef struct SpallBuffer {
#include <linux/futex.h>
#include <linux/limits.h>
#include <linux/perf_event.h>
#include <sys/auxv.h>
#include <elf.h>

SPALL_FN uint64_t spall_auto_get_base_address(void) {
uint64_t hdr_size = 0;
if (sizeof(void *) == 8) {
hdr_size = sizeof(Elf64_Ehdr);
} else {
hdr_size = sizeof(Elf32_Ehdr);
}

uint64_t phdr = getauxval(AT_PHDR);
uint64_t addr = phdr - hdr_size;
return addr;
}

SPALL_FN bool get_program_path(char **out_path) {
char path[PATH_MAX] = {0};
char path[PATH_MAX+1] = {0};
uint32_t size = sizeof(path);

ssize_t buff_len = (ssize_t)readlink("/proc/self/exe", path, size - 1);
Expand All @@ -265,7 +284,7 @@ SPALL_FN bool get_program_path(char **out_path) {
return false;
}

char *post_path = (char *)calloc(PATH_MAX, 1);
char *post_path = (char *)calloc(PATH_MAX+1, 1);
if (realpath(path, post_path) == NULL) {
free(post_path);
*out_path = NULL;
Expand Down Expand Up @@ -320,15 +339,15 @@ SPALL_FN double spall_get_clock_multiplier(void) {
#endif


SPALL_FN SPALL_FORCEINLINE void spall_signal(Spall_Futex *addr) {
SPALL_FN SPALL_FORCE_INLINE void spall_signal(Spall_Futex *addr) {
long ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL, 0);
if (ret == -1) {
perror("Futex wake");
__debugbreak();
}
}

SPALL_FN SPALL_FORCEINLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
SPALL_FN SPALL_FORCE_INLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
for (;;) {
long ret = syscall(SYS_futex, addr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, NULL, NULL, 0);
if (ret == -1) {
Expand All @@ -346,9 +365,15 @@ SPALL_FN SPALL_FORCEINLINE void spall_wait(Spall_Futex *addr, uint64_t val) {

#elif SPALL_IS_DARWIN

#include <mach-o/ldsyms.h>
#include <mach-o/dyld.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <dlfcn.h>

SPALL_FN uint64_t spall_auto_get_base_address(void) {
return (uint64_t)&_mh_execute_header;
}

#if SPALL_IS_X64
SPALL_FN double spall_get_clock_multiplier(void) {
Expand Down Expand Up @@ -395,7 +420,7 @@ SPALL_FN bool get_program_path(char **out_path) {
int __ulock_wait(uint32_t operation, void *addr, uint64_t value, uint32_t timeout);
int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);

SPALL_FN SPALL_FORCEINLINE void spall_signal(Spall_Futex *addr) {
SPALL_FN SPALL_FORCE_INLINE void spall_signal(Spall_Futex *addr) {
for (;;) {
int ret = __ulock_wake(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, addr, 0);
if (ret >= 0) {
Expand All @@ -413,7 +438,7 @@ SPALL_FN SPALL_FORCEINLINE void spall_signal(Spall_Futex *addr) {
}
}

SPALL_FN SPALL_FORCEINLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
SPALL_FN SPALL_FORCE_INLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
for (;;) {
int ret = __ulock_wait(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, addr, val, 0);
if (ret >= 0) {
Expand All @@ -434,6 +459,10 @@ SPALL_FN SPALL_FORCEINLINE void spall_wait(Spall_Futex *addr, uint64_t val) {

#elif SPALL_IS_WINDOWS

SPALL_FN uint64_t spall_auto_get_base_address(void) {
return (uint64_t)GetModuleHandleW(NULL);
}

SPALL_FN bool get_program_path(char **out_path) {
char *post_path = (char *)calloc(MAX_PATH, 1);
if (GetModuleFileNameA(NULL, post_path, MAX_PATH) == 0) {
Expand All @@ -445,7 +474,7 @@ SPALL_FN bool get_program_path(char **out_path) {
return true;
}

SPALL_FN SPALL_FORCEINLINE double spall_get_clock_multiplier(void) {
SPALL_FN SPALL_FORCE_INLINE double spall_get_clock_multiplier(void) {

// Cache the answer so that multiple calls never take the slow path more than once
static double multiplier = 0;
Expand Down Expand Up @@ -473,20 +502,20 @@ SPALL_FN SPALL_FORCEINLINE double spall_get_clock_multiplier(void) {
return multiplier;
}

SPALL_FN SPALL_FORCEINLINE void spall_signal(Spall_Futex *addr) {
SPALL_FN SPALL_FORCE_INLINE void spall_signal(Spall_Futex *addr) {
WakeByAddressSingle((void *)addr);
}

SPALL_FN SPALL_FORCEINLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
SPALL_FN SPALL_FORCE_INLINE void spall_wait(Spall_Futex *addr, uint64_t val) {
WaitOnAddress(addr, (void *)&val, sizeof(val), INFINITE);
}

#endif

// Auto-tracing impl
static SpallProfile spall_ctx;
static _Thread_local SpallBuffer *spall_buffer = NULL;
static _Thread_local bool spall_thread_running = false;
SpallProfile spall_ctx;
_Thread_local SpallBuffer *spall_buffer = NULL;
_Thread_local bool spall_thread_running = false;

SPALL_NOINSTRUMENT void spall_auto_set_thread_instrumenting(bool on) {
spall_thread_running = on;
Expand Down Expand Up @@ -517,7 +546,7 @@ SPALL_FN void *spall_writer(void *arg) {
#endif
}

SPALL_FN SPALL_FORCEINLINE bool spall__file_write(void *p, size_t n) {
SPALL_NOINSTRUMENT SPALL_FORCE_INLINE bool spall__file_write(void *p, size_t n) {
atomic_store(&spall_buffer->writer.size, n);
atomic_store(&spall_buffer->writer.ptr, (uint64_t)p);
spall_signal(&spall_buffer->writer.ptr);
Expand All @@ -531,7 +560,7 @@ SPALL_FN SPALL_FORCEINLINE bool spall__file_write(void *p, size_t n) {
return true;
}

SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool spall_auto_buffer_flush(void) {
SPALL_NOINSTRUMENT SPALL_FORCE_INLINE bool spall_auto_buffer_flush(void) {
if (!spall_buffer) return false;

size_t data_start = spall_buffer->write_half ? spall_buffer->sub_length : 0;
Expand Down Expand Up @@ -560,7 +589,7 @@ SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool spall_auto_buffer_flush(void) {
return true;
}

SPALL_FN SPALL_FORCEINLINE bool spall_buffer_micro_begin(uint64_t addr, uint64_t caller) {
SPALL_FN SPALL_FORCE_INLINE bool spall_buffer_micro_begin(uint64_t addr, uint64_t caller) {
spall_buffer->current_depth += 1;
spall_buffer->max_depth = SPALL_MAX(spall_buffer->max_depth, spall_buffer->current_depth);

Expand Down Expand Up @@ -609,7 +638,7 @@ SPALL_FN SPALL_FORCEINLINE bool spall_buffer_micro_begin(uint64_t addr, uint64_t
return true;
}

SPALL_FN SPALL_FORCEINLINE bool spall_buffer_micro_end(void) {
SPALL_FN SPALL_FORCE_INLINE bool spall_buffer_micro_end(void) {
uint64_t now = spall_get_clock();
spall_buffer->current_depth -= 1;

Expand Down Expand Up @@ -643,7 +672,7 @@ SPALL_FN SPALL_FORCEINLINE bool spall_buffer_micro_end(void) {
return true;
}

SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool spall_auto_buffer_begin(const char *name, signed long name_len, const char *args, signed long args_len) {
SPALL_NOINSTRUMENT bool spall_auto_buffer_begin(const char *name, signed long name_len, const char *args, signed long args_len) {

spall_buffer->current_depth += 1;
spall_buffer->max_depth = SPALL_MAX(spall_buffer->max_depth, spall_buffer->current_depth);
Expand Down Expand Up @@ -690,11 +719,11 @@ SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool spall_auto_buffer_begin(const char *na
return true;
}

SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool spall_auto_buffer_end(void) {
SPALL_NOINSTRUMENT bool spall_auto_buffer_end(void) {
return spall_buffer_micro_end();
}

SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool (spall_auto_thread_init)(uint32_t thread_id, size_t buffer_size) {
SPALL_NOINSTRUMENT SPALL_FORCE_INLINE bool spall_auto_thread_init(uint32_t thread_id, size_t buffer_size) {
if (buffer_size < 512) { return false; }
if (spall_buffer != NULL) { return false; }

Expand All @@ -715,7 +744,7 @@ SPALL_NOINSTRUMENT SPALL_FORCEINLINE bool (spall_auto_thread_init)(uint32_t thre
return true;
}

void (spall_auto_thread_quit)(void) {
SPALL_NOINSTRUMENT SPALL_FORCE_INLINE void spall_auto_thread_quit(void) {
spall_thread_running = false;
spall_auto_buffer_flush();

Expand Down Expand Up @@ -748,7 +777,7 @@ SPALL_FN void *spall_canonical_addr(void* fn) {
}


SPALL_NOINSTRUMENT bool spall_auto_init(char *filename) {
SPALL_NOINSTRUMENT SPALL_FORCE_NOINLINE bool spall_auto_init(char *filename) {
if (!filename) return false;
memset(&spall_ctx, 0, sizeof(spall_ctx));

Expand All @@ -762,9 +791,9 @@ SPALL_NOINSTRUMENT bool spall_auto_init(char *filename) {
spall_ctx.stamp_scale = spall_get_clock_multiplier();
SpallHeader header = {0};
header.magic_header = 0xABADF00D;
header.version = 2;
header.version = 3;
header.timestamp_unit = spall_ctx.stamp_scale;
header.known_address = (uint64_t)spall_canonical_addr((void *)spall_auto_init);
header.base_address = spall_auto_get_base_address();

char *program_path;
if (!get_program_path(&program_path)) { return false; }
Expand Down
6 changes: 1 addition & 5 deletions vm/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ union vm_ast_node_value_t {
};

struct vm_ast_node_t {
vm_location_range_t range;
vm_ast_node_value_t value;
vm_ast_node_type_t type;

struct {
const char *type;
vm_location_range_t range;
} info;
};

const char *vm_ast_format(vm_ast_node_t *node);
Expand Down
Loading

0 comments on commit 45bf363

Please sign in to comment.