source: icGREP/icgrep-devel/icgrep/re/re_range.cpp @ 6173

Last change on this file since 6173 was 6173, checked in by nmedfort, 7 months ago

Added RE_Inspector.

Migrated RE passes to RE_Transformer.

Incorporated Memoizer functionality into RE_Transformer/Inspector.

Removed Memoizer.

Bug fix for unicode_set.

File size: 1.5 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <re/re_range.h>
8#include <re/re_cc.h>
9#include <re/re_name.h>
10#include <llvm/Support/Casting.h>
11#include <llvm/Support/raw_ostream.h>
12#include <llvm/Support/ErrorHandling.h>
13
14using namespace llvm;
15
16namespace re {
17
18RE * makeRange(RE * lo, RE * hi) {
19    if (isa<CC>(lo) && isa<CC>(hi)) {
20        CC * const cc_lo = cast<CC>(lo);
21        CC * const cc_hi = cast<CC>(hi);
22        if (LLVM_LIKELY((cc_lo->count() == 1) && (cc_hi->count() == 1))) {
23            const auto lo_val = cc_lo->at(0);
24            const auto hi_val = cc_hi->at(0);
25            if (LLVM_LIKELY(lo_val <= hi_val)) {
26                return makeCC(lo_val, hi_val, dyn_cast<CC>(hi)->getAlphabet());
27            }
28        }
29        std::string tmp;
30        raw_string_ostream out(tmp);
31        cc_lo->print(out);
32        out << " to ";
33        cc_hi->print(out);
34        out << " are invalid range operands";
35        report_fatal_error(out.str());
36    }
37    else if (isa<Name>(lo) && (cast<Name>(lo)->getDefinition() != nullptr)) {
38        return makeRange(cast<Name>(lo)->getDefinition(), hi);
39    }
40    else if (isa<Name>(hi) && (cast<Name>(hi)->getDefinition() != nullptr)) {
41        return makeRange(lo, cast<Name>(hi)->getDefinition());
42    }
43    else if (lo == hi) { // TODO: general check for equality, not just instance equality
44        return lo;
45    }
46    return new Range(lo, hi);
47}
48   
49}
Note: See TracBrowser for help on using the repository browser.