OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 7:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Strange switches
PostPosted: Wed Nov 06, 2002 1:46 am 
Heya,

been looking into compilers and I didn't know that this was legal C:

Code:
switch(a)
     case 0: case 10: case 53: printf("gotcha.\n");


notice the lack of curly brackets? bit annoying to implement since I've done labels slightly differently in my compiler but I thought it would be interesting to use if you needed a variable to be a certain group of values.

- Nick


Top
  
 
 Post subject: Re:Strange switches
PostPosted: Wed Nov 06, 2002 2:17 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 22, 2004 11:00 pm
Posts: 1076
sometimes fallthrough is good.
what about basics implementation of case..


Code:
case 1 to 20 or 31, 32 and 54
  ....
case 22


etc

_________________
-- Stu --


Top
 Profile  
 
 Post subject: Re:Strange switches
PostPosted: Wed Nov 06, 2002 2:39 am 
I think I'll stick with straight contants for now.. but I might look into that later.

One thing I've noticed while disassembling AGI (and further proof they used a compiler of some sorts for parts of it) is their implementation of switch statements.

They always had the switch statement like this (pseudo):

Code:
goto switch;

one:
  code
two:
  code
three:
  code
default:
  code
goto next;

switch:
if 1 goto one;
if 2 goto two;
if 3 goto three
goto default;

/* jump tables were also used */

next:


I hope that makes sense. the point I'm trying to make is that they always had the statement block first and then the switch jump table

I finally realised the reason why when I started doing my own: single pass compilers.

If you were to put all the switch jumps at the start.. you would have to parse the statements first to look for any case labels, store that in memory somewhere, print out your switch jump table and then the saved statements.

Storing all that data could take a lot of space. And you don't want to have to parse the statments twice because your parser might not allow it.

Alternatively, you could save a bit of space to insert a goto, print out your statement block, and then your switch jump table. once you know where you jump table is, you just have to fill in the proper address at the start via backpatching.

umm. I thought it was interesting.. but kinda hard to explain :)

- Nick


Top
  
 
 Post subject: Re:Strange switches
PostPosted: Wed Nov 06, 2002 6:31 am 
Yar, me matey, then each switch statement is it's own discrete block.

Also, that C format is to support a lot of different things:

case 'a' : case 'A' :

, for example. About the most acceptable kind of fall-through, as well. Almost like overloading...


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 144 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group