Page 1 of 1

[NXC] Implementing Pointers

Posted: 20 Jul 2011, 06:24
by muntoo
If one* were to implement pointers, how would they do it?

Miscellaneous questions:
  • What assembly instruction set is NBC closest to? (x86? "Intel syntax"? I'm a beginner to this stuff. :))
  • How would the RXE byte code look like for the "pointers code"?
  • Where would the changes have to be made in:
    • The firmware (c_cmd.c?)
    • The compiler (implementing it in NBC, then the syntax tree/etc stuff for the NXC compiler)
    • The RXE executable file format
  • NEW: Which [free!!] compiler shall I use? :)
*No one in particular.

Re: [NXC] Implementing Pointers

Posted: 20 Jul 2011, 21:06
by spillerrec
iirc, every variable has an ID. I do not know how the firmware actually handles it but here is how I think it works: To read/write to the variable the firmware will need to look that ID up and return the corresponding variable and type.
A pointer variable type should be added, but instead of having a position in memory it should contain an ID to a variable. It would need its own special type and store the ID as an unsigned int. When the firmware looks up the variable, it should notice the pointer type and instead of returning the result it should look it up again but with the new ID. (A recursive process, with the danger of looping.)

Every opcode which receives the pointer will then act on the variable it points to and never the ID it actually contains. (IIRC you will not be able to directly access members of a pointed structure, you would have to move it into a temporary structure of same type.)
The mov-opcode will also not affect the ID, it will affect the pointed variable. A 'set-address' opcode will do that instead, first parameter is the pointer and second parameter is the address. A 'get-address' opcode will then return the ID of the second parameter into the first.
A bit of a warning though, an ID of 0 is valid, a NULL pointer would actually be an entirely different value... (Can't remember it though, I think it was something in the lines of 6xxxx.)
(Edit: I changed this part significantly since my earlier idea wasn't very C like...)

(Dynamically created variables could be stored in those as long they are given an ID, but that is an entirely different matter.)

Well, this is my idea of how a simple pointer implementation would work on the standard firmware.

Re: [NXC] Implementing Pointers

Posted: 21 Jul 2011, 11:34
by timpattinson
muntoo wrote:
  • NEW: Which [free!!] compiler shall I use? :)
For what?
The FW, or stealing their code?
I know you can create an ARM project in Code::Blocks, with gcc
screen.jpg
Maybe if would work better with IAR embedded Workbench (free 128kb limited version, for the nxt), which the Lego devs used