C-- by Aksel Gresvig. Outline ● What is C--? How does it work? ● What C-- is not ● Relationship to C ● What does it look like? Examples ● Elements of.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Programming Languages and Paradigms
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
The Assembly Language Level
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Program Representations. Representing programs Goals.
Code Generation Mooly Sagiv html:// Chapter 4.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Chapter 9: Subprogram Control
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Imperative Programming
Computing with C# and the.NET Framework Chapter 1 An Introduction to Computing with C# ©2003, 2011 Art Gittleman.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Compiler Chapter# 5 Intermediate code generation.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Basic Semantics Associating meaning with language entities.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Introduction to Java Java Translation Program Structure
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Pointers. Variable Declarations Declarations served dual purpose –Specification of range of values and operations –Specification of Storage requirement.
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
CPS120: Introduction to Computer Science Variables and Constants.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
CS 404 Introduction to Compiler Design
Information and Computer Sciences University of Hawaii, Manoa
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Advanced Computer Systems
Component 1.6.
VBA - Excel VBA is Visual Basic for Applications
Working with Java.
A Simple Syntax-Directed Translator
Analysis of Algorithms
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compilers.
C# and the .NET Framework
PROGRAMMING LANGUAGES
Programmazione I a.a. 2017/2018.
FORTRAN 04 February 1999; CS655.
Chap. 6 :: Control Flow Michael L. Scott.
Designing and Debugging Batch and Interactive COBOL Programs
Chapter 10 Programming Fundamentals with JavaScript
CMPE 152: Compiler Design September 18 Class Meeting
Chapter 6 Intermediate-Code Generation
CSE401 Introduction to Compiler Construction
CMPE 152: Compiler Design October 4 Class Meeting
Lecture 18 Arrays and Pointer Arithmetic
Chap. 6 :: Control Flow Michael L. Scott.
Statement-Level Control Structures
Homework Any Questions?.
Intermediate Code Generation
Course Overview PART I: overview material PART II: inside a compiler
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Corresponds with Chapter 5
SPL – PS1 Introduction to C++.
Presentation transcript:

C-- by Aksel Gresvig

Outline ● What is C--? How does it work? ● What C-- is not ● Relationship to C ● What does it look like? Examples ● Elements of C-- syntax – The C-- type system – The data directive (arrays) – Procedures – Statements – Control flow examples Questions as we go please! 1

What is C--? ● Not your average programming language ● C-- is for people who wish to implement their own language/write compilers ● You compile your high-level language to C--, which then can produce efficient machine language ● It is in many ways an intermediate representation with syntax ●..which makes it easier to debug & inspect ● You (usually) do not write code directly in C-- ● C-- is independent of both source programming language and target architecture 2

The classical sequence ● Hi-level language --(Front end compiler)--> Intermediate representation --(Back end compiler)--> Machine language ● A drawing please.. 3

The C-- sequence ● Hi-level language --(Front end compiler)--> C-- --(Back end compiler)--> Machine language ● Change the drawing please.. ● And it has been done! Front ends for C-- exist 4

A client example ● A client of C-- is divided into two parts: – The front-end compiler generates programs written in the C-- language, which the C-- compiler translates into e ffi cient machine code. – This code interoperates with the front-end run-time system, which implements such services as garbage collection, exception dispatch, thread scheduling, and so on (like Java Virtual Machine) A quick drawing.. 5

What C-- is not ● C-- is not an execution platform, such as the Java Virtual Machine or the.NET Common Language Runtime ● C-- is not “write-once, run-anywhere”. It conceals most architecture-specific details, such as the number of registers, but it exposes some 6

Relationship to C Why isn't C-- a superset of C? ● C is a programming language designed for human programmers, whereas C-- is a compiler target language. ● There are a few features in C that are actually incompatible with C--, most notably support for varargs procedures ● C-- deliberately provides different notation for many things that C can do. For example, where C would have "*p", we write "bits32[p]" in C-- ● The C standard leaves too much up to the implementation, including the representations of structures, the sizes of the built-in types, and the meanings of the operators 7

Relationship to C OK, why isn't C-- a subset of C? ● For efficient compilation of modern languages, C-- needs features that C just doesn't provide efficiently: – Ability to return multiple values in registers – Optimized tail calls to any procedure – Global variables bound to registers – Ways to tie garbage-collection information to particular program points – Support for exceptions – Support for lightweight concurrency 8

OK, lets see what it looks like ● Much of C-- is unremarkable. C-- has parameterized procedures with declared local variables. ● A procedure body consists of a sequence of statements, which include (multiple) assignments, conditionals, gotos, calls, and jumps (tail calls) ● We will look closer at these specific features later, but first lets see some examples 9

Example 1 ● This program computes the sum and product of integers 1 to n.. ●..using ordinary recursion 10

Example 2 ● This program computes the sum and product of integers 1 to n.. ●..using tail recursion 11

Example 3 ● This program computes the sum and product of integers 1 to n.. ●..using loops 12

The C-- Type System ● The C-- type system is only there to help the back end choose the proper machine instruction for an operation, and to help it make effective use of condition codes ● There are just two types in C--: – The k-bit value has type bitsk. For example, a four byte word is specified as bits32 – The Boolean value has type bool. They govern control flow in conditionals. 13

The data directive (arrays) ● A datum reserves memory and may also define its initial contents ● Specifically, a datum is defined by a type, the number of elements of that type to reserve memory for, and optionally the initial values of the elements: ● datum ⇒ type [size] [init] ; ● The amount of memory reserved is determined by the type and the number of elements ● Similar to C ( int my_array[] = {1,23,17,4,-5,100}; ) 14

The data directive (arrays) cont'd. ● Since both the size and the initial values are syntactically optional, a number of variants exist: 1. type ; Memory is reserved for one element of type type with unspecified initial value. 2. type [ expr ] ; The compile-time constant expression expr defines a nonnegative integer n. Memory is reserved for n elements of type type with unspecified initial values. 3. type [ ] { expr, expr, } ; The syntax allows initialization to be specified by zero or more link-time constant expressions, with the comma used either as a separator or a terminator. 15

Procedures ● A C-- procedure definition plays a dual role: it is both data and code. Like initialized data, it reserves space with specific contents, but the contents are not values; they are target-machine instructions 16

Procedures cont'd. ● Overall, C-- procedures are quite a bit like C procedures, but there are many di ff erences: – A C-- procedure may not only accept an arbitrary number of parameters, but may return an arbitrary number of results – Calls to procedures are not typechecked. Nr and types of parameters at call site must match procedure def., so must calling convention. Any mismatch is an unchecked run-time error 17

Procedures cont'd. ● Procedure syntax: – procedure ⇒ [conv] name ( [formals] ) { body } – formals ⇒ formal {, formal} [,] – formal ⇒ [kind] invariant type name ● conv is the calling convention used to call the procedure ● name is name of the procedure; it denotes an immutable value of the native pointer type ● formals are the formal parameters, if any. The formal parameters are in the scope of the procedure body and denote run-time values of the declared types. 18

Procedures cont'd. ● The body of a procedure is a sequence of decls (declarations of local register variables or of stack- allocated data) ●..interleaved with a sequence of stmts (statements) ● The sequence of statements specifies a control-flow graph ● Every path in such a graph must end in jump, return, or cut to; ●..a procedure in which control “falls o ff the end” is rejected by the C-- compiler with a checked compile-time error. 19

Statements ● A statement can read and write memory and registers, and a statement can change the flow of control ● Statements appears only within procedures ● Here are all of them, lets look closer at a few only: 20

Control flow example 1 ● The switch-statement – stmt ⇒ switch expr { arm } – arm ⇒ case range, range, : { body } 21

Control flow example 2 ● Control labels and goto ● stmt ⇒ name : ● | goto expr [targets name {, name} [,]] ; 22

Thats it!