Calling Variadic Functions from a Strongly Typed Language Matthias - - PowerPoint PPT Presentation

calling variadic functions from a strongly typed language
SMART_READER_LITE
LIVE PREVIEW

Calling Variadic Functions from a Strongly Typed Language Matthias - - PowerPoint PPT Presentation

Calling Variadic Functions from a Strongly Typed Language Matthias Blume Toyota Technological Institute at Chicago Mike Rainey University of Chicago John Reppy University of Chicago Variadic functions in C int printf (const char *, ...); M.


slide-1
SLIDE 1

Calling Variadic Functions from a Strongly Typed Language

Matthias Blume Toyota Technological Institute at Chicago Mike Rainey University of Chicago John Reppy University of Chicago

slide-2
SLIDE 2

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Variadic functions in C

int printf (const char *, ...);

slide-3
SLIDE 3

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Variadic functions in C

int printf (const char *, ...); printf (“%d”, 10);

slide-4
SLIDE 4

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Variadic functions in C

int printf (const char *, ...); printf (“%d”, 10); printf (“%g: %d(%f)\n”, 10.0, 3, 0.25);

slide-5
SLIDE 5

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-6
SLIDE 6

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-7
SLIDE 7

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-8
SLIDE 8

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-9
SLIDE 9

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-10
SLIDE 10

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-11
SLIDE 11

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-12
SLIDE 12

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Outline

  • Why are we doing this?
  • How does calling variadic functions work in C?
  • Why doesn’t the same approach work in ML?
  • Located Arguments via Staged Allocation: Our solution to the (low-level part
  • f the) problem
  • Conclusions
  • High-level interface via “Danvy-style” typing
slide-13
SLIDE 13

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Why?

slide-14
SLIDE 14

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Why?

  • Being able to call printf?
  • no
slide-15
SLIDE 15

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Why?

  • Being able to call printf?
  • no
  • Utility:
  • Some APIs rely heavily on variadic functions
slide-16
SLIDE 16

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Why?

  • Being able to call printf?
  • no
  • Utility:
  • Some APIs rely heavily on variadic functions
  • Completeness:
  • NLFFI models the entire C type system - but (until now) with the single

exception of variadic functions

slide-17
SLIDE 17

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-18
SLIDE 18

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-19
SLIDE 19

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-20
SLIDE 20

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-21
SLIDE 21

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-22
SLIDE 22

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-23
SLIDE 23

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-24
SLIDE 24

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-25
SLIDE 25

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a fixed-arity C function Prototype: int f (int, double, float, char, void *); Call: j = f (i, x, w, c, p);

slide-26
SLIDE 26

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from C Call: j = f (i, x, w, c, p);

slide-27
SLIDE 27

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from C Types: int i; double x; float w; char c; void *p; Call: j = f (i, x, w, c, p);

slide-28
SLIDE 28

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from C Types: int i; double x; float w; char c; void *p; Call: j = f (i, x, w, c, p);

slide-29
SLIDE 29

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from C Types: int i; double x; float w; char c; void *p; Call: j = f (i, x, w, c, p);

slide-30
SLIDE 30

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from C Types: int i; double x; float w; char c; void *p; Call: j = f (i, x, w, c, p);

slide-31
SLIDE 31

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Calling from ML: What is the problem?

  • The pieces we need:
  • Calling conventions
  • The sequence of types
  • The argument values
slide-32
SLIDE 32

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Calling from ML: What is the problem?

  • The pieces we need:
  • Calling conventions
  • The sequence of types
  • The argument values
slide-33
SLIDE 33

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

!!

Calling from ML: What is the problem?

  • The pieces we need:
  • Calling conventions
  • The sequence of types
  • The argument values
slide-34
SLIDE 34

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-35
SLIDE 35

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: ? f (?, ?, ?, ?, ?);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-36
SLIDE 36

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (int, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-37
SLIDE 37

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (int, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-38
SLIDE 38

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-39
SLIDE 39

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-40
SLIDE 40

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-41
SLIDE 41

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-42
SLIDE 42

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-43
SLIDE 43

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-44
SLIDE 44

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied (inferred?) Prototype: int f (double, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from ML val j = f (i, x, w, c, p)

slide-45
SLIDE 45

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

fun g i = f (i, x, w, c, p) + 1 inferred Prototype: int f (α, double, double, int, ptr);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function from a polymorphic ML function

slide-46
SLIDE 46

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

If we want to use static ML type information...

slide-47
SLIDE 47

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

If we want to use static ML type information...

  • Two routes:
slide-48
SLIDE 48

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

If we want to use static ML type information...

  • Two routes:

1.Monomorphize

  • ... but that requires whole-program analysis (e.g., as in

MLton)

slide-49
SLIDE 49

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

If we want to use static ML type information...

  • Two routes:

1.Monomorphize

  • ... but that requires whole-program analysis (e.g., as in

MLton) 2.Use intensional type information

  • ... complicated
  • ... not static, i.e., is a runtime technique
slide-50
SLIDE 50

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

slide-51
SLIDE 51

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

datatype arg = INT of xint | CHAR of xchar | FLOAT of xfloat | DOUBLE of xdouble | PTR of xaddr val dispatch : xaddr * arg list -> unit

slide-52
SLIDE 52

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

datatype arg = INT of xint | CHAR of xchar | FLOAT of xfloat | DOUBLE of xdouble | PTR of xaddr val dispatch : xaddr * arg list -> unit

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-53
SLIDE 53

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

slide-54
SLIDE 54

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

  • ... we don’t make use of the ML compiler’s type information
slide-55
SLIDE 55

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

  • ... we don’t make use of the ML compiler’s type information
  • Solution is not ML-specific.
slide-56
SLIDE 56

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

  • ... we don’t make use of the ML compiler’s type information
  • Solution is not ML-specific.
  • It can be adapted even to dynamically typed settings.
slide-57
SLIDE 57

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

So ultimately...

  • ... we don’t make use of the ML compiler’s type information
  • Solution is not ML-specific.
  • It can be adapted even to dynamically typed settings.
  • Use “universal” type of C values in statically typed setting.
slide-58
SLIDE 58

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-59
SLIDE 59

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-60
SLIDE 60

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-61
SLIDE 61

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

implied Prototype: int f (int, double, double, int, void *);

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Calling a variadic function

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-62
SLIDE 62

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Runtime system FI Library User program C Library Located-argument interpreter Located-argument generator Argument assembly

f arg1 ... argn

SML C/asm

slide-63
SLIDE 63

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Runtime system FI Library User program C Library Located-argument interpreter Located-argument generator Argument assembly

f arg1 ... argn

SML C/asm

“Staged Allocation” (Olinsky, Lindig, Ramsey; POPL’06)

Reuses existing specs; < 600 lines of (new) ML code

slide-64
SLIDE 64

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Runtime system FI Library User program C Library Located-argument interpreter Located-argument generator Argument assembly

f arg1 ... argn

SML C/asm

“Staged Allocation” (Olinsky, Lindig, Ramsey; POPL’06)

Reuses existing specs; < 600 lines of (new) ML code < 400 lines of ML code; result is not implementation-

  • r language-specific

Generated for different architectures from a single MLRISC template

slide-65
SLIDE 65

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Located arguments

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-66
SLIDE 66

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

slide-67
SLIDE 67

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation arg constructor (e.g., DOUBLE, ...)

value

slide-68
SLIDE 68

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value

slide-69
SLIDE 69

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

cc

machine location (e.g., REG “f4”, ...) allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value

slide-70
SLIDE 70

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

allocation state

cc

machine location (e.g., REG “f4”, ...) allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value

slide-71
SLIDE 71

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

allocation state allocation state’

cc

machine location (e.g., REG “f4”, ...) allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value

slide-72
SLIDE 72

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

allocation state allocation state’

cc

machine location (e.g., REG “f4”, ...) allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value value

slide-73
SLIDE 73

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Staged Allocation

allocation state allocation state’

cc

machine location (e.g., REG “f4”, ...) allocation request (e.g., (64, FPR, 8), ...)

arg constructor (e.g., DOUBLE, ...)

value value “located” argument

slide-74
SLIDE 74

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Located arguments

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-75
SLIDE 75

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

“r2” “f01” “f23” “r3” “(sp+24)”

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Located arguments

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-76
SLIDE 76

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

“r2” “f01” “f23” “r3” “(sp+24)”

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Located arguments

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-77
SLIDE 77

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

“r2” “f01” “f23” “r3” “(sp+24)”

r0 (sp) r1 (ra) r2 r3 f01 f0 f1 f23 f2 f3

Located arguments

dispatch (f, [INT i, DOUBLE x, FLOAT w, CHAR c, PTR p])

slide-78
SLIDE 78

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Conclusions

slide-79
SLIDE 79

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Conclusions

  • Difficult to utilize static type information for generating the calling sequence

for variadic FFI calls.

  • Use runtime techniques instead.
slide-80
SLIDE 80

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Conclusions

  • Difficult to utilize static type information for generating the calling sequence

for variadic FFI calls.

  • Use runtime techniques instead.
  • Separate the generation of located arguments from their actual placement

into machine registers and stack locations.

  • First task can be done in the high-level language;
  • second task must be done at the assembly level
slide-81
SLIDE 81

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

Conclusions

  • Difficult to utilize static type information for generating the calling sequence

for variadic FFI calls.

  • Use runtime techniques instead.
  • Separate the generation of located arguments from their actual placement

into machine registers and stack locations.

  • First task can be done in the high-level language;
  • second task must be done at the assembly level
  • Reuse existing technology (Staged Allocation, MLRISC).
  • Modular implementation.
  • Overall implementation effort is very manageable.
slide-82
SLIDE 82

Calling Variadic Functions from a Strongly-Typed Language

ML’08, Victoria BC, Sep 21, 2008

  • M. Blume, M. Rainey, J. Reppy

ank y!