Permalink
Browse files

Some tweaks from Bob Evans

  • Loading branch information...
n1be committed Jan 19, 2013
1 parent 807205b commit f23368d11a910b35e76bdc3562cae7cf14f2d3f9
Showing with 29 additions and 7 deletions.
  1. +1 −1 1-typedefs.c
  2. +1 −0 2-special_types.c
  3. +6 −1 3-functions.c
  4. +4 −1 4-jumptable.c
  5. +3 −2 5-macros.c
  6. +6 −1 5.5-xmacros.c
  7. +8 −1 Makefile
View
@@ -72,7 +72,7 @@ part2(void)
mile b = a;
#if 0
- Meter_t c = { 19 };
+ Meter_t c = { 19 }; # This code protects against mixing up units ...
Miles_t d = c; //invalid!
#endif
}
View
@@ -11,6 +11,7 @@ struct Foo {
int c : 16;
};
//NOTE: bitfield int a:1 has possible values 0 and -1. Watch out for sign extension!
+//NOTE: avoid bitfields since endian-ness is indeterminate
struct Foo_normal {
int a;
View
@@ -24,7 +24,12 @@ main(int argc, char **argv)
//stupidly, func = *func
printf("(*func)(5) = %d\n", (*func)(5));
- //cdecl.org deciphers function pointers
+ // NOTE: func contains a pointer to trampoline within
+ // the current stack frame. That pointer is not
+ // valid once this function exits and the frame
+ // is popped off the stack.
+
+ //http://cdecl.org/ deciphers function pointers
//e.g. int (*x(int (*)(int )))(int )
//x is a function that takes
//(pointer to function(int) returning int)
View
@@ -40,6 +40,7 @@ switch_dispatch(int op, int x, int y)
return x / y;
case OP_SUB:
return x - y;
+ //NOTE: case ranges, "n ... m", is a gcc-specific extension
case 7 ... 10:
printf("switch_dispatch invoked with opcode in range 7 to 10\n");
break;
@@ -75,11 +76,13 @@ 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) = {
+ // NOTE: This type of initializer guarantees entire array
+ // is initialized.
//named index notation lets us specify things out-of-order
[OP_MUL] = mul,
[OP_SUB] = sub,
[OP_DIV] = div,
- [OP_ADD] = add
+ [OP_ADD] = add
};
return jumptable[op](x, y);
//or
View
@@ -55,8 +55,9 @@ main(int argc, char **argv)
//scoped variable decls
//surround args with parens to isolate
#define max1(x, y) ((x) > (y) ? (x) : (y))
-//({}) is gcc
-//typeof(x) is gcc
+//({}) is gcc a extension called expression block.
+//An expression block evaluates to value of last expression.
+//typeof(x) is another gcc extension.
#define max2(x, y) ({\
typeof(x) _x = (x); \
typeof(y) _y = (y); \
View
@@ -46,7 +46,10 @@ main(void)
LIST
};
#undef X
- goto *table2[baz];
+
+ unsigned int opcode = baz;
+interpret:
+ goto *table2[opcode];
//by defining GOTO_END as this type of macro, we can
//put it in the for loop
@@ -68,6 +71,8 @@ main(void)
}
the_end:
+// NOTE: If the intent is to interpret multiple opcodes,
+// could fetch next opcode and re-iterate at interpret:.
return 0;
}
View
@@ -2,8 +2,15 @@ all: 1-typedefs 2-special_types 3-functions 4-jumptable 5-macros 5.5-xmacros 6-a
# You may need to remove -fnested-functions if your GCC doesn't support it
# This appears to be the case for earlier versions of GCC on Linux
+ifneq ($(strip $(shell uname -s)),Linux)
+FNESTED_FUNCTIONS_OPTION := -fnested-functions
+else
+FNESTED_FUNCTIONS_OPTION :=
+endif
+$(warning Using FNESTED_FUNCTIONS_OPTION = $(FNESTED_FUNCTIONS_OPTION))
+
%: %.c
- gcc -std=gnu99 -fnested-functions $< -o $@
+ gcc -std=gnu99 $(FNESTED_FUNCTIONS_OPTION) $< -o $@
jumptable.asm: 4-jumptable
objdump -D 4-jumptable > jumptable.asm

0 comments on commit f23368d

Please sign in to comment.