Skip to content
Snippets Groups Projects
tac_string_builder.c 858 B
Newer Older
Clemens Paumgarten's avatar
Clemens Paumgarten committed
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
#include "tac_string_builder.h"

char *mcc_tac_new_temporary_string(struct mcc_tac *tac) {
    size_t num_digits = (size_t) mcc_get_number_of_digits(tac -> temporary_count);

    // +2 for character t and for \0
    char *t = malloc(num_digits + 2);

    if (t == NULL) {
        return NULL;
    }

    sprintf(t, "t%d", tac -> temporary_count);

    tac -> temporary_count++;
    tac -> last_temporary = t;

    return t;
}

char *mcc_tac_new_label_string(struct mcc_tac *tac) {
    size_t num_digits = (size_t) mcc_get_number_of_digits(tac -> label_count);

    // +2 for character t and for \0
    char *t = malloc(num_digits + 2);

    if (t == NULL) {
        return NULL;
    }

    sprintf(t, "L%d", tac -> label_count);

    tac -> label_count++;
    tac -> last_label = t;

    return t;
}