[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: kyseliäs.png (32 KB, 676x503)
32 KB
32 KB PNG
i want to generate x84x64 assembly within c program and have it compiled into bytecode during runtime for jit purposes. how to proceed? is there any nice small library specifically for this purpose?
>>
>>106766174
Ship a copy of GCC with your program and call it to do compilation of your generated code
>>
sir please proceed carefully do the needful bloody sir will be good
>>
>>106766174
C is not a difficult language to write a simple interpreter for
>>
I don't think you understand what you want.
>I want to generate X86_64 assembly from within a C program and have it compiled into byte code.
That's not how a JIT works. A JIT follows this process. code -> AST/ stack -> byte code -> system dependent asm. If you have x86_64 there is no next step you have your output. As for getting there I would suggest using LLVMs libraries. They have their own intermittent code that compiles down to native. Which is the simplest way to do something like this.
>>
>>106766174
Well you have to write a compiler, at best you can use something like llvm or cranelift to turn their IR into assembly otherwise you have to do that yourself
>>
File: 7933262.png (35 KB, 657x527)
35 KB
35 KB PNG
>>106768606
i have simple ast expressions that i want to optimize and turn them into executable bytecode, right now i'm pretty much asking chatgtp to generate defines that matches the asm.

                switch (e->as.bin.op)
{
case OP_Operand_Add:
APPEND_ASM("add rax, r9;\n");
APPEND_BYTES(ADD_RAX_R9);
break;

case OP_Operand_Sub:
APPEND_ASM("sub rax, r9;\n");
APPEND_BYTES(SUB_RAX_R9);
break;

case OP_Operand_Multiply:
APPEND_ASM("imul rax, r9;\n");
APPEND_BYTES(IMUL_RAX_R9);
break;

case OP_Operand_Divide:
APPEND_ASM("cqo;\nidiv r9;\n");
APPEND_BYTES(CDQ);
APPEND_BYTES(IDIV_R9);
break; // rax / r9-> rax

default: PARSER_THROW_ERR("Unsupported operator");
}


..and so on. would be much nicer if i could ditch the APPEND_BYTES out and compile the asm-string into bytecode.

>>106768657
for example, there's libggcjit and dynasm that could do the trick, but they are somewhat big decencies when the asm/bytecode part of the whole program is still relative very small entirety.
>>
>>106768851
>but they are somewhat big decencies when the asm/bytecode part of the whole program is still relative very small entirety.
Then the answer depends how much functionality you need, the libraries are big because they have to cover all functionality while the stuff you'd write yourself could just cover the limited part that you need
>>
>>106766174
AsmJit
>>
>>106768911
thanks, this looks just what i was looking for.
>>
>>106766174
You could look at embedding TCC. If you want just ASM implementing it yourself isn't unreasonable. It might be possible to embed LLVM backend or code gen but I'd imagine that to be pretty complex. Outside of codegen, you'll need to learn how to create executable memory regions on your OS, and this is different between Windows and Linux, and presumably OSX, though I would imagine OSX and Linux are similar.
>>
>>106768964
You're welcome.
>>
>>106766174
you have an asm which you want to compile into bytecode?
or what do you mean



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.