Created
December 13, 2012 20:10
-
-
Save joseanpg/4279382 to your computer and use it in GitHub Desktop.
Understanding coolc : init-order-super.cl
Calling self.recite( value:Int) where self :Basic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FrameBot del caller en fp | |
# self del caller en s0 | |
# ReturAddress en ra | |
# Argumento en sp + 1 | |
# Mediante prima nos referiremos a los valores del callee | |
Base.recite: | |
#Preparamos el Frame | |
addiu $sp $sp -12 # Dejamos sitio para el frame: sp' = sp - 3 | |
# FrameTop' = sp = sp' + 3 | |
# FrameBot' = sp - 2 = sp' + 1 ( sp = FrameBot' + 2 ) | |
sw $fp 12($sp) # [FrameBot' + 2] <- FrameBot , es decir, [sp+3] <- FrameBot | |
sw $s0 8($sp) # [FrameBot' + 1] <- self , es decir, [sp+2] <- self | |
sw $ra 4($sp) # [FrameBot] <- ra , es decir, [sp+1] <- ra | |
addiu $fp $sp 4 # fp' = FrameBot' | |
# Resumiendo | |
# [fp'] = ReturnAddress | |
# [fp'+1] = Caller's self | |
# [fp'+2] = Caller's fp | |
# Además | |
# [fp'+3] = Argumen | |
move $s0 $a0 # self' viene en a0 | |
# el encargado de custodiarlo es s0: s0 <- a0 | |
# Copiamos el argumento en el stack, ya que será también el argumento | |
# de la siguiente llamada | |
lw $a0 12($fp) # El caller depositó el argumento en el stack | |
# es decir, en sp + 1 | |
# como sp = FrameBot + 2 el argumento está en FrameBot + 3 | |
# a0 <- [fp' + 3] | |
sw $a0 0($sp) # push! | |
addiu $sp $sp -4 | |
# Llamada a self.out_int | |
move $a0 $s0 # El self'' debe pasarse en a0 | |
# Además self'' = self ' = s0 | |
# Excepción si self'' es void | |
bne $a0 $zero label5 | |
la $a0 str_const1 # a0 lleva el filename | |
li $t1 1 # t1 ¿qué lleva? | |
jal _dispatch_abort | |
label5: | |
lw $t1 8($a0) # t1 <- [self+2], es decir, t1 <- Base_dispTab ( 8(self) es el dispTab SIEMPRE ) | |
lw $t1 16($t1) # t1 <- [Base_dispTab+4], es decir, t1 <- IO.out_int | |
jalr $t1 # call IO.out_int | |
# el argumento está en sp + 1 | |
# self'' está en a0 | |
# la dirección de retorno está en ra | |
# El valor de retorno quedaría en a0. No es utilizado. | |
# Llamada a self.out_string | |
la $a0 str_const0 # El argumento será '\n' | |
sw $a0 0($sp) # Lo colocamos en el stack | |
addiu $sp $sp -4 | |
move $a0 $s0 # self'' debe pasarse en a0 | |
# Además self'' = self ' = s0 | |
# Excepción si self'' es void | |
bne $a0 $zero label6 | |
la $a0 str_const1 # a0 lleva el filename | |
li $t1 1 # t1 ? | |
jal _dispatch_abort | |
label6: | |
lw $t1 8($a0) # t1 <- [self+2], es decir, t1 <- Base_dispTab ( 8(self) es el dispTab SIEMPRE ) | |
lw $t1 12($t1) # t1 <- [Base_dispTab+3], es decir, t1 <- IO.out_string | |
jalr $t1 # call IO.out_int | |
# el argumento está en sp + 1 | |
# self'' está en a0 | |
# la dirección de retorno está en ra | |
# El valor de retorno quedaría en a0. No es utilizado. | |
# RETURN | |
move $a0 $s0 # El método devuelverá self'. El valor devuelto debe ir en a0 | |
# Restauramos fp (FrameBot), s0 (self), ra (return address) | |
lw $fp 12($sp) # Recordemos que [sp+3] = FrameBot, entonces fp <- [sp+3] | |
lw $s0 8($sp) # Recordemos que [sp+2] = self , entonces self <- [sp+2] | |
lw $ra 4($sp) # Recordemos que [sp+1] = ra , entonces ra <- [sp+2] | |
# Recordemos que sp' = sp - 3, por tanto sp = sp' + 3 | |
addiu $sp $sp 16 # Además el argumento estaría en sp + 1 y ya no es necesario: sp <- sp' + 4 | |
jr $ra # RETURN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initBook(title_p : String, author_p : String) : Book { | |
{ | |
title <- title_p; | |
author <- author_p; | |
self; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FrameBot = fp | |
# self = s0 | |
# ReturnAddress = ra | |
# Argument title_p = [sp + 2] | |
# Argument author_p = [sp + 1] | |
Book.initBook: | |
addiu $sp $sp -12 # sp' = sp - 3, sp = sp'+3 | |
sw $fp 12($sp) # fp -> [sp' + 3] = [FrameBot'+2] | |
sw $s0 8($sp) # s0 -> [sp' + 2] = [FrameBot'+1] | |
sw $ra 4($sp) # ra -> [sp' + 1] = [FrameBot'] | |
addiu $fp $sp 4 # sp' + 1 -> fp' = FrameBot' = sp' + 1 = sp - 2 | |
move $s0 $a0 # self <- self' | |
lw $a0 16($fp) # [FrameBot'+4] -> [self'+ 3], es decir, [sp + 2] -> [self'.title], es decir, title_p -> [self'.title] | |
sw $a0 12($s0) | |
lw $a0 12($fp) # [FrameBot'+3] -> [self'+ 4], es decir, [sp + 1] -> [self'.author], es decir, autho_p -> [self'.autho] | |
sw $a0 16($s0) | |
move $a0 $s0 # self'-> return valu | |
lw $fp 12($sp) # [sp+3] -> fp | |
lw $s0 8($sp) # [sp+2] | |
lw $ra 4($sp) # [sp+1] | |
addiu $sp $sp 20 # sp = sp' + 3; añadir +2 por descarte de dos argumentos | |
jr $ra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
recite( value : Int ) : Object | |
{ | |
{ | |
out_int( value ); | |
out_string( "\n" ); | |
self; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment