Skip to content

Commit

Permalink
improv cast
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Mar 12, 2024
1 parent a70924b commit 9ca2e33
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/edlang_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub enum Expression {
Binary(Box<Self>, BinaryOp, Box<Self>),
Deref(Box<Self>, Span),
AsRef(Box<Self>, bool, Span),
Cast(PathExpr, Type, Span),
Cast(Box<Self>, Type, Span),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
4 changes: 2 additions & 2 deletions lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ fn compile_rvalue<'ctx>(
);
compile_unary_op(ctx, fn_id, locals, *op, value)?
}
ir::RValue::Cast(place, target_ty, span) => {
ir::RValue::Cast(op, target_ty, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
Expand All @@ -1044,7 +1044,7 @@ fn compile_rvalue<'ctx>(

let target_ty = target_ty.clone();
let target_llvm_ty = compile_basic_type(ctx, &target_ty);
let (value, ty) = compile_load_place(ctx, fn_id, locals, place, false)?;
let (value, ty) = compile_load_operand(ctx, fn_id, locals, op)?;
let current_ty = compile_basic_type(ctx, &ty);

if target_llvm_ty.is_pointer_type() {
Expand Down
2 changes: 1 addition & 1 deletion lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub enum RValue {
BinOp(BinOp, Operand, Operand, Span),
LogicOp(LogicalOp, Operand, Operand, Span),
UnOp(UnOp, Operand, Span),
Cast(Place, TypeInfo, Span),
Cast(Operand, TypeInfo, Span),
}

#[derive(Debug, Clone)]
Expand Down
29 changes: 26 additions & 3 deletions lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,14 +735,37 @@ fn lower_expr(

(RValue::Use(Operand::Move(place), info.span), ty, info.span)
}
ast::Expression::Cast(path, cast_ty, span) => {
let (place, _ty, _path_span) = lower_path(builder, path)?;
ast::Expression::Cast(exp, cast_ty, span) => {
let (value, ty, _exp_span) = lower_expr(builder, exp, None)?;
let new_ty = lower_type(&builder.ctx, cast_ty, builder.local_module)?;
let kind = new_ty.kind.clone();

// todo: some checks?

(RValue::Cast(place, new_ty, *span), kind, *span)
// check if its a use directly, to avoid a temporary.
let rvalue = match value {
RValue::Use(op, _) => RValue::Cast(op, new_ty.clone(), *span),
value => {
let inner_local = builder.add_local(Local::temp(ty.clone()));
let inner_place = Place {
local: inner_local,
projection: Default::default(),
};

builder.statements.push(Statement {
span: None,
kind: StatementKind::StorageLive(inner_local),
});

builder.statements.push(Statement {
span: None,
kind: StatementKind::Assign(inner_place.clone(), value),
});
RValue::Cast(Operand::Move(inner_place), new_ty.clone(), *span)
}
};

(rvalue, kind, *span)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/edlang_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub(crate) Expression: ast::Expression = {
Box::new(rhs)
),
#[precedence(level="5")] #[assoc(side="left")]
<lo:@L> <a:PathExpr> "as" <b: Type> <hi:@R> => ast::Expression::Cast(a, b, ast::Span::new(lo, hi)),
<lo:@L> <a:Expression> "as" <b: Type> <hi:@R> => ast::Expression::Cast(Box::new(a), b, ast::Span::new(lo, hi)),
"(" <StructInitExpr> ")" => ast::Expression::StructInit(<>),
}

Expand Down

0 comments on commit 9ca2e33

Please sign in to comment.