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 { ...@@ -1453,6 +1453,7 @@ aspect C_Constructor {
} }
aspect C_Sizeof { aspect C_Sizeof {
public void Decl.C_emitSizeofDeclaration(C_env env) { public void Decl.C_emitSizeofDeclaration(C_env env) {
} }
...@@ -1589,7 +1590,18 @@ aspect C_Sizeof { ...@@ -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) { 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()) { if (getType().C_isDynamic()) {
env.println("{"); env.println("{");
env.indent(); env.indent();
...@@ -1612,10 +1624,6 @@ aspect C_Sizeof { ...@@ -1612,10 +1624,6 @@ aspect C_Sizeof {
env.unindent(); env.unindent();
env.println("}"); env.println("}");
} else { } 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()); env.print("result += " + getType().C_fixedSizeof());
for (int i = 0 ; i < getNumExp() ; i++) { for (int i = 0 ; i < getNumExp() ; i++) {
env.print(" * " + getExp(i).C_getLimit(env, i)); env.print(" * " + getExp(i).C_getLimit(env, i));
......
...@@ -108,6 +108,8 @@ $(TEST_DIR)/gen/%: $(TEST_DIR)/gen/%.o | $(TEST_DIR)/gen ...@@ -108,6 +108,8 @@ $(TEST_DIR)/gen/%: $(TEST_DIR)/gen/%.o | $(TEST_DIR)/gen
$(TEST_DIR)/gen/%.o: $(TEST_DIR)/%.c | $(TEST_DIR)/gen $(TEST_DIR)/gen/%.o: $(TEST_DIR)/%.c | $(TEST_DIR)/gen
$(CC) $(CFLAGS_TEST) -o $@ -c $< $(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 $(TEST_DIR)/gen/%.c $(TEST_DIR)/gen/%.h: $(TEST_DIR)/%.lc | $(TEST_DIR)/gen
$(LABCOMM) \ $(LABCOMM) \
--c=$(TEST_DIR)/gen/$*.c \ --c=$(TEST_DIR)/gen/$*.c \
......
...@@ -103,13 +103,13 @@ static struct labcomm_reader reader = { ...@@ -103,13 +103,13 @@ static struct labcomm_reader reader = {
static int32_t encoder_data[256]; static int32_t encoder_data[256];
static test_sample_test_var encoder_var = { static test_sample_test_var encoder_var = {
.n_0 = 1, .n_0 = 1,
.n_1 = 1, .n_2 = 1,
.a = encoder_data, .a = encoder_data,
}; };
static int32_t decoder_data[256]; static int32_t decoder_data[256];
static test_sample_test_var decoder_var = { static test_sample_test_var decoder_var = {
.n_0 = 1, .n_0 = 1,
.n_1 = 1, .n_2 = 1,
.a = decoder_data, .a = decoder_data,
};; };;
...@@ -146,27 +146,27 @@ int test_decode_one(struct labcomm_decoder *decoder) ...@@ -146,27 +146,27 @@ int test_decode_one(struct labcomm_decoder *decoder)
static void test_encode_decode(struct labcomm_encoder *encoder, static void test_encode_decode(struct labcomm_encoder *encoder,
struct labcomm_decoder *decoder, 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; int err;
writer.pos = 0; writer.pos = 0;
encoder_var.n_0 = n_0; encoder_var.n_0 = n_0;
encoder_var.n_1 = n_1; encoder_var.n_2 = n_2;
encoder_var.a[0] = 314; encoder_var.a[0] = 314;
labcomm_encode_test_sample_test_var(encoder, &encoder_var); labcomm_encode_test_sample_test_var(encoder, &encoder_var);
err = test_decode_one(decoder); err = test_decode_one(decoder);
fprintf(stderr, "decode of sample %u * %u -> size=%d err=%d\n", fprintf(stderr, "decode of sample %u * 2 * %u -> size=%d err=%d\n",
n_0, n_1, writer.pos, err); n_0, n_2, writer.pos, err);
if (writer.pos != labcomm_sizeof_test_sample_test_var(&encoder_var)) { if (writer.pos != labcomm_sizeof_test_sample_test_var(&encoder_var)) {
fprintf(stderr, "Incorrect sizeof %u * %u (%d != %d)\n", fprintf(stderr, "Incorrect sizeof %u * 2 * %u (%d != %d)\n",
n_0, n_1, n_0, n_2,
writer.pos, labcomm_sizeof_test_sample_test_var(&encoder_var)); writer.pos, labcomm_sizeof_test_sample_test_var(&encoder_var));
exit(1); exit(1);
} }
if (writer.pos != expected) { if (writer.pos != expected) {
fprintf(stderr, "Unexpected size %u * %u (%d != %d)\n", fprintf(stderr, "Unexpected size %u * 2 * %u (%d != %d)\n",
n_0, n_1, n_0, n_2,
writer.pos, expected); writer.pos, expected);
exit(1); exit(1);
} }
...@@ -194,13 +194,13 @@ int main(void) ...@@ -194,13 +194,13 @@ int main(void)
labcomm_encoder_register_test_sample_test_var(encoder); labcomm_encoder_register_test_sample_test_var(encoder);
err = test_decode_one(decoder); err = test_decode_one(decoder);
fprintf(stderr, "decode of register -> index %d\n", err); 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]) { if (decoder_var.a[0] != encoder_var.a[0]) {
fprintf(stderr, "Failed to decode correct value %d != %d\n", fprintf(stderr, "Failed to decode correct value %d != %d\n",
encoder_var.a[0], decoder_var.a[0]); encoder_var.a[0], decoder_var.a[0]);
exit(1); 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); test_encode_decode(encoder, decoder, 4, 0, 0);
for (i = 1 ; i <= 4 ; i++) { for (i = 1 ; i <= 4 ; i++) {
test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1); test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1);
......
...@@ -130,11 +130,13 @@ int main(int argc, char **argv) ...@@ -130,11 +130,13 @@ int main(int argc, char **argv)
labcomm_encoder_register_test_sample_test_var(encoder); labcomm_encoder_register_test_sample_test_var(encoder);
test_var.n_0 = 2; test_var.n_0 = 2;
test_var.n_1 = 7; test_var.n_2 = 7;
test_var.a = calloc(test_var.n_0 * test_var.n_1, sizeof(*test_var.a)); 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 i = 0; i < test_var.n_0; i++)
for (int j = 0; j < test_var.n_1; j++) for (int j = 0; j < 2; j++)
test_var.a[i] = 10 * i + 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_encode_test_sample_test_var(encoder, &test_var);
labcomm_encoder_register_more_types_A(encoder); labcomm_encoder_register_more_types_A(encoder);
...@@ -217,10 +219,15 @@ int main(int argc, char **argv) ...@@ -217,10 +219,15 @@ int main(int argc, char **argv)
puts("P copied ok"); puts("P copied ok");
assert(cache_test_var.n_0 == test_var.n_0); 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 i = 0; i < test_var.n_0; i++)
for (int j = 0; j < test_var.n_1; j++) for (int j = 0; j < 2; j++)
assert(cache_test_var.a[p.n_0 * i + j] == test_var.a[p.n_0 * i + 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); free(test_var.a);
puts("test_var copied ok"); 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