Skip to content

Commit

Permalink
input, algo-tests, test-allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Dec 30, 2024
1 parent 55b2a5f commit 6ecf5d3
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 23 deletions.
47 changes: 47 additions & 0 deletions altlib/stdio.al
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,50 @@ func ^.sputi_(dst #1C, n #I) -> #I {
eval _free(str as #1I)
return res
}

func ^.atoi_(str #1C) -> #I {
def i := strlen_(str) - 1
def x := 0
eval while (i >= 0) {
def ch := (str[i] - '0') as #I
x := x * 10 + ch
i := i - 1
}
return x
}

func ^.freads_(fd #I, dst #1C) -> #I {
def i := 0
def dsti := dst as #I
eval while (1) {
def size := posix_read(fd, (dsti + i) as #1C, 1)
eval if (size = 0) { break {} }
def ch := dst[i]
eval if (ch = ' ' or ch = '\n') {
eval if (i = 0) {
i := i - 1
}
else {
break {}
}
}
i := i + 1
}
dst[i]& <- '\0'
return i
}

func ^.freadi_(fd #I, dst #1C) -> #I {
eval freads_(fd, dst)
return atoi_(dst)
}

func ^.reads_(dst #1C) -> #I {
def STDIN := 0
return freads_(STDIN, dst)
}

func ^.readi_(dst #1C) -> #I {
def STDIN := 0
return freadi_(STDIN, dst)
}
5 changes: 5 additions & 0 deletions altlib/stdlib.al
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ func ^.itoa_(n #I) -> #1C {
return str
}
}

func ^.rand_(seed #I) -> #I {
seed := seed * 1103515245 + 12345
return (seed / 65536) % 32768
}
31 changes: 31 additions & 0 deletions altlib/test_allocator.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include altlib."posix.al"

typedef TestAllocator := #S {
data: #1I,
size: #I,
reserved: #I
};

func ^#1TestAllocator.init(this #1TestAllocator, size #I) -> #V {
def PROT_READ := 1
def PROT_WRITE := 2
def MAP_PRIVATE := 2
def MAP_ANONYMOUS := 32
this->data& <- posix_mmap(0 as #1I, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
this->size& <- 0
this->reserved& <- size
}

func ^#1TestAllocator.deinit(this #1TestAllocator) -> #V {
eval posix_munmap(this->data, this->reserved)
}

func ^#1TestAllocator.alloc(this #1TestAllocator, size #I) -> #1I {
return if (this->size + size <= this->reserved) {
def x := this->data as #I
def ptr := (x + this->size) as #1I
this->size& <- this->size + size
return ptr
}
else 0 as #1I
}
4 changes: 2 additions & 2 deletions altlib/vector.al
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ include altlib."memory.al"
include altlib."stdio.al"
include altlib."stdlib.al"

typedef Vector := #S{
typedef Vector := #S {
data: #1I,
size: #I,
reserved: #I
};
}

func ^.vector_init() -> #1Vector {
def this #1Vector := _malloc($#Vector) as #1Vector
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ struct TypeNode *compile_method_call(struct Node *node, struct MethodCall *this,
_fputs3(context->fd_text, "mov rax, ", function_info->name_back, "\n");
_fputs(context->fd_text, "mov [rsp + 8], rax\n");
}
else {
error_semantic("Method was not declared", node);
}

struct TypeFunction *_type = type_function->node_ptr;
int sz = vsize(&_type->types);
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int type_mangle_helper(struct TypeNode *n, struct CPContext *context, char *buff
struct TypeIdentifier *_n = n->node_ptr;
n = context_find_type(context, _n->identifier)->type;
}
const char *ptr_str = _itoa(sum_degree);
const char *ptr_str = _itoa(sum_degree + n->degree);
_strcpy(buffer + pos, ptr_str);
pos += _strlen(ptr_str);

Expand Down
3 changes: 1 addition & 2 deletions stdlib/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ char *_itoa(int n);
void _init_malloc();
void *_malloc(int sz);
void _free(void *ptr);
int _rand();
void _srand(unsigned int seed);
int _rand(int seed);
11 changes: 7 additions & 4 deletions stdlib/src/heap_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>

void heap_test() {
_srand(123);
const int SZ = 100000;
void *arr[SZ];
int sizes[SZ];
Expand All @@ -14,10 +13,13 @@ void heap_test() {
heap_t *heap = create_heap(heap_size, heap_index_size);
int memoryUsed = 0;
int mallocAttempts = 0, mallocSucceeded = 0;
int seed = 123;
for (int op = 1; op <= 1000000; op++) {
if (_rand() % 3) {
int seed = _rand(seed);
if (seed % 3) {
mallocAttempts++;
int x = (_rand() % 9 + 1) * 4;
seed = _rand(seed);
int x = (seed % 9 + 1) * 4;
void *ptr = malloc_heap(heap, x);
if (ptr != 0) {
mallocSucceeded++;
Expand All @@ -33,7 +35,8 @@ void heap_test() {
}
else {
for (int i = 0; i < 10; i++) {
int j = _rand() % SZ;
seed = _rand(seed);
int j = seed % SZ;
if (arr[j] != 0) {
free_heap(heap, arr[j]);
memoryUsed -= sizes[j];
Expand Down
12 changes: 3 additions & 9 deletions stdlib/src/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ void _free(void *ptr) {
free_heap(heap, ptr);
}

unsigned int next = 1;

int _rand() {
next = next * 1103515245 + 12345;
return (next / 65536) % 32768;
}

void _srand(unsigned int seed) {
next = seed;
int _rand(int seed) {
seed = seed * 1103515245 + 12345;
return (seed / 65536) % 32768;
}
7 changes: 5 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
BUILD_DIR=$(abspath build)

.PHONY: unit integration
.PHONY: algo unit integration

all: unit integration
all: algo unit integration

algo:
$(MAKE) -C algo BUILD_DIR=$(BUILD_DIR)

unit:
$(MAKE) -C unit BUILD_DIR=$(BUILD_DIR)
Expand Down
20 changes: 20 additions & 0 deletions test/algo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ALFLAGS=-i altlib ../../altlib/
LDFLAGS=-z noexecstack

SRCS_AL := $(wildcard *.al)
EXES_AL := $(patsubst %.al, $(BUILD_DIR)/test/algo/%, $(SRCS_AL))

OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)
OBJS_ALTLIB := $(wildcard $(BUILD_DIR)/altlib/*.o)

all: $(EXES_AL)

$(BUILD_DIR)/test/algo/%: %.al make_dir
$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $(patsubst %, %.o, $@)
ld $(LDFLAGS) -o $@ $(patsubst %, %.o, $@) $(OBJS_STDLIB) $(OBJS_ALTLIB)

make_dir:
mkdir -p $(BUILD_DIR)/test/algo

clean:
rm -r $(BUILD_DIR)/test
21 changes: 21 additions & 0 deletions test/algo/a_plus_b.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include altlib."posix.al"
include altlib."stdio.al"
include altlib."test_allocator.al"

func ^._start() -> #V {
def allocator := .{
data := 0 as #1I,
size := 0,
reserved := 0
}
eval allocator&.init(1024)

def buffer := allocator&.alloc(128) as #1C
def a := readi_(buffer)
def b := readi_(buffer)
def c := a + b
eval puti_(c)

eval allocator&.deinit()
eval posix_exit(0)
}
19 changes: 19 additions & 0 deletions test/algo/read_write_string.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
include altlib."posix.al"
include altlib."stdio.al"
include altlib."test_allocator.al"

func ^._start() -> #V {
def allocator := .{
data := 0 as #1I,
size := 0,
reserved := 0
}
eval allocator&.init(1024)

def buffer := allocator&.alloc(256) as #1C
eval reads_(buffer)
eval puts_(buffer)

eval allocator&.deinit()
eval posix_exit(0)
}
3 changes: 0 additions & 3 deletions test/integration/string.al
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
proto ._puts(n #1C) -> #I
proto ._puti(n #I) -> #I

include altlib."posix.al"
include altlib."stdio.al"
include altlib."stdlib.al"
Expand Down
25 changes: 25 additions & 0 deletions test/integration/test_allocator.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include altlib."posix.al"
include altlib."stdio.al"
include altlib."test_allocator.al"

func ^._start() -> #V {
def allocator := .{
data := 0 as #1I,
size := 0,
reserved := 0
}
eval allocator&.init(1024)

def buffer1 := allocator&.alloc(256) as #I
def buffer2 := allocator&.alloc(256) as #I
def buffer3 := allocator&.alloc(256) as #I
def buffer4 := allocator&.alloc(256) as #I
def buffer5 := allocator&.alloc(256) as #I
eval puti_(buffer2 - buffer1)
eval puti_(buffer3 - buffer2)
eval puti_(buffer4 - buffer3)
eval puti_(buffer5)

eval allocator&.deinit()
eval posix_exit(0)
}
4 changes: 4 additions & 0 deletions test/integration/test_allocator.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
256
256
256
0

0 comments on commit 6ecf5d3

Please sign in to comment.