Coder Social home page Coder Social logo

How to write portable code? about asmjit HOT 15 CLOSED

asmjit avatar asmjit commented on May 20, 2024
How to write portable code?

from asmjit.

Comments (15)

refi64 avatar refi64 commented on May 20, 2024
#if defined(_M_X64) || defined(__amd64__) // 64-bit
typedef XmmReg FloatingReg;
#else // not 64-bit (hopefully 32-bit)
typedef FpReg FloatingReg;
#endif

from asmjit.

peastman avatar peastman commented on May 20, 2024

Unfortunately, it goes a lot deeper than that. You have to call completely different methods depending on what type of registers you're using. For example, if you're using Fp registers then you call fadd() to do addition, but if you're using Xmm registers then you call addsd().

from asmjit.

refi64 avatar refi64 commented on May 20, 2024
#if defined(_M_X64) || defined(__amd64__) // 64-bit
typedef XmmReg FloatingReg;
inline void addf(X86Assembler& a, FloatingReg& dst, FloatingReg& src) { a.addsd(dst, src); }
// repeat for other operations
#else // not 64-bit (hopefully 32-bit)
typedef FpReg FloatingReg;
inline void addf(X86Assembler& a, FloatingReg& dst, FloatingReg& src) { a.fadd(dst, src); }
// repeat for other operations
#endif

from asmjit.

peastman avatar peastman commented on May 20, 2024

Yes, I could write a completely new API that wraps AsmJit and provides an ABI agnostic interface. I was hoping there was a way to accomplish this without having to do that. :)

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

I would go with SSE2 plus handling only differences in ABI. Asmjit::Compiler can help with that, but it's also fine in raw Assembler mode.

You can probably write your own abstractions on top of asmjit, which can help you with FPU/SSE, but such abstractions are not trivial as FPU provides a lot of things SSE doesn't and vice versa.

I'm currently not planning to develop any abstractions on top of asmjit as there is already LLVM, which does nice job when it comes to universality.

I think that x87 FPU is deprecated anyway, it's maybe a huge waste of time to even think of it :-)

from asmjit.

peastman avatar peastman commented on May 20, 2024

I would go with SSE2 plus handling only differences in ABI.

Could you explain what you mean? For example, if I tell it to use SSE registers for floating point values but I compile in 32 bit mode, then arguments don't get passed correctly to functions. How do I make that happen correctly?

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

Well, this is what I meant by handling ABI differences. There is a class in asmjit called X86FuncDecl, which can be used to map argument to register or stack location. If you use only SSE2 the only problem would be to handle functions returning floating point (xmm0 in 64-bit mode and fp0 in 32-bit mode).

from asmjit.

peastman avatar peastman commented on May 20, 2024

How do I do that? To give a concrete example, here's a utility function I've written. It generates calls to functions that take a double as their argument and return a double.

void generateSingleArgCall(X86Compiler& c, X86XmmVar& dest, X86XmmVar& arg, double (*function)(double)) {
    X86GpVar fn(c, kVarTypeIntPtr);
    c.mov(fn, imm_ptr((void*) function));
    X86CallNode* call = c.call(fn, kFuncConvHost, FuncBuilder1<double, double>());
    call->setArg(0, arg);
    call->setRet(0, dest);
}

I see that X86Compiler::call() returns an X86CallNode, whose getDecl() method returns an X86FuncDecl. So I take it that it's already handling the arguments correctly? What do I need to add to get the return value into dest?

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

Actually returning a float/double is still an issue in 32-bit mode, the oldest open Issue #3 is not closed by because of this. I didn't fix this simply because I have never needed to return float/double from Compiler, but yeah, I should finally fix this one.

However, function parameters (float / double) should be handled properly in both 32-bit and 64-bit modes.

from asmjit.

peastman avatar peastman commented on May 20, 2024

If you can make returning floating point values work, that would be awesome. My application is all about floating point. More specifically, I'm adding JIT support to Lepton (https://simtk.org/home/lepton) for use in OpenMM (https://simtk.org/home/openmm).

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

Yeah, I will definitely take a look and update the issue

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

Peastman: Returning is now supported, I'm closing this for now as there is currently nothing to fix.

from asmjit.

peastman avatar peastman commented on May 20, 2024

That's great news! I'll give it a try.

from asmjit.

peastman avatar peastman commented on May 20, 2024

Using the latest version of asmjit from the repository and compiling in 32 bit mode, my program crashes when I try to execute the compiled code. If I compile in 64 bit mode it still works fine.

Is identical code supposed to work in both modes? Or do I need to do something differently in 32 bit mode?

from asmjit.

kobalicek avatar kobalicek commented on May 20, 2024

It should work the same, it's a bug if it doesn't.

I have just added this feature recently so it's possible it's still buggy, if you have any code that crashes fill a new issue and I will try to fix it.

from asmjit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.