source: icGREP/icgrep-devel/icgrep/pablo/symbol_generator.cpp @ 5828

Last change on this file since 5828 was 5440, checked in by nmedfort, 2 years ago

Large refactoring step. Removed IR generation code from Kernel (formally KernelBuilder?) and moved it into the new KernelBuilder? class.

File size: 2.0 KB
Line 
1/*
2 *  Copyright (c) 2014 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 "symbol_generator.h"
8#include <pablo/pe_string.h>
9#include <pablo/pe_integer.h>
10#include <IR_Gen/idisa_builder.h>
11
12namespace pablo {
13
14String * SymbolGenerator::makeString(const llvm::StringRef prefix) noexcept {
15    auto f = mPrefixMap.find(prefix);
16    if (f == mPrefixMap.end()) {   
17        char * const data = mAllocator.allocate<char>(prefix.size() + 1);
18        std::memcpy(data, prefix.data(), prefix.size());
19        data[prefix.size()] = '\0';
20        llvm::StringRef name(data, prefix.size());
21        mPrefixMap.insert(std::make_pair(name, 1));
22        return new (mAllocator) String(llvm::IntegerType::getInt8PtrTy(mContext), name, mAllocator);
23    } else { // this string already exists; make a new string using the given prefix
24
25        // TODO: check FormatInt from "https://github.com/fmtlib/fmt/blob/master/fmt/format.h" for faster integer conversion
26
27        size_t count = f->second++;
28        size_t length = prefix.size() + 2;
29        size_t digits = 10;
30        while (LLVM_UNLIKELY(digits <= count)) {
31            digits *= 10;
32            length += 1;
33        }
34        char name[length];
35        std::memcpy(name, prefix.data(), prefix.size());
36        char * p = name + length - 1;
37        while (count) {
38            *p-- = (count % 10) + '0';
39            count /= 10;
40        }
41        *p = '_';
42        return makeString(llvm::StringRef(name, length));
43    }
44}
45
46Integer * SymbolGenerator::getInteger(const IntTy value) noexcept {
47    auto f = mIntegerMap.find(value);
48    Integer * result;
49    if (f == mIntegerMap.end()) {       
50        result = new (mAllocator) Integer(value, llvm::IntegerType::getInt64Ty(mContext), mAllocator);
51        assert (result->value() == value);
52        mIntegerMap.emplace(value, result);
53    } else {
54        result = f->second;
55    }
56    return result;
57}
58
59}
Note: See TracBrowser for help on using the repository browser.