Realizing C++11 Lambda Expression in Open64

Realizing C++11 Lambda Expression in Open64

Realizing C++11 Lambda Expression in Open64 Javed Absar Anitha Boyapati Dibyendu Das AMD, India AMD, India AMD, India Richmond Road Richmond Road Richmond Road Bangalore Bangalore Bangalore +91 9901880710 +91 7795334034 +91 9448537014 [email protected] [email protected] [email protected] ABSTRACT higher-order functions. Higher order functions are functions that C++11 is the latest edition of C++ programming language take one or more functions as input and output a function as standard by ISO. It replaces C++03. It includes a number of core return value [4]. Their major use is to abstract common behaviour language extensions, probably the most interesting of which is the into one place [5]. inclusion of the lambda-expressions. The onus is now upon Higher order functions are closely related to first-class functions. compiler writers – gcc, llvm, Open64 and others, to incorporate The distinction between the two is that higher-order function this extension into their existing compiler infrastructure so that describes a mathematical concept while first-class object is a programmers could benefit from this powerful language feature. computer science term that describes programming language Incorporating lambda-expression is not so straight-forward an entity that has no restriction on its use. First-class functions can extension for a compiler. It needs a good understanding of the therefore appear anywhere in the program where other first-class C++11 standard and the many possible intricate use and misuse of entities such as numbers can appear, including as arguments to this language feature in programs. In this paper, we analyze other functions and as the return value. lambda expression from a language-feature perspective, the value it provides to programmers and how Open64 could support it. In mathematics, higher order functions are also known as operators . For example, the definite integral in calculus is an operator that given a function f of a real variable x, and an interval Categories and Subject Descriptors [a, b] on the real line, returns the area under its curve. Later we D.3.4 [Programming Languages ]: Processors – compilers. will illustrate implementation of this operator using anonymous- function construct of C++11. General Terms Anonymous function and lambda-expression are sometimes used Algorithms, Design, Languages and Theory. in a mixed and confusing way, more so in programming language contexts than in mathematical expositions. In mathematics, Keywords lambda-expression is a notational convention in support of lambda expression, anonymous function, C++11, C++0x, closure, lambda calculus. Lambda expression in programming is an higher order function, Open64, WHIRL, compiler. expression that specifies an anonymous function object [6]. Since anonymous functions in programming languages can have statements (control, assignment or expression statement) in the 1. INTRODUCTION function body, the mathematical purity is lost to some extent. The first programming language to adopt anonymous functions C++11 [1] was approved by the ISO in August 2011 as the new was Lisp (1958). Traditionally anonymous functions have found standard for the C++ programming language, replacing C++03. good use in functional languages and languages that treat The name C++11 is derived from the convention of naming functions as first-class objects, such as Haskell, Scheme, ML and language versions based on the year of publication. Lisp [7][8]. In the current era of multi-paradigm languages, many C++11 includes several addition to the core language. In the imperative, procedural and object-oriented languages have added design of C++11 standard, the committee had applied some anonymous class and anonymous functions to their repertoire of directives to help guide their decision. One of the directives was language features – C#, Clojure, Java, JavaScript, PHP, amongst “to prefer changes that can evolve programming technique”. The others. C++ added anonymous function in its C++11 edition. inclusion of anonymous functions or lambda function is a direct Support for anonymous function in C# (.NET) [9] has improved result of that objective. with new versions of the compiler. In C# 1.0, one would create an Anonymous function is a function which is defined and invoked instance of a delegate by explicitly initializing it with a method without being bound to an identifier. Anonymous functions have that was defined elsewhere in the code. In C# 2.0, a delegate their origin in the works of Alonzo Church on λ-calculus [2][3]. could be initialized with inline code, called anonymous method. They are a convenient way to pass functions as arguments to In C# 3.0, a delegate could be initialized with a lambda expression, which is more expressive and concise. E.g. in 2.0 the Permission to make digital or hard copies of all or part of this work for personal or parameter type had to be defined twice (during declaration of classroom us e is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation delegate and during initialization) and this was done away in 3.0. on the first page. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. PLDI’12 . Copyright © 2012 ACM 1 -59593 -XXX -X/0X/000X…$5.00. C++11 has leapfrogged this by removing most redundancies seen in earlier versions of C# and gives a spartan expression of anonymous function definition and invocation. Anonymous functions discussion requires an understanding of the concept of closure. A closure is a function together with a referencing environment for the non-local variables of the function. Or to quote the ISO/IEC sub-committee technical report [9], “ Closure is an anonymous function object that is created automatically by the compiler as a result of evaluating a lambda expression. Closure consists of the code of the body of the lambda function and the environment in which the lambda function is Figure 1. Computing definite integral by partitioning the area defined ”. In practice, this means that external variables referred to under the curve into rectangles in the body are stored (as reference or as copied-value) as member variables of the anonymous function object. Or that a pointer to the frame where the lambda function was created is stored in the The key limitation here is that the pointed-to function ptr2func function object. has to be defined separately from the context in which integrate will be called from. Suppose we need to compute integral of f(x) The concept of closure was developed in 1960s and featured in = u/x+v. We define it as below and then pass a pointer to Scheme programming. C++11 support closures in two default func_inverse each time we need to integrate f (x) = u/x+v. forms. One stores a copy of the variable, the other stores references to the original variables. Both provide functionality to override this default behaviour for individual variables. double func_inverse(double x ) { A key limitation of the C++11 lambda feature, however, is that return u/x+v; C++11 closures are monomorphic . That is, their type does not } adapt to the context in which they are called. This is unlike C#. 2. Organization of the Paper Different contexts from which integrate may be called may have different values of u and v. To overcome this problem, we are forced to set u and v as globals, a highly undesirable solution. In Sec 3, we provide the advantages of using anonymous function. Sec 4 discusses the formal syntax and semantics of C++11 lambda-expression illustrating key concepts with examples. Sec 5 3.2 Integral – Function Object gets into the details of implementing anonymous function in the front-end and in Open64 [12]. Sec 6 forms the conclusion. In C++ prior to C++11, we can use function object or functor to solve the same integral problem in a more elegant manner. A 3. Lambda Expression – Motivation functor is a construct that allows an object to be called as if it were a function. We construct a class (e.g. CFoo ) which overloads In this section, we illustrate with examples the advantages of the function call operator ( ) member function. In C++ this is C++11 lambda functions over other forms of expression – such as called class type functor. function pointers of C and object function of C++03. 3.1 Integral – Function Pointers class CFoo{ public: double u,v; Consider the problem of writing a function to compute the definite integral [a,b] of a mathematical function f : R→R. The basic double operator( )(double x){ return u/x+v; } approach is to divide the area under the curve of f into very }; narrow rectangles and sum the area of all the rectangles. The double integrate( double a, double b, CFoo f){ integrate function is then actually a higher-order function as it int i; double sum =0, dt = (b-a)/N; takes not a value but any continuous function as its input. for( int i = 0 ; i < N; i++ ) sum += f(a+i*dt)*dt;… Now, if we limit ourselves to implementing integral in C, we can use function pointer as shown below. return sum; } int main( ){ double integrate(double a, double b, double (*ptr2func)(double) ) CFoo f_inv_x; { f_inv_x.u = … ; f_inv_x.v = …; double a=… b =…; int i; double sum =0, dt = (b-a)/N; // N is number of segments double t = integrate (a, b, f_inv_x); … for( i = 0; i < N; i++ ) sum += ptr2func(a+i*dt) * dt ; } return sum; Figure 2 Computing definite integral using functor } The code above demonstrates how the idiom of functor is double t = integrate (a, b, [u,v ](double x){ return u/x+v;} ); typically used.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us