Hi Arnold,

The current TOK_SWITCH/TOK_CASE code clearly prefers simplicity over
performance and features. It was just fine for OTCC but I agree with
you it should be improved.

However, I don't like this patch, because it adds time+space complexity
without improving generated code in any way. Also, case ranges are not
handled properly (eg 1..3 + 2 are incorrectly accepted).

With just a little bit more effort, you can do much better.

- switch_start: generate code that loads a reference to the case table
  and calls a function in libtcc.

- TOK_CASE: add a literal (or literal range) to the table. Don't generate
  any code, just gather current code offsets.

- switch_end: sort the table. Check for duplicates (now that's for free).
  Emit the table to some section (if it's not the current code section
  you don't have to generate extra jumps), and fix up the symbol reference
  taken in switch_start.

The runtime function performs a binary search over the sorted array and
to the corresponding code block. Things as defaults and case ranges make it
a bit more complex. Also, note that switch statements can be nested..

Still, even with all this in place, I believe it probably should not be
significantly more difficult than this.

Tinycc-devel mailing list

Reply via email to