Skip to content

Commit 5553a29

Browse files
Introducing llvm-kompile compute-ordinal and compute-loc tools (#1056)
These tools are meant to replace the old `llvm-kompile-compute-ordinal` and `llvm-kompile-compute-loc` bash scripts with an appropriate C++ infrastructure. The new tools can work in two ways: - Parsing the definition and looking for/using the K file location to compute the ordinal and location info. - Using the `definition.kore` lines to compute the ordinal and returning the line in the definition when computing the location. The interface of the new tools is defined as follows: ```bash llvm-kompile-compute-loc [options] <kompiled-dir> <ordinal> [is-k-line] ``` and ```bash llvm-kompile-compute-ordinal [options] <kompiled-dir> <line> [is-k-line] ``` in both cases, `is-k-line` is false by default to have the same behavior as the old scripts. --------- Co-authored-by: rv-jenkins <[email protected]>
1 parent 9ea795c commit 5553a29

File tree

14 files changed

+2378
-12
lines changed

14 files changed

+2378
-12
lines changed

bin/llvm-kompile-compute-loc

Lines changed: 0 additions & 7 deletions
This file was deleted.

bin/llvm-kompile-compute-ordinal

Lines changed: 0 additions & 5 deletions
This file was deleted.

include/kllvm/ast/util.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef UTIL_H
2+
#define UTIL_H
3+
4+
#include <kllvm/ast/AST.h>
5+
6+
namespace kllvm {
7+
8+
/*
9+
* This file contains utility functions that are used in multiple tools and
10+
* are not specific to any one tool. These functions are used to manipulate
11+
* the AST and other data structures.
12+
*/
13+
14+
[[nodiscard]] std::optional<int64_t>
15+
get_start_line_location(kore_axiom_declaration const &axiom);
16+
17+
[[nodiscard]] std::string trim(std::string s);
18+
} // namespace kllvm
19+
20+
#endif // UTIL_H

lib/ast/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_library(AST
22
AST.cpp
33
attribute_set.cpp
44
pattern_matching.cpp
5+
util.cpp
56
definition.cpp
67
)
78

lib/ast/util.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "kllvm/ast/util.h"
2+
3+
#include <algorithm>
4+
#include <string>
5+
6+
using namespace kllvm;
7+
8+
[[nodiscard]] std::optional<int64_t>
9+
kllvm::get_start_line_location(kore_axiom_declaration const &axiom) {
10+
auto location_att = axiom.attributes().get(attribute_set::key::Location);
11+
assert(location_att->get_arguments().size() == 1);
12+
13+
auto str_pattern = std::dynamic_pointer_cast<kore_string_pattern>(
14+
location_att->get_arguments()[0]);
15+
std::string location = str_pattern->get_contents();
16+
17+
size_t l_paren = location.find_first_of('(');
18+
size_t first_comma = location.find_first_of(',');
19+
size_t length = first_comma - l_paren - 1;
20+
return std::stoi(location.substr(l_paren + 1, length));
21+
}
22+
23+
// trim the string from the start
24+
[[nodiscard]] std::string kllvm::trim(std::string s) {
25+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {
26+
return !std::isspace(c);
27+
}));
28+
return s;
29+
}

test/defn/compute-ordinal-loc/definition.kore

Lines changed: 2123 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module INC
2+
imports INT
3+
imports BOOL
4+
5+
rule I => I +Int 1
6+
requires I >Int 0 andBool I <Int 100
7+
rule I => I -Int 1
8+
requires I <Int 0 andBool I >Int -100
9+
10+
endmodule
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

test/lit.cfg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def one_line(s):
104104
('%arity', 'kore-arity'),
105105
('%apply-rule', 'k-rule-apply'),
106106
('%find-rule', 'k-rule-find'),
107+
('%compute-loc', 'llvm-kompile-compute-loc %S'),
108+
('%compute-ordinal', 'llvm-kompile-compute-ordinal %S'),
107109

108110
('%kllvm-clang', 'clang -I %include-path -I Inputs Inputs/api.c'),
109111

tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ endmacro(kllvm_add_tool)
2323

2424
add_subdirectory(llvm-backend-version)
2525
add_subdirectory(llvm-kompile-codegen)
26+
add_subdirectory(llvm-kompile-compute-loc)
27+
add_subdirectory(llvm-kompile-compute-ordinal)
2628
add_subdirectory(llvm-kompile-gc-stats)
2729
add_subdirectory(kprint)
2830
add_subdirectory(kore-arity)

0 commit comments

Comments
 (0)