| // Copyright 2020 The Tint Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "src/writer/msl/generator_impl.h" |
| |
| #include "src/ast/array_accessor_expression.h" |
| #include "src/ast/as_expression.h" |
| #include "src/ast/assignment_statement.h" |
| #include "src/ast/binary_expression.h" |
| #include "src/ast/bool_literal.h" |
| #include "src/ast/cast_expression.h" |
| #include "src/ast/else_statement.h" |
| #include "src/ast/float_literal.h" |
| #include "src/ast/function.h" |
| #include "src/ast/identifier_expression.h" |
| #include "src/ast/if_statement.h" |
| #include "src/ast/member_accessor_expression.h" |
| #include "src/ast/return_statement.h" |
| #include "src/ast/sint_literal.h" |
| #include "src/ast/type/alias_type.h" |
| #include "src/ast/type/array_type.h" |
| #include "src/ast/type/bool_type.h" |
| #include "src/ast/type/f32_type.h" |
| #include "src/ast/type/i32_type.h" |
| #include "src/ast/type/matrix_type.h" |
| #include "src/ast/type/pointer_type.h" |
| #include "src/ast/type/struct_type.h" |
| #include "src/ast/type/u32_type.h" |
| #include "src/ast/type/vector_type.h" |
| #include "src/ast/type/void_type.h" |
| #include "src/ast/uint_literal.h" |
| #include "src/ast/unary_op_expression.h" |
| |
| namespace tint { |
| namespace writer { |
| namespace msl { |
| |
| GeneratorImpl::GeneratorImpl() = default; |
| |
| GeneratorImpl::~GeneratorImpl() = default; |
| |
| bool GeneratorImpl::Generate(const ast::Module& module) { |
| module_ = &module; |
| |
| for (auto* const alias : module.alias_types()) { |
| if (!EmitAliasType(alias)) { |
| return false; |
| } |
| } |
| if (!module.alias_types().empty()) { |
| out_ << std::endl; |
| } |
| |
| for (const auto& func : module.functions()) { |
| if (!EmitFunction(func.get())) { |
| return false; |
| } |
| out_ << std::endl; |
| } |
| |
| module_ = nullptr; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitAliasType(const ast::type::AliasType* alias) { |
| make_indent(); |
| out_ << "typedef "; |
| if (!EmitType(alias->type(), "")) { |
| return false; |
| } |
| out_ << " " << alias->name() << ";" << std::endl; |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { |
| if (!EmitExpression(expr->array())) { |
| return false; |
| } |
| out_ << "["; |
| |
| if (!EmitExpression(expr->idx_expr())) { |
| return false; |
| } |
| out_ << "]"; |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitAs(ast::AsExpression* expr) { |
| out_ << "as_type<"; |
| if (!EmitType(expr->type(), "")) { |
| return false; |
| } |
| |
| out_ << ">("; |
| if (!EmitExpression(expr->expr())) { |
| return false; |
| } |
| |
| out_ << ")"; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { |
| make_indent(); |
| |
| if (!EmitExpression(stmt->lhs())) { |
| return false; |
| } |
| |
| out_ << " = "; |
| |
| if (!EmitExpression(stmt->rhs())) { |
| return false; |
| } |
| |
| out_ << ";" << std::endl; |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { |
| out_ << "("; |
| |
| if (!EmitExpression(expr->lhs())) { |
| return false; |
| } |
| out_ << " "; |
| |
| switch (expr->op()) { |
| case ast::BinaryOp::kAnd: |
| out_ << "&"; |
| break; |
| case ast::BinaryOp::kOr: |
| out_ << "|"; |
| break; |
| case ast::BinaryOp::kXor: |
| out_ << "^"; |
| break; |
| case ast::BinaryOp::kLogicalAnd: |
| out_ << "&&"; |
| break; |
| case ast::BinaryOp::kLogicalOr: |
| out_ << "||"; |
| break; |
| case ast::BinaryOp::kEqual: |
| out_ << "=="; |
| break; |
| case ast::BinaryOp::kNotEqual: |
| out_ << "!="; |
| break; |
| case ast::BinaryOp::kLessThan: |
| out_ << "<"; |
| break; |
| case ast::BinaryOp::kGreaterThan: |
| out_ << ">"; |
| break; |
| case ast::BinaryOp::kLessThanEqual: |
| out_ << "<="; |
| break; |
| case ast::BinaryOp::kGreaterThanEqual: |
| out_ << ">="; |
| break; |
| case ast::BinaryOp::kShiftLeft: |
| out_ << "<<"; |
| break; |
| case ast::BinaryOp::kShiftRight: |
| // TODO(dsinclair): MSL is based on C++14, and >> in C++14 has |
| // implementation-defined behaviour for negative LHS. We may have to |
| // generate extra code to implement WGSL-specified behaviour for negative |
| // LHS. |
| out_ << R"(>>)"; |
| break; |
| |
| case ast::BinaryOp::kAdd: |
| out_ << "+"; |
| break; |
| case ast::BinaryOp::kSubtract: |
| out_ << "-"; |
| break; |
| case ast::BinaryOp::kMultiply: |
| out_ << "*"; |
| break; |
| case ast::BinaryOp::kDivide: |
| out_ << "/"; |
| break; |
| case ast::BinaryOp::kModulo: |
| out_ << "%"; |
| break; |
| case ast::BinaryOp::kNone: |
| error_ = "missing binary operation type"; |
| return false; |
| } |
| out_ << " "; |
| |
| if (!EmitExpression(expr->rhs())) { |
| return false; |
| } |
| |
| out_ << ")"; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitCast(ast::CastExpression* expr) { |
| if (!EmitType(expr->type(), "")) { |
| return false; |
| } |
| |
| out_ << "("; |
| if (!EmitExpression(expr->expr())) { |
| return false; |
| } |
| out_ << ")"; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { |
| if (expr->IsScalarConstructor()) { |
| return EmitScalarConstructor(expr->AsScalarConstructor()); |
| } |
| return EmitTypeConstructor(expr->AsTypeConstructor()); |
| } |
| |
| bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) { |
| if (!EmitType(expr->type(), "")) { |
| return false; |
| } |
| |
| out_ << "("; |
| |
| bool first = true; |
| for (const auto& e : expr->values()) { |
| if (!first) { |
| out_ << ", "; |
| } |
| first = false; |
| |
| if (!EmitExpression(e.get())) { |
| return false; |
| } |
| } |
| |
| out_ << ")"; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitScalarConstructor( |
| ast::ScalarConstructorExpression* expr) { |
| return EmitLiteral(expr->literal()); |
| } |
| |
| bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { |
| if (lit->IsBool()) { |
| out_ << (lit->AsBool()->IsTrue() ? "true" : "false"); |
| } else if (lit->IsFloat()) { |
| auto flags = out_.flags(); |
| auto precision = out_.precision(); |
| |
| out_.flags(flags | std::ios_base::showpoint); |
| out_.precision(std::numeric_limits<float>::max_digits10); |
| |
| out_ << lit->AsFloat()->value() << "f"; |
| |
| out_.precision(precision); |
| out_.flags(flags); |
| } else if (lit->IsSint()) { |
| out_ << lit->AsSint()->value(); |
| } else if (lit->IsUint()) { |
| out_ << lit->AsUint()->value() << "u"; |
| } else { |
| error_ = "unknown literal type"; |
| return false; |
| } |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitExpression(ast::Expression* expr) { |
| if (expr->IsArrayAccessor()) { |
| return EmitArrayAccessor(expr->AsArrayAccessor()); |
| } |
| if (expr->IsAs()) { |
| return EmitAs(expr->AsAs()); |
| } |
| if (expr->IsBinary()) { |
| return EmitBinary(expr->AsBinary()); |
| } |
| if (expr->IsCast()) { |
| return EmitCast(expr->AsCast()); |
| } |
| if (expr->IsConstructor()) { |
| return EmitConstructor(expr->AsConstructor()); |
| } |
| if (expr->IsIdentifier()) { |
| return EmitIdentifier(expr->AsIdentifier()); |
| } |
| if (expr->IsMemberAccessor()) { |
| return EmitMemberAccessor(expr->AsMemberAccessor()); |
| } |
| if (expr->IsUnaryOp()) { |
| return EmitUnaryOp(expr->AsUnaryOp()); |
| } |
| |
| error_ = "unknown expression type"; |
| return false; |
| } |
| |
| void GeneratorImpl::EmitStage(ast::PipelineStage stage) { |
| switch (stage) { |
| case ast::PipelineStage::kFragment: |
| out_ << "fragment"; |
| break; |
| case ast::PipelineStage::kVertex: |
| out_ << "vertex"; |
| break; |
| case ast::PipelineStage::kCompute: |
| out_ << "kernel"; |
| break; |
| case ast::PipelineStage::kNone: |
| break; |
| } |
| return; |
| } |
| |
| bool GeneratorImpl::EmitFunction(ast::Function* func) { |
| make_indent(); |
| |
| // TODO(dsinclair): Technically this is wrong as you could, in theory, have |
| // multiple entry points pointing at the same function. I'm ignoring that for |
| // now. It will either go away with the entry_point changes in the spec |
| // or we'll have to figure out how to deal with it. |
| |
| auto name = func->name(); |
| |
| for (const auto& ep : module_->entry_points()) { |
| if (ep->function_name() == name) { |
| EmitStage(ep->stage()); |
| out_ << " "; |
| |
| if (!ep->name().empty()) { |
| name = ep->name(); |
| } |
| |
| break; |
| } |
| } |
| |
| if (!EmitType(func->return_type(), "")) { |
| return false; |
| } |
| |
| out_ << " " << name << "("; |
| |
| bool first = true; |
| for (const auto& v : func->params()) { |
| if (!first) { |
| out_ << ", "; |
| } |
| first = false; |
| |
| if (!EmitType(v->type(), v->name())) { |
| return false; |
| } |
| // Array name is output as part of the type |
| if (!v->type()->IsArray()) { |
| out_ << " " << v->name(); |
| } |
| } |
| out_ << ")"; |
| |
| return EmitStatementBlockAndNewline(func->body()); |
| } |
| |
| bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { |
| auto* ident = expr->AsIdentifier(); |
| if (ident->has_path()) { |
| // TODO(dsinclair): Handle identifier with path |
| error_ = "Identifier paths not handled yet."; |
| return false; |
| } |
| out_ << ident->name(); |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitKill(ast::KillStatement*) { |
| make_indent(); |
| // TODO(dsinclair): Verify this is correct when the kill semantics are defined |
| // for WGSL (https://github.com/gpuweb/gpuweb/issues/361) |
| out_ << "discard_fragment();" << std::endl; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { |
| if (stmt->HasCondition()) { |
| out_ << " else if ("; |
| if (!EmitExpression(stmt->condition())) { |
| return false; |
| } |
| out_ << ")"; |
| } else { |
| out_ << " else"; |
| } |
| |
| return EmitStatementBlock(stmt->body()); |
| } |
| |
| bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { |
| make_indent(); |
| |
| out_ << "if ("; |
| if (!EmitExpression(stmt->condition())) { |
| return false; |
| } |
| out_ << ")"; |
| |
| if (!EmitStatementBlock(stmt->body())) { |
| return false; |
| } |
| |
| for (const auto& e : stmt->else_statements()) { |
| if (!EmitElse(e.get())) { |
| return false; |
| } |
| } |
| out_ << std::endl; |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) { |
| if (!EmitExpression(expr->structure())) { |
| return false; |
| } |
| |
| out_ << "."; |
| |
| return EmitExpression(expr->member()); |
| } |
| |
| bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { |
| make_indent(); |
| |
| out_ << "return"; |
| if (stmt->has_value()) { |
| out_ << " "; |
| if (!EmitExpression(stmt->value())) { |
| return false; |
| } |
| } |
| out_ << ";" << std::endl; |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitStatementBlock(const ast::StatementList& statements) { |
| out_ << " {" << std::endl; |
| |
| increment_indent(); |
| |
| for (const auto& s : statements) { |
| if (!EmitStatement(s.get())) { |
| return false; |
| } |
| } |
| |
| decrement_indent(); |
| make_indent(); |
| out_ << "}"; |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitStatementBlockAndNewline( |
| const ast::StatementList& statements) { |
| const bool result = EmitStatementBlock(statements); |
| if (result) { |
| out_ << std::endl; |
| } |
| return result; |
| } |
| |
| bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { |
| if (stmt->IsAssign()) { |
| return EmitAssign(stmt->AsAssign()); |
| } |
| if (stmt->IsIf()) { |
| return EmitIf(stmt->AsIf()); |
| } |
| if (stmt->IsKill()) { |
| return EmitKill(stmt->AsKill()); |
| } |
| if (stmt->IsReturn()) { |
| return EmitReturn(stmt->AsReturn()); |
| } |
| |
| error_ = "unknown statement type"; |
| return false; |
| } |
| |
| bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { |
| if (type->IsAlias()) { |
| auto* alias = type->AsAlias(); |
| out_ << alias->name(); |
| } else if (type->IsArray()) { |
| auto* ary = type->AsArray(); |
| |
| if (!EmitType(ary->type(), "")) { |
| return false; |
| } |
| if (!name.empty()) { |
| out_ << " " << name; |
| } |
| out_ << "["; |
| if (ary->IsRuntimeArray()) { |
| out_ << "1"; |
| } else { |
| out_ << std::to_string(ary->size()); |
| } |
| |
| out_ << "]"; |
| } else if (type->IsBool()) { |
| out_ << "bool"; |
| } else if (type->IsF32()) { |
| out_ << "float"; |
| } else if (type->IsI32()) { |
| out_ << "int"; |
| } else if (type->IsMatrix()) { |
| auto* mat = type->AsMatrix(); |
| if (!EmitType(mat->type(), "")) { |
| return false; |
| } |
| out_ << mat->columns() << "x" << mat->rows(); |
| } else if (type->IsPointer()) { |
| auto* ptr = type->AsPointer(); |
| // TODO(dsinclair): Storage class? |
| if (!EmitType(ptr->type(), "")) { |
| return false; |
| } |
| out_ << "*"; |
| } else if (type->IsStruct()) { |
| auto* str = type->AsStruct()->impl(); |
| // TODO(dsinclair): Block decoration? |
| // if (str->decoration() != ast::StructDecoration::kNone) { |
| // } |
| out_ << "struct {" << std::endl; |
| |
| increment_indent(); |
| for (const auto& mem : str->members()) { |
| make_indent(); |
| // TODO(dsinclair): Member decorations? |
| // if (!mem->decorations().empty()) { |
| // } |
| |
| if (!EmitType(mem->type(), mem->name())) { |
| return false; |
| } |
| // Array member name will be output with the type |
| if (!mem->type()->IsArray()) { |
| out_ << " " << mem->name(); |
| } |
| out_ << ";" << std::endl; |
| } |
| decrement_indent(); |
| make_indent(); |
| |
| out_ << "}"; |
| } else if (type->IsU32()) { |
| out_ << "uint"; |
| } else if (type->IsVector()) { |
| auto* vec = type->AsVector(); |
| if (!EmitType(vec->type(), "")) { |
| return false; |
| } |
| out_ << vec->size(); |
| } else if (type->IsVoid()) { |
| out_ << "void"; |
| } else { |
| error_ = "unknown type in EmitType"; |
| return false; |
| } |
| |
| return true; |
| } |
| |
| bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { |
| switch (expr->op()) { |
| case ast::UnaryOp::kNot: |
| out_ << "!"; |
| break; |
| case ast::UnaryOp::kNegation: |
| out_ << "-"; |
| break; |
| } |
| out_ << "("; |
| |
| if (!EmitExpression(expr->expr())) { |
| return false; |
| } |
| |
| out_ << ")"; |
| |
| return true; |
| } |
| |
| } // namespace msl |
| } // namespace writer |
| } // namespace tint |