• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SRI-CSL / yices2 / 36864474362

01 Oct 2026 12:50PM UTC coverage: 71.198% (-0.01%) from 71.209%
36864474362

Pull #663

github

web-flow
Merge 2998114a8 into 9e5d726aa
Pull Request #663: UF fix and various code cleanup

446 of 533 new or added lines in 13 files covered. (83.68%)

299 existing lines in 14 files now uncovered.

95615 of 134294 relevant lines covered (71.2%)

1800360.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.25
/src/parser_utils/term_stack2.c
1
/*
2
 * This file is part of the Yices SMT Solver.
3
 * Copyright (C) 2017 SRI International.
4
 *
5
 * Yices is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Yices is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
/*
20
 * Stack-based API for building terms and types
21
 * Intended to support parsing.
22
 */
23

24
#if defined(CYGWIN) || defined(MINGW)
25
#ifndef __YICES_DLLSPEC__
26
#define __YICES_DLLSPEC__ __declspec(dllexport)
27
#endif
28
#endif
29

30
#include <assert.h>
31
#include <string.h>
32

33
#include "parser_utils/term_stack2.h"
34

35
#include "api/yices_extensions.h"
36
#include "api/yices_api_lock_free.h"
37
#include "api/yices_globals.h"
38
#include "parser_utils/tstack_internals.h"
39
#include "terms/bv64_constants.h"
40
#include "terms/bvarith64_buffer_terms.h"
41
#include "terms/bvarith_buffer_terms.h"
42
#include "terms/rba_buffer_terms.h"
43
#include "utils/hash_functions.h"
44

45
#include "yices.h"
46

47
#ifndef NDEBUG
48
#include <stdio.h>
49
#include <inttypes.h>
50

51
#include "io/type_printer.h"
52
#include "io/term_printer.h"
53
#endif
54

55

56
/****************
57
 *  EXCEPTIONS  *
58
 ***************/
59

60
/*
61
 * Exception raised when processing element e
62
 * - stack->error_pos is set to e->pos
63
 * - stack->error_op is set to stack->top_op
64
 * - stack->error_string is set to e's string field if e is a symbol or a string
65
 *   or a binding, or to NULL otherwise.
66
 * code is returned to exception handler by longjmp
67
 */
68
void __attribute__((noreturn)) raise_exception(tstack_t *stack, stack_elem_t *e, int code) {
1 ✔
69
  stack->error_loc = e->loc;
1 ✔
70
  stack->error_op = stack->top_op;
1 ✔
71
  switch (e->tag) {
1 ✔
72
  case TAG_SYMBOL:
×
73
  case TAG_STRING:
74
    stack->error_string = e->val.string;
×
75
    break;
×
76
  case TAG_BINDING:
×
77
    stack->error_string = e->val.binding.symbol;
×
78
    break;
×
79
  case TAG_TYPE_BINDING:
×
80
    stack->error_string = e->val.type_binding.symbol;
×
81
    break;
×
82
  default:
1 ✔
83
    stack->error_string = NULL;
1 ✔
84
    break;
1 ✔
85
  }
86
  longjmp(stack->env, code);
1 ✔
87
}
88

89
/*
90
 * Exception on a push_op operation
91
 * - loc = corresponding loc
92
 * - code = error code
93
 */
94
#ifndef NDEBUG
95
static void __attribute__((noreturn)) bad_op_exception(tstack_t *stack, loc_t *loc, uint32_t op) {
96
  stack->error_loc = *loc;
97
  stack->error_op = op;
98
  stack->error_string = NULL;
99
  longjmp(stack->env, TSTACK_INVALID_OP);
100
}
101
#endif
102

103

104
/*
105
 * Bad format or other error on a push_rational, push_float, push_bvbin, push_hexbin operation, etc.
106
 */
107
void __attribute__((noreturn)) push_exception(tstack_t *stack, loc_t *loc, char *s, int code) {
3 ✔
108
  stack->error_loc = *loc;
3 ✔
109
  stack->error_op = NO_OP;
3 ✔
110
  stack->error_string = s;
3 ✔
111
  longjmp(stack->env, code);
3 ✔
112
}
113

114
/*
115
 * Translate a yices error into an exception.
116
 */
117
void __attribute__((noreturn)) report_yices_error(tstack_t *stack) {
1 ✔
118
  uint32_t i;
119

120
  i = stack->frame;
1 ✔
121
  stack->error_loc = stack->elem[i].loc;
1 ✔
122
  stack->error_op = stack->top_op;
1 ✔
123
  stack->error_string = NULL;
1 ✔
124
  longjmp(stack->env, TSTACK_YICES_ERROR);
1 ✔
125
}
126

127

128
/********************
129
 *  OPERATOR TABLE  *
130
 *******************/
131

132
/*
133
 * Allocate a table of size n
134
 */
135
static void alloc_op_table(op_table_t *table, uint32_t n) {
1,801 ✔
136
  assert(n <= MAX_OP_TABLE_SIZE);
137
  table->assoc = (uint8_t *) safe_malloc(n * sizeof(uint8_t));
1,801 ✔
138
  table->eval = (eval_fun_t *) safe_malloc(n * sizeof(eval_fun_t));
1,801 ✔
139
  table->check = (check_fun_t *) safe_malloc(n * sizeof(check_fun_t));
1,801 ✔
140
  table->num_ops = 0;
1,801 ✔
141
  table->size = n;
1,801 ✔
142
}
1,801 ✔
143

144
/*
145
 * Delete
146
 */
147
static void delete_op_table(op_table_t *table) {
1,801 ✔
148
  safe_free(table->assoc);
1,801 ✔
149
  safe_free(table->eval);
1,801 ✔
150
  safe_free(table->check);
1,801 ✔
151
  table->assoc = NULL;
1,801 ✔
152
  table->eval = NULL;
1,801 ✔
153
  table->check = NULL;
1,801 ✔
154
}
1,801 ✔
155

156

157
/**************************
158
 *  STACK INITIALIZATION  *
159
 *************************/
160

161
/*
162
 * Initialize stack:
163
 * - nops = size of the operator table
164
 */
165
static void alloc_tstack(tstack_t *stack, uint32_t nops) {
1,801 ✔
166
  uint32_t n;
167
  stack_elem_t *tmp;
168

169
  n = DEFAULT_TERM_STACK_SIZE;
1,801 ✔
170
  tmp = (stack_elem_t *) safe_malloc(n * sizeof(stack_elem_t));
1,801 ✔
171

172
  // mark bottom element
173
  tmp->tag = TAG_OP;
1,801 ✔
174
  tmp->val.opval.opcode = NO_OP;
1,801 ✔
175
  tmp->val.opval.multiplicity = 0;
1,801 ✔
176
  tmp->val.opval.prev = 0;
1,801 ✔
177

178
  stack->elem = tmp;
1,801 ✔
179
  stack->top = 1;
1,801 ✔
180
  stack->size = n;
1,801 ✔
181
  stack->frame = 0;
1,801 ✔
182
  stack->top_op = NO_OP;
1,801 ✔
183

184
  alloc_op_table(&stack->op_table, nops);
1,801 ✔
185

186
  init_arena(&stack->mem);
1,801 ✔
187

188
  stack->aux_buffer = (int32_t *) safe_malloc(DEFAULT_AUX_SIZE * sizeof(int32_t));
1,801 ✔
189
  stack->aux_size = DEFAULT_AUX_SIZE;
1,801 ✔
190

191
  stack->sbuffer = NULL;
1,801 ✔
192
  stack->sbuffer_size = 0;
1,801 ✔
193

194
  stack->name_buffer = NULL;
1,801 ✔
195
  stack->name_buffer_size = 0;
1,801 ✔
196

197
  init_bvconstant(&stack->bvconst_buffer);
1,801 ✔
198

199
  stack->abuffer = NULL;
1,801 ✔
200
  stack->bva64buffer = NULL;
1,801 ✔
201
  stack->bvabuffer = NULL;
1,801 ✔
202
  stack->bvlbuffer = NULL;
1,801 ✔
203

204
  stack->tvar_id = 0;
1,801 ✔
205

206
  stack->avtbl = NULL;
1,801 ✔
207

208
  stack->error_op = NO_OP;
1,801 ✔
209
  stack->error_loc.line = 0;
1,801 ✔
210
  stack->error_loc.column = 0;
1,801 ✔
211
  stack->error_string = NULL;
1,801 ✔
212
}
1,801 ✔
213

214

215
/*
216
 * Extend: increase size by 50%
217
 */
218
static void tstack_extend(tstack_t *stack) {
1,042 ✔
219
  uint32_t n;
220

221
  n = stack->size + 1;
1,042 ✔
222
  n += n >> 1;
1,042 ✔
223

224
  if (n > MAX_TERM_STACK_SIZE) {
1,042 ✔
225
    out_of_memory();
×
226
  }
227

228
  stack->elem = (stack_elem_t *) safe_realloc(stack->elem, n * sizeof(stack_elem_t));
1,042 ✔
229
  stack->size = n;
1,042 ✔
230
}
1,042 ✔
231

232

233
/*
234
 * Return top index and extend the stack if it's full
235
 * also increment top.
236
 */
237
static uint32_t tstack_get_top(tstack_t *stack) {
3,256,267 ✔
238
  uint32_t i;
239

240
  i = stack->top;
3,256,267 ✔
241
  stack->top ++;
3,256,267 ✔
242
  if (i >= stack->size) {
3,256,267 ✔
243
    tstack_extend(stack);
1,042 ✔
244
    assert(i < stack->size);
245
  }
246
  return i;
3,256,267 ✔
247
}
248

249
/*
250
 * Same thing but return a pointer to element i
251
 */
252
static stack_elem_t *tstack_get_topelem(tstack_t *stack) {
1,817,689 ✔
253
  uint32_t k;
254
  // The order is important: tstack_get_top has side effects
255
  // (including changing stack->elem)!!
256
  k = tstack_get_top(stack);
1,817,689 ✔
257
  return stack->elem + k;
1,817,689 ✔
258
}
259

260

261

262

263

264
/*********************
265
 *  PUSH OPERATIONS  *
266
 ********************/
267

268
/*
269
 * Push op: create a new frame and push the operator.
270
 *
271
 * For special operators (such that assoc[op] == 1) , if the current
272
 * top-operator is identical to op, then we just increment its
273
 * multiplicity index.
274
 *
275
 * If op is BIND, then top-op must be LET.
276
 * If op is DECLARE_VAR, then top-op must be MK_EXISTS or MK_FORALL or MK_LAMBDA
277
 *
278
 * For all operators except BIND, DECLARE_VAR, and DECLARE_TYPE_VAR,
279
 * we also open a new scope in the arena. For BIND the arena scope
280
 * remains the one open by the enclosing LET. For DECLARE_VAR, it's
281
 * the one of the enclosing FORALL, EXISTS, or LAMBDA. For
282
 * DECLARE_TYPE_VAR, it's also the one open by the enclosing operator
283
 * (must be something like SMT LIB 2's define-sort).
284
 *
285
 * NOTE: in SMT-LIB 2, we can have DECLARE_VAR inside a define-fun
286
 * operation.
287
 */
288
void tstack_push_op(tstack_t *stack, int32_t op, loc_t *loc) {
1,489,889 ✔
289
  uint32_t i;
290
  stack_elem_t *e;
291

292
#ifndef NDEBUG
293
  if (op < 0 ||
294
      op >= stack->op_table.num_ops ||
295
      stack->op_table.check[op] == NULL ||
296
      stack->op_table.eval[op] == NULL) {
297
    bad_op_exception(stack, loc, op);
298
  }
299
#endif
300

301
  if (stack->op_table.assoc[op] && stack->top_op == op) {
1,489,889 ✔
302
    i = stack->frame;
51,311 ✔
303
    stack->elem[i].val.opval.multiplicity ++;
51,311 ✔
304
    return;
51,311 ✔
305
  }
306

307
  i = tstack_get_top(stack);
1,438,578 ✔
308
  e = stack->elem + i;
1,438,578 ✔
309
  e->tag = TAG_OP;
1,438,578 ✔
310
  e->val.opval.opcode = op;
1,438,578 ✔
311
  e->val.opval.prev = stack->frame;
1,438,578 ✔
312
  e->val.opval.multiplicity = 0;
1,438,578 ✔
313
  e->loc = *loc;
1,438,578 ✔
314
  stack->top_op = op;
1,438,578 ✔
315
  stack->frame = i;
1,438,578 ✔
316

317
  if (op != BIND && op != DECLARE_VAR && op != DECLARE_TYPE_VAR) {
1,438,578 ✔
318
    // mark start of new scope
319
    arena_push(&stack->mem);
1,258,870 ✔
320
  }
321
}
322

323

324
/*
325
 * Push opcode on top of the stack. Don't create a new frame.
326
 * Don't open a new scope in the arena.
327
 */
328
void tstack_push_opcode(tstack_t *stack, int32_t op, loc_t *loc) {
×
329
  stack_elem_t *e;
330

331
#ifndef NDEBUG
332
  if (op < 0 || op >= stack->op_table.num_ops) {
333
    bad_op_exception(stack, loc, op);
334
  }
335
#endif
336

337
  e = tstack_get_topelem(stack);
×
338
  e->tag = TAG_OPCODE;
×
339
  e->val.op = op;
×
340
  e->loc = *loc;
×
341
}
×
342

343
/*
344
 * Push a copy of string s, n = length of s
345
 * - tag = type of string (either TAG_STRING or TAG_SYMBOL)
346
 */
347
void tstack_push_str(tstack_t *stack, tag_t tag, char *s, uint32_t n, loc_t *loc) {
416,958 ✔
348
  char *tmp;
349
  stack_elem_t *e;
350

351
  assert(strlen(s) == n);
352

353
  tmp = (char *) arena_alloc(&stack->mem, n + 1);
416,958 ✔
354
  strcpy(tmp, s);
416,958 ✔
355
  e = tstack_get_topelem(stack);
416,958 ✔
356
  e->tag = tag;
416,958 ✔
357
  e->val.string = tmp;
416,958 ✔
358
  e->loc = *loc;
416,958 ✔
359
}
416,958 ✔
360

361

362
/*
363
 * For define-type or define term: push a name s on the stack.
364
 *
365
 * These functions are like push_symbol but they raise an exception if
366
 * name is already used (TSTACK_TYPENAME_REDEF, TSTACK_TERMNAME_REDEF,
367
 * or TSTACK_MACRO_REDEF)
368
 */
369
void tstack_push_free_typename(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
77 ✔
370
  if (_o_yices_get_type_by_name(s) != NULL_TYPE) {
77 ✔
371
    push_exception(stack, loc, s, TSTACK_TYPENAME_REDEF);
×
372
  }
373
  tstack_push_str(stack, TAG_SYMBOL, s, n, loc);
77 ✔
374
}
77 ✔
375

376
void tstack_push_free_termname(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
97,752 ✔
377
  if (_o_yices_get_term_by_name(s) != NULL_TERM) {
97,752 ✔
378
    push_exception(stack, loc, s, TSTACK_TERMNAME_REDEF);
×
379
  }
380
  tstack_push_str(stack, TAG_SYMBOL, s, n, loc);
97,752 ✔
381
}
97,752 ✔
382

383
void tstack_push_free_macroname(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
×
384
  if (yices_get_macro_by_name(s) >= 0) {
×
385
    push_exception(stack, loc, s, TSTACK_MACRO_REDEF);
×
386
  }
387
  tstack_push_str(stack, TAG_SYMBOL, s, n, loc);
×
388
}
×
389

390
/*
391
 * Variant: raise exception (TSTACK_TYPENAME_REDEF) if s is already
392
 * used either as a type or as a macro name
393
 */
394
void tstack_push_free_type_or_macro_name(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
387 ✔
395
  if (_o_yices_get_type_by_name(s) != NULL_TYPE || yices_get_macro_by_name(s) >= 0) {
387 ✔
396
    push_exception(stack, loc, s, TSTACK_TYPENAME_REDEF);
×
397
  }
398
  tstack_push_str(stack, TAG_SYMBOL, s, n, loc);
387 ✔
399
}
387 ✔
400

401

402
/*
403
 * Convert a string to a rational and push that
404
 * - s must be null-terminated and of rational or floating point formats
405
 */
406
void tstack_push_rational(tstack_t *stack, char *s, loc_t *loc) {
313,138 ✔
407
  stack_elem_t *e;
408
  int code;
409

410
  e = tstack_get_topelem(stack);
313,138 ✔
411
  e->tag = TAG_RATIONAL;
313,138 ✔
412
  e->loc = *loc;
313,138 ✔
413
  q_init(&e->val.rational);
313,138 ✔
414
  code = q_set_from_string(&e->val.rational, s);
313,138 ✔
415
  if (code < 0) {
313,138 ✔
416
    // -1 means that the format is wrong
417
    // -2 means that the denominator is zero
418
    if (code == -1) {
×
419
      push_exception(stack, loc, s, TSTACK_RATIONAL_FORMAT);
×
420
    } else {
421
      assert(code == -2);
422
      push_exception(stack, loc, s, TSTACK_DIVIDE_BY_ZERO);
×
423
    }
424
  }
425
}
313,138 ✔
426

427
void tstack_push_float(tstack_t *stack, char *s, loc_t *loc) {
10,553 ✔
428
  stack_elem_t *e;
429
  int code;
430

431
  e = tstack_get_topelem(stack);
10,553 ✔
432
  e->tag = TAG_RATIONAL;
10,553 ✔
433
  e->loc = *loc;
10,553 ✔
434
  q_init(&e->val.rational);
10,553 ✔
435
  code = q_set_from_float_string(&e->val.rational, s);
10,553 ✔
436
  if (code < 0) {
10,553 ✔
437
    push_exception(stack, loc, s, TSTACK_FLOAT_FORMAT);
×
438
  }
439
}
10,553 ✔
440

441

442
/*
443
 * Push a small bitvector constant:
444
 * - n = bitsize (1 <= n <= 64)
445
 * - c = value
446
 */
447
static void tstack_push_bv64(tstack_t *stack, uint32_t n, uint64_t c, loc_t *loc) {
14,115 ✔
448
  stack_elem_t *e;
449

450
  assert(1 <= n && n <= 64 && c == norm64(c, n));
451

452
  e = tstack_get_topelem(stack);
14,115 ✔
453
  e->tag = TAG_BV64;
14,115 ✔
454
  e->val.bv64.bitsize = n;
14,115 ✔
455
  e->val.bv64.value = c;
14,115 ✔
456
  e->loc = *loc;
14,115 ✔
457
}
14,115 ✔
458

459

460
/*
461
 * Push a generic bitvector constant
462
 * - n = bitsize (n > 64)
463
 * - c = value as an array of words
464
 */
465
static void tstack_push_bv(tstack_t *stack, uint32_t n, uint32_t *c, loc_t *loc) {
28 ✔
466
  stack_elem_t *e;
467

468
  assert(n > 64);
469

470
  e = tstack_get_topelem(stack);
28 ✔
471
  e->tag = TAG_BV;
28 ✔
472
  e->val.bv.bitsize = n;
28 ✔
473
  e->val.bv.data = c;
28 ✔
474
  e->loc = *loc;
28 ✔
475
}
28 ✔
476

477

478
/*
479
 * Convert a string to a bitvector constant and push that
480
 * - n = length of the string
481
 * - s must be a string of binary or hexadecimal digits (no prefix)
482
 */
483
void tstack_push_bvbin(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
14,097 ✔
484
  uint32_t k;
485
  int code;
486
  uint32_t *tmp;
487
  uint64_t c;
488

489
  if (n > 64) {
14,097 ✔
490
    // large constant
491
    k = (n + 31) >> 5; // number of words
28 ✔
492
    tmp = bvconst_alloc(k);
28 ✔
493
    code = bvconst_set_from_string(tmp, n, s);
28 ✔
494
    if (code < 0) goto error;
28 ✔
495

496
    bvconst_normalize(tmp, n);
28 ✔
497
    tstack_push_bv(stack, n, tmp, loc);
28 ✔
498

499
  } else {
500
    // small constant
501
    code = bvconst64_set_from_string(&c, n, s);
14,069 ✔
502
    if (code < 0) goto error;
14,069 ✔
503
    tstack_push_bv64(stack, n, c, loc);
14,069 ✔
504
  }
505
  return;
14,097 ✔
506

507
 error:
×
508
  push_exception(stack, loc, s, TSTACK_BVBIN_FORMAT);
×
509
}
510

511
void tstack_push_bvhex(tstack_t *stack, char *s, uint32_t n, loc_t *loc) {
46 ✔
512
  uint32_t k;
513
  int code;
514
  uint32_t *tmp;
515
  uint64_t c;
516

517
  if (n > 16) {
46 ✔
518
    // large constant
519
    k = (n + 7) >> 3; // number of words
×
520
    tmp = bvconst_alloc(k);
×
521
    code = bvconst_set_from_hexa_string(tmp, n, s);
×
522
    if (code < 0) goto error;
×
523

524
    bvconst_normalize(tmp, 4 * n);
×
525
    tstack_push_bv(stack, 4 * n, tmp, loc);
×
526

527
  } else {
528
    // small constant
529
    code = bvconst64_set_from_hexa_string(&c, n, s);
46 ✔
530
    if (code < 0) goto error;
46 ✔
531
    tstack_push_bv64(stack, 4 * n, c, loc);
46 ✔
532
  }
533
  return;
46 ✔
534

535
 error:
×
536
  push_exception(stack, loc, s, TSTACK_BVHEX_FORMAT);
×
537
}
538

539

540
/*
541
 * Convert a name to a type or a term and push the type or term on the stack
542
 */
543
void tstack_push_type_by_name(tstack_t *stack, char *s, loc_t *loc) {
15,580 ✔
544
  stack_elem_t *e;
545
  type_t tau;
546

547
  tau = _o_yices_get_type_by_name(s);
15,580 ✔
548
  if (tau == NULL_TYPE) push_exception(stack, loc, s, TSTACK_UNDEF_TYPE);
15,580 ✔
549

550
  e = tstack_get_topelem(stack);
15,580 ✔
551
  e->tag = TAG_TYPE;
15,580 ✔
552
  e->val.type = tau;
15,580 ✔
553
  e->loc = *loc;
15,580 ✔
554
}
15,580 ✔
555

556
void tstack_push_term_by_name(tstack_t *stack, char *s, loc_t *loc) {
971,220 ✔
557
  stack_elem_t *e;
558
  term_t t;
559

560
  t = _o_yices_get_term_by_name(s);
971,220 ✔
561
  if (t == NULL_TERM) push_exception(stack, loc, s, TSTACK_UNDEF_TERM);
971,220 ✔
562

563
  e = tstack_get_topelem(stack);
971,217 ✔
564
  e->tag = TAG_TERM;
971,217 ✔
565
  e->val.term = t;
971,217 ✔
566
  e->loc = *loc;
971,217 ✔
567
}
971,217 ✔
568

569
void tstack_push_macro_by_name(tstack_t *stack, char *s, loc_t *loc) {
×
570
  stack_elem_t *e;
571
  int32_t id;
572

573
  id = yices_get_macro_by_name(s);
×
574
  if (id < 0) push_exception(stack, loc, s, TSTACK_UNDEF_MACRO);
×
575

576
  e = tstack_get_topelem(stack);
×
577
  e->tag = TAG_MACRO;
×
578
  e->val.macro = id;
×
579
  e->loc = *loc;
×
580
}
×
581

582

583
/*
584
 * Push primitive types or terms
585
 */
586
void tstack_push_bool_type(tstack_t *stack, loc_t *loc) {
3,171 ✔
587
  stack_elem_t *e;
588

589
  e = tstack_get_topelem(stack);
3,171 ✔
590
  e->tag = TAG_TYPE;
3,171 ✔
591
  e->val.type = _o_yices_bool_type();
3,171 ✔
592
  e->loc = *loc;
3,171 ✔
593
}
3,171 ✔
594

595
void tstack_push_int_type(tstack_t *stack, loc_t *loc) {
99 ✔
596
  stack_elem_t *e;
597

598
  e = tstack_get_topelem(stack);
99 ✔
599
  e->tag = TAG_TYPE;
99 ✔
600
  e->val.type = _o_yices_int_type();
99 ✔
601
  e->loc = *loc;
99 ✔
602
}
99 ✔
603

604
void tstack_push_real_type(tstack_t *stack, loc_t *loc) {
186 ✔
605
  stack_elem_t *e;
606

607
  e = tstack_get_topelem(stack);
186 ✔
608
  e->tag = TAG_TYPE;
186 ✔
609
  e->val.type = _o_yices_real_type();
186 ✔
610
  e->loc = *loc;
186 ✔
611
}
186 ✔
612

613
void tstack_push_true(tstack_t *stack, loc_t *loc) {
42 ✔
614
  stack_elem_t *e;
615

616
  e = tstack_get_topelem(stack);
42 ✔
617
  e->tag = TAG_TERM;
42 ✔
618
  e->val.term = yices_true();
42 ✔
619
  e->loc = *loc;
42 ✔
620
}
42 ✔
621

622
void tstack_push_false(tstack_t *stack, loc_t *loc) {
51 ✔
623
  stack_elem_t *e;
624

625
  e = tstack_get_topelem(stack);
51 ✔
626
  e->tag = TAG_TERM;
51 ✔
627
  e->val.term = yices_false();
51 ✔
628
  e->loc = *loc;
51 ✔
629
}
51 ✔
630

631

632
/*
633
 * Push an integer constant
634
 */
635
void tstack_push_int32(tstack_t *stack, int32_t x, loc_t *loc) {
×
636
  stack_elem_t *e;
637

638
  e = tstack_get_topelem(stack);
×
639
  e->tag = TAG_RATIONAL;
×
640
  e->loc = *loc;
×
641
  q_init(&e->val.rational);
×
642
  q_set32(&e->val.rational, x);
×
643
}
×
644

645

646
/*
647
 * Terms or types
648
 */
649
void tstack_push_term(tstack_t *stack, term_t t, loc_t *loc) {
3,230 ✔
650
  stack_elem_t *e;
651

652
  e = tstack_get_topelem(stack);
3,230 ✔
653
  e->tag = TAG_TERM;
3,230 ✔
654
  e->val.term = t;
3,230 ✔
655
  e->loc = *loc;
3,230 ✔
656
}
3,230 ✔
657

658
void tstack_push_type(tstack_t *stack, type_t tau, loc_t *loc) {
43,264 ✔
659
  stack_elem_t *e;
660

661
  e = tstack_get_topelem(stack);
43,264 ✔
662
  e->tag = TAG_TYPE;
43,264 ✔
663
  e->val.type = tau;
43,264 ✔
664
  e->loc = *loc;
43,264 ✔
665
}
43,264 ✔
666

667
void tstack_push_macro(tstack_t *stack, int32_t id, loc_t *loc) {
×
668
  stack_elem_t *e;
669

670
  e = tstack_get_topelem(stack);
×
671
  e->tag = TAG_MACRO;
×
672
  e->val.macro = id;
×
673
  e->loc = *loc;
×
674
}
×
675

676

677
/**********************
678
 *  INTERNAL BUFFERS  *
679
 *********************/
680

681
/*
682
 * Invariant we want to maintain:
683
 * - stack->abuffer is either NULL or it's pointing to the last
684
 *   arithmetic_buffer allocated and that buffer does not occur in the stack.
685
 * - if an element in the stack has tag = TAG_ARITH_BUFFER
686
 *   then its value is a pointer to an arithmetic buffer != stack->abuffer.
687
 * Same thing for stack->bvabuffer and stack->bvlbuffer.
688
 */
689

690
/*
691
 * Get the internal buffers (or allocate a new one)
692
 */
693
rba_buffer_t *tstack_get_abuffer(tstack_t *stack) {
75,197 ✔
694
  rba_buffer_t *tmp;
695

696
  tmp = stack->abuffer;
75,197 ✔
697
  if (tmp == NULL) {
75,197 ✔
698
    tmp = yices_new_arith_buffer();
19,036 ✔
699
    stack->abuffer = tmp;
19,036 ✔
700
  } else {
701
    reset_rba_buffer(tmp);
56,161 ✔
702
  }
703
  assert(rba_buffer_is_zero(tmp));
704
  return tmp;
75,197 ✔
705
}
706

707
bvarith64_buffer_t *tstack_get_bva64buffer(tstack_t *stack, uint32_t bitsize) {
9,180 ✔
708
  bvarith64_buffer_t *tmp;
709

710
  assert(1 <= bitsize && bitsize <= 64);
711

712
  tmp = stack->bva64buffer;
9,180 ✔
713
  if (tmp == NULL) {
9,180 ✔
714
    tmp = yices_new_bvarith64_buffer(bitsize);
2,678 ✔
715
    stack->bva64buffer = tmp;
2,678 ✔
716
  } else {
717
    bvarith64_buffer_prepare(tmp, bitsize); // reset to zero and set size to bitsize
6,502 ✔
718
  }
719

720
  assert(bvarith64_buffer_is_zero(tmp) && bvarith64_buffer_bitsize(tmp) == bitsize);
721

722
  return tmp;
9,180 ✔
723
}
724

725
bvarith_buffer_t *tstack_get_bvabuffer(tstack_t *stack, uint32_t bitsize) {
902 ✔
726
  bvarith_buffer_t *tmp;
727

728
  assert(64 < bitsize && bitsize <= YICES_MAX_BVSIZE);
729

730
  tmp = stack->bvabuffer;
902 ✔
731
  if (tmp == NULL) {
902 ✔
732
    tmp = yices_new_bvarith_buffer(bitsize);
74 ✔
733
    stack->bvabuffer = tmp;
74 ✔
734
  } else {
735
    bvarith_buffer_prepare(tmp, bitsize); // reset to zero and set size
828 ✔
736
  }
737

738
  assert(bvarith_buffer_is_zero(tmp) && bvarith_buffer_bitsize(tmp) == bitsize);
739

740
  return tmp;
902 ✔
741
}
742

743
bvlogic_buffer_t *tstack_get_bvlbuffer(tstack_t *stack) {
60,630 ✔
744
  bvlogic_buffer_t *tmp;
745

746
  tmp = stack->bvlbuffer;
60,630 ✔
747
  if (tmp == NULL) {
60,630 ✔
748
    tmp = yices_new_bvlogic_buffer();
20,814 ✔
749
    stack->bvlbuffer = tmp;
20,814 ✔
750
  } else {
751
    bvlogic_buffer_clear(tmp);
39,816 ✔
752
  }
753
  return tmp;
60,630 ✔
754
}
755

756

757
/*
758
 * Free or recycle a buffer
759
 */
760
static void recycle_abuffer(tstack_t *stack, rba_buffer_t *b) {
32,931 ✔
761
  if (stack->abuffer == NULL) {
32,931 ✔
762
    reset_rba_buffer(b);
14,617 ✔
763
    stack->abuffer = b;
14,617 ✔
764
  } else if (stack->abuffer != b) {
18,314 ✔
765
    yices_free_arith_buffer(b);
18,314 ✔
766
  }
767
}
32,931 ✔
768

769
static void recycle_bva64buffer(tstack_t *stack, bvarith64_buffer_t *b) {
9,180 ✔
770
  if (stack->bva64buffer == NULL) {
9,180 ✔
771
    bvarith64_buffer_prepare(b, 32); // any non-zero value would work
6,895 ✔
772
    stack->bva64buffer = b;
6,895 ✔
773
  } else if (stack->bva64buffer != b) {
2,285 ✔
774
    yices_free_bvarith64_buffer(b);
2,285 ✔
775
  }
776
}
9,180 ✔
777

778
static void recycle_bvabuffer(tstack_t *stack, bvarith_buffer_t *b) {
902 ✔
779
  if (stack->bvabuffer == NULL) {
902 ✔
780
    bvarith_buffer_prepare(b, 100); // any non-zero value would work
899 ✔
781
    stack->bvabuffer = b;
899 ✔
782
  } else if (stack->bvabuffer != b) {
3 ✔
783
    yices_free_bvarith_buffer(b);
3 ✔
784
  }
785
}
902 ✔
786

787
static void recycle_bvlbuffer(tstack_t *stack, bvlogic_buffer_t *b) {
60,630 ✔
788
  if (stack->bvlbuffer == NULL) {
60,630 ✔
789
    bvlogic_buffer_clear(b);
40,369 ✔
790
    stack->bvlbuffer = b;
40,369 ✔
791
  } else if (stack->bvlbuffer != b) {
20,261 ✔
792
    yices_free_bvlogic_buffer(b);
20,261 ✔
793
  }
794
}
60,630 ✔
795

796

797
/*
798
 * Make the auxiliary buffer large enough for n terms or types
799
 */
800
void extend_aux_buffer(tstack_t *stack, uint32_t n) {
48 ✔
801
  uint32_t new_size;
802

803
  assert (stack->aux_size < n);
804

805
  new_size = stack->aux_size + 1;
48 ✔
806
  new_size += new_size;
48 ✔
807
  if (new_size < n) new_size = n;
48 ✔
808

809
  if (new_size  >= MAX_AUX_SIZE) {
48 ✔
810
    out_of_memory();
×
811
  }
812

813
  stack->aux_buffer = (int32_t *) safe_realloc(stack->aux_buffer, new_size * sizeof(int32_t));
48 ✔
814
  stack->aux_size  = new_size;
48 ✔
815
}
48 ✔
816

817

818
/*
819
 * Make the symbol buffer large enough for n symbols
820
 */
821
void extend_sbuffer(tstack_t *stack, uint32_t n) {
11 ✔
822
  uint32_t new_size;
823

824
  assert(stack->sbuffer_size < n);
825

826
  new_size = stack->sbuffer_size + 1;
11 ✔
827
  new_size += new_size;
11 ✔
828
  if (new_size < n) new_size = n;
11 ✔
829

830
  if (new_size > MAX_SBUFFER_SIZE) {
11 ✔
831
    out_of_memory();
×
832
  }
833

834
  stack->sbuffer = (signed_symbol_t *) safe_realloc(stack->sbuffer, new_size * sizeof(signed_symbol_t));
11 ✔
835
  stack->sbuffer_size = new_size;
11 ✔
836
}
11 ✔
837

838

839
/*
840
 * Make the name buffer large enough for n names
841
 */
842
void extend_name_buffer(tstack_t *stack, uint32_t n) {
1,018 ✔
843
  uint32_t new_size;
844

845
  assert(stack->name_buffer_size < n);
846

847
  new_size = stack->name_buffer_size + 1;
1,018 ✔
848
  new_size += new_size;
1,018 ✔
849
  if (new_size < n) new_size = n;
1,018 ✔
850

851
  if (new_size > MAX_NAME_BUFFER_SIZE) {
1,018 ✔
852
    out_of_memory();
×
853
  }
854

855
  stack->name_buffer = (char **) safe_realloc(stack->name_buffer, new_size * sizeof(char *));
1,018 ✔
856
  stack->name_buffer_size = new_size;
1,018 ✔
857
}
1,018 ✔
858

859

860

861

862

863

864
/*********************
865
 *  POP OPERATIONS   *
866
 ********************/
867

868
/*
869
 * Cleanup object e (before it gets removed from the stack)
870
 */
871
static void tstack_free_val(tstack_t *stack, stack_elem_t *e) {
3,042,818 ✔
872
  uint32_t k;
873

874
  switch (e->tag) {
3,042,818 ✔
875
  case TAG_BV:
2,945 ✔
876
    k = (e->val.bv.bitsize + 31) >> 5;
2,945 ✔
877
    bvconst_free(e->val.bv.data, k);
2,945 ✔
878
    break;
2,945 ✔
879
  case TAG_FINITEFIELD:
405 ✔
880
    q_clear(&e->val.ff.val);
405 ✔
881
    q_clear(&e->val.ff.mod);
405 ✔
882
    break;
405 ✔
883
  case TAG_RATIONAL:
324,894 ✔
884
    q_clear(&e->val.rational);
324,894 ✔
885
    break;
324,894 ✔
886
  case TAG_ATTRIBUTE:
1 ✔
887
    assert(stack->avtbl != NULL);
888
    if (e->val.aval != AVAL_NULL) {
1 ✔
889
      aval_decref(stack->avtbl, e->val.aval);
1 ✔
890
    }
891
    break;
1 ✔
892
  case TAG_ARITH_BUFFER:
32,475 ✔
893
    recycle_abuffer(stack, e->val.arith_buffer);
32,475 ✔
894
    break;
32,475 ✔
895
  case TAG_ARITH_FF_BUFFER:
456 ✔
896
    recycle_abuffer(stack, e->val.mod_arith_buffer.b);
456 ✔
897
    q_clear(&e->val.mod_arith_buffer.mod);
456 ✔
898
    break;
456 ✔
899
  case TAG_BVARITH64_BUFFER:
9,180 ✔
900
    recycle_bva64buffer(stack, e->val.bvarith64_buffer);
9,180 ✔
901
    break;
9,180 ✔
902
  case TAG_BVARITH_BUFFER:
902 ✔
903
    recycle_bvabuffer(stack, e->val.bvarith_buffer);
902 ✔
904
    break;
902 ✔
905
  case TAG_BVLOGIC_BUFFER:
60,618 ✔
906
    recycle_bvlbuffer(stack, e->val.bvlogic_buffer);
60,618 ✔
907
    break;
60,618 ✔
908
  case TAG_BINDING:
205,765 ✔
909
    _o_yices_remove_term_name(e->val.binding.symbol);
205,765 ✔
910
    break;
205,765 ✔
911
  case TAG_TYPE_BINDING:
×
912
    _o_yices_remove_type_name(e->val.type_binding.symbol);
×
913
    break;
×
914
  default:
2,405,177 ✔
915
    break; // prevent GCC warning
2,405,177 ✔
916
  }
917
}
3,042,818 ✔
918

919

920
/*
921
 * Empty the stack and clear error data
922
 */
923
void tstack_reset(tstack_t *stack) {
1,832 ✔
924
  stack_elem_t *e;
925
  uint32_t i;
926

927
  i = stack->top;
1,832 ✔
928
  e = stack->elem + i;
1,832 ✔
929
  while (i > 0) {
3,706 ✔
930
    i --;
1,874 ✔
931
    e --;
1,874 ✔
932
    tstack_free_val(stack, e);
1,874 ✔
933
  }
934

935
  arena_reset(&stack->mem);
1,832 ✔
936
  stack->top = 1;
1,832 ✔
937
  stack->frame = 0;
1,832 ✔
938
  stack->top_op = NO_OP;
1,832 ✔
939

940
  stack->tvar_id = 0;
1,832 ✔
941

942
  stack->error_op = NO_OP;
1,832 ✔
943
  stack->error_loc.line = 0;
1,832 ✔
944
  stack->error_loc.column = 0;
1,832 ✔
945
  stack->error_string = NULL;
1,832 ✔
946
}
1,832 ✔
947

948

949
/*
950
 * Remove the elements above the top-frame index
951
 * (i.e. all the parameters in the top frame, but not the operator)
952
 *
953
 * If top-op is not BIND or DECLARE_VAR or DECLARE_TYPE_VAR, also
954
 * close the arena scope.
955
 */
956
void tstack_pop_frame(tstack_t *stack) {
1,247,727 ✔
957
  uint32_t i, n;
958
  int32_t op;
959

960
  op = stack->top_op;
1,247,727 ✔
961
  n = stack->frame;
1,247,727 ✔
962

963
  assert(0 < n && n < stack->top);
964

965
  // restore previous frame and top_op
966
  i = stack->elem[n].val.opval.prev;
1,247,727 ✔
967
  stack->frame = i;
1,247,727 ✔
968
  stack->top_op = stack->elem[i].val.opval.opcode;
1,247,727 ✔
969

970
  // remove elements at indices n+1 to stack->top-1
971
  i = stack->top;
1,247,727 ✔
972
  n ++;
1,247,727 ✔
973
  while (i > n) {
3,892,231 ✔
974
    i --;
2,644,504 ✔
975
    tstack_free_val(stack, stack->elem + i);
2,644,504 ✔
976
  }
977
  stack->top = n;
1,247,727 ✔
978

979
  if (op != BIND && op != DECLARE_VAR && op != DECLARE_TYPE_VAR) {
1,247,727 ✔
980
    arena_pop(&stack->mem);
1,068,019 ✔
981
  }
982
}
1,247,727 ✔
983

984

985
/*
986
 * Copy v as result in place of the current stack->frame
987
 * then remove all elements above the top frame index.
988
 *
989
 * Cannot be used if v is a string/symbol.
990
 */
991
void copy_result_and_pop_frame(tstack_t *stack, stack_elem_t *v) {
190,815 ✔
992
  uint32_t i, n;
993
  int32_t op;
994

995
  op = stack->top_op;
190,815 ✔
996
  n = stack->frame;
190,815 ✔
997

998
  assert(0 < n && n < stack->top);
999
  assert(&stack->elem[n] < v && v < stack->elem + stack->top);
1000
  assert(v->tag != TAG_SYMBOL);
1001

1002
  // restore previous frame and top_op
1003
  i = stack->elem[n].val.opval.prev;
190,815 ✔
1004
  stack->frame = i;
190,815 ✔
1005
  stack->top_op = stack->elem[i].val.opval.opcode;
190,815 ✔
1006

1007

1008
  stack->elem[n] = *v;
190,815 ✔
1009
  v->tag = TAG_NONE; // prevent deletion of v's value (since it's copied in elem[n])
190,815 ✔
1010

1011
  // remove elements at indices n+1 to stack->top-1
1012
  i = stack->top;
190,815 ✔
1013
  n ++;
190,815 ✔
1014
  while (i > n) {
587,255 ✔
1015
    i --;
396,440 ✔
1016
    tstack_free_val(stack, stack->elem + i);
396,440 ✔
1017
  }
1018
  stack->top = n;
190,815 ✔
1019

1020
  if (op != BIND && op != DECLARE_VAR && op != DECLARE_TYPE_VAR) {
190,815 ✔
1021
    arena_pop(&stack->mem);
190,815 ✔
1022
  }
1023
}
190,815 ✔
1024

1025

1026
/*
1027
 * Replace the top element by its new value
1028
 * - to be used after tstack_pop_frame to replace the operator
1029
 *   by the result of the operation
1030
 * - keep the loc field unchanged
1031
 */
1032
void set_term_result(tstack_t *stack, term_t t) {
626,439 ✔
1033
  stack_elem_t *e;
1034

1035
  e = stack->elem + (stack->top - 1);
626,439 ✔
1036
  e->tag = TAG_TERM;
626,439 ✔
1037
  e->val.term = t;
626,439 ✔
1038
}
626,439 ✔
1039

1040
void set_special_term_result(tstack_t *stack, term_t t) {
89 ✔
1041
  stack_elem_t *e;
1042

1043
  e = stack->elem + (stack->top - 1);
89 ✔
1044
  e->tag = TAG_SPECIAL_TERM;
89 ✔
1045
  e->val.term = t;
89 ✔
1046
}
89 ✔
1047

1048
void set_type_result(tstack_t *stack, type_t tau) {
53,028 ✔
1049
  stack_elem_t *e;
1050

1051
  e = stack->elem + (stack->top - 1);
53,028 ✔
1052
  e->tag = TAG_TYPE;
53,028 ✔
1053
  e->val.type = tau;
53,028 ✔
1054
}
53,028 ✔
1055

1056
// b must be equal to stack->abuffer. We reset stack->abuffer to NULL
1057
void set_arith_result(tstack_t *stack, rba_buffer_t *b) {
30,500 ✔
1058
  stack_elem_t *e;
1059

1060
  assert(b == stack->abuffer);
1061
  stack->abuffer = NULL;
30,500 ✔
1062

1063
  e = stack->elem + (stack->top - 1);
30,500 ✔
1064
  e->tag = TAG_ARITH_BUFFER;
30,500 ✔
1065
  e->val.arith_buffer = b;
30,500 ✔
1066
}
30,500 ✔
1067

1068
// b must be equal to stack->abuffer. We reset stack->abuffer to NULL
1069
void set_arith_ff_result(tstack_t *stack, rba_buffer_t *b, rational_t *mod) {
456 ✔
1070
  stack_elem_t *e;
1071

1072
  assert(b == stack->abuffer);
1073
  stack->abuffer = NULL;
456 ✔
1074

1075
  assert(q_is_pos(mod));
1076

1077
  e = stack->elem + (stack->top - 1);
456 ✔
1078
  e->tag = TAG_ARITH_FF_BUFFER;
456 ✔
1079
  e->val.mod_arith_buffer.b = b;
456 ✔
1080
  q_init(&e->val.mod_arith_buffer.mod);
456 ✔
1081
  q_set(&e->val.mod_arith_buffer.mod, mod);
456 ✔
1082
}
456 ✔
1083

1084
// b must be stack->bva64buffer
1085
void set_bvarith64_result(tstack_t *stack, bvarith64_buffer_t *b) {
8,887 ✔
1086
  stack_elem_t *e;
1087

1088
  assert(b == stack->bva64buffer);
1089
  stack->bva64buffer = NULL;
8,887 ✔
1090

1091
  e = stack->elem + (stack->top - 1);
8,887 ✔
1092
  e->tag = TAG_BVARITH64_BUFFER;
8,887 ✔
1093
  e->val.bvarith64_buffer = b;
8,887 ✔
1094
}
8,887 ✔
1095

1096
// b must be stack->bvabuffer
1097
void set_bvarith_result(tstack_t *stack, bvarith_buffer_t *b) {
861 ✔
1098
  stack_elem_t *e;
1099

1100
  assert(b == stack->bvabuffer);
1101
  stack->bvabuffer = NULL;
861 ✔
1102

1103
  e = stack->elem + (stack->top - 1);
861 ✔
1104
  e->tag = TAG_BVARITH_BUFFER;
861 ✔
1105
  e->val.bvarith_buffer = b;
861 ✔
1106
}
861 ✔
1107

1108
// b must be stack->bvlbuffer
1109
void set_bvlogic_result(tstack_t *stack, bvlogic_buffer_t *b) {
60,630 ✔
1110
  stack_elem_t *e;
1111

1112
  assert(b == stack->bvlbuffer);
1113
  stack->bvlbuffer = NULL;
60,630 ✔
1114

1115
  e = stack->elem + (stack->top - 1);
60,630 ✔
1116
  e->tag = TAG_BVLOGIC_BUFFER;
60,630 ✔
1117
  e->val.bvlogic_buffer = b;
60,630 ✔
1118
}
60,630 ✔
1119

1120
void set_binding_result(tstack_t *stack, term_t t, char *symbol) {
179,708 ✔
1121
  stack_elem_t *e;
1122

1123
  e = stack->elem + (stack->top - 1);
179,708 ✔
1124
  e->tag = TAG_BINDING;
179,708 ✔
1125
  e->val.binding.term = t;
179,708 ✔
1126
  e->val.binding.symbol = symbol;
179,708 ✔
1127
}
179,708 ✔
1128

1129
void set_bv64_result(tstack_t *stack, uint32_t nbits, uint64_t c) {
68,532 ✔
1130
  stack_elem_t *e;
1131

1132
  e = stack->elem + (stack->top - 1);
68,532 ✔
1133
  e->tag = TAG_BV64;
68,532 ✔
1134
  e->val.bv64.bitsize = nbits;
68,532 ✔
1135
  e->val.bv64.value = c;
68,532 ✔
1136
}
68,532 ✔
1137

1138
void set_bv_result(tstack_t *stack, uint32_t nbits, uint32_t *bv) {
2,910 ✔
1139
  stack_elem_t *e;
1140

1141
  e = stack->elem + (stack->top - 1);
2,910 ✔
1142
  e->tag = TAG_BV;
2,910 ✔
1143
  e->val.bv.bitsize = nbits;
2,910 ✔
1144
  e->val.bv.data = bv;
2,910 ✔
1145
}
2,910 ✔
1146

1147
void set_ff_result(tstack_t *stack, rational_t *r, rational_t *m) {
405 ✔
1148
  stack_elem_t *e;
1149

1150
  e = stack->elem + (stack->top - 1);
405 ✔
1151
  e->tag = TAG_FINITEFIELD;
405 ✔
1152
  q_init(&e->val.ff.val);
405 ✔
1153
  q_init(&e->val.ff.mod);
405 ✔
1154
  q_set(&e->val.ff.val, r);
405 ✔
1155
  q_set(&e->val.ff.mod, m);
405 ✔
1156
}
405 ✔
1157

1158
void set_type_binding_result(tstack_t *stack, type_t tau, char *symbol) {
×
1159
  stack_elem_t *e;
1160

1161
  e = stack->elem + (stack->top - 1);
×
1162
  e->tag = TAG_TYPE_BINDING;
×
1163
  e->val.type_binding.type = tau;
×
1164
  e->val.type_binding.symbol = symbol;
×
1165
}
×
1166

1167

1168
void set_aval_result(tstack_t *stack, aval_t v) {
1 ✔
1169
  stack_elem_t *e;
1170

1171
  assert(stack->avtbl != NULL);
1172

1173
  e = stack->elem + (stack->top - 1);
1 ✔
1174
  e->tag = TAG_ATTRIBUTE;
1 ✔
1175
  e->val.aval = v;
1 ✔
1176

1177
  if (v != AVAL_NULL) {
1 ✔
1178
    aval_incref(stack->avtbl, v);
1 ✔
1179
  }
1180
}
1 ✔
1181

1182

1183
#ifndef NDEBUG
1184

1185
/*
1186
 * Print element e (for debugging)
1187
 */
1188
void print_elem(tstack_t *stack, stack_elem_t *e) {
1189
  switch (e->tag) {
1190
  case TAG_NONE:
1191
    printf("<none>");
1192
    break;
1193

1194
  case TAG_OP:
1195
    printf("<op: code = %"PRId32", mult = %"PRIu32", prev = %"PRIu32">", e->val.opval.opcode,
1196
           e->val.opval.multiplicity,e->val.opval.prev);
1197
    break;
1198

1199
  case TAG_OPCODE:
1200
    printf("<opcode: %"PRId32">", e->val.op);
1201
    break;
1202

1203
  case TAG_SYMBOL:
1204
    printf("<symbol: %s>", e->val.string);
1205
    break;
1206

1207
  case TAG_NOT_SYMBOL:
1208
    printf("<not symbol: %s>", e->val.string);
1209
    break;
1210

1211
  case TAG_STRING:
1212
    printf("<string: %s>", e->val.string);
1213
    break;
1214

1215
  case TAG_BV64:
1216
    printf("<bitvector: ");
1217
    bvconst64_print(stdout, e->val.bv64.value, e->val.bv64.bitsize);
1218
    printf(">");
1219
    break;
1220

1221
  case TAG_BV:
1222
    printf("<bitvector: ");
1223
    bvconst_print(stdout, e->val.bv.data, e->val.bv.bitsize);
1224
    printf(">");
1225
    break;
1226

1227
  case TAG_RATIONAL:
1228
    printf("<rational: ");
1229
    q_print(stdout, &e->val.rational);
1230
    printf(">");
1231
    break;
1232

1233
  case TAG_FINITEFIELD:
1234
    printf("<finitefield: ");
1235
    q_print(stdout, &e->val.ff.val);
1236
    printf(" mod ");
1237
    q_print(stdout, &e->val.ff.mod);
1238
    printf(">");
1239
    break;
1240

1241
  case TAG_TERM:
1242
    printf("<term: ");
1243
    print_term_id(stdout, e->val.term);
1244
    printf(">");
1245
    break;
1246

1247
  case TAG_SPECIAL_TERM:
1248
    printf("<special-term: ");
1249
    print_term_id(stdout, e->val.term);
1250
    printf(">");
1251
    break;
1252

1253
  case TAG_TYPE:
1254
    printf("<type: ");
1255
    print_type_id(stdout, e->val.term);
1256
    printf(">");
1257
    break;
1258

1259
  case TAG_MACRO:
1260
    printf("<macro: %"PRId32">", e->val.macro);
1261
    break;
1262

1263
  case TAG_ARITH_BUFFER:
1264
    printf("<arith-buffer: ");
1265
    print_arith_buffer(stdout, e->val.arith_buffer);
1266
    printf(">");
1267
    break;
1268

1269
  case TAG_ARITH_FF_BUFFER:
1270
    printf("<arith-ff-buffer: ");
1271
    print_arith_buffer(stdout, e->val.mod_arith_buffer.b);
1272
    printf(" mod ");
1273
    q_print(stdout, &e->val.mod_arith_buffer.mod);
1274
    printf(">");
1275
    break;
1276

1277
  case TAG_BVARITH64_BUFFER:
1278
    printf("<bvarith64-buffer: ");
1279
    print_bvarith64_buffer(stdout, e->val.bvarith64_buffer);
1280
    printf(">");
1281
    break;
1282

1283
  case TAG_BVARITH_BUFFER:
1284
    printf("<bvarith-buffer: ");
1285
    print_bvarith_buffer(stdout, e->val.bvarith_buffer);
1286
    printf(">");
1287
    break;
1288

1289
  case TAG_BVLOGIC_BUFFER:
1290
    printf("<bvlogic-buffer: ");
1291
    print_bvlogic_buffer(stdout, e->val.bvlogic_buffer);
1292
    printf(">");
1293
    break;
1294

1295
  case TAG_BINDING:
1296
    printf("<binding: %s --> ", e->val.binding.symbol);
1297
    print_term_id(stdout, e->val.binding.term);
1298
    printf(">");
1299
    break;
1300

1301
  case TAG_TYPE_BINDING:
1302
    printf("<type-binding: %s --> ", e->val.type_binding.symbol);
1303
    print_type_id(stdout, e->val.type_binding.type);
1304
    printf(">");
1305
    break;
1306

1307
  case TAG_ATTRIBUTE:
1308
    // no attribute printing implemented yet
1309
  default:
1310
    printf("<error>");
1311
    break;
1312
  }
1313
}
1314

1315
#endif
1316

1317

1318
/***************************************
1319
 *  EVALUATION OF INDIVIDUAL COMMANDS  *
1320
 **************************************/
1321

1322
/*
1323
 * Auxiliary functions
1324
 */
1325
static int invalid_tag(tag_t tg) {
×
1326
  int error_code;
1327

1328
  switch (tg) {
×
1329
  case TAG_SYMBOL:
×
1330
    error_code = TSTACK_NOT_A_SYMBOL;
×
1331
    break;
×
1332

1333
  case TAG_RATIONAL:
×
1334
    error_code = TSTACK_NOT_A_RATIONAL;
×
1335
    break;
×
1336

1337
  case TAG_TYPE:
×
1338
    error_code = TSTACK_NOT_A_TYPE;
×
1339
    break;
×
1340

1341
  default:
×
1342
    error_code = TSTACK_INTERNAL_ERROR;
×
1343
  }
1344

1345
  return error_code;
×
1346
}
1347

1348
void check_tag(tstack_t *stack, stack_elem_t *e, tag_t tg) {
883,035 ✔
1349
  if (e->tag != tg) raise_exception(stack, e, invalid_tag(tg));
883,035 ✔
1350
}
883,035 ✔
1351

1352
void check_op(tstack_t *stack, int32_t op) {
1,438,451 ✔
1353
  if (stack->top_op != op) {
1,438,451 ✔
1354
    raise_exception(stack, stack->elem + stack->frame, TSTACK_INTERNAL_ERROR);
×
1355
  }
1356
}
1,438,451 ✔
1357

1358
void check_size(tstack_t *stack, bool cond) {
1,614,875 ✔
1359
  if (! cond) {
1,614,875 ✔
1360
    raise_exception(stack, stack->elem + stack->frame, TSTACK_INVALID_FRAME);
×
1361
  }
1362
}
1,614,875 ✔
1363

1364
void check_all_tags(tstack_t *stack, stack_elem_t *e, stack_elem_t *end, tag_t tg) {
268,133 ✔
1365
  while (e < end) {
552,439 ✔
1366
    check_tag(stack, e, tg);
284,306 ✔
1367
    e ++;
284,306 ✔
1368
  }
1369
}
268,133 ✔
1370

1371
static void check_type(tstack_t *stack, type_t tau) {
45,305 ✔
1372
  if (tau == NULL_TYPE) report_yices_error(stack);
45,305 ✔
1373
}
45,305 ✔
1374

1375
static void check_term(tstack_t *stack, term_t t) {
364,496 ✔
1376
  if (t == NULL_TERM) report_yices_error(stack);
364,496 ✔
1377
}
364,496 ✔
1378

1379

1380

1381
/*
1382
 * Check whether n string s1...sn are all distinct (for small n).
1383
 */
1384
// pair: string + hash code
1385
typedef struct tagged_string_s {
1386
  uint32_t hash;
1387
  char *string;
1388
} tagged_string_t;
1389

1390

1391
/*
1392
 * Add string s to array a, (as last element)
1393
 * n = number of elements currently in a
1394
 * return true if s is already in the array, false otherwise
1395
 */
1396
static bool check_duplicate_string(tagged_string_t *a, int32_t n, char *s) {
3,280 ✔
1397
  uint32_t h;
1398
  int32_t i;
1399

1400
  h = jenkins_hash_string(s);
3,280 ✔
1401
  for (i=0; i<n; i++) {
14,160 ✔
1402
    if (a[i].hash == h && strcmp(s, a[i].string) == 0) {
10,880 ✔
1403
      return true;
×
1404
    }
1405
  }
1406
  a[i].hash = h;
3,280 ✔
1407
  a[i].string = s;
3,280 ✔
1408
  return false;
3,280 ✔
1409
}
1410

1411

1412
/*
1413
 * Check whether all names in a scalar-type definition are distinct
1414
 * - names are in f[0] .. f[n-1]
1415
 * - all are symbols
1416
 * - n is positive
1417
 */
1418
static void check_distinct_scalar_names(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10 ✔
1419
  uint32_t i;
1420
  tagged_string_t check[n];
10 ✔
1421

1422
  // check for duplicate strings in the sequence
1423
  for (i=0; i<n; i++) {
24 ✔
1424
    assert(f[i].tag == TAG_SYMBOL);
1425
    if (check_duplicate_string(check, i, f[i].val.string)) {
14 ✔
1426
      raise_exception(stack, f+i, TSTACK_DUPLICATE_SCALAR_NAME);
×
1427
    }
1428
  }
1429
}
10 ✔
1430

1431

1432
/*
1433
 * Check whether all names in a list of variable bindings are distinct
1434
 * - names are in f[0] .. f[n-1]
1435
 * - all are bindings
1436
 *
1437
 * NOTE: the declaration check[n] causes the memory sanitizer to report a
1438
 * runtime error if n is 0.
1439
 */
1440
void check_distinct_binding_names(tstack_t *stack, stack_elem_t *f, uint32_t n) {
16,342 ✔
1441
  if (n > 0) {
16,342 ✔
1442
    uint32_t i;
1443
    tagged_string_t check[n];
2,521 ✔
1444

1445
    // check for duplicate strings in the sequence
1446
    for (i=0; i<n; i++) {
5,787 ✔
1447
      assert(f[i].tag == TAG_BINDING);
1448
      if (check_duplicate_string(check, i, f[i].val.binding.symbol)) {
3,266 ✔
1449
        raise_exception(stack, f+i, TSTACK_DUPLICATE_VAR_NAME);
×
1450
      }
1451
    }
1452
  }
1453
}
16,342 ✔
1454

1455

1456
/*
1457
 * Same thing for type-variable bindings
1458
 *
1459
 * NOTE: the declaration check[n] causes the memory sanitizer to report a
1460
 * runtime error if n is 0.
1461
 */
1462
void check_distinct_type_binding_names(tstack_t *stack, stack_elem_t *f, uint32_t n) {
77 ✔
1463
  if (n > 0) {
77 ✔
1464
    uint32_t i;
1465
    tagged_string_t check[n];
×
1466

1467
    // check for duplicate strings in the sequence
1468
    for (i=0; i<n; i++) {
×
1469
      assert(f[i].tag == TAG_TYPE_BINDING);
1470
      if (check_duplicate_string(check, i, f[i].val.type_binding.symbol)) {
×
1471
        raise_exception(stack, f+i, TSTACK_DUPLICATE_TYPE_VAR_NAME);
×
1472
      }
1473
    }
1474
  }
1475
}
77 ✔
1476

1477

1478
/*
1479
 * CONVERSIONS
1480
 */
1481

1482
/*
1483
 * Convert element e to a term or raise an exception
1484
 */
1485
term_t get_term(tstack_t *stack, stack_elem_t *e) {
1,583,788 ✔
1486
  uint64_t c;
1487
  term_t t;
1488

1489
  switch (e->tag) {
1,583,788 ✔
1490
  case TAG_TERM:
1,412,170 ✔
1491
  case TAG_SPECIAL_TERM:
1492
    t = e->val.term;
1,412,170 ✔
1493
    break;
1,412,170 ✔
1494

1495
  case TAG_SYMBOL:
2,162 ✔
1496
    t = _o_yices_get_term_by_name(e->val.string);
2,162 ✔
1497
    if (t == NULL_TERM) {
2,162 ✔
1498
      raise_exception(stack, e, TSTACK_UNDEF_TERM);
×
1499
    }
1500
    break;
2,162 ✔
1501

1502
  case TAG_STRING:
×
1503
    raise_exception(stack, e, TSTACK_STRINGS_ARE_NOT_TERMS);
×
1504
    break;
1505

1506
  case TAG_BV64:
62,167 ✔
1507
    c = norm64(e->val.bv64.value, e->val.bv64.bitsize);
62,167 ✔
1508
    t = yices_bvconst64_term(e->val.bv64.bitsize, c);
62,167 ✔
1509
    break;
62,167 ✔
1510

1511
  case TAG_BV:
1,588 ✔
1512
    bvconst_normalize(e->val.bv.data, e->val.bv.bitsize);
1,588 ✔
1513
    t = yices_bvconst_term(e->val.bv.bitsize, e->val.bv.data);
1,588 ✔
1514
    break;
1,588 ✔
1515

1516
  case TAG_FINITEFIELD:
405 ✔
1517
    t = yices_ffconst_term(&e->val.ff.val, &e->val.ff.mod);
405 ✔
1518
    break;
405 ✔
1519

1520
  case TAG_RATIONAL:
35,074 ✔
1521
    t = yices_rational_term(&e->val.rational);
35,074 ✔
1522
    break;
35,074 ✔
1523

1524
  case TAG_ARITH_BUFFER:
18,435 ✔
1525
    t = arith_buffer_get_term(e->val.arith_buffer);
18,435 ✔
1526
    break;
18,435 ✔
1527

1528
  case TAG_ARITH_FF_BUFFER:
456 ✔
1529
    t = arith_ff_buffer_get_term(e->val.mod_arith_buffer.b, &e->val.mod_arith_buffer.mod);
456 ✔
1530
    break;
456 ✔
1531

1532
  case TAG_BVARITH64_BUFFER:
7,816 ✔
1533
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
7,816 ✔
1534
    break;
7,816 ✔
1535

1536
  case TAG_BVARITH_BUFFER:
879 ✔
1537
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
879 ✔
1538
    break;
879 ✔
1539

1540
  case TAG_BVLOGIC_BUFFER:
42,636 ✔
1541
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
42,636 ✔
1542
    break;
42,636 ✔
1543

1544
  default:
×
1545
    raise_exception(stack, e, TSTACK_INTERNAL_ERROR);
×
1546
    break;
1547
  }
1548

1549
  return t;
1,583,788 ✔
1550
}
1551

1552

1553

1554
/*
1555
 * Return integer value of e (e must have rational tag)
1556
 * Raise an exception if e is too large or is not an integer.
1557
 */
1558
int32_t get_integer(tstack_t *stack, stack_elem_t *e) {
179,303 ✔
1559
  int32_t v;
1560

1561
  assert(e->tag == TAG_RATIONAL);
1562

1563
  if (q_get32(&e->val.rational, &v)) {
179,303 ✔
1564
    return v;
179,303 ✔
1565
  }
1566

1567
  if (q_is_integer(&e->val.rational)) {
×
1568
    raise_exception(stack, e, TSTACK_INTEGER_OVERFLOW);
×
1569
  } else {
1570
    raise_exception(stack, e, TSTACK_NOT_AN_INTEGER);
×
1571
  }
1572
}
1573

1574
/*
1575
 * Return integer value of e as mpz integer (e must have rational tag)
1576
 * Raise an exception if e is not an integer.
1577
 */
1578
void get_integer_mpz(tstack_t *stack, stack_elem_t *e, mpz_t z) {
12 ✔
1579
  assert(e->tag == TAG_RATIONAL);
1580

1581
  if (! q_get_mpz(&e->val.rational, z)) {
12 ✔
1582
    raise_exception(stack, e, TSTACK_NOT_AN_INTEGER);
×
1583
  }
1584
}
12 ✔
1585

1586
/*
1587
 * Support for division: return a rational constant equal to den
1588
 * provided den is constant and non-zero
1589
 */
1590
rational_t *get_divisor(tstack_t *stack, stack_elem_t *den) {
×
1591
  rational_t *d;
1592
  term_t t;
1593
  rba_buffer_t *c;
1594
  term_table_t *terms;
1595
  mono_t *m;
1596

1597
  switch (den->tag) {
×
1598
  case TAG_RATIONAL:
×
1599
    d = &den->val.rational;
×
1600
    if (q_is_zero(d)) {
×
1601
      raise_exception(stack, den, TSTACK_DIVIDE_BY_ZERO);
×
1602
    }
1603
    break;
×
1604

1605
  case TAG_TERM:
×
1606
  case TAG_SPECIAL_TERM:
1607
    terms = __yices_globals.terms;
×
1608
    t = den->val.term;
×
1609
    if (term_kind(terms, t) == ARITH_CONSTANT) {
×
1610
      d = rational_term_desc(terms, t);
×
1611
      if (q_is_zero(d)) {
×
1612
        raise_exception(stack, den, TSTACK_DIVIDE_BY_ZERO);
×
1613
      }
1614
    } else if (is_arithmetic_term(terms, t)) {
×
1615
      raise_exception(stack, den, TSTACK_NON_CONSTANT_DIVISOR);
×
1616
    } else {
1617
      raise_exception(stack, den, TSTACK_ARITH_ERROR);
×
1618
    }
1619
    break;
×
1620

1621
  case TAG_ARITH_BUFFER:
×
1622
    c = den->val.arith_buffer;
×
1623
    if (rba_buffer_is_constant(c)) {
×
1624
      m = rba_buffer_get_constant_mono(c);
×
1625
      if (m == NULL) {
×
1626
        assert(rba_buffer_is_zero(c));
1627
        raise_exception(stack, den, TSTACK_DIVIDE_BY_ZERO);
×
1628
      }
1629
      d = &m->coeff;
×
1630
    } else {
1631
      raise_exception(stack, den, TSTACK_NON_CONSTANT_DIVISOR);
×
1632
    }
1633
    break;
×
1634

1635
  default:
×
1636
    raise_exception(stack, den, TSTACK_ARITH_ERROR);
×
1637
    break;
1638
  }
1639

1640
  return d;
×
1641
}
1642

1643

1644
/*
1645
 * Variant: Check whether e stores a non-zero rational constant
1646
 * If so, store the value in result.
1647
 */
1648
bool elem_is_nz_constant(stack_elem_t *e, rational_t *result) {
3,809 ✔
1649
  rational_t *d;
1650
  term_t t;
1651
  bool ok;
1652
  rba_buffer_t *c;
1653
  term_table_t *terms;
1654
  mono_t *m;
1655

1656
  ok = false;
3,809 ✔
1657

1658
  switch (e->tag) {
3,809 ✔
1659
  case TAG_RATIONAL:
893 ✔
1660
    d = &e->val.rational;
893 ✔
1661
    if (q_is_nonzero(d)) {
893 ✔
1662
      q_set(result, d);
890 ✔
1663
      ok = true;
890 ✔
1664
    }
1665
    break;
893 ✔
1666

1667
  case TAG_TERM:
2,007 ✔
1668
  case TAG_SPECIAL_TERM:
1669
    terms = __yices_globals.terms;
2,007 ✔
1670
    t = e->val.term;
2,007 ✔
1671
    if (term_kind(terms, t) == ARITH_CONSTANT) {
2,007 ✔
1672
      d = rational_term_desc(terms, t);
241 ✔
1673
      if (q_is_nonzero(d)) {
241 ✔
1674
        q_set(result, d);
241 ✔
1675
        ok = true;
241 ✔
1676
      }
1677
    }
1678
    break;
2,007 ✔
1679

1680
  case TAG_ARITH_BUFFER:
909 ✔
1681
    c = e->val.arith_buffer;
909 ✔
1682
    if (rba_buffer_is_constant(c)) {
909 ✔
1683
      m = rba_buffer_get_constant_mono(c);
1 ✔
1684
      if (m != NULL) {
1 ✔
1685
        assert(q_is_nonzero(&m->coeff));
1686
        q_set(result, &m->coeff);
×
1687
        ok = true;
×
1688
      }
1689
    }
1690
    break;
909 ✔
1691

1692
  default:
×
1693
    break;
×
1694
  }
1695

1696
  return ok;
3,809 ✔
1697
}
1698

1699

1700
/*
1701
 * Convert e to a signed symbol (i.e., pair name/polarity).
1702
 * This does not make a copy of the string.
1703
 * Raise an exception if e has tag other than TAG_SYMBOL and TAG_NOT_SYMBOL.
1704
 */
1705
void get_signed_symbol(tstack_t *stack, stack_elem_t *e, signed_symbol_t *s) {
28 ✔
1706
  switch (e->tag) {
28 ✔
1707
  case TAG_SYMBOL:
14 ✔
1708
    s->name = e->val.string;
14 ✔
1709
    s->polarity = true;
14 ✔
1710
    break;
14 ✔
1711

1712
  case TAG_NOT_SYMBOL:
14 ✔
1713
    s->name = e->val.string;
14 ✔
1714
    s->polarity = false;
14 ✔
1715
    break;
14 ✔
1716

1717
  default:
×
1718
    raise_exception(stack, e, TSTACK_INTERNAL_ERROR);
×
1719
    break;
1720
  }
1721
}
28 ✔
1722

1723

1724

1725

1726

1727
/*
1728
 * Bitsize of element e
1729
 * - raise an exception if e is not a bitvector element
1730
 * - also raise an exception if e is an empty bvlogic buffer
1731
 */
1732
static uint32_t elem_bitsize(tstack_t *stack, stack_elem_t *e) {
27,169 ✔
1733
  term_t t;
1734
  uint32_t n;
1735

1736
  switch (e->tag) {
27,169 ✔
1737
  case TAG_BV64:
8,252 ✔
1738
    n = e->val.bv64.bitsize;
8,252 ✔
1739
    break;
8,252 ✔
1740

1741
  case TAG_BV:
76 ✔
1742
    n = e->val.bv.bitsize;
76 ✔
1743
    break;
76 ✔
1744

1745
  case TAG_TERM:
15,825 ✔
1746
  case TAG_SPECIAL_TERM:
1747
    t = e->val.term;
15,825 ✔
1748
    if (! yices_check_bv_term(t)) {
15,825 ✔
1749
      report_yices_error(stack);
×
1750
    }
1751
    n = term_bitsize(__yices_globals.terms, t);
15,825 ✔
1752
    break;
15,825 ✔
1753

1754
  case TAG_BVARITH64_BUFFER:
451 ✔
1755
    n = bvarith64_buffer_bitsize(e->val.bvarith64_buffer);
451 ✔
1756
    break;
451 ✔
1757

1758
  case TAG_BVARITH_BUFFER:
1 ✔
1759
    n = bvarith_buffer_bitsize(e->val.bvarith_buffer);
1 ✔
1760
    break;
1 ✔
1761

1762
  case TAG_BVLOGIC_BUFFER:
2,564 ✔
1763
    if (! yices_check_bvlogic_buffer(e->val.bvlogic_buffer)) {
2,564 ✔
1764
      report_yices_error(stack);
×
1765
    }
1766
    n = bvlogic_buffer_bitsize(e->val.bvlogic_buffer);
2,564 ✔
1767
    break;
2,564 ✔
1768

1769
  default:
×
1770
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
1771
    break;
1772
  }
1773

1774
  assert(0 < n && n <= YICES_MAX_BVSIZE);
1775

1776
  return n;
27,169 ✔
1777
}
1778

1779

1780
/*
1781
 * Get the i-th bit of element e
1782
 * - e must be a bitvector element
1783
 * - i must satisfy 0 <= i < n (where n = bitsize of e)
1784
 */
1785
static term_t elem_bit_select(tstack_t *stack, stack_elem_t *e, uint32_t i) {
50 ✔
1786
  term_t  t;
1787

1788
  switch (e->tag) {
50 ✔
1789
  case TAG_BV64:
×
1790
    assert(i < e->val.bv64.bitsize);
1791
    t = bool2term(tst_bit64(e->val.bv64.value, i));
×
1792
    break;
×
1793

1794
  case TAG_BV:
×
1795
    assert(i < e->val.bv.bitsize);
1796
    t = bool2term(bvconst_tst_bit(e->val.bv.data, i));
×
1797
    break;
×
1798

1799
  case TAG_TERM:
26 ✔
1800
  case TAG_SPECIAL_TERM:
1801
    t = _o_yices_bitextract(e->val.term, i);
26 ✔
1802
    break;
26 ✔
1803

1804
  case TAG_BVARITH64_BUFFER:
24 ✔
1805
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
24 ✔
1806
    t = _o_yices_bitextract(t, i);
24 ✔
1807
    break;
24 ✔
1808

1809
  case TAG_BVARITH_BUFFER:
×
1810
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
×
1811
    t = _o_yices_bitextract(t, i);
×
1812
    break;
×
1813

1814
  case TAG_BVLOGIC_BUFFER:
×
1815
    t = bvlogic_buffer_get_bit(e->val.bvlogic_buffer, i);
×
1816
    break;
×
1817

1818
  default:
×
1819
    raise_exception(stack, e, TSTACK_INTERNAL_ERROR);
×
1820
    break;
1821
  }
1822

1823
  return t;
50 ✔
1824
}
1825

1826

1827
/*
1828
 * Verify that element e is a bitvector term of bitsize equal to n
1829
 * - e must have tag = TAG_TERM or TAG_SPECIAL_TERM
1830
 * - raise an exception if t is not
1831
 */
1832
static void check_bv_term(tstack_t *stack, stack_elem_t *e, uint32_t n) {
17,344 ✔
1833
  term_t t;
1834

1835
  assert(e->tag == TAG_TERM || e->tag == TAG_SPECIAL_TERM);
1836
  t = e->val.term;
17,344 ✔
1837

1838
  if (! yices_check_bv_term(t)) {
17,344 ✔
1839
    report_yices_error(stack);
×
1840
  }
1841
  if (term_bitsize(__yices_globals.terms, t) != n) {
17,344 ✔
1842
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
1843
  }
1844
}
17,344 ✔
1845

1846

1847

1848
/*
1849
 * ARITHMETIC
1850
 */
1851

1852
/*
1853
 * Add arithmetic element e to buffer b. Raise an exception if e is not
1854
 * arithmetic or if the operation fails.
1855
 */
1856
void add_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
92,557 ✔
1857
  switch (e->tag) {
92,557 ✔
1858
  case TAG_RATIONAL:
16,833 ✔
1859
    rba_buffer_add_const(b, &e->val.rational);
16,833 ✔
1860
    break;
16,833 ✔
1861

1862
  case TAG_TERM:
63,761 ✔
1863
  case TAG_SPECIAL_TERM:
1864
    if (! yices_check_arith_term(e->val.term) && ! yices_check_arith_ff_term(e->val.term)) {
63,761 ✔
1865
      report_yices_error(stack);
×
1866
    }
1867
    rba_buffer_add_term(b, __yices_globals.terms, e->val.term);
63,761 ✔
1868
    break;
63,761 ✔
1869

1870
  case TAG_ARITH_BUFFER:
11,963 ✔
1871
    rba_buffer_add_buffer(b, e->val.arith_buffer);
11,963 ✔
1872
    break;
11,963 ✔
1873

1874
  default:
×
1875
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
1876
    break;
1877
  }
1878
}
92,557 ✔
1879

1880

1881
/*
1882
 * Negate element e (in place). Raise an exception if e is not an arithmetic term.
1883
 */
1884
void neg_elem(tstack_t *stack, stack_elem_t *e) {
11,878 ✔
1885
  rba_buffer_t *b;
1886
  term_table_t *terms;
1887
  term_t t;
1888

1889
  switch (e->tag) {
11,878 ✔
1890
  case TAG_RATIONAL:
8,568 ✔
1891
    q_neg(&e->val.rational);
8,568 ✔
1892
    break;
8,568 ✔
1893

1894
  case TAG_TERM:
3,178 ✔
1895
  case TAG_SPECIAL_TERM:
1896
    t = e->val.term;
3,178 ✔
1897
    terms = __yices_globals.terms;
3,178 ✔
1898
    if (! yices_check_arith_term(t)) {
3,178 ✔
1899
      report_yices_error(stack);
×
1900
    }
1901
    if (term_kind(terms, t) == ARITH_CONSTANT) {
3,178 ✔
1902
      // overwrite e: replace it by -(t's value)
1903
      e->tag = TAG_RATIONAL;
1,203 ✔
1904
      q_init(&e->val.rational);
1,203 ✔
1905
      q_set_neg(&e->val.rational, rational_term_desc(terms, t));
1,203 ✔
1906
    } else {
1907
      // compute -b
1908
      b = tstack_get_abuffer(stack);
1,975 ✔
1909
      assert(rba_buffer_is_zero(b));
1910
      rba_buffer_sub_term(b, __yices_globals.terms, e->val.term);
1,975 ✔
1911
      // overwrite e
1912
      e->tag = TAG_ARITH_BUFFER;
1,975 ✔
1913
      e->val.arith_buffer = b;
1,975 ✔
1914
      // reset stack->abuffer to NULL (cf. set_arith_result)
1915
      assert(b == stack->abuffer);
1916
      stack->abuffer = NULL;
1,975 ✔
1917
    }
1918
    break;
3,178 ✔
1919

1920
  case TAG_ARITH_BUFFER:
132 ✔
1921
    rba_buffer_negate(e->val.arith_buffer);
132 ✔
1922
    break;
132 ✔
1923

1924
  default:
×
1925
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
1926
    break;
1927
  }
1928
}
11,878 ✔
1929

1930

1931
/*
1932
 * Subtract element e from buffer b.
1933
 */
1934
void sub_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
49,970 ✔
1935
  switch (e->tag) {
49,970 ✔
1936
  case TAG_RATIONAL:
12,756 ✔
1937
    rba_buffer_sub_const(b, &e->val.rational);
12,756 ✔
1938
    break;
12,756 ✔
1939

1940
  case TAG_TERM:
34,956 ✔
1941
  case TAG_SPECIAL_TERM:
1942
    if (! yices_check_arith_term(e->val.term)) {
34,956 ✔
1943
      report_yices_error(stack);
×
1944
    }
1945
    rba_buffer_sub_term(b, __yices_globals.terms, e->val.term);
34,956 ✔
1946
    break;
34,956 ✔
1947

1948
  case TAG_ARITH_BUFFER:
2,258 ✔
1949
    rba_buffer_sub_buffer(b, e->val.arith_buffer);
2,258 ✔
1950
    break;
2,258 ✔
1951

1952
  default:
×
1953
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
1954
    break;
1955
  }
1956
}
49,970 ✔
1957

1958

1959
/*
1960
 * Product
1961
 */
1962
void mul_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
26,140 ✔
1963
  switch (e->tag) {
26,140 ✔
1964
  case TAG_RATIONAL:
8,298 ✔
1965
    rba_buffer_mul_const(b, &e->val.rational);
8,298 ✔
1966
    break;
8,298 ✔
1967

1968
  case TAG_TERM:
17,647 ✔
1969
  case TAG_SPECIAL_TERM:
1970
    if (! yices_check_arith_term(e->val.term) || ! yices_check_mul_term(b, e->val.term)) {
17,647 ✔
1971
      report_yices_error(stack);
1 ✔
1972
    }
1973
    rba_buffer_mul_term(b, __yices_globals.terms, e->val.term);
17,646 ✔
1974
    break;
17,646 ✔
1975

1976
  case TAG_ARITH_BUFFER:
195 ✔
1977
    if (! yices_check_mul_buffer(b, e->val.arith_buffer)) {
195 ✔
1978
      // degree overflow
1979
      report_yices_error(stack);
×
1980
    }
1981
    rba_buffer_mul_buffer(b, e->val.arith_buffer);
195 ✔
1982
    break;
195 ✔
1983

1984
  default:
×
1985
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
1986
    break;
1987
  }
1988
}
26,139 ✔
1989

1990
/*
1991
 * FINITE FIELD ARITHMETIC
1992
 */
1993

1994
void ff_add_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
816 ✔
1995

1996
  switch (e->tag) {
816 ✔
1997
  case TAG_FINITEFIELD:
×
1998
    rba_buffer_add_const(b, &e->val.ff.val);
×
1999
    break;
×
2000

2001
  case TAG_TERM:
816 ✔
2002
  case TAG_SPECIAL_TERM:
2003
    if (! yices_check_arith_ff_term(e->val.term)) {
816 ✔
2004
      report_yices_error(stack);
×
2005
    }
2006
    rba_buffer_add_term(b, __yices_globals.terms, e->val.term);
816 ✔
2007
    break;
816 ✔
2008

2009
  case TAG_ARITH_FF_BUFFER:
×
2010
    rba_buffer_add_buffer(b, e->val.mod_arith_buffer.b);
×
2011
    break;
×
2012

2013
  default:
×
2014
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
2015
    break;
2016
  }
2017
}
816 ✔
2018

2019
void ff_sub_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
×
2020

2021
  switch (e->tag) {
×
2022
  case TAG_FINITEFIELD:
×
2023
    rba_buffer_sub_const(b, &e->val.ff.val);
×
2024
    break;
×
2025

2026
  case TAG_TERM:
×
2027
  case TAG_SPECIAL_TERM:
2028
    if (! yices_check_arith_ff_term(e->val.term)) {
×
2029
      report_yices_error(stack);
×
2030
    }
2031
    rba_buffer_sub_term(b, __yices_globals.terms, e->val.term);
×
2032
    break;
×
2033

2034
  case TAG_ARITH_FF_BUFFER:
×
2035
    rba_buffer_sub_buffer(b, e->val.mod_arith_buffer.b);
×
2036
    break;
×
2037

2038
  default:
×
2039
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
2040
    break;
2041
  }
2042
}
×
2043

2044
void ff_mul_elem(tstack_t *stack, rba_buffer_t *b, stack_elem_t *e) {
1,104 ✔
2045

2046
  switch (e->tag) {
1,104 ✔
2047
  case TAG_FINITEFIELD:
×
2048
    rba_buffer_mul_const(b, &e->val.ff.val);
×
2049
    break;
×
2050

2051
  case TAG_TERM:
1,104 ✔
2052
  case TAG_SPECIAL_TERM:
2053
    if (! yices_check_arith_ff_term(e->val.term) || ! yices_check_mul_term(b, e->val.term)) {
1,104 ✔
2054
      report_yices_error(stack);
×
2055
    }
2056
    rba_buffer_mul_term(b, __yices_globals.terms, e->val.term);
1,104 ✔
2057
    break;
1,104 ✔
2058

2059
  case TAG_ARITH_FF_BUFFER:
×
2060
    if (! yices_check_mul_buffer(b, e->val.mod_arith_buffer.b)) {
×
2061
      // degree overflow
2062
      report_yices_error(stack);
×
2063
    }
2064
    rba_buffer_mul_buffer(b, e->val.mod_arith_buffer.b);
×
2065
    break;
×
2066

2067
  default:
×
2068
    raise_exception(stack, e, TSTACK_ARITH_ERROR);
×
2069
    break;
2070
  }
2071
}
1,104 ✔
2072

2073

2074
/*
2075
 * BIT-VECTOR ARITHMETIC: BITSIZE BETWEEN 1 and 64
2076
 */
2077

2078
/*
2079
 * Add element e to buffer b.
2080
 * - raise an exception if e is not a bitvector of equal size as b
2081
 */
2082
void bva64_add_elem(tstack_t *stack, bvarith64_buffer_t *b, stack_elem_t *e) {
15,423 ✔
2083
  uint32_t n;
2084
  term_t t;
2085

2086
  n = bvarith64_buffer_bitsize(b);
15,423 ✔
2087

2088
  assert(1 <= n && n <= 64);
2089

2090
  switch (e->tag) {
15,423 ✔
2091
  case TAG_BV64:
3,733 ✔
2092
    if (e->val.bv64.bitsize != n) {
3,733 ✔
2093
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2094
    }
2095
    bvarith64_buffer_add_const(b, e->val.bv64.value);
3,733 ✔
2096
    break;
3,733 ✔
2097

2098
  case TAG_BV:
×
2099
    assert(e->val.bv.bitsize != n);
2100
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2101
    break;
2102

2103
  case TAG_TERM:
9,197 ✔
2104
  case TAG_SPECIAL_TERM:
2105
    check_bv_term(stack, e, n);
9,197 ✔
2106
    bvarith64_buffer_add_term(b, __yices_globals.terms, e->val.term);
9,197 ✔
2107
    break;
9,197 ✔
2108

2109
  case TAG_BVARITH64_BUFFER:
835 ✔
2110
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
835 ✔
2111
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2112
    }
2113
    bvarith64_buffer_add_buffer(b, e->val.bvarith64_buffer);
835 ✔
2114
    break;
835 ✔
2115

2116
  case TAG_BVARITH_BUFFER:
×
2117
    assert(bvarith_buffer_bitsize(e->val.bvarith_buffer) != n);
2118
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2119
    break;
2120

2121
  case TAG_BVLOGIC_BUFFER:
1,658 ✔
2122
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
1,658 ✔
2123
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2124
    }
2125
    // convert e to a term then add to b
2126
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
1,658 ✔
2127
    bvarith64_buffer_add_term(b, __yices_globals.terms, t);
1,658 ✔
2128
    break;
1,658 ✔
2129

2130
  default:
×
2131
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2132
    break;
2133
  }
2134
}
15,423 ✔
2135

2136

2137
/*
2138
 * Subtract element e from buffer b.
2139
 * - raise an exception if e is not a bitvector of equal size as b
2140
 */
2141
void bva64_sub_elem(tstack_t *stack, bvarith64_buffer_t *b, stack_elem_t *e) {
2,313 ✔
2142
  uint32_t n;
2143
  term_t t;
2144

2145
  n = bvarith64_buffer_bitsize(b);
2,313 ✔
2146

2147
  assert(1 <= n && n <= 64);
2148

2149
  switch (e->tag) {
2,313 ✔
2150
  case TAG_BV64:
1,394 ✔
2151
    if (e->val.bv64.bitsize != n) {
1,394 ✔
2152
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2153
    }
2154
    bvarith64_buffer_sub_const(b, e->val.bv64.value);
1,394 ✔
2155
    break;
1,394 ✔
2156

2157
  case TAG_BV:
×
2158
    assert(e->val.bv.bitsize != n);
2159
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2160
    break;
2161

2162
  case TAG_TERM:
641 ✔
2163
  case TAG_SPECIAL_TERM:
2164
    check_bv_term(stack, e, n);
641 ✔
2165
    bvarith64_buffer_sub_term(b, __yices_globals.terms, e->val.term);
641 ✔
2166
    break;
641 ✔
2167

2168
  case TAG_BVARITH64_BUFFER:
26 ✔
2169
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
26 ✔
2170
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2171
    }
2172
    bvarith64_buffer_sub_buffer(b, e->val.bvarith64_buffer);
26 ✔
2173
    break;
26 ✔
2174

2175
  case TAG_BVARITH_BUFFER:
×
2176
    assert(bvarith_buffer_bitsize(e->val.bvarith_buffer) != n);
2177
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2178
    break;
2179

2180
  case TAG_BVLOGIC_BUFFER:
252 ✔
2181
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
252 ✔
2182
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2183
    }
2184
    // convert e to a term then add to b
2185
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
252 ✔
2186
    bvarith64_buffer_sub_term(b, __yices_globals.terms, t);
252 ✔
2187
    break;
252 ✔
2188

2189
  default:
×
2190
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2191
    break;
2192
  }
2193
}
2,313 ✔
2194

2195

2196

2197
/*
2198
 * Multiply b by element e
2199
 * - raise an exception if e is not a bitvector of equal size as b
2200
 *   or if there's a degree overflow
2201
 */
2202
void bva64_mul_elem(tstack_t *stack, bvarith64_buffer_t *b, stack_elem_t *e) {
1,065 ✔
2203
  term_table_t *terms;
2204
  uint32_t n;
2205
  term_t t;
2206

2207
  n = bvarith64_buffer_bitsize(b);
1,065 ✔
2208

2209
  assert(1 <= n && n <= 64);
2210

2211
  switch (e->tag) {
1,065 ✔
2212
  case TAG_BV64:
79 ✔
2213
    if (e->val.bv64.bitsize != n) {
79 ✔
2214
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2215
    }
2216
    bvarith64_buffer_mul_const(b, e->val.bv64.value);
79 ✔
2217
    break;
79 ✔
2218

2219
  case TAG_BV:
×
2220
    assert(e->val.bv.bitsize != n);
2221
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2222
    break;
2223

2224
  case TAG_TERM:
869 ✔
2225
  case TAG_SPECIAL_TERM:
2226
    check_bv_term(stack, e, n);
869 ✔
2227
    t = e->val.term;
869 ✔
2228
    terms = __yices_globals.terms;
869 ✔
2229
    if (! yices_check_bvmul64_term(b, t)) {
869 ✔
2230
      report_yices_error(stack);
×
2231
    }
2232
    bvarith64_buffer_mul_term(b, terms, t);
869 ✔
2233
    break;
869 ✔
2234

2235
  case TAG_BVARITH64_BUFFER:
×
2236
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
×
2237
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2238
    }
2239
    if (! yices_check_bvmul64_buffer(b, e->val.bvarith64_buffer)) {
×
2240
      report_yices_error(stack);
×
2241
    }
2242
    bvarith64_buffer_mul_buffer(b, e->val.bvarith64_buffer);
×
2243
    break;
×
2244

2245
  case TAG_BVARITH_BUFFER:
×
2246
    assert(bvarith_buffer_bitsize(e->val.bvarith_buffer) != n);
2247
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2248
    break;
2249

2250
  case TAG_BVLOGIC_BUFFER:
117 ✔
2251
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
117 ✔
2252
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2253
    }
2254
    // convert e to a term then add to b
2255
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
117 ✔
2256
    if (! yices_check_bvmul64_term(b, t)) {
117 ✔
2257
      report_yices_error(stack);
×
2258
    }
2259
    bvarith64_buffer_mul_term(b, __yices_globals.terms, t);
117 ✔
2260
    break;
117 ✔
2261

2262
  default:
×
2263
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2264
    break;
2265
  }
2266
}
1,065 ✔
2267

2268

2269

2270

2271

2272
/*
2273
 * BIT-VECTOR ARITHMETIC: BITSIZE > 64
2274
 */
2275

2276
/*
2277
 * Add element e to buffer b.
2278
 * - raise an exception if e is not a bitvector of equal size as b
2279
 */
2280
void bva_add_elem(tstack_t *stack, bvarith_buffer_t *b, stack_elem_t *e) {
1,526 ✔
2281
  uint32_t n;
2282
  term_t t;
2283

2284
  n = bvarith_buffer_bitsize(b);
1,526 ✔
2285

2286
  assert(n > 64);
2287

2288
  switch (e->tag) {
1,526 ✔
2289
  case TAG_BV64:
×
2290
    assert(e->val.bv64.bitsize != n);
2291
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2292
    break;
2293

2294
  case TAG_BV:
658 ✔
2295
    if (e->val.bv.bitsize != n) {
658 ✔
2296
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2297
    }
2298
    bvarith_buffer_add_const(b, e->val.bv.data);
658 ✔
2299
    break;
658 ✔
2300

2301
  case TAG_TERM:
775 ✔
2302
  case TAG_SPECIAL_TERM:
2303
    check_bv_term(stack, e, n);
775 ✔
2304
    bvarith_buffer_add_term(b, __yices_globals.terms, e->val.term);
775 ✔
2305
    break;
775 ✔
2306

2307
  case TAG_BVARITH64_BUFFER:
×
2308
    assert(bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n);
2309
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2310
    break;
2311

2312
  case TAG_BVARITH_BUFFER:
×
2313
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
×
2314
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2315
    }
2316
    bvarith_buffer_add_buffer(b, e->val.bvarith_buffer);
×
2317
    break;
×
2318

2319
  case TAG_BVLOGIC_BUFFER:
93 ✔
2320
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
93 ✔
2321
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2322
    }
2323
    // convert e to a term then add to b
2324
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
93 ✔
2325
    bvarith_buffer_add_term(b, __yices_globals.terms, t);
93 ✔
2326
    break;
93 ✔
2327

2328
  default:
×
2329
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2330
    break;
2331
  }
2332
}
1,526 ✔
2333

2334

2335
/*
2336
 * Subtract element e from buffer b.
2337
 * - raise an exception if e is not a bitvector of equal size as b
2338
 */
2339
void bva_sub_elem(tstack_t *stack, bvarith_buffer_t *b, stack_elem_t *e) {
140 ✔
2340
  uint32_t n;
2341
  term_t t;
2342

2343
  n = bvarith_buffer_bitsize(b);
140 ✔
2344

2345
  assert(n > 64);
2346

2347
  switch (e->tag) {
140 ✔
2348
  case TAG_BV64:
×
2349
    assert(e->val.bv64.bitsize != n);
2350
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2351
    break;
2352

2353
  case TAG_BV:
18 ✔
2354
    if (e->val.bv.bitsize != n) {
18 ✔
2355
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2356
    }
2357
    bvarith_buffer_sub_const(b, e->val.bv.data);
18 ✔
2358
    break;
18 ✔
2359

2360
  case TAG_TERM:
115 ✔
2361
  case TAG_SPECIAL_TERM:
2362
    check_bv_term(stack, e, n);
115 ✔
2363
    bvarith_buffer_sub_term(b, __yices_globals.terms, e->val.term);
115 ✔
2364
    break;
115 ✔
2365

2366
  case TAG_BVARITH64_BUFFER:
×
2367
    assert(bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n);
2368
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2369
    break;
2370

2371
  case TAG_BVARITH_BUFFER:
×
2372
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
×
2373
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2374
    }
2375
    bvarith_buffer_sub_buffer(b, e->val.bvarith_buffer);
×
2376
    break;
×
2377

2378
  case TAG_BVLOGIC_BUFFER:
7 ✔
2379
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
7 ✔
2380
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2381
    }
2382
    // convert e to a term then add to b
2383
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
7 ✔
2384
    bvarith_buffer_sub_term(b, __yices_globals.terms, t);
7 ✔
2385
    break;
7 ✔
2386

2387
  default:
×
2388
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2389
    break;
2390
  }
2391
}
140 ✔
2392

2393

2394
/*
2395
 * Multiply b by element e
2396
 * - raise an exception if e is not a bitvector of equal size as b
2397
 *   or if there's a degree overflow
2398
 */
2399
void bva_mul_elem(tstack_t *stack, bvarith_buffer_t *b, stack_elem_t *e) {
74 ✔
2400
  term_table_t *terms;
2401
  uint32_t n;
2402
  term_t t;
2403

2404
  n = bvarith_buffer_bitsize(b);
74 ✔
2405

2406
  assert(n > 64);
2407

2408
  switch (e->tag) {
74 ✔
2409
  case TAG_BV64:
×
2410
    assert(e->val.bv64.bitsize != n);
2411
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2412
    break;
2413

2414
  case TAG_BV:
×
2415
    if (e->val.bv.bitsize != n) {
×
2416
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2417
    }
2418
    bvarith_buffer_mul_const(b, e->val.bv.data);
×
2419
    break;
×
2420

2421
  case TAG_TERM:
53 ✔
2422
  case TAG_SPECIAL_TERM:
2423
    check_bv_term(stack, e, n);
53 ✔
2424
    t = e->val.term;
53 ✔
2425
    terms = __yices_globals.terms;
53 ✔
2426
    if (! yices_check_bvmul_term(b, t)) {
53 ✔
2427
      report_yices_error(stack);
×
2428
    }
2429
    bvarith_buffer_mul_term(b, terms, t);
53 ✔
2430
    break;
53 ✔
2431

2432
  case TAG_BVARITH64_BUFFER:
×
2433
    assert(bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n);
2434
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2435
    break;
2436

2437
  case TAG_BVARITH_BUFFER:
×
2438
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
×
2439
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2440
    }
2441
    if (! yices_check_bvmul_buffer(b, e->val.bvarith_buffer)) {
×
2442
      report_yices_error(stack);
×
2443
    }
2444
    bvarith_buffer_mul_buffer(b, e->val.bvarith_buffer);
×
2445
    break;
×
2446

2447
  case TAG_BVLOGIC_BUFFER:
21 ✔
2448
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
21 ✔
2449
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2450
    }
2451
    // convert e to a term then add to b
2452
    t = bvlogic_buffer_get_term(e->val.bvlogic_buffer);
21 ✔
2453
    if (! yices_check_bvmul_term(b, t)) {
21 ✔
2454
      report_yices_error(stack);
×
2455
    }
2456
    bvarith_buffer_mul_term(b, __yices_globals.terms, t);
21 ✔
2457
    break;
21 ✔
2458

2459
  default:
×
2460
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2461
    break;
2462
  }
2463
}
74 ✔
2464

2465

2466

2467
/*
2468
 * BV-NEG (for both 64/generic bitsizes)
2469
 */
2470

2471
/*
2472
 * Store the opposite of term t in e:
2473
 * - raise an exception if t is not a bitvector.
2474
 * - overwrite the current value of e.
2475
 */
2476
static void copy_bvneg_term(tstack_t *stack, stack_elem_t *e, term_t t) {
373 ✔
2477
  bvarith_buffer_t *b;
2478
  bvarith64_buffer_t *b64;
2479
  term_table_t *terms;
2480
  uint32_t *tmp;
2481
  uint32_t n, k;
2482

2483
  terms = __yices_globals.terms;
373 ✔
2484
  if (! yices_check_bv_term(t)) {
373 ✔
2485
    report_yices_error(stack);
×
2486
  }
2487

2488
  n = term_bitsize(terms, t);
373 ✔
2489

2490
  switch (term_kind(terms, t)) {
373 ✔
2491
  case BV64_CONSTANT:
32 ✔
2492
    // copy the opposite of t's value into e:
2493
    assert(n == bvconst64_term_desc(terms, t)->bitsize);
2494
    e->tag = TAG_BV64;
32 ✔
2495
    e->val.bv64.bitsize = n;
32 ✔
2496
    e->val.bv64.value = - bvconst64_term_desc(terms, t)->value;
32 ✔
2497
    break;
32 ✔
2498

2499
  case BV_CONSTANT:
7 ✔
2500
    assert(n == bvconst_term_desc(terms, t)->bitsize);
2501
    // allocate a buffer for the result
2502
    k = (n + 31) >> 5; // number of words
7 ✔
2503
    tmp = bvconst_alloc(k);
7 ✔
2504
    bvconst_negate2(tmp, k, bvconst_term_desc(terms, t)->data); // tmp := - data
7 ✔
2505
    // store the result as a BV element
2506
    e->tag = TAG_BV;
7 ✔
2507
    e->val.bv.bitsize = n;
7 ✔
2508
    e->val.bv.data = tmp;
7 ✔
2509
    break;
7 ✔
2510

2511
  default:
334 ✔
2512
    if (n <= 64) {
334 ✔
2513
      b64 = tstack_get_bva64buffer(stack, n);
293 ✔
2514
      assert(bvarith64_buffer_is_zero(b64));
2515
      bvarith64_buffer_sub_term(b64, terms, t);
293 ✔
2516

2517
      // overwrite e
2518
      e->tag = TAG_BVARITH64_BUFFER;
293 ✔
2519
      e->val.bvarith64_buffer = b64;
293 ✔
2520

2521
      // reset stack->bva64buffer to NULL
2522
      assert(b64 == stack->bva64buffer);
2523
      stack->bva64buffer = NULL;
293 ✔
2524

2525
    } else {
2526
      b = tstack_get_bvabuffer(stack, n);
41 ✔
2527
      assert(bvarith_buffer_is_zero(b));
2528
      bvarith_buffer_sub_term(b, terms, t);
41 ✔
2529

2530
      // overwrite e
2531
      e->tag = TAG_BVARITH_BUFFER;
41 ✔
2532
      e->val.bvarith_buffer = b;
41 ✔
2533

2534
      // reset stack->bvabuffer to NULL
2535
      assert(b == stack->bvabuffer);
2536
      stack->bvabuffer = NULL;
41 ✔
2537
    }
2538
    break;
334 ✔
2539
  }
2540

2541
}
373 ✔
2542

2543
/*
2544
 * Negate element e in place. Raise an exception if e is not a bitvector element.
2545
 */
2546
void bvneg_elem(tstack_t *stack, stack_elem_t *e) {
690 ✔
2547
  bvlogic_buffer_t *b;
2548
  uint32_t k;
2549
  term_t t;
2550

2551
  switch (e->tag) {
690 ✔
2552
  case TAG_BV64:
150 ✔
2553
    e->val.bv64.value = - e->val.bv64.value;
150 ✔
2554
    break;
150 ✔
2555

2556
  case TAG_BV:
35 ✔
2557
    k = (e->val.bv.bitsize + 31) >> 5; // number of words
35 ✔
2558
    bvconst_negate(e->val.bv.data, k);
35 ✔
2559
    break;
35 ✔
2560

2561
  case TAG_TERM:
361 ✔
2562
  case TAG_SPECIAL_TERM:
2563
    t = e->val.term;
361 ✔
2564
    copy_bvneg_term(stack, e, t);
361 ✔
2565
    break;
361 ✔
2566

2567
  case TAG_BVARITH64_BUFFER:
130 ✔
2568
    bvarith64_buffer_negate(e->val.bvarith64_buffer);
130 ✔
2569
    break;
130 ✔
2570

2571
  case TAG_BVARITH_BUFFER:
2 ✔
2572
    bvarith_buffer_negate(e->val.bvarith_buffer);
2 ✔
2573
    break;
2 ✔
2574

2575
  case TAG_BVLOGIC_BUFFER:
12 ✔
2576
    b = e->val.bvlogic_buffer;
12 ✔
2577
    if (! yices_check_bvlogic_buffer(b)){
12 ✔
2578
      report_yices_error(stack);
×
2579
    }
2580
    // convert to a term then negate.
2581
    t = bvlogic_buffer_get_term(b);
12 ✔
2582
    recycle_bvlbuffer(stack, b); // since e is going to be overwritten
12 ✔
2583
    copy_bvneg_term(stack, e, t);
12 ✔
2584
    break;
12 ✔
2585

2586
  default:
×
2587
    raise_exception(stack, e, TSTACK_BVARITH_ERROR);
×
2588
    break;
2589
  }
2590
}
690 ✔
2591

2592

2593

2594

2595
/*
2596
 * BITVECTOR LOGICAL OPERATIONS
2597
 */
2598

2599
/*
2600
 * Copy element e in bvlogic buffer b
2601
 * Raise an exception if e is not a bitvector
2602
 */
2603
void bvl_set_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
40,070 ✔
2604
  term_t t;
2605

2606
  switch (e->tag) {
40,070 ✔
2607
  case TAG_BV64:
2,616 ✔
2608
    bvlogic_buffer_set_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
2,616 ✔
2609
    break;
2,616 ✔
2610

2611
  case TAG_BV:
566 ✔
2612
    bvlogic_buffer_set_constant(b, e->val.bv.bitsize, e->val.bv.data);
566 ✔
2613
    break;
566 ✔
2614

2615
  case TAG_TERM:
31,110 ✔
2616
  case TAG_SPECIAL_TERM:
2617
    t = e->val.term;
31,110 ✔
2618
    if (! yices_check_bv_term(t)) { // not a bitvector
31,110 ✔
2619
      report_yices_error(stack);
×
2620
    }
2621
    bvlogic_buffer_set_term(b, __yices_globals.terms, t);
31,110 ✔
2622
    break;
31,110 ✔
2623

2624
  case TAG_BVARITH64_BUFFER:
230 ✔
2625
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
230 ✔
2626
    bvlogic_buffer_set_term(b, __yices_globals.terms, t);
230 ✔
2627
    break;
230 ✔
2628

2629
  case TAG_BVARITH_BUFFER:
11 ✔
2630
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
11 ✔
2631
    bvlogic_buffer_set_term(b, __yices_globals.terms, t);
11 ✔
2632
    break;
11 ✔
2633

2634
  case TAG_BVLOGIC_BUFFER:
5,537 ✔
2635
    bvlogic_buffer_set_buffer(b, e->val.bvlogic_buffer);
5,537 ✔
2636
    break;
5,537 ✔
2637

2638
  default:
×
2639
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
2640
    break;
2641
  }
2642
}
40,070 ✔
2643

2644

2645

2646
/*
2647
 * Copy element segment [i .. j] of e into bvlogic buffer b
2648
 * Raise an exception if e is not a bitvector.
2649
 * - i and j must be valid (i.e., 0 <= i <= j < e's bitsize)
2650
 */
2651
void bvl_set_slice_elem(tstack_t *stack, bvlogic_buffer_t *b, uint32_t i, uint32_t j, stack_elem_t *e) {
16,047 ✔
2652
  term_t t;
2653

2654
  assert(i <= j);
2655

2656
  switch (e->tag) {
16,047 ✔
2657
  case TAG_BV64:
6,416 ✔
2658
    assert(j < e->val.bv64.bitsize);
2659
    bvlogic_buffer_set_slice_constant64(b, i, j, e->val.bv64.value);
6,416 ✔
2660
    break;
6,416 ✔
2661

2662
  case TAG_BV:
2 ✔
2663
    assert(j < e->val.bv.bitsize);
2664
    bvlogic_buffer_set_slice_constant(b, i, j, e->val.bv.data);
2 ✔
2665
    break;
2 ✔
2666

2667
  case TAG_TERM:
7,910 ✔
2668
  case TAG_SPECIAL_TERM:
2669
    t = e->val.term;
7,910 ✔
2670
    if (! yices_check_bv_term(t)) { // not a bitvector
7,910 ✔
2671
      report_yices_error(stack);
×
2672
    }
2673
    bvlogic_buffer_set_slice_term(b, __yices_globals.terms, i, j, t);
7,910 ✔
2674
    break;
7,910 ✔
2675

2676
  case TAG_BVARITH64_BUFFER:
123 ✔
2677
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
123 ✔
2678
    bvlogic_buffer_set_slice_term(b, __yices_globals.terms, i, j, t);
123 ✔
2679
    break;
123 ✔
2680

2681
  case TAG_BVARITH_BUFFER:
1 ✔
2682
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
1 ✔
2683
    bvlogic_buffer_set_slice_term(b, __yices_globals.terms, i, j, t);
1 ✔
2684
    break;
1 ✔
2685

2686
  case TAG_BVLOGIC_BUFFER:
1,595 ✔
2687
    bvlogic_buffer_set_slice_buffer(b, i, j, e->val.bvlogic_buffer);
1,595 ✔
2688
    break;
1,595 ✔
2689

2690
  default:
×
2691
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
2692
    break;
2693
  }
2694
}
16,047 ✔
2695

2696

2697

2698
/*
2699
 * Check whether e is a bitvector constant
2700
 */
2701
bool elem_is_bvconst(stack_elem_t *e) {
5,325 ✔
2702
  term_kind_t k;
2703

2704
  switch (e->tag) {
5,325 ✔
2705
  case TAG_BV64:
3,135 ✔
2706
  case TAG_BV:
2707
    return true;
3,135 ✔
2708

2709
  case TAG_TERM:
1,046 ✔
2710
  case TAG_SPECIAL_TERM:
2711
    k = term_kind(__yices_globals.terms, e->val.term);
1,046 ✔
2712
    return k == BV64_CONSTANT || k == BV_CONSTANT;
1,046 ✔
2713

2714
  case TAG_BVARITH64_BUFFER:
1 ✔
2715
    bvarith64_buffer_normalize(e->val.bvarith64_buffer);
1 ✔
2716
    return bvarith64_buffer_is_constant(e->val.bvarith64_buffer);
1 ✔
2717

2718
  case TAG_BVARITH_BUFFER:
×
2719
    bvarith_buffer_normalize(e->val.bvarith_buffer);
×
2720
    return bvarith_buffer_is_constant(e->val.bvarith_buffer);
×
2721

2722
  case TAG_BVLOGIC_BUFFER:
1,143 ✔
2723
    return bvlogic_buffer_is_constant(e->val.bvlogic_buffer);
1,143 ✔
2724

2725
  default:
×
2726
    return false;
×
2727
  }
2728
}
2729

2730

2731
/*
2732
 * Copy term t into c:
2733
 * - t must be a BV_CONSTANT or BV64_CONSTANT
2734
 */
2735
static void bvconstant_copy_term(bvconstant_t *c, term_t t) {
571 ✔
2736
  term_table_t *terms;
2737
  bvconst_term_t *d;
2738
  bvconst64_term_t *d64;
2739

2740
  terms = __yices_globals.terms;
571 ✔
2741
  if (term_kind(terms, t) == BV64_CONSTANT) {
571 ✔
2742
    d64 = bvconst64_term_desc(terms, t);
75 ✔
2743
    bvconstant_copy64(c, d64->bitsize, d64->value);
75 ✔
2744
  } else {
2745
    d = bvconst_term_desc(terms, t);
496 ✔
2746
    bvconstant_copy(c, d->bitsize, d->data);
496 ✔
2747
  }
2748
}
571 ✔
2749

2750

2751
/*
2752
 * Copy the constant value of e into c
2753
 * - e must satisfy elem_is_bvconst(e)
2754
 */
2755
void bvconst_set_elem(bvconstant_t *c, stack_elem_t *e) {
4,579 ✔
2756
  assert(elem_is_bvconst(e));
2757

2758
  switch (e->tag) {
4,579 ✔
2759
  case TAG_BV64:
3,091 ✔
2760
    bvconstant_copy64(c, e->val.bv64.bitsize, e->val.bv64.value);
3,091 ✔
2761
    break;
3,091 ✔
2762

2763
  case TAG_BV:
44 ✔
2764
    bvconstant_copy(c, e->val.bv.bitsize, e->val.bv.data);
44 ✔
2765
    break;
44 ✔
2766

2767
  case TAG_TERM:
571 ✔
2768
  case TAG_SPECIAL_TERM:
2769
    bvconstant_copy_term(c, e->val.term);
571 ✔
2770
    break;
571 ✔
2771

2772
  case TAG_BVARITH64_BUFFER:
1 ✔
2773
    bvarith64_buffer_copy_constant(e->val.bvarith64_buffer, c);
1 ✔
2774
    break;
1 ✔
2775

2776
  case TAG_BVARITH_BUFFER:
×
2777
    bvarith_buffer_copy_constant(e->val.bvarith_buffer, c);
×
2778
    break;
×
2779

2780
  case TAG_BVLOGIC_BUFFER:
872 ✔
2781
    bvlogic_buffer_get_constant(e->val.bvlogic_buffer, c);
872 ✔
2782
    break;
872 ✔
2783

2784
  default: // should not happen
×
2785
    assert(false);
2786
    break;
×
2787
  }
2788
}
4,579 ✔
2789

2790

2791
/*
2792
 * Bitwise operations between buffer b and element e
2793
 */
2794
void bvand_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
6,440 ✔
2795
  uint32_t n;
2796
  term_t t;
2797

2798
  n = bvlogic_buffer_bitsize(b);
6,440 ✔
2799

2800
  switch (e->tag) {
6,440 ✔
2801
  case TAG_BV64:
562 ✔
2802
    if (e->val.bv64.bitsize != n) {
562 ✔
2803
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2804
    }
2805
    bvlogic_buffer_and_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
562 ✔
2806
    break;
562 ✔
2807

2808
  case TAG_BV:
45 ✔
2809
    if (e->val.bv.bitsize != n) {
45 ✔
2810
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2811
    }
2812
    bvlogic_buffer_and_constant(b, e->val.bv.bitsize, e->val.bv.data);
45 ✔
2813
    break;
45 ✔
2814

2815
  case TAG_TERM:
3,657 ✔
2816
  case TAG_SPECIAL_TERM:
2817
    check_bv_term(stack, e, n);
3,657 ✔
2818
    bvlogic_buffer_and_term(b, __yices_globals.terms, e->val.term);
3,657 ✔
2819
    break;
3,657 ✔
2820

2821
  case TAG_BVARITH64_BUFFER:
48 ✔
2822
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
48 ✔
2823
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2824
    }
2825
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
48 ✔
2826
    bvlogic_buffer_and_term(b, __yices_globals.terms, t);
48 ✔
2827
    break;
48 ✔
2828

2829
  case TAG_BVARITH_BUFFER:
10 ✔
2830
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
10 ✔
2831
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2832
    }
2833
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
10 ✔
2834
    bvlogic_buffer_and_term(b, __yices_globals.terms, t);
10 ✔
2835
    break;
10 ✔
2836

2837
  case TAG_BVLOGIC_BUFFER:
2,118 ✔
2838
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
2,118 ✔
2839
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2840
    }
2841
    bvlogic_buffer_and_buffer(b, e->val.bvlogic_buffer);
2,118 ✔
2842
    break;
2,118 ✔
2843

2844
  default:
×
2845
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
2846
  }
2847
}
6,440 ✔
2848

2849
void bvor_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
3,429 ✔
2850
  uint32_t n;
2851
  term_t t;
2852

2853
  n = bvlogic_buffer_bitsize(b);
3,429 ✔
2854

2855
  switch (e->tag) {
3,429 ✔
2856
  case TAG_BV64:
39 ✔
2857
    if (e->val.bv64.bitsize != n) {
39 ✔
2858
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2859
    }
2860
    bvlogic_buffer_or_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
39 ✔
2861
    break;
39 ✔
2862

2863
  case TAG_BV:
2 ✔
2864
    if (e->val.bv.bitsize != n) {
2 ✔
2865
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2866
    }
2867
    bvlogic_buffer_or_constant(b, e->val.bv.bitsize, e->val.bv.data);
2 ✔
2868
    break;
2 ✔
2869

2870
  case TAG_TERM:
1,198 ✔
2871
  case TAG_SPECIAL_TERM:
2872
    check_bv_term(stack, e, n);
1,198 ✔
2873
    bvlogic_buffer_or_term(b, __yices_globals.terms, e->val.term);
1,198 ✔
2874
    break;
1,198 ✔
2875

2876
  case TAG_BVARITH64_BUFFER:
1 ✔
2877
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
1 ✔
2878
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2879
    }
2880
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
1 ✔
2881
    bvlogic_buffer_or_term(b, __yices_globals.terms, t);
1 ✔
2882
    break;
1 ✔
2883

2884
  case TAG_BVARITH_BUFFER:
1 ✔
2885
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
1 ✔
2886
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2887
    }
2888
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
1 ✔
2889
    bvlogic_buffer_or_term(b, __yices_globals.terms, t);
1 ✔
2890
    break;
1 ✔
2891

2892
  case TAG_BVLOGIC_BUFFER:
2,188 ✔
2893
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
2,188 ✔
2894
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2895
    }
2896
    bvlogic_buffer_or_buffer(b, e->val.bvlogic_buffer);
2,188 ✔
2897
    break;
2,188 ✔
2898

2899
  default:
×
2900
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
2901
  }
2902
}
3,429 ✔
2903

2904
void bvxor_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
2,132 ✔
2905
  uint32_t n;
2906
  term_t t;
2907

2908
  n = bvlogic_buffer_bitsize(b);
2,132 ✔
2909

2910
  switch (e->tag) {
2,132 ✔
2911
  case TAG_BV64:
79 ✔
2912
    if (e->val.bv64.bitsize != n) {
79 ✔
2913
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2914
    }
2915
    bvlogic_buffer_xor_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
79 ✔
2916
    break;
79 ✔
2917

2918
  case TAG_BV:
×
2919
    if (e->val.bv.bitsize != n) {
×
2920
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2921
    }
2922
    bvlogic_buffer_xor_constant(b, e->val.bv.bitsize, e->val.bv.data);
×
2923
    break;
×
2924

2925
  case TAG_TERM:
731 ✔
2926
  case TAG_SPECIAL_TERM:
2927
    check_bv_term(stack, e, n);
731 ✔
2928
    bvlogic_buffer_xor_term(b, __yices_globals.terms, e->val.term);
731 ✔
2929
    break;
731 ✔
2930

2931
  case TAG_BVARITH64_BUFFER:
16 ✔
2932
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
16 ✔
2933
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2934
    }
2935
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
16 ✔
2936
    bvlogic_buffer_xor_term(b, __yices_globals.terms, t);
16 ✔
2937
    break;
16 ✔
2938

2939
  case TAG_BVARITH_BUFFER:
×
2940
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
×
2941
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2942
    }
2943
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
×
2944
    bvlogic_buffer_xor_term(b, __yices_globals.terms, t);
×
2945
    break;
×
2946

2947
  case TAG_BVLOGIC_BUFFER:
1,306 ✔
2948
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
1,306 ✔
2949
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2950
    }
2951
    bvlogic_buffer_xor_buffer(b, e->val.bvlogic_buffer);
1,306 ✔
2952
    break;
1,306 ✔
2953

2954
  default:
×
2955
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
2956
    break;
2957
  }
2958
}
2,132 ✔
2959

2960
void bvcomp_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
528 ✔
2961
  uint32_t n;
2962
  term_t t;
2963

2964
  n = bvlogic_buffer_bitsize(b);
528 ✔
2965

2966
  switch (e->tag) {
528 ✔
2967
  case TAG_BV64:
375 ✔
2968
    if (e->val.bv64.bitsize != n) {
375 ✔
2969
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2970
    }
2971
    bvlogic_buffer_comp_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
375 ✔
2972
    break;
375 ✔
2973

2974
  case TAG_BV:
×
2975
    if (e->val.bv.bitsize != n) {
×
2976
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2977
    }
2978
    bvlogic_buffer_comp_constant(b, e->val.bv.bitsize, e->val.bv.data);
×
2979
    break;
×
2980

2981
  case TAG_TERM:
108 ✔
2982
  case TAG_SPECIAL_TERM:
2983
    check_bv_term(stack, e, n);
108 ✔
2984
    bvlogic_buffer_comp_term(b, __yices_globals.terms, e->val.term);
108 ✔
2985
    break;
108 ✔
2986

2987
  case TAG_BVARITH64_BUFFER:
×
2988
    if (bvarith64_buffer_bitsize(e->val.bvarith64_buffer) != n) {
×
2989
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2990
    }
2991
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
×
2992
    bvlogic_buffer_comp_term(b, __yices_globals.terms, t);
×
2993
    break;
×
2994

2995
  case TAG_BVARITH_BUFFER:
×
2996
    if (bvarith_buffer_bitsize(e->val.bvarith_buffer) != n) {
×
2997
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
2998
    }
2999
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
×
3000
    bvlogic_buffer_comp_term(b, __yices_globals.terms, t);
×
3001
    break;
×
3002

3003
  case TAG_BVLOGIC_BUFFER:
45 ✔
3004
    if (bvlogic_buffer_bitsize(e->val.bvlogic_buffer) != n) {
45 ✔
3005
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
3006
    }
3007
    bvlogic_buffer_comp_buffer(b, e->val.bvlogic_buffer);
45 ✔
3008
    break;
45 ✔
3009

3010
  default:
×
3011
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
3012
    break;
3013
  }
3014
}
528 ✔
3015

3016

3017
// add e to the right of b (i.e., high-order bits are from b, low-order bits from e)
3018
void bvconcat_elem(tstack_t *stack, bvlogic_buffer_t *b, stack_elem_t *e) {
14,803 ✔
3019
  term_t t;
3020

3021
  switch (e->tag) {
14,803 ✔
3022
  case TAG_BV64:
2,172 ✔
3023
    bvlogic_buffer_concat_right_constant64(b, e->val.bv64.bitsize, e->val.bv64.value);
2,172 ✔
3024
    break;
2,172 ✔
3025

3026
  case TAG_BV:
22 ✔
3027
    bvlogic_buffer_concat_right_constant(b, e->val.bv.bitsize, e->val.bv.data);
22 ✔
3028
    break;
22 ✔
3029

3030
  case TAG_TERM:
10,376 ✔
3031
  case TAG_SPECIAL_TERM:
3032
    t = e->val.term;
10,376 ✔
3033
    if (! yices_check_bv_term(t)) {
10,376 ✔
3034
      report_yices_error(stack);
×
3035
    }
3036
    bvlogic_buffer_concat_right_term(b, __yices_globals.terms, t);
10,376 ✔
3037
    break;
10,376 ✔
3038

3039
  case TAG_BVARITH64_BUFFER:
60 ✔
3040
    t = bvarith64_buffer_get_term(e->val.bvarith64_buffer);
60 ✔
3041
    bvlogic_buffer_concat_right_term(b, __yices_globals.terms, t);
60 ✔
3042
    break;
60 ✔
3043

3044
  case TAG_BVARITH_BUFFER:
×
3045
    t = bvarith_buffer_get_term(e->val.bvarith_buffer);
×
3046
    bvlogic_buffer_concat_right_term(b, __yices_globals.terms, t);
×
3047
    break;
×
3048

3049
  case TAG_BVLOGIC_BUFFER:
2,173 ✔
3050
    bvlogic_buffer_concat_right_buffer(b, e->val.bvlogic_buffer);
2,173 ✔
3051
    break;
2,173 ✔
3052

3053
  default:
×
3054
    raise_exception(stack, e, TSTACK_BVLOGIC_ERROR);
×
3055
    break;
3056
  }
3057
}
14,803 ✔
3058

3059

3060

3061

3062

3063
/*
3064
 * Every eval function takes three parameters
3065
 * - the stack
3066
 * - f = an array of stack elements.
3067
 * - n = size of this array
3068
 *
3069
 * f is set to the start of the arguments on the top frame,
3070
 * n = the number of arguments
3071
 *
3072
 * For example, if the stack contains a frame with operator code MK_AND
3073
 * and 4 arguments, then the top frame is [MK_AND <arg1> ... <arg4>]
3074
 *
3075
 * tstack_eval will invoke eval_mk_and(stack, f, n)
3076
 * with f pointing to array [<arg1> .... <arg4>] and n = 4
3077
 *
3078
 * With every eval function, there's a check function that takes the
3079
 * same parameters and raises an exception if the arguments or frame
3080
 * are incorrect.
3081
 */
3082

3083

3084
/*
3085
 * COMMANDS MAPPING NAMES TO TYPES OR TERMS
3086
 */
3087

3088
/*
3089
 * [define-type <symbol> ]
3090
 * [define-type <symbol> <type> ]
3091
 */
3092
static void check_define_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
77 ✔
3093
  check_op(stack, DEFINE_TYPE);
77 ✔
3094
  check_size(stack, (n == 1 || n == 2));
77 ✔
3095
  check_tag(stack, f, TAG_SYMBOL);
77 ✔
3096
  if (n == 2) check_tag(stack, f+1, TAG_TYPE);
77 ✔
3097
}
77 ✔
3098

3099
static void eval_define_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
77 ✔
3100
  type_t tau;
3101

3102
  if (n == 1) {
77 ✔
3103
    tau = _o_yices_new_uninterpreted_type();
27 ✔
3104
  } else {
3105
    tau = f[1].val.type;
50 ✔
3106
  }
3107
  _o_yices_set_type_name(tau, f[0].val.string);
77 ✔
3108

3109
  tstack_pop_frame(stack);
77 ✔
3110
  no_result(stack);
77 ✔
3111
}
77 ✔
3112

3113
/*
3114
 * [define-term <symbol> <type> ]
3115
 * [define-term <symbol> <type> <term> ]
3116
 */
3117
static void check_define_term(tstack_t *stack, stack_elem_t *f, uint32_t n) {
12,174 ✔
3118
  check_op(stack, DEFINE_TERM);
12,174 ✔
3119
  check_size(stack, (n == 2 || n == 3));
12,174 ✔
3120
  check_tag(stack, f, TAG_SYMBOL);
12,174 ✔
3121
  check_tag(stack, f+1, TAG_TYPE);
12,174 ✔
3122
  // no need to check val[f+2]: get_term will raise an exception if
3123
  // it can't be converted to a term.
3124
}
12,174 ✔
3125

3126
static void eval_define_term(tstack_t *stack, stack_elem_t *f, uint32_t n) {
12,174 ✔
3127
  type_t tau;
3128
  term_t t;
3129

3130
  tau = f[1].val.type;
12,174 ✔
3131
  if (n == 2) {
12,174 ✔
3132
    t = _o_yices_new_uninterpreted_term(tau);
12,118 ✔
3133
  } else {
3134
    t = get_term(stack, f+2);
56 ✔
3135
    if (! is_subtype(__yices_globals.types, term_type(__yices_globals.terms, t), tau)) {
56 ✔
3136
      raise_exception(stack, f+2, TSTACK_TYPE_ERROR_IN_DEFTERM);
×
3137
    }
3138
  }
3139
  _o_yices_set_term_name(t, f[0].val.string);
12,174 ✔
3140

3141
  tstack_pop_frame(stack);
12,174 ✔
3142
  no_result(stack);
12,174 ✔
3143
}
12,174 ✔
3144

3145

3146
/*
3147
 * [bind <string> <term>  .... <string> <term>]
3148
 */
3149
// bind is parallel: we process a block of pairs <name, term>
3150
// the result of bind is a list of bindings object
3151
// they are pushed on the stack and will be removed when we
3152
// pop out of the enclosing let.
3153

3154
static void check_bind(tstack_t *stack, stack_elem_t *f, uint32_t n) {
176,442 ✔
3155
  uint32_t i;
3156

3157
  check_op(stack, BIND);
176,442 ✔
3158
  check_size(stack, n >= 2);
176,442 ✔
3159
  check_size(stack, (n & 0x1) == 0);
176,442 ✔
3160
  for (i=0; i<n; i+=2) {
378,941 ✔
3161
    check_tag(stack, f+i, TAG_SYMBOL);
202,499 ✔
3162
  }
3163
}
176,442 ✔
3164

3165
// Push a new binding element on top of the stack.
3166
// set_binding_result overwrites the current top element.
3167
// This adds a new element instead.
3168
static void push_binding_result(tstack_t *stack, term_t t, char *symbol, loc_t loc) {
26,057 ✔
3169
  stack_elem_t *e;
3170

3171
  e = tstack_get_topelem(stack);
26,057 ✔
3172
  e->tag = TAG_BINDING;
26,057 ✔
3173
  e->val.binding.term = t;
26,057 ✔
3174
  e->val.binding.symbol = symbol;
26,057 ✔
3175
  e->loc = loc;
26,057 ✔
3176
}
26,057 ✔
3177

3178
static void eval_bind(tstack_t *stack, stack_elem_t *f, uint32_t n) {
176,442 ✔
3179
  term_t *values;
3180
  char **names;
3181
  char *name;
3182
  term_t t;
3183
  loc_t loc;
3184
  uint32_t i, j, nb;
3185

3186
  nb = n/2;
176,442 ✔
3187
  assert(nb > 0);
3188

3189
  values = get_aux_buffer(stack, nb);
176,442 ✔
3190
  names = get_name_buffer(stack, nb);
176,442 ✔
3191
  j = 0;
176,442 ✔
3192
  for (i=0; i<nb; i++) {
378,941 ✔
3193
    name = f[j].val.string;
202,499 ✔
3194
    j ++;
202,499 ✔
3195
    t = get_term(stack, f+j);
202,499 ✔
3196
    j ++;
202,499 ✔
3197
    _o_yices_set_term_name(t, name);
202,499 ✔
3198
    names[i] = name;
202,499 ✔
3199
    values[i] = t;
202,499 ✔
3200
  }
3201
  tstack_pop_frame(stack);
176,442 ✔
3202

3203
  // Push each binding as its own stack element.
3204
  // Popping the LET frame later removes each bound name.
3205
  loc = stack->elem[stack->top - 1].loc;
176,442 ✔
3206
  set_binding_result(stack, values[0], names[0]);
176,442 ✔
3207
  for (i=1; i<nb; i++) {
202,499 ✔
3208
    push_binding_result(stack, values[i], names[i], loc);
26,057 ✔
3209
  }
3210
}
176,442 ✔
3211

3212

3213
/*
3214
 * [declare-var <string> <type>] --> [<string> <var>]
3215
 */
3216
static void check_declare_var(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,266 ✔
3217
  check_op(stack, DECLARE_VAR);
3,266 ✔
3218
  check_size(stack, n == 2);
3,266 ✔
3219
  check_tag(stack, f, TAG_SYMBOL);
3,266 ✔
3220
  check_tag(stack, f+1, TAG_TYPE);
3,266 ✔
3221
}
3,266 ✔
3222

3223
static void eval_declare_var(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,266 ✔
3224
  type_t tau;
3225
  term_t var;
3226
  char *name;
3227

3228
  name = f[0].val.string;
3,266 ✔
3229
  tau = f[1].val.type;
3,266 ✔
3230
  var = _o_yices_new_variable(tau);
3,266 ✔
3231

3232
  _o_yices_set_term_name(var, name);
3,266 ✔
3233
  tstack_pop_frame(stack);
3,266 ✔
3234
  set_binding_result(stack, var, name);
3,266 ✔
3235
}
3,266 ✔
3236

3237

3238
/*
3239
 * [declare-type-var <symbol> ] --> [type_binding <symbol> <type-var]
3240
 */
3241
static void check_declare_type_var(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
3242
  check_op(stack, DECLARE_TYPE_VAR);
×
UNCOV
3243
  check_size(stack, n == 1);
×
UNCOV
3244
  check_tag(stack, f, TAG_SYMBOL);
×
UNCOV
3245
}
×
3246

UNCOV
3247
static void eval_declare_type_var(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
3248
  type_t tau;
3249
  char *name;
3250

UNCOV
3251
  name = f[0].val.string;
×
UNCOV
3252
  tau = yices_type_variable(stack->tvar_id);
×
3253
  assert(tau != NULL_TYPE);
UNCOV
3254
  stack->tvar_id ++;
×
3255

UNCOV
3256
  yices_set_type_name(tau, name);
×
UNCOV
3257
  tstack_pop_frame(stack);
×
UNCOV
3258
  set_type_binding_result(stack, tau, name);
×
UNCOV
3259
}
×
3260

3261

3262
/*
3263
 * [let [do-bind <binding> ... <binding> <term>]
3264
 */
3265
static void check_let(tstack_t *stack, stack_elem_t *f, uint32_t n) {
176,442 ✔
3266
  check_op(stack, LET);
176,442 ✔
3267
  check_size(stack, n>=2);
176,442 ✔
3268
  check_all_tags(stack, f, f + (n-1), TAG_BINDING);
176,442 ✔
3269
}
176,442 ✔
3270

3271
static void eval_let(tstack_t *stack, stack_elem_t *f, uint32_t n) {
176,442 ✔
3272
  copy_result_and_pop_frame(stack, f + (n-1));
176,442 ✔
3273
}
176,442 ✔
3274

3275

3276
/*
3277
 * TYPE CONSTRUCTORS
3278
 */
3279

3280
/*
3281
 * [mk-bv-type <rational>]
3282
 */
3283
static void check_mk_bv_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
45,294 ✔
3284
  check_op(stack, MK_BV_TYPE);
45,294 ✔
3285
  check_size(stack, n == 1);
45,294 ✔
3286
  check_tag(stack, f, TAG_RATIONAL);
45,294 ✔
3287
}
45,294 ✔
3288

3289
static void eval_mk_bv_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
45,294 ✔
3290
  int32_t size;
3291
  type_t tau;
3292

3293
  size = get_integer(stack, f);
45,294 ✔
3294
  if (size <= 0) {
45,294 ✔
UNCOV
3295
    raise_exception(stack, f, TSTACK_NONPOSITIVE_BVSIZE);
×
3296
  }
3297
  tau = _o_yices_bv_type(size);
45,294 ✔
3298
  check_type(stack, tau);
45,294 ✔
3299

3300
  tstack_pop_frame(stack);
45,294 ✔
3301
  set_type_result(stack, tau);
45,294 ✔
3302
}
45,294 ✔
3303

3304
/*
3305
 * [mk-ff-type <rational>]
3306
 */
3307
static void check_mk_ff_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
12 ✔
3308
  check_op(stack, MK_FF_TYPE);
12 ✔
3309
  check_size(stack, n == 1);
12 ✔
3310
  check_tag(stack, f, TAG_RATIONAL);
12 ✔
3311
}
12 ✔
3312

3313
static void eval_mk_ff_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
12 ✔
3314
  type_t tau;
3315
  mpz_t order;
3316

3317
  mpz_init(order);
12 ✔
3318
  get_integer_mpz(stack, f, order);
12 ✔
3319

3320
  tau = _o_yices_ff_type(order);
12 ✔
3321
  mpz_clear(order);
12 ✔
3322

3323
  if (tau == NULL_TYPE) {
12 ✔
3324
    raise_exception(stack, f, TSTACK_INVALID_FFSIZE);
1 ✔
3325
  }
3326

3327
  check_type(stack, tau);
11 ✔
3328

3329
  tstack_pop_frame(stack);
11 ✔
3330
  set_type_result(stack, tau);
11 ✔
3331
}
11 ✔
3332

3333
/*
3334
 * [mk-scalar-type <string> ... <string>]
3335
 */
3336
static void check_mk_scalar_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10 ✔
3337
  check_op(stack, MK_SCALAR_TYPE);
10 ✔
3338
  check_size(stack, n >= 1);
10 ✔
3339
  check_all_tags(stack, f, f+n, TAG_SYMBOL);
10 ✔
3340
  check_distinct_scalar_names(stack, f, n);
10 ✔
3341
}
10 ✔
3342

3343
static void eval_mk_scalar_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10 ✔
3344
  type_t tau;
3345
  int32_t i, card;
3346
  term_t x;
3347

3348
  // build the type
3349
  card = n;
10 ✔
3350
  tau = _o_yices_new_scalar_type(card);
10 ✔
3351
  assert(tau != NULL_TYPE);
3352

3353
  for (i=0; i<card; i++) {
24 ✔
3354
    x = _o_yices_constant(tau, i);
14 ✔
3355
    assert(x != NULL_TERM);
3356
    _o_yices_set_term_name(x, f[i].val.string);
14 ✔
3357
  }
3358

3359
  tstack_pop_frame(stack);
10 ✔
3360
  set_type_result(stack, tau);
10 ✔
3361
}
10 ✔
3362

3363
/*
3364
 * [mk-tuple-type <type> ... <type>]
3365
 */
3366
static void check_mk_tuple_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
26 ✔
3367
  check_op(stack, MK_TUPLE_TYPE);
26 ✔
3368
  check_size(stack, n >= 1);
26 ✔
3369
  check_all_tags(stack, f, f+n, TAG_TYPE);
26 ✔
3370
}
26 ✔
3371

3372
static void eval_mk_tuple_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
26 ✔
3373
  type_t *tau, sigma;
3374
  uint32_t i;
3375

3376
  tau = get_aux_buffer(stack, n);
26 ✔
3377

3378
  for (i=0; i<n; i++) {
77 ✔
3379
    tau[i] = f[i].val.type;
51 ✔
3380
  }
3381
  sigma = _o_yices_tuple_type(n, tau);
26 ✔
3382
  assert(sigma != NULL_TYPE);
3383

3384
  tstack_pop_frame(stack);
26 ✔
3385
  set_type_result(stack, sigma);
26 ✔
3386
}
26 ✔
3387

3388
/*
3389
 * [mk-fun-type <type> ... <type> <type> ]
3390
 *
3391
 * To support SMT-LIB, we also allow [mk-fun-type <type>] and
3392
 * just return <type>.
3393
 */
3394
static void check_mk_fun_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,873 ✔
3395
  check_op(stack, MK_FUN_TYPE);
4,873 ✔
3396
  check_size(stack, n >= 1);
4,873 ✔
3397
  check_all_tags(stack, f, f+n, TAG_TYPE);
4,873 ✔
3398
}
4,873 ✔
3399

3400
static void eval_mk_fun_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,873 ✔
3401
  type_t *tau, sigma;
3402
  uint32_t i;
3403

3404
  if (n >= 2) {
4,873 ✔
3405
    // first n-1 types are the domain, last one is the range
3406
    tau = get_aux_buffer(stack, n);
520 ✔
3407

3408
    for (i=0; i<n; i++) {
1,594 ✔
3409
      tau[i] = f[i].val.type;
1,074 ✔
3410
    }
3411

3412
    n --;
520 ✔
3413
    sigma = _o_yices_function_type(n, tau, tau[n]);
520 ✔
3414
  } else {
3415
    sigma = f[0].val.type;
4,353 ✔
3416
  }
3417

3418
  assert(sigma != NULL_TYPE);
3419
  tstack_pop_frame(stack);
4,873 ✔
3420
  set_type_result(stack, sigma);
4,873 ✔
3421
}
4,873 ✔
3422

3423

3424
/*
3425
 * [mk-app-type <macro> <type> ... <type> ]
3426
 */
3427
static void check_mk_app_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
3428
  check_op(stack, MK_APP_TYPE);
×
3429
  check_size(stack, n >= 2);
×
UNCOV
3430
  check_tag(stack, f, TAG_MACRO);
×
3431
  check_all_tags(stack, f+1, f+n, TAG_TYPE);
×
3432
}
×
3433

3434
static void eval_mk_app_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
3435
  type_t *tau, sigma;
3436
  int32_t id;
3437
  uint32_t i;
3438

3439
  assert(n > 0);
UNCOV
3440
  id = f[0].val.macro;
×
3441

UNCOV
3442
  n --;
×
UNCOV
3443
  f ++;
×
UNCOV
3444
  tau = get_aux_buffer(stack, n);
×
UNCOV
3445
  for (i=0; i<n; i++) {
×
UNCOV
3446
    tau[i] = f[i].val.type;
×
3447
  }
UNCOV
3448
  sigma = yices_instance_type(id, n, tau);
×
UNCOV
3449
  check_type(stack, sigma);
×
3450

UNCOV
3451
  tstack_pop_frame(stack);
×
UNCOV
3452
  set_type_result(stack, sigma);
×
UNCOV
3453
}
×
3454

3455

3456
/*
3457
 * TERM CONSTRUCTORS
3458
 */
3459

3460
/*
3461
 * [mk-apply <term> .... <term>]
3462
 */
3463
static void check_mk_apply(tstack_t *stack, stack_elem_t *f, uint32_t n) {
35,060 ✔
3464
  check_op(stack, MK_APPLY);
35,060 ✔
3465
  check_size(stack, n >= 2);
35,060 ✔
3466
}
35,060 ✔
3467

3468
static void eval_mk_apply(tstack_t *stack, stack_elem_t *f, uint32_t n) {
35,060 ✔
3469
  term_t arg[n], fun, t;
35,060 ✔
3470
  uint32_t i;
3471

3472
  fun = get_term(stack, f);
35,060 ✔
3473
  n --; // number of arguments
35,060 ✔
3474
  f ++;
35,060 ✔
3475
  for (i=0; i<n; i++) {
89,020 ✔
3476
    arg[i] = get_term(stack, f + i);
53,960 ✔
3477
  }
3478
  t = _o_yices_application(fun, n, arg);
35,060 ✔
3479
  check_term(stack, t);
35,060 ✔
3480

3481
  tstack_pop_frame(stack);
35,060 ✔
3482
  set_term_result(stack, t);
35,060 ✔
3483
}
35,060 ✔
3484

3485

3486
/*
3487
 * [mk-ite <term> <term> <term>]
3488
 */
3489
static void check_mk_ite(tstack_t *stack, stack_elem_t *f, uint32_t n) {
71,804 ✔
3490
  check_op(stack, MK_ITE);
71,804 ✔
3491
  check_size(stack, n == 3);
71,804 ✔
3492
}
71,804 ✔
3493

3494
static void eval_mk_ite(tstack_t *stack, stack_elem_t *f, uint32_t n) {
71,804 ✔
3495
  term_t cond, left, right, t;
3496

3497
  cond = get_term(stack, f);
71,804 ✔
3498
  left = get_term(stack, f+1);
71,804 ✔
3499
  right = get_term(stack, f+2);
71,804 ✔
3500
  t = _o_yices_ite(cond, left, right);
71,804 ✔
3501
  check_term(stack, t);
71,804 ✔
3502

3503
  tstack_pop_frame(stack);
71,804 ✔
3504
  set_term_result(stack, t);
71,804 ✔
3505
}
71,804 ✔
3506

3507

3508
/*
3509
 * [mk-eq <term> <term>]
3510
 */
3511
static void check_mk_eq(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,502 ✔
3512
  check_op(stack, MK_EQ);
4,502 ✔
3513
  check_size(stack, n == 2);
4,502 ✔
3514
}
4,502 ✔
3515

3516
static void eval_mk_eq(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,502 ✔
3517
  term_t left, right, t;
3518

3519
  left = get_term(stack, f);
4,502 ✔
3520
  right = get_term(stack, f+1);
4,502 ✔
3521
  t = _o_yices_eq(left, right);
4,502 ✔
3522
  check_term(stack, t);
4,502 ✔
3523

3524
  tstack_pop_frame(stack);
4,502 ✔
3525
  set_term_result(stack, t);
4,502 ✔
3526
}
4,502 ✔
3527

3528

3529
/*
3530
 * [mk-diseq <term> <term>]
3531
 */
3532
static void check_mk_diseq(tstack_t *stack, stack_elem_t *f, uint32_t n) {
72 ✔
3533
  check_op(stack, MK_DISEQ);
72 ✔
3534
  check_size(stack, n == 2);
72 ✔
3535
}
72 ✔
3536

3537
static void eval_mk_diseq(tstack_t *stack, stack_elem_t *f, uint32_t n) {
72 ✔
3538
  term_t left, right, t;
3539

3540
  left = get_term(stack, f);
72 ✔
3541
  right = get_term(stack, f+1);
72 ✔
3542
  t = _o_yices_neq(left, right);
72 ✔
3543
  check_term(stack, t);
72 ✔
3544

3545
  tstack_pop_frame(stack);
72 ✔
3546
  set_term_result(stack, t);
72 ✔
3547
}
72 ✔
3548

3549

3550
/*
3551
 * [mk-distinct <term> ... <term>]
3552
 */
3553
static void check_mk_distinct(tstack_t *stack, stack_elem_t *f, uint32_t n) {
9,043 ✔
3554
  check_op(stack, MK_DISTINCT);
9,043 ✔
3555
  check_size(stack, n >= 2);
9,043 ✔
3556
}
9,043 ✔
3557

3558
static void eval_mk_distinct(tstack_t *stack, stack_elem_t *f, uint32_t n) {
9,043 ✔
3559
  term_t *arg, t;
3560
  uint32_t i;
3561

3562
  arg = get_aux_buffer(stack, n);
9,043 ✔
3563

3564
  for (i=0; i<n; i++) {
31,085 ✔
3565
    arg[i] = get_term(stack, f+i);
22,042 ✔
3566
  }
3567
  t = _o_yices_distinct(n, arg);
9,043 ✔
3568
  check_term(stack, t);
9,043 ✔
3569

3570
  tstack_pop_frame(stack);
9,043 ✔
3571
  set_term_result(stack, t);
9,043 ✔
3572
}
9,043 ✔
3573

3574

3575

3576

3577
/*
3578
 * [mk-not <term>]
3579
 */
3580
static void check_mk_not(tstack_t *stack, stack_elem_t *f, uint32_t n) {
123,431 ✔
3581
  check_op(stack, MK_NOT);
123,431 ✔
3582
  check_size(stack, n == 1);
123,431 ✔
3583
}
123,431 ✔
3584

3585
static void eval_mk_not(tstack_t *stack, stack_elem_t *f, uint32_t n) {
123,431 ✔
3586
  term_t t;
3587

3588
  t = _o_yices_not(get_term(stack, f));
123,431 ✔
3589
  check_term(stack, t);
123,431 ✔
3590

3591
  tstack_pop_frame(stack);
123,431 ✔
3592
  set_term_result(stack, t);
123,431 ✔
3593
}
123,431 ✔
3594

3595

3596
/*
3597
 * [mk-or <term> ... <term>]
3598
 */
3599
static void check_mk_or(tstack_t *stack, stack_elem_t *f, uint32_t n) {
41,756 ✔
3600
  check_op(stack, MK_OR);
41,756 ✔
3601
  check_size(stack, n >= 1);
41,756 ✔
3602
}
41,756 ✔
3603

3604
// use buffer here rather than
3605
static void eval_mk_or(tstack_t *stack, stack_elem_t *f, uint32_t n) {
41,756 ✔
3606
  term_t *arg, t;
3607
  uint32_t i;
3608

3609
  arg = get_aux_buffer(stack, n);
41,756 ✔
3610

3611
  for (i=0; i<n; i++) {
157,922 ✔
3612
    arg[i] = get_term(stack, f+i);
116,166 ✔
3613
  }
3614
  t = _o_yices_or(n, arg);
41,756 ✔
3615
  check_term(stack, t);
41,756 ✔
3616

3617
  tstack_pop_frame(stack);
41,756 ✔
3618
  set_term_result(stack, t);
41,756 ✔
3619
}
41,756 ✔
3620

3621

3622
/*
3623
 * [mk-and <term> ... <term>]
3624
 */
3625
static void check_mk_and(tstack_t *stack, stack_elem_t *f, uint32_t n) {
37,253 ✔
3626
  check_op(stack, MK_AND);
37,253 ✔
3627
  check_size(stack, n >= 1);
37,253 ✔
3628
}
37,253 ✔
3629

3630
static void eval_mk_and(tstack_t *stack, stack_elem_t *f, uint32_t n) {
37,253 ✔
3631
  term_t *arg, t;
3632
  uint32_t i;
3633

3634
  arg = get_aux_buffer(stack, n);
37,253 ✔
3635

3636
  for (i=0; i<n; i++) {
232,742 ✔
3637
    arg[i] = get_term(stack, f+i);
195,489 ✔
3638
  }
3639
  t = _o_yices_and(n, arg);
37,253 ✔
3640
  check_term(stack, t);
37,253 ✔
3641

3642
  tstack_pop_frame(stack);
37,253 ✔
3643
  set_term_result(stack, t);
37,253 ✔
3644
}
37,253 ✔
3645

3646

3647
/*
3648
 * [mk-xor <term> ... <term>]
3649
 */
3650
static void check_mk_xor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8,702 ✔
3651
  check_op(stack, MK_XOR);
8,702 ✔
3652
  check_size(stack, n >= 1);
8,702 ✔
3653
}
8,702 ✔
3654

3655
static void eval_mk_xor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8,702 ✔
3656
  term_t *arg, t;
3657
  uint32_t i;
3658

3659
  arg = get_aux_buffer(stack, n);
8,702 ✔
3660

3661
  for (i=0; i<n; i++) {
26,555 ✔
3662
    arg[i] = get_term(stack, f+i);
17,853 ✔
3663
  }
3664
  t = _o_yices_xor(n, arg);
8,702 ✔
3665
  check_term(stack, t);
8,702 ✔
3666

3667
  tstack_pop_frame(stack);
8,702 ✔
3668
  set_term_result(stack, t);
8,702 ✔
3669
}
8,702 ✔
3670

3671

3672
/*
3673
 * [mk-iff <term> ... <term>]
3674
 */
UNCOV
3675
static void check_mk_iff(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
3676
  check_op(stack, MK_IFF);
×
UNCOV
3677
  check_size(stack, n >= 1);
×
UNCOV
3678
}
×
3679

UNCOV
3680
static void eval_mk_iff(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
3681
  term_t t;
3682
  uint32_t i;
3683

UNCOV
3684
  t = get_term(stack, f);
×
UNCOV
3685
  for (i=1; i<n; i++) {
×
UNCOV
3686
    t = _o_yices_iff(t, get_term(stack, f+i));
×
UNCOV
3687
    check_term(stack, t);
×
3688
  }
UNCOV
3689
  tstack_pop_frame(stack);
×
UNCOV
3690
  set_term_result(stack, t);
×
UNCOV
3691
}
×
3692

3693

3694
/*
3695
 * [mk-implies <term> <term>]
3696
 */
3697
static void check_mk_implies(tstack_t *stack, stack_elem_t *f, uint32_t n) {
549 ✔
3698
  check_op(stack, MK_IMPLIES);
549 ✔
3699
  check_size(stack, n == 2);
549 ✔
3700
}
549 ✔
3701

3702
static void eval_mk_implies(tstack_t *stack, stack_elem_t *f, uint32_t n) {
549 ✔
3703
  term_t left, right, t;
3704

3705
  left = get_term(stack, f);
549 ✔
3706
  right = get_term(stack, f+1);
549 ✔
3707
  t = _o_yices_implies(left, right);
549 ✔
3708
  check_term(stack, t);
549 ✔
3709

3710
  tstack_pop_frame(stack);
549 ✔
3711
  set_term_result(stack, t);
549 ✔
3712
}
549 ✔
3713

3714

3715
/*
3716
 * [mk-tuple <term> ... <term>]
3717
 */
3718
static void check_mk_tuple(tstack_t *stack, stack_elem_t *f, uint32_t n) {
122 ✔
3719
  check_op(stack, MK_TUPLE);
122 ✔
3720
  check_size(stack, n >= 1);
122 ✔
3721
}
122 ✔
3722

3723
static void eval_mk_tuple(tstack_t *stack, stack_elem_t *f, uint32_t n) {
122 ✔
3724
  term_t arg[n], t;
122 ✔
3725
  uint32_t i;
3726

3727
  for (i=0; i<n; i++) {
366 ✔
3728
    arg[i] = get_term(stack, f+i);
244 ✔
3729
  }
3730
  t = _o_yices_tuple(n, arg);
122 ✔
3731
  check_term(stack, t);
122 ✔
3732

3733
  tstack_pop_frame(stack);
122 ✔
3734
  set_term_result(stack, t);
122 ✔
3735
}
122 ✔
3736

3737

3738
/*
3739
 * [mk-select <term> <rational>]
3740
 */
3741
static void check_mk_select(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,054 ✔
3742
  check_op(stack, MK_SELECT);
2,054 ✔
3743
  check_size(stack, n == 2);
2,054 ✔
3744
  check_tag(stack, f+1, TAG_RATIONAL);
2,054 ✔
3745
}
2,054 ✔
3746

3747
static void eval_mk_select(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,054 ✔
3748
  int32_t idx;
3749
  term_t t;
3750

3751
  idx = get_integer(stack, f+1);
2,054 ✔
3752
  t = _o_yices_select(idx, get_term(stack, f));
2,054 ✔
3753
  check_term(stack, t);
2,054 ✔
3754

3755
  tstack_pop_frame(stack);
2,054 ✔
3756
  set_term_result(stack, t);
2,054 ✔
3757
}
2,054 ✔
3758

3759

3760

3761
/*
3762
 * [mk-tuple-update <tuple> <rational> <newvalue> ]
3763
 */
3764
static void check_mk_tuple_update(tstack_t *stack, stack_elem_t *f, uint32_t n) {
555 ✔
3765
  check_op(stack, MK_TUPLE_UPDATE);
555 ✔
3766
  check_size(stack, n == 3);
555 ✔
3767
  check_tag(stack, f+1, TAG_RATIONAL);
555 ✔
3768
}
555 ✔
3769

3770

3771
static void eval_mk_tuple_update(tstack_t *stack, stack_elem_t *f, uint32_t n) {
555 ✔
3772
  int32_t idx;
3773
  term_t t, new_v;
3774

3775
  idx = get_integer(stack, f+1);
555 ✔
3776
  new_v = get_term(stack, f+2);
555 ✔
3777
  t = _o_yices_tuple_update(get_term(stack, f), idx, new_v);
555 ✔
3778
  check_term(stack, t);
555 ✔
3779

3780
  tstack_pop_frame(stack);
555 ✔
3781
  set_term_result(stack, t);
555 ✔
3782
}
555 ✔
3783

3784

3785
/*
3786
 * [mk-update <fun> <arg1> ... <argn> <newvalue>]
3787
 */
3788
static void check_mk_update(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8,586 ✔
3789
  check_op(stack, MK_UPDATE);
8,586 ✔
3790
  check_size(stack, n >= 3);
8,586 ✔
3791
}
8,586 ✔
3792

3793
static void eval_mk_update(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8,586 ✔
3794
  term_t arg[n], t;
8,586 ✔
3795
  uint32_t i;
3796

3797
  for (i=0; i<n; i++) {
34,376 ✔
3798
    arg[i] = get_term(stack, f+i);
25,790 ✔
3799
  }
3800
  t = _o_yices_update(arg[0], n-2, arg+1, arg[n-1]);
8,586 ✔
3801
  check_term(stack, t);
8,586 ✔
3802

3803
  tstack_pop_frame(stack);
8,586 ✔
3804
  set_term_result(stack, t);
8,586 ✔
3805
}
8,586 ✔
3806

3807

3808
/*
3809
 * [mk-forall <binding> ... <binding> <term>]
3810
 */
3811
static void check_mk_forall(tstack_t *stack, stack_elem_t *f, uint32_t n) {
610 ✔
3812
  check_op(stack, MK_FORALL);
610 ✔
3813
  check_size(stack, n >= 2);
610 ✔
3814
  check_all_tags(stack, f, f + (n-1), TAG_BINDING);
610 ✔
3815
  check_distinct_binding_names(stack, f, n-1);
610 ✔
3816
}
610 ✔
3817

3818
static void eval_mk_forall(tstack_t *stack, stack_elem_t *f, uint32_t n) {
610 ✔
3819
  term_t t, arg[n];
610 ✔
3820
  uint32_t i;
3821

3822
  for (i=0; i<n-1; i++) {
1,871 ✔
3823
    arg[i] = f[i].val.binding.term;
1,261 ✔
3824
  }
3825
  // body = last argument
3826
  arg[i] = get_term(stack, f + (n-1));
610 ✔
3827
  t = _o_yices_forall(n-1, arg, arg[n-1]);
610 ✔
3828
  check_term(stack, t);
610 ✔
3829

3830
  tstack_pop_frame(stack);
610 ✔
3831
  set_term_result(stack, t);
610 ✔
3832
}
610 ✔
3833

3834

3835
/*
3836
 * [mk-exists <binding> ... <binding> <term>]
3837
 */
3838
static void check_mk_exists(tstack_t *stack, stack_elem_t *f, uint32_t n) {
531 ✔
3839
  check_op(stack, MK_EXISTS);
531 ✔
3840
  check_size(stack, n >= 2);
531 ✔
3841
  check_all_tags(stack, f, f + (n-1), TAG_BINDING);
531 ✔
3842
  check_distinct_binding_names(stack, f, n-1);
531 ✔
3843
}
531 ✔
3844

3845
static void eval_mk_exists(tstack_t *stack, stack_elem_t *f, uint32_t n) {
531 ✔
3846
  term_t t, arg[n];
531 ✔
3847
  uint32_t i;
3848

3849
  for (i=0; i<n-1; i++) {
1,134 ✔
3850
    arg[i] = f[i].val.binding.term;
603 ✔
3851
  }
3852
  // body = last argument
3853
  arg[i] = get_term(stack, f + (n-1));
531 ✔
3854
  t = _o_yices_exists(n-1, arg, arg[n-1]);
531 ✔
3855
  check_term(stack, t);
531 ✔
3856

3857
  tstack_pop_frame(stack);
531 ✔
3858
  set_term_result(stack, t);
531 ✔
3859
}
531 ✔
3860

3861

3862
/*
3863
 * [mk-lambda <binding> ... <binding> <term>]
3864
 */
3865
static void check_mk_lambda(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
3866
  check_op(stack, MK_LAMBDA);
×
3867
  check_size(stack, n >= 2);
×
3868
  check_all_tags(stack, f, f + (n-1), TAG_BINDING);
×
3869
  check_distinct_binding_names(stack, f, n-1);
×
UNCOV
3870
}
×
3871

UNCOV
3872
static void eval_mk_lambda(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
3873
  term_t t, arg[n];
×
3874
  uint32_t i;
3875

UNCOV
3876
  for (i=0; i<n-1; i++) {
×
UNCOV
3877
    arg[i] = f[i].val.binding.term;
×
3878
  }
3879
  // body = last argument
UNCOV
3880
  arg[i] = get_term(stack, f + (n-1));
×
UNCOV
3881
  t = _o_yices_lambda(n-1, arg, arg[n-1]);
×
UNCOV
3882
  check_term(stack, t);
×
3883

UNCOV
3884
  tstack_pop_frame(stack);
×
UNCOV
3885
  set_term_result(stack, t);
×
UNCOV
3886
}
×
3887

3888

3889

3890

3891
/*
3892
 *  ARITHMETIC
3893
 */
3894

3895
/*
3896
 * [mk-add <arith> ... <arith>]
3897
 */
3898
static void check_mk_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10,851 ✔
3899
  check_op(stack, MK_ADD);
10,851 ✔
3900
  check_size(stack, n>=1);
10,851 ✔
3901
}
10,851 ✔
3902

3903
static void eval_mk_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10,851 ✔
3904
  uint32_t i;
3905
  rba_buffer_t *b;
3906

3907
  b = tstack_get_abuffer(stack);
10,851 ✔
3908
  for (i=0; i<n; i++) {
41,485 ✔
3909
    add_elem(stack, b, f+i);
30,634 ✔
3910
  }
3911
  tstack_pop_frame(stack);
10,851 ✔
3912
  set_arith_result(stack, b);
10,851 ✔
3913
}
10,851 ✔
3914

3915

3916
/*
3917
 * [mk-sub <arith> ... <arith>]
3918
 */
3919
static void check_mk_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
19,366 ✔
3920
  check_op(stack, MK_SUB);
19,366 ✔
3921
  check_size(stack, n>=1);
19,366 ✔
3922
}
19,366 ✔
3923

3924
static void eval_mk_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
19,366 ✔
3925
  uint32_t i;
3926
  rba_buffer_t *b;
3927

3928
  // if n == 1, we interpret this a unary minus (unlike yices-1.0.x)
3929
  if (n == 1) {
19,366 ✔
3930
    // [mk-neg xxx] ---> - xxx
3931
    neg_elem(stack, f);
11,878 ✔
3932
    copy_result_and_pop_frame(stack, f);
11,878 ✔
3933
  } else {
3934
    b = tstack_get_abuffer(stack);
7,488 ✔
3935
    add_elem(stack, b, f);
7,488 ✔
3936
    for (i=1; i<n; i++) {
15,192 ✔
3937
      sub_elem(stack, b, f+i);
7,704 ✔
3938
    }
3939
    tstack_pop_frame(stack);
7,488 ✔
3940
    set_arith_result(stack, b);
7,488 ✔
3941
  }
3942

3943
}
19,366 ✔
3944

3945

3946
/*
3947
 * [mk-neg <arith>]
3948
 */
UNCOV
3949
static void check_mk_neg(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
3950
  check_op(stack, MK_NEG);
×
UNCOV
3951
  check_size(stack, n==1);
×
UNCOV
3952
}
×
3953

UNCOV
3954
static void eval_mk_neg(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
3955
  neg_elem(stack, f);
×
UNCOV
3956
  copy_result_and_pop_frame(stack, f);
×
UNCOV
3957
}
×
3958

3959

3960
/*
3961
 * [mk-mul <arith> ... <arith>]
3962
 */
3963
static void check_mk_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
11,507 ✔
3964
  check_op(stack, MK_MUL);
11,507 ✔
3965
  check_size(stack, n>=1);
11,507 ✔
3966
}
11,507 ✔
3967

3968
static void eval_mk_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
11,507 ✔
3969
  uint32_t i;
3970
  rba_buffer_t *b;
3971

3972
  b = tstack_get_abuffer(stack);
11,507 ✔
3973
  add_elem(stack, b, f);
11,507 ✔
3974
  for (i=1; i<n; i++) {
37,634 ✔
3975
    mul_elem(stack, b, f+i);
26,127 ✔
3976
  }
3977
  tstack_pop_frame(stack);
11,507 ✔
3978
  set_arith_result(stack, b);
11,507 ✔
3979
}
11,507 ✔
3980

3981

3982
/*
3983
 * [mk-division <arith> <arith>]
3984
 */
3985
static void check_mk_division(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8 ✔
3986
  check_op(stack, MK_DIVISION);
8 ✔
3987
  check_size(stack, n == 2);
8 ✔
3988
}
8 ✔
3989

3990
// GENERIC VERSION: THE DIVIDER CAN BE ZERO OR NON-CONSTANT
3991
static void eval_mk_division(tstack_t *stack, stack_elem_t *f, uint32_t n) {
8 ✔
3992
  rba_buffer_t *b;
3993
  rational_t divider;
3994
  term_t t1, t2, t;
3995

3996
  q_init(&divider);
8 ✔
3997
  if (elem_is_nz_constant(f + 1, &divider)) {
8 ✔
3998
    assert(q_is_nonzero(&divider));
3999
    // Division by a non-zero constant
4000
    if (f->tag == TAG_RATIONAL) {
8 ✔
4001
      q_div(&f->val.rational, &divider);
8 ✔
4002
      copy_result_and_pop_frame(stack, f);
8 ✔
4003
    } else {
UNCOV
4004
      b = tstack_get_abuffer(stack);
×
UNCOV
4005
      add_elem(stack, b, f);
×
UNCOV
4006
      rba_buffer_div_const(b, &divider);
×
UNCOV
4007
      tstack_pop_frame(stack);
×
UNCOV
4008
      set_arith_result(stack, b);
×
4009
    }
4010
  } else {
4011
    // Not a constant
UNCOV
4012
    t1 = get_term(stack, f);
×
UNCOV
4013
    t2 = get_term(stack, f+1);
×
UNCOV
4014
    t = _o_yices_division(t1, t2);
×
UNCOV
4015
    check_term(stack, t);
×
UNCOV
4016
    tstack_pop_frame(stack);
×
UNCOV
4017
    set_term_result(stack, t);
×
4018
  }
4019

4020
  /*
4021
   * It's safe to clear the divider only here.
4022
   * If the code above raises an exception, divider is still 0/1
4023
   * and q_clear would do nothing anyway.
4024
   */
4025
  q_clear(&divider);
8 ✔
4026
}
8 ✔
4027

4028

4029

4030

4031

4032

4033
/*
4034
 * [mk-pow <arith> <integer>]
4035
 */
UNCOV
4036
static void check_mk_pow(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4037
  check_op(stack, MK_POW);
×
UNCOV
4038
  check_size(stack, n == 2);
×
4039
  check_tag(stack, f+1, TAG_RATIONAL);
×
4040
}
×
4041

UNCOV
4042
static void eval_mk_pow(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4043
  int32_t exponent;
4044
  term_t t;
4045

UNCOV
4046
  exponent = get_integer(stack, f+1);
×
UNCOV
4047
  if (exponent < 0) {
×
UNCOV
4048
    raise_exception(stack, f+1, TSTACK_NEGATIVE_EXPONENT);
×
4049
  }
4050

4051
  /*
4052
   * Note: we could avoid creating the intermediate term t
4053
   * by calling directly the exponentiation functions for
4054
   * arith_buffers, rationals, etc.?
4055
   */
UNCOV
4056
  t = get_term(stack, f);
×
UNCOV
4057
  t = _o_yices_power(t, exponent);
×
UNCOV
4058
  check_term(stack, t);
×
4059

UNCOV
4060
  tstack_pop_frame(stack);
×
UNCOV
4061
  set_term_result(stack, t);
×
UNCOV
4062
}
×
4063

4064
/*
4065
 * [mk-ge <arith> <arith>]
4066
 */
4067
static void check_mk_ge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
459 ✔
4068
  check_op(stack, MK_GE);
459 ✔
4069
  check_size(stack, n == 2);
459 ✔
4070
}
459 ✔
4071

4072
static void eval_mk_ge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
459 ✔
4073
  rba_buffer_t *b;
4074
  term_t t;
4075

4076
  b = tstack_get_abuffer(stack);
459 ✔
4077
  add_elem(stack, b, f);
459 ✔
4078
  sub_elem(stack, b, f+1);
459 ✔
4079
  t = arith_buffer_get_geq0_atom(b); // term for [f] - [f+1] >= 0
459 ✔
4080
  assert(t != NULL_TERM);
4081

4082
  tstack_pop_frame(stack);
459 ✔
4083
  set_term_result(stack, t);
459 ✔
4084
}
459 ✔
4085

4086

4087
/*
4088
 * [mk-gt <arith> <arith>]
4089
 */
4090
static void check_mk_gt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
14 ✔
4091
  check_op(stack, MK_GT);
14 ✔
4092
  check_size(stack, n == 2);
14 ✔
4093
}
14 ✔
4094

4095
static void eval_mk_gt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
14 ✔
4096
  rba_buffer_t *b;
4097
  term_t t;
4098

4099
  b = tstack_get_abuffer(stack);
14 ✔
4100
  add_elem(stack, b, f);
14 ✔
4101
  sub_elem(stack, b, f+1);
14 ✔
4102
  t = arith_buffer_get_gt0_atom(b); // term for [f] - [f+1] > 0
14 ✔
4103
  assert(t != NULL_TERM);
4104

4105
  tstack_pop_frame(stack);
14 ✔
4106
  set_term_result(stack, t);
14 ✔
4107
}
14 ✔
4108

4109

4110
/*
4111
 * [mk-le <arith> <arith>]
4112
 */
4113
static void check_mk_le(tstack_t *stack, stack_elem_t *f, uint32_t n) {
6 ✔
4114
  check_op(stack, MK_LE);
6 ✔
4115
  check_size(stack, n == 2);
6 ✔
4116
}
6 ✔
4117

4118
static void eval_mk_le(tstack_t *stack, stack_elem_t *f, uint32_t n) {
6 ✔
4119
  rba_buffer_t *b;
4120
  term_t t;
4121

4122
  b = tstack_get_abuffer(stack);
6 ✔
4123
  add_elem(stack, b, f);
6 ✔
4124
  sub_elem(stack, b, f+1);
6 ✔
4125
  t = arith_buffer_get_leq0_atom(b); // term for [f] - [f+1] <= 0
6 ✔
4126
  assert(t != NULL_TERM);
4127

4128
  tstack_pop_frame(stack);
6 ✔
4129
  set_term_result(stack, t);
6 ✔
4130
}
6 ✔
4131

4132

4133
/*
4134
 * [mk-lt <arith> <arith>]
4135
 */
4136
static void check_mk_lt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10 ✔
4137
  check_op(stack, MK_LT);
10 ✔
4138
  check_size(stack, n == 2);
10 ✔
4139
}
10 ✔
4140

4141
static void eval_mk_lt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
10 ✔
4142
  rba_buffer_t *b;
4143
  term_t t;
4144

4145
  b = tstack_get_abuffer(stack);
10 ✔
4146
  add_elem(stack, b, f);
10 ✔
4147
  sub_elem(stack, b, f+1);
10 ✔
4148
  t = arith_buffer_get_lt0_atom(b); // term for [f] - [f+1] < 0
10 ✔
4149
  assert(t != NULL_TERM);
4150

4151
  tstack_pop_frame(stack);
10 ✔
4152
  set_term_result(stack, t);
10 ✔
4153
}
10 ✔
4154

4155

4156

4157

4158

4159

4160
/*
4161
 * BITVECTOR ARITHMETIC
4162
 */
4163

4164
/*
4165
 * Build bitvector constant defined by val and size:
4166
 * - size = number of bits
4167
 * - val = value
4168
 * - f = frame index: it's used for error reporting only
4169
 * Raise an exception if size <= 0 or size > MAX_BV_SIZE or val is not a non-negative
4170
 * integer
4171
 *
4172
 * Warning: val is a pointer inside a stack element that is reset by q_clear on pop_frame.
4173
 * So we must make a copy before calling pop_frame
4174
 */
4175
void mk_bv_const_core(tstack_t *stack, stack_elem_t *f, int32_t size, rational_t *val) {
71,442 ✔
4176
  uint32_t k;
4177
  uint32_t *tmp;
4178
  uint64_t c;
4179

4180
  if (size <= 0) {
71,442 ✔
UNCOV
4181
    raise_exception(stack, f, TSTACK_NONPOSITIVE_BVSIZE);
×
4182
  }
4183

4184
  if (! yices_check_bvsize((uint32_t) size)) {
71,442 ✔
UNCOV
4185
    report_yices_error(stack);
×
4186
  }
4187

4188
  if (! q_is_integer(val) || ! q_is_nonneg(val)) {
71,442 ✔
UNCOV
4189
    raise_exception(stack, f, TSTACK_INVALID_BVCONSTANT);
×
4190
  }
4191

4192
  if (size <= 64) {
71,442 ✔
4193
    c =  bvconst64_from_q(size, val);
68,532 ✔
4194
    assert(c == norm64(c, size));
4195

4196
    tstack_pop_frame(stack);
68,532 ✔
4197
    set_bv64_result(stack, size, c);
68,532 ✔
4198

4199
  } else {
4200
    k = (size + 31) >> 5;
2,910 ✔
4201
    tmp = bvconst_alloc(k);
2,910 ✔
4202
    bvconst_set_q(tmp, k, val);
2,910 ✔
4203
    bvconst_normalize(tmp, size);
2,910 ✔
4204

4205
    tstack_pop_frame(stack);
2,910 ✔
4206
    set_bv_result(stack, size, tmp);
2,910 ✔
4207
  }
4208
}
71,442 ✔
4209

4210

4211
/*
4212
 * Sign-extend: bv = bitvector, idx = number of bits to add
4213
 */
4214
void mk_bv_sign_extend_core(tstack_t *stack, stack_elem_t *bv, stack_elem_t *idx) {
9,850 ✔
4215
  int32_t i;
4216
  bvlogic_buffer_t *b;
4217

4218
  i = get_integer(stack, idx);
9,850 ✔
4219
  b = tstack_get_bvlbuffer(stack);
9,850 ✔
4220
  bvl_set_elem(stack, b, bv);
9,850 ✔
4221
  if (! yices_check_bvextend(b, i)) {
9,850 ✔
UNCOV
4222
    report_yices_error(stack);
×
4223
  }
4224
  bvlogic_buffer_sign_extend(b, i + bvlogic_buffer_bitsize(b));
9,850 ✔
4225

4226
  tstack_pop_frame(stack);
9,850 ✔
4227
  set_bvlogic_result(stack, b);
9,850 ✔
4228
}
9,850 ✔
4229

4230

4231
/*
4232
 * Zero-extend: bv, idx as for sign-extend
4233
 */
4234
void mk_bv_zero_extend_core(tstack_t *stack, stack_elem_t *bv, stack_elem_t *idx) {
11,671 ✔
4235
  int32_t i;
4236
  bvlogic_buffer_t *b;
4237

4238
  i = get_integer(stack, idx);
11,671 ✔
4239
  b = tstack_get_bvlbuffer(stack);
11,671 ✔
4240
  bvl_set_elem(stack, b, bv);
11,671 ✔
4241
  if (! yices_check_bvextend(b, i)) {
11,671 ✔
UNCOV
4242
    report_yices_error(stack);
×
4243
  }
4244
  bvlogic_buffer_zero_extend(b, i + bvlogic_buffer_bitsize(b));
11,671 ✔
4245

4246
  tstack_pop_frame(stack);
11,671 ✔
4247
  set_bvlogic_result(stack, b);
11,671 ✔
4248
}
11,671 ✔
4249

4250

4251

4252

4253
/*
4254
 * [mk-bv-const <size> <value>]
4255
 */
4256
static void check_mk_bv_const(tstack_t *stack, stack_elem_t *f, uint32_t n) {
20 ✔
4257
  check_op(stack, MK_BV_CONST);
20 ✔
4258
  check_size(stack, n == 2);
20 ✔
4259
  check_tag(stack, f, TAG_RATIONAL);
20 ✔
4260
  check_tag(stack, f+1, TAG_RATIONAL);
20 ✔
4261
}
20 ✔
4262

4263
static void eval_mk_bv_const(tstack_t *stack, stack_elem_t *f, uint32_t n) {
20 ✔
4264
  int32_t size;
4265
  rational_t *val;
4266

4267
  size = get_integer(stack, f);
20 ✔
4268
  val = &f[1].val.rational;
20 ✔
4269
  mk_bv_const_core(stack, f, size, val);
20 ✔
4270
}
20 ✔
4271

4272

4273
/*
4274
 * [mk-bv-add <bv> ... <bv>]
4275
 */
4276
static void check_mk_bv_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
6,180 ✔
4277
  check_op(stack, MK_BV_ADD);
6,180 ✔
4278
  check_size(stack, n >= 1);
6,180 ✔
4279
}
6,180 ✔
4280

4281
static void eval_mk_bv_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
6,180 ✔
4282
  uint32_t i, bitsize;
4283
  bvarith_buffer_t *b;
4284
  bvarith64_buffer_t *b64;
4285

4286
  bitsize = elem_bitsize(stack, f);
6,180 ✔
4287
  if (bitsize <= 64) {
6,180 ✔
4288
    b64 = tstack_get_bva64buffer(stack, bitsize);
5,530 ✔
4289
    for (i=0; i<n; i++) {
17,596 ✔
4290
      bva64_add_elem(stack, b64, f+i);
12,066 ✔
4291
    }
4292
    tstack_pop_frame(stack);
5,530 ✔
4293
    set_bvarith64_result(stack, b64);
5,530 ✔
4294

4295
  } else {
4296
    b = tstack_get_bvabuffer(stack, bitsize);
650 ✔
4297
    for (i=0; i<n; i++) {
1,965 ✔
4298
      bva_add_elem(stack, b, f+i);
1,315 ✔
4299
    }
4300
    tstack_pop_frame(stack);
650 ✔
4301
    set_bvarith_result(stack, b);
650 ✔
4302
  }
4303
}
6,180 ✔
4304

4305

4306
/*
4307
 * [mk-bv-sub <bv> ... <bv>]
4308
 */
4309
static void check_mk_bv_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,453 ✔
4310
  check_op(stack, MK_BV_SUB);
2,453 ✔
4311
  check_size(stack, n >= 2);
2,453 ✔
4312
}
2,453 ✔
4313

4314
static void eval_mk_bv_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,453 ✔
4315
  uint32_t i, bitsize;
4316
  bvarith_buffer_t *b;
4317
  bvarith64_buffer_t *b64;
4318

4319
  bitsize = elem_bitsize(stack, f);
2,453 ✔
4320
  if (bitsize <= 64) {
2,453 ✔
4321
    b64 = tstack_get_bva64buffer(stack, bitsize);
2,313 ✔
4322
    bva64_add_elem(stack, b64, f);
2,313 ✔
4323
    for (i=1; i<n; i++) {
4,626 ✔
4324
      bva64_sub_elem(stack, b64, f+i);
2,313 ✔
4325
    }
4326

4327
    tstack_pop_frame(stack);
2,313 ✔
4328
    set_bvarith64_result(stack, b64);
2,313 ✔
4329

4330
  } else {
4331
    b = tstack_get_bvabuffer(stack, bitsize);
140 ✔
4332
    bva_add_elem(stack, b, f);
140 ✔
4333
    for (i=1; i<n; i++) {
280 ✔
4334
      bva_sub_elem(stack, b, f+i);
140 ✔
4335
    }
4336

4337
    tstack_pop_frame(stack);
140 ✔
4338
    set_bvarith_result(stack, b);
140 ✔
4339
  }
4340
}
2,453 ✔
4341

4342

4343
/*
4344
 * [mk-bv-mul <bv> ... <bv>]
4345
 */
4346
static void check_mk_bv_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,115 ✔
4347
  check_op(stack, MK_BV_MUL);
1,115 ✔
4348
  check_size(stack, n >= 1);
1,115 ✔
4349
}
1,115 ✔
4350

4351
static void eval_mk_bv_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,115 ✔
4352
  uint32_t i, bitsize;
4353
  bvarith_buffer_t *b;
4354
  bvarith64_buffer_t *b64;
4355

4356
  bitsize = elem_bitsize(stack, f);
1,115 ✔
4357
  if (bitsize <= 64) {
1,115 ✔
4358
    b64 = tstack_get_bva64buffer(stack, bitsize);
1,044 ✔
4359
    bva64_add_elem(stack, b64, f);
1,044 ✔
4360
    for (i=1; i<n; i++) {
2,109 ✔
4361
      bva64_mul_elem(stack, b64, f+i);
1,065 ✔
4362
    }
4363

4364
    tstack_pop_frame(stack);
1,044 ✔
4365
    set_bvarith64_result(stack, b64);
1,044 ✔
4366

4367
  } else {
4368
    b = tstack_get_bvabuffer(stack, bitsize);
71 ✔
4369
    bva_add_elem(stack, b, f);
71 ✔
4370
    for (i=1; i<n; i++) {
145 ✔
4371
      bva_mul_elem(stack, b, f+i);
74 ✔
4372
    }
4373

4374
    tstack_pop_frame(stack);
71 ✔
4375
    set_bvarith_result(stack, b);
71 ✔
4376
  }
4377
}
1,115 ✔
4378

4379

4380
/*
4381
 * [mk-bv-neg <bv>]
4382
 */
4383
static void check_mk_bv_neg(tstack_t *stack, stack_elem_t *f, uint32_t n) {
690 ✔
4384
  check_op(stack, MK_BV_NEG);
690 ✔
4385
  check_size(stack, n == 1);
690 ✔
4386
}
690 ✔
4387

4388
static void eval_mk_bv_neg(tstack_t *stack, stack_elem_t *f, uint32_t n) {
690 ✔
4389
  bvneg_elem(stack, f);
690 ✔
4390
  copy_result_and_pop_frame(stack, f);
690 ✔
4391
}
690 ✔
4392

4393

4394
/*
4395
 * [mk-bv-pow <bv> <integer]
4396
 */
UNCOV
4397
static void check_mk_bv_pow(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4398
  check_op(stack, MK_BV_POW);
×
UNCOV
4399
  check_size(stack, n == 2);
×
4400
  check_tag(stack, f+1, TAG_RATIONAL);
×
4401
}
×
4402

UNCOV
4403
static void eval_mk_bv_pow(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4404
  int32_t exponent;
4405
  term_t t;
4406

UNCOV
4407
  exponent = get_integer(stack, f+1);
×
UNCOV
4408
  if (exponent < 0) {
×
UNCOV
4409
    raise_exception(stack, f+1, TSTACK_NEGATIVE_EXPONENT);
×
4410
  }
4411

4412
  /*
4413
   * Note: we could avoid creating the intermediate term t
4414
   * by calling directly the exponentiation functions for
4415
   * arith_buffers, rationals, etc.?
4416
   */
UNCOV
4417
  t = get_term(stack, f);
×
UNCOV
4418
  t = _o_yices_bvpower(t, exponent);
×
UNCOV
4419
  check_term(stack, t);
×
4420

UNCOV
4421
  tstack_pop_frame(stack);
×
UNCOV
4422
  set_term_result(stack, t);
×
UNCOV
4423
}
×
4424

4425

4426
/*
4427
 * BITVECTOR LOGIC
4428
 */
4429

4430
/*
4431
 * [mk-bv-not <bv>]
4432
 */
4433
static void check_mk_bv_not(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,443 ✔
4434
  check_op(stack, MK_BV_NOT);
3,443 ✔
4435
  check_size(stack, n == 1);
3,443 ✔
4436
}
3,443 ✔
4437

4438
static void eval_mk_bv_not(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,443 ✔
4439
  bvlogic_buffer_t *b;
4440

4441
  b = tstack_get_bvlbuffer(stack);
3,443 ✔
4442
  bvl_set_elem(stack, b, f);
3,443 ✔
4443
  bvlogic_buffer_not(b);
3,443 ✔
4444
  tstack_pop_frame(stack);
3,443 ✔
4445
  set_bvlogic_result(stack, b);
3,443 ✔
4446
}
3,443 ✔
4447

4448

4449

4450
/*
4451
 * [mk-bv-and <bv> ... <bv>]
4452
 */
4453
static void check_mk_bv_and(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,993 ✔
4454
  check_op(stack, MK_BV_AND);
4,993 ✔
4455
  check_size(stack, n >= 1);
4,993 ✔
4456
}
4,993 ✔
4457

4458
static void eval_mk_bv_and(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,993 ✔
4459
  bvlogic_buffer_t *b;
4460
  uint32_t i;
4461

4462
  b = tstack_get_bvlbuffer(stack);
4,993 ✔
4463
  bvl_set_elem(stack, b, f);
4,993 ✔
4464
  for (i=1; i<n; i++) {
11,266 ✔
4465
    bvand_elem(stack, b, f+i);
6,273 ✔
4466
  }
4467
  tstack_pop_frame(stack);
4,993 ✔
4468
  set_bvlogic_result(stack, b);
4,993 ✔
4469
}
4,993 ✔
4470

4471

4472
/*
4473
 * [mk-bv-or <bv> ... <bv>]
4474
 */
4475
static void check_mk_bv_or(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,522 ✔
4476
  check_op(stack, MK_BV_OR);
2,522 ✔
4477
  check_size(stack, n >= 1);
2,522 ✔
4478
}
2,522 ✔
4479

4480
static void eval_mk_bv_or(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,522 ✔
4481
  bvlogic_buffer_t *b;
4482
  uint32_t i;
4483

4484
  b = tstack_get_bvlbuffer(stack);
2,522 ✔
4485
  bvl_set_elem(stack, b, f);
2,522 ✔
4486
  for (i=1; i<n; i++) {
5,702 ✔
4487
    bvor_elem(stack, b, f+i);
3,180 ✔
4488
  }
4489
  tstack_pop_frame(stack);
2,522 ✔
4490
  set_bvlogic_result(stack, b);
2,522 ✔
4491
}
2,522 ✔
4492

4493

4494
/*
4495
 * [mk-bv-xor <bv> ... <bv>]
4496
 */
4497
static void check_mk_bv_xor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,447 ✔
4498
  check_op(stack, MK_BV_XOR);
1,447 ✔
4499
  check_size(stack, n >= 1);
1,447 ✔
4500
}
1,447 ✔
4501

4502
static void eval_mk_bv_xor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,447 ✔
4503
  bvlogic_buffer_t *b;
4504
  uint32_t i;
4505

4506
  b = tstack_get_bvlbuffer(stack);
1,447 ✔
4507
  bvl_set_elem(stack, b, f);
1,447 ✔
4508
  for (i=1; i<n; i++) {
3,427 ✔
4509
    bvxor_elem(stack, b, f+i);
1,980 ✔
4510
  }
4511
  tstack_pop_frame(stack);
1,447 ✔
4512
  set_bvlogic_result(stack, b);
1,447 ✔
4513
}
1,447 ✔
4514

4515

4516
/*
4517
 * [mk-bv-nand <bv> ... <bv>]
4518
 */
4519
static void check_mk_bv_nand(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4520
  check_op(stack, MK_BV_NAND);
×
UNCOV
4521
  check_size(stack, n >= 1);
×
UNCOV
4522
}
×
4523

UNCOV
4524
static void eval_mk_bv_nand(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4525
  bvlogic_buffer_t *b;
4526
  uint32_t i;
4527

4528
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
4529
  bvl_set_elem(stack, b, f);
×
4530
  for (i=1; i<n; i++) {
×
UNCOV
4531
    bvand_elem(stack, b, f+i);
×
4532
  }
UNCOV
4533
  bvlogic_buffer_not(b);
×
4534
  tstack_pop_frame(stack);
×
4535
  set_bvlogic_result(stack, b);
×
4536
}
×
4537

4538

4539
/*
4540
 * [mk-bv-nor <bv> ... <bv>]
4541
 */
4542
static void check_mk_bv_nor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4543
  check_op(stack, MK_BV_NOR);
×
UNCOV
4544
  check_size(stack, n >= 1);
×
UNCOV
4545
}
×
4546

UNCOV
4547
static void eval_mk_bv_nor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4548
  bvlogic_buffer_t *b;
4549
  uint32_t i;
4550

4551
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
4552
  bvl_set_elem(stack, b, f);
×
4553
  for (i=1; i<n; i++) {
×
UNCOV
4554
    bvor_elem(stack, b, f+i);
×
4555
  }
UNCOV
4556
  bvlogic_buffer_not(b);
×
4557
  tstack_pop_frame(stack);
×
4558
  set_bvlogic_result(stack, b);
×
4559
}
×
4560

4561

4562
/*
4563
 * [mk-bv-xnor <bv> ... <bv>]
4564
 */
4565
static void check_mk_bv_xnor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4566
  check_op(stack, MK_BV_XNOR);
×
UNCOV
4567
  check_size(stack, n >= 1);
×
UNCOV
4568
}
×
4569

UNCOV
4570
static void eval_mk_bv_xnor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4571
  bvlogic_buffer_t *b;
4572
  uint32_t i;
4573

UNCOV
4574
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
4575
  bvl_set_elem(stack, b, f);
×
UNCOV
4576
  for (i=1; i<n; i++) {
×
UNCOV
4577
    bvxor_elem(stack, b, f+i);
×
4578
  }
UNCOV
4579
  bvlogic_buffer_not(b);
×
UNCOV
4580
  tstack_pop_frame(stack);
×
UNCOV
4581
  set_bvlogic_result(stack, b);
×
UNCOV
4582
}
×
4583

4584

4585
/*
4586
 * [mk-bv-shift-left0 <bv> <rational>]
4587
 */
4588
static void check_mk_bv_shift_left0(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
4589
  check_op(stack, MK_BV_SHIFT_LEFT0);
1 ✔
4590
  check_size(stack, n == 2);
1 ✔
4591
  check_tag(stack, f+1, TAG_RATIONAL);
1 ✔
4592
}
1 ✔
4593

4594
static void eval_mk_bv_shift_left0(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
4595
  int32_t index;
4596
  bvlogic_buffer_t *b;
4597

4598
  index = get_integer(stack, f+1);
1 ✔
4599
  b = tstack_get_bvlbuffer(stack);
1 ✔
4600
  bvl_set_elem(stack, b, f);
1 ✔
4601
  if (! yices_check_bitshift(b, index)) {
1 ✔
4602
    report_yices_error(stack);
×
4603
  }
4604
  bvlogic_buffer_shift_left0(b, index);
1 ✔
4605
  tstack_pop_frame(stack);
1 ✔
4606
  set_bvlogic_result(stack, b);
1 ✔
4607
}
1 ✔
4608

4609

4610
/*
4611
 * [mk-bv-shift-left1 <bv> <rational>]
4612
 */
4613
static void check_mk_bv_shift_left1(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4614
  check_op(stack, MK_BV_SHIFT_LEFT1);
×
4615
  check_size(stack, n == 2);
×
UNCOV
4616
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4617
}
×
4618

UNCOV
4619
static void eval_mk_bv_shift_left1(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4620
  int32_t index;
4621
  bvlogic_buffer_t *b;
4622

4623
  index = get_integer(stack, f+1);
×
4624
  b = tstack_get_bvlbuffer(stack);
×
4625
  bvl_set_elem(stack, b, f);
×
UNCOV
4626
  if (! yices_check_bitshift(b, index)) {
×
4627
    report_yices_error(stack);
×
4628
  }
UNCOV
4629
  bvlogic_buffer_shift_left1(b, index);
×
UNCOV
4630
  tstack_pop_frame(stack);
×
4631
  set_bvlogic_result(stack, b);
×
4632
}
×
4633

4634

4635
/*
4636
 * [mk-bv-shift-right0 <bv> <rational>]
4637
 */
4638
static void check_mk_bv_shift_right0(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4639
  check_op(stack, MK_BV_SHIFT_RIGHT0);
×
4640
  check_size(stack, n == 2);
×
UNCOV
4641
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4642
}
×
4643

UNCOV
4644
static void eval_mk_bv_shift_right0(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4645
  int32_t index;
4646
  bvlogic_buffer_t *b;
4647

4648
  index = get_integer(stack, f+1);
×
4649
  b = tstack_get_bvlbuffer(stack);
×
4650
  bvl_set_elem(stack, b, f);
×
UNCOV
4651
  if (! yices_check_bitshift(b, index)) {
×
4652
    report_yices_error(stack);
×
4653
  }
UNCOV
4654
  bvlogic_buffer_shift_right0(b, index);
×
UNCOV
4655
  tstack_pop_frame(stack);
×
4656
  set_bvlogic_result(stack, b);
×
4657
}
×
4658

4659

4660
/*
4661
 * [mk-bv-shift-right1 <bv> <rational>]
4662
 */
4663
static void check_mk_bv_shift_right1(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4664
  check_op(stack, MK_BV_SHIFT_RIGHT1);
×
4665
  check_size(stack, n == 2);
×
UNCOV
4666
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4667
}
×
4668

UNCOV
4669
static void eval_mk_bv_shift_right1(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4670
  int32_t index;
4671
  bvlogic_buffer_t *b;
4672

4673
  index = get_integer(stack, f+1);
×
4674
  b = tstack_get_bvlbuffer(stack);
×
4675
  bvl_set_elem(stack, b, f);
×
UNCOV
4676
  if (! yices_check_bitshift(b, index)) {
×
4677
    report_yices_error(stack);
×
4678
  }
UNCOV
4679
  bvlogic_buffer_shift_right1(b, index);
×
UNCOV
4680
  tstack_pop_frame(stack);
×
4681
  set_bvlogic_result(stack, b);
×
4682
}
×
4683

4684

4685
/*
4686
 * [mk-bv-ashift-right <bv> <rational>]
4687
 */
4688
static void check_mk_bv_ashift_right(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4689
  check_op(stack, MK_BV_ASHIFT_RIGHT);
×
4690
  check_size(stack, n == 2);
×
UNCOV
4691
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4692
}
×
4693

UNCOV
4694
static void eval_mk_bv_ashift_right(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4695
  int32_t index;
4696
  bvlogic_buffer_t *b;
4697

4698
  index = get_integer(stack, f+1);
×
4699
  b = tstack_get_bvlbuffer(stack);
×
4700
  bvl_set_elem(stack, b, f);
×
UNCOV
4701
  if (! yices_check_bitshift(b, index)) {
×
4702
    report_yices_error(stack);
×
4703
  }
UNCOV
4704
  bvlogic_buffer_ashift_right(b, index);
×
UNCOV
4705
  tstack_pop_frame(stack);
×
4706
  set_bvlogic_result(stack, b);
×
4707
}
×
4708

4709

4710
/*
4711
 * [mk-bv-rotate-left <bv> <rational>]
4712
 */
4713
static void check_mk_bv_rotate_left(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4714
  check_op(stack, MK_BV_ROTATE_LEFT);
×
UNCOV
4715
  check_size(stack, n == 2);
×
4716
  check_tag(stack, f+1, TAG_RATIONAL);
×
4717
}
×
4718

UNCOV
4719
static void eval_mk_bv_rotate_left(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4720
  int32_t index;
4721
  bvlogic_buffer_t *b;
4722

4723
  index = get_integer(stack, f+1);
×
4724
  b = tstack_get_bvlbuffer(stack);
×
4725
  bvl_set_elem(stack, b, f);
×
4726
  if (! yices_check_bitshift(b, index)) {
×
4727
    report_yices_error(stack);
×
4728
  }
4729
  // we known 0 <= index <= bitsize of b
UNCOV
4730
  if (index < bvlogic_buffer_bitsize(b)) {
×
UNCOV
4731
    bvlogic_buffer_rotate_left(b, index);
×
4732
  }
4733
  tstack_pop_frame(stack);
×
4734
  set_bvlogic_result(stack, b);
×
4735
}
×
4736

4737
/*
4738
 * [mk-bv-rotate-right <bv> <rational>]
4739
 */
4740
static void check_mk_bv_rotate_right(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4741
  check_op(stack, MK_BV_ROTATE_RIGHT);
×
UNCOV
4742
  check_size(stack, n == 2);
×
4743
  check_tag(stack, f+1, TAG_RATIONAL);
×
4744
}
×
4745

UNCOV
4746
static void eval_mk_bv_rotate_right(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4747
  int32_t index;
4748
  bvlogic_buffer_t *b;
4749

UNCOV
4750
  index = get_integer(stack, f+1);
×
UNCOV
4751
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
4752
  bvl_set_elem(stack, b, f);
×
UNCOV
4753
  if (! yices_check_bitshift(b, index)) {
×
UNCOV
4754
    report_yices_error(stack);
×
4755
  }
4756
  // we known 0 <= index <= bitsize of b
UNCOV
4757
  if (index < bvlogic_buffer_bitsize(b)) {
×
UNCOV
4758
    bvlogic_buffer_rotate_right(b, index);
×
4759
  }
UNCOV
4760
  tstack_pop_frame(stack);
×
UNCOV
4761
  set_bvlogic_result(stack, b);
×
UNCOV
4762
}
×
4763

4764

4765
/*
4766
 * [mk-bv-extract <rational> <rational> <bv>]
4767
 */
4768
static void check_mk_bv_extract(tstack_t *stack, stack_elem_t *f, uint32_t n) {
17,371 ✔
4769
  check_op(stack, MK_BV_EXTRACT);
17,371 ✔
4770
  check_size(stack, n == 3);
17,371 ✔
4771
  check_tag(stack, f, TAG_RATIONAL);
17,371 ✔
4772
  check_tag(stack, f+1, TAG_RATIONAL);
17,371 ✔
4773
}
17,371 ✔
4774

4775
static void eval_mk_bv_extract(tstack_t *stack, stack_elem_t *f, uint32_t n) {
17,371 ✔
4776
  bvlogic_buffer_t *b;
4777
  int32_t i, j;
4778
  uint32_t size;
4779

4780
  // the syntax is (bv-extract end begin bv)
4781
  i = get_integer(stack, f);        // end index
17,371 ✔
4782
  j = get_integer(stack, f+1);      // start index
17,371 ✔
4783
  size = elem_bitsize(stack, f+2);  // vector
17,371 ✔
4784
  if (! yices_check_bvextract(size, j, i)) {
17,371 ✔
UNCOV
4785
    report_yices_error(stack);
×
4786
  }
4787

4788
  if (j == 0 && i == size-1) {
17,371 ✔
4789
    // (bv-extract 0 size-1 bv) = bv
4790
    copy_result_and_pop_frame(stack, f+2);
1,324 ✔
4791
  } else {
4792
    // copy slice [i .. j] into b
4793
    b = tstack_get_bvlbuffer(stack);
16,047 ✔
4794
    bvl_set_slice_elem(stack, b, j, i, f+2);
16,047 ✔
4795
    tstack_pop_frame(stack);
16,047 ✔
4796
    set_bvlogic_result(stack, b);
16,047 ✔
4797
  }
4798
}
17,371 ✔
4799

4800

4801
/*
4802
 * [mk-bv-concat <bv> ... <bv>]
4803
 */
4804
static void check_mk_bv_concat(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,512 ✔
4805
  check_op(stack, MK_BV_CONCAT);
4,512 ✔
4806
  check_size(stack, n >= 1);
4,512 ✔
4807
}
4,512 ✔
4808

4809
static void eval_mk_bv_concat(tstack_t *stack, stack_elem_t *f, uint32_t n) {
4,512 ✔
4810
  bvlogic_buffer_t *b;
4811
  uint32_t i;
4812

4813
  b = tstack_get_bvlbuffer(stack);
4,512 ✔
4814
  for (i=0; i<n; i++) {
19,315 ✔
4815
    bvconcat_elem(stack, b, f+i);
14,803 ✔
4816
  }
4817
  tstack_pop_frame(stack);
4,512 ✔
4818
  set_bvlogic_result(stack, b);
4,512 ✔
4819
}
4,512 ✔
4820

4821

4822
/*
4823
 * [mk-bv-repeat <bv> <rational>]
4824
 */
4825
static void check_mk_bv_repeat(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4826
  check_op(stack, MK_BV_REPEAT);
×
4827
  check_size(stack, n == 2);
×
4828
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4829
}
×
4830

UNCOV
4831
static void eval_mk_bv_repeat(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4832
  bvlogic_buffer_t *b;
4833
  int32_t i;
4834

UNCOV
4835
  i = get_integer(stack, f+1);
×
UNCOV
4836
  b = tstack_get_bvlbuffer(stack);
×
4837
  bvl_set_elem(stack, b, f);
×
4838
  // check for overflow or for i <= 0
4839
  if (! yices_check_bvrepeat(b, i)) {
×
4840
    report_yices_error(stack);
×
4841
  }
UNCOV
4842
  bvlogic_buffer_repeat_concat(b, i);
×
4843
  tstack_pop_frame(stack);
×
4844
  set_bvlogic_result(stack, b);
×
4845
}
×
4846

4847

4848

4849

4850
/*
4851
 * [mk-bv-sign-extend <bv> <rational>]
4852
 * rational n = number of bits to add
4853
 */
4854
static void check_mk_bv_sign_extend(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4855
  check_op(stack, MK_BV_SIGN_EXTEND);
×
4856
  check_size(stack, n == 2);
×
4857
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4858
}
×
4859

4860
static void eval_mk_bv_sign_extend(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
4861
  mk_bv_sign_extend_core(stack, f, f+1);
×
UNCOV
4862
}
×
4863

4864

4865

4866
/*
4867
 * [mk-bv-zero-extend <bv> <rational>]
4868
 * rational n = number of bits to add
4869
 */
UNCOV
4870
static void check_mk_bv_zero_extend(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4871
  check_op(stack, MK_BV_ZERO_EXTEND);
×
UNCOV
4872
  check_size(stack, n == 2);
×
UNCOV
4873
  check_tag(stack, f+1, TAG_RATIONAL);
×
UNCOV
4874
}
×
4875

UNCOV
4876
static void eval_mk_bv_zero_extend(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
4877
  mk_bv_zero_extend_core(stack, f, f+1);
×
UNCOV
4878
}
×
4879

4880

4881

4882

4883
/*
4884
 * BITVECTOR ATOMS
4885
 */
4886

4887
/*
4888
 * [mk-bv-ge <bv> <bv>]
4889
 */
4890
static void check_mk_bv_ge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,645 ✔
4891
  check_op(stack, MK_BV_GE);
1,645 ✔
4892
  check_size(stack, n == 2);
1,645 ✔
4893
}
1,645 ✔
4894

4895
static void eval_mk_bv_ge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,645 ✔
4896
  term_t t1, t2, t;
4897

4898
  t1 = get_term(stack, f);
1,645 ✔
4899
  t2 = get_term(stack, f+1);
1,645 ✔
4900
  t = _o_yices_bvge_atom(t1, t2);
1,645 ✔
4901
  check_term(stack, t);
1,645 ✔
4902

4903
  tstack_pop_frame(stack);
1,645 ✔
4904
  set_term_result(stack, t);
1,645 ✔
4905
}
1,645 ✔
4906

4907

4908
/*
4909
 * [mk-bv-gt <bv> <bv>]
4910
 */
4911
static void check_mk_bv_gt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,670 ✔
4912
  check_op(stack, MK_BV_GT);
1,670 ✔
4913
  check_size(stack, n == 2);
1,670 ✔
4914
}
1,670 ✔
4915

4916
static void eval_mk_bv_gt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,670 ✔
4917
  term_t t1, t2, t;
4918

4919
  t1 = get_term(stack, f);
1,670 ✔
4920
  t2 = get_term(stack, f+1);
1,670 ✔
4921
  t = _o_yices_bvgt_atom(t1, t2);
1,670 ✔
4922
  check_term(stack, t);
1,670 ✔
4923

4924
  tstack_pop_frame(stack);
1,670 ✔
4925
  set_term_result(stack, t);
1,670 ✔
4926
}
1,670 ✔
4927

4928

4929
/*
4930
 * [mk-bv-le <bv> <bv>]
4931
 */
4932
static void check_mk_bv_le(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,390 ✔
4933
  check_op(stack, MK_BV_LE);
3,390 ✔
4934
  check_size(stack, n == 2);
3,390 ✔
4935
}
3,390 ✔
4936

4937
static void eval_mk_bv_le(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,390 ✔
4938
  term_t t1, t2, t;
4939

4940
  t1 = get_term(stack, f);
3,390 ✔
4941
  t2 = get_term(stack, f+1);
3,390 ✔
4942
  t = _o_yices_bvle_atom(t1, t2);
3,390 ✔
4943
  check_term(stack, t);
3,390 ✔
4944

4945
  tstack_pop_frame(stack);
3,390 ✔
4946
  set_term_result(stack, t);
3,390 ✔
4947
}
3,390 ✔
4948

4949

4950
/*
4951
 * [mk-bv-lt <bv> <bv>]
4952
 */
4953
static void check_mk_bv_lt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,873 ✔
4954
  check_op(stack, MK_BV_LT);
3,873 ✔
4955
  check_size(stack, n == 2);
3,873 ✔
4956
}
3,873 ✔
4957

4958
static void eval_mk_bv_lt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
3,873 ✔
4959
  term_t t1, t2, t;
4960

4961
  t1 = get_term(stack, f);
3,873 ✔
4962
  t2 = get_term(stack, f+1);
3,873 ✔
4963
  t = _o_yices_bvlt_atom(t1, t2);
3,873 ✔
4964
  check_term(stack, t);
3,873 ✔
4965

4966
  tstack_pop_frame(stack);
3,873 ✔
4967
  set_term_result(stack, t);
3,873 ✔
4968
}
3,873 ✔
4969

4970

4971
/*
4972
 * [mk-bv-sge <bv> <bv>]
4973
 */
4974
static void check_mk_bv_sge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,618 ✔
4975
  check_op(stack, MK_BV_SGE);
1,618 ✔
4976
  check_size(stack, n == 2);
1,618 ✔
4977
}
1,618 ✔
4978

4979
static void eval_mk_bv_sge(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,618 ✔
4980
  term_t t1, t2, t;
4981

4982
  t1 = get_term(stack, f);
1,618 ✔
4983
  t2 = get_term(stack, f+1);
1,618 ✔
4984
  t = _o_yices_bvsge_atom(t1, t2);
1,618 ✔
4985
  check_term(stack, t);
1,618 ✔
4986

4987
  tstack_pop_frame(stack);
1,618 ✔
4988
  set_term_result(stack, t);
1,618 ✔
4989
}
1,618 ✔
4990

4991

4992
/*
4993
 * [mk-bv-sgt <bv> <bv>]
4994
 */
4995
static void check_mk_bv_sgt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,607 ✔
4996
  check_op(stack, MK_BV_SGT);
1,607 ✔
4997
  check_size(stack, n == 2);
1,607 ✔
4998
}
1,607 ✔
4999

5000
static void eval_mk_bv_sgt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1,607 ✔
5001
  term_t t1, t2, t;
5002

5003
  t1 = get_term(stack, f);
1,607 ✔
5004
  t2 = get_term(stack, f+1);
1,607 ✔
5005
  t = _o_yices_bvsgt_atom(t1, t2);
1,607 ✔
5006
  check_term(stack, t);
1,607 ✔
5007

5008
  tstack_pop_frame(stack);
1,607 ✔
5009
  set_term_result(stack, t);
1,607 ✔
5010
}
1,607 ✔
5011

5012

5013
/*
5014
 * [mk-bv-sle <bv> <bv>]
5015
 */
5016
static void check_mk_bv_sle(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,197 ✔
5017
  check_op(stack, MK_BV_SLE);
2,197 ✔
5018
  check_size(stack, n == 2);
2,197 ✔
5019
}
2,197 ✔
5020

5021
static void eval_mk_bv_sle(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,197 ✔
5022
  term_t t1, t2, t;
5023

5024
  t1 = get_term(stack, f);
2,197 ✔
5025
  t2 = get_term(stack, f+1);
2,197 ✔
5026
  t = _o_yices_bvsle_atom(t1, t2);
2,197 ✔
5027
  check_term(stack, t);
2,197 ✔
5028

5029
  tstack_pop_frame(stack);
2,197 ✔
5030
  set_term_result(stack, t);
2,197 ✔
5031
}
2,197 ✔
5032

5033

5034
/*
5035
 * [mk-bv-slt <bv> <bv>]
5036
 */
5037
static void check_mk_bv_slt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,045 ✔
5038
  check_op(stack, MK_BV_SLT);
2,045 ✔
5039
  check_size(stack, n == 2);
2,045 ✔
5040
}
2,045 ✔
5041

5042
static void eval_mk_bv_slt(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,045 ✔
5043
  term_t t1, t2, t;
5044

5045
  t1 = get_term(stack, f);
2,045 ✔
5046
  t2 = get_term(stack, f+1);
2,045 ✔
5047
  t = _o_yices_bvslt_atom(t1, t2);
2,045 ✔
5048
  check_term(stack, t);
2,045 ✔
5049

5050
  tstack_pop_frame(stack);
2,045 ✔
5051
  set_term_result(stack, t);
2,045 ✔
5052
}
2,045 ✔
5053

5054

5055

5056

5057
/*
5058
 * NEW BITVECTOR OPERATORS IN SMT-LIB
5059
 */
5060

5061
/*
5062
 * Shift operators: [shift <bv1> <bv2>]
5063
 * - bv1 and bv2 must be bitvectors of the same size
5064
 * - shift bv1
5065
 * - the number of bits to shift = value of vv2
5066
 *
5067
 * bv-shl: shift left padding with 0
5068
 * bv-lshr: logical shift right (padding with 0)
5069
 * bv-ashr: arithmetic shift (padding with sign bit)
5070
 */
5071

5072
/*
5073
 * [mk-bv-shl <bv1> <bv2>]
5074
 */
5075
static void check_mk_bv_shl(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,516 ✔
5076
  check_op(stack, MK_BV_SHL);
2,516 ✔
5077
  check_size(stack, n == 2);
2,516 ✔
5078
}
2,516 ✔
5079

5080
static void eval_mk_bv_shl(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,516 ✔
5081
  bvlogic_buffer_t *b;
5082
  bvconstant_t *c;
5083
  stack_elem_t *e;
5084
  term_t t1, t2, t;
5085

5086
  e = f + 1;
2,516 ✔
5087
  if (! elem_is_bvconst(e)) {
2,516 ✔
5088
    // variable shift
5089
    t1 = get_term(stack, f);
232 ✔
5090
    t2 = get_term(stack, f+1);
232 ✔
5091
    t = _o_yices_bvshl(t1, t2);
232 ✔
5092
    check_term(stack, t);
232 ✔
5093

5094
    tstack_pop_frame(stack);
232 ✔
5095
    set_term_result(stack, t);
232 ✔
5096

5097
  } else {
5098
    // constant shift amount
5099

5100
    b = tstack_get_bvlbuffer(stack);
2,284 ✔
5101
    bvl_set_elem(stack, b, f);
2,284 ✔
5102
    c = &stack->bvconst_buffer;
2,284 ✔
5103
    bvconst_set_elem(c, e);
2,284 ✔
5104

5105
    if (c->bitsize != bvlogic_buffer_bitsize(b)) {
2,284 ✔
UNCOV
5106
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
5107
    }
5108
    bvlogic_buffer_shl_constant(b, c->bitsize, c->data);
2,284 ✔
5109
    tstack_pop_frame(stack);
2,284 ✔
5110
    set_bvlogic_result(stack, b);
2,284 ✔
5111
  }
5112
}
2,516 ✔
5113

5114

5115
/*
5116
 * [mk-bv-lshr <bv1> <bv2>]
5117
 */
5118
static void check_mk_bv_lshr(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,597 ✔
5119
  check_op(stack, MK_BV_LSHR);
2,597 ✔
5120
  check_size(stack, n == 2);
2,597 ✔
5121
}
2,597 ✔
5122

5123
static void eval_mk_bv_lshr(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2,597 ✔
5124
  bvlogic_buffer_t *b;
5125
  bvconstant_t *c;
5126
  stack_elem_t *e;
5127
  term_t t1, t2, t;
5128

5129
  e = f + 1;
2,597 ✔
5130
  if (! elem_is_bvconst(e)) {
2,597 ✔
5131
    // variable shift
5132
    t1 = get_term(stack, f);
399 ✔
5133
    t2 = get_term(stack, f+1);
399 ✔
5134
    t = _o_yices_bvlshr(t1, t2);
399 ✔
5135
    check_term(stack, t);
399 ✔
5136

5137
    tstack_pop_frame(stack);
399 ✔
5138
    set_term_result(stack, t);
399 ✔
5139

5140
  } else {
5141
    // constant shift amount
5142

5143
    b = tstack_get_bvlbuffer(stack);
2,198 ✔
5144
    bvl_set_elem(stack, b, f);
2,198 ✔
5145
    c = &stack->bvconst_buffer;
2,198 ✔
5146
    bvconst_set_elem(c, e);
2,198 ✔
5147

5148
    if (c->bitsize != bvlogic_buffer_bitsize(b)) {
2,198 ✔
UNCOV
5149
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
5150
    }
5151
    bvlogic_buffer_lshr_constant(b, c->bitsize, c->data);
2,198 ✔
5152
    tstack_pop_frame(stack);
2,198 ✔
5153
    set_bvlogic_result(stack, b);
2,198 ✔
5154
  }
5155
}
2,597 ✔
5156

5157
/*
5158
 * [mk-bv-ashr <bv1> <bv2>]
5159
 */
5160
static void check_mk_bv_ashr(tstack_t *stack, stack_elem_t *f, uint32_t n) {
212 ✔
5161
  check_op(stack, MK_BV_ASHR);
212 ✔
5162
  check_size(stack, n == 2);
212 ✔
5163
}
212 ✔
5164

5165
static void eval_mk_bv_ashr(tstack_t *stack, stack_elem_t *f, uint32_t n) {
212 ✔
5166
  bvlogic_buffer_t *b;
5167
  bvconstant_t *c;
5168
  stack_elem_t *e;
5169
  term_t t1, t2, t;
5170

5171
  e = f + 1;
212 ✔
5172
  if (! elem_is_bvconst(e)) {
212 ✔
5173
    // variable shift
5174
    t1 = get_term(stack, f);
115 ✔
5175
    t2 = get_term(stack, f+1);
115 ✔
5176
    t = _o_yices_bvashr(t1, t2);
115 ✔
5177
    check_term(stack, t);
115 ✔
5178

5179
    tstack_pop_frame(stack);
115 ✔
5180
    set_term_result(stack, t);
115 ✔
5181

5182
  } else {
5183
    // constant shift amount
5184

5185
    b = tstack_get_bvlbuffer(stack);
97 ✔
5186
    bvl_set_elem(stack, b, f);
97 ✔
5187
    c = &stack->bvconst_buffer;
97 ✔
5188
    bvconst_set_elem(c, e);
97 ✔
5189

5190
    if (c->bitsize != bvlogic_buffer_bitsize(b)) {
97 ✔
UNCOV
5191
      raise_exception(stack, e, TSTACK_INCOMPATIBLE_BVSIZES);
×
5192
    }
5193
    bvlogic_buffer_ashr_constant(b, c->bitsize, c->data);
97 ✔
5194
    tstack_pop_frame(stack);
97 ✔
5195
    set_bvlogic_result(stack, b);
97 ✔
5196
  }
5197
}
212 ✔
5198

5199

5200
/*
5201
 * Unsigned division and remainder
5202
 */
5203

5204
/*
5205
 * [mk-bv-div <bv1> <bv2>]
5206
 */
5207
static void check_mk_bv_div(tstack_t *stack, stack_elem_t *f, uint32_t n) {
381 ✔
5208
  check_op(stack, MK_BV_DIV);
381 ✔
5209
  check_size(stack, n == 2);
381 ✔
5210
}
381 ✔
5211

5212
static void eval_mk_bv_div(tstack_t *stack, stack_elem_t *f, uint32_t n) {
381 ✔
5213
  term_t t1, t2, t;
5214

5215
  t1 = get_term(stack, f);
381 ✔
5216
  t2 = get_term(stack, f+1);
381 ✔
5217
  t = _o_yices_bvdiv(t1, t2);
381 ✔
5218
  check_term(stack, t);
381 ✔
5219

5220
  tstack_pop_frame(stack);
381 ✔
5221
  set_term_result(stack, t);
381 ✔
5222
}
381 ✔
5223

5224
/*
5225
 * [mk-bv-rem <bv1> <bv2>]
5226
 */
5227
static void check_mk_bv_rem(tstack_t *stack, stack_elem_t *f, uint32_t n) {
330 ✔
5228
  check_op(stack, MK_BV_REM);
330 ✔
5229
  check_size(stack, n == 2);
330 ✔
5230
}
330 ✔
5231

5232
static void eval_mk_bv_rem(tstack_t *stack, stack_elem_t *f, uint32_t n) {
330 ✔
5233
  term_t t1, t2, t;
5234

5235
  t1 = get_term(stack, f);
330 ✔
5236
  t2 = get_term(stack, f+1);
330 ✔
5237
  t = _o_yices_bvrem(t1, t2);
330 ✔
5238
  check_term(stack, t);
330 ✔
5239

5240
  tstack_pop_frame(stack);
330 ✔
5241
  set_term_result(stack, t);
330 ✔
5242
}
330 ✔
5243

5244

5245

5246

5247
/*
5248
 * Signed division and remainder
5249
 */
5250

5251
/*
5252
 * [mk-bv-sdiv <bv1> <bv2>]
5253
 */
5254
static void check_mk_bv_sdiv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
167 ✔
5255
  check_op(stack, MK_BV_SDIV);
167 ✔
5256
  check_size(stack, n == 2);
167 ✔
5257
}
167 ✔
5258

5259
static void eval_mk_bv_sdiv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
167 ✔
5260
  term_t t1, t2, t;
5261

5262
  t1 = get_term(stack, f);
167 ✔
5263
  t2 = get_term(stack, f+1);
167 ✔
5264
  t = _o_yices_bvsdiv(t1, t2);
167 ✔
5265
  check_term(stack, t);
167 ✔
5266

5267
  tstack_pop_frame(stack);
167 ✔
5268
  set_term_result(stack, t);
167 ✔
5269
}
167 ✔
5270

5271
/*
5272
 * [mk-bv-srem <bv1> <bv2>]
5273
 */
5274
static void check_mk_bv_srem(tstack_t *stack, stack_elem_t *f, uint32_t n) {
165 ✔
5275
  check_op(stack, MK_BV_SREM);
165 ✔
5276
  check_size(stack, n == 2);
165 ✔
5277
}
165 ✔
5278

5279
static void eval_mk_bv_srem(tstack_t *stack, stack_elem_t *f, uint32_t n) {
165 ✔
5280
  term_t t1, t2, t;
5281

5282
  t1 = get_term(stack, f);
165 ✔
5283
  t2 = get_term(stack, f+1);
165 ✔
5284
  t = _o_yices_bvsrem(t1, t2);
165 ✔
5285
  check_term(stack, t);
165 ✔
5286

5287
  tstack_pop_frame(stack);
165 ✔
5288
  set_term_result(stack, t);
165 ✔
5289
}
165 ✔
5290

5291

5292
/*
5293
 * [mk-bv-smod <bv1> <bv2>]
5294
 */
5295
static void check_mk_bv_smod(tstack_t *stack, stack_elem_t *f, uint32_t n) {
20 ✔
5296
  check_op(stack, MK_BV_SMOD);
20 ✔
5297
  check_size(stack, n == 2);
20 ✔
5298
}
20 ✔
5299

5300
static void eval_mk_bv_smod(tstack_t *stack, stack_elem_t *f, uint32_t n) {
20 ✔
5301
  term_t t1, t2, t;
5302

5303
  t1 = get_term(stack, f);
20 ✔
5304
  t2 = get_term(stack, f+1);
20 ✔
5305
  t = _o_yices_bvsmod(t1, t2);
20 ✔
5306
  check_term(stack, t);
20 ✔
5307

5308
  tstack_pop_frame(stack);
20 ✔
5309
  set_term_result(stack, t);
20 ✔
5310
}
20 ✔
5311

5312

5313

5314
/*
5315
 * Redor/redand/bvcomp
5316
 */
5317

5318
/*
5319
 * [mk-bv-redor <bv1>]
5320
 */
UNCOV
5321
static void check_mk_bv_redor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5322
  check_op(stack, MK_BV_REDOR);
×
UNCOV
5323
  check_size(stack, n == 1);
×
UNCOV
5324
}
×
5325

5326
static void eval_mk_bv_redor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
5327
  bvlogic_buffer_t *b;
5328

5329
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
5330
  bvl_set_elem(stack, b, f);
×
5331
  if (! yices_check_bvlogic_buffer(b)) {
×
UNCOV
5332
    report_yices_error(stack); // empty buffer
×
5333
  }
5334
  bvlogic_buffer_redor(b);
×
5335
  tstack_pop_frame(stack);
×
5336
  set_bvlogic_result(stack, b);
×
5337
}
×
5338

5339

5340
/*
5341
 * [mk-bv-redand <bv1>]
5342
 */
UNCOV
5343
static void check_mk_bv_redand(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5344
  check_op(stack, MK_BV_REDAND);
×
UNCOV
5345
  check_size(stack, n == 1);
×
UNCOV
5346
}
×
5347

UNCOV
5348
static void eval_mk_bv_redand(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
5349
  bvlogic_buffer_t *b;
5350

UNCOV
5351
  b = tstack_get_bvlbuffer(stack);
×
UNCOV
5352
  bvl_set_elem(stack, b, f);
×
UNCOV
5353
  if (! yices_check_bvlogic_buffer(b)) {
×
UNCOV
5354
    report_yices_error(stack); // empty buffer
×
5355
  }
UNCOV
5356
  bvlogic_buffer_redand(b);
×
UNCOV
5357
  tstack_pop_frame(stack);
×
UNCOV
5358
  set_bvlogic_result(stack, b);
×
UNCOV
5359
}
×
5360

5361

5362
/*
5363
 * [mk-bv-comp <bv1> <bv2>]
5364
 */
5365
static void check_mk_bv_comp(tstack_t *stack, stack_elem_t *f, uint32_t n) {
528 ✔
5366
  check_op(stack, MK_BV_COMP);
528 ✔
5367
  check_size(stack, n == 2);
528 ✔
5368
}
528 ✔
5369

5370
static void eval_mk_bv_comp(tstack_t *stack, stack_elem_t *f, uint32_t n) {
528 ✔
5371
  bvlogic_buffer_t *b;
5372

5373
  b = tstack_get_bvlbuffer(stack);
528 ✔
5374
  bvl_set_elem(stack, b, f);
528 ✔
5375
  bvcomp_elem(stack, b, f+1);
528 ✔
5376
  tstack_pop_frame(stack);
528 ✔
5377
  set_bvlogic_result(stack, b);
528 ✔
5378
}
528 ✔
5379

5380

5381

5382
/*
5383
 * Conversion from Booleans to bitvectors
5384
 */
5385

5386
/*
5387
 * [mk-bool-to-bv <bool> .... <bool>]
5388
 */
5389
static void check_mk_bool2bv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5390
  check_op(stack, MK_BOOL_TO_BV);
1 ✔
5391
  check_size(stack, n>=1);
1 ✔
5392
}
1 ✔
5393

5394
/*
5395
 * Build a bitvector from n boolean terms
5396
 * - we reverse the bit order: in [mk-bool-to-bv u_0 ... u_{n-1}], we want
5397
 *   u_0 to be the high order bit and u_{n-1} to be the low order bit.
5398
 * - the result is stored as a bvlogic_buffer element
5399
 */
5400
static void eval_mk_bool2bv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5401
  bvlogic_buffer_t *b;
5402
  term_t *aux;
5403
  term_t t;
5404
  uint32_t i;
5405

5406
  if (! yices_check_bvsize(n)) {
1 ✔
UNCOV
5407
    report_yices_error(stack);
×
5408
  }
5409

5410
  aux = get_aux_buffer(stack, n);
1 ✔
5411
  for (i=0; i<n; i++) {
13 ✔
5412
    t = get_term(stack, f+i);
12 ✔
5413
    if (! yices_check_boolean_term(t)) {
12 ✔
UNCOV
5414
      report_yices_error(stack);
×
5415
    }
5416
    aux[(n-1) - i] = t;
12 ✔
5417
  }
5418

5419
  b = tstack_get_bvlbuffer(stack);
1 ✔
5420
  bvlogic_buffer_set_term_array(b, __yices_globals.terms, n, aux);
1 ✔
5421

5422
  tstack_pop_frame(stack);
1 ✔
5423
  set_bvlogic_result(stack, b);
1 ✔
5424
}
1 ✔
5425

5426

5427
/*
5428
 * Extract a bit from a bitvector: [mk-bit <bv> <index> ]
5429
 */
5430
static void check_mk_bit(tstack_t *stack, stack_elem_t *f, uint32_t n) {
50 ✔
5431
  check_op(stack, MK_BIT);
50 ✔
5432
  check_size(stack, n == 2);
50 ✔
5433
  check_tag(stack, f+1, TAG_RATIONAL);
50 ✔
5434
}
50 ✔
5435

5436
static void eval_mk_bit(tstack_t *stack, stack_elem_t *f, uint32_t n) {
50 ✔
5437
  uint32_t nbits;
5438
  int32_t i;
5439
  term_t t;
5440

5441
  nbits = elem_bitsize(stack, f);
50 ✔
5442
  i = get_integer(stack, f+1);
50 ✔
5443

5444
  if (! yices_check_bitextract(nbits, i)) {
50 ✔
UNCOV
5445
    report_yices_error(stack);
×
5446
  }
5447

5448
  t = elem_bit_select(stack, f, i);
50 ✔
5449

5450
  tstack_pop_frame(stack);
50 ✔
5451
  set_term_result(stack, t);
50 ✔
5452
}
50 ✔
5453

5454

5455
/*
5456
 * MORE ARITHMETIC FUNCTIONS (FROM SMT-LIB2)
5457
 */
5458

5459
/*
5460
 * Floor/ceil/absolute value: all take a single argument
5461
 */
5462
static void check_mk_floor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5463
  check_op(stack, MK_FLOOR);
1 ✔
5464
  check_size(stack, n == 1);
1 ✔
5465
}
1 ✔
5466

5467
static void eval_mk_floor(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5468
  term_t t;
5469

5470
  t = get_term(stack, f);
1 ✔
5471
  t = _o_yices_floor(t);
1 ✔
5472
  check_term(stack, t);
1 ✔
5473

5474
  tstack_pop_frame(stack);
1 ✔
5475
  set_term_result(stack, t);
1 ✔
5476
}
1 ✔
5477

5478

5479
static void check_mk_ceil(tstack_t *stack, stack_elem_t *f, uint32_t n) {
7 ✔
5480
  check_op(stack, MK_CEIL);
7 ✔
5481
  check_size(stack, n == 1);
7 ✔
5482
}
7 ✔
5483

5484
static void eval_mk_ceil(tstack_t *stack, stack_elem_t *f, uint32_t n) {
7 ✔
5485
  term_t t;
5486

5487
  t = get_term(stack, f);
7 ✔
5488
  t = _o_yices_ceil(t);
7 ✔
5489
  check_term(stack, t);
7 ✔
5490

5491
  tstack_pop_frame(stack);
7 ✔
5492
  set_term_result(stack, t);
7 ✔
5493
}
7 ✔
5494

5495

5496

5497
static void check_mk_abs(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5498
  check_op(stack, MK_ABS);
1 ✔
5499
  check_size(stack, n == 1);
1 ✔
5500
}
1 ✔
5501

5502
static void eval_mk_abs(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5503
  term_t t;
5504

5505
  t = get_term(stack, f);
1 ✔
5506
  t = _o_yices_abs(t);
1 ✔
5507
  check_term(stack, t);
1 ✔
5508

5509
  tstack_pop_frame(stack);
1 ✔
5510
  set_term_result(stack, t);
1 ✔
5511
}
1 ✔
5512

5513

5514
/*
5515
 * Integer division and modulo: two parameters.
5516
 * NOTE: to support QF_NIA/QF_NRA and variants, we allow arbitrary dividers.
5517
 */
UNCOV
5518
static void check_mk_idiv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5519
  check_op(stack, MK_IDIV);
×
UNCOV
5520
  check_size(stack, n == 2);
×
UNCOV
5521
}
×
5522

UNCOV
5523
static void eval_mk_idiv(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
5524
  term_t t1, t2;
5525

UNCOV
5526
  t1 = get_term(stack, f);
×
UNCOV
5527
  t2 = get_term(stack, f+1); // divider
×
UNCOV
5528
  t1 = _o_yices_idiv(t1, t2);
×
UNCOV
5529
  check_term(stack, t1);
×
5530

UNCOV
5531
  tstack_pop_frame(stack);
×
UNCOV
5532
  set_term_result(stack, t1);
×
UNCOV
5533
}
×
5534

5535

5536
static void check_mk_mod(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2 ✔
5537
  check_op(stack, MK_MOD);
2 ✔
5538
  check_size(stack, n == 2);
2 ✔
5539
}
2 ✔
5540

5541
static void eval_mk_mod(tstack_t *stack, stack_elem_t *f, uint32_t n) {
2 ✔
5542
  term_t t1, t2;
5543

5544
  t1 = get_term(stack, f);
2 ✔
5545
  t2 = get_term(stack, f+1); // divider
2 ✔
5546
  t1 = _o_yices_imod(t1, t2);
2 ✔
5547
  check_term(stack, t1);
2 ✔
5548

5549
  tstack_pop_frame(stack);
2 ✔
5550
  set_term_result(stack, t1);
2 ✔
5551
}
2 ✔
5552

5553

5554
/*
5555
 * [mk-is-int <term>]
5556
 */
5557
static void check_mk_is_int(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5558
  check_op(stack, MK_IS_INT);
1 ✔
5559
  check_size(stack, n == 1);
1 ✔
5560
}
1 ✔
5561

5562
static void eval_mk_is_int(tstack_t *stack, stack_elem_t *f, uint32_t n) {
1 ✔
5563
  term_t t;
5564

5565
  t = get_term(stack, f);
1 ✔
5566
  t = _o_yices_is_int_atom(t);
1 ✔
5567
  check_term(stack, t);
1 ✔
5568

5569
  tstack_pop_frame(stack);
1 ✔
5570
  set_term_result(stack, t);
1 ✔
5571
}
1 ✔
5572

5573

5574
/*
5575
 * [mk-divides <arith> <arith> ]
5576
 * - the first term must be an arithmetic constant
5577
 */
UNCOV
5578
static void check_mk_divides(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5579
  check_op(stack, MK_DIVIDES);
×
UNCOV
5580
  check_size(stack, n == 2);
×
UNCOV
5581
}
×
5582

UNCOV
5583
static void eval_mk_divides(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
5584
  term_t t1, t2;
5585

UNCOV
5586
  t1 = get_term(stack, f); // divider
×
UNCOV
5587
  t2 = get_term(stack, f+1);
×
UNCOV
5588
  t1 = _o_yices_divides_atom(t1, t2);
×
UNCOV
5589
  check_term(stack, t1);
×
5590

UNCOV
5591
  tstack_pop_frame(stack);
×
UNCOV
5592
  set_term_result(stack, t1);
×
UNCOV
5593
}
×
5594

5595
/*
5596
 * FINITE FIELD FUNCTIONS
5597
 */
5598

5599
/*
5600
 * [mk-ff-const <numeral> [<numeral>]
5601
 */
5602
static void check_mk_ff_const(tstack_t *stack, stack_elem_t *f, uint32_t n) {
405 ✔
5603
  check_op(stack, MK_FF_CONST);
405 ✔
5604
  check_size(stack, n == 1 || n == 2);
405 ✔
5605
  check_tag(stack, f, TAG_RATIONAL);
405 ✔
5606
  if (n == 2)
405 ✔
5607
    check_tag(stack, f+1, TAG_RATIONAL);
×
5608
}
405 ✔
5609

5610
static void eval_mk_ff_const(tstack_t *stack, stack_elem_t *f, uint32_t n) {
405 ✔
5611
  rational_t val, mod;
5612

5613
  q_init(&val);
405 ✔
5614
  q_init(&mod);
405 ✔
5615

5616
  q_set(&val, &f->val.rational);
405 ✔
5617
  if (! q_is_integer(&val)) {
405 ✔
UNCOV
5618
    raise_exception(stack, f, TSTACK_INVALID_FFCONSTANT);
×
5619
  }
5620

5621
  if (n == 2) {
405 ✔
UNCOV
5622
    q_set(&mod, &(f+1)->val.rational);
×
UNCOV
5623
    if (! q_is_integer(&mod) || ! q_is_nonneg(&mod) || ! q_lt(&val, &mod)) {
×
UNCOV
5624
      raise_exception(stack, f, TSTACK_INVALID_FFCONSTANT);
×
5625
    }
5626
  } else {
5627
    // we don't know the field size yet
5628
    q_set_minus_one(&mod);
405 ✔
5629
  }
5630

5631
  tstack_pop_frame(stack);
405 ✔
5632
  // the field order (i.e. type) may be yet unknown (requires an `as` to set it)
5633
  set_ff_result(stack, &val, &mod);
405 ✔
5634

5635
  q_clear(&val);
405 ✔
5636
  q_clear(&mod);
405 ✔
5637
}
405 ✔
5638

5639
static const rational_t* ff_get_mod(tstack_t *stack, stack_elem_t *e) {
1,920 ✔
5640
  term_t t;
5641

5642
  switch (e->tag) {
1,920 ✔
UNCOV
5643
  case TAG_FINITEFIELD:
×
UNCOV
5644
    return &e->val.ff.mod;
×
5645

5646
  case TAG_TERM:
1,920 ✔
5647
  case TAG_SPECIAL_TERM:
5648
    t = e->val.term;
1,920 ✔
5649
    if (! yices_check_arith_ff_term(t)) {
1,920 ✔
UNCOV
5650
      report_yices_error(stack);
×
5651
    }
5652
    return finitefield_term_order(__yices_globals.terms, t);
1,920 ✔
5653

UNCOV
5654
  case TAG_ARITH_FF_BUFFER:
×
UNCOV
5655
    return &e->val.mod_arith_buffer.mod;
×
5656

UNCOV
5657
  default:
×
UNCOV
5658
    raise_exception(stack, e, TSTACK_INCOMPATIBLE_FFSIZES);
×
5659
    break;
5660
  }
5661
}
5662

5663
static void ff_calc_mod(tstack_t *stack, rational_t *mod, stack_elem_t *f, uint32_t n) {
456 ✔
5664
  uint32_t i;
5665
  const rational_t *tmp;
5666

5667
  q_set_minus_one(mod);
456 ✔
5668
  for (i=0; i<n; ++i) {
2,376 ✔
5669
    tmp = ff_get_mod(stack, f+i);
1,920 ✔
5670
    if (q_is_minus_one(tmp)) { continue; }
1,920 ✔
5671
    if (q_is_minus_one(mod)) { q_set(mod, tmp); continue; }
1,920 ✔
5672
    if (q_neq(tmp, mod)) {
1,464 ✔
UNCOV
5673
      raise_exception(stack, f+i, TSTACK_INCOMPATIBLE_FFSIZES);
×
5674
    }
5675
  }
5676
}
456 ✔
5677

5678
/*
5679
 * [mk-ff-add <arith> <arith> ]
5680
 */
5681
static void check_mk_ff_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
66 ✔
5682
  check_op(stack, MK_FF_ADD);
66 ✔
5683
  check_size(stack, n >= 1);
66 ✔
5684
}
66 ✔
5685

5686
static void eval_mk_ff_add(tstack_t *stack, stack_elem_t *f, uint32_t n) {
66 ✔
5687
  uint32_t i;
5688
  rba_buffer_t *b;
5689
  rational_t mod;
5690

5691
  q_init(&mod);
66 ✔
5692
  ff_calc_mod(stack, &mod, f, n);
66 ✔
5693
  b = tstack_get_abuffer(stack);
66 ✔
5694
  for (i=0; i<n; i++) {
492 ✔
5695
    ff_add_elem(stack, b, f+i);
426 ✔
5696
  }
5697
  tstack_pop_frame(stack);
66 ✔
5698
  set_arith_ff_result(stack, b, &mod);
66 ✔
5699
  q_clear(&mod);
66 ✔
5700
}
66 ✔
5701

5702
// TODO add sub command
5703
#if 0
5704
/*
5705
 * [mk-ff-sub <arith> <arith> ]
5706
 */
5707
static void check_mk_ff_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
5708
  check_op(stack, MK_FF_SUB);
5709
  check_size(stack, n >= 1);
5710
}
5711

5712
static void eval_mk_ff_sub(tstack_t *stack, stack_elem_t *f, uint32_t n) {
5713
  uint32_t i;
5714
  rba_buffer_t *b;
5715
  rational_t mod;
5716

5717
  q_init(&mod);
5718
  ff_calc_mod(stack, &mod, f, n);
5719
  b = tstack_get_abuffer(stack);
5720
  for (i=0; i<n; i++) {
5721
    ff_sub_elem(stack, b, f+i);
5722
  }
5723
  tstack_pop_frame(stack);
5724
  set_arith_ff_result(stack, b, &mod);
5725
  q_clear(&mod);
5726
}
5727
#endif
5728

5729
/*
5730
 * [mk-ff-mul <arith> <arith> ]
5731
 */
5732
static void check_mk_ff_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
390 ✔
5733
  check_op(stack, MK_FF_MUL);
390 ✔
5734
  check_size(stack, n >= 1);
390 ✔
5735
}
390 ✔
5736

5737
static void eval_mk_ff_mul(tstack_t *stack, stack_elem_t *f, uint32_t n) {
390 ✔
5738
  uint32_t i;
5739
  rba_buffer_t *b;
5740
  rational_t mod;
5741

5742
  q_init(&mod);
390 ✔
5743
  ff_calc_mod(stack, &mod, f, n);
390 ✔
5744
  b = tstack_get_abuffer(stack);
390 ✔
5745
  ff_add_elem(stack, b, f);
390 ✔
5746
  for (i=1; i<n; i++) {
1,494 ✔
5747
    ff_mul_elem(stack, b, f+i);
1,104 ✔
5748
  }
5749
  tstack_pop_frame(stack);
390 ✔
5750
  set_arith_ff_result(stack, b, &mod);
390 ✔
5751
  q_clear(&mod);
390 ✔
5752
}
390 ✔
5753

5754

5755
/*
5756
 * BUILD RESULT
5757
 */
5758

5759
/*
5760
 * [build-term <term>]
5761
 */
5762
static void check_build_term(tstack_t *stack, stack_elem_t *f, uint32_t n) {
56 ✔
5763
  check_op(stack, BUILD_TERM);
56 ✔
5764
  check_size(stack, n == 1);
56 ✔
5765
}
56 ✔
5766

5767
static void eval_build_term(tstack_t *stack, stack_elem_t *f, uint32_t n) {
56 ✔
5768
  term_t t;
5769

5770
  t = get_term(stack, f);
56 ✔
5771
  stack->result.term = t;
56 ✔
5772
  tstack_pop_frame(stack);
56 ✔
5773
  no_result(stack);
56 ✔
5774
}
56 ✔
5775

5776
/*
5777
 * [build-type <type>]
5778
 */
5779
static void check_build_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
5780
  check_op(stack, BUILD_TYPE);
×
UNCOV
5781
  check_size(stack, n == 1);
×
UNCOV
5782
  check_tag(stack, f, TAG_TYPE);
×
UNCOV
5783
}
×
5784

UNCOV
5785
static void eval_build_type(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5786
  stack->result.type = f[0].val.type;;
×
UNCOV
5787
  tstack_pop_frame(stack);
×
UNCOV
5788
  no_result(stack);
×
UNCOV
5789
}
×
5790

5791

5792

5793
/*
5794
 * Not supported or other error
5795
 */
UNCOV
5796
static void eval_error(tstack_t *stack, stack_elem_t *f, uint32_t n) {
×
UNCOV
5797
  raise_exception(stack, f, TSTACK_INVALID_OP);
×
5798
}
5799

5800

5801
/*
5802
 * Eval the top-level operation
5803
 */
5804
void tstack_eval(tstack_t *stack) {
1,489,881 ✔
5805
  uint32_t n;
5806
  int32_t op;
5807
  stack_elem_t *f;
5808

5809
  n = stack->frame;
1,489,881 ✔
5810
  f = stack->elem + n;
1,489,881 ✔
5811

5812
  if (f->val.opval.multiplicity > 0) {
1,489,881 ✔
5813
    // decrement and do nothing. This is a special associative operator
5814
    f->val.opval.multiplicity --;
51,311 ✔
5815

5816
  } else {
5817
    // pass start frame and frame size to the check and eval function
5818
    op = stack->top_op;
1,438,570 ✔
5819
    assert(stack->top > stack->frame);
5820
    n = stack->top - stack->frame - 1;
1,438,570 ✔
5821
    f ++;
1,438,570 ✔
5822

5823
    stack->op_table.check[op](stack, f, n);
1,438,570 ✔
5824
    stack->op_table.eval[op](stack, f, n);
1,438,570 ✔
5825
  }
5826
}
1,489,879 ✔
5827

5828

5829

5830
/*
5831
 * Direct calls of check[op] and eval[op]
5832
 */
5833
void call_tstack_check(tstack_t *stack, int32_t op, stack_elem_t *f, uint32_t n) {
7,898 ✔
5834
  int32_t saved_op;
5835

5836
  assert(0 <= op && op < stack->op_table.num_ops && stack->op_table.check[op] != NULL);
5837

5838
  /*
5839
   * we save top_op and replace it by op to prevent 'bad_opcode exception'
5840
   * raised by 'check[op]
5841
   */
5842
  saved_op = stack->top_op;
7,898 ✔
5843
  stack->top_op = op;
7,898 ✔
5844
  stack->op_table.check[op](stack, f, n);
7,898 ✔
5845
  stack->top_op = saved_op;
7,898 ✔
5846
}
7,898 ✔
5847

5848
void call_tstack_eval(tstack_t *stack, int32_t op, stack_elem_t *f, uint32_t n) {
7,898 ✔
5849
  assert(0 <= op && op < stack->op_table.num_ops && stack->op_table.eval[op] != NULL);
5850
  stack->top_op = op;
7,898 ✔
5851
  stack->op_table.eval[op](stack, f, n);
7,898 ✔
5852
}
7,898 ✔
5853

5854

5855

5856

5857
/*
5858
 * PREDEFINED OPERATION TABLE
5859
 */
5860
static const uint8_t assoc[NUM_BASE_OPCODES] = {
5861
  0, // NO_OP
5862
  0, // DEFINE_TYPE
5863
  0, // DEFINE_TERM
5864
  0, // BIND
5865
  0, // DECLARE_VAR
5866
  0, // DECLARE_TYPE_VAR
5867
  0, // LET
5868
  0, // MK_BV_TYPE
5869
  0, // MK_FF_TYPE
5870
  0, // MK_SCALAR_TYPE
5871
  0, // MK_TUPLE_TYPE
5872
  0, // MK_FUN_TYPE
5873
  0, // MK_APP_TYPE
5874
  0, // MK_APPLY
5875
  0, // MK_ITE
5876
  0, // MK_EQ
5877
  0, // MK_DISEQ
5878
  0, // MK_DISTINCT
5879
  0, // MK_NOT
5880
  1, // MK_OR
5881
  1, // MK_AND
5882
  1, // MK_XOR
5883
  0, // MK_IFF
5884
  0, // MK_IMPLIES
5885
  0, // MK_TUPLE
5886
  0, // MK_SELECT
5887
  0, // MK_TUPLE_UPDATE
5888
  0, // MK_UPDATE
5889
  0, // MK_FORALL
5890
  0, // MK_EXISTS
5891
  0, // MK_LAMBDA
5892
  1, // MK_ADD
5893
  0, // MK_SUB
5894
  0, // MK_NEG
5895
  1, // MK_MUL
5896
  0, // MK_DIVISION
5897
  0, // MK_POW
5898
  0, // MK_GE
5899
  0, // MK_GT
5900
  0, // MK_LE
5901
  0, // MK_LT
5902
  0, // MK_BV_CONST
5903
  1, // MK_BV_ADD
5904
  0, // MK_BV_SUB
5905
  1, // MK_BV_MUL
5906
  0, // MK_BV_NEG
5907
  0, // MK_BV_POW
5908
  0, // MK_BV_DIV
5909
  0, // MK_BV_REM
5910
  0, // MK_BV_SDIV
5911
  0, // MK_BV_SREM
5912
  0, // MK_BV_SMOD
5913
  0, // MK_BV_NOT
5914
  1, // MK_BV_AND
5915
  1, // MK_BV_OR
5916
  1, // MK_BV_XOR
5917
  0, // MK_BV_NAND
5918
  0, // MK_BV_NOR
5919
  0, // MK_BV_XNOR
5920
  0, // MK_BV_SHIFT_LEFT0
5921
  0, // MK_BV_SHIFT_LEFT1
5922
  0, // MK_BV_SHIFT_RIGHT0
5923
  0, // MK_BV_SHIFT_RIGHT1
5924
  0, // MK_BV_ASHIFT_RIGHT
5925
  0, // MK_BV_ROTATE_LEFT
5926
  0, // MK_BV_ROTATE_RIGHT
5927
  0, // MK_BV_SHL
5928
  0, // MK_BV_LSHR
5929
  0, // MK_BV_ASHR
5930
  0, // MK_BV_EXTRACT
5931
  1, // MK_BV_CONCAT
5932
  0, // MK_BV_REPEAT
5933
  0, // MK_BV_SIGN_EXTEND
5934
  0, // MK_BV_ZERO_EXTEND
5935
  0, // MK_BV_REDAND
5936
  0, // MK_BV_REDOR
5937
  0, // MK_BV_COMP
5938
  0, // MK_BV_GE
5939
  0, // MK_BV_GT
5940
  0, // MK_BV_LE
5941
  0, // MK_BV_LT
5942
  0, // MK_BV_SGE
5943
  0, // MK_BV_SGT
5944
  0, // MK_BV_SLE
5945
  0, // MK_BV_SLT
5946
  0, // MK_BOOL_TO_BV
5947
  0, // MK_BIT
5948
  0, // MK_FLOOR
5949
  0, // MK_CEIL
5950
  0, // MK_ABS
5951
  0, // MK_IDIV
5952
  0, // MK_MOD
5953
  0, // MK_DIVIDES
5954
  0, // MK_IS_INT
5955
  0, // MK_FF_CONST
5956
  1, // MK_FF_ADD
5957
  1, // MK_FF_MUL
5958
  0, // BUILD_TERM
5959
  0, // BUILD_TYPE
5960
};
5961

5962
static const check_fun_t check[NUM_BASE_OPCODES] = {
5963
  eval_error, // NO_OP
5964
  check_define_type,
5965
  check_define_term,
5966
  check_bind,
5967
  check_declare_var,
5968
  check_declare_type_var,
5969
  check_let,
5970
  check_mk_bv_type,
5971
  check_mk_ff_type,
5972
  check_mk_scalar_type,
5973
  check_mk_tuple_type,
5974
  check_mk_fun_type,
5975
  check_mk_app_type,
5976
  check_mk_apply,
5977
  check_mk_ite,
5978
  check_mk_eq,
5979
  check_mk_diseq,
5980
  check_mk_distinct,
5981
  check_mk_not,
5982
  check_mk_or,
5983
  check_mk_and,
5984
  check_mk_xor,
5985
  check_mk_iff,
5986
  check_mk_implies,
5987
  check_mk_tuple,
5988
  check_mk_select,
5989
  check_mk_tuple_update,
5990
  check_mk_update,
5991
  check_mk_forall,
5992
  check_mk_exists,
5993
  check_mk_lambda,
5994
  check_mk_add,
5995
  check_mk_sub,
5996
  check_mk_neg,
5997
  check_mk_mul,
5998
  check_mk_division,
5999
  check_mk_pow,
6000
  check_mk_ge,
6001
  check_mk_gt,
6002
  check_mk_le,
6003
  check_mk_lt,
6004
  check_mk_bv_const,
6005
  check_mk_bv_add,
6006
  check_mk_bv_sub,
6007
  check_mk_bv_mul,
6008
  check_mk_bv_neg,
6009
  check_mk_bv_pow,
6010
  check_mk_bv_div,
6011
  check_mk_bv_rem,
6012
  check_mk_bv_sdiv,
6013
  check_mk_bv_srem,
6014
  check_mk_bv_smod,
6015
  check_mk_bv_not,
6016
  check_mk_bv_and,
6017
  check_mk_bv_or,
6018
  check_mk_bv_xor,
6019
  check_mk_bv_nand,
6020
  check_mk_bv_nor,
6021
  check_mk_bv_xnor,
6022
  check_mk_bv_shift_left0,
6023
  check_mk_bv_shift_left1,
6024
  check_mk_bv_shift_right0,
6025
  check_mk_bv_shift_right1,
6026
  check_mk_bv_ashift_right,
6027
  check_mk_bv_rotate_left,
6028
  check_mk_bv_rotate_right,
6029
  check_mk_bv_shl,
6030
  check_mk_bv_lshr,
6031
  check_mk_bv_ashr,
6032
  check_mk_bv_extract,
6033
  check_mk_bv_concat,
6034
  check_mk_bv_repeat,
6035
  check_mk_bv_sign_extend,
6036
  check_mk_bv_zero_extend,
6037
  check_mk_bv_redand,
6038
  check_mk_bv_redor,
6039
  check_mk_bv_comp,
6040
  check_mk_bv_ge,
6041
  check_mk_bv_gt,
6042
  check_mk_bv_le,
6043
  check_mk_bv_lt,
6044
  check_mk_bv_sge,
6045
  check_mk_bv_sgt,
6046
  check_mk_bv_sle,
6047
  check_mk_bv_slt,
6048
  check_mk_bool2bv,
6049
  check_mk_bit,
6050
  check_mk_floor,
6051
  check_mk_ceil,
6052
  check_mk_abs,
6053
  check_mk_idiv,
6054
  check_mk_mod,
6055
  check_mk_divides,
6056
  check_mk_is_int,
6057
  check_mk_ff_const,
6058
  check_mk_ff_add,
6059
  check_mk_ff_mul,
6060
  check_build_term,
6061
  check_build_type,
6062
};
6063

6064
static const eval_fun_t eval[NUM_BASE_OPCODES] = {
6065
  NULL, // NO_OP
6066
  eval_define_type,
6067
  eval_define_term,
6068
  eval_bind,
6069
  eval_declare_var,
6070
  eval_declare_type_var,
6071
  eval_let,
6072
  eval_mk_bv_type,
6073
  eval_mk_ff_type,
6074
  eval_mk_scalar_type,
6075
  eval_mk_tuple_type,
6076
  eval_mk_fun_type,
6077
  eval_mk_app_type,
6078
  eval_mk_apply,
6079
  eval_mk_ite,
6080
  eval_mk_eq,
6081
  eval_mk_diseq,
6082
  eval_mk_distinct,
6083
  eval_mk_not,
6084
  eval_mk_or,
6085
  eval_mk_and,
6086
  eval_mk_xor,
6087
  eval_mk_iff,
6088
  eval_mk_implies,
6089
  eval_mk_tuple,
6090
  eval_mk_select,
6091
  eval_mk_tuple_update,
6092
  eval_mk_update,
6093
  eval_mk_forall,
6094
  eval_mk_exists,
6095
  eval_mk_lambda,
6096
  eval_mk_add,
6097
  eval_mk_sub,
6098
  eval_mk_neg,
6099
  eval_mk_mul,
6100
  eval_mk_division,
6101
  eval_mk_pow,
6102
  eval_mk_ge,
6103
  eval_mk_gt,
6104
  eval_mk_le,
6105
  eval_mk_lt,
6106
  eval_mk_bv_const,
6107
  eval_mk_bv_add,
6108
  eval_mk_bv_sub,
6109
  eval_mk_bv_mul,
6110
  eval_mk_bv_neg,
6111
  eval_mk_bv_pow,
6112
  eval_mk_bv_div,
6113
  eval_mk_bv_rem,
6114
  eval_mk_bv_sdiv,
6115
  eval_mk_bv_srem,
6116
  eval_mk_bv_smod,
6117
  eval_mk_bv_not,
6118
  eval_mk_bv_and,
6119
  eval_mk_bv_or,
6120
  eval_mk_bv_xor,
6121
  eval_mk_bv_nand,
6122
  eval_mk_bv_nor,
6123
  eval_mk_bv_xnor,
6124
  eval_mk_bv_shift_left0,
6125
  eval_mk_bv_shift_left1,
6126
  eval_mk_bv_shift_right0,
6127
  eval_mk_bv_shift_right1,
6128
  eval_mk_bv_ashift_right,
6129
  eval_mk_bv_rotate_left,
6130
  eval_mk_bv_rotate_right,
6131
  eval_mk_bv_shl,
6132
  eval_mk_bv_lshr,
6133
  eval_mk_bv_ashr,
6134
  eval_mk_bv_extract,
6135
  eval_mk_bv_concat,
6136
  eval_mk_bv_repeat,
6137
  eval_mk_bv_sign_extend,
6138
  eval_mk_bv_zero_extend,
6139
  eval_mk_bv_redand,
6140
  eval_mk_bv_redor,
6141
  eval_mk_bv_comp,
6142
  eval_mk_bv_ge,
6143
  eval_mk_bv_gt,
6144
  eval_mk_bv_le,
6145
  eval_mk_bv_lt,
6146
  eval_mk_bv_sge,
6147
  eval_mk_bv_sgt,
6148
  eval_mk_bv_sle,
6149
  eval_mk_bv_slt,
6150
  eval_mk_bool2bv,
6151
  eval_mk_bit,
6152
  eval_mk_floor,
6153
  eval_mk_ceil,
6154
  eval_mk_abs,
6155
  eval_mk_idiv,
6156
  eval_mk_mod,
6157
  eval_mk_divides,
6158
  eval_mk_is_int,
6159
  eval_mk_ff_const,
6160
  eval_mk_ff_add,
6161
  eval_mk_ff_mul,
6162
  eval_build_term,
6163
  eval_build_type,
6164
};
6165

6166

6167
/*
6168
 * Initialize the stack
6169
 * - n = size of the operator table (must be >= NUM_BASE_OPCODES)
6170
 * - the op_table is initialized: all default operators are defined
6171
 */
6172
void init_tstack(tstack_t *stack, uint32_t n) {
1,801 ✔
6173
  op_table_t *table;
6174
  uint32_t i;
6175

6176
  assert(n >= NUM_BASE_OPCODES);
6177

6178
  alloc_tstack(stack, n);
1,801 ✔
6179
  table = &stack->op_table;
1,801 ✔
6180
  for (i=0; i<NUM_BASE_OPCODES; i++) {
180,100 ✔
6181
    table->assoc[i] = assoc[i];
178,299 ✔
6182
    table->eval[i] = eval[i];
178,299 ✔
6183
    table->check[i] = check[i];
178,299 ✔
6184
  }
6185
  table->num_ops = i;
1,801 ✔
6186
}
1,801 ✔
6187

6188

6189
/*
6190
 * Delete the stack
6191
 */
6192
void delete_tstack(tstack_t *stack) {
1,801 ✔
6193
  tstack_reset(stack);
1,801 ✔
6194

6195
  safe_free(stack->elem);
1,801 ✔
6196
  stack->elem = NULL;
1,801 ✔
6197

6198
  delete_op_table(&stack->op_table);
1,801 ✔
6199

6200
  delete_arena(&stack->mem);
1,801 ✔
6201

6202
  safe_free(stack->aux_buffer);
1,801 ✔
6203
  stack->aux_buffer = NULL;
1,801 ✔
6204

6205
  if (stack->sbuffer != NULL) {
1,801 ✔
6206
    safe_free(stack->sbuffer);
11 ✔
6207
    stack->sbuffer = NULL;
11 ✔
6208
  }
6209

6210
  if (stack->name_buffer != NULL) {
1,801 ✔
6211
    safe_free(stack->name_buffer);
864 ✔
6212
    stack->name_buffer = NULL;
864 ✔
6213
  }
6214

6215
  delete_bvconstant(&stack->bvconst_buffer);
1,801 ✔
6216

6217
  if (stack->abuffer != NULL) {
1,801 ✔
6218
    yices_free_arith_buffer(stack->abuffer);
722 ✔
6219
    stack->abuffer = NULL;
722 ✔
6220
  }
6221

6222
  if (stack->bva64buffer != NULL) {
1,801 ✔
6223
    yices_free_bvarith64_buffer(stack->bva64buffer);
368 ✔
6224
    stack->bva64buffer = NULL;
368 ✔
6225
  }
6226

6227
  if (stack->bvabuffer != NULL) {
1,801 ✔
6228
    yices_free_bvarith_buffer(stack->bvabuffer);
71 ✔
6229
    stack->bvabuffer = NULL;
71 ✔
6230
  }
6231

6232
  if (stack->bvlbuffer != NULL) {
1,801 ✔
6233
    yices_free_bvlogic_buffer(stack->bvlbuffer);
532 ✔
6234
    stack->bvlbuffer = NULL;
532 ✔
6235
  }
6236
}
1,801 ✔
6237

6238

6239
/*
6240
 * Reset all the internal buffers
6241
 */
6242
void tstack_reset_buffers(tstack_t *stack) {
26 ✔
6243
  if (stack->abuffer != NULL) {
26 ✔
UNCOV
6244
    yices_free_arith_buffer(stack->abuffer);
×
UNCOV
6245
    stack->abuffer = NULL;
×
6246
  }
6247

6248
  if (stack->bva64buffer != NULL) {
26 ✔
6249
    yices_free_bvarith64_buffer(stack->bva64buffer);
25 ✔
6250
    stack->bva64buffer = NULL;
25 ✔
6251
  }
6252

6253
  if (stack->bvabuffer != NULL) {
26 ✔
UNCOV
6254
    yices_free_bvarith_buffer(stack->bvabuffer);
×
UNCOV
6255
    stack->bvabuffer = NULL;
×
6256
  }
6257

6258
  if (stack->bvlbuffer != NULL) {
26 ✔
6259
    yices_free_bvlogic_buffer(stack->bvlbuffer);
21 ✔
6260
    stack->bvlbuffer = NULL;
21 ✔
6261
  }
6262
}
26 ✔
6263

6264

6265

6266
/*
6267
 * Add or replace an operator
6268
 * - op = operator code
6269
 * - asssoc = whether op is associative or not
6270
 * - eval. check = evaluator and checker functions
6271
 * - op must be non-negative and less than the operator's table size
6272
 *   (as set in init_tstack)
6273
 *
6274
 * If op is between 0 and stack->op_table.num_ops then the
6275
 * current values for op are replaced. If op is larger than
6276
 * num_ops, then a new operation is added.
6277
 */
6278
void tstack_add_op(tstack_t *stack, int32_t op, bool assoc, eval_fun_t eval, check_fun_t check) {
109,510 ✔
6279
  uint32_t i, nops;
6280

6281
  assert(0 <= op && op < stack->op_table.size);
6282

6283
  i = op;
109,510 ✔
6284
  stack->op_table.assoc[i] = assoc;
109,510 ✔
6285
  stack->op_table.eval[i] = eval;
109,510 ✔
6286
  stack->op_table.check[i] = check;
109,510 ✔
6287

6288
  nops = stack->op_table.num_ops;
109,510 ✔
6289
  if (i >= nops) {
109,510 ✔
6290
    stack->op_table.num_ops = i+1;
80,832 ✔
6291
  }
6292
}
109,510 ✔
6293

6294

6295
/*
6296
 * Get the operator enclosing the top-frame
6297
 * - if the stack is empty, it will contain just the bottom marker,
6298
 *   which has index 0 and contains
6299
 *     tag = TAG_OP
6300
 *     opval.opcode = NO_OP
6301
 *     opval.prev = 0
6302
 * - so there's no need to test for n == 0 in the code below.
6303
 */
6304
int32_t get_enclosing_op(tstack_t *stack) {
50,616 ✔
6305
  uint32_t n, i;
6306

6307
  n = stack->frame; // top frame
50,616 ✔
6308
  assert(stack->elem[n].tag == TAG_OP);
6309
  i = stack->elem[n].val.opval.prev;  // previous frame (unless n = 0)
50,616 ✔
6310
  assert(stack->elem[i].tag == TAG_OP);
6311

6312
  return stack->elem[i].val.opval.opcode;
50,616 ✔
6313
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc