Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/10/2016\course\cpeg323-07F\Topic2c-323.ppt1 Topic2c High-Level languages and System Software (Toolchain) Introduction to Computer Systems Engineering.

Similar presentations


Presentation on theme: "1/10/2016\course\cpeg323-07F\Topic2c-323.ppt1 Topic2c High-Level languages and System Software (Toolchain) Introduction to Computer Systems Engineering."— Presentation transcript:

1 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt1 Topic2c High-Level languages and System Software (Toolchain) Introduction to Computer Systems Engineering (CPEG 323)

2 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt2 Reading List Slides: Topic2c Operating System and Compiler Books

3 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt3 Tool Chain toolchain A collection of system softwares used to develop for a particular hardware target If you designed a new processor, what is the basic system software tool set you need?

4 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt4 C program Compiler Assembly language program Assembler Object: Machine language module Object: Library routine (machine language) Linker Executable: Machine language program Loader Memory A Typical Toolchain and its Translation Hierarchy Toolchain Debugger Utility tools

5 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt5 Tool Chain Two good examples SimpleScalar www.simplescalar.com GNUPro www.intel.comwww.intel.com (Search GNUPro)

6 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt6 Tool Chain Basic Set: Compilers: C, C++, Fortran, and etc. Binary utilities: assembler, linker, objdump, ar, nm Debugger Simulator (functional / cycle-accurate) Others: performance monitor (VTune of Intel)

7 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt7 Tool Chain gcc –v –O0 –o foo foo.c (old Step 0: cpp) foo.i Step 1: cc1 - compiler foo.s Step 2: as - assembler foo.o Step 3: ld - linker foo

8 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt8 Tool Chain - Preprocessor Functionality Header files Definitions Conditional compilation Pragma(Preprocessor Directives ) Delete the comments

9 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt9 Tool Chain - Compiler Transform a program from high level language to assembly language (or machine language) Optimizations

10 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt10 Parameter Passing Caller save. The calling procedure (caller) is responsible for saving and restoring any registers that must be preserved across the call. The called procedure (callee) can then modify any register without constraint. Callee save. The callee is responsible for saving and restoring any registers that it might use. The caller uses registers without worrying about restoring them after a call.

11 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt11 Saving Registers If you call a function, whatever you have in $s0 to $s7 is guaranteed to be there when the function gets back to you But registers $t0 - $t9 are fair game to be reused by the function What are the alternatives? - Save nothing? - Save everything? Why caller/callee save ?

12 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt12 Why caller save / callee save? If all caller save ? Even callee doesn’t kill any of the saved registers – waste of cycles and memory resource If all callee save ? Callee has to save all the register (which will be used by callee), even caller doesn’t use them

13 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt13 Caller save Add $10, $11,$12 Save $10, $12, $13 Jal B Restore $10, $12, $13 Sub $11, $2, $12 Mul $12, $10, $13 Add $2, $4, $5 Br $31 Function A Function B How to save ? - save to stack sw $10, 20(sp)

14 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt14 Callee Save Add $10, $11,$12 Jal B Sub $11, $2, $12 Mul $12, $10, $13 Save $10, $11, $12 (if they are used in B) Lw $10, 4(sp) Add $11, $8, $9 Sub $12, $11, $10 Mul $2, $12, $11 Restore $10, $11, $12 Br $31 Function A Function B

15 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt15 Caller Save / Callee Save When to use caller save register ? TEMPORARY VARIABLE Also called Scratch Register When to use callee save register ? GLOBAL VARIABLE

16 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt16 Tool Chain - Assembler Transform assembly code into binary (machine code)

17 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt17 Tool Chain - Linker Linking – resolve symbols Relocation – assign memory address

18 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt18 Tool Chain - loader Cannot see by user Done by Shell and OS kernel

19 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt19 Tool Chain – Library Libc/Libm

20 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt20 Tool Chain Objdump See the memory layout and sections Symbol table Disassembly code Relocation information

21 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt21 C program Compiler Assembly language program Assembler Object: Machine language module Object: Library routine (machine language) Linker Executable: Machine language program Loader Memory Creating an Executable File(User view)

22 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt22 Compiling/Assembling a Program Code converted from high-level to machine language (binary) Each source file converted to a separate “object file” - in Unix, object files have extension.o - This is not executable (yet)! - Intended to be combined with other modules, not stand- alone - May not have everything we need (e.g., a main () function) Functions not assigned specific locations in memory Each function given a “relocation table” - This table tells exactly which addresses need to be resolved

23 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt23 Relocation Table Suppose function f() in module a.c calls g() in b.c a.c should declare g with extern (directly or in.h file) Relocation table in a.o says something like: ”the jal at the 52 nd instruction in f calls g, but I don’t know where g is.” Relocation table in b.o says something like: “I have a function g, which starts at location 628 in my file.”

24 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt24 Linking a program Linker combines one or more object files into a proper executable Linker determines which functions needed, discards the rest All needed functions put one after the other in text segment Linker resolves all labels; for instance - Function f() in a.o calls g() in b.o - Linker knows where it put g(), so it fixes the jal in f() Linker includes extra code: - Initialization code before call to main() - “Cleanup” code after main() returns

25 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt25 Loading A program that links without an error can be run. Before being run, the program resides in a file on secondary storage, such as a disk. On Unix system, the operating system kernel brings a program into memory and starts running.

26 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt26 Libraries Libraries contain functions intended to be shared & reused, e.g., - C library: printf(), malloc(), strcmp(), sin(), cos() - STL (Standard Template Library) in C++ - Big software projects may make their own libraries Static libraries (*.a in Unix) made part of the executable by linker Dynamic libraries (*.so in Unix, *.dll in Windows) combined at runtime - Executable still has relocation table of unresolved function calls - Loader does the final resolution when you execute the program

27 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt27 Dynamic vs. Static Library Dynamic library: processes share one copy of the code Static library: each process has its own copy of the code Why?

28 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt28 Dynamic Shared Library ? Also called dynamic linked library Call printf Program AProgram B Printf: DSO( dynamic shared object )Table DSO( dynamic shared object )Table

29 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt29 Static Library Call printf Program A Program B Printf:

30 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt30 Comparison Dynamic Less memory space Less disk space Most of the case: Slower Static More memory size More disk size Most of the case: faster

31 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt31 Debugger Instruction level debugger Source level debugger Major techniques Ptrace (POSIX API. on Linux/Unix system) Embedded or raw machine  Software trap  Single step mode

32 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt32 Debugger “Source-level” debugger lets you step through your source code Requires extra information attached to executable - Location and type of every function and variable - First instruction address corresponding to each line of source Usually requires extra switches to compiler and linker, e.g., -g Two popular graphical debuggers are ddd and xxgdb (on ECE/CIS machines)

33 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt33 The operating system performs the following steps: 1.Reads the executable file’s header to determine the size of the text and data segments. 2.“Establish” a new address space (e.g. via the creation of a new page table) for the program. This address space is large enough to hold the text and data segments, along with a stack segment 3.Copies instructions and data from the executable file into the new address space Run a Executable File (OS View)

34 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt34 4.Copies arguments passed to the program onto the stack. 5.Initializes the machine registers. In general, most registers are cleared but the stack pointer must be assigned the address of the first free stack location 6.Jumps to a start-up routine that copies the program’s arguments from the stack to registers and calls the program’s main routine. If the main routine returns, the start-up routine terminates the program with the exit system call. (cont’d)

35 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt35 Typical Layout of an Executable File stack Dynamic data Reserved Text Static data $sp 7fff ffff hex $gp 1000 8000 hex 1000 8000 hex pc 0040 0000 hex (From Patterson and Hennessy, p. 152; COPYRIGHT 1988 MORGAN KAUFMANN PUBLISHERS, INC. ALL RRIGHTS RESERVED)

36 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt36 The Role of the OS Kernel The OS “kernel” performs the following essential functions: Manages resources (memory, disks, I/O) – mostly via “drivers” Switches between users (in a multi-user system such as copland) Provides convenient functions for applications to access resources Protects users from one another Provides essential “glue”, e.g., support for loaders For this to work efficiently, the CPU must have some support for the kernel.

37 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt37 Processor Support for the OS kernel Most processors have at least 2 distinct levels or “modes”: - “Supervisor” (or “privileged” or “kernel”) mode - “User” level (including “root” or “administrator”) Lower levels can’t do some things, e.g., access the disk drive CPU boots in kernel mode; drops to user mode to run user code Early micros (such as 8086) lacked such modes

38 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt38 Traps So once we’re in user mode, how do we get back to the privileged mode? Through “traps” – exceptional or unusual conditions requiring intervention by the kernel: Hardware error (divide by 0 or access to illegal memory address) Hardware “interrupt” (Ethernet card got data; mouse clicked) Clock signal telling multi-user OS to switch to another user “Software trap” when user code requests something from kernel PC reaches value stored in special “breakpoint” register

39 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt39 Trap Handlers When a trap occurs, the CPU: Sets bits in special “status” reg., indicating the cause of the trap Switches to privileged mode Jumps to a “trap handler” (installed at boot time) at fixed location - Handler reads status bits and takes appropriate action - Return address saved, like jal instruction * When kernel is done, a special instruction return to the user code, dropping into user mode automatically

40 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt40 Software Traps To do just about anything on the system involving shared resources (such as write to a file), the user code must ask the kernel to do it! User code gets access to the kernel through “trap” instructions “System calls” provided for operations such as writing files A function call to a system call converted to a software trap Args passed in the usual way (e.g., $a0-$a3 in MIPS) In MIPS, use the “syscall” instruction - No operands in assemble-language instruction - Specify which system call you want by putting a value in $v0

41 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt41 System Calls POSIX standard defines system calls and their numbers For instance, call no. 4 is the write() function: #include ssize_t write(int fildes, cost void *buf, size_t nbyte); Every open file is identified by a unique “file descriptor” (int) This function writes nbyte bytes, starting at address buf, to the file

42 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt42 Example: Call to printf() User code a.c calls printf (“Answer is %d\n”, i); printf() declared as an extern function in stdio.h Compiler generates a.o with printf unresolved in relocation table Data segment of a.o has string “Answer is %dl_” (NUL at end) - 14 bytes, with local label (e.g., L314) in relocation table Reference resolved when linked with libc (C library): - By linker if statically (e.g., -Bstatic in Sun CC) - By loader if dynamically

43 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt43 Calling printf() Program sets args to printf (L314 and i)’ does jal printf printf (still in user mode) does the following: - Creates new stack frame (as any non-leaf function should) - Processes args; makes new string “Answer is 42l” in heap - Creates args to write() function: Constant 1 in $a0 (file descriptor 1 is stdout) Address of heap string in $a1 Constant 13 in $a2 - Puts constant 4 in $v0 and does a syscall instruction

44 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt44 Processing the Trap The CPU executes the syscall (trap) instruction: - Switches to privileged mode - Sets bits in status regs indicating trap caused by syscall - Jumps to trap handler Trap handler checks status bits; sees trap came from syscall Checks call # in $v0; fetches 4 th entry in function table and jumps System call transfers 13 bytes to low-level driver - Driver writes them to graphics display (if normal stdout)

45 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt45 Toolchain Review Caller save register. The registers that the calling procedure (caller) is responsible for saving and restoring across the call. The called procedure (callee) can then modify the registers without constraint. Callee save register. The registers that the callee is responsible for saving and restoring if it might use. The caller uses the registers without worrying about restoring them after a call. Caller save /Callee save register

46 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt46 Program Translates (Summary) a.c Int I; … Printf(“Answer is %d”, i) … compile a.s.text … Parameter pass Jal printf ….data assembly a.o 323: Parameter pass 444: Jal reloc add. Ref: 444 a.o- relocation table

47 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt47 Program Translates(cont.) a.o 323: Parameter pass 444: Jal reloc add. ret 555: Create stack Process args $a0 <- file ID $a1 <- adds. Of heap $a2 <- length of string $v0 <- 4 (write) Syscall ret printf.o Ref: 444 Def: 555 a.o- relocation table printf.o- relocation table L323: Parameter pass L444: Jal L555 L555: Create stack Process args $a0 <- 1 $a1 <- adds. Of heap $a2 <- 13 $v0 <- 4 Syscall ret linker Executable file Start-up routine __main: jal main jal exit

48 1/10/2016\course\cpeg323-07F\Topic2c-323.ppt48 Program Translates (Cont.) 323: Parameter pass 444: J reloc 555 555: Create stack Process args $a0 <- 1 $a1 <- adds. Of heap $a2 <- 13 $v0 <- 4 Syscall ret. Executable file (software trap) user mode privileged mode Set status register Jal trap(4) handler trap(4) handler what trap -- syscall $v0 ? ------- 4 Jal 4 th function driver Transfer 13 bytes to graphic display 4th function driver __main: jal main jal exit main:


Download ppt "1/10/2016\course\cpeg323-07F\Topic2c-323.ppt1 Topic2c High-Level languages and System Software (Toolchain) Introduction to Computer Systems Engineering."

Similar presentations


Ads by Google