views:

168

answers:

7
+9  Q: 

Example compilers

I'm searching for the source code of a compiler capable of creating Win32 programs from an input program in a programming language (It doesn't matter which, maybe the simpler the better)

Yet I can't find anything right for me and huge compilers like GCC make me extremely confused as they have so many features that I don't know where to start.

  • Is there an OpenSource Win32 micro-compiler for some programming language out there I could take a look at?
+4  A: 

http://en.wikipedia.org/wiki/List_of_compilers chose ones with License type "open source"

Andrey
Those are the kind of compilers that don't help me. How do you expect me to dig through ten thousand lines of code just to find out the basics?
saf
do you think there are compilers with number of codes that is less then 10 ths? i have a feeling that there are none.
Andrey
@Andrey: There's plenty of such compilers. If you take away complex syntax and optimization, a compiler really isn't that complicated. Granted, a compiler like that is not that useful for actual coding work, but ideal for learning the basics about how compilers work.
Matti Virkkunen
A: 

gas can turn assembly language into object code, which ld can then link with the appropriate runtimes to generate a program.

Ignacio Vazquez-Abrams
Neither of which is a compiler.
WhirlWind
Downvoted in agreement with WhirlWind.
Kawa
Assembler is a compiler, by definition.Upvoted in disagreement with downvoters.And yes, if you're implementing a compiler, you should not be bothered with PE files format - it's not a compilers' job, it's linkers' job.
SK-logic
+2  A: 

I would suggest picking up a compilers textbook. They explain all the required components of a compiler and usually develop a compiler from start to finish.

I own a copy of Modern Compiler Implementation in Java from my compilers course and I'm quite pleased with it.

Ben S
A: 

If your objective is to learn how compilers work and/or how to code then, you really should read a book on the subject. However, when looking for source code samples, try Pascal compilers samples like this one.

Pascal is has a somewhat simple grammar and is implemented in many books.

Bruno Brant
A: 

The simplest programming language is Assembly, x86-assembly to be precise. The most famous assembler, NASM, is open-source; you can check out it's source at:

http://repo.or.cz/w/nasm.git

NASM has macro's which are indeed "compiled" into assembler statements. Though it's not a fully fledged language, its focused on the basics.

Pindatjuh
A: 

I would suggest looking for a Pascal, C or SmallC compiler source for MS-DOS (16-bit). Particularly if they limit themselves to 16-bit flat real mode (<= 64KB memory mode), they should be fairly simple.

Support for segmented real mode adds a great deal of complexity that would be best to avoid, and it is irrelevant for 32-bit flat protected mode program (non-OS 32-bit programming).

For a non-x86 processor, the example program for Jack Crenshaw's series of articles "Let's Build a Compiler" which is a Pascal-like syntax compiler targeting the Motorola MC68000 16/32-bit processor. There is also Marcel Hendrix's written in Forth on win32 version of the series.

There is also the TCC - Tiny C Compiler, which support PE-i386 output (i.e. Win32).

Though if you couldn't bother to read the entire SO link that Firas Assaad gave in the comments, which contains all but one of these links, I believe you're going to find the source code of a compiler terribly long.

mctylr
+1  A: 

Are you trying to understand how to build a compiler or are you trying to learn what a Win32 executable looks like internally?

If the former then reading a book on how to create a compiler will be way more efficient than trying to reverse engineer source code. Compilers are unlike anything you've ever seen and it really helps to have an author walk you through the principles and common techniques and tools used. It's also worth pointing out that compilers are built in stages and it's only towards the end of the implementation do you need to have any knowledge of the CPU and operating system that you are targeting. If you are new to compiler construction then you have plenty to learn about before you'll get to that stage.

However if you want to understand the format of Win32 executables then you need to understand x86 (or x64) assembly code and the Portable Execution (PE) file format (which is used by Windows for executables). For an introduction to x86 assembly code I'd recommend Dr Paul Carter's excellent "PC Assembly Language" free eBook http://www.drpaulcarter.com/pcasm/ and for PE I'd start here http://msdn.microsoft.com/en-us/magazine/cc301805.aspx and http://msdn.microsoft.com/en-us/magazine/cc301808.aspx

Andrew O'Reilly