commit 4bf1114b3007996077a0647175c00f0882a0261c
parent c94c8b40f40a34d25d745fbd07f9e0b456364238
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 20 Mar 2023 14:45:58 +0100
Enforce stricter source requirements
Diffstat:
17 files changed, 149 insertions(+), 150 deletions(-)
diff --git a/01/main.c b/01/main.c
@@ -19,7 +19,7 @@ part1(void)
ans = 0;
while (readtok(buf, sizeof(buf), '\n', &pos, end)) {
- fuel = strtoull(buf, NULL, 0);
+ fuel = strtoll(buf, NULL, 0);
ans += fuel / 3 - 2;
}
@@ -39,7 +39,7 @@ part2(void)
ans = 0;
while (readtok(buf, sizeof(buf), '\n', &pos, end)) {
- fuel = strtoull(buf, NULL, 0);
+ fuel = strtoll(buf, NULL, 0);
while ((fuel = fuel / 3 - 2) > 0) {
ans += fuel;
}
diff --git a/02/main.c b/02/main.c
@@ -57,9 +57,11 @@ part2(void)
while (icc.state != ICC_HALT) {
rc = icc_step_inst(&icc);
if (rc == ICC_OOB_WRITE) {
- dvec_reserve(&icc.instructions, icc.write_addr + 1);
- icc.instructions.len =
- MAX(icc.instructions.len, icc.write_addr + 1);
+ dvec_reserve(&icc.instructions,
+ (size_t) icc.write_addr + 1);
+ icc.instructions.len = MAX(
+ icc.instructions.len,
+ (size_t) icc.write_addr + 1);
} else if (rc != ICC_OK) {
break;
}
diff --git a/03/main.c b/03/main.c
@@ -44,7 +44,7 @@ parse_lines(const char **pos, const char *end)
line->sx = x;
line->sy = y;
- val = parsei64(part+1);
+ val = (int) parsei64(part+1);
switch (part[0]) {
case 'L':
x -= val;
diff --git a/06/main.c b/06/main.c
@@ -23,7 +23,6 @@ struct planet *
planets_get(const char *name)
{
struct hashmap_link *link;
- int status;
link = hashmap_get(&planets, name, strlen(name));
return link ? link->value : NULL;
@@ -127,7 +126,7 @@ void
part2(void)
{
struct planet *p1, *p2, *san, *you;
- int i, s1, s2;
+ int s1, s2;
planets_init();
diff --git a/07/main.c b/07/main.c
@@ -33,10 +33,11 @@ run_amp(struct icc *icc, int phase, int input, int *out)
}
void
-bf_phase(struct icc *icc, struct dvec *inst, int *state,
- int max_depth, int depth, int in, int *max, int *maxstate)
+bf_phase(struct icc *icc, struct dvec *inst, size_t *state,
+ size_t max_depth, int depth, int in, int *max, size_t *maxstate)
{
- int i, k, out;
+ size_t i, k;
+ int out;
if (depth == max_depth) {
aoc_debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1],
@@ -57,16 +58,18 @@ bf_phase(struct icc *icc, struct dvec *inst, int *state,
state[depth] = i;
icc_reset(icc, inst);
aoc_debug("START AMP %i (%i):\n", depth, i);
- run_amp(icc, i, in, &out);
+ run_amp(icc, (int) i, in, &out);
aoc_debug("END AMP %i (%i): %i -> %i\n", depth, i, in, out);
- bf_phase(icc, inst, state, max_depth, depth + 1, out, max, maxstate);
+ bf_phase(icc, inst, state, max_depth,
+ depth + 1, out, max, maxstate);
}
}
int
-run_phase_loop(struct icc *iccs, struct dvec *inst, int *state, int n)
+run_phase_loop(struct icc *iccs, struct dvec *inst, size_t *state, size_t n)
{
- int i, passes, done;
+ size_t i;
+ int passes, done;
int *ins, out, in_cnt;
ins = malloc(n * sizeof(int));
@@ -85,7 +88,9 @@ run_phase_loop(struct icc *iccs, struct dvec *inst, int *state, int n)
icc_step_inst(&iccs[i]);
switch (iccs[i].state) {
case ICC_INPUT:
- iccs[i].in = (!passes && !in_cnt) ? state[i] : ins[i];
+ iccs[i].in =
+ (!passes && !in_cnt
+ ? (int) state[i] : ins[i]);
in_cnt++;
break;
case ICC_OUTPUT:
@@ -110,10 +115,11 @@ run_phase_loop(struct icc *iccs, struct dvec *inst, int *state, int n)
}
void
-bf_phase_loop(int *state, int max_depth, int depth,
- struct icc *iccs, void *inst, int *max, int *maxstate)
+bf_phase_loop(size_t *state, size_t max_depth, int depth,
+ struct icc *iccs, void *inst, int *max, size_t *maxstate)
{
- int i, k, out;
+ size_t i, k;
+ int out;
if (depth == max_depth) {
out = run_phase_loop(iccs, inst, state, max_depth);
@@ -141,7 +147,8 @@ part1(void)
{
struct icc icc;
struct dvec inst;
- int max, state[5], maxstate[5];
+ size_t state[5], maxstate[5];
+ int max;
icc_init(&icc);
icc_parse_inst(&icc, aoc.input, aoc.input_size);
@@ -165,9 +172,10 @@ part1(void)
void
part2(void)
{
+ size_t state[5], maxstate[5];
struct icc iccs[5];
struct dvec inst;
- int i, max, state[5], maxstate[5];
+ int i, max;
icc_init(&iccs[0]);
icc_parse_inst(&iccs[0], aoc.input, aoc.input_size);
diff --git a/08/main.c b/08/main.c
@@ -6,8 +6,8 @@
#include <string.h>
#include <stdlib.h>
-const int width = 25;
-const int height = 6;
+const size_t width = 25;
+const size_t height = 6;
const char *sol2 = "\
# # #### ## #### # # \n\
@@ -21,8 +21,8 @@ const char *sol2 = "\
void
part1(void)
{
- int i, k;
- int count[3];
+ size_t i, k;
+ size_t count[3];
int minz = -1, res;
i = 0;
@@ -31,8 +31,8 @@ part1(void)
for (k = 0; k < width * height; k++)
count[aoc.input[i+k] - '0'] += 1;
if (minz == -1 || minz > count[0]) {
- minz = count[0];
- res = count[1] * count[2];
+ minz = (int) count[0];
+ res = (int) (count[1] * count[2]);
}
i += k;
}
@@ -46,7 +46,7 @@ part1(void)
void
part2(void)
{
- int i, k;
+ size_t i, k;
int img[width * height];
char *str;
diff --git a/09/main.c b/09/main.c
@@ -9,7 +9,6 @@ void
part1(void)
{
struct icc icc;
- char buf[256];
icc_init(&icc);
icc_parse_inst(&icc, aoc.input, aoc.input_size);
@@ -37,7 +36,6 @@ void
part2(void)
{
struct icc icc;
- char buf[256];
icc_init(&icc);
icc_parse_inst(&icc, aoc.input, aoc.input_size);
diff --git a/10/main.c b/10/main.c
@@ -117,8 +117,8 @@ get_vis(struct hashmap *hm, struct asteroid *a1)
{
struct hashmap_link *link;
struct asteroid *a2, *tmp;
- struct dir dir, odir, *dira;
- void *key, *value;
+ struct dir dir, odir;
+ void *key;
int rc;
for (a2 = asteroids; a2; a2 = a2->next) {
@@ -146,11 +146,10 @@ get_vis(struct hashmap *hm, struct asteroid *a1)
float
angle(struct asteroid *a, struct asteroid *base)
{
- double ang;
-
if (a->x == base->x)
- return a->y > base->y ? 0 : -M_PI;
- return atan2(base->x - a->x, a->y - base->y);
+ return a->y > base->y ? 0 : -(float)M_PI;
+
+ return (float) atan2(base->x - a->x, a->y - base->y);
}
void
@@ -215,8 +214,8 @@ part2(void)
struct hashmap_iter iter;
struct asteroid **list;
struct hashmap dirmap;
- float prev, ang;
int i, k, len;
+ float prev;
asteroids_init();
find_base(&base, NULL);
@@ -228,7 +227,7 @@ part2(void)
len = 0;
for (HASHMAP_ITER(&dirmap, &iter))
len++;
- list = malloc(sizeof(struct asteroid *) * len);
+ list = malloc(sizeof(struct asteroid *) * (size_t) len);
assert(list != NULL);
i = 0;
for (HASHMAP_ITER(&dirmap, &iter))
diff --git a/11/Makefile b/11/Makefile
@@ -1,13 +0,0 @@
-CFLAGS = -g -I ../../libs/include -L ../../libs/build
-LDLIBS = -liccmp -laoc
-
-all: lib main
-
-clean:
- rm main
-
-lib:
- make -C ../../libs
-
-main: main.c ../../libs/build/*
- $(CC) -o $@ $< $(CFLAGS) $(LDLIBS)
diff --git a/11/main.c b/11/main.c
@@ -42,7 +42,7 @@ uint32_t
pos_hasher(const void *p, size_t len)
{
const struct vec2 *vec;
- int hash;
+ uint32_t hash;
vec = p;
hash = (uint16_t) vec->x;
@@ -80,9 +80,10 @@ gen_map(struct hashmap *panels, struct vec2 *pos, int rot)
dims.x = max.x - min.x + 1;
dims.y = max.y - min.y + 1;
- map = malloc((dims.x + 1) * dims.y);
+ assert(dims.x > 0 && dims.y > 0);
+ map = malloc(((size_t) dims.x + 1) * (size_t) dims.y);
assert(map != NULL);
- memset(map, '.', (dims.x + 1) * dims.y);
+ memset(map, '.', ((size_t) dims.x + 1) * (size_t) dims.y);
for (HASHMAP_ITER(panels, &iter)) {
ppos = iter.link->key;
@@ -132,7 +133,7 @@ panels_init(struct hashmap *panels, int start_color)
switch (icc.state) {
case ICC_OUTPUT:
if (state == STATE_COLOR) {
- color = mi_cast_ul(&icc.out);
+ color = (int) mi_cast_ul(&icc.out);
assert(color == 0 || color == 1);
aoc_debug("OUTPUT %i %i: %i\n", pos.x, pos.y, color);
value = color ? &white : &black;
@@ -147,7 +148,7 @@ panels_init(struct hashmap *panels, int start_color)
}
state = STATE_ROTATE;
} else if (state == STATE_ROTATE) {
- brot = mi_cast_ul(&icc.out);
+ brot = (int) mi_cast_ul(&icc.out);
assert(brot == 0 || brot == 1);
aoc_debug("ROT %i %i: %i\n", pos.x, pos.y, brot);
rot = (rot + (brot ? 1 : -1) + 4) % 4;
@@ -166,7 +167,8 @@ panels_init(struct hashmap *panels, int start_color)
if (link) {
aoc_debug("INPUT %i %i: %i\n", pos.x, pos.y,
*((int*)(*link)->value));
- mi_setv(&icc.in, *((int*)(*link)->value), MI_POS);
+ mi_setv(&icc.in, (mi_ul) *((int*)(*link)->value),
+ MI_POS);
} else {
aoc_debug("INPUT %i %i: 0\n", pos.x, pos.y);
mi_setv(&icc.in, 0, MI_POS);
diff --git a/12/info.mk b/12/info.mk
@@ -0,0 +1,4 @@
+12_SRC = 12/main.c common/main.c common/aoc.c common/util.c
+12_SRC += lib/libdvec/build/libdvec.a lib/liballoc/build/liballoc.a
+12_SRC += lib/libmaxint/build/libmaxint.a
+12_HDR = common/aoc.h common/util.h
diff --git a/12/main.c b/12/main.c
@@ -1,9 +1,12 @@
#include "aoc.h"
-#include "memvec.h"
+#include "allocator.h"
+#include "dvec.h"
+#include "util.h"
#include "maxint.h"
-#include <stdlib.h>
+#include <assert.h>
#include <stdio.h>
+#include <stdlib.h>
struct vec3 {
union {
@@ -19,29 +22,31 @@ struct moon {
};
void
-moons_init(struct memvec *vec)
+moons_init(struct dvec *vec)
{
- char *p, *p2, *tok, *val;
- struct moon m;
- int i;
+ char *p, *tok, *val;
+ struct moon m, *slot;
+ int i, rc;
m.vel = (struct vec3) { 0, 0, 0 };
- memvec_init(vec, 100, sizeof(struct moon));
+ dvec_init(vec, 100, sizeof(struct moon), &stdlib_strict_heap_allocator);
tok = aoc.input;
while ((p = strchr(tok, '\n'))) {
val = tok;
for (i = 0; i < 3 && (val = strchr(val + 1, '=')); i++)
- m.pos.axis[i] = strtol(val + 1, NULL, 10);
- memvec_add(vec, &m);
+ m.pos.axis[i] = (int) strtol(val + 1, NULL, 10);
+ slot = dvec_add_slot(vec, &rc);
+ assert(!rc);
+ memcpy(slot, &m, sizeof(struct moon));
tok = p + 1;
}
}
void
-moons_free(struct memvec *vec)
+moons_deinit(struct dvec *vec)
{
- memvec_free(vec);
+ dvec_deinit(vec);
}
int
@@ -68,24 +73,25 @@ rel(int a, int b)
}
void
-sim_step(struct memvec *moons, int show)
+sim_step(struct dvec *moons, int show)
{
struct moon *m1, *m2;
static int step = 0;
- int i, k, l, relv;
+ size_t i, k, l;
+ int relv;
/* initial state */
- for (i = 0; !step && show && i < memvec_len(moons); i++) {
- m1 = memvec_get(moons, i);
- debug("%i: %i %i %i , %i %i %i = %i\n", step, m1->pos.x, m1->pos.y, m1->pos.z,
+ for (i = 0; !step && show && i < dvec_len(moons); i++) {
+ m1 = dvec_at(moons, i);
+ aoc_debug("%i: %i %i %i , %i %i %i = %i\n", step, m1->pos.x, m1->pos.y, m1->pos.z,
m1->vel.x, m1->vel.y, m1->vel.z, get_energy(m1));
}
/* update velocity */
- for (i = 0; i < memvec_len(moons); i++) {
- for (k = i + 1; k < memvec_len(moons); k++) {
- m1 = memvec_get(moons, i);
- m2 = memvec_get(moons, k);
+ for (i = 0; i < dvec_len(moons); i++) {
+ for (k = i + 1; k < dvec_len(moons); k++) {
+ m1 = dvec_at(moons, i);
+ m2 = dvec_at(moons, k);
for (l = 0; l < 3; l++) {
relv = rel(m1->pos.axis[l], m2->pos.axis[l]);
@@ -96,12 +102,12 @@ sim_step(struct memvec *moons, int show)
}
/* update position */
- for (i = 0; i < memvec_len(moons); i++) {
- m1 = memvec_get(moons, i);
+ for (i = 0; i < dvec_len(moons); i++) {
+ m1 = dvec_at(moons, i);
for (k = 0; k < 3; k++)
m1->pos.axis[k] += m1->vel.axis[k];
- if (show) debug("%i: %i %i %i , %i %i %i = %i\n", step+1,
+ if (show) aoc_debug("%i: %i %i %i , %i %i %i = %i\n", step+1,
m1->pos.x, m1->pos.y, m1->pos.z, m1->vel.x,
m1->vel.y, m1->vel.z, get_energy(m1));
}
@@ -112,9 +118,9 @@ sim_step(struct memvec *moons, int show)
void
part1(void)
{
- struct memvec moons;
- struct moon *m;
- int i, energy;
+ struct dvec moons;
+ size_t i;
+ int energy;
moons_init(&moons);
@@ -122,29 +128,33 @@ part1(void)
sim_step(&moons, aoc.debug);
energy = 0;
- for (i = 0; i < memvec_len(&moons); i++)
- energy += get_energy(memvec_get(&moons, i));
+ for (i = 0; i < dvec_len(&moons); i++)
+ energy += get_energy(dvec_at(&moons, i));
- aoc.answer = CHKP(aprintf("%i", energy));
+ aoc.answer = aprintf("%i", energy);
aoc.solution = "12053";
- moons_free(&moons);
+ moons_deinit(&moons);
}
void
part2(void)
{
- struct memvec init, moons;
+ struct dvec init, moons;
struct maxint cycles[3];
+ struct maxint imm = MI_INIT;
+ mi_ul tmp;
struct maxint *steps;
struct moon *m1, *m2;
int same, poseq, veleq;
- int i, k;
- char *s;
+ char buf[64];
+ size_t i, k;
moons_init(&init);
- s = NULL;
+ for (i = 0; i < 3; i++)
+ mi_init(&cycles[i]);
+
for (i = 0; i < 3; i++) {
moons_init(&moons);
@@ -156,9 +166,9 @@ part2(void)
sim_step(&moons, aoc.debug == 2);
same = 1;
- for (k = 0; k < memvec_len(&moons); k++) {
- m1 = memvec_get(&moons, k);
- m2 = memvec_get(&init, k);
+ for (k = 0; k < dvec_len(&moons); k++) {
+ m1 = dvec_at(&moons, k);
+ m2 = dvec_at(&init, k);
veleq = m1->pos.axis[i] == m2->pos.axis[i];
poseq = m1->vel.axis[i] == m2->vel.axis[i];
if (!veleq || !poseq) {
@@ -167,24 +177,25 @@ part2(void)
}
}
- mi_add(&cycles[i], &mi_one);
+ mi_add(&cycles[i], &cycles[i], mi_imm(&imm, &tmp, 1, MI_POS));
}
- debug("Cycles Axis %i: %s\n", i+1, (s = mi_decstr(s, &cycles[i])));
+ mi_dec(&cycles[i], buf, sizeof(buf), 0);
+ aoc_debug("Cycles Axis %i: %s\n", i+1, buf);
- moons_free(&moons);
+ moons_deinit(&moons);
}
- free(s);
steps = &cycles[0];
for (i = 1; i < 3; i++)
- mi_lcm(steps, &cycles[i]);
+ mi_lcm(steps, steps, &cycles[i]);
- aoc.answer = mi_decstr(NULL, steps);
+ mi_dec(steps, buf, sizeof(buf), 0);
+ aoc.answer = strdup(buf);
aoc.solution = "320380285873116";
for (i = 0; i < 3; i++)
- mi_free(&cycles[i]);
+ mi_deinit(&cycles[i]);
- moons_free(&init);
+ moons_deinit(&init);
}
diff --git a/Makefile b/Makefile
@@ -1,6 +1,7 @@
CFLAGS = -I lib/libdvec/include -I lib/libhashmap/include
CFLAGS += -I lib/libmaxint/include -I lib/liballoc/include
-CFLAGS += -I common -g
+CFLAGS += -Wunused-variable -Wunused-function -Wformat -Wconversion
+CFLAGS += -I common -g -Werror
DAYS = $(shell seq 1 24 | xargs printf "%02i\n")
diff --git a/common/icc.c b/common/icc.c
@@ -73,14 +73,13 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len)
{
const char *pos, *end;
char buf[256];
- size_t linelen;
int val, *slot;
int rc;
pos = str;
end = str + len;
while (readtok(buf, sizeof(buf), ',', &pos, end)) {
- val = parsei64(buf);
+ val = (int) parsei64(buf);
slot = dvec_add_slot(&icc->instructions, &rc);
assert(slot);
*slot = val;
@@ -107,17 +106,16 @@ icc_debug_op_pre(struct icc *icc)
if (!aoc.debug) return;
rc = icc_get_inst(icc, icc->rip, &inst);
- if (!rc) fprintf(stderr, "%04li: (%05i) ", icc->rip, inst);
- else fprintf(stderr, "%04li: (\?\?\?\?\?) ", icc->rip);
+ if (!rc) fprintf(stderr, "%04i: (%05i) ", icc->rip, inst);
+ else fprintf(stderr, "%04i: (\?\?\?\?\?) ", icc->rip);
icc->line_terminated = false;
}
void
-icc_debug_op_main(struct icc *icc, const char *opstr, int n)
+icc_debug_op_main(struct icc *icc, const char *opstr, size_t n)
{
- int i, val, val2, inst;
- int rc;
+ int i, rc, val, val2, inst;
if (!aoc.debug) return;
@@ -136,7 +134,7 @@ icc_debug_op_main(struct icc *icc, const char *opstr, int n)
fprintf(stderr, "%i", val);
break;
case ICC_PARAM_POS:
- rc = icc_get_inst(icc, val, &val2);
+ rc = icc_get_inst(icc, (size_t) val, &val2);
if (!rc) fprintf(stderr, "[%i]=%i", val, val2);
else fprintf(stderr, "[%i]=?", val);
break;
@@ -159,7 +157,7 @@ icc_debug_op_post(struct icc *icc, int dst)
fprintf(stderr, " -> ");
- rc = icc_get_inst(icc, dst, &val);
+ rc = icc_get_inst(icc, (size_t) dst, &val);
if (!rc) fprintf(stderr, "[%i]=%i", dst, val);
else fprintf(stderr, "[%i]=?", dst);
@@ -168,11 +166,8 @@ icc_debug_op_post(struct icc *icc, int dst)
}
void
-icc_debug_op(struct icc *icc, const char *opstr, int n)
+icc_debug_op(struct icc *icc, const char *opstr, size_t n)
{
- int inst;
- int rc;
-
if (!aoc.debug) return;
icc_debug_op_main(icc, opstr, n);
@@ -457,7 +452,7 @@ icc_step_inst(struct icc *icc)
}
int
-icc_set_inst(struct icc *icc, size_t addr, int val)
+icc_set_inst(struct icc *icc, int addr, int val)
{
int *slot;
@@ -467,14 +462,14 @@ icc_set_inst(struct icc *icc, size_t addr, int val)
return ICC_OOB_WRITE;
}
- slot = dvec_at(&icc->instructions, addr);
+ slot = dvec_at(&icc->instructions, (size_t) addr);
*slot = val;
return 0;
}
int
-icc_get_inst(struct icc *icc, size_t addr, int *out)
+icc_get_inst(struct icc *icc, int addr, int *out)
{
if (addr >= icc->instructions.len) {
icc->read_addr = addr;
@@ -482,7 +477,7 @@ icc_get_inst(struct icc *icc, size_t addr, int *out)
return ICC_OOB_READ;
}
- *out = *(int*)dvec_at(&icc->instructions, addr);
+ *out = *(int*)dvec_at(&icc->instructions, (size_t) addr);
return 0;
}
diff --git a/common/icc.h b/common/icc.h
@@ -40,13 +40,13 @@ struct icc {
int state;
int in, out;
- size_t read_addr;
- size_t write_addr;
+ int read_addr;
+ int write_addr;
bool abort_on_err;
bool line_terminated;
- size_t rip, base;
+ int rip, base;
struct dvec instructions;
};
@@ -60,8 +60,8 @@ void icc_reset(struct icc *icc, struct dvec *inst);
void icc_parse_inst(struct icc *icc, const char *str, size_t len);
int icc_step_inst(struct icc *icc);
-int icc_set_inst(struct icc *icc, size_t addr, int in);
-int icc_get_inst(struct icc *icc, size_t addr, int *out);
+int icc_set_inst(struct icc *icc, int addr, int in);
+int icc_get_inst(struct icc *icc, int addr, int *out);
int icc_param_mode(int inst, int param);
int icc_get_param(struct icc *icc, int param, int *out);
diff --git a/common/iccmp.c b/common/iccmp.c
@@ -22,7 +22,7 @@ mi_tmp(int64_t val)
static mi_ul data = 0;
static struct maxint m = { &data, 1, 1, MI_POS };
- data = ABS(val);
+ data = (mi_ul) ABS(val);
m.sign = val >= 0 ? MI_POS : MI_NEG;
return &m;
@@ -37,7 +37,7 @@ icc_hashmap_cmp(const void *key1, size_t size1, const void *key2, size_t size2)
static uint32_t
icc_addr_hasher(const void *key, size_t size)
{
- return mi_cast_ul(key);
+ return (uint32_t) mi_cast_ul(key);
}
void
@@ -131,9 +131,7 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len)
struct maxint *key, *val;
const char *pos, *end;
char buf[256];
- size_t linelen;
int64_t v;
- int *slot;
int rc, rip;
pos = str;
@@ -149,10 +147,10 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len)
assert(val != NULL);
mi_init(key);
- mi_setv(key, rip, MI_POS);
+ mi_setv(key, (mi_ul) rip, MI_POS);
mi_init(val);
- mi_setv(val, ABS(v), v >= 0 ? MI_POS : MI_NEG);
+ mi_setv(val, (mi_ul) ABS(v), v >= 0 ? MI_POS : MI_NEG);
rc = hashmap_add(&icc->instructions, key,
sizeof(struct maxint), val);
@@ -192,8 +190,6 @@ icc_value_str(struct icc *icc, struct maxint *addr, size_t zfill)
static void
icc_debug_op_pre(struct icc *icc)
{
- int rc;
-
if (!aoc.debug) return;
fprintf(stderr, "%*s: ", 5, icc_literal_str(icc, &icc->rip, 0));
@@ -216,7 +212,7 @@ icc_debug_op_main(struct icc *icc, struct maxint *tmp, const char *opstr, int n)
icc->line_terminated = false;
mi_set(tmp, &icc->rip);
- instint = mi_cast_ul(inst);
+ instint = (int) mi_cast_ul(inst);
for (i = 1; i <= n; i++) {
if (i > 1) fprintf(stderr, ", ");
mi_add(tmp, tmp, mi_tmp(1));
@@ -249,8 +245,6 @@ icc_debug_op_main(struct icc *icc, struct maxint *tmp, const char *opstr, int n)
static void
icc_debug_op_post(struct icc *icc, struct maxint *addr)
{
- int rc;
-
if (!aoc.debug) return;
fprintf(stderr, " -> [%s]=%s\n", icc_literal_str(icc, addr, 0),
@@ -261,9 +255,6 @@ icc_debug_op_post(struct icc *icc, struct maxint *addr)
static void
icc_debug_op(struct icc *icc, struct maxint *tmp, const char *opstr, int n)
{
- int inst;
- int rc;
-
if (!aoc.debug) return;
icc_debug_op_main(icc, tmp, opstr, n);
@@ -353,7 +344,7 @@ icc_inst_store(struct icc *icc)
int
icc_inst_load(struct icc *icc)
{
- int out, rc;
+ int rc;
if (icc->state != ICC_OUTPUT) {
icc_debug_op(icc, &icc->tmp, "LOAD", 1);
@@ -376,7 +367,6 @@ icc_inst_load(struct icc *icc)
int
icc_inst_jmp_true(struct icc *icc)
{
- int cond, addr;
int rc;
icc_debug_op(icc, &icc->tmp, "JMPT", 2);
@@ -400,7 +390,6 @@ icc_inst_jmp_true(struct icc *icc)
int
icc_inst_jmp_false(struct icc *icc)
{
- int cond, addr;
int rc;
icc_debug_op(icc, &icc->tmp, "JMPF", 2);
@@ -476,7 +465,7 @@ icc_inst_test_eq(struct icc *icc)
int
icc_inst_base(struct icc *icc)
{
- int off, rc;
+ int rc;
icc_debug_op_main(icc, &icc->tmp, "BASE", 1);
@@ -655,13 +644,13 @@ icc_get_param(struct icc *icc, struct maxint *tmp, int param, struct maxint *out
rc = icc_read(icc, &icc->rip, &inst);
if (rc) return rc;
- mi_setv(tmp, ABS(param), param >= 0 ? MI_POS : MI_NEG);
+ mi_setv(tmp, (mi_ul) ABS(param), param >= 0 ? MI_POS : MI_NEG);
mi_add(tmp, tmp, &icc->rip);
rc = icc_read(icc, tmp, &val);
if (rc) return rc;
assert(mi_lastset(inst) == 0);
- switch (icc_param_mode(mi_cast_ul(inst), param)) {
+ switch (icc_param_mode((int) mi_cast_ul(inst), param)) {
case ICC_PARAM_IMM:
mi_set(out, val);
break;
@@ -691,12 +680,12 @@ icc_get_dest(struct icc *icc, struct maxint *tmp, int param, struct maxint *out)
rc = icc_read(icc, &icc->rip, &inst);
if (rc) return rc;
- mi_setv(tmp, ABS(param), param >= 0 ? MI_POS : MI_NEG);
+ mi_setv(tmp, (mi_ul) ABS(param), param >= 0 ? MI_POS : MI_NEG);
mi_add(tmp, tmp, &icc->rip);
rc = icc_read(icc, tmp, &val);
if (rc) return rc;
- switch (icc_param_mode(mi_cast_ul(inst), param)) {
+ switch (icc_param_mode((int) mi_cast_ul(inst), param)) {
case ICC_PARAM_POS:
mi_set(out, val);
break;
diff --git a/common/util.c b/common/util.c
@@ -69,11 +69,11 @@ aprintf(const char *fmtstr, ...)
if (nb < 0) die("util: aprintf: invalid fmtstr: %s", fmtstr);
va_end(cpy);
- str = malloc(nb + 1);
+ str = malloc((size_t) nb + 1);
if (!str) die("util: aprintf: malloc %lu", nb + 1);
va_start(ap, fmtstr);
- nb = vsnprintf(str, nb + 1, fmtstr, ap);
+ nb = vsnprintf(str, (size_t) nb + 1, fmtstr, ap);
va_end(ap);
return str;
@@ -93,8 +93,12 @@ memdup(void *data, size_t size)
void
readall(FILE *file, void **data, size_t *size)
{
+ ssize_t pos;
+
fseek(file, 0, SEEK_END);
- *size = ftell(file);
+ pos = ftell(file);
+ if (pos < 0) die("util: readall: ftell");
+ *size = (size_t) pos;
fseek(file, 0, SEEK_SET);
*data = malloc(*size);