# HG changeset patch # User Kaito Tokumori # Date 1435756739 -32400 # Node ID 20257f618ddd8b83d073dbe33bd20aa0b67207e0 # Parent c50a033e663546bbdb91462a02386b62f1e2d6b7 slide: llvm structure diff -r c50a033e6635 -r 20257f618ddd presentation/presen.html --- a/presentation/presen.html Wed Jul 01 19:06:07 2015 +0900 +++ b/presentation/presen.html Wed Jul 01 22:18:59 2015 +0900 @@ -54,7 +54,7 @@ @@ -154,124 +154,51 @@
-

Continuation based C (CbC)

- -
- -
-

CbC sample (with normal arguments)

+

CbC sample

-

Implimentating Continuation based language in Clang and LLVM

+

Implementating Continuation based language in Clang and LLVM

-
-
__code code1(int n,__code(*exit_code)(int,void *),void *exit_env){ - printf("code1 : code entry1\n"); - goto exit_code(n,exit_env); -}
- -int caller(){ - printf("caller : main1 entry\n"); - __code (*__ret)(int, void *) = __return; - struct __CbC_env *__env = __environment; - goto code1(1, __ret, __env); - return 0; + +__code code(struct Context* context, struct Allocate* allocate, struct Element* element) { + allocate->after_append = Code2; + element ->value = 10; + goto meta(context, Append); } -
int caller(){ - printf("caller : main1 entry\n"); - __code (*__ret)(int, void *) = __return; - struct __CbC_env *__env = __environment; - goto code1(1, __ret, __env); - return 0; -}
+__code append(struct Context* context, struct Allocate* allocate, struct List* list, struct Element* element) { + if(list->head) { + list->tail->next = element; + } else { + list->head = element; + } + list->tail = element; + list->tail->next = 0; + goto meta(context, allocate->after_append); +} -int main(){ - int n; - n = caller(); - printf("return = %d\n",n); - return 0; -}
+__code meta(struct Context* context, enum Code next) { + goto (context->code[next])(context); +} +
    -
  • The feature for return to C functions from code segments. -
  • __return is a code segment pointer for return C functions. -
  • __environment is a envitonment for return C functions. -
  • Code1 use a continuation with environments to return main function. -
- ---> - -
-

CbC sample (with data segments)

- -
-
-__code code1(Data1 data){
-  goto code2(data);
-}
-
-__code code2(Data2 data){
-  goto code3(data);
-}
-
-int main(){
-  goto start_code(context, Code1);
-}      
-
-
    -
  • -
  • -
  • +
  • Code segments like C functions. +
  • CbC transition is goto so code segments do not return to previous. +
  • There are no return values. +
@@ -283,24 +210,32 @@
  • GCC(GNU Compiler Collection)
  • LLVM and Clang
    -

    What is LLVM and Clang?

    +

    What is LLVM?

      -
    • LLVM is a compiler framework. -
    • LLVM has a intermidiate language which is called LLVM IR, LLVM language or LLVM bitcode. -
    • LLVM translates LLVM IR to assembly language. -
    • LLVM has a many kinds of optimization. +
    • Compiler frameworks. +
    • has a intermidiate language which is called LLVM IR, LLVM language or LLVM bitcode. +
    • Translates LLVM IR to assembly language. +
    • Many kinds of optimization.
    • Clang is C, C++ and Obj-C compiler frontend.
    • Clang uses LLVM for compiler backend.
    +

    What is Clang?

    +
      +
    • C, C++ and Obj-C compiler frontend. +
    • Uses LLVM for compiler backend. +
    +
    + +

    Why LLVM?

    • Apple supported. @@ -313,10 +248,9 @@

      LLVM and Clang's compilation flow

        -
      • Sorce codes are translated into clang AST by parser. -
      • clang AST is translated into LLVM IR by code generator. -
      • LLVM IR is translated into machine code by SelectionDAGISel. -
      • Machine code is optimized by optimizations and then, it is translated into assembly code. +
      • Clang translate C/C++/Obj-C into LLVM IR. +
      • LLVM translate LLVM IR into assembly code. +
      • LLVM optimize all of intermidiate representation.
      @@ -356,32 +290,7 @@
      -

      LLVM's intermidiate representations are do not be modified.

      -
    - -
    -

    Abstract Syntax Tree

    -
      -
    • You can see it if you give clang '-Xclang -ast-dump' options. -
    • The nodes indicate Decl (declaration), Stmt (statement) or Expr (expresstion). -
    - - - - - -
    source code - AST -
    -
    -__code code1(int n,__code(*exit_code)(int,void *),void *exit_env){
    -  printf("code1 : code entry1\n");
    -  goto exit_code(n,exit_env);
    -}
    -
    -
    -
    -

    We modify Clang which come to generate the AST when they get CbC syntax.

    +

    Intermidiate representations are not modified.

    @@ -405,21 +314,12 @@

    Implementating CbC compiler in LLVM and Clang

    -

    Implemented

    • add __code type for code segment. -
    • translate Clang's __code type into LLVM's __code type.
    • add goto syntax for transition.
    • force to tail call elimination.
    • goto with environment.
    • automatically prototype declatation genarating. - -
    -

    Implementing now

    -
      -
    • connect code segments with meta code segments. -
    • generate data segment automatically. -
    • Syntax for accessing to data segment type.
    @@ -436,9 +336,6 @@
    • Clang and LLVM handle code segments as __code type functions.
    • Code segments do not have return value so they are handled like void functions. -
    • Clang and LLVM use different class for handling type so we have to modify both. -
    • In Clang, we create type keyword, ID, and Type class. -
    • In LLVM, we create Type class and ID.
    • The following code is the place where Clang parse a __code type.
    • DS.SetTypeSpecType() set AST nodes __code type.
    @@ -458,38 +355,6 @@
    -

    translation Clang's __code type into LLVM's __code type

    -

    Clang's types are translated in CodeGen.

    -
    -
    - -
    -

    translation Clang's __code type into LLVM's __code type

    - - - - -
    -
      -
    • The following code is the translation place. -
    • Code segments have no return value so __code type is handled like void type. -
    -
    -
    -case ABIArgInfo::Ignore:
    -#ifndef noCbC
    -  if (FI.getReturnType().getTypePtr()->is__CodeType())
    -    resultType = llvm::Type::get__CodeTy(getLLVMContext());
    -  else
    -    resultType = llvm::Type::getVoidTy(getLLVMContext());
    -#else
    -  resultType = llvm::Type::getVoidTy(getLLVMContext());
    -#endif
    -  break;
    -
    -
    - -

    goto syntax for transition

    modify parser.

    @@ -711,58 +576,19 @@
    -

    Connect code segments with meta code segments

    +

    Conclusion

      -
    • All code segments are transition to next one via meta code segments. - -
    • Normal level code segments don't have to know meta code segments. -
    • When code segments transition to next code segment, compiler connect it with meta code segments. -
    • Meta code segments use context which has code segments pointer and name. -
    • Context is added arguments by compiler. -
    • You can omit meta computing if you do not need it. -
        -
      • In this case, code segments transition to next one via default meta code segment. -
      • Default meta code segment get next code segment from context and transition to it. -
      +
    • CbC compiler on LLVM and Clang was implemented. +
    • LLVM IR was not modified.
    -

    code segments view

    -
    -

    actual code segments transition

    -
    -

    Connect code segments with meta code segments

    - - - - - -
    original input code - Clang genarates it -
    -__code code1() {
    -  goto code2();
    -}
    -
    -__code code2(){
    -  goto code3();
    -}
    -              
    -
    -__code code1(struct Context* context) {
    -  goto meta(context,Code2);
    -}
    -
    -__code code2(struct Context* context){
    -  goto meta(context,Code3);
    -}
    -
    -__code meta(struct Context* context, enum Code next) {
    -  goto (context->code[next])(context);
    -}
    -              
    -
    +

    Future works

    +
      +
    • Operating system +
    • superset language +