1 6.001 SICP Compilation • Context: special purpose vs. universal machines • Compilation vs. Interpretation • Evolution from ec-eval to compiler • Why is compiled code more efficient than interpreter? • Some ideas in compiler implementation • Target, linkages, & register management
6.001 SICP Compilation. Context: special purpose vs. universal machines Compilation vs. Interpretation Evolution from ec-eval to compiler Why is compiled code more efficient than interpreter? Some ideas in compiler implementation Target, linkages, & register management. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
6.001 SICPCompilation
• Context: special purpose vs. universal machines• Compilation vs. Interpretation
• Evolution from ec-eval to compiler• Why is compiled code more efficient than interpreter?
• Some ideas in compiler implementation• Target, linkages, & register management
2
Special-Purpose Machines
Register Machine
Input Output
a b
rem
t
a, b holdintegers asinput
a holdsgcd(a,b)
Example: gcd register machine
3
Universal Machine
MITScheme
Scheme Evaluator as Universal Machine
(gcd 12 8) 4
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
• Able to reconfigure (or program) our universal machine to perform the desired computation
4
Universal Machine
mc-eval
MC-Eval is a Universal Machine
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
(gcd 12 8) 4
MITScheme
5
Alternative Language versions possible
Universal Machine
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
C Languageversion ofScheme
Evaluator
(gcd 12 8) 4
MIT Scheme is a C program!
6
Universal Machine
EC-Eval – Register Machine Code
UniversalRegisterMachine
ec-eval:StoredMachineInstructions
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
(gcd 12 8) 4
7
Compiler
UniversalRegisterMachine
12 8 4
gcd:StoredMachineInstructions
(define (gcd a b) (if (= b 0) a (gcd b (remainder a b))))
compiler
Not necessarily a universal machine
8
Compilation – A Plan
• Use same register machine as ec-eval... except we don't hard-wire in the ec-eval controller
• Look at the machine instructions that ec-eval used• save these instructions for later execution
• Compiler implementation:• ev-xxx serve as initial template for compile-xxx
• We'll see that this is grossly inefficient• many machine instructions are executed during
interpretation that are not necessary during compilation
• so we will examine compiler ideas that lead us toward more efficient generated machine code
9
Register Machine for Compiler
• 7 registers
• exp temporary register (rarely used)
• env current environment
• continue return point
• val resulting value
• unev temporary register (rarely used)
• proc operator value
• argl argument values
• Abstract operations
• environment model, primitive procedures
• syntax operations now needed by compiler but not in output compiled code
• In general, may need to save/restore registers;e.g. eval of operator may itself modify env, continue
[(save continue)][(save env)]<evaluate operator; result in proc>[(restore env)][(save proc)]<evaluate operands; result in argl>[(restore proc)][(restore continue)]<apply procedure in proc to args in argl, and link>
• Paranoid/simple approach: save & restore anyway• More efficient approach: manage register usage carefully
26
Managing Registers – Encode the Contract!
; needs: exp expression to evaluate; env environment; continue return point; output: val value of expression; modifies: unev ; stack: unchanged
• "instruction sequence" data structure:• remember registers needed and modified in addition to code
27
Appending Machine Instructions
• If we know code-1 and code-2 don't interfere, just append code• Update merged "needs" and "modifies" data for combined sequence
modifies-1needs-1code-1
modifies-2needs-2code-2
modifies-1UNION
modifies-2
needs-1UNION
(needs-2 –modifies-1)
code-1
code-2
(append-instruction-sequences seq-1 seq-2)
28
Preserving Needed Registers
• A "smart" append of two code sequences which "preserves" registers• save any of specified registers if (and only if) seq-1 clobbers them
modifies-1needs-1code-1
modifies-2needs-2code-2
(preserving regs seq-1 seq-2)save regs(needs-2
ANDmod-1)
modifies-1UNION
modifies-2
needs-1UNION
(needs-2 –modifies-1)
code-1
restore
code-2
29
Now Understand Compile of Application
[(save continue)][(save env)]<evaluate operator; result in proc>[(restore env)][(save proc)]<evaluate operands; result in argl>[(restore proc)][(restore continue)]<apply procedure in proc to args in argl, and link>
• Optimization: constant '(10) directly into argl• Optimization?: replace whole thing with value 10
MachineInstructionSequence
decompile
Schemeexpression
(+ 10)
31
Summary
• Compilation vs. Interpretation• Interpretation: evaluate expressions at run-time • Compilation: analyze program code and generate
register machine instructions for later execution
• Some ideas in compilation – towards efficient code• Templates, targets & linkages• Strategies for register management• Many further optimizations possible!
32
May 3, 2000 Recitation Problem 6.001
• Decompile the following register code (what expression was compiled to produce these machine instructions)?