Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 • Function expression: i = f1*3 + f2 • Function calls: double f1(int a, float b) • Control statements: for, while, do, if-else-then • Recursive function calls Term Project Presentation 報告流程 • • • • • • Scanner & Parser Symbol Table Semantic Record Data Structure Declaration Expression Control Flow Scanner Input: scan.l flex What’s up in scan.l? • • • • Output: lexyy.c Token: yytext check_type( ): return identifier or type_name count( ): return line and column of a token SlideCompile( ): the main function Parser Input: gram.y Declaration Expression Control Statement I/O Statement Output: gram_tab.h& yacc gram_tab.cpp SYMBOL TABLE •Each function has its own symbol table •Global symbol table is visible everywhere Symbol Table Symbol Table •Push a symtab_class pointer into sym_stack while entering a function scope. Semantic Data Structure Semantic Record • Record kind • ID, TYPEREF, ID_LIST, DATAOBJECT, TOKENOBJECT, USERTYPE, PARAM_LIST, RECORDDEF, CONSTOPTION, ERRORREC • Semantic data DataObject in Semantic Record OBJECTVALUE: Literal values including int,float..basic types OBJECTNAME: Declared non-pointer variables OBJECTARRAY: Declared array OBJECTRECORD: Declared record 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. 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? Expression Unary_Operation: • +, -, ~, !, ++, -- TypeCast_Operation: Arithmetic_Operation • *, /, %, +, - Shift_Operation • >>, << Expression…(more) Relational_Operation: • <, >, <=, >=, !=, == Bitwise_Operation: • &, ^, | Logic_Operation: • &&, || ps. According to precedence rule Expression Semantic Routines ( Call by Parser) Process literal • TRUE ,FALSE, INTEGER, REAL Process Operator • Type :Token ,Record operator Eval_operator • Unary: not Binary : relation , +,-,*,/ Expression 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 Expression It must be DataObject .op. DataObject or .op. DataOject DataObject has four types • • • • ObjectName ObjectValue ObjecTemp ObjectArray Expression 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]) Expression Other Case: • ObjectName,ObjectTemp,ObjectValue • Use it Directively • Return temp[index] Error Detect • simple process Next Presentation 報告流程 • • • • • • • Scanner & Parser Semantic Data Structure Symbol Table Expression Control Assignment HPF Symbol Table 1/4 Symbol Table 2/4 Symbol Table 3/4 IF statement IF expr THEN IF expr THEN IF expr THEN ENDIF ENDIF ENDIF Assignment Left Hand Side • Variable • Array Element • Vector Operand Right Hand Side • • • • Value Variable Temp Variable Array Element • Vector Operand Vector 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]]; HPF Directive TEMPLATE PROCESSOR ALIGN DISTRIBUTE TEMPLATE flag: IsTemplate , in attribute record Setting IsTemplate flag in ALIGN & DISTRIBUTE for Testing whether HPF_READ/WRITE Connect type_descriptor to Semantic Record (id_type) PROCESSORS !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 ()() 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. 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. 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; DISTRIBUTE(2/4) -- hpf_declare_A Allocate memory space for array in each processor. char* hpf_A[][]; Using multi-for-loop to declare array, according to Processor Number. hpf_declare(hpf_P[][],size,&hpf_A[][]) DISTRIBUTE(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 DISTRIBUTE(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]); Initialization & Close HPF_Initialization • Create Processor • Declare Array HPF_Close • hpf_end(&hpf_P[][], ... ); • pvm_exit(); Demo .................... HPF Generated Code & Running on PVM