From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id QMugJn7sKmdF1gAAqHPOHw:P1 (envelope-from ) for ; Wed, 06 Nov 2024 05:11:42 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id QMugJn7sKmdF1gAAqHPOHw (envelope-from ) for ; Wed, 06 Nov 2024 05:11:42 +0100 X-Envelope-To: patches@johnnyrichard.com Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b="Yj/79RzT"; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=e6OWmGxF; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Seal: i=2; s=key1; d=johnnyrichard.com; t=1730866302; a=rsa-sha256; cv=pass; b=GWrQUov4YmDLhP4Z9YRlDUl7bX72zgsJavTLtzjdUNWBEOB0IicxeQmSZswms9JVa6iuiF ULw0mb/1bSZMk0eMZQ7w1V1vByeCn0ePMl829b+fUOTjwiTPYOoHNpDP9EAhKPsIreSBI7 SO6wupns3AGAKnO7eLEfHNHJLN8v0gr9bO2IaSi2a7SOsrzwPeR0xXtBg0qOL0bsCfXDoh ltm1CgRAgAj4pt6QrGRePJzYSd6h6B2/IVboap7/T5YEptN1/gP8TmJTey7cxls0p91H+M HX6ett6VgLAzwKNgzU0wKHJn1HaqfbKPPfur2Pq+ps4OF+QbxHV2fD4sEhn4sA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=lists.sr.ht header.s=20240113 header.b="Yj/79RzT"; dkim=pass header.d=maniero.me header.s=hostingermail1 header.b=e6OWmGxF; dmarc=pass (policy=none) header.from=maniero.me; spf=pass (aspmx1.migadu.com: domain of lists@sr.ht designates 46.23.81.152 as permitted sender) smtp.mailfrom=lists@sr.ht; arc=pass ("mailchannels.net:s=arc-2022:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=johnnyrichard.com; s=key1; t=1730866302; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=5HAkmawU8qX0KzOvlr9y624sej2xjrwHoZieNdYPLL8=; b=OWu9hyoB+WZONbWbEAu5H7j4HqM4qoE4/astUd0yxLwcgyHPuKb/QosJSGtJhP7i6rsPW0 oblrlJpUY1Qn96BFCBLRKiekRWO/aruRp46S6VvUSxt9NU/1CCQ9DUJeEZntprtzIUS9Pr riKhf3ic0YqqaNE/QkeIOqNcpmZGlJppybnoNufA2APXfyuIZBNgZZQIarlEYR46NsNp6j RjPpZRY72uzIG1eP3o1B4NZGXtItz5I5rStd9gu0VdMm6L2La9FIpB0XmTD6+fXgKI0/lf V8ly0UKhJw/ZOBcwEujTxbEE5d2Zvemtgz6gvgveAbS5VDePv68BRH95UzhzXg== Received: from mail-a.sr.ht (mail-a.sr.ht [46.23.81.152]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 6DC991B6D8 for ; Wed, 06 Nov 2024 05:11:39 +0100 (CET) DKIM-Signature: a=rsa-sha256; bh=qJTDSkQgIGoVgvEzAzy7lArEXa+/wlFxNr1/lJQrUIk=; c=simple/simple; d=lists.sr.ht; h=From:To:Cc:Subject:In-Reply-To:References:Date:List-Unsubscribe:List-Subscribe:List-Archive:List-Post:List-ID; q=dns/txt; s=20240113; t=1730866299; v=1; b=Yj/79RzTwZVTXDAYBe236zk14pmOKag1EZDHuRyGm65tqdHszk3Rh5GGrFfB6lhFyfrZi2NT PjdH+3OSkSoOp49kVbF3egh2PQTcpJri4qpd0++IpLGvbB4/lwI6IquvaCcG8G6EpbubZylWbye zp2oMyBeuFaIKtdvfaGKSlf9i6uBT3E4uLJcU78vw52qs2GSakN20b1XIa/p1mKzDj28yb9Mv5Z Sq1+E9N1YgsAhk3ba5jMkq8qN/2Ax5JlHhjKcvkB7kR/tJssHtoyqaQvXFMoNdzJjSogUDu+dj7 EcLAJ1WRGqeiDYzzw6OMk0giufnhVXymlmjx95xVhm7HQ== Received: from lists.sr.ht (unknown [46.23.81.154]) by mail-a.sr.ht (Postfix) with ESMTPSA id 31B9220302 for ; Wed, 06 Nov 2024 04:11:39 +0000 (UTC) Received: from cornsilk.maple.relay.mailchannels.net (cornsilk.maple.relay.mailchannels.net [23.83.214.40]) by mail-a.sr.ht (Postfix) with ESMTPS id 4CFF620257 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 06 Nov 2024 04:11:09 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id CD5E19424CF for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 6 Nov 2024 04:11:07 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (100-103-140-31.trex-nlb.outbound.svc.cluster.local [100.103.140.31]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 3A3EF942334 for <~johnnyrichard/olang-devel@lists.sr.ht>; Wed, 6 Nov 2024 04:11:07 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1730866267; a=rsa-sha256; cv=none; b=JwsWHFthU+FlOThwXCG3Y++0V4DbVm3+PIq4JzILOrHPeervfp0qNmWL3h/IkvkAUKFboO E3R0G5y0GNK4s6mktmWsAybx1+sNSgjZG22ZuILws9SOtyXDF65TijuqLChkGWoYZFQwUL zUaRQJQQdJ1l/BX+9wlPZHUI8wRKWGa65TYX3eXu2A4UBXOzs6YHb6nhbAvEQXOsnV29K7 W/G8S5yhES8AKtPutbWnFx+JLlm+Agn+34xafSl5NcX8q1YIv9tblCpYGlkjPJPC2ifxCn 5tagzvVswH5i/nDCjw7PfjNoYLJwR6E6yPMYy0naTuZDprzHHbKHe3eNHv+zbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1730866267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5HAkmawU8qX0KzOvlr9y624sej2xjrwHoZieNdYPLL8=; b=E2ePeiKumblCQpO+WeeVp1pQavn0phCE+6+Ng8ETpoDRKS4zUOu4IXYRdL1Y9zhShg4g7r 6Q+9w+vedutKxGkacAcKXR4W2cESjk8kyTG5rjzksob+9VR8RKE9q5wIOOF/en3g6wnXpD gRTxtgcv81kRZ00CmhdX6wWvWpKPU3Ts3F6xmhcDur/H4jfdQVK44YaVzysOCKWJa+Ylw7 vHsFZ7uhUIibS2KPuYiTJNs7s+LFsUFALx5Wkcom1ucARJPA6uDcEJ/GBza7v1dICWp7ul roM3QC3LpYA/5vHPr8LD3poAf/rJA4RrwaZsk85Hrs9ULvKnQhkBp15SRImdOg== ARC-Authentication-Results: i=1; rspamd-78c64749d6-bpz4h; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Abortive-Minister: 114601fb78b0ac6f_1730866267732_3911629365 X-MC-Loop-Signature: 1730866267732:2628367602 X-MC-Ingress-Time: 1730866267732 Received: from fr-int-smtpout5.hostinger.io (fr-int-smtpout5.hostinger.io [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.140.31 (trex/7.0.2); Wed, 06 Nov 2024 04:11:07 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1730866265; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5HAkmawU8qX0KzOvlr9y624sej2xjrwHoZieNdYPLL8=; b=e6OWmGxFUqV2UbcSkaujqttNPgv3+WZXj+rReWcUA67J1Bs3I3EDXAO2t7Z9Il8QyBIF+P Ldth0jh7xAtJBIBW0ugrhR1znsaWVYehJVvcF31IGGZK9dffjxT/kmeZaZFzzhWim1bBeI jFTaZJ0xRfvfX324FKD/0iRgNeKcvpTmggzxYzA8IMRISUkTSVuSz+6eGpwBusCPfeNEUX JBjAV0CFpDMusZFSDi5/T+2qdAEh4rnAdM3a59EPdHTZtb1BB2g0Vx9khDz2tbGl19UlLd yomURGhFKB1J8xyLymaGUBxEGwXjSUOPaDwTb5kLuQWIK/u64oRcuDGhEehq6A== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v1 2/2] docs: add the C FFI section Message-ID: <20241106041055.194975-3-carlos@maniero.me> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241106041055.194975-1-carlos@maniero.me> References: <20241106041055.194975-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Wed, 06 Nov 2024 04:11:04 +0000 (UTC) X-CM-Analysis: v=2.4 cv=Z6G+H2RA c=1 sm=1 tr=0 ts=672aec59 a=WwxFCuf3mf1fs3oSi6/dng==:117 a=WwxFCuf3mf1fs3oSi6/dng==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=YGU5J_XgPHVi2RgENsMA:9 a=BXDaF_L80NY05PYiAFlV:22 X-CM-Envelope: MS4xfKdiBQjjm0WRxGJdY5PeSkPOOrOuoqUnDHjqy5LKXW9ePTMV7DNySikcX/DyWb0PEkXEgUNxTe4hu8b2M4I5IXzB9KomrelXr7DoCaRUEmwqtSI/pAKy fOD40UXCzPJgS/vDDaHG/9TzoJNKqFKwieMj77JfNzTI/4YTM0WzjE5bLa3BWVaDkeGvdCqJcU3/2dTm1aqcoLFHBqPoxZKktaaZ6De65QXT5uHTSRSN3FaS X-AuthUser: carlos@maniero.me X-Sourcehut-Patchset-Status: PROPOSED List-Unsubscribe: List-Subscribe: List-Archive: Archived-At: List-Post: List-ID: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel.lists.sr.ht> Sender: ~johnnyrichard/olang-devel <~johnnyrichard/olang-devel@lists.sr.ht> X-Migadu-Flow: FLOW_IN X-Migadu-Country: NL X-Migadu-Spam-Score: -0.71 X-Spam-Score: -0.71 X-Migadu-Queue-Id: 6DC991B6D8 X-Migadu-Scanner: mx12.migadu.com X-TUID: tI0kb8y1DDS2 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 --- 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 + +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 + +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