Skip to content
Snippets Groups Projects
Commit d9353a98 authored by FlorianKrull's avatar FlorianKrull
Browse files

Stack size and constants added

parent 8d81b229
No related branches found
No related tags found
No related merge requests found
#ifndef MCC_ASSEMBLY_GEN_H
#define MCC_ASSEMBLY_GEN_H
#include "mcc/dynamic_array.h"
struct mcc_assembly_data {
int array_index;
int parameter_counter;
int arg_counter;
int is_main;
};
enum mcc_asm_contant_type {MCC_ASM_CONST_FLOAT, MCC_ASM_CONST_STRING};
struct mcc_asm_constant {
enum mcc_asm_contant_type const_type;
int number;
struct mcc_asm_constant *next;
union {
double f_value;
char *s_value;
};
}
#endif //MCC_ASSEMBLY_GEN_H
File added
......@@ -4,10 +4,73 @@
#include "mcc/tac.h"
#include "mcc/tac_print.h"
#include "mcc/symbol_table.h"
#include "mcc/assembly_gen.h"
#define VARIABLE_SIZE 4
int compute_stack(int variable_size)
{
int stack_size = variable_size * VARIABLE_SIZE;
if (stack_size % 16 != 0) {
stack_size += (16 - stack_size % 16);
}
return stack_size;
}
void mcc_asm_add_constant(struct mcc_asm_constant **entries, enum mcc_asm_constant_type type, int *counter, void *value ){
struct mcc_asm_constant *add_const = (struct mcc_asm_constant *)malloc(sizeof(*add_const));
add_const->const_type = type;
add_const->next = NULL;
add_const->number = ++(*counter);
if(type == MCC_ASM_CONST_STRING){
add_const->s_value = (char *) value;
} else if (type == MCC_ASM_CONST_FLOAT){
add_const->f_value = *((double *)value);
}
if (*entries == NULL) {
*entries = add_const;
} else {
struct mcc_asm_constant *constant = *entries;
while (constant->next != NULL) {
constant = constant->next;
}
constant->next = add_const;
}
}
void mcc_asm_print_constant(FILE *out, struct mcc_asm_constant *constant)
{
if (constant == NULL) {
return;
}
fprintf(out, "\t.section\t.rodata\n");
if (constant == NULL) {
struct mcc_asm_constant *current = constant;
while (current != NULL) {
fprintf(out, ".LC%d:\n", current->number);
switch (current->const_type) {
case MCC_ASM_CONST_FLOAT:
fprintf(out, "\t.float\t%lf\n", current->f_value);
break;
case MCC_ASM_CONST_STRING:
fprintf(out, "\t.string\t\"%s\"\n", current->s_value);
break;
default:
break;
}
current = current->next;
}
}
char *mcc_asm_gen(FILE *out, struct mcc_tac *tac)
{
int const_count = 0;
struct mcc_asm_constant *constants = NULL;
for(int i = 0; i < tac->tac_entries->size; i++)
{
......@@ -227,10 +290,12 @@ char *mcc_asm_gen(FILE *out, struct mcc_tac *tac)
fprintf(out, "\tmovl\t$%s, %d(%%ebp)\n",arg1, result->offset);
break;
case MCC_TAC_STRING_LITERAL:
mcc_asm_add_constant(&constants,MCC_ASM_CONST_STRING,&const_count, arg1 );
fprintf(out, "\tmovl\t$.LCONST%d, %d(%%ebp)\n", const_count,result->offset);
break;
case MCC_TAC_FLOAT_LITERAL:
//TODO create new constant
mcc_asm_add_constant(&constants,MCC_ASM_CONST_FLOAT,&const_count, arg1 );
fprintf(out, "\tflds\t.LCONST%d\n", const_count);
fprintf(out, "\tfstps\t%d(%%ebp)\n", result->offset);
break;
......@@ -339,6 +404,8 @@ char *mcc_asm_gen(FILE *out, struct mcc_tac *tac)
fprintf(out, "\n");
//TODO add constants
mcc_asm_print_constant(out,constants);
//fprintf(out, ".LCONST%d:\n", i);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment