From ac297d36db2fa5bbdffaf21cda842a6c8d9c7129 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Wed, 23 Oct 2019 19:25:16 +0200 Subject: [PATCH] kaem: scaffold/main.kaem. --- .gitignore | 17 ++++--- lib/linux/x86-mes-m2/crt1.M1 | 34 ++++++++++++++ lib/linux/x86-mes-m2/crt1.c | 68 +++++++++++++++++++++++++++ lib/linux/x86-mes-m2/mini.c | 37 +++++++++++++++ lib/m2/eputs.c | 2 - lib/m2/write.c | 21 ++++++++- lib/m2/x86/x86_defs.M1 | 90 ++++++++++++++++++++++++++++++++++++ scaffold/hello.c | 2 +- scaffold/main.c | 7 +-- scaffold/main.kaem | 50 ++++++++++++++++++++ 10 files changed, 309 insertions(+), 19 deletions(-) create mode 100644 lib/linux/x86-mes-m2/crt1.M1 create mode 100644 lib/linux/x86-mes-m2/crt1.c create mode 100644 lib/linux/x86-mes-m2/mini.c create mode 100644 lib/m2/x86/x86_defs.M1 create mode 100644 scaffold/main.kaem diff --git a/.gitignore b/.gitignore index c93b57de..e0b0fdb1 100644 --- a/.gitignore +++ b/.gitignore @@ -52,21 +52,20 @@ !/lib/tests/*/*.exit !/lib/tests/*/*.stdout +/scaffold/*.M1 +/scaffold/*.m2 +/scaffold/*.hex2 /scaffold/argv /scaffold/hello /scaffold/main /scaffold/micro-mes /scaffold/tiny-mes -/scaffold/x86-mes-* -/scaffold/x86_64-mes-* - -/scaffold/tests/x86-mes-* -/scaffold/tests/[0-9a][0-9a-z]-* -/scaffold/tests/t.* -!/scaffold/tests/*.c -!/scaffold/tests/*.exit -!/scaffold/tests/*.stdout +/scaffold/argv-m2 +/scaffold/hello-m2 +/scaffold/main-m2 +/scaffold/micro-mes-m2 +/scaffold/tiny-mes-m2 /src/mes diff --git a/lib/linux/x86-mes-m2/crt1.M1 b/lib/linux/x86-mes-m2/crt1.M1 new file mode 100644 index 00000000..99aa13a2 --- /dev/null +++ b/lib/linux/x86-mes-m2/crt1.M1 @@ -0,0 +1,34 @@ +:_start + push___%ebp + mov____%esp,%ebp + sub____$i32,%esp %0x1054 + mov____$i8,%eax !0 + mov____%eax,0x32 &GLOBAL___stdin + mov____$i8,%eax !1 + mov____%eax,0x32 &GLOBAL___stdout + mov____$i8,%eax !2 + mov____%eax,0x32 &GLOBAL___stderr + mov____%ebp,%eax + add____$i8,%eax !4 + movzbl_(%eax),%eax + add____$i8,%eax !3 + shl____$i8,%eax !0x02 + add____%ebp,%eax + mov____%eax,0x32 &GLOBAL_environ + push___%eax + mov____%ebp,%eax + add____$i8,%eax !8 + push___%eax + mov____%ebp,%eax + add____$i8,%eax !4 + movzbl_(%eax),%eax + push___%eax + call32 %FUNCTION_main + add____$i8,%esp !0x0 + test___%eax,%eax + mov____%eax,%ebx + mov____$i32,%eax %1 + int____$0x80 + hlt + leave + ret diff --git a/lib/linux/x86-mes-m2/crt1.c b/lib/linux/x86-mes-m2/crt1.c new file mode 100644 index 00000000..596e7748 --- /dev/null +++ b/lib/linux/x86-mes-m2/crt1.c @@ -0,0 +1,68 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +int __stdin; +int __stdout; +int __stderr; +char **environ; +int main (int argc, char **argv, char **envp); + +/* FIXME: this is going to be called `FUNCTION__start' +int +_start () +{ + asm ("mov____$i8,%eax !0"); + asm ("mov____%eax,0x32 &GLOBAL___stdin"); + + asm ("mov____$i8,%eax !1"); + asm ("mov____%eax,0x32 &GLOBAL___stdout"); + + asm ("mov____$i8,%eax !2"); + asm ("mov____%eax,0x32 &GLOBAL___stderr"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !4"); + + asm ("movzbl_(%eax),%eax"); + asm ("add____$i8,%eax !3"); + + asm ("shl____$i8,%eax !0x02"); + asm ("add____%ebp,%eax"); + asm ("mov____%eax,0x32 &GLOBAL_environ"); + asm ("push___%eax"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !8"); + asm ("push___%eax"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !4"); + asm ("movzbl_(%eax),%eax"); + asm ("push___%eax"); + main (); + + asm ("mov____%eax,%ebx"); + asm ("mov____$i32,%eax %1"); + asm ("int____$0x80"); + asm ("hlt"); +} +*/ diff --git a/lib/linux/x86-mes-m2/mini.c b/lib/linux/x86-mes-m2/mini.c new file mode 100644 index 00000000..68d999f2 --- /dev/null +++ b/lib/linux/x86-mes-m2/mini.c @@ -0,0 +1,37 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +void +_exit () +{ + asm ("mov____$i32,%eax SYS_exit"); + asm ("mov____0x8(%ebp),%ebx !-4"); + asm ("int____$0x80"); +} + +void +_write (int filedes, void *buffer, int size) +{ + asm ("mov____$i32,%eax SYS_write"); + asm ("mov____0x8(%ebp),%ebx !-4"); + asm ("mov____0x8(%ebp),%ecx !-8"); + asm ("mov____0x8(%ebp),%edx !-12"); + asm ("int____$0x80"); +} diff --git a/lib/m2/eputs.c b/lib/m2/eputs.c index ba89a2e4..4b3a848b 100644 --- a/lib/m2/eputs.c +++ b/lib/m2/eputs.c @@ -18,8 +18,6 @@ * along with GNU Mes. If not, see . */ -int __stderr; - int eputs (char *s) { diff --git a/lib/m2/write.c b/lib/m2/write.c index 4c4df792..a8d59b08 100644 --- a/lib/m2/write.c +++ b/lib/m2/write.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -18,4 +18,21 @@ * along with GNU Mes. If not, see . */ -int write (int fd, char *s, int length); +#include +#include + +int errno; + +int +write (int filedes, void *buffer, int size) +{ + int r = _write (filedes, buffer, size); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} diff --git a/lib/m2/x86/x86_defs.M1 b/lib/m2/x86/x86_defs.M1 new file mode 100644 index 00000000..ef081da6 --- /dev/null +++ b/lib/m2/x86/x86_defs.M1 @@ -0,0 +1,90 @@ +## Copyright (C) 2017 Jeremiah Orians +## This file is part of M2-Planet. +## +## M2-Planet is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## M2-Planet is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with M2-Planet. If not, see . + + +DEFINE ADD_IMMEDIATE_to_eax 81C0 +DEFINE ADD_IMMEDIATE_to_ebp 81C5 +DEFINE ADD_eax_to_ebx 01C3 +DEFINE ADD_ebp_to_eax 01E8 +DEFINE ADD_ebx_to_eax 01D8 +DEFINE AND_eax_ebx 21D8 +DEFINE CALL_IMMEDIATE E8 +DEFINE CALL_eax FFD0 +DEFINE CMP 39C3 +DEFINE COPY_eax_to_ebx 89C3 +DEFINE COPY_eax_to_ecx 89C1 +DEFINE COPY_ebp_to_eax 89E8 +DEFINE COPY_ebx_to_eax 89D8 +DEFINE COPY_ebx_to_edx 89DA +DEFINE COPY_ecx_to_ebp 89CD +DEFINE COPY_edi_to_ebp 89fd +DEFINE COPY_esp_to_ebp 89E5 +DEFINE COPY_esp_to_ecx 89E1 +DEFINE COPY_esp_to_edi 89E7 +DEFINE DIVIDE_eax_by_ebx_into_eax F7FB +DEFINE INT_80 CD80 +DEFINE JUMP E9 +DEFINE JUMP_EQ 0F84 +DEFINE JUMP_NE 0F85 +DEFINE JUMP_EQ8 74 +DEFINE JUMP_NE8 75 +DEFINE LOAD_BASE_ADDRESS_eax 8D85 +DEFINE LOAD_BYTE 0FBE00 +DEFINE LOAD_EFFECTIVE_ADDRESS 8D8424 +DEFINE LOAD_EFFECTIVE_ADDRESS_ebx 8D9C24 +DEFINE LOAD_EFFECTIVE_ADDRESS_ecx 8D8C24 +DEFINE LOAD_EFFECTIVE_ADDRESS_edx 8D9424 +DEFINE LOAD_ESP_IMMEDIATE_into_eax 8B8424 +DEFINE LOAD_IMMEDIATE_eax B8 +DEFINE LOAD_IMMEDIATE_ebx BB +DEFINE LOAD_IMMEDIATE_edx BA +DEFINE LOAD_INTEGER 8B00 +DEFINE LOAD_INTEGER_ebx 8B1B +DEFINE LOAD_INTEGER_ecx 8B09 +DEFINE LOAD_INTEGER_edx 8B12 +DEFINE MODULUS_eax_from_ebx_into_ebx F7FB +DEFINE MOVEZBL 0FB6C0 +DEFINE MOVE_ebx_to_eax 89D8 +DEFINE MOVE_ecx_to_eax 89C8 +DEFINE MOVE_edx_to_eax 89D0 +DEFINE MULTIPLY_eax_by_ebx_into_eax F7E3 +DEFINE NOP 00000000 +DEFINE NOT_eax F7D0 +DEFINE OR_eax_ebx 09D8 +DEFINE POP_eax 58 +DEFINE POP_ebx 5B +DEFINE POP_ebp 5D +DEFINE POP_edi 5F +DEFINE PUSH_eax 50 +DEFINE PUSH_ebx 53 +DEFINE PUSH_ebp 55 +DEFINE PUSH_edi 57 +DEFINE RETURN C3 +DEFINE SAL_eax_Immediate8 C1E0 +DEFINE SAL_eax_cl D3E0 +DEFINE SAR_eax_cl D3F8 +DEFINE SETE 0F94C0 +DEFINE SETLE 0F9EC0 +DEFINE SETL 0F9CC0 +DEFINE SETGE 0F9DC0 +DEFINE SETG 0F9FC0 +DEFINE SETNE 0F95C0 +DEFINE STORE_CHAR 8803 +DEFINE STORE_INTEGER 8903 +DEFINE SUBTRACT_eax_from_ebx_into_ebx 29C3 +DEFINE TEST 85C0 +DEFINE XCHG_eax_ebx 93 +DEFINE XOR_ebx_eax_into_eax 31D8 diff --git a/scaffold/hello.c b/scaffold/hello.c index 23bb416e..43f68925 100644 --- a/scaffold/hello.c +++ b/scaffold/hello.c @@ -21,7 +21,7 @@ #include int -main (int argc, char *argv[]) +main (int argc, char **argv) { eputs ("Hello, Mescc!\n"); return 42; diff --git a/scaffold/main.c b/scaffold/main.c index 376419cc..5c072775 100644 --- a/scaffold/main.c +++ b/scaffold/main.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2016,2017 Jan (janneke) Nieuwenhuizen + * Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -18,11 +18,8 @@ * along with GNU Mes. If not, see . */ -//V=2 CC32=i686-unknown-linux-gnu-gcc build-aux/cc32-mes.sh scaffold/main -//V=2 CC64=gcc build-aux/cc64-mes.sh scaffold/main - int -main (int argc, char *argv[]) +main (int argc, char **argv) { argc = 42; return argc; diff --git a/scaffold/main.kaem b/scaffold/main.kaem new file mode 100644 index 00000000..04d70bcc --- /dev/null +++ b/scaffold/main.kaem @@ -0,0 +1,50 @@ +#!/bin/bash +# Copyright (C) 2019 Jeremiah Orians +# This file is part of Gnu Mes +# +# Gnu Mes is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Gnu Mes is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gnu Mes. If not, see . + +# Usage: +# kaem --verbose --strict --file scaffold/main.kaem +# scaffold/main-m2 + +M2-Planet \ + --debug \ + --architecture x86 \ + -f lib/linux/x86-mes-m2/crt1.c \ + -f scaffold/main.c \ + -o scaffold/main.M1 + +blood-elf -f scaffold/main.M1 -o scaffold/main.blood-elf-M1 + +M1 \ + --LittleEndian \ + --architecture x86 \ + -f lib/m2/x86/x86_defs.M1 \ + -f lib/x86-mes/x86.M1 \ + -f lib/linux/x86-mes-m2/crt1.M1 \ + -f scaffold/main.M1 \ + -f scaffold/main.blood-elf-M1 \ + -o scaffold/main.hex2 + +hex2 \ + --LittleEndian \ + --architecture x86 \ + --BaseAddress 0x1000000 \ + --exec_enable \ + -f lib/x86-mes/elf32-header.hex2 \ + -f scaffold/main.hex2 \ + -o scaffold/main-m2 + +echo Now run: scaffold/main-m2