// Copyright 2022 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/tint/ir/disassembler.h"

#include "src//tint/ir/unary.h"
#include "src/tint/constant/composite.h"
#include "src/tint/constant/scalar.h"
#include "src/tint/constant/splat.h"
#include "src/tint/ir/binary.h"
#include "src/tint/ir/bitcast.h"
#include "src/tint/ir/block.h"
#include "src/tint/ir/builtin.h"
#include "src/tint/ir/construct.h"
#include "src/tint/ir/convert.h"
#include "src/tint/ir/discard.h"
#include "src/tint/ir/function_terminator.h"
#include "src/tint/ir/if.h"
#include "src/tint/ir/load.h"
#include "src/tint/ir/loop.h"
#include "src/tint/ir/root_terminator.h"
#include "src/tint/ir/store.h"
#include "src/tint/ir/switch.h"
#include "src/tint/ir/user_call.h"
#include "src/tint/ir/var.h"
#include "src/tint/switch.h"
#include "src/tint/type/type.h"
#include "src/tint/utils/scoped_assignment.h"

namespace tint::ir {
namespace {

class ScopedIndent {
  public:
    explicit ScopedIndent(uint32_t& indent) : indent_(indent) { indent_ += 2; }

    ~ScopedIndent() { indent_ -= 2; }

  private:
    uint32_t& indent_;
};

}  // namespace

Disassembler::Disassembler(const Module& mod) : mod_(mod) {}

Disassembler::~Disassembler() = default;

utils::StringStream& Disassembler::Indent() {
    for (uint32_t i = 0; i < indent_size_; i++) {
        out_ << " ";
    }
    return out_;
}

void Disassembler::EmitBlockInstructions(const Block* b) {
    for (const auto* inst : b->Instructions()) {
        Indent();
        EmitInstruction(inst);
        out_ << std::endl;
    }
}

size_t Disassembler::IdOf(const Block* node) {
    TINT_ASSERT(IR, node);
    return block_ids_.GetOrCreate(node, [&] { return block_ids_.Count(); });
}

std::string_view Disassembler::IdOf(const Value* value) {
    TINT_ASSERT(IR, value);
    return value_ids_.GetOrCreate(value, [&] {
        if (auto sym = mod_.NameOf(value)) {
            return sym.Name();
        }
        return std::to_string(value_ids_.Count());
    });
}

std::string Disassembler::Disassemble() {
    if (mod_.root_block) {
        Indent() << "# Root block" << std::endl;
        Walk(mod_.root_block);
        Walk(mod_.root_block->Branch()->To());
    }

    for (auto* func : mod_.functions) {
        EmitFunction(func);
    }
    return out_.str();
}

void Disassembler::Walk(const Block* blk) {
    if (visited_.Contains(blk)) {
        return;
    }
    visited_.Add(blk);

    tint::Switch(
        blk,
        [&](const ir::FunctionTerminator* t) {
            TINT_ASSERT(IR, in_function_);
            Indent() << "%b" << IdOf(t) << " = func_terminator" << std::endl;
            in_function_ = false;
        },
        [&](const ir::RootTerminator* t) {
            TINT_ASSERT(IR, !in_function_);
            Indent() << "%b" << IdOf(t) << " = root_terminator" << std::endl << std::endl;
        },
        [&](const ir::Block* b) {
            // If this block is dead, nothing to do
            if (!b->HasBranchTarget()) {
                return;
            }

            Indent() << "%b" << IdOf(b) << " = block";
            if (!b->Params().IsEmpty()) {
                out_ << " (";
                for (auto* p : b->Params()) {
                    if (p != b->Params().Front()) {
                        out_ << ", ";
                    }
                    EmitValue(p);
                }
                out_ << ")";
            }

            out_ << " {" << std::endl;
            {
                ScopedIndent si(indent_size_);
                EmitBlockInstructions(b);
            }
            Indent() << "}" << std::endl;

            if (!b->Branch()->To()->Is<FunctionTerminator>()) {
                out_ << std::endl;
            }
        });
}

void Disassembler::EmitFunction(const Function* func) {
    in_function_ = true;

    Indent() << "%" << IdOf(func) << " = func(";
    for (auto* p : func->Params()) {
        if (p != func->Params().Front()) {
            out_ << ", ";
        }
        out_ << "%" << IdOf(p) << ":" << p->Type()->FriendlyName();
    }
    out_ << "):" << func->ReturnType()->FriendlyName();

    if (func->Stage() != Function::PipelineStage::kUndefined) {
        out_ << " [@" << func->Stage();

        if (func->WorkgroupSize()) {
            auto arr = func->WorkgroupSize().value();
            out_ << " @workgroup_size(" << arr[0] << ", " << arr[1] << ", " << arr[2] << ")";
        }

        if (!func->ReturnAttributes().IsEmpty()) {
            out_ << " ra:";

            for (auto attr : func->ReturnAttributes()) {
                out_ << " @" << attr;
                if (attr == Function::ReturnAttribute::kLocation) {
                    out_ << "(" << func->ReturnLocation().value() << ")";
                }
            }
        }

        out_ << "]";
    }
    out_ << " -> %b" << IdOf(func->StartTarget()) << " {" << std::endl;

    {
        ScopedIndent si(indent_size_);
        Walk(func->StartTarget());
        Walk(func->EndTarget());
    }
    Indent() << "}" << std::endl;
}

void Disassembler::EmitValueWithType(const Value* val) {
    EmitValue(val);
    if (auto* i = val->As<ir::Instruction>(); i->Type() != nullptr) {
        out_ << ":" << i->Type()->FriendlyName();
    }
}

void Disassembler::EmitValue(const Value* val) {
    tint::Switch(
        val,
        [&](const ir::Constant* constant) {
            std::function<void(const constant::Value*)> emit = [&](const constant::Value* c) {
                tint::Switch(
                    c,
                    [&](const constant::Scalar<AFloat>* scalar) {
                        out_ << scalar->ValueAs<AFloat>().value;
                    },
                    [&](const constant::Scalar<AInt>* scalar) {
                        out_ << scalar->ValueAs<AInt>().value;
                    },
                    [&](const constant::Scalar<i32>* scalar) {
                        out_ << scalar->ValueAs<i32>().value << "i";
                    },
                    [&](const constant::Scalar<u32>* scalar) {
                        out_ << scalar->ValueAs<u32>().value << "u";
                    },
                    [&](const constant::Scalar<f32>* scalar) {
                        out_ << scalar->ValueAs<f32>().value << "f";
                    },
                    [&](const constant::Scalar<f16>* scalar) {
                        out_ << scalar->ValueAs<f16>().value << "h";
                    },
                    [&](const constant::Scalar<bool>* scalar) {
                        out_ << (scalar->ValueAs<bool>() ? "true" : "false");
                    },
                    [&](const constant::Splat* splat) {
                        out_ << splat->Type()->FriendlyName() << " ";
                        emit(splat->Index(0));
                    },
                    [&](const constant::Composite* composite) {
                        out_ << composite->Type()->FriendlyName() << " ";
                        for (const auto* elem : composite->elements) {
                            if (elem != composite->elements[0]) {
                                out_ << ", ";
                            }
                            emit(elem);
                        }
                    });
            };
            emit(constant->Value());
        },
        [&](const ir::Instruction* i) { out_ << "%" << IdOf(i); },
        [&](const ir::BlockParam* p) {
            out_ << "%" << IdOf(p) << ":" << p->Type()->FriendlyName();
        },
        [&](const ir::FunctionParam* p) { out_ << "%" << IdOf(p); },
        [&](Default) { out_ << "Unknown value: " << val->TypeInfo().name; });
}

void Disassembler::EmitInstruction(const Instruction* inst) {
    tint::Switch(
        inst,                                         //
        [&](const ir::Switch* s) { EmitSwitch(s); },  //
        [&](const ir::If* i) { EmitIf(i); },          //
        [&](const ir::Loop* l) { EmitLoop(l); },      //
        [&](const ir::Binary* b) { EmitBinary(b); },  //
        [&](const ir::Unary* u) { EmitUnary(u); },
        [&](const ir::Bitcast* b) {
            EmitValueWithType(b);
            out_ << " = bitcast ";
            EmitArgs(b);
        },
        [&](const ir::Discard*) { out_ << "discard"; },
        [&](const ir::Builtin* b) {
            EmitValueWithType(b);
            out_ << " = " << builtin::str(b->Func()) << " ";
            EmitArgs(b);
        },
        [&](const ir::Construct* c) {
            EmitValueWithType(c);
            out_ << " = construct ";
            EmitArgs(c);
        },
        [&](const ir::Convert* c) {
            EmitValueWithType(c);
            out_ << " = convert " << c->FromType()->FriendlyName() << ", ";
            EmitArgs(c);
        },
        [&](const ir::Load* l) {
            EmitValueWithType(l);
            out_ << " = load ";
            EmitValue(l->From());
        },
        [&](const ir::Store* s) {
            out_ << "store ";
            EmitValue(s->To());
            out_ << ", ";
            EmitValue(s->From());
        },
        [&](const ir::UserCall* uc) {
            EmitValueWithType(uc);
            out_ << " = call %" << IdOf(uc->Func());
            if (!uc->Args().IsEmpty()) {
                out_ << ", ";
            }
            EmitArgs(uc);
        },
        [&](const ir::Var* v) {
            EmitValueWithType(v);
            out_ << " = var";
            if (v->Initializer()) {
                out_ << ", ";
                EmitValue(v->Initializer());
            }
        },
        [&](const ir::Branch* b) { EmitBranch(b); },
        [&](Default) { out_ << "Unknown instruction: " << inst->TypeInfo().name; });
}

void Disassembler::EmitIf(const If* i) {
    out_ << "if ";
    EmitValue(i->Condition());

    bool has_true = i->True()->HasBranchTarget();
    bool has_false = i->False()->HasBranchTarget();

    out_ << " [";
    if (has_true) {
        out_ << "t: %b" << IdOf(i->True());
    }
    if (has_false) {
        if (has_true) {
            out_ << ", ";
        }
        out_ << "f: %b" << IdOf(i->False());
    }
    if (i->Merge()->HasBranchTarget()) {
        out_ << ", m: %b" << IdOf(i->Merge());
    }
    out_ << "]" << std::endl;

    if (has_true) {
        ScopedIndent si(indent_size_);
        Indent() << "# True block" << std::endl;
        Walk(i->True());
    }
    if (has_false) {
        ScopedIndent si(indent_size_);
        Indent() << "# False block" << std::endl;
        Walk(i->False());
    }
    if (i->Merge()->HasBranchTarget()) {
        Indent() << "# Merge block" << std::endl;
        Walk(i->Merge());
    }
}

void Disassembler::EmitLoop(const Loop* l) {
    out_ << "loop [s: %b" << IdOf(l->Start());

    if (l->Continuing()->HasBranchTarget()) {
        out_ << ", c: %b" << IdOf(l->Continuing());
    }
    if (l->Merge()->HasBranchTarget()) {
        out_ << ", m: %b" << IdOf(l->Merge());
    }
    out_ << "]" << std::endl;

    {
        ScopedIndent si(indent_size_);
        Walk(l->Start());
    }

    if (l->Continuing()->HasBranchTarget()) {
        ScopedIndent si(indent_size_);
        Indent() << "# Continuing block" << std::endl;
        Walk(l->Continuing());
    }
    if (l->Merge()->HasBranchTarget()) {
        Indent() << "# Merge block" << std::endl;
        Walk(l->Merge());
    }
}

void Disassembler::EmitSwitch(const Switch* s) {
    out_ << "switch ";
    EmitValue(s->Condition());
    out_ << " [";
    for (const auto& c : s->Cases()) {
        if (&c != &s->Cases().Front()) {
            out_ << ", ";
        }
        out_ << "c: (";
        for (const auto& selector : c.selectors) {
            if (&selector != &c.selectors.Front()) {
                out_ << " ";
            }

            if (selector.IsDefault()) {
                out_ << "default";
            } else {
                EmitValue(selector.val);
            }
        }
        out_ << ", %b" << IdOf(c.Start()) << ")";
    }
    if (s->Merge()->HasBranchTarget()) {
        out_ << ", m: %b" << IdOf(s->Merge());
    }
    out_ << "]" << std::endl;

    for (auto& c : s->Cases()) {
        ScopedIndent si(indent_size_);
        Indent() << "# Case block" << std::endl;
        Walk(c.Start());
    }
    if (s->Merge()->HasBranchTarget()) {
        Indent() << "# Merge block" << std::endl;
        Walk(s->Merge());
    }
}

void Disassembler::EmitBranch(const Branch* b) {
    std::string suffix = "";
    out_ << "br %b" << IdOf(b->To());
    if (b->To()->Is<FunctionTerminator>()) {
        suffix = "return";
    } else if (b->To()->Is<RootTerminator>()) {
        suffix = "root_end";
    }

    if (!b->Args().IsEmpty()) {
        out_ << " ";
        for (auto* v : b->Args()) {
            if (v != b->Args().Front()) {
                out_ << ", ";
            }
            EmitValue(v);
        }
    }
    if (!suffix.empty()) {
        out_ << "  # " << suffix;
    }
}

void Disassembler::EmitArgs(const Call* call) {
    bool first = true;
    for (const auto* arg : call->Args()) {
        if (!first) {
            out_ << ", ";
        }
        first = false;
        EmitValue(arg);
    }
}

void Disassembler::EmitBinary(const Binary* b) {
    EmitValueWithType(b);
    out_ << " = ";
    switch (b->Kind()) {
        case Binary::Kind::kAdd:
            out_ << "add";
            break;
        case Binary::Kind::kSubtract:
            out_ << "sub";
            break;
        case Binary::Kind::kMultiply:
            out_ << "mul";
            break;
        case Binary::Kind::kDivide:
            out_ << "div";
            break;
        case Binary::Kind::kModulo:
            out_ << "mod";
            break;
        case Binary::Kind::kAnd:
            out_ << "and";
            break;
        case Binary::Kind::kOr:
            out_ << "or";
            break;
        case Binary::Kind::kXor:
            out_ << "xor";
            break;
        case Binary::Kind::kEqual:
            out_ << "eq";
            break;
        case Binary::Kind::kNotEqual:
            out_ << "neq";
            break;
        case Binary::Kind::kLessThan:
            out_ << "lt";
            break;
        case Binary::Kind::kGreaterThan:
            out_ << "gt";
            break;
        case Binary::Kind::kLessThanEqual:
            out_ << "lte";
            break;
        case Binary::Kind::kGreaterThanEqual:
            out_ << "gte";
            break;
        case Binary::Kind::kShiftLeft:
            out_ << "shiftl";
            break;
        case Binary::Kind::kShiftRight:
            out_ << "shiftr";
            break;
    }
    out_ << " ";
    EmitValue(b->LHS());
    out_ << ", ";
    EmitValue(b->RHS());
}

void Disassembler::EmitUnary(const Unary* u) {
    EmitValueWithType(u);
    out_ << " = ";
    switch (u->Kind()) {
        case Unary::Kind::kComplement:
            out_ << "complement";
            break;
        case Unary::Kind::kNegation:
            out_ << "negation";
            break;
    }
    out_ << " ";
    EmitValue(u->Val());
}

}  // namespace tint::ir
