diff --git a/genx.sh b/genx.sh index 5bbb6f9..7025bd0 100755 --- a/genx.sh +++ b/genx.sh @@ -25,6 +25,7 @@ sToInclude=() cToBuild=() sToInclude+=("library/alpha_lib_reg.s") sToInclude+=("library/alpha_driver.s") +sToInclude+=("library/std.s") filename="" s_name="" diff --git a/library/alphastdio.c b/library/alphastdio.c new file mode 100644 index 0000000..f4da2ce --- /dev/null +++ b/library/alphastdio.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include + + + +int printS(char *str) { + if (str == NULL) return -1; + printf("%s", str); + return 0; +} + +char *inS() { + char *buffer = malloc(100); + if (buffer == NULL) return NULL; + if (fgets(buffer, 100, stdin) == NULL) { + free(buffer); + return NULL; + } + buffer[strcspn(buffer, "\n")] = 0; + return buffer; +} + + + +int printI(int i) { + printf("%d", i); + return 0; +} + +int inI() { + int i; + char buffer[100]; + if (fgets(buffer, sizeof(buffer), stdin) == NULL) return 0; + if (sscanf(buffer, "%d", &i) != 1) return 0; + return i; +} + + + +int printC(char c) { + printf("%c", c); + return 0; +} + +char inC() { + char c; + if (scanf(" %c", &c) != 1) return 0; + return c; +} + + + +int printB(bool b) { + if (b) + printf("true"); + else + printf("false"); + return 0; +} \ No newline at end of file diff --git a/library/std.alpha b/library/std.alpha index a2fc025..2d0f1a9 100644 --- a/library/std.alpha +++ b/library/std.alpha @@ -1,4 +1,4 @@ -(* Standard Alpha Library - Provided by Carl *) +(* Standard Alpha Library *) type string: 1 -> character type BooleanXBoolean: [Boolean: x; Boolean: y] @@ -16,9 +16,20 @@ type characterXcharacter2Boolean: characterXcharacter -> Boolean type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean type integer2address: integer -> address type address2integer: address -> integer +type integer2string: integer -> string +type integer2character: integer -> character +type integer2Boolean: integer -> Boolean + external function printInteger: integer2integer external function printCharacter: character2integer external function printBoolean: Boolean2integer -function entry: string2integer +external function printS: string2integer +external function printI: integer2integer +external function printC: character2integer +external function printB: Boolean2integer + +external function inS: integer2string +external function inI: integer2integer +external function inC: integer2character \ No newline at end of file diff --git a/library/std.s b/library/std.s new file mode 100644 index 0000000..6cdcb90 --- /dev/null +++ b/library/std.s @@ -0,0 +1,273 @@ + .file "alphastdio.c" + .text + .section .rodata +.LC0: + .string "%s" + .text + .globl printS + .type printS, @function +printS: +.LFB6: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movq %rdi, -8(%rbp) + cmpq $0, -8(%rbp) + jne .L2 + movl $-1, %eax + jmp .L3 +.L2: + movq -8(%rbp), %rax + movq %rax, %rsi + leaq .LC0(%rip), %rax + movq %rax, %rdi + movl $0, %eax + call printf@PLT + movl $0, %eax +.L3: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE6: + .size printS, .-printS + .section .rodata +.LC1: + .string "\n" + .text + .globl inS + .type inS, @function +inS: +.LFB7: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl $100, %edi + call malloc@PLT + movq %rax, -8(%rbp) + cmpq $0, -8(%rbp) + jne .L5 + movl $0, %eax + jmp .L6 +.L5: + movq stdin(%rip), %rdx + movq -8(%rbp), %rax + movl $100, %esi + movq %rax, %rdi + call fgets@PLT + testq %rax, %rax + jne .L7 + movq -8(%rbp), %rax + movq %rax, %rdi + call free@PLT + movl $0, %eax + jmp .L6 +.L7: + movq -8(%rbp), %rax + leaq .LC1(%rip), %rdx + movq %rdx, %rsi + movq %rax, %rdi + call strcspn@PLT + movq -8(%rbp), %rdx + addq %rdx, %rax + movb $0, (%rax) + movq -8(%rbp), %rax +.L6: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE7: + .size inS, .-inS + .section .rodata +.LC2: + .string "%d" + .text + .globl printI + .type printI, @function +printI: +.LFB8: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl %edi, -4(%rbp) + movl -4(%rbp), %eax + movl %eax, %esi + leaq .LC2(%rip), %rax + movq %rax, %rdi + movl $0, %eax + call printf@PLT + movl $0, %eax + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE8: + .size printI, .-printI + .globl inI + .type inI, @function +inI: +.LFB9: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + addq $-128, %rsp + movq %fs:40, %rax + movq %rax, -8(%rbp) + xorl %eax, %eax + movq stdin(%rip), %rdx + leaq -112(%rbp), %rax + movl $100, %esi + movq %rax, %rdi + call fgets@PLT + testq %rax, %rax + jne .L11 + movl $0, %eax + jmp .L14 +.L11: + leaq -116(%rbp), %rdx + leaq -112(%rbp), %rax + leaq .LC2(%rip), %rcx + movq %rcx, %rsi + movq %rax, %rdi + movl $0, %eax + call __isoc99_sscanf@PLT + cmpl $1, %eax + je .L13 + movl $0, %eax + jmp .L14 +.L13: + movl -116(%rbp), %eax +.L14: + movq -8(%rbp), %rdx + subq %fs:40, %rdx + je .L15 + call __stack_chk_fail@PLT +.L15: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE9: + .size inI, .-inI + .globl printC + .type printC, @function +printC: +.LFB10: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl %edi, %eax + movb %al, -4(%rbp) + movsbl -4(%rbp), %eax + movl %eax, %edi + call putchar@PLT + movl $0, %eax + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE10: + .size printC, .-printC + .section .rodata +.LC3: + .string " %c" + .text + .globl inC + .type inC, @function +inC: +.LFB11: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movq %fs:40, %rax + movq %rax, -8(%rbp) + xorl %eax, %eax + leaq -9(%rbp), %rax + movq %rax, %rsi + leaq .LC3(%rip), %rax + movq %rax, %rdi + movl $0, %eax + call __isoc99_scanf@PLT + cmpl $1, %eax + je .L19 + movl $0, %eax + jmp .L21 +.L19: + movzbl -9(%rbp), %eax +.L21: + movq -8(%rbp), %rdx + subq %fs:40, %rdx + je .L22 + call __stack_chk_fail@PLT +.L22: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE11: + .size inC, .-inC + .section .rodata +.LC4: + .string "true" +.LC5: + .string "false" + .text + .globl printB + .type printB, @function +printB: +.LFB12: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl %edi, %eax + movb %al, -4(%rbp) + cmpb $0, -4(%rbp) + je .L24 + leaq .LC4(%rip), %rax + movq %rax, %rdi + movl $0, %eax + call printf@PLT + jmp .L25 +.L24: + leaq .LC5(%rip), %rax + movq %rax, %rdi + movl $0, %eax + call printf@PLT +.L25: + movl $0, %eax + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE12: + .size printB, .-printB + .ident "GCC: (GNU) 14.2.1 20250207" + .section .note.GNU-stack,"",@progbits diff --git a/tests/programs/fib.alpha b/tests/programs/fib.alpha index 2f33e06..b136295 100644 --- a/tests/programs/fib.alpha +++ b/tests/programs/fib.alpha @@ -1 +1 @@ -(* TODO: Prints out fib sequence. *) \ No newline at end of file +(* TODO *) \ No newline at end of file diff --git a/tests/programs/io.alpha b/tests/programs/io.alpha new file mode 100644 index 0000000..eac7b03 --- /dev/null +++ b/tests/programs/io.alpha @@ -0,0 +1,18 @@ +#include "std.alpha" + +function entry: string2integer + +entry (arg) := { + [integer: x; string: s; character: c; integer: result] + + s := inS(1); + result := printS(s); + + x := inI(1); + result := printI(x); + + c := inC(1); + result := printC(c); + + return 0; +} \ No newline at end of file