ABI Compatibility Through A Customizable Language Kevin Atkinson, - - PowerPoint PPT Presentation

abi compatibility through a customizable language
SMART_READER_LITE
LIVE PREVIEW

ABI Compatibility Through A Customizable Language Kevin Atkinson, - - PowerPoint PPT Presentation

ABI Compatibility Through A Customizable Language Kevin Atkinson, Matthew Flatt, Gary Lindstrom University of Utah 1 Application Programmer Interface (API) class X { virtual int f(); Library ... API }; Application Library class Y


slide-1
SLIDE 1

1

ABI Compatibility Through A Customizable Language

Kevin Atkinson, Matthew Flatt, Gary Lindstrom University of Utah

slide-2
SLIDE 2

2

Application Programmer Interface (API)

Application

int main() { X x; x.f(); } Code Library Library class X { virtual int f(); ... }; API class Y API

slide-3
SLIDE 3

3

Application Binary Interface (ABI)

class X { virtual int f(); ... }; int main() { X x; x.f(); }

Application

API Code Library Library gcc ABI

Class Layout Calling Convention

gcc Object Code class Y API gcc ABI

slide-4
SLIDE 4

4

Source vs Binary Compatibility

Application

Doesn't Change Recompile class X { virtual int f(); virtual int g(); ... }; API Library gcc ABI int main() { X x; x.f(); } Code gcc Object Code

(ABI Fragility)

slide-5
SLIDE 5

5

ABI Incompatibility

Application

Doesn't Change Change Compilers and Recompile class X { virtual int f(); ... }; API Library gcc vc++ ABI int main() { X x; x.f(); } Code vc++ Object Code

slide-6
SLIDE 6

6

Previous Work

  • Some Standardization

Attempts

  • Itanium C++ ABI
  • Some Work Towards

Less Fragile ABIs

  • Delta C++ (Palay, 1992)
  • Object Binary Interface

(Williams and Kindel, 1994)

  • Still Two Common ABIs:
  • GCC
  • Visual C++
  • Sacrifice Performance
  • Not Commonly Used
slide-7
SLIDE 7

7

Our Contribution

ABI Compatibility Through Macros

int main() { ... } Object Code Class Impl. Macro Extensible Compiler

ZL

slide-8
SLIDE 8

8

Breaking Binary Compatibility

class X { int x; ... }; gcc class X { int x; int y; ... }; gcc class X int x class X int x int y size 4 size 8 API (.h) API (.h)

slide-9
SLIDE 9

9

Preserving Binary Compatibility

Dummy Member

class X { int x; int dummy; ... }; gcc class X { int x; int y; ... }; gcc class X int x int dummy size 8 class X int x int y size 8 API (.h) API (.h)

slide-10
SLIDE 10

10

Preserving Binary Compatibility

Pointer to Implementation

API (.h) gcc

Implementation

class X { Impl* imp; int get_x(); }; class X::Impl { int x; }; int X::get_x() {imp->x;}

class X

Impl

int x

slide-11
SLIDE 11

11

Breaking Binary Compatibility

class X { virtual void foo(); ... }; gcc class X { virtual void foo(); virtual void bar(); ... }; gcc

  • For Preserving: Options Limited

vtable void foo() size 4 vtable void foo() void bar() size 8

slide-12
SLIDE 12

12

Dummy Member Pointer to Impl.

class X { int x; int dummy; ... }; gcc class X { int x; int y; ... }; gcc class X int x int dummy size 8 class X int x int y size 8

Automate These Patterns

API (.h) gcc

Implementation

class X { Impl* imp; int get_x(); }; class X::Impl { int x; }; int X::get_x() {imp->x;}

class X

Impl

int x

slide-13
SLIDE 13

13

Automation is Not Enough...

Breaking Binary Compatibility

class X { virtual void foo(); ... }; gcc class X { virtual void foo(); virtual void bar(); ... }; gcc

  • For Preserving: Options Limited

vtable void foo() size 4 vtable void foo() void bar() size 8

Breaking Binary Compatibility

class X { virtual void foo(); ... }; gcc class X { virtual void foo(); virtual void bar(); ... }; gcc

  • For Preserving: Options Limited

vtable void foo() size 4 vtable void foo() void bar() size 8

slide-14
SLIDE 14

14 int main() { ... } Extensible Compiler Object Code int main() { ... } Macro Extensible Compiler Object Code Class Impl. Class Impl. Extension

slide-15
SLIDE 15

15 int main() { ... } Object Code Class Impl. import ... int main() { ... } Object Code Class Impl. Library Macro Extensible Compiler Macro Extensible Compiler

slide-16
SLIDE 16

16

Two ABI's at Once

class : ABI(gcc) class : ABI(vc++) Object Code Gcc ABI VC++ ABI Macro Extensible Compiler

slide-17
SLIDE 17

17

ZL

import ...; int main() { ... } Macro Extensible Compiler Object Code default class implementation

  • ther macro

library

C Like Core w/ Minimal Amount of C++

slide-18
SLIDE 18

18

Hygienic Pattern Macros

macro or(x, y) { ({typeof(x) t = x; t ? t : y;}); }

  • r(0.0,t)

({typeof(0.0) t0 = 0.0; t0 ? t0 : t;});

slide-19
SLIDE 19

19

Procedural Macros

Syntax* foreach(Syntax*) {...} make_macro foreach int main() { Container c; ... foreach(el, c, el.print()); }

Syntax Forms: new_mark syntax make_macro Callbacks: match replace ct_value error

slide-20
SLIDE 20

20

The Class Macro

import_file "class-simple.zlh"; ... .class ParseClass { ... virtual Syntax * parse(Syntax * p) {...} virtual void init() {...} virtual void init_parent_info() {...} ... }; Syntax * parse_class(Syntax * p) { ParseClass * pc = new ParseClass; return pc->parse(p); } make_macro class parse_class;

900 Lines

  • f Code
slide-21
SLIDE 21

21

  • .f()

C::f(...)

Expanding Method Calls

What is f? C o

slide-22
SLIDE 22

22

User Types

(ZL's Notion of Classes)

user_type C { struct Data { int i; }; associate_type struct Data; int foo(int j) macro i(:this ths) {...} macro f(x, :this ths) {...} } void main() { C o; C::f(x, :this = &o); int x = C::i(:this = &o); } void main() { C o;

  • .f(x);

int x = o.i; }

slide-23
SLIDE 23

23 `

Classes

class C { int i; int f(int j) {return i + j;} }; user_type C { struct Data { int i; }; associate_type struct Data; int f`internal(C * this, int j) {return i + j;} macro i (:this ths = this) {(*(C *)ths)..i;} macro f(j, :this ths = this) {f`internal(ths, j);} }

slide-24
SLIDE 24

24

Inheritance

class D : public C { int j; }; user_type D { import C; struct Data { struct C::Data parent; int j; }; associate_type struct Data; make_subtype C _up_cast _down_cast; macro _up_cast ... macro _down_cast ... macro j (:this ths = this) {...} }

slide-25
SLIDE 25

25

Virtual Methods

class D : public C { virtual void f(int j); }; user_type D { ... class VTable : public C::VTable { void (*f)(int j); }; struct Data { VTable* _vptr; ... }; associate_type struct Data; macro f(j, :this ths = this) {_vptr->f(ths, j);} ... }

slide-26
SLIDE 26

26

Examples

Mitigating the ABI Problem Through Macros

slide-27
SLIDE 27

27

Example: Fixing Size of Class

class C { int x; char dummy[8] }; class X int x dummy[8] class C : fix_size(12) { int x; }; 8 12 class C { int x; int y; char dummy[4] }; class X int x int y dummy[4] class C : fix_size(12) { int x; int y; }; 4 12

(Complete Macro In Paper, 36 Lines of Code)

slide-28
SLIDE 28

28

Example: Allowing More Fields

class C : fix_size(12) { int x; int y; int i; int j; }; int main() { C obj;

  • bj.x;
  • bj.i;

} class X int x int y Overflow int i int j 12

(Expanded Macro, Around 100 Lines of Code)

slide-29
SLIDE 29

29

Example: Pointer to Impl.

class X Overflow int x int y int i; int j;

(Same Macro, Around 100 Lines of Code)

class C : fix_size(0) { int x; int y; int i; int j; }; int main() { C obj;

  • bj.x;
  • bj.i;

}

slide-30
SLIDE 30

30

Fixing Virtual Table Size

class X : fix_vtable_size(8) { virtual void f(); virtual void g(); virtual void h(); };

(Modified Macro, Around 120 Lines of Code)

macro alt_vtable (name, body) { class name : fix_size(8) {body}; } class X : vtable_class(alt_vtable) { { virtual void f(); virtual void g(); virtual void h(); };

slide-31
SLIDE 31

31

Matching Other ABIs

class : ABI(v1) class : ABI(v2)

ZL Class Implementation

v1

Implemented In Under 45 Lines of Code

.class ParseClass { ... }

This Global Variable

v2

.class ParseClassNewABI : public ParseClass { ... }

slide-32
SLIDE 32

32

ZL Implementation Status

  • Prototype Compiler
  • Most Of C
  • Enough C++ to Demonstrate Approach
  • Compiled Several Real Applications
  • C: bzip2, gzip
  • C++: randprog
  • Compile Time 2-3 slower
  • No Impact on Performance
slide-33
SLIDE 33

33

Conclusion

Demonstrated ABI Compatibility Through Macros

int main() { ... } Object Code Class Impl. Macro Extensible Compiler

ZL