chapter 3
play

Chapter 3: Programming CS105: Great Insights in Computer Science - PowerPoint PPT Presentation

Chapter 3: Programming CS105: Great Insights in Computer Science Compiler An interpreter takes a program as input and makes it happen. A compiler takes a program as input and creates a machine-language program as output. A


  1. Chapter 3: Programming CS105: Great Insights in Computer Science

  2. Compiler • An interpreter takes a program as input and makes it happen. • A compiler takes a program as input and creates a machine-language program as output. • A program that converts a program into a program— twisted , but useful!

  3. Game Plan • We’ll develop two schemes that compute the value of the expression. ‣ One leaves the final value in “acc”. ‣ The other leaves it in a variable in memory. • Both are given a list of variables they can use for temporary storage. • Mutually recursive (oooh).

  4. Code Generation: Var • Expression: A answer in acc answer in P acc = A acc = A P = acc A

  5. Code Generation: not • Expression: not EXPR answer in acc (temp N) answer in P generate code for generate code for EXPR, answer in N EXPR, answer in acc acc = not N P = not acc not ET • Note: We keep a pool of temporary variables to use as needed (not just N).

  6. Code Generation: or • Expression: (EXPR 1 or EXPR 2 ) answer in acc (temp N) answer in P generate code for generate code for EXPR 1 , answer in N EXPR 1 , answer in P generate code for generate code for EXPR 2 , answer in acc EXPR 2 , answer in acc acc = acc or N P = acc or P or • Note: “and” is handled the same ET 1 ET 2 way.

  7. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  8. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  9. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  10. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  11. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  12. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  13. Example Expression • Expression: not A or B answer in acc answer in P generate code for generate code for not A, answer in N not A, answer in P generate code for generate code for A, answer in acc A, answer in acc acc = A acc = A or N = not acc P = not acc generate code for generate code for B, answer in acc B, answer in acc not B acc = B acc = B acc = acc or N P = acc or P A

  14. Assembler • An assembler handles the last little step of translating the series of instructions to a series of numbers. answer in acc answer in P acc = A 32 acc = A 32 N = not acc 125 P = not acc 127 acc = B 33 acc = B 33 acc = acc or N 13 P = acc or P 79

  15. Inefficiency • (not A or B) • Automatically generated machine code: answer in acc acc = A 32 N = not acc 125 acc = B 33 acc = acc or N 13 • By hand: answer in acc acc = not A 48 acc = acc or B 1 • Often more than one way to do it!

  16. Optimizations • Many ways of speeding up compiled code have been developed. • Want to minimize steps, temporary variables. • I’ll describe two important ones: ‣ Shared subexpressions ‣ Logical equivalence

  17. Automatic Code (13 inst.) ‣ • E = ((A and B) and C) or acc = (A and B) and D ((A and B) and D) - N = A and B ‣ E = (A and B) and C - acc = A - E = A and B - N = acc - acc = A - acc = B - E = acc - N = acc and N - acc = B - acc = D - E = acc and E - acc = acc and N - acc = C ‣ E = acc or E - E = acc and E • 1 temps, 13 instructions

  18. Shared Subexpression • E=((AandB)andC)or((AandB)andD) - E = acc ‣ N = A and B - acc = C - N = A - E = E and acc - acc = A - acc = N and D - N = acc - acc = N - acc = B - O = acc - N = N and acc - acc = D ‣ E = (N and C) or (N and D) - acc = acc and O - E = N and C - E = E or acc - acc = N • 2 temps, 13 instructions

  19. Logical Equivalence • E = ((A and B) and C) or - N = acc ((A and B) and D) - acc = D • E = (A and B) and (C or D) - acc = acc or N - E = A and B - E = E and acc - acc = A • 1 temps, 9 instructions - E = acc - acc = B - E = E and acc - acc = C or D - acc = C

  20. A Compiler • A program that translates computer programs that people write into a machine language instructions for the computer to execute. • (Adapted from notes by Barbara Ryder.)

  21. Interpreters • Compiler translates program to machine lang. • Interpreter translates statements by executing equivalent commands ‣ No real translation step • Interpretation requires programming language have a defined meaning for its statements--- semantics ‣ Sometimes defined mathematically, sometimes in English.

  22. Recap: Reduction Level Examples Alternatives graphics, animation, networking, security, software libraries robotics mathematics C, Java, C++, Logo, LISP, high-level language Python Fortran, ML machine language ML 3 x86, CARDIAC, Z80 logic gates equal, ifthenelse, add memlookup, memwrite basic logic gates and, or, not nor, nand, xor 0,1 via high/low water pressure, physical bits voltage kinetic energy

  23. Does It End There? • Of course not. • We can continue to build sophisticated programs out of simpler programs. • The idea of subroutines (procedures, functions) makes this work.

  24. Subroutines • A lot of research in computer science is about designing and creating just the right set of subroutines, sometimes called libraries . • You don’t have enough background yet to weigh in on these problems. • But, there is an analogous set of problems where you are already an expert...

  25. Sing-A-Long Programs Gilligan’s Island Theme Just sit right back and you'll hear a tale, a tale of a fateful trip. That started from this tropic port, aboard this tiny ship. The mate was a mighty sailin' man, the skipper brave and sure. Five passengers set sail that day, for a three hour tour, a three hour tour… The weather started getting rough, the tiny ship was tossed. If not for the courage of the fearless crew, the Minnow would be lost; the Minnow would be lost. The ship took ground on the shore of this uncharted desert isle, with Gilligan, the Skipper too, the Millionaire, and his Wife, the Movie Star, the Professor and Mary Ann, here on Gilligan's Isle.

  26. Scratch Code

  27. Chorus Structure Clementine Ev'ry morning just at nine, I forgot my Clementine. Hit her foot against a splinter, Fell into the foaming brine. Oh my darling, oh my darling, In a cavern, in a canyon, Oh my darling, Clementine! Excavating for a mine, Oh my darling, oh my darling, Thou art lost and gone forever Dwelt a miner forty niner, Oh my darling, Clementine! Dreadful sorry, Clementine. And his daughter Clementine. Thou art lost and gone forever Dreadful sorry, Clementine. Oh my darling, oh my darling, Oh my darling, Clementine! Ruby lips above the water, Thou art lost and gone forever Blowing bubbles, soft and fine, Dreadful sorry, Clementine. But, alas, I was no swimmer, So I lost my Clementine. Light she was and like a fairy, And her shoes were number nine, Oh my darling, oh my darling, Herring boxes, without topses, Oh my darling, Clementine! Sandals were for Clementine. Thou art lost and gone forever Dreadful sorry, Clementine. Oh my darling, oh my darling, Oh my darling, Clementine! Thou art lost and gone forever Dreadful sorry, Clementine How I missed her! How I missed her, How I missed my Clementine, Drove she ducklings to the water But I kissed her little sister,

  28. Scratch Code

  29. Scratch Code

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend