F diff --git a/doc/todo.txt b/doc/todo.txt
--- a/doc/todo.txt
+++ b/doc/todo.txt
Make variadic functions and macros
Implement declarations in for cycle - for(int i=0;i<10;++i)
Make wonky_calloc check for overflows
+ Add include tree structure
F diff --git a/src/backend/asm/intel/intel_asm.c b/src/backend/asm/intel/intel_asm.c
--- a/src/backend/asm/intel/intel_asm.c
+++ b/src/backend/asm/intel/intel_asm.c
{
}
+
void intel_asm_memory_location_to_rax(struct Compile_Data_Intel_Asm *compile_data,struct Intel_Asm_Memory_Location *location)
{
switch(location->type)
get_intel_asm_binary_instruction(get_intel_asm_register(INTEL_ASM_REGISTER_RAX),location,INTEL_ASM_OP_MOV)
);
break;
- /*case INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET:*/
+ break;
+ case INTEL_ASM_MEMORY_LOCATION_STACK_OFFSET:
push_intel_asm_instruction(
compile_data,
- get_intel_asm_binary_instruction(get_intel_asm_register(INTEL_ASM_REGISTER_RAX),location,INTEL_ASM_OP_MOV)
+ get_intel_asm_binary_instruction(get_intel_asm_register(INTEL_ASM_REGISTER_RAX),get_intel_asm_register(INTEL_ASM_REGISTER_RBP),INTEL_ASM_OP_MOV)
);
push_intel_asm_instruction(
compile_data,
get_intel_asm_binary_instruction(
get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
get_intel_asm_in_instruction_number(
- ((struct Intel_Asm_Memory_Location_By_Stack_Offset*)location)->offset
+ ((struct Intel_Asm_Memory_Location_Stack_Offset*)location)->offset
),
- INTEL_ASM_OP_ADD
+ INTEL_ASM_OP_SUB
)
);
break;
F diff --git a/src/backend/asm/intel/intel_compile.c b/src/backend/asm/intel/intel_compile.c
--- a/src/backend/asm/intel/intel_compile.c
+++ b/src/backend/asm/intel/intel_compile.c
}
void compile_return_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Return_Statement *ret)
{
+ struct Scope *current_scope;
+
compile_ast_to_intel_asm(compile_data,ret->return_expression);
+
+
+ for(current_scope=ret->checkpoint;current_scope!=NULL;current_scope=current_scope->parent)
+ {
+ if(current_scope->type==BLOCK_SCOPE)
+ release_stack_space_for_whole_block_intel_asm(compile_data,(struct Normal_Scope*)ret->checkpoint);
+ }
push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RBP));
compile_ast_to_intel_asm(compile_data,(struct AST*)bin->left);
compile_ast_to_intel_asm(compile_data,(struct AST*)bin->right);
+ push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RDX));
push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
push_intel_asm_instruction(compile_data,
get_intel_asm_binary_instruction(
- (struct Intel_Asm_Memory_Location*)((struct Expression_Value_LValue*)(bin->left->value))->object->location,
- get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ get_intel_asm_by_register(INTEL_ASM_REGISTER_RAX),
+ get_intel_asm_register(INTEL_ASM_REGISTER_RDX),
INTEL_ASM_OP_MOV)
);
+ push_intel_asm_instruction(compile_data,intel_asm_get_push(INTEL_ASM_REGISTER_RAX));
}else
{
push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));
F diff --git a/src/backend/asm/intel/intel_location.c b/src/backend/asm/intel/intel_location.c
--- a/src/backend/asm/intel/intel_location.c
+++ b/src/backend/asm/intel/intel_location.c
return (struct Intel_Asm_Memory_Location*)ret;
}
+ struct Intel_Asm_Memory_Location* get_intel_asm_by_register(enum Intel_Asm_Registers reg)
+ {
+ struct Intel_Asm_Memory_Location_Register *ret;
+ ret=wonky_malloc(sizeof(struct Intel_Asm_Memory_Location_Register));
+ ret->type=INTEL_ASM_MEMORY_LOCATION_BY_REGISTER;
+ ret->reg=reg;
+
+ return (struct Intel_Asm_Memory_Location*)ret;
+ }
struct Intel_Asm_Memory_Location* get_intel_asm_label_location(struct Intel_Asm_Label *label)
{
struct Intel_Asm_Memory_Location_By_Label *ret;
}
struct Intel_Asm_Memory_Location* get_intel_asm_stack_offset(int offset)
{
+ struct Intel_Asm_Memory_Location_Stack_Offset *ret;
+ ret=wonky_malloc(sizeof(struct Intel_Asm_Memory_Location_Stack_Offset));
+ ret->type=INTEL_ASM_MEMORY_LOCATION_STACK_OFFSET;
+ ret->offset=offset;
+
+ return (struct Intel_Asm_Memory_Location*)ret;
+ }
+ struct Intel_Asm_Memory_Location* get_intel_by_asm_stack_offset(int offset)
+ {
struct Intel_Asm_Memory_Location_By_Stack_Offset *ret;
ret=wonky_malloc(sizeof(struct Intel_Asm_Memory_Location_By_Stack_Offset));
ret->type=INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET;
{
fprintf(out,"%s",label->label->label_name);
}
+ void save_intel_asm_by_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out)
+ {
+ fprintf(out,"[RBP-%d]",sp->offset);
+ }
void save_intel_asm_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out)
{
fprintf(out,"[RBP-%d]",sp->offset);
[INTEL_ASM_MEMORY_LOCATION_BY_REGISTER]=(map_entry)save_intel_asm_by_register_location,
[INTEL_ASM_MEMORY_LOCATION_REGISTER]=(map_entry)save_intel_asm_register_location,
[INTEL_ASM_MEMORY_LOCATION_BY_LABEL]=(map_entry)save_intel_asm_label_location,
- [INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET]=(map_entry)save_intel_asm_stack_offset_location,
+ [INTEL_ASM_MEMORY_LOCATION_STACK_OFFSET]=(map_entry)save_intel_asm_stack_offset_location,
+ [INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET]=(map_entry)save_intel_asm_by_stack_offset_location,
[INTEL_ASM_MEMORY_LOCATION_IN_INSTRUCTION_NUMBER]=(map_entry)save_intel_asm_number_in_instruction,
};
wonky_assert(map[location->type]!=NULL);
F diff --git a/src/backend/asm/intel/intel_location.h b/src/backend/asm/intel/intel_location.h
--- a/src/backend/asm/intel/intel_location.h
+++ b/src/backend/asm/intel/intel_location.h
enum Intel_Asm_Memory_Location_Type type;
int offset;
};
+ struct Intel_Asm_Memory_Location_Stack_Offset
+ {
+ enum Intel_Asm_Memory_Location_Type type;
+ int offset;
+ };
struct Intel_Asm_Memory_Location_In_Instruction_Number
{
enum Intel_Asm_Memory_Location_Type type;
void save_intel_asm_location(struct Intel_Asm_Memory_Location *location,FILE *out);
void save_intel_asm_label_location(struct Intel_Asm_Memory_Location_By_Label *label,FILE *out);
void save_intel_asm_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out);
+ void save_intel_asm_by_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out);
void save_intel_asm_by_register_location(struct Intel_Asm_Memory_Location_By_Register *reg,FILE *out);
void save_intel_asm_register_location(struct Intel_Asm_Memory_Location_Register *reg,FILE *out);
void save_intel_asm_number_in_instruction(struct Intel_Asm_Memory_Location_In_Instruction_Number *num,FILE *out);
struct Intel_Asm_Memory_Location* get_intel_asm_by_register(enum Intel_Asm_Registers reg);
struct Intel_Asm_Memory_Location* get_intel_asm_label_location(struct Intel_Asm_Label *label);
struct Intel_Asm_Memory_Location* get_intel_asm_stack_offset(int offset);
+ struct Intel_Asm_Memory_Location* get_intel_asm_by_stack_offset(int offset);
struct Intel_Asm_Memory_Location* get_intel_asm_in_instruction_number(int number);
struct Intel_Asm_Memory_Location* intel_asm_get_ax_register();
struct Intel_Asm_Memory_Location* intel_asm_get_dx_register();
F diff --git a/src/backend/asm/intel/intel_location.hh b/src/backend/asm/intel/intel_location.hh
--- a/src/backend/asm/intel/intel_location.hh
+++ b/src/backend/asm/intel/intel_location.hh
{
INTEL_ASM_MEMORY_LOCATION_BY_REGISTER,
INTEL_ASM_MEMORY_LOCATION_REGISTER,
+ INTEL_ASM_MEMORY_LOCATION_STACK_OFFSET,
INTEL_ASM_MEMORY_LOCATION_BY_LABEL,
INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET,
INTEL_ASM_MEMORY_LOCATION_IN_INSTRUCTION_NUMBER,
struct Intel_Asm_Memory_Location_Register;
struct Intel_Asm_Memory_Location_By_Label;
struct Intel_Asm_Memory_Location_By_Stack_Offset;
+ struct Intel_Asm_Memory_Location_Stack_Offset;
struct Intel_Asm_Memory_Location_In_Instruction_Number;
struct Intel_Asm_Memory_Location;
F diff --git a/src/frontend/parse/parse_statement.c b/src/frontend/parse/parse_statement.c
--- a/src/frontend/parse/parse_statement.c
+++ b/src/frontend/parse/parse_statement.c
hold=get_return_statement_tree(parse_expression(translation_data,scope),translation_data,scope);
if(get_and_check(translation_data,KW_SEMI_COLUMN))
{
- wonky_assert(is_valid_return_statement(hold));
return (struct AST*)hold;
}else
{
F diff --git a/src/misc/queue.c b/src/misc/queue.c
--- a/src/misc/queue.c
+++ b/src/misc/queue.c
q->first=q->last=wonky_malloc(sizeof(struct Queue_Node));
q->first->data=data;
- q->last->prev=NULL;
+ q->last->next=q->last->prev=NULL;
++q->size;
return;
struct Queue_Node *temp=wonky_malloc(sizeof(struct Queue_Node));
q->last->prev=temp;
temp->data=data;
+
temp->prev=NULL;
+ temp->next=q->last;
q->last=temp;
++q->size;
q->first=q->last=wonky_malloc(sizeof(struct Queue_Node));
q->first->data=data;
- q->last->prev=NULL;
+ q->last->next=q->last->prev=NULL;
++q->size;
}else
{
struct Queue_Node *temp=wonky_malloc(sizeof(struct Queue_Node));
+
temp->prev=q->first;
+ temp->next=NULL;
+
temp->data=data;
q->first=temp;
F diff --git a/src/misc/queue.h b/src/misc/queue.h
--- a/src/misc/queue.h
+++ b/src/misc/queue.h
#include <common.h>
#include <wonky_malloc.h>
+ struct Queue_Node
+ {
+ void *data;
+ struct Queue_Node *prev; /*+1*/
+ struct Queue_Node *next; /*-1*/
+ };
struct Queue
{
- struct Queue_Node
- {
- void *data;
- struct Queue_Node *prev;
- }*first,*last;
+ struct Queue_Node *first;
+ struct Queue_Node *last;
size_t size;
-
};
void Queue_Init(Queue *q);
void Queue_Push(Queue *q,void *data);
F diff --git a/src/misc/queue.hh b/src/misc/queue.hh
--- a/src/misc/queue.hh
+++ b/src/misc/queue.hh
#define GQUEUE_HH GQUEUE_HH
struct Queue;
+ struct Queue_Node;
typedef struct Queue Queue;
+ typedef struct Queue_Node Queue_Node;
#endif
F diff --git a/src/semantics/ast.c b/src/semantics/ast.c
--- a/src/semantics/ast.c
+++ b/src/semantics/ast.c
struct AST_Compound_Statement* get_compound_statement_tree(struct Scope *parent_scope)
{
struct AST_Compound_Statement *ret;
+
+
ret=wonky_malloc(sizeof(struct AST_Compound_Statement));
ret->components=wonky_malloc(sizeof(struct Queue));
Queue_Init(ret->components);
- ret->scope=get_normal_scope(parent_scope,BLOCK_SCOPE);
+ ret->scope=get_normal_scope(get_partial_normal_scope((struct Normal_Scope*)parent_scope),BLOCK_SCOPE);
return ret;
}
struct AST_If_Statement* get_if_statement_tree()
struct Denoted_Function *hold_function;
struct Type *return_type;
+ wonky_assert(is_valid_normal_scope((struct Normal_Scope*)scope));
+
hold_function=get_enclosing_function(scope);
return_type=((struct Type_Function*)hold_function->type)->return_type;
{
ret=wonky_malloc(sizeof(struct AST_If_Statement));
ret->type=ST_RETURN;
+ ret->checkpoint=get_partial_normal_scope((struct Normal_Scope*)scope);
+
if(return_expression==NULL)
ret->return_expression=NULL;
else
ret->return_expression=(struct AST*)get_cast_expression_tree((struct AST_Expression*)return_expression,return_type,translation_data);
+ wonky_assert(is_valid_return_statement(ret));
return ret;
}else
{
F diff --git a/src/semantics/ast.h b/src/semantics/ast.h
--- a/src/semantics/ast.h
+++ b/src/semantics/ast.h
struct AST_Return_Statement
{
enum AST_Type type;
- struct AST* return_expression;
+ struct Scope *checkpoint;
+ struct AST *return_expression;
};
F diff --git a/src/semantics/identifiers/scope.c b/src/semantics/identifiers/scope.c
--- a/src/semantics/identifiers/scope.c
+++ b/src/semantics/identifiers/scope.c
return (struct Scope*)ret;
}
+ struct Scope* get_partial_normal_scope(struct Normal_Scope *scope)
+ {
+ struct Normal_Scope *ret;
+ ret=wonky_malloc(sizeof(struct Normal_Scope));
+ ret->object_order=wonky_malloc(sizeof(struct Queue));
+ *ret->object_order=*scope->object_order;
+
+ ret->type=scope->type;
+ ret->parent=scope->parent;
+ ret->tags=scope->tags;
+ ret->ordinary=scope->ordinary;
+
+ return (struct Scope*)ret;
+ }
struct Scope* get_function_scope(struct Scope *parent,struct Denoted_Function *function)
{
struct Function_Scope *ret;
wonky_assert(is_valid_function_scope(ret));
return (struct Scope*)ret;
}
-
void delete_normal_scope(struct Normal_Scope *scope)
{
Map_Map(scope->tags,delete_denoted_wrapper);
F diff --git a/src/semantics/identifiers/scope.h b/src/semantics/identifiers/scope.h
--- a/src/semantics/identifiers/scope.h
+++ b/src/semantics/identifiers/scope.h
enum Scope_Type;
+
struct Scope
{
enum Scope_Type type;
struct Queue *label_order; /*queue of denoted statements*/
};
-
struct Scope* get_normal_scope(struct Scope *parent,enum Scope_Type type);
+ struct Scope* get_partial_normal_scope(struct Normal_Scope *scope);
struct Scope* get_function_scope(struct Scope *parent,struct Denoted_Function *function);
struct Denoted_Statement* check_label(struct Scope *current,struct token *id);
F diff --git a/src/semantics/value/value.h b/src/semantics/value/value.h
--- a/src/semantics/value/value.h
+++ b/src/semantics/value/value.h
struct Object *object;
char is_modifiable;
};
-
struct Expression_Value_Constant
{
enum Expression_Value_Type type;
enum Expression_Value_Type type;
struct Object *temp_object;
};
-
struct Expression_Value_Function_Designator
{
enum Expression_Value_Type type;