• No results found

The Design and Implementation of Gnu Compiler Collection

N/A
N/A
Protected

Academic year: 2022

Share "The Design and Implementation of Gnu Compiler Collection"

Copied!
32
0
0

Loading.... (view fulltext now)

Full text

(1)

The Design and Implementation of Gnu Compiler Collection

Uday Khedker

(www.cse.iitb.ac.in/˜uday)

Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Aug 2008

(2)

Part 1

Compilation Flow: Machine

Independent Phases

(3)

cs 715 GCC: Compilation Flow: Machine Independent Phases 2/12

The GNU Tool Chain

gcc Source Program

Target Program

UPK IIT Bombay

(4)

The GNU Tool Chain

gcc Source Program

Target Program

cc1

(5)

cs 715 GCC: Compilation Flow: Machine Independent Phases 2/12

The GNU Tool Chain

gcc Source Program

Target Program

cc1 cpp

UPK IIT Bombay

(6)

The GNU Tool Chain

gcc Source Program

Target Program

cc1 cpp

as

(7)

cs 715 GCC: Compilation Flow: Machine Independent Phases 2/12

The GNU Tool Chain

gcc Source Program

Target Program

cc1 cpp

as

ld

UPK IIT Bombay

(8)

The GNU Tool Chain

gcc Source Program

Target Program

cc1 cpp

as

ld

glibc/newlib

(9)

cs 715 GCC: Compilation Flow: Machine Independent Phases 3/12

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent

Generator Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator

gcc

UPK IIT Bombay

(10)

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent

Generator Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator

gcc Source Program

Assembly Program

(11)

cs 715 GCC: Compilation Flow: Machine Independent Phases 3/12

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent Generator

Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator Selected

gcc

UPK IIT Bombay

(12)

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent Generator

Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator

Selected Copied

Copied

gcc

(13)

cs 715 GCC: Compilation Flow: Machine Independent Phases 3/12

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent Generator

Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator

Selected Copied

Copied Generated

Generated

gcc

UPK IIT Bombay

(14)

The GCC Framework

Language Specific Code

Language and Machine Independent Generic Code

Machine dependent Generator

Code

Machine Descrip- tions GCC

Input Language Machine Description

cc1 Parser Genericizer Gimplifier Tree SSA

Optimizer

RTL

Generator Optimizer Code Generator

Selected Copied

gcc

(15)

cs 715 GCC: Compilation Flow: Machine Independent Phases 4/12

Invocation of cc1 from gcc (4.0.2)

main () gcc.c

do_spec () gcc.c

do_spec_2 () gcc.c

do_spec_1 () gcc.c

execute () gcc.c

pexecute () ../libiberty/pex-unix.c /* TO: cc1 */

Tip

Static Inspection: Use cscope and/or ctags

Dynamic Inspection: Set breakpoints in gdb on cc1.

UPK IIT Bombay

(16)

The cc1 Phase Sequence as IR Chain The GCC Phase Sequence

Generic GIMPLE RTL ASM

(17)

cs 715 GCC: Compilation Flow: Machine Independent Phases 5/12

The cc1 Phase Sequence as IR Chain

The GCC Phase Sequence

Generic GIMPLE RTL ASM

HLL

Scope Explication Lower control flow

Lower procedures

& data

Target

UPK IIT Bombay

(18)

Front End Processing Sequence for C in cc1 and GCC (4.0.2)

toplev_main () toplev.c

general_init () toplev.c

init_tree_optimization_passes () tree-optimize.c

decode_options () toplev.c

do_compile () toplev.c

compile_file() toplev.c

lang_hooks.parse_file () toplev.c c_common_parse_file () c-opts.c c_parse_file () c-parse.y (c-parse.c)

finish_function () c-decl.c

c_genericize () c-gimplify.c

/* TO: Gimplification */

(19)

cs 715 GCC: Compilation Flow: Machine Independent Phases 7/12

GIMPLE Phase sequence in cc1 and GCC (4.0.2) Creating GIMPLE representation in cc1 and GCC

finish_function () c-decl.c

c_genericize() c-gimplify.c

gimplify_function_tree() gimplify.c

gimplify_body() gimplify.c

gimplify_stmt() gimplify.c

gimplify_expr() gimplify.c

c_expand_body() c-decl.c

tree_rest_of_compilation() tree-optimize.c execute_pass_list() tree-optimize.c execute_one_pass() tree-optimize.c /* calls pass entry point function pointer */

targetm.asm_out.constructor()

UPK IIT Bombay

(20)

The Tree passes list (4.0.2) (Partial) Passes list (tree-optimize.c) (∼ 63 passes)

pass_remove_useless_stmts // Pass

pass_lower_cf // Pass

pass_all_optimizations // Optimiser pass_build_ssa // Optimiser

pass_dce // Optimiser

pass_loop // Optimiser

pass_complete_unroll // Optimiser pass_loop_done // Optimiser pass_del_ssa // Optimiser pass_warn_function_return // Optimiser

pass_expand // RTL Expander

pass_rest_of_compilation // RTL passes

(21)

Part 2

Adding a Pass on Gimple IR

(22)

GCC Tree Passes: Code organisation

Tree Pass Organisation

• Data structure records pass info: name, function to execute etc.

(struct tree opt pass in tree-pass.h)

• Instantiate a struct tree opt pass variable in each pass file.

• List the pass variables (in init tree optimization passes.c).

(23)

cs 715 GCC: Adding a Pass on Gimple IR 10/12

Adding a Pass on Gimple IR

• Step 0. Write function cs715 main() in file cs715.c.

• Step 1. Create the following data structure in file cs715.c.

struct tree_opt_pass pass_cs715 = { "cs715", /* name */

NULL, /* gate, for conditional entry to this pass */

cs715_main, /* execute, main entry point */

NULL, /* sub-passes, depending on the gate predicate */

NULL, /* next sub-passes, independ of the gate predicate */

0, /* static_pass_number , used for dump file name*/

0, /* tv_id */

0, /* properties_required, indicated by bit position */

0, /* properties_provided , indicated by bit position*/

0, /* properties_destroyed , indicated by bit position*/

0, /* todo_flags_start */

0, /* todo_flags_finish */

0 /* letter for RTL dump */

};

UPK IIT Bombay

(24)

Adding a Pass on Gimple IR

• Step 2. Add the following line to tree-pass.h

extern tree opt pass pass cs715;

(25)

cs 715 GCC: Adding a Pass on Gimple IR 11/12

Adding a Pass on Gimple IR

• Step 2. Add the following line to tree-pass.h extern tree opt pass pass cs715;

• Step 3. Include the following call at an appropriate place in the function init tree optimization passes() in the file tree-optimize.c

NEXT PASS (pass cs715);

UPK IIT Bombay

(26)

Adding a Pass on Gimple IR

• Step 2. Add the following line to tree-pass.h extern tree opt pass pass cs715;

• Step 3. Include the following call at an appropriate place in the function init tree optimization passes() in the file tree-optimize.c

NEXT PASS (pass cs715);

• Step 4. Add the file name in the Makefile

Either in $SOURCE/gcc/Makefile.in Reconfigure and remake

Or in $BUILD/gcc/Makefile

Remake

(27)

cs 715 GCC: Adding a Pass on Gimple IR 11/12

Adding a Pass on Gimple IR

• Step 2. Add the following line to tree-pass.h extern tree opt pass pass cs715;

• Step 3. Include the following call at an appropriate place in the function init tree optimization passes() in the file tree-optimize.c

NEXT PASS (pass cs715);

• Step 4. Add the file name in the Makefile

Either in $SOURCE/gcc/Makefile.in Reconfigure and remake

Or in $BUILD/gcc/Makefile Remake

• Step 5. Build the compiler

UPK IIT Bombay

(28)

Adding a Pass on Gimple IR

• Step 2. Add the following line to tree-pass.h extern tree opt pass pass cs715;

• Step 3. Include the following call at an appropriate place in the function init tree optimization passes() in the file tree-optimize.c

NEXT PASS (pass cs715);

• Step 4. Add the file name in the Makefile

Either in $SOURCE/gcc/Makefile.in Reconfigure and remake

Or in $BUILD/gcc/Makefile Remake

• Step 5. Build the compiler

• Step 6. Wonder what went wrong?

(29)

cs 715 GCC: Adding a Pass on Gimple IR 12/12

Traversing Control Flow Graph

for(n=0; n < number_of_nodes; n++) { bb = VARRAY_BB(dfs_order_bb,n);

for( bsi =bsi_start(bb);!bsi_end_p(bsi);bsi_next(&bsi)) {

}

} Block Statement Iterator

UPK IIT Bombay

(30)

Traversing Control Flow Graph

for(n=0; n < number_of_nodes; n++) { bb = VARRAY_BB(dfs_order_bb,n);

for( bsi =bsi_start(bb);!bsi_end_p(bsi);bsi_next(&bsi)) { stmt = bsi_stmt(bsi);

switch(TREE_CODE(stmt)) {

}

}

}

(31)

cs 715 GCC: Adding a Pass on Gimple IR 12/12

Traversing Control Flow Graph

for(n=0; n < number_of_nodes; n++) { bb = VARRAY_BB(dfs_order_bb,n);

for( bsi =bsi_start(bb);!bsi_end_p(bsi);bsi_next(&bsi)) { stmt = bsi_stmt(bsi);

switch(TREE_CODE(stmt)) { case MODIFY_EXPR:

} } }

UPK IIT Bombay

(32)

Traversing Control Flow Graph

for(n=0; n < number_of_nodes; n++) { bb = VARRAY_BB(dfs_order_bb,n);

for( bsi =bsi_start(bb);!bsi_end_p(bsi);bsi_next(&bsi)) { stmt = bsi_stmt(bsi);

switch(TREE_CODE(stmt)) { case MODIFY_EXPR:

expr = TREE_OPERAND(stmt,1);

/* SET_BIT(GEN(current_pf_L,bb),expr); */

lval = TREE_OPERAND(stmt,0);

FOR expr_no in exprList(lval) {

/* RESET_BIT(av_L[bb]->gen),expr_no); */

/* SET_BIT(av_L[bb]->kill),expr_no); */

} break;

...

}

}

}

References

Related documents

The necessary set of data includes a panel of country-level exports from Sub-Saharan African countries to the United States; a set of macroeconomic variables that would

Percentage of countries with DRR integrated in climate change adaptation frameworks, mechanisms and processes Disaster risk reduction is an integral objective of

The Congo has ratified CITES and other international conventions relevant to shark conservation and management, notably the Convention on the Conservation of Migratory

Although a refined source apportionment study is needed to quantify the contribution of each source to the pollution level, road transport stands out as a key source of PM 2.5

INDEPENDENT MONITORING BOARD | RECOMMENDED ACTION.. Rationale: Repeatedly, in field surveys, from front-line polio workers, and in meeting after meeting, it has become clear that

With respect to other government schemes, only 3.7 per cent of waste workers said that they were enrolled in ICDS, out of which 50 per cent could access it after lockdown, 11 per

While Greenpeace Southeast Asia welcomes the company’s commitment to return to 100% FAD free by the end 2020, we recommend that the company put in place a strong procurement

Of those who have used the internet to access information and advice about health, the most trustworthy sources are considered to be the NHS website (81 per cent), charity