B84506024 Tien-Hsin Lee

37
B84506024 Tien-Hsin Lee Compiler Term Project

description

Compiler Term Project. B84506024 Tien-Hsin Lee. Features:. Expression: ‘+’, ‘-’, ‘*’, ‘/’, ‘%’, ‘’, ‘? :’ ‘+=‘, ‘++i’, ….. Basic type expression: integer, float, double, char Multiarray expression: A[i+3][j*4]+B[5] Nested structures expression: R.a.aa + S.b.bb - PowerPoint PPT Presentation

Transcript of B84506024 Tien-Hsin Lee

Page 1: B84506024  Tien-Hsin Lee

B84506024 Tien-Hsin Lee

Compiler Term

Project

Compiler Term

Project

Page 2: B84506024  Tien-Hsin Lee

Features:Features:

• Expression: ‘+’, ‘-’, ‘*’, ‘/’, ‘%’, ‘<‘, ‘>’, ‘? :’ Expression: ‘+’, ‘-’, ‘*’, ‘/’, ‘%’, ‘<‘, ‘>’, ‘? :’

‘ ‘+=‘, ‘++i’, …..+=‘, ‘++i’, …..

• Basic type expression: integer, float, double, charBasic type expression: integer, float, double, char

• Multiarray expression: A[i+3][j*4]+B[5]Multiarray expression: A[i+3][j*4]+B[5]

• Nested structures expression: R.a.aa + S.b.bbNested structures expression: R.a.aa + S.b.bb

• Function expression: i = f1*3 + f2 Function expression: i = f1*3 + f2

• Function calls: double f1(int a, float b)Function calls: double f1(int a, float b)

• Control statements: for, while, do, if-else-thenControl statements: for, while, do, if-else-then

• Recursive function callsRecursive function calls

Page 3: B84506024  Tien-Hsin Lee

Term Project PresentationTerm Project Presentation

報告流程 • Scanner & Parser • Symbol Table• Semantic Record Data Structure • Declaration • Expression • Control Flow

Page 4: B84506024  Tien-Hsin Lee

ScannerScanner

Input: scan.l Output: lexyy.c flex

What’s up in scan.l?• Token: yytext• check_type( ): return identifier or type_name• count( ): return line and column of a token • SlideCompile( ): the main function

Page 5: B84506024  Tien-Hsin Lee

ParserParser

Input: gram.y Output: gram_tab.h&

yacc gram_tab.cpp Declaration Expression Control Statement I/O Statement

Page 6: B84506024  Tien-Hsin Lee

SYMBOL TABLE•Each function has its own symbol tableEach function has its own symbol table

•Global symbol table is visible everywhereGlobal symbol table is visible everywhere

Page 7: B84506024  Tien-Hsin Lee

Symbol Table

Page 8: B84506024  Tien-Hsin Lee

Symbol Table•Push a symtab_class pointer into sym_stack Push a symtab_class pointer into sym_stack while entering a function scope.while entering a function scope.

Page 9: B84506024  Tien-Hsin Lee

Semantic Data StructureSemantic Data Structure

Semantic Record• Record kind

• ID, TYPEREF, ID_LIST, DATAOBJECT, TOKENOBJECT, USERTYPE, PARAM_LIST, RECORDDEF, CONSTOPTION, ERRORREC

• Semantic data

Page 10: B84506024  Tien-Hsin Lee

DataObject in Semantic Record OBJECTVALUE:

Literal values including int,float..basic types

OBJECTNAME:Declared non-pointer variables

OBJECTARRAY:Declared array

OBJECTRECORD:Declared record

Page 11: B84506024  Tien-Hsin Lee

DataObject …(more)

OBJECTTEMP:The most common type of DataObject that is carried in the semantic record. Whenever an expression is evaluated, it becomes an OBJECTTEMP. The code generated will be carried as well.

Page 12: B84506024  Tien-Hsin Lee

More about OBJECTTEMP...

How to become an OBJECTTEMP? Why OBJECTTEMP needs to bring code?

Due to the bottom up parsing design and different combination of binary operation.

When is the right time to generate code into the output file?

Page 13: B84506024  Tien-Hsin Lee

ExpressionExpression

Unary_Operation:• +, -, ~, !, ++, --

TypeCast_Operation: Arithmetic_Operation

• *, /, %, +, -

Shift_Operation• >>, <<

Page 14: B84506024  Tien-Hsin Lee

Expression…(more)

Relational_Operation:• <, >, <=, >=, !=, ==

Bitwise_Operation:• &, ^, |

Logic_Operation:• &&, ||

ps. According to precedence rule

Page 15: B84506024  Tien-Hsin Lee

ExpressionExpression

Semantic Routines ( Call by Parser) Process literal

• TRUE ,FALSE, INTEGER, REAL

Process Operator• Type :Token ,Record operator

Eval_operator• Unary: not Binary : relation , +,-,*,/

Page 16: B84506024  Tien-Hsin Lee

ExpressionExpression

Sematic_Record• Record-Kind

Error ,ID ,Array , Range,DataObject,Token(op)

If_Stmt Do_For Do_While

BlockFormat CyclicFormat• Semantic-data• Next : point to another Semantic _Record

Page 17: B84506024  Tien-Hsin Lee

ExpressionExpression

It must be DataObject .op. DataObject

or .op. DataOject DataObject has four types

• ObjectName • ObjectValue• ObjecTemp • ObjectArray

Page 18: B84506024  Tien-Hsin Lee

ExpressionExpression

Case 1: ObjectValue .op. ObjectValue “ +-*/ ”=>Compute it directive and turn value

Case 2: ObjectArray operation• NotTemplate

A(I1,I2,...In)=>A[I1][I2]....[In]• Template

A(I1,I2,...In)=>hpf_read_A(I1,I2,..In,temp[J])

Page 19: B84506024  Tien-Hsin Lee

ExpressionExpression

Other Case: • ObjectName,ObjectTemp,ObjectValue• Use it Directively• Return temp[index]

Error Detect• simple process

Page 20: B84506024  Tien-Hsin Lee

Next PresentationNext Presentation

報告流程 • Scanner & Parser • Semantic Data Structure • Symbol Table• Expression • Control • Assignment• HPF

Page 21: B84506024  Tien-Hsin Lee

Symbol Table 1/4Symbol Table 1/4

Page 22: B84506024  Tien-Hsin Lee

Symbol Table 2/4Symbol Table 2/4

Page 23: B84506024  Tien-Hsin Lee

Symbol Table 3/4Symbol Table 3/4

Page 24: B84506024  Tien-Hsin Lee

IF statementIF statement

IF expr THEN

IF expr THEN

IF expr THEN

ENDIF

ENDIF

ENDIF

Page 25: B84506024  Tien-Hsin Lee

AssignmentAssignment

Left Hand Side

• Variable

• Array Element

• Vector Operand

Right Hand Side

• Value

• Variable

• Temp Variable

• Array Element

• Vector Operand

Page 26: B84506024  Tien-Hsin Lee

Vector Operand AssignmentVector Operand Assignment

V_C(1:5:2) = V_D(1:5:2)

for(index[0]=1,index[1]=3;index[0]<=5;index[0]+=2,index[1]++)

int_temp[index[1]] = V_D[index[0]];

for(index[0]=1,index[1]=3;index[0]<=5;index[0]+=2,index[1]++)

V_C[index[0]] = int_temp[index[1]];

Page 27: B84506024  Tien-Hsin Lee

HPF DirectiveHPF DirectiveTEMPLATEPROCESSORALIGNDISTRIBUTEInitialization & Close

Page 28: B84506024  Tien-Hsin Lee

TEMPLATETEMPLATE

flag: IsTemplate , in attribute record Setting IsTemplate flag in ALIGN & DIST

RIBUTE for Testing whether HPF_READ/WRITE Connect type_descriptor to Semantic Recor

d (id_type)

Page 29: B84506024  Tien-Hsin Lee

PROCESSORSPROCESSORS

!HPF$ PROCESSORS P(3,5)

int hpf_P[3][5]

called in-main:void hpf_create_processor_P()

Using for loop.........

{hpf_create_processor(&hpf_P[][],X) ;}

X:P(x)(y) --> using for to do ()()

Page 30: B84506024  Tien-Hsin Lee

ALIGN(1/2)ALIGN(1/2)

!HPF$ ALIGN A(I,J,K) WITH T(I,11*J-9)

Using In HPF_READ/WRITE Need to calculate address of A on T being called in hpf_read_A/hpf_write_A for

actual distributed elements of A.

Page 31: B84506024  Tien-Hsin Lee

ALIGN(2/2)ALIGN(2/2)

For each ALIGN, we generate a function calculating address.

Index_ArrayName(int dim, int_index_list)• dim : which dimension to compute• index_list : array index to convert• function_Name():set subFuncName;• gen_index_func():generate function header• gen_case():produce each dimension case.

Page 32: B84506024  Tien-Hsin Lee

DISTRIBUTE(1/4)DISTRIBUTE(1/4)

!HPFS DISTRIBUTE T(BLOCK,CYCLIC(4)) ONTO P

Generate 3 functions for each distributed_array

• hpf_declare_ArrayName

• hpf_read_ArrayName

• hpf_write_ArrayName Handle distribute-data methods in dimension.

• In Semantic Record :• BLOCK(X) : BLOCKFORMAT , block_value;

• CYCLIC(Y) : CYCLICFORMAT, cyclic_value;

Page 33: B84506024  Tien-Hsin Lee

DISTRIBUTE(2/4) -- hpf_declare_ADISTRIBUTE(2/4) -- hpf_declare_A

Allocate memory space for array in each processor.

char* hpf_A[][]; Using multi-for-loop to declare array, accor

ding to Processor Number. hpf_declare(hpf_P[][],size,&hpf_A[][])

Page 34: B84506024  Tien-Hsin Lee

DISTRIBUTE(3/4) -- hpf_(read/write)_ADISTRIBUTE(3/4) -- hpf_(read/write)_A

void hpf_(read/write)_A(int_index_list, int *data)

Call Index_Function to calculate ALIGN address.

Compute offset in each processor according to BLOCK and CYCLIC structure.

Call hpf_(read/write) of pvm

Page 35: B84506024  Tien-Hsin Lee

DISTRIBUTE(4/4) -- Generated CodesDISTRIBUTE(4/4) -- Generated Codes

A(1,2,3) = B(3,2,1)

hpf_read_B(3,2,1,&int_temp[2]);

hpf_write_A(1,2,3,int_temp[2]);

Page 36: B84506024  Tien-Hsin Lee

Initialization & CloseInitialization & Close

HPF_Initialization• Create Processor• Declare Array

HPF_Close• hpf_end(&hpf_P[][], ... );• pvm_exit();

Page 37: B84506024  Tien-Hsin Lee

Demo ....................Demo ....................

HPF Generated Code

&

Running on PVM