-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf-translate.cxx
2133 lines (1883 loc) · 57.7 KB
/
bpf-translate.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// bpf translation pass
// Copyright (C) 2016 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "config.h"
#include "bpf-internal.h"
#include "staptree.h"
#include "elaborate.h"
#include "session.h"
#include "translator-output.h"
#include "tapsets.h"
#include <sstream>
#include <unistd.h>
#include <fcntl.h>
extern "C" {
#include <libelf.h>
/* Unfortunately strtab manipulation functions were only officially added
to elfutils libdw in 0.167. Before that there were internal unsupported
ebl variants. While libebl.h isn't supported we'll try to use it anyway
if the elfutils we build against is too old. */
#include <elfutils/version.h>
#if _ELFUTILS_PREREQ (0, 167)
#include <elfutils/libdwelf.h>
typedef Dwelf_Strent Stap_Strent;
typedef Dwelf_Strtab Stap_Strtab;
#define stap_strtab_init dwelf_strtab_init
#define stap_strtab_add(X,Y) dwelf_strtab_add(X,Y)
#define stap_strtab_free dwelf_strtab_free
#define stap_strtab_finalize dwelf_strtab_finalize
#define stap_strent_offset dwelf_strent_off
#else
#include <elfutils/libebl.h>
typedef Ebl_Strent Stap_Strent;
typedef Ebl_Strtab Stap_Strtab;
#define stap_strtab_init ebl_strtabinit
#define stap_strtab_add(X,Y) ebl_strtabadd(X,Y,0)
#define stap_strtab_free ebl_strtabfree
#define stap_strtab_finalize ebl_strtabfinalize
#define stap_strent_offset ebl_strtaboffset
#endif
#include <linux/version.h>
#include <asm/ptrace.h>
}
#ifndef EM_BPF
#define EM_BPF 0xeb9f
#endif
#ifndef R_BPF_MAP_FD
#define R_BPF_MAP_FD 1
#endif
namespace bpf {
struct side_effects_visitor : public expression_visitor
{
bool side_effects;
side_effects_visitor() : side_effects(false) { }
void visit_expression(expression *) { }
void visit_pre_crement(pre_crement *) { side_effects = true; }
void visit_post_crement(post_crement *) { side_effects = true; }
void visit_assignment (assignment *) { side_effects = true; }
void visit_functioncall (functioncall *) { side_effects = true; }
void visit_print_format (print_format *) { side_effects = true; }
void visit_stat_op (stat_op *) { side_effects = true; }
void visit_hist_op (hist_op *) { side_effects = true; }
};
static bool
has_side_effects (expression *e)
{
side_effects_visitor t;
e->visit (&t);
return t.side_effects;
}
struct bpf_unparser : public throwing_visitor
{
// The visitor class isn't as helpful as it might be. As a consequence,
// the RESULT member is set after visiting any expression type. Use the
// emit_expr helper to return the result properly.
value *result;
// The program into which we are emitting code.
program &this_prog;
globals &glob;
value *this_in_arg0;
// The "current" block into which we are currently emitting code.
insn_append_inserter this_ins;
void set_block(block *b)
{ this_ins.b = b; this_ins.i = b->last; }
void clear_block()
{ this_ins.b = NULL; this_ins.i = NULL; }
bool in_block() const
{ return this_ins.b != NULL; }
// Destinations for "break", "continue", and "return" respectively.
std::vector<block *> loop_break;
std::vector<block *> loop_cont;
std::vector<block *> func_return;
std::vector<value *> func_return_val;
std::vector<functiondecl *> func_calls;
// Local variable declarations.
typedef std::unordered_map<vardecl *, value *> locals_map;
locals_map *this_locals;
// Return 0.
block *ret0_block;
block *exit_block;
block *get_ret0_block();
block *get_exit_block();
virtual void visit_block (::block *s);
virtual void visit_embeddedcode (embeddedcode *s);
virtual void visit_null_statement (null_statement *s);
virtual void visit_expr_statement (expr_statement *s);
virtual void visit_if_statement (if_statement* s);
virtual void visit_for_loop (for_loop* s);
virtual void visit_return_statement (return_statement* s);
virtual void visit_break_statement (break_statement* s);
virtual void visit_continue_statement (continue_statement* s);
virtual void visit_delete_statement (delete_statement* s);
virtual void visit_literal_number (literal_number* e);
virtual void visit_binary_expression (binary_expression* e);
virtual void visit_unary_expression (unary_expression* e);
virtual void visit_pre_crement (pre_crement* e);
virtual void visit_post_crement (post_crement* e);
virtual void visit_logical_or_expr (logical_or_expr* e);
virtual void visit_logical_and_expr (logical_and_expr* e);
virtual void visit_comparison (comparison* e);
virtual void visit_ternary_expression (ternary_expression* e);
virtual void visit_assignment (assignment* e);
virtual void visit_symbol (symbol* e);
virtual void visit_arrayindex (arrayindex *e);
virtual void visit_functioncall (functioncall* e);
virtual void visit_print_format (print_format* e);
virtual void visit_target_register (target_register* e);
virtual void visit_target_deref (target_deref* e);
void emit_stmt(statement *s);
void emit_mov(value *d, value *s);
void emit_jmp(block *b);
void emit_cond(expression *e, block *t, block *f);
void emit_store(expression *dest, value *src);
value *emit_expr(expression *e);
value *emit_bool(expression *e);
value *parse_reg(const std::string &str, embeddedcode *s);
locals_map *new_locals(const std::vector<vardecl *> &);
bpf_unparser (program &c, globals &g);
virtual ~bpf_unparser ();
};
bpf_unparser::bpf_unparser(program &p, globals &g)
: throwing_visitor ("unhandled statement type"),
result(NULL), this_prog(p), glob(g), this_locals(NULL),
ret0_block(NULL), exit_block(NULL)
{ }
bpf_unparser::~bpf_unparser()
{
delete this_locals;
}
bpf_unparser::locals_map *
bpf_unparser::new_locals(const std::vector<vardecl *> &vars)
{
locals_map *m = new locals_map;
for (std::vector<vardecl *>::const_iterator i = vars.begin ();
i != vars.end (); ++i)
{
const locals_map::value_type v (*i, this_prog.new_reg());
auto ok = m->insert (v);
assert (ok.second);
}
return m;
}
block *
bpf_unparser::get_exit_block()
{
if (exit_block)
return exit_block;
block *b = this_prog.new_block();
insn_append_inserter ins(b);
this_prog.mk_exit(ins);
exit_block = b;
return b;
}
block *
bpf_unparser::get_ret0_block()
{
if (ret0_block)
return ret0_block;
block *b = this_prog.new_block();
insn_append_inserter ins(b);
this_prog.mk_mov(ins, this_prog.lookup_reg(BPF_REG_0), this_prog.new_imm(0));
b->fallthru = new edge(b, get_exit_block());
ret0_block = b;
return b;
}
void
bpf_unparser::emit_stmt(statement *s)
{
if (s)
s->visit (this);
}
value *
bpf_unparser::emit_expr(expression *e)
{
e->visit (this);
value *v = result;
result = NULL;
return v;
}
void
bpf_unparser::emit_mov(value *d, value *s)
{
this_prog.mk_mov(this_ins, d, s);
}
void
bpf_unparser::emit_jmp(block *b)
{
// Begin by hoping that we can simply place the destination as fallthru.
// If this assumption doesn't hold, it'll be fixed by reorder_blocks.
block *this_block = this_ins.get_block ();
this_block->fallthru = new edge(this_block, b);
clear_block ();
}
void
bpf_unparser::emit_cond(expression *e, block *t_dest, block *f_dest)
{
condition cond;
value *s0, *s1;
// Look for and handle logical operators first.
if (logical_or_expr *l = dynamic_cast<logical_or_expr *>(e))
{
block *cont_block = this_prog.new_block ();
emit_cond (l->left, t_dest, cont_block);
set_block (cont_block);
emit_cond (l->right, t_dest, f_dest);
return;
}
if (logical_and_expr *l = dynamic_cast<logical_and_expr *>(e))
{
block *cont_block = this_prog.new_block ();
emit_cond (l->left, cont_block, f_dest);
set_block (cont_block);
emit_cond (l->right, t_dest, f_dest);
return;
}
if (unary_expression *u = dynamic_cast<unary_expression *>(e))
if (u->op == "!")
{
emit_cond (u->operand, f_dest, t_dest);
return;
}
// What is left must generate a comparison + conditional branch.
if (comparison *c = dynamic_cast<comparison *>(e))
{
s0 = emit_expr (c->left);
s1 = emit_expr (c->right);
if (c->op == "==")
cond = EQ;
else if (c->op == "!=")
cond = NE;
else if (c->op == "<")
cond = LT;
else if (c->op == "<=")
cond = LE;
else if (c->op == ">")
cond = GT;
else if (c->op == ">=")
cond = GE;
else
throw SEMANTIC_ERROR (_("unhandled comparison operator"), e->tok);
}
else
{
binary_expression *bin = dynamic_cast<binary_expression *>(e);
if (bin && bin->op == "&")
{
s0 = emit_expr (bin->left);
s1 = emit_expr (bin->right);
cond = TEST;
}
else
{
// Fall back to E != 0.
s0 = emit_expr (e);
s1 = this_prog.new_imm(0);
cond = NE;
}
}
this_prog.mk_jcond (this_ins, cond, s1, s0, t_dest, f_dest);
clear_block ();
}
value *
bpf_unparser::emit_bool (expression *e)
{
block *else_block = this_prog.new_block ();
block *join_block = this_prog.new_block ();
value *r = this_prog.new_reg();
emit_mov (r, this_prog.new_imm(1));
emit_cond (e, join_block, else_block);
set_block (else_block);
emit_mov (r, this_prog.new_imm(0));
emit_jmp (join_block);
set_block(join_block);
return r;
}
void
bpf_unparser::emit_store(expression *e, value *val)
{
if (symbol *s = dynamic_cast<symbol *>(e))
{
vardecl *var = s->referent;
assert (var->arity == 0);
auto g = glob.globals.find (var);
if (g != glob.globals.end())
{
value *frame = this_prog.lookup_reg(BPF_REG_10);
int key_ofs, val_ofs;
switch (var->type)
{
case pe_long:
val_ofs = -8;
this_prog.mk_st(this_ins, BPF_DW, frame, val_ofs, val);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_3),
frame, this_prog.new_imm(val_ofs));
break;
// ??? pe_string
// ??? pe_stats
default:
goto err;
}
key_ofs = val_ofs - 4;
this_prog.mk_st(this_ins, BPF_W, frame, key_ofs,
this_prog.new_imm(g->second.second));
this_prog.use_tmp_space(-key_ofs);
this_prog.load_map(this_ins, this_prog.lookup_reg(BPF_REG_1),
g->second.first);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_2),
frame, this_prog.new_imm(key_ofs));
emit_mov(this_prog.lookup_reg(BPF_REG_4), this_prog.new_imm(0));
this_prog.mk_call(this_ins, BPF_FUNC_map_update_elem, 4);
return;
}
auto i = this_locals->find (var);
if (i != this_locals->end ())
{
emit_mov (i->second, val);
return;
}
}
else if (arrayindex *a = dynamic_cast<arrayindex *>(e))
{
if (symbol *a_sym = dynamic_cast<symbol *>(a->base))
{
vardecl *v = a_sym->referent;
int key_ofs, val_ofs;
if (v->arity != 1)
throw SEMANTIC_ERROR(_("unhandled multi-dimensional array"), v->tok);
auto g = glob.globals.find(v);
if (g == glob.globals.end())
throw SEMANTIC_ERROR(_("unknown array variable"), v->tok);
value *idx = emit_expr(a->indexes[0]);
value *frame = this_prog.lookup_reg(BPF_REG_10);
switch (v->index_types[0])
{
case pe_long:
key_ofs = -8;
this_prog.mk_st(this_ins, BPF_DW, frame, key_ofs, idx);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_2),
frame, this_prog.new_imm(key_ofs));
break;
// ??? pe_string
default:
throw SEMANTIC_ERROR(_("unhandled index type"), e->tok);
}
switch (v->type)
{
case pe_long:
val_ofs = key_ofs - 8;
this_prog.mk_st(this_ins, BPF_DW, frame, val_ofs, val);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_3),
frame, this_prog.new_imm(val_ofs));
break;
// ??? pe_string
default:
throw SEMANTIC_ERROR(_("unhandled array type"), v->tok);
}
this_prog.load_map(this_ins, this_prog.lookup_reg(BPF_REG_1),
g->second.first);
this_prog.mk_call(this_ins, BPF_FUNC_map_update_elem, 4);
return;
}
}
err:
throw SEMANTIC_ERROR (_("unknown lvalue"), e->tok);
}
void
bpf_unparser::visit_block (::block *s)
{
unsigned n = s->statements.size();
for (unsigned i = 0; i < n; ++i)
emit_stmt (s->statements[i]);
}
value *
bpf_unparser::parse_reg(const std::string &str, embeddedcode *s)
{
if (str == "$$")
{
if (func_return.empty ())
throw SEMANTIC_ERROR (_("no return value outside function"), s->tok);
return func_return_val.back();
}
else if (str[0] == '$')
{
std::string var = str.substr(1);
for (auto i = this_locals->begin(); i != this_locals->end(); ++i)
{
vardecl *v = i->first;
if (var == v->unmangled_name)
return i->second;
}
throw SEMANTIC_ERROR (_("unknown variable"), s->tok);
}
else
{
unsigned long num = stoul(str, 0, 0);
if (num > 10)
throw SEMANTIC_ERROR (_("invalid bpf register"), s->tok);
return this_prog.lookup_reg(num);
}
}
void
bpf_unparser::visit_embeddedcode (embeddedcode *s)
{
std::string strip;
{
const interned_string &code = s->code;
unsigned n = code.size();
bool in_comment = false;
for (unsigned i = 0; i < n; ++i)
{
char c = code[i];
if (isspace(c))
continue;
if (in_comment)
{
if (c == '*' && code[i + 1] == '/')
++i, in_comment = false;
}
else if (c == '/' && code[i + 1] == '*')
++i, in_comment = true;
else
strip += c;
}
}
std::istringstream ii (strip);
ii >> std::setbase(0);
while (true)
{
unsigned code;
char s1, s2, s3, s4;
char dest_b[256], src1_b[256];
int64_t off, imm;
ii >> code >> s1;
ii.get(dest_b, sizeof(dest_b), ',') >> s2;
ii.get(src1_b, sizeof(src1_b), ',') >> s3;
ii >> off >> s4 >> imm;
if (ii.fail() || s1 != ',' || s2 != ',' || s3 != ',' || s4 != ',')
throw SEMANTIC_ERROR (_("invalid bpf embeddedcode syntax"), s->tok);
if (code > 0xff)
throw SEMANTIC_ERROR (_("invalid bpf code"), s->tok);
bool r_dest = false, r_src0 = false, r_src1 = false, i_src1 = false;
switch (BPF_CLASS (code))
{
case BPF_LDX:
r_dest = r_src1 = true;
break;
case BPF_STX:
r_src0 = r_src1 = true;
break;
case BPF_ST:
r_src0 = i_src1 = true;
break;
case BPF_ALU:
case BPF_ALU64:
r_dest = true;
if (code & BPF_X)
r_src1 = true;
else
i_src1 = true;
switch (BPF_OP (code))
{
case BPF_NEG:
case BPF_MOV:
break;
case BPF_END:
/* X/K bit repurposed as LE/BE. */
i_src1 = false, r_src1 = true;
break;
default:
r_src0 = true;
}
break;
case BPF_JMP:
switch (BPF_OP (code))
{
case BPF_EXIT:
break;
case BPF_CALL:
i_src1 = true;
break;
default:
throw SEMANTIC_ERROR (_("invalid branch in bpf code"), s->tok);
}
break;
default:
throw SEMANTIC_ERROR (_("unknown opcode in bpf code"), s->tok);
}
std::string dest(dest_b);
value *v_dest = NULL;
if (r_dest || r_src0)
v_dest = parse_reg(dest, s);
else if (dest != "0")
throw SEMANTIC_ERROR (_("invalid register field in bpf code"), s->tok);
std::string src1(src1_b);
value *v_src1 = NULL;
if (r_src1)
v_src1 = parse_reg(src1, s);
else
{
if (src1 != "0")
throw SEMANTIC_ERROR (_("invalid register field in bpf code"), s->tok);
if (i_src1)
v_src1 = this_prog.new_imm(imm);
else if (imm != 0)
throw SEMANTIC_ERROR (_("invalid immediate field in bpf code"), s->tok);
}
if (off != (int16_t)off)
throw SEMANTIC_ERROR (_("offset field out of range in bpf code"), s->tok);
insn *i = this_ins.new_insn();
i->code = code;
i->dest = (r_dest ? v_dest : NULL);
i->src0 = (r_src0 ? v_dest : NULL);
i->src1 = v_src1;
i->off = off;
ii >> s1;
if (ii.eof())
break;
if (s1 != ';')
throw SEMANTIC_ERROR (_("invalid bpf embeddedcode syntax"), s->tok);
}
}
void
bpf_unparser::visit_null_statement (null_statement *)
{ }
void
bpf_unparser::visit_expr_statement (expr_statement *s)
{
(void) emit_expr (s->value);
}
void
bpf_unparser::visit_if_statement (if_statement* s)
{
block *then_block = this_prog.new_block ();
block *join_block = this_prog.new_block ();
if (s->elseblock)
{
block *else_block = this_prog.new_block ();
emit_cond (s->condition, then_block, else_block);
set_block (then_block);
emit_stmt (s->thenblock);
if (in_block ())
emit_jmp (join_block);
set_block (else_block);
emit_stmt (s->elseblock);
if (in_block ())
emit_jmp (join_block);
}
else
{
emit_cond (s->condition, then_block, join_block);
set_block (then_block);
emit_stmt (s->thenblock);
if (in_block ())
emit_jmp (join_block);
}
set_block (join_block);
}
void
bpf_unparser::visit_for_loop (for_loop* s)
{
block *body_block = this_prog.new_block ();
block *iter_block = this_prog.new_block ();
block *test_block = this_prog.new_block ();
block *join_block = this_prog.new_block ();
emit_stmt (s->init);
if (!in_block ())
return;
emit_jmp (test_block);
loop_break.push_back (join_block);
loop_cont.push_back (iter_block);
set_block (body_block);
emit_stmt (s->block);
if (in_block ())
emit_jmp (iter_block);
loop_cont.pop_back ();
loop_break.pop_back ();
set_block (iter_block);
emit_stmt (s->incr);
if (in_block ())
emit_jmp (test_block);
set_block (test_block);
emit_cond (s->cond, body_block, join_block);
set_block (join_block);
}
void
bpf_unparser::visit_break_statement (break_statement* s)
{
if (loop_break.empty ())
throw SEMANTIC_ERROR (_("cannot 'break' outside loop"), s->tok);
emit_jmp (loop_break.back ());
}
void
bpf_unparser:: visit_continue_statement (continue_statement* s)
{
if (loop_cont.empty ())
throw SEMANTIC_ERROR (_("cannot 'continue' outside loop"), s->tok);
emit_jmp (loop_cont.back ());
}
void
bpf_unparser::visit_return_statement (return_statement* s)
{
if (func_return.empty ())
throw SEMANTIC_ERROR (_("cannot 'return' outside function"), s->tok);
assert (!func_return_val.empty ());
emit_mov (func_return_val.back (), emit_expr (s->value));
emit_jmp (func_return.back ());
}
void
bpf_unparser::visit_delete_statement (delete_statement *s)
{
expression *e = s->value;
if (symbol *s = dynamic_cast<symbol *>(e))
{
vardecl *var = s->referent;
if (var->arity != 0)
throw SEMANTIC_ERROR (_("unimplemented delete of array"), s->tok);
auto g = glob.globals.find (var);
if (g != glob.globals.end())
{
value *frame = this_prog.lookup_reg(BPF_REG_10);
int key_ofs, val_ofs;
switch (var->type)
{
case pe_long:
val_ofs = -8;
this_prog.mk_st(this_ins, BPF_DW, frame, val_ofs,
this_prog.new_imm(0));
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_3),
frame, this_prog.new_imm(val_ofs));
break;
// ??? pe_string
default:
goto err;
}
key_ofs = val_ofs - 4;
this_prog.mk_st(this_ins, BPF_W, frame, key_ofs,
this_prog.new_imm(g->second.second));
this_prog.use_tmp_space(-key_ofs);
this_prog.load_map(this_ins, this_prog.lookup_reg(BPF_REG_1),
g->second.first);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_2),
frame, this_prog.new_imm(key_ofs));
emit_mov(this_prog.lookup_reg(BPF_REG_4), this_prog.new_imm(0));
this_prog.mk_call(this_ins, BPF_FUNC_map_update_elem, 4);
return;
}
auto i = this_locals->find (var);
if (i != this_locals->end ())
{
emit_mov (i->second, this_prog.new_imm(0));
return;
}
}
else if (arrayindex *a = dynamic_cast<arrayindex *>(e))
{
if (symbol *a_sym = dynamic_cast<symbol *>(a->base))
{
vardecl *v = a_sym->referent;
int key_ofs;
if (v->arity != 1)
throw SEMANTIC_ERROR(_("unhandled multi-dimensional array"), v->tok);
auto g = glob.globals.find(v);
if (g == glob.globals.end())
throw SEMANTIC_ERROR(_("unknown array variable"), v->tok);
value *idx = emit_expr(a->indexes[0]);
value *frame = this_prog.lookup_reg(BPF_REG_10);
switch (v->index_types[0])
{
case pe_long:
key_ofs = -8;
this_prog.mk_st(this_ins, BPF_DW, frame, key_ofs, idx);
this_prog.mk_binary(this_ins, BPF_ADD,
this_prog.lookup_reg(BPF_REG_2),
frame, this_prog.new_imm(key_ofs));
break;
// ??? pe_string
default:
throw SEMANTIC_ERROR(_("unhandled index type"), e->tok);
}
this_prog.load_map(this_ins, this_prog.lookup_reg(BPF_REG_1),
g->second.first);
this_prog.mk_call(this_ins, BPF_FUNC_map_delete_elem, 2);
return;
}
}
err:
throw SEMANTIC_ERROR (_("unknown lvalue"), e->tok);
}
void
bpf_unparser::visit_literal_number (literal_number* e)
{
result = this_prog.new_imm(e->value);
}
void
bpf_unparser::visit_binary_expression (binary_expression* e)
{
int code;
if (e->op == "+")
code = BPF_ADD;
else if (e->op == "-")
code = BPF_SUB;
else if (e->op == "*")
code = BPF_MUL;
else if (e->op == "&")
code = BPF_AND;
else if (e->op == "|")
code = BPF_OR;
else if (e->op == "^")
code = BPF_XOR;
else if (e->op == "<<")
code = BPF_LSH;
else if (e->op == ">>")
code = BPF_ARSH;
else if (e->op == ">>>")
code = BPF_RSH;
else if (e->op == "/")
code = BPF_DIV;
else if (e->op == "%")
code = BPF_MOD;
else
throw SEMANTIC_ERROR (_("unhandled binary operator"), e->tok);
value *s0 = emit_expr (e->left);
value *s1 = emit_expr (e->right);
value *d = this_prog.new_reg ();
this_prog.mk_binary (this_ins, code, d, s0, s1);
result = d;
}
void
bpf_unparser::visit_unary_expression (unary_expression* e)
{
if (e->op == "-")
{
// Note that negative literals appear in the script langauge as
// unary negations over positive literals.
if (literal_number *lit = dynamic_cast<literal_number *>(e))
result = this_prog.new_imm(-(uint64_t)lit->value);
else
{
value *s = emit_expr (e->operand);
value *d = this_prog.new_reg();
this_prog.mk_unary (this_ins, BPF_NEG, d, s);
result = d;
}
}
else if (e->op == "~")
{
value *s1 = this_prog.new_imm(-1);
value *s0 = emit_expr (e->operand);
value *d = this_prog.new_reg ();
this_prog.mk_binary (this_ins, BPF_XOR, d, s0, s1);
result = d;
}
else if (e->op == "!")
result = emit_bool (e);
else if (e->op == "+")
result = emit_expr (e->operand);
else
throw SEMANTIC_ERROR (_("unhandled unary operator"), e->tok);
}
void
bpf_unparser::visit_pre_crement (pre_crement* e)
{
int dir;
if (e->op == "++")
dir = 1;
else if (e->op == "--")
dir = -1;
else
throw SEMANTIC_ERROR (_("unhandled crement operator"), e->tok);
value *c = this_prog.new_imm(dir);
value *v = emit_expr (e->operand);
this_prog.mk_binary (this_ins, BPF_ADD, v, v, c);
emit_store (e->operand, v);
result = v;
}
void
bpf_unparser::visit_post_crement (post_crement* e)
{
int dir;
if (e->op == "++")
dir = 1;
else if (e->op == "--")
dir = -1;
else
throw SEMANTIC_ERROR (_("unhandled crement operator"), e->tok);
value *c = this_prog.new_imm(dir);
value *r = this_prog.new_reg ();
value *v = emit_expr (e->operand);
emit_mov (r, v);
this_prog.mk_binary (this_ins, BPF_ADD, v, v, c);
emit_store (e->operand, v);
result = r;
}
void
bpf_unparser::visit_logical_or_expr (logical_or_expr* e)
{
result = emit_bool (e);
}
void
bpf_unparser::visit_logical_and_expr (logical_and_expr* e)
{
result = emit_bool (e);
}
void
bpf_unparser::visit_comparison (comparison* e)
{
result = emit_bool (e);
}
void
bpf_unparser::visit_ternary_expression (ternary_expression* e)
{
block *join_block = this_prog.new_block ();
value *r = this_prog.new_reg ();
if (!has_side_effects (e->truevalue))
{
block *else_block = this_prog.new_block ();
emit_mov (r, emit_expr (e->truevalue));
emit_cond (e->cond, join_block, else_block);
set_block (else_block);
emit_mov (r, emit_expr (e->falsevalue));
emit_jmp (join_block);
}
else if (!has_side_effects (e->falsevalue))
{
block *then_block = this_prog.new_block ();
emit_mov (r, emit_expr (e->falsevalue));
emit_cond (e->cond, join_block, then_block);
set_block (then_block);
emit_mov (r, emit_expr (e->truevalue));
emit_jmp (join_block);
}
else
{
block *then_block = this_prog.new_block ();
block *else_block = this_prog.new_block ();
emit_cond (e->cond, then_block, else_block);
set_block (then_block);
emit_mov (r, emit_expr (e->truevalue));
emit_jmp (join_block);
set_block (else_block);
emit_mov (r, emit_expr (e->falsevalue));
emit_jmp (join_block);
}
set_block (join_block);
result = r;
}
void
bpf_unparser::visit_assignment (assignment* e)
{
value *r = emit_expr (e->right);