Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
13ebe8e8
Commit
13ebe8e8
authored
Nov 29, 2016
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement StatusTuple class instead of using std::tuple
parent
f5dce2c7
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
248 additions
and
247 deletions
+248
-247
examples/cpp/CPUDistribution.cc
examples/cpp/CPUDistribution.cc
+6
-6
examples/cpp/HelloWorld.cc
examples/cpp/HelloWorld.cc
+6
-6
examples/cpp/RandomRead.cc
examples/cpp/RandomRead.cc
+6
-6
examples/cpp/RecordMySQLQuery.cc
examples/cpp/RecordMySQLQuery.cc
+6
-6
examples/cpp/TCPSendStack.cc
examples/cpp/TCPSendStack.cc
+6
-6
src/cc/BPF.cc
src/cc/BPF.cc
+58
-56
src/cc/BPFTable.cc
src/cc/BPFTable.cc
+15
-15
src/cc/bcc_exception.h
src/cc/bcc_exception.h
+24
-24
src/cc/frontends/b/codegen_llvm.cc
src/cc/frontends/b/codegen_llvm.cc
+45
-45
src/cc/frontends/b/loader.cc
src/cc/frontends/b/loader.cc
+5
-6
src/cc/frontends/b/node.h
src/cc/frontends/b/node.h
+5
-5
src/cc/frontends/b/printer.cc
src/cc/frontends/b/printer.cc
+29
-29
src/cc/frontends/b/type_check.cc
src/cc/frontends/b/type_check.cc
+37
-37
No files found.
examples/cpp/CPUDistribution.cc
View file @
13ebe8e8
...
...
@@ -61,15 +61,15 @@ int task_switch_event(struct pt_regs *ctx, struct task_struct *prev) {
int
main
(
int
argc
,
char
**
argv
)
{
ebpf
::
BPF
bpf
;
auto
init_res
=
bpf
.
init
(
BPF_PROGRAM
);
if
(
std
::
get
<
0
>
(
init_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
init_res
)
<<
std
::
endl
;
if
(
init_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
auto
attach_res
=
bpf
.
attach_kprobe
(
"finish_task_switch"
,
"task_switch_event"
);
if
(
std
::
get
<
0
>
(
attach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
attach_res
)
<<
std
::
endl
;
if
(
attach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
@@ -88,8 +88,8 @@ int main(int argc, char** argv) {
}
auto
detach_res
=
bpf
.
detach_kprobe
(
"finish_task_switch"
);
if
(
std
::
get
<
0
>
(
detach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
detach_res
)
<<
std
::
endl
;
if
(
detach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
detach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
examples/cpp/HelloWorld.cc
View file @
13ebe8e8
...
...
@@ -20,8 +20,8 @@ int on_sys_clone(void *ctx) {
int
main
()
{
ebpf
::
BPF
bpf
;
auto
init_res
=
bpf
.
init
(
BPF_PROGRAM
);
if
(
std
::
get
<
0
>
(
init_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
init_res
)
<<
std
::
endl
;
if
(
init_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
@@ -29,8 +29,8 @@ int main() {
std
::
string
line
;
auto
attach_res
=
bpf
.
attach_kprobe
(
"sys_clone"
,
"on_sys_clone"
);
if
(
std
::
get
<
0
>
(
attach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
attach_res
)
<<
std
::
endl
;
if
(
attach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
@@ -39,8 +39,8 @@ int main() {
std
::
cout
<<
line
<<
std
::
endl
;
// Detach the probe if we got at least one line.
auto
detach_res
=
bpf
.
detach_kprobe
(
"sys_clone"
);
if
(
std
::
get
<
0
>
(
detach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
detach_res
)
<<
std
::
endl
;
if
(
detach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
detach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
break
;
...
...
examples/cpp/RandomRead.cc
View file @
13ebe8e8
...
...
@@ -70,21 +70,21 @@ void signal_handler(int s) {
int
main
(
int
argc
,
char
**
argv
)
{
bpf
=
new
ebpf
::
BPF
();
auto
init_res
=
bpf
->
init
(
BPF_PROGRAM
);
if
(
std
::
get
<
0
>
(
init_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
init_res
)
<<
std
::
endl
;
if
(
init_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
auto
attach_res
=
bpf
->
attach_tracepoint
(
"random:urandom_read"
,
"on_urandom_read"
);
if
(
std
::
get
<
0
>
(
attach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
attach_res
)
<<
std
::
endl
;
if
(
attach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
auto
open_res
=
bpf
->
open_perf_buffer
(
"events"
,
&
handle_output
);
if
(
std
::
get
<
0
>
(
open_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
open_res
)
<<
std
::
endl
;
if
(
open_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
open_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
examples/cpp/RecordMySQLQuery.cc
View file @
13ebe8e8
...
...
@@ -63,15 +63,15 @@ int main(int argc, char** argv) {
ebpf
::
BPF
bpf
;
auto
init_res
=
bpf
.
init
(
BPF_PROGRAM
);
if
(
std
::
get
<
0
>
(
init_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
init_res
)
<<
std
::
endl
;
if
(
init_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
auto
attach_res
=
bpf
.
attach_uprobe
(
mysql_path
,
ALLOC_QUERY_FUNC
,
"probe_mysql_query"
);
if
(
std
::
get
<
0
>
(
attach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
attach_res
)
<<
std
::
endl
;
if
(
attach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
@@ -94,8 +94,8 @@ int main(int argc, char** argv) {
}
auto
detach_res
=
bpf
.
detach_uprobe
(
mysql_path
,
ALLOC_QUERY_FUNC
);
if
(
std
::
get
<
0
>
(
detach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
detach_res
)
<<
std
::
endl
;
if
(
detach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
detach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
examples/cpp/TCPSendStack.cc
View file @
13ebe8e8
...
...
@@ -58,14 +58,14 @@ struct stack_key_t {
int
main
(
int
argc
,
char
**
argv
)
{
ebpf
::
BPF
bpf
;
auto
init_res
=
bpf
.
init
(
BPF_PROGRAM
);
if
(
std
::
get
<
0
>
(
init_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
init_res
)
<<
std
::
endl
;
if
(
init_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
auto
attach_res
=
bpf
.
attach_kprobe
(
"tcp_sendmsg"
,
"on_tcp_send"
);
if
(
std
::
get
<
0
>
(
attach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
attach_res
)
<<
std
::
endl
;
if
(
attach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
@@ -105,8 +105,8 @@ int main(int argc, char** argv) {
}
auto
detach_res
=
bpf
.
detach_kprobe
(
"tcp_sendmsg"
);
if
(
std
::
get
<
0
>
(
detach_res
)
!=
0
)
{
std
::
cerr
<<
std
::
get
<
1
>
(
detach_res
)
<<
std
::
endl
;
if
(
detach_res
.
code
(
)
!=
0
)
{
std
::
cerr
<<
detach_res
.
msg
(
)
<<
std
::
endl
;
return
1
;
}
...
...
src/cc/BPF.cc
View file @
13ebe8e8
...
...
@@ -54,15 +54,15 @@ StatusTuple BPF::init(const std::string& bpf_program,
for
(
size_t
i
=
0
;
i
<
flags_len
;
i
++
)
flags
[
i
]
=
cflags
[
i
].
c_str
();
if
(
bpf_module_
->
load_string
(
bpf_program
,
flags
,
flags_len
)
!=
0
)
return
mkstatus
(
-
1
,
"Unable to initialize BPF program"
);
return
mkstatus
(
0
);
return
StatusTuple
(
-
1
,
"Unable to initialize BPF program"
);
return
StatusTuple
(
0
);
};
BPF
::~
BPF
()
{
auto
res
=
detach_all
();
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
if
(
res
.
code
(
)
!=
0
)
std
::
cerr
<<
"Failed to detach all probes on destruction: "
<<
std
::
endl
<<
std
::
get
<
1
>
(
res
)
<<
std
::
endl
;
<<
res
.
msg
(
)
<<
std
::
endl
;
}
StatusTuple
BPF
::
detach_all
()
{
...
...
@@ -71,45 +71,45 @@ StatusTuple BPF::detach_all() {
for
(
auto
it
:
kprobes_
)
{
auto
res
=
detach_kprobe_event
(
it
.
first
,
it
.
second
);
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
{
if
(
res
.
code
(
)
!=
0
)
{
error_msg
+=
"Failed to detach kprobe event "
+
it
.
first
+
": "
;
error_msg
+=
std
::
get
<
1
>
(
res
)
+
"
\n
"
;
error_msg
+=
res
.
msg
(
)
+
"
\n
"
;
has_error
=
true
;
}
}
for
(
auto
it
:
uprobes_
)
{
auto
res
=
detach_uprobe_event
(
it
.
first
,
it
.
second
);
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
{
if
(
res
.
code
(
)
!=
0
)
{
error_msg
+=
"Failed to detach uprobe event "
+
it
.
first
+
": "
;
error_msg
+=
std
::
get
<
1
>
(
res
)
+
"
\n
"
;
error_msg
+=
res
.
msg
(
)
+
"
\n
"
;
has_error
=
true
;
}
}
for
(
auto
it
:
tracepoints_
)
{
auto
res
=
detach_tracepoint_event
(
it
.
first
,
it
.
second
);
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
{
if
(
res
.
code
(
)
!=
0
)
{
error_msg
+=
"Failed to detach Tracepoint "
+
it
.
first
+
": "
;
error_msg
+=
std
::
get
<
1
>
(
res
)
+
"
\n
"
;
error_msg
+=
res
.
msg
(
)
+
"
\n
"
;
has_error
=
true
;
}
}
for
(
auto
it
:
perf_buffers_
)
{
auto
res
=
it
.
second
->
close
();
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
{
if
(
res
.
code
(
)
!=
0
)
{
error_msg
+=
"Failed to close perf buffer "
+
it
.
first
+
": "
;
error_msg
+=
std
::
get
<
1
>
(
res
)
+
"
\n
"
;
error_msg
+=
res
.
msg
(
)
+
"
\n
"
;
has_error
=
true
;
}
delete
it
.
second
;
}
if
(
has_error
)
return
mkstatus
(
-
1
,
error_msg
);
return
StatusTuple
(
-
1
,
error_msg
);
else
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
attach_kprobe
(
const
std
::
string
&
kernel_func
,
...
...
@@ -123,7 +123,7 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
std
::
string
probe_event
=
get_kprobe_event
(
kernel_func
,
attach_type
);
if
(
kprobes_
.
find
(
probe_event
)
!=
kprobes_
.
end
())
{
TRY2
(
unload_func
(
probe_func
));
return
mkstatus
(
-
1
,
"kprobe %s already attached"
,
probe_event
.
c_str
());
return
StatusTuple
(
-
1
,
"kprobe %s already attached"
,
probe_event
.
c_str
());
}
std
::
string
probe_event_desc
=
attach_type_prefix
(
attach_type
);
...
...
@@ -135,16 +135,16 @@ StatusTuple BPF::attach_kprobe(const std::string& kernel_func,
if
(
!
res
)
{
TRY2
(
unload_func
(
probe_func
));
return
mkstatus
(
-
1
,
"Unable to attach %skprobe for %s using %s"
,
attach_type_debug
(
attach_type
).
c_str
(),
kernel_func
.
c_str
(),
probe_func
.
c_str
());
return
StatusTuple
(
-
1
,
"Unable to attach %skprobe for %s using %s"
,
attach_type_debug
(
attach_type
)
.
c_str
(),
kernel_func
.
c_str
(),
probe_func
.
c_str
());
}
open_probe_t
p
=
{};
p
.
reader_ptr
=
res
;
p
.
func
=
probe_func
;
kprobes_
[
probe_event
]
=
std
::
move
(
p
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
attach_uprobe
(
const
std
::
string
&
binary_path
,
...
...
@@ -164,7 +164,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
get_uprobe_event
(
sym
.
module
,
sym
.
offset
,
attach_type
);
if
(
uprobes_
.
find
(
probe_event
)
!=
uprobes_
.
end
())
{
TRY2
(
unload_func
(
probe_func
));
return
mkstatus
(
-
1
,
"uprobe %s already attached"
,
probe_event
.
c_str
());
return
StatusTuple
(
-
1
,
"uprobe %s already attached"
,
probe_event
.
c_str
());
}
std
::
string
probe_event_desc
=
attach_type_prefix
(
attach_type
);
...
...
@@ -177,7 +177,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
if
(
!
res
)
{
TRY2
(
unload_func
(
probe_func
));
return
mkstatus
(
return
StatusTuple
(
-
1
,
"Unable to attach %suprobe for binary %s symbol %s addr %lx using %s
\n
"
,
attach_type_debug
(
attach_type
).
c_str
(),
binary_path
.
c_str
(),
...
...
@@ -188,7 +188,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path,
p
.
reader_ptr
=
res
;
p
.
func
=
probe_func
;
uprobes_
[
probe_event
]
=
std
::
move
(
p
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
attach_tracepoint
(
const
std
::
string
&
tracepoint
,
...
...
@@ -199,11 +199,12 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
TRY2
(
load_func
(
probe_func
,
BPF_PROG_TYPE_TRACEPOINT
,
probe_fd
));
if
(
tracepoints_
.
find
(
tracepoint
)
!=
tracepoints_
.
end
())
return
mkstatus
(
-
1
,
"Tracepoint %s already attached"
,
tracepoint
.
c_str
());
return
StatusTuple
(
-
1
,
"Tracepoint %s already attached"
,
tracepoint
.
c_str
());
auto
pos
=
tracepoint
.
find
(
":"
);
if
((
pos
==
std
::
string
::
npos
)
||
(
pos
!=
tracepoint
.
rfind
(
":"
)))
return
mkstatus
(
-
1
,
"Unable to parse Tracepoint %s"
,
tracepoint
.
c_str
());
return
StatusTuple
(
-
1
,
"Unable to parse Tracepoint %s"
,
tracepoint
.
c_str
());
std
::
string
tp_category
=
tracepoint
.
substr
(
0
,
pos
);
std
::
string
tp_name
=
tracepoint
.
substr
(
pos
+
1
);
...
...
@@ -213,15 +214,15 @@ StatusTuple BPF::attach_tracepoint(const std::string& tracepoint,
if
(
!
res
)
{
TRY2
(
unload_func
(
probe_func
));
return
mkstatus
(
-
1
,
"Unable to attach Tracepoint %s using %s"
,
tracepoint
.
c_str
(),
probe_func
.
c_str
());
return
StatusTuple
(
-
1
,
"Unable to attach Tracepoint %s using %s"
,
tracepoint
.
c_str
(),
probe_func
.
c_str
());
}
open_probe_t
p
=
{};
p
.
reader_ptr
=
res
;
p
.
func
=
probe_func
;
tracepoints_
[
tracepoint
]
=
std
::
move
(
p
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_kprobe
(
const
std
::
string
&
kernel_func
,
...
...
@@ -230,13 +231,13 @@ StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
auto
it
=
kprobes_
.
find
(
event
);
if
(
it
==
kprobes_
.
end
())
return
mkstatus
(
-
1
,
"No open %skprobe for %s"
,
attach_type_debug
(
attach_type
).
c_str
(),
kernel_func
.
c_str
());
return
StatusTuple
(
-
1
,
"No open %skprobe for %s"
,
attach_type_debug
(
attach_type
).
c_str
(),
kernel_func
.
c_str
());
TRY2
(
detach_kprobe_event
(
it
->
first
,
it
->
second
));
kprobes_
.
erase
(
it
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_uprobe
(
const
std
::
string
&
binary_path
,
...
...
@@ -248,23 +249,23 @@ StatusTuple BPF::detach_uprobe(const std::string& binary_path,
std
::
string
event
=
get_uprobe_event
(
sym
.
module
,
sym
.
offset
,
attach_type
);
auto
it
=
uprobes_
.
find
(
event
);
if
(
it
==
uprobes_
.
end
())
return
mkstatus
(
-
1
,
"No open %suprobe for binary %s symbol %s addr %lx"
,
attach_type_debug
(
attach_type
).
c_str
(),
binary_path
.
c_str
(),
symbol
.
c_str
(),
symbol_addr
);
return
StatusTuple
(
-
1
,
"No open %suprobe for binary %s symbol %s addr %lx"
,
attach_type_debug
(
attach_type
)
.
c_str
(),
binary_path
.
c_str
(),
symbol
.
c_str
(),
symbol_addr
);
TRY2
(
detach_uprobe_event
(
it
->
first
,
it
->
second
));
uprobes_
.
erase
(
it
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_tracepoint
(
const
std
::
string
&
tracepoint
)
{
auto
it
=
tracepoints_
.
find
(
tracepoint
);
if
(
it
==
tracepoints_
.
end
())
return
mkstatus
(
-
1
,
"No open Tracepoint %s"
,
tracepoint
.
c_str
());
return
StatusTuple
(
-
1
,
"No open Tracepoint %s"
,
tracepoint
.
c_str
());
TRY2
(
detach_tracepoint_event
(
it
->
first
,
it
->
second
));
tracepoints_
.
erase
(
it
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
open_perf_buffer
(
const
std
::
string
&
name
,
...
...
@@ -273,15 +274,15 @@ StatusTuple BPF::open_perf_buffer(const std::string& name,
perf_buffers_
[
name
]
=
new
BPFPerfBuffer
(
bpf_module_
.
get
(),
name
);
auto
table
=
perf_buffers_
[
name
];
TRY2
(
table
->
open
(
cb
,
cb_cookie
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
close_perf_buffer
(
const
std
::
string
&
name
)
{
auto
it
=
perf_buffers_
.
find
(
name
);
if
(
it
==
perf_buffers_
.
end
())
return
mkstatus
(
-
1
,
"Perf buffer for %s not open"
,
name
.
c_str
());
return
StatusTuple
(
-
1
,
"Perf buffer for %s not open"
,
name
.
c_str
());
TRY2
(
it
->
second
->
close
());
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
void
BPF
::
poll_perf_buffer
(
const
std
::
string
&
name
,
int
timeout
)
{
...
...
@@ -295,12 +296,13 @@ StatusTuple BPF::load_func(const std::string& func_name,
enum
bpf_prog_type
type
,
int
&
fd
)
{
if
(
funcs_
.
find
(
func_name
)
!=
funcs_
.
end
())
{
fd
=
funcs_
[
func_name
];
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
uint8_t
*
func_start
=
bpf_module_
->
function_start
(
func_name
);
if
(
!
func_start
)
return
mkstatus
(
-
1
,
"Can't find start of function %s"
,
func_name
.
c_str
());
return
StatusTuple
(
-
1
,
"Can't find start of function %s"
,
func_name
.
c_str
());
size_t
func_size
=
bpf_module_
->
function_size
(
func_name
);
fd
=
bpf_prog_load
(
type
,
reinterpret_cast
<
struct
bpf_insn
*>
(
func_start
),
...
...
@@ -310,22 +312,22 @@ StatusTuple BPF::load_func(const std::string& func_name,
);
if
(
fd
<
0
)
return
mkstatus
(
-
1
,
"Failed to load %s: %d"
,
func_name
.
c_str
(),
fd
);
return
StatusTuple
(
-
1
,
"Failed to load %s: %d"
,
func_name
.
c_str
(),
fd
);
funcs_
[
func_name
]
=
fd
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
unload_func
(
const
std
::
string
&
func_name
)
{
auto
it
=
funcs_
.
find
(
func_name
);
if
(
it
==
funcs_
.
end
())
return
mkstatus
(
-
1
,
"Probe function %s not loaded"
,
func_name
.
c_str
());
return
StatusTuple
(
-
1
,
"Probe function %s not loaded"
,
func_name
.
c_str
());
int
res
=
close
(
it
->
second
);
if
(
res
!=
0
)
return
mkstatus
(
-
1
,
"Can't close FD for %s: %d"
,
it
->
first
.
c_str
(),
res
);
return
StatusTuple
(
-
1
,
"Can't close FD for %s: %d"
,
it
->
first
.
c_str
(),
res
);
funcs_
.
erase
(
it
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
check_binary_symbol
(
const
std
::
string
&
binary_path
,
...
...
@@ -334,10 +336,10 @@ StatusTuple BPF::check_binary_symbol(const std::string& binary_path,
int
res
=
bcc_resolve_symname
(
binary_path
.
c_str
(),
symbol
.
c_str
(),
symbol_addr
,
output
);
if
(
res
<
0
)
return
mkstatus
(
-
1
,
"Unable to find offset for binary %s symbol %s address %lx"
,
binary_path
.
c_str
(),
symbol
.
c_str
(),
symbol_addr
);
return
mkstatus
(
0
);
return
StatusTuple
(
-
1
,
"Unable to find offset for binary %s symbol %s address %lx"
,
binary_path
.
c_str
(),
symbol
.
c_str
(),
symbol_addr
);
return
StatusTuple
(
0
);
}
std
::
string
BPF
::
get_kprobe_event
(
const
std
::
string
&
kernel_func
,
...
...
@@ -364,8 +366,8 @@ StatusTuple BPF::detach_kprobe_event(const std::string& event,
TRY2
(
unload_func
(
attr
.
func
));
std
::
string
detach_event
=
"-:kprobes/"
+
event
;
if
(
bpf_detach_kprobe
(
detach_event
.
c_str
())
<
0
)
return
mkstatus
(
-
1
,
"Unable to detach kprobe %s"
,
event
.
c_str
());
return
mkstatus
(
0
);
return
StatusTuple
(
-
1
,
"Unable to detach kprobe %s"
,
event
.
c_str
());
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_uprobe_event
(
const
std
::
string
&
event
,
...
...
@@ -377,8 +379,8 @@ StatusTuple BPF::detach_uprobe_event(const std::string& event,
TRY2
(
unload_func
(
attr
.
func
));
std
::
string
detach_event
=
"-:uprobes/"
+
event
;
if
(
bpf_detach_uprobe
(
detach_event
.
c_str
())
<
0
)
return
mkstatus
(
-
1
,
"Unable to detach uprobe %s"
,
event
.
c_str
());
return
mkstatus
(
0
);
return
StatusTuple
(
-
1
,
"Unable to detach uprobe %s"
,
event
.
c_str
());
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_tracepoint_event
(
const
std
::
string
&
tracepoint
,
...
...
@@ -390,7 +392,7 @@ StatusTuple BPF::detach_tracepoint_event(const std::string& tracepoint,
TRY2
(
unload_func
(
attr
.
func
));
// TODO: bpf_detach_tracepoint currently does nothing.
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
}
// namespace ebpf
src/cc/BPFTable.cc
View file @
13ebe8e8
...
...
@@ -68,36 +68,36 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
StatusTuple
BPFPerfBuffer
::
open_on_cpu
(
perf_reader_raw_cb
cb
,
int
cpu
,
void
*
cb_cookie
)
{
if
(
cpu_readers_
.
find
(
cpu
)
!=
cpu_readers_
.
end
())
return
mkstatus
(
-
1
,
"Perf buffer already open on CPU %d"
,
cpu
);
return
StatusTuple
(
-
1
,
"Perf buffer already open on CPU %d"
,
cpu
);
auto
reader
=
static_cast
<
perf_reader
*>
(
bpf_open_perf_buffer
(
cb
,
cb_cookie
,
-
1
,
cpu
));
if
(
reader
==
nullptr
)
return
mkstatus
(
-
1
,
"Unable to construct perf reader"
);
return
StatusTuple
(
-
1
,
"Unable to construct perf reader"
);
int
reader_fd
=
perf_reader_fd
(
reader
);
if
(
!
update
(
&
cpu
,
&
reader_fd
))
{
perf_reader_free
(
static_cast
<
void
*>
(
reader
));
return
mkstatus
(
-
1
,
"Unable to open perf buffer on CPU %d: %s"
,
cpu
,
strerror
(
errno
));
return
StatusTuple
(
-
1
,
"Unable to open perf buffer on CPU %d: %s"
,
cpu
,
strerror
(
errno
));
}
cpu_readers_
[
cpu
]
=
static_cast
<
perf_reader
*>
(
reader
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPFPerfBuffer
::
open
(
perf_reader_raw_cb
cb
,
void
*
cb_cookie
)
{
for
(
int
i
=
0
;
i
<
sysconf
(
_SC_NPROCESSORS_ONLN
);
i
++
)
TRY2
(
open_on_cpu
(
cb
,
i
,
cb_cookie
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPFPerfBuffer
::
close_on_cpu
(
int
cpu
)
{
auto
it
=
cpu_readers_
.
find
(
cpu
);
if
(
it
==
cpu_readers_
.
end
())
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
perf_reader_free
(
static_cast
<
void
*>
(
it
->
second
));
if
(
!
remove
(
const_cast
<
int
*>
(
&
(
it
->
first
))))
return
mkstatus
(
-
1
,
"Unable to close perf buffer on CPU %d"
,
it
->
first
);
return
StatusTuple
(
-
1
,
"Unable to close perf buffer on CPU %d"
,
it
->
first
);
cpu_readers_
.
erase
(
it
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
BPFPerfBuffer
::
close
()
{
...
...
@@ -105,15 +105,15 @@ StatusTuple BPFPerfBuffer::close() {
bool
has_error
=
false
;
for
(
int
i
=
0
;
i
<
sysconf
(
_SC_NPROCESSORS_ONLN
);
i
++
)
{
auto
res
=
close_on_cpu
(
i
);
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
{
if
(
res
.
code
(
)
!=
0
)
{
errors
+=
"Failed to close CPU"
+
std
::
to_string
(
i
)
+
" perf buffer: "
;
errors
+=
std
::
get
<
1
>
(
res
)
+
"
\n
"
;
errors
+=
res
.
msg
(
)
+
"
\n
"
;
has_error
=
true
;
}
}
if
(
has_error
)
return
mkstatus
(
-
1
,
errors
);
return
mkstatus
(
0
);
return
StatusTuple
(
-
1
,
errors
);
return
StatusTuple
(
0
);
}
void
BPFPerfBuffer
::
poll
(
int
timeout
)
{
...
...
@@ -126,9 +126,9 @@ void BPFPerfBuffer::poll(int timeout) {
BPFPerfBuffer
::~
BPFPerfBuffer
()
{
auto
res
=
close
();
if
(
std
::
get
<
0
>
(
res
)
!=
0
)
if
(
res
.
code
(
)
!=
0
)
std
::
cerr
<<
"Failed to close all perf buffer on destruction: "
<<
std
::
get
<
1
>
(
res
)
<<
std
::
endl
;
<<
res
.
msg
(
)
<<
std
::
endl
;
}
}
// namespace ebpf
src/cc/bcc_exception.h
View file @
13ebe8e8
...
...
@@ -18,40 +18,40 @@
#include <cstdio>
#include <string>
#include <tuple>
#undef NDEBUG
namespace
ebpf
{
typedef
std
::
tuple
<
int
,
std
::
string
>
StatusTuple
;
class
StatusTuple
{
public:
StatusTuple
(
int
ret
)
:
ret_
(
ret
)
{}
template
<
typename
...
Args
>
StatusTuple
mkstatus
(
int
ret
,
const
char
*
fmt
,
Args
...
args
)
{
char
buf
[
1024
];
snprintf
(
buf
,
sizeof
(
buf
),
fmt
,
args
...);
return
std
::
make_tuple
(
ret
,
std
::
string
(
buf
));
}
StatusTuple
(
int
ret
,
const
char
*
msg
)
:
ret_
(
ret
),
msg_
(
msg
)
{}
static
inline
StatusTuple
mkstatus
(
int
ret
,
const
char
*
msg
)
{
return
std
::
make_tuple
(
ret
,
std
::
string
(
msg
));
}
StatusTuple
(
int
ret
,
const
std
::
string
&
msg
)
:
ret_
(
ret
),
msg_
(
msg
)
{}
static
inline
StatusTuple
mkstatus
(
int
ret
,
const
std
::
string
&
msg
)
{
return
std
::
make_tuple
(
ret
,
msg
);
}
template
<
typename
...
Args
>
StatusTuple
(
int
ret
,
const
char
*
fmt
,
Args
...
args
)
:
ret_
(
ret
)
{
char
buf
[
2048
];
snprintf
(
buf
,
sizeof
(
buf
),
fmt
,
args
...);
msg_
=
std
::
string
(
buf
);
}
static
inline
StatusTuple
mkstatus
(
int
ret
)
{
return
std
::
make_tuple
(
ret
,
std
::
string
());
}
int
code
()
{
return
ret_
;
}
#define TRY2(CMD) \
do { \
std
::
string
msg
()
{
return
msg_
;
}
private:
int
ret_
;
std
::
string
msg_
;
};
#define TRY2(CMD) \
do { \
StatusTuple __stp = (CMD); \
if (
std::get<0>(__stp) != 0) {
\
return __stp; \
} \
if (
__stp.code() != 0) {
\
return __stp;
\
}
\
} while (0)
}
// namespace ebpf
src/cc/frontends/b/codegen_llvm.cc
View file @
13ebe8e8
...
...
@@ -126,7 +126,7 @@ StatusTuple CodegenLLVM::visit_block_stmt_node(BlockStmtNode *n) {
if
(
n
->
scope_
)
scopes_
->
pop_var
();
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_if_stmt_node
(
IfStmtNode
*
n
)
{
...
...
@@ -159,7 +159,7 @@ StatusTuple CodegenLLVM::visit_if_stmt_node(IfStmtNode *n) {
B
.
SetInsertPoint
(
label_end
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_onvalid_stmt_node
(
OnValidStmtNode
*
n
)
{
...
...
@@ -192,7 +192,7 @@ StatusTuple CodegenLLVM::visit_onvalid_stmt_node(OnValidStmtNode *n) {
}
B
.
SetInsertPoint
(
label_end
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_switch_stmt_node
(
SwitchStmtNode
*
n
)
{
...
...
@@ -213,7 +213,7 @@ StatusTuple CodegenLLVM::visit_switch_stmt_node(SwitchStmtNode *n) {
B
.
SetInsertPoint
(
resolve_label
(
"DONE"
));
label_end
->
eraseFromParent
();
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_case_stmt_node
(
CaseStmtNode
*
n
)
{
...
...
@@ -236,7 +236,7 @@ StatusTuple CodegenLLVM::visit_case_stmt_node(CaseStmtNode *n) {
if
(
!
B
.
GetInsertBlock
()
->
getTerminator
())
B
.
CreateBr
(
label_end
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_ident_expr_node
(
IdentExprNode
*
n
)
{
...
...
@@ -308,7 +308,7 @@ StatusTuple CodegenLLVM::visit_ident_expr_node(IdentExprNode *n) {
}
}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_assign_expr_node
(
AssignExprNode
*
n
)
{
...
...
@@ -343,7 +343,7 @@ StatusTuple CodegenLLVM::visit_assign_expr_node(AssignExprNode *n) {
}
}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
lookup_var
(
Node
*
n
,
const
string
&
name
,
Scopes
::
VarScope
*
scope
,
...
...
@@ -353,7 +353,7 @@ StatusTuple CodegenLLVM::lookup_var(Node *n, const string &name, Scopes::VarScop
auto
it
=
vars_
.
find
(
*
decl
);
if
(
it
==
vars_
.
end
())
return
mkstatus_
(
n
,
"unable to find %s memory location"
,
name
.
c_str
());
*
mem
=
it
->
second
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_packet_expr_node
(
PacketExprNode
*
n
)
{
...
...
@@ -407,7 +407,7 @@ StatusTuple CodegenLLVM::visit_packet_expr_node(PacketExprNode *n) {
return
mkstatus_
(
n
,
"unsupported"
);
}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_integer_expr_node
(
IntegerExprNode
*
n
)
{
...
...
@@ -416,7 +416,7 @@ StatusTuple CodegenLLVM::visit_integer_expr_node(IntegerExprNode *n) {
expr_
=
ConstantInt
::
get
(
mod_
->
getContext
(),
val
);
if
(
n
->
bits_
)
expr_
=
B
.
CreateIntCast
(
expr_
,
B
.
getIntNTy
(
n
->
bits_
),
false
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_string_expr_node
(
StringExprNode
*
n
)
{
...
...
@@ -427,7 +427,7 @@ StatusTuple CodegenLLVM::visit_string_expr_node(StringExprNode *n) {
B
.
CreateMemCpy
(
ptr
,
global
,
n
->
val_
.
size
()
+
1
,
1
);
expr_
=
ptr
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_short_circuit_and
(
BinopExprNode
*
n
)
{
...
...
@@ -454,7 +454,7 @@ StatusTuple CodegenLLVM::emit_short_circuit_and(BinopExprNode *n) {
phi
->
addIncoming
(
pop_expr
(),
label_then
);
expr_
=
phi
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_short_circuit_or
(
BinopExprNode
*
n
)
{
...
...
@@ -481,7 +481,7 @@ StatusTuple CodegenLLVM::emit_short_circuit_or(BinopExprNode *n) {
phi
->
addIncoming
(
pop_expr
(),
label_then
);
expr_
=
phi
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_binop_expr_node
(
BinopExprNode
*
n
)
{
...
...
@@ -509,7 +509,7 @@ StatusTuple CodegenLLVM::visit_binop_expr_node(BinopExprNode *n) {
case
Tok
:
:
TLOR
:
expr_
=
B
.
CreateOr
(
lhs
,
rhs
);
break
;
default:
return
mkstatus_
(
n
,
"unsupported binary operator"
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_unop_expr_node
(
UnopExprNode
*
n
)
{
...
...
@@ -519,11 +519,11 @@ StatusTuple CodegenLLVM::visit_unop_expr_node(UnopExprNode *n) {
case
Tok
:
:
TCMPL
:
expr_
=
B
.
CreateNeg
(
pop_expr
());
break
;
default:
{}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_bitop_expr_node
(
BitopExprNode
*
n
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_goto_expr_node
(
GotoExprNode
*
n
)
{
...
...
@@ -553,7 +553,7 @@ StatusTuple CodegenLLVM::visit_goto_expr_node(GotoExprNode *n) {
}
}
B
.
CreateBr
(
resolve_label
(
jump_label
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_return_expr_node
(
ReturnExprNode
*
n
)
{
...
...
@@ -562,7 +562,7 @@ StatusTuple CodegenLLVM::visit_return_expr_node(ReturnExprNode *n) {
Value
*
cast_1
=
B
.
CreateIntCast
(
pop_expr
(),
parent
->
getReturnType
(),
true
);
B
.
CreateStore
(
cast_1
,
retval_
);
B
.
CreateBr
(
resolve_label
(
"DONE"
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_table_lookup
(
MethodCallExprNode
*
n
)
{
...
...
@@ -605,7 +605,7 @@ StatusTuple CodegenLLVM::emit_table_lookup(MethodCallExprNode *n) {
}
else
{
return
mkstatus_
(
n
,
"lookup in table type %s unsupported"
,
table
->
type_id
()
->
c_str
());
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_table_update
(
MethodCallExprNode
*
n
)
{
...
...
@@ -636,7 +636,7 @@ StatusTuple CodegenLLVM::emit_table_update(MethodCallExprNode *n) {
}
else
{
return
mkstatus_
(
n
,
"unsupported"
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_table_delete
(
MethodCallExprNode
*
n
)
{
...
...
@@ -663,7 +663,7 @@ StatusTuple CodegenLLVM::emit_table_delete(MethodCallExprNode *n) {
}
else
{
return
mkstatus_
(
n
,
"unsupported"
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_log
(
MethodCallExprNode
*
n
)
{
...
...
@@ -684,13 +684,13 @@ StatusTuple CodegenLLVM::emit_log(MethodCallExprNode *n) {
PointerType
::
getUnqual
(
printk_fn_type
));
expr_
=
B
.
CreateCall
(
printk_fn
,
args
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_packet_rewrite_field
(
MethodCallExprNode
*
n
)
{
TRY2
(
n
->
args_
[
1
]
->
accept
(
this
));
TRY2
(
n
->
args_
[
0
]
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_atomic_add
(
MethodCallExprNode
*
n
)
{
...
...
@@ -701,7 +701,7 @@ StatusTuple CodegenLLVM::emit_atomic_add(MethodCallExprNode *n) {
AtomicRMWInst
*
atomic_inst
=
B
.
CreateAtomicRMW
(
AtomicRMWInst
::
Add
,
lhs
,
rhs
,
AtomicOrdering
::
SequentiallyConsistent
);
atomic_inst
->
setVolatile
(
false
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_incr_cksum
(
MethodCallExprNode
*
n
,
size_t
sz
)
{
...
...
@@ -738,11 +738,11 @@ StatusTuple CodegenLLVM::emit_incr_cksum(MethodCallExprNode *n, size_t sz) {
Value
*
skb_ptr8
=
B
.
CreateBitCast
(
skb_ptr
,
B
.
getInt8PtrTy
());
expr_
=
B
.
CreateCall
(
csum_fn
,
vector
<
Value
*>
({
skb_ptr8
,
offset
,
old_val
,
new_val
,
flags
}));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
emit_get_usec_time
(
MethodCallExprNode
*
n
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_method_call_expr_node
(
MethodCallExprNode
*
n
)
{
...
...
@@ -768,7 +768,7 @@ StatusTuple CodegenLLVM::visit_method_call_expr_node(MethodCallExprNode *n) {
return
mkstatus_
(
n
,
"unsupported"
);
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
/* result = lookup(key)
...
...
@@ -874,7 +874,7 @@ StatusTuple CodegenLLVM::visit_table_index_expr_node(TableIndexExprNode *n) {
}
else
{
expr_
=
result
;
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
/// on_match
...
...
@@ -907,7 +907,7 @@ StatusTuple CodegenLLVM::visit_match_decl_stmt_node(MatchDeclStmtNode *n) {
}
B
.
SetInsertPoint
(
label_end
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
/// on_miss
...
...
@@ -935,7 +935,7 @@ StatusTuple CodegenLLVM::visit_miss_decl_stmt_node(MissDeclStmtNode *n) {
}
B
.
SetInsertPoint
(
label_end
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_failure_decl_stmt_node
(
FailureDeclStmtNode
*
n
)
{
...
...
@@ -945,12 +945,12 @@ StatusTuple CodegenLLVM::visit_failure_decl_stmt_node(FailureDeclStmtNode *n) {
StatusTuple
CodegenLLVM
::
visit_expr_stmt_node
(
ExprStmtNode
*
n
)
{
TRY2
(
n
->
expr_
->
accept
(
this
));
expr_
=
nullptr
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_struct_variable_decl_stmt_node
(
StructVariableDeclStmtNode
*
n
)
{
if
(
n
->
struct_id_
->
name_
==
""
||
n
->
struct_id_
->
name_
[
0
]
==
'_'
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StructType
*
stype
;
...
...
@@ -1004,12 +1004,12 @@ StatusTuple CodegenLLVM::visit_struct_variable_decl_stmt_node(StructVariableDecl
}
}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_integer_variable_decl_stmt_node
(
IntegerVariableDeclStmtNode
*
n
)
{
if
(
!
B
.
GetInsertBlock
())
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
// uintX var = init
AllocaInst
*
ptr_a
=
new
AllocaInst
(
B
.
getIntNTy
(
n
->
bit_width_
),
n
->
id_
->
name_
,
resolve_entry_stack
());
...
...
@@ -1018,7 +1018,7 @@ StatusTuple CodegenLLVM::visit_integer_variable_decl_stmt_node(IntegerVariableDe
// todo
if
(
!
n
->
init_
.
empty
())
TRY2
(
n
->
init_
[
0
]
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_struct_decl_stmt_node
(
StructDeclStmtNode
*
n
)
{
...
...
@@ -1029,7 +1029,7 @@ StatusTuple CodegenLLVM::visit_struct_decl_stmt_node(StructDeclStmtNode *n) {
fields
.
push_back
(
B
.
getIntNTy
((
*
it
)
->
bit_width_
));
struct_type
->
setBody
(
fields
,
n
->
is_packed
());
structs_
[
n
]
=
struct_type
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_parser_state_stmt_node
(
ParserStateStmtNode
*
n
)
{
...
...
@@ -1038,12 +1038,12 @@ StatusTuple CodegenLLVM::visit_parser_state_stmt_node(ParserStateStmtNode *n) {
B
.
SetInsertPoint
(
label_entry
);
if
(
n
->
next_state_
)
TRY2
(
n
->
next_state_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_state_decl_stmt_node
(
StateDeclStmtNode
*
n
)
{
if
(
!
n
->
id_
)
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
string
jump_label
=
n
->
scoped_name
();
BasicBlock
*
label_entry
=
resolve_label
(
jump_label
);
B
.
SetInsertPoint
(
label_entry
);
...
...
@@ -1067,7 +1067,7 @@ StatusTuple CodegenLLVM::visit_state_decl_stmt_node(StateDeclStmtNode *n) {
}
scopes_
->
pop_state
();
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_table_decl_stmt_node
(
TableDeclStmtNode
*
n
)
{
...
...
@@ -1107,7 +1107,7 @@ StatusTuple CodegenLLVM::visit_table_decl_stmt_node(TableDeclStmtNode *n) {
}
else
{
return
mkstatus_
(
n
,
"Table %s not implemented"
,
n
->
table_type_
->
name_
.
c_str
());
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
lookup_struct_type
(
StructDeclStmtNode
*
decl
,
StructType
**
stype
)
const
{
...
...
@@ -1116,7 +1116,7 @@ StatusTuple CodegenLLVM::lookup_struct_type(StructDeclStmtNode *decl, StructType
return
mkstatus_
(
decl
,
"could not find IR for type %s"
,
decl
->
id_
->
c_str
());
*
stype
=
struct_it
->
second
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
lookup_struct_type
(
VariableDeclStmtNode
*
n
,
StructType
**
stype
,
...
...
@@ -1138,7 +1138,7 @@ StatusTuple CodegenLLVM::lookup_struct_type(VariableDeclStmtNode *n, StructType
if
(
decl
)
*
decl
=
type
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit_func_decl_stmt_node
(
FuncDeclStmtNode
*
n
)
{
...
...
@@ -1216,7 +1216,7 @@ StatusTuple CodegenLLVM::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
B
.
CreateRet
(
B
.
CreateLoad
(
retval_
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
visit
(
Node
*
root
,
vector
<
TableDesc
>
&
tables
)
{
...
...
@@ -1248,7 +1248,7 @@ StatusTuple CodegenLLVM::visit(Node* root, vector<TableDesc> &tables) {
""
,
""
,
});
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
CodegenLLVM
::
print_header
()
{
...
...
@@ -1276,7 +1276,7 @@ StatusTuple CodegenLLVM::print_header() {
continue
;
TRY2
((
*
it
)
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
int
CodegenLLVM
::
get_table_fd
(
const
string
&
name
)
const
{
...
...
src/cc/frontends/b/loader.cc
View file @
13ebe8e8
...
...
@@ -20,7 +20,6 @@
#include "loader.h"
#include "table_desc.h"
using
std
::
get
;
using
std
::
string
;
using
std
::
unique_ptr
;
using
std
::
vector
;
...
...
@@ -57,8 +56,8 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot
ebpf
::
cc
::
TypeCheck
type_check
(
parser_
->
scopes_
.
get
(),
proto_parser_
->
scopes_
.
get
());
auto
ret
=
type_check
.
visit
(
parser_
->
root_node_
);
if
(
get
<
0
>
(
ret
)
!=
0
||
get
<
1
>
(
ret
).
size
())
{
fprintf
(
stderr
,
"Type error @line=%d: %s
\n
"
,
get
<
0
>
(
ret
),
get
<
1
>
(
ret
).
c_str
());
if
(
ret
.
code
()
!=
0
||
ret
.
msg
(
).
size
())
{
fprintf
(
stderr
,
"Type error @line=%d: %s
\n
"
,
ret
.
code
(),
ret
.
msg
(
).
c_str
());
return
-
1
;
}
...
...
@@ -66,9 +65,9 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot
codegen_
=
ebpf
::
make_unique
<
ebpf
::
cc
::
CodegenLLVM
>
(
mod
,
parser_
->
scopes_
.
get
(),
proto_parser_
->
scopes_
.
get
());
ret
=
codegen_
->
visit
(
parser_
->
root_node_
,
**
tables
);
if
(
get
<
0
>
(
ret
)
!=
0
||
get
<
1
>
(
ret
).
size
())
{
fprintf
(
stderr
,
"Codegen error @line=%d: %s
\n
"
,
get
<
0
>
(
ret
),
get
<
1
>
(
ret
).
c_str
());
return
get
<
0
>
(
ret
);
if
(
ret
.
code
()
!=
0
||
ret
.
msg
(
).
size
())
{
fprintf
(
stderr
,
"Codegen error @line=%d: %s
\n
"
,
ret
.
code
(),
ret
.
msg
(
).
c_str
());
return
ret
.
code
(
);
}
return
0
;
...
...
src/cc/frontends/b/node.h
View file @
13ebe8e8
...
...
@@ -114,20 +114,20 @@ class Node {
};
template
<
typename
...
Args
>
std
::
tuple
<
int
,
std
::
string
>
mkstatus_
(
Node
*
n
,
const
char
*
fmt
,
Args
...
args
)
{
char
buf
[
1024
];
StatusTuple
mkstatus_
(
Node
*
n
,
const
char
*
fmt
,
Args
...
args
)
{
char
buf
[
2048
];
snprintf
(
buf
,
sizeof
(
buf
),
fmt
,
args
...);
string
out_msg
(
buf
);
if
(
n
->
line_
>
0
)
out_msg
+=
"
\n
"
+
n
->
text_
;
return
std
::
make_t
uple
(
n
->
line_
?
n
->
line_
:
-
1
,
out_msg
);
return
StatusT
uple
(
n
->
line_
?
n
->
line_
:
-
1
,
out_msg
);
}
static
inline
std
::
tuple
<
int
,
std
::
string
>
mkstatus_
(
Node
*
n
,
const
char
*
msg
)
{
static
inline
StatusTuple
mkstatus_
(
Node
*
n
,
const
char
*
msg
)
{
string
out_msg
(
msg
);
if
(
n
->
line_
>
0
)
out_msg
+=
"
\n
"
+
n
->
text_
;
return
std
::
make_t
uple
(
n
->
line_
?
n
->
line_
:
-
1
,
out_msg
);
return
StatusT
uple
(
n
->
line_
?
n
->
line_
:
-
1
,
out_msg
);
}
class
StmtNode
:
public
Node
{
...
...
src/cc/frontends/b/printer.cc
View file @
13ebe8e8
...
...
@@ -38,7 +38,7 @@ StatusTuple Printer::visit_block_stmt_node(BlockStmtNode* n) {
--
indent_
;
}
fprintf
(
out_
,
"%*s}"
,
indent_
,
""
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_if_stmt_node
(
IfStmtNode
*
n
)
{
...
...
@@ -50,7 +50,7 @@ StatusTuple Printer::visit_if_stmt_node(IfStmtNode* n) {
fprintf
(
out_
,
" else "
);
TRY2
(
n
->
false_block_
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_onvalid_stmt_node
(
OnValidStmtNode
*
n
)
{
...
...
@@ -62,7 +62,7 @@ StatusTuple Printer::visit_onvalid_stmt_node(OnValidStmtNode* n) {
fprintf
(
out_
,
" else "
);
TRY2
(
n
->
else_block_
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_switch_stmt_node
(
SwitchStmtNode
*
n
)
{
...
...
@@ -70,7 +70,7 @@ StatusTuple Printer::visit_switch_stmt_node(SwitchStmtNode* n) {
TRY2
(
n
->
cond_
->
accept
(
this
));
fprintf
(
out_
,
") "
);
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_case_stmt_node
(
CaseStmtNode
*
n
)
{
...
...
@@ -81,7 +81,7 @@ StatusTuple Printer::visit_case_stmt_node(CaseStmtNode* n) {
fprintf
(
out_
,
"default"
);
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_ident_expr_node
(
IdentExprNode
*
n
)
{
...
...
@@ -92,37 +92,37 @@ StatusTuple Printer::visit_ident_expr_node(IdentExprNode* n) {
if
(
n
->
sub_name_
.
size
())
{
fprintf
(
out_
,
".%s"
,
n
->
sub_name_
.
c_str
());
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_assign_expr_node
(
AssignExprNode
*
n
)
{
TRY2
(
n
->
lhs_
->
accept
(
this
));
fprintf
(
out_
,
" = "
);
TRY2
(
n
->
rhs_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_packet_expr_node
(
PacketExprNode
*
n
)
{
fprintf
(
out_
,
"$"
);
TRY2
(
n
->
id_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_integer_expr_node
(
IntegerExprNode
*
n
)
{
fprintf
(
out_
,
"%s:%zu"
,
n
->
val_
.
c_str
(),
n
->
bits_
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_string_expr_node
(
StringExprNode
*
n
)
{
fprintf
(
out_
,
"%s"
,
n
->
val_
.
c_str
());
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_binop_expr_node
(
BinopExprNode
*
n
)
{
TRY2
(
n
->
lhs_
->
accept
(
this
));
fprintf
(
out_
,
"%d"
,
n
->
op_
);
TRY2
(
n
->
rhs_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_unop_expr_node
(
UnopExprNode
*
n
)
{
...
...
@@ -135,25 +135,25 @@ StatusTuple Printer::visit_unop_expr_node(UnopExprNode* n) {
}
fprintf
(
out_
,
"%s"
,
s
);
TRY2
(
n
->
expr_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_bitop_expr_node
(
BitopExprNode
*
n
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_return_expr_node
(
ReturnExprNode
*
n
)
{
fprintf
(
out_
,
"return "
);
TRY2
(
n
->
expr_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_goto_expr_node
(
GotoExprNode
*
n
)
{
const
char
*
s
=
n
->
is_continue_
?
"continue "
:
"goto "
;
fprintf
(
out_
,
"%s"
,
s
);
TRY2
(
n
->
id_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_method_call_expr_node
(
MethodCallExprNode
*
n
)
{
...
...
@@ -177,19 +177,19 @@ StatusTuple Printer::visit_method_call_expr_node(MethodCallExprNode* n) {
--
indent_
;
fprintf
(
out_
,
"%*s}"
,
indent_
,
""
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_table_index_expr_node
(
TableIndexExprNode
*
n
)
{
fprintf
(
out_
,
"%s["
,
n
->
id_
->
c_str
());
TRY2
(
n
->
index_
->
accept
(
this
));
fprintf
(
out_
,
"]"
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_expr_stmt_node
(
ExprStmtNode
*
n
)
{
TRY2
(
n
->
expr_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_struct_variable_decl_stmt_node
(
StructVariableDeclStmtNode
*
n
)
{
...
...
@@ -207,7 +207,7 @@ StatusTuple Printer::visit_struct_variable_decl_stmt_node(StructVariableDeclStmt
}
fprintf
(
out_
,
"}"
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_integer_variable_decl_stmt_node
(
IntegerVariableDeclStmtNode
*
n
)
{
...
...
@@ -218,7 +218,7 @@ StatusTuple Printer::visit_integer_variable_decl_stmt_node(IntegerVariableDeclSt
fprintf
(
out_
,
"; "
);
TRY2
(
n
->
init_
[
0
]
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_struct_decl_stmt_node
(
StructDeclStmtNode
*
n
)
{
...
...
@@ -233,12 +233,12 @@ StatusTuple Printer::visit_struct_decl_stmt_node(StructDeclStmtNode* n) {
}
--
indent_
;
fprintf
(
out_
,
"%*s}"
,
indent_
,
""
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_state_decl_stmt_node
(
StateDeclStmtNode
*
n
)
{
if
(
!
n
->
id_
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
fprintf
(
out_
,
"state "
);
TRY2
(
n
->
id_
->
accept
(
this
));
...
...
@@ -249,11 +249,11 @@ StatusTuple Printer::visit_state_decl_stmt_node(StateDeclStmtNode* n) {
// TRY2(n->id2_->accept(this));
//}
//TRY2(n->block_->accept(this));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_parser_state_stmt_node
(
ParserStateStmtNode
*
n
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_match_decl_stmt_node
(
MatchDeclStmtNode
*
n
)
{
...
...
@@ -268,7 +268,7 @@ StatusTuple Printer::visit_match_decl_stmt_node(MatchDeclStmtNode* n) {
}
fprintf
(
out_
,
") "
);
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_miss_decl_stmt_node
(
MissDeclStmtNode
*
n
)
{
...
...
@@ -283,7 +283,7 @@ StatusTuple Printer::visit_miss_decl_stmt_node(MissDeclStmtNode* n) {
}
fprintf
(
out_
,
") "
);
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_failure_decl_stmt_node
(
FailureDeclStmtNode
*
n
)
{
...
...
@@ -298,7 +298,7 @@ StatusTuple Printer::visit_failure_decl_stmt_node(FailureDeclStmtNode* n) {
}
fprintf
(
out_
,
") "
);
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_table_decl_stmt_node
(
TableDeclStmtNode
*
n
)
{
...
...
@@ -313,7 +313,7 @@ StatusTuple Printer::visit_table_decl_stmt_node(TableDeclStmtNode* n) {
fprintf
(
out_
,
"> "
);
TRY2
(
n
->
id_
->
accept
(
this
));
fprintf
(
out_
,
"(%zu)"
,
n
->
size_
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
Printer
::
visit_func_decl_stmt_node
(
FuncDeclStmtNode
*
n
)
{
...
...
@@ -328,7 +328,7 @@ StatusTuple Printer::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
}
fprintf
(
out_
,
") "
);
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
}
// namespace cc
...
...
src/cc/frontends/b/type_check.cc
View file @
13ebe8e8
...
...
@@ -37,7 +37,7 @@ StatusTuple TypeCheck::visit_block_stmt_node(BlockStmtNode *n) {
if
(
n
->
scope_
)
scopes_
->
pop_var
();
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_if_stmt_node
(
IfStmtNode
*
n
)
{
...
...
@@ -48,7 +48,7 @@ StatusTuple TypeCheck::visit_if_stmt_node(IfStmtNode *n) {
if
(
n
->
false_block_
)
{
TRY2
(
n
->
false_block_
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_onvalid_stmt_node
(
OnValidStmtNode
*
n
)
{
...
...
@@ -60,7 +60,7 @@ StatusTuple TypeCheck::visit_onvalid_stmt_node(OnValidStmtNode *n) {
if
(
n
->
else_block_
)
{
TRY2
(
n
->
else_block_
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_switch_stmt_node
(
SwitchStmtNode
*
n
)
{
...
...
@@ -71,7 +71,7 @@ StatusTuple TypeCheck::visit_switch_stmt_node(SwitchStmtNode *n) {
for
(
auto
it
=
n
->
block_
->
stmts_
.
begin
();
it
!=
n
->
block_
->
stmts_
.
end
();
++
it
)
{
/// @todo check for duplicates
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_case_stmt_node
(
CaseStmtNode
*
n
)
{
...
...
@@ -81,7 +81,7 @@ StatusTuple TypeCheck::visit_case_stmt_node(CaseStmtNode *n) {
return
mkstatus_
(
n
,
"Switch condition must be a numeric type"
);
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_ident_expr_node
(
IdentExprNode
*
n
)
{
...
...
@@ -131,7 +131,7 @@ StatusTuple TypeCheck::visit_ident_expr_node(IdentExprNode *n) {
n
->
bit_width_
=
n
->
sub_decl_
->
bit_width_
;
n
->
flags_
[
ExprNode
::
WRITE
]
=
true
;
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_assign_expr_node
(
AssignExprNode
*
n
)
{
...
...
@@ -151,7 +151,7 @@ StatusTuple TypeCheck::visit_assign_expr_node(AssignExprNode *n) {
return
mkstatus_
(
n
,
"Right-hand side of assignment must be a numeric type"
);
}
n
->
typeof_
=
ExprNode
::
VOID
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_packet_expr_node
(
PacketExprNode
*
n
)
{
...
...
@@ -172,20 +172,20 @@ StatusTuple TypeCheck::visit_packet_expr_node(PacketExprNode *n) {
n
->
bit_width_
=
sub_decl
->
bit_width_
;
}
n
->
flags_
[
ExprNode
::
WRITE
]
=
true
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_integer_expr_node
(
IntegerExprNode
*
n
)
{
n
->
typeof_
=
ExprNode
::
INTEGER
;
n
->
bit_width_
=
n
->
bits_
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_string_expr_node
(
StringExprNode
*
n
)
{
n
->
typeof_
=
ExprNode
::
STRING
;
n
->
flags_
[
ExprNode
::
IS_REF
]
=
true
;
n
->
bit_width_
=
n
->
val_
.
size
()
<<
3
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_binop_expr_node
(
BinopExprNode
*
n
)
{
...
...
@@ -207,7 +207,7 @@ StatusTuple TypeCheck::visit_binop_expr_node(BinopExprNode *n) {
default:
n
->
bit_width_
=
std
::
max
(
n
->
lhs_
->
bit_width_
,
n
->
rhs_
->
bit_width_
);
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_unop_expr_node
(
UnopExprNode
*
n
)
{
...
...
@@ -215,26 +215,26 @@ StatusTuple TypeCheck::visit_unop_expr_node(UnopExprNode *n) {
if
(
n
->
expr_
->
typeof_
!=
ExprNode
::
INTEGER
)
return
mkstatus_
(
n
,
"Unary operand must be a numeric type"
);
n
->
copy_type
(
*
n
->
expr_
);
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_bitop_expr_node
(
BitopExprNode
*
n
)
{
if
(
n
->
expr_
->
typeof_
!=
ExprNode
::
INTEGER
)
return
mkstatus_
(
n
,
"Bitop [] can only operate on numeric types"
);
n
->
typeof_
=
ExprNode
::
INTEGER
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_goto_expr_node
(
GotoExprNode
*
n
)
{
//n->id_->accept(this);
n
->
typeof_
=
ExprNode
::
VOID
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_return_expr_node
(
ReturnExprNode
*
n
)
{
TRY2
(
n
->
expr_
->
accept
(
this
));
n
->
typeof_
=
ExprNode
::
VOID
;
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
expect_method_arg
(
MethodCallExprNode
*
n
,
size_t
num
,
size_t
num_def_args
=
0
)
{
...
...
@@ -247,7 +247,7 @@ StatusTuple TypeCheck::expect_method_arg(MethodCallExprNode *n, size_t num, size
return
mkstatus_
(
n
,
"%s expected %d argument%s (%d default), %zu given"
,
n
->
id_
->
sub_name_
.
c_str
(),
num
,
num
==
1
?
""
:
"s"
,
num_def_args
,
n
->
args_
.
size
());
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
check_lookup_method
(
MethodCallExprNode
*
n
)
{
...
...
@@ -263,7 +263,7 @@ StatusTuple TypeCheck::check_lookup_method(MethodCallExprNode *n) {
n
->
block_
->
scope_
->
add
(
"_result"
,
result
.
get
());
n
->
block_
->
stmts_
.
insert
(
n
->
block_
->
stmts_
.
begin
(),
move
(
result
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
check_update_method
(
MethodCallExprNode
*
n
)
{
...
...
@@ -274,7 +274,7 @@ StatusTuple TypeCheck::check_update_method(MethodCallExprNode *n) {
TRY2
(
expect_method_arg
(
n
,
2
));
else
if
(
table
->
type_id
()
->
name_
==
"LPM"
)
TRY2
(
expect_method_arg
(
n
,
3
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
check_delete_method
(
MethodCallExprNode
*
n
)
{
...
...
@@ -285,7 +285,7 @@ StatusTuple TypeCheck::check_delete_method(MethodCallExprNode *n) {
TRY2
(
expect_method_arg
(
n
,
1
));
else
if
(
table
->
type_id
()
->
name_
==
"LPM"
)
{}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_method_call_expr_node
(
MethodCallExprNode
*
n
)
{
...
...
@@ -338,7 +338,7 @@ StatusTuple TypeCheck::visit_method_call_expr_node(MethodCallExprNode *n) {
return
mkstatus_
(
n
,
"%s does not allow trailing block statements"
,
n
->
id_
->
full_name
().
c_str
());
TRY2
(
n
->
block_
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_table_index_expr_node
(
TableIndexExprNode
*
n
)
{
...
...
@@ -358,12 +358,12 @@ StatusTuple TypeCheck::visit_table_index_expr_node(TableIndexExprNode *n) {
n
->
flags_
[
ExprNode
::
IS_REF
]
=
true
;
n
->
struct_type_
=
n
->
table_
->
leaf_type_
;
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_expr_stmt_node
(
ExprStmtNode
*
n
)
{
TRY2
(
n
->
expr_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_struct_variable_decl_stmt_node
(
StructVariableDeclStmtNode
*
n
)
{
...
...
@@ -398,7 +398,7 @@ StatusTuple TypeCheck::visit_struct_variable_decl_stmt_node(StructVariableDeclSt
TRY2
((
*
it
)
->
accept
(
this
));
}
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_integer_variable_decl_stmt_node
(
IntegerVariableDeclStmtNode
*
n
)
{
...
...
@@ -406,7 +406,7 @@ StatusTuple TypeCheck::visit_integer_variable_decl_stmt_node(IntegerVariableDecl
if
(
!
n
->
init_
.
empty
())
{
TRY2
(
n
->
init_
[
0
]
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_struct_decl_stmt_node
(
StructDeclStmtNode
*
n
)
{
...
...
@@ -414,16 +414,16 @@ StatusTuple TypeCheck::visit_struct_decl_stmt_node(StructDeclStmtNode *n) {
for
(
auto
it
=
n
->
stmts_
.
begin
();
it
!=
n
->
stmts_
.
end
();
++
it
)
{
TRY2
((
*
it
)
->
accept
(
this
));
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_parser_state_stmt_node
(
ParserStateStmtNode
*
n
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_state_decl_stmt_node
(
StateDeclStmtNode
*
n
)
{
if
(
!
n
->
id_
)
{
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
auto
s1
=
proto_scopes_
->
top_state
()
->
lookup
(
n
->
id_
->
name_
,
true
);
if
(
s1
)
{
...
...
@@ -478,7 +478,7 @@ StatusTuple TypeCheck::visit_state_decl_stmt_node(StateDeclStmtNode *n) {
scopes_
->
pop_state
();
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_match_decl_stmt_node
(
MatchDeclStmtNode
*
n
)
{
...
...
@@ -487,7 +487,7 @@ StatusTuple TypeCheck::visit_match_decl_stmt_node(MatchDeclStmtNode *n) {
TRY2
((
*
it
)
->
accept
(
this
));
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_miss_decl_stmt_node
(
MissDeclStmtNode
*
n
)
{
...
...
@@ -496,7 +496,7 @@ StatusTuple TypeCheck::visit_miss_decl_stmt_node(MissDeclStmtNode *n) {
TRY2
((
*
it
)
->
accept
(
this
));
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_failure_decl_stmt_node
(
FailureDeclStmtNode
*
n
)
{
...
...
@@ -505,7 +505,7 @@ StatusTuple TypeCheck::visit_failure_decl_stmt_node(FailureDeclStmtNode *n) {
TRY2
((
*
it
)
->
accept
(
this
));
}
TRY2
(
n
->
block_
->
accept
(
this
));
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_table_decl_stmt_node
(
TableDeclStmtNode
*
n
)
{
...
...
@@ -523,7 +523,7 @@ StatusTuple TypeCheck::visit_table_decl_stmt_node(TableDeclStmtNode *n) {
}
if
(
n
->
policy_id
()
->
name_
!=
"AUTO"
&&
n
->
policy_id
()
->
name_
!=
"NONE"
)
return
mkstatus_
(
n
,
"Unsupported policy type %s"
,
n
->
policy_id
()
->
c_str
());
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit_func_decl_stmt_node
(
FuncDeclStmtNode
*
n
)
{
...
...
@@ -538,7 +538,7 @@ StatusTuple TypeCheck::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
scopes_
->
push_state
(
n
->
scope_
);
TRY2
(
n
->
block_
->
accept
(
this
));
scopes_
->
pop_state
();
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
StatusTuple
TypeCheck
::
visit
(
Node
*
root
)
{
...
...
@@ -549,14 +549,14 @@ StatusTuple TypeCheck::visit(Node *root) {
// // packet data in bpf socket
// if (scopes_->top_struct()->lookup("_skbuff", true)) {
// return
mkstatus
(-1, "_skbuff already defined");
// return
StatusTuple
(-1, "_skbuff already defined");
// }
// auto skb_type = make_unique<StructDeclStmtNode>(make_unique<IdentExprNode>("_skbuff"));
// scopes_->top_struct()->add("_skbuff", skb_type.get());
// b->stmts_.push_back(move(skb_type));
// if (scopes_->current_var()->lookup("skb", true)) {
// return
mkstatus
(-1, "skb already defined");
// return
StatusTuple
(-1, "skb already defined");
// }
// auto skb = make_unique<StructVariableDeclStmtNode>(make_unique<IdentExprNode>("_skbuff"),
// make_unique<IdentExprNode>("skb"));
...
...
@@ -577,9 +577,9 @@ StatusTuple TypeCheck::visit(Node *root) {
for
(
auto
it
=
errors_
.
begin
();
it
!=
errors_
.
end
();
++
it
)
{
fprintf
(
stderr
,
"%s
\n
"
,
it
->
c_str
());
}
return
mkstatus
(
-
1
,
errors_
.
begin
()
->
c_str
());
return
StatusTuple
(
-
1
,
errors_
.
begin
()
->
c_str
());
}
return
mkstatus
(
0
);
return
StatusTuple
(
0
);
}
}
// namespace cc
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment