I’ve been working on a tool (called leaven) to convert LLVM IR (intermediate 
representation) to Go. So you can compile C to LLVM with clang, and then 
convert the result to Go. It’s actually pretty easy, because LLVM instructions 
are such simple operations. But it’s not very readable; given this:

int strcmp(const char *l, const char *r)
{
        for (; *l==*r && *l; l++, r++);
        return *(unsigned char *)l - *(unsigned char *)r;
}

It produces this:

func strcmp(v0 *byte, v1 *byte) int32 {
        var v10, v11, v12, v13 *byte
        var v5, v6, v7, v16, v17, v18 bool
        var v3, v4, v14, v15, v21, v22 byte
        var v23, v24, v25 int32

        _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = v3, v4, v5, 
v6, v7, v10, v11, v12, v13, v14, v15, v16, v17, v18, v21, v22, v23, v24, v25

        v3 = *v0
        v4 = *v1
        v5 = v3 != v4
        v6 = v3 == 0
        v7 = v6 || v5
        if v7 {
                v21, v22 = v3, v4
                goto block20
        } else {
                goto block8
        }

block8:
        v10, v11 = v1, v0
        goto block9

block9:
        v12 = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(v11)) + 
1*unsafe.Sizeof(*(*byte)(nil))))
        v13 = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(v10)) + 
1*unsafe.Sizeof(*(*byte)(nil))))
        v14 = *v12
        v15 = *v13
        v16 = v14 != v15
        v17 = v14 == 0
        v18 = v17 || v16
        if v18 {
                goto block19
        } else {
                v10, v11 = v13, v12
                goto block9
        }

block19:
        v21, v22 = v14, v15
        goto block20

block20:
        v23 = int32(uint32(v21))
        v24 = int32(uint32(v22))
        v25 = v23 - v24
        return v25
}

But it works! 

I’ve never tried it with a C++ program, but once it’s compiled down to LLVM, 
there shouldn’t be much difference.

Whether it is anything like what you are looking for depends on your goals for 
the translation. (Though it’s almost certainly not complete enough yet.)

If your goal is to produce maintainable Go source that maintains the general 
appearance of the C++ original, you will need to build a custom tool that 
recognizes the idioms of your codebase and converts them to equivalent Go 
idioms, like Russ Cox did for translating the Go compiler. But keep in mind 
that he had the unfair advantage that he was translating C written by Go 
programmers.

I don’t think a general-purpose tool to convert C or C++ into maintainable Go 
is possible. As you handle more of the odd corner cases of C, the output looks 
more and more like machine code. Leaven skips that painful journey and produces 
asm.go (by analogy with asm.js) from day 1.

Leaven isn’t really ready for general use, but I decided to throw it on GitHub 
in response to your question. It’s at https://github.com/andybalholm/leaven 
<https://github.com/andybalholm/leaven>, for whatever it’s worth. I haven’t 
gotten around to adding a README or a license, but I’m planning to use the MIT 
license.

Andy

> On Jan 2, 2019, at 8:03 PM, Ian Lance Taylor <i...@golang.org> wrote:
> 
> On Wed, Jan 2, 2019 at 7:37 PM <aureallm2...@gmail.com> wrote:
>> 
>> I have C++ 11 source files which I need to convert to Go language code
>> Is there any converter  tool for this.
>> I google quite a few tools, none seems powerful and complete enough to do 
>> the job.for me.
> 
> C++ 11 is a much more complex language than Go.  I think that if you
> want to support all the features of C++11 this would essentially
> require writing a C++11 compiler that generates Go code as its output.
> I don't know of any such tool.
> 
> Ian
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to