1 / 37

B84506024 Tien-Hsin Lee

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

Télécharger la présentation

B84506024 Tien-Hsin Lee

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compiler Term Project • B84506024 Tien-Hsin Lee

  2. 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

  3. Term Project Presentation • 報告流程 • Scanner & Parser • Symbol Table • Semantic Record Data Structure • Declaration • Expression • Control Flow

  4. Scanner • 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

  5. Parser • Input: gram.y Output: gram_tab.h& yacc gram_tab.cpp • Declaration • Expression • Control Statement • I/O Statement

  6. SYMBOL TABLE • Each function has its own symbol table • Global symbol table is visible everywhere

  7. Symbol Table

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

  9. Semantic Data Structure • Semantic Record • Record kind • ID, TYPEREF, ID_LIST, DATAOBJECT, TOKENOBJECT, USERTYPE, PARAM_LIST, RECORDDEF, CONSTOPTION, ERRORREC • Semantic data

  10. DataObject in Semantic Record • OBJECTVALUE: Literal values including int,float..basic types • OBJECTNAME: Declared non-pointer variables • OBJECTARRAY: Declared array • OBJECTRECORD: Declared record

  11. 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.

  12. 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?

  13. Expression • Unary_Operation: • +, -, ~, !, ++, -- • TypeCast_Operation: • Arithmetic_Operation • *, /, %, +, - • Shift_Operation • >>, <<

  14. Expression…(more) • Relational_Operation: • <, >, <=, >=, !=, == • Bitwise_Operation: • &, ^, | • Logic_Operation: • &&, || ps. According to precedence rule

  15. Expression • Semantic Routines ( Call by Parser) • Process literal • TRUE ,FALSE, INTEGER, REAL • Process Operator • Type :Token ,Record operator • Eval_operator • Unary: not Binary : relation , +,-,*,/

  16. 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

  17. Expression • It must be DataObject .op. DataObject or .op. DataOject • DataObject has four types • ObjectName • ObjectValue • ObjecTemp • ObjectArray

  18. 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])

  19. Expression • Other Case: • ObjectName,ObjectTemp,ObjectValue • Use it Directively • Return temp[index] • Error Detect • simple process

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

  21. Symbol Table 1/4

  22. Symbol Table 2/4

  23. Symbol Table 3/4

  24. IF statement IF expr THEN IF expr THEN IF expr THEN ENDIF ENDIF ENDIF

  25. Left Hand Side Variable Array Element Vector Operand Right Hand Side Value Variable Temp Variable Array Element Vector Operand Assignment

  26. 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]];

  27. HPF Directive TEMPLATE PROCESSOR ALIGN DISTRIBUTE Initialization & Close

  28. 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)

  29. 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 ()()

  30. 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.

  31. 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.

  32. 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;

  33. 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[][])

  34. 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

  35. 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]);

  36. Initialization & Close • HPF_Initialization • Create Processor • Declare Array • HPF_Close • hpf_end(&hpf_P[][], ... ); • pvm_exit();

  37. Demo .................... HPF Generated Code & Running on PVM

More Related