By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

Generated function

P: n/a
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.
Thanks
Shaan

Aug 26 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Sat, 26 Aug 2006 03:29:17 -0700, shaanxxx wrote:
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.
Since you're generating code, why can you not also generate code that
calls other functions in your generated code?

For other cases you have things like `dlsym', `dlopen' and `libffi'.

--
Lars Rune Nøstdal
http://lars.nostdal.org/

Aug 26 '06 #2

P: n/a
Lars Rune Nøstdal wrote:
On Sat, 26 Aug 2006 03:29:17 -0700, shaanxxx wrote:
>I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.

Since you're generating code, why can you not also generate code that
calls other functions in your generated code?
Then there would be the question of how to call the functions that call
the functions. Or did you think introducing another layer would perform
some kind of magic?
For other cases you have things like `dlsym', `dlopen' and `libffi'.
Not on my Windows box you don't. They are not part of the C standard and
so not available in all C implementations. The only ways I can C to
achieve the OPs ends are:

1) Generate and compile code that creates an entire executable program
and then call it using the system function and some system specific
string. This still leave problems in terms of getting results out, but
you could make the program write the results to a file then read that
file. All highly messy.

2) Embed a C interpreter in your program and don't bother compiling at
all just interpret the C code. <OT>cint might be worth a look, google
for it and ask about it somewhere other than here</OT>

3) Use whatever system specific methods your system provides for
dynamically loading libraries and running functions from them. These
vary so you will have to ask in a group dedicated to your specific
system, be the Windows, Linux, some form of Unix or something else.
--
Flash Gordon.
Aug 26 '06 #3

P: n/a
"shaanxxx" <sh******@yahoo.comwrites:
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.
What is It? That is, what do you want to call the function in
the code?
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Aug 26 '06 #4

P: n/a
On 26 Aug 2006 03:29:17 -0700, in comp.lang.c , "shaanxxx"
<sh******@yahoo.comwrote:
>I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.
You mean you want to dynamically create new functions during the
execution of your programme, and call them? You can't do that in
Standard C, except by putting your new functions into another
executable, and using system() to execute that.

Some platforms have a mechanism for calling dynamically-linked
functions (though few even of them let you call previously-unknown
functions, normally the linker inserts a dummy function into your
executable that tells the operating system where to go to fetch the
dynamic routine).

So you will need to ask the guys who specialise in your operating
system.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Aug 26 '06 #5

P: n/a
Since you're generating code, why can you not also generate code that
calls other functions in your generated code?

I give you an example on this.(not relevant to my work)

lets assume,We have to solve this expression 3 + 4 * 5 . What we
generally do is , we write on programme who interpret this expression.
Think if you are going to interpret this expression billion time. i
would better to use compiled fuction for this expression.

compiled function would be :
int fun(int a, b int, c int){return (a + b*c);}

This was simple example.

Aug 27 '06 #6

P: n/a

Ben Pfaff wrote:
"shaanxxx" <sh******@yahoo.comwrites:
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

What is It? That is, what do you want to call the function in
the code?
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
I give you an example on this.(not relevant to my work)

lets assume,We have to solve this expression 3 + 4 * 5 . What we
generally do is , we write on programme who interpret this expression.
Think if you are going to interpret this expression billion time. i
would better to use compiled fuction for this expression.

compiled function would be :
int fun(int a, b int, c int){return (a + b*c);}

This was simple example.

Aug 27 '06 #7

P: n/a
"shaanxxx" <sh******@yahoo.comwrites:
lets assume,We have to solve this expression 3 + 4 * 5 . What we
generally do is , we write on programme who interpret this expression.
Think if you are going to interpret this expression billion time. i
would better to use compiled fuction for this expression.

compiled function would be :
int fun(int a, b int, c int){return (a + b*c);}
Although it's certainly useful, this sort of thing can't be done
in portable C. You'd be best off taking this to a newsgroup
where your implementation is discussed.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Aug 27 '06 #8

P: n/a
shaanxxx wrote:
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.
It's time to try out Lisp ;-)

Aug 27 '06 #9

P: n/a
"shaanxxx" <sh******@yahoo.comwrote:
I want to write a programme. I explain what it does.
1) It generates C code . // i know how to do it
2) It compiles that generated code. // i know how to do it.

IMPORTANT ONE :
3) Now It calls one of the functions in generated code. // need help on
this.

I need guidance in third point.
Frankly, as a self-described newbie, I think this is something you
shouldn't concern yourself with yet. Not only is it (as the other
replies have indicated) highly system-specific, it is also a bit
trickier than compiling functions into a single program the normal way.
If I were you, I'd make sure that I knew the language before going on to
such more advanced subjects.

Richard
Aug 28 '06 #10

P: n/a
"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
I give you an example on this.(not relevant to my work)
Why don't you give an example that is relevant to your work? Perhaps we can
suggest a method better suited to your problem.
lets assume,We have to solve this expression 3 + 4 * 5 . What we
generally do is , we write on programme who interpret this expression.
Think if you are going to interpret this expression billion time. i
would better to use compiled fuction for this expression.

compiled function would be :
int fun(int a, b int, c int){return (a + b*c);}
so you're saying you'll see the expression a + b*c a billion times with the
same format but different values of a, b, and c? And next time you run your
program the expression might be a + b/c instead? Sounds like a very strange
problem.

Philip

Aug 28 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.