C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and...

9
Chuen-Liang Chen, NTUCS&IE / COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN

Transcript of C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and...

Page 1: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 1

COMPILERCOMPILER

Chuen-Liang Chen

Department of Computer Science

and Information Engineering

National Taiwan University

Taipei, TAIWAN

Page 2: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 2

CompilerCompiler first compiler -- Fortran, late 1950s, 18 man-years

scopes of this course sequential procedural language von Neumann machine

compiler /interpreter

humanprogramming

language

machinemachine language target

– bare machine– virtual machine

format– assembly– relocatable– memory-image

analysislanguage-dependent

synthesismachine-dependent

constructions– hand-coded– compiler generator

(compiler compiler)

Page 3: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 3

Compiling technologyCompiling technology

siliconcompiler

commandinterpreter

of O.S.

query lang.interpreterof DBMS

and so on

compiler /interpreter

compilingtechnology

Page 4: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 4

AnalysesAnalyses

lexical

– regular expression syntax (structure)

– context-free grammar semantics (meaning)

static semantics

– attribute grammar run-time semantics

– Vienna definition language, ...

syntax error

a = b + ; static semantic error

int a , b ; boolean c ;a = b + c ;

run-time semantic error

int a , b , c ;b = largest_integer ;c = 1 ;a = b + c ;

Page 5: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 5

Structures of compilers (1/3)Structures of compilers (1/3) syntax-directed translation -- driven by the syntactic structure of

source program calling tree (2 passes)

machinecode

main

parser

scanner semanticroutines optimizer

code generator

symbol tableattribute table

token

SS : syntactic structure (parse tree)IR : intermediate representation

sourcecode

SS

IR

pass 1 pass 2

Page 6: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 6

Structures of compilers (2/3)Structures of compilers (2/3)

calling tree (1 pass)

machinecode

main

parser

scanner semanticroutines

optimizer

symbol tableattribute table

token

SS : syntactic structure (parse tree)

sourcecode

SS

pass 1

code generator

Page 7: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 7

Structures of compilers (3/3)Structures of compilers (3/3) calling tree (n passes, n > 2)

emphasizing on optimizations especially for advanced computer architectures, e.g.,

RISC, VLIW, vector computer, multiprocessor system, ...

machinecode

main

parser

scanner semanticroutines

code generator

symbol tableattribute table

token

SS : syntactic structure (parse tree)IR : intermediate representations

sourcecode

SS

IR opt 2opt 1

Page 8: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 8

Compiler compilerCompiler compiler for scanner and parser, based on precise mathematical modeling table-driven, usually lex and yacc

on UNIX

lex.yy.c and y.tab.c include table and driver program *.l, *.y, yyerror.c and main.c have many common external variables

ccas

ccas

ld

yacc

lex*.l

*.y

yyerror.c

main.c main.o yacc lib

lex lib

compiler

yyerror.o

y.tab.o

lex.yy.o

y.tab.h

lex.yy.cint yylex()

y.tab.cint yyparse()

cc / as

cc / as

Page 9: C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,

c

Chuen-Liang Chen, NTUCS&IE / 9

About this courseAbout this course

text book: Charles N. Fischer and Richard J. LeBlanc, Jr., Crafting a Compiler with C, Benjamin/Cummings, 1991.

reference books: many

evaluation: 2 examinations + term project

term project: a high-level language to byteCode compiler

email: [email protected], [email protected]

slides, ... : ntucs?:/usr/course/compiler

office hour: Thursday 8:00-9:00, Room 513 (1998 Fall)