The New C Standard (Excerpted material)

Similar documents
The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

A Fast Review of C Essentials Part II

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

6. Control Statements II

Concepts Introduced in Chapter 7

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

The New C Standard (Excerpted material)

Programming Languages Third Edition. Chapter 7 Basic Semantics

QUIZ Friends class Y;

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lecture Notes on Static Single Assignment Form

G Programming Languages - Fall 2012

What about Object-Oriented Languages?

The New C Standard (Excerpted material)

Chapter 5. Names, Bindings, and Scopes

Run-time Environments

Run-time Environments

Today's Topics. CISC 458 Winter J.R. Cordy

Control Flow Analysis

Review of the C Programming Language


Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

The New C Standard (Excerpted material)

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Chapter 5 Names, Binding, Type Checking and Scopes

UNIT-4 (COMPILER DESIGN)

Programming Languages: Lecture 12

6. Operatoren. 7. Safe Programming: Assertions. Table of Operators. Table of Operators - Explanations. Tabular overview of all relevant operators

Implementing Subroutines. Outline [1]

Informatica 3 Marcello Restelli

Intermediate Code Generation

When do We Run a Compiler?

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CSCI312 Principles of Programming Languages!

Types and Type Inference

Programming Languages

The New C Standard (Excerpted material)

M1-R4: Programing and Problem Solving using C (JAN 2019)

Contents of Lecture 3

Chapter 10. Implementing Subprograms ISBN

Contents. 8-1 Copyright (c) N. Afshartous

Template Issue Resolutions from the Stockholm Meeting

Names, Scopes, and Bindings II. Hwansoo Han

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Implementing Subprograms

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The Structure of a Syntax-Directed Compiler

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

The New C Standard (Excerpted material)

Fundamentals of Programming Session 12

Weeks 6&7: Procedures and Parameter Passing

Short Notes of CS201

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Properties of an identifier (and the object it represents) may be set at

Object-Oriented Principles and Practice / C++

Intermediate Representations & Symbol Tables

CS201 - Introduction to Programming Glossary By

Semantic Analysis. Compiler Architecture

Procedural Programming & Fundamentals of Programming

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

The role of semantic analysis in a compiler

Fundamentals of Programming Languages. PL quality factors Lecture 01 sl. dr. ing. Ciprian-Bogdan Chirila

Chapter 10. Implementing Subprograms

Machine-Independent Optimizations

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Lecture Notes on Static Single Assignment Form

// the current object. functioninvocation expression. identifier (expressionlist ) // call of an inner function

Goals of this Lecture

Motivation was to facilitate development of systems software, especially OS development.

G Programming Languages - Fall 2012

8. Control statements

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Programming Languages, Summary CSC419; Odelia Schwartz

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Topic 9: Type Checking

Topic 9: Type Checking

Memory optimisation in a first-order dataflow synchronous language

Chapter 5 Names, Bindings, Type Checking, and Scopes

American National Standards Institute Reply to: Josee Lajoie

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Transcription:

The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved.

1788 goto statement Constraints The identifier in a goto statement shall name a label located somewhere in the enclosing function. The situation where a label having a corresponding name does not occur within the enclosing function is likely to be some kind of fault. Having translators issue a diagnostic is probably the most useful behavior for the standard to specify. Other Languages Some languages that support nested function definitions (e.g., Pascal) only require that the label name be visible (i.e., it is possible to jump to a label in a different function). Other languages (e.g., Algol 68) give labels block scope, which restricts its visibility. Perl has a form of goto that causes the function named by the label to be called. However, when that function returns control is returned to the function that called the function that performed the goto (i.e., the behavior is as-if control returns to the goto which then immediately performs a return). 1787 goto VLA lifetime starts/ends storage layout Storage allocation for objects having a variably differs from storage allocation for objects having other types in that it requires internal implementation housekeeping code to be executed, when its definition is encountered during program execution, for it to occur (storage for objects having other types can be reserved at translation time). Similarly, storage deallocation requires housekeeping code to be executed when the lifetime of an object having a variably ends (implementation techniques that have no dependency between execution of an objects definition and the termination of its lifetime discussed elsewhere); storage deallocation for objects having other types does not require any execution time actions. This constraint, along with an equivalent one for the switch statement, provides a guarantee to imple- mentations that if the storage deallocation code is executed, then the storage allocation code will have been previous executed (i.e., there is no way to bypass it in a conforming program). Jumping from outside of the scope of an identifier denoting such an object to inside the scope of that identifier bypasses execution of its definition. Bypassing the definition might be thought to be harmless if the object is never referenced. However, storage for the object has to be deallocated when its lifetime terminates, which is a (implicit) reference to the object. This constraint does not require the identifier to be visible at the jumped to label, only that the label be within the scope of the identifier. Also, there is no constraint prohibiting the block containing the goto statement from containing an object defined to have a variably and the destination of the jump to be outside of that block. VLA lifetime starts/ends switch A goto statement shall not jump from outside the scope of an identifier having a variably to inside the scope of that identifier. C90 Support for variably s is new in C99. C++ Support for variably s is new in C99 and they are not specified in the C++ Standard. However, the C++ Standard contains the additional requirement that (the wording in a subsequent example suggests that being visible rather than in scope more accurately reflects the intent): 1788 6.7p3 A program that jumps 77) from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5). v 1.1 January 30, 2008

1790 A C function that performs a jump into a block that declares an object with an initializer will cause a C++ translator to issue a diagnostic. 1 void f(void) 2 { 3 goto lab; /* strictly conforming */ 4 // ill-formed 5 int loc = 1; 6 7 lab: ; 8 Semantics 1789 A goto statement causes an unconditional jump to the statement prefixed by the named label in the enclosing goto causes unconditional function. jump Unless prefixed by a label, statements appearing after a goto statement, in the same block, are dead code. Other Languages Some languages (e.g., Pascal) support jumping to statements contained within other functions. Common Implementations There are a number of practical algorithms for analyzing program control flow that depend on the graph of the control flow being reducible [2, 4] (i.e., not irreducible; an irreducible graph might be thought of one that contains a cycle with multiple entry points). To create an irreducible flow graph in C requires the use of the goto statement, for instance: 1 if (a > b) 2 goto L_1; 3 L_2: a++; 4 L_1: if (a < 3) 5 goto L_2; Although algorithms are available for transforming an irreducible flow graph into a reducible one, [5] many optimizers and static analyzers don t perform such transformations (because of the potentially significant increase in code size created by the node splitting algorithms, [3] and because it is believed that function definitions rarely have a control flow that is irreducible [1] ). For this reason the quality of the machine code generated for the few functions that do contain an irreducible control flow graph can be much lower than that for other functions (because irreducibility can prevent some of the information needed for optimization being deduced). dead code 1790 EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. The goto following outline presents one possible approach to a problem based on these three assumptions: EXAMPLE 1. The general initialization code accesses objects only visible to the current function. 2. The general initialization code is too large to warrant duplication. 3. The code to determine the next operation is at the head of the loop. (To allow it to be reached by continue statements, for example.) goto first_time; for (;;) { // determine next operation January 30, 2008 v 1.1

1791 if (need to reinitialize) { // reinitialize-only code first_time: // general initialization code continue; // handle other operations While this might seem like a contrived example, the same could probably be said for all examples of the use of the goto statement. EXAMPLE goto variable length EXAMPLE 2 A goto statement is not allowed to jump past any declarations of objects with variably s. A jump within the scope, however, is permitted. 1791 goto lab3; { double a[n]; a[j] = 4.4; lab3: a[j] = 3.3; goto lab4; a[j] = 5.5; lab4: a[j] = 6.6; goto lab4; // invalid: going INTO scope of VLA. // valid: going WITHIN scope of VLA. // invalid: going INTO scope of VLA. goto 1788 It is a constraint violation for a goto statement to jump past any declarations of objects with variably modified types. v 1.1 January 30, 2008

References 1. F. E. Allen. Control flow analysis. SIGPLAN Notices, 5(7):1 19, 1970. 2. R. Allen and K. Kennedy. Optimizing Compilers for Modern Architecture. Morgan Kaufmann Publishers, 2002. 3. L. Carter, J. Ferrante, and C. Thomborson. Folklore confirmed: Reducible flow graphs are exponentially larger. In Proceedings of the 30 th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL 03), pages 106 114, Jan. 2003. 4. M. S. Hecht. Flow Analysis of Computer Programs. Elsevier, North- Holland, 1977. 5. S. Unger. Transforming Irreducible Regions of Control Flow into Reducible Regions by Optimized Node Splitting. PhD thesis, Humboldt Universität zu Berlin, 1998. January 30, 2008 v 1.1