Coverage Report

Created: 2026-10-31 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zeek/src/script_opt/ZAM/Stmt.cc
Line
Count
Source
1
// See the file "COPYING" in the main distribution directory for copyright.
2
3
// Methods for traversing Stmt AST nodes to generate ZAM code.
4
5
#include "zeek/IPAddr.h"
6
#include "zeek/Reporter.h"
7
#include "zeek/ZeekString.h"
8
#include "zeek/script_opt/ZAM/BuiltIn.h"
9
#include "zeek/script_opt/ZAM/Compile.h"
10
11
namespace zeek::detail {
12
13
0
ZAMStmt ZAMCompiler::CompileStmt(const Stmt* s) {
14
0
    auto loc = s->GetLocationInfo();
15
0
    ASSERT(loc->FirstLine() != 0 || s->Tag() == STMT_NULL);
16
0
    auto loc_copy = std::make_shared<Location>(loc->FileName(), loc->FirstLine(), loc->LastLine());
17
0
    ASSERT(! AST_blocks || s->Tag() == STMT_NULL || AST_blocks->HaveExpDesc(loc_copy.get()));
18
0
    auto loc_parent = ZAM::curr_loc->Parent();
19
0
    ZAM::curr_loc = std::make_shared<ZAMLocInfo>(ZAM::curr_func, std::move(loc_copy), ZAM::curr_loc->Parent());
20
21
0
    switch ( s->Tag() ) {
22
0
        case STMT_PRINT: return CompilePrint(static_cast<const PrintStmt*>(s));
23
24
0
        case STMT_EXPR: return CompileExpr(static_cast<const ExprStmt*>(s));
25
26
0
        case STMT_IF: return CompileIf(static_cast<const IfStmt*>(s));
27
28
0
        case STMT_SWITCH: return CompileSwitch(static_cast<const SwitchStmt*>(s));
29
30
0
        case STMT_EVENT: {
31
0
            auto es = static_cast<const EventStmt*>(s);
32
0
            auto e = static_cast<const EventExpr*>(es->StmtExpr());
33
0
            return CompileExpr(e);
34
0
        }
35
36
0
        case STMT_WHILE: return CompileWhile(static_cast<const WhileStmt*>(s));
37
38
0
        case STMT_FOR: return CompileFor(static_cast<const ForStmt*>(s));
39
40
0
        case STMT_RETURN: return CompileReturn(static_cast<const ReturnStmt*>(s));
41
42
0
        case STMT_CATCH_RETURN: return CompileCatchReturn(static_cast<const CatchReturnStmt*>(s));
43
44
0
        case STMT_LIST: return CompileStmts(static_cast<const StmtList*>(s));
45
46
0
        case STMT_INIT: return CompileInit(static_cast<const InitStmt*>(s));
47
48
0
        case STMT_WHEN: return CompileWhen(static_cast<const WhenStmt*>(s));
49
50
0
        case STMT_ASSERT: return CompileAssert(static_cast<const AssertStmt*>(s));
51
52
0
        case STMT_NULL: return EmptyStmt();
53
54
0
        case STMT_CHECK_ANY_LEN: {
55
0
            auto cs = static_cast<const CheckAnyLenStmt*>(s);
56
0
            auto n = cs->StmtExpr()->AsNameExpr();
57
0
            auto expected_len = cs->ExpectedLen();
58
0
            return CheckAnyLenVi(n, expected_len);
59
0
        }
60
61
0
        case STMT_NEXT: return CompileNext();
62
63
0
        case STMT_BREAK: return CompileBreak();
64
65
0
        case STMT_FALLTHROUGH: return CompileFallThrough();
66
67
0
        default: reporter->InternalError("bad statement type in ZAMCompile::CompileStmt");
68
0
    }
69
0
}
70
71
0
ZAMStmt ZAMCompiler::CompilePrint(const PrintStmt* ps) {
72
0
    auto& l = ps->ExprListPtr();
73
74
0
    if ( l->Exprs().length() == 1 ) { // special-case the common situation of printing just 1 item
75
0
        auto e0 = l->Exprs()[0];
76
0
        if ( e0->Tag() == EXPR_NAME )
77
0
            return Print1V(e0->AsNameExpr());
78
0
        else
79
0
            return Print1C(e0->AsConstExpr());
80
0
    }
81
82
0
    return PrintO(BuildVals(l).get());
83
0
}
84
85
0
ZAMStmt ZAMCompiler::CompileExpr(const ExprStmt* es) {
86
0
    auto e = es->StmtExprPtr();
87
88
0
    if ( e->Tag() == EXPR_CALL )
89
0
        return Call(es);
90
91
0
    if ( e->Tag() == EXPR_ASSIGN && e->GetOp2()->Tag() == EXPR_CALL )
92
0
        return AssignToCall(es);
93
94
0
    return CompileExpr(e);
95
0
}
96
97
0
ZAMStmt ZAMCompiler::CompileIf(const IfStmt* is) {
98
0
    auto e = is->StmtExprPtr();
99
0
    auto block1 = is->TrueBranch();
100
0
    auto block2 = is->FalseBranch();
101
102
0
    if ( block1->Tag() == STMT_NULL )
103
0
        block1 = nullptr;
104
105
0
    if ( block2->Tag() == STMT_NULL )
106
0
        block2 = nullptr;
107
108
0
    if ( ! block1 && ! block2 )
109
        // No need to evaluate conditional as it ought to be
110
        // side-effect free in reduced form.
111
0
        return EmptyStmt();
112
113
0
    if ( ! block1 ) {
114
        // See if we're able to invert the conditional.  If not,
115
        // then IfElse() will need to deal with inverting the test.
116
        // But we try here first, since some conditionals blow
117
        // up into zillions of different operators depending
118
        // on the type of their operands, so it's much simpler to
119
        // deal with them now.
120
0
        if ( e->InvertSense() ) {
121
0
            block1 = block2;
122
0
            block2 = nullptr;
123
0
        }
124
0
    }
125
126
0
    return IfElse(e.get(), block1, block2);
127
0
}
128
129
0
ZAMStmt ZAMCompiler::IfElse(const Expr* e, const Stmt* s1, const Stmt* s2) {
130
0
    ZAMStmt cond_stmt = EmptyStmt();
131
0
    int branch_v;
132
133
0
    if ( e->Tag() == EXPR_NAME ) {
134
0
        auto n = e->AsNameExpr();
135
136
0
        ZOp op = (s1 && s2) ? OP_IF_ELSE_Vb : (s1 ? OP_IF_Vb : OP_IF_NOT_Vb);
137
138
0
        ZInstI cond(op, FrameSlot(n), 0);
139
0
        cond_stmt = AddInst(cond);
140
0
        branch_v = 2;
141
0
    }
142
0
    else
143
0
        cond_stmt = GenCond(e, branch_v);
144
145
0
    AddCFT(insts1.back(), CFT_IF);
146
147
0
    if ( s1 ) {
148
0
        auto s1_end = CompileStmt(s1);
149
0
        AddCFT(insts1.back(), CFT_BLOCK_END);
150
151
0
        if ( s2 ) {
152
0
            auto branch_after_s1 = GoToStub();
153
0
            auto else_start = insts1.size();
154
0
            auto s2_end = CompileStmt(s2);
155
156
0
            SetV(cond_stmt, GoToTargetBeyond(branch_after_s1), branch_v);
157
0
            SetGoTo(branch_after_s1, GoToTargetBeyond(s2_end));
158
159
0
            if ( else_start < insts1.size() )
160
                // There was a non-empty else branch.
161
0
                AddCFT(insts1[else_start], CFT_ELSE);
162
163
0
            AddCFT(insts1.back(), CFT_BLOCK_END);
164
165
0
            return s2_end;
166
0
        }
167
168
0
        else {
169
0
            SetV(cond_stmt, GoToTargetBeyond(s1_end), branch_v);
170
0
            return s1_end;
171
0
        }
172
0
    }
173
174
    // Only the else clause is non-empty.
175
0
    auto s2_end = CompileStmt(s2);
176
0
    AddCFT(insts1.back(), CFT_BLOCK_END);
177
178
    // For complex conditionals, we need to invert their sense since
179
    // we're switching to "if ( ! cond ) s2".
180
0
    auto z = insts1[cond_stmt.stmt_num];
181
182
0
    switch ( z->op ) {
183
0
        case OP_IF_ELSE_Vb:
184
0
        case OP_IF_Vb:
185
0
        case OP_IF_NOT_Vb:
186
            // These are generated correctly above, no need
187
            // to fix up.
188
0
            break;
189
190
0
        case OP_HAS_FIELD_COND_Vib: z->op = OP_NOT_HAS_FIELD_COND_Vib; break;
191
0
        case OP_NOT_HAS_FIELD_COND_Vib: z->op = OP_HAS_FIELD_COND_Vib; break;
192
193
0
        case OP_CAN_CONVERT_COND_Vb: z->op = OP_NOT_CAN_CONVERT_COND_Vb; break;
194
0
        case OP_NOT_CAN_CONVERT_COND_Vb: z->op = OP_CAN_CONVERT_COND_Vb; break;
195
196
0
        case OP_CONN_EXISTS_COND_Vb: z->op = OP_NOT_CONN_EXISTS_COND_Vb; break;
197
0
        case OP_NOT_CONN_EXISTS_COND_Vb: z->op = OP_CONN_EXISTS_COND_Vb; break;
198
199
0
        case OP_IS_ICMP_PORT_COND_Vb: z->op = OP_NOT_IS_ICMP_PORT_COND_Vb; break;
200
0
        case OP_NOT_IS_ICMP_PORT_COND_Vb: z->op = OP_IS_ICMP_PORT_COND_Vb; break;
201
202
0
        case OP_IS_TCP_PORT_COND_Vb: z->op = OP_NOT_IS_TCP_PORT_COND_Vb; break;
203
0
        case OP_NOT_IS_TCP_PORT_COND_Vb: z->op = OP_IS_TCP_PORT_COND_Vb; break;
204
205
0
        case OP_IS_UDP_PORT_COND_Vb: z->op = OP_NOT_IS_UDP_PORT_COND_Vb; break;
206
0
        case OP_NOT_IS_UDP_PORT_COND_Vb: z->op = OP_IS_UDP_PORT_COND_Vb; break;
207
208
0
        case OP_IS_V4_ADDR_COND_Vb: z->op = OP_NOT_IS_V4_ADDR_COND_Vb; break;
209
0
        case OP_NOT_IS_V4_ADDR_COND_Vb: z->op = OP_IS_V4_ADDR_COND_Vb; break;
210
211
0
        case OP_IS_V6_ADDR_COND_Vb: z->op = OP_NOT_IS_V6_ADDR_COND_Vb; break;
212
0
        case OP_NOT_IS_V6_ADDR_COND_Vb: z->op = OP_IS_V6_ADDR_COND_Vb; break;
213
214
0
        case OP_READING_LIVE_TRAFFIC_COND_b: z->op = OP_NOT_READING_LIVE_TRAFFIC_COND_b; break;
215
0
        case OP_NOT_READING_LIVE_TRAFFIC_COND_b: z->op = OP_READING_LIVE_TRAFFIC_COND_b; break;
216
217
0
        case OP_READING_TRACES_COND_b: z->op = OP_NOT_READING_TRACES_COND_b; break;
218
0
        case OP_NOT_READING_TRACES_COND_b: z->op = OP_READING_TRACES_COND_b; break;
219
220
0
        case OP_TABLE_HAS_ELEMENTS_COND_Vb: z->op = OP_NOT_TABLE_HAS_ELEMENTS_COND_Vb; break;
221
0
        case OP_NOT_TABLE_HAS_ELEMENTS_COND_Vb: z->op = OP_TABLE_HAS_ELEMENTS_COND_Vb; break;
222
223
0
        case OP_VECTOR_HAS_ELEMENTS_COND_Vb: z->op = OP_NOT_VECTOR_HAS_ELEMENTS_COND_Vb; break;
224
0
        case OP_NOT_VECTOR_HAS_ELEMENTS_COND_Vb: z->op = OP_VECTOR_HAS_ELEMENTS_COND_Vb; break;
225
226
0
        case OP_VAL_IS_IN_TABLE_COND_VVb: z->op = OP_NOT_VAL_IS_IN_TABLE_COND_VVb; break;
227
0
        case OP_NOT_VAL_IS_IN_TABLE_COND_VVb: z->op = OP_VAL_IS_IN_TABLE_COND_VVb; break;
228
0
        case OP_VAL_IS_IN_PATTERN_TABLE_COND_VVb: z->op = OP_NOT_VAL_IS_IN_PATTERN_TABLE_COND_VVb; break;
229
0
        case OP_NOT_VAL_IS_IN_PATTERN_TABLE_COND_VVb: z->op = OP_VAL_IS_IN_PATTERN_TABLE_COND_VVb; break;
230
231
0
        case OP_CONST_IS_IN_TABLE_COND_VCb: z->op = OP_NOT_CONST_IS_IN_TABLE_COND_VCb; break;
232
0
        case OP_NOT_CONST_IS_IN_TABLE_COND_VCb: z->op = OP_CONST_IS_IN_TABLE_COND_VCb; break;
233
0
        case OP_CONST_IS_IN_PATTERN_TABLE_COND_VCb: z->op = OP_NOT_CONST_IS_IN_PATTERN_TABLE_COND_VCb; break;
234
0
        case OP_NOT_CONST_IS_IN_PATTERN_TABLE_COND_VCb: z->op = OP_CONST_IS_IN_PATTERN_TABLE_COND_VCb; break;
235
236
0
        case OP_VAL2_IS_IN_TABLE_COND_VVVb: z->op = OP_VAL2_IS_NOT_IN_TABLE_COND_VVVb; break;
237
0
        case OP_VAL2_IS_NOT_IN_TABLE_COND_VVVb: z->op = OP_VAL2_IS_IN_TABLE_COND_VVVb; break;
238
239
0
        case OP_VAL2_IS_IN_TABLE_COND_VVbC: z->op = OP_VAL2_IS_NOT_IN_TABLE_COND_VVbC; break;
240
0
        case OP_VAL2_IS_NOT_IN_TABLE_COND_VVbC: z->op = OP_VAL2_IS_IN_TABLE_COND_VVbC; break;
241
242
0
        case OP_VAL2_IS_IN_TABLE_COND_VVCb: z->op = OP_VAL2_IS_NOT_IN_TABLE_COND_VVCb; break;
243
0
        case OP_VAL2_IS_NOT_IN_TABLE_COND_VVCb: z->op = OP_VAL2_IS_IN_TABLE_COND_VVCb; break;
244
245
0
        default: reporter->InternalError("inconsistency in ZAMCompiler::IfElse");
246
0
    }
247
248
0
    SetV(cond_stmt, GoToTargetBeyond(s2_end), branch_v);
249
0
    return s2_end;
250
0
}
251
252
0
ZAMStmt ZAMCompiler::GenCond(const Expr* e, int& branch_v) {
253
0
    auto op1 = e->GetOp1();
254
0
    auto op2 = e->GetOp2();
255
256
0
    if ( e->Tag() == EXPR_HAS_FIELD ) {
257
0
        auto hf = e->AsHasFieldExpr();
258
0
        auto f = hf->Field();
259
0
        auto z = GenInst(OP_HAS_FIELD_COND_Vib, op1->AsNameExpr(), f);
260
0
        z.op_type = OP_VVV_I2_I3;
261
0
        z.TrackRecordTypeForField(cast_intrusive<RecordType>(op1->GetType()), f);
262
0
        branch_v = 3;
263
0
        return AddInst(z);
264
0
    }
265
266
0
    if ( e->Tag() == EXPR_CAN_CONVERT ) {
267
0
        auto ccf = e->AsCanConvertExpr();
268
0
        auto z = GenInst(OP_CAN_CONVERT_COND_Vb, op1->AsNameExpr());
269
0
        z.op_type = OP_VV_I2;
270
0
        z.SetType(op1->GetType());
271
0
        z.SetType2(ccf->ConversionType());
272
0
        branch_v = 2;
273
0
        return AddInst(z);
274
0
    }
275
276
0
    if ( e->Tag() == EXPR_IN ) {
277
        // First, deal with the easy cases: it's a single index.
278
0
        if ( op1->Tag() == EXPR_LIST ) {
279
0
            auto& ind = op1->AsListExpr()->Exprs();
280
0
            if ( ind.length() == 1 )
281
0
                op1 = {NewRef{}, ind[0]};
282
0
        }
283
284
0
        auto op2 = e->GetOp2()->AsNameExpr();
285
0
        auto op2_t = op2->GetType()->AsTableType();
286
0
        auto& ind_t = op2_t->GetIndexTypes();
287
288
0
        auto pat_tbl = ind_t.size() == 1 && ind_t[0]->Tag() == TYPE_PATTERN;
289
0
        if ( op1->Tag() == EXPR_NAME ) {
290
0
            auto op = pat_tbl ? OP_VAL_IS_IN_PATTERN_TABLE_COND_VVb : OP_VAL_IS_IN_TABLE_COND_VVb;
291
0
            auto z = GenInst(op, op1->AsNameExpr(), op2, 0);
292
0
            z.SetType(op1->GetType());
293
0
            branch_v = 3;
294
0
            return AddInst(z);
295
0
        }
296
297
0
        if ( op1->Tag() == EXPR_CONST ) {
298
0
            auto op = pat_tbl ? OP_CONST_IS_IN_PATTERN_TABLE_COND_VCb : OP_CONST_IS_IN_TABLE_COND_VCb;
299
0
            auto z = GenInst(op, op2, op1->AsConstExpr(), 0);
300
0
            z.SetType(op1->GetType());
301
0
            branch_v = 2;
302
0
            return AddInst(z);
303
0
        }
304
305
        // Now the harder case: 2 indexes.  (Any number here other
306
        // than two should have been disallowed due to how we reduce
307
        // conditional expressions.)
308
309
0
        auto& ind = op1->AsListExpr()->Exprs();
310
0
        ASSERT(ind.length() == 2);
311
312
0
        auto ind0 = ind[0];
313
0
        auto ind1 = ind[1];
314
315
0
        auto name0 = ind0->Tag() == EXPR_NAME;
316
0
        auto name1 = ind1->Tag() == EXPR_NAME;
317
318
0
        auto n0 = name0 ? ind0->AsNameExpr() : nullptr;
319
0
        auto n1 = name1 ? ind1->AsNameExpr() : nullptr;
320
321
0
        auto c0 = name0 ? nullptr : ind0->AsConstExpr();
322
0
        auto c1 = name1 ? nullptr : ind1->AsConstExpr();
323
324
0
        ZInstI z;
325
326
0
        if ( name0 && name1 ) {
327
0
            z = GenInst(OP_VAL2_IS_IN_TABLE_COND_VVVb, n0, n1, op2, 0);
328
0
            branch_v = 4;
329
0
            z.SetType2(n0->GetType());
330
0
        }
331
332
0
        else if ( name0 ) {
333
0
            z = GenInst(OP_VAL2_IS_IN_TABLE_COND_VVbC, n0, op2, c1, 0);
334
0
            branch_v = 3;
335
0
            z.SetType2(n0->GetType());
336
0
        }
337
338
0
        else if ( name1 ) {
339
0
            z = GenInst(OP_VAL2_IS_IN_TABLE_COND_VVCb, n1, op2, c0, 0);
340
0
            branch_v = 3;
341
0
            z.SetType2(n1->GetType());
342
0
        }
343
344
0
        else { // Both are constants, assign first to temporary.
345
0
            auto slot = TempForConst(c0);
346
347
0
            z = ZInstI(OP_VAL2_IS_IN_TABLE_COND_VVbC, slot, FrameSlot(op2), 0, c1);
348
0
            z.op_type = OP_VVVC_I3;
349
0
            branch_v = 3;
350
0
            z.SetType2(c0->GetType());
351
0
        }
352
353
0
        return AddInst(z);
354
0
    }
355
356
0
    if ( e->Tag() == EXPR_CALL ) {
357
0
        auto c = static_cast<const CallExpr*>(e);
358
0
        if ( IsZAM_BuiltInCond(this, c, branch_v) )
359
0
            return LastInst();
360
0
    }
361
362
0
    if ( e->Tag() == EXPR_SCRIPT_OPT_BUILTIN ) {
363
0
        auto bi = static_cast<const ScriptOptBuiltinExpr*>(e);
364
0
        ASSERT(bi->Tag() == ScriptOptBuiltinExpr::HAS_ELEMENTS);
365
0
        auto aggr = bi->GetOp1()->AsNameExpr();
366
367
0
        ZOp op;
368
0
        if ( aggr->GetType()->Tag() == TYPE_TABLE )
369
0
            op = OP_TABLE_HAS_ELEMENTS_COND_Vb;
370
0
        else
371
0
            op = OP_VECTOR_HAS_ELEMENTS_COND_Vb;
372
373
0
        branch_v = 2;
374
0
        return AddInst(GenInst(op, aggr, +0));
375
0
    }
376
377
0
    NameExpr* n1 = nullptr;
378
0
    NameExpr* n2 = nullptr;
379
0
    ConstExpr* c = nullptr;
380
381
0
    if ( op1->Tag() == EXPR_NAME ) {
382
0
        n1 = op1->AsNameExpr();
383
384
0
        if ( op2->Tag() == EXPR_NAME )
385
0
            n2 = op2->AsNameExpr();
386
0
        else
387
0
            c = op2->AsConstExpr();
388
0
    }
389
390
0
    else {
391
0
        c = op1->AsConstExpr();
392
0
        n2 = op2->AsNameExpr();
393
0
    }
394
395
0
    if ( n1 && n2 )
396
0
        branch_v = 3;
397
0
    else
398
0
        branch_v = 2;
399
400
// clang 10 gets perturbed that the indentation of the "default" in the
401
// following switch block doesn't match that of the cases that we include
402
// from "ZAM-Conds.h".  It really shouldn't worry about indentation mismatches
403
// across included files since those are not indicative of possible
404
// logic errors, but Oh Well.
405
0
#ifdef __GNUC__
406
0
#pragma GCC diagnostic push
407
0
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
408
0
#endif
409
0
    switch ( e->Tag() ) {
410
0
#include "ZAM-Conds.h"
411
412
0
        default: reporter->InternalError("bad expression type in ZAMCompiler::GenCond");
413
0
    }
414
0
#ifdef __GNUC__
415
0
#pragma GCC diagnostic pop
416
0
#endif
417
418
    // Not reached.
419
0
}
420
421
0
ZAMStmt ZAMCompiler::CompileSwitch(const SwitchStmt* sw) {
422
0
    auto e = sw->StmtExpr();
423
424
0
    auto n = e->Tag() == EXPR_NAME ? e->AsNameExpr() : nullptr;
425
0
    auto c = e->Tag() == EXPR_CONST ? e->AsConstExpr() : nullptr;
426
427
0
    auto t = e->GetType()->Tag();
428
429
    // Need to track a new set of contexts for "break" statements.
430
0
    PushBreaks();
431
432
0
    if ( sw->TypeMap()->empty() )
433
0
        return ValueSwitch(sw, n, c);
434
0
    else
435
0
        return TypeSwitch(sw, n, c);
436
0
}
437
438
0
ZAMStmt ZAMCompiler::ValueSwitch(const SwitchStmt* sw, const NameExpr* v, const ConstExpr* c) {
439
0
    int slot = v ? FrameSlot(v) : -1;
440
441
0
    if ( c )
442
        // Weird to have a constant switch expression, enough
443
        // so that it doesn't seem worth optimizing.
444
0
        slot = TempForConst(c);
445
446
0
    ASSERT(slot >= 0);
447
448
    // Figure out which jump table we're using.
449
0
    auto t = v ? v->GetType() : c->GetType();
450
451
0
    return GenSwitch(sw, slot, t->InternalType());
452
0
}
453
454
0
ZAMStmt ZAMCompiler::GenSwitch(const SwitchStmt* sw, int slot, InternalTypeTag it) {
455
0
    ZOp op;
456
457
0
    switch ( it ) {
458
0
        case TYPE_INTERNAL_INT: op = OP_SWITCHI_Vii; break;
459
460
0
        case TYPE_INTERNAL_UNSIGNED: op = OP_SWITCHU_Vii; break;
461
462
0
        case TYPE_INTERNAL_DOUBLE: op = OP_SWITCHD_Vii; break;
463
464
0
        case TYPE_INTERNAL_STRING: op = OP_SWITCHS_Vii; break;
465
466
0
        case TYPE_INTERNAL_ADDR: op = OP_SWITCHA_Vii; break;
467
468
0
        case TYPE_INTERNAL_SUBNET: op = OP_SWITCHN_Vii; break;
469
470
0
        default: reporter->InternalError("bad switch type");
471
0
    }
472
473
    // Add the "head", i.e., the execution of the jump table. At this point,
474
    // we leave the table (v2) and default (v3) TBD.
475
0
    auto sw_head_op = ZInstI(op, slot, 0, 0);
476
0
    sw_head_op.op_type = OP_VVV_I2_I3;
477
478
0
    auto sw_head = AddInst(sw_head_op);
479
0
    auto body_end = sw_head;
480
481
    // Generate each of the cases.
482
0
    auto cases = sw->Cases();
483
0
    std::vector<InstLabel> case_start;
484
0
    int case_index = 0;
485
486
0
    PushFallThroughs();
487
0
    for ( auto sw_case : *cases ) {
488
0
        auto start = GoToTargetBeyond(body_end);
489
0
        ResolveFallThroughs(start);
490
0
        case_start.push_back(start);
491
0
        PushFallThroughs();
492
0
        body_end = CompileStmt(sw_case->Body());
493
0
    }
494
495
0
    auto sw_end = GoToTargetBeyond(body_end);
496
0
    ResolveFallThroughs(sw_end);
497
0
    ResolveBreaks(sw_end);
498
499
0
    int def_ind = sw->DefaultCaseIndex();
500
0
    if ( def_ind >= 0 ) {
501
0
        auto def = case_start[def_ind];
502
0
        SetV3(sw_head, def);
503
0
        AddCFT(def, CFT_DEFAULT);
504
0
    }
505
0
    else
506
0
        SetV3(sw_head, sw_end);
507
508
    // Now fill out the corresponding jump table.
509
    //
510
    // We will only use one of these.
511
0
    CaseMapI<zeek_int_t> new_int_cases;
512
0
    CaseMapI<zeek_uint_t> new_uint_cases;
513
0
    CaseMapI<double> new_double_cases;
514
0
    CaseMapI<std::string> new_str_cases;
515
516
0
    for ( auto [cv, index] : sw->ValueMap() ) {
517
0
        auto case_body_start = case_start[index];
518
519
0
        switch ( cv->GetType()->InternalType() ) {
520
0
            case TYPE_INTERNAL_INT: new_int_cases[cv->InternalInt()] = case_body_start; break;
521
522
0
            case TYPE_INTERNAL_UNSIGNED: new_uint_cases[cv->InternalUnsigned()] = case_body_start; break;
523
524
0
            case TYPE_INTERNAL_DOUBLE: new_double_cases[cv->InternalDouble()] = case_body_start; break;
525
526
0
            case TYPE_INTERNAL_STRING: {
527
                // This leaks, but only statically so not worth
528
                // tracking the value for ultimate deletion.
529
0
                auto sv = cv->AsString()->Render();
530
0
                std::string s(sv);
531
0
                new_str_cases[s] = case_body_start;
532
0
                delete[] sv;
533
0
                break;
534
0
            }
535
536
0
            case TYPE_INTERNAL_ADDR: {
537
0
                auto a = cv->AsAddr().AsString();
538
0
                new_str_cases[a] = case_body_start;
539
0
                break;
540
0
            }
541
542
0
            case TYPE_INTERNAL_SUBNET: {
543
0
                auto n = cv->AsSubNet().AsString();
544
0
                new_str_cases[n] = case_body_start;
545
0
                break;
546
0
            }
547
548
0
            default: reporter->InternalError("bad recovered type when compiling switch");
549
0
        }
550
0
    }
551
552
    // For type switches, we map them to consecutive numbers, and then use
553
    // a integer-valued switch on those.
554
0
    int tm_ctr = 0;
555
0
    for ( const auto& [_, index] : *sw->TypeMap() ) {
556
0
        auto case_body_start = case_start[index];
557
0
        new_int_cases[tm_ctr++] = case_body_start;
558
0
    }
559
560
    // Now add the jump table to the set we're keeping for the
561
    // corresponding type.
562
563
0
    size_t tbl;
564
565
0
    switch ( it ) {
566
0
        case TYPE_INTERNAL_INT:
567
0
            tbl = int_casesI.size();
568
0
            int_casesI.push_back(std::move(new_int_cases));
569
0
            break;
570
571
0
        case TYPE_INTERNAL_UNSIGNED:
572
0
            tbl = uint_casesI.size();
573
0
            uint_casesI.push_back(std::move(new_uint_cases));
574
0
            break;
575
576
0
        case TYPE_INTERNAL_DOUBLE:
577
0
            tbl = double_casesI.size();
578
0
            double_casesI.push_back(std::move(new_double_cases));
579
0
            break;
580
581
0
        case TYPE_INTERNAL_STRING:
582
0
        case TYPE_INTERNAL_ADDR:
583
0
        case TYPE_INTERNAL_SUBNET:
584
0
            tbl = str_casesI.size();
585
0
            str_casesI.push_back(std::move(new_str_cases));
586
0
            break;
587
588
0
        default: reporter->InternalError("bad switch type");
589
0
    }
590
591
0
    insts1[sw_head.stmt_num]->v2 = static_cast<int>(tbl);
592
593
0
    AddCFT(insts1[body_end.stmt_num], CFT_BLOCK_END);
594
595
0
    return body_end;
596
0
}
597
598
0
ZAMStmt ZAMCompiler::TypeSwitch(const SwitchStmt* sw, const NameExpr* v, const ConstExpr* c) {
599
0
    auto cases = sw->Cases();
600
0
    auto type_map = sw->TypeMap();
601
0
    auto tmp = NewSlot(true); // true since we know "any" is managed
602
603
0
    int slot = v ? FrameSlot(v) : 0;
604
605
0
    if ( v ) {
606
0
        if ( v->GetType()->Tag() != TYPE_ANY ) {
607
0
            auto z = ZInstI(OP_ASSIGN_ANY_VV, tmp, slot);
608
0
            AddInst(z);
609
0
            slot = tmp;
610
0
        }
611
0
    }
612
613
0
    else {
614
0
        ASSERT(c);
615
0
        auto z = ZInstI(OP_ASSIGN_ANY_VC, tmp, c);
616
0
        AddInst(z);
617
0
        slot = tmp;
618
0
    }
619
620
0
    int ntypes = type_map->size();
621
0
    auto aux = new ZInstAux(ntypes);
622
623
0
    for ( size_t i = 0; i < type_map->size(); ++i ) {
624
0
        auto& tm = (*type_map)[i];
625
0
        auto id_i = tm.first;
626
0
        auto id_case = tm.second;
627
628
0
        auto slot = id_i->Name() ? FrameSlot(id_i) : -1;
629
0
        aux->Add(i, slot, id_i->GetType());
630
0
    }
631
632
0
    auto match_tmp = NewSlot(false);
633
0
    auto z = ZInstI(OP_DETERMINE_TYPE_MATCH_VV, match_tmp, slot);
634
0
    z.op_type = OP_VV;
635
0
    z.aux = aux;
636
0
    AddInst(z);
637
638
0
    return GenSwitch(sw, match_tmp, TYPE_INTERNAL_INT);
639
0
}
640
641
0
ZAMStmt ZAMCompiler::CompileWhile(const WhileStmt* ws) {
642
0
    const auto& loop_condition = ws->Condition();
643
644
0
    if ( loop_condition->Tag() == EXPR_CONST ) {
645
0
        if ( loop_condition->IsZero() )
646
0
            return EmptyStmt();
647
0
        else
648
0
            return Loop(ws->Body().get());
649
0
    }
650
651
0
    auto cond_pred = ws->CondPredStmt();
652
653
0
    return While(cond_pred.get(), loop_condition.get(), ws->Body().get());
654
0
}
655
656
0
ZAMStmt ZAMCompiler::While(const Stmt* cond_stmt, const Expr* cond, const Stmt* body) {
657
0
    auto head = StartingBlock();
658
659
0
    if ( cond_stmt )
660
0
        (void)CompileStmt(cond_stmt);
661
662
0
    ZAMStmt cond_IF = EmptyStmt();
663
0
    int branch_v;
664
665
0
    if ( cond->Tag() == EXPR_NAME ) {
666
0
        auto n = cond->AsNameExpr();
667
0
        cond_IF = AddInst(ZInstI(OP_IF_Vb, FrameSlot(n), 0));
668
0
        branch_v = 2;
669
0
    }
670
0
    else
671
0
        cond_IF = GenCond(cond, branch_v);
672
673
0
    AddCFT(insts1[head.stmt_num], CFT_LOOP);
674
0
    AddCFT(insts1[cond_IF.stmt_num], CFT_LOOP_COND);
675
676
0
    PushNexts();
677
0
    PushBreaks();
678
679
0
    if ( body && body->Tag() != STMT_NULL )
680
0
        (void)CompileStmt(body);
681
682
0
    AddCFT(insts1.back(), CFT_LOOP_END);
683
684
0
    auto tail = GoTo(GoToTarget(head));
685
686
0
    auto beyond_tail = GoToTargetBeyond(tail);
687
0
    SetV(cond_IF, beyond_tail, branch_v);
688
689
0
    ResolveNexts(GoToTarget(head));
690
0
    ResolveBreaks(beyond_tail);
691
692
0
    return tail;
693
0
}
694
695
0
ZAMStmt ZAMCompiler::CompileFor(const ForStmt* f) {
696
0
    auto e = f->LoopExpr();
697
0
    auto val = e->Tag() == EXPR_NAME ? e->AsNameExpr() : nullptr;
698
0
    auto et = e->GetType()->Tag();
699
700
0
    PushNexts();
701
0
    PushBreaks();
702
703
0
    ZAMStmt z;
704
705
0
    if ( et == TYPE_TABLE )
706
0
        z = LoopOverTable(f, val);
707
708
0
    else if ( et == TYPE_VECTOR )
709
0
        z = LoopOverVector(f, val);
710
711
0
    else if ( et == TYPE_STRING )
712
0
        z = LoopOverString(f, e);
713
714
0
    else
715
0
        reporter->InternalError("bad \"for\" loop-over value when compiling");
716
717
0
    return z;
718
0
}
719
720
0
ZAMStmt ZAMCompiler::LoopOverTable(const ForStmt* f, const NameExpr* val) {
721
0
    auto loop_vars = f->LoopVars();
722
0
    auto value_var = f->ValueVar();
723
0
    auto body = f->LoopBody();
724
725
    // We used to have more involved logic here to check whether the loop
726
    // variables are actually used in the body. Now that we have '_'
727
    // loop placeholder variables, this is no longer worth trying to
728
    // optimize for, though we still optimize for those placeholders.
729
0
    size_t num_unused = 0;
730
731
0
    auto aux = new ZInstAux(0);
732
733
0
    for ( const auto& id : *loop_vars ) {
734
0
        if ( id->IsBlank() )
735
0
            ++num_unused;
736
737
0
        int slot = id->IsBlank() ? -1 : FrameSlot(id);
738
0
        aux->loop_vars.push_back(slot);
739
0
        auto& t = id->GetType();
740
0
        aux->types.push_back(t);
741
0
        aux->is_managed.push_back(ZVal::IsManagedType(t));
742
0
    }
743
744
0
    bool no_loop_vars = (num_unused == loop_vars->size());
745
746
0
    if ( value_var )
747
0
        aux->value_var_type = value_var->GetType();
748
749
0
    auto iter_slot = table_iters.size();
750
0
    table_iters.emplace_back();
751
752
0
    auto zi = ZInstI(OP_INIT_TABLE_LOOP_Vf, FrameSlot(val), iter_slot);
753
0
    zi.op_type = OP_VV_I2;
754
0
    if ( value_var )
755
0
        zi.SetType(value_var->GetType());
756
0
    zi.aux = aux;
757
758
0
    (void)AddInst(zi);
759
760
0
    ZInstI zn;
761
0
    auto iter_head = StartingBlock();
762
763
0
    if ( value_var ) {
764
0
        ZOp op = no_loop_vars ? OP_NEXT_TABLE_ITER_VAL_VAR_NO_VARS_Vfb : OP_NEXT_TABLE_ITER_VAL_VAR_Vfb;
765
0
        zn = ZInstI(op, FrameSlot(value_var), iter_slot, 0);
766
0
        zn.CheckIfManaged(value_var->GetType());
767
0
        zn.op_type = OP_VVV_I2_I3;
768
0
    }
769
0
    else {
770
0
        ZOp op = no_loop_vars ? OP_NEXT_TABLE_ITER_NO_VARS_fb : OP_NEXT_TABLE_ITER_fb;
771
0
        zn = ZInstI(op, iter_slot, 0);
772
0
        zn.op_type = OP_VV_I1_I2;
773
0
    }
774
775
    // Need a separate instance of aux so the CFT info doesn't get shared with
776
    // the loop init. We populate it with the loop_vars (only) because the
777
    // optimizer needs access to those for (1) tracking their lifetime, and
778
    // (2) remapping them (not strictly needed, see the comment in ReMapFrame()).
779
0
    zn.aux = new ZInstAux(0);
780
0
    zn.aux->loop_vars = aux->loop_vars;
781
0
    AddCFT(&zn, CFT_LOOP);
782
0
    AddCFT(&zn, CFT_LOOP_COND);
783
784
0
    return FinishLoop(iter_head, zn, body, iter_slot, true);
785
0
}
786
787
0
ZAMStmt ZAMCompiler::LoopOverVector(const ForStmt* f, const NameExpr* val) {
788
0
    auto loop_vars = f->LoopVars();
789
0
    auto loop_var = (*loop_vars)[0];
790
0
    auto value_var = f->ValueVar();
791
792
0
    int iter_slot = num_step_iters++;
793
794
0
    auto z = ZInstI(OP_INIT_VECTOR_LOOP_Vs, FrameSlot(val), iter_slot);
795
0
    z.op_type = OP_VV_I2;
796
797
0
    auto init_end = AddInst(z);
798
0
    auto iter_head = StartingBlock();
799
800
0
    int slot = loop_var->IsBlank() ? -1 : FrameSlot(loop_var);
801
802
0
    if ( value_var ) {
803
0
        if ( slot >= 0 ) {
804
0
            z = ZInstI(OP_NEXT_VECTOR_ITER_VAL_VAR_VVsb, slot, FrameSlot(value_var), iter_slot, 0);
805
0
            z.op_type = OP_VVVV_I3_I4;
806
0
        }
807
0
        else {
808
0
            z = ZInstI(OP_NEXT_VECTOR_BLANK_ITER_VAL_VAR_Vsb, FrameSlot(value_var), iter_slot, 0);
809
0
            z.op_type = OP_VVV_I2_I3;
810
0
        }
811
812
0
        z.SetType(value_var->GetType());
813
0
    }
814
815
0
    else {
816
0
        if ( slot >= 0 ) {
817
0
            z = ZInstI(OP_NEXT_VECTOR_ITER_Vsb, slot, iter_slot, 0);
818
0
            z.op_type = OP_VVV_I2_I3;
819
0
        }
820
0
        else {
821
0
            z = ZInstI(OP_NEXT_VECTOR_BLANK_ITER_sb, iter_slot, 0);
822
0
            z.op_type = OP_VV_I1_I2;
823
0
        }
824
0
    }
825
826
0
    AddCFT(&z, CFT_LOOP);
827
0
    AddCFT(&z, CFT_LOOP_COND);
828
829
0
    return FinishLoop(iter_head, z, f->LoopBody(), iter_slot, false);
830
0
}
831
832
0
ZAMStmt ZAMCompiler::LoopOverString(const ForStmt* f, const Expr* e) {
833
0
    auto n = e->Tag() == EXPR_NAME ? e->AsNameExpr() : nullptr;
834
0
    auto c = e->Tag() == EXPR_CONST ? e->AsConstExpr() : nullptr;
835
0
    auto loop_vars = f->LoopVars();
836
0
    auto loop_var = (*loop_vars)[0];
837
838
0
    int iter_slot = num_step_iters++;
839
840
0
    ZInstI z;
841
842
0
    if ( n ) {
843
0
        z = ZInstI(OP_INIT_STRING_LOOP_Vs, FrameSlot(n), iter_slot);
844
0
        z.op_type = OP_VV_I2;
845
0
    }
846
0
    else {
847
0
        ASSERT(c);
848
0
        z = ZInstI(OP_INIT_STRING_LOOP_Cs, iter_slot, c);
849
0
        z.op_type = OP_VC_I1;
850
0
    }
851
852
0
    auto init_end = AddInst(z);
853
0
    auto iter_head = StartingBlock();
854
855
0
    if ( loop_var->IsBlank() ) {
856
0
        z = ZInstI(OP_NEXT_STRING_BLANK_ITER_sb, iter_slot, 0);
857
0
        z.op_type = OP_VV_I1_I2;
858
0
    }
859
0
    else {
860
0
        z = ZInstI(OP_NEXT_STRING_ITER_Vsb, FrameSlot(loop_var), iter_slot, 0);
861
0
        z.op_type = OP_VVV_I2_I3;
862
0
        z.is_managed = true;
863
0
    }
864
865
0
    AddCFT(&z, CFT_LOOP);
866
0
    AddCFT(&z, CFT_LOOP_COND);
867
868
0
    return FinishLoop(iter_head, z, f->LoopBody(), iter_slot, false);
869
0
}
870
871
0
ZAMStmt ZAMCompiler::Loop(const Stmt* body) {
872
0
    PushNexts();
873
0
    PushBreaks();
874
875
0
    auto head = StartingBlock();
876
0
    auto b = CompileStmt(body);
877
878
0
    if ( head.stmt_num == static_cast<int>(insts1.size()) ) {
879
0
        reporter->Error("infinite empty loop: %s", obj_desc(body).c_str());
880
0
        return head;
881
0
    }
882
883
0
    AddCFT(insts1[head.stmt_num], CFT_LOOP);
884
0
    AddCFT(insts1[b.stmt_num], CFT_LOOP_END);
885
886
0
    auto tail = GoTo(GoToTarget(head));
887
888
0
    ResolveNexts(GoToTarget(head));
889
0
    ResolveBreaks(GoToTargetBeyond(tail));
890
891
0
    return tail;
892
0
}
893
894
0
ZAMStmt ZAMCompiler::FinishLoop(ZAMStmt iter_head, ZInstI& iter_stmt, const Stmt* body, int iter_slot, bool is_table) {
895
0
    auto loop_iter = AddInst(iter_stmt);
896
0
    auto body_end = CompileStmt(body);
897
898
0
    auto loop_end = GoTo(GoToTarget(iter_head));
899
0
    AddCFT(insts1[loop_end.stmt_num], CFT_LOOP_END);
900
901
    // We only need cleanup for looping over tables, but for now we
902
    // need some sort of placeholder instruction (until the optimizer
903
    // can elide it) to resolve loop exits.
904
0
    ZOp op = is_table ? OP_END_TABLE_LOOP_f : OP_NOP;
905
906
0
    auto z = ZInstI(op, iter_slot);
907
0
    z.op_type = is_table ? OP_V_I1 : OP_X;
908
0
    auto final_stmt = AddInst(z);
909
910
0
    auto ot = iter_stmt.op_type;
911
0
    if ( ot == OP_VVVV_I3_I4 )
912
0
        SetV4(loop_iter, GoToTarget(final_stmt));
913
0
    else if ( ot == OP_VVV_I3 || ot == OP_VVV_I2_I3 )
914
0
        SetV3(loop_iter, GoToTarget(final_stmt));
915
0
    else
916
0
        SetV2(loop_iter, GoToTarget(final_stmt));
917
918
0
    ResolveNexts(GoToTarget(iter_head));
919
0
    ResolveBreaks(GoToTarget(final_stmt));
920
921
0
    return final_stmt;
922
0
}
923
924
0
ZAMStmt ZAMCompiler::CompileReturn(const ReturnStmt* r) {
925
0
    auto e = r->StmtExpr();
926
927
0
    if ( retvars.empty() ) { // a "true" return
928
0
        if ( e ) {
929
0
            if ( pf->ProfiledFunc()->Flavor() == FUNC_FLAVOR_HOOK ) {
930
0
                ASSERT(e->GetType()->Tag() == TYPE_BOOL);
931
0
                auto true_c = make_intrusive<ConstExpr>(val_mgr->True());
932
0
                return ReturnC(true_c.get());
933
0
            }
934
935
0
            if ( e->Tag() == EXPR_NAME )
936
0
                return ReturnV(e->AsNameExpr());
937
0
            else
938
0
                return ReturnC(e->AsConstExpr());
939
0
        }
940
941
0
        else
942
0
            return ReturnX();
943
0
    }
944
945
0
    auto rv = retvars.back();
946
0
    if ( e && ! rv )
947
0
        reporter->InternalError("unexpected returned value inside inlined block");
948
0
    if ( ! e && rv )
949
0
        reporter->InternalError("expected returned value inside inlined block but none provider");
950
951
0
    if ( e ) {
952
0
        if ( e->Tag() == EXPR_NAME )
953
0
            (void)AssignVV(rv, e->AsNameExpr());
954
0
        else
955
0
            (void)AssignVC(rv, e->AsConstExpr());
956
0
    }
957
958
0
    return CompileCatchReturn();
959
0
}
960
961
0
ZAMStmt ZAMCompiler::CompileCatchReturn(const CatchReturnStmt* cr) {
962
0
    retvars.push_back(cr->RetVar());
963
964
0
    const auto& hold_func = ZAM::curr_func;
965
0
    const auto& hold_loc = ZAM::curr_loc;
966
967
0
    ZAM::curr_func = cr->Func()->GetName();
968
969
0
    bool is_event_inline = (hold_func == ZAM::curr_func);
970
971
0
    if ( ! is_event_inline )
972
0
        ZAM::curr_loc = std::make_shared<ZAMLocInfo>(ZAM::curr_func, ZAM::curr_loc->LocPtr(), hold_loc);
973
974
0
    PushCatchReturns();
975
976
0
    auto block = cr->Block();
977
0
    auto block_end = CompileStmt(block);
978
0
    retvars.pop_back();
979
980
0
    ResolveCatchReturns(GoToTargetBeyond(block_end));
981
982
0
    if ( ! is_event_inline ) {
983
        // Strictly speaking, we could do this even if is_event_inline
984
        // is true, because the values won't have changed. However, that
985
        // just looks weird, so we condition this to match the above.
986
0
        ZAM::curr_func = hold_func;
987
0
        ZAM::curr_loc = hold_loc;
988
0
    }
989
990
0
    return block_end;
991
0
}
992
993
0
ZAMStmt ZAMCompiler::CompileStmts(const StmtList* ws) {
994
0
    auto start = StartingBlock();
995
996
0
    for ( const auto& stmt : ws->Stmts() )
997
0
        CompileStmt(stmt);
998
999
0
    return FinishBlock(start);
1000
0
}
1001
1002
0
ZAMStmt ZAMCompiler::CompileInit(const InitStmt* is) {
1003
0
    auto last = EmptyStmt();
1004
1005
0
    for ( const auto& aggr : is->Inits() ) {
1006
0
        if ( IsUnused(aggr, is) )
1007
0
            continue;
1008
1009
0
        auto& t = aggr->GetType();
1010
1011
0
        switch ( t->Tag() ) {
1012
0
            case TYPE_RECORD: last = InitRecord(aggr, t->AsRecordType()); break;
1013
1014
0
            case TYPE_VECTOR: last = InitVector(aggr, t->AsVectorType()); break;
1015
1016
0
            case TYPE_TABLE: last = InitTable(aggr, t->AsTableType(), aggr->GetAttrs().get()); break;
1017
1018
0
            default: break;
1019
0
        }
1020
0
    }
1021
1022
0
    return last;
1023
0
}
1024
1025
0
ZAMStmt ZAMCompiler::CompileWhen(const WhenStmt* ws) {
1026
0
    auto wi = ws->Info();
1027
0
    auto timeout = wi->TimeoutExpr();
1028
1029
0
    auto lambda = NewSlot(true);
1030
0
    (void)BuildLambda(lambda, wi->Lambda());
1031
1032
0
    std::vector<IDPtr> local_aggr_slots;
1033
0
    for ( auto& l : wi->WhenExprLocals() )
1034
0
        if ( IsAggr(l->GetType()->Tag()) )
1035
0
            local_aggr_slots.push_back(l);
1036
1037
0
    int n = local_aggr_slots.size();
1038
0
    auto aux = new ZInstAux(n);
1039
0
    aux->wi = wi;
1040
1041
0
    for ( auto i = 0; i < n; ++i ) {
1042
0
        const auto& la = local_aggr_slots[i];
1043
0
        aux->Add(i, FrameSlot(la), la->GetType());
1044
0
    }
1045
1046
0
    ZInstI z;
1047
1048
0
    if ( timeout ) {
1049
0
        if ( timeout->Tag() == EXPR_NAME ) {
1050
0
            auto ns = FrameSlot(timeout->AsNameExpr());
1051
0
            z = ZInstI(OP_WHEN_TIMEOUT_VV, lambda, ns);
1052
0
        }
1053
0
        else {
1054
0
            ASSERT(timeout->Tag() == EXPR_CONST);
1055
0
            z = ZInstI(OP_WHEN_TIMEOUT_VC, lambda, timeout->AsConstExpr());
1056
0
        }
1057
0
    }
1058
1059
0
    else
1060
0
        z = ZInstI(OP_WHEN_V, lambda);
1061
1062
0
    z.aux = aux;
1063
1064
0
    if ( ws->IsReturn() ) {
1065
0
        (void)AddInst(z);
1066
0
        z = ZInstI(OP_WHEN_RETURN_X);
1067
0
    }
1068
1069
0
    return AddInst(z);
1070
0
}
1071
1072
0
ZAMStmt ZAMCompiler::CompileAssert(const AssertStmt* as) {
1073
0
    auto cond = as->StmtExpr();
1074
1075
0
    int cond_slot;
1076
0
    if ( cond->Tag() == EXPR_CONST )
1077
0
        cond_slot = TempForConst(cond->AsConstExpr());
1078
0
    else
1079
0
        cond_slot = FrameSlot(cond->AsNameExpr());
1080
1081
0
    auto decision_slot = NewSlot(false);
1082
1083
0
    (void)AddInst(ZInstI(OP_SHOULD_REPORT_ASSERT_VV, decision_slot, cond_slot));
1084
1085
0
    auto cond_stmt = AddInst(ZInstI(OP_IF_Vb, decision_slot, 0));
1086
0
    AddCFT(insts1.back(), CFT_IF);
1087
1088
0
    ZInstI z;
1089
1090
    // We don't have a convenient way of directly introducing a std::string
1091
    // constant, so we build one to hold it.
1092
0
    auto cond_desc = make_intrusive<StringVal>(new String(as->CondDesc()));
1093
0
    auto cond_desc_e = make_intrusive<ConstExpr>(cond_desc);
1094
1095
0
    if ( const auto& msg = as->Msg() ) {
1096
0
        auto& msg_setup_stmt = as->MsgSetupStmt();
1097
0
        if ( msg_setup_stmt )
1098
0
            (void)CompileStmt(msg_setup_stmt);
1099
1100
0
        int msg_slot;
1101
0
        if ( msg->Tag() == EXPR_CONST )
1102
0
            msg_slot = TempForConst(msg->AsConstExpr());
1103
0
        else
1104
0
            msg_slot = FrameSlot(msg->AsNameExpr());
1105
1106
0
        z = ZInstI(OP_REPORT_ASSERT_WITH_MESSAGE_VVC, cond_slot, msg_slot, cond_desc_e.get());
1107
0
    }
1108
0
    else
1109
0
        z = ZInstI(OP_REPORT_ASSERT_VC, cond_slot, cond_desc_e.get());
1110
1111
0
    auto end_inst = AddInst(z);
1112
0
    AddCFT(insts1.back(), CFT_BLOCK_END);
1113
0
    SetV(cond_stmt, GoToTargetBeyond(end_inst), 2);
1114
1115
0
    return end_inst;
1116
0
}
1117
1118
0
ZAMStmt ZAMCompiler::InitRecord(IDPtr id, RecordType* rt) {
1119
0
    auto z = ZInstI(OP_INIT_RECORD_V, FrameSlot(id));
1120
0
    z.SetType({NewRef{}, rt});
1121
0
    return AddInst(z);
1122
0
}
1123
1124
0
ZAMStmt ZAMCompiler::InitVector(IDPtr id, VectorType* vt) {
1125
0
    auto z = ZInstI(OP_INIT_VECTOR_V, FrameSlot(id));
1126
0
    z.SetType({NewRef{}, vt});
1127
0
    return AddInst(z);
1128
0
}
1129
1130
0
ZAMStmt ZAMCompiler::InitTable(IDPtr id, TableType* tt, Attributes* attrs) {
1131
0
    auto z = ZInstI(OP_INIT_TABLE_V, FrameSlot(id));
1132
0
    z.SetType({NewRef{}, tt});
1133
0
    z.aux = new ZInstAux(0);
1134
0
    z.aux->attrs = {NewRef{}, attrs};
1135
0
    return AddInst(z);
1136
0
}
1137
1138
} // namespace zeek::detail