From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 2/2] docs: add the C FFI section
Date: Wed, 06 Nov 2024 04:11:04 +0000 (UTC) [thread overview]
Message-ID: <20241106041055.194975-3-carlos@maniero.me> (raw)
In-Reply-To: <20241106041055.194975-1-carlos@maniero.me>
This section describe both: How to call O lang from C and how to call C
from O.
Even that the "Calling O from C" may change as we evolve the discussions
if we you use or not some symbol mangle strategy it is worth to
document what we have working so far.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
docs/info/c-ffi.texi | 102 +++++++++++++++++++++++++++++++++++++++++++
docs/info/olang.texi | 3 ++
2 files changed, 105 insertions(+)
create mode 100644 docs/info/c-ffi.texi
diff --git a/docs/info/c-ffi.texi b/docs/info/c-ffi.texi
new file mode 100644
index 0000000..719fcd5
--- /dev/null
+++ b/docs/info/c-ffi.texi
@@ -0,0 +1,102 @@
+@node C FFI
+@chapter C FFI
+
+O programming language follows C's calling conventions, meaning that every O
+function can be called by C without any extra effort.
+
+All example bellow will use @code{gcc} but you can use the compiler/linker of
+your preference.
+
+@section Calling C from O
+
+To call external code you can use the @code{extern} statement.
+
+@subsection extern
+
+@verbatim
+# file: answer.ol
+
+extern fn u32print(number: u32): u32
+
+fn main(): u8 {
+ u32print(42)
+
+ return 0
+}
+@end verbatim
+
+That way instead of defining the @code{u32print} function. It must be provided
+at the linkage step.
+
+@verbatim
+// file: u32print.c
+
+#include <stdio.h>
+
+int u32print(int number) {
+ return printf("%d\n", number);
+}
+@end verbatim
+
+@subsection Compiling without linking
+
+If you try to compile the answer.ol file you will receive the follow error:
+
+@verbatim
+olc answer.ol -o answer.o
+/usr/bin/ld: answer.o.o: in function `main':
+(.text+0x10): undefined reference to `u32print'
+collect2: error: ld returned 1 exit status
+@end verbatim
+
+That's because @code{O} tries to link by default. To assemble the code without
+linking it, you can use the @code{-c} option.
+
+@verbatim
+olc answer.ol -c -o answer.o
+@end verbatim
+
+After that you can do the same with the C file that contains the function that
+you are looking to call.
+
+@verbatim
+gcc u32print.c -c -o u32print.o
+@end verbatim
+
+And then you can link both object files into an executable:
+
+@verbatim
+gcc answer.o u32print.o -o answer
+@end verbatim
+
+@subsection Calling O from C
+
+All that is required is to set the function prototype.
+
+@verbatim
+# file: sum.ol
+
+fn sum(a: u32, b: u32): u32 {
+ return a + b
+}
+@end verbatim
+
+@verbatim
+# file: csum.c
+
+#include <stdio.h>
+
+int sum(int a, int b);
+
+int main() {
+ printf("%d\n", sum(41, 1));
+ return 0;
+}
+@end verbatim
+
+Compiling:
+
+@verbatim
+olc sum.ol -c -o sum.o
+gcc sum.o csum.c -o csum
+@end verbatim
diff --git a/docs/info/olang.texi b/docs/info/olang.texi
index eea4c29..e6339c1 100644
--- a/docs/info/olang.texi
+++ b/docs/info/olang.texi
@@ -27,6 +27,7 @@ programming language.
* Introduction::
* Installation::
* Getting Started::
+* C FFI::
* Language Specification::
* Contribution Guide::
@end menu
@@ -37,6 +38,8 @@ programming language.
@include getting-started.texi
+@include c-ffi.texi
+
@include specification.texi
@include contribution-guide.texi
--
2.46.1
next prev parent reply other threads:[~2024-11-06 4:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-06 4:10 [PATCH olang v1 0/2] docs: add pointer and extern docs Carlos Maniero
2024-11-06 4:11 ` [PATCH olang v1 1/2] docs: add pointer docs Carlos Maniero
2024-11-06 4:11 ` Carlos Maniero [this message]
2024-11-06 4:12 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-11-06 14:46 ` [PATCH olang v1 0/2] docs: add pointer and extern docs Johnny Richard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241106041055.194975-3-carlos@maniero.me \
--to=carlos@maniero.me \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox