OVERVIEW OF CODE GENERATION .. ASTs...-> [ Static Analysis ] | | IR v [ Code Generation ] | | Machine Code | v Virtual Machine Execution The IR (= Intermediate Representation) records information from static analysis including attributes of identifiers IR TREES An IR is a tree structure, somewhat like an AST but in general it's also like an abstract machine code with information needed for code generation Helps in modularizing compilers and code generation WITHOUT IR WITH IR Java ------>x86 Java ->x86 \/ ||| \ / C ------>MIPS C \\ /-->MIPS \\/ || ->IR C++ ------>Sparc C++ // \-->Sparc \\\/ / \ C# ------>A1 C#/ \>A1 OUR CHOICES FOR AN IR To keep things simple, we will use a modified AST type as an IR Parser: - records structure of programs - provides placeholders for attributes needed for code generation Static analysis: - records attributes of names used (in this modified AST) GENERAL STRATEGY FOR CODE GENERATION Follow the grammar of the ASTs by doing a tree walk which will generate the IR trees (which are just like ASTs with id_use information) FOLLOWING THE GRAMMAR Code resembles the grammar that describes the input data (i.e., the ASTs) When - the grammar has a recursion, code generation recurses - the grammar has alternatives, use a switch statement - for the base cases, generate code TARGET: CODE SEQUENCES Need sequences of machine code instructions including jumps Why? so can run in VM (or assemble them)