This commit is contained in:
Scarlett
2025-05-06 15:21:36 -04:00
parent cab30dc2c5
commit 596434e152
6 changed files with 367 additions and 3 deletions

View File

@ -25,6 +25,7 @@ sToInclude=()
cToBuild=()
sToInclude+=("library/alpha_lib_reg.s")
sToInclude+=("library/alpha_driver.s")
sToInclude+=("library/std.s")
filename=""
s_name=""

61
library/alphastdio.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

View File

@ -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

273
library/std.s Normal file
View File

@ -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

View File

@ -1 +1 @@
(* TODO: Prints out fib sequence. *)
(* TODO *)

18
tests/programs/io.alpha Normal file
View File

@ -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;
}