restructure fs

This commit is contained in:
nub31
2025-05-03 16:55:06 +02:00
parent bfc3aad516
commit 3b142b2453
75 changed files with 38 additions and 387 deletions

4
.gitignore vendored
View File

@@ -33,6 +33,4 @@ Thumbs.db
Desktop.ini Desktop.ini
.DS_Store .DS_Store
output/*.o out
output/out
output/out.asm

View File

@@ -1,9 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

15
build.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/bin/sh
mkdir -p out
dotnet run --project lang/Nub.Lang example out/out.asm
gcc -c -g -fno-stack-protector -fno-builtin std/baseline/gc.c -o out/gc.o
nasm -g -felf64 std/baseline/str_cmp.asm -o out/str_cmp.o
nasm -g -felf64 std/core/str_len.asm -o out/str_len.o
nasm -g -felf64 std/core/arr_size.asm -o out/arr_size.o
nasm -g -felf64 std/core/itoa.asm -o out/itoa.o
nasm -g -felf64 out/out.asm -o out/out.o
gcc -no-pie -nostartfiles -o out/program out/gc.o out/str_cmp.o out/str_len.o out/arr_size.o out/itoa.o out/out.o

2
clean.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
rm -rf out

4
debug.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
./clean.sh
./build.sh
gdb -tui ./out/program

View File

@@ -1,311 +0,0 @@
;*********************************************************************
; util.asm
; Version: 1.2
; Author: mjbrusso
; Contributors: AlessandroFonseca
; Licensed under the MIT license (see "license.txt").
;*********************************************************************
global exit, exit0, strlen, atoi, endl, printstr, printint, readstr, readint
SYS_READ: equ 0
SYS_WRITE: equ 1
SYS_EXIT: equ 60
STDIN: equ 0
STDOUT: equ 1
LINEFEED: equ 0x0A
section .text
;*********************************************************************
; void exit(int64 code)
;
; Description:
; Quit program
;
; Arguments:
; rdi: int64 code: Exit code (0=Success, >0=Error)
;
; Returns:
; This function does not return
;
;*********************************************************************
exit:
mov rax, SYS_EXIT ; rax: system call number
syscall
;*********************************************************************
;*********************************************************************
; void exit0()
;
; Description:
; Quit program with status code = 0
;
; Arguments:
; None
;
; Returns:
; This function does not return
;
;*********************************************************************
exit0:
xor rdi, rdi ; rdi = 0
jmp exit ; TCO: tail call optimization
;*********************************************************************
;*********************************************************************
; int64 strlen(char *s)
;
; Description:
; Calculates the length of string ( excluding the terminating null)
;
; Arguments:
; rdi: char *s: address of a null-terminated string (array of chars terminated by 0)
;
; Returns:
; rax: int64: string size
;
;*********************************************************************
strlen:
xor rax, rax ; rax=0; // reset counter
.loop: ; do{
cmp byte [rdi], 0 ; if (*s==0); // If zero, skip loop
je strlen.end ; break;
inc rax ; rax++; // increment counter
inc rdi ; s++; // advance to the next char
jmp strlen.loop ; }while(true);
.end:
ret ; return rax;
;*********************************************************************
;*********************************************************************
; void itoa(int64 value, char *s)
;
; Description:
; Converts an integer to a null-terminated string.
;
; Arguments:
; rdi: int64 value: Integer value to convert.
; rsi: char *s: Memory address where to store the resulting string.
;
; Returns:
; rax: int64: string size
;
;*********************************************************************
itoa:
test rdi, rdi ; value = rdi
jz itoa.iszero ; value==0 has a direct solution
jns itoa.notneg ; if(value <0 )
mov byte [rsi], '-' ; *s = '-'
neg rdi ; value = -value
inc rsi ; s++
.notneg:
mov r9b, 1 ; bool leftzero=true
mov r10, 10 ; base = 10
mov rcx, 1000000000000000000 ; divisor = 1000000000000000000
mov r8, 19 ; cont = 19 // Will repeat 19 times
.loop: ; do{
mov rax, rdi ; dividend[0..31] = value
xor rdx, rdx ; dividend[32..63] = 0
idiv rcx ; rax=(rdx:rax)/rcx ; rdx=(rdx:rax)%rcx
test al, al ; digit = rax[0..7]
jnz itoa.notdigit0 ; if(digit!=0)
test r9b, r9b ; if(leftzero)
jnz itoa.nextdigit ; continue
jmp itoa.digit0
.notdigit0:
xor r9b, r9b ; leftzero = false
.digit0:
add eax, 48 ; digit = '0' + digit
mov rdi, rdx ; value %= divisor
mov byte [rsi], al ; *p = digit
inc rsi ; p++
.nextdigit:
mov rax, rcx ; dividend[0..31] = value
xor rdx, rdx ; dividend[32..63] = 0
idiv r10 ; rax=(rdx:rax)/10 ; rdx=(rdx:rax)%10
mov rcx, rax ; divisor /= 10
dec r8 ; cont--
jne itoa.loop ; }while(cont!=0)
.end:
mov byte [rsi], 0 ; *p = '\0'
ret
.iszero:
mov word [rsi], 0x0030 ; *p = "0" (x86 is little endian)
ret
;*********************************************************************
;*********************************************************************
; int64 atoi(char *s)
;
; Description:
; Convert string to integer.
;
; Arguments:
; rdi: char *s: Address of a null-terminated string (array of chars terminated by 0)
;
; Returns:
; rax: int64: integer value
;*********************************************************************
atoi:
push r12 ; r12 is callee saved
mov r12, rdi ; rdi is caller saved
call strlen
lea rdi, [r12+rax-1] ; char *p = &s[strlen(string)]; //scans string backward
xor rax, rax ; result value
mov rdx, 1 ; multiplier
.beginloop:
cmp rdi, r12 ; while(p>=s){
jl atoi.end ;
xor rcx, rcx ;
mov cl, byte [rdi] ; cl = current char
cmp cl, '-' ; if(cl=='-')
jne atoi.notneg ;
neg rax ; rax=-rax
jmp atoi.end ;
.notneg:
cmp cl, '9' ; if(!isdigit(cl)) nextdigit
jg atoi.endloop ;
sub cl, '0' ;
jl atoi.endloop ;
imul rcx, rdx ; digit_value = current_char * multiplier
add rax, rcx ; result += digit_value
imul rdx, 10 ; multiplier *= 10
.endloop:
dec rdi ; previous char //scans string backward
jmp atoi.beginloop ; }
.end:
pop r12 ; restore r12
ret
;*********************************************************************
;*********************************************************************
; void endl()
;
; Description:
; Prints a newline (line break)
;
; Arguments:
; None
;
; Returns:
; Nothing
;
;*********************************************************************
endl:
lea rdi, [endl.str] ; print the string
call printstr
ret
;*********************************************************************
;*********************************************************************
; void printstr(char *s)
;
; Description:
; Print a string
;
; Arguments:
; rdi: char *s: address of a null-terminated string (array of chars terminated by 0)
;
; Returns:
; Nothing
;
;*********************************************************************
printstr:
push r15 ; r15 is callee saved
mov r15, rdi ; save copy (rdi should be caller saved)
call strlen
mov rdx, rax ; string size
mov rsi, r15 ; string
mov rax, SYS_WRITE ; system call number
mov rdi, STDOUT ; file descriptor
syscall ; system call
pop r15
ret
;*********************************************************************
;*********************************************************************
; void printint(int64 n)
;
; Description:
; Print integer number (decimal)
;
; Arguments:
; rdi: int64 n: Value to print
;
; Returns:
; Nothing
;
;*********************************************************************
printint:
sub rsp, 40 ; stack allocate a temp string
mov rsi, rsp ; rdi=value, rsi=&str[0]
call itoa
mov rdi, rsp ; rdi=&str[0]
call printstr ; print number
add rsp, 40 ; deallocate the string
ret
;*********************************************************************
;*********************************************************************
; int64 readstr(char *s, int64 maxsize)
;
; Description:
; Read up to *maxsize* chars from standard input into a string.
;
; Arguments:
; rdi: char *s: address of a string (array of chars)
; rsi: int64 maxsize: input size limit
;
; Returns:
; rax: int64: Number of characters read
;
;*********************************************************************
readstr:
mov r8, rdi ; copy of buffer address
mov rax, SYS_READ ; system call number
mov rdx, rsi ; pointer to buffer
mov rsi, rdi ; max size
mov rdi, STDIN ; file descriptor
syscall ; system call
dec rax ; removing trailing newline char
mov byte [r8+rax], 0 ; replace with '\0'
ret
;*********************************************************************
;*********************************************************************
; int64 readint()
;
; Description:
; Read int64 from standard input
;
; Arguments:
; None
;
; Returns:
; rax: int64: The value entered
;
;*********************************************************************
readint:
sub rsp, 40 ; char s[40]
mov rdi, rsp ; rdi = &s[0]
mov rsi, 21 ; max input size
call readstr ; read number as string
mov rdi, rsp ;
call atoi ; rax = atoi(s)
add rsp, 40 ; deallocate s from stack
ret
;*********************************************************************
section .data
endl.str: db LINEFEED, 0

1
lang/.idea/.idea.Nub.Lang/.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
Nub.Lang

View File

@@ -2,8 +2,7 @@
<project version="4"> <project version="4">
<component name="UserContentModel"> <component name="UserContentModel">
<attachedFolders> <attachedFolders>
<Path>../input</Path> <Path>../std</Path>
<Path>../output</Path>
</attachedFolders> </attachedFolders>
<explicitIncludes /> <explicitIncludes />
<explicitExcludes /> <explicitExcludes />

View File

@@ -2,8 +2,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nub.Lang", "Nub.Lang\Nub.Lang.csproj", "{5047E21F-590D-4CB3-AFF3-064316485009}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nub.Lang", "Nub.Lang\Nub.Lang.csproj", "{5047E21F-590D-4CB3-AFF3-064316485009}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nub.Core", "Nub.Core\Nub.Core.csproj", "{903F2D49-4F69-4287-A709-EFC68BDD9654}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -14,9 +12,5 @@ Global
{5047E21F-590D-4CB3-AFF3-064316485009}.Debug|Any CPU.Build.0 = Debug|Any CPU {5047E21F-590D-4CB3-AFF3-064316485009}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5047E21F-590D-4CB3-AFF3-064316485009}.Release|Any CPU.ActiveCfg = Release|Any CPU {5047E21F-590D-4CB3-AFF3-064316485009}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5047E21F-590D-4CB3-AFF3-064316485009}.Release|Any CPU.Build.0 = Release|Any CPU {5047E21F-590D-4CB3-AFF3-064316485009}.Release|Any CPU.Build.0 = Release|Any CPU
{903F2D49-4F69-4287-A709-EFC68BDD9654}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{903F2D49-4F69-4287-A709-EFC68BDD9654}.Debug|Any CPU.Build.0 = Debug|Any CPU
{903F2D49-4F69-4287-A709-EFC68BDD9654}.Release|Any CPU.ActiveCfg = Release|Any CPU
{903F2D49-4F69-4287-A709-EFC68BDD9654}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -1,5 +1,4 @@
using Nub.Core; using Nub.Lang.Frontend.Parsing;
using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Backend.Custom; namespace Nub.Lang.Backend.Custom;

View File

@@ -1,6 +1,4 @@
using Nub.Core; namespace Nub.Lang.Frontend.Lexing;
namespace Nub.Lang.Frontend.Lexing;
public class Lexer public class Lexer
{ {

View File

@@ -1,6 +1,4 @@
using Nub.Core; namespace Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Frontend.Parsing;
public class ExternFuncDefinitionNode(string name, List<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode public class ExternFuncDefinitionNode(string name, List<FuncParameter> parameters, Optional<Type> returnType) : DefinitionNode
{ {

View File

@@ -1,6 +1,4 @@
using Nub.Core; namespace Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Frontend.Parsing;
public class IfNode(ExpressionNode condition, BlockNode body, Optional<Variant<IfNode, BlockNode>> @else) : StatementNode public class IfNode(ExpressionNode condition, BlockNode body, Optional<Variant<IfNode, BlockNode>> @else) : StatementNode
{ {

View File

@@ -1,6 +1,4 @@
using Nub.Core; namespace Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Frontend.Parsing;
public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<Type> returnType) : DefinitionNode public class LocalFuncDefinitionNode(string name, List<FuncParameter> parameters, BlockNode body, Optional<Type> returnType) : DefinitionNode
{ {

View File

@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Nub.Core;
using Nub.Lang.Frontend.Lexing; using Nub.Lang.Frontend.Lexing;
namespace Nub.Lang.Frontend.Parsing; namespace Nub.Lang.Frontend.Parsing;

View File

@@ -1,6 +1,4 @@
using Nub.Core; namespace Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Frontend.Parsing;
public class ReturnNode(Optional<ExpressionNode> value) : StatementNode public class ReturnNode(Optional<ExpressionNode> value) : StatementNode
{ {

View File

@@ -1,5 +1,4 @@
using Nub.Core; using Nub.Lang.Frontend.Parsing;
using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang.Frontend.Typing; namespace Nub.Lang.Frontend.Typing;

View File

@@ -7,8 +7,4 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Nub.Core\Nub.Core.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -1,6 +1,6 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Nub.Core; namespace Nub.Lang;
public readonly struct Optional public readonly struct Optional
{ {

View File

@@ -19,8 +19,8 @@ internal static class Program
return 1; return 1;
} }
var input = args[0]; var input = Path.GetFullPath(args[0]);
var output = args[1]; var output = Path.GetFullPath(args[1]);
if (!Directory.Exists(input)) if (!Directory.Exists(input))
{ {

View File

@@ -1,5 +1,4 @@
using Nub.Core; using Nub.Lang.Frontend.Parsing;
using Nub.Lang.Frontend.Parsing;
namespace Nub.Lang; namespace Nub.Lang;

View File

@@ -1,4 +1,4 @@
namespace Nub.Core; namespace Nub.Lang;
public readonly struct Variant<T1, T2> where T1 : notnull where T2 : notnull public readonly struct Variant<T1, T2> where T1 : notnull where T2 : notnull
{ {

View File

@@ -1,11 +0,0 @@
#!/bin/sh
gcc -c -g -O2 -fno-stack-protector -fno-builtin ../input/baseline/gc.c -o gc.o
nasm -g -felf64 ../input/baseline/str_cmp.asm -o str_cmp.o
nasm -g -felf64 ../input/core/str_len.asm -o str_len.o
nasm -g -felf64 ../input/core/arr_size.asm -o arr_size.o
nasm -g -felf64 ../input/core/itoa.asm -o itoa.o
nasm -g -felf64 out.asm -o out.o
gcc -no-pie -nostartfiles -o out gc.o str_cmp.o str_len.o arr_size.o itoa.o out.o

View File

@@ -1,8 +0,0 @@
#!/bin/sh
rm ./arr_size.o
rm ./gc.o
rm ./itoa.o
rm ./out.o
rm ./str_cmp.o
rm ./str_len.o
rm out

View File

@@ -1,3 +0,0 @@
#!/bin/sh
./build.sh
gdb -tui out

View File

@@ -1,3 +0,0 @@
#!/bin/sh
./build.sh
valgrind -s ./out

View File

@@ -1,4 +1,5 @@
#!/bin/sh #!/bin/sh
./clean.sh
./build.sh ./build.sh
./out ./out/program
echo "Process exited with status code $?" echo "Process exited with status code $?"