This commit is contained in:
Scarlett
2025-05-04 17:53:17 -04:00
parent 3eddc04079
commit 8c6372fcfd
9 changed files with 345 additions and 1 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@ grammar.tab.h
out
tmp
parser
*.save
*.save
binaries

View File

@ -25,6 +25,7 @@ compiler: clean tmp $(OBJS)
clean:
rm -f $(EXE)
rm -rf out
rm -rf binaries
rm -rf tmp
rm -f *.s
rm -f *.out

View File

61
library/Makefile Normal file
View File

@ -0,0 +1,61 @@
# Makefile to make executables from alpha source code files
# and also make executables from some sample assembly files execising the alpha library
#
# Carl Alphonce
# April 20, 2024
# The alpha compiler and flags (adjust the flags as needed)
AC := ./alpha
AFLAGS := -tok -asc -tc -st -ir -cg
# The preprocessor and flags (you should not adjust these)
CPP := cpp
CPPFLAGS := -P -x c
# Adjust for the library your team is using (register-based or stack-based parameter passing)
ALPHA_LIB = alpha_lib_reg.s ## Register-based parameter passing
#ALPHA_LIB = alpha_lib_st.s ## Stack-based parameter passing
# Adjust for the parameter passing approach your compiler uses:
# alpha_driver_reg.s for register-based parameter passing
# alpha_driver_st.s for stack-based parameter passing
# This file provides a main function that packages up argv[1] (or "") as an alpha string
# (type 1->character) and calls entry with that argument
ALPHA_DRIVER = alpha_driver_reg.s ## Register-based parameter passing
#ALPHA_DRIVER = alpha_driver_st.s ## Stack-based parameter passing
# Create an assembly (.s) file from an alpha source file
# This involves several steps:
%.s : %.alpha
@mv $< $<._temporary_ # 1. rename input file so we can us it as
@$(CPP) $(CPPFLAGS) $<._temporary_ > $< # 2. input to CPP, writing output to original filename
@$(AC) $(AFLAGS) $< # 3. run the alpha compiler on the pre-processed file
@mv $<._temporary_ $< # 4. restore the original input file
# Examples of assembly code using the alpha library files
# In these examples the calling code is in assembly, and defines main (so the driver is not included here)
# Example #1: calling the printBoolean function
printBoolean : printBoolean_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# Example #2: calling the printInt function
printInt : printInt_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# Example #3: calling the reserve and release functions
reserve_release : reserve_release_reg.s
@gcc $< $(ALPHA_LIB) -no-pie -o $@
# The rule for assembling .s files and linking them together (using the gcc compiler driver)
# to produce an executable (assuming no earlier make rule triggers first)
% : %.s $(ALPHA_LIB) $(ALPHA_DRIVER)
@gcc $< $(ALPHA_LIB) $(ALPHA_DRIVER) -no-pie -o $@

81
library/alpha_driver.s Normal file
View File

@ -0,0 +1,81 @@
.file "alpha_driver.c"
.text
.globl main
.type main, @function
main:
.LFB0:
endbr64
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp
movl %edi, -36(%rbp)
movq %rsi, -48(%rbp)
movl $0, -32(%rbp)
cmpl $1, -36(%rbp)
jle .L2
movq -48(%rbp), %rax
addq $8, %rax
movq (%rax), %rax
movq %rax, %rdi
call strlen@PLT
movl %eax, -32(%rbp)
.L2:
movl -32(%rbp), %eax
cltq
addq $4, %rax
movq %rax, %rdi
call malloc@PLT
movq %rax, -16(%rbp)
movq -16(%rbp), %rax
movq %rax, -24(%rbp)
movq -24(%rbp), %rax
movl -32(%rbp), %edx
movl %edx, (%rax)
cmpl $1, -36(%rbp)
jle .L3
addq $4, -24(%rbp)
movq -48(%rbp), %rax
movq 8(%rax), %rax
movq %rax, -8(%rbp)
movl $0, -28(%rbp)
jmp .L4
.L5:
movl -28(%rbp), %eax
movslq %eax, %rdx
movq -8(%rbp), %rax
addq %rdx, %rax
movzbl (%rax), %edx
movq -24(%rbp), %rax
movb %dl, (%rax)
addq $1, -24(%rbp)
addl $1, -28(%rbp)
.L4:
movl -28(%rbp), %eax
cmpl -32(%rbp), %eax
jl .L5
.L3:
movq -16(%rbp), %rax
movq %rax, %rdi
call entry@PLT
leave
ret
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:

115
library/alpha_lib_reg.s Normal file
View File

@ -0,0 +1,115 @@
.file "alpha_lib.c"
.section .rodata
.LC0:
.string "%d"
.text
.globl printInteger
.type printInteger, @function
printInteger:
.LFB2:
pushq %rbp # push old base pointer
movq %rsp, %rbp # move base pointer
subq $16, %rsp # make room on stack
movl %edi, -4(%rbp) # spill arg to stack
movl -4(%rbp), %eax # move argument to %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
.LFE2:
.size printInteger, .-printInteger
.globl printCharacter
.type printCharacter, @function
printCharacter:
.LFB4:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movb %dil, %al
movb %al, -4(%rbp)
movsbl -4(%rbp), %eax
movl %eax, %edi
call putchar
movl $0, %eax
leave
ret
.LFE4:
.size printCharacter, .-printCharacter
.section .rodata
.LC2:
.string "false"
.LC3:
.string "true"
.LC4:
.string "%s"
.text
.globl printBoolean
.type printBoolean, @function
printBoolean:
.LFB5:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movb %dil, -4(%rbp)
cmpb $0, -4(%rbp)
jne .L8
movl $.LC2, %eax
jmp .L9
.L8:
movl $.LC3, %eax
.L9:
movq %rax, %rsi
movl $.LC4, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
.LFE5:
.size printBoolean, .-printBoolean
.globl reserve
.type reserve, @function
reserve:
.LFB8:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
cltq
movq %rax, %rdi
call malloc
leave
ret
.LFE8:
.size reserve, .-reserve
.globl release
.type release, @function
release:
.LFB9:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq %rdi, -8(%rbp)
movq -8(%rbp), %rax
movq %rax, %rdi
call free
movl $0, %eax
leave
ret
.LFE9:
.size release, .-release
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

View File

@ -0,0 +1,26 @@
.file "printBoolean.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movb $1, %dil # the representation of 'true'
call printBoolean
movb $10, %dil
call printCharacter
movb $0, %dil # the representation of 'false'
call printBoolean
movb $10, %dil
call printCharacter
movl $0, %eax
popq %rbp
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

22
library/printInt_reg.s Normal file
View File

@ -0,0 +1,22 @@
.file "printInt.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movl $10, %edi # Move the immediate value 10 to %edi
call printInteger # call the alpha_lib function printInteger
movl $10, %edi # Put the \n character (decimal 10) into %edi
call printCharacter # then call the alpha_lib function printCharacter
movl $0, %eax
popq %rbp
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits

View File

@ -0,0 +1,37 @@
.file "reserve_release.c"
.text
.globl main
.type main, @function
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $8, %edi # move sizeof(*a) into %edi
call reserve # call alpha_lib_reg function reserve
movq %rax, -8(%rbp) # put the returned pointer on the stack at offset -8
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl $20, (%rax) # put value 20 into location pointed at by %rax --> (*a).x
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl $45, 4(%rax) # put value 45 into location 4(%rax) --> (*a).y
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movl 4(%rax), %eax # move (*a).y into %eax
movl %eax, %edi # and then into %edi
call printInteger # call alpha_lib_reg function printInteger
movl $10, %edi # move '\n' into %edi
call printCharacter # call alpha_lib_reg function printCharacter
movq -8(%rbp), %rax # put base pointer of struct (a) into %rax
movq %rax, %rdi # and then into %rdi
call release # call alpha_lib_reg function reserve
movl $0, %eax
leave
ret
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 6.4.0"
.section .note.GNU-stack,"",@progbits