on master help sign in
C style changes while porting rts to 9front (#3596)

* Avoid pointer arithmetic on void pointers The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1. http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html Below is an excerpt from http://stackoverflow.com/a/3855633 Per the online C standard (n1256 draft): 6.2.5 Types ... 27 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. * Using void for functions without arguments Using void foo(void) instead of void foo() for functions without arguments, as per http://stackoverflow.com/a/693794 * Avoid pointer arithmetic on void pointers in the C generated code

commit36aaf7f6f957454b3c5e1fe1c3afa6882c04f004 authorjoe9 <joe9@users.noreply.github.com> dateTue 3 Jan 2017, 08:19 (10 years ago) parent1a4c80a
6 files changed+40−40
rts/idris_bitstring.c+8−8 rts/idris_gmp.c+1−1 rts/idris_gmp.h+1−1 rts/idris_rts.c+19−19 rts/idris_rts.h+10−10 src/IRTS/CodegenC.hs+1−1
rts/idris_bitstring.c+8−8
@@ -823,37 +823,37 @@ VAL idris_b64T32(VM *vm, VAL a) {
823823}
824824
825825VAL idris_peekB8(VM* vm, VAL ptr, VAL offset) {
826− return MKB8(vm, *(uint8_t*)(GETPTR(ptr) + GETINT(offset)));
826+ return MKB8(vm, *(uint8_t*)((char *)GETPTR(ptr) + GETINT(offset)));
827827}
828828
829829VAL idris_pokeB8(VAL ptr, VAL offset, VAL data) {
830− *(uint8_t*)(GETPTR(ptr) + GETINT(offset)) = GETBITS8(data);
830+ *(uint8_t*)((char *)GETPTR(ptr) + GETINT(offset)) = GETBITS8(data);
831831 return MKINT(0);
832832}
833833
834834VAL idris_peekB16(VM* vm, VAL ptr, VAL offset) {
835− return MKB16(vm, *(uint16_t*)(GETPTR(ptr) + GETINT(offset)));
835+ return MKB16(vm, *(uint16_t*)((char *)GETPTR(ptr) + GETINT(offset)));
836836}
837837
838838VAL idris_pokeB16(VAL ptr, VAL offset, VAL data) {
839− *(uint16_t*)(GETPTR(ptr) + GETINT(offset)) = GETBITS16(data);
839+ *(uint16_t*)((char *)GETPTR(ptr) + GETINT(offset)) = GETBITS16(data);
840840 return MKINT(0);
841841}
842842
843843VAL idris_peekB32(VM* vm, VAL ptr, VAL offset) {
844− return MKB32(vm, *(uint32_t*)(GETPTR(ptr) + GETINT(offset)));
844+ return MKB32(vm, *(uint32_t*)((char *)GETPTR(ptr) + GETINT(offset)));
845845}
846846
847847VAL idris_pokeB32(VAL ptr, VAL offset, VAL data) {
848− *(uint32_t*)(GETPTR(ptr) + GETINT(offset)) = GETBITS32(data);
848+ *(uint32_t*)((char *)GETPTR(ptr) + GETINT(offset)) = GETBITS32(data);
849849 return MKINT(0);
850850}
851851
852852VAL idris_peekB64(VM* vm, VAL ptr, VAL offset) {
853− return MKB64(vm, *(uint64_t*)(GETPTR(ptr) + GETINT(offset)));
853+ return MKB64(vm, *(uint64_t*)((char *)GETPTR(ptr) + GETINT(offset)));
854854}
855855
856856VAL idris_pokeB64(VAL ptr, VAL offset, VAL data) {
857− *(uint64_t*)(GETPTR(ptr) + GETINT(offset)) = GETBITS64(data);
857+ *(uint64_t*)((char *)GETPTR(ptr) + GETINT(offset)) = GETBITS64(data);
858858 return MKINT(0);
859859}
rts/idris_gmp.c+1−1
@@ -14,7 +14,7 @@
1414// much space is needed (or find another way of preventing copying)
1515#define IDRIS_MAXGMP 65536
1616
17−void init_gmpalloc() {
17+void init_gmpalloc(void) {
1818 mp_set_memory_functions(idris_alloc, idris_realloc, idris_free);
1919}
2020
rts/idris_gmp.h+1−1
@@ -8,7 +8,7 @@
88#endif
99
1010// Set memory allocation functions
11−void init_gmpalloc();
11+void init_gmpalloc(void);
1212
1313VAL MKBIGI(int val);
1414VAL MKBIGC(VM* vm, char* bigint);
rts/idris_rts.c+19−19
@@ -70,7 +70,7 @@ VM* init_vm(int stack_size, size_t heap_size,
7070 return vm;
7171}
7272
73−VM* idris_vm() {
73+VM* idris_vm(void) {
7474 VM* vm = init_vm(4096000, 4096000, 1);
7575 init_threadkeys();
7676 init_threaddata(vm);
@@ -95,12 +95,12 @@ void close_vm(VM* vm) {
9595}
9696
9797#ifdef HAS_PTHREAD
98−void create_key() {
98+void create_key(void) {
9999 pthread_key_create(&vm_key, (void*)free_key);
100100}
101101#endif
102102
103−void init_threadkeys() {
103+void init_threadkeys(void) {
104104#ifdef HAS_PTHREAD
105105 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
106106 pthread_once(&key_once, create_key);
@@ -113,7 +113,7 @@ void init_threaddata(VM *vm) {
113113#endif
114114}
115115
116−void init_signals() {
116+void init_signals(void) {
117117#if (__linux__ || __APPLE__ || __FreeBSD__ || __DragonFly__)
118118 signal(SIGPIPE, SIG_IGN);
119119#endif
@@ -173,7 +173,7 @@ void idris_requireAlloc(size_t size) {
173173#endif
174174}
175175
176−void idris_doneAlloc() {
176+void idris_doneAlloc(void) {
177177#ifdef HAS_PTHREAD
178178 VM* vm = pthread_getspecific(vm_key);
179179 int lock = vm->processes > 0;
@@ -191,7 +191,7 @@ void* idris_alloc(size_t size) {
191191 Closure* cl = (Closure*) allocate(sizeof(Closure)+size, 0);
192192 SETTY(cl, CT_RAWDATA);
193193 cl->info.size = size;
194− return (void*)cl+sizeof(Closure);
194+ return (void*)((char *)cl+sizeof(Closure));
195195}
196196
197197void* idris_realloc(void* old, size_t old_size, size_t size) {
@@ -449,36 +449,36 @@ void idris_poke(void* ptr, i_int offset, uint8_t data) {
449449
450450
451451VAL idris_peekPtr(VM* vm, VAL ptr, VAL offset) {
452− void** addr = GETPTR(ptr) + GETINT(offset);
452+ void** addr = (void **)((char *)GETPTR(ptr) + GETINT(offset));
453453 return MKPTR(vm, *addr);
454454}
455455
456456VAL idris_pokePtr(VAL ptr, VAL offset, VAL data) {
457− void** addr = GETPTR(ptr) + GETINT(offset);
457+ void** addr = (void **)((char *)GETPTR(ptr) + GETINT(offset));
458458 *addr = GETPTR(data);
459459 return MKINT(0);
460460}
461461
462462VAL idris_peekDouble(VM* vm, VAL ptr, VAL offset) {
463− return MKFLOAT(vm, *(double*)(GETPTR(ptr) + GETINT(offset)));
463+ return MKFLOAT(vm, *(double*)((char *)GETPTR(ptr) + GETINT(offset)));
464464}
465465
466466VAL idris_pokeDouble(VAL ptr, VAL offset, VAL data) {
467− *(double*)(GETPTR(ptr) + GETINT(offset)) = GETFLOAT(data);
467+ *(double*)((char *)GETPTR(ptr) + GETINT(offset)) = GETFLOAT(data);
468468 return MKINT(0);
469469}
470470
471471VAL idris_peekSingle(VM* vm, VAL ptr, VAL offset) {
472− return MKFLOAT(vm, *(float*)(GETPTR(ptr) + GETINT(offset)));
472+ return MKFLOAT(vm, *(float*)((char *)GETPTR(ptr) + GETINT(offset)));
473473}
474474
475475VAL idris_pokeSingle(VAL ptr, VAL offset, VAL data) {
476− *(float*)(GETPTR(ptr) + GETINT(offset)) = GETFLOAT(data);
476+ *(float*)((char *)GETPTR(ptr) + GETINT(offset)) = GETFLOAT(data);
477477 return MKINT(0);
478478}
479479
480480void idris_memmove(void* dest, void* src, i_int dest_offset, i_int src_offset, i_int size) {
481− memmove(dest + dest_offset, src + src_offset, size);
481+ memmove((char *)dest + dest_offset, (char *)src + src_offset, size);
482482}
483483
484484VAL idris_castIntStr(VM* vm, VAL i) {
@@ -1046,7 +1046,7 @@ void idris_freeMsg(Msg* msg) {
10461046 free(msg);
10471047}
10481048
1049−int idris_errno() {
1049+int idris_errno(void) {
10501050 return errno;
10511051}
10521052
@@ -1056,7 +1056,7 @@ char* idris_showerror(int err) {
10561056
10571057VAL* nullary_cons;
10581058
1059−void init_nullaries() {
1059+void init_nullaries(void) {
10601060 int i;
10611061 VAL cl;
10621062 nullary_cons = malloc(256 * sizeof(VAL));
@@ -1068,7 +1068,7 @@ void init_nullaries() {
10681068 }
10691069}
10701070
1071−void free_nullaries() {
1071+void free_nullaries(void) {
10721072 int i;
10731073 for(i = 0; i < 256; ++i) {
10741074 free(nullary_cons[i]);
@@ -1079,7 +1079,7 @@ void free_nullaries() {
10791079int __idris_argc;
10801080char **__idris_argv;
10811081
1082−int idris_numArgs() {
1082+int idris_numArgs(void) {
10831083 return __idris_argc;
10841084}
10851085
@@ -1087,12 +1087,12 @@ const char* idris_getArg(int i) {
10871087 return __idris_argv[i];
10881088}
10891089
1090−void idris_disableBuffering() {
1090+void idris_disableBuffering(void) {
10911091 setvbuf(stdin, NULL, _IONBF, 0);
10921092 setvbuf(stdout, NULL, _IONBF, 0);
10931093}
10941094
1095−void stackOverflow() {
1095+void stackOverflow(void) {
10961096 fprintf(stderr, "Stack overflow");
10971097 exit(-1);
10981098}
rts/idris_rts.h+10−10
@@ -161,11 +161,11 @@ Stats terminate(VM* vm);
161161
162162// Create a new VM, set up everything with sensible defaults (use when
163163// calling Idris from C)
164−VM* idris_vm();
164+VM* idris_vm(void);
165165void close_vm(VM* vm);
166166
167167// Set up key for thread-local data - called once from idris_main
168−void init_threadkeys();
168+void init_threadkeys(void);
169169
170170// Functions all take a pointer to their VM, and previous stack base,
171171// and return nothing.
@@ -278,7 +278,7 @@ void* allocate(size_t size, int outerlock);
278278// may take a lock if other threads are running).
279279
280280void idris_requireAlloc(size_t size);
281−void idris_doneAlloc();
281+void idris_doneAlloc(void);
282282
283283// public interface to allocation (note that this may move other pointers
284284// if allocating beyond the limits given by idris_requireAlloc!)
@@ -299,14 +299,14 @@ void idris_free(void* ptr, size_t size);
299299
300300#define NULL_CON(x) nullary_cons[x]
301301
302−int idris_errno();
302+int idris_errno(void);
303303char* idris_showerror(int err);
304304
305305extern VAL* nullary_cons;
306−void init_nullaries();
307−void free_nullaries();
306+void init_nullaries(void);
307+void free_nullaries(void);
308308
309−void init_signals();
309+void init_signals(void);
310310
311311void* vmThread(VM* callvm, func f, VAL arg);
312312void* idris_stopThread(VM* vm);
@@ -394,16 +394,16 @@ VAL idris_systemInfo(VM* vm, VAL index);
394394extern int __idris_argc;
395395extern char **__idris_argv;
396396
397−int idris_numArgs();
397+int idris_numArgs(void);
398398const char *idris_getArg(int i);
399399
400400// disable stdin/stdout buffering
401−void idris_disableBuffering();
401+void idris_disableBuffering(void);
402402
403403// Handle stack overflow.
404404// Just reports an error and exits.
405405
406−void stackOverflow();
406+void stackOverflow(void);
407407
408408// I think these names are nicer for an API...
409409
src/IRTS/CodegenC.hs+1−1
@@ -674,7 +674,7 @@ doOp v (LExternal pk) [] | pk == sUN "prim__sizeofPtr"
674674doOp v (LExternal mpt) [p] | mpt == sUN "prim__asPtr"
675675 = v ++ "MKPTR(vm, GETMPTR("++ creg p ++"))"
676676doOp v (LExternal offs) [p, n] | offs == sUN "prim__ptrOffset"
677− = v ++ "MKPTR(vm, GETPTR(" ++ creg p ++ ") + GETINT(" ++ creg n ++ "))"
677+ = v ++ "MKPTR(vm, (void *)((char *)GETPTR(" ++ creg p ++ ") + GETINT(" ++ creg n ++ ")))"
678678doOp _ op args = error $ "doOp not implemented (" ++ show (op, args) ++ ")"
679679
680680