0

Sorry if the title is a little obscure, I am not a native speaker and had a bit of trouble formulating my idea...

Assuming I have all the necessary functions and objects for a collection of procedures to be executed compiled and created in memory and that I know their addresses and sizes, how can I control the flow of the program with a pseudo "binary" file that is basically a script that says "push this data into this function's address, call this function address, push the returned value into this function address" and so on...

Basically I need to process pseudo "machine" code to access and dynamically control a collection of interconnected objects and static logic through their memory addresses.

Thanks!

EDIT: Please, post a few code snippets before closing my question as a duplicate to a question that doesn't really provide the specific information I need.

EDIT2: Added this from a comment below, possibly it will bring more clarification to my question:

Instead of compiling entire programs I try to use pre-compiled components to create dynamic objects on the go. The trees of objects can serialize to disk and be recreated in an instant (allocating the entire tree in a pool rather than object by object) so I have a way to create a dynamic program, save it to disk and reconstruct it in memory, with all the design time identifiers substituted with their addresses. NOW all I need is a way to make that entire program structure run.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • possible duplicate of [How to write an interpreter?](http://stackoverflow.com/questions/1926835/how-to-write-an-interpreter) – parapura rajkumar Jan 17 '12 at 14:16
  • My first reaction is that you should either write machine code here, or use an interpreted language and forget the byte codes completely. – Mark B Jan 17 '12 at 14:21
  • @Mark B - Machine code will be machine specific and non portable, I want code that is native to my components so if the components are compiled on a different machine the same portable managing code can be used. – dtech Jan 17 '12 at 14:24
  • So you know their addresses and sizes? If you also know their types then you can just cast the addresses to the functions and call them. – PeterT Jan 17 '12 at 14:25
  • 1
    To the OP: you seem to have fixated on some _implementation details_ (addresses, sizes) for something you haven't yet implemented. Perhaps stop worrying about these little details and explain the real requirements first. You talk about what sounds like an object graph, which you want to (de)serialize, and then equate that with an executable program. What does that mean? What relationship do these data structures have with your code and control flow? – Useless Jan 17 '12 at 14:47
  • Size is irrelevant, my system passes only pointers around and they are cast in place, it eliminates type safety but this is something that is being managed during the design stage and not a runtime issue. I think what I should implement is "function function" so I can initiate the execution of the program. – dtech Jan 17 '12 at 14:49
  • @ddriver well could you post a diagram of the layout of these binary objects in memory or add a description of how you serialize these objects so that we actually know what we are dealing with because your descriptions are very general so we can only give you general advice. – PeterT Jan 17 '12 at 17:29

2 Answers2

0

It sounds like you need to write an emulator for the machine whose language you want to interpret. Here is a link that explains how to do it.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • I don't want to emulate any other machine, I just want to access the per-compiled functionality of my program objects. How to push data into function pointers and call function pointers... – dtech Jan 17 '12 at 14:22
  • 1
    It's possible I'm just not understanding what you're trying to do, but when you say, "Basically I need to process pseudo 'machine' code to access and dynamically control a collection of interconnected objects and static logic through their memory addresses", it sounds like you need to emulate a machine. Can you describe in more detail what you're trying to do, if that's not it? You can call functions you wrote yourself via function pointers, for example, but I don't know if that's enough for what you're trying to do or not, since I don't fully understand what you're trying to do. – user1118321 Jan 17 '12 at 14:26
  • Well, it is kind of hard to explain it really. Instead of compiling entire programs I try to use pre-compiled components to create dynamic objects on the go. The trees of objects can serialize to disk and be recreated in an instant (allocating the entire tree in a pool rather than object by object) so I have a way to create a dynamic program, save it to disk and reconstruct it in memory, with all the design time identifiers substituted with their addresses. NOW all I need is a way to make that entire program structure run. – dtech Jan 17 '12 at 14:32
  • @ddriver well you can always memcpy it into place and cast it into your type but unless it's all basically one nested struct you'll have to deal with pointers/references that are no longer valid unless you save them as offsets and readd them to the their new memory-root. Also, why don't you use dlls as your "pre-compiled-components"? – PeterT Jan 17 '12 at 14:38
  • @PeterT - I already did the size calculation + alignment for objects, the idea is basically to to the same thing as the compiler does to stack objects, compact them and allocate/deallocate memory for the entire procedure. I can build a program in memory but I have problems accessing it since access logic is not supposed to be hardcoded, as a metaphor I have already set all the domino tiles to fall the right way but now I need to initiate the process... Sorry that I can't explain it better... – dtech Jan 17 '12 at 14:43
0

This is essentially what the Java Virtual Machine does, which I believe is what others are referring to as a machine. It is an interpreter, which is probably what you need to construct.

Assuming I understand what you said - the answer is no, not like that. You will have to do something to create program state and control logic flow, which is what interpreters and "machines" do.

You will need a front end that reads your code file, the file that tells the program how to run. Your front end reads the "logic file", then runs through the logic your front end just learned to call each of your objects. You can create this by linking smart plugins that do the same things, ie. construct a program state from your objects. But then you need a bunch of different plugins, one for each instance.

In any event there is no way to link your objects and then expect them to know who runs in what order, and who does not run. You have to transfer the "smarts" of your file into action somehow. Linking alone will not do it, because you want the smarts to be external to your object library.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • You seem to have generally got my idea. I haven't implemented linking yet but it should not be all that hard, any program "tree" that "#includes" a module will check the module bank for that module, and if the object already exists as implementation, the existing addresses are returned to be used by the linked instance. If not, the object implementation is inserted and then linked to its instance. At least that is the general idea... – dtech Jan 17 '12 at 15:14