Skip to content

Commit 0297423

Browse files
committed
[Core] sync slicer to 93caac6
1 parent 74b5eed commit 0297423

38 files changed

+6653
-6578
lines changed

Core/third_party/slicer/bytecode_encoder.cc

Lines changed: 560 additions & 581 deletions
Large diffs are not rendered by default.

Core/third_party/slicer/code_ir.cc

Lines changed: 565 additions & 565 deletions
Large diffs are not rendered by default.

Core/third_party/slicer/common.cc

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,52 @@
1818

1919
#include <stdio.h>
2020
#include <stdlib.h>
21-
#include <cstdarg>
21+
22+
#include <iostream>
23+
#include <sstream>
2224
#include <set>
2325
#include <utility>
2426

2527
namespace slicer {
2628

29+
static void log_(const std::string& msg) {
30+
printf("%s", msg.c_str());
31+
fflush(stdout);
32+
}
33+
34+
static logger_type log = log_;
35+
36+
void set_logger(logger_type new_logger) {
37+
log = new_logger;
38+
}
39+
2740
// Helper for the default SLICER_CHECK() policy
2841
void _checkFailed(const char* expr, int line, const char* file) {
29-
printf("\nSLICER_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
42+
std::stringstream ss;
43+
ss << std::endl << "SLICER_CHECK failed [";
44+
ss << expr << "] at " << file << ":" << line;
45+
ss << std::endl << std::endl;
46+
log(ss.str());
47+
abort();
48+
}
49+
50+
void _checkFailedOp(const void* lhs, const void* rhs, const char* op, const char* suffix, int line,
51+
const char* file) {
52+
std::stringstream ss;
53+
ss << std::endl << "SLICER_CHECK_" << suffix << " failed [";
54+
ss << lhs << " " << op << " " << rhs;
55+
ss << "] at " << file << ":" << line;
56+
log(ss.str());
57+
abort();
58+
}
59+
60+
void _checkFailedOp(uint32_t lhs, uint32_t rhs, const char* op, const char* suffix, int line,
61+
const char* file) {
62+
std::stringstream ss;
63+
ss << std::endl << "SLICER_CHECK_" << suffix << " failed [";
64+
ss << lhs << " " << op << " " << rhs;
65+
ss << "] at " << file << ":" << line;
66+
log(ss.str());
3067
abort();
3168
}
3269

@@ -40,18 +77,19 @@ thread_local std::set<std::pair<int, const char*>> weak_failures;
4077
void _weakCheckFailed(const char* expr, int line, const char* file) {
4178
auto failure_id = std::make_pair(line, file);
4279
if (weak_failures.find(failure_id) == weak_failures.end()) {
43-
printf("\nSLICER_WEAK_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
80+
std::stringstream ss;
81+
ss << std::endl << "SLICER_WEAK_CHECK failed [";
82+
ss << expr << "] at " << file << ":";
83+
ss << line << std::endl << std::endl;
84+
log(ss.str());
4485
weak_failures.insert(failure_id);
4586
}
4687
}
4788

4889
// Prints a formatted message and aborts
49-
void _fatal(const char* format, ...) {
50-
va_list args;
51-
va_start(args, format);
52-
vprintf(format, args);
53-
va_end(args);
90+
void _fatal(const std::string& msg) {
91+
log("SLICER_FATAL: " + msg);
5492
abort();
5593
}
5694

57-
} // namespace export
95+
} // namespace slicer

Core/third_party/slicer/control_flow_graph.cc

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -19,120 +19,120 @@
1919
namespace lir {
2020

2121
std::vector<BasicBlock> BasicBlocksVisitor::Finish() {
22-
// the .dex format specification has the following constraint:
23-
//
24-
// B17 The last reachable instruction of a method must either be a
25-
// backwards goto or branch, a return, or a throw instruction. It must not
26-
// be possible to leave the insns array at the bottom. 4.8.2.20
27-
//
28-
// NOTE: this is a very aggressive check though since in the LIR we also
29-
// have labels, annotations, directives, etc. For example it's possible to have
30-
// debug annotations (.line, .endlocal, ...) after the last bytecode.
31-
//
32-
SLICER_WEAK_CHECK(state_ == State::Outside);
33-
SLICER_CHECK(state_ != State::BlockBody);
34-
current_block_.region = {};
35-
state_ = State::Outside;
36-
return std::move(basic_blocks_);
22+
// the .dex format specification has the following constraint:
23+
//
24+
// B17 The last reachable instruction of a method must either be a
25+
// backwards goto or branch, a return, or a throw instruction. It must not
26+
// be possible to leave the insns array at the bottom. 4.8.2.20
27+
//
28+
// NOTE: this is a very aggressive check though since in the LIR we also
29+
// have labels, annotations, directives, etc. For example it's possible to have
30+
// debug annotations (.line, .endlocal, ...) after the last bytecode.
31+
//
32+
SLICER_WEAK_CHECK(state_ == State::Outside);
33+
SLICER_CHECK(state_ != State::BlockBody);
34+
current_block_.region = {};
35+
state_ = State::Outside;
36+
return std::move(basic_blocks_);
3737
}
3838

39-
bool BasicBlocksVisitor::Visit(Bytecode *bytecode) {
40-
switch (state_) {
41-
case State::Outside:
42-
StartBlock(bytecode);
43-
state_ = State::BlockBody;
44-
break;
45-
case State::BlockHeader:
46-
state_ = State::BlockBody;
47-
break;
48-
case State::BlockBody:
49-
// inside basic block body, nothing to do.
50-
break;
51-
}
39+
bool BasicBlocksVisitor::Visit(Bytecode* bytecode) {
40+
switch (state_) {
41+
case State::Outside:
42+
StartBlock(bytecode);
43+
state_ = State::BlockBody;
44+
break;
45+
case State::BlockHeader:
46+
state_ = State::BlockBody;
47+
break;
48+
case State::BlockBody:
49+
// inside basic block body, nothing to do.
50+
break;
51+
}
5252

53-
// terminate the current block?
54-
bool terminate_block = false;
55-
const auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
56-
if (model_exceptions_) {
57-
constexpr auto exit_instr_flags =
58-
dex::kBranch |
59-
dex::kSwitch |
60-
dex::kThrow |
61-
dex::kReturn;
62-
terminate_block = (flags & exit_instr_flags) != 0;
63-
} else {
64-
constexpr auto exit_instr_flags =
65-
dex::kBranch |
66-
dex::kSwitch |
67-
dex::kReturn;
68-
terminate_block = bytecode->opcode == dex::OP_THROW || (flags & exit_instr_flags) != 0;
69-
}
70-
if (terminate_block) {
71-
EndBlock(bytecode);
72-
}
53+
// terminate the current block?
54+
bool terminate_block = false;
55+
const auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
56+
if (model_exceptions_) {
57+
constexpr auto exit_instr_flags =
58+
dex::kBranch |
59+
dex::kSwitch |
60+
dex::kThrow |
61+
dex::kReturn;
62+
terminate_block = (flags & exit_instr_flags) != 0;
63+
} else {
64+
constexpr auto exit_instr_flags =
65+
dex::kBranch |
66+
dex::kSwitch |
67+
dex::kReturn;
68+
terminate_block = bytecode->opcode == dex::OP_THROW || (flags & exit_instr_flags) != 0;
69+
}
70+
if (terminate_block) {
71+
EndBlock(bytecode);
72+
}
7373

74-
return true;
74+
return true;
7575
}
7676

77-
bool BasicBlocksVisitor::Visit(Label *label) {
78-
switch (state_) {
79-
case State::Outside:
80-
StartBlock(label);
81-
break;
82-
case State::BlockBody:
83-
EndBlock(label->prev);
84-
StartBlock(label);
85-
break;
86-
case State::BlockHeader:
87-
break;
88-
}
89-
return true;
77+
bool BasicBlocksVisitor::Visit(Label* label) {
78+
switch (state_) {
79+
case State::Outside:
80+
StartBlock(label);
81+
break;
82+
case State::BlockBody:
83+
EndBlock(label->prev);
84+
StartBlock(label);
85+
break;
86+
case State::BlockHeader:
87+
break;
88+
}
89+
return true;
9090
}
9191

92-
bool BasicBlocksVisitor::HandleAnnotation(Instruction *instr) {
93-
if (state_ == State::Outside) {
94-
StartBlock(instr);
95-
}
96-
return true;
92+
bool BasicBlocksVisitor::HandleAnnotation(Instruction* instr) {
93+
if (state_ == State::Outside) {
94+
StartBlock(instr);
95+
}
96+
return true;
9797
}
9898

99-
bool BasicBlocksVisitor::SkipInstruction(Instruction *instr) {
100-
if (state_ != State::Outside) {
101-
EndBlock(instr->prev);
102-
}
103-
return true;
99+
bool BasicBlocksVisitor::SkipInstruction(Instruction* instr) {
100+
if (state_ != State::Outside) {
101+
EndBlock(instr->prev);
102+
}
103+
return true;
104104
}
105105

106-
void BasicBlocksVisitor::StartBlock(Instruction *instr) {
107-
assert(instr != nullptr);
108-
assert(state_ == State::Outside);
109-
// mark the location of the "first" instruction,
110-
// "last" will be set when we end the basic block.
111-
current_block_.region.first = instr;
112-
current_block_.region.last = nullptr;
113-
state_ = State::BlockHeader;
106+
void BasicBlocksVisitor::StartBlock(Instruction* instr) {
107+
assert(instr != nullptr);
108+
assert(state_ == State::Outside);
109+
// mark the location of the "first" instruction,
110+
// "last" will be set when we end the basic block.
111+
current_block_.region.first = instr;
112+
current_block_.region.last = nullptr;
113+
state_ = State::BlockHeader;
114114
}
115115

116-
void BasicBlocksVisitor::EndBlock(Instruction *instr) {
117-
assert(instr != nullptr);
118-
if (state_ == State::BlockBody) {
119-
++current_block_.id;
120-
assert(current_block_.region.first != nullptr);
121-
current_block_.region.last = instr;
122-
basic_blocks_.push_back(current_block_);
123-
} else {
124-
assert(state_ == State::BlockHeader);
125-
}
126-
current_block_.region = {};
127-
state_ = State::Outside;
116+
void BasicBlocksVisitor::EndBlock(Instruction* instr) {
117+
assert(instr != nullptr);
118+
if (state_ == State::BlockBody) {
119+
++current_block_.id;
120+
assert(current_block_.region.first != nullptr);
121+
current_block_.region.last = instr;
122+
basic_blocks_.push_back(current_block_);
123+
} else {
124+
assert(state_ == State::BlockHeader);
125+
}
126+
current_block_.region = {};
127+
state_ = State::Outside;
128128
}
129129

130130
void ControlFlowGraph::CreateBasicBlocks(bool model_exceptions) {
131-
BasicBlocksVisitor visitor(model_exceptions);
132-
for (auto instr: code_ir->instructions) {
133-
instr->Accept(&visitor);
134-
}
135-
basic_blocks = visitor.Finish();
131+
BasicBlocksVisitor visitor(model_exceptions);
132+
for (auto instr : code_ir->instructions) {
133+
instr->Accept(&visitor);
134+
}
135+
basic_blocks = visitor.Finish();
136136
}
137137

138-
} // namespace lir
138+
} // namespace lir

0 commit comments

Comments
 (0)