ark-lang / ark

A compiled systems programming language written in Go using the LLVM framework
https://ark-lang.github.io/
MIT License
682 stars 47 forks source link

Inline assembly #205

Open felixangell opened 9 years ago

felixangell commented 9 years ago

I wanted to port my Yash kernel thing to alloy, but I need inline assembly to make things a little easier... so inline assembly; how should we go about this?

MovingtoMars commented 9 years ago

Pipe it into GNU as?

I think the gcc syntax for output/input/clobbered registers in inline assembly is not great. A nicer syntax that makes more sense would be brilliant in Alloy.

muscar commented 9 years ago

Both GCC and clang support intel syntax, but it has some limitations.

felixangell commented 9 years ago

I'm not too sure about inline-assembly myself, it's rare that I use it. But if you guys have any ideas of what would be nice, throw em at me and I'll see what I can do. @MovingtoMars Are you implying we don't even use C's inline assembly stuff?

MovingtoMars commented 9 years ago

Sorry, I wasn't clear.

I just think that this syntax is ugly and has low readability:

asm ("cld\n\t"
             "rep\n\t"
             "stosl"
             : /* no output registers */ // ugly
             : "c" (count), "a" (fill_value), "D" (dest) // ugly
             : "%ecx", "%edi" // ugly
             );

This method-chaining solution seems nicer: asm("stuff goes here").setClobbered("%rax", "%rcx").addOutput(...).addInput(...)

I wasn't talking about the syntax of the assembly itself.

edit: github chomped up my newlines before, whoops

felixangell commented 9 years ago

@MovingtoMars Does look a lot cleaner, I like it.

raoulvdberge commented 9 years ago

IMHO I think that we should just provide a raw asm function, it is for libraries to add more syntactic sugar to this.

felixangell commented 9 years ago

@raoulvdberge Or both, perhaps?

muscar commented 9 years ago

@raoulvdberge :+1:

raoulvdberge commented 9 years ago

I still think we should keep it simple and just provide asm.

felixangell commented 9 years ago

@raoulvdberge We can do that to start with, and then we can add a nice wrapper around it later on.

semrekkers commented 9 years ago

Why not inline LLVM-IR code instead of inline assembly? Since you are porting the backend to LLVM, this may be better.

felixangell commented 9 years ago

@semrekkers Interesting idea, but I don't think it's the best choice, I'm not sure why and I'm not familiar with assembly. But I just have a gut feeling?

To elaborate, I think that in some cases people would prefer just having inline assembly over IR, since they would have to learn LLVM's IR too.

raoulvdberge commented 9 years ago

@felixangell I agree.

SamTebbs33 commented 9 years ago

Also, I don't really think LLVL bitcode is made for humans to write :D A lot of people will need actual assembly for things like operating system development and drivers etc.

semrekkers commented 9 years ago

@felixangell @raoulvdberge @SamTebbs33 I agree but, keep in mind that IR is portable across different architectures and assembly not. And if people really want inline assembly, that is not a big problem in LLVM's IR. Or maybe we can implement both?

raoulvdberge commented 9 years ago

@semrekkers Also, the codebase is modular enough to allow for multiple code generators, and the LLVM code generator might not always be available, so that wouldn't be too portable.

felixangell commented 9 years ago

I feel inline LLVM IR is not a great idea, LLVM IR isn't stable since the compiler may compile with 3.x but not 3.y, and the IR could vary between those versions.

semrekkers commented 9 years ago

@SamTebbs33 I don't mean the bitcode but the IR xD

semrekkers commented 9 years ago

@raoulvdberge yeah but most popular architectures have support. @felixangell that's right, so I guess inline assembly +1

felixangell commented 9 years ago

@semrekkers I mean I think inline ASM would take priority for now, but I'm not saying we can't do inline LLVM IR, but for now it's not a great idea.

semrekkers commented 9 years ago

@felixangell I understand, it's fine! Maybe also implement volatile? Since optimizing asm is not always a good idea.

raoulvdberge commented 9 years ago

@semrekkers That is not what I mean, if the language is compiled without LLVM support, LLVM IR code cannot be ommitted.

MovingtoMars commented 9 years ago

What would be the point of inline IR? The reason inline assembly is included is for low-level operating system access for writing things like kernels (ie loading the GDT, interrupt handlers). I don't think IR can accomplish that.

semrekkers commented 9 years ago

@raoulvdberge Aah you mean when LLVM is not used as backend, I had not thought about that. At this point it is indeed not a great idea to implement inline IR, and I agree with @MovingtoMars. It was just an idea, because I do not know any language that supports "inline IR" you know? What I don't like about assembly is that it's using different syntaxes and opcodes on different architectures and IR is a kind of abstract assembly lang. That's why I came on the idea, but it's a bad idea after all :D

raoulvdberge commented 9 years ago

i still think we should just provide a raw asm function

felixangell commented 9 years ago

@ark-lang/owners conclusions please

I'm good with a raw asm function too.

MovingtoMars commented 9 years ago

@0xc6 Ark can link with external symbol archives fine right now. Granted, there is only a mechanism for linking to shared libraries right now, but you can still compile Ark files to object files and then link them with a static object file manually.

@raoulvdberge @felixangell Define "raw asm function". That doesn't really mean anything.

felixangell commented 9 years ago

@MovingtoMars No clue, just what raoul said lol. Was kind of thinking of how Rust does asm though when I wrote it.

SamTebbs33 commented 9 years ago

Isn't that just something like

asm("mov edi, eax
add edx
sta edi")
MovingtoMars commented 9 years ago

@SamTebbs33 You can't just have that, you need syntax to tell the Ark compiler which registers are clobbered, etc. So you cannot use a true "raw" asm function.

SamTebbs33 commented 9 years ago

@MovingtoMars Yeah but you get the idea of what I'm assuming they mean about a raw asm function.

MovingtoMars commented 9 years ago

@0xc6 It's not a high priority right now, but it would be nice to include later.

kiljacken commented 9 years ago

:+1: For osdev and the like, inline assembly is a big quality of life improvement (if done properly). Invoking an external assembler shouldn't be neccesarry, as the llvm backend support generating arbitrary assembly (http://llvm.org/docs/LangRef.html#inline-assembler-expressions)

MovingtoMars commented 9 years ago

@kiljacken this is exactly what I'm thinking. Plus, the compiler cannot inline external assembly functions, so you're essentially using a useless function call to do something like disable interrupts.

kiljacken commented 9 years ago

LLVM might be able to do such link-time-optimizations, but we currently do not have support in the compiler for specifying external libraries/objects to link with.

kiljacken commented 8 years ago

So let's get to some conclusions up in this one.

The simplest way to go about this would be to wrap the llvm syntax directly. This would mean doing asm(code, clobbers, values...) or for example asm("bswap $0", "=r,r", y). This could play together nicely with #692.

felixangell commented 8 years ago

@kiljacken looks good to me

kiljacken commented 8 years ago

As of ark-lang/go-llvm@b7878fa there are no hindrances with regards to implementing inline asm. Now we just need to agree on the syntax, and then implement.