Commit f85c91ac authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Fixed incorrect sizeof calculations in C-code.

parent e70c1e31
......@@ -1453,6 +1453,7 @@ aspect C_Constructor {
}
aspect C_Sizeof {
public void Decl.C_emitSizeofDeclaration(C_env env) {
}
......@@ -1589,7 +1590,18 @@ aspect C_Sizeof {
}
}
public void Exp.C_emitSizeof(C_env env, int i) {
}
public void VariableSize.C_emitSizeof(C_env env, int i) {
env.println("result += labcomm"+env.verStr+"_size_packed32(" +
env.qualid + env.memberAccessor() + "n_" + i + ");");
}
public void ArrayType.C_emitSizeof(C_env env) {
for (int i = 0 ; i < getNumExp() ; i++) {
getExp(i).C_emitSizeof(env, i);
}
if (getType().C_isDynamic()) {
env.println("{");
env.indent();
......@@ -1612,10 +1624,6 @@ aspect C_Sizeof {
env.unindent();
env.println("}");
} else {
for (int i = 0 ; i < getNumExp() ; i++) {
env.println("result += labcomm"+env.verStr+"_size_packed32(" +
getExp(i).C_getLimit(env, i) + ");");
}
env.print("result += " + getType().C_fixedSizeof());
for (int i = 0 ; i < getNumExp() ; i++) {
env.print(" * " + getExp(i).C_getLimit(env, i));
......
......@@ -108,6 +108,8 @@ $(TEST_DIR)/gen/%: $(TEST_DIR)/gen/%.o | $(TEST_DIR)/gen
$(TEST_DIR)/gen/%.o: $(TEST_DIR)/%.c | $(TEST_DIR)/gen
$(CC) $(CFLAGS_TEST) -o $@ -c $<
.PRECIOUS: $(TEST_DIR)/gen/%.c
.PRECIOUS: $(TEST_DIR)/gen/%.h
$(TEST_DIR)/gen/%.c $(TEST_DIR)/gen/%.h: $(TEST_DIR)/%.lc | $(TEST_DIR)/gen
$(LABCOMM) \
--c=$(TEST_DIR)/gen/$*.c \
......
......@@ -103,13 +103,13 @@ static struct labcomm_reader reader = {
static int32_t encoder_data[256];
static test_sample_test_var encoder_var = {
.n_0 = 1,
.n_1 = 1,
.n_2 = 1,
.a = encoder_data,
};
static int32_t decoder_data[256];
static test_sample_test_var decoder_var = {
.n_0 = 1,
.n_1 = 1,
.n_2 = 1,
.a = decoder_data,
};;
......@@ -146,27 +146,27 @@ int test_decode_one(struct labcomm_decoder *decoder)
static void test_encode_decode(struct labcomm_encoder *encoder,
struct labcomm_decoder *decoder,
int expected, uint32_t n_0, uint32_t n_1)
int expected, uint32_t n_0, uint32_t n_2)
{
int err;
writer.pos = 0;
encoder_var.n_0 = n_0;
encoder_var.n_1 = n_1;
encoder_var.n_2 = n_2;
encoder_var.a[0] = 314;
labcomm_encode_test_sample_test_var(encoder, &encoder_var);
err = test_decode_one(decoder);
fprintf(stderr, "decode of sample %u * %u -> size=%d err=%d\n",
n_0, n_1, writer.pos, err);
fprintf(stderr, "decode of sample %u * 2 * %u -> size=%d err=%d\n",
n_0, n_2, writer.pos, err);
if (writer.pos != labcomm_sizeof_test_sample_test_var(&encoder_var)) {
fprintf(stderr, "Incorrect sizeof %u * %u (%d != %d)\n",
n_0, n_1,
fprintf(stderr, "Incorrect sizeof %u * 2 * %u (%d != %d)\n",
n_0, n_2,
writer.pos, labcomm_sizeof_test_sample_test_var(&encoder_var));
exit(1);
}
if (writer.pos != expected) {
fprintf(stderr, "Unexpected size %u * %u (%d != %d)\n",
n_0, n_1,
fprintf(stderr, "Unexpected size %u * 2 * %u (%d != %d)\n",
n_0, n_2,
writer.pos, expected);
exit(1);
}
......@@ -194,13 +194,13 @@ int main(void)
labcomm_encoder_register_test_sample_test_var(encoder);
err = test_decode_one(decoder);
fprintf(stderr, "decode of register -> index %d\n", err);
test_encode_decode(encoder, decoder, 8, 1, 1);
test_encode_decode(encoder, decoder, 12, 1, 1);
if (decoder_var.a[0] != encoder_var.a[0]) {
fprintf(stderr, "Failed to decode correct value %d != %d\n",
encoder_var.a[0], decoder_var.a[0]);
exit(1);
}
test_encode_decode(encoder, decoder, 20, 2, 2);
test_encode_decode(encoder, decoder, 36, 2, 2);
test_encode_decode(encoder, decoder, 4, 0, 0);
for (i = 1 ; i <= 4 ; i++) {
test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1);
......
......@@ -130,11 +130,13 @@ int main(int argc, char **argv)
labcomm_encoder_register_test_sample_test_var(encoder);
test_var.n_0 = 2;
test_var.n_1 = 7;
test_var.a = calloc(test_var.n_0 * test_var.n_1, sizeof(*test_var.a));
test_var.n_2 = 7;
test_var.a = calloc(test_var.n_0 * 2 * test_var.n_2, sizeof(*test_var.a));
for (int i = 0; i < test_var.n_0; i++)
for (int j = 0; j < test_var.n_1; j++)
test_var.a[i] = 10 * i + j;
for (int j = 0; j < 2; j++)
for (int k = 0; k < test_var.n_2; k++) {
test_var.a[(((i) * 2 + j) * test_var.n_2) + k] = 100 * i + 10 * j + k;
}
labcomm_encode_test_sample_test_var(encoder, &test_var);
labcomm_encoder_register_more_types_A(encoder);
......@@ -217,10 +219,15 @@ int main(int argc, char **argv)
puts("P copied ok");
assert(cache_test_var.n_0 == test_var.n_0);
assert(cache_test_var.n_1 == test_var.n_1);
assert(cache_test_var.n_2 == test_var.n_2);
for (int i = 0; i < test_var.n_0; i++)
for (int j = 0; j < test_var.n_1; j++)
assert(cache_test_var.a[p.n_0 * i + j] == test_var.a[p.n_0 * i + j]);
for (int j = 0; j < 2; j++)
for (int k = 0; k < test_var.n_2; k++) {
assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] ==
test_var.a[(((i) * 2 + j) * test_var.n_2) + k]);
assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] ==
100 * i + 10 * j + k);
}
free(test_var.a);
puts("test_var copied ok");
......
sample int test_var[_,_];
sample int test_var[_,2,_];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment