252-210: Compiler Design 1.1 Simple compiler model 1.2 - - PowerPoint PPT Presentation

252 210 compiler design 1 1 simple compiler model 1 2
SMART_READER_LITE
LIVE PREVIEW

252-210: Compiler Design 1.1 Simple compiler model 1.2 - - PowerPoint PPT Presentation

252-210: Compiler Design 1.1 Simple compiler model 1.2 Admin issues Thomas R. Gross Computer Science Department ETH Zurich, Switzerland 1.1


slide-1
SLIDE 1

252-­‑210: ¡Compiler ¡Design ¡ ¡ 1.1 ¡Simple ¡compiler ¡model ¡ 1.2 ¡Admin ¡issues ¡

Thomas ¡R. ¡Gross ¡ ¡ Computer ¡Science ¡Department ¡ ETH ¡Zurich, ¡Switzerland ¡

slide-2
SLIDE 2

1.1 ¡Simple ¡realisCc ¡compiler ¡model ¡

§ Simple: ¡can ¡be ¡handled ¡in ¡one ¡semester, ¡8 ¡credits ¡ § RealisCc: ¡experience ¡problems ¡encountered ¡by ¡real ¡ compilers ¡

§ Mirrors ¡structure ¡of ¡many ¡compilers ¡

2 ¡

slide-3
SLIDE 3
slide-4
SLIDE 4

Compiler ¡model ¡

5 ¡

§ CompilaCon ¡prior ¡to ¡execuCon ¡

§ AOT ¡“Ahead ¡of ¡(ExecuAon) ¡Time” ¡compilaAon ¡ § Commonly ¡used ¡for ¡languages ¡without ¡language-­‑specific ¡execuAon ¡ environments ¡ ¡(e.g., ¡C, ¡C++, ¡…) ¡

§ Other ¡model: ¡conCnuous ¡compilaCon ¡

§ JIT ¡“Just ¡in ¡Time” ¡compilaAon ¡ ¡ § Usually ¡op-miza-on ¡ ¡prior ¡to ¡execuAon ¡of ¡methods ¡that ¡are ¡invoked ¡ frequently ¡ § Commonly ¡used ¡with ¡language ¡virtual ¡machines ¡(e.g., ¡JVM) ¡

slide-5
SLIDE 5
slide-6
SLIDE 6

IR ¡– ¡intermediate ¡representaCon ¡ ¡

§ Compiler-­‑internal ¡representaCon ¡

§ E.g., ¡compiler ¡must ¡disAnguish ¡between ¡names ¡in ¡different ¡scopes ¡ § E.g., ¡many ¡programs ¡work ¡with ¡variables, ¡computers ¡work ¡with ¡ locaAons ¡

§ Must ¡express ¡all ¡language ¡constructs/concepts ¡ § Code ¡generator ¡must ¡map ¡IR ¡to ¡assembly ¡code ¡

§ Machine ¡code ¡another ¡opAon ¡ ¡

§ No ¡“best” ¡IR ¡– ¡all ¡are ¡compromises ¡ ¡ ¡

9 ¡

slide-7
SLIDE 7
slide-8
SLIDE 8

Compiler ¡design ¡ ¡

§ Overriding ¡concern ¡in ¡compiler ¡construcCon: ¡ § Correctness ¡

§ The ¡translated ¡program ¡PL2 ¡behaves ¡the ¡same ¡way ¡as ¡the ¡original ¡ program ¡PL1 ¡

§ For ¡correct ¡programs, ¡for ¡defined ¡inputs ¡ § Correct: ¡PL1 ¡obeys ¡the ¡rules ¡of ¡L1 ¡

§ For ¡incorrect ¡programs, ¡or ¡undefined/illegal ¡inputs, ¡PL2 ¡behaves ¡in ¡a ¡ defined ¡ ¡way ¡

§ Ideally ¡defined ¡by ¡L1, ¡someAmes ¡defined ¡by ¡L1 ¡and ¡L2 ¡resp. ¡the ¡target ¡machine ¡

13 ¡

slide-9
SLIDE 9

Checking ¡input ¡(PL1) ¡

§ Compiler ¡must ¡check ¡that ¡program ¡can ¡be ¡translated ¡

  • correctly. ¡

§ The ¡program ¡may ¡sAll ¡be ¡flawed ¡ § ExecuAon ¡behaves ¡as ¡expected ¡(or ¡specified) ¡

§ Issues ¡or ¡aspects ¡that ¡a ¡compiler ¡should ¡check: ¡

14 ¡

slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

Issues/ProperCes ¡

§ All ¡variables/methods/symbols ¡defined ¡ § All ¡variables ¡iniCalized ¡before ¡use ¡ § Program ¡has ¡no ¡syntax ¡errors ¡ § Defined ¡program ¡start ¡(main ¡method ¡if ¡appropriate) ¡ § ExcepCons ¡are ¡caught ¡ § No ¡null-­‑ptr ¡accesses ¡ § No ¡out-­‑of-­‑bound ¡accesses ¡for ¡arrays, ¡records/instances ¡ § Program ¡type ¡checks ¡

17 ¡

slide-14
SLIDE 14

§ No ¡unused ¡variables/fields ¡ § Return ¡statement ¡is ¡reachable ¡ § ExecuCon ¡reaches ¡end ¡of ¡program ¡ § All ¡funcCons ¡return ¡value ¡ § Rules ¡ ¡of ¡the ¡module ¡system ¡followed ¡

§ No ¡access ¡to ¡private/protected ¡members ¡unless ¡legal ¡

§ No ¡cycle ¡in ¡inheritance ¡graph ¡ § Interfaces/abstract ¡funcCons ¡implemented ¡ § Loops ¡terminate ¡ § Program ¡executes ¡within ¡a ¡given ¡Cme ¡ § Program ¡stays ¡within ¡power ¡budget ¡ § All ¡values ¡computed ¡can ¡be ¡represented ¡ ¡

18 ¡

slide-15
SLIDE 15
slide-16
SLIDE 16

252-­‑210: ¡Compiler ¡Design ¡ ¡ 2.0 ¡TranslaAon ¡of ¡assignment ¡statements ¡

Thomas ¡R. ¡Gross ¡ ¡ Computer ¡Science ¡Department ¡ ETH ¡Zurich, ¡Switzerland ¡

slide-17
SLIDE 17

2.0 ¡TranslaCon ¡of ¡assignment ¡statements ¡

§ Assignment ¡statements ¡(stmts) ¡are ¡found ¡in ¡many ¡ programming ¡languages ¡ § We ¡look ¡at ¡simple ¡assignment ¡stmts: ¡

§ A ¡= ¡B ¡+ ¡1 ¡ § Types ¡of ¡A, ¡B? ¡ ¡int ¡ ¡ ¡(32bit) ¡

§ As ¡in ¡C, ¡C++, ¡Java, ¡Pascal, ¡…. ¡

§ Only ¡scalar ¡enAAes ¡ § Only ¡32-­‑bit ¡integer ¡constants ¡

21 ¡

slide-18
SLIDE 18

One ¡boring ¡assignment ¡stmt ¡

§ Examples: ¡

int A, B, C; A = B + C; // “+” generic operator int A, B, C, D; A = B + C + D; int A, B, C; A = B + C + 2 + 4;

§ Simple. ¡ ¡ ¡ § Your ¡task: ¡given ¡a ¡single ¡assignment ¡stmt, ¡generate ¡code ¡for ¡ this ¡stmt. ¡

§ We ¡give ¡you ¡the ¡IR ¡for ¡the ¡stmt. ¡

22 ¡

slide-19
SLIDE 19

A ¡start ¡

§ Use ¡assignment ¡stmt ¡to ¡explore ¡(part ¡of) ¡the ¡IR ¡ § Can ¡you ¡generate ¡“opCmal” ¡code? ¡

§ What ¡is ¡opAmal? ¡ § Is ¡there ¡an ¡efficient ¡algorithm? ¡

§ Efficient: ¡guaranteed ¡to ¡terminate ¡before ¡the ¡deadline ¡

§ Easy ¡extension: ¡one ¡stmt ¡ ¡list ¡of ¡stmts ¡ ¡

§ Known ¡as ¡a ¡block ¡ ¡ § SoluAon ¡to ¡this ¡assignment ¡to ¡be ¡reused ¡later ¡(Homework ¡5, ¡November) ¡

23 ¡

slide-20
SLIDE 20

2.1 ¡Intermediate ¡representaCon ¡ ¡

§ There ¡exist ¡many ¡IRs ¡

§ Some ¡compilers ¡use ¡several ¡IRs ¡during ¡translaAon ¡

§ First ¡IR ¡to ¡represent ¡input ¡program ¡ § Analyze, ¡add ¡annotaAons ¡ § “Lower” ¡IR: ¡expose ¡more ¡details ¡

§ No ¡single ¡IR ¡is ¡best ¡

§ IR ¡for ¡this ¡course ¡is ¡tree-­‑based ¡

§ Easy ¡to ¡understand ¡ § Easy ¡to ¡extend: ¡tree ¡à ¡forest ¡ § Close ¡to ¡IR ¡used ¡in ¡real ¡compilers ¡

§ More ¡sophisCcated ¡IR ¡le_ ¡for ¡“Advanced ¡Compiler ¡Design” ¡

24 ¡

slide-21
SLIDE 21
slide-22
SLIDE 22

Approach ¡

§ Tree ¡describes ¡computaCon ¡to ¡be ¡done ¡ § Determines ¡“evaluaCon ¡order” ¡

§ First ¡ ¡(C ¡* ¡D) ¡ § Then ¡add ¡product ¡to ¡B ¡

§ Compile ¡one ¡tree ¡at ¡a ¡Cme ¡ § Code ¡generator ¡“visits” ¡a ¡node ¡

§ Bookkeeping ¡ § (maybe) ¡Generate ¡code ¡ § Visit ¡subnodes ¡ § Bookkeeping ¡ § (maybe) ¡Generate ¡code ¡

29 ¡

Some ¡texts ¡say ¡“Code ¡ generator ¡evaluates ¡a ¡ node” ¡

slide-23
SLIDE 23

Binary ¡trees ¡

§ IR ¡allows ¡only ¡binary ¡trees ¡

int A, B, C, D; A = B + C + D;

30 ¡

slide-24
SLIDE 24
slide-25
SLIDE 25

§ Tree ¡determines ¡evaluaCon ¡order ¡ § Compare ¡

Add B, C // B = B + C Add B, D // B = B + D § With ¡ Add B, D // B = B + D Add B, C // B = B + C

§ The ¡results ¡may ¡differ ¡

§ FloaAng ¡point ¡arithmeAc ¡is ¡not ¡associaAve ¡

34 ¡

slide-26
SLIDE 26

Non-­‑associaCve ¡arithmeCc ¡

§ (X ¡+fp ¡Y) ¡+fp ¡Z ¡ ¡is ¡possibly ¡different ¡from ¡X ¡+fp ¡(Y ¡+fp ¡Z) ¡ ¡ ¡

§ +fp ¡floaAng ¡point ¡arithmeAc ¡

35 ¡

slide-27
SLIDE 27

How ¡to ¡proceed? ¡

§ Who ¡should ¡decide ¡which ¡tree ¡to ¡build? ¡ § Programming ¡language ¡specificaCon ¡should ¡address ¡this ¡

  • issue. ¡

§ Maybe ¡allow ¡user ¡opAons ¡ ¡ § E.g., ¡icc ¡(Intel’s ¡C ¡compiler) ¡supports ¡ ¡

§ –fp-­‑model ¡precise ¡ § –fp-­‑model ¡source ¡ ¡

38 ¡

slide-28
SLIDE 28

252-­‑210: ¡Compiler ¡Design ¡ ¡ 1.2 ¡Admin ¡issues ¡ ¡

Thomas ¡R. ¡Gross ¡ ¡ Computer ¡Science ¡Department ¡ ETH ¡Zurich, ¡Switzerland ¡

slide-29
SLIDE 29

Topics ¡for ¡this ¡class ¡

§ Compiler ¡design: ¡structure ¡of ¡a ¡simple ¡compiler ¡ § So_ware ¡engineering: ¡ ¡

§ Design ¡of ¡a ¡non-­‑trivial ¡system ¡ § Solware ¡reuse ¡

§ Homework ¡(design ¡and ¡implementaCon ¡of ¡a ¡compiler) ¡core ¡ part ¡of ¡this ¡course ¡

40 ¡

slide-30
SLIDE 30

Recall ¡

§ Compiler ¡translates ¡program ¡wriden ¡in ¡L1 ¡(PL1) ¡into ¡program ¡ wriden ¡in ¡ ¡L2 ¡(PL2) ¡

§ ¡PL1 ¡à ¡PL2 ¡

§ Languages ¡

§ Source ¡language ¡L1 ¡ § Target ¡language ¡L2 ¡ § Host ¡language ¡P ¡ ¡

§ Want ¡to ¡use ¡tools, ¡reuse ¡framework: ¡

§ P: ¡Java ¡

41 ¡

slide-31
SLIDE 31

Homework ¡

§ Build ¡a ¡compiler ¡

§ 5 ¡(or ¡6) ¡easy ¡steps ¡

§ Teams ¡of ¡2 ¡persons ¡ § SCll ¡a ¡lot ¡of ¡work ¡

§ Use ¡compiler ¡framework ¡(provided ¡by ¡TAs) ¡

42 ¡

slide-32
SLIDE 32

Compiler ¡target ¡

§ Resource ¡issues ¡criCcal ¡in ¡compilaCon ¡ § Use ¡real ¡machine ¡

§ Real ¡machines ¡have ¡real ¡flaws ¡ § Only ¡paper ¡designs ¡are ¡perfect ¡ § Target ¡(language): ¡x86 ¡(32-­‑bit) ¡assembler, ¡Linux ¡

§ ObjecCve ¡is ¡to ¡understand ¡issues, ¡not ¡become ¡an ¡expert ¡in ¡ x86 ¡assembly ¡language ¡programming ¡

§ Knowledge ¡from ¡“Computer ¡Architecture ¡and ¡Systems ¡Programming” ¡ sufficient ¡

45 ¡

slide-33
SLIDE 33
slide-34
SLIDE 34
slide-35
SLIDE 35

Source ¡language ¡

§ Object-­‑oriented ¡ § Polymorphism ¡ § Class-­‑based ¡

§ Single ¡inheritance ¡

§ …. ¡subset ¡of ¡Java: ¡JavaLi ¡

§ No ¡finalizers ¡ § No ¡staAc ¡fields/methods ¡ § No ¡mulA-­‑threading ¡ § No ¡module ¡system ¡ § … ¡

48 ¡

slide-36
SLIDE 36

JavaLi ¡ ¡ ¡

class Record { int j; int next() { j = j + 1; return j; } }

49 ¡

slide-37
SLIDE 37

Grading ¡ ¡

§ Homework ¡done ¡in ¡teams ¡

§ Graded ¡ § Contributes ¡to ¡final ¡grade: ¡ ¡50% ¡of ¡the ¡final ¡grade ¡is ¡determined ¡by ¡the ¡ grades ¡you ¡get ¡for ¡homework ¡ § If ¡you ¡can’t ¡do ¡homework ¡by ¡due ¡date: ¡Talk ¡to ¡the ¡TAs ¡or ¡the ¡ instructor ¡before ¡the ¡deadline ¡

§ We ¡are ¡reasonable ¡

§ Final ¡exam ¡ ¡DATE ¡unknown ¡

§ 2 ¡hrs ¡ ¡ § During ¡examinaAon ¡period ¡ § Talk ¡to ¡me ¡by ¡Nov ¡15 ¡if ¡you ¡*must* ¡be ¡absent ¡

51 ¡

slide-38
SLIDE 38

Homework ¡

§ Both ¡members ¡of ¡a ¡team ¡must ¡contribute ¡

§ Both ¡of ¡you ¡are ¡responsible ¡for ¡the ¡soluAon ¡you ¡turn ¡in ¡ § We ¡reserve ¡the ¡right ¡to ¡ask ¡quesAons ¡about ¡the ¡design, ¡ implementaAon, ¡tesAng ¡plan, ¡etc. ¡

§ You ¡must ¡turn ¡in ¡your ¡own ¡work. ¡

§ “Finding” ¡a ¡soluAon ¡in ¡a ¡someone ¡else’s ¡directory ¡is ¡chea-ng. ¡ ¡ § ETH ¡has ¡ ¡a ¡policy ¡on ¡cheaAng. ¡

52 ¡

slide-39
SLIDE 39

53 ¡

slide-40
SLIDE 40

54 ¡

slide-41
SLIDE 41
slide-42
SLIDE 42

Homework ¡

§ We ¡do ¡not ¡want ¡to ¡play ¡police. ¡ § Do ¡the ¡homework ¡yourself. ¡ § Discussions ¡are ¡fine ¡(and ¡encouraged) ¡– ¡copying ¡code ¡is ¡not. ¡

56 ¡

slide-43
SLIDE 43

1st ¡homework ¡

§ Form ¡a ¡team ¡of ¡2 ¡persons ¡

§ Choose ¡your ¡team’s ¡name ¡wisely ¡

§ Register ¡team ¡

§ Details ¡on ¡the ¡web ¡page ¡

§ Write ¡a ¡small ¡program ¡

§ See ¡web ¡page ¡

§ No ¡team ¡partner? ¡

§ Come ¡to ¡front ¡aler ¡class ¡(today, ¡next ¡Wed) ¡ § Write ¡program ¡(above) ¡on ¡your ¡own ¡

57 ¡

slide-44
SLIDE 44

Summary ¡

§ L1: ¡JavaLi ¡

§ Classes/methods ¡ § PrimiAve ¡type ¡int § Inheritance ¡(single) ¡ § NO ¡mulAple ¡inheritance, ¡NO ¡excepAons, ¡NO ¡threads, ¡NO ¡finalizer ¡

§ L2: ¡x86 ¡ ¡(32-­‑bit, ¡IA32) ¡ § P ¡-­‑-­‑ ¡compiler ¡language: ¡Java ¡ § First ¡homework ¡

§ Form ¡team ¡ ¡

58 ¡

slide-45
SLIDE 45

Non-­‑topics ¡(for ¡this ¡class) ¡

§ Efficiency ¡of ¡generated ¡code ¡(in ¡L2) ¡ § Efficiency ¡of ¡compiler ¡

§ Can’t ¡make ¡stupid ¡mistakes ¡

§ Ease ¡of ¡re-­‑targeCng ¡(from ¡L2 ¡to ¡L3) ¡ § Ease ¡of ¡porCng ¡(from ¡P ¡to ¡P’) ¡

59 ¡

slide-46
SLIDE 46

Literature ¡

§ Aho/Lam/Sethi/Ullman: ¡Compilers: ¡Principles, ¡Techniques, ¡ and ¡Tools, ¡2nd ¡ediCon ¡ § Muchnick: ¡Advanced ¡Compiler ¡Design ¡and ¡ImplementaCon ¡ § Books ¡on ¡paderns, ¡frameworks, ¡so_ware ¡design ¡

60 ¡