From d5f3944b1a5f1dea9d33f12e1ba82167d263c7b4 Mon Sep 17 00:00:00 2001 From: veclav talica Date: Thu, 21 Sep 2023 11:43:12 +0500 Subject: [PATCH] cosmopolitan build --- .gitignore | 6 ++++++ .gitmodules | 0 build-cosmo.sh | 5 +++++ extern/cosmopolitan/get.sh | 4 ++++ extern/get.sh | 4 ++++ prepare.sh | 7 +++++++ src/cosmopolitan/.getkeep | 0 src/main.zig | 19 +++++++++++++++---- 8 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100755 build-cosmo.sh create mode 100755 extern/cosmopolitan/get.sh create mode 100755 extern/get.sh create mode 100755 prepare.sh create mode 100644 src/cosmopolitan/.getkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcf03b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +**/*.o +**/*.a +**/*.lds +**/*.h +zig-out/ +zig-cache/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e69de29 diff --git a/build-cosmo.sh b/build-cosmo.sh new file mode 100755 index 0000000..50d2c11 --- /dev/null +++ b/build-cosmo.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +zig build-obj ./src/main.zig -fno-PIE -fno-PIC -fno-stack-check +ld.bfd main.o -o zig-out/bin/nmvm.com.dbg -T extern/cosmopolitan/ape.lds extern/cosmopolitan/crt.o extern/cosmopolitan/ape-copy-self.o \ + extern/cosmopolitan/cosmopolitan.a -z common-page-size=0x1000 -z max-page-size=0x1000 --gc-sections diff --git a/extern/cosmopolitan/get.sh b/extern/cosmopolitan/get.sh new file mode 100755 index 0000000..dc4ee9a --- /dev/null +++ b/extern/cosmopolitan/get.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +wget wget https://justine.lol/cosmopolitan/cosmopolitan.tar.gz +unzip cosmopolitan.tar.gz diff --git a/extern/get.sh b/extern/get.sh new file mode 100755 index 0000000..055cca6 --- /dev/null +++ b/extern/get.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cd ./cosmopolitan +./get.sh diff --git a/prepare.sh b/prepare.sh new file mode 100755 index 0000000..a750c45 --- /dev/null +++ b/prepare.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +cd ./extern +./get.sh + +cd ../ +zig translate-c ./extern/cosmopolitan/cosmopolitan.h > ./src/cosmopolitan/cosmopolitan.zig diff --git a/src/cosmopolitan/.getkeep b/src/cosmopolitan/.getkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main.zig b/src/main.zig index 2443fee..cc53c90 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,20 +1,29 @@ -const std = @import("std"); const tolmac = @import("tolmac.zig"); +const cosmo = @import("cosmopolitan/cosmopolitan.zig"); fn printInt3(int: u64, other: u32, another: u16) callconv(.SysV) void { @setAlignStack(16); - std.debug.print("test: {}, {}, {}\n", .{ int, other, another }); + _ = cosmo.printf("%u\n", @as(c_uint, @intCast(int + other + another))); } fn printInt2(int: u64, other: u8) callconv(.SysV) void { @setAlignStack(16); - std.debug.print("test: {}, {}\n", .{ int, other }); + _ = cosmo.printf("%u\n", @as(c_uint, @intCast(int + other))); } const opPrintInt3Zov = tolmac.generateOpZovSysvFromPrototype(printInt3) catch unreachable; const opPrintInt2Zov = tolmac.generateOpZovSysvFromPrototype(printInt2) catch unreachable; -pub fn main() !void { +comptime { + @export(cosmopolitanMain, .{ .name = "main" }); +} + +// todo: No cosmopolitan main. + +export fn cosmopolitanMain(argc: c_int, argv: [*][*:0]u8) c_int { + _ = argc; + _ = argv; + const add = [_]tolmac.Word{ @as(tolmac.Word, @intFromPtr(&tolmac.opSumWordsWithOverflow)), @as(tolmac.Word, @intFromPtr(&tolmac.opReturn)), @@ -43,6 +52,8 @@ pub fn main() !void { }; tolmac.execute(&entry, 0); + + return 0; } test {