I'm curious to know if any language exists that gives the programmer the possibility to 'emit' bytecode in the middle of the source code. To be more clear, is there any interpreted language that has a facility similar to the asm
keyword for c/c++ ?

- 279
- 2
- 8
-
1https://stackoverflow.com/tags/java-bytecode-asm/info says: "Output classes can be saved into files, or loaded dynamically by a `ClassLoader`". – Peter Cordes Sep 06 '18 at 19:45
-
Classic "interpreted" languages usually just have `eval` for injecting *dynamic* code in the same language. (Opposite of `asm` in C/C++, where the asm must be known at compile time). For most really interpreted languages, any intermediate representation is purely an implementation detail. You can *dump* it in CPython ([Exploring and decompiling python bytecode](https://stackoverflow.com/q/1149513)), but IDK if you can statically provide it. – Peter Cordes Sep 06 '18 at 19:52
-
I was more interested in a more direct way of using bytecode. Thanks for your comment. – Antonio Molinaro Sep 06 '18 at 19:52
-
I agree. I didn't know about the distinction between the two words – Antonio Molinaro Sep 06 '18 at 20:11
-
1disliking a question and not explaining why it's totally inexcusable – Antonio Molinaro Sep 07 '18 at 00:50
-
That reminds me, how the some of the old Basic dialects on home computes worked, especially the popular C64. You could enter the two byte token instead of the command, but the next time you list your program, it will pretty-print them as if you entered them normally. But your question is not consistent; are you talking about *bytecode* or “a facility similar to the `asm` keyword”, as the latter uses assembly syntax (mnemonics) rather than bytecode? – Holger Sep 07 '18 at 06:58
-
1@AntonioMolinaro I suppose the downvote is because this is a “list random things with this property” question which doesn't really fit the format of this site. – fuz Sep 07 '18 at 10:23
-
@Holger Let's say pretty-printed byte-code. I'm talking about an IR mixed in the source code – Antonio Molinaro Sep 07 '18 at 15:21
2 Answers
I'm not sure if this counts, but in Forth you can traditionally do this. You can leave the compiler with [
at any point and manipulate the byte code and compiler state as you like before resuming compilation with ]
. The word ,
directly emits the word on the stack into the byte code. For example, the following word pushes 6 × 7 on the stack; comments are delimited with parentheses:
: answer ( create a word answer, start the compiler )
[ ( stop the compiler )
6 7 * ( compute 6 × 7 )
' LIT ( push the word LIT (push literal) on the stack )
, ( append it to the machine code )
, ( append 6 × 7 to the machine code )
] ( resume compilation )
; ( finish the definition of answer )
This code works the same as if you wrote
: answer 42 ;
which is compiled to the byte code
LIT 42 EXIT
The word LIT
takes the next word from the byte code stream and pushes it on the stack, EXIT
returns from the current byte code function.

- 88,405
- 25
- 200
- 352
There cannot be stricto sensu an equivalent of asm
, because it is essentially for compiled languages (and asm
is possible in C because C compilers emit assembler code!).
I have published in my DSL2011 paper a description of MELT - a Translated Domain Specific Language Embedded in the GCC Compiler
I describ in that paper several traits which helps in generating C code from MELT (which is a Lisp-like language translated to C or C++).
But interpreted languages with a bytecode interpreter (e.g. Lua, Guile, Nim, Ocaml) provide hooks to add new primitives into that bytecode interpreter. Usually, the bytecode operation would be something like invoke primitive#N with arguments arg1 arg2 arg3.
You could implement your language (some DSL) as a translator to C. This is usual practice, and quite fun to do. You then code some "naive" compiler from your language to C. You could consider instead using some JIT-compiling library like libgccjit or LLVM or libjit or lightning or asmjit.
And some languages are homoiconic, they are then exposing somehow their bytecode or some good enough IR. Learn Lisp (at least read SICP) then read Lisp In Small Pieces
Be aware of Greenspun's tenth rule. Look into The circuit less traveled talk by Liam Proven at FOSDEM 2018.

- 223,805
- 18
- 296
- 547
-
Honestly, I have achieved it. I'm only curious to know if it has been already done.func size() { asm: "native_call", " #1\n", "exp_ret\n"; } – Antonio Molinaro Sep 06 '18 at 19:57
-
-
-
Ber sure to read the references I gave you. You probably want to mention them – Basile Starynkevitch Sep 06 '18 at 20:06
-
Which one? I mentioned several things! And take time to *read* it! – Basile Starynkevitch Sep 06 '18 at 20:09
-
-
1That is the paper I wrote. But *Lisp In Small Pieces* will teach you much more (and also SICP) – Basile Starynkevitch Sep 06 '18 at 20:22