Skip to content

Commit

Permalink
Abstract arch, remove stdlib's malloc from altlib and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ParfenovIgor committed Jan 3, 2025
1 parent 7ba29e4 commit 9fbb42d
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 300 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
BUILD_DIR=$(abspath build)

.PHONY: compiler stdlib altlib test clean
.PHONY: compiler arch stdlib altlib test clean

compiler: stdlib
compiler: arch stdlib
$(MAKE) -C compiler BUILD_DIR=$(BUILD_DIR)

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

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

Expand Down
6 changes: 2 additions & 4 deletions altlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CFLAGS=-I ../stdlib/include
SRCS_AL := $(wildcard *.al)
OBJS_AL := $(patsubst %.al, $(BUILD_DIR)/altlib/%.o, $(SRCS_AL))

OBJS_ARCH := $(wildcard $(BUILD_DIR)/arch/*.o)
OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)

all: $(OBJS_AL) docs
Expand All @@ -12,14 +13,11 @@ $(BUILD_DIR)/altlib/%.o: %.al make_dir
$(BUILD_DIR)/calias -a $(ALFLAGS) $< -o $@

$(BUILD_DIR)/altlib/generate_docs: generate_docs.c
gcc $(CFLAGS) $< $(OBJS_STDLIB) -o $@
gcc $(CFLAGS) $< $(OBJS_ARCH) $(OBJS_STDLIB) -o $@

docs: $(BUILD_DIR)/altlib/generate_docs
cat header.html > ../docs/altlibref.html
$< $(SRCS_AL) >> ../docs/altlibref.html

make_dir:
mkdir -p $(BUILD_DIR)/altlib

clean:
rm -r $(BUILD_DIR)/altlib
40 changes: 31 additions & 9 deletions altlib/stdio.al
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,35 @@ func ^.fputs3_(fd #I, str1 #1C, str2 #1C, str3 #1C) -> #I
//* fputi_
//* Prints the integer `n` to the descriptor `fd`. Returns length of the string printed.
func ^.fputi_(fd #I, n #I) -> #I {
def str := itoa_(n)
def res := posix_write(fd, str, strlen_(str))
eval _free(str as #1I)
return res
return if (n = 0) {
def ch := '0'
return posix_write(fd, ch&, 1)
}
else {
def neg := 0
eval if (n < 0) {
neg := 1
def ch := '-'
eval posix_write(fd, ch&, 1)
n := -n
}
def len := 1
def mod := 1
eval while (mod * 10 <= n) {
mod := mod * 10
len := len + 1
}
def i := len - 1
eval while (i >= 0) {
def ch := (n / mod) as #C
ch := ch + '0'
n := n % mod
mod := mod / 10
eval posix_write(fd, ch&, 1)
i := i - 1
}
return len + neg
}
}

//* fputsi_
Expand Down Expand Up @@ -62,11 +87,8 @@ func ^.sputs_(dst #1C, src #1C) -> #I {
//* sputi_
//* Prints the integer `n` to the string `dst`. Doesn't check for the length. Returns length of the string printed.
func ^.sputi_(dst #1C, n #I) -> #I {
def str := itoa_(n)
eval sputs_(dst, str)
def res := strlen_(str)
eval _free(str as #1I)
return res
eval itoa_(n, dst, 10)
return strlen_(dst)
}
//* freadc_
Expand Down
30 changes: 12 additions & 18 deletions altlib/stdlib.al
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ include altlib."string.al"
//* stdlib
//* todo
proto ._malloc(sz #I) -> #1I
proto ._free(ptr #1I) -> #V
func ^.itoa_(n #I) -> #1C {
return if (n = 0) {
def str := _malloc(2 * $#C) as #1C
str[0]& <- '0'
str[1]& <- '\0'
return str
func ^.itoa_(n #I, dst #1C, radix #I) -> #1C {
eval if (n = 0) {
dst[0]& <- '0'
dst[1]& <- '\0'
}
else {
def sign := 1
Expand All @@ -24,26 +19,25 @@ func ^.itoa_(n #I) -> #1C {
def m := n
eval while (m) {
len := len + 1
m := m / 10
m := m / radix
}
eval if (sign = -1) {
len := len + 1
}

def str := _malloc((len + 1) * $#C) as #1C
eval if (sign = -1) {
str[0]& <- '-'
eval if (sign = -1) {
dst[0]& <- '-'
}
str[len]& <- '\0'
dst[len]& <- '\0'
len := len - 1
eval while (n) {
def val := n % 10 as #C
str[len]& <- val + '0'
n := n / 10
def val := n % radix as #C
dst[len]& <- val + '0'
n := n / radix
len := len - 1
}
return str
}
return dst
}

func ^.atoi_(str #1C) -> #I {
Expand Down
57 changes: 0 additions & 57 deletions altlib/string.al
Original file line number Diff line number Diff line change
Expand Up @@ -55,60 +55,3 @@ func ^.strnlen_(a #1C, n #I) -> #I {
i := i + 1
} else n
}
func ^.concat_(a #1C, b #1C) -> #1C {
def s_a := strlen_(a)
def s_b := strlen_(b)
def buf := _malloc(s_a + s_b + 1) as #1C
def i := 0
eval while (i < s_a) {
buf[i]& <- a[i]
i := i + 1
}
i := 0
eval while (i < s_b) {
buf[s_a + i]& <- b[i]
i := i + 1
}
buf[s_a + s_b]& <- '\0'
return buf
}
func ^.concat3_(a #1C, b #1C, c #1C) -> #1C {
def s_a := strlen_(a)
def s_b := strlen_(b)
def s_c := strlen_(c)
def buf := _malloc(s_a + s_b + s_c + 1) as #1C
def i := 0
eval while (i < s_a) {
buf[i]& <- a[i]
i := i + 1
}
i := 0
eval while (i < s_b) {
buf[s_a + i]& <- b[i]
i := i + 1
}
i := 0
eval while (i < s_c) {
buf[s_a + s_b + i]& <- c[i]
i := i + 1
}
buf[s_a + s_b + s_c]& <- '\0'
return buf
}
func ^.substr_(a #1C, n #I) -> #1C {
def m := strlen_(a)
eval if (m <= n) {
n := m
}
def b := _malloc(n + 1) as #1C
def i := 0
eval while (i < n) {
b[i]& <- a[i]
i := i + 1
}
b[n]& <- '\0'
return b
}
14 changes: 7 additions & 7 deletions altlib/vector.al
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
include altlib."memory.al"
include altlib."stdio.al"
include altlib."stdlib.al"
include altlib."test_allocator.al"

//* vector
//* todo
typedef Vector := #S {
data: #1I,
size: #I,
reserved: #I
reserved: #I,
allocator: #1TestAllocator
}
func ^.vector_init() -> #1Vector {
def this #1Vector := _malloc($#Vector) as #1Vector
func ^#1Vector.init(this #1Vector, allocator #1TestAllocator) -> #V {
this->size& <- 0
this->reserved& <- 10
this->data& <- _malloc(this->reserved * $#I)
return this
this->data& <- allocator.alloc(this->reserved * $#I)
this->allocator& <- allocator
}
func ^#1Vector.push(this #1Vector, x #I) -> #V {
eval if (this->size = this->reserved) {
def buffer #1I := _malloc(this->reserved * 2 * $#I)
def buffer #1I := this->allocator.alloc(this->reserved * 2 * $#I)
eval puti_(this->data as #I)
eval _memcpy(buffer, this->data, this->size * $#I)
eval _free(this->data)
this->data& <- buffer
this->reserved& <- this->reserved * 2
}
Expand Down
12 changes: 12 additions & 0 deletions arch/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ASFLAGS=-f elf64

SRCS_ASM := $(wildcard x86/*.asm)
OBJS_ASM := $(patsubst x86/%.asm, $(BUILD_DIR)/arch/%.o, $(SRCS_ASM))

all: $(OBJS_ASM) $(OBJS_C)

$(BUILD_DIR)/arch/%.o: x86/%.asm make_dir
nasm $(ASFLAGS) $< -o $@

make_dir:
mkdir -p $(BUILD_DIR)/arch
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions compiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ LDFLAGS=-z noexecstack
SRCS_C := $(wildcard src/*.c)
OBJS_C := $(patsubst src/%.c, $(BUILD_DIR)/compiler/%.o, $(SRCS_C))

OBJS_ARCH := $(wildcard $(BUILD_DIR)/arch/*.o)
OBJS_STDLIB := $(wildcard $(BUILD_DIR)/stdlib/*.o)

all: $(OBJS_C)
gcc -g -fno-stack-protector -I include -c httpd/httpd.c -o $(BUILD_DIR)/compiler/httpd.o
gcc $(LDFLAGS) -o $(BUILD_DIR)/calias $(OBJS_C) $(BUILD_DIR)/compiler/httpd.o $(OBJS_STDLIB)
gcc $(LDFLAGS) -o $(BUILD_DIR)/calias $(OBJS_C) $(BUILD_DIR)/compiler/httpd.o $(OBJS_ARCH) $(OBJS_STDLIB)

$(BUILD_DIR)/compiler/%.o: src/%.c make_dir
gcc $(CFLAGS) -c $< -o $@

make_dir:
mkdir -p $(BUILD_DIR)/compiler

clean:
rm -r $(BUILD_DIR)/compiler
Loading

0 comments on commit 9fbb42d

Please sign in to comment.