Skip to content

Commit

Permalink
made const arrays provide const elements
Browse files Browse the repository at this point in the history
  • Loading branch information
xarkenz committed Dec 9, 2023
1 parent a2ab4b3 commit c16315e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 54 deletions.
26 changes: 11 additions & 15 deletions src/compiler/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl<W: Write> Generator<W> {
Ok(value)
}
else {
match (&original_format, target_format) {
match (original_format.as_unqualified(), target_format.as_unqualified()) {
(Format::Pointer(_), Format::Pointer(_)) => {
let result = self.new_anonymous_register(target_format.clone());
self.emitter.emit_bitwise_cast(&result, &value)?;
Expand Down Expand Up @@ -858,14 +858,12 @@ impl<W: Write> Generator<W> {
Format::Pointer(pointee_format) => match pointee_format.as_unqualified() {
Format::Array { item_format, length } => {
// const &*[T; N], const &*[T]
let element_pointer_format = item_format.as_ref().clone().into_pointer();
let element_pointer_format = match pointee_format.as_ref() {
Format::Constant(_) => element_pointer_format.into_constant(),
_ => element_pointer_format
let element_format = match pointee_format.as_ref() {
Format::Constant(_) => item_format.as_ref().clone().into_constant(),
_ => item_format.as_ref().clone()
};

let loaded_pointer = self.new_anonymous_register(loaded_format.clone());
let element_pointer = self.new_anonymous_register(element_pointer_format);
let element_pointer = self.new_anonymous_register(element_format.clone().into_pointer());
let indices = match length {
Some(_) => vec![Value::Constant(Constant::Integer(0, Format::default_integer())), Value::Constant(rhs)],
None => vec![Value::Constant(rhs)],
Expand All @@ -881,7 +879,7 @@ impl<W: Write> Generator<W> {

Value::Indirect {
pointer: Box::new(Value::Register(element_pointer)),
loaded_format: item_format.as_ref().clone(),
loaded_format: element_format,
}
},
_ => return Err(cannot_index_error())
Expand Down Expand Up @@ -940,14 +938,12 @@ impl<W: Write> Generator<W> {
Format::Pointer(pointee_format) => match pointee_format.as_unqualified() {
Format::Array { item_format, length } => {
// &*[T; N], &*[T]
let element_pointer_format = item_format.as_ref().clone().into_pointer();
let element_pointer_format = match pointee_format.as_ref() {
Format::Constant(_) => element_pointer_format.into_constant(),
_ => element_pointer_format
let element_format = match pointee_format.as_ref() {
Format::Constant(_) => item_format.as_ref().clone().into_constant(),
_ => item_format.as_ref().clone()
};

let loaded_pointer = self.new_anonymous_register(loaded_format.clone());
let element_pointer = self.new_anonymous_register(element_pointer_format);
let element_pointer = self.new_anonymous_register(element_format.clone().into_pointer());
let indices = match length {
Some(_) => vec![Value::Constant(Constant::Integer(0, Format::default_integer())), rhs],
None => vec![rhs],
Expand All @@ -963,7 +959,7 @@ impl<W: Write> Generator<W> {

Value::Indirect {
pointer: Box::new(Value::Register(element_pointer)),
loaded_format: item_format.as_ref().clone(),
loaded_format: element_format,
}
},
_ => return Err(cannot_index_error())
Expand Down
39 changes: 0 additions & 39 deletions src/compiler_driver/test.ll
Original file line number Diff line number Diff line change
Expand Up @@ -203,42 +203,3 @@ define dso_local void @aoc_01_p1() #0 {
define dso_local i32 @main() #0 {
%1 = load i8*, i8** @input
%2 = getelementptr inbounds i8, i8* %1, i32 0
store i8 99, i8* %2
call void() @aoc_01_p1()
ret i32 0
}

declare i32 @printf(i8* noundef, ...) #1

declare i8* @malloc(i64 noundef) #1

declare void @free(i8* noundef) #1

attributes #0 = {
noinline nounwind optnone uwtable
"frame-pointer"="all"
"min-legal-vector-width"="0"
"no-trapping-math"="true"
"stack-protector-buffer-size"="8"
"target-cpu"="x86-64"
"target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
"tune-cpu"="generic"
}

attributes #1 = {
"frame-pointer"="all"
"no-trapping-math"="true"
"stack-protector-buffer-size"="8"
"target-cpu"="x86-64"
"target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
"tune-cpu"="generic"
}

!llvm.module.flags = !{ !0, !1, !2, !3, !4 }
!llvm.ident = !{ !5 }
!0 = !{ i32 1, !"wchar_size", i32 4 }
!1 = !{ i32 7, !"PIC Level", i32 2 }
!2 = !{ i32 7, !"PIE Level", i32 2 }
!3 = !{ i32 7, !"uwtable", i32 1 }
!4 = !{ i32 7, !"frame-pointer", i32 2 }
!5 = !{ !"Ubuntu clang version 14.0.0-1ubuntu1.1" }

0 comments on commit c16315e

Please sign in to comment.