Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2005, 1:51 PM   #1
SittingDuck
Programmer
 
SittingDuck's Avatar
 
Join Date: Nov 2005
Location: Moseley, Birmingham, England, Earth
Posts: 51
Rep Power: 3 SittingDuck is on a distinguished road
Global variables

How do I deal with global variables, from the point of view of writing a compiler?

How do I know the address that the variable will be in?
SittingDuck is offline   Reply With Quote
Old Nov 20th, 2005, 2:01 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You don't. Different systems (and languages) have different conventions, but nowadays they're mostly all relative. It's part of the job of what you're writing to conform to any external requirements and make your own determinations where there aren't any to conform to. You have a fair bit of reading ahead of you. Mere compilation is only a part of the job, albeit not a trivial part.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 21st, 2005, 7:57 PM   #3
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
One way to do this is to allocate global variables to a separate section of memory (the DATA segment) and allocate one CPU register to point to the base of that section. All you need to know is the relative offsets of each global variable from the base .
Scorpions4ever is offline   Reply With Quote
Old Nov 22nd, 2005, 11:51 AM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by SittingDuck
How do I deal with global variables, from the point of view of writing a compiler?

How do I know the address that the variable will be in?
If you're writing a compiler, you need to know how your target platform formats its executable files. If you're aiming for a Win32 target, you'll want to look up specs on the PE (portable executable) format.

In addition to the actual code, your binary image will contain information about what segments/sections your program has, what type they are (code, data, readable, executable, etc), and other stuff (for example, Windows often has embedded 'resources', like icons and bitmaps).

For many modern platforms, you have three usual segments/sections: One for code (often called "TEXT" or something similar; it is a readable, executable segment), one for static data (often called "DATA"; it is readable and writable), and one for stack (often "BSS"; it is readable, writable, and it behaves as a stack, meaning it's addressed with the stack register, and it expands down, rather than up). Sometimes, you will have additional segments, but most often these are just more data segments.

Having said all that, what compilers usually emit is object code, not executable code. This means you have to determine which object file format is best suited to your needs, emit code in that format, and then feed it to a linker. This in turn necessitates that you choose an object file format that has a compatible linker available, given your target platform.

As you can gather, it's not a trivial task- and that's without actually generating the code!

Still, once you have all those issues ironed out, handling globals will be easy. You will maintain some kind of symbol table, with the names, types, and addresses of each reference. From the types, you can calculate the address of each item relative to some predefined starting point (ie, the beginning of the data segment), since the address of an item will be the address of the starting point (for the first item), and the address of the previous item plus the size of the previous item plus any padding for memory alignment (for subsequent items).

Local variables are likewise handled similarly, except the symbol table will be local in scope, and variables will be relative to the current stack position, rather than the global data segment.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC