c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Support for function address in ASM

Open ygorpontelo opened this issue 11 months ago • 6 comments

Seems like dealing with function adress is not yet implemented. The assembly i'm trying to inline kinda requires it, so it would be useful to have it. I hit a TODO at c3c/src/compiler/sema_asm.c:537.

Also, as a side note, the asm block is a bit confusing as to what is implemented and what is not. For example, i tried to use jmp but it said it wasn't a valid instruction, another one was xchgq. Can you elaborate a bit more on what's available? I'm using x86 arch. It seems like there's some variations on the syntax to AT&T too, could you clarify that as well?

It would also be very nice to have some of the instructions infer the size, so i don't have to put the suffix all the time, like pushq.

ygorpontelo avatar Jan 25 '25 03:01 ygorpontelo

Inferring the size is problematic.

Regarding the instruction set I am hoping for someone to take charge of this to implement all the remaining instructions. It should be straightforward to contribute additional instructions if you look at the implemented instructions: in / out needs to be defined + the clobbers.

jmp is missing having label implemented, but mostly this is because I didn't have any users, so nothing to really code towards. If you send me the code you want to work, then I'll fix that quickly. I need use-cases basically.

lerno avatar Jan 25 '25 21:01 lerno

Understood. My use case was implementing coroutines, i'm using this as a reference, other implementations were too confusing for me. Most of the C part was easy to do in C3, but the assembly i was hoping to use with asm blocks to minimize the dependencies of the project, in particular the x86_64 assembly for now.

I'm interested in helping, but i'm no expert so it will take time for me to get it right. Btw, i'm aware @rexim also implemented coroutines in C. I believe his implementation would be classified as symmetric coroutines, because they don't give the control back to the caller. This paper i was reading has more info on it. Maybe he can also help with the assembly given the shared interest in this. :D

ygorpontelo avatar Jan 26 '25 03:01 ygorpontelo

If you give me some asm that works good in C I can use that as a test case, it doesn't need to be the exact same asm.

lerno avatar Jan 27 '25 12:01 lerno

Is this the same error as #1927 ?

lerno avatar Feb 05 '25 00:02 lerno

They are similar but are not the same for two reasons that i can see:

  1. TODO line is different, 537 != 206
  2. In the other code he is trying to reference a variable address, i wanted to reference a function address.

Regarding the asm you asked (i forgot to respond, sorry!), i don't quite have anything that is working in C regarding the project i'm working on. I was trying to port some things to C3 but eventually decided it was best to just use nasm. I could try to make something simple that just uses a function address, prob will have some time this weekend. Inline assembly in C is quite ugly.

ygorpontelo avatar Feb 05 '25 08:02 ygorpontelo

I'll hopefully look at the asm this weekend too. It was hard to understand the code when I did an attempt on #1927. In particular I was unsure that I did it correctly. I have also been thinking whether one can make the definition files for each ASM even easier to write. I want to add labels while I am at it.

lerno avatar Feb 05 '25 09:02 lerno