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

Corrected sizeof for 2006 C-code

parent 1e9fe533
......@@ -1589,36 +1589,43 @@ 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();
C_emitLoopVariables(env);
for (int i = 0 ; i < getNumExp() ; i++) {
String iterator = "i_" + env.depth + "_" + i;
env.println("for (" + iterator + " = 0" +
" ; " +
iterator + " < " + getExp(i).C_getLimit(env, i) +
" ; " +
iterator + "++) {");
env.indent();
String iterator = "i_" + env.depth + "_" + i;
env.println("for (" + iterator + " = 0" +
" ; " +
iterator + " < " + getExp(i).C_getLimit(env, i) +
" ; " +
iterator + "++) {");
env.indent();
}
C_emitCalcIndex(env);
getType().C_emitSizeof(C_Nest(env));
for (int i = 0 ; i < getNumExp() ; i++) {
env.unindent();
env.println("}");
env.unindent();
env.println("}");
}
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));
env.print(" * " + getExp(i).C_getLimit(env, i));
}
env.println(";");
}
......
......@@ -92,6 +92,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 labcomm2006_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,
};;
......@@ -151,27 +151,27 @@ int test_decode_one(struct labcomm2006_decoder *decoder)
static void test_encode_decode(struct labcomm2006_encoder *encoder,
struct labcomm2006_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;
labcomm2006_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 != labcomm2006_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, labcomm2006_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,
n_0, n_2,
writer.pos, expected);
exit(1);
}
......@@ -196,13 +196,13 @@ int main(void)
labcomm2006_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, 16, 1, 1);
test_encode_decode(encoder, decoder, 20, 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, 28, 2, 2);
test_encode_decode(encoder, decoder, 44, 2, 2);
test_encode_decode(encoder, decoder, 12, 0, 0);
for (i = 1 ; i <= 4 ; i++) {
test_encode_decode(encoder, decoder, 12, 0, (1<<(7*i))-1);
......
......@@ -123,11 +123,13 @@ int main(int argc, char **argv)
labcomm2006_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;
}
labcomm2006_encode_test_sample_test_var(encoder, &test_var);
labcomm2006_encoder_register_more_types_A(encoder);
......@@ -194,10 +196,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