Skip to content

Commit

Permalink
Removed old reference-counting and lifetime analysis code
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Aug 28, 2024
1 parent 02ec1e0 commit d399d3d
Show file tree
Hide file tree
Showing 28 changed files with 41 additions and 840 deletions.
3 changes: 0 additions & 3 deletions src/ast/datatype/kind/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ impl Display for &TypeKind {
TypeKind::Pointer(inner) => {
write!(f, "ptr<{inner}>")?;
}
TypeKind::PlainOldData(inner) => {
write!(f, "pod<{inner}>")?;
}
TypeKind::Void => {
write!(f, "void")?;
}
Expand Down
1 change: 0 additions & 1 deletion src/ast/datatype/kind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub enum TypeKind {
Floating(FloatSize),
Pointer(Box<Type>),
FixedArray(Box<FixedArray>),
PlainOldData(Box<Type>),
Void,
Named(String),
AnonymousStruct(AnonymousStruct),
Expand Down
1 change: 0 additions & 1 deletion src/ast/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct Structure {
pub name: String,
pub fields: IndexMap<String, Field>,
pub is_packed: bool,
pub prefer_pod: bool,
pub source: Source,
}

Expand Down
1 change: 0 additions & 1 deletion src/c/translation/types/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub fn make_composite(
name: name.clone(),
fields,
is_packed,
prefer_pod: true,
source: composite.source,
});

Expand Down
1 change: 0 additions & 1 deletion src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
},
)]),
is_packed: false,
prefer_pod: false,
source,
});

Expand Down
23 changes: 0 additions & 23 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,29 +377,6 @@ impl Type {
Type::Pointer(Box::new(self.clone()))
}

pub fn reference_counted_pointer(&self) -> Self {
// Don't allow wrapping pointer values with reference counting
// This will catch us if we accidentally nest more than once
assert!(!self.is_pointer());

Type::Pointer(Box::new(self.reference_counted_no_pointer()))
}

pub fn reference_counted_no_pointer(&self) -> Self {
let subtypes = vec![
// Reference count
Field::basic(Type::U64, Source::internal()),
// Value
Field::basic(self.clone(), Source::internal()),
];

Type::AnonymousComposite(TypeComposite {
fields: subtypes,
is_packed: false,
source: Source::internal(),
})
}

pub fn is_integer_like(&self) -> bool {
matches!(
self,
Expand Down
142 changes: 3 additions & 139 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,7 @@ fn lower_stmts(

for stmt in stmts.iter() {
result = match &stmt.kind {
StmtKind::Return(expr, drops) => {
for variable_key in drops.drops.iter() {
lower_drop(
builder,
&ir_module.target,
*variable_key,
function,
resolved_ast,
)?;
}

StmtKind::Return(expr) => {
let instruction = ir::Instruction::Return(if let Some(expr) = expr {
Some(lower_expr(
builder,
Expand Down Expand Up @@ -311,43 +301,11 @@ fn lower_stmts(
Value::Literal(Literal::Void)
}
};

for variable_key in stmt.drops.iter() {
lower_drop(
builder,
&ir_module.target,
*variable_key,
function,
resolved_ast,
)?;
}
}

Ok(result)
}

fn lower_drop(
builder: &mut Builder,
target: &Target,
variable_key: VariableStorageKey,
function: &resolved::Function,
resolved_ast: &resolved::Ast,
) -> Result<(), LowerError> {
let variable = function
.variables
.get(variable_key)
.expect("referenced variable to exist");

if variable.resolved_type.kind.is_managed_structure() {
let variable_pointer = lower_variable_to_value(variable_key);
let variable_type = lower_type(target, &variable.resolved_type, resolved_ast)?;
let heap_pointer = builder.push(ir::Instruction::Load((variable_pointer, variable_type)));
builder.push(ir::Instruction::Free(heap_pointer));
}

Ok(())
}

fn lower_type(
target: &Target,
resolved_type: &resolved::Type,
Expand Down Expand Up @@ -390,12 +348,7 @@ fn lower_type(
resolved_ast,
)?))),
resolved::TypeKind::Void => Ok(ir::Type::Void),
resolved::TypeKind::ManagedStructure(_, structure_ref) => {
Ok(ir::Type::Structure(*structure_ref).reference_counted_pointer())
}
resolved::TypeKind::PlainOldData(_, structure_ref) => {
Ok(ir::Type::Structure(*structure_ref))
}
resolved::TypeKind::Structure(_, structure_ref) => Ok(ir::Type::Structure(*structure_ref)),
resolved::TypeKind::AnonymousStruct() => {
todo!("lower anonymous struct")
}
Expand Down Expand Up @@ -450,33 +403,11 @@ fn lower_destination(
subject,
structure_ref,
index,
memory_management,
..
} => {
let subject_pointer =
lower_destination(builder, ir_module, subject, function, resolved_ast)?;

let subject_pointer = match memory_management {
resolved::MemoryManagement::None => subject_pointer,
resolved::MemoryManagement::ReferenceCounted => {
// Load pointer from pointed object

let struct_type =
ir::Type::Structure(*structure_ref).reference_counted_no_pointer();

let subject_pointer = builder.push(ir::Instruction::Load((
subject_pointer,
struct_type.pointer(),
)));

builder.push(ir::Instruction::Member {
struct_type,
subject_pointer,
index: 1,
})
}
};

Ok(builder.push(ir::Instruction::Member {
subject_pointer,
struct_type: ir::Type::Structure(*structure_ref),
Expand Down Expand Up @@ -733,36 +664,11 @@ fn lower_expr(
structure_ref,
index,
field_type,
memory_management,
} = &**member;

let subject_pointer =
lower_destination(builder, ir_module, subject, function, resolved_ast)?;

let subject_pointer = match memory_management {
resolved::MemoryManagement::None => subject_pointer,
resolved::MemoryManagement::ReferenceCounted => {
// Take off reference counted wrapper

// Get inner structure type
let struct_type =
ir::Type::Structure(*structure_ref).reference_counted_no_pointer();

// Load pointer to referece counted wrapper
let subject_pointer = builder.push(ir::Instruction::Load((
subject_pointer,
struct_type.pointer(),
)));

// Obtain pointer to inner data
builder.push(ir::Instruction::Member {
subject_pointer,
struct_type,
index: 1,
})
}
};

// Access member of structure
let member = builder.push(ir::Instruction::Member {
subject_pointer,
Expand Down Expand Up @@ -802,7 +708,6 @@ fn lower_expr(
let StructureLiteral {
structure_type,
fields,
memory_management,
} = &**structure_literal;

let result_ir_type = lower_type(&ir_module.target, structure_type, resolved_ast)?;
Expand All @@ -820,48 +725,7 @@ fn lower_expr(
// Drop the index part of the values
let values = values.drain(..).map(|(_, value)| value).collect();

match memory_management {
resolved::MemoryManagement::None => {
Ok(builder.push(ir::Instruction::StructureLiteral(result_ir_type, values)))
}
resolved::MemoryManagement::ReferenceCounted => {
let flat = builder.push(ir::Instruction::StructureLiteral(
result_ir_type.clone(),
values,
));

let wrapper_struct_type = result_ir_type.reference_counted_no_pointer();

let heap_memory =
builder.push(ir::Instruction::Malloc(wrapper_struct_type.clone()));

// TODO: Assert that malloc didn't return NULL

let at_reference_count = builder.push(ir::Instruction::Member {
subject_pointer: heap_memory.clone(),
struct_type: wrapper_struct_type.clone(),
index: 0,
});

builder.push(ir::Instruction::Store(ir::Store {
new_value: ir::Value::Literal(Literal::Unsigned64(1)),
destination: at_reference_count,
}));

let at_value = builder.push(ir::Instruction::Member {
subject_pointer: heap_memory.clone(),
struct_type: wrapper_struct_type.clone(),
index: 1,
});

builder.push(ir::Instruction::Store(ir::Store {
new_value: flat,
destination: at_value,
}));

Ok(heap_memory)
}
}
Ok(builder.push(ir::Instruction::StructureLiteral(result_ir_type, values)))
}
ExprKind::UnaryOperation(unary_operation) => {
let inner = lower_expr(
Expand Down
2 changes: 0 additions & 2 deletions src/parser/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub enum AnnotationKind {
Foreign,
ThreadLocal,
Packed,
Pod,
AbideAbi,
}

Expand All @@ -34,7 +33,6 @@ impl Display for AnnotationKind {
Self::Foreign => "foreign",
Self::ThreadLocal => "thread_local",
Self::Packed => "packed",
Self::Pod => "pod",
Self::AbideAbi => "abide_abi",
})
}
Expand Down
1 change: 0 additions & 1 deletion src/parser/parse_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
"foreign" => AnnotationKind::Foreign,
"thread_local" => AnnotationKind::ThreadLocal,
"packed" => AnnotationKind::Packed,
"pod" => AnnotationKind::Pod,
"abide_abi" => AnnotationKind::AbideAbi,
_ => {
return Err(ParseErrorKind::UnrecognizedAnnotation {
Expand Down
3 changes: 0 additions & 3 deletions src/parser/parse_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.ignore_newlines();

let mut is_packed = false;
let mut prefer_pod = false;

for annotation in annotations {
match annotation.kind {
AnnotationKind::Packed => is_packed = true,
AnnotationKind::Pod => prefer_pod = true,
_ => return Err(self.unexpected_annotation(&annotation, Some("for structure"))),
}
}
Expand Down Expand Up @@ -66,7 +64,6 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
name,
fields,
is_packed,
prefer_pod,
source,
})
}
Expand Down
6 changes: 0 additions & 6 deletions src/parser/parse_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
count,
})))
}
"pod" => {
self.parse_token(TokenKind::OpenAngle, Some("to specify inner type of 'pod'"))?;
let inner = self.parse_type(None::<&str>, None::<&str>)?;
self.parse_type_parameters_close()?;
Ok(TypeKind::PlainOldData(Box::new(inner)))
}
identifier => Ok(TypeKind::Named(identifier.into())),
}?;

Expand Down
21 changes: 6 additions & 15 deletions src/resolve/core_structure_info.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
use super::error::{ResolveError, ResolveErrorKind};
use crate::{
resolved::{self, MemoryManagement, StructureRef},
resolved::{self, StructureRef},
source_files::Source,
};

pub fn get_core_structure_info(
resolved_type: &resolved::Type,
source: Source,
) -> Result<(&str, StructureRef, MemoryManagement), ResolveError> {
) -> Result<(&str, StructureRef), ResolveError> {
match &resolved_type.kind {
resolved::TypeKind::PlainOldData(name, structure_ref) => {
Ok((name, *structure_ref, resolved::MemoryManagement::None))
resolved::TypeKind::Structure(name, structure_ref) => Ok((name, *structure_ref)),
_ => Err(ResolveErrorKind::CannotCreateStructLiteralForNonStructure {
bad_type: resolved_type.to_string(),
}
resolved::TypeKind::ManagedStructure(name, structure_ref) => Ok((
name,
*structure_ref,
resolved::MemoryManagement::ReferenceCounted,
)),
_ => Err(
ResolveErrorKind::CannotCreateStructLiteralForNonPlainOldDataStructure {
bad_type: resolved_type.to_string(),
}
.at(source),
),
.at(source)),
}
}
Loading

0 comments on commit d399d3d

Please sign in to comment.