Skip to content

Commit

Permalink
[spv-out] Add scalar conversion support
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo-DK authored and kvark committed Oct 3, 2020
1 parent 9816710 commit 90c56ac
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/back/spv/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,49 @@ pub(super) fn instruction_function_call(
//
// Conversion Instructions
//
fn instruction_unary(op: Op, result_type_id: Word, id: Word, value: Word) -> Instruction {
let mut instruction = Instruction::new(op);
instruction.set_type(result_type_id);
instruction.set_result(id);
instruction.add_operand(value);
instruction
}

pub(super) fn instruction_convert_f_to_u(
result_type_id: Word,
id: Word,
float_value: Word,
) -> Instruction {
instruction_unary(Op::ConvertFToU, result_type_id, id, float_value)
}

pub(super) fn instruction_convert_f_to_s(
result_type_id: Word,
id: Word,
float_value: Word,
) -> Instruction {
instruction_unary(Op::ConvertFToS, result_type_id, id, float_value)
}

pub(super) fn instruction_convert_s_to_f(
result_type_id: Word,
id: Word,
signed_value: Word,
) -> Instruction {
instruction_unary(Op::ConvertSToF, result_type_id, id, signed_value)
}

pub(super) fn instruction_convert_u_to_f(
result_type_id: Word,
id: Word,
unsigned_value: Word,
) -> Instruction {
instruction_unary(Op::ConvertUToF, result_type_id, id, unsigned_value)
}

pub(super) fn instruction_bit_cast(result_type_id: Word, id: Word, operand: Word) -> Instruction {
instruction_unary(Op::Bitcast, result_type_id, id, operand)
}
//
// Composite Instructions
//
Expand Down
73 changes: 73 additions & 0 deletions src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,79 @@ impl Writer {
}
_ => unimplemented!("{:?}", origin),
},
crate::Expression::As {
expr,
kind,
convert,
} => {
if !convert {
return None;
}

let (expr_id, expr_type) = self
.write_expression(
ir_module,
ir_function,
&ir_function.expressions[*expr],
block,
function,
)
.unwrap();

let id = self.generate_id();
let instruction = match ir_module.types[expr_type.unwrap()].inner {
crate::TypeInner::Scalar {
kind: expr_kind,
width,
} => {
let kind_type_id = self.get_type_id(
&ir_module.types,
LookupType::Local(LocalType::Scalar { kind: *kind, width }),
);

if *convert {
super::instructions::instruction_bit_cast(kind_type_id, id, expr_id)
} else {
match (expr_kind, kind) {
(crate::ScalarKind::Float, crate::ScalarKind::Uint) => {
super::instructions::instruction_convert_f_to_u(
kind_type_id,
id,
expr_id,
)
}
(crate::ScalarKind::Float, crate::ScalarKind::Sint) => {
super::instructions::instruction_convert_f_to_s(
kind_type_id,
id,
expr_id,
)
}
(crate::ScalarKind::Sint, crate::ScalarKind::Float) => {
super::instructions::instruction_convert_s_to_f(
kind_type_id,
id,
expr_id,
)
}
(crate::ScalarKind::Uint, crate::ScalarKind::Float) => {
super::instructions::instruction_convert_u_to_f(
kind_type_id,
id,
expr_id,
)
}
_ => unreachable!(),
}
}
}
_ => unreachable!(),
};

block.body.push(instruction);

Some((id, None))
}
_ => unimplemented!("{:?}", expression),
}
}
Expand Down

0 comments on commit 90c56ac

Please sign in to comment.