src/codegen.c
static int64_t
calculateSize(Type *type) {
  switch (type->type) {
    case TYP_S8:
    case TYP_U8:
      return 1;
    case TYP_S16:
    case TYP_U16:
      return 2;
    case TYP_S32:
    case TYP_BOOL:
    case TYP_U32:
    case TYP_CHAR:
      return 4;
    case TYP_S64:
    case TYP_U64:
    case TYP_PTR:
    case TYP_VOID:
      return 0;
    case TYP_FUN:
      return 8;
    case TYP_INTLIT:
      printf("Internal compiler error: Cannot calculate size of integer "
             "literal,\n");
      exit(1);
    case TYP_RECORD:
      {
        int64_t size = 0;
        for (size_t i = 0; i < type->record.vec->numItems; i++) {
          HashEntry *entry = *((HashEntry **)indexVector(type->record.vec, i));
          size += calculateSize((Type *)entry->data);
        }
        return size;
      }
    case TYP_BINDING:
      return calculateSize(type->typeEntry->data);
    case TYP_ARRAY:
      return type->array.size * calculateSize(type->array.type);
  }
  printf("Internal compiler error: Reached end of calculateSize without "
         "returning.\n");
  exit(1);
}
/* Calculates the size of a type in bytes */
Calculates the size of a type in bytes
Calculates the size of a type in bytes
static int64_t
calculateSize(Type *type) {
  switch (type->type) {
    case TYP_S8:
    case TYP_U8:
      return 1;
    case TYP_S16:
    case TYP_U16:
      return 2;
    case TYP_S32:
    case TYP_BOOL:
    case TYP_U32:
    case TYP_CHAR:
      return 4;
    case TYP_S64:
    case TYP_U64:
    case TYP_PTR:
    case TYP_VOID:
      return 0;
    case TYP_FUN:
      return 8;
    case TYP_INTLIT:
      printf("Internal compiler error: Cannot calculate size of integer "
             "literal,\n");
      exit(1);
    case TYP_RECORD:
      {
        int64_t size = 0;
        for (size_t i = 0; i < type->record.vec->numItems; i++) {
          HashEntry *entry = *((HashEntry **)indexVector(type->record.vec, i));
          size += calculateSize((Type *)entry->data);
        }
        return size;
      }
    case TYP_BINDING:
      return calculateSize(type->typeEntry->data);
    case TYP_ARRAY:
      return type->array.size * calculateSize(type->array.type);
  }
  printf("Internal compiler error: Reached end of calculateSize without "
         "returning.\n");
  exit(1);
}
static int64_t calculateSize ( Type * type ) { switch ( type -> type ) { case TYP_S8 : case TYP_U8 : return 1 ; case TYP_S16 : case TYP_U16 : return 2 ; case TYP_S32 : case TYP_BOOL : case TYP_U32 : case TYP_CHAR : return 4 ; case TYP_S64 : case TYP_U64 : case TYP_PTR : case TYP_VOID : return 0 ; case TYP_FUN : return 8 ; case TYP_INTLIT : printf ( " " " \n " ) ; exit ( 1 ) ; case TYP_RECORD : { int64_t size = 0 ; for ( size_t i = 0 ; i < type -> record . vec -> numItems ; i ++ ) { HashEntry * entry = * ( ( HashEntry * * ) indexVector ( type -> record . vec , i ) ) ; size += calculateSize ( ( Type * ) entry -> data ) ; } return size ; } case TYP_BINDING : return calculateSize ( type -> typeEntry -> data ) ; case TYP_ARRAY : return type -> array . size * calculateSize ( type -> array . type ) ; } printf ( " " " \n " ) ; exit ( 1 ) ; }
Calculates the size of a type in bytes
Calculates the size of a type in bytes