C source code optimisers? 
Author Message
 C source code optimisers?

Quote:

>Is there such a beast? That is, a program which will take a C source file and
>produce a shorter, but functionally equivalent, source file.

>A couple of simple examples,

>   if ( ((foo == 3) && (bar == 4)) || potato == 6 ) { ...

>=> if(foo==3&&bar==4||potato==6){...

>   if ( !barf ) fred();

>=> barf||fred();

>(given that fred() returns a value)

Seems like a horrible thing to do to me.  If you are really interested in such
a thing, look at a pcode compiler.  Use the same techniques used to create
pcode.  Since you have the symbol table, you could turn things like:

   energy = mass_in_kilograms * speed_of_light_in_meters_per_second *
speed_of_light_in_meters_per_second;
to
   a0=a1*a2*a2;

If you are trying to obfuscate, run it through an obfuscator.  This kind of
thing won't make your code run any faster.  If you are trying to optimize for
size, many compilers have a switch for that.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.*-*-*.com/



Sun, 16 Apr 2000 03:00:00 GMT  
 C source code optimisers?


| Is there such a beast? That is, a program which will take a C source file and
| produce a shorter, but functionally equivalent, source file.
|
| A couple of simple examples,
|
|    if ( ((foo == 3) && (bar == 4)) || potato == 6 ) { ...
|
| => if(foo==3&&bar==4||potato==6){...
|
|    if ( !barf ) fred();
|
| => barf||fred();

There are enough debates hinging on definitions of terms around here
already... but optimization in the context of computer programming
means improving execution speed or reducing memory usage.  Removing
spaces and/or parentheses from the source code will have no effect on
that.  For some hints about what will have an effect, check out
http://www.ontek.com/mikey/optimization.html .

What you want is an "obfuscator," or rather, that is what you think you
want.  For a small single-file project, you can get pretty small files
by using one-letter variable names and changing stuff by hand.  For
large projects, it would simply be insane to intentionally obfuscate
source code.  I've heard some companies forced by contract or customer
demands to distribute source code will obfuscate it before releasing
it, but I doubt you are in this situation.

     mikey



Mon, 17 Apr 2000 03:00:00 GMT  
 C source code optimisers?


: | Is there such a beast? That is, a program which will take a C source file and
: | produce a shorter, but functionally equivalent, source file.
: |
: | A couple of simple examples,
: |
: |    if ( ((foo == 3) && (bar == 4)) || potato == 6 ) { ...
: |
: | => if(foo==3&&bar==4||potato==6){...
: |
: |    if ( !barf ) fred();
: |
: | => barf||fred();

: There are enough debates hinging on definitions of terms around here
: already... but optimization in the context of computer programming
: means improving execution speed or reducing memory usage.

I don't want to start another debate, so assume I mean "optimise" in its
generally accepted sense - to render optimal by whatever measure.

: What you want is an "obfuscator," or rather, that is what you think you
: want.

Or rather, that is what you think I think I want. Which is strange, given that
the original post was quite explicit - note "shorter", not "unreadable".

:        For a small single-file project, you can get pretty small files
: by using one-letter variable names and changing stuff by hand.

You can create faster and smaller executables in the same way, but it's nice
to have a program do some of the work for you, isn't it?

--
           Mathew Hendry (actually at dial.pipex.com, not dev.null)
                                                                            --



Mon, 17 Apr 2000 03:00:00 GMT  
 C source code optimisers?

[snip]

Quote:
>:        For a small single-file project, you can get pretty small files
>: by using one-letter variable names and changing stuff by hand.

>You can create faster and smaller executables in the same way, but it's nice
>to have a program do some of the work for you, isn't it?

I think if we knew *why* you want to do such a silly thing as this, we could
offer better help.  It is clear that such a transformation will not produce
obfuscated code as well as an obfuscator.  It is clear that this will not make
the generated code smaller or faster.  What exactly is it that you are trying
to accomplish anyway?
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Mon, 17 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:



> : | Is there such a beast? That is, a program which will take a C source file and
> : | produce a shorter, but functionally equivalent, source file.
> : |
> : | A couple of simple examples,
> : |
> : |    if ( ((foo == 3) && (bar == 4)) || potato == 6 ) { ...
> : |
> : | => if(foo==3&&bar==4||potato==6){...
> : |
> : |    if ( !barf ) fred();
> : |
> : | => barf||fred();

[snip]

Whatever happened to code readability and reusability.  Making the
source optimal might shorten compile time, but not execution time.
Which takes more time, somebody else understanding the optimized source
code or somebody understanding a well documented, easy to read program?
Which
costs more, manpower or machine time?

How about something like this:

#define AND &&
#define OR  ||
#define NOT !

if ( (foo == 3) AND (bar == 4) OR (potato == 6) )
{
   ...

Quote:
}

if (NOT barf)
    fred();

More time is wasted trying to understand the existing code so that it
can be modified or used by other people.  Disk space and processing time
is cheap compared to the man-hours required to fully understand existing
code.
-- "Unless someone like you
    Cares a whole awful lot,
    Nothing's going to get better,
    It's not." -- The Lorax
Thomas Matthews



Mon, 17 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Is there such a beast? That is, a program which will take a C source file and
produce a shorter, but functionally equivalent, source file.

A couple of simple examples,

   if ( ((foo == 3) && (bar == 4)) || potato == 6 ) { ...

=> if(foo==3&&bar==4||potato==6){...

   if ( !barf ) fred();

=> barf||fred();

(given that fred() returns a value)

--
           Mathew Hendry (actually at dial.pipex.com, not dev.null)
                                                                            --



Mon, 17 Apr 2000 03:00:00 GMT  
 C source code optimisers?


:                                                         Making the
: source optimal might shorten compile time, but not execution time.

Did I say I wanted to reduce either?

: Which takes more time, somebody else understanding the optimized source
: code or somebody understanding a well documented, easy to read program?
: Which costs more, manpower or machine time?

My time, my code, my money: why ask?

: More time is wasted trying to understand the existing code so that it
: can be modified or used by other people.  Disk space and processing time
: is cheap compared to the man-hours required to fully understand existing
: code.

Summary of thread so far:

"How might I do this?"
"Don't."

Thank you.

--
           Mathew Hendry (actually at dial.pipex.com, not dev.null)
                                                                            --



Tue, 18 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Sprake Mathew:

Quote:
>Simply, I like the way that C can express so much so tersely, and
>I'd like to see how far it can go. An amu{*filter*}t, nothing more.

Go here for the ultimate in C terseness and sophistication:

http://www.*-*-*.com/

--
<\___/>
/ O O \      | Due to circumstances beyond your control, you are
\_____/ FTB. | master of your fate and captain of your soul.



Tue, 18 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:

> Sprake Mathew:
> >Simply, I like the way that C can express so much so tersely, and
> >I'd like to see how far it can go. An amu{*filter*}t, nothing more.

> Go here for the ultimate in C terseness and sophistication:

> http://www.*-*-*.com/

Hah, don't go there ! It's a trap !
It's extremely dangerous and might ruin your coding style for ever
and eternity thereafter.

(now if that does not attrackt lots of curios poor souls, I don't know
what will ... Ok, so I do know something that would be even better in
luring programmers, but that would be too low ... ;-) (and I'm not
speaking about promising lots of coffee) )

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 18 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:



>> | Is there such a beast? That is, a program which will take a C
>> | source file and produce a shorter, but functionally equivalent,
>> | source file.
>> [snip] optimization in the context of computer programming
>> means improving execution speed or reducing memory usage.  Removing
>> spaces and/or parentheses from the source code will have no effect on
>> that.

>Optimization in the broader area of computer science -- not just
>programming -- also means doing logic reductions like Matthew wanted.
>It's a common and well-known problem in logic design, for example.
>When designing some kind of 'combinational logic' circuit, you will
>typically have a set of inputs, and a set of outputs, and a set of
>logic equations that describe the desired state of of the outputs as a
>function of the inputs.

>If you can minimize the levels of logic to a 'sum of products'
>expression then it's pretty straightforward to implement the circuit
>in a PLA - programmed logic array.  Minimizing the logic means fewer
>gates used for this circuit, so you can either cram more stuff onto
>your chip, and make it do more stuff, or you can make a smaller chip,
>which increases yield; either goal is highly desired by chip makers.

>Several logic optimization tools to do this are publically available
>as offshoots of University VLSI design programs; I've used the ones
>from Berkeley's (now obsolete) MAGIC VLSI toolset (writing command
>parsers for MAGIC is what drove Ousterhaut to invent TCL, for you
>computer history geeks).

>So what does this have to do with C?  I thought that Matthew was
>asking if there was a tool that did similar logic transforms on C
>code.  This is a mildly intriguing question and doesn't, IMHO, deserve
>the flak it has gotten here.  It needn't be a source code obfuscator
>so much as a 'simplifier'; sometimes complex logic expressions can be
>simplified in non-obvious ways.

>I know that it's common for optimizing compilers to perform similar
>operations on their parse trees as an intermediate step before
>generating code, perhaps with an eye to efficient implementation in
>the target CPU's instruction set, but I am unaware of any separate
>tools that produce C source.

AHA!  Logic transforms!  If that was all he was after, why did he not just say
so?  It was not at all clear to me that this was what he was after.   I have a
package called BOOL that will perform logic transformations.  It was written
by Adrian Johnstone.  {Karnaugh map, minimize, factorize, etc.}.  Comes with a
postscript manual even.  If anybody wants a copy, send me an email.  I don't
anti-spam my address.

--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Tue, 18 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:


> | Is there such a beast? That is, a program which will take a C
> | source file and produce a shorter, but functionally equivalent,
> | source file.
> [snip] optimization in the context of computer programming
> means improving execution speed or reducing memory usage.  Removing
> spaces and/or parentheses from the source code will have no effect on
> that.

Optimization in the broader area of computer science -- not just
programming -- also means doing logic reductions like Matthew wanted.
It's a common and well-known problem in logic design, for example.
When designing some kind of 'combinational logic' circuit, you will
typically have a set of inputs, and a set of outputs, and a set of
logic equations that describe the desired state of of the outputs as a
function of the inputs.  

If you can minimize the levels of logic to a 'sum of products'
expression then it's pretty straightforward to implement the circuit
in a PLA - programmed logic array.  Minimizing the logic means fewer
gates used for this circuit, so you can either cram more stuff onto
your chip, and make it do more stuff, or you can make a smaller chip,
which increases yield; either goal is highly desired by chip makers.

Several logic optimization tools to do this are publically available
as offshoots of University VLSI design programs; I've used the ones
from Berkeley's (now obsolete) MAGIC VLSI toolset (writing command
parsers for MAGIC is what drove Ousterhaut to invent TCL, for you
computer history geeks).

So what does this have to do with C?  I thought that Matthew was
asking if there was a tool that did similar logic transforms on C
code.  This is a mildly intriguing question and doesn't, IMHO, deserve
the flak it has gotten here.  It needn't be a source code obfuscator
so much as a 'simplifier'; sometimes complex logic expressions can be
simplified in non-obvious ways.

I know that it's common for optimizing compilers to perform similar
operations on their parse trees as an intermediate step before
generating code, perhaps with an eye to efficient implementation in
the target CPU's instruction set, but I am unaware of any separate
tools that produce C source.

Of course, it all takes back seat to getting correct, stable, well
documented code in the first place.

  --berry

P.S. Today is my last day at PARC so I can say what I want :-)

--
Berry Kercheval :: Xerox Palo Alto Research Center :: Palo Alto, CA
"Having major planets disappear is always a bad sign."  - Jim Blinn



Tue, 18 Apr 2000 03:00:00 GMT  
 C source code optimisers?

: If you can minimize the levels of logic to a 'sum of products'
: expression then it's pretty straightforward to implement the circuit
: in a PLA - programmed logic array.  Minimizing the logic means fewer
: gates used for this circuit, so you can either cram more stuff onto
: your chip, and make it do more stuff, or you can make a smaller chip,
: which increases yield; either goal is highly desired by chip makers.

A good source code size optimiser has to do more than this. To shorten code
substantially, you may have to alter not only the underlying logic but also
the implementation of that logic. This same is true, of course, when working
with silicon.

--
           Mathew Hendry (actually at dial.pipex.com, not dev.null)
                                                                            --



Wed, 19 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:


> A good source code size optimiser has to do more than this. To shorten code
> substantially, you may have to alter not only the underlying logic but also
> the implementation of that logic. This same is true, of course, when working
> with silicon.

Well, sure, but I was just trying to point out a plausible scenario in
which your request wasn't stupid, as some folks had asserted.  I was
by no means trying to enumerate the problem space.

--
Berry Kercheval :: Palo Alto, CA
"Having major planets disappear is always a bad sign."  - Jim Blinn



Thu, 20 Apr 2000 03:00:00 GMT  
 C source code optimisers?

Quote:


> >Optimization in the broader area of computer science -- not just
> >programming -- also means doing logic reductions like Matthew wanted.
> AHA!  Logic transforms!  If that was all he was after, why did he not just say
> so?  It was not at all clear to me that this was what he was after.

It wasn't clear to me either, but it seemed a plausible interpretation
everyone else seemed to have missed.

--
Berry Kercheval :: Palo Alto, CA
"Having major planets disappear is always a bad sign."  - Jim Blinn



Thu, 20 Apr 2000 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Compile CS source code using ICodeCompiler

2. cs source code

3. Binary code optimiser

4. how to convert source code pascal to source code c

5. Java source code to CSharp source code converter

6. Newbie: separate big .cs file into small .cs files

7. Include code in other Cs files

8. newbe/cs student, need help w/ code

9. mp3 source code ? - Source.zip (1/1)

10. mp3 source code ? - Source.zip (0/1)

11. Source code sources?

12. New C# Source Code Sample - Running JScript.NET Code from C# Apps

 

 
Powered by phpBB® Forum Software