cis 330 c++ and unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ......

35
CIS 330 C++ and Unix Lecture 6 Compilers

Upload: others

Post on 26-Mar-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

CIS 330 C++ and UnixLecture 6

Compilers

Page 2: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Quiz

� Question 1 – Which of the following methods is the correct way to declare a function pointer funcptr and assign the function double compute_average(double *y, int cnt) to it

a. double (funcptr) (double* y, int cnt); funcptr = &compute_average;

b. double (*funcptr) (double* x, int cnt); funcptr = &compute_average;

c. double (funcptr) (y, cnt); functpr = compute_average;

d. double (*funcptr) (double* y, int cnt); *funcptr = compute_average;

Page 3: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Quiz

� Question 2 – Which of the following is NOT correct?

a. We can pass a function pointer as a parameter to another function

b. A 2-D matrix stores all of its data consecutively in memory (just like a 1-D matrix)

c. In some cases, we can use a function that takes in a 2-D matrix as input (e.g., int** some_array) for processing a 1-D matrix (e.g., int* some_other_array)

d. We can create a 100 arrays using a single malloc()

Page 4: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Quiz

� Question 3 – What will be the output when the following code is executed?

a. 101

b. 102

c. some address

d. undefined behavior

int some_func(int* i) {

int j = *i;return j++;

}

int i = 101;some_func(&i);fprintf(stdout, "%d\n", i);

Page 5: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Quiz

� Question 4 – Which of the following statements is correct?

a. b and b_tmp have different data types, so it will not return a valid address to the main function (where my_malloc() was called)

b. Since malloc’d memory was assigned to a variable created inside of a function (i.e., b_tmp is on the stack), it will be lost when the function returns

c. It behaves like a smart malloc() function, where the return value from malloc() is checked to see if it’s valid

d. None of the above are true.

void my_malloc(double** b){

double* b_tmp;b_tmp = (double*) malloc(sizeof(double) * 128);assert(b_tmp);fprintf(stdout, "My address is %p\n", b_tmp);*b = b_tmp;

}

Page 6: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Homework 2

� Why was output parameter included?� It does not seem like it is needed for the function implementation� Yes, but what do you do if your code has to exit with EXIT_FAILURE. What happens to the memory that has been allocated already?

� You should free() the memory before you exit.� You will not be penalized (since I haven’t covered valgrind yet), but

something to keep in mind.

Page 7: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Homework 2

1. FILE* file1 = fopen(argv[1], "r");

2. FILE* file2 = fopen(argv[2], "r");

3. if(file1 == NULL || file2 == NULL) {

4. fprintf(stderr, "ERR: One or both of the input files do not exist\n");

5. free(input_one); free(input_two); free(output);

6. exit(EXIT_FAILURE);

7. }

8.for(int i = 0; i < num_ints; i++) {

9. if((fgets(line1, MAX_NUM_LENGTH, file1) != NULL) && (fgets(line2, MAX_NUM_LENGTH, file2) != NULL)) {

10. input_one[i] = atoi(line1);

11. input_two[i] = atoi(line2);

12. } else {

13. fprintf(stderr, "ERR: Error reading numbers\n");

14. free(input_one); free(input_two); free(output);

15. exit(EXIT_FAILURE);

16. }

17. }

18.}

Page 8: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Debugging � use printf

� The end

Page 9: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Debugging

� GNU debugger (gdb)

� Compile your code with –g and –W -Wall options

� Start gdb with `gdb ./a.out’

� gdb provides an interactive shell � get help by typing `help <command>’

Page 10: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Debugging

� (gdb) run <arguments>

� Runs to completion if there are no problems with your code

� (gdb) run

� Starting program: /home/users/jeec/lecture06/ex01/prog

� 9

� [Inferior 1 (process 10178) exited normally]

Page 11: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Debugging

� (gdb) run <arguments>

� Runs to completion if there are no problems with your code

� If there are problems, gdb takes control after it terminates and displays some useful information

� line number where it terminated� what type of problem (e.g., seg fault)� enclosing function � etc.

Page 12: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Debugging

� (gdb) a.out

� Starting program: /home/users/jeec/lecture07/a.out

� Program received signal SIGSEGV, Segmentation fault.

� 0x000055555555513d in add_numbers (

� a=<error reading variable: Cannot access memory at address 0x7fffff7fefec>, b=<error reading variable: Cannot access memory at address 0x7fffff7fefe8>)

� at add.c:4

� 4 {

Page 13: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Useful commands

� gdb allows you to step through the code and print the contents of the memory, variables, etc.

� (gdb) bt � backtrace – traces the steps to see what happened

� breakpoint� break <location>� Location could be function name, or line number (add.c:8)� You can back trace from the breakpoint� use `clear’ to clear all breakpoints

� step� step through your code, including function invocation

� next� step through your code, but not into other functions

� continue� resume execution after gdb pauses (e.g., at a breakpoint)

Page 14: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Useful commands

� print – print the content of variables

� watch – you can `watch’ a variable and gdb will tell you when it has been modified

� info <args/locals/reg> - print information about these resources

Page 15: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

valgrind

� Program execution monitoring framework

� memcheck� Use of uninitialized memory� Reading/writing to heap memory after it has been freed� Reading/writing to end of malloc space� Heap allocated memory leaks� Mismatched use of malloc and free� etc.

Page 16: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

valgrind

� #include <stdlib.h>

� int main(int argc, char** argv)

� {

� int x, y;

� if (x < 3)

� y = 4;

� else

� y = 5;

� return 0;

� }

Page 17: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

valgrind

� valgrind a.out

� ==16716== Memcheck, a memory error detector

� ==16716==

� ==16716== Conditional jump or move depends on uninitialised value(s)

� ==16716== at 0x109134: main (main.c:7)� ==16716==

� ==16716==

� ==16716== HEAP SUMMARY:

� ==16716== in use at exit: 0 bytes in 0 blocks

� ==16716== total heap usage: 0 allocs, 0 frees, 0 bytes allocated

� ==16716==� ==16716== All heap blocks were freed -- no leaks are possible

� ==16716==� ==16716== For counts of detected and suppressed errors, rerun with: -v

� ==16716== Use --track-origins=yes to see where uninitialised values come from

� ==16716== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Page 18: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Live Coding � Use gdb and valgrind

Page 19: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Modular Programming

� C is a functional language – modularize your code into functions!

� Reduces the amount of code you need to write, and makes debugging easier

Page 20: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Code Spanning Multiple Files

� You could write your entire program in a single, very large, .c file

� However, it’s better to separate your code into multiple files� Easier to keep track of your code� Easier to compile (more on this later)� Easier to collaborate

Page 21: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Code Spanning Multiple Files

� First file – contains the main function

� Second file(s)� .c file that contains the code� .h file that contains the function declaration (header file)� stdio.h, stdlib.h, and string.h are examples of header files� Interface to using the functions written in the .c file

� Third file(s), etc.

� Header files should be included carefully to avoid multiple inclusions

� use if-not-defined check if a header file is already included

Page 22: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Header File Example

arithmetic.h1. int add_two_numbers(int a, int b);

1. #include ”arithmetic.h”

2. int main()3. {

4. int a = 1;5. int b = 3;6. int c = add_two_numbers(a, b);

7. fprintf(stdout, "%d + %d = %d\n", a, b, c);8. return 0;

9. }

Page 23: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Examples of if-not-defined

1. #ifndef ARITHMETIC_H2. #define ARITHMETIC_H3. int add_two_numbers(int a, int b);4. #endif /* not defined ARITHMETIC_H */� The first time arithmetic.h is called, it defines the variable

ARITHMETIC_H and the regular definitions contained within arithmetic.h

� Next time ARITHMETIC.h is called (redundantly), ARITHMETIC_H will already be defined, and the content within the if-not-defined will be skipped

Page 24: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Compiling Multiple Files

� The simple method – just list all the files� e.g., > gcc arithmetic.c numbers.c� With this method, we have to compile both files every time we make

changes to one or the other – not much better than having one large file

� Compiling the code separately� Compile each .c file into object files, and then link them together

Page 25: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Linker

> gcc -c arithmetic.c> gcc -c numbers.c> gcc arithmetic.o numbers.o

� Why is this better?

Compile the code into object files

Link the object files together to create the executable

Page 26: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Linker

> gcc -c arithmetic.c> gcc -c numbers.c> gcc arithmetic.o numbers.o

� If we change one file (e.g., arithmetic.c), we only have to recompile arithmetic.c then re-link the object files

Compile the code into object files

Link the object files together to create the executable

Page 27: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Compiler Options

� -o -> specify the executable name� e.g., gcc -o run arithmetic.o numbers.o

� -Wall -> enable all compiler warning messages. � It is -W with all option� You can use -W to enable/disable specific warnings

� -O -> set the compiler optimization level� e.g., -O3

� -std -> sets the C standard to follow� e.g., -std=c11 (follow the C11 standard)

� -g -> enable debugging

Page 28: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Optimization Level

Option Optimization

-Oo Optimize for compile time (no optimization, default)

-O1 or -O Moderate optimization – optimizes reasonably well but does not degrade compilation time

-O2 Full optimization – highly optimized code and slowest compilation time

-O3 -O2 + aggressive subprogram inlining and vectorization

-Os Optimize for code size

-Ofast -O3 + non-accurate math calculation (floating point roundoff error)

Page 29: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Compiler Driver

� gcc is actually a compiler driver – it invokes several “tools” to accomplish the task of converting source code to executable code

� For example,

> gcc arithmetic.c numbers.cInvokes

1. Preprocessor

2. Compiler (cc1)

3. Assembler (as)

4. Linker (ld)

Page 30: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Preprocessor

� Macro processor that transforms your code:� Includes header files� Macro expansion� Removes comments� And more

� Output typically looks similar to the input (i.e., source code)

Page 31: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Compiler

� Compiler takes preprocessed C language file and generates assembly code

� cc1 contains the preprocessor and the compiler.� Compilation stages consist of

� Front end� Middle end� Back end

� Front end� Parses the source code to generate abstract syntax tree (AST)� Data structure that is the tree representation of the abstract syntactic

structure of the source code

� Middle end� Converts AST to different representations for optimization� Generates register-transfer language (RTL)� RTL is a hardware-specific representation that corresponds to some

abstract target architecture (with infinite number of registers)

� Back end� Generates assembly code for the target architecture

Page 32: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

AST

sub x(a,b,c) {i = a while (i < 20) { if odd(i) { i = i + b }

else { i = i + c print("in else")

} print("in loop")

} // end-while print(i)

}

Page 33: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

RTL

ADDL R0,R1

DECL R3

ADDL R5,@R6

ADDL (R3)+,R1

MOVL R5,-(SP)

MOVL 8(R0),R0

MOVAL 8(R0),R1

ADDL @4(R3),@0(R3)

Page 34: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Assembler � Converts assembly language to object code

� Object code is in binary (but readable with tools such as objdump)

Page 35: CIS 330 C++ and Unix · of a function (i.e., b_tmpis on the stack), it will be lost when the ... Macro processor that transforms your code: Includes header files Macro expansion Removes

Linker

� “Merges” object files into a single executable object file

� As part of the merging process, it resolves external references� e.g., you can compile your code using fprintf without knowing

exactly what fprintf is. � However, when you want to actually run this code, you must know

where this piece of code is located (i.e., in an external library).

� ”Relocates” symbols from their relative position in the object files to absolute position in the executable, and updates their references (i.e., use) to this new position

� It “copy & paste” the fprintf function from the original objective file to the executable