LCOV - code coverage report
Current view: top level - MinimalCC - allocate.c (source / functions) Hit Total Coverage
Test: test.info Lines: 0 66 0.0 %
Date: 2022-06-06 21:08:49 Functions: 0 9 0.0 %
Branches: 0 24 0.0 %

           Branch data     Line data    Source code
       1                 :            : #include <stdio.h>
       2                 :            : #include "allocate.h"
       3                 :            : 
       4                 :            : int stack_size = 0;
       5                 :            : int variables_size = 0;
       6                 :            : int saved_stack_size = 0;
       7                 :            : reg_list register_list;
       8                 :            : 
       9                 :          0 : void initialize_register_list(){
      10                 :          0 :         unsigned int i;
      11                 :            : 
      12                 :          0 :         register_list.num_allocated = 0;
      13                 :            : 
      14         [ #  # ]:          0 :         for(i = 0; i < NUM_REGISTERS; i++){
      15                 :          0 :                 register_list.registers[i] = i;
      16                 :          0 :                 register_list.positions[i] = i;
      17                 :            :         }
      18                 :          0 : }
      19                 :            : 
      20                 :            : //For now, it's impossible to allocate anything but 4 bytes on the stack
      21                 :            : //This is because I need to be able to cast values which are on the stack
      22                 :            : //Doing so without everything aligned on the stack would require me to
      23                 :            : //Know in advance if I need to give space around a value on the stack to
      24                 :            : //allow it to be cast and replaced in the same spot.
      25                 :          0 : data_entry allocate(unsigned char force_stack){
      26                 :          0 :         data_entry output;
      27                 :            : 
      28   [ #  #  #  # ]:          0 :         if(force_stack || register_list.num_allocated >= NUM_REGISTERS){
      29                 :          0 :                 output.type = data_stack;
      30                 :          0 :                 output.prev_stack_size = stack_size;
      31                 :          0 :                 output.stack_pos = stack_size;
      32                 :          0 :                 stack_size += 4;
      33                 :            :         } else {
      34                 :          0 :                 output.type = data_register;
      35                 :          0 :                 output.reg = register_list.registers[register_list.num_allocated];
      36                 :          0 :                 register_list.num_allocated++;
      37                 :            :         }
      38                 :            : 
      39         [ #  # ]:          0 :         if(stack_size > saved_stack_size){
      40                 :          0 :                 saved_stack_size = stack_size;
      41                 :            :         }
      42                 :            : 
      43                 :          0 :         return output;
      44                 :            : }
      45                 :            : 
      46                 :            : //Note that if we are allocating on the stack, block allocated last should be deallocated first
      47                 :          0 : void deallocate(data_entry entry){
      48                 :          0 :         unsigned int temp;
      49                 :            : 
      50         [ #  # ]:          0 :         if(entry.type == data_register){
      51                 :          0 :                 register_list.num_allocated--;
      52                 :          0 :                 temp = register_list.registers[register_list.num_allocated];
      53                 :          0 :                 register_list.registers[register_list.num_allocated] = entry.reg;
      54                 :          0 :                 register_list.registers[register_list.positions[entry.reg]] = temp;
      55                 :          0 :                 register_list.positions[temp] = register_list.positions[entry.reg];
      56                 :          0 :                 register_list.positions[entry.reg] = register_list.num_allocated;
      57         [ #  # ]:          0 :         } else if(entry.type == data_stack){
      58                 :          0 :                 stack_size = entry.prev_stack_size;
      59                 :            :         }
      60                 :          0 : }
      61                 :            : 
      62                 :          0 : int get_stack_pos(data_entry entry){
      63         [ #  # ]:          0 :         if(entry.type == data_stack){
      64                 :          0 :                 return saved_stack_size - entry.stack_pos - 4;
      65                 :            :         }
      66                 :            : 
      67                 :          0 :         return -1;
      68                 :            : }
      69                 :            : 
      70                 :          0 : static void store_register(unsigned char reg, FILE *output_file){
      71         [ #  # ]:          0 :         if(output_file)
      72                 :          0 :                 fprintf(output_file, "sw $s%d, %d($sp)\n", (int) reg, saved_stack_size - stack_size - 4);
      73                 :          0 :         stack_size += 4;
      74         [ #  # ]:          0 :         if(stack_size > saved_stack_size){
      75                 :          0 :                 saved_stack_size = stack_size;
      76                 :            :         }
      77                 :          0 : }
      78                 :            : 
      79                 :          0 : reg_list push_registers(FILE *output_file){
      80                 :          0 :         reg_list output;
      81                 :          0 :         int i;
      82                 :            : 
      83                 :          0 :         output = register_list;
      84         [ #  # ]:          0 :         for(i = register_list.num_allocated - 1; i >= 0; i--){
      85                 :          0 :                 store_register(register_list.registers[i], output_file);
      86                 :            :         }
      87                 :          0 :         register_list.num_allocated = 0;
      88                 :            : 
      89                 :          0 :         return output;
      90                 :            : }
      91                 :            : 
      92                 :          0 : int get_reg_stack_pos(reg_list regs, unsigned char reg){
      93                 :          0 :         int index;
      94                 :            : 
      95                 :          0 :         index = regs.positions[reg];
      96                 :          0 :         return saved_stack_size - stack_size + REGISTER_SIZE*(index + 1) - 4;
      97                 :            : }
      98                 :            : 
      99                 :          0 : static void load_register(unsigned char reg, FILE *output_file){
     100                 :          0 :         stack_size -= 4;
     101         [ #  # ]:          0 :         if(output_file)
     102                 :          0 :                 fprintf(output_file, "lw $s%d, %d($sp)\n", (int) reg, saved_stack_size - stack_size - 4);
     103                 :          0 : }
     104                 :            : 
     105                 :          0 : void pull_registers(reg_list regs, FILE *output_file){
     106                 :          0 :         unsigned int i;
     107                 :            : 
     108         [ #  # ]:          0 :         for(i = 0; i < regs.num_allocated; i++){
     109                 :          0 :                 load_register(regs.registers[i], output_file);
     110                 :            :         }
     111                 :            : 
     112                 :          0 :         register_list = regs;
     113                 :          0 : }

Generated by: LCOV version 1.14