Download presentation
Presentation is loading. Please wait.
Published byWalter Morgan Modified over 8 years ago
1
C-- by Aksel Gresvig
2
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
3
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
4
The classical sequence ● Hi-level language --(Front end compiler)--> Intermediate representation --(Back end compiler)--> Machine language ● A drawing please.. 3
5
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
6
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
7
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
8
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
9
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
10
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
11
Example 1 ● This program computes the sum and product of integers 1 to n.. ●..using ordinary recursion 10
12
Example 2 ● This program computes the sum and product of integers 1 to n.. ●..using tail recursion 11
13
Example 3 ● This program computes the sum and product of integers 1 to n.. ●..using loops 12
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
Control flow example 1 ● The switch-statement – stmt ⇒ switch expr { arm } – arm ⇒ case range, range, : { body } 21
23
Control flow example 2 ● Control labels and goto ● stmt ⇒ name : ● | goto expr [targets name {, name} [,]] ; 22
24
Thats it!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.