Metaprogramming - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Metaprogramming

Description:

Meta means 'about', or 'at a higher level' ... Bash shell script #!/bin/bash. echo '#!/bin/bash' program. for ((I=1; I =1000; I )); do ... – PowerPoint PPT presentation

Number of Views:349
Avg rating:3.0/5.0
Slides: 20
Provided by: Ken667
Category:

less

Transcript and Presenter's Notes

Title: Metaprogramming


1
Metaprogramming
  • (Some slides based on Veldhuizens)

2
Metaprogramming
  • What does meta mean?
  • Meta means about, or at a higher level.
  • A metaprogram is a program that writes or
    manipulates its own source code.
  • (Sometimes broadened to include a program that
    writes or manipulates other programs.)

3
Stages
  • Normally, a program gets executed in one
    conceptual step.

Source
MachineLanguage
Compiler
CPU
Source
Interpreter
  • If there is a loop in the source, there is a loop
    in the machine language, etc.

4
  • In metaprogramming, the source code rewrites or
    modifies itself, to generate a new version.

SourceVersion 1
SourceVersion 2
CompilerStage 1
CompilerStage 2
5
  • There could be multiple stages.
  • The language and compiler at each stage could be
    exactly the same.
  • Or could be different.

SourceVersion 1
SourceVersion 3
CompilerStage 1
CompilerStage 3
SourceVersion 2
SourceVersion 4
CompilerStage 2
CompilerStage 4
6
Examples
  • Bash shell script
  • !/bin/bashecho '!/bin/bash' gtprogramfor
    ((I1 Ilt1000 I)) do echo "echo I"
    gtgtprogramdonechmod x program./program

7
C/Perl
  • A C/Perl hybrid
  • void func(/ BEGIN EMBEDDED PERL /for (i 1
    i lt 10 i) print int ai,print
    int ai/ END EMBEDDED PERL /)
  • How do we compile this code?
  • Write a front-end driver that
  • Takes the input file, finds the sections
    bracketed by BEGIN and END lines.
  • Runs those sections through Perl interpreter,
    save output
  • Takes the output, splices them back into the
    original file, replacing the embedded Perl
    sections.
  • Then runs the file through the C compiler.

8
  • Original
  • void func(/ BEGIN EMBEDDED PERL /for (i 1
    i lt 4 i) print int ai, print
    int ai\n/ END EMBEDDED PERL /)
  • After stage 1 compilation
  • void func(int a1, int a2, int a3, int a4, int
    a5)

9
Metaprogramming vs. Preprocessing
  • Macros and conditional compilation can be thought
    of as a very simple kind of metaprogramming.
  • if 1 2 gt 5 printf(Version 1)else
    printf(Version 2)endif
  • However, the preprocessor is not rich enough
    (Turing complete), to support a real program.

10
  • For example, cannot do this
  • for i 1 to 3emit void fi()
    printf(funci\n)
  • which using our hypothetical preprocessor would
    output
  • void f1() printf(func1\n) void f2()
    printf(func2\n) void f3()
    printf(func3\n)

11
C Metaprogramming
  • Turns out that in C, you can abuse the template
    system to actually make the compiler compute
    things.
  • Stroustrup never imagined or intended this, so it
    really is abusing the template system.

12
Enumerations
  • Can be used in templates
  • template lttypename T, int Ngtstruct A enum
    NUMBER 2N
  • Altint, 3gtNUMBER ?

13
Recursive Templates
  • Templates can be recursive
  • template ltint Ngtclass Pow3 enum
    VALUE3Pow3ltN-1gtVALUE
  • What is the value of Pow3lt2gtVALUE?
  • Expand it out
  • Pow3lt2gtVALUE 3Pow3lt1gtVALUE
    3(3Pow3lt0gtVALUE) 3(3(3Pow3lt-1gtVALUE))

14
  • How do we make it terminate?
  • Use specialization (or partial specialization).
  • template ltint Ngtstruct A enum NUMAltN
    1gtNUM template ltgtstruct Alt0gt enum
    NUM1234
  • What is the value of Alt2gtNUM?
  • Alt2gtNUM Alt1gtNUM Alt0gtNUM 1234

15
  • A template to compute 2N
  • template ltint Ngtstruct Pow2 enum
    VALUE2Pow2ltN-1gtVALUE template
    ltgtstruct Pow2lt1gt enum VALUE2
  • What is the value of Pow2lt3gtVALUE?
  • Expand it out
  • Pow2lt3gtVALUE 2Pow2lt2gtVALUE
    2(2Pow2lt1gtVALUE) 2(2(2)) 8

16
Another Specialization Example
  • Consider
  • template ltint N, int Mgtstruct IsSame enum
    RESULT0 template ltint Ngtstruct
    IsSameltN, Ngt enum RESULT1
  • IsSamelt0, 1gtRESULT is ?.
  • IsSamelt1, 1gtRESULT is ?.

17
Metaprogramming Code
  • Suppose we want to find a specific value in an
    array
  • int find(int a, int n, int v) for (int i
    0 i lt n i) if (ai v)
    return i return -1
  • Now suppose we want to unroll the loop for small
    arrays.
  • int find3(int a3, int v) if (a0 v)
    return 0 if (a1 v) return 1 if
    (a2 v) return 2 return -1
  • Now, how do we do this for all array sizes from 1
    to 20?
  • int find1() int find2() int
    find3()

18
  • Use metaprogramming to generate all the different
    versions.
  • template ltint Ngt struct Find static int
    find(int a, int v) if (a0 v)
    return 0 else return
    1 FindltN-1gtfind(a1, v)template ltgt
    struct Findlt1gt static int find(int a, int
    v) if a0 v) return 0
    else return -1
  • Use it like
  • int a10int index Findlt10gtfind(a,
    1234)

19
  • template ltint Ngtstruct Find int find(int
    a, int v) if a0 v)
    return 0 else return
    1findltN-1gtfind(a1, v)
  • Expand to see how it works
  • int Findlt3gtf(a, v) if (a0 v) return
    0 else return 1 Findlt2gtf(a 1,
    v)int Findlt2gtf(a, v) if (a0 v)
    return 0 else return 1 Findlt1gtf(a 1,
    v)int Findlt1gtf(a, v) if (a0 v)
    return 0 else return -1
Write a Comment
User Comments (0)
About PowerShow.com