diff --git a/compiler/2014/C_CodeGen.jrag b/compiler/2014/C_CodeGen.jrag index fe64ca1d68bb625b9ea95da29f407b8e4cdbe3a5..f63268c041798d0f6b791e43a39321b501a55169 100644 --- a/compiler/2014/C_CodeGen.jrag +++ b/compiler/2014/C_CodeGen.jrag @@ -221,6 +221,7 @@ aspect C_CodeGen { getDecl(i).C_emitDecoderDeclaration(env); getDecl(i).C_emitEncoderDeclaration(env); getDecl(i).C_emitSizeofDeclaration(env); + getDecl(i).C_emitCopyStaticDeclaration(env); getDecl(i).C_emitCopyDeclaration(env); getDecl(i).C_emitCopyDeallocationDeclaration(env); env.println(""); @@ -239,6 +240,7 @@ aspect C_CodeGen { getDecl(i).C_emitEncoderRegisterHandler(env); getDecl(i).C_emitEncoderIoctl(env); getDecl(i).C_emitSizeof(env); + getDecl(i).C_emitCopyStatic(env); getDecl(i).C_emitCopy(env); getDecl(i).C_emitCopyDeallocation(env); } @@ -957,6 +959,139 @@ aspect C_copy { } } +aspect C_copy_static { + + private void SampleDecl.C_emitCopyStaticFunctionParam(C_env env, String src, + String dst) + { + env.println("void labcomm2014_copy_static_" + + env.prefix + getName() + "("); + env.indent(); + env.println(env.prefix + getName() + " *" + dst + ","); + env.println(env.prefix + getName() + " *" + src); + env.unindent(); + env.print(")"); + } + + public void Decl.C_emitCopyStaticDeclaration(C_env env) { + } + + public void SampleDecl.C_emitCopyStaticDeclaration(C_env env) { + C_emitCopyStaticFunctionParam(env, "src", "dst"); + env.println(";"); + } + + public void Decl.C_emitCopyStatic(C_env env) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void TypeDecl.C_emitCopyStatic(C_env env) { + } + + public void SampleDecl.C_emitCopyStatic(C_env env) { + final String dst = "dst"; + final String src = "src"; + C_env env_src = env.nestStruct(src).setPointer(); + C_env env_dst = env.nestStruct(dst).setPointer(); + + C_emitCopyStaticFunctionParam(env_src, src, dst); + env_src.println(""); + env_src.println("{"); + env_src.indent(); + getDataType().C_emitCopyStatic(env_src, env_dst); + env_src.unindent(); + env_src.println("}"); + } + + public void DataType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void VoidType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + } + + public void PrimType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + if (C_isDynamic()) { + env_src.println(String.format( + "memcpy(%s%s, %s%s, strlen(%s%s)+1);", + env_dst.accessor(), env_dst.qualid, + env_src.accessor(), env_src.qualid, + env_src.accessor(), env_src.qualid)); + } else { + env_src.println(env_dst.accessor() + env_dst.qualid + " = " + + env_src.accessor() + env_src.qualid + ";"); + } + } + + public void UserType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + lookupType(getName()).getDataType().C_emitCopyStatic(env_src, env_dst); + } + + public void StructType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumField() ; i++) { + getField(i).C_emitCopyStatic(env_src, env_dst); + } + } + + public void ArrayType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + C_emitCopyStaticDecodeLimit(env_src, env_dst); + C_emitCopyStaticArrayAllocate(env_src, env_dst); + env_src.println("{"); + env_src.indent(); + C_emitLoopVariables(env_src); + for (int i = 0 ; i < getNumExp() ; i++) { + String iterator = "i_" + env_src.depth + "_" + i; + env_src.println("for (" + iterator + " = 0" + + " ; " + + iterator + " < " + getExp(i).C_getLimit(env_src, i) + + " ; " + + iterator + "++) {"); + env_src.indent(); + } + C_emitCalcIndex(env_src); + getDataType().C_emitCopyStatic(C_Nest(env_src), C_Nest(env_dst)); + for (int i = getNumExp() - 1 ; i >= 0 ; i--) { + env_src.unindent(); + env_src.println("}"); + } + env_src.unindent(); + env_src.println("}"); + } + + public void Field.C_emitCopyStatic(C_env env_src, C_env env_dst) { + String fnam = env_src.memberAccessor() + getName(); + getDataType().C_emitCopyStatic(env_src.nestStruct(fnam), env_dst.nestStruct(fnam)); + } + + public void Exp.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + // Ordinary array has no length-member. + } + + public void VariableSize.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + String src = env_src.qualid + env_src.memberAccessor() + "n_" + i; + String dst = env_dst.qualid + env_dst.memberAccessor() + "n_" + i; + env_src.println(dst + " = " + src + ";"); + } + + public void ArrayType.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumExp() ; i++) { + getExp(i).C_emitCopyStaticDecodeLimit(env_src, env_dst, i); + } + } + + public void ArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, C_env env_dst) { + } + + public void VariableArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, + C_env env_dst) + { + } +} + aspect C_DecoderIoctl { public void Decl.C_emitDecoderIoctl(C_env env) { diff --git a/lib/c/2014/Makefile b/lib/c/2014/Makefile index 0b0d0ee3cfd33b38962cf5e231efdd572e31c8a4..74242c8c50abea4a17c3bfd8fd89579e0f50dd63 100644 --- a/lib/c/2014/Makefile +++ b/lib/c/2014/Makefile @@ -102,7 +102,7 @@ endif # compilation rules %.pic.o: %.c - $(CC) -fPIC $(CFLAGS) -c -o $@ $< + $(CC) $(FPIC) $(CFLAGS) -c -o $@ $< %.o: %.c %.h $(CC) $(CFLAGS) -c -o $@ $< @@ -110,7 +110,7 @@ endif # rules invoked by 'test' .PHONY: run-test-% run-test-%: $(TEST_DIR)/gen/% | $(TEST_DIR)/gen - $(VALGRIND) $< + LD_LIBRARY_PATH=.. $(VALGRIND) $< .PRECIOUS: $(TEST_DIR)/gen/% diff --git a/lib/c/2014/labcomm2014.c b/lib/c/2014/labcomm2014.c index f5ac3194a9cb15538a549407c1ddf8ec8a8431a4..27287d8464d17792dddeb6295119673c51ccdcec 100644 --- a/lib/c/2014/labcomm2014.c +++ b/lib/c/2014/labcomm2014.c @@ -43,26 +43,28 @@ void *labcomm2014_void_instance = &labcomm2014_void_instance; -/* Unwrapping reader/writer functions */ -#define UNWRAP_ac(rw, ac, ...) ac -#define UNWRAP(func, ...) \ - while (1) { \ - if (UNWRAP_ac(__VA_ARGS__)->action->func) { \ - return UNWRAP_ac(__VA_ARGS__)->action->func(__VA_ARGS__); } \ - if (UNWRAP_ac(__VA_ARGS__)->next == NULL) { return -ENOSYS; } \ - UNWRAP_ac( __VA_ARGS__) = UNWRAP_ac(__VA_ARGS__)->next; \ - } - int labcomm2014_reader_alloc(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(alloc, r, action_context); + while (1) { + if (action_context->action->alloc) + return action_context->action->alloc(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_free(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(free, r, action_context); + while (1) { + if (action_context->action->free) + return action_context->action->free(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_start(struct labcomm2014_reader *r, @@ -71,19 +73,37 @@ int labcomm2014_reader_start(struct labcomm2014_reader *r, const struct labcomm2014_signature *signature, void *value) { - UNWRAP(start, r, action_context, local_index, remote_index, signature, value); + while (1) { + if (action_context->action->start) + return action_context->action->start(r, action_context, local_index, remote_index, signature, value); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_end(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(end, r, action_context); + while (1) { + if (action_context->action->end) + return action_context->action->end(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_fill(struct labcomm2014_reader *r, struct labcomm2014_reader_action_context *action_context) { - UNWRAP(fill, r, action_context); + while (1) { + if (action_context->action->fill) + return action_context->action->fill(r, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_reader_ioctl(struct labcomm2014_reader *r, @@ -92,20 +112,37 @@ int labcomm2014_reader_ioctl(struct labcomm2014_reader *r, const struct labcomm2014_signature *signature, uint32_t ioctl_action, va_list args) { - UNWRAP(ioctl, r, action_context, - local_index, remote_index, signature, ioctl_action, args); + while (1) { + if (action_context->action->ioctl) + return action_context->action->ioctl(r, action_context, local_index, remote_index, signature, ioctl_action, args); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_alloc(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(alloc, w, action_context); + while (1) { + if (action_context->action->alloc) + return action_context->action->alloc(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_free(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(free, w, action_context); + while (1) { + if (action_context->action->free) + return action_context->action->free(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_start(struct labcomm2014_writer *w, @@ -113,19 +150,37 @@ int labcomm2014_writer_start(struct labcomm2014_writer *w, int index, const struct labcomm2014_signature *signature, void *value) { - UNWRAP(start, w, action_context, index, signature, value); + while (1) { + if (action_context->action->start) + return action_context->action->start(w, action_context, index, signature, value); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_end(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(end, w, action_context); + while (1) { + if (action_context->action->end) + return action_context->action->end(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_flush(struct labcomm2014_writer *w, struct labcomm2014_writer_action_context *action_context) { - UNWRAP(flush, w, action_context); + while (1) { + if (action_context->action->flush) + return action_context->action->flush(w, action_context); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } int labcomm2014_writer_ioctl(struct labcomm2014_writer *w, @@ -134,15 +189,15 @@ int labcomm2014_writer_ioctl(struct labcomm2014_writer *w, const struct labcomm2014_signature *signature, uint32_t ioctl_action, va_list args) { - UNWRAP(ioctl, w, action_context, index, signature, ioctl_action, args); + while (1) { + if (action_context->action->ioctl) + return action_context->action->ioctl(w, action_context, index, signature, ioctl_action, args); + if (action_context->next == NULL) + return -ENOSYS; + action_context = action_context->next; + } } -#undef UNWRAP -#undef UNWRAP_ac - - - - static const char *labcomm2014_error_string[] = { #define LABCOMM2014_ERROR(name, description) description , #include "labcomm2014_error.h" @@ -227,7 +282,7 @@ void *labcomm2014_signature_array_ref(struct labcomm2014_memory *memory, *data = labcomm2014_memory_alloc(memory, 0, n * size); if (*data) { memset(*data, 0, n * size); - memcpy(*data + (old_first - *first) * size, + memcpy((char *) *data + (old_first - *first) * size, old_data, (old_last - old_first) * size); } @@ -236,7 +291,7 @@ void *labcomm2014_signature_array_ref(struct labcomm2014_memory *memory, } if (*data) { // dump(*data, size, *first, *last); - return *data + (index - *first) * size; + return (char *) *data + (index - *first) * size; } else { return NULL; } diff --git a/lib/c/2014/labcomm2014.h b/lib/c/2014/labcomm2014.h index 96599c5c8f940493d0e491473f9d15df007ec28c..7a8cbeb3ca0da48edf450fad714d241422c12be5 100644 --- a/lib/c/2014/labcomm2014.h +++ b/lib/c/2014/labcomm2014.h @@ -30,8 +30,8 @@ #else #include #include - #include #endif +#include #include "labcomm2014_error.h" #include "labcomm2014_scheduler.h" diff --git a/lib/c/2014/labcomm2014_compat_tc31.h b/lib/c/2014/labcomm2014_compat_tc31.h new file mode 100644 index 0000000000000000000000000000000000000000..4f6f97574335cda96598e09d7cbdd33a26af4d14 --- /dev/null +++ b/lib/c/2014/labcomm2014_compat_tc31.h @@ -0,0 +1,24 @@ +#ifndef LABCOMM_COMPAT_TC31 +#define LABCOMM_COMPAT_TC31 + +#include + +#define LABCOMM_CONSTRUCTOR + +#define ECONNRESET 101 +#define ENOTSUP 102 +#define EALREADY 103 +#define ENODATA 104 + +#define inline __inline + +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; + +#endif diff --git a/lib/c/2014/labcomm2014_decoder.c b/lib/c/2014/labcomm2014_decoder.c index 7f16a79537d1caf5811859c437c6185716c273ca..4b078e4dddaeff33df9f796191d247298f41fc41 100644 --- a/lib/c/2014/labcomm2014_decoder.c +++ b/lib/c/2014/labcomm2014_decoder.c @@ -26,7 +26,7 @@ #include "labcomm2014_ioctl.h" #include "labcomm2014_dynamic_buffer_writer.h" -#ifdef DEBUG +#if defined(DEBUG) && !defined(LABCOMM2014_NO_STDIO) #define DEBUG_FPRINTF(str, ...) fprintf(str, ##__VA_ARGS__) #else #define DEBUG_FPRINTF(str, ...) @@ -158,6 +158,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind) goto out; } signature.name = labcomm2014_read_string(d->reader); + if (signature.name == NULL) { + result = -ENOMEM; + goto out; + } if (d->reader->error < 0) { result = d->reader->error; goto free_signature_name; @@ -168,6 +172,10 @@ static int decode_sample_def_or_ref(struct labcomm2014_decoder *d, int kind) goto free_signature_name; } signature.signature = labcomm2014_memory_alloc(d->memory, 1, signature.size); + if (signature.signature == NULL) { + result = -ENOMEM; + goto free_signature_name; + } if (d->reader->error < 0) { result = d->reader->error; goto free_signature_name; @@ -253,6 +261,10 @@ static int decode_pragma(struct labcomm2014_decoder *d, char *pragma_type; int result; pragma_type = labcomm2014_read_string(d->reader); + if (pragma_type == NULL) { + result = -ENOMEM; + goto out; + } if (d->reader->error < 0) { result = d->reader->error; goto out; @@ -346,7 +358,9 @@ static int do_decode_one(struct labcomm2014_decoder *d) } if (remote_index == LABCOMM2014_VERSION) { char *version = labcomm2014_read_string(d->reader); - if (d->reader->error < 0) { + if (version == NULL) { + result = -ENOMEM; + } else if (d->reader->error < 0) { result = d->reader->error; } else if (strcmp(version, CURRENT_VERSION) == 0) { result = LABCOMM2014_VERSION; @@ -488,6 +502,7 @@ static void decode_raw_type_def( v.index = labcomm2014_read_packed32(r); if (r->error < 0) { goto out; } v.name = labcomm2014_read_string(r); + if (v.name == NULL) { goto out; } if (r->error < 0) { goto free_name; } v.length = labcomm2014_read_packed32(r); if (r->error < 0) { goto free_name; } @@ -716,8 +731,9 @@ struct labcomm2014_decoder *labcomm2014_decoder_new( LABCOMM2014_SIGNATURE_ARRAY_INIT(result->local_ref, const struct labcomm2014_signature*); LABCOMM2014_SIGNATURE_ARRAY_INIT(result->remote_to_local_ref, int); + return &(result->decoder); } - return &(result->decoder); + return NULL; } diff --git a/lib/c/2014/labcomm2014_encoder.c b/lib/c/2014/labcomm2014_encoder.c index 59b5a7b1cb27c55660027131c6647068873a3c82..85b6b5ac1a79db1a61a07715fbb07ded89cf0e6a 100644 --- a/lib/c/2014/labcomm2014_encoder.c +++ b/lib/c/2014/labcomm2014_encoder.c @@ -420,8 +420,9 @@ static struct labcomm2014_encoder *internal_encoder_new( labcomm2014_writer_end(result->encoder.writer, result->encoder.writer->action_context); } + return &(result->encoder); } - return &(result->encoder); + return NULL; } struct labcomm2014_encoder *labcomm2014_encoder_new( diff --git a/lib/c/2014/labcomm2014_error.c b/lib/c/2014/labcomm2014_error.c index a002026d6df70a558dd2125683a14e1182a8b8ec..794096ac9133e94b7ba59997ac250618772fa976 100644 --- a/lib/c/2014/labcomm2014_error.c +++ b/lib/c/2014/labcomm2014_error.c @@ -36,12 +36,16 @@ void labcomm2014_error_fatal_global(enum labcomm2014_error error, { va_list args; +#ifndef LABCOMM2014_NO_STDIO fprintf(stderr, "Fatal error %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); va_end(args); +#endif +#ifdef LABCOMM_EXIT exit(1); +#endif } void labcomm2014_error_warning(struct labcomm2014_error_handler *e, @@ -51,11 +55,15 @@ void labcomm2014_error_warning(struct labcomm2014_error_handler *e, { va_list args; +#ifndef LABCOMM2014_NO_STDIO fprintf(stderr, "Fatal warning %d (%s)\n", error, description[error]); va_start(args, format); vfprintf(stderr, format, args); va_end(args); +#endif +#ifdef LABCOMM_EXIT exit(1); +#endif } diff --git a/lib/c/2014/labcomm2014_renaming.c b/lib/c/2014/labcomm2014_renaming.c index 332c63843c7ec2cb8841ed1fe76fe97207332e5d..fbdb851fde3814447ae3936a5f9cdf5c17ef51e0 100644 --- a/lib/c/2014/labcomm2014_renaming.c +++ b/lib/c/2014/labcomm2014_renaming.c @@ -29,7 +29,7 @@ char *labcomm2014_renaming_prefix(struct labcomm2014_memory *m, char *result, *prefix = context; int length; - length = strlen(name) + strlen(prefix) + 1; + length = (int) (strlen(name) + strlen(prefix) + 1); result = labcomm2014_memory_alloc(m, 0, length); if (result != NULL) { strcpy(result, prefix); diff --git a/lib/c/2014/labcomm2014_scheduler.c b/lib/c/2014/labcomm2014_scheduler.c index 40288207149d93efc11c5084433b04d374b26a0c..465398b118b7e6601b02a5791ea3ff8eb49aca79 100644 --- a/lib/c/2014/labcomm2014_scheduler.c +++ b/lib/c/2014/labcomm2014_scheduler.c @@ -22,56 +22,62 @@ #include #include "labcomm2014_scheduler_private.h" -#define SCHEDULER_scheduler(scheduler, ...) scheduler -#define SCHEDULER(func, ...) \ - if (SCHEDULER_scheduler(__VA_ARGS__) && \ - SCHEDULER_scheduler(__VA_ARGS__)->action->func) { \ - return SCHEDULER_scheduler(__VA_ARGS__)->action->func(__VA_ARGS__); \ - } \ - return -ENOSYS; - int labcomm2014_scheduler_free(struct labcomm2014_scheduler *s) { - SCHEDULER(free, s); + if (s && s->action->free) + return s->action->free(s); + return -ENOSYS; } int labcomm2014_scheduler_writer_lock(struct labcomm2014_scheduler *s) { - SCHEDULER(writer_lock, s); + if (s && s->action->writer_lock) + return s->action->writer_lock(s); + return -ENOSYS; } int labcomm2014_scheduler_writer_unlock(struct labcomm2014_scheduler *s) { - SCHEDULER(writer_unlock, s); + if (s && s->action->writer_unlock) + return s->action->writer_unlock(s); + return -ENOSYS; } int labcomm2014_scheduler_data_lock(struct labcomm2014_scheduler *s) { - SCHEDULER(data_lock, s); + if (s && s->action->data_lock) + return s->action->data_lock(s); + return -ENOSYS; } int labcomm2014_scheduler_data_unlock(struct labcomm2014_scheduler *s) { - SCHEDULER(data_unlock, s); + if (s && s->action->data_unlock) + return s->action->data_unlock(s); + return -ENOSYS; } struct labcomm2014_time *labcomm2014_scheduler_now(struct labcomm2014_scheduler *s) { if (s && s->action->now) { - return s->action->now(s); - } - return NULL; + return s->action->now(s); + } + return 0; } int labcomm2014_scheduler_sleep(struct labcomm2014_scheduler *s, struct labcomm2014_time *wakeup) { - SCHEDULER(sleep, s, wakeup); + if (s && s->action->sleep) + return s->action->sleep(s, wakeup); + return -ENOSYS; } int labcomm2014_scheduler_wakeup(struct labcomm2014_scheduler *s) { - SCHEDULER(wakeup, s); + if (s && s->action->wakeup) + return s->action->wakeup(s); + return -ENOSYS; } int labcomm2014_scheduler_enqueue(struct labcomm2014_scheduler *s, @@ -79,7 +85,7 @@ int labcomm2014_scheduler_enqueue(struct labcomm2014_scheduler *s, void (*func)(void *context), void *context) { - SCHEDULER(enqueue, s, delay, func, context); + if (s && s->action->enqueue) + return s->action->enqueue(s, delay, func, context); + return -ENOSYS; } - - diff --git a/lib/c/os_compat.mk b/lib/c/os_compat.mk index ed947b5cf9af57c3252d24b13a954f82cc583fe0..1e41ad13f494c3992b68bf5db8edfb1d33b13a54 100644 --- a/lib/c/os_compat.mk +++ b/lib/c/os_compat.mk @@ -2,6 +2,7 @@ UNAME_S=$(shell uname -s) VERSION=2014 LIBVERSION=2014 +FPIC=-fPIC CC=$(CROSS_COMPILE)gcc LD=$(CROSS_COMPILE)gcc @@ -27,4 +28,3 @@ else ifneq ($(findstring CYGWIN,$(UNAME_S)),) else $(error Unknown system $(UNAME_S)) endif -