Inline assembly

Discussion in 'Java' started by samir, Aug 7, 2006.

  1. samir

    samir Guest

    Is it possible to use "inline assembly" within java code?
    samir, Aug 7, 2006
    #1
    1. Advertising

  2. samir

    Daniel Dyer Guest

    On Mon, 07 Aug 2006 21:39:15 +0100, samir <>
    wrote:

    > Is it possible to use "inline assembly" within java code?


    For which architecture? Java programs run on virtual machines. The
    actual hardware could be x86, Sparc, PowerPC or something else.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Aug 7, 2006
    #2
    1. Advertising

  3. samir wrote:
    > Is it possible to use "inline assembly" within java code?
    >


    why? what is it you are trying to achieve?
    AndrewMcDonagh, Aug 7, 2006
    #3
  4. samir

    Chris Uppal Guest

    samir wrote:

    > Is it possible to use "inline assembly" within java code?


    No.

    If you want to "inline" JVM bytecode in your Java code then you could possibly
    do it by finding/writing a custom compiler. But there is very little point in
    doing that since JVM bytecode is very, very, similar to Java and there's not
    much that you can do at that level which you can't also do in pure java.

    If you want to inline harware-level machine instructions, then you cannot do it
    at all. It is completely impossible.

    -- chris
    Chris Uppal, Aug 8, 2006
    #4
  5. samir

    samir Guest

    Thanks for the replies, I didn't think someone will be interested in
    it.

    I want to use inline assembly to talk directly to the JVM (you might
    have figured it out, I'm addicted to C and assembly).

    Also, I think that java can't deal properly with "soft based" dynamic
    typed objects.

    Adiaux
    Samir
    samir, Aug 8, 2006
    #5
  6. samir wrote:
    > Thanks for the replies, I didn't think someone will be interested in
    > it.
    >
    > I want to use inline assembly to talk directly to the JVM (you might
    > have figured it out, I'm addicted to C and assembly).


    You would probably do more harm than good. The hotspot JIT can get
    confused by premature optimization and you might get slower code, not
    faster. I fact, Sun removed certain "optimizations" from the code
    generation of the compiler to allow the JIT to work better.

    But if you want to try: http://asm.objectweb.org/

    > Also, I think that java can't deal properly with "soft based" dynamic
    > typed objects.


    Whatever that should be. Java is (by the usual definitions), a
    statically typed language. You might need a miracle to turn it into a
    dynamic typed language.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Aug 8, 2006
    #6
  7. samir

    Chris Uppal Guest

    samir wrote:

    > I want to use inline assembly to talk directly to the JVM (you might
    > have figured it out, I'm addicted to C and assembly).


    I suspect you'd be better of working directly with bytecode for everything.
    There are a number of byte code libraries available -- ASM and BCEL are the
    best known. There are also some higher-level libraries which may help in one
    way or another. This page lists several (none of which I can vouch for in any
    way):

    http://java-source.net/open-source/bytecode-libraries


    > Also, I think that java can't deal properly with "soft based" dynamic
    > typed objects.


    Working at bytecode level gives you only a little better chance to to implement
    "soft typed" languages -- the JVM bytecode is a statically typed language
    itself (checked by the verifier) and while working at that level does at least
    allow you to call methods which the Java compiler wouldn't (if it didn't know
    in advance that they were going to exist at runtime), it doesn't provide help
    in recovering from situations where the method turns out not to exist after
    all. At least, not if you want to do anything more elaborate than throwing an
    exception.

    However, this might interest you, if you haven't seen it already:

    http://blogs.sun.com/roller/page/gbracha?entry=invokedynamic

    I have no idea at all how likely it is to come to fruition.

    -- chris
    Chris Uppal, Aug 8, 2006
    #7
  8. samir

    Daniel Dyer Guest

    On Tue, 08 Aug 2006 14:36:56 +0100, Chris Uppal
    <-THIS.org> wrote:

    > However, this might interest you, if you haven't seen it already:
    >
    > http://blogs.sun.com/roller/page/gbracha?entry=invokedynamic


    A man with the job title "Computational Theologist" complaining about
    inventing confusing new names for old things. A Computational Theologist
    at Sun Microsystems no less. The same Sun Microsystems that gave us the
    Java version mess, the Solaris version mess and the brilliantly misleading
    "Sun Java Desktop".

    > I have no idea at all how likely it is to come to fruition.


    Here's a more recent mention of the same thing:

    http://www.eweek.com/article2/0,1759,1997386,00.asp?kc=EWRSS03119TX1K0000594

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Aug 8, 2006
    #8
  9. samir

    Chris Uppal Guest

    Daniel Dyer wrote:

    [me:]
    > > http://blogs.sun.com/roller/page/gbracha?entry=invokedynamic

    >
    > A man with the job title "Computational Theologist" complaining about
    > inventing confusing new names for old things. A Computational Theologist
    > at Sun Microsystems no less.


    I rather doubt whether "Computational Theologist" really is his official job
    title, but if it is then it's definitely not a case of "jargon [which] has been
    used throughout the ages to intimidate and exclude" but rather what we, in the
    dynamic language world, call a "joke" (perhaps you've heard of them?). Or
    possibly a piss-take aimed at the various folk with "evangelist" in their
    titles. Or both...

    FWIW, I don't -- not by a long, long, way -- include Bracha in the cluster of
    head-up-own-arse technolgists who have done so much to produce, promote, and
    develop Java into the ungainly hybrid it has become (and which, indeed, it
    started as -- but not so much so).


    > The same Sun Microsystems that gave us the
    > Java version mess, the Solaris version mess and the brilliantly misleading
    > "Sun Java Desktop".


    It's not an impressive record, is it ? My own favorite was when the JVM
    finally gained a not-completely-brain-dead garbage collector[*] -- the "exact
    GC", IIRC -- and Sun had the nerve to phrase their descriptions of it as if
    they had /invented/ proper garbage collection !

    ([*] the current GC is impressive, but early Java gc was a pathetic joke).

    -- chris
    Chris Uppal, Aug 8, 2006
    #9
  10. samir

    Daniel Dyer Guest

    On Tue, 08 Aug 2006 15:44:50 +0100, Chris Uppal
    <-THIS.org> wrote:
    > I rather doubt whether "Computational Theologist" really is his official
    > job
    > title, but if it is then it's definitely not a case of "jargon [which]
    > has been
    > used throughout the ages to intimidate and exclude" but rather what we,
    > in the
    > dynamic language world, call a "joke" (perhaps you've heard of them?).


    http://bracha.org/Site/Theology.html

    If it is a joke, it's not a funny one :(

    > FWIW, I don't -- not by a long, long, way -- include Bracha in the
    > cluster of
    > head-up-own-arse technolgists who have done so much to produce, promote,
    > and
    > develop Java into the ungainly hybrid it has become (and which, indeed,
    > it
    > started as -- but not so much so).


    I was merely commenting on the irony of his complaints about Microsoft
    given his own position. My main reason for responding was the link I
    posted which seems to suggest that the invokedynamic thing is going to
    happen.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Aug 8, 2006
    #10
  11. samir

    Oliver Wong Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:44d85c15$3$637$...
    > samir wrote:
    >
    >> Is it possible to use "inline assembly" within java code?

    >
    > No.
    >
    > If you want to "inline" JVM bytecode in your Java code then you could
    > possibly
    > do it by finding/writing a custom compiler. But there is very little
    > point in
    > doing that since JVM bytecode is very, very, similar to Java and there's
    > not
    > much that you can do at that level which you can't also do in pure java.
    >
    > If you want to inline harware-level machine instructions, then you cannot
    > do it
    > at all. It is completely impossible.


    What if you stored the machine instructions as data, and at the
    appropriate time, emit them to a file, and called Runtime.exec(filename)?

    - Oliver
    Oliver Wong, Aug 8, 2006
    #11
  12. samir

    samir Guest

    Thanks for the links, they where very usefull (the ASM one was perfect
    :).

    Oliver Wong wrote:
    > What if you stored the machine instructions as data, and at the
    > appropriate time, emit them to a file, and called Runtime.exec(filename)?


    Nice idea! So, i'll return (yes!) to my old gcc, lex and yacc and write
    an interface that will build all the necessary files and supervise the
    "make" process.
    The problem now is parsing the compilers error messages but it will be
    fine :)

    I've always admired the auto changing code, using a JIT seems to be the
    right answer but inline assembly has its charms too.

    Adiaux
    Samir
    samir, Aug 8, 2006
    #12
  13. samir

    jmcgill Guest

    samir wrote:

    > Nice idea! So, i'll return (yes!) to my old gcc, lex and yacc and write
    > an interface that will build all the necessary files and supervise the
    > "make" process.


    You talk about these tools as though you think they are bad things.

    While I love Java for its strengths, I still have a deep appreciation for C.
    jmcgill, Aug 8, 2006
    #13
  14. samir

    Chris Uppal Guest

    Oliver Wong wrote:

    > What if you stored the machine instructions as data, and at the
    > appropriate time, emit them to a file, and called Runtime.exec(filename)?


    Or even jump directly to them with a bit of JNI magic.

    But in neither case is the inserted code really being executed inline -- you
    don't know what the surrounding code is going to be JITed into, so you can't
    really do anything that you couldn't do with a normal function call (native or
    not).

    -- chris
    Chris Uppal, Aug 9, 2006
    #14
  15. samir

    bugbear Guest

    samir wrote:
    > Thanks for the replies, I didn't think someone will be interested in
    > it.
    >
    > I want to use inline assembly to talk directly to the JVM (you might
    > have figured it out, I'm addicted to C and assembly).


    I'd stay with them, then, rather than trying to make Java
    do something it wasn't designed for.

    I find that Java supports considerable freedom of expression
    for significantly complex programs, and "adequate" optimisation
    (courtesy of JIT) in tight processing loops.

    I have used 'C' and assembler for performance (at one time
    in a world leading piece of software, which was primarily
    judged on performance), but hav been using Java for
    the last few years without too many pangs of loss.

    BugBear
    bugbear, Aug 9, 2006
    #15
  16. On Wed, 9 Aug 2006 10:08:05 +0100, Chris Uppal wrote:
    > Oliver Wong wrote:
    >> What if you stored the machine instructions as data, and at the
    >> appropriate time, emit them to a file, and called Runtime.exec(filename)?

    >
    > Or even jump directly to them with a bit of JNI magic.


    That's not possible on systems where code segments aren't writeable,
    and data segments aren't executable. I believe that's true of
    virtually all modern hardware.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Aug 9, 2006
    #16
  17. samir

    Chris Uppal Guest

    Gordon Beaton wrote:

    [me:]
    > > Or even jump directly to them with a bit of JNI magic.

    >
    > That's not possible on systems where code segments aren't writeable,
    > and data segments aren't executable. I believe that's true of
    > virtually all modern hardware.


    Ok, ok...

    So /copy/ the bytes to a writable area, then change it into a code segment --
    that's what the JITer has to do after all ;-)

    -- chris
    Chris Uppal, Aug 9, 2006
    #17
  18. samir

    samir Guest

    Chris Uppal wrote:
    > Gordon Beaton wrote:
    >
    > [me:]
    > > > Or even jump directly to them with a bit of JNI magic.

    > >
    > > That's not possible on systems where code segments aren't writeable,
    > > and data segments aren't executable. I believe that's true of
    > > virtually all modern hardware.

    >
    > Ok, ok...
    >
    > So /copy/ the bytes to a writable area, then change it into a code segment --
    > that's what the JITer has to do after all ;-)
    >
    > -- chris


    That remindes me of "superposing" the code segment and the data segment
    using the same (copy of) GDT (x86) entry but with diffrent flags. This
    is used with Grub and some other bootloaders when getting to the
    protected mode.

    This would only work if you have the access to such low level
    structures, and would make the java code lose it's portability.

    Adiaux
    Samir
    samir, Aug 9, 2006
    #18
  19. samir

    Ray Thompson Guest

    On Tue, 08 Aug 2006 03:57:17 -0500, Chris Uppal
    <-THIS.org> wrote:

    > samir wrote:
    >
    >> Is it possible to use "inline assembly" within java code?

    >
    > No.
    >

    Well, since NONE of the books I've read about Java programming mention
    inline assembly (or even ASSEMBLY), I have to assume tht it's either
    impossible or impractical. So, I agree with Chris.
    Ray Thompson, Aug 9, 2006
    #19
  20. samir wrote:
    > Chris Uppal wrote:
    >> Gordon Beaton wrote:
    >>
    >> [me:]
    >>>> Or even jump directly to them with a bit of JNI magic.
    >>> That's not possible on systems where code segments aren't writeable,
    >>> and data segments aren't executable. I believe that's true of
    >>> virtually all modern hardware.

    >> Ok, ok...
    >>
    >> So /copy/ the bytes to a writable area, then change it into a code segment --
    >> that's what the JITer has to do after all ;-)
    >>
    >> -- chris

    >
    > That remindes me of "superposing" the code segment and the data segment
    > using the same (copy of) GDT (x86) entry but with diffrent flags. This
    > is used with Grub and some other bootloaders when getting to the
    > protected mode.
    >
    > This would only work if you have the access to such low level
    > structures, and would make the java code lose it's portability.
    >


    doing any assembler in /calling from Java, losing Java's portability.



    > Adiaux
    > Samir
    >
    AndrewMcDonagh, Aug 9, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    725
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    464
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    447
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    1,830
    Kevin Bracey
    Nov 21, 2003
  5. Nish
    Replies:
    4
    Views:
    479
    Thomas Stegen
    Oct 8, 2004
Loading...

Share This Page