Example compilers [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI'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?
http://en.wikipedia.org/wiki/List_of_compilers chose ones with License type "open source"
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.
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
gas
can turn assembly language into object code, which ld
can then link with the appropriate runtimes to generate a program.
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.
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.
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.
精彩评论