Add Makefile by @neon, fix array typechecking during typeinference
This commit is contained in:
parent
bcad9b57fe
commit
28d632d51c
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ main
|
|||||||
reid_src/*.o
|
reid_src/*.o
|
||||||
reid_src/*.ll
|
reid_src/*.ll
|
||||||
reid_src/*.asm
|
reid_src/*.asm
|
||||||
|
reid_src/*.out
|
||||||
|
17
Makefile
Normal file
17
Makefile
Normal 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) $<
|
16
libtest.sh
16
libtest.sh
@ -5,13 +5,17 @@
|
|||||||
#
|
#
|
||||||
# Do note this file is extremely simply for my own personal convenience
|
# Do note this file is extremely simply for my own personal convenience
|
||||||
|
|
||||||
export .env
|
# export .env
|
||||||
cargo run --release --example cli $1 && \
|
# cargo run --release --example cli $1 && \
|
||||||
# clang hello.o -o main && \
|
# # clang hello.o -o main && \
|
||||||
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
# ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
||||||
-o main /usr/lib/crt1.o hello.o -lc && \
|
# -o main /usr/lib/crt1.o hello.o -lc && \
|
||||||
./main ; echo "Return value: ""$?"
|
# ./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
|
## Command from: clang -v hello.o -o test
|
||||||
## Original command:
|
## Original command:
|
||||||
|
@ -163,7 +163,9 @@ impl IndexedVariableReference {
|
|||||||
}
|
}
|
||||||
super::IndexedVariableReferenceKind::Index(inner, _) => {
|
super::IndexedVariableReferenceKind::Index(inner, _) => {
|
||||||
if let Some((mutable, inner_ref)) = inner.find_hint(hints)? {
|
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 {
|
match inner_ty {
|
||||||
Array(type_kind, _) => Ok(hints
|
Array(type_kind, _) => Ok(hints
|
||||||
.from_type(&type_kind)
|
.from_type(&type_kind)
|
||||||
@ -284,7 +286,9 @@ impl Expression {
|
|||||||
ExprKind::Index(expression, index_ty, _) => {
|
ExprKind::Index(expression, index_ty, _) => {
|
||||||
let expr_ty = expression.infer_types(state, type_refs)?;
|
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 {
|
match kind {
|
||||||
Array(type_kind, _) => {
|
Array(type_kind, _) => {
|
||||||
let elem_ty = type_refs.from_type(&type_kind).unwrap();
|
let elem_ty = type_refs.from_type(&type_kind).unwrap();
|
||||||
|
@ -15,23 +15,22 @@ use super::{
|
|||||||
pub struct TypeRef<'scope>(TypeIdRef, &'scope ScopeTypeRefs<'scope>);
|
pub struct TypeRef<'scope>(TypeIdRef, &'scope ScopeTypeRefs<'scope>);
|
||||||
|
|
||||||
impl<'scope> TypeRef<'scope> {
|
impl<'scope> TypeRef<'scope> {
|
||||||
pub unsafe fn resolve_type(&self) -> TypeKind {
|
/// Resolve current type in a weak manner, not resolving any Arrays or
|
||||||
unsafe {
|
/// further inner types
|
||||||
let resolved = self
|
pub fn resolve_weak(&self) -> Option<TypeKind> {
|
||||||
.1
|
Some(self.1.types.retrieve_type(*self.0.borrow())?)
|
||||||
.types
|
}
|
||||||
.hints
|
|
||||||
.borrow()
|
|
||||||
.get_unchecked(*self.0.borrow())
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
match resolved {
|
/// Resolve type deeply, trying to resolve any inner types as well.
|
||||||
TypeKind::Array(elem_ty, len) => {
|
pub fn resolve_deep(&self) -> Option<TypeKind> {
|
||||||
let resolved_elem_ty = self.1.from_type(&elem_ty).unwrap().resolve_type();
|
let resolved = self.resolve_weak()?;
|
||||||
TypeKind::Array(Box::new(resolved_elem_ty), len)
|
|
||||||
}
|
match resolved {
|
||||||
_ => 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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_tuple("Hint")
|
f.debug_tuple("Hint")
|
||||||
.field(&self.0)
|
.field(&self.0)
|
||||||
.field(unsafe { &self.resolve_type() })
|
.field(&self.resolve_deep().unwrap())
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
reid_src/array
Executable file
BIN
reid_src/array
Executable file
Binary file not shown.
38
test.ll
38
test.ll
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user