greeting[] = "Hello, world!"; printf("%s\n", greeting); return 0; } So many questions! What is stdio.h? What is #include? Why? Who invokes main()? Where’re the arguments from? When’s const-ness checked? Where’s and How’s printf() implemented? Why does main() return an int? What’ll happen after it returns? How does printf() format the output string? How does it take variable-length arguments? Where is the string literal stored? Can we modify the content? Are char[] and char* different? What’s the value of greetings? When’s the value assigned? Is it always the same value?
Assignment = int Subscription [] int Multiplication * int Identifier array int[] Identifier index int Addition + int Addition + int Identifier index int Number 4 int Number 2 int Number 6 int 1. Check for static semantics (e.g., type information) 2. Insert nodes for implicit type conversion
modules 2. External global symbols — defined by some other module 3. Local symbols — defined and referenced exclusively extern int shared; int add(int, int); int a = 0x689; int main() { return add(a, shared); } a.c int shared = 0x92; static int private; int add(int x, int y) { return x + y; } b.c External global Global Global Global Local Global
“Compiling a Compiler” • Kito Cheng, ʬᕆஊฤᩄث࠷ՂԽٕज़ʭ • MSDN, “Peering Inside the PE: A Tour of the Win32 Portable Executable File Format” • Alexander Sotirov, “Tiny PE: Creating the smallest possible PE executable” • Randal E. Bryant & David R. O'Hallaron, “Computer Systems: A Programmer's Perspective” w 俞ߕࢠɺੴຌɺᖊѪຽ ʬఔࣜઃܭࢣతࣗզमཆɿ࿈݁ɺࡌೖɺఔࣜݿʭ http://csapp.cs.cmu.edu/ http://www.tenlong.com.tw/items/9861818286?item_id=53897 http://www.phreedom.org/research/tinype/ https://msdn.microsoft.com/en-us/library/ms809762.aspx http://www.slideshare.net/kitocheng/ss-42438227 https://wiki.sars.tw/doku.php?id=programming:compiling_a_compiler http://www.slideshare.net/jserv/how-a-compiler-works-gnu-toolchain Further Reading