Implementing Continuation based language in GCC

Reading time: 5 minute
...

📝 Original Info

  • Title: Implementing Continuation based language in GCC
  • ArXiv ID: 1109.4048
  • Date: 2011-09-20
  • Authors: Shinji Kono and Kento Yogi

📝 Abstract

We have implemented C like Continuation based programming language. Continuation based C, CbC was implemented using micro-C on various architecture, and we have tried several CbC programming experiments. Here we report new implementation of CbC compiler based on GCC 4.2.3. Since it contains full C capability, we can use CbC and C in a mixture.

💡 Deep Analysis

Figure 1

📄 Full Content

If CPS theory is successful, it should also be working well in practical area. Our idea is simple. How about a programming language which has continuation passing style only? How about it runs as fast as current GNU C compiler? Instead of creating complete new programming language, we designed a lower language of C, so called Continuation based C, here after CbC. Using CPS transformation like method, we can compile C into CbC, that is, we have some kind of backward compatibility.

We have implemented CbC using micro-C on various architecture, and we have tried several CbC programming experiments. Here we report new partial implementation of CbC compiler [5] based on GCC 4.2.3 [1]. Since it contains full C capability, we can use CbC and C in a mixture, so when call the mixture C with C, here after CwC.

First we show CbC language overview.

CbC’s basic programming unit is a code segment. It is not a subroutine, but it looks like a function, because it has input and output. We can use C struct as input and output interfaces. In this example, a code segment f has input a and sends output b to a code segment g. There is no return from code segment b, b should call another continuation using goto. Any control structure in C is allowed in CwC language, but in case of CbC, we restrict ourselves to use if statement only, because it is sufficient to implement C to CbC translation. In this case, code segment has one input interface and several output interfaces (fig. 2).

__code and parameterized global goto statement is an extension of Continuation based C. Unlike C– are impossible to write in C. Usually it requires some help of assembler language such as __asm statement extension which is of course not portable.

We can easily write these things in CbC, because CbC has no hidden information behind the stack frame of C. A thread simply go to the scheduler, goto scheduler(self, task_list);

and the scheduler simply pass the control to the next thread in the task queue. Of course it is a simulator, but it is an implementation also. If we have a CPU resource API, we can write real multi CPU scheduler in CbC. This is impossible in C, because we cannot access the hidden stack which is necessary to switch in the scheduler. In CbC, everything is visible, so we can switch threads very easily.

This means we can use CbC as an executable specification language of OS API.

Since we can write a scheduler in CbC, we can also enumerate all possible interleaving of a concurrent program. We have implement a model checker in CwC. CbC can be a self verifiable language [7].

SPIN [3] is a very reliable model checker, but it have to use special specification language PROMELA. We cannot directly use PROMELA as an implementation language, and it is slightly difficult to study its concurrent execution semantics including communication ports.

There are another kind of model checker for real programming language, such as Java PathFinder [2]. Java PathFinder use Java Virtual Machine (JVM) for state space enumeration which is very expensive some time.

In CbC, state enumerator itself is written in CbC, and its concurrency semantics is written in CbC itself. Besides it is very close to the implementation. Actually we can use CbC as an implementation language. Since enumerator is written in the application itself, we can perform abstraction or approximation in the application specific way, which is a little difficult in Java PathFinder. It is possible to handle JVM API for the purpose, although.

We can use CPS transformed CbC source code for verification, but we don’t have to transform all of the source code, because CwC supports all C constructs. (But not in C++… Theoretically it is possible with using cfront converter, it should be difficult).

Now we have GCC implementation of CbC, it runs very fast. Many popular languages are implemented on top of C. Some of them uses very large switch statement for the byte code interpreter. We don’t have to use these hacks, when we use CbC as an implementation language.

CbC is naturally similar to the state charts. It means it is very close to UML diagrams. Although CbC does not have Object Oriented feature such as message passing nor inheritance, which is not crucial in UML.

Conversion from C to CbC is straight forward, but it generates a lot of code segments. Since CbC does not have heap management itself, the stack area have to be allocated explicitly. We find GCC can perform better optimization in translated code segment. We will discuss it later.

We have an easy implementation of C to CbC compilation, but it is not a practical level, but we need good converter for backward compatibility.

We can also consider possible conversion from C++ to CbC. In this case, all hidden operation in C++ should become explicit, for examples, object allocations and deallocations in the stack, handling of auto pointer and so on.

So how to implement CwC in GCC. The idea itself is simple[6], forcing C tail call e

📸 Image Gallery

cover.png

Reference

This content is AI-processed based on open access ArXiv data.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut