Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   .equiv @sbrk, 9
  2.   .equiv @print_string, 4
  3.   .equiv @print_char, 11
  4.   .equiv @print_int, 1
  5.   .equiv @exit2, 17
  6.   .equiv @.__obj_size__, 4
  7.   .equiv @.__len__, 12
  8.   .equiv @.__int__, 12
  9.   .equiv @.__bool__, 12
  10.   .equiv @.__str__, 16
  11.   .equiv @.__elts__, 16
  12.   .equiv @error_div_zero, 2
  13.   .equiv @error_arg, 1
  14.   .equiv @error_oob, 3
  15.   .equiv @error_none, 4
  16.   .equiv @error_oom, 5
  17.   .equiv @error_nyi, 6
  18.  
  19. .data
  20.  
  21. .globl $object$prototype
  22. $object$prototype:
  23.   .word 0                                  # Type tag for class: object
  24.   .word 3                                  # Object size
  25.   .word $object$dispatchTable              # Pointer to dispatch table
  26.   .align 2
  27.  
  28. .globl $int$prototype
  29. $int$prototype:
  30.   .word 1                                  # Type tag for class: int
  31.   .word 4                                  # Object size
  32.   .word $int$dispatchTable                 # Pointer to dispatch table
  33.   .word 0                                  # Initial value of attribute: __int__
  34.   .align 2
  35.  
  36. .globl $bool$prototype
  37. $bool$prototype:
  38.   .word 2                                  # Type tag for class: bool
  39.   .word 4                                  # Object size
  40.   .word $bool$dispatchTable                # Pointer to dispatch table
  41.   .word 0                                  # Initial value of attribute: __bool__
  42.   .align 2
  43.  
  44. .globl $str$prototype
  45. $str$prototype:
  46.   .word 3                                  # Type tag for class: str
  47.   .word 5                                  # Object size
  48.   .word $str$dispatchTable                 # Pointer to dispatch table
  49.   .word 0                                  # Initial value of attribute: __len__
  50.   .word 0                                  # Initial value of attribute: __str__
  51.   .align 2
  52.  
  53. .globl $.list$prototype
  54. $.list$prototype:
  55.   .word -1                                 # Type tag for class: .list
  56.   .word 4                                  # Object size
  57.   .word 0                                  # Pointer to dispatch table
  58.   .word 0                                  # Initial value of attribute: __len__
  59.   .align 2
  60.  
  61. .globl $object$dispatchTable
  62. $object$dispatchTable:
  63.   .word $object.__init__                   # Implementation for method: object.__init__
  64.  
  65. .globl $int$dispatchTable
  66. $int$dispatchTable:
  67.   .word $object.__init__                   # Implementation for method: int.__init__
  68.  
  69. .globl $bool$dispatchTable
  70. $bool$dispatchTable:
  71.   .word $object.__init__                   # Implementation for method: bool.__init__
  72.  
  73. .globl $str$dispatchTable
  74. $str$dispatchTable:
  75.   .word $object.__init__                   # Implementation for method: str.__init__
  76.  
  77. .globl $g
  78. $g:
  79.   .word 1                                  # Initial value of global var: g
  80.  
  81. .globl $h
  82. $h:
  83.   .word 1                                  # Initial value of global var: h
  84.  
  85. .globl $a
  86. $a:
  87.   .word 0                                  # Initial value of global var: a
  88.  
  89. .globl $b
  90. $b:
  91.   .word 0                                  # Initial value of global var: b
  92.  
  93. .globl $c
  94. $c:
  95.   .word 0                                  # Initial value of global var: c
  96.  
  97. .globl $d
  98. $d:
  99.   .word 0                                  # Initial value of global var: d
  100.  
  101. .globl $e
  102. $e:
  103.   .word 0                                  # Initial value of global var: e
  104.  
  105. .globl $f
  106. $f:
  107.   .word 0                                  # Initial value of global var: f
  108.  
  109. .text
  110.  
  111. .globl main
  112. main:
  113.   lui a0, 8192                             # Initialize heap size (in multiples of 4KB)
  114.   add s11, s11, a0                         # Save heap size
  115.   jal heap.init                            # Call heap.init routine
  116.   mv gp, a0                                # Initialize heap pointer
  117.   mv s10, gp                               # Set beginning of heap
  118.   add s11, s10, s11                        # Set end of heap (= start of heap + heap size)
  119.   mv ra, zero                              # No normal return from main program.
  120.   mv fp, zero                              # No preceding frame.
  121.   mv fp, zero                              # Top saved FP is 0.
  122.   mv ra, zero                              # No function return from top level.
  123.   addi sp, sp, -@..main.size               # Reserve space for stack frame.
  124.   sw ra, @..main.size-4(sp)                # return address
  125.   sw fp, @..main.size-8(sp)                # control link
  126.   addi fp, sp, @..main.size                # New fp is at old SP.
  127.   lw a0, $g                                # Load global: g
  128.   sw a0, @..main.negsize+0(fp)             # Push argument 0 from last.
  129.   addi sp, fp, @..main.negsize             # Set SP to last argument.
  130.   jal $foo                                 # Invoke function: foo
  131.   addi sp, fp, @..main.negsize             # Set SP to stack frame top.
  132.   sw a0, -20(fp)                           # Push on stack slot 5
  133.   la a0, $int$prototype                    # Load pointer to prototype of: int
  134.   jal alloc                                # Allocate new object
  135.   lw t0, -20(fp)                           # Pop stack slot 5
  136.   sw t0, 12(a0)                            # Set attribute: __int__
  137.   sw a0, @..main.negsize+0(fp)             # Push argument 0 from last.
  138.   addi sp, fp, @..main.negsize             # Set SP to last argument.
  139.   jal $print                               # Invoke function: print
  140.   addi sp, fp, @..main.negsize             # Set SP to stack frame top.
  141.   .equiv @..main.size, 16
  142.   .equiv @..main.negsize, -16
  143. label_0:                                   # End of program
  144.   li a0, 10                                # Code for ecall: exit
  145.   ecall
  146.  
  147. .globl $object.__init__
  148. $object.__init__:
  149. # Init method for type object. 
  150.   mv a0, zero                              # `None` constant
  151.   jr ra                                    # Return
  152.  
  153. .globl $print
  154. $print:
  155. # Function print
  156.   lw a0, 0(sp)                             # Load arg
  157.   beq a0, zero, print_6                    # None is an illegal argument
  158.   lw t0, 0(a0)                             # Get type tag of arg
  159.   li t1, 1                                 # Load type tag of `int`
  160.   beq t0, t1, print_7                      # Go to print(int)
  161.   li t1, 3                                 # Load type tag of `str`
  162.   beq t0, t1, print_8                      # Go to print(str)
  163.   li t1, 2                                 # Load type tag of `bool`
  164.   beq t0, t1, print_9                      # Go to print(bool)
  165. print_6:                                   # Invalid argument
  166.   li a0, 1                                 # Exit code for: Invalid argument
  167.   la a1, const_2                           # Load error message as str
  168.   addi a1, a1, @.__str__                   # Load address of attribute __str__
  169.   j abort                                  # Abort
  170.  
  171. # Printing bools
  172. print_9:                                   # Print bool object in A0
  173.   lw a0, @.__bool__(a0)                    # Load attribute __bool__
  174.   beq a0, zero, print_10                   # Go to: print(False)
  175.   la a0, const_3                           # String representation: True
  176.   j print_8                                # Go to: print(str)
  177. print_10:                                  # Print False object in A0
  178.   la a0, const_4                           # String representation: False
  179.   j print_8                                # Go to: print(str)
  180.  
  181. # Printing strs.
  182. print_8:                                   # Print str object in A0
  183.   addi a1, a0, @.__str__                   # Load address of attribute __str__
  184.   j print_11                               # Print the null-terminated string is now in A1
  185.   mv a0, zero                              # Load None
  186.   j print_5                                # Go to return
  187. print_11:                                  # Print null-terminated string in A1
  188.   li a0, @print_string                     # Code for ecall: print_string
  189.   ecall                                    # Print string
  190.   li a1, 10                                # Load newline character
  191.   li a0, @print_char                       # Code for ecall: print_char
  192.   ecall                                    # Print character
  193.   j print_5                                # Go to return
  194.  
  195. # Printing ints.
  196. print_7:                                   # Print int object in A0
  197.   lw a1, @.__int__(a0)                     # Load attribute __int__
  198.   li a0, @print_int                        # Code for ecall: print_int
  199.   ecall                                    # Print integer
  200.   li a1, 10                                # Load newline character
  201.   li a0, 11                                # Code for ecall: print_char
  202.   ecall                                    # Print character
  203.  
  204. print_5:                                   # End of function
  205.   mv a0, zero                              # Load None
  206.   jr ra                                    # Return to caller
  207.  
  208. .globl $len
  209. $len:
  210. # Function len
  211.       # We do not save/restore fp/ra for this function
  212.       # because we know that it does not use the stack or does not
  213.       # call other functions.
  214.  
  215.   lw a0, 0(sp)                             # Load arg
  216.   beq a0, zero, len_12                     # None is an illegal argument
  217.   lw t0, 0(a0)                             # Get type tag of arg
  218.   li t1, 3                                 # Load type tag of `str`
  219.   beq t0, t1, len_13                       # Go to len(str)
  220.   li t1, -1                                # Load type tag for list objects
  221.   beq t0, t1, len_13                       # Go to len(list)
  222. len_12:                                    # Invalid argument
  223.   li a0, @error_arg                        # Exit code for: Invalid argument
  224.   la a1, const_2                           # Load error message as str
  225.   addi a1, a1, @.__str__                   # Load address of attribute __str__
  226.   j abort                                  # Abort
  227. len_13:                                    # Get length of string
  228.   lw a0, @.__len__(a0)                     # Load attribute: __len__
  229.   jr ra                                    # Return to caller
  230.  
  231. .globl $input
  232. $input:
  233. # Function input
  234.   li a0, @error_nyi                        # Exit code for: Unsupported operation
  235.   la a1, const_5                           # Load error message as str
  236.   addi a1, a1, @.__str__                   # Load address of attribute __str__
  237.   j abort                                  # Abort
  238.  
  239. .globl $foo.bar.baz.caz.jaz
  240. $foo.bar.baz.caz.jaz:
  241.   addi sp, sp, -@foo.bar.baz.caz.jaz.size  # Reserve space for stack frame.
  242.   sw ra, @foo.bar.baz.caz.jaz.size-4(sp)   # return address
  243.   sw fp, @foo.bar.baz.caz.jaz.size-8(sp)   # control link
  244.   addi fp, sp, @foo.bar.baz.caz.jaz.size   # New fp is at old SP.
  245.   la a0, const_6                           # Load string literal
  246.   sw a0, @foo.bar.baz.caz.jaz.negsize+0(fp) # Push argument 0 from last.
  247.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to last argument.
  248.   jal $print                               # Invoke function: print
  249.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to stack frame top.
  250.   lw t0, 0(fp)                             # Load static link from foo.bar.baz.caz.jaz to foo.bar.baz.caz
  251.   lw t0, 0(t0)                             # Load static link from foo.bar.baz.caz to foo.bar.baz
  252.   lw t0, 4(t0)                             # Load static link from foo.bar.baz to foo.bar
  253.   lw a0, -12(t0)                           # Load var: foo.bar.z
  254.   sw a0, -20(fp)                           # Push on stack slot 5
  255.   la a0, $int$prototype                    # Load pointer to prototype of: int
  256.   jal alloc                                # Allocate new object
  257.   lw t0, -20(fp)                           # Pop stack slot 5
  258.   sw t0, 12(a0)                            # Set attribute: __int__
  259.   sw a0, @foo.bar.baz.caz.jaz.negsize+0(fp) # Push argument 0 from last.
  260.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to last argument.
  261.   jal $print                               # Invoke function: print
  262.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to stack frame top.
  263.   lw t0, 0(fp)                             # Load static link from foo.bar.baz.caz.jaz to foo.bar.baz.caz
  264.   lw t0, 0(t0)                             # Load static link from foo.bar.baz.caz to foo.bar.baz
  265.   lw t0, 4(t0)                             # Load static link from foo.bar.baz to foo.bar
  266.   lw t0, 8(t0)                             # Load static link from foo.bar to foo
  267.   lw a0, -16(t0)                           # Load var: foo.y
  268.   sw a0, -20(fp)                           # Push on stack slot 5
  269.   la a0, $int$prototype                    # Load pointer to prototype of: int
  270.   jal alloc                                # Allocate new object
  271.   lw t0, -20(fp)                           # Pop stack slot 5
  272.   sw t0, 12(a0)                            # Set attribute: __int__
  273.   sw a0, @foo.bar.baz.caz.jaz.negsize+0(fp) # Push argument 0 from last.
  274.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to last argument.
  275.   jal $print                               # Invoke function: print
  276.   addi sp, fp, @foo.bar.baz.caz.jaz.negsize # Set SP to stack frame top.
  277.   lw t0, 0(fp)                             # Load static link from foo.bar.baz.caz.jaz to foo.bar.baz.caz
  278.   lw t0, 0(t0)                             # Load static link from foo.bar.baz.caz to foo.bar.baz
  279.   lw t0, 4(t0)                             # Load static link from foo.bar.baz to foo.bar
  280.   lw a0, -12(t0)                           # Load var: foo.bar.z
  281.   j label_1                                # Go to return
  282.   mv a0, zero                              # Load None
  283.   j label_1                                # Jump to function epilogue
  284. label_1:                                   # Epilogue
  285.   .equiv @foo.bar.baz.caz.jaz.size, 16
  286.   .equiv @foo.bar.baz.caz.jaz.negsize, -16
  287.   lw ra, -4(fp)                            # Get return address
  288.   lw fp, -8(fp)                            # Use control link to restore caller's fp
  289.  addi sp, sp, @foo.bar.baz.caz.jaz.size   # Restore stack pointer
  290.  jr ra                                    # Return to caller
  291.  
  292. .globl $foo.bar.baz.caz
  293. $foo.bar.baz.caz:
  294.  addi sp, sp, -@foo.bar.baz.caz.size      # Reserve space for stack frame.
  295.  sw ra, @foo.bar.baz.caz.size-4(sp)       # return address
  296.  sw fp, @foo.bar.baz.caz.size-8(sp)       # control link
  297.  addi fp, sp, @foo.bar.baz.caz.size       # New fp is at old SP.
  298.  la a0, const_7                           # Load string literal
  299.  sw a0, @foo.bar.baz.caz.negsize+0(fp)    # Push argument 0 from last.
  300.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to last argument.
  301.  jal $print                               # Invoke function: print
  302.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to stack frame top.
  303.  mv t0, fp                                # Get static link to foo.bar.baz.caz
  304.  sw t0, @foo.bar.baz.caz.negsize+0(fp)    # Push argument 0 from last (this was fp)
  305.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to last argument.
  306.  jal $foo.bar.baz.caz.jaz                 # Invoke function: foo.bar.baz.caz.jaz
  307.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to stack frame top.
  308.  sw a0, -20(fp)                           # Push on stack slot 5
  309.  la a0, $int$prototype                    # Load pointer to prototype of: int
  310.  jal alloc                                # Allocate new object
  311.  lw t0, -20(fp)                           # Pop stack slot 5
  312.  sw t0, 12(a0)                            # Set attribute: __int__
  313.  sw a0, @foo.bar.baz.caz.negsize+0(fp)    # Push argument 0 from last.
  314.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to last argument.
  315.  jal $print                               # Invoke function: print
  316.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to stack frame top.
  317.  lw t0, 0(fp)                             # Load static link from foo.bar.baz.caz to foo.bar.baz
  318.  lw t0, 4(t0)                             # Load static link from foo.bar.baz to foo.bar
  319.  lw a0, -12(t0)                           # Load var: foo.bar.z
  320.  sw a0, -20(fp)                           # Push on stack slot 5
  321.  la a0, $int$prototype                    # Load pointer to prototype of: int
  322.  jal alloc                                # Allocate new object
  323.  lw t0, -20(fp)                           # Pop stack slot 5
  324.  sw t0, 12(a0)                            # Set attribute: __int__
  325.  sw a0, @foo.bar.baz.caz.negsize+0(fp)    # Push argument 0 from last.
  326.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to last argument.
  327.  jal $print                               # Invoke function: print
  328.  addi sp, fp, @foo.bar.baz.caz.negsize    # Set SP to stack frame top.
  329.  lw t0, 0(fp)                             # Load static link from foo.bar.baz.caz to foo.bar.baz
  330.  lw t0, 4(t0)                             # Load static link from foo.bar.baz to foo.bar
  331.  lw a0, -12(t0)                           # Load var: foo.bar.z
  332.  j label_2                                # Go to return
  333.  mv a0, zero                              # Load None
  334.  j label_2                                # Jump to function epilogue
  335. label_2:                                   # Epilogue
  336.  .equiv @foo.bar.baz.caz.size, 16
  337.  .equiv @foo.bar.baz.caz.negsize, -16
  338.  lw ra, -4(fp)                            # Get return address
  339.  lw fp, -8(fp)                            # Use control link to restore caller's fp
  340.   addi sp, sp, @foo.bar.baz.caz.size       # Restore stack pointer
  341.   jr ra                                    # Return to caller
  342.  
  343. .globl $foo.bar.baz
  344. $foo.bar.baz:
  345.   addi sp, sp, -@foo.bar.baz.size          # Reserve space for stack frame.
  346.   sw ra, @foo.bar.baz.size-4(sp)           # return address
  347.   sw fp, @foo.bar.baz.size-8(sp)           # control link
  348.   addi fp, sp, @foo.bar.baz.size           # New fp is at old SP.
  349.   la a0, const_8                           # Load string literal
  350.   sw a0, @foo.bar.baz.negsize+0(fp)        # Push argument 0 from last.
  351.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to last argument.
  352.   jal $print                               # Invoke function: print
  353.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to stack frame top.
  354.   mv t0, fp                                # Get static link to foo.bar.baz
  355.   sw t0, @foo.bar.baz.negsize+0(fp)        # Push argument 0 from last (this was fp)
  356.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to last argument.
  357.   jal $foo.bar.baz.caz                     # Invoke function: foo.bar.baz.caz
  358.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to stack frame top.
  359.   sw a0, -20(fp)                           # Push on stack slot 5
  360.   la a0, $int$prototype                    # Load pointer to prototype of: int
  361.   jal alloc                                # Allocate new object
  362.   lw t0, -20(fp)                           # Pop stack slot 5
  363.   sw t0, 12(a0)                            # Set attribute: __int__
  364.   sw a0, @foo.bar.baz.negsize+0(fp)        # Push argument 0 from last.
  365.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to last argument.
  366.   jal $print                               # Invoke function: print
  367.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to stack frame top.
  368.   lw t0, 4(fp)                             # Load static link from foo.bar.baz to foo.bar
  369.   lw t0, 8(t0)                             # Load static link from foo.bar to foo
  370.   lw a0, -16(t0)                           # Load var: foo.y
  371.   sw a0, -20(fp)                           # Push on stack slot 5
  372.   la a0, $int$prototype                    # Load pointer to prototype of: int
  373.   jal alloc                                # Allocate new object
  374.   lw t0, -20(fp)                           # Pop stack slot 5
  375.   sw t0, 12(a0)                            # Set attribute: __int__
  376.   sw a0, @foo.bar.baz.negsize+0(fp)        # Push argument 0 from last.
  377.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to last argument.
  378.   jal $print                               # Invoke function: print
  379.   addi sp, fp, @foo.bar.baz.negsize        # Set SP to stack frame top.
  380.   lw t0, 4(fp)                             # Load static link from foo.bar.baz to foo.bar
  381.   lw t0, 8(t0)                             # Load static link from foo.bar to foo
  382.   lw a0, -16(t0)                           # Load var: foo.y
  383.   j label_3                                # Go to return
  384.   mv a0, zero                              # Load None
  385.   j label_3                                # Jump to function epilogue
  386. label_3:                                   # Epilogue
  387.   .equiv @foo.bar.baz.size, 16
  388.   .equiv @foo.bar.baz.negsize, -16
  389.   lw ra, -4(fp)                            # Get return address
  390.   lw fp, -8(fp)                            # Use control link to restore caller's fp
  391.  addi sp, sp, @foo.bar.baz.size           # Restore stack pointer
  392.  jr ra                                    # Return to caller
  393.  
  394. .globl $foo.bar.caz
  395. $foo.bar.caz:
  396.  addi sp, sp, -@foo.bar.caz.size          # Reserve space for stack frame.
  397.  sw ra, @foo.bar.caz.size-4(sp)           # return address
  398.  sw fp, @foo.bar.caz.size-8(sp)           # control link
  399.  addi fp, sp, @foo.bar.caz.size           # New fp is at old SP.
  400.  la a0, const_9                           # Load string literal
  401.  sw a0, @foo.bar.caz.negsize+0(fp)        # Push argument 0 from last.
  402.  addi sp, fp, @foo.bar.caz.negsize        # Set SP to last argument.
  403.  jal $print                               # Invoke function: print
  404.  addi sp, fp, @foo.bar.caz.negsize        # Set SP to stack frame top.
  405.  lw t0, 0(fp)                             # Load static link from foo.bar.caz to foo.bar
  406.  lw a0, -12(t0)                           # Load var: foo.bar.z
  407.  sw a0, -20(fp)                           # Push on stack slot 5
  408.  la a0, $int$prototype                    # Load pointer to prototype of: int
  409.  jal alloc                                # Allocate new object
  410.  lw t0, -20(fp)                           # Pop stack slot 5
  411.  sw t0, 12(a0)                            # Set attribute: __int__
  412.  sw a0, @foo.bar.caz.negsize+0(fp)        # Push argument 0 from last.
  413.  addi sp, fp, @foo.bar.caz.negsize        # Set SP to last argument.
  414.  jal $print                               # Invoke function: print
  415.  addi sp, fp, @foo.bar.caz.negsize        # Set SP to stack frame top.
  416.  lw t0, 0(fp)                             # Load static link from foo.bar.caz to foo.bar
  417.  lw a0, -12(t0)                           # Load var: foo.bar.z
  418.  j label_4                                # Go to return
  419.  mv a0, zero                              # Load None
  420.  j label_4                                # Jump to function epilogue
  421. label_4:                                   # Epilogue
  422.  .equiv @foo.bar.caz.size, 16
  423.  .equiv @foo.bar.caz.negsize, -16
  424.  lw ra, -4(fp)                            # Get return address
  425.  lw fp, -8(fp)                            # Use control link to restore caller's fp
  426.   addi sp, sp, @foo.bar.caz.size           # Restore stack pointer
  427.   jr ra                                    # Return to caller
  428.  
  429. .globl $foo.bar
  430. $foo.bar:
  431.   addi sp, sp, -@foo.bar.size              # Reserve space for stack frame.
  432.   sw ra, @foo.bar.size-4(sp)               # return address
  433.   sw fp, @foo.bar.size-8(sp)               # control link
  434.   addi fp, sp, @foo.bar.size               # New fp is at old SP.
  435.   li a0, 3                                 # Load integer literal 3
  436.   sw a0, -12(fp)                           # local variable z
  437.   la a0, const_10                          # Load string literal
  438.   sw a0, @foo.bar.negsize+0(fp)            # Push argument 0 from last.
  439.   addi sp, fp, @foo.bar.negsize            # Set SP to last argument.
  440.   jal $print                               # Invoke function: print
  441.   addi sp, fp, @foo.bar.negsize            # Set SP to stack frame top.
  442.   lw a0, 4(fp)                             # Load var: foo.bar.a
  443.   sw a0, @foo.bar.negsize+0(fp)            # Push argument 0 from last.
  444.   addi sp, fp, @foo.bar.negsize            # Set SP to last argument.
  445.   jal $print                               # Invoke function: print
  446.   addi sp, fp, @foo.bar.negsize            # Set SP to stack frame top.
  447.   mv t0, fp                                # Get static link to foo.bar
  448.   sw t0, @foo.bar.negsize+4(fp)            # Push argument 1 from last (this was fp)
  449.   li a0, 1                                 # Load boolean literal: true
  450.   la t0, const_0                           # Address of False object
  451.   slli a0, a0, 4                           # Get offset of the correct boolean object
  452.   add a0, t0, a0                           # Get address of the correct boolean object
  453.   sw a0, @foo.bar.negsize+0(fp)            # Push argument 0 from last.
  454.   addi sp, fp, @foo.bar.negsize            # Set SP to last argument.
  455.   jal $foo.bar.baz                         # Invoke function: foo.bar.baz
  456.   addi sp, fp, @foo.bar.negsize            # Set SP to stack frame top.
  457.   sw a0, -24(fp)                           # Push on stack slot 6
  458.   la a0, $int$prototype                    # Load pointer to prototype of: int
  459.   jal alloc                                # Allocate new object
  460.   lw t0, -24(fp)                           # Pop stack slot 6
  461.   sw t0, 12(a0)                            # Set attribute: __int__
  462.   sw a0, @foo.bar.negsize+0(fp)            # Push argument 0 from last.
  463.   addi sp, fp, @foo.bar.negsize            # Set SP to last argument.
  464.   jal $print                               # Invoke function: print
  465.   addi sp, fp, @foo.bar.negsize            # Set SP to stack frame top.
  466.   li a0, 6                                 # Load integer literal 6
  467.   sw a0, -12(fp)                           # Assign var: foo.bar.z
  468.   mv t0, fp                                # Get static link to foo.bar
  469.   sw t0, @foo.bar.negsize+4(fp)            # Push argument 1 from last (this was fp)
  470.   li a0, 0                                 # Load boolean literal: false
  471.   la t0, const_0                           # Address of False object
  472.   slli a0, a0, 4                           # Get offset of the correct boolean object
  473.   add a0, t0, a0                           # Get address of the correct boolean object
  474.   sw a0, @foo.bar.negsize+0(fp)            # Push argument 0 from last.
  475.   addi sp, fp, @foo.bar.negsize            # Set SP to last argument.
  476.   jal $foo.bar.baz                         # Invoke function: foo.bar.baz
  477.   addi sp, fp, @foo.bar.negsize            # Set SP to stack frame top.
  478.   j label_5                                # Go to return
  479.   mv a0, zero                              # Load None
  480.   j label_5                                # Jump to function epilogue
  481. label_5:                                   # Epilogue
  482.   .equiv @foo.bar.size, 32
  483.   .equiv @foo.bar.negsize, -32
  484.   lw ra, -4(fp)                            # Get return address
  485.   lw fp, -8(fp)                            # Use control link to restore caller's fp
  486.  addi sp, sp, @foo.bar.size               # Restore stack pointer
  487.  jr ra                                    # Return to caller
  488.  
  489. .globl $foo
  490. $foo:
  491.  addi sp, sp, -@foo.size                  # Reserve space for stack frame.
  492.  sw ra, @foo.size-4(sp)                   # return address
  493.  sw fp, @foo.size-8(sp)                   # control link
  494.  addi fp, sp, @foo.size                   # New fp is at old SP.
  495.  la a0, const_11                          # Load string literal
  496.  sw a0, -12(fp)                           # local variable z
  497.  li a0, 2                                 # Load integer literal 2
  498.  sw a0, -16(fp)                           # local variable y
  499.  mv t0, fp                                # Get static link to foo
  500.  sw t0, @foo.negsize+8(fp)                # Push argument 2 from last (this was fp)
  501.  lw a0, -12(fp)                           # Load var: foo.z
  502.  sw a0, @foo.negsize+4(fp)                # Push argument 1 from last.
  503.  li a0, 1                                 # Load integer literal 1
  504.  sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  505.  addi sp, fp, @foo.negsize                # Set SP to last argument.
  506.  jal $foo.bar                             # Invoke function: foo.bar
  507.  addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  508.  li a0, 5                                 # Load integer literal 5
  509.  sw a0, -16(fp)                           # Assign var: foo.y
  510.  li a0, 5                                 # Load integer literal 5
  511.  sw a0, -16(fp)                           # Assign var: foo.y
  512.  li a0, 5                                 # Load integer literal 5
  513.  sw a0, -16(fp)                           # Assign var: foo.y
  514.  li a0, 93838                             # Load integer literal 93838
  515.  sw a0, -16(fp)                           # Assign var: foo.y
  516.  mv t0, fp                                # Get static link to foo
  517.  sw t0, @foo.negsize+8(fp)                # Push argument 2 from last (this was fp)
  518.  li a0, 5                                 # Load integer literal 5
  519.  sw a0, -36(fp)                           # Push on stack slot 9
  520.  li a0, 3                                 # Load integer literal 3
  521.  sw a0, -40(fp)                           # Push on stack slot 10
  522.  li a0, 9                                 # Load integer literal 9
  523.  lw t0, -40(fp)                           # Pop stack slot 10
  524.  mul a0, t0, a0                           # Operator *
  525.  sw a0, -40(fp)                           # Push on stack slot 10
  526.  li a0, 4                                 # Load integer literal 4
  527.  lw t0, -40(fp)                           # Pop stack slot 10
  528.  bnez a0, label_7                         # Ensure non-zero divisor
  529.  j error.Div                              # Go to error handler
  530. label_7:                                   # Divisor is non-zero
  531.  xor t2, t0, a0                           # Check for same sign
  532.  bltz t2, label_9                         # If !=, need to adjust left operand
  533.  div a0, t0, a0                           # Operator //
  534.  j label_8                                # Jump to end of division
  535. label_9:                                   # Operands have differing signs
  536.  slt t2, zero, a0                         # tmp = 1 if right > 0 else 0
  537.  add t2, t2, t2                           # tmp *= 2
  538.  addi t2, t2, -1                          # tmp = 1 if right>=0 else -1
  539.  add t2, t0, t2                           # Adjust left operand
  540.  div t2, t2, a0                           # Adjusted division, toward 0
  541.  addi a0, t2, -1                          # Complete division when signs !=
  542. label_8:                                   # End of //
  543.  sw a0, -40(fp)                           # Push on stack slot 10
  544.  li a0, 7                                 # Load integer literal 7
  545.  lw t0, -40(fp)                           # Pop stack slot 10
  546.  bnez a0, label_10                        # Ensure non-zero divisor
  547.  j error.Div                              # Go to error handler
  548. label_10:                                  # Divisor is non-zero
  549.  rem t2, t0, a0                           # Operator rem
  550.  beqz t2, label_11                        # If no remainder, no adjustment
  551.  xor t3, t2, a0                           # Check for differing signs.
  552.  bgez t3, label_11                        # Don't adjust if signs equal.
  553.   add a0, t2, a0                           # Adjust
  554.   j label_12                               # Jump to end of mod
  555. label_11:                                  # Store result
  556.   mv a0, t2
  557. label_12:                                  # End of %
  558.   lw t0, -36(fp)                           # Pop stack slot 9
  559.   add a0, t0, a0                           # Operator +
  560.   sw a0, -36(fp)                           # Push on stack slot 9
  561.   li a0, 4                                 # Load integer literal 4
  562.   lw t0, -36(fp)                           # Pop stack slot 9
  563.   sub a0, t0, a0                           # Operator -
  564.   sw a0, -36(fp)                           # Push on stack slot 9
  565.   li a0, 90                                # Load integer literal 90
  566.   sub a0, zero, a0                         # Unary negation
  567.   sw a0, -40(fp)                           # Push on stack slot 10
  568.   li a0, 49                                # Load integer literal 49
  569.   lw t0, -40(fp)                           # Pop stack slot 10
  570.   mul a0, t0, a0                           # Operator *
  571.   sw a0, -40(fp)                           # Push on stack slot 10
  572.   li a0, 7374                              # Load integer literal 7374
  573.   lw t0, -40(fp)                           # Pop stack slot 10
  574.   bnez a0, label_13                        # Ensure non-zero divisor
  575.   j error.Div                              # Go to error handler
  576. label_13:                                  # Divisor is non-zero
  577.   xor t2, t0, a0                           # Check for same sign
  578.   bltz t2, label_15                        # If !=, need to adjust left operand
  579.   div a0, t0, a0                           # Operator //
  580.   j label_14                               # Jump to end of division
  581. label_15:                                  # Operands have differing signs
  582.   slt t2, zero, a0                         # tmp = 1 if right > 0 else 0
  583.   add t2, t2, t2                           # tmp *= 2
  584.   addi t2, t2, -1                          # tmp = 1 if right>=0 else -1
  585.   add t2, t0, t2                           # Adjust left operand
  586.   div t2, t2, a0                           # Adjusted division, toward 0
  587.   addi a0, t2, -1                          # Complete division when signs !=
  588. label_14:                                  # End of //
  589.   lw t0, -36(fp)                           # Pop stack slot 9
  590.   add a0, t0, a0                           # Operator +
  591.   sw a0, -36(fp)                           # Push on stack slot 9
  592.   la a0, $int$prototype                    # Load pointer to prototype of: int
  593.   jal alloc                                # Allocate new object
  594.   lw t0, -36(fp)                           # Pop stack slot 9
  595.   sw t0, 12(a0)                            # Set attribute: __int__
  596.   sw a0, @foo.negsize+4(fp)                # Push argument 1 from last.
  597.   li a0, 9001                              # Load integer literal 9001
  598.   sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  599.   addi sp, fp, @foo.negsize                # Set SP to last argument.
  600.   jal $foo.bar                             # Invoke function: foo.bar
  601.   addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  602.   li a0, 69                                # Load integer literal 69
  603.   sw a0, -16(fp)                           # Assign var: foo.y
  604.   la a0, const_12                          # Load string literal
  605.   sw a0, -12(fp)                           # Assign var: foo.z
  606.   la a0, const_13                          # Load string literal
  607.   sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  608.   addi sp, fp, @foo.negsize                # Set SP to last argument.
  609.   jal $print                               # Invoke function: print
  610.   addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  611.   mv t0, fp                                # Get static link to foo
  612.   sw t0, @foo.negsize+8(fp)                # Push argument 2 from last (this was fp)
  613.   li a0, 5                                 # Load integer literal 5
  614.   sw a0, -40(fp)                           # Push on stack slot 10
  615.   la a0, $int$prototype                    # Load pointer to prototype of: int
  616.   jal alloc                                # Allocate new object
  617.   lw t0, -40(fp)                           # Pop stack slot 10
  618.   sw t0, 12(a0)                            # Set attribute: __int__
  619.   sw a0, @foo.negsize+4(fp)                # Push argument 1 from last.
  620.   li a0, 9001                              # Load integer literal 9001
  621.   sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  622.   addi sp, fp, @foo.negsize                # Set SP to last argument.
  623.   jal $foo.bar                             # Invoke function: foo.bar
  624.   addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  625.   sw a0, -28(fp)                           # Push on stack slot 7
  626.   la a0, $int$prototype                    # Load pointer to prototype of: int
  627.   jal alloc                                # Allocate new object
  628.   lw t0, -28(fp)                           # Pop stack slot 7
  629.   sw t0, 12(a0)                            # Set attribute: __int__
  630.   sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  631.   addi sp, fp, @foo.negsize                # Set SP to last argument.
  632.   jal $print                               # Invoke function: print
  633.   addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  634.   mv t0, fp                                # Get static link to foo
  635.   sw t0, @foo.negsize+8(fp)                # Push argument 2 from last (this was fp)
  636.   li a0, 5                                 # Load integer literal 5
  637.   sw a0, -36(fp)                           # Push on stack slot 9
  638.   la a0, $int$prototype                    # Load pointer to prototype of: int
  639.   jal alloc                                # Allocate new object
  640.   lw t0, -36(fp)                           # Pop stack slot 9
  641.   sw t0, 12(a0)                            # Set attribute: __int__
  642.   sw a0, @foo.negsize+4(fp)                # Push argument 1 from last.
  643.   li a0, 9001                              # Load integer literal 9001
  644.   sw a0, @foo.negsize+0(fp)                # Push argument 0 from last.
  645.   addi sp, fp, @foo.negsize                # Set SP to last argument.
  646.   jal $foo.bar                             # Invoke function: foo.bar
  647.   addi sp, fp, @foo.negsize                # Set SP to stack frame top.
  648.   j label_6                                # Go to return
  649.   mv a0, zero                              # Load None
  650.   j label_6                                # Jump to function epilogue
  651. label_6:                                   # Epilogue
  652.   .equiv @foo.size, 48
  653.   .equiv @foo.negsize, -48
  654.   lw ra, -4(fp)                            # Get return address
  655.   lw fp, -8(fp)                            # Use control link to restore caller's fp
  656.  addi sp, sp, @foo.size                   # Restore stack pointer
  657.  jr ra                                    # Return to caller
  658.  
  659. .globl alloc
  660. alloc:
  661. # Runtime support function alloc.
  662.        # Prototype address is in a0.
  663.  lw a1, 4(a0)                             # Get size of object in words
  664.  j alloc2                                 # Allocate object with exact size
  665.  
  666. .globl alloc2
  667. alloc2:
  668. # Runtime support function alloc2 (realloc).
  669.        # Prototype address is in a0.
  670.        # Number of words to allocate is in a1.
  671.  li a2, 4                                 # Word size in bytes
  672.  mul a2, a1, a2                           # Calculate number of bytes to allocate
  673.  add a2, gp, a2                           # Estimate where GP will move
  674.  bgeu a2, s11, alloc2_15                  # Go to OOM handler if too large
  675.  lw t0, @.__obj_size__(a0)                # Get size of object in words
  676.  mv t2, a0                                # Initialize src ptr
  677.  mv t3, gp                                # Initialize dest ptr
  678. alloc2_16:                                 # Copy-loop header
  679.  lw t1, 0(t2)                             # Load next word from src
  680.  sw t1, 0(t3)                             # Store next word to dest
  681.  addi t2, t2, 4                           # Increment src
  682.  addi t3, t3, 4                           # Increment dest
  683.  addi t0, t0, -1                          # Decrement counter
  684.  bne t0, zero, alloc2_16                  # Loop if more words left to copy
  685.  mv a0, gp                                # Save new object's address to return
  686.   sw a1, @.__obj_size__(a0)                # Set size of new object in words
  687.                                            # (same as requested size)
  688.   mv gp, a2                                # Set next free slot in the heap
  689.   jr ra                                    # Return to caller
  690. alloc2_15:                                 # OOM handler
  691.   li a0, @error_oom                        # Exit code for: Out of memory
  692.   la a1, const_14                          # Load error message as str
  693.   addi a1, a1, @.__str__                   # Load address of attribute __str__
  694.   j abort                                  # Abort
  695.  
  696. .globl abort
  697. abort:
  698. # Runtime support function abort (does not return).
  699.   mv t0, a0                                # Save exit code in temp
  700.   li a0, @print_string                     # Code for print_string ecall
  701.   ecall                                    # Print error message in a1
  702.   li a1, 10                                # Load newline character
  703.   li a0, @print_char                       # Code for print_char ecall
  704.   ecall                                    # Print newline
  705.   mv a1, t0                                # Move exit code to a1
  706.   li a0, @exit2                            # Code for exit2 ecall
  707.   ecall                                    # Exit with code
  708. abort_17:                                  # Infinite loop
  709.   j abort_17                               # Prevent fallthrough
  710.  
  711. .globl heap.init
  712. heap.init:
  713. # Runtime support function heap.init.
  714.   mv a1, a0                                # Move requested size to A1
  715.   li a0, @sbrk                             # Code for ecall: sbrk
  716.   ecall                                    # Request A1 bytes
  717.   jr ra                                    # Return to caller
  718.  
  719. .globl error.None
  720. error.None:
  721.   li a0, 4                                 # Exit code for: Operation on None
  722.   la a1, const_15                          # Load error message as str
  723.   addi a1, a1, 16                          # Load address of attribute __str__
  724.   j abort                                  # Abort
  725.  
  726. .globl error.Div
  727. error.Div:
  728.   li a0, 2                                 # Exit code for: Divison by zero
  729.   la a1, const_16                          # Load error message as str
  730.   addi a1, a1, 16                          # Load address of attribute __str__
  731.   j abort                                  # Abort
  732.  
  733. .globl error.OOB
  734. error.OOB:
  735.   li a0, 3                                 # Exit code for: Index out of bounds
  736.   la a1, const_17                          # Load error message as str
  737.   addi a1, a1, 16                          # Load address of attribute __str__
  738.   j abort                                  # Abort
  739.  
  740. .data
  741.  
  742. .globl const_0
  743. const_0:
  744.   .word 2                                  # Type tag for class: bool
  745.   .word 4                                  # Object size
  746.   .word $bool$dispatchTable                # Pointer to dispatch table
  747.   .word 0                                  # Constant value of attribute: __bool__
  748.   .align 2
  749.  
  750. .globl const_1
  751. const_1:
  752.   .word 2                                  # Type tag for class: bool
  753.   .word 4                                  # Object size
  754.   .word $bool$dispatchTable                # Pointer to dispatch table
  755.   .word 1                                  # Constant value of attribute: __bool__
  756.   .align 2
  757.  
  758. .globl const_12
  759. const_12:
  760.   .word 3                                  # Type tag for class: str
  761.   .word 6                                  # Object size
  762.   .word $str$dispatchTable                 # Pointer to dispatch table
  763.   .word 6                                  # Constant value of attribute: __len__
  764.   .string "jfjfjf"                         # Constant value of attribute: __str__
  765.   .align 2
  766.  
  767. .globl const_14
  768. const_14:
  769.   .word 3                                  # Type tag for class: str
  770.   .word 8                                  # Object size
  771.   .word $str$dispatchTable                 # Pointer to dispatch table
  772.   .word 13                                 # Constant value of attribute: __len__
  773.   .string "Out of memory"                  # Constant value of attribute: __str__
  774.   .align 2
  775.  
  776. .globl const_6
  777. const_6:
  778.   .word 3                                  # Type tag for class: str
  779.   .word 9                                  # Object size
  780.   .word $str$dispatchTable                 # Pointer to dispatch table
  781.   .word 19                                 # Constant value of attribute: __len__
  782.   .string "foo.bar.baz.caz.jaz"            # Constant value of attribute: __str__
  783.   .align 2
  784.  
  785. .globl const_9
  786. const_9:
  787.   .word 3                                  # Type tag for class: str
  788.   .word 7                                  # Object size
  789.   .word $str$dispatchTable                 # Pointer to dispatch table
  790.   .word 11                                 # Constant value of attribute: __len__
  791.   .string "foo.bar.caz"                    # Constant value of attribute: __str__
  792.   .align 2
  793.  
  794. .globl const_8
  795. const_8:
  796.   .word 3                                  # Type tag for class: str
  797.   .word 7                                  # Object size
  798.   .word $str$dispatchTable                 # Pointer to dispatch table
  799.   .word 11                                 # Constant value of attribute: __len__
  800.   .string "foo.bar.baz"                    # Constant value of attribute: __str__
  801.   .align 2
  802.  
  803. .globl const_13
  804. const_13:
  805.   .word 3                                  # Type tag for class: str
  806.   .word 5                                  # Object size
  807.   .word $str$dispatchTable                 # Pointer to dispatch table
  808.   .word 3                                  # Constant value of attribute: __len__
  809.   .string "foo"                            # Constant value of attribute: __str__
  810.   .align 2
  811.  
  812. .globl const_3
  813. const_3:
  814.   .word 3                                  # Type tag for class: str
  815.   .word 6                                  # Object size
  816.   .word $str$dispatchTable                 # Pointer to dispatch table
  817.   .word 4                                  # Constant value of attribute: __len__
  818.   .string "True"                           # Constant value of attribute: __str__
  819.   .align 2
  820.  
  821. .globl const_4
  822. const_4:
  823.   .word 3                                  # Type tag for class: str
  824.   .word 6                                  # Object size
  825.   .word $str$dispatchTable                 # Pointer to dispatch table
  826.   .word 5                                  # Constant value of attribute: __len__
  827.   .string "False"                          # Constant value of attribute: __str__
  828.   .align 2
  829.  
  830. .globl const_11
  831. const_11:
  832.   .word 3                                  # Type tag for class: str
  833.   .word 6                                  # Object size
  834.   .word $str$dispatchTable                 # Pointer to dispatch table
  835.   .word 6                                  # Constant value of attribute: __len__
  836.   .string "jfdkjf"                         # Constant value of attribute: __str__
  837.   .align 2
  838.  
  839. .globl const_10
  840. const_10:
  841.   .word 3                                  # Type tag for class: str
  842.   .word 6                                  # Object size
  843.   .word $str$dispatchTable                 # Pointer to dispatch table
  844.   .word 7                                  # Constant value of attribute: __len__
  845.   .string "foo.bar"                        # Constant value of attribute: __str__
  846.   .align 2
  847.  
  848. .globl const_17
  849. const_17:
  850.   .word 3                                  # Type tag for class: str
  851.   .word 9                                  # Object size
  852.   .word $str$dispatchTable                 # Pointer to dispatch table
  853.   .word 19                                 # Constant value of attribute: __len__
  854.   .string "Index out of bounds"            # Constant value of attribute: __str__
  855.   .align 2
  856.  
  857. .globl const_16
  858. const_16:
  859.   .word 3                                  # Type tag for class: str
  860.   .word 8                                  # Object size
  861.   .word $str$dispatchTable                 # Pointer to dispatch table
  862.   .word 15                                 # Constant value of attribute: __len__
  863.   .string "Divison by zero"                # Constant value of attribute: __str__
  864.   .align 2
  865.  
  866. .globl const_15
  867. const_15:
  868.   .word 3                                  # Type tag for class: str
  869.   .word 9                                  # Object size
  870.   .word $str$dispatchTable                 # Pointer to dispatch table
  871.   .word 17                                 # Constant value of attribute: __len__
  872.   .string "Operation on None"              # Constant value of attribute: __str__
  873.   .align 2
  874.  
  875. .globl const_2
  876. const_2:
  877.   .word 3                                  # Type tag for class: str
  878.   .word 9                                  # Object size
  879.   .word $str$dispatchTable                 # Pointer to dispatch table
  880.   .word 16                                 # Constant value of attribute: __len__
  881.   .string "Invalid argument"               # Constant value of attribute: __str__
  882.   .align 2
  883.  
  884. .globl const_5
  885. const_5:
  886.   .word 3                                  # Type tag for class: str
  887.   .word 10                                 # Object size
  888.   .word $str$dispatchTable                 # Pointer to dispatch table
  889.   .word 21                                 # Constant value of attribute: __len__
  890.   .string "Unsupported operation"          # Constant value of attribute: __str__
  891.   .align 2
  892.  
  893. .globl const_7
  894. const_7:
  895.   .word 3                                  # Type tag for class: str
  896.   .word 8                                  # Object size
  897.   .word $str$dispatchTable                 # Pointer to dispatch table
  898.   .word 15                                 # Constant value of attribute: __len__
  899.   .string "foo.bar.baz.caz"                # Constant value of attribute: __str__
  900.   .align 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement