question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compile snippets into bytecode in assembler

See original GitHub issue

Compile snippets into bytecode in assembler

The idea is to allow users to open a method assembler, then open a prompt allowing them to type in something like System.out.println("test"). This line would then be compiled against the current source and inserted into the assembler as lines of method bytecode.

It would be nice to support arbitrary variable support too, such as: if ($0 != 0){} --> *LOAD 0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
Col-Ecommented, Dec 19, 2020

Could be improved, but variable support is in!

Need to get some proper unit tests made too…

3reactions
andylizicommented, Aug 24, 2020

How can javassist access the value on the stack?

No it can’t - at least, not in its current state. After some digging however, I believe its possible to make it work without major modifications.

Javassist has a complete lexer/parser/bytecode-generator system just built for this, like a lightweight version of JavaParser. It will first parse the statement using a pre-populated symbol table and generate an AST tree representation. Then the corresponding bytecode will be emitted at the various at*** methods in CodeGen, MemberCodeGen and JvstCodeGen. One of these methods, atVariable, goes like this:

@Override
public void atVariable(Variable v) throws CompileError {
    Declarator d = v.getDeclarator();
    exprType = d.getType();
    arrayDim = d.getArrayDim();
    className = d.getClassName();
    int var = getLocalVar(d);

    if (arrayDim > 0)
        bytecode.addAload(var);
    else
        switch (exprType) {
        case CLASS :
            bytecode.addAload(var);
            break;
        case LONG :
            bytecode.addLload(var);
            break;
        case FLOAT :
            bytecode.addFload(var);
            break;
        case DOUBLE :
            bytecode.addDload(var);
            break;
        default :   // BOOLEAN, BYTE, CHAR, SHORT, INT
            bytecode.addIload(var);
            break;
        }
}

As you can see, this kind of structuring makes it easy to implement our own bytecode emitter that works on the stack. We won’t even need to change the pesky parser part (except here), just feed it some type info about things-on-stack through the symbol table and write some custom at*** generators and it magically works. At least, it should. Never done this before but don’t see why it shouldn’t - a value is a value, no matter whether it comes from a variable or the stack. It won’t be ‘piece of cake’ level of easy, but it should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to translate assembly code snippet into binary machine ...
I did a workaround to my problem. It works on Windows platforms and uses ML64 assembler that goes with Visual Studio.
Read more >
Know Your Stack — Scala to Java to Bytecode to Assembly
A JVM takes bytecode and JITs it to machine code at runtime. It will even compile hot code that is being JITd frequently....
Read more >
RAVM: compile once and run anywhere - zsmith.co
a simple assembler (RASM) that produces bytecode, which is to say the virtual machine language that RAVM runs. What is the justification for...
Read more >
Delphi bytecode compiler - Jon L. Aasenden - WordPress.com
I decided to do something a bit novel. Rather than just creating some classes to help you generate bytecode, I decided to create...
Read more >
Building Compilers and Interpreters - Open Book Project
and an "assembler" program would translate this into machine code (just ... Python first compiles source code in a .py file into "byte...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found