CS406: Compilers Spring 2020 Week 7: (IR) Code Generation - For - - PowerPoint PPT Presentation

cs406 compilers
SMART_READER_LITE
LIVE PREVIEW

CS406: Compilers Spring 2020 Week 7: (IR) Code Generation - For - - PowerPoint PPT Presentation

CS406: Compilers Spring 2020 Week 7: (IR) Code Generation - For Loops, Switch Statements, and Functions (Slides courtesy: Prof. Milind Kulkarni) 1 2 To handle a continue statement 3 4 5 6 7 8 9 Functions 10 11 Different Kinds of


slide-1
SLIDE 1

1

CS406: Compilers

Spring 2020 Week 7: (IR) Code Generation - For Loops, Switch Statements, and Functions (Slides courtesy: Prof. Milind Kulkarni)

slide-2
SLIDE 2

2

slide-3
SLIDE 3

3

To handle a continue statement

slide-4
SLIDE 4

4

slide-5
SLIDE 5

5

slide-6
SLIDE 6

6

slide-7
SLIDE 7

7

slide-8
SLIDE 8

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

10

Functions

slide-11
SLIDE 11

11

slide-12
SLIDE 12

Different Kinds of Parameters

  • Value
  • Reference
  • Result
  • Value-Reference
  • Read-only
  • Call-by-Name

12

slide-13
SLIDE 13

13

Advantage: ‘side-effect’ free – caller can be sure that the argument is not modified by the callee Disadvantage: Not efficient for larger sized arguments.

slide-14
SLIDE 14

14

slide-15
SLIDE 15

15

Advantage: Efficiency – for larger sized arguments Disadvantage: results in clumsy code at times (e.g. check for null pointers)

slide-16
SLIDE 16

16

slide-17
SLIDE 17

Result Parameters

  • To capture the return value of a function
  • Copied at the end of function into arguments of

the caller

  • E.g. output ports in Verilog module definitions

17

slide-18
SLIDE 18

Result Parameters

int x = 1 void main () { foo(x, x); print(x); } void foo(int y, result int z) { y = 2; z = 3; print(x); }

18

  • What do the following

statements print?

  • Answer:

print(x); //prints 3 print(x) //prints 1

slide-19
SLIDE 19

Value-Result Parameters

  • “Copy-in copy-out”
  • Evaluate argument expression, copy to parameters
  • After subroutine is done, copy values of parameters back

into arguments

  • Results are often similar to pass-by-reference, but there

are some subtle situations where they are different

19

slide-20
SLIDE 20

Value-Result Parameters

int x = 1 void main () { foo(x, x); print(x); } void foo(int y, value result int z) { y = 2; z = 3; print(x); }

20

  • What do the following

statements print?

  • Answer:

print(x); //prints 3 print(x) //prints 1

slide-21
SLIDE 21

Read-only Parameters

  • Used when callee will not change value of parameters
  • Read-only restriction must be enforced by compiler
  • E.g. const parameter in C/C++
  • Enforcing becomes tricky when in the presence of

aliasing and control flow. E.g.

21

void foo(readonly int x, int y) { int * p; if (...) p = &x else p = &y *p = 4 }

slide-22
SLIDE 22

Call-by-name Parameters

  • The arguments are passed to the function before

evaluation

– Usually, we evaluate the arguments before passing them

  • Not used in many languages, but Haskell uses a variant

22

int x = 1 void main () { foo(x+2); print(x); } void foo(int y) { z = y + 3; //expands to z = x + 2 + 3 print(z); }

slide-23
SLIDE 23

Call-by-name Parameters

  • Why is this useful?

– E.g. to analyze certain properties of a program/function – termination – Even if bar has an infinite loop, the program terminates.

23

void main () { foo(bar()); } void foo(int y) { z = 3; if(z > 3) z = y + z; }

slide-24
SLIDE 24

24

slide-25
SLIDE 25

25

slide-26
SLIDE 26

26

slide-27
SLIDE 27

27

slide-28
SLIDE 28

28

  • Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D.Ullman:

Compilers: Principles, Techniques, and Tools, 2/E, AddisonWesley 2007

– Sections: TODO

  • Fisher and LeBlanc: Crafting a Compiler with C

– Sections: TODO

Suggested Reading