TweetFollow Us on Twitter

Think C 5.0
Volume Number:7
Issue Number:9
Column Tag:Tools of the Trade

THINK C 5.0

By Chris Faigle, Richmond, VA

Think C 5.0 is here! This new version of Symantec’s compiler and environment has an incredible number of new features. I have had a few months to use it (since I was a beta tester), and I feel it is so far superior to 4.0, that they are not in the same class. This article is too short to describe all of the new features, but I will try to hit all of the major changes, and a lot of the minor ones. Just to whet you appetite, I’ll mention a few now: Completely rewritten compiler, new disassembler, new code optimizer, new preprocessor, fuller object implementation, class browser, and supports MPW header files!

The Compiler

The compiler has been completely rewritten. The folks at Symantec decided that in order to support all of the features that they wanted, they needed to almost start over. This cost them a lot of time and effort, but the results were worth it: A C compiler so flexible, that it can become ANSI conformant, down to even recognizing trigraphs, or change to accept Objects and Think C Extensions, use the native 68000 floating point structure, change how it decides a function’s prototype, generate MacsBugs labels in both short and long format, and even run a global Optimizer over the code. I will try to elaborate briefly on all of these areas and more.

Figure 1: Options Dialog (Language Settings Shown)

Compiler Options

The Options dialog (Figure 1) is your main control over the compiler. It has now been expanded to have six areas of options: Preferences, Language Settings, Compiler Settings, Code Optimization, Debugging and Prefix. Each option, when selected will bring up a help message in the dialog. The Factory Settings button will change the settings in each area, not just the area that you are in, to the settings that THINK C comes with. All of the settings are saved in the project, and you can also change the settings that Think C will open a new project with. Further, most of the options can also be changed by using #pragma options preprocessor directive in your source. Two other options for applications are located in the Set Project Type dialog, and they are Far Code: jump tables (but not segments) >32k and Far Data: global data up to 256k.

  File:   0 “Hello World.c”
  File:   1 “MacTraps”
  File:   2 “ANSI”

Segment “%GlobalData” size=$000622
 _abnormal_exit  -$000610(A5)    file=”ANSI”
 __console_options -$000536(A5)
 __log_stdout    -$0004E2(A5)
 __ctype-$000424(A5)
 errno  -$000324(A5)
 _ftype -$0002F0(A5)
 _fcreator-$0002EC(A5)
 __file -$0002E8(A5)
 __copyright-$00003A(A5)

Segment “Seg2” size=$000010 rsrcid=2
 main   $000004 JT=$000072(A5)  file=”Hello World.c”

Segment “Seg3” size=$00479A rsrcid=3
 malloc $000004     file=”ANSI”
 calloc $000040
 realloc$0000C0
 free   $0001C6
 atexit $000290
    etc

Figure 2: Link Map (Abbreviated)

Language Settings

Three main areas occupy the language settings (Figure 1). The ANSI conformance section has options that when on are ANSI conformant. These are things like #define _STDC_. The easiest way to provide complete ANSI conformance is to click the ANSI Conformance button that is added to the dialog, however, you must read this section of the manual. You can get burned if you do not understand all of the ramifications of being totally ANSI conformant. For example, if you are completely ANSI conformant, the compiler will think ‘????’ is a trigraph and not a long (as used in File and Creator types).

The Language extensions section allows you to choose to support the ThinkC extensions (like asm{}, pascal keyword, and // comments), ThinkC and Object extensions, or no extensions. The prototype section selects whether Think C strictly enforces prototypes, and if it does, whether it requires an explicit prototype, or whether it will infer what the prototypes is from either the function, or a call to that function, whichever comes first.

Compiler Settings

This area let’s you change some of the ways that Think C generates code, like whether to generate 68020 & 68881 instructions, instead of plain 68000. It also lets you change several of the default characteristics of Think’s Objects. In addition, it will let you use ‘Native Floating Point’ format. The manual has a long discussion of all of the ramifications of this, but suffice it to say that floating point calls are faster with this option on (1:10 vs 1:14 for 32000 sin’s & no SANE), but the ANSI library (and any that you have that use floating point variables) must be recompiled with this option on, and you should probably not distribute libraries with this feature on, unless the recipient is also going to use it.

Code Optimization

Think C now provides six different types of optimization that the user can select. You can use all of them, or choose only the ones you want, plus automatic register assignment. Unfortunately, almost all can interfere to some extent with the Source Debugger, since they change calls and variables, and can make the code look somewhat different than what the Debugger expects. I have not really had any serious problems when source debugging, but it is generally a good idea to save the optimization for your builds.

Defer and Combine Stack Adjusts (Figure 3) accumulates adjustments to the stack until they are necessary. In the example, without DCSA on, two stack modifications are made [ADDQ.L #$2,A7], but with it on, they are removed. The compiler is even smart enough to recognize that instead of accumulating them into [ADDQ.L #$4,A7] they can be removed altogether, since [UNLK A6] is the next instruction!

Supress Redundant Loads (Figure 4) will not load variables into registers if they are already in a register. Furthermore, it will also perform moves from registers [0014MOVE.W D0,$FFFA(A6)] rather than from memory [0014 MOVE.W $FFFC(A6),$FFFA(A6)] if the variable is already in a register. This instruction is faster to both load (2 less bytes) and faster to execute.

Induction Variable Elimination will optimize loops that access arrays by remembering the address of the last accessed element of the array and adding the size of the element to access the next one, rather than making the Mac perform 32-bit multiplication every time. An example of the code that would be optimized by this is:

/* 1 */

Example source code:
main()
{
 short  x;
 short  y;

 func1(x);
 func2(y);
}
Defer and Combine Stack Adjusts OFF:
main:
00000000        LINK      A6,#$FFFC
00000004        MOVE.W    $FFFE(A6),-(A7)
00000008        JSR       $0000(A5)
0000000C        ADDQ.L    #$2,A7
0000000E        MOVE.W    $FFFC(A6),-(A7)
00000012        JSR       $0000(A5)
00000016        ADDQ.L    #$2,A7
00000018        UNLK      A6
0000001A        RTS
Defer and Combine Stack Adjusts ON:
main:
00000000        LINK      A6,#$FFFC
00000004        MOVE.W    $FFFE(A6),-(A7)
00000008        JSR       $0000(A5)
0000000C        MOVE.W    $FFFC(A6),(A7)
00000010        JSR       $0000(A5)
00000014        UNLK      A6
00000016        RTS

Figure 3: Sample Code Optimization (Defer and Combine Stack Adjusts)

/* 2 */

Example Source Code:
main()
{
 short  i=1;
 short  j;
 short  k;

 j=i+1;
 k=j;
}
Supress Redundant Loads OFF:
main:
00000000        LINK      A6,#$FFFA
00000004        MOVE.W    #$0001,$FFFE(A6)
0000000A        MOVEQ     #$01,D0
0000000C        ADD.W     $FFFE(A6),D0
00000010        MOVE.W    D0,$FFFC(A6)
00000014        MOVE.W    $FFFC(A6),$FFFA(A6)
0000001A        UNLK      A6
0000001C        RTS
Supress Redundant Loads ON:
main:
00000000        LINK      A6,#$FFFA
00000004        MOVE.W    #$0001,$FFFE(A6)
0000000A        MOVEQ     #$01,D0
0000000C        ADD.W     $FFFE(A6),D0
00000010        MOVE.W    D0,$FFFC(A6)
00000014        MOVE.W    D0,$FFFA(A6)
00000018        UNLK      A6
0000001A        RTS

Figure 4: Sample Code Optimization (Supress Redundant Loads)

/* 3 */

int a[ARRAY_SIZE], i;

 for(i=0;i<ARRAY_SIZE;++i)
 a[i]=GetNextElement();

Code Motion will remove expressions from a loop that are not changed or accessed within that loop:

/* 4 */
            
while(!feop(fp)) {
 i=x*5;
 DoSomething(fp,i);
 }

would be recast as:

/* 5 */

 i=x*5;
 while(!feop(fp))
 DoeSomething(fp,i);

CSE Elimination reduces common expressions by assigning them to a temporary variable:

/* 6 */

 a=i*2+3;
 b=sqrt(i*2);

would be treated as:

/* 7 */

 temp=i*2;
 a=temp+3;
 b=sqrt(temp)

Register Coloring will search your code for variables that are never used at the same time and assign them to the same variable or register if it is available:

/* 8 */

 int  i,j;
 for(i=0;i<10;++i) {
 DoSomething(fp,i)
 }
 for(j=0;j<10;++j) {
 DoSomethingElse(fp,i)
 }

would be treated as:

/* 9 */

 int  i;
 for(i=0;i<10;++i) {
 DoSomething(fp,i)
 }
 for(i=0;i<10;++i) {
 DoSomethingElse(fp,i)
 }

Debugging

In this area are all the previous options for control over the debugger, plus some directives to the compiler about MacsBug names (both short and long format are now supported), and whether the compiler should try to always generate a stack frame, which is helpful when debugging.

Prefix

Instead of only allowing the inclusion of <MacHeaders> as in 4.0, 5.0 allows you, on a project-wide basis, to specify preprocessor directives in this dialog. The default is #include <MacHeaders>, but you could add for example:

/* 10 */

 #define_DEBUG_  1

then _DEBUG_ will be defined for every source file. When you are finished with your debugging and want to build the final, simply remove the line from the prefix dialog. (I love this feature!) However, you can still only #include one precompiled header file.

Preprocessor directives

Think C now has an expanded set of preprocessor directives, including __option to test compiler option settings:

/* 11 */

 #if __option(native_fp) && !__option(mc68881)
 (Do I have native floating point and non-68881?)

and also the #pragma options to change compiler settings on the fly, some even within functions:

/* 12 */

 #pragma options (macsbug_names,!long_macsbug_names)
 (Change macsbug labels to short format)

Think C’s Objects

Think C 5.0 is not a C++ implementation, although it is based upon it. 5.0 has a much fuller object implementation than 4.0 and also seems to be cleaner in general. There have been quite a few changes, some of which would have been tough to implement without a complete rewrite, but because Symantec bit the bullet and did it, Think C’s objects will now move back and forth from C++ with relative ease. If you use Think C’s objects, you definitely should take the time to read 5.0’s manual because this and ANSI conformance are the two most affected area of the compiler!

Some of the changes from 4.0 are:

new and delete are now keywords instead of functions

class functions

class variables

private, protected and public variables

virtual and non-virtual methods

constructors and destructors

allocators and deallocators

sizeof(classname) no longer allowed

member() to test class membership

__class operator returns a pointer to the class information record

Differences from C++:

Every class must have at least one method

no operator overloading

public, protected and private are not keywords

constructors cannot create an object

friends are not allowed

no multiple inheritance

inline methods not supported

member() and __class are extensions to Think C

Think C’s manual provides a good explanation of Think C’s

implementation of Objects and also provides some good examples of some of the subleties that you should be aware of.

Inline Assembler

Think C’s inline assembler also has changes of note. The asm cpu {} construct will now recognize 68000, 68020, 68030, 68040, 68881, and 68882, and has more stringent error reporting if you are using addressing modes or instructions that are not supported. Note, however, that using the inline assembler disables the global optimizer for that function. Therefore it may be advisable to do your assembly in separate functions that could not be optimized anyway.

The Editor

The Think C editor has been criticized in the past as being the weakest part of the environment. Symantec has made a number of changes to it including:

home, end, page up & page down keys on an extended keyboard now work

del (not Delete) on an extended keyboard is a right-wise delete

Command-left arrow and Command-right arrow skip to previous and next word

The editor also supports markers, and they are even compatible with MPW. Markers are added by moving the cursor to the desired line, choosing Mark and entering the name of the marker. To access a marker, click on the title bar of the source window with the command key pressed. A pop-up menu, like the header file list, will appear. Selecting one will jump the editor directly to the source line containing that marker.

The Debugger

At first glance, the Debugger seems the same as in 4.0, and basically the interface is, but the underlying code has changed substantially. First, the debugger can save it’s session so the next time that you debug all of your expressions and breakpoints will already be entered. Second, the debugger is faster to load and run, than 4.0.

The Disassembler

That’s right, now you can dissassemble on the fly! While the output is not of TMON quality, it can be (and has been) very helpful. Note that all the optimizer assembly examples (Figures 3 & 4) were generated by Think C itself! (Pretty neat, huh?) This is another one of my favorite features.

The Preprocessor

Another handy feature is the addition of a preprocessor. This will output to an untitled window the contents of a source file after the preprocessor has worked it’s magic on it. This can be useful in cases where you want to see what your source really looks like or you have code that needs different #includes or #defines under different compiler options, and you need to verify that your preprocessor directives are correct.

The Class Browser

Again, another great feature! The class browser (Figure 5) displays graphically the dependence of your objects (as long as they have been compiled). Each box is also a pop-up menu which displays the methods defined in each class. Selecting a method opens that source file, and jumps to where that method is defined.

Figure 5: Class Browser (For NewDemoClass.Π)

Think Class Library 1.1

The Think Class Library has changed a fair amount. 1.1 containss quite a few new classes (including a dialog class, several text classes, and classes for pop-up menu), and has changes to some of the existing classes. Several classes, CPane for instance, can now use an optional 32-bit coordinate system. Symantec has tried to make changes that will not affect existing applications, but the price of progress is incompatibility. Really though, the changes to the TCL are beyond the scope of the article, and any developer that has a serious project under TCL will have to spend some time reading before he can even try to compile his classes. Symantec does provide a complete list of TCL changes, documentation for all the classes, and each file that has been changed has comments in the source denoting the changes.

System 7.0, header files and MacTraps

Think C 5.0 is, of course, completely compatible with System 7.0. Further, it includes all of the needed 7.0 libraries and headers. The really nice thing is that now all the header files are compatible with MPW, so you will no longer need extremely complicated #IF THINK_C directives to determine whether you are compiling under MPW or THINK C. Also THINK_C is defined to be 5 and not 1, so you can tell by using #if THINK_C<5 whether you have an older version or not.

MacTraps has now been split into two files: MacTraps which has most of the same toolbox traps, and MacTraps2, which has the Inside Mac VI traps, and some of the more arcane traps that developers rarely use.

Demos/Sample Source

The original demos (MiniEdit, Bullseye, and HexDump DA) are still shipped, but Think C now includes Object Bullseye, and LearnOOP. Neither of these object oriented demos require the Think Class Library, and both are excellent tutorials. The TCL Demos still include TinyEdit, Starter, and ArtClass, but now also includes NewClassDemo (Figure 5). New ClassDemo demos the use of almost every class imaginable. If you are interested to see one of the new features implemented, here is the best place to look.

Little Things

Sometimes the little things make all of the difference, and one of the nicest little changes is the Add File dialog box. It has been changed to allow the addition of multiple files at the same time, and can even add all the files in a folder with one button press. Another little feature is an application included with Think C 5.0 called Prototype Helper, that will both produce prototypes from existing source, and also change the source to ‘new-style’ C function declarations.

In Summary

Think C 5.0 has really impressed me. Not only has it addressed many of it’s former failings, it has an incredible number of new features. Even though Think C 4.0 was an extremely popuplar compiler, Symantec decided to completely rewrite it. This is the sort of product dedication that produces the kind of fantastic software that Think C 5.0 is.

Special thanks to Michael Rockhold of Symantec Corp. for his time and effort in answering my questions for this article.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Dropbox 193.4.5594 - Cloud backup and sy...
Dropbox is a file hosting service that provides cloud storage, file synchronization, personal cloud, and client software. It is a modern workspace that allows you to get to all of your files, manage... Read more
Google Chrome 122.0.6261.57 - Modern and...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
Skype 8.113.0.210 - Voice-over-internet...
Skype is a telecommunications app that provides HD video calls, instant messaging, calling to any phone number or landline, and Skype for Business for productive cooperation on the projects. This... Read more
Tor Browser 13.0.10 - Anonymize Web brow...
Using Tor Browser you can protect yourself against tracking, surveillance, and censorship. Tor was originally designed, implemented, and deployed as a third-generation onion-routing project of the U.... Read more
Deeper 3.0.4 - Enable hidden features in...
Deeper is a personalization utility for macOS which allows you to enable and disable the hidden functions of the Finder, Dock, QuickTime, Safari, iTunes, login window, Spotlight, and many of Apple's... Read more
OnyX 4.5.5 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more
Hopper Disassembler 5.14.1 - Binary disa...
Hopper Disassembler is a binary disassembler, decompiler, and debugger for 32- and 64-bit executables. It will let you disassemble any binary you want, and provide you all the information about its... Read more

Latest Forum Discussions

See All

Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »
TouchArcade Game of the Week: ‘Vroomies’
So here’s a thing: Vroomies from developer Alex Taber aka Unordered Games is the Game of the Week! Except… Vroomies came out an entire month ago. It wasn’t on my radar until this week, which is why I included it in our weekly new games round-up, but... | Read more »
SwitchArcade Round-Up: ‘MLB The Show 24’...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for March 15th, 2024. We’re closing out the week with a bunch of new games, with Sony’s baseball franchise MLB The Show up to bat yet again. There are several other interesting games to... | Read more »
Steam Deck Weekly: WWE 2K24 and Summerho...
Welcome to this week’s edition of the Steam Deck Weekly. The busy season has begun with games we’ve been looking forward to playing including Dragon’s Dogma 2, Horizon Forbidden West Complete Edition, and also console exclusives like Rise of the... | Read more »
Steam Spring Sale 2024 – The 10 Best Ste...
The Steam Spring Sale 2024 began last night, and while it isn’t as big of a deal as say the Steam Winter Sale, you may as well take advantage of it to save money on some games you were planning to buy. I obviously recommend checking out your own... | Read more »
New ‘SaGa Emerald Beyond’ Gameplay Showc...
Last month, Square Enix posted a Let’s Play video featuring SaGa Localization Director Neil Broadley who showcased the worlds, companions, and more from the upcoming and highly-anticipated RPG SaGa Emerald Beyond. | Read more »
Choose Your Side in the Latest ‘Marvel S...
Last month, Marvel Snap (Free) held its very first “imbalance" event in honor of Valentine’s Day. For a limited time, certain well-known couples were given special boosts when conditions were right. It must have gone over well, because we’ve got a... | Read more »
Warframe welcomes the arrival of a new s...
As a Warframe player one of the best things about it launching on iOS, despite it being arguably the best way to play the game if you have a controller, is that I can now be paid to talk about it. To whit, we are gearing up to receive the first... | Read more »
Apple Arcade Weekly Round-Up: Updates an...
Following the new releases earlier in the month and April 2024’s games being revealed by Apple, this week has seen some notable game updates and events go live for Apple Arcade. What The Golf? has an April Fool’s Day celebration event going live “... | Read more »

Price Scanner via MacPrices.net

Apple Education is offering $100 discounts on...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take $100 off the price of a new M3 MacBook Air.... Read more
Apple Watch Ultra 2 with Blood Oxygen feature...
Best Buy is offering Apple Watch Ultra 2 models for $50 off MSRP on their online store this week. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
New promo at Sams Club: Apple HomePods for $2...
Sams Club has Apple HomePods on sale for $259 through March 31, 2024. Their price is $40 off Apple’s MSRP, and both Space Gray and White colors are available. Sale price is for online orders only, in... Read more
Get Apple’s 2nd generation Apple Pencil for $...
Apple’s Pencil (2nd generation) works with the 12″ iPad Pro (3rd, 4th, 5th, and 6th generation), 11″ iPad Pro (1st, 2nd, 3rd, and 4th generation), iPad Air (4th and 5th generation), and iPad mini (... Read more
10th generation Apple iPads on sale for $100...
Best Buy has Apple’s 10th-generation WiFi iPads back on sale for $100 off MSRP on their online store, starting at only $349. With the discount, Best Buy’s prices are the lowest currently available... Read more
iPad Airs on sale again starting at $449 on B...
Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices again for $150 off Apple’s MSRP, starting at $449. Sale prices for online orders only, in-store price may vary. Order online, and choose... Read more
Best Buy is blowing out clearance 13-inch M1...
Best Buy is blowing out clearance Apple 13″ M1 MacBook Airs this weekend for only $649.99, or $350 off Apple’s original MSRP. Sale prices for online orders only, in-store prices may vary. Order... Read more
Low price alert! You can now get a 13-inch M1...
Walmart has, for the first time, begun offering new Apple MacBooks for sale on their online store, albeit clearance previous-generation models. They now have the 13″ M1 MacBook Air (8GB RAM, 256GB... Read more
Best Apple MacBook deal this weekend: Get the...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New 15-inch M3 MacBook Air (Midnight) on sale...
Amazon has the new 15″ M3 MacBook Air (8GB RAM/256GB SSD/Midnight) in stock and on sale today for $1249.99 including free shipping. Their price is $50 off MSRP, and it’s the lowest price currently... Read more

Jobs Board

Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Relationship Banker *Apple* Valley Main - W...
…Alcohol Policy to learn more. **Company:** WELLS FARGO BANK **Req Number:** R-350696 **Updated:** Mon Mar 11 00:00:00 UTC 2024 **Location:** APPLE VALLEY,California Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill WellSpan Medical Group, York, PA | Nursing | Nursing Support | FTE: 1 | Regular | Tracking Code: 200555 Apply Now Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.