13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "condition.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include <compiler/core/pandagen.h>
193af6ab5fSopenharmony_ci#include <ir/expressions/binaryExpression.h>
203af6ab5fSopenharmony_ci#include <ir/expressions/unaryExpression.h>
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_cinamespace panda::es2panda::compiler {
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_civoid Condition::Compile(PandaGen *pg, const ir::Expression *expr, Label *falseLabel)
253af6ab5fSopenharmony_ci{
263af6ab5fSopenharmony_ci    if (expr->IsBinaryExpression()) {
273af6ab5fSopenharmony_ci        const auto *binExpr = expr->AsBinaryExpression();
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci        switch (binExpr->OperatorType()) {
303af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_EQUAL:
313af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_NOT_EQUAL:
323af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_STRICT_EQUAL:
333af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL:
343af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_LESS_THAN:
353af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_LESS_THAN_EQUAL:
363af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_GREATER_THAN:
373af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_GREATER_THAN_EQUAL: {
383af6ab5fSopenharmony_ci                // This is a special case
393af6ab5fSopenharmony_ci                // These operators are expressed via cmp instructions and the following
403af6ab5fSopenharmony_ci                // if-else branches. Condition also expressed via cmp instruction and
413af6ab5fSopenharmony_ci                // the following if-else.
423af6ab5fSopenharmony_ci                // the goal of this method is to merge these two sequences of instructions.
433af6ab5fSopenharmony_ci                RegScope rs(pg);
443af6ab5fSopenharmony_ci                VReg lhs = pg->AllocReg();
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci                binExpr->Left()->Compile(pg);
473af6ab5fSopenharmony_ci                pg->StoreAccumulator(binExpr, lhs);
483af6ab5fSopenharmony_ci                binExpr->Right()->Compile(pg);
493af6ab5fSopenharmony_ci                pg->Condition(binExpr, binExpr->OperatorType(), lhs, falseLabel);
503af6ab5fSopenharmony_ci                return;
513af6ab5fSopenharmony_ci            }
523af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: {
533af6ab5fSopenharmony_ci                binExpr->Left()->Compile(pg);
543af6ab5fSopenharmony_ci                pg->BranchIfFalse(binExpr, falseLabel);
553af6ab5fSopenharmony_ci
563af6ab5fSopenharmony_ci                binExpr->Right()->Compile(pg);
573af6ab5fSopenharmony_ci                pg->BranchIfFalse(binExpr, falseLabel);
583af6ab5fSopenharmony_ci                return;
593af6ab5fSopenharmony_ci            }
603af6ab5fSopenharmony_ci            case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: {
613af6ab5fSopenharmony_ci                auto *endLabel = pg->AllocLabel();
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci                binExpr->Left()->Compile(pg);
643af6ab5fSopenharmony_ci                pg->BranchIfTrue(binExpr, endLabel);
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci                binExpr->Right()->Compile(pg);
673af6ab5fSopenharmony_ci                pg->BranchIfFalse(binExpr, falseLabel);
683af6ab5fSopenharmony_ci                pg->SetLabel(binExpr, endLabel);
693af6ab5fSopenharmony_ci                return;
703af6ab5fSopenharmony_ci            }
713af6ab5fSopenharmony_ci            default: {
723af6ab5fSopenharmony_ci                break;
733af6ab5fSopenharmony_ci            }
743af6ab5fSopenharmony_ci        }
753af6ab5fSopenharmony_ci    } else if (expr->IsUnaryExpression() &&
763af6ab5fSopenharmony_ci               expr->AsUnaryExpression()->OperatorType() == lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK) {
773af6ab5fSopenharmony_ci        expr->AsUnaryExpression()->Argument()->Compile(pg);
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci        pg->Negate(expr);
803af6ab5fSopenharmony_ci        pg->BranchIfFalse(expr, falseLabel);
813af6ab5fSopenharmony_ci        return;
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci
843af6ab5fSopenharmony_ci    // General case including some binExpr i.E.(a+b)
853af6ab5fSopenharmony_ci    expr->Compile(pg);
863af6ab5fSopenharmony_ci    pg->BranchIfFalse(expr, falseLabel);
873af6ab5fSopenharmony_ci}
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci}  // namespace panda::es2panda::compiler
90