views:

468

answers:

11

I've worked on a number of products that make use of code generation. It seems to be the only way to achieve both a high degree of user-customizability and high execution speed.

The downside is that we are requiring users to install a compiler (primarily on MS Windows).

This has been an on-going headache, because vendors like MS keep obsoleting compilers, and some users tend to have more than one compiler installed.

We're considering using GNU C, and possibly C++, but even there, there are continual version issues.

I've considered possibly generating assembly language, in an effort to get off the compiler-version-treadmill, but assembly languages are all machine-specific.

Ideally there would be some way to produce generated code that would be flexible, run fast, and not expose us to the whims of third-party providers.

Maybe I'm overlooking something simple, like Java. Any ideas would be appreciated. Thanks.

+2  A: 

start here: http://www.codegeneration.net/

SQLMenace
Sounds good. (So far, that page is not opening for me.)
Mike Dunlavey
+3  A: 

I might be missing some context here, but could you just pin yourself to a specific version? E.g., .NET 2.0 can be installed side by side with .NET 1.1 and .NET 3.5, as well as other versions that will come out in the future. So as long as your code makes use of a specific version of a compiler, what's the problem?

Paul Stovell
That's a good suggestion, aside from being windows-specific. I will consider that.
Mike Dunlavey
Ideally, it wouldn't be windows-limited, and could be relied on to work 10-15 years from now.
Mike Dunlavey
Yes, but you could also target something like C# 1.0. That should be forwards compatible and supported on other platforms via the Mono project.
Dan
visual basic 4.0 was released about 15 years ago, and applications built with it still run (although it there is no more tech support). Why would .NET applications suddenly stop running in 10 years?
Paul Stovell
and as Dan pointed out, the mono project provides an open source implementation of .NET 2.0 that works on windows and other platforms. And C# and the CLI have been through ECMA and ISO standardisation. http://en.wikipedia.org/wiki/Microsoft_.NET#Standardization_and_licensing
Paul Stovell
+2  A: 

One option would be to use a language/environment that provides access to the compiler in code; For example, here is a C# example.

Chris Shaffer
Thanks. That looks good, but I wonder about its longevity and/or platform independence.
Mike Dunlavey
I don't think Longevity should be a concern; As long as your application can run on the client machine, the code generation should work(since they both depend on the same framework). Platform dependence will depend on your language; For C#, you could look into Mono (Open source .net implementation).
Chris Shaffer
Very helpful. I will check that out.
Mike Dunlavey
After reading some of your other comments regarding longevity I see I misunderstood what you were looking for. 10-15 years is going to be hard to guarantee anywhere, though I think a language that builds to intermediate code (like C# or Java) would be the safest bet...
Chris Shaffer
+2  A: 

I've considered possibly generating assembly language, in an effort to get off the compiler-version-treadmill, but assembly languages are all machine-specific.

That would be called a compiler :)

Why don't you stick to C90?

I haven't heard much of severe violations of standards from gcc's side, if you don't use extensions.

And you can always distribute a certain version of gcc along with your product, say, 4.3.2, giving an option to users to use their own compiler at their own risk.

As long as all code is generated by you (i. e. you don't embed your instructions into other's code), there shouldn't be any problems in testing against this version and using it to compile your libraries.

Quassnoi
That is what we're leaning toward. I'm wondering if people have had similar experiences.
Mike Dunlavey
Pro*C sticks to C90 and works under everything I've seen since 1996 -- Borland C, Visual C, gcc etc.
Quassnoi
Pro*C is an EmbeddedSQL precompiler for Oracle, in case you're not familiar with it :)
Quassnoi
I'm not, but if you say it sticks to C90, and users have no trouble installing and using that, that sounds pretty good.
Mike Dunlavey
Mike Dunlavey
C90 is probably the most prevalent language standard. You will find lots of cases of incomplete C99 compliance.
David Thornley
Believe it or not, it was a typo :) Must be a freudian slip but on keyboard.
Quassnoi
+6  A: 

If you're considering C and even assembler, take a look at LLVM first: http://llvm.org

Shannon Weyrick
That looks interesting. Thanks for the link.
Mike Dunlavey
I'm using it in a project now (http://code.roadsend.com/rphp). It's got a nice API, liberal license, targets many platforms, and optimizes heavily.
Shannon Weyrick
+2  A: 

Why not ship a GNU C compiler with your code generator? That way you have no version issues, and the client can constantly generate code that is usable.

Jess
That is what we're considering. I'm hoping there are no gotchas.
Mike Dunlavey
+1  A: 

I would stick to that language that you use for generating that language. You can generate and compile Java code in Java, Python code in Python, C# in C# even LISP in LISP etc.

But it is not clear whether such languages are sufficiently fast for you. For top speed I would choose to generate C++ and use gcc for compilation.

Jiri
Thanks. We're considering gcc. We want it to be a compiler/linker/libraries that can still be installed and will work years and years from now.
Mike Dunlavey
FWIW, some Lisp implementations are extremely fast (such as CMU CL). They may be unsuitable for other reasons (like lack of libraries, or bad integration with the OS).
David Thornley
@David: Good suggestion. A little far-out for this outfit, though.
Mike Dunlavey
+2  A: 

It sounds like you're looking for LLVM.

Ken
Shannon suggested that also. Thanks for the tip.
Mike Dunlavey
+1  A: 

Why not use something like SpiderMonkey or Rhino (Javascript support in Java or C++). You can export your objects to Javascript namespaces, and your users don't have to compile anything.

Chris Kaminski
Thanks. I'm getting plenty of good ideas here.
Mike Dunlavey
+1  A: 

If you want to generate asm code, you may take a look at asmjit .

kcwu
+1  A: 

this might be too late. but here is my two cents. embed an interpreter for a language like lua/scheme into your program. and generate code in that language.

alvin
@alvin: Thanks. Not too late. I think that's a good idea. An argument against it is if performance is an issue, it is better to generate a language that can be dynamically compiled. Of course, the problem with that is my app isn't complete without such a compiler/linker. I've always been on the lookout for a middle ground. But if performance is not such an issue, I like your idea.
Mike Dunlavey