Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sven Gestegård Robertz
LabComm
Commits
5b7e2b76
Commit
5b7e2b76
authored
May 17, 2013
by
Anders Blomdell
Browse files
Changed generated C code to use reader/writer interface and moved
labcomm_encoder/labcomm_decoder into labcomm.c
parent
88bfe766
Changes
5
Hide whitespace changes
Inline
Side-by-side
compiler/C_CodeGen.jrag
View file @
5b7e2b76
...
@@ -443,7 +443,7 @@ aspect C_Decoder {
...
@@ -443,7 +443,7 @@ aspect C_Decoder {
env = env.nestStruct("v");
env = env.nestStruct("v");
env.println("static void decode_" + getName() + "(");
env.println("static void decode_" + getName() + "(");
env.indent();
env.indent();
env.println("struct labcomm_
deco
der *
d
,");
env.println("struct labcomm_
rea
der *
r
,");
env.println("void (*handle)(");
env.println("void (*handle)(");
env.indent();
env.indent();
env.println(env.prefix + getName() + " *v,");
env.println(env.prefix + getName() + " *v,");
...
@@ -479,7 +479,7 @@ aspect C_Decoder {
...
@@ -479,7 +479,7 @@ aspect C_Decoder {
}
}
public void PrimType.C_emitDecoder(C_env env) {
public void PrimType.C_emitDecoder(C_env env) {
env.println(env.qualid + " = labcomm_
decode
_" + getName() + "(
d
);");
env.println(env.qualid + " = labcomm_
read
_" + getName() + "(
r
);");
}
}
public void UserType.C_emitDecoder(C_env env) {
public void UserType.C_emitDecoder(C_env env) {
...
@@ -525,8 +525,7 @@ aspect C_Decoder {
...
@@ -525,8 +525,7 @@ aspect C_Decoder {
}
}
public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
//env.println(env.qualid + ".n_" + i + " = labcomm_decode_int(d);");
env.println(env.qualid + ".n_" + i + " = labcomm_read_packed32(r);");
env.println(env.qualid + ".n_" + i + " = labcomm_decode_packed32(d);");
}
}
public void ArrayType.C_emitDecoderDecodeLimit(C_env env) {
public void ArrayType.C_emitDecoderDecodeLimit(C_env env) {
...
@@ -700,7 +699,7 @@ aspect C_Encoder {
...
@@ -700,7 +699,7 @@ aspect C_Encoder {
env = env.nestStruct("(*v)");
env = env.nestStruct("(*v)");
env.println("static int encode_" + getName() + "(");
env.println("static int encode_" + getName() + "(");
env.indent();
env.indent();
env.println("struct labcomm_
encod
er *
e
,");
env.println("struct labcomm_
writ
er *
w
,");
env.println(env.prefix + getName() + " *v");
env.println(env.prefix + getName() + " *v");
env.unindent();
env.unindent();
env.println(")");
env.println(")");
...
@@ -739,8 +738,8 @@ aspect C_Encoder {
...
@@ -739,8 +738,8 @@ aspect C_Encoder {
}
}
public void PrimType.C_emitEncoder(C_env env) {
public void PrimType.C_emitEncoder(C_env env) {
env.println("result = labcomm_
encod
e_" + getName() +
env.println("result = labcomm_
writ
e_" + getName() +
"(
e
, " + env.qualid + ");");
"(
w
, " + env.qualid + ");");
env.println("if (result != 0) { return result; }");
env.println("if (result != 0) { return result; }");
}
}
...
@@ -786,8 +785,7 @@ aspect C_Encoder {
...
@@ -786,8 +785,7 @@ aspect C_Encoder {
}
}
public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
//env.println("labcomm_encode_int(e, " + env.qualid + ".n_" + i + ");");
env.println("labcomm_write_packed32(w, " + env.qualid + ".n_" + i + ");");
env.println("labcomm_encode_packed32(e, " + env.qualid + ".n_" + i + ");");
}
}
public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
...
...
lib/c/labcomm.c
View file @
5b7e2b76
...
@@ -36,6 +36,27 @@
...
@@ -36,6 +36,27 @@
#define LABCOMM_VERSION "LabComm2013"
#define LABCOMM_VERSION "LabComm2013"
struct
labcomm_decoder
{
void
*
context
;
struct
labcomm_reader
reader
;
struct
{
void
*
context
;
const
struct
labcomm_lock_action
*
action
;
}
lock
;
labcomm_error_handler_callback
on_error
;
labcomm_handle_new_datatype_callback
on_new_datatype
;
};
struct
labcomm_encoder
{
void
*
context
;
struct
labcomm_writer
writer
;
struct
{
void
*
context
;
const
struct
labcomm_lock_action
*
action
;
}
lock
;
labcomm_error_handler_callback
on_error
;
};
struct
labcomm_sample_entry
{
struct
labcomm_sample_entry
{
struct
labcomm_sample_entry
*
next
;
struct
labcomm_sample_entry
*
next
;
int
index
;
int
index
;
...
@@ -408,7 +429,7 @@ int labcomm_internal_encode(
...
@@ -408,7 +429,7 @@ int labcomm_internal_encode(
if
(
result
!=
0
)
{
goto
out
;
}
if
(
result
!=
0
)
{
goto
out
;
}
result
=
labcomm_write_packed32
(
&
e
->
writer
,
index
);
result
=
labcomm_write_packed32
(
&
e
->
writer
,
index
);
if
(
result
!=
0
)
{
goto
out
;
}
if
(
result
!=
0
)
{
goto
out
;
}
result
=
encode
(
e
,
value
);
result
=
encode
(
&
e
->
writer
,
value
);
out:
out:
e
->
writer
.
action
.
end
(
&
e
->
writer
);
e
->
writer
.
action
.
end
(
&
e
->
writer
);
no_end:
no_end:
...
@@ -633,7 +654,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
...
@@ -633,7 +654,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
d
->
on_error
(
LABCOMM_ERROR_DEC_TYPE_NOT_FOUND
,
3
,
"%s(): type not found (id=0x%x)
\n
"
,
__FUNCTION__
,
result
);
d
->
on_error
(
LABCOMM_ERROR_DEC_TYPE_NOT_FOUND
,
3
,
"%s(): type not found (id=0x%x)
\n
"
,
__FUNCTION__
,
result
);
result
=
-
ENOENT
;
result
=
-
ENOENT
;
}
else
{
}
else
{
entry
->
decoder
(
d
,
entry
->
handler
,
entry
->
context
);
entry
->
decoder
(
&
d
->
reader
,
entry
->
handler
,
entry
->
context
);
}
}
}
}
}
}
...
...
lib/c/labcomm_private.h
View file @
5b7e2b76
...
@@ -50,21 +50,10 @@
...
@@ -50,21 +50,10 @@
typedef
void
(
*
labcomm_handler_function
)(
void
*
value
,
void
*
context
);
typedef
void
(
*
labcomm_handler_function
)(
void
*
value
,
void
*
context
);
typedef
void
(
*
labcomm_decoder_function
)(
typedef
void
(
*
labcomm_decoder_function
)(
struct
labcomm_
deco
der
*
d
,
struct
labcomm_
rea
der
*
r
,
labcomm_handler_function
handler
,
labcomm_handler_function
handler
,
void
*
context
);
void
*
context
);
struct
labcomm_decoder
{
void
*
context
;
struct
labcomm_reader
reader
;
struct
{
void
*
context
;
const
struct
labcomm_lock_action
*
action
;
}
lock
;
labcomm_error_handler_callback
on_error
;
labcomm_handle_new_datatype_callback
on_new_datatype
;
};
/*
/*
* Non typesafe registration function to be called from
* Non typesafe registration function to be called from
* generated labcomm_decoder_register_* functions.
* generated labcomm_decoder_register_* functions.
...
@@ -94,9 +83,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
...
@@ -94,9 +83,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \
r->pos++; \
} \
} \
return result; \
return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
}
}
#else
#else
...
@@ -112,9 +98,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
...
@@ -112,9 +98,6 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
r->pos++; \
r->pos++; \
} \
} \
return result; \
return result; \
} \
static inline type labcomm_decode_##name(struct labcomm_decoder *d) { \
return labcomm_read_##name(&d->reader); \
}
}
#endif
#endif
...
@@ -147,11 +130,6 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
...
@@ -147,11 +130,6 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
return
result
;
return
result
;
}
}
static
inline
unsigned
int
labcomm_decode_packed32
(
struct
labcomm_decoder
*
d
)
{
return
labcomm_read_packed32
(
&
d
->
reader
);
}
static
inline
char
*
labcomm_read_string
(
struct
labcomm_reader
*
r
)
static
inline
char
*
labcomm_read_string
(
struct
labcomm_reader
*
r
)
{
{
char
*
result
;
char
*
result
;
...
@@ -170,28 +148,13 @@ static inline char *labcomm_read_string(struct labcomm_reader *r)
...
@@ -170,28 +148,13 @@ static inline char *labcomm_read_string(struct labcomm_reader *r)
return
result
;
return
result
;
}
}
static
inline
char
*
labcomm_decode_string
(
struct
labcomm_decoder
*
d
)
{
return
labcomm_read_string
(
&
d
->
reader
);
}
/*
/*
* Semi private encoder declarations
* Semi private encoder declarations
*/
*/
typedef
int
(
*
labcomm_encoder_function
)(
typedef
int
(
*
labcomm_encoder_function
)(
struct
labcomm_
encod
er
*
,
struct
labcomm_
writ
er
*
,
void
*
value
);
void
*
value
);
struct
labcomm_encoder
{
void
*
context
;
struct
labcomm_writer
writer
;
struct
{
void
*
context
;
const
struct
labcomm_lock_action
*
action
;
}
lock
;
labcomm_error_handler_callback
on_error
;
};
void
labcomm_internal_encoder_register
(
void
labcomm_internal_encoder_register
(
struct
labcomm_encoder
*
encoder
,
struct
labcomm_encoder
*
encoder
,
struct
labcomm_signature
*
signature
,
struct
labcomm_signature
*
signature
,
...
@@ -223,9 +186,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
...
@@ -223,9 +186,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \
w->pos++; \
} \
} \
return 0; \
return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
}
}
#else
#else
...
@@ -243,9 +203,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
...
@@ -243,9 +203,6 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
w->pos++; \
w->pos++; \
} \
} \
return 0; \
return 0; \
} \
static inline int labcomm_encode_##name(struct labcomm_encoder *e, type data) { \
return labcomm_write_##name(&e->writer, data); \
}
}
#endif
#endif
...
@@ -278,14 +235,6 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w,
...
@@ -278,14 +235,6 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w,
return
0
;
return
0
;
}
}
static
inline
int
labcomm_encode_packed32
(
struct
labcomm_encoder
*
e
,
unsigned
int
data
)
{
return
labcomm_write_packed32
(
&
e
->
writer
,
data
);
}
static
inline
int
labcomm_write_string
(
struct
labcomm_writer
*
w
,
char
*
s
)
static
inline
int
labcomm_write_string
(
struct
labcomm_writer
*
w
,
char
*
s
)
{
{
int
length
,
i
,
err
;
int
length
,
i
,
err
;
...
@@ -305,10 +254,4 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
...
@@ -305,10 +254,4 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
return
0
;
return
0
;
}
}
static
inline
int
labcomm_encode_string
(
struct
labcomm_encoder
*
e
,
char
*
s
)
{
return
labcomm_write_string
(
&
e
->
writer
,
s
);
}
#endif
#endif
lib/c/test/test_labcomm_basic_type_encoding.c
View file @
5b7e2b76
...
@@ -6,34 +6,25 @@
...
@@ -6,34 +6,25 @@
static
int
line
;
static
int
line
;
static
unsigned
char
buffer
[
128
];
static
unsigned
char
buffer
[
128
];
static
struct
labcomm_encoder
encoder
=
{
static
struct
labcomm_writer
writer
=
{
.
context
=
NULL
,
.
context
=
NULL
,
.
writer
=
{
.
data
=
buffer
,
.
context
=
NULL
,
.
data_size
=
sizeof
(
buffer
),
.
data
=
buffer
,
.
count
=
sizeof
(
buffer
),
.
data_size
=
sizeof
(
buffer
),
.
pos
=
0
,
.
count
=
sizeof
(
buffer
),
.
error
=
0
,
.
pos
=
0
,
.
action
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
},
.
error
=
0
,
.
action
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
},
.
on_error
=
NULL
,
},
.
lock
=
{
NULL
,
NULL
},
.
on_error
=
NULL
,
.
on_error
=
NULL
,
};
};
static
struct
labcomm_
decoder
deco
der
=
{
static
struct
labcomm_
reader
rea
der
=
{
.
context
=
NULL
,
.
context
=
NULL
,
.
reader
=
{
.
data
=
buffer
,
.
context
=
NULL
,
.
data_size
=
sizeof
(
buffer
),
.
data
=
buffer
,
.
count
=
0
,
.
data_size
=
sizeof
(
buffer
),
.
pos
=
0
,
.
count
=
0
,
.
action
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
},
.
pos
=
0
,
.
action
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
},
.
on_error
=
NULL
,
},
.
lock
=
{
NULL
,
NULL
},
.
on_error
=
NULL
,
.
on_error
=
NULL
,
};
};
...
@@ -43,15 +34,15 @@ typedef uint32_t packed32;
...
@@ -43,15 +34,15 @@ typedef uint32_t packed32;
{ \
{ \
type decoded; \
type decoded; \
line = __LINE__; \
line = __LINE__; \
encoder.
writer.pos = 0; \
writer.pos = 0;
\
labcomm_
encod
e_##ltype(&
encod
er, value); \
labcomm_
writ
e_##ltype(&
writ
er, value); \
writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes);
\
writer_assert(#ltype, expect_count, (uint8_t*)expect_bytes); \
decoder.
reader.count =
encoder.
writer.pos; \
reader.count = writer.pos;
\
decoder.
reader.pos = 0; \
reader.pos = 0;
\
decoded = labcomm_
decode
_##ltype(&
deco
der); \
decoded = labcomm_
read
_##ltype(&
rea
der); \
if (decoded != value) { \
if (decoded != value) { \
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n",
value, decoded,
\
fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", \
__FILE__, __LINE__); \
value, decoded,
__FILE__, __LINE__); \
exit(1); \
exit(1); \
} \
} \
}
}
...
@@ -60,18 +51,18 @@ static void writer_assert(char *type,
...
@@ -60,18 +51,18 @@ static void writer_assert(char *type,
int
count
,
int
count
,
uint8_t
*
bytes
)
uint8_t
*
bytes
)
{
{
if
(
encoder
.
writer
.
pos
!=
count
)
{
if
(
writer
.
pos
!=
count
)
{
fprintf
(
stderr
,
fprintf
(
stderr
,
"Wrong number of bytes written for '%s' (%d != %d) @%s:%d
\n
"
,
"Wrong number of bytes written for '%s' (%d != %d) @%s:%d
\n
"
,
type
,
encoder
.
writer
.
pos
,
count
,
__FILE__
,
line
);
type
,
writer
.
pos
,
count
,
__FILE__
,
line
);
exit
(
1
);
exit
(
1
);
}
}
if
(
memcmp
(
encoder
.
writer
.
data
,
bytes
,
count
)
!=
0
)
{
if
(
memcmp
(
writer
.
data
,
bytes
,
count
)
!=
0
)
{
int
i
;
int
i
;
fprintf
(
stderr
,
"Wrong bytes written for '%s' ( "
,
type
);
fprintf
(
stderr
,
"Wrong bytes written for '%s' ( "
,
type
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
for
(
i
=
0
;
i
<
count
;
i
++
)
{
fprintf
(
stderr
,
"%2.2x "
,
encoder
.
writer
.
data
[
i
]);
fprintf
(
stderr
,
"%2.2x "
,
writer
.
data
[
i
]);
}
}
fprintf
(
stderr
,
"!= "
);
fprintf
(
stderr
,
"!= "
);
for
(
i
=
0
;
i
<
count
;
i
++
)
{
for
(
i
=
0
;
i
<
count
;
i
++
)
{
...
...
lib/c/test/test_labcomm_generated_encoding.c
View file @
5b7e2b76
...
@@ -9,9 +9,11 @@
...
@@ -9,9 +9,11 @@
#define IOCTL_WRITER_RESET 4097
#define IOCTL_WRITER_RESET 4097
static
unsigned
char
buffer
[
128
];
static
unsigned
char
buffer
[
128
];
struct
labcomm_writer
*
writer
;
static
int
buf_writer_alloc
(
struct
labcomm_writer
*
w
,
char
*
labcomm_version
)
static
int
buf_writer_alloc
(
struct
labcomm_writer
*
w
,
char
*
labcomm_version
)
{
{
writer
=
w
;
/* Hack */
w
->
data_size
=
sizeof
(
buffer
);
w
->
data_size
=
sizeof
(
buffer
);
w
->
count
=
w
->
data_size
;
w
->
count
=
w
->
data_size
;
w
->
data
=
buffer
;
w
->
data
=
buffer
;
...
@@ -112,8 +114,8 @@ void dump_encoder(struct labcomm_encoder *encoder)
...
@@ -112,8 +114,8 @@ void dump_encoder(struct labcomm_encoder *encoder)
{
{
int
i
;
int
i
;
for
(
i
=
0
;
i
<
encoder
->
writer
.
pos
;
i
++
)
{
for
(
i
=
0
;
i
<
writer
->
pos
;
i
++
)
{
printf
(
"%2.2x "
,
encoder
->
writer
.
data
[
i
]);
printf
(
"%2.2x "
,
writer
->
data
[
i
]);
}
}
printf
(
"
\n
"
);
printf
(
"
\n
"
);
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment