Download presentation
Presentation is loading. Please wait.
Published byBogna Borowska Modified over 5 years ago
1
Code Generation My suggestion: Pick three registers to be used throughout Assuming stmt of form dest = s1 op s2 Generate code by: Load s1 into r5 Load s2 into r6 r7 = r5 op r6 Store r7 into dest
2
Array Declaration Algorithm
Dimension Node { int min; int max; int size; }
3
Declaration Algorithm (2)
Doubly linked list of dimension nodes Pass 1 – while parsing Build linked list from left to right Insert min, max Size = size of an element (e.g. 4 for int) Append node to end of list min = max = size = 1
4
Declaration Algorithm (3) Pass 2
Traverse list from tail to head For each node, n, going “right” to “left” Factor = n.max – n.min + 1 For each node, m, right to left starting with n m.size = m.size * factor For each node, n, going right to left max = N->left->max; min = N->left->min Save size of first node as size of entire array Delete first element of list Set tail->size = size of an element (e.g. 4 for int)
5
Array Declaration (Row Major)
int weight[ ][1..12][1..31]; list of “dimension” nodes int min, max, size size of element of this dimension 1488 124 4
6
Array Offset (Row Major)
Traverse list summing (max-min) * size int weight[ ][1..12][1..31]; x = weight [2002][5][31] ( ) * (5-1) * (31-1) * 4 1488 124 4
7
Array Offset (Row Major)
Traverse list summing (max-min) * size int weight[ ][1..12][1..31]; x = weight [i][j][k] (i ) * (j-1) * (k-1) * 4 1488 124 4
8
Your Turn Assume “Show” A’s dimension list
int A[10][20][30]; Row major order “Show” A’s dimension list Show hypothetical 3-addr code for X = A[2][3][4] ; A[3][4][5] = 9
9
My “Assembly” code X = A[2][3][4]; r1 = 4800; # 2 * 2400 r2 = 360;
r3 = r1 + r2; r4 = 16; r5 = r3 + r4; r6 = r5 + 64; #offset of A iptr1 = &r6; r7 = *iptr1;
10
Your Turn 2 Assume “Show” A’s dimension list
int A[10][20][30]; Column major order “Show” A’s dimension list Show hypothetical 3-addr code for X = A[2][3][4] ; A[3][4][5] = 9
11
Control Constructs Can be cumbersome, but not difficult
“Write” control construct in 3-addr pseudo code using labels and gotos. Map that “control construct” to grammar rule action(s).
12
Semantic Hooks Selection_statement : IF ‘(‘ comma_expr ‘)’ stmt | IF ‘(‘ comma_expr ‘)’ stmt ELSE stmt ; 1 shift/reduce error
13
Add actions (1) Selection_statement : IF ‘(‘ comma_expr ‘)’ {printf(“start IF \n”);} stmt {printf(“IF Body \n”);}
14
Add actions (2) | IF ‘(‘ comma_expr ‘)’ {printf(“start IF \n”);} stmt {printf(“Then Body \n”);} ELSE stmt {printf(“ELSE Body \n”);} ; 31 reduce/reduce errors !
15
Solution (1) Selection_statement : if_start | if_start ELSE stmt {printf(“ELSE body”);} }
16
Solution (2) if_start : IF ‘(‘ comma_expr ‘)’ {printf(“start IF \n”);} stmt {printf(“Then Body \n”);} ; 1 shift-reduce
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.