Browse Source

Fix some bugs and style to satisfy gcc -Wclobbered

pull/5/head
Gavin Howard 1 year ago
parent
commit
97c59b427a
Signed by: gavin GPG Key ID: C08038BDF280D33E
  1. 12
      include/vm.h
  2. 12
      src/bc/parse.c
  3. 13
      src/num.c
  4. 19
      src/program.c
  5. 14
      src/vm.c

12
include/vm.h

@ -187,7 +187,10 @@
do { \
sigjmp_buf sjb; \
BC_SIG_LOCK; \
if (sigsetjmp(sjb, 0)) goto l; \
if (sigsetjmp(sjb, 0)) { \
assert(BC_SIG_EXC); \
goto l; \
} \
bc_vec_push(&vm.jmp_bufs, &sjb); \
BC_SIG_UNLOCK; \
} while (0)
@ -196,7 +199,10 @@
do { \
sigjmp_buf sjb; \
BC_SIG_ASSERT_LOCKED; \
if (sigsetjmp(sjb, 0)) goto l; \
if (sigsetjmp(sjb, 0)) { \
assert(BC_SIG_EXC); \
goto l; \
} \
bc_vec_push(&vm.jmp_bufs, &sjb); \
} while (0)
@ -263,6 +269,8 @@ typedef struct BcVm {
uint16_t nchars;
uint16_t line_len;
bool eof;
BcBigDig maxes[BC_PROG_GLOBALS_LEN + BC_ENABLE_EXTRA_MATH];
BcVec files;

12
src/bc/parse.c

@ -241,7 +241,6 @@ static void bc_parse_name(BcParse *p, BcInst *type,
bool *can_assign, uint8_t flags)
{
char *name;
bool need_free = true;
BC_SIG_LOCK;
@ -267,10 +266,10 @@ static void bc_parse_name(BcParse *p, BcInst *type,
}
else {
flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL);
flags |= BC_PARSE_NEEDVAL;
uint8_t flags2 = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) |
BC_PARSE_NEEDVAL;
bc_parse_expr_status(p, flags, bc_parse_next_elem);
bc_parse_expr_status(p, flags2, bc_parse_next_elem);
if (BC_ERR(p->l.t != BC_LEX_RBRACKET))
bc_parse_err(p, BC_ERROR_PARSE_TOKEN);
@ -293,7 +292,8 @@ static void bc_parse_name(BcParse *p, BcInst *type,
*can_assign = false;
// bc_parse_call() will tell us if we need to free or not.
need_free = bc_parse_call(p, name, flags);
// If not, we set to NULL because free(NULL) is safe.
if (!bc_parse_call(p, name, flags)) name = NULL;
}
else {
*type = BC_INST_VAR;
@ -304,7 +304,7 @@ static void bc_parse_name(BcParse *p, BcInst *type,
err:
BC_SIG_MAYLOCK;
if (need_free) free(name);
free(name);
BC_LONGJMP_CONT;
}

13
src/num.c

@ -899,12 +899,19 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
ardx = cpa.rdx * BC_BASE_DIGS;
bc_num_shiftLeft(&cpa, ardx);
bc_num_clean(&cpa);
azero = bc_num_shiftZero(&cpa);
brdx = cpb.rdx * BC_BASE_DIGS;
bc_num_shiftLeft(&cpb, brdx);
// We need to lock here because azero and bzero are used in the cleanup.
BC_SIG_LOCK;
azero = bc_num_shiftZero(&cpa);
bzero = bc_num_shiftZero(&cpb);
BC_SIG_UNLOCK;
bc_num_clean(&cpa);
bc_num_clean(&cpb);
bc_num_k(&cpa, &cpb, c);
@ -1231,12 +1238,14 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
}
BC_SIG_LOCK;
neg = b->neg;
b->neg = false;
bc_num_bigdig(b, &pow);
b->neg = neg;
bc_num_createCopy(&copy, a);
BC_SETJMP_LOCKED(err);
BC_SIG_UNLOCK;

19
src/program.c

@ -1360,7 +1360,6 @@ static uchar bc_program_asciifyNum(BcProgram *p, BcNum *n) {
BcNum num;
BcBigDig val;
uchar c;
num.num = NULL;
@ -1379,13 +1378,11 @@ static uchar bc_program_asciifyNum(BcProgram *p, BcNum *n) {
// it is not negative.
bc_num_bigdig2(&num, &val);
c = (uchar) val;
num_err:
BC_SIG_MAYLOCK;
bc_num_free(&num);
BC_LONGJMP_CONT;
return c;
return (uchar) val;
}
static void bc_program_asciify(BcProgram *p) {
@ -1500,7 +1497,6 @@ static void bc_program_execStr(BcProgram *p, const char *restrict code,
BcInstPtr ip;
size_t fidx, sidx;
BcNum *n;
bool exec;
assert(p->stack.len == p->tail_calls.len);
@ -1512,23 +1508,21 @@ static void bc_program_execStr(BcProgram *p, const char *restrict code,
if (cond) {
size_t idx = SIZE_MAX, then_idx, else_idx;
bool exec;
size_t idx, then_idx, else_idx;
then_idx = bc_program_index(code, bgn);
else_idx = bc_program_index(code, bgn);
exec = (r->d.n.len != 0);
if (exec) idx = then_idx;
else {
exec = (else_idx != SIZE_MAX);
idx = else_idx;
}
idx = exec ? then_idx : else_idx;
BC_SIG_LOCK;
BC_SETJMP_LOCKED(exit);
if (exec) n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR));
if (exec || (else_idx != SIZE_MAX))
n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR));
else goto exit;
if (BC_ERR(!BC_PROG_STR(n))) bc_vm_err(BC_ERROR_EXEC_TYPE);
@ -1599,6 +1593,7 @@ static void bc_program_execStr(BcProgram *p, const char *restrict code,
return;
err:
BC_SIG_MAYLOCK;
bc_parse_free(&prs);
f = bc_vec_item(&p->fns, fidx);
bc_vec_npop(&f->code, f->code.len);

14
src/vm.c

@ -527,7 +527,7 @@ static void bc_vm_stdin(void) {
BcStatus s;
BcVec buf, buffer;
size_t string = 0;
bool comment = false, done = false, hash_comment = false;
bool comment = false, hash_comment = false;
bc_lex_file(&vm.prs.l, bc_program_stdin_name);
@ -544,14 +544,12 @@ restart:
// with a backslash to the parser. The reason for that is because the parser
// treats a backslash+newline combo as whitespace, per the bc spec. In that
// case, and for strings and comments, the parser will expect more stuff.
while ((!(s = bc_read_line(&buf, ">>> ")) || s == BC_STATUS_EOF) &&
buf.len > 1)
while ((!(s = bc_read_line(&buf, ">>> ")) ||
(vm.eof = (s == BC_STATUS_EOF))) && buf.len > 1)
{
char c2, *str = buf.v;
size_t i, len = buf.len - 1;
done = (s == BC_STATUS_EOF);
for (i = 0; i < len; ++i) {
bool notend = len > i + 1;
@ -594,7 +592,7 @@ restart:
bc_vm_process(buffer.v, true);
bc_vec_empty(&buffer);
if (done) break;
if (vm.eof) break;
}
if (!BC_STATUS_IS_ERROR(s)) {
@ -608,8 +606,6 @@ restart:
#endif // BC_ENABLED
}
done = (s == BC_STATUS_EOF);
err:
BC_SIG_MAYLOCK;
@ -618,7 +614,7 @@ err:
vm.status = vm.status == BC_STATUS_QUIT || !BC_I ?
vm.status : BC_STATUS_SUCCESS;
if (!vm.status && !done) {
if (!vm.status && !vm.eof) {
bc_vec_empty(&buffer);
BC_LONGJMP_STOP;
goto restart;

Loading…
Cancel
Save