program execution execution models
play

Program Execution Execution Models 1 How are Programs Executed? - PowerPoint PPT Presentation

Program Execution Execution Models 1 How are Programs Executed? Ultimately, the instructions of a program run on the hardware foo.c0 Source program Processor chip o But the hardware does not understand C0 Two main ways to bridge the


  1. Program Execution

  2. Execution Models 1

  3. How are Programs Executed?  Ultimately, the instructions of a program run on the hardware foo.c0 Source program Processor chip o But the hardware does not understand C0  Two main ways to bridge the gap o through a compiler o through an interpreter 2

  4. Compilation  A compiler translates the source program into machine code o an equivalent program in the language that the processor understands and can execute directly  with the help of the OS foo.c0 a.out cc0 Machine code In reality, relocatable o The compiler itself is a program object code in machine code  when we execute it 3

  5. Interpreters  An interpreter reads each line in the source program and simulates it on the hardware #use <conio> int main() { int *p = alloc(int); coin foo.c0 *p = 42; return 0; } o The interpreter itself is a program in machine code  when we execute it o The interpreter acts like a virtual processor for the source language 4

  6. Compilation cc0 foo.c0 a.out  To run a program, all we need is the executable  on the same hardware and with the same OS o distribute the executable, not the source program  The (executable) code runs very fast o The compiler can perform lots of optimizations  Recompiling a large program takes time  Running a program on new hardware requires a new compiler o Writing a compiler is hard if we want the code to be fast  Languages that are typically compiled: o languages where performance is paramount C, … 5

  7. #use <conio> int main() { Interpretation int *p = alloc(int); coin foo.c0 *p = 42; return 0; }  To run a program, we need the source code and the interpreter  Each source instruction is simulated o this slows down execution o but the instructions can easily be screened for safety  Running a program on new hardware requires a new interpreter  Languages that are typically interpreted: o Shell scripts, make, … o languages used to write small programs where performance is not critical 6

  8. Compilation vs. Interpretation Compilation Interpretation • Code is very fast • Instructions can be screened Pro • Just executable required to run • Can be use interactively • Lengthy recompilation • Interpreter and source code • No safety checks Cons are needed for running • Not portable • Execution is slower 7

  9. The Best of Both Worlds 1. Compile the high-level source program to a lower level intermediate representation 2. Interpret the intermediate representation o This interpreter is called a virtual machine (VM) C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00 Virtual machine  This is called two-stage execution 8

  10. Two-stage Execution C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00  We gain benefits if the intermediate representation language is much simpler than the source language o the VM can be lightweight  very little simulation overhead o the compiler can perform complex optimizations  An intermediate language where each instruction fits in one byte is called a bytecode 9

  11. Two-stage Execution C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00  To run program, all we need is the bytecode and the VM  To run a program on new hardware we need o a new VM  easy to implement because the compiler does the heavy lifting o We can compile source program on different hardware, or o if the compiler is written in the source language, Write the compiler once and for all it can compile itself to bytecode and then run on the new VM Chicken and egg problem? Solved through bootstrapping 10

  12. Two-stage Execution C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00  Most modern languages use this two-stage approach o a Python program is first compiled to Python bytecode and then executed in the Python VM A data structure in memory o PHP, Javascript and many others are compiled to a common bytecode called the LLVM IR and then executed in the LLVM  Implementations of gcc based on Clang do that too 11

  13. Two-stage Execution C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00  The first mainstream language to use this two-stage approach was Pascal in 1970 o the goal was portability  have programs run in a uniform way across hardware  have an efficient way to get them running on new hardware 12

  14. Two-stage Execution C0 C0 FF EE 00 13 00 00 00 00 00 01 IR foo.c0 compiler 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C interpreter B0 00 00  The language that popularized it was Java in 1995  the IR language is called Java bytecode The contents of a .class file  the virtual machine is called the JVM o the goal was supporting mobile code on the nascent Web  a browser downloaded an applet and ran it  the bytecode was compact to minimize download time and cost  the JVM ran it (relatively) fast  the bytecode was untrusted  it was typechecked for statically unsafe operations Mainly security concerns  it was screened at run-time for unsafe operations 13

  15. C0 Execution Models 14

  16. Compiling a C0 Program with cc0  Under the hood, cc0 translates a C0 program to C and then runs gcc to compile it cc0 C0 foo.c0 foo.c gcc a.out translator To view this file, run # cc0 – s foo.c0  Why? o Writing a C0-to-C translator is relatively easy  the most complicated part is dealing with C’s undefined behaviors o The resulting executable is extremely fast  the gcc compiler is really good o This makes cc0 very portable  there is a gcc compiler for almost every hardware 15

  17. Compiling a C0 Program without cc0  CMU’s compiler course (15 -441) teaches how to write a standalone compiler for C0 foo.c0 a.out 15-441 compiler Machine code 16

  18. Interpreting a C0 Program in coin  Under the hood, coin compiles a C0 program to a bytecode data structure in memory and then runs a virtual machine coin C0 C0 FF EE 00 13 foo.c0 compiler IR 00 00 00 00 00 01 VM 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C B0 00 00 Data structure in memory  A web-based variant of coin is under development 17

  19. Two-stage Execution of a C0 Program  A C0 program can be compiled to C0VM bytecode with Linux Terminal This produces the C0VM # cc0 -b foo.c0 bytecode file foo.bc0  The bytecode file is then executed using the C0 virtual machine Linux Terminal This runs foo.bc0 # c0vm foo.bc0 in the C0VM C0 C0 FF EE 00 13 00 00 00 00 00 01 foo.c0 cc0 -b foo.bc0 C0VM 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C B0 00 00 18

  20. Two-stage Execution of a C0 Program C0 C0 FF EE 00 13 00 00 00 00 00 01 foo.c0 cc0 -b foo.bc0 C0VM 00 00 00 00 00 0C 10 03 10 04 60 10 05 68 10 02 6C B0 00 00  Compiling to C0VM bytecode takes some effort … … but implementing the C0VM is relatively easy  We will now examine what this involves o understand the structure of the C0VM bytecode o describe how to execute C0VM bytecode instructions o outline what it takes to implement the C0VM 19

  21. C0 Bytecode 20

  22. Compiling a Simple C0 Program  Consider this C0 program  in file ex1.c0 int main() { return (3 + 4) * 5 / 2; }  We compile it to bytecode with Linux Terminal # cc0 -b ex1.c0 If we had contracts, we also could pass the -d flag  Let’s look at the bytecode file ex1.bc0 21

  23. int main() { return (3 + 4) * 5 / 2; } A C0VM Bytecode File  This is text file C0 C0 FF EE # magic number 00 13 # version 9, arch = 1 (64 bits) o This is because C0VM is 00 00 # int pool count # int pool pedagogical architecture 00 00 # string pool total size # string pool to learn how virtual machines work 00 01 # function count # function_pool o An actual bytecode file would be #<main> raw binary 00 00 # number of arguments = 0 00 00 # number of local variables = 0 That’s what a Java 00 0C # code length = 12 bytes .class file is 10 03 # bipush 3 # 3 10 04 # bipush 4 # 4 60 # iadd # (3 + 4) 10 05 # bipush 5 # 5 68 # imul # ((3 + 4) * 5)  It would be easy to produce binary 10 02 # bipush 2 # 2 6C # idiv # (((3 + 4) * 5) / 2) instead B0 # return # 00 00 # native count # native pool 22

  24. int main() { return (3 + 4) * 5 / 2; } A C0VM Bytecode File  The (ASCII representation of the) C0 C0 FF EE # magic number 00 13 # version 9, arch = 1 (64 bits) bytes in hexadecimal are on the 00 00 # int pool count left # int pool  two hex digits represent 1 byte 00 00 # string pool total size o Everything after a # is a comment # string pool o Spaces and new lines are for 00 01 # function count # function_pool readability #<main> 00 00 # number of arguments = 0  The actual bytecode is 00 00 # number of local variables = 0 00 0C # code length = 12 bytes 10 03 # bipush 3 # 3 10 04 # bipush 4 # 4 C0C0FFEE001300000000000100000 60 # iadd # (3 + 4) 000000C100310046010056810026C 10 05 # bipush 5 # 5 68 # imul # ((3 + 4) * 5) B00000 10 02 # bipush 2 # 2 6C # idiv # (((3 + 4) * 5) / 2) B0 # return #  as a bit sequence 00 00 # native count # native pool 23

Recommend


More recommend