Presentation is loading. Please wait.

Presentation is loading. Please wait.

Efficient Runtime Code Generation in Rotor Antonio Cisternino 23/4/2003 Università di Pisa Supported by Microsoft Research grant.

Similar presentations


Presentation on theme: "Efficient Runtime Code Generation in Rotor Antonio Cisternino 23/4/2003 Università di Pisa Supported by Microsoft Research grant."— Presentation transcript:

1 Efficient Runtime Code Generation in Rotor Antonio Cisternino 23/4/2003 Università di Pisa Supported by Microsoft Research grant

2 Introduction Dynamic code generation is a programming technique that is being widely used in complex systemsDynamic code generation is a programming technique that is being widely used in complex systems Strongly Typed Execution Environments (STEE), such as JVM and Microsoft CLR, provide rich services to running code (dynamic loading, security, …)Strongly Typed Execution Environments (STEE), such as JVM and Microsoft CLR, provide rich services to running code (dynamic loading, security, …) To enforce type safety at load time STEE should retain enough information in binaries about types (metadata)To enforce type safety at load time STEE should retain enough information in binaries about types (metadata) It is convenient for PL targeting STEE to share a significant amount of programming abstractionsIt is convenient for PL targeting STEE to share a significant amount of programming abstractions

3 Encoding Code Generation into Binaries Programming language Intermediate (or machine) language Real transformation Perceived transformation Language Level RTCG EE level RTCG Type T

4 What we did? We introduced a Code type to represents code valuesWe introduced a Code type to represents code values Code instances are built around methodsCode instances are built around methods A single operation called Bind allow code manipulationA single operation called Bind allow code manipulation public static int add(int i, int j) { return i + j; } return i + j; } Code c = new Code(typeof(T).GetMethod("add")); Code inc = c.Bind(1, new Free()); Delegate int F(int i); F f = (F)inc.MakeDelegate(typeof(F));

5 Binding Values (Bind v ) Values can be bound to argumentsValues can be bound to arguments All values can be bound to an input argument:All values can be bound to an input argument: Code c = SelectCodeGen(); DBConnection conn = new Connection(…); c.Bind(conn);// Fix the connection

6 Binding Code Values to Args (Bind  ) An input argument of a code value can be bound with another code value returning a value (compatible with it)An input argument of a code value can be bound with another code value returning a value (compatible with it) Arguments of the code value bound to an argument are lifted into the signature of the resulting code valueArguments of the code value bound to an argument are lifted into the signature of the resulting code value Code c = CodeOfAdd(); // c is x,y.x+y Code d = c.Bind(c, new Free()); // d is x,y,z.x + y + z

7 Splicing Code Values (Bind  ) If the argument is of higher order type (a delegate type in CLI) we can splice a code value in place of invocations of the argumentIf the argument is of higher order type (a delegate type in CLI) we can splice a code value in place of invocations of the argument With this mechanism it is possible to write methods with holes for other methodsWith this mechanism it is possible to write methods with holes for other methods delegate void Cmd(int i); static void For(Cmd c) { for (int j; j < 10; j++) c(j); } for (int j; j < 10; j++) c(j); } static void Print(int i) { Console.WriteLine("Iteration {0}", i); } Console.WriteLine("Iteration {0}", i); } Code f = CodeOfFor(), p = CodeOfPrint(); Code c = f.Bind(new Splice(p));

8 Formal Model LetLet –c = addCode.Bind(addCode.Bind(2, new Free()), new Free()), we expect: –add(add(2, x), y) == c(x, y) We have defined a transformation on IL code that perform substitution of code fragmentsWe have defined a transformation on IL code that perform substitution of code fragments Theorem: the code generated by the transformation preserves the expected semanticsTheorem: the code generated by the transformation preserves the expected semantics Theorem: only well-formed and type-safe code can be generatedTheorem: only well-formed and type-safe code can be generated

9 Applications Bind  is useful to turn interpreter in dummy compilersBind  is useful to turn interpreter in dummy compilers Our current implementation is pretty fast: it is suitable for runtime code generationOur current implementation is pretty fast: it is suitable for runtime code generation General combinators can be defined to generate arbitrary programsGeneral combinators can be defined to generate arbitrary programs It is possible to express staged computations because transformation is IL -> ILIt is possible to express staged computations because transformation is IL -> IL

10 RE: Benchmark It is a worst case test: we use debug code!!!It is a worst case test: we use debug code!!! Compilation time of Code Bricks version is about one third of the SSCLI versionCompilation time of Code Bricks version is about one third of the SSCLI version

11 Code Bricks and Multi-Staging Multi stage programs are programs that, when executed, produce other programsMulti stage programs are programs that, when executed, produce other programs The program is annotated so that the whole execution can be controlled in a single sourceThe program is annotated so that the whole execution can be controlled in a single source Real applications make use of multi-stage programming though based on interpreters and preprocessors: examples are PHP and ASP technologiesReal applications make use of multi-stage programming though based on interpreters and preprocessors: examples are PHP and ASP technologies One essential aspect of Code Bricks is that code transformation is performed at IL level so it is easy to write a code which outputs a function which makes use of Code values insideOne essential aspect of Code Bricks is that code transformation is performed at IL level so it is easy to write a code which outputs a function which makes use of Code values inside Usually this would be possible only by shipping an interpreter together with the programUsually this would be possible only by shipping an interpreter together with the program

12 MetaML and Code Bricks The four operators of MetaML:The four operators of MetaML: 1.Brackets <> construct a code fragment 2.Escape ~ combines code fragments 3.Run run executes a code fragment 4.Lift lift constructs a code fragment from a ground value MetaML compiler can rely on Code Bricks to implement staging operatorsMetaML compiler can rely on Code Bricks to implement staging operators

13 Code Bricks and Rotor Current implementation supports with.NET and SSCLI for Windows (CLIFileReader depends on file mapping)Current implementation supports with.NET and SSCLI for Windows (CLIFileReader depends on file mapping) RE benchmark has been possible because sources are availableRE benchmark has been possible because sources are available MakeDelegate is implemented with Reflection.Emit: we are changing Rotor to support Code objectsMakeDelegate is implemented with Reflection.Emit: we are changing Rotor to support Code objects Code generated by CodeBricks is associated with a delegate: garbage collection of code can be done when the delegate is collectedCode generated by CodeBricks is associated with a delegate: garbage collection of code can be done when the delegate is collected

14 Conclusions Code generation is expressed by means of combination of code values (basically methods)Code generation is expressed by means of combination of code values (basically methods) Method application notion offers control over the generated codeMethod application notion offers control over the generated code It is possible to express staged computations that can even be ‘cross process’It is possible to express staged computations that can even be ‘cross process’ It is possible to derive properties of generated code relying on the structure of the bindingIt is possible to derive properties of generated code relying on the structure of the binding CodeBricks will be soon available! (http://dotnet.di.unipi.it/Code)CodeBricks will be soon available! (http://dotnet.di.unipi.it/Code)http://dotnet.di.unipi.it/Code


Download ppt "Efficient Runtime Code Generation in Rotor Antonio Cisternino 23/4/2003 Università di Pisa Supported by Microsoft Research grant."

Similar presentations


Ads by Google