Add Makefile by @neon, fix array typechecking during typeinference

This commit is contained in:
Sofia 2025-07-15 01:16:21 +03:00
parent bcad9b57fe
commit 28d632d51c
7 changed files with 49 additions and 62 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ main
reid_src/*.o
reid_src/*.ll
reid_src/*.asm
reid_src/*.out

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
SRC=foo.reid
BIN=$(SRC:.reid=.out)
REID=cargo run --example cli
LD=ld
LDFLAGS=
all: $(BIN)
clean:
rm -f $(BIN) $(SRC:.reid=.o) $(SRC:.reid=.asm) $(SRC:.reid=.ll)
$(BIN): $(SRC:.reid=.o)
$(LD) -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/crt1.o -lc $(LDFLAGS) $< -o$@
.SUFFIXES: .o .reid
.reid.o:
$(REID) $<

View File

@ -5,13 +5,17 @@
#
# Do note this file is extremely simply for my own personal convenience
export .env
cargo run --release --example cli $1 && \
# clang hello.o -o main && \
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
-o main /usr/lib/crt1.o hello.o -lc && \
./main ; echo "Return value: ""$?"
# export .env
# cargo run --release --example cli $1 && \
# # clang hello.o -o main && \
# ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
# -o main /usr/lib/crt1.o hello.o -lc && \
# ./main ; echo "Return value: ""$?"
$BINARY="$($1 | cut -d'.' -f1)"".out"
make clean SRC=$1 && make SRC=$1 && echo "" && \
$BINARY ; echo "Return value: ""$?"
## Command from: clang -v hello.o -o test
## Original command:

View File

@ -163,7 +163,9 @@ impl IndexedVariableReference {
}
super::IndexedVariableReferenceKind::Index(inner, _) => {
if let Some((mutable, inner_ref)) = inner.find_hint(hints)? {
let inner_ty = inner_ref.as_type();
// Check that the resolved type is at least an array, no
// need for further resolution.
let inner_ty = inner_ref.resolve_weak().unwrap();
match inner_ty {
Array(type_kind, _) => Ok(hints
.from_type(&type_kind)
@ -284,7 +286,9 @@ impl Expression {
ExprKind::Index(expression, index_ty, _) => {
let expr_ty = expression.infer_types(state, type_refs)?;
let kind = unsafe { expr_ty.resolve_type() };
// Check that the resolved type is at least an array, no
// need for further resolution.
let kind = expr_ty.resolve_weak().unwrap();
match kind {
Array(type_kind, _) => {
let elem_ty = type_refs.from_type(&type_kind).unwrap();

View File

@ -15,23 +15,22 @@ use super::{
pub struct TypeRef<'scope>(TypeIdRef, &'scope ScopeTypeRefs<'scope>);
impl<'scope> TypeRef<'scope> {
pub unsafe fn resolve_type(&self) -> TypeKind {
unsafe {
let resolved = self
.1
.types
.hints
.borrow()
.get_unchecked(*self.0.borrow())
.clone();
/// Resolve current type in a weak manner, not resolving any Arrays or
/// further inner types
pub fn resolve_weak(&self) -> Option<TypeKind> {
Some(self.1.types.retrieve_type(*self.0.borrow())?)
}
match resolved {
TypeKind::Array(elem_ty, len) => {
let resolved_elem_ty = self.1.from_type(&elem_ty).unwrap().resolve_type();
TypeKind::Array(Box::new(resolved_elem_ty), len)
}
_ => resolved,
/// Resolve type deeply, trying to resolve any inner types as well.
pub fn resolve_deep(&self) -> Option<TypeKind> {
let resolved = self.resolve_weak()?;
match resolved {
TypeKind::Array(elem_ty, len) => {
let resolved_elem_ty = self.1.from_type(&elem_ty).unwrap().resolve_weak()?;
Some(TypeKind::Array(Box::new(resolved_elem_ty), len))
}
_ => Some(resolved),
}
}
@ -48,7 +47,7 @@ impl<'scope> std::fmt::Debug for TypeRef<'scope> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Hint")
.field(&self.0)
.field(unsafe { &self.resolve_type() })
.field(&self.resolve_deep().unwrap())
.finish()
}
}

BIN
reid_src/array Executable file

Binary file not shown.

38
test.ll
View File

@ -1,38 +0,0 @@
; ModuleID = 'test'
source_filename = "test"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define ptr @array() {
array:
%array_alloca = alloca i16, i16 4, align 2
%array_gep = getelementptr i16, ptr %array_alloca, i32 0
store i16 10, ptr %array_gep, align 2
%array_gep1 = getelementptr i16, ptr %array_alloca, i32 1
store i16 15, ptr %array_gep1, align 2
%array_gep2 = getelementptr i16, ptr %array_alloca, i32 2
store i16 7, ptr %array_gep2, align 2
%array_gep3 = getelementptr i16, ptr %array_alloca, i32 3
store i16 9, ptr %array_gep3, align 2
%array_alloca4 = alloca ptr, i16 1, align 8
%array_gep5 = getelementptr ptr, ptr %array_alloca4, i32 0
store ptr %array_alloca, ptr %array_gep5, align 8
ret ptr %array_alloca4
}
define i16 @main() {
main:
%call = call ptr @array()
%array_gep = getelementptr ptr, ptr %call, i32 0
%load5 = load ptr, ptr %array_gep2, align 8
%array_gep1 = getelementptr i16, ptr %load5, i32 3
store i16 5, ptr %array_gep1, align 2
%array_gep2 = getelementptr ptr, ptr %call, i32 0
%load = load ptr, ptr %array_gep2, align 8
%array_gep3 = getelementptr i16, ptr %load, i32 3
%load4 = load i16, ptr %array_gep3, align 2
ret i16 %load4
}