473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Just-in-time compiling of assembly for use in a c++ program

Mostly for testing reasons I'd like to see if it makes sense to chose
the following approach for just-in-time compilation of shaders for a
renderer:
Seeing as the shaders themsefs consist mostly of very basic operations
I'd like to translate them into assembly, have an assembler compile the
binary code and then call the resulting machine code from c++.

The thing is that up until now I have only used inline assembly in my
c++ projects, so there's a few things I hardly know anything about and
would be very greatful if anyone here could point me in the right
direction:
- Having a set of asm instructions, say "addl 5, %%eax" or "add eax, 5"
respectively, how would I go about translating just this one line into
binary? (in a way that doesn't mean i'll have to re-write the whole
thing when porting to a different os if at all possible :)
- How do I jump into the binary from my c++ app in a way that I can jmp
back at the end of my assembly code segment?

Thanks!

Dec 23 '06 #1
2 1997
On 23 Dec 2006 06:53:15 -0800, "Jan Althaus" <he*******@gmail.com>
wrote in comp.lang.c++:
Mostly for testing reasons I'd like to see if it makes sense to chose
the following approach for just-in-time compilation of shaders for a
renderer:
Seeing as the shaders themsefs consist mostly of very basic operations
I'd like to translate them into assembly, have an assembler compile the
binary code and then call the resulting machine code from c++.
Your question is really off-topic for this group. While C++ does
define a syntax for embedding inline assembly language in a C++
program, all details about the assembly language and what it does is
completely implementation-defined and outside the scope of C++ itself.
The thing is that up until now I have only used inline assembly in my
c++ projects, so there's a few things I hardly know anything about and
would be very greatful if anyone here could point me in the right
direction:
- Having a set of asm instructions, say "addl 5, %%eax" or "add eax, 5"
respectively, how would I go about translating just this one line into
binary? (in a way that doesn't mean i'll have to re-write the whole
thing when porting to a different os if at all possible :)
No matter what you do, it will never be portable to a platform using a
different CPU architecture, such as ARM, SPARC, or PowerPC.

As to how you generate binary code from assembly language source code,
you can use system() or a platform specific extension to invoke an
assembler, or you could incorporate some for of assembler in your
program yourself.
- How do I jump into the binary from my c++ app in a way that I can jmp
back at the end of my assembly code segment?
Here's the point where you leave the C++ language standard completely
behind. If, for example, you incorporate some sort of mini assembler
into your program, you could read assembly language source code and
translate it into object code, most likely storing it in an array of
unsigned chars.

If you have a pointer to this array, you still have the problem that
there is no defined conversion between data and code in C++, and in
fact some operating systems do not ever allow a program to try to
execute anything that it can access as data, or write to any memory
that it can execute from.

So you really need to ask this in a group specific to your platform
(Mac?) to find out if this can be done on your platform, and how to do
it. The C++ language just doesn't define this at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 23 '06 #2
Jan Althaus wrote:
Seeing as the shaders themsefs consist mostly of very basic operations
I'd like to translate them into assembly, have an assembler compile the
binary code and then call the resulting machine code from c++.
You could literally do that, invoking your standard assembler (ie
with system()) and loading the resulting object code, with library
help if available, if speed isn't an issue.

Perhaps you can structure your shaders as concatenations of short
fragments that you can write and assemble and link to your program,
using a table of pointer and size for each fragment, and then just
write the code to copy the appropriate fragments into a buffer.
The first and last fragments will be function entry and exit.
You will need to overcome whatever hurdle your CPU poses to execution
of what was recently data -- perhaps a system call to execute a
cache flush, or some operation on the pointer to the buffer to label
it as a pointer to code.
Dec 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dan glenn | last post by:
I want to redirect from an HTML file to a PHP script (to conserve a past link published elsewhere). What I'm doing is like: (file oldfile.html) <html> <head> <meta http-equiv=refresh...
3
by: Elaine Jackson | last post by:
I'm new to Python, and I've noticed the following: >>> def f(a,b): a+=b >>> def g(a,b): a=a+b >>> p= >>> q= >>> r= >>> s=
4
by: Richard Shea | last post by:
Hi - This is probably quite a stupid question but I've never understood what setup.py does. I've got a situation at the moment where I would like to use a script (which someone else has written and...
0
by: Janning Vygen | last post by:
Hi all, To all those people using mysql and asking if they should change to postgres, i just want them to tell that i came from mysql too but very (!) soon really wanted to use all these nice...
1
by: 9to5 | last post by:
Hey All, On my last compile something strange just happened. My main menu bar just disappeared from both my form designer view and the running application. I can't seem to get it back. All the...
22
by: Ark Khasin | last post by:
Due to a peculiar need (code instrumentation) I came across unexpected behavior of Visual Studio 6.0 and 2005 (doing the same thing): #include <stdio.h> #define CAT1(a,b) a ## b #define...
1
by: joestevens232 | last post by:
I'm stuck on how I can creat a LE object and pass it the line just input..than could i use a for loop and the .push_back to get the lines into the vector? vector<Log_Entry> parse(string); This...
0
by: money_making | last post by:
Just be a part of the big game and become our partner.It is easy and free.If you have a web site you just need to put our banner and wait your money.Just go to :...
0
by: manikandan | last post by:
dont miss it just open dont miss it just open dont miss it just open #############################
5
by: John Salerno | last post by:
I just installed Pylons onto my hosting server so I could try out templating with Mako, but it seems a little more complicated than that. From the look of it all, the site seems to want a full...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.