From 46bc882d19f012c6a3491f647fee5b886079e48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Sun, 12 Jun 2022 16:29:11 +0100 Subject: [PATCH] msl: don't rely on cached expressions Expressions marked for caching might not be cached, this can happen for example when these expressions are pre emitted (for example Contants). --- src/back/msl/writer.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/back/msl/writer.rs b/src/back/msl/writer.rs index 1d63a07535..1f85c4d0ab 100644 --- a/src/back/msl/writer.rs +++ b/src/back/msl/writer.rs @@ -1223,20 +1223,30 @@ impl Writer { arg: Handle, arg1: Handle, size: usize, + context: &ExpressionContext, ) -> BackendResult { + // Write parantheses around the dot product expression to prevent operators + // with different precedences from applying earlier. write!(self.out, "(")?; - let arg0_name = &self.named_expressions[&arg]; - let arg1_name = &self.named_expressions[&arg1]; - - // This will print an extra '+' at the beginning but that is fine in msl + // Cycle trough all the components of the vector for index in 0..size { let component = back::COMPONENTS[index]; - write!( - self.out, - " + {}.{} * {}.{}", - arg0_name, component, arg1_name, component - )?; + // Write the addition to the previous product + // This will print an extra '+' at the beginning but that is fine in msl + write!(self.out, " + ")?; + // Write the first vector expression, this expression is marked to be + // cached so unless it can't be cached (for example, it's a Constant) + // it shouldn't produce large expressions. + self.put_expression(arg, context, true)?; + // Access the current component on the first vector + write!(self.out, ".{} * ", component)?; + // Write the second vector expression, this expression is marked to be + // cached so unless it can't be cached (for example, it's a Constant) + // it shouldn't produce large expressions. + self.put_expression(arg1, context, true)?; + // Access the current component on the second vector + write!(self.out, ".{}", component)?; } write!(self.out, ")")?; @@ -1654,7 +1664,7 @@ impl Writer { .. } => "dot", crate::TypeInner::Vector { size, .. } => { - return self.put_dot_product(arg, arg1.unwrap(), size as usize) + return self.put_dot_product(arg, arg1.unwrap(), size as usize, context) } _ => unreachable!( "Correct TypeInner for dot product should be already validated"