[Assorted-commits] SF.net SVN: assorted:[1138] sandbox/trunk/src/c
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-01-24 10:15:06
|
Revision: 1138 http://assorted.svn.sourceforge.net/assorted/?rev=1138&view=rev Author: yangzhang Date: 2009-01-24 10:15:03 +0000 (Sat, 24 Jan 2009) Log Message: ----------- added more c sandbox items Modified Paths: -------------- sandbox/trunk/src/c/switches.c Added Paths: ----------- sandbox/trunk/src/c/arrayinit.c sandbox/trunk/src/c/coercion.mk sandbox/trunk/src/c/overflow/ sandbox/trunk/src/c/rand.c sandbox/trunk/src/c/switchsyntax.c Added: sandbox/trunk/src/c/arrayinit.c =================================================================== --- sandbox/trunk/src/c/arrayinit.c (rev 0) +++ sandbox/trunk/src/c/arrayinit.c 2009-01-24 10:15:03 UTC (rev 1138) @@ -0,0 +1,141 @@ +#include <stdio.h> + +// Array initialization syntax. + +int main() { + // Can be any order. + int xs[] = { [0] 0, [4] -4, [1] -1, [2] -2 }; + printf("%d\n", xs[3]); // Garbage. + printf("%d\n", xs[4]); // As expected. + return 0; +} + +#if 0 +/* + * Warning -- this file must be compiled with GCC + * to show the goto dispatching. In particular, + * the compiler must be compatible with gnu99 + * (i.e. -std=gnu99). + */ + +/* + * Reminder -- disassemble with: + * objdump 4-jumptable -d > jumptable.asm + * Consider the generated asm. + */ + +//enum simplifies unique numbers for opcodes +enum { + OP_ADD = 0, + OP_SUB, + OP_MUL, + OP_DIV, + OP_MAX +}; + +int add(int x, int y); +int sub(int x, int y); +int mul(int x, int y); +int div(int x, int y); +int goto_dispatch(int op, int x, int y); + +int +main(int argc, char **argv) +{ + void print_sw(int op, int r, int s) { + printf("SWCH: op %d (%d, %d) = %d\n", op, r, s, switch_dispatch(op, r, s)); + } + print_sw(OP_ADD, 4, 5); + print_sw(OP_DIV, 8, 2); + switch_dispatch(7, 0, 0); + switch_dispatch(10, 0, 0); + switch_dispatch(15, 0, 0); + switch_dispatch(20, 0, 0); + + //nested convenience function prints the op + void print_func(int op, int r, int s) { + printf("FUNC: op %d (%d, %d) = %d\n", op, r, s, func_dispatch(op, r, s)); + }; + //test a few dispatches + print_func(OP_ADD, 4, 5); + print_func(OP_DIV, 8, 2); + + //what if we don't want to make a new stack frame? + void print_goto(int op, int r, int s) { + printf("GOTO: op %d (%d, %d) = %d\n", op, r, s, goto_dispatch(op, r, s)); + } + print_goto(OP_ADD, 4, 5); + print_goto(OP_DIV, 8, 2); + + return 0; +} + +int +switch_dispatch(int op, int x, int y) +{ + switch (op) { + case OP_ADD: + return x + y; + case OP_MUL: + return x * y; + case OP_DIV: + return x / y; + case OP_SUB: + return x - y; + case 7 ... 10: + printf("switch_dispatch invoked with opcode in range 7 to 10\n"); + break; + case 15 ... 20: + printf("switch_dispatch invoked with opcode in range 15 to 20\n"); + break; + } + return -1; +} + +int +func_dispatch(int op, int x, int y) +{ + //let's make a jumptable with function pointers + //compare with and without static + static int (*jumptable[OP_MAX])(int a, int b) = { + //named index notation lets us specify things out-of-order + [OP_ADD] add, + [OP_MUL] mul, + [OP_SUB] sub, + [OP_DIV] div + }; + return jumptable[op](x, y); + //or + //return (*jumptable[op])(x, y); +} + +int add(int x, int y) { return x + y; } +int sub(int x, int y) { return x - y; } +int mul(int x, int y) { return x * y; } +int div(int x, int y) { return x / y; } + +int +goto_dispatch(int op, int x, int y) +{ + //now, lets make a jumptable using gotos + static void *labeltable[OP_MAX] = { + //here we see the address-of-label syntax + [OP_ADD] &&l_add, + [OP_MUL] &&l_mul, + [OP_SUB] &&l_sub, + [OP_DIV] &&l_div + }; + + //we must dereference a label's address if we want to goto it + goto *labeltable[op]; + +l_add: + return x+y; +l_sub: + return x-y; +l_mul: + return x*y; +l_div: + return x/y; +} +#endif Added: sandbox/trunk/src/c/coercion.mk =================================================================== --- sandbox/trunk/src/c/coercion.mk (rev 0) +++ sandbox/trunk/src/c/coercion.mk 2009-01-24 10:15:03 UTC (rev 1138) @@ -0,0 +1,2 @@ +coercion: coercion.c + gcc -Wconversion coercion.c Added: sandbox/trunk/src/c/rand.c =================================================================== --- sandbox/trunk/src/c/rand.c (rev 0) +++ sandbox/trunk/src/c/rand.c 2009-01-24 10:15:03 UTC (rev 1138) @@ -0,0 +1,8 @@ +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +int main() { + printf("RAND_MAX = %d\n", RAND_MAX); + printf("INT_MAX = %d\n", INT_MAX); + return 0; +} Modified: sandbox/trunk/src/c/switches.c =================================================================== --- sandbox/trunk/src/c/switches.c 2009-01-23 21:44:55 UTC (rev 1137) +++ sandbox/trunk/src/c/switches.c 2009-01-24 10:15:03 UTC (rev 1138) @@ -1,4 +1,4 @@ - +// Explore the generated asm code. int main() { int x = 3; switch (x) { Added: sandbox/trunk/src/c/switchsyntax.c =================================================================== --- sandbox/trunk/src/c/switchsyntax.c (rev 0) +++ sandbox/trunk/src/c/switchsyntax.c 2009-01-24 10:15:03 UTC (rev 1138) @@ -0,0 +1,9 @@ +// Demo fancy switch range syntax. + +int main(int argc, char **argv) { + switch (argc) { + case 0 ... 3: return 0; + case 4 ... 8: return 1; + default: return 2; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |