LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Parsers.h
Go to the documentation of this file.
1//===-- Parsers.h -----------------------------------------------*- C++ -*-===//
2//
3// Command line parsers for LLZK transformation passes.
4//
5// Part of the LLZK Project, under the Apache License v2.0.
6// See LICENSE.txt for license information.
7// Copyright 2025 Veridise Inc.
8// SPDX-License-Identifier: Apache-2.0
9//
10//===----------------------------------------------------------------------===//
11
12#include <llvm/ADT/APInt.h>
13#include <llvm/ADT/StringExtras.h>
14#include <llvm/Support/CommandLine.h>
15
16// Custom command line parsers
17namespace llvm {
18namespace cl {
19
20// Parser for APInt
21template <> class parser<APInt> : public basic_parser<APInt> {
22public:
23 parser(Option &O) : basic_parser(O) {}
24
25 bool parse(Option &O, StringRef, StringRef Arg, APInt &Val) {
26 if (Arg.empty()) {
27 return O.error("empty integer literal");
28 }
29 if (!all_of(Arg, [](char c) { return isDigit(c); })) {
30 return O.error("arg must be in base 10 (digits).");
31 }
32 // Decimal-only: allocate a safe width then shrink.
33 assert(std::cmp_less_equal(Arg.size(), std::numeric_limits<unsigned>::max()));
34 unsigned bits = std::max(1u, 4u * (unsigned)Arg.size());
35 APInt tmp(bits, Arg, 10);
36 unsigned active = tmp.getActiveBits();
37 if (active == 0) {
38 active = 1;
39 }
40 Val = tmp.zextOrTrunc(active);
41 return false;
42 }
43
44 // Prints how the passed option differs from the default one specified in the pass
45 // For example, if V = 17 and Default = 11 then it should print
46 // [OptionName] 17 (default: 11)
48 const Option &O, const APInt &V, OptionValue<APInt> Default, size_t GlobalWidth
49 ) const {
50 std::string Cur = llvm::toString(V, 10, false);
51
52 std::string Def = "<unspecified>";
53 if (Default.hasValue()) {
54 const APInt &D = Default.getValue();
55 Def = llvm::toString(D, 10, false);
56 }
57
58 printOptionName(O, GlobalWidth);
59 llvm::outs() << Cur << " (default: " << Def << ")\n";
60 }
61};
62
63} // namespace cl
64} // namespace llvm
void printOptionDiff(const Option &O, const APInt &V, OptionValue< APInt > Default, size_t GlobalWidth) const
Definition Parsers.h:47
bool parse(Option &O, StringRef, StringRef Arg, APInt &Val)
Definition Parsers.h:25