Presentation is loading. Please wait.

Presentation is loading. Please wait.

שפות ביניים – Intermediate Languages/Representations Aho, Sethi and Ullman – Chapter 8 Cooper and Torczon – Chapter 5.

Similar presentations


Presentation on theme: "שפות ביניים – Intermediate Languages/Representations Aho, Sethi and Ullman – Chapter 8 Cooper and Torczon – Chapter 5."— Presentation transcript:

1 שפות ביניים – Intermediate Languages/Representations Aho, Sethi and Ullman – Chapter 8 Cooper and Torczon – Chapter 5

2 יצירת קוד ביניים syntax analysis semantic analysis intermediate code generator machine independent optimizations syntax tree decorated syntax tree intermediate code code generator

3 חשיבות קוד הביניים אפשרות לייצר קוד עבור מכונות שונות באמצעות אותו front end שימוש בשיטות אופטימיזציה שאינן תלויות במכונה מסויימת שימוש באותו back end עבור שפות שונות – מספר front ends

4 ייצוג ביניים – intermediate representation ייצוגים אפשריים syntax tree postfix notation three address code נתקדם בשלבים תרגום מונחה דקדוק ליצירת עץ הניתוח התחבירי + סימונים בצמתים את העץ נתרגם ל- three address code הערה – לפעמים נשתמש ב- DAG במקום בעץ

5 decorated syntax trees / DAGs נתון: a := b * – c + b * – c ייצוג כ- DAG assign +a+a *** unimusb b b ccc ייצוג כעץ ייצוג ב- postfix a b c uminus * b c uminus * + assign

6 תרגום מונחה דקדוק ליצירת עץ מעוטר פונקציות עזר mkleaf – יצירת עלה mkunode – יצירת צומת חדש עבור אופרטור אונרי mknode – יצירת צומת חדש עבור אופרטור בינארי id.place – מצביע לטבלת הסמלים הערה – אפשר להחליף את mkleaf, mkunode, ו- mknode בפונקציות המחזירות מצביע לצמתים קיימים על מנת ליצור DAG semantic ruleproduction S.nptr := mknode ( ' assign ', mkleaf ( id, id.place ), E.nptr ) S → id := E E.nptr := mknode ( ' + ', E 1.nptr, E 2.nptr ) E → E 1 + E 2 E.nptr := mknode ( ' * ', E 1.nptr, E 2.nptr ) E → E 1 * E 2 E.nptr := mkunode ( ' uminus ', E 1.nptr ) E → – E 1 E.nptr := E 1.nptr E → ( E 1 ) E.nptr := mkleaf ( id, id.place ) E → id

7 * * uminus cidc b b assign + aid        b 0 c 1 1uminus2 20*3 bid4 c 5 5uminus6 64*7 73+8 aid9 89assign10 · · ·11 ייצוג בזיכרון של עץ מעוטר

8 three address code הצורה הכללית x := y op z x, y, ו-z הם 3 שמות, קבועים, או משתנים זמניים שנוצרו ע"י הקומפיילר op הוא אופרטור כלשהו אין פעולות מורכבות ↑↑↑ 3 הכתובות אופרטור

9 three address code cc unimusb b *  +a assign t5t5 :=a t 2 + t 4 :=t5t5 b * t 3 :=t4t4 – c:=t3t3 b * t 1 :=t2t2 – c:=t1t1 c unimusb * +a assign t3t3 :=a t 2 + t 2 :=t3t3 b * t 1 :=t2t2 – c:=t1t1

10 קוד ביניים – סוגי המשפטים 1.משפטי השמה עם פעולה בינאריתx := y op z 2.משפטי השמה עם פעולה אונריתx := op y 3.משפטי העתקהx := y 4.קפיצה בלתי מותנהgoto L 5.קפיצה מותנהif x relop y goto L 6.פרמטרים וקריאה לפרוצדורותparam x call p, n return y 7.indexed assignmentsx := y [ i ] x [ i ] := y 8.השמה של כתובות ומצביעיםx := addr y x := * y * x := y הבחירה של אוסף פקודות מתאים היא חשובה. אוסף מצומצם יקל על ייצור הקוד, אך הקוד יהיה פחות יעיל, ומעמסה גדולה יותר תיפול על ה- optimizer relop = relational op (==, >=, etc.) n = actual number of parameters

11 איך בוחרים אופרטורים? אילו אופרטורים נכלול בשפת הביניים שלנו? אופרטורים רבים: קשה יותר למימוש על מכונות חדשות אופרטורים מעטים: אופטימיזציות קשות יותר לביצוע, אי-ניצול יכולות של מכונות חדשות

12 יצירת קוד ביניים בעל 3 כתובות על ידי תרגום מונחה דקדוק השיטה – שימוש במשתנים זמניים S.code – תכונה המכילה את הקוד הנוצר עבור S E.code – הקוד הנוצר עבור E E.place – שם של משתנה שעתיד להכיל את הערך של E newtemp – פונקציה המחזירה שם של משתנה חדש

13 יצירת קוד ביניים בעל 3 כתובות על ידי תרגום מונחה דקדוק semantic ruleproduction S.code := E.code || gen ( id.place ' := ' E.place ) S → id := E E.place := newtemp; E.code := E 1.code || E 2.code || gen ( E.place ' := ' E 1.place ' + ' E 2.place ) E → E 1 + E 2 E.place := newtemp; E.code := E 1.code || E 2.code || gen ( E.place ' := ' E 1.place ' * ' E 2.place ) E → E 1 * E 2 E.place := newtemp; E.code := E 1.code || gen ( E.place ' := ' ' uminus ' E 1.place ) E → – E 1 E.place := E 1.place E.code := E 1.code E → ( E 1 ) E.place := id.place ; E.code := ' ' E → id

14 יצירת קוד ביניים בעל 3 כתובות על ידי תרגום מונחה דקדוק S.begin := newlable ; S.after := newlabel ; S.code := gen ( S.begin ' : ' ) || E.code || gen ( ' if ' E.place ' = ' ' 0 ' ' goto ' S.after ) || S 1.code || gen ( ' goto ' S.begin ) || gen (S.after ' : ' ) S → while E do S 1 semantic ruleproduction E.code S.begin: if E.place = 0 goto S.after S 1.code goto S.begin · · ·S.after: newlabel– פונקציה היוצרת תווית חדשה S.begin – תווית המסמנת את תחילת הקוד S.after – תווית המסמנת את סוף הקוד 0 – מייצג את false האם באמת קופצים לקוד שאחרי ה-while?

15 מימוש של קוד בעל 3 כתובות רביעיות op, arg 1, arg 2, result חסרון – temporaries נמצאים בטבלת הסמלים ↑↑↑ מצביעים לטבלת הסמלים at5t5 =:(5) t5t5 t4t4 t2t2 +(4) t4t4 t3t3 b*(3) t3t3 cuminus(2) t2t2 t1t1 b*(1) t1t1 cuminus(0) resultarg 2arg 1op

16 מימוש של קוד בעל 3 כתובות שלשות op, arg 1, arg 2 אין צורך ב- result פעולה טרנרית כמו x [ i ] := y דורשת שתי שורות ↑↑ מצביעים לטבלת הסמלים או למספר הסידורי של השורה המחשבת את הערך (4)aassign(5) (3)(1)+(4) (2)b*(3) cuminus(2) (0)b*(1) cuminus(0) arg 2arg 1op y(0)assign(1) ix[ ] =(0) arg 2arg 1op (0)xassign(1) iy= [ ](0) arg 2arg 1op x [ i ] := yx := y [ i ]

17 מימוש של קוד בעל 3 כתובות indirect triples – השלשות מופרדות מהסדר בינהן הערות בייצוג כרביעיות קל להזיז קטעי קוד; בשיטת השלשות הדבר בלתי אפשרי בייצוג כ- indirect triples אפשר להזיז קטעי קוד ולחסוך במקום אם קיימות שלשות זהות (19)(5) (18)(4) (17)(3) (16)(2) (15)(1) (14)(0) statement (18)aassign (17)(15)+ (16)b* cuminus (14)b* cuminus arg 2arg 1op (19) (18) (17) (16) (15) (14)

18 הכרזות וטיפול במשתנים הבעיה המרכזית – חישוב ה- offset semantic ruleproduction { offset := 0 } P → P  D D → D ; D { enter ( id.name, T.type, offset ); offset := offset + T.width } D → id : T { T.type := integer ; T.width := 4 } T → integer { T.type := real ; T.width := 8 } T → real { T.type := array ( num.val, T 1.type ) ; T.width := num.val  T 1.width } E → array [ num ] of T 1 T.type := pointer (T 1.type ) ; T.width := 4 } T → ↑ T 1

19 הכרזות וטיפול במשתנים הבעיה המרכזית – חישוב ה- offset הערה – אין כאן יצירה של קוד semantic ruleproduction { offset := 0 } P → MD M →  P → P  D D → D ; D { enter ( id.name, T.type, offset ); offset := offset + T.width } D → id : T { T.type := integer ; T.width := 4 } T → integer { T.type := real ; T.width := 8 } T → real { T.type := array ( num.val, T 1.type ) ; T.width := num.val  T 1.width } E → array [ num ] of T 1 T.type := pointer (T 1.type ) ; T.width := 4 } T → ↑ T 1 _____________


Download ppt "שפות ביניים – Intermediate Languages/Representations Aho, Sethi and Ullman – Chapter 8 Cooper and Torczon – Chapter 5."

Similar presentations


Ads by Google