Welcome, Guest. Please login or register.

Author Topic: Threaded Code  (Read 2408 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Threaded Code
« on: November 24, 2010, 02:31:29 PM »
So here I am thinking about an idea... And it turns out my idea already has a name, "Threaded Code"!

My question is, is there any C/C++ legal way to jump to an address? A computed Goto if you will...

goto *someaddress;

Offline ElPolloDiabl

  • Hero Member
  • *****
  • Join Date: May 2009
  • Posts: 1702
    • Show only replies by ElPolloDiabl
Re: Threaded Code
« Reply #1 on: November 24, 2010, 02:51:14 PM »
Don't no too much about C, but I read that it explicitly doesn't allow such things.
Go Go Gadget Signature!
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #2 on: November 24, 2010, 02:55:18 PM »
Quote from: ElPolloDiabl;594070
Don't no too much about C, but I read that it explicitly doesn't allow such things.
Yeah... I think I've found a gcc extention that allows it... But I'd prefer something more portable :(

Offline skurk

  • Hero Member
  • *****
  • Join Date: Dec 2006
  • Posts: 929
    • Show only replies by skurk
Re: Threaded Code
« Reply #3 on: November 24, 2010, 02:57:33 PM »
Yes, something like this should work

void (*app)() = (void(*))0x123456;

and then later

app();

I can't test this right now, but I'm pretty sure the above would jump to addres 0x123456.
Code 6502 asm or... DIE!!

[C64, C128, A500, A600, A1200, A3000, MBP+Mini, Efika/MOS2.1, Sam440 w/AOS4.1
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Threaded Code
« Reply #4 on: November 24, 2010, 02:59:42 PM »
Quote from: bloodline;594067
So here I am thinking about an idea... And it turns out my idea already has a name, "Threaded Code"!

My question is, is there any C/C++ legal way to jump to an address? A computed Goto if you will...

goto *someaddress;


The most obvious legal way to do this is to use a table of function pointers.
int p; // A
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #5 on: November 24, 2010, 02:59:44 PM »
Quote from: skurk;594072
Yes, something like this should work

void (*app)() = (void(*))0x123456;

and then later

app();

I can't test this right now, but I'm pretty sure the above would jump to addres 0x123456.
No, that's a function pointer which will invoke the calling convention (saving registers etc) which would add a lot of overhead a threaded code Virtual Machine...

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Threaded Code
« Reply #6 on: November 24, 2010, 03:03:36 PM »
Quote from: bloodline;594074
No, that's a function pointer which will invoke the calling convention (saving registers etc) which would add a lot of overhead a threaded code Virtual Machine...


Well the gcc extension is as portable as gcc is. If you keep your dubious computed branch target code to just one translation unit, you could always make it an exception in your makefile and have everything else all nice and ANSI.
int p; // A
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #7 on: November 24, 2010, 03:06:09 PM »
Quote from: Karlos;594073
The most obvious legal way to do this is to use a table of function pointers.
I appreciate the advice (and from skurk too), but a function pointer is just too expensive in this regard... My VM needs to run on my 100mhz ARM M3 microcontroller... We have to think lightweight! But I also need it to be portable so I can test it on my test machine... An x86... So ARM Asm is going to be problematic...

Offline skurk

  • Hero Member
  • *****
  • Join Date: Dec 2006
  • Posts: 929
    • Show only replies by skurk
Re: Threaded Code
« Reply #8 on: November 24, 2010, 03:08:04 PM »
Quote from: bloodline;594074
No, that's a function pointer which will invoke the calling convention (saving registers etc) which would add a lot of overhead a threaded code Virtual Machine...


Oh, then I misunderstood your question. :-)
Code 6502 asm or... DIE!!

[C64, C128, A500, A600, A1200, A3000, MBP+Mini, Efika/MOS2.1, Sam440 w/AOS4.1
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #9 on: November 24, 2010, 03:08:11 PM »
Quote from: Karlos;594076
Well the gcc extension is as portable as gcc is. If you keep your dubious computed branch target code to just one translation unit, you could always make it an exception in your makefile and have everything else all nice and ANSI.
Yeah, that's almost certainly the most intelligent way to do it! :)

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #10 on: November 24, 2010, 03:10:22 PM »
Quote from: bloodline;594074
No, that's a function pointer which will invoke the calling convention (saving registers etc) which would add a lot of overhead a threaded code Virtual Machine...


Quote from: skurk;594078
Oh, then I misunderstood your question. :-)


Not really, your advice was good, I didn't provide enough specification for you! As Karlos has also pointed out function pointers are the legal way to do this... I just don't want to save the registers for every function call!

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #11 on: November 24, 2010, 03:39:50 PM »
If I'm happy to stay with gcc, then this seems to be the winner:

http://docs.freebsd.org/info/gcc/gcc.info.Labels_as_Values.html

Bit sucky really...

Offline commodorejohn

  • Hero Member
  • *****
  • Join Date: Mar 2010
  • Posts: 3165
    • Show only replies by commodorejohn
    • http://www.commodorejohn.com
Re: Threaded Code
« Reply #12 on: November 24, 2010, 06:14:00 PM »
If you're just writing for ARM and testing on x86, couldn't you use a conditionally-compiled bit of assembler like:
Code: [Select]
#ifdef ARM
asm { /* whatever */}
#else
asm { /* x86 equivalent */}
#endif
or somesuch?
Computers: Amiga 1200, DEC VAXStation 4000/60, DEC MicroPDP-11/73
Synthesizers: Roland JX-10/MT-32/D-10, Oberheim Matrix-6, Yamaha DX7/FB-01, Korg MS-20 Mini, Ensoniq Mirage/SQ-80, Sequential Circuits Prophet-600, Hohner String Performer

"\'Legacy code\' often differs from its suggested alternative by actually working and scaling." - Bjarne Stroustrup
 

Offline bloodlineTopic starter

  • Master Sock Abuser
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 12113
    • Show only replies by bloodline
    • http://www.troubled-mind.com
Re: Threaded Code
« Reply #13 on: November 24, 2010, 06:16:46 PM »
Quote from: commodorejohn;594103
If you're just writing for ARM and testing on x86, couldn't you use a conditionally-compiled bit of assembler like:
Code: [Select]
#ifdef ARM
asm { /* whatever */}
#else
asm { /* x86 equivalent */}
#endif
or somesuch?
It's not the code paths that I have a problem with... it's maintaining two code paths :lol: and in ASM too... kinda defeats the point of using C in the first place ;)

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: Threaded Code
« Reply #14 on: November 24, 2010, 06:38:20 PM »
Quote from: bloodline;594081
Not really, your advice was good, I didn't provide enough specification for you! As Karlos has also pointed out function pointers are the legal way to do this... I just don't want to save the registers for every function call!


Necessarily it is not saving the registers for every function call.  It is platform specific...
My Amigas: A500, Mac Mini and PowerBook