navid/haze / 29e7ef5e690b244d506a169b7a4f892c737395b40bafffaedbaf7b650bf94bb8 / src/cfg.cpp

cfg.cpp

← src · 7510 bytes · raw · download

#include "haze.h"

#include <string>

namespace haze {

namespace {

TargetInfo g_target;
bool g_target_set = false;

struct CfgValue {
    enum class Kind { Bool, Int, String };
    Kind kind = Kind::Bool;
    bool boolean = false;
    long long integer = 0;
    std::string string;
};

CfgValue cfg_bool(bool v) {
    CfgValue c;
    c.kind = CfgValue::Kind::Bool;
    c.boolean = v;
    return c;
}

CfgValue cfg_int(long long v) {
    CfgValue c;
    c.kind = CfgValue::Kind::Int;
    c.integer = v;
    return c;
}

CfgValue cfg_string(std::string v) {
    CfgValue c;
    c.kind = CfgValue::Kind::String;
    c.string = std::move(v);
    return c;
}

CfgValue eval_cfg_expr(const Expr& e, const TargetInfo& target) {
    switch (e.kind) {
    case Expr::Kind::Bool:
        return cfg_bool(e.bval);
    case Expr::Kind::Int:
        return cfg_int(e.ival);
    case Expr::Kind::String:
        return cfg_string(e.text);
    case Expr::Kind::Var:
        if (e.text == "os")
            return cfg_string(target.os);
        if (e.text == "arch")
            return cfg_string(target.arch);
        if (e.text == "family")
            return cfg_string(target.family);
        if (e.text == "endian")
            return cfg_string(target.endian);
        if (e.text == "ptr_width")
            return cfg_int(target.ptr_width);
        if (e.text == "true")
            return cfg_bool(true);
        if (e.text == "false")
            return cfg_bool(false);
        return cfg_string(e.text);
    case Expr::Kind::Unary: {
        CfgValue value = eval_cfg_expr(*e.lhs, target);
        if (e.op == "!") {
            if (value.kind != CfgValue::Kind::Bool)
                throw Error(e.pos, "cfg '!' requires a boolean");
            return cfg_bool(!value.boolean);
        }
        if (e.op == "-") {
            if (value.kind != CfgValue::Kind::Int)
                throw Error(e.pos, "cfg unary '-' requires an integer");
            return cfg_int(-value.integer);
        }
        throw Error(e.pos, "unsupported cfg unary operator '" + e.op + "'");
    }
    case Expr::Kind::Binary: {
        CfgValue a = eval_cfg_expr(*e.lhs, target);
        CfgValue b = eval_cfg_expr(*e.rhs, target);
        if (e.op == "&&" || e.op == "||") {
            if (a.kind != CfgValue::Kind::Bool || b.kind != CfgValue::Kind::Bool)
                throw Error(e.pos, "cfg logical operands must be boolean");
            return cfg_bool(e.op == "&&" ? (a.boolean && b.boolean)
                                          : (a.boolean || b.boolean));
        }
        if (e.op == "==" || e.op == "!=") {
            bool equal = false;
            if (a.kind == CfgValue::Kind::String && b.kind == CfgValue::Kind::String)
                equal = a.string == b.string;
            else if (a.kind == CfgValue::Kind::Int && b.kind == CfgValue::Kind::Int)
                equal = a.integer == b.integer;
            else if (a.kind == CfgValue::Kind::Bool && b.kind == CfgValue::Kind::Bool)
                equal = a.boolean == b.boolean;
            else
                throw Error(e.pos, "cfg comparison operands have different kinds");
            return cfg_bool(e.op == "==" ? equal : !equal);
        }
        if (a.kind == CfgValue::Kind::Int && b.kind == CfgValue::Kind::Int) {
            if (e.op == "<")
                return cfg_bool(a.integer < b.integer);
            if (e.op == "<=")
                return cfg_bool(a.integer <= b.integer);
            if (e.op == ">")
                return cfg_bool(a.integer > b.integer);
            if (e.op == ">=")
                return cfg_bool(a.integer >= b.integer);
        }
        throw Error(e.pos, "unsupported cfg condition operator '" + e.op + "'");
    }
    case Expr::Kind::Call: {
        if (e.lhs->kind != Expr::Kind::Var)
            throw Error(e.pos, "cfg call must be all(...), any(...), or not(...)");
        const std::string& name = e.lhs->text;
        if (name == "not") {
            if (e.args.size() != 1)
                throw Error(e.pos, "cfg not(...) expects one argument");
            CfgValue arg = eval_cfg_expr(*e.args[0], target);
            if (arg.kind != CfgValue::Kind::Bool)
                throw Error(e.pos, "cfg not(...) requires a boolean");
            return cfg_bool(!arg.boolean);
        }
        if (name == "all" || name == "any") {
            if (e.args.empty())
                throw Error(e.pos, "cfg " + name + "(...) requires arguments");
            bool result = name == "all";
            for (auto& arg : e.args) {
                CfgValue value = eval_cfg_expr(*arg, target);
                if (value.kind != CfgValue::Kind::Bool)
                    throw Error(arg->pos, "cfg " + name + "(...) requires booleans");
                if (name == "all")
                    result = result && value.boolean;
                else
                    result = result || value.boolean;
            }
            return cfg_bool(result);
        }
        throw Error(e.pos, "unknown cfg predicate '" + name + "'");
    }
    default:
        throw Error(e.pos, "unsupported cfg expression");
    }
}

} // namespace

TargetInfo host_target() {
    TargetInfo target;
#if defined(_WIN32)
    target.os = "windows";
    target.family = "windows";
#elif defined(__APPLE__)
    target.os = "macos";
    target.family = "unix";
#elif defined(__FreeBSD__)
    target.os = "freebsd";
    target.family = "unix";
#elif defined(__DragonFly__)
    target.os = "dragonflybsd";
    target.family = "unix";
#elif defined(__OpenBSD__)
    target.os = "openbsd";
    target.family = "unix";
#elif defined(__NetBSD__)
    target.os = "netbsd";
    target.family = "unix";
#elif defined(__linux__)
    target.os = "linux";
    target.family = "unix";
#else
    target.os = "unknown";
    target.family = "unix";
#endif

#if defined(__x86_64__) || defined(_M_X64)
    target.arch = "x86_64";
#elif defined(__aarch64__) || defined(_M_ARM64)
    target.arch = "aarch64";
#elif defined(__riscv) && __riscv_xlen == 64
    target.arch = "riscv64";
#else
    target.arch = "unknown";
#endif

#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
    __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    target.endian = "big";
#else
    target.endian = "little";
#endif

    target.ptr_width = static_cast<int>(sizeof(void*) * 8);
    return target;
}

TargetInfo target_for_qbe(const std::string& name) {
    TargetInfo target = host_target();
    if (name.rfind("amd64", 0) == 0)
        target.arch = "x86_64";
    else if (name.rfind("arm64", 0) == 0)
        target.arch = "aarch64";
    else if (name.rfind("rv64", 0) == 0)
        target.arch = "riscv64";
    if (name.find("apple") != std::string::npos) {
        target.os = "macos";
        target.family = "unix";
    } else if (name.find("win") != std::string::npos) {
        target.os = "windows";
        target.family = "windows";
    }
    target.endian = "little";
    target.ptr_width = 64;
    return target;
}

void set_compile_target(const TargetInfo& target) {
    g_target = target;
    g_target_set = true;
}

const TargetInfo& compile_target() {
    if (!g_target_set) {
        g_target = host_target();
        g_target_set = true;
    }
    return g_target;
}

bool eval_cfg_condition(const Expr& expr) {
    CfgValue value = eval_cfg_expr(expr, compile_target());
    if (value.kind != CfgValue::Kind::Bool)
        throw Error(expr.pos, "cfg condition must produce a boolean");
    return value.boolean;
}

} // namespace haze