juice500ml / cminus

C- Compiler Project for Sogang Univ. (17' Spring)
5 stars 1 forks source link

SPIM Implementations #20

Closed juice500ml closed 7 years ago

juice500ml commented 7 years ago

If

if(test_expr) {
  true_stmt;
}
else {
  false_stmt;
}
test_expr               ; returns to Rsrc
beqz Rsrc, L_false      ; if zero, goto L_false
true_stmt               ; fallthrough
J L_exit                ; exit if-else stmt
L_false:
false_stmt
L_exit:

While

while(test_expr) {
  stmt;
}
J L_cmp      ; jump only first time
L_loop:
stmt;
L_cmp:
test_expr     ; returns to Rsrc
bnez Rsrc, L_loop

Function call

f(arr, var);  // array: arr, variable: var
# 1. Parameter pushing: 뒤에서부터 차례대로 sp-4, sp-8, sp-12... 
# var: 원본 값을 푸쉬

System call

i = input(); // integer input
output(i);   // integer output
.data
newline: .asciiz "\n"
.text

; input
li $v0, 5 ; read_int
syscall   ; returns value as int

; output
li $a0, $v0
li $v0, 1 ; print_int
syscall
li $v0, 4 ; print_str
la $a0, newline
syscall

Misc.

main 함수 explicit하게 불러주어야 하나?

.globl main # Is this necessary?
.text
; Every code ...
juice500ml commented 7 years ago

https://courses.cs.washington.edu/courses/cse410/09sp/examples/MIPSCallingConventionsSummary.pdf 위 convention을 따라가는 중. 단, function 내의 local decl을 먼저 alloc해주지는 않았음. 이 외에는 모두 동일. 그런데 caller이랑 callee에서 어떻게 파라미터 넘겨줄지 아직 fix가 안됐음...

juice500ml commented 7 years ago

0ccf615 에서 fix 시킴. callee단의 stack에 쌓이고 caller은 fp+0, fp+4, ... 으로 접근.

taeguk commented 7 years ago

@juice500ml @taeseunglee Project 4 구현 끝난듯. boss.c 까지 통과 확인 완료.